Created isSending flag

This commit is contained in:
Saxon Milton 2018-05-07 12:23:50 +09:30
parent 098cc19c60
commit 47125ef281
1 changed files with 20 additions and 2 deletions

View File

@ -75,6 +75,7 @@ const (
clipSizeThreshold = 11
rtmpConnectionMaxTries = 5
raspividNoOfTries = 3
sendingWaitTime = time.Duration(5)*time.Millisecond
)
// Log Types
@ -103,6 +104,7 @@ type revid struct {
ringBuffer ringbuffer.RingBuffer
config Config
isRunning bool
isSending bool
outputFile *os.File
inputFile *os.File
generator generator.Generator
@ -134,6 +136,8 @@ func NewRevid(config Config) (r *revid, err error) {
return
}
r.outputChan = make(chan []byte, outputChanSize)
r.isSending = false
r.isRunning = false
return
}
@ -273,12 +277,18 @@ func (r *revid) Start() {
// 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()
r.isRunning = false
@ -465,15 +475,18 @@ func (r *revid) outputClips() {
// senClipToFile writes the passed clip to a file
func (r *revid) sendClipToFile(clip []byte) error {
r.isSending = true
_, 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
timeout := time.Duration(httpTimeOut * time.Second)
client := http.Client{Timeout: timeout}
url := r.config.HttpAddress + strconv.Itoa(len(clip))
@ -489,20 +502,25 @@ 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
_, 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;
err = r.rtmpInst.WriteFrame(clip, uint(len(clip)))
r.isSending = false
return
}