diff --git a/transcoder/src/filestream.go b/transcoder/src/filestream.go index f0d82226..3cd17901 100644 --- a/transcoder/src/filestream.go +++ b/transcoder/src/filestream.go @@ -90,10 +90,15 @@ func GetKeyframes(path string) ([]float64, bool, error) { func (fs *FileStream) IsDead() bool { for _, s := range fs.streams { if len(s.Clients) > 0 { - return true + return false } } - return false + // TODO: Also check how long this stream has been unused. We dont want to kill streams created 2min ago + return true +} + +func (fs *FileStream) Destroy() { + // TODO: kill child process and delete data } func (fs *FileStream) GetMaster() string { diff --git a/transcoder/src/transcoder.go b/transcoder/src/transcoder.go index acd74a91..93a91fb5 100644 --- a/transcoder/src/transcoder.go +++ b/transcoder/src/transcoder.go @@ -30,6 +30,7 @@ func (t *Transcoder) GetMaster(path string, client string) (string, error) { } else if !ok { t.mutex.Lock() t.preparing[path] = true + t.cleanUnused() t.mutex.Unlock() stream, err := NewFileStream(path) @@ -49,5 +50,17 @@ func (t *Transcoder) GetMaster(path string, client string) (string, error) { t.channel <- stream } + return stream.GetMaster(), nil } + +// This method assume the lock is already taken. +func (t *Transcoder) cleanUnused() { + for path, stream := range t.streams { + if !stream.IsDead() { + continue + } + stream.Destroy() + delete(t.streams, path) + } +}