/* NAME mjpeg.go DESCRIPTION See Readme.md AUTHOR Saxon Nelson-Milton LICENSE mjpeg.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 http://www.gnu.org/licenses. */ package parser const frameStartCode = 0xD8 type MJPEG struct { inputBuffer []byte isParsing bool parserOutputChanRef chan []byte userOutputChanRef chan []byte inputChan chan byte delay uint } func NewMJPEGParser(inputChanLen int) (p *MJPEG) { p = new(MJPEG) p.isParsing = true p.inputChan = make(chan byte, inputChanLen) return } func (p *MJPEG) Stop() { p.isParsing = false } func (p *MJPEG) Start() { go p.parse() } func (p *MJPEG) SetDelay(delay uint) { p.delay = delay } func (p *MJPEG) InputChan() chan byte { return p.inputChan } func (p *MJPEG) OutputChan() <-chan []byte { return p.userOutputChanRef } func (p *MJPEG) SetOutputChan(o chan []byte) { p.parserOutputChanRef = o p.userOutputChanRef = o } func (p *MJPEG) parse() { var outputBuffer []byte for p.isParsing { aByte := <-p.inputChan outputBuffer = append(outputBuffer, aByte) 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:] } } } }