av/packets/helpers.go

79 lines
2.0 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 packets
import (
_"os"
_"fmt"
"reflect"
)
func boolToByte(in bool) (out uint8) {
if in {
out = 1
}
return
}
func GetOctectType(p *RtpPacket) byte {
return p.Payload[0] & 0x1F
}
func GetStartBit(p *RtpPacket) byte {
return (p.Payload[1] & 0x80) >> 7
}
func getEndBit(p *RtpPacket) byte {
return (p.Payload[1] & 0x40) >> 6
}
func ParseH264Buffer(buffer []byte, outputChan chan<- []byte) {
startCode1 := []byte{0x00,0x00,0x01}
startCode2 := []byte{0x00,0x00,0x00,0x01}
for i := 0; i < len(buffer); i++ {
var start bool
i, start = func() (int,bool) {
switch{
case reflect.DeepEqual(buffer[i:i+3],startCode1):
return i+3, true
case reflect.DeepEqual(buffer[i:i+4],startCode2):
return i+4, true
}
return i, false
}()
if nalType := buffer[i] & 0x1F; start && ( nalType == 1 || nalType == 5) {
for ; i < len(buffer) && !(i+3 < len(buffer) && ( reflect.DeepEqual(buffer[i:i+3],startCode1) ||
reflect.DeepEqual(buffer[i:i+4],startCode2))); i++ {}
outputChan<-append(append(startCode1,[]byte{0x09,0xF0}...),buffer[:i]...)
buffer = buffer[i:]
i=0
}
}
}