Browse Source

Add query as optional block for SRT publish and read (#2656)

pull/2711/head
Bohdan Cherniakh 2 years ago committed by GitHub
parent
commit
4fa6c16c85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      internal/core/path_test.go
  2. 30
      internal/core/srt_conn.go

10
internal/core/path_test.go

@ -359,7 +359,7 @@ func TestPathRunOnRead(t *testing.T) {
case "srt": case "srt":
conf := srt.DefaultConfig() conf := srt.DefaultConfig()
address, err := conf.UnmarshalURL("srt://localhost:8890?streamid=read:test") address, err := conf.UnmarshalURL("srt://localhost:8890?streamid=read:test:query=value")
require.NoError(t, err) require.NoError(t, err)
err = conf.Validate() err = conf.Validate()
@ -390,19 +390,11 @@ func TestPathRunOnRead(t *testing.T) {
byts, err := os.ReadFile(onReadFile) byts, err := os.ReadFile(onReadFile)
require.NoError(t, err) require.NoError(t, err)
if ca == "srt" {
require.Equal(t, "test \n", string(byts))
} else {
require.Equal(t, "test query=value\n", string(byts)) require.Equal(t, "test query=value\n", string(byts))
}
byts, err = os.ReadFile(onUnreadFile) byts, err = os.ReadFile(onUnreadFile)
require.NoError(t, err) require.NoError(t, err)
if ca == "srt" {
require.Equal(t, "test \n", string(byts))
} else {
require.Equal(t, "test query=value\n", string(byts)) require.Equal(t, "test query=value\n", string(byts))
}
}) })
} }
} }

30
internal/core/srt_conn.go

@ -186,28 +186,38 @@ func (c *srtConn) runInner() error {
func (c *srtConn) runInner2(req srtNewConnReq) (bool, error) { func (c *srtConn) runInner2(req srtNewConnReq) (bool, error) {
parts := strings.Split(req.connReq.StreamId(), ":") parts := strings.Split(req.connReq.StreamId(), ":")
if (len(parts) != 2 && len(parts) != 4) || (parts[0] != "read" && parts[0] != "publish") { if (len(parts) < 2 || len(parts) > 5) || (parts[0] != "read" && parts[0] != "publish") {
return false, fmt.Errorf("invalid streamid '%s':"+ return false, fmt.Errorf("invalid streamid '%s':"+
" it must be 'action:pathname' or 'action:pathname:user:pass', "+ " it must be 'action:pathname[:query]' or 'action:pathname:user:pass[:query]', "+
"where action is either read or publish, pathname is the path name, user and pass are the credentials", "where action is either read or publish, pathname is the path name, user and pass are the credentials, "+
"query is an optional token containing additional information",
req.connReq.StreamId()) req.connReq.StreamId())
} }
pathName := parts[1] pathName := parts[1]
user := "" user := ""
pass := "" pass := ""
query := ""
if len(parts) == 4 { if len(parts) == 4 || len(parts) == 5 {
user, pass = parts[2], parts[3] user, pass = parts[2], parts[3]
} }
if len(parts) == 3 {
query = parts[2]
}
if len(parts) == 5 {
query = parts[4]
}
if parts[0] == "publish" { if parts[0] == "publish" {
return c.runPublish(req, pathName, user, pass) return c.runPublish(req, pathName, user, pass, query)
} }
return c.runRead(req, pathName, user, pass) return c.runRead(req, pathName, user, pass, query)
} }
func (c *srtConn) runPublish(req srtNewConnReq, pathName string, user string, pass string) (bool, error) { func (c *srtConn) runPublish(req srtNewConnReq, pathName string, user string, pass string, query string) (bool, error) {
res := c.pathManager.addPublisher(pathAddPublisherReq{ res := c.pathManager.addPublisher(pathAddPublisherReq{
author: c, author: c,
accessRequest: pathAccessRequest{ accessRequest: pathAccessRequest{
@ -218,6 +228,7 @@ func (c *srtConn) runPublish(req srtNewConnReq, pathName string, user string, pa
pass: pass, pass: pass,
proto: authProtocolSRT, proto: authProtocolSRT,
id: &c.uuid, id: &c.uuid,
query: query,
}, },
}) })
@ -305,7 +316,7 @@ func (c *srtConn) runPublishReader(sconn srt.Conn, path *path) error {
} }
} }
func (c *srtConn) runRead(req srtNewConnReq, pathName string, user string, pass string) (bool, error) { func (c *srtConn) runRead(req srtNewConnReq, pathName string, user string, pass string, query string) (bool, error) {
res := c.pathManager.addReader(pathAddReaderReq{ res := c.pathManager.addReader(pathAddReaderReq{
author: c, author: c,
accessRequest: pathAccessRequest{ accessRequest: pathAccessRequest{
@ -315,6 +326,7 @@ func (c *srtConn) runRead(req srtNewConnReq, pathName string, user string, pass
pass: pass, pass: pass,
proto: authProtocolSRT, proto: authProtocolSRT,
id: &c.uuid, id: &c.uuid,
query: query,
}, },
}) })
@ -368,7 +380,7 @@ func (c *srtConn) runRead(req srtNewConnReq, pathName string, user string, pass
pathConf, pathConf,
res.path, res.path,
c.apiReaderDescribe(), c.apiReaderDescribe(),
"", query,
c, c,
) )
defer onUnreadHook() defer onUnreadHook()

Loading…
Cancel
Save