api: return rtspsSessions in case of RTSPS sessions

This commit is contained in:
aler9 2021-08-20 10:32:43 +02:00
parent da44bbd18a
commit 6e5564c0a2
3 changed files with 62 additions and 4 deletions

View File

@ -150,19 +150,59 @@ func TestAPIConfigPathsRemove(t *testing.T) {
} }
func TestAPIPathsList(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" + p, ok := newInstance("api: yes\n" +
"encryption: optional\n" +
"serverCert: " + serverCertFpath + "\n" +
"serverKey: " + serverKeyFpath + "\n" +
"paths:\n" + "paths:\n" +
" mypath:\n") " mypath:\n")
require.Equal(t, true, ok) require.Equal(t, true, ok)
defer p.close() defer p.close()
var out struct { 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) require.NoError(t, err)
_, ok = out.Items["mypath"] _, ok = out.Items["mypath"]
require.Equal(t, true, ok) 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) { func TestAPIList(t *testing.T) {

View File

@ -277,6 +277,7 @@ func (s *rtspServer) OnSessionOpen(ctx *gortsplib.ServerHandlerOnSessionOpenCtx)
id, _ := s.newSessionID() id, _ := s.newSessionID()
se := newRTSPSession( se := newRTSPSession(
s.isTLS,
s.rtspAddress, s.rtspAddress,
s.protocols, s.protocols,
id, id,

View File

@ -29,6 +29,7 @@ type rtspSessionParent interface {
} }
type rtspSession struct { type rtspSession struct {
isTLS bool
rtspAddress string rtspAddress string
protocols map[conf.Protocol]struct{} protocols map[conf.Protocol]struct{}
id string id string
@ -47,6 +48,7 @@ type rtspSession struct {
} }
func newRTSPSession( func newRTSPSession(
isTLS bool,
rtspAddress string, rtspAddress string,
protocols map[conf.Protocol]struct{}, protocols map[conf.Protocol]struct{},
id string, id string,
@ -55,6 +57,7 @@ func newRTSPSession(
pathManager rtspSessionPathManager, pathManager rtspSessionPathManager,
parent rtspSessionParent) *rtspSession { parent rtspSessionParent) *rtspSession {
s := &rtspSession{ s := &rtspSession{
isTLS: isTLS,
rtspAddress: rtspAddress, rtspAddress: rtspAddress,
protocols: protocols, protocols: protocols,
id: id, id: id,
@ -349,18 +352,32 @@ func (s *rtspSession) OnReaderFrame(trackID int, streamType gortsplib.StreamType
// OnReaderAPIDescribe implements reader. // OnReaderAPIDescribe implements reader.
func (s *rtspSession) OnReaderAPIDescribe() interface{} { func (s *rtspSession) OnReaderAPIDescribe() interface{} {
var typ string
if s.isTLS {
typ = "rtspsSession"
} else {
typ = "rtspSession"
}
return struct { return struct {
Type string `json:"type"` Type string `json:"type"`
ID string `json:"id"` ID string `json:"id"`
}{"rtspSession", s.id} }{typ, s.id}
} }
// OnSourceAPIDescribe implements source. // OnSourceAPIDescribe implements source.
func (s *rtspSession) OnSourceAPIDescribe() interface{} { func (s *rtspSession) OnSourceAPIDescribe() interface{} {
var typ string
if s.isTLS {
typ = "rtspsSession"
} else {
typ = "rtspSession"
}
return struct { return struct {
Type string `json:"type"` Type string `json:"type"`
ID string `json:"id"` ID string `json:"id"`
}{"rtspSession", s.id} }{typ, s.id}
} }
// OnPublisherAccepted implements publisher. // OnPublisherAccepted implements publisher.