added mutex to SaveStreamInstant to prevent weirdness

This commit is contained in:
BroodjeAap 2020-07-09 19:03:38 +00:00
parent 8f4d46bc3f
commit 69d30587cb

View file

@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path/filepath"
"sync"
"time"
"gocv.io/x/gocv"
@ -18,6 +19,7 @@ type Stream struct {
URL string `json:"url"`
Base64 string `json:"base64"`
Interval int `json:"interval"`
FileLock sync.Mutex
}
// NewStream creates a new Stream Object
@ -80,6 +82,7 @@ func (s Stream) GetStreamInstant() gocv.Mat {
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
func (s Stream) SaveStreamInstant(mat gocv.Mat) {
s.FileLock.Lock()
streamStoreDir := s.GetStreamStoreDirPath()
os.MkdirAll(streamStoreDir, os.ModePerm)
@ -95,6 +98,7 @@ func (s Stream) SaveStreamInstant(mat gocv.Mat) {
if swap {
s.SwapInstants(s.GetPreviousInstantPath(), s.GetCurrentInstantPath(), s.GetNextInstantPath())
}
s.FileLock.Unlock()
}
// SwapInstants swaps the file location, first current -> previous and then next -> current