av/parser/mjpeg.go

88 lines
2.0 KiB
Go
Raw Normal View History

2018-03-13 03:54:37 +03:00
/*
NAME
mjpeg.go
2018-03-13 03:54:37 +03:00
DESCRIPTION
See Readme.md
AUTHOR
Saxon Nelson-Milton <saxon@ausocean.org>
LICENSE
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
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
2018-03-13 03:54:37 +03:00
*/
package parser
const frameStartCode = 0xD8
2018-06-17 15:00:00 +03:00
type MJPEG 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-06-17 15:00:00 +03:00
func NewMJPEGParser(inputChanLen int) (p *MJPEG) {
p = new(MJPEG)
2018-03-13 03:54:37 +03:00
p.isParsing = true
p.inputChan = make(chan byte, inputChanLen)
return
}
2018-06-17 15:00:00 +03:00
func (p *MJPEG) Stop() {
2018-03-13 03:54:37 +03:00
p.isParsing = false
}
2018-06-17 15:00:00 +03:00
func (p *MJPEG) Start() {
2018-03-13 03:54:37 +03:00
go p.parse()
}
2018-06-17 15:00:00 +03:00
func (p *MJPEG) SetDelay(delay uint) {
2018-03-13 03:54:37 +03:00
p.delay = delay
}
2018-06-17 15:00:00 +03:00
func (p *MJPEG) InputChan() chan byte {
2018-03-13 03:54:37 +03:00
return p.inputChan
}
2018-06-17 15:00:00 +03:00
func (p *MJPEG) OutputChan() <-chan []byte {
2018-03-13 03:54:37 +03:00
return p.userOutputChanRef
}
2018-06-17 15:00:00 +03:00
func (p *MJPEG) SetOutputChan(o chan []byte) {
p.parserOutputChanRef = o
p.userOutputChanRef = o
}
2018-06-17 15:00:00 +03:00
func (p *MJPEG) parse() {
2018-03-13 03:54:37 +03:00
var outputBuffer []byte
for p.isParsing {
2018-03-13 03:54:37 +03:00
aByte := <-p.inputChan
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:]
}
}
}
}