Skip to content

Commit c682ff5

Browse files
authored
server: fix compatibility with rtspclientsink and query parameters (bluenviron/mediamtx#3295) (#619)
1 parent 2023bf1 commit c682ff5

File tree

2 files changed

+48
-23
lines changed

2 files changed

+48
-23
lines changed

server_record_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,21 @@ func TestServerRecordPath(t *testing.T) {
298298
"",
299299
},
300300
{
301-
"subpath and query",
301+
"subpath and query, ffmpeg format",
302302
"fff=ggg",
303303
"rtsp://localhost:8554/test/stream?testing=0",
304304
"rtsp://localhost:8554/test/stream?testing=0/fff=ggg",
305305
"/test/stream",
306306
"testing=0",
307307
},
308+
{
309+
"subpath and query, gstreamer format",
310+
"fff=ggg",
311+
"rtsp://localhost:8554/test/stream?testing=0",
312+
"rtsp://localhost:8554/test/stream/fff=ggg?testing=0",
313+
"/test/stream",
314+
"testing=0",
315+
},
308316
{
309317
"no path",
310318
"streamid=1",

server_session.go

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,45 @@ func serverParseURLForPlay(u *base.URL) (string, string, string, error) {
5656
return path, query, trackID, nil
5757
}
5858

59-
func recordBaseURL(u *base.URL, path string, query string) *base.URL {
60-
baseURL := &base.URL{
61-
Scheme: u.Scheme,
62-
Host: u.Host,
63-
Path: path,
64-
RawQuery: query,
65-
}
66-
67-
if baseURL.RawQuery != "" {
68-
baseURL.RawQuery += "/"
69-
} else {
70-
baseURL.Path += "/"
71-
}
72-
73-
return baseURL
74-
}
75-
76-
func findMediaByURL(medias []*description.Media, baseURL *base.URL, u *base.URL) *description.Media {
59+
func findMediaByURL(
60+
medias []*description.Media,
61+
path string,
62+
query string,
63+
u *base.URL,
64+
) *description.Media {
7765
for _, media := range medias {
78-
u1, err := media.URL(baseURL)
79-
if err == nil && u1.String() == u.String() {
80-
return media
66+
if strings.HasPrefix(media.Control, "rtsp://") ||
67+
strings.HasPrefix(media.Control, "rtsps://") {
68+
if media.Control == u.String() {
69+
return media
70+
}
71+
} else {
72+
// FFmpeg format
73+
u1 := &base.URL{
74+
Scheme: u.Scheme,
75+
Host: u.Host,
76+
Path: path,
77+
RawQuery: query,
78+
}
79+
if query != "" {
80+
u1.RawQuery += "/" + media.Control
81+
} else {
82+
u1.Path += "/" + media.Control
83+
}
84+
if u1.String() == u.String() {
85+
return media
86+
}
87+
88+
// GStreamer format
89+
u2 := &base.URL{
90+
Scheme: u.Scheme,
91+
Host: u.Host,
92+
Path: path + "/" + media.Control,
93+
RawQuery: query,
94+
}
95+
if u2.String() == u.String() {
96+
return media
97+
}
8198
}
8299
}
83100

@@ -775,7 +792,7 @@ func (ss *ServerSession) handleRequestInner(sc *ServerConn, req *base.Request) (
775792
case ServerSessionStateInitial, ServerSessionStatePrePlay: // play
776793
medi = findMediaByTrackID(stream.desc.Medias, trackID)
777794
default: // record
778-
medi = findMediaByURL(ss.announcedDesc.Medias, recordBaseURL(req.URL, path, query), req.URL)
795+
medi = findMediaByURL(ss.announcedDesc.Medias, path, query, req.URL)
779796
}
780797

781798
if medi == nil {

0 commit comments

Comments
 (0)