2020-10-14 23:07:38 +02:00
|
|
|
package storageproviders
|
|
|
|
|
|
|
|
import (
|
2023-05-31 20:10:04 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2020-10-14 23:07:38 +02:00
|
|
|
|
2023-05-31 20:10:04 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-10-14 23:07:38 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/owncast/owncast/config"
|
2023-05-30 23:05:24 +02:00
|
|
|
"github.com/owncast/owncast/core/data"
|
2020-10-14 23:07:38 +02:00
|
|
|
)
|
|
|
|
|
2021-09-12 09:18:15 +02:00
|
|
|
// LocalStorage represents an instance of the local storage provider for HLS video.
|
2023-09-22 02:58:02 +02:00
|
|
|
type LocalStorage struct {
|
|
|
|
host string
|
|
|
|
}
|
2020-10-14 23:07:38 +02:00
|
|
|
|
2021-10-12 01:29:36 +02:00
|
|
|
// NewLocalStorage returns a new LocalStorage instance.
|
|
|
|
func NewLocalStorage() *LocalStorage {
|
|
|
|
return &LocalStorage{}
|
|
|
|
}
|
2020-10-16 02:50:17 +02:00
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// Setup configures this storage provider.
|
2020-10-14 23:07:38 +02:00
|
|
|
func (s *LocalStorage) Setup() error {
|
2023-09-22 02:58:02 +02:00
|
|
|
s.host = data.GetVideoServingEndpoint()
|
2020-10-14 23:07:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// SegmentWritten is called when a single segment of video is written.
|
2020-10-14 23:07:38 +02:00
|
|
|
func (s *LocalStorage) SegmentWritten(localFilePath string) {
|
2021-07-09 20:16:44 +02:00
|
|
|
if _, err := s.Save(localFilePath, 0); err != nil {
|
2020-11-15 03:39:53 +01:00
|
|
|
log.Warnln(err)
|
|
|
|
}
|
2020-10-14 23:07:38 +02:00
|
|
|
}
|
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// VariantPlaylistWritten is called when a variant hls playlist is written.
|
2020-10-14 23:07:38 +02:00
|
|
|
func (s *LocalStorage) VariantPlaylistWritten(localFilePath string) {
|
2021-07-09 20:16:44 +02:00
|
|
|
if _, err := s.Save(localFilePath, 0); err != nil {
|
2020-10-17 00:04:31 +02:00
|
|
|
log.Errorln(err)
|
2020-10-14 23:07:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// MasterPlaylistWritten is called when the master hls playlist is written.
|
2020-10-14 23:07:38 +02:00
|
|
|
func (s *LocalStorage) MasterPlaylistWritten(localFilePath string) {
|
2023-09-22 02:58:02 +02:00
|
|
|
// If we're using a remote serving endpoint, we need to rewrite the master playlist
|
|
|
|
if s.host != "" {
|
|
|
|
if err := rewritePlaylistLocations(localFilePath, s.host, ""); err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := s.Save(localFilePath, 0); err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
}
|
2020-11-15 03:39:53 +01:00
|
|
|
}
|
2020-10-14 23:07:38 +02:00
|
|
|
}
|
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// Save will save a local filepath using the storage provider.
|
2020-10-14 23:07:38 +02:00
|
|
|
func (s *LocalStorage) Save(filePath string, retryCount int) (string, error) {
|
2021-09-12 20:32:42 +02:00
|
|
|
return filePath, nil
|
2020-10-14 23:07:38 +02:00
|
|
|
}
|
2023-05-31 20:10:04 +02:00
|
|
|
|
|
|
|
func (s *LocalStorage) Cleanup() error {
|
|
|
|
// Determine how many files we should keep on disk
|
|
|
|
maxNumber := data.GetStreamLatencyLevel().SegmentCount
|
|
|
|
buffer := 10
|
|
|
|
baseDirectory := config.HLSStoragePath
|
|
|
|
|
|
|
|
files, err := getAllFilesRecursive(baseDirectory)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "unable find old video files for cleanup")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete old private HLS files on disk
|
|
|
|
for directory := range files {
|
|
|
|
files := files[directory]
|
|
|
|
if len(files) < maxNumber+buffer {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
filesToDelete := files[maxNumber+buffer:]
|
|
|
|
log.Traceln("Deleting", len(filesToDelete), "old files from", baseDirectory, "for video variant", directory)
|
|
|
|
|
|
|
|
for _, file := range filesToDelete {
|
|
|
|
fileToDelete := filepath.Join(baseDirectory, directory, file.Name())
|
|
|
|
err := os.Remove(fileToDelete)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "unable to delete old video files")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAllFilesRecursive(baseDirectory string) (map[string][]os.FileInfo, error) {
|
|
|
|
files := make(map[string][]os.FileInfo)
|
|
|
|
|
|
|
|
var directory string
|
|
|
|
err := filepath.Walk(baseDirectory, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
directory = info.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.Ext(info.Name()) == ".ts" {
|
|
|
|
files[directory] = append(files[directory], info)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort by date so we can delete old files
|
|
|
|
for directory := range files {
|
|
|
|
sort.Slice(files[directory], func(i, j int) bool {
|
|
|
|
return files[directory][i].ModTime().UnixNano() > files[directory][j].ModTime().UnixNano()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|