2018-03-13 03:54:37 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2018-05-31 12:54:20 +03:00
|
|
|
mjpeg.go
|
2018-03-13 03:54:37 +03:00
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
2018-05-31 12:54:20 +03:00
|
|
|
mjpeg.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
|
2018-03-13 03:54:37 +03:00
|
|
|
|
|
|
|
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
|
2018-05-31 12:54:20 +03:00
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
2018-03-13 03:54:37 +03:00
|
|
|
*/
|
|
|
|
|
2018-02-10 09:59:56 +03:00
|
|
|
package parser
|
|
|
|
|
2018-05-31 12:54:20 +03:00
|
|
|
const frameStartCode = 0xD8
|
2018-02-12 10:58:29 +03:00
|
|
|
|
2018-02-10 09:59:56 +03:00
|
|
|
type mjpegParser struct {
|
2018-03-13 03:54:37 +03:00
|
|
|
inputBuffer []byte
|
|
|
|
isParsing bool
|
|
|
|
parserOutputChanRef chan []byte
|
|
|
|
userOutputChanRef chan []byte
|
|
|
|
inputChan chan byte
|
|
|
|
delay uint
|
2018-02-10 09:59:56 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 03:54:37 +03:00
|
|
|
func NewMJPEGParser(inputChanLen int) (p *mjpegParser) {
|
|
|
|
p = new(mjpegParser)
|
|
|
|
p.isParsing = true
|
|
|
|
p.inputChan = make(chan byte, inputChanLen)
|
|
|
|
return
|
2018-02-10 09:59:56 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 03:54:37 +03:00
|
|
|
func (p *mjpegParser) Stop() {
|
|
|
|
p.isParsing = false
|
2018-02-10 09:59:56 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 03:54:37 +03:00
|
|
|
func (p *mjpegParser) Start() {
|
|
|
|
go p.parse()
|
2018-02-10 09:59:56 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 03:54:37 +03:00
|
|
|
func (p *mjpegParser) SetDelay(delay uint) {
|
|
|
|
p.delay = delay
|
2018-02-19 08:06:13 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 03:54:37 +03:00
|
|
|
func (p *mjpegParser) GetInputChan() chan byte {
|
|
|
|
return p.inputChan
|
2018-02-10 09:59:56 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 03:54:37 +03:00
|
|
|
func (p *mjpegParser) GetOutputChan() chan []byte {
|
|
|
|
return p.userOutputChanRef
|
2018-02-10 09:59:56 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 03:54:37 +03:00
|
|
|
func (p *mjpegParser) SetOutputChan(aChan chan []byte) {
|
|
|
|
p.parserOutputChanRef = aChan
|
|
|
|
p.userOutputChanRef = aChan
|
2018-02-10 09:59:56 +03:00
|
|
|
}
|
|
|
|
|
2018-03-13 03:54:37 +03:00
|
|
|
func (p *mjpegParser) parse() {
|
|
|
|
var outputBuffer []byte
|
2018-02-10 09:59:56 +03:00
|
|
|
for p.isParsing {
|
2018-03-13 03:54:37 +03:00
|
|
|
aByte := <-p.inputChan
|
2018-02-10 09:59:56 +03:00
|
|
|
outputBuffer = append(outputBuffer, aByte)
|
2018-03-13 03:54:37 +03:00
|
|
|
if aByte == 0xFF && len(outputBuffer) != 0 {
|
|
|
|
aByte := <-p.inputChan
|
|
|
|
outputBuffer = append(outputBuffer, aByte)
|
|
|
|
if aByte == frameStartCode {
|
|
|
|
p.parserOutputChanRef <- outputBuffer[:len(outputBuffer)-2]
|
|
|
|
outputBuffer = outputBuffer[len(outputBuffer)-2:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-10 09:59:56 +03:00
|
|
|
}
|