mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Restructure ffmpeg (#2392)
* Refactor transcode generation * Move phash generation into separate package * Refactor image thumbnail generation * Move JSONTime to separate package * Ffmpeg refactoring * Refactor live transcoding * Refactor scene marker preview generation * Refactor preview generation * Refactor screenshot generation * Refactor sprite generation * Change ffmpeg.IsStreamable to return error * Move frame rate calculation into ffmpeg * Refactor file locking * Refactor title set during scan * Add missing lockmanager instance * Return error instead of logging in MatchContainer
This commit is contained in:
@@ -2,51 +2,16 @@ package manager
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/stashapp/stash/internal/manager/config"
|
||||
"github.com/stashapp/stash/pkg/ffmpeg"
|
||||
"github.com/stashapp/stash/pkg/fsutil"
|
||||
"github.com/stashapp/stash/pkg/logger"
|
||||
"github.com/stashapp/stash/pkg/models"
|
||||
"github.com/stashapp/stash/pkg/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
streamingFiles = make(map[string][]*http.ResponseWriter)
|
||||
streamingFilesMutex = sync.RWMutex{}
|
||||
)
|
||||
|
||||
func RegisterStream(filepath string, w *http.ResponseWriter) {
|
||||
streamingFilesMutex.Lock()
|
||||
streams := streamingFiles[filepath]
|
||||
streamingFiles[filepath] = append(streams, w)
|
||||
streamingFilesMutex.Unlock()
|
||||
}
|
||||
|
||||
func deregisterStream(filepath string, w *http.ResponseWriter) {
|
||||
streamingFilesMutex.Lock()
|
||||
defer streamingFilesMutex.Unlock()
|
||||
streams := streamingFiles[filepath]
|
||||
|
||||
for i, v := range streams {
|
||||
if v == w {
|
||||
streamingFiles[filepath] = append(streams[:i], streams[i+1:]...)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WaitAndDeregisterStream(filepath string, w *http.ResponseWriter, r *http.Request) {
|
||||
notify := r.Context().Done()
|
||||
go func() {
|
||||
<-notify
|
||||
deregisterStream(filepath, w)
|
||||
}()
|
||||
}
|
||||
|
||||
func KillRunningStreams(scene *models.Scene, fileNamingAlgo models.HashAlgorithm) {
|
||||
killRunningStreams(scene.Path)
|
||||
instance.ReadLockManager.Cancel(scene.Path)
|
||||
|
||||
sceneHash := scene.GetHash(fileNamingAlgo)
|
||||
|
||||
@@ -55,32 +20,7 @@ func KillRunningStreams(scene *models.Scene, fileNamingAlgo models.HashAlgorithm
|
||||
}
|
||||
|
||||
transcodePath := GetInstance().Paths.Scene.GetTranscodePath(sceneHash)
|
||||
killRunningStreams(transcodePath)
|
||||
}
|
||||
|
||||
func killRunningStreams(path string) {
|
||||
ffmpeg.KillRunningEncoders(path)
|
||||
|
||||
streamingFilesMutex.RLock()
|
||||
streams := streamingFiles[path]
|
||||
streamingFilesMutex.RUnlock()
|
||||
|
||||
for _, w := range streams {
|
||||
hj, ok := (*w).(http.Hijacker)
|
||||
if !ok {
|
||||
// if we can't close the connection can't really do anything else
|
||||
logger.Warnf("cannot close running stream for: %s", path)
|
||||
return
|
||||
}
|
||||
|
||||
// hijack and close the connection
|
||||
conn, _, err := hj.Hijack()
|
||||
if err != nil {
|
||||
logger.Errorf("cannot close running stream for '%s' due to error: %s", path, err.Error())
|
||||
} else {
|
||||
conn.Close()
|
||||
}
|
||||
}
|
||||
instance.ReadLockManager.Cancel(transcodePath)
|
||||
}
|
||||
|
||||
type SceneServer struct {
|
||||
@@ -91,9 +31,9 @@ func (s *SceneServer) StreamSceneDirect(scene *models.Scene, w http.ResponseWrit
|
||||
fileNamingAlgo := config.GetInstance().GetVideoFileNamingAlgorithm()
|
||||
|
||||
filepath := GetInstance().Paths.Scene.GetStreamPath(scene.Path, scene.GetHash(fileNamingAlgo))
|
||||
RegisterStream(filepath, &w)
|
||||
lockCtx := GetInstance().ReadLockManager.ReadLock(r.Context(), filepath)
|
||||
defer lockCtx.Cancel()
|
||||
http.ServeFile(w, r, filepath)
|
||||
WaitAndDeregisterStream(filepath, &w, r)
|
||||
}
|
||||
|
||||
func (s *SceneServer) ServeScreenshot(scene *models.Scene, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user