diff --git a/packets/MpegTs.go b/packets/MpegTs.go index 3b558217..f9f58915 100644 --- a/packets/MpegTs.go +++ b/packets/MpegTs.go @@ -1,7 +1,7 @@ /* NAME MpegTs.go - provides a data structure intended to encapsulate the properties - of an MpegTs packet. + of an MpegTs packet and also functions to allow manipulation of these packets. DESCRIPTION See Readme.md @@ -28,77 +28,106 @@ LICENSE package packets -import _ "fmt" - -// Length of some fields in bits -const ( - syncByteLength = 8 - teiLength = 1 - pusiLength = 1 - priorityLength = 1 - pidLength = 13 - tscLength = 2 - afcLength = 2 - ccLength = 4 - packetLength = 188 -) - -// Index of the fields -const ( - syncByteIndex = 0 - teiIndex = syncByteIndex + syncByteLength - pusiIndex = teiIndex + teiLength - priorityIndex = pusiIndex + pusiLength - pidIndex = priorityIndex + priorityLength - tscIndex = pidIndex + pidLength - afcIndex = tscIndex + tscLength - ccIndex = afcIndex + afcLength - afIndex = ccIndex + ccLength -) +/* +The below data struct encapsulates the fields of an MPEG-TS packet. Below is +the formatting of an MPEG-TS packet for reference! + MPEG-TS Packet Formatting +============================================================================ +| octet no | bit 0 | bit 1 | bit 2 | bit 3 | bit 4 | bit 5 | bit 6 | bit 7 | +============================================================================ +| octet 0 | sync byte (0x47) | +---------------------------------------------------------------------------- +| octet 1 | TEI | PUSI | Prior | PID | +---------------------------------------------------------------------------- +| octet 2 | PID cont. | +---------------------------------------------------------------------------- +| octet 3 | TSC | AFC | CC | +---------------------------------------------------------------------------- +| octet 4 | AFL | +---------------------------------------------------------------------------- +| octet 5 | DI | RAI | ESPI | PCRF | OPCRF | SPF | TPDF | AFEF | +---------------------------------------------------------------------------- +| optional | PCR (48 bits => 6 bytes) | +---------------------------------------------------------------------------- +| - | PCR cont. | +---------------------------------------------------------------------------- +| - | PCR cont. | +---------------------------------------------------------------------------- +| - | PCR cont. | +---------------------------------------------------------------------------- +| - | PCR cont. | +---------------------------------------------------------------------------- +| - | PCR cont. | +---------------------------------------------------------------------------- +| optional | OPCR (48 bits => 6 bytes) | +---------------------------------------------------------------------------- +| - | OPCR cont. | +---------------------------------------------------------------------------- +| - | OPCR cont. | +---------------------------------------------------------------------------- +| - | OPCR cont. | +---------------------------------------------------------------------------- +| - | OPCR cont. | +---------------------------------------------------------------------------- +| - | OPCR cont. | +---------------------------------------------------------------------------- +| optional | SC | +---------------------------------------------------------------------------- +| optional | TPDL | +---------------------------------------------------------------------------- +| optional | TPD (variable length) | +---------------------------------------------------------------------------- +| - | ... | +---------------------------------------------------------------------------- +| optional | Extension (variable length) | +---------------------------------------------------------------------------- +| - | ... | +---------------------------------------------------------------------------- +| optional | Stuffing (variable length) | +---------------------------------------------------------------------------- +| - | ... | +---------------------------------------------------------------------------- +| optional | Payload (variable length) | +---------------------------------------------------------------------------- +| - | ... | +---------------------------------------------------------------------------- +*/ type MpegTsPacket struct { - // syncByte byte // (octet:0 bit:0 - octet:0 bit:7) - TEI bool // (octet:1 bit:0) Transport Error Indicator - PUSI bool // (octet:1 bit:1) Payload Unit Start Indicator - Priority bool // (octet:1 bit:2) Tranposrt priority indicator - PID uint16 // (octet:1 bit:3 - octect:3 bit:7) Packet identifier - TSC byte // (octet:4 bit:0 - octect:4 bit:1) Transport Scrambling Control - AFC byte // (octet:4 bit:2 - octect:4 bit:3) Adaption Field Control - CC byte // (octet:4 bit:4 - octect:4 bit:7) Continuity Counter - AFL byte // (octet:5 bit:0 - octect:5 bit:7) Adaptation field length - DI bool // (octet:6 bit:0) Discontinouty indicator - RAI bool // (octet:6 bit:1) random access indicator - ESPI bool // (octet:6 bit:2) Elementary stream priority indicator - PCRF bool // (octet:6 bit:3) pcr flag - OPCRF bool // (octet:6 bit:4) opcr flag - SPF bool // (octet:6 bit:5) splicing point flag - TPDF bool // (octet:6 bit:6) transport private data flag - AFEF bool // (octet:6 bit:7) adaptation field extension flag - PCR uint64 // (optional 48 bits) program clock reference - OPCR uint64 // (optional 48 bits) Original program clock reference - SC byte // (optional 8 bits) splice countdown - TPDL byte // (optional 8 bits) tranposrt private data length - TPD []byte // (optional variable length) private data - Extension []byte // (optional variable length) adaptation field extension - Stuffing []byte // (optional variable length) stuffing bytes - Payload []byte // (optional variable length) mpeg ts payload + TEI bool // Transport Error Indicator + PUSI bool // Payload Unit Start Indicator + Priority bool // Tranposrt priority indicator + PID uint16 // Packet identifier + TSC byte // Transport Scrambling Control + AFC byte // Adaption Field Control + CC byte // Continuity Counter + AFL byte // Adaptation field length + DI bool // Discontinouty indicator + RAI bool // random access indicator + ESPI bool // Elementary stream priority indicator + PCRF bool // PCR flag + OPCRF bool // OPCR flag + SPF bool // Splicing point flag + TPDF bool // Transport private data flag + AFEF bool // Adaptation field extension flag + PCR uint64 // Program clock reference + OPCR uint64 // Original program clock reference + SC byte // Splice countdown + TPDL byte // Tranposrt private data length + TPD []byte // Private data + Extension []byte // Adaptation field extension + Stuffing []byte // Stuffing bytes + Payload []byte // Mpeg ts payload } func (p *MpegTsPacket) ToByteSlice() (output []byte) { output = make([]byte, 188) - output[0] = 0x47 // sync byte always the same - output[1] = (boolToByte(p.TEI) << (7 - teiIndex%8)) | - (boolToByte(p.PUSI) << (7 - pusiIndex%8)) | - (boolToByte(p.Priority) << (7 - priorityIndex%8)) | - byte((p.PID&0xFF00)>>8) + output[0] = 0x47 + output[1] = boolToByte(p.TEI) << 7 | boolToByte(p.PUSI) << 6 | + boolToByte(p.Priority) << 5 | byte((p.PID&0xFF00) >> 8) output[2] = byte(p.PID & 0x00FF) - output[3] = (p.TSC << 6) | (p.AFC << 4) | p.CC - for ii := 4; ii-4 < len(p.AF); ii++ { - output[ii] = p.AF[ii-4] - } - payloadIndex := 4 + len(p.AF) - for ii := payloadIndex; ii < packetLength; ii++ { - output[ii] = p.Payload[ii-payloadIndex] - } + output[3] = p.TSC << 6 | (p.AFC << 4) | p.CC + output[4] = p.AFL + output[5] = return }