owncast/core/transcoder/fileWriterReceiverService.go
Gabe Kangas 5214d81264
Codec selection (#892)
* Query for installed codecs

* Start modeling out codecs

* Can now specify a codec and get the correct settings returned from the model

* Return codecs in admin/serverconfig

* Start handling transcoding errors and return messages to user

* filter available codecs against a whitelist

* Fix merge

* Codecs are working

* Switching between codecs work

* Add apis for setting a custom video codec

* Cleanup the logging of transcoder errors

* Add v4l codec

* Add fetching v4l

* Add support for per-codec presets

* Use updated nvenc encoding parameters

* Update log message

* Some more codec WIP

* Turn off v4l. It is a mess.

* Try to make the lowest latency level a bit more playable

* Use a human redable display name in console messages

* Turn on transcoder persistent connections

* Add more codec-related user-facing error messages

* Give the initial offline state transcoder an id

* Force a minimum segment count of 3

* Disable qsv for now. set x264 specific params in VariantFlags

* Close body in case

* Ignore vbv underflow message, it is not actionable

* Determine a dynamic gop value based on the length of segments

* Add codec-specific tests

* Cleanup

* Ignore goconst lint warnings in codec file

* Troubleshoot omx

* Add more codec tests

* Remove no longer accurate comment

* Bundle admin from codec branch

* Revert back to old setting

* Cleanup list of codecs a bit

* Remove old references to the encoder preset

* Commit updated API documentation

* Update admin bundle

* Commit updated API documentation

* Add codec setting to api spec

* Commit updated API documentation

Co-authored-by: Owncast <owncast@owncast.online>
2021-04-15 13:55:51 -07:00

100 lines
2.6 KiB
Go

package transcoder
import (
"bytes"
"io"
"net"
"os"
"path/filepath"
"strings"
"net/http"
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/utils"
log "github.com/sirupsen/logrus"
)
// FileWriterReceiverServiceCallback are to be fired when transcoder responses are written to disk.
type FileWriterReceiverServiceCallback interface {
SegmentWritten(localFilePath string)
VariantPlaylistWritten(localFilePath string)
MasterPlaylistWritten(localFilePath string)
}
// FileWriterReceiverService accepts transcoder responses via HTTP and fires the callbacks.
type FileWriterReceiverService struct {
callbacks FileWriterReceiverServiceCallback
}
// SetupFileWriterReceiverService will start listening for transcoder responses.
func (s *FileWriterReceiverService) SetupFileWriterReceiverService(callbacks FileWriterReceiverServiceCallback) {
s.callbacks = callbacks
httpServer := http.NewServeMux()
httpServer.HandleFunc("/", s.uploadHandler)
localListenerAddress := "127.0.0.1:0"
// go func() {
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)
go func() {
if err := http.Serve(listener, httpServer); err != nil {
log.Fatalln("Unable to start internal video writing service", err)
}
}()
}
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
writePath := filepath.Join(config.PrivateHLSStoragePath, path)
var buf bytes.Buffer
defer r.Body.Close()
_, _ = io.Copy(&buf, r.Body)
data := buf.Bytes()
f, err := os.Create(writePath)
if err != nil {
returnError(err, w)
return
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
returnError(err, w)
return
}
s.fileWritten(writePath)
w.WriteHeader(http.StatusOK)
}
func (s *FileWriterReceiverService) fileWritten(path string) {
if utils.GetRelativePathFromAbsolutePath(path) == "hls/stream.m3u8" {
s.callbacks.MasterPlaylistWritten(path)
} else if strings.HasSuffix(path, ".ts") {
s.callbacks.SegmentWritten(path)
} else if strings.HasSuffix(path, ".m3u8") {
s.callbacks.VariantPlaylistWritten(path)
}
}
func returnError(err error, w http.ResponseWriter) {
log.Debugln(err)
http.Error(w, http.StatusText(http.StatusInternalServerError)+": "+err.Error(), http.StatusInternalServerError)
}