From d28debabe9691e9ed32a890a19081226ae976096 Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Mon, 22 Aug 2022 14:44:19 +0300 Subject: [PATCH] Update fix for parsing RTSP SDP --- cmd/streams/stream_test.go | 3 ++- pkg/rtsp/conn.go | 46 ++++++++++++++++++++++++-------------- pkg/streamer/media.go | 20 ----------------- 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/cmd/streams/stream_test.go b/cmd/streams/stream_test.go index 959e224f..b16ceae4 100644 --- a/cmd/streams/stream_test.go +++ b/cmd/streams/stream_test.go @@ -2,6 +2,7 @@ package streams import ( "github.com/AlexxIT/go2rtc/pkg/fake" + "github.com/AlexxIT/go2rtc/pkg/rtsp" "github.com/AlexxIT/go2rtc/pkg/streamer" "github.com/stretchr/testify/assert" "testing" @@ -103,7 +104,7 @@ a=control:streamid=0 func TestRouting(t *testing.T) { prod := &fake.Producer{} - prod.Medias, _ = streamer.UnmarshalRTSPSDP([]byte(dahuaSimple)) + prod.Medias, _ = rtsp.UnmarshalSDP([]byte(dahuaSimple)) assert.Len(t, prod.Medias, 3) HandleFunc("fake", func(url string) (streamer.Producer, error) { diff --git a/pkg/rtsp/conn.go b/pkg/rtsp/conn.go index 9820e385..aa278aea 100644 --- a/pkg/rtsp/conn.go +++ b/pkg/rtsp/conn.go @@ -282,13 +282,7 @@ func (c *Conn) Describe() error { } } - // fix bug in Sonoff camera SDP "o=- 1 1 IN IP4 rom t_rtsplin" - // TODO: make some universal fix - if i := bytes.Index(res.Body, []byte("rom t_rtsplin")); i > 0 { - res.Body[i+3] = '_' - } - - c.Medias, err = streamer.UnmarshalRTSPSDP(res.Body) + c.Medias, err = UnmarshalSDP(res.Body) if err != nil { return err } @@ -479,7 +473,7 @@ func (c *Conn) Accept() error { return errors.New("wrong content type") } - c.Medias, err = streamer.UnmarshalRTSPSDP(req.Body) + c.Medias, err = UnmarshalSDP(req.Body) if err != nil { return err } @@ -728,17 +722,35 @@ type RTCP struct { Packets []rtcp.Packet } -func between(s, sub1, sub2 string) (res string, ok1 bool, ok2 bool) { - i := strings.Index(s, sub1) - if i >= 0 { - ok1 = true - s = s[i+len(sub1):] +const sdpHeader = `v=0 +o=- 0 0 IN IP4 0.0.0.0 +s=- +t=0 0` + +func UnmarshalSDP(rawSDP []byte) ([]*streamer.Media, error) { + medias, err := streamer.UnmarshalSDP(rawSDP) + if err != nil { + // fix SDP header for some cameras + i := bytes.Index(rawSDP, []byte("\nm=")) + if i > 0 { + rawSDP = append([]byte(sdpHeader), rawSDP[i:]...) + medias, err = streamer.UnmarshalSDP(rawSDP) + } + if err != nil { + return nil, err + } } - i = strings.Index(s, sub2) - if i >= 0 { - return s[:i], ok1, true + // fix bug in ONVIF spec + // https://www.onvif.org/specs/stream/ONVIF-Streaming-Spec-v241.pdf + for _, media := range medias { + switch media.Direction { + case streamer.DirectionRecvonly, "": + media.Direction = streamer.DirectionSendonly + case streamer.DirectionSendonly: + media.Direction = streamer.DirectionRecvonly + } } - return s, ok1, false + return medias, nil } diff --git a/pkg/streamer/media.go b/pkg/streamer/media.go index 9ac10f48..529236ac 100644 --- a/pkg/streamer/media.go +++ b/pkg/streamer/media.go @@ -180,26 +180,6 @@ func UnmarshalSDP(rawSDP []byte) ([]*Media, error) { return medias, nil } -func UnmarshalRTSPSDP(rawSDP []byte) ([]*Media, error) { - medias, err := UnmarshalSDP(rawSDP) - if err != nil { - return nil, err - } - - // fix bug in ONVIF spec - // https://www.onvif.org/specs/stream/ONVIF-Streaming-Spec-v241.pdf - for _, media := range medias { - switch media.Direction { - case DirectionRecvonly, "": - media.Direction = DirectionSendonly - case DirectionSendonly: - media.Direction = DirectionRecvonly - } - } - - return medias, nil -} - func MarshalSDP(medias []*Media) ([]byte, error) { sd := &sdp.SessionDescription{}