Skip to content

Commit

Permalink
Fix panic in ReadPanic when no streams
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel committed May 15, 2018
1 parent f98bf9b commit b85074b
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions format/mp4/demuxer.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package mp4

import (
"time"
"errors"
"fmt"
"io"
"time"

"github.com/nareix/joy4/av"
"github.com/nareix/joy4/codec/aacparser"
"github.com/nareix/joy4/codec/h264parser"
"github.com/nareix/joy4/format/mp4/mp4io"
"io"
)

type Demuxer struct {
r io.ReadSeeker
r io.ReadSeeker
streams []*Stream
movieAtom *mp4io.Movie
}
Expand Down Expand Up @@ -124,7 +126,7 @@ func (self *Stream) setSampleIndex(index int) (err error) {
}

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 {
if index >= len(self.sample.SampleSize.Entries) {
err = fmt.Errorf("mp4: stream[%d]: sample index out of range", self.idx)
Expand All @@ -145,12 +147,12 @@ func (self *Stream) setSampleIndex(index int) (err error) {
n := int(entry.Count)
if index >= start && index < start+n {
self.sampleIndexInSttsEntry = index - start
self.dts += int64(index-start)*int64(entry.Duration)
self.dts += int64(index-start) * int64(entry.Duration)
found = true
break
}
start += n
self.dts += int64(n)*int64(entry.Duration)
self.dts += int64(n) * int64(entry.Duration)
self.sttsEntryIndex++
}
if !found {
Expand Down Expand Up @@ -299,6 +301,10 @@ func (self *Demuxer) ReadPacket() (pkt av.Packet, err error) {
if err = self.probe(); err != nil {
return
}
if len(self.streams) == 0 {
err = errors.New("mp4: no streams available while trying to read a packet")
return
}

var chosen *Stream
var chosenidx int
Expand Down Expand Up @@ -430,7 +436,7 @@ func (self *Stream) timeToSampleIndex(tm time.Duration) int {
entries := self.sample.SyncSample.Entries
for i := len(entries) - 1; i >= 0; i-- {
if entries[i]-1 < uint32(targetIndex) {
targetIndex = int(entries[i]-1)
targetIndex = int(entries[i] - 1)
break
}
}
Expand Down

0 comments on commit b85074b

Please sign in to comment.