diff --git a/revid/RevidInstance.go b/revid/RevidInstance.go index 76b86ff3..98bb4d19 100644 --- a/revid/RevidInstance.go +++ b/revid/RevidInstance.go @@ -72,6 +72,13 @@ const ( bitrateTime = 60 ) +const ( + Error = "Error" + Warning = "Warning" + Info = "Info" + Debug = "Debug" +) + const ( Raspivid = 0 Rtp = 1 @@ -106,8 +113,6 @@ type RevidInst interface { } type revidInst struct { - expectCC int - dumpCC int dumpPCRBase uint64 conn net.Conn ffmpegPath string @@ -115,7 +120,6 @@ type revidInst struct { ringBuffer ringbuffer.RingBuffer config Config isRunning bool - Error *log.Logger outputFile *os.File inputFile *os.File generator tsgenerator.TsGenerator @@ -129,9 +133,6 @@ func (r *revidInst) GetConfigRef() *Config{ func NewRevidInstance(config Config) (r *revidInst, err error) { r = new(revidInst) r.ringBuffer = ringbuffer.NewRingBuffer(bufferSize, mp2tPacketSize*mp2tMaxPackets) - r.Error = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) - r.expectCC = -1 - r.dumpCC = -1 r.dumpPCRBase = 0 r.ChangeState(config) switch r.config.Output { @@ -156,8 +157,8 @@ func NewRevidInstance(config Config) (r *revidInst, err error) { go r.h264Parser.Parse() go r.input() go r.generator.Generate() - r.ErrOut("New revid instance created! config is:") - r.ErrOut(fmt.Sprintf("%v",r.config)) + r.Log(Info, "New revid instance created! config is:") + r.Log(Info, fmt.Sprintf("%v",r.config)) return } @@ -167,8 +168,8 @@ func (r *revidInst) ChangeState(newconfig Config) error { return nil } -func (r *revidInst) ErrOut(m string){ - r.config.Logger.Log("Debug",m) +func (r *revidInst) Log(type, m string){ + r.config.Logger.Log(type,m) } func (r *revidInst)IsRunning() bool { @@ -177,58 +178,53 @@ func (r *revidInst)IsRunning() bool { func (r *revidInst) Start() { if r.isRunning { - r.ErrOut("Start() has been called but revid already running!") + r.Log(Warning,"Start() has been called but revid already running!") return } - r.ErrOut("Starting Revid!") + r.Log(Debug,"Starting Revid!") + var h264Data []byte switch r.config.Input { case Raspivid: - r.ErrOut("Starting raspivid!") + r.Log(Debug,"Starting raspivid!") cmd = exec.Command("raspivid", "-o", "-", "-n", "-t", "0", "-b", r.config.Bitrate,"-qp", r.config.Quantization, "-w", r.config.Width, "-h", r.config.Height, "-fps", r.config.FrameRate, "-ih", "-g", "100") stdout, _ := cmd.StdoutPipe() err := cmd.Start() inputReader = bufio.NewReader(stdout) if err != nil { - r.ErrOut(err.Error()) + r.Log(Error,err.Error()) return } - case File: - default: - r.ErrOut("Input not valid!") - } - r.isRunning = true - var h264Data []byte - switch r.config.Input { - case Raspivid: + r.isRunning = true go func() { - r.ErrOut("Reading camera data!") + r.Log(Debug, "Reading camera data!") for r.isRunning { h264Data = make([]byte, 1) _, err := io.ReadFull(inputReader, h264Data) if err != nil { - if err.Error() == "EOF" && r.isRunning { - r.ErrOut("No data from camera!") + switch{ + case err.Error() == "EOF" && r.isRunning: + r.Log(Error, "No data from camera!") time.Sleep(5*time.Second) - } else { - r.ErrOut(err.Error()) + case r.isRunning: + r.Log(Error, err.Error()) } } else { r.h264Parser.InputByteChan <- h264Data[0] } } - r.ErrOut("Out of reading routine") + r.Log(Debug, "Out of reading routine!") }() case File: stats, err := r.inputFile.Stat() if err != nil { - r.ErrOut("Could not get file stats!") + r.Log(Error, "Could not get input file stats!") return } h264Data = make([]byte, stats.Size()) _, err = r.inputFile.Read(h264Data) if err != nil { - r.ErrOut(err.Error()) + r.Log(Error, err.Error()) } for i := range h264Data { r.h264Parser.InputByteChan <- h264Data[i] @@ -239,7 +235,7 @@ func (r *revidInst) Start() { func (r *revidInst) Stop() { if r.isRunning { - r.ErrOut("Stopping revid!") + r.Log(Debug,"Stopping revid!") r.isRunning = false cmd.Process.Kill() } @@ -252,7 +248,8 @@ func (r *revidInst) input() { prevTime := now for { if clip, err := r.ringBuffer.Get(); err != nil { - r.ErrOut(err.Error()) + r.Log(Error,err.Error()) + r.Log(Warning,"Clearing tsPkt chan!") tsPktChanLen := len(r.generator.GetTsOutputChan()) for i := 0; i < tsPktChanLen; i++ { <-(r.generator.GetTsOutputChan()) @@ -263,7 +260,7 @@ func (r *revidInst) input() { tsPacket := <-(r.generator.GetTsOutputChan()) byteSlice, err := tsPacket.ToByteSlice() if err != nil { - r.ErrOut(err.Error()) + r.Log(Error,err.Error()) } upperBound := clipSize + mp2tPacketSize copy(clip[clipSize:upperBound], byteSlice) @@ -274,8 +271,8 @@ func (r *revidInst) input() { if (packetCount == mp2tMaxPackets) || (now.Sub(prevTime) > clipDuration*time.Second && packetCount%packetsPerFrame == 0) { if err := r.ringBuffer.DoneWriting(clipSize); err != nil { - r.ErrOut(err.Error()) - r.ErrOut("Dropping that clip!") + r.Log(Error,err.Error()) + r.Log(Warning,"Dropping clip!") } clipSize = 0 packetCount = 0 @@ -293,36 +290,36 @@ func (r *revidInst) output() { bytes := 0 delay := 0 for r.isRunning { - if r.ringBuffer.GetNoOfElements() < 2 { + switch{ + case r.ringBuffer.GetNoOfElements() < 2: delay++ time.Sleep(time.Duration(delay)*time.Millisecond) - } else { - if delay > 10 { - delay -= 10 - } + case delay > 10: + delay -= 10 } if clip, err := r.ringBuffer.Read(); err == nil { - r.ErrOut(fmt.Sprintf("Delay is: %v\n", delay)) - r.ErrOut(fmt.Sprintf("Ring buffer size: %v\n", r.ringBuffer.GetNoOfElements())) + r.Log(Debug,fmt.Sprintf("Delay is: %v\n", delay)) switch r.config.Output { case File: r.outputFile.Write(clip) case HttpOut: bytes += len(clip) for err := r.sendClipToHTTP(clip, r.config.HttpAddress); err != nil; { - r.ErrOut("Post failed trying again!") + r.Log(Error,err.Error()) + r.Log(Warning,"Post failed trying again!") err = r.sendClipToHTTP(clip, r.config.HttpAddress) } default: - r.ErrOut("No output defined!") + r.Log(Error,"No output defined!") } if err := r.ringBuffer.DoneReading(); err != nil { - r.ErrOut(err.Error()) + r.Log(Error,err.Error()) } now = time.Now() deltaTime := now.Sub(prevTime) if deltaTime > time.Duration(bitrateTime)*time.Second { - fmt.Printf("Bitrate: %v bytes/s\n", int64(float64(bytes) / float64(deltaTime/1e9))) + r.Log(Info,fmt.Sprintf("Bitrate: %v bits/s\n", int64(float64(bytes*8) / float64(deltaTime/1e9)))) + r.Log(Info,fmt.Sprintf("Ring buffer size: %v\n", r.ringBuffer.GetNoOfElements())) prevTime = now bytes = 0 } @@ -337,7 +334,7 @@ func (r *revidInst)sendClipToHTTP(clip []byte, output string) error { Timeout: timeout, } url := output + strconv.Itoa(len(clip)) - r.ErrOut(fmt.Sprintf("Posting %s (%d bytes)\n", url, len(clip))) + r.Log(Debug,fmt.Sprintf("Posting %s (%d bytes)\n", url, len(clip))) resp, err := client.Post(url, "video/mp2t", bytes.NewReader(clip)) // lighter than NewBuffer if err != nil { return fmt.Errorf("Error posting to %s: %s", output, err) @@ -345,9 +342,9 @@ func (r *revidInst)sendClipToHTTP(clip []byte, output string) error { defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err == nil { - r.ErrOut(fmt.Sprintf("%s\n", body)) + r.Log(Debug,fmt.Sprintf("%s\n", body)) } else { - r.ErrOut(err.Error()) + r.Log(Error,err.Error()) } return nil }