Skip to content

Commit

Permalink
update gortsplib (#2126)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Jul 30, 2023
1 parent 83484b1 commit e3d4856
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 83 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/alecthomas/kong v0.8.0
github.com/asticode/go-astits v1.11.1-0.20230727094110-0df190a2dd87
github.com/bluenviron/gohlslib v0.3.1-0.20230730162911-eb9f86511072
github.com/bluenviron/gortsplib/v3 v3.9.0
github.com/bluenviron/gortsplib/v3 v3.9.1-0.20230730204034-8b8b52e689d9
github.com/bluenviron/mediacommon v0.7.1-0.20230730144331-10b74a4f6eda
github.com/fsnotify/fsnotify v1.6.0
github.com/gin-gonic/gin v1.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ github.com/asticode/go-astits v1.11.1-0.20230727094110-0df190a2dd87 h1:SCAqalLhg
github.com/asticode/go-astits v1.11.1-0.20230727094110-0df190a2dd87/go.mod h1:QSHmknZ51pf6KJdHKZHJTLlMegIrhega3LPWz3ND/iI=
github.com/bluenviron/gohlslib v0.3.1-0.20230730162911-eb9f86511072 h1:pAbC7frXsTMxP7Ck3E50hl7oFeSeD2dgc2lWjmHXztQ=
github.com/bluenviron/gohlslib v0.3.1-0.20230730162911-eb9f86511072/go.mod h1:rK4b161qErs82QqvBEl84vpi2xhdZBUT0yubXuytZ7E=
github.com/bluenviron/gortsplib/v3 v3.9.0 h1:aAHV6MhsDtgBF6yKaNBBCdvtSpLB8ne4kyUfLQlN7nM=
github.com/bluenviron/gortsplib/v3 v3.9.0/go.mod h1:5h3Zu7jkzwDknYrf+89q2saab//oioKgM9mgvBEX3pg=
github.com/bluenviron/gortsplib/v3 v3.9.1-0.20230730204034-8b8b52e689d9 h1:QBdUlT/taEG0b8dxguJ6GYT7r6vJFRhvwlhs1LGWYlQ=
github.com/bluenviron/gortsplib/v3 v3.9.1-0.20230730204034-8b8b52e689d9/go.mod h1:vBmCYjtox2pXWSvU+r9ROpEbwSOhh5lVWd+t9Sj8glc=
github.com/bluenviron/mediacommon v0.7.1-0.20230730144331-10b74a4f6eda h1:+ungCWRNDjsy/CVL1l/UjAj4vYL4+NIJQoJJWbR3Xw8=
github.com/bluenviron/mediacommon v0.7.1-0.20230730144331-10b74a4f6eda/go.mod h1:tfk0qGPhqnOxVCrElu8ct3LKQn6Cj4Tpu3zbbJBTKj4=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
Expand Down
14 changes: 7 additions & 7 deletions internal/core/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type pathReaderAddReq struct {
res chan pathReaderSetupPlayRes
}

type pathPublisherAnnounceRes struct {
type pathPublisherAddRes struct {
path *path
err error
}
Expand All @@ -125,7 +125,7 @@ type pathPublisherAddReq struct {
pathName string
skipAuth bool
credentials authCredentials
res chan pathPublisherAnnounceRes
res chan pathPublisherAddRes
}

type pathPublisherRecordRes struct {
Expand Down Expand Up @@ -743,15 +743,15 @@ func (pa *path) handlePublisherRemove(req pathPublisherRemoveReq) {

func (pa *path) handlePublisherAdd(req pathPublisherAddReq) {
if pa.conf.Source != "publisher" {
req.res <- pathPublisherAnnounceRes{
req.res <- pathPublisherAddRes{
err: fmt.Errorf("can't publish to path '%s' since 'source' is not 'publisher'", pa.name),
}
return
}

if pa.source != nil {
if pa.conf.DisablePublisherOverride {
req.res <- pathPublisherAnnounceRes{err: fmt.Errorf("someone is already publishing to path '%s'", pa.name)}
req.res <- pathPublisherAddRes{err: fmt.Errorf("someone is already publishing to path '%s'", pa.name)}
return
}

Expand All @@ -762,7 +762,7 @@ func (pa *path) handlePublisherAdd(req pathPublisherAddReq) {

pa.source = req.author

req.res <- pathPublisherAnnounceRes{path: pa}
req.res <- pathPublisherAddRes{path: pa}
}

func (pa *path) handlePublisherStart(req pathPublisherStartReq) {
Expand Down Expand Up @@ -973,12 +973,12 @@ func (pa *path) publisherRemove(req pathPublisherRemoveReq) {
}

// publisherAdd is called by a publisher through pathManager.
func (pa *path) publisherAdd(req pathPublisherAddReq) pathPublisherAnnounceRes {
func (pa *path) publisherAdd(req pathPublisherAddReq) pathPublisherAddRes {
select {
case pa.chPublisherAdd <- req:
return <-req.res
case <-pa.ctx.Done():
return pathPublisherAnnounceRes{err: fmt.Errorf("terminated")}
return pathPublisherAddRes{err: fmt.Errorf("terminated")}
}
}

Expand Down
12 changes: 6 additions & 6 deletions internal/core/path_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,14 @@ outer:
case req := <-pm.chPublisherAdd:
pathConfName, pathConf, pathMatches, err := getConfForPath(pm.pathConfs, req.pathName)
if err != nil {
req.res <- pathPublisherAnnounceRes{err: err}
req.res <- pathPublisherAddRes{err: err}
continue
}

if !req.skipAuth {
err = doAuthentication(pm.externalAuthenticationURL, pm.authMethods, req.pathName, pathConf, true, req.credentials)
if err != nil {
req.res <- pathPublisherAnnounceRes{err: err}
req.res <- pathPublisherAddRes{err: err}
continue
}
}
Expand All @@ -306,7 +306,7 @@ outer:
pm.createPath(pathConfName, pathConf, req.pathName, pathMatches)
}

req.res <- pathPublisherAnnounceRes{path: pm.paths[req.pathName]}
req.res <- pathPublisherAddRes{path: pm.paths[req.pathName]}

case s := <-pm.chHLSManagerSet:
pm.hlsManager = s
Expand Down Expand Up @@ -449,8 +449,8 @@ func (pm *pathManager) describe(req pathDescribeReq) pathDescribeRes {
}

// publisherAnnounce is called by a publisher.
func (pm *pathManager) publisherAdd(req pathPublisherAddReq) pathPublisherAnnounceRes {
req.res = make(chan pathPublisherAnnounceRes)
func (pm *pathManager) publisherAdd(req pathPublisherAddReq) pathPublisherAddRes {
req.res = make(chan pathPublisherAddRes)
select {
case pm.chPublisherAdd <- req:
res := <-req.res
Expand All @@ -461,7 +461,7 @@ func (pm *pathManager) publisherAdd(req pathPublisherAddReq) pathPublisherAnnoun
return res.path.publisherAdd(req)

case <-pm.ctx.Done():
return pathPublisherAnnounceRes{err: fmt.Errorf("terminated")}
return pathPublisherAddRes{err: fmt.Errorf("terminated")}
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/core/rtmp_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const (

type rtmpConnPathManager interface {
readerAdd(req pathReaderAddReq) pathReaderSetupPlayRes
publisherAdd(req pathPublisherAddReq) pathPublisherAnnounceRes
publisherAdd(req pathPublisherAddReq) pathPublisherAddRes
}

type rtmpConnParent interface {
Expand Down
8 changes: 7 additions & 1 deletion internal/core/rtsp_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ func (c *rtspConn) onDescribe(ctx *gortsplib.ServerHandlerOnDescribeCtx,
ctx.Path = ctx.Path[1:]

if c.authNonce == "" {
c.authNonce = auth.GenerateNonce()
var err error
c.authNonce, err = auth.GenerateNonce()
if err != nil {
return &base.Response{
StatusCode: base.StatusInternalServerError,
}, nil, err
}
}

res := c.pathManager.describe(pathDescribeReq{
Expand Down
18 changes: 15 additions & 3 deletions internal/core/rtsp_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

type rtspSessionPathManager interface {
publisherAdd(req pathPublisherAddReq) pathPublisherAnnounceRes
publisherAdd(req pathPublisherAddReq) pathPublisherAddRes
readerAdd(req pathReaderAddReq) pathReaderSetupPlayRes
}

Expand Down Expand Up @@ -122,7 +122,13 @@ func (s *rtspSession) onAnnounce(c *rtspConn, ctx *gortsplib.ServerHandlerOnAnno
ctx.Path = ctx.Path[1:]

if c.authNonce == "" {
c.authNonce = auth.GenerateNonce()
var err error
c.authNonce, err = auth.GenerateNonce()
if err != nil {
return &base.Response{
StatusCode: base.StatusInternalServerError,
}, err
}
}

res := s.pathManager.publisherAdd(pathPublisherAddReq{
Expand Down Expand Up @@ -201,7 +207,13 @@ func (s *rtspSession) onSetup(c *rtspConn, ctx *gortsplib.ServerHandlerOnSetupCt
}

if c.authNonce == "" {
c.authNonce = auth.GenerateNonce()
var err error
c.authNonce, err = auth.GenerateNonce()
if err != nil {
return &base.Response{
StatusCode: base.StatusInternalServerError,
}, nil, err
}
}

res := s.pathManager.readerAdd(pathReaderAddReq{
Expand Down
89 changes: 32 additions & 57 deletions internal/core/rtsp_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ package core

import (
"context"
"crypto/sha256"
"crypto/tls"
"encoding/hex"
"fmt"
"strings"
"time"

"github.com/bluenviron/gortsplib/v3"
Expand All @@ -19,37 +14,6 @@ import (
"github.com/bluenviron/mediamtx/internal/logger"
)

type rtspSourceParent interface {
logger.Writer
sourceStaticImplSetReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
sourceStaticImplSetNotReady(req pathSourceStaticSetNotReadyReq)
}

type rtspSource struct {
readTimeout conf.StringDuration
writeTimeout conf.StringDuration
readBufferCount int
parent rtspSourceParent
}

func newRTSPSource(
readTimeout conf.StringDuration,
writeTimeout conf.StringDuration,
readBufferCount int,
parent rtspSourceParent,
) *rtspSource {
return &rtspSource{
readTimeout: readTimeout,
writeTimeout: writeTimeout,
readBufferCount: readBufferCount,
parent: parent,
}
}

func (s *rtspSource) Log(level logger.Level, format string, args ...interface{}) {
s.parent.Log(level, "[rtsp source] "+format, args...)
}

func createRangeHeader(cnf *conf.PathConf) (*headers.Range, error) {
switch cnf.RtspRangeType {
case conf.RtspRangeTypeClock:
Expand Down Expand Up @@ -95,33 +59,44 @@ func createRangeHeader(cnf *conf.PathConf) (*headers.Range, error) {
}
}

// run implements sourceStaticImpl.
func (s *rtspSource) run(ctx context.Context, cnf *conf.PathConf, reloadConf chan *conf.PathConf) error {
s.Log(logger.Debug, "connecting")
type rtspSourceParent interface {
logger.Writer
sourceStaticImplSetReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
sourceStaticImplSetNotReady(req pathSourceStaticSetNotReadyReq)
}

var tlsConfig *tls.Config
if cnf.SourceFingerprint != "" {
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(cnf.SourceFingerprint)

if hstr != fingerprintLower {
return fmt.Errorf("server fingerprint do not match: expected %s, got %s",
fingerprintLower, hstr)
}
type rtspSource struct {
readTimeout conf.StringDuration
writeTimeout conf.StringDuration
readBufferCount int
parent rtspSourceParent
}

return nil
},
}
func newRTSPSource(
readTimeout conf.StringDuration,
writeTimeout conf.StringDuration,
readBufferCount int,
parent rtspSourceParent,
) *rtspSource {
return &rtspSource{
readTimeout: readTimeout,
writeTimeout: writeTimeout,
readBufferCount: readBufferCount,
parent: parent,
}
}

func (s *rtspSource) Log(level logger.Level, format string, args ...interface{}) {
s.parent.Log(level, "[rtsp source] "+format, args...)
}

// run implements sourceStaticImpl.
func (s *rtspSource) run(ctx context.Context, cnf *conf.PathConf, reloadConf chan *conf.PathConf) error {
s.Log(logger.Debug, "connecting")

c := &gortsplib.Client{
Transport: cnf.SourceProtocol.Transport,
TLSConfig: tlsConfig,
TLSConfig: tlsConfigForFingerprint(cnf.SourceFingerprint),
ReadTimeout: time.Duration(s.readTimeout),
WriteTimeout: time.Duration(s.writeTimeout),
ReadBufferCount: s.readBufferCount,
Expand Down
13 changes: 9 additions & 4 deletions internal/core/rtsp_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func TestRTSPSource(t *testing.T) {
t.Run(source, func(t *testing.T) {
medi := testMediaH264
stream := gortsplib.NewServerStream(media.Medias{medi})
nonce := auth.GenerateNonce()

nonce, err := auth.GenerateNonce()
require.NoError(t, err)

s := gortsplib.Server{
Handler: &testServer{
Expand Down Expand Up @@ -112,7 +114,7 @@ func TestRTSPSource(t *testing.T) {
s.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cert}}
}

err := s.Start()
err = s.Start()
require.NoError(t, err)
defer s.Wait()
defer s.Close()
Expand Down Expand Up @@ -167,7 +169,10 @@ func TestRTSPSource(t *testing.T) {

func TestRTSPSourceNoPassword(t *testing.T) {
stream := gortsplib.NewServerStream(media.Medias{testMediaH264})
nonce := auth.GenerateNonce()

nonce, err := auth.GenerateNonce()
require.NoError(t, err)

done := make(chan struct{})

s := gortsplib.Server{
Expand Down Expand Up @@ -201,7 +206,7 @@ func TestRTSPSourceNoPassword(t *testing.T) {
},
RTSPAddress: "127.0.0.1:8555",
}
err := s.Start()
err = s.Start()
require.NoError(t, err)
defer s.Wait()
defer s.Close()
Expand Down
2 changes: 1 addition & 1 deletion internal/core/webrtc_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func gatherIncomingTracks(
}

type webRTCSessionPathManager interface {
publisherAdd(req pathPublisherAddReq) pathPublisherAnnounceRes
publisherAdd(req pathPublisherAddReq) pathPublisherAddRes
readerAdd(req pathReaderAddReq) pathReaderSetupPlayRes
}

Expand Down

0 comments on commit e3d4856

Please sign in to comment.