2020-06-23 03:11:56 +02:00
|
|
|
package rtmp
|
|
|
|
|
|
|
|
import (
|
2020-07-12 01:04:02 +02:00
|
|
|
"fmt"
|
2020-07-06 00:30:30 +02:00
|
|
|
"io"
|
|
|
|
"net"
|
2020-07-02 22:24:22 +02:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2020-07-06 00:30:30 +02:00
|
|
|
"time"
|
2020-07-04 03:48:42 +02:00
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
"github.com/nareix/joy5/format/flv"
|
2020-07-14 08:32:35 +02:00
|
|
|
"github.com/nareix/joy5/format/flv/flvio"
|
2020-07-04 03:48:42 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-07-02 22:24:22 +02:00
|
|
|
|
2020-10-08 04:59:55 +02:00
|
|
|
"github.com/nareix/joy5/format/rtmp"
|
2021-02-19 08:05:52 +01:00
|
|
|
"github.com/owncast/owncast/core/data"
|
2020-10-29 22:09:28 +01:00
|
|
|
"github.com/owncast/owncast/models"
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/utils"
|
2020-06-23 03:11:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-10-22 02:07:00 +02:00
|
|
|
_hasInboundRTMPConnection = false
|
2020-06-23 03:11:56 +02:00
|
|
|
)
|
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
var _pipe *os.File
|
2020-10-02 03:16:58 +02:00
|
|
|
var _rtmpConnection net.Conn
|
2020-07-04 03:48:42 +02:00
|
|
|
|
2020-10-29 22:09:28 +01:00
|
|
|
var _setStreamAsConnected func()
|
|
|
|
var _setBroadcaster func(models.Broadcaster)
|
|
|
|
|
2020-12-17 18:56:04 +01:00
|
|
|
// Start starts the rtmp service, listening on specified RTMP port.
|
2020-10-29 22:09:28 +01:00
|
|
|
func Start(setStreamAsConnected func(), setBroadcaster func(models.Broadcaster)) {
|
|
|
|
_setStreamAsConnected = setStreamAsConnected
|
|
|
|
_setBroadcaster = setBroadcaster
|
|
|
|
|
2021-02-19 08:05:52 +01:00
|
|
|
port := data.GetRTMPPortNumber()
|
2020-07-12 01:04:02 +02:00
|
|
|
s := rtmp.NewServer()
|
|
|
|
var lis net.Listener
|
2020-10-17 00:04:31 +02:00
|
|
|
var err error
|
|
|
|
if lis, err = net.Listen("tcp", fmt.Sprintf(":%d", port)); err != nil {
|
2020-12-20 07:17:10 +01:00
|
|
|
log.Fatal(err)
|
2020-07-12 01:04:02 +02:00
|
|
|
}
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
s.LogEvent = func(c *rtmp.Conn, nc net.Conn, e int) {
|
|
|
|
es := rtmp.EventString[e]
|
2021-02-19 08:05:52 +01:00
|
|
|
log.Traceln("RTMP", nc.LocalAddr(), nc.RemoteAddr(), es)
|
2020-07-12 01:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s.HandleConn = HandleConn
|
2020-07-02 22:24:22 +02:00
|
|
|
|
2020-10-17 00:04:31 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
2020-11-07 00:12:35 +01:00
|
|
|
log.Tracef("RTMP server is listening for incoming stream on port: %d", port)
|
2020-07-12 01:04:02 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
nc, err := lis.Accept()
|
|
|
|
if err != nil {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
go s.HandleNetConn(nc)
|
|
|
|
}
|
2020-07-04 03:48:42 +02:00
|
|
|
}
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
func HandleConn(c *rtmp.Conn, nc net.Conn) {
|
2020-07-14 08:32:35 +02:00
|
|
|
c.LogTagEvent = func(isRead bool, t flvio.Tag) {
|
|
|
|
if t.Type == flvio.TAG_AMF0 {
|
2020-07-15 01:48:41 +02:00
|
|
|
log.Tracef("%+v\n", t.DebugFields())
|
2020-10-02 09:12:47 +02:00
|
|
|
setCurrentBroadcasterInfo(t, nc.RemoteAddr().String())
|
2020-07-14 08:32:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 02:07:00 +02:00
|
|
|
if _hasInboundRTMPConnection {
|
2020-07-06 00:30:30 +02:00
|
|
|
log.Errorln("stream already running; can not overtake an existing stream")
|
2020-07-12 01:04:02 +02:00
|
|
|
nc.Close()
|
2020-07-06 00:30:30 +02:00
|
|
|
return
|
|
|
|
}
|
2020-07-04 03:48:42 +02:00
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
streamingKeyComponents := strings.Split(c.URL.Path, "/")
|
2020-07-04 03:48:42 +02:00
|
|
|
streamingKey := streamingKeyComponents[len(streamingKeyComponents)-1]
|
2021-02-19 08:05:52 +01:00
|
|
|
if streamingKey != data.GetStreamKey() {
|
2020-07-04 03:48:42 +02:00
|
|
|
log.Errorln("invalid streaming key; rejecting incoming stream")
|
2020-07-12 01:04:02 +02:00
|
|
|
nc.Close()
|
2020-07-04 03:48:42 +02:00
|
|
|
return
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:25:18 +01:00
|
|
|
log.Infoln("Inbound stream connected.")
|
2020-12-06 23:28:00 +01:00
|
|
|
_setStreamAsConnected()
|
2020-07-06 00:30:30 +02:00
|
|
|
|
2021-04-02 03:56:00 +02:00
|
|
|
pipePath := utils.GetTemporaryPipePath(fmt.Sprint(data.GetRTMPPortNumber()))
|
2020-11-15 03:39:53 +01:00
|
|
|
if !utils.DoesFileExists(pipePath) {
|
|
|
|
err := syscall.Mkfifo(pipePath, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 01:04:02 +02:00
|
|
|
|
2020-10-22 02:07:00 +02:00
|
|
|
_hasInboundRTMPConnection = true
|
2020-10-02 03:16:58 +02:00
|
|
|
_rtmpConnection = nc
|
2020-07-02 22:24:22 +02:00
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
f, err := os.OpenFile(pipePath, os.O_RDWR, os.ModeNamedPipe)
|
|
|
|
_pipe = f
|
2020-07-04 03:48:42 +02:00
|
|
|
if err != nil {
|
2021-01-29 20:25:18 +01:00
|
|
|
log.Fatalln("unable to open", pipePath, "and will exit")
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
2020-07-02 22:24:22 +02:00
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
w := flv.NewMuxer(f)
|
|
|
|
|
|
|
|
for {
|
2020-10-22 02:07:00 +02:00
|
|
|
if !_hasInboundRTMPConnection {
|
2020-10-08 04:59:55 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:58:34 +01:00
|
|
|
// If we don't get a readable packet in 10 seconds give up and disconnect
|
2021-01-17 02:24:12 +01:00
|
|
|
if err := _rtmpConnection.SetReadDeadline(time.Now().Add(10 * time.Second)); err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
}
|
2021-01-29 20:25:18 +01:00
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
pkt, err := c.ReadPacket()
|
2021-01-14 06:58:34 +01:00
|
|
|
|
|
|
|
// Broadcaster disconnected
|
2020-07-12 08:54:10 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
handleDisconnect(nc)
|
2021-01-29 20:25:18 +01:00
|
|
|
return
|
2020-07-06 00:30:30 +02:00
|
|
|
}
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2021-01-14 06:58:34 +01:00
|
|
|
// Read timeout. Disconnect.
|
|
|
|
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
|
|
|
|
log.Debugln("Timeout reading the inbound stream from the broadcaster. Assuming that they disconnected and ending the stream.")
|
|
|
|
handleDisconnect(nc)
|
2021-01-29 20:25:18 +01:00
|
|
|
return
|
2021-01-14 06:58:34 +01:00
|
|
|
}
|
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
if err := w.WritePacket(pkt); err != nil {
|
2021-01-29 20:25:18 +01:00
|
|
|
log.Errorln("unable to write rtmp packet", err)
|
|
|
|
handleDisconnect(nc)
|
|
|
|
return
|
2020-07-06 00:30:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
func handleDisconnect(conn net.Conn) {
|
2020-11-05 07:33:56 +01:00
|
|
|
if !_hasInboundRTMPConnection {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:25:18 +01:00
|
|
|
log.Infoln("Inbound stream disconnected.")
|
2020-07-06 00:30:30 +02:00
|
|
|
conn.Close()
|
2020-07-12 01:04:02 +02:00
|
|
|
_pipe.Close()
|
2020-10-22 02:07:00 +02:00
|
|
|
_hasInboundRTMPConnection = false
|
2020-07-06 00:30:30 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 03:16:58 +02:00
|
|
|
// Disconnect will force disconnect the current inbound RTMP connection.
|
|
|
|
func Disconnect() {
|
|
|
|
if _rtmpConnection == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-29 22:09:28 +01:00
|
|
|
log.Traceln("Inbound stream disconnect requested.")
|
2020-10-02 03:16:58 +02:00
|
|
|
handleDisconnect(_rtmpConnection)
|
|
|
|
}
|