Allow transcoder to run concurently

This commit is contained in:
Zoe Roux 2024-01-12 01:17:27 +01:00
parent 8805f0f804
commit 68304af99e
7 changed files with 48 additions and 23 deletions

View File

@ -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() {

View File

@ -1,4 +1,4 @@
package transcoder
package src
import (
"math"

View File

@ -1,4 +1,4 @@
package transcoder
package src
type Quality int8

View File

@ -1,4 +1,4 @@
package transcoder
package src
type TranscodeStream struct {
File FileStream

View File

@ -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
}

BIN
transcoder/transcoder Executable file

Binary file not shown.

View File

@ -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
}