2021-07-24 13:55:42 +00:00
|
|
|
package core
|
2020-10-03 19:10:41 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-24 18:47:43 +00:00
|
|
|
"context"
|
2020-10-03 19:10:41 +00:00
|
|
|
"fmt"
|
2022-07-09 15:25:33 +00:00
|
|
|
"net"
|
|
|
|
"net/url"
|
2020-10-03 19:10:41 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aler9/gortsplib"
|
2021-09-23 18:14:20 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/h264"
|
2020-11-15 16:56:54 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/rtpaac"
|
|
|
|
"github.com/aler9/gortsplib/pkg/rtph264"
|
2022-07-17 13:17:18 +00:00
|
|
|
"github.com/notedit/rtmp/format/flv/flvio"
|
2020-10-25 10:41:41 +00:00
|
|
|
|
2021-09-26 21:06:40 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/conf"
|
2020-12-08 11:21:06 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
2021-04-03 09:39:19 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/rtmp"
|
2022-07-17 13:17:18 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/rtmp/message"
|
2020-10-03 19:10:41 +00:00
|
|
|
)
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type rtmpSourceParent interface {
|
2021-10-27 19:01:00 +00:00
|
|
|
log(logger.Level, string, ...interface{})
|
2022-08-04 19:07:17 +00:00
|
|
|
sourceStaticImplSetReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
|
|
|
|
sourceStaticImplSetNotReady(req pathSourceStaticSetNotReadyReq)
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type rtmpSource struct {
|
2021-04-24 18:47:43 +00:00
|
|
|
ur string
|
2021-09-26 21:06:40 +00:00
|
|
|
readTimeout conf.StringDuration
|
|
|
|
writeTimeout conf.StringDuration
|
2021-07-24 13:55:42 +00:00
|
|
|
parent rtmpSourceParent
|
2020-10-03 19:10:41 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func newRTMPSource(
|
2021-05-11 15:20:32 +00:00
|
|
|
ur string,
|
2021-09-26 21:06:40 +00:00
|
|
|
readTimeout conf.StringDuration,
|
|
|
|
writeTimeout conf.StringDuration,
|
2022-04-07 10:50:35 +00:00
|
|
|
parent rtmpSourceParent,
|
|
|
|
) *rtmpSource {
|
2022-07-30 19:51:38 +00:00
|
|
|
return &rtmpSource{
|
2021-04-24 18:47:43 +00:00
|
|
|
ur: ur,
|
|
|
|
readTimeout: readTimeout,
|
|
|
|
writeTimeout: writeTimeout,
|
|
|
|
parent: parent,
|
2020-10-03 19:10:41 +00:00
|
|
|
}
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
2020-10-03 19:10:41 +00:00
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
func (s *rtmpSource) Log(level logger.Level, format string, args ...interface{}) {
|
2021-10-27 19:01:00 +00:00
|
|
|
s.parent.log(level, "[rtmp source] "+format, args...)
|
2020-12-08 11:21:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 19:51:38 +00:00
|
|
|
// run implements sourceStaticImpl.
|
|
|
|
func (s *rtmpSource) run(ctx context.Context) error {
|
2022-07-24 16:11:53 +00:00
|
|
|
s.Log(logger.Debug, "connecting")
|
2020-10-03 19:10:41 +00:00
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
u, err := url.Parse(s.ur)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// add default port
|
|
|
|
_, _, err = net.SplitHostPort(u.Host)
|
|
|
|
if err != nil {
|
|
|
|
u.Host = net.JoinHostPort(u.Host, "1935")
|
|
|
|
}
|
2020-10-03 19:10:41 +00:00
|
|
|
|
2022-07-30 19:51:38 +00:00
|
|
|
ctx2, cancel2 := context.WithTimeout(ctx, time.Duration(s.readTimeout))
|
2022-07-24 16:11:53 +00:00
|
|
|
defer cancel2()
|
|
|
|
|
|
|
|
var d net.Dialer
|
|
|
|
nconn, err := d.DialContext(ctx2, "tcp", u.Host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := rtmp.NewConn(nconn)
|
|
|
|
|
|
|
|
readDone := make(chan error)
|
|
|
|
go func() {
|
|
|
|
readDone <- func() error {
|
|
|
|
nconn.SetReadDeadline(time.Now().Add(time.Duration(s.readTimeout)))
|
|
|
|
nconn.SetWriteDeadline(time.Now().Add(time.Duration(s.writeTimeout)))
|
|
|
|
err = conn.InitializeClient(u, true)
|
2022-07-09 15:25:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
nconn.SetWriteDeadline(time.Time{})
|
|
|
|
nconn.SetReadDeadline(time.Now().Add(time.Duration(s.readTimeout)))
|
|
|
|
videoTrack, audioTrack, err := conn.ReadTracks()
|
2022-07-09 15:25:33 +00:00
|
|
|
if err != nil {
|
2022-07-24 16:11:53 +00:00
|
|
|
return err
|
2022-07-09 15:25:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
var tracks gortsplib.Tracks
|
|
|
|
videoTrackID := -1
|
|
|
|
audioTrackID := -1
|
2021-01-31 20:42:27 +00:00
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
var h264Encoder *rtph264.Encoder
|
|
|
|
if videoTrack != nil {
|
|
|
|
h264Encoder = &rtph264.Encoder{PayloadType: 96}
|
|
|
|
h264Encoder.Init()
|
|
|
|
videoTrackID = len(tracks)
|
|
|
|
tracks = append(tracks, videoTrack)
|
2021-03-22 20:40:07 +00:00
|
|
|
}
|
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
var aacEncoder *rtpaac.Encoder
|
|
|
|
if audioTrack != nil {
|
|
|
|
aacEncoder = &rtpaac.Encoder{
|
|
|
|
PayloadType: 96,
|
|
|
|
SampleRate: audioTrack.ClockRate(),
|
|
|
|
SizeLength: 13,
|
|
|
|
IndexLength: 3,
|
|
|
|
IndexDeltaLength: 3,
|
|
|
|
}
|
|
|
|
aacEncoder.Init()
|
|
|
|
audioTrackID = len(tracks)
|
|
|
|
tracks = append(tracks, audioTrack)
|
|
|
|
}
|
2020-10-03 19:10:41 +00:00
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
res := s.parent.sourceStaticImplSetReady(pathSourceStaticSetReadyReq{tracks: tracks})
|
2022-07-24 16:11:53 +00:00
|
|
|
if res.err != nil {
|
|
|
|
return res.err
|
|
|
|
}
|
2020-10-03 19:10:41 +00:00
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
s.Log(logger.Info, "ready")
|
2021-04-05 16:14:06 +00:00
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
defer func() {
|
2022-08-04 19:07:17 +00:00
|
|
|
s.parent.sourceStaticImplSetNotReady(pathSourceStaticSetNotReadyReq{})
|
2022-07-24 16:11:53 +00:00
|
|
|
}()
|
2021-03-10 14:06:45 +00:00
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
for {
|
|
|
|
nconn.SetReadDeadline(time.Now().Add(time.Duration(s.readTimeout)))
|
|
|
|
msg, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tmsg := msg.(type) {
|
|
|
|
case *message.MsgVideo:
|
|
|
|
if tmsg.H264Type == flvio.AVC_NALU {
|
|
|
|
if videoTrack == nil {
|
|
|
|
return fmt.Errorf("received an H264 packet, but track is not set up")
|
2022-03-24 11:59:22 +00:00
|
|
|
}
|
2021-04-24 18:47:43 +00:00
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
nalus, err := h264.AVCCUnmarshal(tmsg.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decode AVCC: %v", err)
|
|
|
|
}
|
2021-08-18 13:42:41 +00:00
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
pts := tmsg.DTS + tmsg.PTSDelta
|
2022-03-02 21:52:05 +00:00
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
pkts, err := h264Encoder.Encode(nalus, pts)
|
2021-04-24 18:47:43 +00:00
|
|
|
if err != nil {
|
2022-07-24 16:11:53 +00:00
|
|
|
return fmt.Errorf("error while encoding H264: %v", err)
|
2021-04-24 18:47:43 +00:00
|
|
|
}
|
2020-10-05 19:07:34 +00:00
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
lastPkt := len(pkts) - 1
|
|
|
|
for i, pkt := range pkts {
|
|
|
|
if i != lastPkt {
|
|
|
|
res.stream.writeData(&data{
|
|
|
|
trackID: videoTrackID,
|
|
|
|
rtp: pkt,
|
|
|
|
ptsEqualsDTS: false,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
res.stream.writeData(&data{
|
|
|
|
trackID: videoTrackID,
|
|
|
|
rtp: pkt,
|
|
|
|
ptsEqualsDTS: h264.IDRPresent(nalus),
|
|
|
|
h264NALUs: nalus,
|
|
|
|
h264PTS: pts,
|
|
|
|
})
|
2021-04-24 18:47:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-24 20:35:23 +00:00
|
|
|
}
|
2021-01-30 20:19:50 +00:00
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
case *message.MsgAudio:
|
|
|
|
if tmsg.AACType == flvio.AAC_RAW {
|
|
|
|
if audioTrack == nil {
|
|
|
|
return fmt.Errorf("received an AAC packet, but track is not set up")
|
|
|
|
}
|
2021-04-24 18:47:43 +00:00
|
|
|
|
2022-07-24 16:11:53 +00:00
|
|
|
pkts, err := aacEncoder.Encode([][]byte{tmsg.Payload}, tmsg.DTS)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while encoding AAC: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pkt := range pkts {
|
|
|
|
res.stream.writeData(&data{
|
|
|
|
trackID: audioTrackID,
|
|
|
|
rtp: pkt,
|
|
|
|
ptsEqualsDTS: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-03 19:10:41 +00:00
|
|
|
}
|
2021-01-30 20:19:50 +00:00
|
|
|
}()
|
2020-10-03 19:10:41 +00:00
|
|
|
}()
|
|
|
|
|
2021-04-04 17:45:41 +00:00
|
|
|
select {
|
2022-07-24 16:11:53 +00:00
|
|
|
case err := <-readDone:
|
|
|
|
nconn.Close()
|
|
|
|
return err
|
|
|
|
|
2022-07-30 19:51:38 +00:00
|
|
|
case <-ctx.Done():
|
2022-07-24 16:11:53 +00:00
|
|
|
nconn.Close()
|
|
|
|
<-readDone
|
|
|
|
return nil
|
2020-10-03 19:10:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
// apiSourceDescribe implements sourceStaticImpl.
|
|
|
|
func (*rtmpSource) apiSourceDescribe() interface{} {
|
2021-07-04 16:13:49 +00:00
|
|
|
return struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
}{"rtmpSource"}
|
|
|
|
}
|