Merge pull request #61 from sprucehealth/master

Fix panic in ReadPanic when no streams
This commit is contained in:
Eric Tang 2018-08-14 18:15:33 -04:00 committed by GitHub
commit 7d93fa5e2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,17 +1,19 @@
package mp4 package mp4
import ( import (
"time" "errors"
"fmt" "fmt"
"io"
"time"
"github.com/nareix/joy4/av" "github.com/nareix/joy4/av"
"github.com/nareix/joy4/codec/aacparser" "github.com/nareix/joy4/codec/aacparser"
"github.com/nareix/joy4/codec/h264parser" "github.com/nareix/joy4/codec/h264parser"
"github.com/nareix/joy4/format/mp4/mp4io" "github.com/nareix/joy4/format/mp4/mp4io"
"io"
) )
type Demuxer struct { type Demuxer struct {
r io.ReadSeeker r io.ReadSeeker
streams []*Stream streams []*Stream
movieAtom *mp4io.Movie movieAtom *mp4io.Movie
} }
@ -124,7 +126,7 @@ func (self *Stream) setSampleIndex(index int) (err error) {
} }
if self.sample.SampleSize.SampleSize != 0 { if self.sample.SampleSize.SampleSize != 0 {
self.sampleOffsetInChunk = int64(self.sampleIndexInChunk)*int64(self.sample.SampleSize.SampleSize) self.sampleOffsetInChunk = int64(self.sampleIndexInChunk) * int64(self.sample.SampleSize.SampleSize)
} else { } else {
if index >= len(self.sample.SampleSize.Entries) { if index >= len(self.sample.SampleSize.Entries) {
err = fmt.Errorf("mp4: stream[%d]: sample index out of range", self.idx) err = fmt.Errorf("mp4: stream[%d]: sample index out of range", self.idx)
@ -145,12 +147,12 @@ func (self *Stream) setSampleIndex(index int) (err error) {
n := int(entry.Count) n := int(entry.Count)
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)*int64(entry.Duration) self.dts += int64(index-start) * int64(entry.Duration)
found = true found = true
break break
} }
start += n start += n
self.dts += int64(n)*int64(entry.Duration) self.dts += int64(n) * int64(entry.Duration)
self.sttsEntryIndex++ self.sttsEntryIndex++
} }
if !found { if !found {
@ -299,6 +301,10 @@ func (self *Demuxer) ReadPacket() (pkt av.Packet, err error) {
if err = self.probe(); err != nil { if err = self.probe(); err != nil {
return return
} }
if len(self.streams) == 0 {
err = errors.New("mp4: no streams available while trying to read a packet")
return
}
var chosen *Stream var chosen *Stream
var chosenidx int var chosenidx int
@ -430,7 +436,7 @@ func (self *Stream) timeToSampleIndex(tm time.Duration) int {
entries := self.sample.SyncSample.Entries entries := self.sample.SyncSample.Entries
for i := len(entries) - 1; i >= 0; i-- { for i := len(entries) - 1; i >= 0; i-- {
if entries[i]-1 < uint32(targetIndex) { if entries[i]-1 < uint32(targetIndex) {
targetIndex = int(entries[i]-1) targetIndex = int(entries[i] - 1)
break break
} }
} }