av/packet/MpegTs.go

92 lines
2.5 KiB
Go

/*
NAME
MpegTs.go - provides a data structure intended to encapsulate the properties
of an MpegTs packet.
DESCRIPTION
See Readme.md
AUTHOR
Saxon Nelson-Milton <saxon.milton@gmail.com>
LICENSE
MpegTs.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
*/
package packet
// 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
)
type MpegTsPacket struct {
SyncByte byte
TEI bool // Transport Error Indicator
PUSI bool // Payload Unit Start Indicator
Priority bool
PID uint16
TSC byte // Transport Scrambling Control
AFC byte // Adaption Field Control
CC byte // Continuity Counter
AF []byte // Adaption Field
Payload []byte
}
func boolToByte( in bool ) (out uint8){
if in { out = 1 }
return
}
func (p *MpegTsPacket) ToByteSlice() (output [188]byte) {
output[0] = p.SyncByte
output[1] = ( boolToByte(p.TEI) << (8-teiIndex%8) ) |
( boolToByte(p.PUSI) << (8-pusiIndex%8) ) |
( boolToByte(p.Priority) << (8-priorityIndex%8) ) |
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]
}
headerSize := packetLength-len(p.Payload)
for ii := headerSize; ii < packetLength; ii++ {
output[ii] = p.Payload[ii-headerSize]
}
return
}