Improving handling of send errors

This commit is contained in:
Saxon1 2018-04-24 15:08:30 +09:30
parent 6f602479e2
commit ab31f0bd67
1 changed files with 23 additions and 4 deletions

View File

@ -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