diff --git a/h264/H264Writer.go b/parser/H264Writer.go similarity index 100% rename from h264/H264Writer.go rename to parser/H264Writer.go diff --git a/h264/h264Parser.go b/parser/Parser.go similarity index 58% rename from h264/h264Parser.go rename to parser/Parser.go index 09d9bf53..41faeec6 100644 --- a/h264/h264Parser.go +++ b/parser/Parser.go @@ -1,7 +1,6 @@ /* NAME - RtpToTsConverter.go - provides utilities for the conversion of Rtp packets - to equivalent MpegTs packets. + Parser.go DESCRIPTION See Readme.md @@ -36,6 +35,7 @@ import ( _"time" ) +// h264 consts const ( acceptedLength = 1000 ) @@ -45,31 +45,47 @@ var ( mutex *sync.Mutex ) -type H264Parser struct { +type Parser interface { + Stop() + Start() + GetInputChan() + GetOutputChan() +} + +type h264Parser struct { inputBuffer []byte isParsing bool OutputChan chan<- []byte - InputByteChan chan byte + InputChan chan byte } -func (p* H264Parser)Stop(){ +func NewH264Parser() (p *h264Parser) { + p = new(h264Parser) + p.isParsing = true + p.InputChan = make(chan byte, 10000) + return +} + +func (p* h264Parser)Stop(){ p.isParsing = false } -func (p *H264Parser)Start(){ +func (p *h264Parser)Start(){ go p.parse() } +func (p *h264Parser)GetInput() chan byte { + +} + func (p *H264Parser)parse() { - p.isParsing = true outputBuffer := make([]byte, 0, 10000) searchingForEnd := false - p.InputByteChan = make(chan byte, 10000) for p.isParsing { - aByte := <-p.InputByteChan + aByte := <-p.InputChan outputBuffer = append(outputBuffer, aByte) for i:=1; aByte == 0x00 && i != 4; i++ { - aByte = <-p.InputByteChan + aByte = <-p.InputChan outputBuffer = append(outputBuffer, aByte) if ( aByte == 0x01 && i == 2 ) || ( aByte == 0x01 && i == 3 ) { if searchingForEnd { @@ -78,7 +94,7 @@ func (p *H264Parser)parse() { outputBuffer = outputBuffer[len(outputBuffer)-1-i:] searchingForEnd = false } - aByte = <-p.InputByteChan + aByte = <-p.InputChan outputBuffer = append(outputBuffer, aByte) if nalType := aByte & 0x1F; nalType == 1 || nalType == 5 { searchingForEnd = true @@ -87,3 +103,46 @@ func (p *H264Parser)parse() { } } } + + +type mjpegParser struct { + inputBuffer []byte + isParsing bool + outputChan chan<- []byte + inputChan chan byte +} + +func NewMJPEGParser() (p *mjpegParser){ + p = new(mjpegParser) + p.isParsing = true + p.InputChan = make(chan byte, 10000) + return +} + +func (p *mjpegParser)Stop(){ + p.isParsing = false +} + +func (p *mjpegParser)Start(){ + go p.parse() +} + +func (p *mjpegParser)GetInput() chan byte { + return p.inputChan +} + +func (p *mjpegParser)parse() { + outputBuffer := make([]byte, 0, 10000) + for p.isParsing { + aByte := <-p.InputChan + outputBuffer = append(outputBuffer, aByte) + if aByte = 0xFF && len(outputBuffer) > 1 { + aByte := <-p.InputChan + outputBuffer = append(outputBuffer, aByte) + if aByte = 0xD8 { + p.OutputChan<-output + outputBuffer = outputBuffer[len(outputBuffer)-1-i:] + } + } + } +} diff --git a/parser/parser_test.go b/parser/parser_test.go new file mode 100644 index 00000000..5caf57f1 --- /dev/null +++ b/parser/parser_test.go @@ -0,0 +1,37 @@ +package parser + +import ( + "testing" +) + +const ( + testInputFileName = "inputFiles/inputFile.avi" +) + +func TestMJPEGParser(t *testing.T){ + parser := NewMJPEGParser() + // TODO: Find mjpeg file and see if the mjpeg parser can output individual + // jpeg images... + inputFile, err = os.Open(testInputFileName) + if err != nil { + t.Errorf("Should not have got error opening file!") + } + stats, err := inputFile.Stat() + if err != nil { + t.Errorf("Could not get input file stats!") + return + } + data := make([]byte, stats.Size()) + _, err = r.inputFile.Read(data) + if err != nil { + t.Errorf("Should not have got read error!") + return + } + for i := range data { + parser.InputByteChan <- data[i] + } + + for len(parser.GetOutputByteChan()) > 0 { + + } +} diff --git a/parser/testInput/testInput.avi b/parser/testInput/testInput.avi new file mode 100644 index 00000000..e455f8c4 Binary files /dev/null and b/parser/testInput/testInput.avi differ diff --git a/revid/RevidInstance.go b/revid/RevidInstance.go index 6db415c3..9c8076f6 100644 --- a/revid/RevidInstance.go +++ b/revid/RevidInstance.go @@ -175,7 +175,7 @@ func (r *revidInst) Start() { return } r.Log(Info, "Starting Revid!") - var h264Data []byte + var data []byte switch r.config.Input { case Raspivid: r.Log(Info, "Starting raspivid!") @@ -192,8 +192,8 @@ func (r *revidInst) Start() { go func() { r.Log(Info, "Reading camera data!") for r.isRunning { - h264Data = make([]byte, 1) - _, err := io.ReadFull(r.inputReader, h264Data) + data = make([]byte, 1) + _, err := io.ReadFull(r.inputReader, data) switch { case err != nil && err.Error() == "EOF" && r.isRunning: r.Log(Error, "No data from camera!") @@ -201,7 +201,7 @@ func (r *revidInst) Start() { case err != nil && r.isRunning: r.Log(Error, err.Error()) default: - r.h264Parser.InputByteChan <- h264Data[0] + r.parser.GetInput() <-h264Data[0] } } r.Log(Info, "Out of reading routine!")