Merged in add-findpid (pull request #145)

stream/mts: added general FindPID func and FindPAT func.
This commit is contained in:
Saxon Milton 2019-02-28 05:46:39 +00:00
commit 6228123f7d
1 changed files with 19 additions and 6 deletions

View File

@ -30,6 +30,7 @@ package mts
import ( import (
"errors" "errors"
"fmt"
) )
// General mpegts packet properties. // General mpegts packet properties.
@ -157,20 +158,32 @@ type Packet struct {
Payload []byte // Mpeg ts Payload Payload []byte // Mpeg ts Payload
} }
// FindPMT will take a clip of mpegts and try to find a PMT table - if one // FindPmt will take a clip of mpegts and try to find a PMT table - if one
// is found, then it is returned along with its index, otherwise nil, -1 and an error is returned. // is found, then it is returned along with its index, otherwise nil, -1 and an error is returned.
func FindPMT(d []byte) (p []byte, i int, err error) { func FindPmt(d []byte) ([]byte, int, error) {
return FindPid(d, PmtPid)
}
// FindPat will take a clip of mpegts and try to find a PAT table - if one
// is found, then it is returned along with its index, otherwise nil, -1 and an error is returned.
func FindPat(d []byte) ([]byte, int, error) {
return FindPid(d, PatPid)
}
// FindPid will take a clip of mpegts and try to find a packet with given PID - if one
// is found, then it is returned along with its index, otherwise nil, -1 and an error is returned.
func FindPid(d []byte, pid uint16) (pkt []byte, i int, err error) {
if len(d) < PacketSize { if len(d) < PacketSize {
return nil, -1, errors.New("Mmpegts data not of valid length") return nil, -1, errors.New("Mmpegts data not of valid length")
} }
for i = 0; i < len(d); i += PacketSize { for i = 0; i < len(d); i += PacketSize {
pid := (uint16(d[i+1]&0x1f) << 8) | uint16(d[i+2]) p := (uint16(d[i+1]&0x1f) << 8) | uint16(d[i+2])
if pid == pmtPid { if p == pid {
p = d[i+4 : i+PacketSize] pkt = d[i+4 : i+PacketSize]
return return
} }
} }
return nil, -1, errors.New("Could not find pmt table in mpegts data") return nil, -1, fmt.Errorf("could not find packet with pid: %d", pid)
} }
// FillPayload takes a channel and fills the packets Payload field until the // FillPayload takes a channel and fills the packets Payload field until the