2017-12-11 08:20:24 +03:00
|
|
|
/*
|
|
|
|
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.
|
2017-12-11 07:54:49 +03:00
|
|
|
|
2017-12-11 08:20:24 +03:00
|
|
|
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).
|
|
|
|
*/
|
|
|
|
|
2017-12-13 09:52:18 +03:00
|
|
|
package packets
|
2017-12-11 07:54:49 +03:00
|
|
|
|
2017-12-26 11:44:21 +03:00
|
|
|
import _"fmt"
|
2017-12-19 11:30:47 +03:00
|
|
|
|
2017-12-11 09:38:09 +03:00
|
|
|
// Length of some fields in bits
|
|
|
|
const (
|
2017-12-12 09:40:32 +03:00
|
|
|
syncByteLength = 8
|
|
|
|
teiLength = 1
|
|
|
|
pusiLength = 1
|
|
|
|
priorityLength = 1
|
|
|
|
pidLength = 13
|
|
|
|
tscLength = 2
|
|
|
|
afcLength = 2
|
|
|
|
ccLength = 4
|
|
|
|
packetLength = 188
|
2017-12-11 09:38:09 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Index of the fields
|
|
|
|
const (
|
2017-12-12 09:40:32 +03:00
|
|
|
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
|
2017-12-11 09:38:09 +03:00
|
|
|
)
|
|
|
|
|
2017-12-11 07:54:49 +03:00
|
|
|
type MpegTsPacket struct {
|
2017-12-11 09:38:09 +03:00
|
|
|
SyncByte byte
|
|
|
|
TEI bool // Transport Error Indicator
|
|
|
|
PUSI bool // Payload Unit Start Indicator
|
|
|
|
Priority bool
|
2017-12-12 09:40:32 +03:00
|
|
|
PID uint16
|
2017-12-11 09:38:09 +03:00
|
|
|
TSC byte // Transport Scrambling Control
|
|
|
|
AFC byte // Adaption Field Control
|
2017-12-12 09:40:32 +03:00
|
|
|
CC byte // Continuity Counter
|
2017-12-11 09:38:09 +03:00
|
|
|
AF []byte // Adaption Field
|
|
|
|
Payload []byte
|
2017-12-11 08:20:24 +03:00
|
|
|
}
|
|
|
|
|
2017-12-13 09:52:18 +03:00
|
|
|
func (p *MpegTsPacket) ToByteSlice() (output []byte) {
|
|
|
|
output = make([]byte,188)
|
2017-12-11 09:38:09 +03:00
|
|
|
output[0] = p.SyncByte
|
2017-12-13 09:52:18 +03:00
|
|
|
output[1] = ( boolToByte(p.TEI) << (7-teiIndex%8) ) |
|
|
|
|
( boolToByte(p.PUSI) << (7-pusiIndex%8) ) |
|
|
|
|
( boolToByte(p.Priority) << (7-priorityIndex%8) ) |
|
2017-12-12 09:40:32 +03:00
|
|
|
byte(( p.PID & 0xFF00 ) >> 8)
|
|
|
|
output[2] = byte(p.PID & 0x00FF)
|
|
|
|
output[3] = ( p.TSC << 6 ) | ( p.AFC << 4 ) | p.CC
|
2017-12-13 03:03:37 +03:00
|
|
|
for ii := 4; ii-4 < len(p.AF); ii++ {
|
2017-12-12 09:40:32 +03:00
|
|
|
output[ii] = p.AF[ii-4]
|
|
|
|
}
|
2017-12-22 11:12:03 +03:00
|
|
|
payloadIndex := 4 + len(p.AF)
|
|
|
|
for ii := payloadIndex; ii < packetLength; ii++ {
|
|
|
|
output[ii] = p.Payload[ii-payloadIndex]
|
2017-12-12 09:40:32 +03:00
|
|
|
}
|
|
|
|
return
|
2017-12-11 07:54:49 +03:00
|
|
|
}
|