2021-02-19 10:05:52 +03:00
|
|
|
package transcoder
|
2020-10-15 00:07:38 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2021-02-19 10:05:52 +03:00
|
|
|
"net"
|
2022-01-28 01:58:31 +03:00
|
|
|
"net/http"
|
2020-10-15 00:07:38 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"github.com/owncast/owncast/utils"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// FileWriterReceiverServiceCallback are to be fired when transcoder responses are written to disk.
|
2020-10-15 00:07:38 +03:00
|
|
|
type FileWriterReceiverServiceCallback interface {
|
|
|
|
SegmentWritten(localFilePath string)
|
|
|
|
VariantPlaylistWritten(localFilePath string)
|
|
|
|
MasterPlaylistWritten(localFilePath string)
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// FileWriterReceiverService accepts transcoder responses via HTTP and fires the callbacks.
|
2023-03-28 01:57:53 +03:00
|
|
|
// It is intended to be the middleman between the transcoder and the storage provider and allows
|
|
|
|
// the transcoder process to be completely isolated and even run remotely in the future, as long
|
|
|
|
// as it can send HTTP requests to this service with the results.
|
2020-10-15 00:07:38 +03:00
|
|
|
type FileWriterReceiverService struct {
|
|
|
|
callbacks FileWriterReceiverServiceCallback
|
2023-08-10 02:19:09 +03:00
|
|
|
streamId string
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// SetupFileWriterReceiverService will start listening for transcoder responses.
|
2020-10-15 00:07:38 +03:00
|
|
|
func (s *FileWriterReceiverService) SetupFileWriterReceiverService(callbacks FileWriterReceiverServiceCallback) {
|
|
|
|
s.callbacks = callbacks
|
|
|
|
|
|
|
|
httpServer := http.NewServeMux()
|
|
|
|
httpServer.HandleFunc("/", s.uploadHandler)
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
localListenerAddress := "127.0.0.1:0"
|
2020-11-12 23:42:18 +03:00
|
|
|
|
2021-04-01 09:08:14 +03:00
|
|
|
listener, err := net.Listen("tcp", localListenerAddress)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Unable to start internal video writing service", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
listenerPort := strings.Split(listener.Addr().String(), ":")[1]
|
|
|
|
config.InternalHLSListenerPort = listenerPort
|
|
|
|
log.Traceln("Transcoder response service listening on: " + listenerPort)
|
2020-11-12 23:42:18 +03:00
|
|
|
go func() {
|
2022-08-27 05:26:51 +03:00
|
|
|
//nolint: gosec
|
2021-02-19 10:05:52 +03:00
|
|
|
if err := http.Serve(listener, httpServer); err != nil {
|
|
|
|
log.Fatalln("Unable to start internal video writing service", err)
|
|
|
|
}
|
|
|
|
}()
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
|
|
|
|
2023-08-10 02:19:09 +03:00
|
|
|
func (s *FileWriterReceiverService) SetStreamID(streamID string) {
|
|
|
|
s.streamId = streamID
|
|
|
|
}
|
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
func (s *FileWriterReceiverService) uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "PUT" {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
path := r.URL.Path
|
2021-09-12 21:32:42 +03:00
|
|
|
writePath := filepath.Join(config.HLSStoragePath, path)
|
2022-01-28 01:58:31 +03:00
|
|
|
f, err := os.Create(writePath) //nolint: gosec
|
2020-10-15 00:07:38 +03:00
|
|
|
if err != nil {
|
2020-11-15 05:39:53 +03:00
|
|
|
returnError(err, w)
|
2020-10-15 00:07:38 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
2022-01-28 07:14:19 +03:00
|
|
|
|
|
|
|
if _, err := io.Copy(f, r.Body); err != nil {
|
2020-11-15 05:39:53 +03:00
|
|
|
returnError(err, w)
|
2020-10-15 00:07:38 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.fileWritten(writePath)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileWriterReceiverService) fileWritten(path string) {
|
2023-08-10 02:19:09 +03:00
|
|
|
if utils.GetRelativePathFromAbsolutePath(path) == s.streamId+"/stream.m3u8" {
|
2020-10-15 00:07:38 +03:00
|
|
|
s.callbacks.MasterPlaylistWritten(path)
|
|
|
|
} else if strings.HasSuffix(path, ".ts") {
|
|
|
|
s.callbacks.SegmentWritten(path)
|
|
|
|
} else if strings.HasSuffix(path, ".m3u8") {
|
|
|
|
s.callbacks.VariantPlaylistWritten(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 05:39:53 +03:00
|
|
|
func returnError(err error, w http.ResponseWriter) {
|
2021-02-19 10:05:52 +03:00
|
|
|
log.Debugln(err)
|
2020-10-15 00:07:38 +03:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError)+": "+err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|