mediamtx/internal/core/rtsp_source.go

209 lines
4.3 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"
2020-10-19 20:17:48 +00:00
"time"
2023-08-26 16:54:28 +00:00
"github.com/bluenviron/gortsplib/v4"
"github.com/bluenviron/gortsplib/v4/pkg/base"
"github.com/bluenviron/gortsplib/v4/pkg/headers"
"github.com/pion/rtp"
2023-08-26 16:54:28 +00:00
"github.com/bluenviron/gortsplib/v4/pkg/url"
2023-05-16 14:14:20 +00:00
"github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/logger"
2020-10-19 20:17:48 +00:00
)
func createRangeHeader(cnf *conf.PathConf) (*headers.Range, error) {
switch cnf.RtspRangeType {
case conf.RtspRangeTypeClock:
start, err := time.Parse("20060102T150405Z", cnf.RtspRangeStart)
if err != nil {
return nil, err
}
return &headers.Range{
Value: &headers.RangeUTC{
Start: start,
},
}, nil
case conf.RtspRangeTypeNPT:
start, err := time.ParseDuration(cnf.RtspRangeStart)
if err != nil {
return nil, err
}
return &headers.Range{
Value: &headers.RangeNPT{
Start: start,
},
}, nil
case conf.RtspRangeTypeSMPTE:
start, err := time.ParseDuration(cnf.RtspRangeStart)
if err != nil {
return nil, err
}
return &headers.Range{
Value: &headers.RangeSMPTE{
Start: headers.RangeSMPTETime{
Time: start,
},
},
}, nil
default:
return nil, nil
}
}
2023-07-30 21:39:24 +00:00
type rtspSourceParent interface {
logger.Writer
setReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
setNotReady(req pathSourceStaticSetNotReadyReq)
2023-07-30 21:39:24 +00:00
}
type rtspSource struct {
readTimeout conf.StringDuration
writeTimeout conf.StringDuration
writeQueueSize int
parent rtspSourceParent
2023-07-30 21:39:24 +00:00
}
func newRTSPSource(
readTimeout conf.StringDuration,
writeTimeout conf.StringDuration,
writeQueueSize int,
2023-07-30 21:39:24 +00:00
parent rtspSourceParent,
) *rtspSource {
return &rtspSource{
readTimeout: readTimeout,
writeTimeout: writeTimeout,
writeQueueSize: writeQueueSize,
parent: parent,
2023-07-30 21:39:24 +00:00
}
}
func (s *rtspSource) Log(level logger.Level, format string, args ...interface{}) {
s.parent.Log(level, "[RTSP source] "+format, args...)
2023-07-30 21:39:24 +00:00
}
// run implements sourceStaticImpl.
func (s *rtspSource) run(ctx context.Context, cnf *conf.PathConf, reloadConf chan *conf.PathConf) error {
2022-07-24 16:11:53 +00:00
s.Log(logger.Debug, "connecting")
2020-10-19 20:17:48 +00:00
decodeErrLogger := logger.NewLimitedLogger(s)
2023-08-26 21:34:39 +00:00
2021-11-12 21:29:56 +00:00
c := &gortsplib.Client{
2023-08-26 16:54:28 +00:00
Transport: cnf.SourceProtocol.Transport,
TLSConfig: tlsConfigForFingerprint(cnf.SourceFingerprint),
ReadTimeout: time.Duration(s.readTimeout),
WriteTimeout: time.Duration(s.writeTimeout),
WriteQueueSize: s.writeQueueSize,
AnyPortEnable: cnf.SourceAnyPortEnable,
2021-05-10 21:23:56 +00:00
OnRequest: func(req *base.Request) {
s.Log(logger.Debug, "[c->s] %v", req)
2021-05-10 21:23:56 +00:00
},
OnResponse: func(res *base.Response) {
s.Log(logger.Debug, "[s->c] %v", res)
2021-05-10 21:23:56 +00:00
},
2023-04-09 15:11:54 +00:00
OnTransportSwitch: func(err error) {
s.Log(logger.Warn, err.Error())
},
OnPacketLost: func(err error) {
2023-08-26 21:34:39 +00:00
decodeErrLogger.Log(logger.Warn, err.Error())
2023-04-09 15:11:54 +00:00
},
OnDecodeError: func(err error) {
2023-08-26 21:34:39 +00:00
decodeErrLogger.Log(logger.Warn, err.Error())
2022-10-31 18:10:32 +00:00
},
2021-05-10 21:23:56 +00:00
}
u, err := url.Parse(cnf.Source)
2020-10-19 20:17:48 +00:00
if err != nil {
2022-07-24 16:11:53 +00:00
return err
2020-10-19 20:17:48 +00:00
}
2021-11-12 21:29:56 +00:00
err = c.Start(u.Scheme, u.Host)
if err != nil {
2022-07-24 16:11:53 +00:00
return err
}
defer c.Close()
2021-03-23 19:37:43 +00:00
2021-05-10 21:23:56 +00:00
readErr := make(chan error)
go func() {
2021-11-12 21:29:56 +00:00
readErr <- func() error {
2023-08-26 16:54:28 +00:00
desc, _, err := c.Describe(u)
2021-11-12 21:29:56 +00:00
if err != nil {
return err
}
2023-08-26 16:54:28 +00:00
err = c.SetupAll(desc.BaseURL, desc.Medias)
if err != nil {
return err
2021-11-12 21:29:56 +00:00
}
res := s.parent.setReady(pathSourceStaticSetReadyReq{
2023-08-26 16:54:28 +00:00
desc: desc,
generateRTPPackets: false,
})
2022-01-14 22:42:41 +00:00
if res.err != nil {
return res.err
2021-11-12 21:29:56 +00:00
}
defer s.parent.setNotReady(pathSourceStaticSetNotReadyReq{})
2021-11-12 21:29:56 +00:00
2023-08-26 16:54:28 +00:00
for _, medi := range desc.Medias {
for _, forma := range medi.Formats {
cmedi := medi
cforma := forma
c.OnPacketRTP(cmedi, cforma, func(pkt *rtp.Packet) {
2023-08-26 16:54:28 +00:00
pts, ok := c.PacketPTS(cmedi, pkt)
if !ok {
return
}
res.stream.WriteRTPPacket(cmedi, cforma, pkt, time.Now(), pts)
})
}
2021-11-12 21:29:56 +00:00
}
rangeHeader, err := createRangeHeader(cnf)
if err != nil {
return err
}
_, err = c.Play(rangeHeader)
2021-11-12 21:29:56 +00:00
if err != nil {
return err
}
return c.Wait()
}()
2021-05-10 21:23:56 +00:00
}()
2020-10-19 20:17:48 +00:00
for {
select {
case err := <-readErr:
return err
case <-reloadConf:
2021-11-12 21:29:56 +00:00
case <-ctx.Done():
c.Close()
<-readErr
return nil
}
2020-10-19 20:17:48 +00:00
}
}
// apiSourceDescribe implements sourceStaticImpl.
func (*rtspSource) apiSourceDescribe() apiPathSourceOrReader {
return apiPathSourceOrReader{
Type: "rtspSource",
ID: "",
}
}