mirror of https://bitbucket.org/ausocean/av.git
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
/*
|
|
NAME
|
|
RtpToTsConverter.go - provides utilities for the conversion of Rtp packets
|
|
to equivalent MpegTs packets.
|
|
|
|
DESCRIPTION
|
|
See Readme.md
|
|
|
|
AUTHOR
|
|
Saxon Nelson-Milton <saxon.milton@gmail.com>
|
|
|
|
LICENSE
|
|
RtpToTsConverter.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 h264
|
|
|
|
import (
|
|
"bitbucket.org/ausocean/av/itut"
|
|
"reflect"
|
|
)
|
|
|
|
type H264Parser struct {
|
|
inputBuffer []byte
|
|
isParsing bool
|
|
OutputChan chan<- []byte
|
|
}
|
|
|
|
func (p* H264Parser)SendInputData(someData []byte){
|
|
p.inputBuffer = append(p.inputBuffer, someData...)
|
|
}
|
|
|
|
func (p* H264Parser)Stop(){
|
|
p.isParsing = false
|
|
}
|
|
|
|
func (p* H264Parser)Parse() {
|
|
p.isParsing = true
|
|
buffer := p.inputBuffer
|
|
for p.isParsing {
|
|
for i := 0; len(buffer) > 10; i++{
|
|
var start bool
|
|
i, start = func() (int,bool) {
|
|
switch{
|
|
case reflect.DeepEqual(buffer[i:i+3],itut.StartCode1()):
|
|
return i+3, true
|
|
case reflect.DeepEqual(buffer[i:i+4],itut.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],itut.StartCode1()) ||
|
|
reflect.DeepEqual(buffer[i:i+4],itut.StartCode2()))); i++ {}
|
|
p.OutputChan<-append(append(itut.StartCode1(),itut.AUD()...),buffer[:i]...)
|
|
buffer = buffer[i:]
|
|
i=0
|
|
}
|
|
if i >= len(buffer) {
|
|
buffer = []byte{}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|