From 6e5564c0a227cb139f4bf247f95f316ad29580e1 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Fri, 20 Aug 2021 10:32:43 +0200 Subject: [PATCH] api: return rtspsSessions in case of RTSPS sessions --- internal/core/api_test.go | 44 +++++++++++++++++++++++++++++++++-- internal/core/rtsp_server.go | 1 + internal/core/rtsp_session.go | 21 +++++++++++++++-- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/internal/core/api_test.go b/internal/core/api_test.go index b89a0aff..3870112a 100644 --- a/internal/core/api_test.go +++ b/internal/core/api_test.go @@ -150,19 +150,59 @@ func TestAPIConfigPathsRemove(t *testing.T) { } func TestAPIPathsList(t *testing.T) { + serverCertFpath, err := writeTempFile(serverCert) + require.NoError(t, err) + defer os.Remove(serverCertFpath) + + serverKeyFpath, err := writeTempFile(serverKey) + require.NoError(t, err) + defer os.Remove(serverKeyFpath) + p, ok := newInstance("api: yes\n" + + "encryption: optional\n" + + "serverCert: " + serverCertFpath + "\n" + + "serverKey: " + serverKeyFpath + "\n" + "paths:\n" + " mypath:\n") require.Equal(t, true, ok) defer p.close() var out struct { - Items map[string]interface{} `json:"items"` + Items map[string]struct { + Source struct { + Type string `json:"type"` + } `json:"source"` + } `json:"items"` } - err := httpRequest(http.MethodGet, "http://localhost:9997/v1/paths/list", nil, &out) + err = httpRequest(http.MethodGet, "http://localhost:9997/v1/paths/list", nil, &out) require.NoError(t, err) _, ok = out.Items["mypath"] require.Equal(t, true, ok) + + track, err := gortsplib.NewTrackH264(96, []byte("123456"), []byte("123456")) + require.NoError(t, err) + + func() { + source, err := gortsplib.DialPublish("rtsp://localhost:8554/mypath", + gortsplib.Tracks{track}) + require.NoError(t, err) + defer source.Close() + + err = httpRequest(http.MethodGet, "http://localhost:9997/v1/paths/list", nil, &out) + require.NoError(t, err) + require.Equal(t, "rtspSession", out.Items["mypath"].Source.Type) + }() + + func() { + source, err := gortsplib.DialPublish("rtsps://localhost:8555/mypath", + gortsplib.Tracks{track}) + require.NoError(t, err) + defer source.Close() + + err = httpRequest(http.MethodGet, "http://localhost:9997/v1/paths/list", nil, &out) + require.NoError(t, err) + require.Equal(t, "rtspsSession", out.Items["mypath"].Source.Type) + }() } func TestAPIList(t *testing.T) { diff --git a/internal/core/rtsp_server.go b/internal/core/rtsp_server.go index 6b137e6a..a5354bb0 100644 --- a/internal/core/rtsp_server.go +++ b/internal/core/rtsp_server.go @@ -277,6 +277,7 @@ func (s *rtspServer) OnSessionOpen(ctx *gortsplib.ServerHandlerOnSessionOpenCtx) id, _ := s.newSessionID() se := newRTSPSession( + s.isTLS, s.rtspAddress, s.protocols, id, diff --git a/internal/core/rtsp_session.go b/internal/core/rtsp_session.go index cbfa5288..70930acd 100644 --- a/internal/core/rtsp_session.go +++ b/internal/core/rtsp_session.go @@ -29,6 +29,7 @@ type rtspSessionParent interface { } type rtspSession struct { + isTLS bool rtspAddress string protocols map[conf.Protocol]struct{} id string @@ -47,6 +48,7 @@ type rtspSession struct { } func newRTSPSession( + isTLS bool, rtspAddress string, protocols map[conf.Protocol]struct{}, id string, @@ -55,6 +57,7 @@ func newRTSPSession( pathManager rtspSessionPathManager, parent rtspSessionParent) *rtspSession { s := &rtspSession{ + isTLS: isTLS, rtspAddress: rtspAddress, protocols: protocols, id: id, @@ -349,18 +352,32 @@ func (s *rtspSession) OnReaderFrame(trackID int, streamType gortsplib.StreamType // OnReaderAPIDescribe implements reader. func (s *rtspSession) OnReaderAPIDescribe() interface{} { + var typ string + if s.isTLS { + typ = "rtspsSession" + } else { + typ = "rtspSession" + } + return struct { Type string `json:"type"` ID string `json:"id"` - }{"rtspSession", s.id} + }{typ, s.id} } // OnSourceAPIDescribe implements source. func (s *rtspSession) OnSourceAPIDescribe() interface{} { + var typ string + if s.isTLS { + typ = "rtspsSession" + } else { + typ = "rtspSession" + } + return struct { Type string `json:"type"` ID string `json:"id"` - }{"rtspSession", s.id} + }{typ, s.id} } // OnPublisherAccepted implements publisher.