2020-06-23 04:11:56 +03:00
|
|
|
package rtmp
|
|
|
|
|
|
|
|
import (
|
2020-07-12 02:04:02 +03:00
|
|
|
"fmt"
|
2020-07-06 01:30:30 +03:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"time"
|
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"
|
2022-11-29 08:32:11 +03:00
|
|
|
"github.com/owncast/owncast/config"
|
2021-02-19 10:05:52 +03:00
|
|
|
"github.com/owncast/owncast/core/data"
|
2020-10-30 00:09:28 +03:00
|
|
|
"github.com/owncast/owncast/models"
|
2020-06-23 04:11:56 +03:00
|
|
|
)
|
|
|
|
|
2022-11-23 09:08:25 +03:00
|
|
|
var _hasInboundRTMPConnection = false
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
var (
|
2022-11-23 09:08:25 +03:00
|
|
|
_pipe *io.PipeWriter
|
|
|
|
_rtmpConnection net.Conn
|
2020-06-23 04:11:56 +03:00
|
|
|
)
|
|
|
|
|
2022-11-23 09:08:25 +03:00
|
|
|
var (
|
|
|
|
_setStreamAsConnected func(*io.PipeReader)
|
|
|
|
_setBroadcaster func(models.Broadcaster)
|
|
|
|
)
|
2020-10-30 00:09:28 +03:00
|
|
|
|
2020-12-17 20:56:04 +03:00
|
|
|
// Start starts the rtmp service, listening on specified RTMP port.
|
2021-07-03 22:28:25 +03:00
|
|
|
func Start(setStreamAsConnected func(*io.PipeReader), setBroadcaster func(models.Broadcaster)) {
|
2020-10-30 00:09:28 +03:00
|
|
|
_setStreamAsConnected = setStreamAsConnected
|
|
|
|
_setBroadcaster = setBroadcaster
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
port := data.GetRTMPPortNumber()
|
2020-07-12 02:04:02 +03:00
|
|
|
s := rtmp.NewServer()
|
|
|
|
var lis net.Listener
|
2020-10-17 01:04:31 +03:00
|
|
|
var err error
|
|
|
|
if lis, err = net.Listen("tcp", fmt.Sprintf(":%d", port)); err != nil {
|
2020-12-20 09:17:10 +03:00
|
|
|
log.Fatal(err)
|
2020-07-12 02:04:02 +03:00
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2020-07-12 02:04:02 +03:00
|
|
|
s.LogEvent = func(c *rtmp.Conn, nc net.Conn, e int) {
|
|
|
|
es := rtmp.EventString[e]
|
2021-02-19 10:05:52 +03:00
|
|
|
log.Traceln("RTMP", nc.LocalAddr(), nc.RemoteAddr(), es)
|
2020-07-12 02:04:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s.HandleConn = HandleConn
|
2020-07-02 23:24:22 +03:00
|
|
|
|
2020-10-17 01:04:31 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
2020-11-07 02:12:35 +03:00
|
|
|
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-06-23 04:11:56 +03:00
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
// HandleConn is fired when an inbound RTMP connection takes place.
|
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())
|
2020-10-02 10:12:47 +03:00
|
|
|
setCurrentBroadcasterInfo(t, nc.RemoteAddr().String())
|
2020-07-14 09:32:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 03:07:00 +03:00
|
|
|
if _hasInboundRTMPConnection {
|
2020-07-06 01:30:30 +03:00
|
|
|
log.Errorln("stream already running; can not overtake an existing stream")
|
2021-09-12 10:18:15 +03:00
|
|
|
_ = nc.Close()
|
2020-07-06 01:30:30 +03:00
|
|
|
return
|
|
|
|
}
|
2020-07-04 04:48:42 +03:00
|
|
|
|
2022-11-23 09:08:25 +03:00
|
|
|
accessGranted := false
|
|
|
|
validStreamingKeys := data.GetStreamKeys()
|
|
|
|
|
2023-02-28 04:08:52 +03:00
|
|
|
// If a stream key override was specified then use that instead.
|
|
|
|
if config.TemporaryStreamKey != "" {
|
|
|
|
validStreamingKeys = []models.StreamKey{{Key: config.TemporaryStreamKey}}
|
|
|
|
}
|
|
|
|
|
2022-11-23 09:08:25 +03:00
|
|
|
for _, key := range validStreamingKeys {
|
2022-11-24 03:39:55 +03:00
|
|
|
if secretMatch(key.Key, c.URL.Path) {
|
2022-11-23 09:08:25 +03:00
|
|
|
accessGranted = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !accessGranted {
|
2020-07-04 04:48:42 +03:00
|
|
|
log.Errorln("invalid streaming key; rejecting incoming stream")
|
2021-09-12 10:18:15 +03:00
|
|
|
_ = nc.Close()
|
2020-07-04 04:48:42 +03:00
|
|
|
return
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 09:37:06 +03:00
|
|
|
rtmpOut, rtmpIn := io.Pipe()
|
|
|
|
_pipe = rtmpIn
|
2021-01-29 22:25:18 +03:00
|
|
|
log.Infoln("Inbound stream connected.")
|
2021-07-03 22:28:25 +03:00
|
|
|
_setStreamAsConnected(rtmpOut)
|
2020-07-12 02:04:02 +03:00
|
|
|
|
2020-10-22 03:07:00 +03:00
|
|
|
_hasInboundRTMPConnection = true
|
2020-10-02 04:16:58 +03:00
|
|
|
_rtmpConnection = nc
|
2020-07-02 23:24:22 +03:00
|
|
|
|
2021-07-03 22:28:25 +03:00
|
|
|
w := flv.NewMuxer(rtmpIn)
|
2020-07-12 02:04:02 +03:00
|
|
|
|
|
|
|
for {
|
2020-10-22 03:07:00 +03:00
|
|
|
if !_hasInboundRTMPConnection {
|
2020-10-08 05:59:55 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-01-14 08:58:34 +03:00
|
|
|
// If we don't get a readable packet in 10 seconds give up and disconnect
|
2021-01-17 04:24:12 +03:00
|
|
|
if err := _rtmpConnection.SetReadDeadline(time.Now().Add(10 * time.Second)); err != nil {
|
2021-04-06 05:04:02 +03:00
|
|
|
log.Debugln(err)
|
2021-01-17 04:24:12 +03:00
|
|
|
}
|
2021-01-29 22:25:18 +03:00
|
|
|
|
2020-07-12 02:04:02 +03:00
|
|
|
pkt, err := c.ReadPacket()
|
2021-01-14 08:58:34 +03:00
|
|
|
|
|
|
|
// Broadcaster disconnected
|
2020-07-12 09:54:10 +03:00
|
|
|
if err == io.EOF {
|
|
|
|
handleDisconnect(nc)
|
2021-01-29 22:25:18 +03:00
|
|
|
return
|
2020-07-06 01:30:30 +03:00
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2021-01-14 08:58:34 +03: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 22:25:18 +03:00
|
|
|
return
|
2021-01-14 08:58:34 +03:00
|
|
|
}
|
|
|
|
|
2020-07-12 02:04:02 +03:00
|
|
|
if err := w.WritePacket(pkt); err != nil {
|
2021-01-29 22:25:18 +03:00
|
|
|
log.Errorln("unable to write rtmp packet", err)
|
|
|
|
handleDisconnect(nc)
|
|
|
|
return
|
2020-07-06 01:30:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 02:04:02 +03:00
|
|
|
func handleDisconnect(conn net.Conn) {
|
2020-11-05 09:33:56 +03:00
|
|
|
if !_hasInboundRTMPConnection {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-29 22:25:18 +03:00
|
|
|
log.Infoln("Inbound stream disconnected.")
|
2021-09-12 10:18:15 +03:00
|
|
|
_ = conn.Close()
|
|
|
|
_ = _pipe.Close()
|
2020-10-22 03:07:00 +03:00
|
|
|
_hasInboundRTMPConnection = false
|
2020-07-06 01:30:30 +03:00
|
|
|
}
|
|
|
|
|
2020-10-02 04:16:58 +03:00
|
|
|
// Disconnect will force disconnect the current inbound RTMP connection.
|
|
|
|
func Disconnect() {
|
|
|
|
if _rtmpConnection == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-30 00:09:28 +03:00
|
|
|
log.Traceln("Inbound stream disconnect requested.")
|
2020-10-02 04:16:58 +03:00
|
|
|
handleDisconnect(_rtmpConnection)
|
|
|
|
}
|