rename path id into path name

This commit is contained in:
aler9 2020-08-05 11:49:36 +02:00
parent 161f1ec2bd
commit 1399543e90
5 changed files with 77 additions and 77 deletions

View File

@ -83,7 +83,7 @@ type client struct {
p *program
conn *gortsplib.ConnServer
state clientState
pathId string
pathName string
authUser string
authPass string
authHelper *gortsplib.AuthServer
@ -459,8 +459,8 @@ func (c *client) handleRequest(req *gortsplib.Request) bool {
return true
}
if c.pathId != "" && basePath != c.pathId {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("path has changed, was '%s', now is '%s'", c.pathId, basePath))
if c.pathName != "" && basePath != c.pathName {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("path has changed, was '%s', now is '%s'", c.pathName, basePath))
return false
}
@ -592,9 +592,9 @@ func (c *client) handleRequest(req *gortsplib.Request) bool {
return false
}
// after ANNOUNCE, c.pathId is already set
if basePath != c.pathId {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("path has changed, was '%s', now is '%s'", c.pathId, basePath))
// after ANNOUNCE, c.pathName is already set
if basePath != c.pathName {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("path has changed, was '%s', now is '%s'", c.pathName, basePath))
return false
}
@ -626,7 +626,7 @@ func (c *client) handleRequest(req *gortsplib.Request) bool {
return false
}
if len(c.streamTracks) >= len(c.p.paths[c.pathId].publisherSdpParsed.MediaDescriptions) {
if len(c.streamTracks) >= len(c.p.paths[c.pathName].publisherSdpParsed.MediaDescriptions) {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("all the tracks have already been setup"))
return false
}
@ -684,7 +684,7 @@ func (c *client) handleRequest(req *gortsplib.Request) bool {
return false
}
if len(c.streamTracks) >= len(c.p.paths[c.pathId].publisherSdpParsed.MediaDescriptions) {
if len(c.streamTracks) >= len(c.p.paths[c.pathName].publisherSdpParsed.MediaDescriptions) {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("all the tracks have already been setup"))
return false
}
@ -737,8 +737,8 @@ func (c *client) handleRequest(req *gortsplib.Request) bool {
// path can end with a slash, remove it
path = strings.TrimSuffix(path, "/")
if path != c.pathId {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("path has changed, was '%s', now is '%s'", c.pathId, path))
if path != c.pathName {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("path has changed, was '%s', now is '%s'", c.pathName, path))
return false
}
@ -775,12 +775,12 @@ func (c *client) handleRequest(req *gortsplib.Request) bool {
// path can end with a slash, remove it
path = strings.TrimSuffix(path, "/")
if path != c.pathId {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("path has changed, was '%s', now is '%s'", c.pathId, path))
if path != c.pathName {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("path has changed, was '%s', now is '%s'", c.pathName, path))
return false
}
if len(c.streamTracks) != len(c.p.paths[c.pathId].publisherSdpParsed.MediaDescriptions) {
if len(c.streamTracks) != len(c.p.paths[c.pathName].publisherSdpParsed.MediaDescriptions) {
c.writeResError(req, gortsplib.StatusBadRequest, fmt.Errorf("not all tracks have been setup"))
return false
}
@ -819,7 +819,7 @@ func (c *client) runPlay(path string) {
c.p.events <- programEventClientPlay2{done, c}
<-done
c.log("is receiving on path '%s', %d %s via %s", c.pathId, len(c.streamTracks), func() string {
c.log("is receiving on path '%s', %d %s via %s", c.pathName, len(c.streamTracks), func() string {
if len(c.streamTracks) == 1 {
return "track"
}
@ -921,7 +921,7 @@ func (c *client) runRecord(path string) {
c.p.events <- programEventClientRecord{done, c}
<-done
c.log("is publishing on path '%s', %d %s via %s", c.pathId, len(c.streamTracks), func() string {
c.log("is publishing on path '%s', %d %s via %s", c.pathName, len(c.streamTracks), func() string {
if len(c.streamTracks) == 1 {
return "track"
}
@ -1025,7 +1025,7 @@ func (c *client) runRecord(path string) {
c.rtcpReceivers[frame.TrackId].OnFrame(frame.StreamType, frame.Content)
c.p.events <- programEventClientFrameTcp{
c.pathId,
c.pathName,
frame.TrackId,
frame.StreamType,
frame.Content,

10
conf.go
View File

@ -174,10 +174,10 @@ func loadConf(fpath string, stdin io.Reader) (*conf, error) {
}
}
for path, confp := range conf.Paths {
for name, confp := range conf.Paths {
if confp == nil {
conf.Paths[path] = &confPath{}
confp = conf.Paths[path]
conf.Paths[name] = &confPath{}
confp = conf.Paths[name]
}
if confp.Source == "" {
@ -185,7 +185,7 @@ func loadConf(fpath string, stdin io.Reader) (*conf, error) {
}
if confp.Source != "record" {
if path == "all" {
if name == "all" {
return nil, fmt.Errorf("path 'all' cannot have a RTSP source; use another path")
}
@ -260,7 +260,7 @@ func loadConf(fpath string, stdin io.Reader) (*conf, error) {
return nil, err
}
if confp.RunOnDemand != "" && path == "all" {
if confp.RunOnDemand != "" && name == "all" {
return nil, fmt.Errorf("path 'all' does not support option 'runOnDemand'; use another path")
}
}

46
main.go
View File

@ -218,17 +218,17 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
p.log("rtsp-simple-server %s", Version)
for path, confp := range conf.Paths {
if path == "all" {
for name, confp := range conf.Paths {
if name == "all" {
continue
}
p.paths[path] = newPath(p, path, confp, true)
p.paths[name] = newPath(p, name, confp, true)
if confp.Source != "record" {
s := newSource(p, path, confp)
s := newSource(p, name, confp)
p.sources = append(p.sources, s)
p.paths[path].publisher = s
p.paths[name].publisher = s
}
}
@ -262,11 +262,11 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
return nil, err
}
for path, confp := range conf.Paths {
for name, confp := range conf.Paths {
if confp.RunOnInit != "" {
onInitCmd := exec.Command("/bin/sh", "-c", confp.RunOnInit)
onInitCmd.Env = append(os.Environ(),
"RTSP_SERVER_PATH="+path,
"RTSP_SERVER_PATH="+name,
)
onInitCmd.Stdout = os.Stdout
onInitCmd.Stderr = os.Stderr
@ -334,13 +334,13 @@ outer:
case programEventClientClose:
delete(p.clients, evt.client)
if evt.client.pathId != "" {
if path, ok := p.paths[evt.client.pathId]; ok {
if evt.client.pathName != "" {
if path, ok := p.paths[evt.client.pathName]; ok {
if path.publisher == evt.client {
path.publisherRemove()
if !path.permanent {
delete(p.paths, evt.client.pathId)
delete(p.paths, evt.client.pathName)
}
}
}
@ -373,7 +373,7 @@ outer:
p.paths[evt.path].publisherSdpText = evt.sdpText
p.paths[evt.path].publisherSdpParsed = evt.sdpParsed
evt.client.pathId = evt.path
evt.client.pathName = evt.path
evt.client.state = clientStateAnnounce
evt.res <- nil
@ -389,7 +389,7 @@ outer:
continue
}
evt.client.pathId = evt.path
evt.client.pathName = evt.path
evt.client.state = clientStatePrePlay
evt.res <- nil
@ -398,9 +398,9 @@ outer:
evt.res <- nil
case programEventClientPlay1:
path, ok := p.paths[evt.client.pathId]
path, ok := p.paths[evt.client.pathName]
if !ok || !path.publisherReady {
evt.res <- fmt.Errorf("no one is publishing on path '%s'", evt.client.pathId)
evt.res <- fmt.Errorf("no one is publishing on path '%s'", evt.client.pathName)
continue
}
@ -427,7 +427,7 @@ outer:
if evt.client.streamProtocol == gortsplib.StreamProtocolUdp {
p.udpClientPublishers[makeIpKey(evt.client.ip())] = evt.client
}
p.paths[evt.client.pathId].publisherSetReady()
p.paths[evt.client.pathName].publisherSetReady()
close(evt.done)
case programEventClientRecordStop:
@ -436,7 +436,7 @@ outer:
if evt.client.streamProtocol == gortsplib.StreamProtocolUdp {
delete(p.udpClientPublishers, makeIpKey(evt.client.ip()))
}
p.paths[evt.client.pathId].publisherSetNotReady()
p.paths[evt.client.pathName].publisherSetNotReady()
close(evt.done)
case programEventClientFrameUdp:
@ -446,21 +446,21 @@ outer:
}
client.rtcpReceivers[trackId].OnFrame(evt.streamType, evt.buf)
p.forwardFrame(client.pathId, trackId, evt.streamType, evt.buf)
p.forwardFrame(client.pathName, trackId, evt.streamType, evt.buf)
case programEventClientFrameTcp:
p.forwardFrame(evt.path, evt.trackId, evt.streamType, evt.buf)
case programEventSourceReady:
evt.source.log("ready")
p.paths[evt.source.pathId].publisherSetReady()
p.paths[evt.source.pathName].publisherSetReady()
case programEventSourceNotReady:
evt.source.log("not ready")
p.paths[evt.source.pathId].publisherSetNotReady()
p.paths[evt.source.pathName].publisherSetNotReady()
case programEventSourceFrame:
p.forwardFrame(evt.source.pathId, evt.trackId, evt.streamType, evt.buf)
p.forwardFrame(evt.source.pathName, evt.trackId, evt.streamType, evt.buf)
case programEventTerminate:
break outer
@ -543,8 +543,8 @@ func (p *program) close() {
<-p.done
}
func (p *program) findConfForPath(path string) *confPath {
if confp, ok := p.conf.Paths[path]; ok {
func (p *program) findConfForPath(name string) *confPath {
if confp, ok := p.conf.Paths[name]; ok {
return confp
}
@ -576,7 +576,7 @@ func (p *program) findUdpClientPublisher(addr *net.UDPAddr, streamType gortsplib
func (p *program) forwardFrame(path string, trackId int, streamType gortsplib.StreamType, frame []byte) {
for c := range p.clients {
if c.pathId != path ||
if c.pathName != path ||
c.state != clientStatePlay {
continue
}

38
path.go
View File

@ -16,7 +16,7 @@ type publisher interface {
type path struct {
p *program
id string
name string
confp *confPath
permanent bool
publisher publisher
@ -28,10 +28,10 @@ type path struct {
onDemandCmd *exec.Cmd
}
func newPath(p *program, id string, confp *confPath, permanent bool) *path {
func newPath(p *program, name string, confp *confPath, permanent bool) *path {
pa := &path{
p: p,
id: id,
name: name,
confp: confp,
permanent: permanent,
}
@ -42,7 +42,7 @@ func newPath(p *program, id string, confp *confPath, permanent bool) *path {
func (pa *path) check() {
hasClientsWaitingDescribe := func() bool {
for c := range pa.p.clients {
if c.state == clientStateWaitingDescription && c.pathId == pa.id {
if c.state == clientStateWaitingDescription && c.pathName == pa.name {
return true
}
}
@ -54,10 +54,10 @@ func (pa *path) check() {
time.Since(pa.lastActivation) >= 5*time.Second {
for c := range pa.p.clients {
if c.state == clientStateWaitingDescription &&
c.pathId == pa.id {
c.pathId = ""
c.pathName == pa.name {
c.pathName = ""
c.state = clientStateInitial
c.describeRes <- describeRes{nil, fmt.Errorf("publisher of path '%s' has timed out", pa.id)}
c.describeRes <- describeRes{nil, fmt.Errorf("publisher of path '%s' has timed out", pa.name)}
}
}
@ -73,7 +73,7 @@ func (pa *path) check() {
hasClients := func() bool {
for c := range pa.p.clients {
if c.pathId == pa.id {
if c.pathName == pa.name {
return true
}
}
@ -93,7 +93,7 @@ func (pa *path) check() {
hasClientReaders := func() bool {
for c := range pa.p.clients {
if c.pathId == pa.id && c != pa.publisher {
if c.pathName == pa.name && c != pa.publisher {
return true
}
}
@ -123,7 +123,7 @@ func (pa *path) describe(client *client) {
pa.lastActivation = time.Now()
pa.onDemandCmd = exec.Command("/bin/sh", "-c", pa.confp.RunOnDemand)
pa.onDemandCmd.Env = append(os.Environ(),
"RTSP_SERVER_PATH="+pa.id,
"RTSP_SERVER_PATH="+pa.name,
)
pa.onDemandCmd.Stdout = os.Stdout
pa.onDemandCmd.Stderr = os.Stderr
@ -133,13 +133,13 @@ func (pa *path) describe(client *client) {
}
}
client.pathId = pa.id
client.pathName = pa.name
client.state = clientStateWaitingDescription
return
}
// no on-demand: reply with 404
client.describeRes <- describeRes{nil, fmt.Errorf("no one is publishing on path '%s'", pa.id)}
client.describeRes <- describeRes{nil, fmt.Errorf("no one is publishing on path '%s'", pa.name)}
return
}
@ -153,7 +153,7 @@ func (pa *path) describe(client *client) {
source.events <- sourceEventApplyState{source.state}
}
client.pathId = pa.id
client.pathName = pa.name
client.state = clientStateWaitingDescription
return
}
@ -165,10 +165,10 @@ func (pa *path) describe(client *client) {
func (pa *path) publisherRemove() {
for c := range pa.p.clients {
if c.state == clientStateWaitingDescription &&
c.pathId == pa.id {
c.pathId = ""
c.pathName == pa.name {
c.pathName = ""
c.state = clientStateInitial
c.describeRes <- describeRes{nil, fmt.Errorf("publisher of path '%s' is not available anymore", pa.id)}
c.describeRes <- describeRes{nil, fmt.Errorf("publisher of path '%s' is not available anymore", pa.name)}
}
}
@ -181,8 +181,8 @@ func (pa *path) publisherSetReady() {
// reply to all clients that are waiting for a description
for c := range pa.p.clients {
if c.state == clientStateWaitingDescription &&
c.pathId == pa.id {
c.pathId = ""
c.pathName == pa.name {
c.pathName = ""
c.state = clientStateInitial
c.describeRes <- describeRes{pa.publisherSdpText, nil}
}
@ -196,7 +196,7 @@ func (pa *path) publisherSetNotReady() {
for c := range pa.p.clients {
if c.state != clientStateWaitingDescription &&
c != pa.publisher &&
c.pathId == pa.id {
c.pathName == pa.name {
c.conn.NetConn().Close()
}
}

View File

@ -38,23 +38,23 @@ type sourceEventTerminate struct{}
func (sourceEventTerminate) isSourceEvent() {}
type source struct {
p *program
pathId string
confp *confPath
state sourceState
tracks []*gortsplib.Track
p *program
pathName string
confp *confPath
state sourceState
tracks []*gortsplib.Track
events chan sourceEvent
done chan struct{}
}
func newSource(p *program, pathId string, confp *confPath) *source {
func newSource(p *program, pathName string, confp *confPath) *source {
s := &source{
p: p,
pathId: pathId,
confp: confp,
events: make(chan sourceEvent),
done: make(chan struct{}),
p: p,
pathName: pathName,
confp: confp,
events: make(chan sourceEvent),
done: make(chan struct{}),
}
if confp.SourceOnDemand {
@ -67,7 +67,7 @@ func newSource(p *program, pathId string, confp *confPath) *source {
}
func (s *source) log(format string, args ...interface{}) {
s.p.log("[source "+s.pathId+"] "+format, args...)
s.p.log("[source "+s.pathName+"] "+format, args...)
}
func (s *source) isPublisher() {}
@ -188,8 +188,8 @@ func (s *source) doInner(terminate chan struct{}) bool {
serverSdpParsed, serverSdpText := sdpForServer(tracks)
s.tracks = tracks
s.p.paths[s.pathId].publisherSdpText = serverSdpText
s.p.paths[s.pathId].publisherSdpParsed = serverSdpParsed
s.p.paths[s.pathName].publisherSdpText = serverSdpText
s.p.paths[s.pathName].publisherSdpParsed = serverSdpParsed
if s.confp.sourceProtocolParsed == gortsplib.StreamProtocolUdp {
return s.runUdp(terminate, conn)