diff --git a/container/mts/mpegts.go b/container/mts/mpegts.go index 2a5f9483..28ad802b 100644 --- a/container/mts/mpegts.go +++ b/container/mts/mpegts.go @@ -178,8 +178,9 @@ func FindPat(d []byte) ([]byte, int, error) { // Errors used by FindPid. var ( - errInvalidLen = errors.New("MPEG-TS data not of valid length") - errCouldNotFind = errors.New("could not find packet with given PID") + errInvalidLen = errors.New("MPEG-TS data not of valid length") + errCouldNotFind = errors.New("could not find packet with given PID") + errNotConsecutive = errors.New("could not find consecutive PIDs") ) // FindPid will take a clip of MPEG-TS and try to find a packet with given PID - if one @@ -223,10 +224,12 @@ func LastPid(d []byte, pid uint16) (pkt []byte, i int, err error) { func IndexPid(d []byte, pids ...uint16) (idx int, m map[string]string, err error) { idx = -1 for _, pid := range pids { - pkt, i, _err := FindPid(d, pid) + if len(d) < PacketSize { + return idx, m, errInvalidLen + } + pkt, i, err := FindPid(d, pid) if err != nil { - err = errors.Wrap(_err, "could not find PID") - return + return idx, m, errCouldNotFind } if pid == PmtPid { m, _ = metaFromPMT(pkt) @@ -234,8 +237,7 @@ func IndexPid(d []byte, pids ...uint16) (idx int, m map[string]string, err error if idx == -1 { idx = i } else if i != 0 { - err = errors.New("PIDs not consecutive") - return + return idx, m, errNotConsecutive } d = d[i+PacketSize:] }