fix SeekToTime() bug

This commit is contained in:
nareix 2016-04-19 19:08:32 +08:00
parent 2ffa926532
commit 0162e613e6

View File

@ -118,7 +118,7 @@ func (self *Stream) setSampleIndex(index int) (err error) {
} }
} }
if !found { if !found {
err = io.EOF err = fmt.Errorf("stream[%d]: cannot locate sample index in chunk", self.idx)
return return
} }
@ -126,7 +126,7 @@ func (self *Stream) setSampleIndex(index int) (err error) {
self.sampleOffsetInChunk = int64(self.sampleIndexInChunk * self.sample.SampleSize.SampleSize) self.sampleOffsetInChunk = int64(self.sampleIndexInChunk * self.sample.SampleSize.SampleSize)
} else { } else {
if index >= len(self.sample.SampleSize.Entries) { if index >= len(self.sample.SampleSize.Entries) {
err = io.EOF err = fmt.Errorf("stream[%d]: sample index out of range", self.idx)
return return
} }
self.sampleOffsetInChunk = int64(0) self.sampleOffsetInChunk = int64(0)
@ -145,6 +145,7 @@ func (self *Stream) setSampleIndex(index int) (err error) {
if index >= start && index < start+n { if index >= start && index < start+n {
self.sampleIndexInSttsEntry = index - start self.sampleIndexInSttsEntry = index - start
self.dts += int64((index - start) * entry.Duration) self.dts += int64((index - start) * entry.Duration)
found = true
break break
} }
start += n start += n
@ -152,7 +153,7 @@ func (self *Stream) setSampleIndex(index int) (err error) {
self.sttsEntryIndex++ self.sttsEntryIndex++
} }
if !found { if !found {
err = io.EOF err = fmt.Errorf("stream[%d]: cannot locate sample index in stts entry", self.idx)
return return
} }
@ -164,13 +165,14 @@ func (self *Stream) setSampleIndex(index int) (err error) {
n := self.sample.CompositionOffset.Entries[self.cttsEntryIndex].Count n := self.sample.CompositionOffset.Entries[self.cttsEntryIndex].Count
if index >= start && index < start+n { if index >= start && index < start+n {
self.sampleIndexInCttsEntry = index - start self.sampleIndexInCttsEntry = index - start
found = true
break break
} }
start += n start += n
self.cttsEntryIndex++ self.cttsEntryIndex++
} }
if !found { if !found {
err = io.EOF err = fmt.Errorf("stream[%d]: cannot locate sample index in ctts entry", self.idx)
return return
} }
} }
@ -283,10 +285,13 @@ func (self *Stream) sampleCount() int {
func (self *Demuxer) ReadPacket() (pkt av.Packet, err error) { func (self *Demuxer) ReadPacket() (pkt av.Packet, err error) {
var choose *Stream var choose *Stream
for _, stream := range(self.streams) { for _, stream := range(self.streams) {
if choose == nil || stream.dts < choose.dts { if choose == nil || stream.TsToTime(stream.dts) < choose.TsToTime(choose.dts) {
choose = stream choose = stream
} }
} }
if false {
fmt.Printf("ReadPacket: choose index=%v time=%v\n", choose.idx, choose.TsToTime(choose.dts))
}
pkt.StreamIdx = choose.idx pkt.StreamIdx = choose.idx
pkt.Pts, pkt.Dts, pkt.IsKeyFrame, pkt.Data, err = choose.readSample() pkt.Pts, pkt.Dts, pkt.IsKeyFrame, pkt.Data, err = choose.readSample()
return return
@ -351,9 +356,15 @@ func (self *Stream) duration() float64 {
return float64(total) / float64(self.TimeScale()) return float64(total) / float64(self.TimeScale())
} }
func (self *Stream) seekToTime(time float64) error { func (self *Stream) seekToTime(time float64) (err error) {
index := self.timeToSampleIndex(time) index := self.timeToSampleIndex(time)
return self.setSampleIndex(index) if err = self.setSampleIndex(index); err != nil {
return
}
if false {
fmt.Printf("stream[%d]: seekToTime index=%v time=%v cur=%v\n", self.idx, index, time, self.TsToTime(self.dts))
}
return
} }
func (self *Stream) timeToSampleIndex(time float64) int { func (self *Stream) timeToSampleIndex(time float64) int {