mediamtx/internal/sourcertsp/source.go

173 lines
3.3 KiB
Go
Raw Normal View History

2020-10-19 20:17:48 +00:00
package sourcertsp
import (
"sync"
"sync/atomic"
2020-10-19 20:17:48 +00:00
"time"
"github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/base"
"github.com/aler9/rtsp-simple-server/internal/logger"
2020-11-01 21:56:56 +00:00
"github.com/aler9/rtsp-simple-server/internal/stats"
2020-10-19 20:17:48 +00:00
)
const (
retryPause = 5 * time.Second
2020-10-19 20:17:48 +00:00
)
2020-11-05 11:30:25 +00:00
// Parent is implemented by path.Path.
2020-10-19 20:17:48 +00:00
type Parent interface {
Log(logger.Level, string, ...interface{})
OnSourceSetReady(gortsplib.Tracks)
OnSourceSetNotReady()
2020-10-19 20:17:48 +00:00
OnFrame(int, gortsplib.StreamType, []byte)
}
2020-11-05 11:30:25 +00:00
// Source is a RTSP source.
2020-10-19 20:17:48 +00:00
type Source struct {
2021-01-10 11:55:53 +00:00
ur string
proto *gortsplib.StreamProtocol
readTimeout time.Duration
writeTimeout time.Duration
readBufferCount uint64
wg *sync.WaitGroup
stats *stats.Stats
parent Parent
2020-10-19 20:17:48 +00:00
// in
terminate chan struct{}
2020-10-19 20:17:48 +00:00
}
2020-11-05 11:30:25 +00:00
// New allocates a Source.
2020-10-19 20:17:48 +00:00
func New(ur string,
proto *gortsplib.StreamProtocol,
2020-10-19 20:17:48 +00:00
readTimeout time.Duration,
writeTimeout time.Duration,
2021-01-10 11:55:53 +00:00
readBufferCount uint64,
wg *sync.WaitGroup,
stats *stats.Stats,
2020-10-19 20:17:48 +00:00
parent Parent) *Source {
s := &Source{
2021-01-10 11:55:53 +00:00
ur: ur,
proto: proto,
readTimeout: readTimeout,
writeTimeout: writeTimeout,
readBufferCount: readBufferCount,
wg: wg,
stats: stats,
parent: parent,
terminate: make(chan struct{}),
2020-10-19 20:17:48 +00:00
}
atomic.AddInt64(s.stats.CountSourcesRtsp, +1)
s.log(logger.Info, "started")
s.wg.Add(1)
go s.run()
2020-10-19 20:17:48 +00:00
return s
}
2020-11-05 11:30:25 +00:00
// Close closes a Source.
2020-10-19 20:17:48 +00:00
func (s *Source) Close() {
atomic.AddInt64(s.stats.CountSourcesRtsp, -1)
s.log(logger.Info, "stopped")
2020-10-19 20:17:48 +00:00
close(s.terminate)
}
2020-11-05 11:30:25 +00:00
// IsSource implements path.source.
2020-10-19 20:17:48 +00:00
func (s *Source) IsSource() {}
2020-11-05 11:30:25 +00:00
// IsSourceExternal implements path.sourceExternal.
func (s *Source) IsSourceExternal() {}
2020-10-19 20:17:48 +00:00
func (s *Source) log(level logger.Level, format string, args ...interface{}) {
s.parent.Log(level, "[rtsp source] "+format, args...)
}
func (s *Source) run() {
defer s.wg.Done()
2020-10-19 20:17:48 +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-19 20:17:48 +00:00
t := time.NewTimer(retryPause)
2020-10-31 16:03:03 +00:00
defer t.Stop()
2020-10-19 20:17:48 +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-19 20:17:48 +00:00
}
}
}
func (s *Source) runInner() bool {
s.log(logger.Info, "connecting")
2020-10-19 20:17:48 +00:00
2020-12-06 17:01:10 +00:00
var conn *gortsplib.ClientConn
2020-10-19 20:17:48 +00:00
var err error
dialDone := make(chan struct{}, 1)
go func() {
defer close(dialDone)
2020-11-15 16:56:54 +00:00
conf := gortsplib.ClientConf{
2020-11-15 16:56:54 +00:00
StreamProtocol: s.proto,
2020-10-19 20:17:48 +00:00
ReadTimeout: s.readTimeout,
WriteTimeout: s.writeTimeout,
2021-01-10 11:55:53 +00:00
ReadBufferCount: s.readBufferCount,
OnRequest: func(req *base.Request) {
s.log(logger.Debug, "c->s %v", req)
},
OnResponse: func(res *base.Response) {
s.log(logger.Debug, "s->c %v", res)
},
2020-11-15 16:56:54 +00:00
}
conn, err = conf.DialRead(s.ur)
2020-10-19 20:17:48 +00:00
}()
select {
case <-s.terminate:
2020-10-19 20:17:48 +00:00
return false
case <-dialDone:
}
if err != nil {
s.log(logger.Info, "ERR: %s", err)
2020-10-19 20:17:48 +00:00
return true
}
tracks := conn.Tracks()
2020-10-19 20:17:48 +00:00
s.log(logger.Info, "ready")
s.parent.OnSourceSetReady(tracks)
defer s.parent.OnSourceSetNotReady()
2020-10-19 20:17:48 +00:00
2021-01-06 11:40:18 +00:00
done := conn.ReadFrames(func(trackID int, streamType gortsplib.StreamType, payload []byte) {
s.parent.OnFrame(trackID, streamType, payload)
2020-11-15 19:15:06 +00:00
})
2020-10-19 20:17:48 +00:00
for {
select {
case <-s.terminate:
2020-10-19 20:17:48 +00:00
conn.Close()
<-done
return false
2020-10-19 20:17:48 +00:00
case err := <-done:
2020-10-19 20:17:48 +00:00
conn.Close()
s.log(logger.Info, "ERR: %s", err)
return true
2020-10-19 20:17:48 +00:00
}
}
}