Skip to content

Commit beb8204

Browse files
committed
Fix yet another broken Content-Base for RTSP #1852
1 parent 33f0fd5 commit beb8204

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

pkg/rtsp/helpers.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,39 @@ func findFmtpLine(payloadType uint8, descriptions []*sdp.MediaDescription) strin
116116
// urlParse fix bugs:
117117
// 1. Content-Base: rtsp://::ffff:192.168.1.123/onvif/profile.1/
118118
// 2. Content-Base: rtsp://rtsp://turret2-cam.lan:554/stream1/
119+
// 3. Content-Base: 192.168.253.220:1935/
119120
func urlParse(rawURL string) (*url.URL, error) {
120121
// fix https://github.com/AlexxIT/go2rtc/issues/830
121122
if strings.HasPrefix(rawURL, "rtsp://rtsp://") {
122123
rawURL = rawURL[7:]
123124
}
124125

126+
// fix https://github.com/AlexxIT/go2rtc/issues/1852
127+
if !strings.Contains(rawURL, "://") {
128+
rawURL = "rtsp://" + rawURL
129+
}
130+
125131
u, err := url.Parse(rawURL)
126132
if err != nil && strings.HasSuffix(err.Error(), "after host") {
127-
if i1 := strings.Index(rawURL, "://"); i1 > 0 {
128-
if i2 := strings.IndexByte(rawURL[i1+3:], '/'); i2 > 0 {
129-
return urlParse(rawURL[:i1+3+i2] + ":" + rawURL[i1+3+i2:])
130-
}
133+
if i := indexN(rawURL, '/', 3); i > 0 {
134+
return urlParse(rawURL[:i] + ":" + rawURL[i:])
131135
}
132136
}
133137

134138
return u, err
135139
}
140+
141+
func indexN(s string, c byte, n int) int {
142+
var offset int
143+
for {
144+
i := strings.IndexByte(s[offset:], c)
145+
if i < 0 {
146+
break
147+
}
148+
if n--; n == 0 {
149+
return offset + i
150+
}
151+
offset += i + 1
152+
}
153+
return -1
154+
}

pkg/rtsp/rtsp_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ func TestURLParse(t *testing.T) {
1111
// https://github.com/AlexxIT/WebRTC/issues/395
1212
base := "rtsp://::ffff:192.168.1.123/onvif/profile.1/"
1313
u, err := urlParse(base)
14-
assert.Empty(t, err)
14+
assert.NoError(t, err)
1515
assert.Equal(t, "::ffff:192.168.1.123:", u.Host)
1616

1717
// https://github.com/AlexxIT/go2rtc/issues/208
1818
base = "rtsp://rtsp://turret2-cam.lan:554/stream1/"
1919
u, err = urlParse(base)
20-
assert.Empty(t, err)
20+
assert.NoError(t, err)
2121
assert.Equal(t, "turret2-cam.lan:554", u.Host)
22+
23+
// https://github.com/AlexxIT/go2rtc/issues/1852
24+
base = "192.168.253.220:1935/"
25+
u, err = urlParse(base)
26+
assert.NoError(t, err)
27+
assert.Equal(t, "192.168.253.220:1935", u.Host)
2228
}
2329

2430
func TestBugSDP1(t *testing.T) {

0 commit comments

Comments
 (0)