Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
refactor: use io.Pipe and ffmpeg's stdin (#1148)
Jannikd5ef72b9ca97
diff --git a/core/rtmp/rtmp.go b/core/rtmp/rtmp.goindex 20b3037920..bc089538e2 100644--- a/core/rtmp/rtmp.go+++ b/core/rtmp/rtmp.go@@ -4,8 +4,6 @@ import ("fmt""io""net"- "os"- "syscall""time""github.com/nareix/joy5/format/flv"@@ -15,21 +13,20 @@ import ("github.com/nareix/joy5/format/rtmp""github.com/owncast/owncast/core/data""github.com/owncast/owncast/models"- "github.com/owncast/owncast/utils")var (_hasInboundRTMPConnection = false)-var _pipe *os.File+var _pipe *io.PipeWritervar _rtmpConnection net.Conn-var _setStreamAsConnected func()+var _setStreamAsConnected func(*io.PipeReader)var _setBroadcaster func(models.Broadcaster)// Start starts the rtmp service, listening on specified RTMP port.-func Start(setStreamAsConnected func(), setBroadcaster func(models.Broadcaster)) {+func Start(setStreamAsConnected func(*io.PipeReader), setBroadcaster func(models.Broadcaster)) {_setStreamAsConnected = setStreamAsConnected_setBroadcaster = setBroadcaster@@ -83,27 +80,15 @@ func HandleConn(c *rtmp.Conn, nc net.Conn) {return}+ rtmpOut, rtmpIn := io.Pipe()+ _pipe = rtmpInlog.Infoln("Inbound stream connected.")- _setStreamAsConnected()-- pipePath := utils.GetTemporaryPipePath(fmt.Sprint(data.GetRTMPPortNumber()))- if !utils.DoesFileExists(pipePath) {- err := syscall.Mkfifo(pipePath, 0666)- if err != nil {- log.Fatalln(err)- }- }+ _setStreamAsConnected(rtmpOut)_hasInboundRTMPConnection = true_rtmpConnection = nc- f, err := os.OpenFile(pipePath, os.O_RDWR, os.ModeNamedPipe)- _pipe = f- if err != nil {- log.Fatalln("unable to open", pipePath, "and will exit")- }-- w := flv.NewMuxer(f)+ w := flv.NewMuxer(rtmpIn)for {if !_hasInboundRTMPConnection {diff --git a/core/streamState.go b/core/streamState.goindex d88c498958..0b13f77492 100644--- a/core/streamState.go+++ b/core/streamState.go@@ -3,6 +3,7 @@ package coreimport ("bufio""fmt"+ "io""os""path/filepath""time"@@ -29,7 +30,7 @@ var _onlineCleanupTicker *time.Tickervar _currentBroadcast *models.CurrentBroadcast// setStreamAsConnected sets the stream as connected.-func setStreamAsConnected() {+func setStreamAsConnected(rtmpOut *io.PipeReader) {_stats.StreamConnected = true_stats.LastConnectTime = utils.NullTime{Time: time.Now(), Valid: true}_stats.LastDisconnectTime = utils.NullTime{Time: time.Now(), Valid: false}@@ -65,6 +66,7 @@ func setStreamAsConnected() {_transcoder = nil_currentBroadcast = nil}+ _transcoder.SetStdin(rtmpOut)_transcoder.Start()}()diff --git a/core/transcoder/transcoder.go b/core/transcoder/transcoder.goindex 39b809e5d0..4cb8a47a90 100644--- a/core/transcoder/transcoder.go+++ b/core/transcoder/transcoder.go@@ -3,6 +3,7 @@ package transcoderimport ("bufio""fmt"+ "io""os/exec""strconv""strings"@@ -22,6 +23,7 @@ var _commandExec *exec.Cmd// Transcoder is a single instance of a video transcoder.type Transcoder struct {input string+ stdin *io.PipeReadersegmentOutputPath stringplaylistOutputPath stringvariants []HLSVariant@@ -95,6 +97,11 @@ func (t *Transcoder) Start() {}_commandExec = exec.Command("sh", "-c", command)++ if t.stdin != nil {+ _commandExec.Stdin = t.stdin+ }+stdout, err := _commandExec.StderrPipe()if err != nil {panic(err)@@ -240,7 +247,7 @@ func NewTranscoder() *Transcoder {// Playlists are available via the local HTTP servertranscoder.playlistOutputPath = config.PublicHLSStoragePath- transcoder.input = utils.GetTemporaryPipePath(fmt.Sprint(data.GetRTMPPortNumber()))+ transcoder.input = "pipe:0" // stdinfor index, quality := range transcoder.currentStreamOutputSettings {variant := getVariantFromConfigQuality(quality, index)@@ -389,6 +396,11 @@ func (t *Transcoder) SetInput(input string) {t.input = input}+// SetStdin sets the Stdin of the ffmpeg command.+func (t *Transcoder) SetStdin(rtmp *io.PipeReader) {+ t.stdin = rtmp+}+// SetOutputPath sets the root directory that should include playlists and video segments.func (t *Transcoder) SetOutputPath(output string) {t.segmentOutputPath = outputdiff --git a/utils/utils.go b/utils/utils.goindex 9c4e437be3..b0f3a992ef 100644--- a/utils/utils.go+++ b/utils/utils.go@@ -20,11 +20,6 @@ import ("mvdan.cc/xurls")-// GetTemporaryPipePath gets the temporary path for the streampipe.flv file.-func GetTemporaryPipePath(identifier string) string {- return filepath.Join(os.TempDir(), "streampipe."+identifier)-}-// DoesFileExists checks if the file exists.func DoesFileExists(name string) bool {if _, err := os.Stat(name); err != nil {