diff --git a/revid/Revid.go b/revid/Revid.go index b4255861..29ca2e9c 100644 --- a/revid/Revid.go +++ b/revid/Revid.go @@ -115,6 +115,7 @@ type revid struct { sendClip func(clip []byte) error rtmpInst rtmp.RTMPSession mutex sync.Mutex + sendMutex sync.Mutex currentBitrate int64 } @@ -124,6 +125,7 @@ type revid struct { func NewRevid(config Config) (r *revid, err error) { r = new(revid) r.mutex = sync.Mutex{} + r.sendMutex = sync.Mutex{} r.ringBuffer = ringbuffer.NewRingBuffer(ringBufferSize, ringBufferElementSize) err = r.changeState(config) if err != nil { @@ -280,6 +282,9 @@ func (r *revid) Stop() { } r.Log(Info, "Stopping revid!") + // wait for sending to finish + r.sendMutex.Lock() + defer r.sendMutex.Unlock() r.rtmpInst.EndSession() r.isRunning = false @@ -463,8 +468,8 @@ func (r *revid) outputClips() { // senClipToFile writes the passed clip to a file func (r *revid) sendClipToFile(clip []byte) error { - r.mutex.Lock() - defer r.mutex.Unlock() + r.sendMutex.Lock() + defer r.sendMutex.Unlock() _, err := r.outputFile.Write(clip) if err != nil { return err @@ -474,8 +479,8 @@ func (r *revid) sendClipToFile(clip []byte) error { // sendClipToHTTP takes a clip and an output url and posts through http. func (r *revid) sendClipToHTTP(clip []byte) error { - r.mutex.Lock() - defer r.mutex.Unlock() + r.sendMutex.Lock() + defer r.sendMutex.Unlock() timeout := time.Duration(httpTimeOut * time.Second) client := http.Client{Timeout: timeout} url := r.config.HttpAddress + strconv.Itoa(len(clip)) @@ -497,8 +502,8 @@ func (r *revid) sendClipToHTTP(clip []byte) error { // sendClipToFfmpegRtmp sends the clip over the current rtmp connection using // an ffmpeg process. func (r *revid) sendClipToFfmpegRtmp(clip []byte) (err error) { - r.mutex.Lock() - defer r.mutex.Unlock() + r.sendMutex.Lock() + defer r.sendMutex.Unlock() _, err = r.ffmpegStdin.Write(clip) return } @@ -506,8 +511,8 @@ func (r *revid) sendClipToFfmpegRtmp(clip []byte) (err error) { // sendClipToLibRtmp send the clip over the current rtmp connection using the // c based librtmp library func (r *revid) sendClipToLibRtmp(clip []byte) (err error) { - r.mutex.Lock() - defer r.mutex.Unlock() + r.sendMutex.Lock() + defer r.sendMutex.Unlock() err = r.rtmpInst.WriteFrame(clip, uint(len(clip))) return }