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-12 01:04:02 +02:00
|
|
|
"unsafe"
|
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
|
|
|
|
|
|
|
"github.com/gabek/owncast/config"
|
|
|
|
"github.com/gabek/owncast/core"
|
|
|
|
"github.com/gabek/owncast/core/ffmpeg"
|
|
|
|
"github.com/gabek/owncast/utils"
|
2020-07-12 01:04:02 +02:00
|
|
|
"github.com/nareix/joy5/format/rtmp"
|
2020-06-23 03:11:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
//IsConnected whether there is a connection or not
|
|
|
|
_isConnected = false
|
|
|
|
)
|
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
var _transcoder ffmpeg.Transcoder
|
|
|
|
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-06-23 03:11:56 +02:00
|
|
|
//Start starts the rtmp service, listening on port 1935
|
|
|
|
func Start() {
|
|
|
|
port := 1935
|
2020-07-12 01:04:02 +02:00
|
|
|
s := rtmp.NewServer()
|
|
|
|
var lis net.Listener
|
|
|
|
var error error
|
|
|
|
if lis, error = net.Listen("tcp", fmt.Sprintf(":%d", port)); error != nil {
|
|
|
|
return
|
|
|
|
}
|
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]
|
|
|
|
log.Traceln(unsafe.Pointer(c), nc.LocalAddr(), nc.RemoteAddr(), es)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.HandleConn = HandleConn
|
2020-07-02 22:24:22 +02:00
|
|
|
|
2020-07-04 03:48:42 +02:00
|
|
|
if error != nil {
|
|
|
|
log.Panicln(error)
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
2020-07-09 03:27:24 +02:00
|
|
|
log.Infof("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-07-14 08:32:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 00:30:30 +02:00
|
|
|
if _isConnected {
|
|
|
|
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]
|
|
|
|
if streamingKey != config.Config.VideoSettings.StreamingKey {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-07 07:04:37 +02:00
|
|
|
log.Infoln("Incoming RTMP connected.")
|
2020-07-06 00:30:30 +02:00
|
|
|
|
2020-07-04 03:48:42 +02:00
|
|
|
pipePath := utils.GetTemporaryPipePath()
|
|
|
|
syscall.Mkfifo(pipePath, 0666)
|
2020-07-12 01:04:02 +02:00
|
|
|
|
|
|
|
_transcoder = ffmpeg.NewTranscoder()
|
|
|
|
go _transcoder.Start()
|
2020-07-02 22:24:22 +02:00
|
|
|
|
2020-07-04 03:48:42 +02:00
|
|
|
_isConnected = true
|
|
|
|
core.SetStreamAsConnected()
|
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 {
|
|
|
|
panic(err)
|
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 {
|
|
|
|
pkt, err := c.ReadPacket()
|
2020-07-12 08:54:10 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
handleDisconnect(nc)
|
|
|
|
return
|
2020-07-06 00:30:30 +02:00
|
|
|
}
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
if err := w.WritePacket(pkt); err != nil {
|
|
|
|
panic(err)
|
2020-07-06 00:30:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-12 01:04:02 +02:00
|
|
|
func handleDisconnect(conn net.Conn) {
|
2020-07-07 07:04:37 +02:00
|
|
|
log.Infoln("RTMP disconnected.")
|
2020-07-06 00:30:30 +02:00
|
|
|
conn.Close()
|
2020-07-12 01:04:02 +02:00
|
|
|
_pipe.Close()
|
2020-07-06 00:30:30 +02:00
|
|
|
_isConnected = false
|
2020-07-12 01:04:02 +02:00
|
|
|
_transcoder.Stop()
|
2020-10-02 03:16:58 +02:00
|
|
|
_rtmpConnection = nil
|
2020-07-06 00:30:30 +02:00
|
|
|
core.SetStreamAsDisconnected()
|
|
|
|
}
|
|
|
|
|
2020-10-02 03:16:58 +02:00
|
|
|
// Disconnect will force disconnect the current inbound RTMP connection.
|
|
|
|
func Disconnect() {
|
|
|
|
if _rtmpConnection == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infoln("Inbound stream disconnect requested.")
|
|
|
|
handleDisconnect(_rtmpConnection)
|
|
|
|
}
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
//IsConnected gets whether there is an rtmp connection or not
|
|
|
|
//this is only a getter since it is controlled by the rtmp handler
|
|
|
|
func IsConnected() bool {
|
|
|
|
return _isConnected
|
|
|
|
}
|