Skip to content

Commit

Permalink
api: add 'session' field to RTSPConn (#3974)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Dec 25, 2024
1 parent 57addb1 commit 8894c0d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 10 deletions.
3 changes: 3 additions & 0 deletions apidocs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,9 @@ components:
bytesSent:
type: integer
format: int64
session:
type: string
nullable: true

RTSPConnList:
type: object
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/alecthomas/kong v1.6.0
github.com/asticode/go-astits v1.13.0
github.com/bluenviron/gohlslib/v2 v2.1.0
github.com/bluenviron/gortsplib/v4 v4.12.1-0.20241225143216-4d3d6bc108f3
github.com/bluenviron/gortsplib/v4 v4.12.1-0.20241225145501-66410517c85f
github.com/bluenviron/mediacommon v1.13.2
github.com/datarhei/gosrt v0.8.0
github.com/fsnotify/fsnotify v1.8.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ github.com/benburkert/openpgp v0.0.0-20160410205803-c2471f86866c h1:8XZeJrs4+ZYh
github.com/benburkert/openpgp v0.0.0-20160410205803-c2471f86866c/go.mod h1:x1vxHcL/9AVzuk5HOloOEPrtJY0MaalYr78afXZ+pWI=
github.com/bluenviron/gohlslib/v2 v2.1.0 h1:I0KXXPjnt7QxsR39z97fKe/x1yj22e1NhSqZ5P6FbWE=
github.com/bluenviron/gohlslib/v2 v2.1.0/go.mod h1:irD+TAdUsb400Gp8v80LKPPC4YumiAieUSO6ICykeWo=
github.com/bluenviron/gortsplib/v4 v4.12.1-0.20241225143216-4d3d6bc108f3 h1:9JqYzxhzIQhPqe5MIq2leJNARrs7VPNgL2o3qwmlgqA=
github.com/bluenviron/gortsplib/v4 v4.12.1-0.20241225143216-4d3d6bc108f3/go.mod h1:MwFrCmflxvLTMjgtnPJ2H4SQSB/r9wX8nsR5YPtUs7M=
github.com/bluenviron/gortsplib/v4 v4.12.1-0.20241225145501-66410517c85f h1:x+W67M2KMNCumzQ8XpIFXbVE+quKD/xP5PiaJhKwqTY=
github.com/bluenviron/gortsplib/v4 v4.12.1-0.20241225145501-66410517c85f/go.mod h1:MwFrCmflxvLTMjgtnPJ2H4SQSB/r9wX8nsR5YPtUs7M=
github.com/bluenviron/mediacommon v1.13.2 h1:Ssq+59ZtPm5f9iAVVugWNOyl89Vp0G758RMv033lkik=
github.com/bluenviron/mediacommon v1.13.2/go.mod h1:tffg+sPMErUIe7WMq7ZlYry/rPE6TyENWCrYT5JWcgs=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
Expand Down
2 changes: 2 additions & 0 deletions internal/core/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ func TestAPIProtocolListGet(t *testing.T) {
"created": out1.(map[string]interface{})["items"].([]interface{})[0].(map[string]interface{})["created"],
"id": out1.(map[string]interface{})["items"].([]interface{})[0].(map[string]interface{})["id"],
"remoteAddr": out1.(map[string]interface{})["items"].([]interface{})[0].(map[string]interface{})["remoteAddr"],
"session": out1.(map[string]interface{})["items"].([]interface{})[0].(map[string]interface{})["session"],
},
},
}, out1)
Expand Down Expand Up @@ -644,6 +645,7 @@ func TestAPIProtocolListGet(t *testing.T) {
"created": out1.(map[string]interface{})["items"].([]interface{})[0].(map[string]interface{})["created"],
"id": out1.(map[string]interface{})["items"].([]interface{})[0].(map[string]interface{})["id"],
"remoteAddr": out1.(map[string]interface{})["items"].([]interface{})[0].(map[string]interface{})["remoteAddr"],
"session": out1.(map[string]interface{})["items"].([]interface{})[0].(map[string]interface{})["session"],
},
},
}, out1)
Expand Down
11 changes: 6 additions & 5 deletions internal/defs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ type APIRTMPConnList struct {

// APIRTSPConn is a RTSP connection.
type APIRTSPConn struct {
ID uuid.UUID `json:"id"`
Created time.Time `json:"created"`
RemoteAddr string `json:"remoteAddr"`
BytesReceived uint64 `json:"bytesReceived"`
BytesSent uint64 `json:"bytesSent"`
ID uuid.UUID `json:"id"`
Created time.Time `json:"created"`
RemoteAddr string `json:"remoteAddr"`
BytesReceived uint64 `json:"bytesReceived"`
BytesSent uint64 `json:"bytesSent"`
Session *uuid.UUID `json:"session"`
}

// APIRTSPConnsList is a list of RTSP connections.
Expand Down
14 changes: 13 additions & 1 deletion internal/servers/rtsp/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const (
rtspAuthRealm = "IPCAM"
)

type connParent interface {
logger.Writer
findSessionByRSession(rsession *gortsplib.ServerSession) *session
}

type conn struct {
isTLS bool
rtspAddress string
Expand All @@ -35,7 +40,7 @@ type conn struct {
pathManager serverPathManager
rconn *gortsplib.ServerConn
rserver *gortsplib.Server
parent *Server
parent connParent

uuid uuid.UUID
created time.Time
Expand Down Expand Up @@ -216,5 +221,12 @@ func (c *conn) apiItem() *defs.APIRTSPConn {
RemoteAddr: c.remoteAddr().String(),
BytesReceived: stats.BytesReceived,
BytesSent: stats.BytesSent,
Session: func() *uuid.UUID {
sx := c.parent.findSessionByRSession(c.rconn.Session())
if sx != nil {
return &sx.uuid
}
return nil

Check warning on line 229 in internal/servers/rtsp/conn.go

View check run for this annotation

Codecov / codecov/patch

internal/servers/rtsp/conn.go#L224-L229

Added lines #L224 - L229 were not covered by tests
}(),
}
}
7 changes: 7 additions & 0 deletions internal/servers/rtsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ func (s *Server) findSessionByUUID(uuid uuid.UUID) (*gortsplib.ServerSession, *s
return nil, nil
}

func (s *Server) findSessionByRSession(rsession *gortsplib.ServerSession) *session {
s.mutex.RLock()
defer s.mutex.RUnlock()

return s.sessions[rsession]

Check warning on line 344 in internal/servers/rtsp/server.go

View check run for this annotation

Codecov / codecov/patch

internal/servers/rtsp/server.go#L340-L344

Added lines #L340 - L344 were not covered by tests
}

// APIConnsList is called by api and metrics.
func (s *Server) APIConnsList() (*defs.APIRTSPConnsList, error) {
select {
Expand Down
2 changes: 1 addition & 1 deletion internal/servers/rtsp/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type session struct {
rserver *gortsplib.Server
externalCmdPool *externalcmd.Pool
pathManager serverPathManager
parent *Server
parent logger.Writer

uuid uuid.UUID
created time.Time
Expand Down

0 comments on commit 8894c0d

Please sign in to comment.