Fix yet another broken Content-Base for RTSP #1852

This commit is contained in:
Alex X
2025-08-29 16:45:19 +03:00
parent 33f0fd5fe6
commit beb82045ff
2 changed files with 31 additions and 6 deletions

View File

@@ -116,20 +116,39 @@ func findFmtpLine(payloadType uint8, descriptions []*sdp.MediaDescription) strin
// urlParse fix bugs:
// 1. Content-Base: rtsp://::ffff:192.168.1.123/onvif/profile.1/
// 2. Content-Base: rtsp://rtsp://turret2-cam.lan:554/stream1/
// 3. Content-Base: 192.168.253.220:1935/
func urlParse(rawURL string) (*url.URL, error) {
// fix https://github.com/AlexxIT/go2rtc/issues/830
if strings.HasPrefix(rawURL, "rtsp://rtsp://") {
rawURL = rawURL[7:]
}
// fix https://github.com/AlexxIT/go2rtc/issues/1852
if !strings.Contains(rawURL, "://") {
rawURL = "rtsp://" + rawURL
}
u, err := url.Parse(rawURL)
if err != nil && strings.HasSuffix(err.Error(), "after host") {
if i1 := strings.Index(rawURL, "://"); i1 > 0 {
if i2 := strings.IndexByte(rawURL[i1+3:], '/'); i2 > 0 {
return urlParse(rawURL[:i1+3+i2] + ":" + rawURL[i1+3+i2:])
}
if i := indexN(rawURL, '/', 3); i > 0 {
return urlParse(rawURL[:i] + ":" + rawURL[i:])
}
}
return u, err
}
func indexN(s string, c byte, n int) int {
var offset int
for {
i := strings.IndexByte(s[offset:], c)
if i < 0 {
break
}
if n--; n == 0 {
return offset + i
}
offset += i + 1
}
return -1
}

View File

@@ -11,14 +11,20 @@ func TestURLParse(t *testing.T) {
// https://github.com/AlexxIT/WebRTC/issues/395
base := "rtsp://::ffff:192.168.1.123/onvif/profile.1/"
u, err := urlParse(base)
assert.Empty(t, err)
assert.NoError(t, err)
assert.Equal(t, "::ffff:192.168.1.123:", u.Host)
// https://github.com/AlexxIT/go2rtc/issues/208
base = "rtsp://rtsp://turret2-cam.lan:554/stream1/"
u, err = urlParse(base)
assert.Empty(t, err)
assert.NoError(t, err)
assert.Equal(t, "turret2-cam.lan:554", u.Host)
// https://github.com/AlexxIT/go2rtc/issues/1852
base = "192.168.253.220:1935/"
u, err = urlParse(base)
assert.NoError(t, err)
assert.Equal(t, "192.168.253.220:1935", u.Host)
}
func TestBugSDP1(t *testing.T) {