2020-06-23 03:11:56 +02:00
|
|
|
package rtmp
|
|
|
|
|
|
|
|
import (
|
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-06 00:30:30 +02:00
|
|
|
"github.com/nareix/joy4/av/avutil"
|
|
|
|
"github.com/nareix/joy4/format/ts"
|
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-06 00:30:30 +02:00
|
|
|
"github.com/nareix/joy4/format"
|
|
|
|
"github.com/nareix/joy4/format/rtmp"
|
2020-06-23 03:11:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
//IsConnected whether there is a connection or not
|
|
|
|
_isConnected = false
|
|
|
|
)
|
|
|
|
|
2020-07-04 03:48:42 +02:00
|
|
|
func init() {
|
|
|
|
format.RegisterAll()
|
|
|
|
}
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
//Start starts the rtmp service, listening on port 1935
|
|
|
|
func Start() {
|
|
|
|
port := 1935
|
2020-07-04 03:48:42 +02:00
|
|
|
server := &rtmp.Server{}
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-07-04 03:48:42 +02:00
|
|
|
server.HandlePublish = handlePublish
|
2020-07-02 22:24:22 +02:00
|
|
|
|
2020-07-04 03:48:42 +02:00
|
|
|
error := server.ListenAndServe()
|
|
|
|
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-04 03:48:42 +02:00
|
|
|
}
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-07-04 03:48:42 +02:00
|
|
|
func handlePublish(conn *rtmp.Conn) {
|
2020-07-06 00:30:30 +02:00
|
|
|
if _isConnected {
|
|
|
|
log.Errorln("stream already running; can not overtake an existing stream")
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
2020-07-04 03:48:42 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
conn.Close()
|
|
|
|
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)
|
|
|
|
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-07-02 22:24:22 +02:00
|
|
|
|
2020-07-04 03:48:42 +02:00
|
|
|
f, err := os.OpenFile(pipePath, os.O_WRONLY, os.ModeNamedPipe)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
2020-07-02 22:24:22 +02:00
|
|
|
|
2020-07-06 00:30:30 +02:00
|
|
|
// Is this too fast? Are there downsides to peeking
|
|
|
|
// into the stream so frequently?
|
|
|
|
ticker := time.NewTicker(500 * time.Millisecond)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
error := connCheck(conn.NetConn())
|
|
|
|
if error == io.EOF {
|
|
|
|
handleDisconnect(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-07-04 03:48:42 +02:00
|
|
|
muxer := ts.NewMuxer(f)
|
|
|
|
avutil.CopyFile(muxer, conn)
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 00:30:30 +02:00
|
|
|
// Proactively check if the RTMP connection is still active or not.
|
|
|
|
// Taken from https://stackoverflow.com/a/58664631.
|
|
|
|
func connCheck(conn net.Conn) error {
|
|
|
|
var sysErr error = nil
|
|
|
|
rc, err := conn.(syscall.Conn).SyscallConn()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = rc.Read(func(fd uintptr) bool {
|
|
|
|
var buf []byte = []byte{0}
|
|
|
|
n, _, err := syscall.Recvfrom(int(fd), buf, syscall.MSG_PEEK|syscall.MSG_DONTWAIT)
|
|
|
|
switch {
|
|
|
|
case n == 0 && err == nil:
|
|
|
|
sysErr = io.EOF
|
|
|
|
case err == syscall.EAGAIN || err == syscall.EWOULDBLOCK:
|
|
|
|
sysErr = nil
|
|
|
|
default:
|
|
|
|
sysErr = err
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sysErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleDisconnect(conn *rtmp.Conn) {
|
2020-07-07 07:04:37 +02:00
|
|
|
log.Infoln("RTMP disconnected.")
|
2020-07-06 00:30:30 +02:00
|
|
|
conn.Close()
|
|
|
|
_isConnected = false
|
|
|
|
core.SetStreamAsDisconnected()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|