2020-06-23 04:11:56 +03:00
|
|
|
package rtmp
|
2020-05-24 03:57:49 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-06-23 04:11:56 +03:00
|
|
|
"errors"
|
2020-05-24 03:57:49 +03:00
|
|
|
"io"
|
|
|
|
"os"
|
2020-05-30 04:08:33 +03:00
|
|
|
"syscall"
|
2020-05-24 03:57:49 +03:00
|
|
|
|
2020-06-18 02:29:47 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-05-24 03:57:49 +03:00
|
|
|
"github.com/yutopp/go-flv"
|
|
|
|
flvtag "github.com/yutopp/go-flv/tag"
|
2020-06-23 04:11:56 +03:00
|
|
|
yutmp "github.com/yutopp/go-rtmp"
|
2020-05-24 03:57:49 +03:00
|
|
|
rtmpmsg "github.com/yutopp/go-rtmp/message"
|
2020-06-23 04:11:56 +03:00
|
|
|
|
|
|
|
"github.com/gabek/owncast/config"
|
|
|
|
"github.com/gabek/owncast/core"
|
|
|
|
"github.com/gabek/owncast/core/ffmpeg"
|
|
|
|
"github.com/gabek/owncast/utils"
|
2020-05-24 03:57:49 +03:00
|
|
|
)
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
var _ yutmp.Handler = (*Handler)(nil)
|
2020-05-24 03:57:49 +03:00
|
|
|
|
|
|
|
// Handler An RTMP connection handler
|
|
|
|
type Handler struct {
|
2020-06-23 04:11:56 +03:00
|
|
|
yutmp.DefaultHandler
|
2020-05-24 03:57:49 +03:00
|
|
|
flvFile *os.File
|
|
|
|
flvEnc *flv.Encoder
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
//OnServe handles the "OnServe" of the rtmp service
|
|
|
|
func (h *Handler) OnServe(conn *yutmp.Conn) {
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
//OnConnect handles the "OnConnect" of the rtmp service
|
2020-05-24 03:57:49 +03:00
|
|
|
func (h *Handler) OnConnect(timestamp uint32, cmd *rtmpmsg.NetConnectionConnect) error {
|
2020-06-01 23:28:14 +03:00
|
|
|
// log.Printf("OnConnect: %#v", cmd)
|
2020-06-19 20:00:27 +03:00
|
|
|
|
2020-05-24 03:57:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
//OnCreateStream handles the "OnCreateStream" of the rtmp service
|
2020-05-24 03:57:49 +03:00
|
|
|
func (h *Handler) OnCreateStream(timestamp uint32, cmd *rtmpmsg.NetConnectionCreateStream) error {
|
2020-06-01 23:28:14 +03:00
|
|
|
// log.Printf("OnCreateStream: %#v", cmd)
|
2020-06-19 20:00:27 +03:00
|
|
|
|
2020-05-24 03:57:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
//OnPublish handles the "OnPublish" of the rtmp service
|
2020-05-24 03:57:49 +03:00
|
|
|
func (h *Handler) OnPublish(timestamp uint32, cmd *rtmpmsg.NetStreamPublish) error {
|
2020-06-01 23:28:14 +03:00
|
|
|
// log.Printf("OnPublish: %#v", cmd)
|
2020-07-07 07:27:31 +03:00
|
|
|
log.Trace("Incoming stream connected.")
|
2020-06-02 03:15:11 +03:00
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
if cmd.PublishingName != config.Config.VideoSettings.StreamingKey {
|
2020-06-19 20:00:27 +03:00
|
|
|
return errors.New("invalid streaming key; rejecting incoming stream")
|
2020-06-02 03:15:11 +03:00
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
if _isConnected {
|
2020-06-19 20:00:27 +03:00
|
|
|
return errors.New("stream already running; can not overtake an existing stream")
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-06-02 03:42:36 +03:00
|
|
|
// Record streams as FLV
|
2020-06-23 04:11:56 +03:00
|
|
|
p := utils.GetTemporaryPipePath()
|
2020-05-30 04:08:33 +03:00
|
|
|
syscall.Mkfifo(p, 0666)
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2020-05-30 04:08:33 +03:00
|
|
|
f, err := os.OpenFile(p, os.O_RDWR, os.ModeNamedPipe)
|
2020-05-24 03:57:49 +03:00
|
|
|
if err != nil {
|
2020-06-23 04:11:56 +03:00
|
|
|
return err
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
h.flvFile = f
|
|
|
|
|
|
|
|
enc, err := flv.NewEncoder(f, flv.FlagsAudio|flv.FlagsVideo)
|
|
|
|
if err != nil {
|
|
|
|
_ = f.Close()
|
2020-06-23 04:11:56 +03:00
|
|
|
return err
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
h.flvEnc = enc
|
|
|
|
|
2020-06-26 03:44:47 +03:00
|
|
|
transcoder := ffmpeg.NewTranscoder()
|
|
|
|
go transcoder.Start()
|
2020-05-30 04:08:33 +03:00
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
_isConnected = true
|
|
|
|
core.SetStreamAsConnected()
|
2020-06-02 10:27:54 +03:00
|
|
|
|
2020-05-24 03:57:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
//OnSetDataFrame handles the setting of the data frame
|
2020-05-24 03:57:49 +03:00
|
|
|
func (h *Handler) OnSetDataFrame(timestamp uint32, data *rtmpmsg.NetStreamSetDataFrame) error {
|
|
|
|
r := bytes.NewReader(data.Payload)
|
|
|
|
|
|
|
|
var script flvtag.ScriptData
|
|
|
|
if err := flvtag.DecodeScriptData(r, &script); err != nil {
|
|
|
|
log.Printf("Failed to decode script data: Err = %+v", err)
|
|
|
|
return nil // ignore
|
|
|
|
}
|
|
|
|
|
2020-06-01 23:28:14 +03:00
|
|
|
// log.Printf("SetDataFrame: Script = %#v", script)
|
2020-05-24 03:57:49 +03:00
|
|
|
|
|
|
|
if err := h.flvEnc.Encode(&flvtag.FlvTag{
|
|
|
|
TagType: flvtag.TagTypeScriptData,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
Data: &script,
|
|
|
|
}); err != nil {
|
|
|
|
log.Printf("Failed to write script data: Err = %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
//OnAudio handles when we get audio from the rtmp service
|
2020-05-24 03:57:49 +03:00
|
|
|
func (h *Handler) OnAudio(timestamp uint32, payload io.Reader) error {
|
|
|
|
var audio flvtag.AudioData
|
|
|
|
if err := flvtag.DecodeAudioData(payload, &audio); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
flvBody := new(bytes.Buffer)
|
|
|
|
if _, err := io.Copy(flvBody, audio.Data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
audio.Data = flvBody
|
|
|
|
|
|
|
|
// log.Printf("FLV Audio Data: Timestamp = %d, SoundFormat = %+v, SoundRate = %+v, SoundSize = %+v, SoundType = %+v, AACPacketType = %+v, Data length = %+v",
|
|
|
|
// timestamp,
|
|
|
|
// audio.SoundFormat,
|
|
|
|
// audio.SoundRate,
|
|
|
|
// audio.SoundSize,
|
|
|
|
// audio.SoundType,
|
|
|
|
// audio.AACPacketType,
|
|
|
|
// len(flvBody.Bytes()),
|
|
|
|
// )
|
|
|
|
|
|
|
|
if err := h.flvEnc.Encode(&flvtag.FlvTag{
|
|
|
|
TagType: flvtag.TagTypeAudio,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
Data: &audio,
|
|
|
|
}); err != nil {
|
|
|
|
log.Printf("Failed to write audio: Err = %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
//OnVideo handles when we video from the rtmp service
|
2020-05-24 03:57:49 +03:00
|
|
|
func (h *Handler) OnVideo(timestamp uint32, payload io.Reader) error {
|
|
|
|
var video flvtag.VideoData
|
|
|
|
if err := flvtag.DecodeVideoData(payload, &video); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
flvBody := new(bytes.Buffer)
|
|
|
|
if _, err := io.Copy(flvBody, video.Data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
video.Data = flvBody
|
|
|
|
|
|
|
|
// log.Printf("FLV Video Data: Timestamp = %d, FrameType = %+v, CodecID = %+v, AVCPacketType = %+v, CT = %+v, Data length = %+v",
|
|
|
|
// timestamp,
|
|
|
|
// video.FrameType,
|
|
|
|
// video.CodecID,
|
|
|
|
// video.AVCPacketType,
|
|
|
|
// video.CompositionTime,
|
|
|
|
// len(flvBody.Bytes()),
|
|
|
|
// )
|
|
|
|
|
|
|
|
if err := h.flvEnc.Encode(&flvtag.FlvTag{
|
|
|
|
TagType: flvtag.TagTypeVideo,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
Data: &video,
|
|
|
|
}); err != nil {
|
|
|
|
log.Printf("Failed to write video: Err = %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
//OnClose handles the closing of the rtmp connection
|
2020-05-24 03:57:49 +03:00
|
|
|
func (h *Handler) OnClose() {
|
2020-06-23 04:11:56 +03:00
|
|
|
log.Printf("OnClose of the rtmp service")
|
2020-05-24 03:57:49 +03:00
|
|
|
|
|
|
|
if h.flvFile != nil {
|
|
|
|
_ = h.flvFile.Close()
|
|
|
|
}
|
2020-06-02 10:27:54 +03:00
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
_isConnected = false
|
|
|
|
core.SetStreamAsDisconnected()
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|