Improving mpegts file to allow for setting of pcr

This commit is contained in:
Unknown 2018-01-06 15:30:45 +10:30
parent df5ff04fd1
commit fae1fdd4ee
1 changed files with 95 additions and 66 deletions

View File

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