Use mutex instead of isSending to be safe.

This commit is contained in:
Alan Noble 2018-05-07 22:39:58 +09:30
parent defe5c54a8
commit cc45b02609
1 changed files with 12 additions and 18 deletions

View File

@ -100,7 +100,6 @@ type revid struct {
ringBuffer ringbuffer.RingBuffer
config Config
isRunning bool
isSending bool
outputFile *os.File
inputFile *os.File
generator generator.Generator
@ -132,7 +131,6 @@ func NewRevid(config Config) (r *revid, err error) {
return
}
r.outputChan = make(chan []byte, outputChanSize)
r.isSending = false
r.isRunning = false
return
}
@ -267,23 +265,19 @@ func (r *revid) Start() {
r.generator.Start()
r.Log(Info, "Starting parser!")
r.parser.Start()
r.Log(Info, "Setting up input and recieving content!")
r.Log(Info, "Setting up input and receiving content!")
go r.setupInput()
}
// Stop halts any processing of video data from a camera or file
func (r *revid) Stop() {
r.mutex.Lock()
defer r.mutex.Unlock()
if !r.isRunning {
r.Log(Warning, "revid.Stop() called but revid not running!")
return
}
// Wait until we're not sending anything before we stop things
for r.isSending {
time.Sleep(sendingWaitTime)
}
r.mutex.Lock()
defer r.mutex.Unlock()
r.Log(Info, "Stopping revid!")
r.rtmpInst.EndSession()
@ -469,18 +463,19 @@ func (r *revid) outputClips() {
// senClipToFile writes the passed clip to a file
func (r *revid) sendClipToFile(clip []byte) error {
r.isSending = true
r.mutex.Lock()
defer r.mutex.Unlock()
_, err := r.outputFile.Write(clip)
if err != nil {
return err
}
r.isSending = false
return nil
}
// sendClipToHTTP takes a clip and an output url and posts through http.
func (r *revid) sendClipToHTTP(clip []byte) error {
r.isSending = true
r.mutex.Lock()
defer r.mutex.Unlock()
timeout := time.Duration(httpTimeOut * time.Second)
client := http.Client{Timeout: timeout}
url := r.config.HttpAddress + strconv.Itoa(len(clip))
@ -496,25 +491,24 @@ func (r *revid) sendClipToHTTP(clip []byte) error {
} else {
r.Log(Error, err.Error())
}
r.isSending = false
return nil
}
// sendClipToFfmpegRtmp sends the clip over the current rtmp connection using
// an ffmpeg process.
func (r *revid) sendClipToFfmpegRtmp(clip []byte) (err error) {
r.isSending = true
r.mutex.Lock()
defer r.mutex.Unlock()
_, err = r.ffmpegStdin.Write(clip)
r.isSending = false
return
}
// sendClipToLibRtmp send the clip over the current rtmp connection using the
// c based librtmp library
func (r *revid) sendClipToLibRtmp(clip []byte) (err error) {
r.isSending = true
r.mutex.Lock()
defer r.mutex.Unlock()
err = r.rtmpInst.WriteFrame(clip, uint(len(clip)))
r.isSending = false
return
}