From 43d434747bf277c7e056ad531147ac4d71564f7d Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Thu, 2 Jul 2020 13:24:22 -0700 Subject: [PATCH] Attempt with Joy5 --- core/rtmp/rtmp.go | 122 +++++++++++++++++++++++++++++------- go.mod | 3 + go.sum | 6 ++ webroot/js/player/player.js | 3 + 4 files changed, 112 insertions(+), 22 deletions(-) diff --git a/core/rtmp/rtmp.go b/core/rtmp/rtmp.go index f0a39ccb3..795cc3900 100644 --- a/core/rtmp/rtmp.go +++ b/core/rtmp/rtmp.go @@ -4,51 +4,129 @@ import ( "fmt" "io" "net" + "os" + "strings" + "syscall" + "time" + + "github.com/gabek/owncast/config" + "github.com/gabek/owncast/core" + "github.com/gabek/owncast/core/ffmpeg" + "github.com/gabek/owncast/utils" + + "github.com/nareix/joy5/format/flv" + "github.com/nareix/joy5/format/rtmp" log "github.com/sirupsen/logrus" - yutmp "github.com/yutopp/go-rtmp" ) var ( //IsConnected whether there is a connection or not _isConnected = false + pipePath = utils.GetTemporaryPipePath() + filePipe *os.File ) //Start starts the rtmp service, listening on port 1935 func Start() { port := 1935 - tcpAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", port)) - if err != nil { - log.Panicf("Failed to resolve the tcp address for the rtmp service: %+v", err) + server := rtmp.NewServer() + + server.LogEvent = func(conn *rtmp.Conn, nc net.Conn, e int) { + log.Errorln("RTMP status:", rtmp.EventString[e]) } - listener, err := net.ListenTCP("tcp", tcpAddr) - if err != nil { - log.Panicf("Failed to acquire the tcp listener: %+v", err) + server.OnNewConn = func(conn *rtmp.Conn) { + log.Println("OnNewConn!", conn.FlashVer) } - srv := yutmp.NewServer(&yutmp.ServerConfig{ - OnConnect: func(conn net.Conn) (io.ReadWriteCloser, *yutmp.ConnConfig) { - l := log.StandardLogger() - l.SetLevel(log.WarnLevel) + server.HandleConn = func(conn *rtmp.Conn, nc net.Conn) { + if _isConnected { + log.Errorln("stream already running; can not overtake an existing stream") + nc.Close() + } - return conn, &yutmp.ConnConfig{ - Handler: &Handler{}, + streamingKeyComponents := strings.Split(conn.URL.Path, "/") + streamingKey := streamingKeyComponents[len(streamingKeyComponents)-1] - ControlState: yutmp.StreamControlStateConfig{ - DefaultBandwidthWindowSize: 6 * 1024 * 1024 / 8, - }, + if streamingKey != config.Config.VideoSettings.StreamingKey { + log.Errorln("invalid streaming key; rejecting incoming stream") + nc.Close() + return + } - Logger: l, + // Record streams as FLV + syscall.Mkfifo(pipePath, 0666) + file, err := os.OpenFile(pipePath, os.O_RDWR, os.ModeNamedPipe) + if err != nil { + log.Panicln(err) + } + + filePipe = file + fmt.Println(pipePath) + + muxer := flv.NewMuxer(filePipe) + + _isConnected = true + core.SetStreamAsConnected() + + defer filePipe.Close() + + if err != nil { + panic(err) + } + + defer nc.Close() + + transcoder := ffmpeg.NewTranscoder() + go transcoder.Start() + + for { + pkt, err := conn.ReadPacket() + if err != nil { + log.Errorln(err) + + if err == io.EOF { + _isConnected = false + core.SetStreamAsDisconnected() + break + } + + return } - }, - }) - log.Infof("RTMP server is listening for incoming stream on port: %d", port) - if err := srv.Serve(listener); err != nil { - log.Panicf("Failed to serve the rtmp service: %+v", err) + // err = muxer.WriteFileHeader() + // if err != nil { + // log.Errorln(err) + // } + + err = muxer.WritePacket(pkt) + if err != nil { + log.Errorln(err) + } + + } } + + var lis net.Listener + var err error + if lis, err = net.Listen("tcp", fmt.Sprintf(":%d", port)); err != nil { + return + } + log.Printf("RTMP server is listening for incoming stream on port: %d", port) + + go func() { + for { + nc, err := lis.Accept() + if err != nil { + time.Sleep(time.Second) + continue + } + go server.HandleNetConn(nc) + } + }() + } //IsConnected gets whether there is an rtmp connection or not diff --git a/go.mod b/go.mod index 06f241d5c..1f6909dd5 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/gabek/owncast go 1.14 require ( + github.com/Seize/joy4 v0.0.8 github.com/aws/aws-sdk-go v1.32.1 github.com/ipfs/go-ipfs v0.5.1 github.com/ipfs/go-ipfs-config v0.5.3 @@ -12,6 +13,8 @@ require ( github.com/libp2p/go-libp2p-peerstore v0.2.6 github.com/mssola/user_agent v0.5.2 github.com/multiformats/go-multiaddr v0.2.2 + github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369 // indirect + github.com/nareix/joy5 v0.0.0-20200409150540-6c2a804a2816 github.com/radovskyb/watcher v1.0.7 github.com/sirupsen/logrus v1.6.0 github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf diff --git a/go.sum b/go.sum index 995d1a9e3..99f7bf9a3 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/Kubuxu/go-os-helper v0.0.1/go.mod h1:N8B+I7vPCT80IcP58r50u4+gEEcsZETFUpAzWW2ep1Y= github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/Seize/joy4 v0.0.8 h1:wG7awuz+v4LIyJlPiuXYt2fcH4dEj2MC5Ib4fPd05NI= +github.com/Seize/joy4 v0.0.8/go.mod h1:l2OVEo5xnZOOkIiqh3Jqku/pTBOGp/tTYTJue78zpO0= github.com/Stebalien/go-bitfield v0.0.0-20180330043415-076a62f9ce6e/go.mod h1:3oM7gXIttpYDAJXpVNnSCiUMYBLIZ6cb1t+Ip982MRo= github.com/Stebalien/go-bitfield v0.0.1 h1:X3kbSSPUaJK60wV2hjOPZwmpljr6VGCqdq4cBLhbQBo= github.com/Stebalien/go-bitfield v0.0.1/go.mod h1:GNjFpasyUVkHMsfEOk8EFLJ9syQ6SI+XWrX9Wf2XH0s= @@ -806,6 +808,10 @@ github.com/multiformats/go-varint v0.0.2/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXS github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg= github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369 h1:Yp0zFEufLz0H7jzffb4UPXijavlyqlYeOg7dcyVUNnQ= +github.com/nareix/joy4 v0.0.0-20200507095837-05a4ffbb5369/go.mod h1:aFJ1ZwLjvHN4yEzE5Bkz8rD8/d8Vlj3UIuvz2yfET7I= +github.com/nareix/joy5 v0.0.0-20200409150540-6c2a804a2816 h1:YXL1GIHlL39qw2Eh8IQQIuSRwhb/lq3fMNODosQA6ic= +github.com/nareix/joy5 v0.0.0-20200409150540-6c2a804a2816/go.mod h1:KQWqnCy0MEAaiqq/A/1M4ZUAs1mv6y3tGN8ICkcyNms= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/webroot/js/player/player.js b/webroot/js/player/player.js index 73a6aa9ce..def48bc00 100644 --- a/webroot/js/player/player.js +++ b/webroot/js/player/player.js @@ -1,3 +1,6 @@ +// const streamURL = '/hls/stream.m3u8'; +const streamURL = '/hls/stream.m3u8'; // Uncomment me to point to remote video + // style hackings window.VIDEOJS_NO_DYNAMIC_STYLE = true;