Skip to content

Commit fef3091

Browse files
committed
Fix support webrtc creality format #1600
1 parent af7509e commit fef3091

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

internal/webrtc/client_creality.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/AlexxIT/go2rtc/pkg/core"
1212
"github.com/AlexxIT/go2rtc/pkg/webrtc"
13+
"github.com/pion/sdp/v3"
1314
)
1415

1516
// https://github.com/AlexxIT/go2rtc/issues/1600
@@ -27,7 +28,6 @@ func crealityClient(url string) (core.Producer, error) {
2728

2829
medias := []*core.Media{
2930
{Kind: core.KindVideo, Direction: core.DirectionRecvonly},
30-
{Kind: core.KindAudio, Direction: core.DirectionRecvonly},
3131
}
3232

3333
// TODO: return webrtc.SessionDescription
@@ -36,6 +36,8 @@ func crealityClient(url string) (core.Producer, error) {
3636
return nil, err
3737
}
3838

39+
log.Trace().Msgf("[webrtc] offer:\n%s", offer)
40+
3941
body, err := offerToB64(offer)
4042
if err != nil {
4143
return nil, err
@@ -61,6 +63,12 @@ func crealityClient(url string) (core.Producer, error) {
6163
return nil, err
6264
}
6365

66+
log.Trace().Msgf("[webrtc] answer:\n%s", answer)
67+
68+
if answer, err = fixCrealitySDP(answer); err != nil {
69+
return nil, err
70+
}
71+
6472
if err = prod.SetAnswer(answer); err != nil {
6573
return nil, err
6674
}
@@ -108,3 +116,37 @@ func answerFromB64(r io.Reader) (string, error) {
108116
// string "v=0..."
109117
return v["sdp"], nil
110118
}
119+
120+
func fixCrealitySDP(value string) (string, error) {
121+
var sd sdp.SessionDescription
122+
if err := sd.UnmarshalString(value); err != nil {
123+
return "", err
124+
}
125+
126+
md := sd.MediaDescriptions[0]
127+
128+
// important to skip first codec, because second codec will be used
129+
skip := md.MediaName.Formats[0]
130+
md.MediaName.Formats = md.MediaName.Formats[1:]
131+
132+
attrs := make([]sdp.Attribute, 0, len(md.Attributes))
133+
for _, attr := range md.Attributes {
134+
switch attr.Key {
135+
case "fmtp", "rtpmap":
136+
// important to skip fmtp with x-google, because this is second fmtp for same codec
137+
// and pion library will fail parsing this SDP
138+
if strings.HasPrefix(attr.Value, skip) || strings.Contains(attr.Value, "x-google") {
139+
continue
140+
}
141+
}
142+
attrs = append(attrs, attr)
143+
}
144+
145+
md.Attributes = attrs
146+
147+
b, err := sd.Marshal()
148+
if err != nil {
149+
return "", err
150+
}
151+
return string(b), nil
152+
}

internal/webrtc/webrtc_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package webrtc
22

33
import (
44
"encoding/json"
5+
"strings"
56
"testing"
67

78
"github.com/AlexxIT/go2rtc/internal/api/ws"
@@ -36,3 +37,37 @@ func TestWebRTCAPIv2(t *testing.T) {
3637
require.Equal(t, "v=0\n...", offer.SDP)
3738
require.Equal(t, "stun:stun.l.google.com:19302", offer.ICEServers[0].URLs[0])
3839
}
40+
41+
func TestCrealitySDP(t *testing.T) {
42+
sdp := `v=0
43+
o=- 1495799811084970 1495799811084970 IN IP4 0.0.0.0
44+
s=-
45+
t=0 0
46+
a=msid-semantic:WMS *
47+
a=group:BUNDLE 0
48+
m=video 9 UDP/TLS/RTP/SAVPF 96 98
49+
a=rtcp-fb:98 nack
50+
a=rtcp-fb:98 nack pli
51+
a=fmtp:96 profile-level-id=42e01f;level-asymmetry-allowed=1
52+
a=fmtp:98 profile-level-id=42e01f;packetization-mode=1;level-asymmetry-allowed=1
53+
a=fmtp:98 x-google-max-bitrate=6000;x-google-min-bitrate=2000;x-google-start-bitrate=4000
54+
a=rtpmap:96 H264/90000
55+
a=rtpmap:98 H264/90000
56+
a=ssrc:1 cname:pear
57+
c=IN IP4 0.0.0.0
58+
a=sendonly
59+
a=mid:0
60+
a=rtcp-mux
61+
a=ice-ufrag:7AVa
62+
a=ice-pwd:T+F/5y05Paw+mtG5Jrd8N3
63+
a=ice-options:trickle
64+
a=fingerprint:sha-256 A5:AB:C0:4E:29:5B:BD:3B:7D:88:24:6C:56:6B:2A:79:A3:76:99:35:57:75:AD:C8:5A:A6:34:20:88:1B:68:EF
65+
a=setup:passive
66+
a=candidate:1 1 UDP 2015363327 172.22.233.10 48929 typ host
67+
a=candidate:2 1 TCP 1015021823 172.22.233.10 0 typ host tcptype active
68+
a=candidate:3 1 TCP 1010827519 172.22.233.10 60677 typ host tcptype passive
69+
`
70+
sdp, err := fixCrealitySDP(sdp)
71+
require.Nil(t, err)
72+
require.False(t, strings.Contains(sdp, "x-google-max-bitrate"))
73+
}

0 commit comments

Comments
 (0)