2018-02-10 09:59:56 +03:00
|
|
|
package parser
|
|
|
|
|
2018-02-12 10:58:29 +03:00
|
|
|
import (
|
|
|
|
//"bitbucket.org/ausocean/av/itut"
|
|
|
|
"../itut"
|
|
|
|
_"fmt"
|
2018-02-15 10:02:04 +03:00
|
|
|
_"time"
|
2018-02-12 10:58:29 +03:00
|
|
|
)
|
|
|
|
|
2018-02-10 09:59:56 +03:00
|
|
|
type h264Parser struct {
|
|
|
|
inputBuffer []byte
|
|
|
|
isParsing bool
|
|
|
|
parserOutputChanRef chan []byte
|
|
|
|
userOutputChanRef chan []byte
|
|
|
|
inputChan chan byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewH264Parser() (p *h264Parser) {
|
|
|
|
p = new(h264Parser)
|
|
|
|
p.isParsing = true
|
2018-02-15 10:02:04 +03:00
|
|
|
p.inputChan = make(chan byte, 100000)
|
2018-02-10 09:59:56 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p* h264Parser)Stop(){
|
|
|
|
p.isParsing = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *h264Parser)Start(){
|
|
|
|
go p.parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *h264Parser)GetInputChan() chan byte {
|
|
|
|
return p.inputChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *h264Parser)GetOutputChan() chan []byte {
|
|
|
|
return p.userOutputChanRef
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *h264Parser)SetOutputChan(aChan chan []byte){
|
|
|
|
p.parserOutputChanRef = aChan
|
|
|
|
p.userOutputChanRef = aChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *h264Parser)parse() {
|
|
|
|
outputBuffer := make([]byte, 0, 10000)
|
|
|
|
searchingForEnd := false
|
|
|
|
for p.isParsing {
|
|
|
|
aByte := <-p.inputChan
|
|
|
|
outputBuffer = append(outputBuffer, aByte)
|
|
|
|
for i:=1; aByte == 0x00 && i != 4; i++ {
|
|
|
|
aByte = <-p.inputChan
|
|
|
|
outputBuffer = append(outputBuffer, aByte)
|
|
|
|
if ( aByte == 0x01 && i == 2 ) || ( aByte == 0x01 && i == 3 ) {
|
|
|
|
if searchingForEnd {
|
|
|
|
output := append(append(itut.StartCode1(),itut.AUD()...),outputBuffer[:len(outputBuffer)-(i+1)]...)
|
2018-02-15 10:02:04 +03:00
|
|
|
//time.Sleep(40*time.Millisecond)
|
2018-02-10 09:59:56 +03:00
|
|
|
p.parserOutputChanRef<-output
|
|
|
|
outputBuffer = outputBuffer[len(outputBuffer)-1-i:]
|
|
|
|
searchingForEnd = false
|
|
|
|
}
|
|
|
|
aByte = <-p.inputChan
|
|
|
|
outputBuffer = append(outputBuffer, aByte)
|
|
|
|
if nalType := aByte & 0x1F; nalType == 1 || nalType == 5 {
|
|
|
|
searchingForEnd = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|