2021-07-24 13:55:42 +00:00
|
|
|
package core
|
2020-10-19 20:17:48 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-10 21:23:56 +00:00
|
|
|
"context"
|
2021-04-16 20:46:22 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-10-19 20:17:48 +00:00
|
|
|
"time"
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v3"
|
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/base"
|
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/formats"
|
2022-11-02 19:52:12 +00:00
|
|
|
"github.com/pion/rtp"
|
2020-10-25 10:41:41 +00:00
|
|
|
|
2023-04-01 17:52:06 +00:00
|
|
|
"github.com/aler9/mediamtx/internal/conf"
|
|
|
|
"github.com/aler9/mediamtx/internal/formatprocessor"
|
|
|
|
"github.com/aler9/mediamtx/internal/logger"
|
2023-04-01 16:39:12 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/url"
|
2020-10-19 20:17:48 +00:00
|
|
|
)
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type rtspSourceParent interface {
|
2021-10-27 19:01:00 +00:00
|
|
|
log(logger.Level, string, ...interface{})
|
2022-08-04 19:07:17 +00:00
|
|
|
sourceStaticImplSetReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
|
|
|
|
sourceStaticImplSetNotReady(req pathSourceStaticSetNotReadyReq)
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type rtspSource struct {
|
2021-09-26 21:06:40 +00:00
|
|
|
readTimeout conf.StringDuration
|
|
|
|
writeTimeout conf.StringDuration
|
2021-02-18 22:26:45 +00:00
|
|
|
readBufferCount int
|
2021-07-24 13:55:42 +00:00
|
|
|
parent rtspSourceParent
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func newRTSPSource(
|
2021-09-26 21:06:40 +00:00
|
|
|
readTimeout conf.StringDuration,
|
|
|
|
writeTimeout conf.StringDuration,
|
2021-02-18 22:26:45 +00:00
|
|
|
readBufferCount int,
|
2022-04-07 10:50:35 +00:00
|
|
|
parent rtspSourceParent,
|
|
|
|
) *rtspSource {
|
2022-07-30 19:51:38 +00:00
|
|
|
return &rtspSource{
|
2021-01-10 11:55:53 +00:00
|
|
|
readTimeout: readTimeout,
|
|
|
|
writeTimeout: writeTimeout,
|
|
|
|
readBufferCount: readBufferCount,
|
|
|
|
parent: parent,
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 16:11:53 +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...)
|
2020-12-08 11:21:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 19:51:38 +00:00
|
|
|
// run implements sourceStaticImpl.
|
2023-02-13 11:12:04 +00:00
|
|
|
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
|
|
|
|
2021-12-03 22:15:21 +00:00
|
|
|
var tlsConfig *tls.Config
|
2023-02-13 11:12:04 +00:00
|
|
|
if cnf.SourceFingerprint != "" {
|
2021-12-03 22:15:21 +00:00
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
VerifyConnection: func(cs tls.ConnectionState) error {
|
|
|
|
h := sha256.New()
|
|
|
|
h.Write(cs.PeerCertificates[0].Raw)
|
|
|
|
hstr := hex.EncodeToString(h.Sum(nil))
|
2023-02-13 11:12:04 +00:00
|
|
|
fingerprintLower := strings.ToLower(cnf.SourceFingerprint)
|
2021-12-03 22:15:21 +00:00
|
|
|
|
|
|
|
if hstr != fingerprintLower {
|
|
|
|
return fmt.Errorf("server fingerprint do not match: expected %s, got %s",
|
|
|
|
fingerprintLower, hstr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2021-10-25 19:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 21:29:56 +00:00
|
|
|
c := &gortsplib.Client{
|
2023-02-13 11:12:04 +00:00
|
|
|
Transport: cnf.SourceProtocol.Transport,
|
2021-10-25 19:01:29 +00:00
|
|
|
TLSConfig: tlsConfig,
|
2021-09-26 21:06:40 +00:00
|
|
|
ReadTimeout: time.Duration(s.readTimeout),
|
|
|
|
WriteTimeout: time.Duration(s.writeTimeout),
|
2021-05-10 21:23:56 +00:00
|
|
|
ReadBufferCount: s.readBufferCount,
|
2023-02-13 11:12:04 +00:00
|
|
|
AnyPortEnable: cnf.SourceAnyPortEnable,
|
2021-05-10 21:23:56 +00:00
|
|
|
OnRequest: func(req *base.Request) {
|
2022-07-24 16:11:53 +00:00
|
|
|
s.Log(logger.Debug, "c->s %v", req)
|
2021-05-10 21:23:56 +00:00
|
|
|
},
|
|
|
|
OnResponse: func(res *base.Response) {
|
2022-07-24 16:11:53 +00:00
|
|
|
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) {
|
|
|
|
s.Log(logger.Warn, err.Error())
|
|
|
|
},
|
|
|
|
OnDecodeError: func(err error) {
|
|
|
|
s.Log(logger.Warn, err.Error())
|
2022-10-31 18:10:32 +00:00
|
|
|
},
|
2021-05-10 21:23:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 11:12:04 +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
|
2021-08-10 16:34:10 +00:00
|
|
|
}
|
2022-01-23 18:36:33 +00:00
|
|
|
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 {
|
2022-12-13 19:54:17 +00:00
|
|
|
medias, baseURL, _, err := c.Describe(u)
|
2021-11-12 21:29:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
err = c.SetupAll(medias, baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-11-12 21:29:56 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 11:01:06 +00:00
|
|
|
res := s.parent.sourceStaticImplSetReady(pathSourceStaticSetReadyReq{
|
2022-12-13 19:54:17 +00:00
|
|
|
medias: medias,
|
2022-08-14 11:01:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
s.Log(logger.Info, "ready: %s", sourceMediaInfo(medias))
|
2021-11-12 21:29:56 +00:00
|
|
|
|
|
|
|
defer func() {
|
2022-08-04 19:07:17 +00:00
|
|
|
s.parent.sourceStaticImplSetNotReady(pathSourceStaticSetNotReadyReq{})
|
2021-11-12 21:29:56 +00:00
|
|
|
}()
|
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
for _, medi := range medias {
|
|
|
|
for _, forma := range medi.Formats {
|
|
|
|
cmedia := medi
|
|
|
|
cformat := forma
|
|
|
|
|
|
|
|
switch forma.(type) {
|
2023-04-01 16:39:12 +00:00
|
|
|
case *formats.H264:
|
2022-12-13 19:54:17 +00:00
|
|
|
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
|
2023-03-10 11:44:59 +00:00
|
|
|
err := res.stream.writeData(cmedia, cformat, &formatprocessor.UnitH264{
|
2023-01-05 11:54:00 +00:00
|
|
|
RTPPackets: []*rtp.Packet{pkt},
|
|
|
|
NTP: time.Now(),
|
2022-12-13 19:54:17 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.Log(logger.Warn, "%v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
case *formats.H265:
|
2022-12-13 20:26:35 +00:00
|
|
|
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
|
2023-03-10 11:44:59 +00:00
|
|
|
err := res.stream.writeData(cmedia, cformat, &formatprocessor.UnitH265{
|
2023-01-05 11:54:00 +00:00
|
|
|
RTPPackets: []*rtp.Packet{pkt},
|
|
|
|
NTP: time.Now(),
|
2022-12-13 20:26:35 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.Log(logger.Warn, "%v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
case *formats.VP8:
|
2022-12-13 19:54:17 +00:00
|
|
|
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
|
2023-03-10 11:44:59 +00:00
|
|
|
err := res.stream.writeData(cmedia, cformat, &formatprocessor.UnitVP8{
|
2023-01-05 11:54:00 +00:00
|
|
|
RTPPackets: []*rtp.Packet{pkt},
|
|
|
|
NTP: time.Now(),
|
2022-12-13 19:54:17 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.Log(logger.Warn, "%v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
case *formats.VP9:
|
2022-12-13 20:26:35 +00:00
|
|
|
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
|
2023-03-10 11:44:59 +00:00
|
|
|
err := res.stream.writeData(cmedia, cformat, &formatprocessor.UnitVP9{
|
2023-01-05 11:54:00 +00:00
|
|
|
RTPPackets: []*rtp.Packet{pkt},
|
|
|
|
NTP: time.Now(),
|
2022-12-15 23:50:47 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.Log(logger.Warn, "%v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
case *formats.MPEG4Audio:
|
2022-12-15 23:50:47 +00:00
|
|
|
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
|
2023-03-10 11:44:59 +00:00
|
|
|
err := res.stream.writeData(cmedia, cformat, &formatprocessor.UnitMPEG4Audio{
|
2023-01-05 11:54:00 +00:00
|
|
|
RTPPackets: []*rtp.Packet{pkt},
|
|
|
|
NTP: time.Now(),
|
2022-12-13 20:26:35 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.Log(logger.Warn, "%v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
case *formats.Opus:
|
2023-01-03 17:36:13 +00:00
|
|
|
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
|
2023-03-10 11:44:59 +00:00
|
|
|
err := res.stream.writeData(cmedia, cformat, &formatprocessor.UnitOpus{
|
2023-01-05 11:54:00 +00:00
|
|
|
RTPPackets: []*rtp.Packet{pkt},
|
|
|
|
NTP: time.Now(),
|
2023-01-03 17:36:13 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.Log(logger.Warn, "%v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
default:
|
|
|
|
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
|
2023-03-10 11:44:59 +00:00
|
|
|
err := res.stream.writeData(cmedia, cformat, &formatprocessor.UnitGeneric{
|
2023-01-05 11:54:00 +00:00
|
|
|
RTPPackets: []*rtp.Packet{pkt},
|
|
|
|
NTP: time.Now(),
|
2022-12-13 19:54:17 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.Log(logger.Warn, "%v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-11-02 19:52:12 +00:00
|
|
|
}
|
2021-11-12 21:29:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.Play(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Wait()
|
|
|
|
}()
|
2021-05-10 21:23:56 +00:00
|
|
|
}()
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2023-02-13 11:12:04 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-readErr:
|
|
|
|
return err
|
|
|
|
|
|
|
|
case <-reloadConf:
|
2021-11-12 21:29:56 +00:00
|
|
|
|
2023-02-13 11:12:04 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
c.Close()
|
|
|
|
<-readErr
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
// apiSourceDescribe implements sourceStaticImpl.
|
|
|
|
func (*rtspSource) apiSourceDescribe() interface{} {
|
2021-07-04 16:13:49 +00:00
|
|
|
return struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
}{"rtspSource"}
|
|
|
|
}
|