2018-01-09 08:06:09 +03:00
|
|
|
/*
|
|
|
|
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).
|
|
|
|
*/
|
|
|
|
|
2018-01-09 07:26:34 +03:00
|
|
|
package h264
|
2018-01-09 08:06:09 +03:00
|
|
|
|
|
|
|
import (
|
2018-01-11 09:49:33 +03:00
|
|
|
"bitbucket.org/ausocean/av/itut"
|
2018-01-11 07:55:59 +03:00
|
|
|
"log"
|
|
|
|
"sync"
|
2018-01-11 09:13:21 +03:00
|
|
|
_"fmt"
|
|
|
|
_"time"
|
2018-01-11 07:55:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
acceptedLength = 1000
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Info *log.Logger
|
|
|
|
mutex *sync.Mutex
|
2018-01-09 08:06:09 +03:00
|
|
|
)
|
|
|
|
|
2018-01-10 06:57:56 +03:00
|
|
|
type H264Parser struct {
|
2018-01-09 08:06:09 +03:00
|
|
|
inputBuffer []byte
|
|
|
|
isParsing bool
|
2018-01-10 06:57:56 +03:00
|
|
|
OutputChan chan<- []byte
|
2018-01-11 07:55:59 +03:00
|
|
|
InputByteChan chan byte
|
2018-01-09 08:06:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p* H264Parser)Stop(){
|
2018-01-10 06:57:56 +03:00
|
|
|
p.isParsing = false
|
2018-01-09 08:06:09 +03:00
|
|
|
}
|
|
|
|
|
2018-01-10 06:57:56 +03:00
|
|
|
func (p* H264Parser)Parse() {
|
2018-01-11 09:13:21 +03:00
|
|
|
p.isParsing = true
|
2018-01-11 07:55:59 +03:00
|
|
|
outputBuffer := []byte{}
|
2018-01-11 09:13:21 +03:00
|
|
|
searchingForEnd := false
|
2018-01-11 07:55:59 +03:00
|
|
|
p.InputByteChan = make(chan byte, 10000)
|
|
|
|
for p.isParsing {
|
2018-01-11 09:13:21 +03:00
|
|
|
aByte := <-p.InputByteChan
|
2018-01-11 07:55:59 +03:00
|
|
|
outputBuffer = append(outputBuffer, aByte)
|
|
|
|
for i:=1; aByte == 0x00 && i != 4; i++ {
|
2018-01-11 09:13:21 +03:00
|
|
|
aByte = <-p.InputByteChan
|
2018-01-11 07:55:59 +03:00
|
|
|
outputBuffer = append(outputBuffer, aByte)
|
|
|
|
if ( aByte == 0x01 && i == 2 ) || ( aByte == 0x01 && i == 3 ) {
|
|
|
|
if searchingForEnd {
|
2018-01-11 09:13:21 +03:00
|
|
|
output := append(append(itut.StartCode1(),itut.AUD()...),outputBuffer[:len(outputBuffer)-(i+1)]...)
|
|
|
|
p.OutputChan<-output
|
|
|
|
outputBuffer = outputBuffer[len(outputBuffer)-1-i:]
|
|
|
|
searchingForEnd = false
|
2018-01-11 07:55:59 +03:00
|
|
|
}
|
|
|
|
aByte = <-p.InputByteChan
|
|
|
|
outputBuffer = append(outputBuffer, aByte)
|
|
|
|
if nalType := aByte & 0x1F; nalType == 1 || nalType == 5 {
|
2018-01-11 09:13:21 +03:00
|
|
|
searchingForEnd = true
|
2018-01-11 07:55:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-09 08:06:09 +03:00
|
|
|
}
|