mediamtx/internal/sourcertsp/source.go

183 lines
3.6 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"
"github.com/aler9/rtsp-simple-server/internal/source"
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{})
OnExtSourceSetReady(req source.ExtSetReadyReq)
OnExtSourceSetNotReady(req source.ExtSetNotReadyReq)
2020-10-19 20:17:48 +00:00
}
// Source is a RTSP external 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 int
readBufferSize int
2021-01-10 11:55:53 +00:00
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,
readBufferCount int,
readBufferSize int,
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,
readBufferSize: readBufferSize,
2021-01-10 11:55:53 +00:00
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)
}
// IsSource implements source.Source.
2020-10-19 20:17:48 +00:00
func (s *Source) IsSource() {}
2021-03-14 16:27:04 +00:00
// IsExtSource implements path.extSource.
func (s *Source) IsExtSource() {}
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
2020-10-31 16:03:03 +00:00
select {
2021-03-14 16:27:04 +00:00
case <-time.After(retryPause):
2020-10-31 16:03:03 +00:00
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,
ReadBufferSize: s.readBufferSize,
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
}
s.log(logger.Info, "ready")
2021-03-23 19:37:43 +00:00
cres := make(chan source.ExtSetReadyRes)
s.parent.OnExtSourceSetReady(source.ExtSetReadyReq{
2021-03-23 19:37:43 +00:00
Tracks: conn.Tracks(),
Res: cres,
})
2021-03-23 19:37:43 +00:00
res := <-cres
defer func() {
res := make(chan struct{})
s.parent.OnExtSourceSetNotReady(source.ExtSetNotReadyReq{
Res: res,
})
<-res
}()
2021-01-06 11:40:18 +00:00
done := conn.ReadFrames(func(trackID int, streamType gortsplib.StreamType, payload []byte) {
2021-03-23 19:37:43 +00:00
res.SP.OnFrame(trackID, streamType, payload)
2020-11-15 19:15:06 +00:00
})
2020-10-19 20:17:48 +00:00
select {
case <-s.terminate:
conn.Close()
<-done
return false
case err := <-done:
s.log(logger.Info, "ERR: %s", err)
conn.Close()
return true
2020-10-19 20:17:48 +00:00
}
}