stream/mts: added general FindPID func and FindPAT func.

This commit is contained in:
saxon 2019-02-13 14:40:58 +10:30
parent bfdefa97f8
commit b96df6d3a7
1 changed files with 17 additions and 5 deletions

View File

@ -159,18 +159,30 @@ type Packet struct {
// 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, errors.New("Could not find packet with given pid in mpegts data")
} }
// FillPayload takes a channel and fills the packets Payload field until the // FillPayload takes a channel and fills the packets Payload field until the