Fix SDP parsing from cheap Chinese cameras

This commit is contained in:
Alexey Khit
2023-02-04 10:00:53 +03:00
parent da3137b6f0
commit 6d1c0a2459
2 changed files with 38 additions and 2 deletions
+6 -2
View File
@@ -5,6 +5,7 @@ import (
"github.com/AlexxIT/go2rtc/pkg/streamer"
"github.com/pion/rtcp"
"net/url"
"regexp"
"strings"
)
@@ -22,9 +23,12 @@ t=0 0`
func UnmarshalSDP(rawSDP []byte) ([]*streamer.Media, error) {
medias, err := streamer.UnmarshalSDP(rawSDP)
if err != nil {
// fix multiple `s=` https://github.com/AlexxIT/WebRTC/issues/417
re, _ := regexp.Compile("\ns=[^\n]+")
rawSDP = re.ReplaceAll(rawSDP, nil)
// fix SDP header for some cameras
i := bytes.Index(rawSDP, []byte("\nm="))
if i > 0 {
if i := bytes.Index(rawSDP, []byte("\nm=")); i > 0 {
rawSDP = append([]byte(sdpHeader), rawSDP[i:]...)
medias, err = streamer.UnmarshalSDP(rawSDP)
}
+32
View File
@@ -18,3 +18,35 @@ func TestURLParse(t *testing.T) {
assert.Empty(t, err)
assert.Equal(t, "turret2-cam.lan:554", u.Host)
}
func TestMultipleSinSDP(t *testing.T) {
s := `v=0
o=- 91674849066 1 IN IP4 192.168.1.123
s=RtspServer
i=live
t=0 0
a=control:*
a=range:npt=0-
m=video 0 RTP/AVP 96
c=IN IP4 0.0.0.0
s=RtspServer
i=live
a=control:track0
a=range:npt=0-
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=42001E;sprop-parameter-sets=Z0IAHvQCgC3I,aM48gA==
a=control:track0
m=audio 0 RTP/AVP 97
c=IN IP4 0.0.0.0
s=RtspServer
i=live
a=control:track1
a=range:npt=0-
a=rtpmap:97 MPEG4-GENERIC/8000/1
a=fmtp:97 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1588
a=control:track1
`
medias, err := UnmarshalSDP([]byte(s))
assert.Nil(t, err)
assert.NotNil(t, medias)
}