owncast/core/rtmp/rtmp.go

149 lines
3 KiB
Go
Raw Normal View History

package rtmp
import (
2020-07-12 02:04:02 +03:00
"fmt"
"io"
"net"
2020-07-02 23:24:22 +03:00
"os"
"strings"
"syscall"
"time"
2020-07-12 02:04:02 +03:00
"unsafe"
2020-07-04 04:48:42 +03:00
2020-07-12 02:04:02 +03:00
"github.com/nareix/joy5/format/flv"
2020-07-14 09:32:35 +03:00
"github.com/nareix/joy5/format/flv/flvio"
2020-07-04 04:48:42 +03:00
log "github.com/sirupsen/logrus"
2020-07-02 23:24:22 +03:00
2020-10-08 05:59:55 +03:00
"github.com/nareix/joy5/format/rtmp"
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/models"
"github.com/owncast/owncast/utils"
)
var (
_hasInboundRTMPConnection = false
)
2020-07-12 02:04:02 +03:00
var _pipe *os.File
var _rtmpConnection net.Conn
2020-07-04 04:48:42 +03:00
var _setStreamAsConnected func()
var _setBroadcaster func(models.Broadcaster)
2020-11-13 02:14:59 +03:00
// Start starts the rtmp service, listening on port 1935.
func Start(setStreamAsConnected func(), setBroadcaster func(models.Broadcaster)) {
_setStreamAsConnected = setStreamAsConnected
_setBroadcaster = setBroadcaster
port := 1935
2020-07-12 02:04:02 +03:00
s := rtmp.NewServer()
var lis net.Listener
var err error
if lis, err = net.Listen("tcp", fmt.Sprintf(":%d", port)); err != nil {
2020-07-12 02:04:02 +03:00
return
}
2020-07-12 02:04:02 +03:00
s.LogEvent = func(c *rtmp.Conn, nc net.Conn, e int) {
es := rtmp.EventString[e]
log.Traceln(unsafe.Pointer(c), nc.LocalAddr(), nc.RemoteAddr(), es)
}
s.HandleConn = HandleConn
2020-07-02 23:24:22 +03:00
if err != nil {
log.Panicln(err)
}
log.Tracef("RTMP server is listening for incoming stream on port: %d", port)
2020-07-12 02:04:02 +03:00
for {
nc, err := lis.Accept()
if err != nil {
time.Sleep(time.Second)
continue
}
go s.HandleNetConn(nc)
}
2020-07-04 04:48:42 +03:00
}
2020-07-12 02:04:02 +03:00
func HandleConn(c *rtmp.Conn, nc net.Conn) {
2020-07-14 09:32:35 +03:00
c.LogTagEvent = func(isRead bool, t flvio.Tag) {
if t.Type == flvio.TAG_AMF0 {
2020-07-15 02:48:41 +03:00
log.Tracef("%+v\n", t.DebugFields())
setCurrentBroadcasterInfo(t, nc.RemoteAddr().String())
2020-07-14 09:32:35 +03:00
}
}
if _hasInboundRTMPConnection {
log.Errorln("stream already running; can not overtake an existing stream")
2020-07-12 02:04:02 +03:00
nc.Close()
return
}
2020-07-04 04:48:42 +03:00
2020-07-12 02:04:02 +03:00
streamingKeyComponents := strings.Split(c.URL.Path, "/")
2020-07-04 04:48:42 +03:00
streamingKey := streamingKeyComponents[len(streamingKeyComponents)-1]
if streamingKey != config.Config.VideoSettings.StreamingKey {
log.Errorln("invalid streaming key; rejecting incoming stream")
2020-07-12 02:04:02 +03:00
nc.Close()
2020-07-04 04:48:42 +03:00
return
}
2020-07-07 08:04:37 +03:00
log.Infoln("Incoming RTMP connected.")
_setStreamAsConnected()
2020-07-04 04:48:42 +03:00
pipePath := utils.GetTemporaryPipePath()
if !utils.DoesFileExists(pipePath) {
err := syscall.Mkfifo(pipePath, 0666)
if err != nil {
log.Fatalln(err)
}
}
2020-07-12 02:04:02 +03:00
_hasInboundRTMPConnection = true
_rtmpConnection = nc
2020-07-02 23:24:22 +03:00
2020-07-12 02:04:02 +03:00
f, err := os.OpenFile(pipePath, os.O_RDWR, os.ModeNamedPipe)
_pipe = f
2020-07-04 04:48:42 +03:00
if err != nil {
panic(err)
}
2020-07-02 23:24:22 +03:00
2020-07-12 02:04:02 +03:00
w := flv.NewMuxer(f)
for {
if !_hasInboundRTMPConnection {
2020-10-08 05:59:55 +03:00
break
}
2020-07-12 02:04:02 +03:00
pkt, err := c.ReadPacket()
2020-07-12 09:54:10 +03:00
if err == io.EOF {
handleDisconnect(nc)
2020-10-08 05:59:55 +03:00
break
}
2020-07-12 02:04:02 +03:00
if err := w.WritePacket(pkt); err != nil {
panic(err)
}
}
}
2020-07-12 02:04:02 +03:00
func handleDisconnect(conn net.Conn) {
if !_hasInboundRTMPConnection {
return
}
2020-07-07 08:04:37 +03:00
log.Infoln("RTMP disconnected.")
conn.Close()
2020-07-12 02:04:02 +03:00
_pipe.Close()
_hasInboundRTMPConnection = false
}
// Disconnect will force disconnect the current inbound RTMP connection.
func Disconnect() {
if _rtmpConnection == nil {
return
}
log.Traceln("Inbound stream disconnect requested.")
handleDisconnect(_rtmpConnection)
}