mediamtx/internal/core/rtsp_source.go

221 lines
4.5 KiB
Go
Raw Normal View History

package core
2020-10-19 20:17:48 +00:00
import (
2021-05-10 21:23:56 +00:00
"context"
"crypto/sha256"
"crypto/tls"
"encoding/hex"
"fmt"
"strings"
2020-10-19 20:17:48 +00:00
"sync"
"time"
"github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/base"
"github.com/aler9/rtsp-simple-server/internal/conf"
"github.com/aler9/rtsp-simple-server/internal/logger"
2020-10-19 20:17:48 +00:00
)
const (
rtspSourceRetryPause = 5 * time.Second
2020-10-19 20:17:48 +00:00
)
type rtspSourceParent interface {
2021-10-27 19:01:00 +00:00
log(logger.Level, string, ...interface{})
onSourceStaticSetReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
OnSourceStaticSetNotReady(req pathSourceStaticSetNotReadyReq)
2020-10-19 20:17:48 +00:00
}
type rtspSource struct {
2021-01-10 11:55:53 +00:00
ur string
proto conf.SourceProtocol
anyPortEnable bool
fingerprint string
readTimeout conf.StringDuration
writeTimeout conf.StringDuration
readBufferCount int
readBufferSize int
2021-01-10 11:55:53 +00:00
wg *sync.WaitGroup
parent rtspSourceParent
2020-10-19 20:17:48 +00:00
2021-05-10 19:32:59 +00:00
ctx context.Context
ctxCancel func()
2020-10-19 20:17:48 +00:00
}
func newRTSPSource(
parentCtx context.Context,
ur string,
proto conf.SourceProtocol,
anyPortEnable bool,
fingerprint string,
readTimeout conf.StringDuration,
writeTimeout conf.StringDuration,
readBufferCount int,
readBufferSize int,
wg *sync.WaitGroup,
parent rtspSourceParent) *rtspSource {
ctx, ctxCancel := context.WithCancel(parentCtx)
2021-05-10 19:32:59 +00:00
s := &rtspSource{
2021-01-10 11:55:53 +00:00
ur: ur,
proto: proto,
anyPortEnable: anyPortEnable,
fingerprint: fingerprint,
2021-01-10 11:55:53 +00:00
readTimeout: readTimeout,
writeTimeout: writeTimeout,
readBufferCount: readBufferCount,
readBufferSize: readBufferSize,
2021-01-10 11:55:53 +00:00
wg: wg,
parent: parent,
2021-05-10 19:32:59 +00:00
ctx: ctx,
ctxCancel: ctxCancel,
2020-10-19 20:17:48 +00:00
}
s.log(logger.Info, "started")
s.wg.Add(1)
go s.run()
2021-05-23 16:43:49 +00:00
2020-10-19 20:17:48 +00:00
return s
}
2021-10-27 19:01:00 +00:00
func (s *rtspSource) close() {
s.log(logger.Info, "stopped")
2021-05-10 19:32:59 +00:00
s.ctxCancel()
2020-10-19 20:17:48 +00:00
}
func (s *rtspSource) log(level logger.Level, format string, args ...interface{}) {
2021-10-27 19:01:00 +00:00
s.parent.log(level, "[rtsp source] "+format, args...)
}
func (s *rtspSource) 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 {
case <-time.After(rtspSourceRetryPause):
2020-10-31 16:03:03 +00:00
return true
2021-05-10 19:32:59 +00:00
case <-s.ctx.Done():
2020-10-31 16:03:03 +00:00
return false
}
}()
if !ok {
break
2020-10-19 20:17:48 +00:00
}
}
2021-05-10 19:32:59 +00:00
s.ctxCancel()
2020-10-19 20:17:48 +00:00
}
func (s *rtspSource) runInner() bool {
s.log(logger.Debug, "connecting")
2020-10-19 20:17:48 +00:00
tlsConfig := &tls.Config{}
if s.fingerprint != "" {
tlsConfig.InsecureSkipVerify = true
tlsConfig.VerifyConnection = func(cs tls.ConnectionState) error {
h := sha256.New()
h.Write(cs.PeerCertificates[0].Raw)
hstr := hex.EncodeToString(h.Sum(nil))
fingerprintLower := strings.ToLower(s.fingerprint)
if hstr != fingerprintLower {
return fmt.Errorf("server fingerprint do not match: expected %s, got %s",
fingerprintLower, hstr)
}
return nil
}
}
2021-05-10 21:23:56 +00:00
client := &gortsplib.Client{
Transport: s.proto.Transport,
TLSConfig: tlsConfig,
ReadTimeout: time.Duration(s.readTimeout),
WriteTimeout: time.Duration(s.writeTimeout),
2021-05-10 21:23:56 +00:00
ReadBufferCount: s.readBufferCount,
ReadBufferSize: s.readBufferSize,
AnyPortEnable: s.anyPortEnable,
2021-05-10 21:23:56 +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)
},
}
2021-05-10 19:32:59 +00:00
innerCtx, innerCtxCancel := context.WithCancel(context.Background())
2021-05-10 21:23:56 +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
2021-05-10 21:23:56 +00:00
dialDone := make(chan struct{})
2020-10-19 20:17:48 +00:00
go func() {
defer close(dialDone)
2021-05-10 19:32:59 +00:00
conn, err = client.DialReadContext(innerCtx, s.ur)
2020-10-19 20:17:48 +00:00
}()
select {
2021-05-10 19:32:59 +00:00
case <-s.ctx.Done():
innerCtxCancel()
2021-05-10 21:23:56 +00:00
<-dialDone
2020-10-19 20:17:48 +00:00
return false
2021-05-10 21:23:56 +00:00
2020-10-19 20:17:48 +00:00
case <-dialDone:
2021-05-10 19:32:59 +00:00
innerCtxCancel()
2020-10-19 20:17:48 +00:00
}
if err != nil {
s.log(logger.Info, "ERR: %s", err)
2020-10-19 20:17:48 +00:00
return true
}
2021-10-27 19:01:00 +00:00
res := s.parent.onSourceStaticSetReady(pathSourceStaticSetReadyReq{
Source: s,
2021-03-23 19:37:43 +00:00
Tracks: conn.Tracks(),
})
if res.Err != nil {
2021-08-19 15:28:42 +00:00
s.log(logger.Info, "ERR: %s", res.Err)
return true
}
2021-03-23 19:37:43 +00:00
s.log(logger.Info, "ready")
defer func() {
2021-08-03 20:40:47 +00:00
s.parent.OnSourceStaticSetNotReady(pathSourceStaticSetNotReadyReq{Source: s})
}()
2021-05-10 21:23:56 +00:00
readErr := make(chan error)
go func() {
readErr <- conn.ReadFrames(func(trackID int, streamType gortsplib.StreamType, payload []byte) {
res.Stream.onFrame(trackID, streamType, payload)
2021-05-10 21:23:56 +00:00
})
}()
2020-10-19 20:17:48 +00:00
select {
2021-05-10 19:32:59 +00:00
case <-s.ctx.Done():
conn.Close()
2021-05-10 21:23:56 +00:00
<-readErr
return false
2021-05-10 21:23:56 +00:00
case err := <-readErr:
s.log(logger.Info, "ERR: %s", err)
conn.Close()
return true
2020-10-19 20:17:48 +00:00
}
}
2021-10-27 19:01:00 +00:00
// onSourceAPIDescribe implements source.
func (*rtspSource) onSourceAPIDescribe() interface{} {
return struct {
Type string `json:"type"`
}{"rtspSource"}
}