2020-05-30 03:08:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2020-06-02 01:53:31 +02:00
|
|
|
"path"
|
2020-05-30 03:08:33 +02:00
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2020-06-02 01:53:31 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-05-30 03:08:33 +02:00
|
|
|
"github.com/radovskyb/watcher"
|
|
|
|
)
|
|
|
|
|
|
|
|
var filesToUpload = make(map[string]string)
|
|
|
|
|
2020-06-03 08:05:15 +02:00
|
|
|
func monitorVideoContent(pathToMonitor string, configuration Config, storage ChunkStorage) {
|
2020-06-03 10:34:05 +02:00
|
|
|
log.Printf("Using %s files...\n", pathToMonitor)
|
2020-06-02 01:53:31 +02:00
|
|
|
|
2020-05-30 03:08:33 +02:00
|
|
|
w := watcher.New()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-w.Event:
|
|
|
|
if event.Op != watcher.Write {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if filepath.Base(event.Path) == "temp.m3u8" {
|
|
|
|
|
2020-06-03 10:34:05 +02:00
|
|
|
for filePath, objectID := range filesToUpload {
|
|
|
|
if objectID != "" {
|
|
|
|
continue
|
2020-06-02 01:53:31 +02:00
|
|
|
}
|
2020-06-03 10:34:05 +02:00
|
|
|
|
|
|
|
newObjectPath := storage.Save(path.Join(configuration.PrivateHLSPath, filePath))
|
|
|
|
filesToUpload[filePath] = newObjectPath
|
2020-05-30 03:08:33 +02:00
|
|
|
}
|
2020-06-02 01:53:31 +02:00
|
|
|
|
2020-05-30 03:08:33 +02:00
|
|
|
playlistBytes, err := ioutil.ReadFile(event.Path)
|
|
|
|
verifyError(err)
|
|
|
|
playlistString := string(playlistBytes)
|
2020-06-01 22:28:14 +02:00
|
|
|
|
2020-06-03 10:34:05 +02:00
|
|
|
playlistString = storage.GenerateRemotePlaylist(playlistString, filesToUpload)
|
2020-06-02 01:53:31 +02:00
|
|
|
writePlaylist(playlistString, path.Join(configuration.PublicHLSPath, "/stream.m3u8"))
|
2020-06-01 22:28:14 +02:00
|
|
|
|
2020-05-30 03:08:33 +02:00
|
|
|
} else if filepath.Ext(event.Path) == ".ts" {
|
2020-06-03 10:34:05 +02:00
|
|
|
filesToUpload[filepath.Base(event.Path)] = ""
|
2020-05-30 03:08:33 +02:00
|
|
|
}
|
|
|
|
case err := <-w.Error:
|
|
|
|
log.Fatalln(err)
|
|
|
|
case <-w.Closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Watch this folder for changes.
|
2020-06-02 01:53:31 +02:00
|
|
|
if err := w.Add(pathToMonitor); err != nil {
|
2020-05-30 03:08:33 +02:00
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2020-06-01 21:15:07 +02:00
|
|
|
if err := w.Start(time.Millisecond * 100); err != nil {
|
2020-05-30 03:08:33 +02:00
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|