Merged in remove-unsafe-chan-use (pull request #69)

revid: clean up output clips some more
This commit is contained in:
kortschak 2018-10-24 00:27:35 +00:00
commit b4fc5fb045
1 changed files with 24 additions and 27 deletions

View File

@ -65,7 +65,7 @@ const (
rtmpConnectionTimout = 10 rtmpConnectionTimout = 10
outputChanSize = 1000 outputChanSize = 1000
cameraRetryPeriod = 5 * time.Second cameraRetryPeriod = 5 * time.Second
sendFailedDelay = 5 sendFailedDelay = 5 * time.Millisecond
maxSendFailedErrorCount = 500 maxSendFailedErrorCount = 500
clipSizeThreshold = 11 clipSizeThreshold = 11
rtmpConnectionMaxTries = 5 rtmpConnectionMaxTries = 5
@ -317,29 +317,26 @@ func (r *Revid) Stop() {
// outputClips takes the clips produced in the packClips method and outputs them // outputClips takes the clips produced in the packClips method and outputs them
// to the desired output defined in the revid config // to the desired output defined in the revid config
func (r *Revid) outputClips() { func (r *Revid) outputClips() {
now := time.Now() lastTime := time.Now()
prevTime := now var count int
bytes := 0 loop:
delay := 0
for r.isRunning { for r.isRunning {
// Here we slow things down as much as we can to decrease cpu usage
switch {
case r.buffer.Len() < 2:
delay++
time.Sleep(time.Duration(delay) * time.Millisecond)
case delay > 0:
delay--
}
// If the ring buffer has something we can read and send off // If the ring buffer has something we can read and send off
chunk, err := r.buffer.Next(readTimeout) chunk, err := r.buffer.Next(readTimeout)
if err != nil || !r.isRunning { switch err {
if err == io.EOF { case nil:
break // Do nothing.
} case ring.ErrTimeout:
r.config.Logger.Log(smartlogger.Warning, pkg+"ring buffer read timeout")
continue continue
default:
r.config.Logger.Log(smartlogger.Error, pkg+"unexpected error", "error", err.Error())
fallthrough
case io.EOF:
break loop
} }
bytes += chunk.Len() count += chunk.Len()
r.config.Logger.Log(smartlogger.Debug, pkg+"about to send") r.config.Logger.Log(smartlogger.Debug, pkg+"about to send")
err = r.destination.load(chunk) err = r.destination.load(chunk)
if err != nil { if err != nil {
@ -350,16 +347,16 @@ func (r *Revid) outputClips() {
r.config.Logger.Log(smartlogger.Debug, pkg+"sent clip") r.config.Logger.Log(smartlogger.Debug, pkg+"sent clip")
} }
if r.isRunning && err != nil && chunk.Len() > 11 { if err != nil && chunk.Len() > 11 {
r.config.Logger.Log(smartlogger.Debug, pkg+"send failed, trying again") r.config.Logger.Log(smartlogger.Debug, pkg+"send failed, trying again")
// Try and send again // Try and send again
err = r.destination.send() err = r.destination.send()
r.config.Logger.Log(smartlogger.Error, pkg+"destination send error", "error", err.Error()) r.config.Logger.Log(smartlogger.Error, pkg+"destination send error", "error", err.Error())
// if there's still an error we try and reconnect, unless we're stopping // if there's still an error we try and reconnect, unless we're stopping
for r.isRunning && err != nil { for err != nil {
r.config.Logger.Log(smartlogger.Debug, pkg+"send failed a again, trying to reconnect...") r.config.Logger.Log(smartlogger.Debug, pkg+"send failed again, trying to reconnect...")
time.Sleep(time.Duration(sendFailedDelay) * time.Millisecond) time.Sleep(sendFailedDelay)
r.config.Logger.Log(smartlogger.Error, pkg+"send failed with error", "error", err.Error()) r.config.Logger.Log(smartlogger.Error, pkg+"send failed with error", "error", err.Error())
if rs, ok := r.destination.(restarter); ok { if rs, ok := r.destination.(restarter); ok {
@ -387,15 +384,15 @@ func (r *Revid) outputClips() {
r.config.Logger.Log(smartlogger.Debug, pkg+"done reading that clip from ring buffer") r.config.Logger.Log(smartlogger.Debug, pkg+"done reading that clip from ring buffer")
// Log some information regarding bitrate and ring buffer size if it's time // Log some information regarding bitrate and ring buffer size if it's time
now = time.Now() now := time.Now()
deltaTime := now.Sub(prevTime) deltaTime := now.Sub(lastTime)
if deltaTime > bitrateTime { if deltaTime > bitrateTime {
// FIXME(kortschak): For subsecond deltaTime, this will give infinite bitrate. // FIXME(kortschak): For subsecond deltaTime, this will give infinite bitrate.
r.bitrate = int(float64(bytes*8) / float64(deltaTime/time.Second)) r.bitrate = int(float64(count*8) / float64(deltaTime/time.Second))
r.config.Logger.Log(smartlogger.Debug, pkg+"bitrate (bits/s)", "bitrate", r.bitrate) r.config.Logger.Log(smartlogger.Debug, pkg+"bitrate (bits/s)", "bitrate", r.bitrate)
r.config.Logger.Log(smartlogger.Debug, pkg+"ring buffer size", "value", r.buffer.Len()) r.config.Logger.Log(smartlogger.Debug, pkg+"ring buffer size", "value", r.buffer.Len())
prevTime = now lastTime = now
bytes = 0 count = 0
} }
} }
r.config.Logger.Log(smartlogger.Info, pkg+"not outputting clips anymore") r.config.Logger.Log(smartlogger.Info, pkg+"not outputting clips anymore")