Update fix for parsing RTSP SDP
This commit is contained in:
@@ -2,6 +2,7 @@ package streams
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/AlexxIT/go2rtc/pkg/fake"
|
"github.com/AlexxIT/go2rtc/pkg/fake"
|
||||||
|
"github.com/AlexxIT/go2rtc/pkg/rtsp"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/streamer"
|
"github.com/AlexxIT/go2rtc/pkg/streamer"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -103,7 +104,7 @@ a=control:streamid=0
|
|||||||
|
|
||||||
func TestRouting(t *testing.T) {
|
func TestRouting(t *testing.T) {
|
||||||
prod := &fake.Producer{}
|
prod := &fake.Producer{}
|
||||||
prod.Medias, _ = streamer.UnmarshalRTSPSDP([]byte(dahuaSimple))
|
prod.Medias, _ = rtsp.UnmarshalSDP([]byte(dahuaSimple))
|
||||||
assert.Len(t, prod.Medias, 3)
|
assert.Len(t, prod.Medias, 3)
|
||||||
|
|
||||||
HandleFunc("fake", func(url string) (streamer.Producer, error) {
|
HandleFunc("fake", func(url string) (streamer.Producer, error) {
|
||||||
|
|||||||
+29
-17
@@ -282,13 +282,7 @@ func (c *Conn) Describe() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fix bug in Sonoff camera SDP "o=- 1 1 IN IP4 rom t_rtsplin"
|
c.Medias, err = UnmarshalSDP(res.Body)
|
||||||
// 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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -479,7 +473,7 @@ func (c *Conn) Accept() error {
|
|||||||
return errors.New("wrong content type")
|
return errors.New("wrong content type")
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Medias, err = streamer.UnmarshalRTSPSDP(req.Body)
|
c.Medias, err = UnmarshalSDP(req.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -728,17 +722,35 @@ type RTCP struct {
|
|||||||
Packets []rtcp.Packet
|
Packets []rtcp.Packet
|
||||||
}
|
}
|
||||||
|
|
||||||
func between(s, sub1, sub2 string) (res string, ok1 bool, ok2 bool) {
|
const sdpHeader = `v=0
|
||||||
i := strings.Index(s, sub1)
|
o=- 0 0 IN IP4 0.0.0.0
|
||||||
if i >= 0 {
|
s=-
|
||||||
ok1 = true
|
t=0 0`
|
||||||
s = s[i+len(sub1):]
|
|
||||||
|
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)
|
// fix bug in ONVIF spec
|
||||||
if i >= 0 {
|
// https://www.onvif.org/specs/stream/ONVIF-Streaming-Spec-v241.pdf
|
||||||
return s[:i], ok1, true
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -180,26 +180,6 @@ func UnmarshalSDP(rawSDP []byte) ([]*Media, error) {
|
|||||||
return medias, nil
|
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) {
|
func MarshalSDP(medias []*Media) ([]byte, error) {
|
||||||
sd := &sdp.SessionDescription{}
|
sd := &sdp.SessionDescription{}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user