mediamtx/internal/core/rtsp_source.go

257 lines
6.0 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
"time"
"github.com/aler9/gortsplib/v2"
"github.com/aler9/gortsplib/v2/pkg/base"
"github.com/aler9/gortsplib/v2/pkg/format"
"github.com/pion/rtp"
"github.com/aler9/gortsplib/v2/pkg/url"
"github.com/aler9/rtsp-simple-server/internal/conf"
"github.com/aler9/rtsp-simple-server/internal/logger"
2020-10-19 20:17:48 +00:00
)
type rtspSourceParent interface {
2021-10-27 19:01:00 +00:00
log(logger.Level, string, ...interface{})
sourceStaticImplSetReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
sourceStaticImplSetNotReady(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
parent rtspSourceParent
2020-10-19 20:17:48 +00:00
}
func newRTSPSource(
ur string,
proto conf.SourceProtocol,
anyPortEnable bool,
fingerprint string,
readTimeout conf.StringDuration,
writeTimeout conf.StringDuration,
readBufferCount int,
2022-04-07 10:50:35 +00:00
parent rtspSourceParent,
) *rtspSource {
return &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,
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...)
}
// run implements sourceStaticImpl.
func (s *rtspSource) run(ctx context.Context) error {
2022-07-24 16:11:53 +00:00
s.Log(logger.Debug, "connecting")
2020-10-19 20:17:48 +00:00
var tlsConfig *tls.Config
if s.fingerprint != "" {
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))
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-11-12 21:29:56 +00:00
c := &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,
AnyPortEnable: s.anyPortEnable,
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
},
2022-10-31 18:10:32 +00:00
OnDecodeError: func(err error) {
s.Log(logger.Warn, "%v", err)
},
2021-05-10 21:23:56 +00:00
}
2022-06-04 23:36:29 +00:00
u, err := url.Parse(s.ur)
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 {
medias, baseURL, _, err := c.Describe(u)
2021-11-12 21:29:56 +00:00
if err != nil {
return err
}
err = c.SetupAll(medias, baseURL)
if err != nil {
return err
2021-11-12 21:29:56 +00:00
}
res := s.parent.sourceStaticImplSetReady(pathSourceStaticSetReadyReq{
medias: medias,
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
}
s.Log(logger.Info, "ready: %s", sourceMediaInfo(medias))
2021-11-12 21:29:56 +00:00
defer func() {
s.parent.sourceStaticImplSetNotReady(pathSourceStaticSetNotReadyReq{})
2021-11-12 21:29:56 +00:00
}()
for _, medi := range medias {
for _, forma := range medi.Formats {
cmedia := medi
cformat := forma
switch forma.(type) {
case *format.H264:
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
err := res.stream.writeData(cmedia, cformat, &dataH264{
rtpPackets: []*rtp.Packet{pkt},
ntp: time.Now(),
})
if err != nil {
s.Log(logger.Warn, "%v", err)
}
})
case *format.H265:
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
err := res.stream.writeData(cmedia, cformat, &dataH265{
rtpPackets: []*rtp.Packet{pkt},
ntp: time.Now(),
})
if err != nil {
s.Log(logger.Warn, "%v", err)
}
})
2022-12-15 23:50:47 +00:00
case *format.VP8:
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
2022-12-15 23:50:47 +00:00
err := res.stream.writeData(cmedia, cformat, &dataVP8{
rtpPackets: []*rtp.Packet{pkt},
ntp: time.Now(),
})
if err != nil {
s.Log(logger.Warn, "%v", err)
}
})
2022-12-15 23:50:47 +00:00
case *format.VP9:
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
2022-12-15 23:50:47 +00:00
err := res.stream.writeData(cmedia, cformat, &dataVP9{
rtpPackets: []*rtp.Packet{pkt},
ntp: time.Now(),
})
if err != nil {
s.Log(logger.Warn, "%v", err)
}
})
case *format.MPEG4Audio:
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
err := res.stream.writeData(cmedia, cformat, &dataMPEG4Audio{
rtpPackets: []*rtp.Packet{pkt},
ntp: time.Now(),
})
if err != nil {
s.Log(logger.Warn, "%v", err)
}
})
case *format.Opus:
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
err := res.stream.writeData(cmedia, cformat, &dataOpus{
rtpPackets: []*rtp.Packet{pkt},
ntp: time.Now(),
})
if err != nil {
s.Log(logger.Warn, "%v", err)
}
})
default:
c.OnPacketRTP(medi, forma, func(pkt *rtp.Packet) {
err := res.stream.writeData(cmedia, cformat, &dataGeneric{
rtpPackets: []*rtp.Packet{pkt},
ntp: time.Now(),
})
if err != nil {
s.Log(logger.Warn, "%v", err)
}
})
}
}
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
select {
2021-05-10 21:23:56 +00:00
case err := <-readErr:
2022-07-24 16:11:53 +00:00
return err
2021-11-12 21:29:56 +00:00
case <-ctx.Done():
c.Close()
2021-11-12 21:29:56 +00:00
<-readErr
2022-07-24 16:11:53 +00:00
return nil
2020-10-19 20:17:48 +00:00
}
}
// apiSourceDescribe implements sourceStaticImpl.
func (*rtspSource) apiSourceDescribe() interface{} {
return struct {
Type string `json:"type"`
}{"rtspSource"}
}