diff --git a/apidocs/openapi.yaml b/apidocs/openapi.yaml index 281621d2..96e498b4 100644 --- a/apidocs/openapi.yaml +++ b/apidocs/openapi.yaml @@ -295,6 +295,8 @@ components: RTSPSession: type: object properties: + created: + type: string remoteAddr: type: string state: @@ -313,6 +315,8 @@ components: RTMPConn: type: object properties: + created: + type: string remoteAddr: type: string state: @@ -322,6 +326,8 @@ components: HLSMuxer: type: object properties: + created: + type: string lastRequest: type: string diff --git a/internal/core/hls_muxer.go b/internal/core/hls_muxer.go index 75c6a268..fa73a310 100644 --- a/internal/core/hls_muxer.go +++ b/internal/core/hls_muxer.go @@ -127,6 +127,7 @@ type hlsMuxer struct { ctx context.Context ctxCancel func() + created time.Time path *path ringBuffer *ringbuffer.RingBuffer lastRequestTime *int64 @@ -173,6 +174,7 @@ func newHLSMuxer( parent: parent, ctx: ctx, ctxCancel: ctxCancel, + created: time.Now(), lastRequestTime: func() *int64 { v := time.Now().Unix() return &v @@ -240,6 +242,7 @@ func (m *hlsMuxer) run() { case req := <-m.chAPIHLSMuxersList: req.data.Items[m.name] = hlsServerAPIMuxersListItem{ + Created: m.created, LastRequest: time.Unix(atomic.LoadInt64(m.lastRequestTime), 0).String(), } close(req.res) diff --git a/internal/core/hls_server.go b/internal/core/hls_server.go index dbda6dba..7d0c0178 100644 --- a/internal/core/hls_server.go +++ b/internal/core/hls_server.go @@ -12,6 +12,7 @@ import ( gopath "path" "strings" "sync" + "time" "github.com/gin-gonic/gin" @@ -27,7 +28,8 @@ func (nilWriter) Write(p []byte) (int, error) { } type hlsServerAPIMuxersListItem struct { - LastRequest string `json:"lastRequest"` + Created time.Time `json:"created"` + LastRequest string `json:"lastRequest"` } type hlsServerAPIMuxersListData struct { diff --git a/internal/core/path.go b/internal/core/path.go index 3ace7af7..9f29aa93 100644 --- a/internal/core/path.go +++ b/internal/core/path.go @@ -67,7 +67,7 @@ type pathParent interface { } type pathRTSPSession interface { - IsRTSPSession() + isRTSPSession() } type pathReaderState int diff --git a/internal/core/rtmp_conn.go b/internal/core/rtmp_conn.go index 752d4f37..a6256ff7 100644 --- a/internal/core/rtmp_conn.go +++ b/internal/core/rtmp_conn.go @@ -74,6 +74,7 @@ type rtmpConn struct { ctx context.Context ctxCancel func() + created time.Time path *path ringBuffer *ringbuffer.RingBuffer // read state rtmpConnState @@ -115,6 +116,7 @@ func newRTMPConn( parent: parent, ctx: ctx, ctxCancel: ctxCancel, + created: time.Now(), } c.log(logger.Info, "opened") @@ -125,18 +127,11 @@ func newRTMPConn( return c } -// Close closes a Conn. func (c *rtmpConn) close() { c.ctxCancel() } -// ID returns the ID of the Conn. -func (c *rtmpConn) ID() string { - return c.id -} - -// RemoteAddr returns the remote address of the Conn. -func (c *rtmpConn) RemoteAddr() net.Addr { +func (c *rtmpConn) remoteAddr() net.Addr { return c.nconn.RemoteAddr() } diff --git a/internal/core/rtmp_server.go b/internal/core/rtmp_server.go index ce6741c3..18f25419 100644 --- a/internal/core/rtmp_server.go +++ b/internal/core/rtmp_server.go @@ -7,6 +7,7 @@ import ( "net" "strconv" "sync" + "time" "github.com/aler9/rtsp-simple-server/internal/conf" "github.com/aler9/rtsp-simple-server/internal/externalcmd" @@ -14,8 +15,9 @@ import ( ) type rtmpServerAPIConnsListItem struct { - RemoteAddr string `json:"remoteAddr"` - State string `json:"state"` + Created time.Time `json:"created"` + RemoteAddr string `json:"remoteAddr"` + State string `json:"state"` } type rtmpServerAPIConnsListData struct { @@ -202,8 +204,9 @@ outer: } for c := range s.conns { - data.Items[c.ID()] = rtmpServerAPIConnsListItem{ - RemoteAddr: c.RemoteAddr().String(), + data.Items[c.id] = rtmpServerAPIConnsListItem{ + Created: c.created, + RemoteAddr: c.remoteAddr().String(), State: func() string { switch c.safeState() { case rtmpConnStateRead: @@ -222,7 +225,7 @@ outer: case req := <-s.chAPIConnsKick: res := func() bool { for c := range s.conns { - if c.ID() == req.id { + if c.id == req.id { delete(s.conns, c) c.close() return true @@ -266,7 +269,7 @@ func (s *rtmpServer) newConnID() (string, error) { alreadyPresent := func() bool { for c := range s.conns { - if c.ID() == id { + if c.id == id { return true } } diff --git a/internal/core/rtsp_server.go b/internal/core/rtsp_server.go index 22fc4594..99d847fa 100644 --- a/internal/core/rtsp_server.go +++ b/internal/core/rtsp_server.go @@ -21,8 +21,9 @@ import ( ) type rtspServerAPISessionsListItem struct { - RemoteAddr string `json:"remoteAddr"` - State string `json:"state"` + Created time.Time `json:"created"` + RemoteAddr string `json:"remoteAddr"` + State string `json:"state"` } type rtspServerAPISessionsListData struct { @@ -242,7 +243,7 @@ func (s *rtspServer) newSessionID() (string, error) { alreadyPresent := func() bool { for _, s := range s.sessions { - if s.ID() == id { + if s.id == id { return true } } @@ -408,8 +409,9 @@ func (s *rtspServer) apiSessionsList(req rtspServerAPISessionsListReq) rtspServe } for _, s := range s.sessions { - data.Items[s.ID()] = rtspServerAPISessionsListItem{ - RemoteAddr: s.RemoteAddr().String(), + data.Items[s.id] = rtspServerAPISessionsListItem{ + Created: s.created, + RemoteAddr: s.remoteAddr().String(), State: func() string { switch s.safeState() { case gortsplib.ServerSessionStatePrePlay, @@ -440,7 +442,7 @@ func (s *rtspServer) apiSessionsKick(req rtspServerAPISessionsKickReq) rtspServe defer s.mutex.RUnlock() for key, se := range s.sessions { - if se.ID() == req.id { + if se.id == req.id { se.close() delete(s.sessions, key) se.onClose(liberrors.ErrServerTerminated{}) diff --git a/internal/core/rtsp_session.go b/internal/core/rtsp_session.go index 8841078a..4b3fce75 100644 --- a/internal/core/rtsp_session.go +++ b/internal/core/rtsp_session.go @@ -38,6 +38,7 @@ type rtspSession struct { pathManager rtspSessionPathManager parent rtspSessionParent + created time.Time path *path state gortsplib.ServerSessionState stateMutex sync.Mutex @@ -65,6 +66,7 @@ func newRTSPSession( externalCmdPool: externalCmdPool, pathManager: pathManager, parent: parent, + created: time.Now(), } s.log(logger.Info, "created by %v", s.author.NetConn().RemoteAddr()) @@ -77,13 +79,8 @@ func (s *rtspSession) close() { s.ss.Close() } -// IsRTSPSession implements pathRTSPSession. -func (s *rtspSession) IsRTSPSession() {} - -// ID returns the public ID of the session. -func (s *rtspSession) ID() string { - return s.id -} +// isRTSPSession implements pathRTSPSession. +func (s *rtspSession) isRTSPSession() {} func (s *rtspSession) safeState() gortsplib.ServerSessionState { s.stateMutex.Lock() @@ -91,8 +88,7 @@ func (s *rtspSession) safeState() gortsplib.ServerSessionState { return s.state } -// RemoteAddr returns the remote address of the author of the session. -func (s *rtspSession) RemoteAddr() net.Addr { +func (s *rtspSession) remoteAddr() net.Addr { return s.author.NetConn().RemoteAddr() }