From ab31f0bd679a40580959cbf6a42e1f9567f06e4f Mon Sep 17 00:00:00 2001 From: Saxon1 Date: Tue, 24 Apr 2018 15:08:30 +0930 Subject: [PATCH] Improving handling of send errors --- revid/Revid.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/revid/Revid.go b/revid/Revid.go index f6fe6b26..84f1291f 100644 --- a/revid/Revid.go +++ b/revid/Revid.go @@ -71,6 +71,9 @@ const ( rtmpConnectionTimout = 10 outputChanSize = 10000 cameraRetryPeriod = 5 * time.Second + sendFailedDelay = 5 + maxSendFailedErrorCount = 500 + clipsSizeThreshold = 11 ) // Log Types @@ -115,6 +118,7 @@ type revid struct { sendClip func(clip []byte) error rtmpInst rtmp.RTMPSession mutex sync.Mutex + currentBitrate int64 } // NewRevid returns a pointer to a new revid with the desired @@ -135,6 +139,12 @@ func NewRevid(config Config) (r *revid, err error) { return } +// Returns the currently saved bitrate from the most recent bitrate check +// check bitrate output delay in consts for this period +func (r *revid) GetBitrate() int64 { + return r.currentBitrate +} + // GetConfigRef returns a pointer to the revidInst's Config struct object func (r *revid) GetConfigRef() *Config { return &r.config @@ -357,13 +367,21 @@ func (r *revid) outputClips() { if clip, err := r.ringBuffer.Read(); err == nil { bytes += len(clip) errorCount := 0 - for err := r.sendClip(clip); err != nil; errorCount++ { - if len(clip) >= 11 { - r.Log(Warning, "Send failed trying again!") + err := r.sendClip(clip) + for ; err != nil; errorCount++ { + r.Log(Warning, "Send failed trying again!") + // If the clip size is not bigger than the threshold then we classify + // it as junk and we don't try to send it off again. + if len(clip) >= clipSizeThreshold { err = r.sendClip(clip) } else { break } + // So that we don't fill up the log, once we reach the maxSendFailedErrorCount + // we will add some delay to slow things down until we have a connection again + if errorCount > maxSendFailedErrorCount { + time.Sleep(time.Duration(sendFailedDelay) * time.Second) + } r.Log(Error, err.Error()) if r.config.Output == NativeRtmp && errorCount > 5 { r.reboot() @@ -378,7 +396,8 @@ func (r *revid) outputClips() { now = time.Now() deltaTime := now.Sub(prevTime) if deltaTime > time.Duration(bitrateTime)*time.Second { - r.Log(Info, fmt.Sprintf("Bitrate: %v bits/s\n", int64(float64(bytes*8)/float64(deltaTime/1e9)))) + r.currentBitrate = int64(float64(bytes*8)/float64(deltaTime/1e9))) + r.Log(Info, fmt.Sprintf("Bitrate: %v bits/s\n", r.currentBitrate ) r.Log(Info, fmt.Sprintf("Ring buffer size: %v\n", r.ringBuffer.GetNoOfElements())) prevTime = now bytes = 0