From d559ec0208ca41e4dc3cc3d94d92c46cc4d4ca39 Mon Sep 17 00:00:00 2001 From: Alex X Date: Fri, 26 Jul 2024 17:00:16 +0300 Subject: [PATCH] Fix wrong media values in SDP for some cameras #1278 --- pkg/rtsp/helpers.go | 10 ++++++++-- pkg/rtsp/rtsp_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/pkg/rtsp/helpers.go b/pkg/rtsp/helpers.go index c0f02f5b..1a687c01 100644 --- a/pkg/rtsp/helpers.go +++ b/pkg/rtsp/helpers.go @@ -38,8 +38,14 @@ func UnmarshalSDP(rawSDP []byte) ([]*core.Media, error) { // Fix invalid media type (errSDPInvalidValue) caused by // some TP-LINK IP camera, e.g. TL-IPC44GW - m := regexp.MustCompile("m=application/[^ ]+") - rawSDP = m.ReplaceAll(rawSDP, []byte("m=application")) + m := regexp.MustCompile("m=[^ ]+ ") + for _, i := range m.FindAll(rawSDP, -1) { + switch string(i[2 : len(i)-1]) { + case "audio", "video", "application": + default: + rawSDP = bytes.Replace(rawSDP, i, []byte("m=application "), 1) + } + } if err == io.EOF { rawSDP = append(rawSDP, '\n') diff --git a/pkg/rtsp/rtsp_test.go b/pkg/rtsp/rtsp_test.go index a13341b4..43248ba6 100644 --- a/pkg/rtsp/rtsp_test.go +++ b/pkg/rtsp/rtsp_test.go @@ -161,6 +161,48 @@ a=control:trackID=2 assert.Equal(t, "recvonly", medias[1].Direction) } +func TestBugSDP6(t *testing.T) { + // https://github.com/AlexxIT/go2rtc/issues/1278 + s := `v=0 +o=- 3730506281693 1 IN IP4 172.20.0.215 +s=IP camera Live streaming +i=stream1 +t=0 0 +a=tool:LIVE555 Streaming Media v2014.02.04 +a=type:broadcast +a=control:* +a=range:npt=0- +a=x-qt-text-nam:IP camera Live streaming +a=x-qt-text-inf:stream1 +m=video 0 RTP/AVP 26 +c=IN IP4 172.20.0.215 +b=AS:1500 +a=x-bufferdelay:0.55000 +a=x-dimensions:1280,960 +a=control:track1 +m=audio 0 RTP/AVP 0 +c=IN IP4 172.20.0.215 +b=AS:64 +a=x-bufferdelay:0.55000 +a=control:track2 +m=application 0 RTP/AVP 107 +c=IN IP4 172.20.0.215 +b=AS:1 +a=x-bufferdelay:0.55000 +a=rtpmap:107 vnd.onvif.metadata/90000/500 +a=control:track4 +m=vana 0 RTP/AVP 108 +c=IN IP4 172.20.0.215 +b=AS:1 +a=x-bufferdelay:0.55000 +a=rtpmap:108 video.analysis/90000/500 +a=control:track5 +` + medias, err := UnmarshalSDP([]byte(s)) + assert.Nil(t, err) + assert.Len(t, medias, 4) +} + func TestHikvisionPCM(t *testing.T) { s := `v=0 o=- 1721969533379665 1721969533379665 IN IP4 192.168.1.12