From 6d1c0a2459864376d137ab76bce855e874110904 Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Sat, 4 Feb 2023 10:00:53 +0300 Subject: [PATCH] Fix SDP parsing from cheap Chinese cameras --- pkg/rtsp/helpers.go | 8 ++++++-- pkg/rtsp/rtsp_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/pkg/rtsp/helpers.go b/pkg/rtsp/helpers.go index f748c9a2..893f3b87 100644 --- a/pkg/rtsp/helpers.go +++ b/pkg/rtsp/helpers.go @@ -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) } diff --git a/pkg/rtsp/rtsp_test.go b/pkg/rtsp/rtsp_test.go index 7e32b272..e7024f4e 100644 --- a/pkg/rtsp/rtsp_test.go +++ b/pkg/rtsp/rtsp_test.go @@ -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) +}