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
outputChanSize = 1000
cameraRetryPeriod = 5 * time.Second
sendFailedDelay = 5
sendFailedDelay = 5 * time.Millisecond
maxSendFailedErrorCount = 500
clipSizeThreshold = 11
rtmpConnectionMaxTries = 5
@ -317,29 +317,26 @@ func (r *Revid) Stop() {
// outputClips takes the clips produced in the packClips method and outputs them
// to the desired output defined in the revid config
func (r *Revid) outputClips() {
now := time.Now()
prevTime := now
bytes := 0
delay := 0
lastTime := time.Now()
var count int
loop:
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
chunk, err := r.buffer.Next(readTimeout)
if err != nil || !r.isRunning {
if err == io.EOF {
break
}
switch err {
case nil:
// Do nothing.
case ring.ErrTimeout:
r.config.Logger.Log(smartlogger.Warning, pkg+"ring buffer read timeout")
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")
err = r.destination.load(chunk)
if err != nil {
@ -350,16 +347,16 @@ func (r *Revid) outputClips() {
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")
// Try and send again
err = r.destination.send()
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
for r.isRunning && err != nil {
r.config.Logger.Log(smartlogger.Debug, pkg+"send failed a again, trying to reconnect...")
time.Sleep(time.Duration(sendFailedDelay) * time.Millisecond)
for err != nil {
r.config.Logger.Log(smartlogger.Debug, pkg+"send failed again, trying to reconnect...")
time.Sleep(sendFailedDelay)
r.config.Logger.Log(smartlogger.Error, pkg+"send failed with error", "error", err.Error())
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")
// Log some information regarding bitrate and ring buffer size if it's time
now = time.Now()
deltaTime := now.Sub(prevTime)
now := time.Now()
deltaTime := now.Sub(lastTime)
if deltaTime > bitrateTime {
// 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+"ring buffer size", "value", r.buffer.Len())
prevTime = now
bytes = 0
lastTime = now
count = 0
}
}
r.config.Logger.Log(smartlogger.Info, pkg+"not outputting clips anymore")