This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mediaengine_test.go
171 lines (148 loc) · 4.32 KB
/
mediaengine_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// +build !js
package webrtc
import (
"regexp"
"strings"
"testing"
"github.com/pion/sdp/v3"
"github.com/stretchr/testify/assert"
)
const sdpValue = `v=0
o=- 884433216 1576829404 IN IP4 0.0.0.0
s=-
t=0 0
a=fingerprint:sha-256 1D:6B:6D:18:95:41:F9:BC:E4:AC:25:6A:26:A3:C8:09:D2:8C:EE:1B:7D:54:53:33:F7:E3:2C:0D:FE:7A:9D:6B
a=group:BUNDLE 0 1 2
m=audio 9 UDP/TLS/RTP/SAVPF 0 8 111 9
c=IN IP4 0.0.0.0
a=mid:0
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:111 opus/48000/2
a=fmtp:111 minptime=10;useinbandfec=1
a=rtpmap:9 G722/8000
a=ssrc:1823804162 cname:pion1
a=ssrc:1823804162 msid:pion1 audio
a=ssrc:1823804162 mslabel:pion1
a=ssrc:1823804162 label:audio
a=msid:pion1 audio
m=video 9 UDP/TLS/RTP/SAVPF 105 115 135
c=IN IP4 0.0.0.0
a=mid:1
a=rtpmap:105 VP8/90000
a=rtpmap:115 H264/90000
a=fmtp:115 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f
a=rtpmap:135 VP9/90000
a=ssrc:2949882636 cname:pion2
a=ssrc:2949882636 msid:pion2 video
a=ssrc:2949882636 mslabel:pion2
a=ssrc:2949882636 label:video
a=msid:pion2 video
m=application 9 DTLS/SCTP 5000
c=IN IP4 0.0.0.0
a=mid:2
a=sctpmap:5000 webrtc-datachannel 1024
`
func TestCodecRegistration(t *testing.T) {
api := NewAPI()
const invalidPT = 255
api.mediaEngine.RegisterDefaultCodecs()
testCases := []struct {
c uint8
e error
}{
{DefaultPayloadTypePCMU, nil},
{DefaultPayloadTypePCMA, nil},
{DefaultPayloadTypeG722, nil},
{DefaultPayloadTypeOpus, nil},
{DefaultPayloadTypeVP8, nil},
{DefaultPayloadTypeVP9, nil},
{DefaultPayloadTypeH264, nil},
{invalidPT, ErrCodecNotFound},
}
for _, f := range testCases {
_, err := api.mediaEngine.getCodec(f.c)
assert.Equal(t, f.e, err)
}
_, err := api.mediaEngine.getCodecSDP(sdp.Codec{PayloadType: invalidPT})
assert.Equal(t, err, ErrCodecNotFound)
}
func TestPopulateFromSDP(t *testing.T) {
m := MediaEngine{}
assertCodecWithPayloadType := func(name string, payloadType uint8) {
for _, c := range m.codecs {
if c.PayloadType == payloadType && c.Name == name {
return
}
}
t.Fatalf("Failed to find codec(%s) with PayloadType(%d)", name, payloadType)
}
m.RegisterDefaultCodecs()
assert.NoError(t, m.PopulateFromSDP(SessionDescription{SDP: sdpValue}))
assertCodecWithPayloadType(Opus, 111)
assertCodecWithPayloadType(VP8, 105)
assertCodecWithPayloadType(H264, 115)
assertCodecWithPayloadType(VP9, 135)
}
// pion/webrtc#1078
func TestOpusCase(t *testing.T) {
pc, err := NewPeerConnection(Configuration{})
assert.NoError(t, err)
_, err = pc.AddTransceiverFromKind(RTPCodecTypeAudio)
assert.NoError(t, err)
offer, err := pc.CreateOffer(nil)
assert.NoError(t, err)
assert.True(t, regexp.MustCompile(`(?m)^a=rtpmap:\d+ opus/48000/2`).MatchString(offer.SDP))
assert.NoError(t, pc.Close())
}
// pion/webrtc#1442
func TestCaseInsensitive(t *testing.T) {
m := MediaEngine{}
m.RegisterDefaultCodecs()
testCases := []struct {
nameUpperCase string
nameLowerCase string
clockrate uint32
fmtp string
}{
{strings.ToUpper(Opus), strings.ToLower(Opus), 48000, "minptime=10;useinbandfec=1"},
{strings.ToUpper(PCMU), strings.ToLower(PCMU), 8000, ""},
{strings.ToUpper(PCMA), strings.ToLower(PCMA), 8000, ""},
{strings.ToUpper(G722), strings.ToLower(G722), 8000, ""},
{strings.ToUpper(VP8), strings.ToLower(VP8), 90000, ""},
{strings.ToUpper(VP9), strings.ToLower(VP9), 90000, ""},
{strings.ToUpper(H264), strings.ToLower(H264), 90000, "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f"},
}
for _, f := range testCases {
upperCase, err := m.getCodecSDP(sdp.Codec{
Name: f.nameUpperCase,
ClockRate: f.clockrate,
Fmtp: f.fmtp,
})
assert.NoError(t, err)
lowerCase, err := m.getCodecSDP(sdp.Codec{
Name: f.nameLowerCase,
ClockRate: f.clockrate,
Fmtp: f.fmtp,
})
assert.NoError(t, err)
assert.Equal(t, upperCase, lowerCase)
}
}
func TestGetCodecsByName(t *testing.T) {
var cdc *RTPCodec
m := MediaEngine{}
assert.NoError(t, m.PopulateFromSDP(SessionDescription{SDP: sdpValue}))
assertGetCodecsByName := func(name string) {
for _, cdc = range m.GetCodecsByName(name) {
if strings.EqualFold(cdc.Name, name) {
return
}
}
t.Fatalf("Failed to getting codec(%s) by name (%s)", cdc.Name, name)
}
assertGetCodecsByName(VP8)
assertGetCodecsByName(H264)
assertGetCodecsByName(VP9)
assertGetCodecsByName(Opus)
}