Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
Attempt with Joy5
Gabe Kangas4ad43f6e0b68
diff --git a/core/rtmp/rtmp.go b/core/rtmp/rtmp.goindex f0a39ccb3e..795cc3900a 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 1935func 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()+ }++ streamingKeyComponents := strings.Split(conn.URL.Path, "/")+ streamingKey := streamingKeyComponents[len(streamingKeyComponents)-1]++ if streamingKey != config.Config.VideoSettings.StreamingKey {+ log.Errorln("invalid streaming key; rejecting incoming stream")+ nc.Close()+ return+ }++ // 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)+ }- return conn, &yutmp.ConnConfig{- Handler: &Handler{},+ defer nc.Close()- ControlState: yutmp.StreamControlStateConfig{- DefaultBandwidthWindowSize: 6 * 1024 * 1024 / 8,- },+ transcoder := ffmpeg.NewTranscoder()+ go transcoder.Start()- Logger: l,+ for {+ pkt, err := conn.ReadPacket()+ if err != nil {+ log.Errorln(err)++ if err == io.EOF {+ _isConnected = false+ core.SetStreamAsDisconnected()+ break+ }++ return+ }++ // err = muxer.WriteFileHeader()+ // if err != nil {+ // log.Errorln(err)+ // }++ err = muxer.WritePacket(pkt)+ if err != nil {+ log.Errorln(err)}- },- })- 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)+ }}++ 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 notdiff --git a/go.mod b/go.modindex 06f241d5cf..1f6909dd54 100644--- a/go.mod+++ b/go.mod@@ -3,6 +3,7 @@ module github.com/gabek/owncastgo 1.14require (+ github.com/Seize/joy4 v0.0.8github.com/aws/aws-sdk-go v1.32.1github.com/ipfs/go-ipfs v0.5.1github.com/ipfs/go-ipfs-config v0.5.3@@ -12,6 +13,8 @@ require (github.com/libp2p/go-libp2p-peerstore v0.2.6github.com/mssola/user_agent v0.5.2github.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-6c2a804a2816github.com/radovskyb/watcher v1.0.7github.com/sirupsen/logrus v1.6.0github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cfdiff --git a/go.sum b/go.sumindex 995d1a9e32..99f7bf9a36 100644--- a/go.sum+++ b/go.sum@@ -18,6 +18,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03github.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+B7PbrXRPxHRPuXSgithub.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.jsindex 73a6aa9ce0..def48bc00b 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 hackingswindow.VIDEOJS_NO_DYNAMIC_STYLE = true;