diff --git a/transcoder/main.go b/transcoder/main.go index 0a47cb33..2e4c37d6 100644 --- a/transcoder/main.go +++ b/transcoder/main.go @@ -3,7 +3,7 @@ package main import ( "net/http" - "github.com/zoriya/kyoo/transcoder/transcoder" + "github.com/zoriya/kyoo/transcoder/src" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -55,7 +55,7 @@ func (h *Handler) GetMaster(c echo.Context) error { } type Handler struct { - transcoder *transcoder.Transcoder + transcoder *src.Transcoder } func main() { diff --git a/transcoder/transcoder/filestream.go b/transcoder/src/filestream.go similarity index 99% rename from transcoder/transcoder/filestream.go rename to transcoder/src/filestream.go index 3599a02d..f0d82226 100644 --- a/transcoder/transcoder/filestream.go +++ b/transcoder/src/filestream.go @@ -1,4 +1,4 @@ -package transcoder +package src import ( "math" diff --git a/transcoder/transcoder/quality.go b/transcoder/src/quality.go similarity index 95% rename from transcoder/transcoder/quality.go rename to transcoder/src/quality.go index 09305c6b..22ba101d 100644 --- a/transcoder/transcoder/quality.go +++ b/transcoder/src/quality.go @@ -1,4 +1,4 @@ -package transcoder +package src type Quality int8 diff --git a/transcoder/transcoder/stream.go b/transcoder/src/stream.go similarity index 91% rename from transcoder/transcoder/stream.go rename to transcoder/src/stream.go index 935aab8c..1ce67953 100644 --- a/transcoder/transcoder/stream.go +++ b/transcoder/src/stream.go @@ -1,4 +1,4 @@ -package transcoder +package src type TranscodeStream struct { File FileStream diff --git a/transcoder/src/transcoder.go b/transcoder/src/transcoder.go new file mode 100644 index 00000000..5e13dc4d --- /dev/null +++ b/transcoder/src/transcoder.go @@ -0,0 +1,43 @@ +package src + +import ( + "sync" +) + +type Transcoder struct { + // All file streams currently running, index is file path + streams map[string]FileStream + // Streams that are staring up + preparing map[string]bool + + mutex sync.RWMutex + channel chan *FileStream +} + +func (t *Transcoder) GetMaster(path string, client string) (string, error) { + t.mutex.RLock() + stream, ok := t.streams[path] + preparing := t.preparing[path] + t.mutex.RUnlock() + + if preparing { + stream = *<- t.channel + } else if !ok { + t.mutex.Lock() + t.preparing[path] = true + t.mutex.Unlock() + + stream, err := NewFileStream(path) + if err != nil { + return "", err + } + + t.mutex.Lock() + t.streams[path] = *stream + delete(t.preparing, path) + t.mutex.Unlock() + + t.channel <- stream + } + return stream.GetMaster(), nil +} diff --git a/transcoder/transcoder b/transcoder/transcoder new file mode 100755 index 00000000..c830f402 Binary files /dev/null and b/transcoder/transcoder differ diff --git a/transcoder/transcoder/transcoder.go b/transcoder/transcoder/transcoder.go deleted file mode 100644 index 9af4e254..00000000 --- a/transcoder/transcoder/transcoder.go +++ /dev/null @@ -1,18 +0,0 @@ -package transcoder - -type Transcoder struct { - // All file streams currently running, index is file path - streams map[string]FileStream -} - -func (t *Transcoder) GetMaster(path string, client string) (string, error) { - stream, ok := t.streams[path] - if !ok { - stream, err := NewFileStream(path) - if err != nil { - return "", err - } - t.streams[path] = *stream - } - return stream.GetMaster(), nil -}