2018-01-09 08:06:09 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2018-01-29 09:34:08 +03:00
|
|
|
Parser.go
|
2018-01-09 08:06:09 +03:00
|
|
|
|
|
|
|
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-30 05:19:39 +03:00
|
|
|
package parser
|
2018-01-09 08:06:09 +03:00
|
|
|
|
|
|
|
import (
|
2018-02-08 10:21:43 +03:00
|
|
|
//"bitbucket.org/ausocean/av/itut"
|
|
|
|
"../itut"
|
2018-01-11 07:55:59 +03:00
|
|
|
"log"
|
|
|
|
"sync"
|
2018-02-09 09:23:06 +03:00
|
|
|
_"fmt"
|
2018-01-11 07:55:59 +03:00
|
|
|
)
|
|
|
|
|
2018-01-29 09:34:08 +03:00
|
|
|
// h264 consts
|
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-29 09:34:08 +03:00
|
|
|
type Parser interface {
|
|
|
|
Stop()
|
|
|
|
Start()
|
2018-01-30 09:24:39 +03:00
|
|
|
GetInputChan() chan byte
|
|
|
|
GetOutputChan() chan []byte
|
|
|
|
SetOutputChan(achan chan []byte)
|
2018-01-29 09:34:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type h264Parser struct {
|
2018-01-09 08:06:09 +03:00
|
|
|
inputBuffer []byte
|
|
|
|
isParsing bool
|
2018-01-30 09:24:39 +03:00
|
|
|
parserOutputChanRef chan []byte
|
|
|
|
userOutputChanRef chan []byte
|
2018-01-30 05:19:39 +03:00
|
|
|
inputChan chan byte
|
2018-01-29 09:34:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewH264Parser() (p *h264Parser) {
|
|
|
|
p = new(h264Parser)
|
|
|
|
p.isParsing = true
|
2018-01-30 05:19:39 +03:00
|
|
|
p.inputChan = make(chan byte, 10000)
|
2018-01-29 09:34:08 +03:00
|
|
|
return
|
2018-01-09 08:06:09 +03:00
|
|
|
}
|
|
|
|
|
2018-01-29 09:34:08 +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-29 09:34:08 +03:00
|
|
|
func (p *h264Parser)Start(){
|
2018-01-24 07:12:22 +03:00
|
|
|
go p.parse()
|
|
|
|
}
|
|
|
|
|
2018-01-30 05:19:39 +03:00
|
|
|
func (p *h264Parser)GetInputChan() chan byte {
|
|
|
|
return p.inputChan
|
|
|
|
}
|
|
|
|
|
2018-01-30 09:24:39 +03:00
|
|
|
func (p *h264Parser)GetOutputChan() chan []byte {
|
2018-01-30 05:19:39 +03:00
|
|
|
return p.userOutputChanRef
|
|
|
|
}
|
2018-01-29 09:34:08 +03:00
|
|
|
|
2018-01-30 05:19:39 +03:00
|
|
|
func (p *h264Parser)SetOutputChan(aChan chan []byte){
|
|
|
|
p.parserOutputChanRef = aChan
|
|
|
|
p.userOutputChanRef = aChan
|
2018-01-29 09:34:08 +03:00
|
|
|
}
|
|
|
|
|
2018-01-30 05:19:39 +03:00
|
|
|
func (p *h264Parser)parse() {
|
2018-01-16 08:06:51 +03:00
|
|
|
outputBuffer := make([]byte, 0, 10000)
|
2018-01-11 09:13:21 +03:00
|
|
|
searchingForEnd := false
|
2018-01-11 07:55:59 +03:00
|
|
|
for p.isParsing {
|
2018-01-30 05:19:39 +03:00
|
|
|
aByte := <-p.inputChan
|
2018-01-11 07:55:59 +03:00
|
|
|
outputBuffer = append(outputBuffer, aByte)
|
|
|
|
for i:=1; aByte == 0x00 && i != 4; i++ {
|
2018-01-30 05:19:39 +03:00
|
|
|
aByte = <-p.inputChan
|
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)]...)
|
2018-01-30 05:19:39 +03:00
|
|
|
p.parserOutputChanRef<-output
|
2018-01-11 09:13:21 +03:00
|
|
|
outputBuffer = outputBuffer[len(outputBuffer)-1-i:]
|
|
|
|
searchingForEnd = false
|
2018-01-11 07:55:59 +03:00
|
|
|
}
|
2018-01-30 05:19:39 +03:00
|
|
|
aByte = <-p.inputChan
|
2018-01-11 07:55:59 +03:00
|
|
|
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
|
|
|
}
|
2018-01-29 09:34:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
type mjpegParser struct {
|
|
|
|
inputBuffer []byte
|
|
|
|
isParsing bool
|
2018-01-30 09:24:39 +03:00
|
|
|
parserOutputChanRef chan []byte
|
|
|
|
userOutputChanRef chan []byte
|
2018-01-29 09:34:08 +03:00
|
|
|
inputChan chan byte
|
|
|
|
}
|
|
|
|
|
2018-01-30 05:19:39 +03:00
|
|
|
func NewMJPEGParser(inputChanLen int) (p *mjpegParser){
|
2018-01-29 09:34:08 +03:00
|
|
|
p = new(mjpegParser)
|
|
|
|
p.isParsing = true
|
2018-01-30 05:19:39 +03:00
|
|
|
p.inputChan = make(chan byte, inputChanLen )
|
2018-01-29 09:34:08 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *mjpegParser)Stop(){
|
|
|
|
p.isParsing = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *mjpegParser)Start(){
|
|
|
|
go p.parse()
|
|
|
|
}
|
|
|
|
|
2018-01-30 05:19:39 +03:00
|
|
|
func (p *mjpegParser)GetInputChan() chan byte {
|
2018-01-29 09:34:08 +03:00
|
|
|
return p.inputChan
|
|
|
|
}
|
|
|
|
|
2018-01-30 09:24:39 +03:00
|
|
|
func (p *mjpegParser)GetOutputChan() chan []byte {
|
2018-01-30 05:19:39 +03:00
|
|
|
return p.userOutputChanRef
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *mjpegParser)SetOutputChan(aChan chan []byte){
|
|
|
|
p.parserOutputChanRef = aChan
|
|
|
|
p.userOutputChanRef = aChan
|
|
|
|
}
|
|
|
|
|
2018-01-29 09:34:08 +03:00
|
|
|
func (p *mjpegParser)parse() {
|
2018-01-30 05:19:39 +03:00
|
|
|
var outputBuffer []byte
|
2018-01-29 09:34:08 +03:00
|
|
|
for p.isParsing {
|
2018-01-30 05:19:39 +03:00
|
|
|
aByte := <-p.inputChan
|
2018-01-29 09:34:08 +03:00
|
|
|
outputBuffer = append(outputBuffer, aByte)
|
2018-01-30 05:19:39 +03:00
|
|
|
if aByte == 0xFF && len(outputBuffer) != 0 {
|
|
|
|
aByte := <-p.inputChan
|
2018-01-29 09:34:08 +03:00
|
|
|
outputBuffer = append(outputBuffer, aByte)
|
2018-01-30 05:19:39 +03:00
|
|
|
if aByte == 0xD8 {
|
|
|
|
p.parserOutputChanRef<-outputBuffer[:len(outputBuffer)-2]
|
|
|
|
outputBuffer = outputBuffer[len(outputBuffer)-2:]
|
2018-01-29 09:34:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|