mediamtx/internal/sourcertmp/source.go

306 lines
5.4 KiB
Go
Raw Normal View History

2020-10-19 20:17:48 +00:00
package sourcertmp
2020-10-03 19:10:41 +00:00
import (
"fmt"
"net"
"sync"
2020-10-03 19:10:41 +00:00
"sync/atomic"
"time"
"github.com/aler9/gortsplib"
2020-10-05 19:07:34 +00:00
"github.com/aler9/gortsplib/rtpaac"
"github.com/aler9/gortsplib/rtph264"
2020-10-03 19:10:41 +00:00
"github.com/notedit/rtmp/av"
"github.com/notedit/rtmp/codec/h264"
"github.com/notedit/rtmp/format/rtmp"
2020-11-01 21:56:56 +00:00
"github.com/aler9/rtsp-simple-server/internal/stats"
2020-10-03 19:10:41 +00:00
)
const (
retryPause = 5 * time.Second
2020-10-03 19:10:41 +00:00
)
2020-10-19 20:17:48 +00:00
type Parent interface {
Log(string, ...interface{})
OnSourceSetReady(gortsplib.Tracks)
OnSourceSetNotReady()
2020-10-19 20:17:48 +00:00
OnFrame(int, gortsplib.StreamType, []byte)
}
type Source struct {
ur string
state bool
wg *sync.WaitGroup
stats *stats.Stats
2020-10-19 20:17:48 +00:00
parent Parent
// in
terminate chan struct{}
2020-10-03 19:10:41 +00:00
}
2020-10-19 20:17:48 +00:00
func New(ur string,
wg *sync.WaitGroup,
stats *stats.Stats,
2020-10-19 20:17:48 +00:00
parent Parent) *Source {
s := &Source{
ur: ur,
wg: wg,
stats: stats,
parent: parent,
terminate: make(chan struct{}),
2020-10-03 19:10:41 +00:00
}
atomic.AddInt64(s.stats.CountSourcesRtmp, +1)
s.parent.Log("rtmp source started")
s.wg.Add(1)
go s.run()
2020-10-19 20:17:48 +00:00
return s
}
2020-10-03 19:10:41 +00:00
2020-10-19 20:17:48 +00:00
func (s *Source) Close() {
atomic.AddInt64(s.stats.CountSourcesRtmpRunning, -1)
s.parent.Log("rtmp source stopped")
2020-10-19 20:17:48 +00:00
close(s.terminate)
}
2020-10-03 19:10:41 +00:00
2020-10-19 20:17:48 +00:00
func (s *Source) IsSource() {}
func (s *Source) IsSourceExternal() {}
2020-10-03 19:10:41 +00:00
func (s *Source) run() {
defer s.wg.Done()
2020-10-03 19:10:41 +00:00
for {
2020-10-31 16:03:03 +00:00
ok := func() bool {
ok := s.runInner()
2020-10-31 16:03:03 +00:00
if !ok {
return false
}
2020-10-03 19:10:41 +00:00
t := time.NewTimer(retryPause)
2020-10-31 16:03:03 +00:00
defer t.Stop()
2020-10-03 19:10:41 +00:00
2020-10-31 16:03:03 +00:00
select {
case <-t.C:
return true
case <-s.terminate:
2020-10-31 16:03:03 +00:00
return false
}
}()
if !ok {
break
2020-10-03 19:10:41 +00:00
}
}
}
func (s *Source) runInner() bool {
2020-10-19 20:17:48 +00:00
s.parent.Log("connecting to rtmp source")
2020-10-03 19:10:41 +00:00
var conn *rtmp.Conn
var nconn net.Conn
var err error
dialDone := make(chan struct{}, 1)
go func() {
defer close(dialDone)
2020-10-19 20:17:48 +00:00
conn, nconn, err = rtmp.NewClient().Dial(s.ur, rtmp.PrepareReading)
2020-10-03 19:10:41 +00:00
}()
select {
case <-s.terminate:
2020-10-03 19:10:41 +00:00
return false
case <-dialDone:
}
if err != nil {
2020-10-19 20:17:48 +00:00
s.parent.Log("rtmp source ERR: %s", err)
2020-10-03 19:10:41 +00:00
return true
}
2020-10-05 19:07:34 +00:00
// gather video and audio features
var h264Sps []byte
var h264Pps []byte
var aacConfig []byte
confDone := make(chan struct{})
confClose := uint32(0)
go func() {
defer close(confDone)
2020-10-03 19:10:41 +00:00
for {
2020-10-05 19:07:34 +00:00
var pkt av.Packet
pkt, err = conn.ReadPacket()
2020-10-03 19:10:41 +00:00
if err != nil {
2020-10-05 19:07:34 +00:00
return
}
if atomic.LoadUint32(&confClose) > 0 {
return
2020-10-03 19:10:41 +00:00
}
2020-10-05 19:07:34 +00:00
switch pkt.Type {
case av.H264DecoderConfig:
2020-10-03 19:10:41 +00:00
codec, err := h264.FromDecoderConfig(pkt.Data)
if err != nil {
panic(err)
}
2020-10-05 19:07:34 +00:00
h264Sps = codec.SPS[0]
h264Pps = codec.PPS[0]
if aacConfig != nil {
return
}
case av.AACDecoderConfig:
aacConfig = pkt.Data
if h264Sps != nil {
return
}
2020-10-03 19:10:41 +00:00
}
}
}()
2020-10-05 19:07:34 +00:00
timer := time.NewTimer(5 * time.Second)
2020-10-31 16:03:03 +00:00
defer timer.Stop()
2020-10-05 19:07:34 +00:00
select {
case <-confDone:
case <-timer.C:
atomic.StoreUint32(&confClose, 1)
<-confDone
}
2020-10-03 19:10:41 +00:00
if err != nil {
2020-10-19 20:17:48 +00:00
s.parent.Log("rtmp source ERR: %s", err)
2020-10-03 19:10:41 +00:00
return true
}
2020-10-05 19:07:34 +00:00
var tracks gortsplib.Tracks
var videoTrack *gortsplib.Track
var audioTrack *gortsplib.Track
var h264Encoder *rtph264.Encoder
var aacEncoder *rtpaac.Encoder
if h264Sps != nil {
videoTrack, err = gortsplib.NewTrackH264(len(tracks), h264Sps, h264Pps)
if err != nil {
2020-10-19 20:17:48 +00:00
s.parent.Log("rtmp source ERR: %s", err)
2020-10-05 19:07:34 +00:00
return true
}
h264Encoder, err = rtph264.NewEncoder(uint8(len(tracks)))
if err != nil {
2020-10-19 20:17:48 +00:00
s.parent.Log("rtmp source ERR: %s", err)
2020-10-05 19:07:34 +00:00
return true
}
tracks = append(tracks, videoTrack)
}
if aacConfig != nil {
audioTrack, err = gortsplib.NewTrackAac(len(tracks), aacConfig)
if err != nil {
2020-10-19 20:17:48 +00:00
s.parent.Log("rtmp source ERR: %s", err)
2020-10-05 19:07:34 +00:00
return true
}
aacEncoder, err = rtpaac.NewEncoder(uint8(len(tracks)), aacConfig)
if err != nil {
2020-10-19 20:17:48 +00:00
s.parent.Log("rtmp source ERR: %s", err)
2020-10-05 19:07:34 +00:00
return true
}
tracks = append(tracks, audioTrack)
}
if len(tracks) == 0 {
2020-10-19 20:17:48 +00:00
s.parent.Log("rtmp source ERR: no tracks found")
2020-10-05 19:07:34 +00:00
return true
}
2020-10-19 20:17:48 +00:00
s.parent.Log("rtmp source ready")
s.parent.OnSourceSetReady(tracks)
2020-10-03 19:10:41 +00:00
readDone := make(chan error)
go func() {
for {
pkt, err := conn.ReadPacket()
if err != nil {
readDone <- err
return
}
2020-10-05 19:07:34 +00:00
switch pkt.Type {
case av.H264:
if h264Sps == nil {
readDone <- fmt.Errorf("rtmp source ERR: received an H264 frame, but track is not setup up")
return
}
2020-10-03 19:10:41 +00:00
// decode from AVCC format
nalus, typ := h264.SplitNALUs(pkt.Data)
if typ != h264.NALU_AVCC {
readDone <- fmt.Errorf("invalid NALU format (%d)", typ)
return
}
// encode into RTP/H264 format
2020-10-05 19:39:55 +00:00
frames, err := h264Encoder.Write(nalus, pkt.Time+pkt.CTime)
2020-10-03 19:10:41 +00:00
if err != nil {
readDone <- err
return
}
for _, f := range frames {
2020-10-19 20:17:48 +00:00
s.parent.OnFrame(videoTrack.Id, gortsplib.StreamTypeRtp, f)
2020-10-03 19:10:41 +00:00
}
2020-10-05 19:07:34 +00:00
case av.AAC:
if aacConfig == nil {
readDone <- fmt.Errorf("rtmp source ERR: received an AAC frame, but track is not setup up")
return
}
2020-10-05 19:39:55 +00:00
frames, err := aacEncoder.Write(pkt.Data, pkt.Time+pkt.CTime)
2020-10-05 19:07:34 +00:00
if err != nil {
readDone <- err
return
}
for _, f := range frames {
2020-10-19 20:17:48 +00:00
s.parent.OnFrame(audioTrack.Id, gortsplib.StreamTypeRtp, f)
2020-10-05 19:07:34 +00:00
}
default:
readDone <- fmt.Errorf("rtmp source ERR: unexpected packet: %v", pkt.Type)
return
2020-10-03 19:10:41 +00:00
}
}
}()
var ret bool
outer:
for {
select {
case <-s.terminate:
2020-10-03 19:10:41 +00:00
nconn.Close()
<-readDone
ret = false
break outer
case err := <-readDone:
nconn.Close()
2020-10-19 20:17:48 +00:00
s.parent.Log("rtmp source ERR: %s", err)
2020-10-03 19:10:41 +00:00
ret = true
break outer
}
}
s.parent.OnSourceSetNotReady()
2020-10-03 19:10:41 +00:00
return ret
}