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 clipSizeThreshold = 11
rtmpConnectionMaxTries = 5 rtmpConnectionMaxTries = 5
raspividNoOfTries = 3 raspividNoOfTries = 3
sendingWaitTime = time.Duration(5)*time.Millisecond
) )
// Log Types // Log Types
@ -103,6 +104,7 @@ type revid struct {
ringBuffer ringbuffer.RingBuffer ringBuffer ringbuffer.RingBuffer
config Config config Config
isRunning bool isRunning bool
isSending bool
outputFile *os.File outputFile *os.File
inputFile *os.File inputFile *os.File
generator generator.Generator generator generator.Generator
@ -134,6 +136,8 @@ func NewRevid(config Config) (r *revid, err error) {
return return
} }
r.outputChan = make(chan []byte, outputChanSize) r.outputChan = make(chan []byte, outputChanSize)
r.isSending = false
r.isRunning = false
return return
} }
@ -273,12 +277,18 @@ func (r *revid) Start() {
// Stop halts any processing of video data from a camera or file // Stop halts any processing of video data from a camera or file
func (r *revid) Stop() { func (r *revid) Stop() {
r.mutex.Lock()
defer r.mutex.Unlock()
if !r.isRunning { if !r.isRunning {
r.Log(Warning, "revid.Stop() called but revid not running!") r.Log(Warning, "revid.Stop() called but revid not running!")
return 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.Log(Info, "Stopping revid!")
r.rtmpInst.EndSession() r.rtmpInst.EndSession()
r.isRunning = false r.isRunning = false
@ -465,15 +475,18 @@ func (r *revid) outputClips() {
// senClipToFile writes the passed clip to a file // senClipToFile writes the passed clip to a file
func (r *revid) sendClipToFile(clip []byte) error { func (r *revid) sendClipToFile(clip []byte) error {
r.isSending = true
_, err := r.outputFile.Write(clip) _, err := r.outputFile.Write(clip)
if err != nil { if err != nil {
return err return err
} }
r.isSending = false
return nil return nil
} }
// sendClipToHTTP takes a clip and an output url and posts through http. // sendClipToHTTP takes a clip and an output url and posts through http.
func (r *revid) sendClipToHTTP(clip []byte) error { func (r *revid) sendClipToHTTP(clip []byte) error {
r.isSending = true
timeout := time.Duration(httpTimeOut * time.Second) timeout := time.Duration(httpTimeOut * time.Second)
client := http.Client{Timeout: timeout} client := http.Client{Timeout: timeout}
url := r.config.HttpAddress + strconv.Itoa(len(clip)) url := r.config.HttpAddress + strconv.Itoa(len(clip))
@ -489,20 +502,25 @@ func (r *revid) sendClipToHTTP(clip []byte) error {
} else { } else {
r.Log(Error, err.Error()) r.Log(Error, err.Error())
} }
r.isSending = false
return nil return nil
} }
// sendClipToFfmpegRtmp sends the clip over the current rtmp connection using // sendClipToFfmpegRtmp sends the clip over the current rtmp connection using
// an ffmpeg process. // an ffmpeg process.
func (r *revid) sendClipToFfmpegRtmp(clip []byte) (err error) { func (r *revid) sendClipToFfmpegRtmp(clip []byte) (err error) {
r.isSending = true
_, err = r.ffmpegStdin.Write(clip) _, err = r.ffmpegStdin.Write(clip)
r.isSending = false
return return
} }
// sendClipToLibRtmp send the clip over the current rtmp connection using the // sendClipToLibRtmp send the clip over the current rtmp connection using the
// c based librtmp library // c based librtmp library
func (r *revid) sendClipToLibRtmp(clip []byte) (err error) { func (r *revid) sendClipToLibRtmp(clip []byte) (err error) {
r.isSending = true;
err = r.rtmpInst.WriteFrame(clip, uint(len(clip))) err = r.rtmpInst.WriteFrame(clip, uint(len(clip)))
r.isSending = false
return return
} }