/
simulcast-playground.js
238 lines (220 loc) · 9.24 KB
/
simulcast-playground.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* webrtc interop testing using using selenium
* Copyright (c) 2016, Philipp Hancke
*/
const os = require('os');
const test = require('tape');
const buildDriver = require('./webdriver').buildDriver;
const getTestpage = require('./webdriver').getTestpage;
const WebRTCClient = require('./webrtcclient');
const SDPUtils = require('sdp');
const TIMEOUT = 30000;
function waitNVideosExist(driver, n) {
return driver.wait(() => driver.executeScript(n => document.querySelectorAll('video').length === n, n), TIMEOUT);
}
function waitAllVideosHaveEnoughData(driver) {
return driver.wait(() => driver.executeScript(() => {
const videos = document.querySelectorAll('video');
let ready = 0;
for (let i = 0; i < videos.length; i++) {
if (videos[i].readyState >= videos[i].HAVE_ENOUGH_DATA) {
ready++;
}
}
return ready === videos.length;
}), TIMEOUT);
}
// Edge Webdriver resolves quit slightly too early, wait a bit.
function maybeWaitForEdge(browserA, browserB, browserC, browserD) {
if (browserA === 'MicrosoftEdge' || browserB === 'MicrosoftEdge') {
return new Promise(resolve => {
setTimeout(resolve, 2000);
});
}
return Promise.resolve();
}
// Chrome simulcast-munging.
function mungeChromeSimulcast(sdp, numberOfSimulcastLayers) {
let cname;
let msid;
const sections = SDPUtils.splitSections(sdp);
SDPUtils.matchPrefix(sections[1], 'a=ssrc:').forEach(line => {
const media = SDPUtils.parseSsrcMedia(line);
if (media.attribute === 'cname') {
cname = media.value;
} else if (media.attribute === 'msid') {
msid = media.value;
}
});
const fidGroup = SDPUtils.matchPrefix(sections[1], 'a=ssrc-group:FID ')[0].substr(17);
const lines = sections[1].trim().split('\r\n').filter(line => {
return line.indexOf('a=ssrc:') !== 0 && line.indexOf('a=ssrc-group:') !== 0;
});
const simSSRCs = [];
const [videoSSRC1, rtxSSRC1] = fidGroup.split(' ').map(ssrc => parseInt(ssrc, 10));
lines.push('a=ssrc:' + videoSSRC1 + ' cname:' + cname);
lines.push('a=ssrc:' + videoSSRC1 + ' msid:' + msid);
lines.push('a=ssrc:' + rtxSSRC1 + ' cname:' + cname);
lines.push('a=ssrc:' + rtxSSRC1 + ' msid:' + msid);
lines.push('a=ssrc-group:FID ' + videoSSRC1 + ' ' + rtxSSRC1);
simSSRCs.push(videoSSRC1);
if (numberOfSimulcastLayers >= 2) {
const videoSSRC2 = videoSSRC1 + 1;
const rtxSSRC2 = videoSSRC1 + 2;
lines.push('a=ssrc:' + videoSSRC2 + ' cname:' + cname);
lines.push('a=ssrc:' + videoSSRC2 + ' msid:' + msid);
lines.push('a=ssrc:' + rtxSSRC2 + ' cname:' + cname);
lines.push('a=ssrc:' + rtxSSRC2 + ' msid:' + msid);
lines.push('a=ssrc-group:FID ' + videoSSRC2 + ' ' + rtxSSRC2);
simSSRCs.push(videoSSRC2);
}
if (numberOfSimulcastLayers >= 3) {
const videoSSRC3 = videoSSRC1 + 3;
const rtxSSRC3 = videoSSRC1 + 4;
lines.push('a=ssrc:' + videoSSRC3 + ' cname:' + cname);
lines.push('a=ssrc:' + videoSSRC3 + ' msid:' + msid);
lines.push('a=ssrc:' + rtxSSRC3 + ' cname:' + cname);
lines.push('a=ssrc:' + rtxSSRC3 + ' msid:' + msid);
lines.push('a=ssrc-group:FID ' + videoSSRC3 + ' ' + rtxSSRC3);
simSSRCs.push(videoSSRC3);
}
lines.push('a=ssrc-group:SIM ' + simSSRCs.join(' '));
sections[1] = lines.join('\r\n') + '\r\n';
return sections.join('');
}
// Splits the three ssrcs of simulcast into three different tracks/m-lines.
function splitSimulcast(sdp) {
const sections = SDPUtils.splitSections(sdp);
const candidates = SDPUtils.matchPrefix(sections[1], 'a=candidate:');
const dtls = SDPUtils.getDtlsParameters(sections[1], sections[0]);
const ice = SDPUtils.getIceParameters(sections[1], sections[0]);
const rtpParameters = SDPUtils.parseRtpParameters(sections[1]);
// unified plan things.
rtpParameters.headerExtensions = rtpParameters.headerExtensions.filter(ext => {
return ext.uri !== 'urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id' &&
ext.uri !== 'urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id' &&
ext.uri !== 'urn:ietf:params:rtp-hdrext:sdes:mid';
});
sdp = 'v=0\r\n' +
'o=mozilla...THIS_IS_SDPARTA-61.0 8324701712193024513 0 IN IP4 0.0.0.0\r\n' +
's=-\r\n' +
't=0 0\r\n' +
'a=fingerprint:' + dtls.fingerprints[0].algorithm + ' ' + dtls.fingerprints[0].value + '\r\n' +
'a=ice-ufrag:' + ice.usernameFragment + '\r\n' +
'a=ice-pwd:' + ice.password + '\r\n' +
'a=group:BUNDLE 0 1\r\n' +
'a=msid-semantic:WMS *\r\n';
// rtpParameters.codecs = rtpParameters.codecs.filter(c => c.payloadType === 98 || c.payloadType === 99);
const codecs = SDPUtils.writeRtpDescription('video', rtpParameters);
const fidGroups = SDPUtils.matchPrefix(sections[1], 'a=ssrc-group:FID ');
if (fidGroups.length > 0) {
const [videoSSRC1, rtxSSRC1] = fidGroups[0].substr(17).split(' ').map(ssrc => parseInt(ssrc, 10));
sdp += codecs +
'a=setup:actpass\r\n' +
'a=mid:0\r\n' +
'a=msid:low low\r\n' +
'a=ssrc:' + videoSSRC1 + ' cname:something\r\n';
'a=ssrc:' + rtxSSRC1 + ' cname:something\r\n' +
'a=ssrc-group:FID ' + videoSSRC1 + ' ' + rtxSSRC1 + '\r\n';
candidates.forEach(c => sdp += c + '\r\n');
}
if (fidGroups.length > 1) {
const [videoSSRC2, rtxSSRC2] = fidGroups[1].substr(17).split(' ').map(ssrc => parseInt(ssrc, 10));
sdp += codecs +
'a=setup:actpass\r\n' +
'a=mid:1\r\n' +
'a=msid:mid mid\r\n' +
'a=ssrc:' + videoSSRC2 + ' cname:something\r\n' +
'a=ssrc:' + rtxSSRC2 + ' cname:something\r\n' +
'a=ssrc-group:FID ' + videoSSRC2 + ' ' + rtxSSRC2 + '\r\n';
}
if (fidGroups.length > 2) {
const [videoSSRC3, rtxSSRC3] = fidGroups[2].substr(17).split(' ').map(ssrc => parseInt(ssrc, 10));
sdp += codecs +
'a=setup:actpass\r\n' +
'a=mid:2\r\n' +
'a=msid:hi hi\r\n' +
'a=ssrc:' + videoSSRC3 + ' cname:something\r\n' +
'a=ssrc:' + rtxSSRC3 + ' cname:something\r\n' +
'a=ssrc-group:FID ' + videoSSRC3 + ' ' + rtxSSRC3 + '\r\n';
}
return sdp;
}
// Merges the three m-lіnes into a single one.
function mergeSimulcast(sdp) {
const sections = SDPUtils.splitSections(sdp);
const candidates = SDPUtils.matchPrefix(sections[1], 'a=candidate:');
const dtls = SDPUtils.getDtlsParameters(sections[1], sections[0]);
const ice = SDPUtils.getIceParameters(sections[1], sections[0]);
const rtpParameters = SDPUtils.parseRtpParameters(sections[1]);
sdp = 'v=0\r\n' +
'o=mozilla...THIS_IS_SDPARTA-61.0 8324701712193024513 0 IN IP4 0.0.0.0\r\n' +
's=-\r\n' +
't=0 0\r\n' +
'a=fingerprint:' + dtls.fingerprints[0].algorithm + ' ' + dtls.fingerprints[0].value + '\r\n' +
'a=ice-ufrag:' + ice.usernameFragment + '\r\n' +
'a=ice-pwd:' + ice.password + '\r\n' +
'a=group:BUNDLE 0\r\n' +
'a=msid-semantic:WMS *\r\n';
const codecs = SDPUtils.writeRtpDescription('video', rtpParameters);
sdp += codecs;
candidates.forEach(c => sdp += c + '\r\n');
return sdp;
}
function simulcast(t, browserA, browserB, numberOfLayers) {
const driverA = buildDriver(browserA);
const driverB = buildDriver(browserB);
const clientA = new WebRTCClient(driverA);
const clientB = new WebRTCClient(driverB);
getTestpage(driverA)
.then(() => getTestpage(driverB))
.then(() => clientA.create())
.then(() => clientB.create())
.then(() => clientA.getUserMedia({audio: false, video: true}))
.then((stream) => {
t.pass('got user media');
return clientA.addStream(stream);
})
.then(() => clientA.createOffer())
.then(offer => {
t.pass('created offer');
if (browserA === 'chrome' || browserA === 'safari') {
offer.sdp = mungeChromeSimulcast(offer.sdp, numberOfLayers);
}
return clientA.setLocalDescription(offer);
})
.then(offerWithCandidates => {
t.pass('offer ready to signal');
offerWithCandidates.sdp = splitSimulcast(offerWithCandidates.sdp);
return clientB.setRemoteDescription(offerWithCandidates);
})
.then(() => clientB.createAnswer())
.then(answer => {
t.pass('created answer');
return clientB.setLocalDescription(answer); // modify answer here?
})
.then(answerWithCandidates => {
t.pass('answer ready to signal');
answerWithCandidates.sdp = mergeSimulcast(answerWithCandidates.sdp);
return clientA.setRemoteDescription(answerWithCandidates);
})
// wait for the iceConnectionState to become either connected/completed
// or failed.
.then(() => clientA.waitForIceConnectionStateChange())
.then(iceConnectionState => {
t.ok(iceConnectionState !== 'failed', 'ICE connection is established');
})
.then(() => waitNVideosExist(driverB, numberOfLayers))
.then(() => waitAllVideosHaveEnoughData(driverB))
.then(() => driverA.sleep(5000)) // wait a bit since its nice.
.then(() => Promise.all([driverA.quit(), driverB.quit()])
.then(() => maybeWaitForEdge(browserA, browserB))
.then(() => {
t.end();
}))
.catch(err => {
t.fail(err);
});
}
test('Chrome-Firefox, VP8', t => {
simulcast(t, 'chrome', 'firefox', 2);
});