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) {
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) {

View File

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

View File

@ -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.