2020-10-19 20:17:48 +00:00
|
|
|
package sourcertsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2020-10-25 10:41:41 +00:00
|
|
|
"sync/atomic"
|
2020-10-19 20:17:48 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aler9/gortsplib"
|
2020-12-08 11:21:06 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/base"
|
2020-10-25 10:41:41 +00:00
|
|
|
|
2020-12-08 11:21:06 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
2021-03-22 20:40:07 +00:00
|
|
|
"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 (
|
2020-11-01 15:51:12 +00:00
|
|
|
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 {
|
2020-12-08 11:21:06 +00:00
|
|
|
Log(logger.Level, string, ...interface{})
|
2021-03-22 20:40:07 +00:00
|
|
|
OnExtSourceSetReady(req source.ExtSetReadyReq)
|
|
|
|
OnExtSourceSetNotReady(req source.ExtSetNotReadyReq)
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 20:40:07 +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
|
2021-02-18 22:26:45 +00:00
|
|
|
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
|
2020-11-01 15:51:12 +00:00
|
|
|
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,
|
2020-11-21 12:34:27 +00:00
|
|
|
proto *gortsplib.StreamProtocol,
|
2020-10-19 20:17:48 +00:00
|
|
|
readTimeout time.Duration,
|
|
|
|
writeTimeout time.Duration,
|
2021-02-18 22:26:45 +00:00
|
|
|
readBufferCount int,
|
|
|
|
readBufferSize int,
|
2020-11-01 15:51:12 +00:00
|
|
|
wg *sync.WaitGroup,
|
2020-10-25 10:41:41 +00:00
|
|
|
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,
|
2021-02-18 22:26:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-25 10:41:41 +00:00
|
|
|
atomic.AddInt64(s.stats.CountSourcesRtsp, +1)
|
2020-12-08 11:21:06 +00:00
|
|
|
s.log(logger.Info, "started")
|
2020-10-25 10:41:41 +00:00
|
|
|
|
2020-11-01 15:51:12 +00:00
|
|
|
s.wg.Add(1)
|
2020-10-25 10:41:41 +00:00
|
|
|
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() {
|
2020-11-01 15:51:12 +00:00
|
|
|
atomic.AddInt64(s.stats.CountSourcesRtsp, -1)
|
2020-12-08 11:21:06 +00:00
|
|
|
s.log(logger.Info, "stopped")
|
2020-10-19 20:17:48 +00:00
|
|
|
close(s.terminate)
|
|
|
|
}
|
|
|
|
|
2021-03-22 20:40:07 +00:00
|
|
|
// 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
|
|
|
|
2020-12-08 11:21:06 +00:00
|
|
|
func (s *Source) log(level logger.Level, format string, args ...interface{}) {
|
|
|
|
s.parent.Log(level, "[rtsp source] "+format, args...)
|
|
|
|
}
|
|
|
|
|
2020-10-25 10:41:41 +00:00
|
|
|
func (s *Source) run() {
|
2020-11-01 15:51:12 +00:00
|
|
|
defer s.wg.Done()
|
2020-10-19 20:17:48 +00:00
|
|
|
|
|
|
|
for {
|
2020-10-31 16:03:03 +00:00
|
|
|
ok := func() bool {
|
2020-11-01 15:51:12 +00:00
|
|
|
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
|
2020-11-01 15:51:12 +00:00
|
|
|
case <-s.terminate:
|
2020-10-31 16:03:03 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if !ok {
|
|
|
|
break
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 15:51:12 +00:00
|
|
|
func (s *Source) runInner() bool {
|
2020-12-08 11:21:06 +00:00
|
|
|
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
|
|
|
|
2020-12-08 11:21:06 +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,
|
2021-02-18 22:26:45 +00:00
|
|
|
ReadBufferSize: s.readBufferSize,
|
2020-12-08 11:21:06 +00:00
|
|
|
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
|
|
|
}
|
2020-12-08 11:21:06 +00:00
|
|
|
conn, err = conf.DialRead(s.ur)
|
2020-10-19 20:17:48 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2020-11-01 15:51:12 +00:00
|
|
|
case <-s.terminate:
|
2020-10-19 20:17:48 +00:00
|
|
|
return false
|
|
|
|
case <-dialDone:
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2020-12-08 11:21:06 +00:00
|
|
|
s.log(logger.Info, "ERR: %s", err)
|
2020-10-19 20:17:48 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-08 11:21:06 +00:00
|
|
|
s.log(logger.Info, "ready")
|
2021-03-23 19:37:43 +00:00
|
|
|
|
|
|
|
cres := make(chan source.ExtSetReadyRes)
|
2021-03-22 20:40:07 +00:00
|
|
|
s.parent.OnExtSourceSetReady(source.ExtSetReadyReq{
|
2021-03-23 19:37:43 +00:00
|
|
|
Tracks: conn.Tracks(),
|
|
|
|
Res: cres,
|
2021-03-22 20:40:07 +00:00
|
|
|
})
|
2021-03-23 19:37:43 +00:00
|
|
|
res := <-cres
|
|
|
|
|
2021-03-22 20:40:07 +00:00
|
|
|
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
|
|
|
|
2021-04-04 17:45:41 +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
|
|
|
}
|
|
|
|
}
|