-
Notifications
You must be signed in to change notification settings - Fork 54
/
RTCPeerConnection-addIceCandidate.html
591 lines (492 loc) · 18.5 KB
/
RTCPeerConnection-addIceCandidate.html
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<!doctype html>
<title>Test RTCPeerConnection.prototype.addIceCandidate</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
'use strict';
// Test is based on the following editor draft:
// https://w3c.github.io/webrtc-pc/archives/20170605/webrtc.htm
/*
4.3.2. Interface Definition
interface RTCPeerConnection : EventTarget {
...
Promise<void> addIceCandidate((RTCIceCandidateInit or RTCIceCandidate) candidate);
};
interface RTCIceCandidate {
readonly attribute DOMString candidate;
readonly attribute DOMString? sdpMid;
readonly attribute unsigned short? sdpMLineIndex;
readonly attribute DOMString? ufrag;
...
};
dictionary RTCIceCandidateInit {
DOMString candidate = "";
DOMString? sdpMid = null;
unsigned short? sdpMLineIndex = null;
DOMString ufrag;
};
*/
// SDP copied from JSEP Example 7.1
// It contains two media streams with different ufrags
// to test if candidate is added to the correct stream
const sdp = `v=0
o=- 4962303333179871722 1 IN IP4 0.0.0.0
s=-
t=0 0
a=ice-options:trickle
a=group:BUNDLE a1 v1
a=group:LS a1 v1
m=audio 10100 UDP/TLS/RTP/SAVPF 96 0 8 97 98
c=IN IP4 203.0.113.100
a=mid:a1
a=sendrecv
a=rtpmap:96 opus/48000/2
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:97 telephone-event/8000
a=rtpmap:98 telephone-event/48000
a=maxptime:120
a=extmap:1 urn:ietf:params:rtp-hdrext:sdes:mid
a=extmap:2 urn:ietf:params:rtp-hdrext:ssrc-audio-level
a=msid:47017fee-b6c1-4162-929c-a25110252400 f83006c5-a0ff-4e0a-9ed9-d3e6747be7d9
a=ice-ufrag:ETEn
a=ice-pwd:OtSK0WpNtpUjkY4+86js7ZQl
a=fingerprint:sha-256 19:E2:1C:3B:4B:9F:81:E6:B8:5C:F4:A5:A8:D8:73:04:BB:05:2F:70:9F:04:A9:0E:05:E9:26:33:E8:70:88:A2
a=setup:actpass
a=dtls-id:1
a=rtcp:10101 IN IP4 203.0.113.100
a=rtcp-mux
a=rtcp-rsize
m=video 10102 UDP/TLS/RTP/SAVPF 100 101
c=IN IP4 203.0.113.100
a=mid:v1
a=sendrecv
a=rtpmap:100 VP8/90000
a=rtpmap:101 rtx/90000
a=fmtp:101 apt=100
a=extmap:1 urn:ietf:params:rtp-hdrext:sdes:mid
a=rtcp-fb:100 ccm fir
a=rtcp-fb:100 nack
a=rtcp-fb:100 nack pli
a=msid:47017fee-b6c1-4162-929c-a25110252400 f30bdb4a-5db8-49b5-bcdc-e0c9a23172e0
a=ice-ufrag:BGKk
a=ice-pwd:mqyWsAjvtKwTGnvhPztQ9mIf
a=fingerprint:sha-256 19:E2:1C:3B:4B:9F:81:E6:B8:5C:F4:A5:A8:D8:73:04:BB:05:2F:70:9F:04:A9:0E:05:E9:26:33:E8:70:88:A2
a=setup:actpass
a=dtls-id:1
a=rtcp:10103 IN IP4 203.0.113.100
a=rtcp-mux
a=rtcp-rsize
`;
const sessionDesc = { type: 'offer', sdp };
// valid candidate attributes
const sdpMid = 'a1';
const sdpMLineIndex = 0;
const ufrag = 'ETEn';
const sdpMid2 = 'v1';
const sdpMLineIndex2 = 1;
const ufrag2 = 'BGKk';
const mediaLine1 = 'm=audio';
const mediaLine2 = 'm=video';
const candidateStr1 = 'candidate:1 1 udp 2113929471 203.0.113.100 10100 typ host';
const candidateStr2 = 'candidate:1 2 udp 2113929470 203.0.113.100 10101 typ host';
const invalidCandidateStr = '(Invalid) candidate \r\n string';
const candidateLine1 = `a=${candidateStr1}`;
const candidateLine2 = `a=${candidateStr2}`;
const endOfCandidateLine = 'a=end-of-candidates';
// Copied from MDN
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
// Check that a candidate line is found after the first media line
// but before the second, i.e. it belongs to the first media stream
function assert_candidate_line_between(sdp, beforeMediaLine, candidateLine, afterMediaLine) {
const line1 = escapeRegExp(beforeMediaLine);
const line2 = escapeRegExp(candidateLine);
const line3 = escapeRegExp(afterMediaLine);
const regex = new RegExp(`${line1}[^]+${line2}[^]+${line3}`);
assert_true(regex.test(sdp),
`Expect candidate line to be found between media lines ${beforeMediaLine} and ${afterMediaLine}`);
}
// Check that a candidate line is found after the second media line
// i.e. it belongs to the second media stream
function assert_candidate_line_after(sdp, beforeMediaLine, candidateLine) {
const line1 = escapeRegExp(beforeMediaLine);
const line2 = escapeRegExp(candidateLine);
const regex = new RegExp(`${line1}[^]+${line2}`);
assert_true(regex.test(sdp),
`Expect candidate line to be found after media line ${beforeMediaLine}`);
}
// Reject because WebIDL for addIceCandidate does not allow null argument
// null can be accidentally passed from onicecandidate event handler
// when null is used to indicate end of candidate
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() =>
promise_rejects(t, new TypeError(),
pc.addIceCandidate(null)));
}, 'Add null candidate should reject with TypeError');
/*
4.3.2. addIceCandidate
4. Return the result of enqueuing the following steps:
1. If remoteDescription is null return a promise rejected with a
newly created InvalidStateError.
*/
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return promise_rejects(t, 'InvalidStateError',
pc.addIceCandidate({
candidate: candidateStr1,
sdpMid, sdpMLineIndex, ufrag
}));
}, 'Add ICE candidate before setting remote description should reject with InvalidStateError');
/*
Success cases
*/
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() => pc.addIceCandidate({
candidate: candidateStr1,
sdpMid, sdpMLineIndex, ufrag
}));
}, 'Add ICE candidate after setting remote description should succeed');
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() => pc.addIceCandidate(new RTCIceCandidate({
candidate: candidateStr1,
sdpMid, sdpMLineIndex, ufrag
})));
}, 'Add ICE candidate with RTCIceCandidate should succeed');
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() => pc.addIceCandidate({ sdpMid }));
}, 'Add candidate with only valid sdpMid should succeed');
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() => pc.addIceCandidate({ sdpMLineIndex }));
}, 'Add candidate with only valid sdpMLineIndex should succeed');
/*
4.3.2. addIceCandidate
4.6.2. If candidate is applied successfully, the user agent MUST queue
a task that runs the following steps:
2. Let remoteDescription be connection's pendingRemoteDescription
if not null, otherwise connection's currentRemoteDescription.
3. Add candidate to remoteDescription.
*/
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() => pc.addIceCandidate({
candidate: candidateStr1,
sdpMid, sdpMLineIndex, ufrag
}))
.then(() => {
assert_candidate_line_between(pc.remoteDescription.sdp,
mediaLine1, candidateLine1, mediaLine2);
});
}, 'addIceCandidate with first sdpMid and sdpMLineIndex add candidate to first media stream');
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() => pc.addIceCandidate({
candidate: candidateStr2,
sdpMid: sdpMid2,
sdpMLineIndex: sdpMLineIndex2,
ufrag: ufrag2
}))
.then(() => {
assert_candidate_line_after(pc.remoteDescription.sdp,
mediaLine2, candidateLine2);
});
}, 'addIceCandidate with second sdpMid and sdpMLineIndex should add candidate to second media stream');
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() => pc.addIceCandidate({
candidate: candidateStr1,
sdpMid, sdpMLineIndex,
ufrag: null
}))
.then(() => {
assert_candidate_line_between(pc.remoteDescription.sdp,
mediaLine1, candidateLine1, mediaLine2);
});
}, 'Add candidate for first media stream with null ufrag should add candidate to first media stream');
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() => pc.addIceCandidate({
candidate: candidateStr1,
sdpMid, sdpMLineIndex, ufrag
}))
.then(() => pc.addIceCandidate({
candidate: candidateStr2,
sdpMid: sdpMid2,
sdpMLineIndex: sdpMLineIndex2,
ufrag: ufrag2
}))
.then(() => {
assert_candidate_line_between(pc.remoteDescription.sdp,
mediaLine1, candidateLine1, mediaLine2);
assert_candidate_line_after(pc.remoteDescription.sdp,
mediaLine2, candidateLine2);
});
}, 'Adding multiple candidates should add candidates to their corresponding media stream');
/*
4.3.2. addIceCandidate
4.6. If candidate.candidate is an empty string, process candidate as an
end-of-candidates indication for the corresponding media description
and ICE candidate generation.
2. If candidate is applied successfully, the user agent MUST queue
a task that runs the following steps:
2. Let remoteDescription be connection's pendingRemoteDescription
if not null, otherwise connection's currentRemoteDescription.
3. Add candidate to remoteDescription.
*/
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() => pc.addIceCandidate({
candidate: candidateStr1,
sdpMid, sdpMLineIndex, ufrag
}))
.then(() => pc.addIceCandidate({
candidate: '',
sdpMid, sdpMLineIndex,
ufrag
}))
.then(() => {
assert_candidate_line_between(pc.remoteDescription.sdp,
mediaLine1, candidateLine1, mediaLine2);
assert_candidate_line_between(pc.remoteDescription.sdp,
mediaLine1, endOfCandidateLine, mediaLine2);
});
}, 'Add with empty candidate string (end of candidate) should succeed');
/*
4.3.2. addIceCandidate
3. If both sdpMid and sdpMLineIndex are null, return a promise rejected
with a newly created TypeError.
*/
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() =>
promise_rejects(t, new TypeError(),
pc.addIceCandidate({
candidate: candidateStr1,
sdpMid: null,
sdpMLineIndex: null
})));
}, 'Add candidate with both sdpMid and sdpMLineIndex manually set to null should reject with TypeError');
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() =>
promise_rejects(t, new TypeError(),
pc.addIceCandidate({
candidate: candidateStr1
})));
}, 'Add candidate with only valid candidate string should reject with TypeError');
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() =>
promise_rejects(t, new TypeError(),
pc.addIceCandidate({
candidate: invalidCandidateStr,
sdpMid: null,
sdpMLineIndex: null
})));
}, 'Add candidate with invalid candidate string and both sdpMid and sdpMLineIndex null should reject with TypeError');
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() =>
promise_rejects(t, new TypeError(),
pc.addIceCandidate({})));
}, 'Add candidate with empty dict should reject with TypeError');
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() =>
promise_rejects(t, new TypeError(),
pc.addIceCandidate({
candidate: '',
sdpMid: null,
sdpMLineIndex: null,
ufrag: undefined
})));
}, 'Add candidate with manually filled default values should reject with TypeError');
/*
4.3.2. addIceCandidate
4.3. If candidate.sdpMid is not null, run the following steps:
1. If candidate.sdpMid is not equal to the mid of any media
description in remoteDescription , reject p with a newly
created OperationError and abort these steps.
*/
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() =>
promise_rejects(t, 'OperationError',
pc.addIceCandidate({
candidate: candidateStr1,
sdpMid: 'invalid', sdpMLineIndex, ufrag
})));
}, 'Add candidate with invalid sdpMid should reject with OperationError');
/*
4.3.2. addIceCandidate
4.4. Else, if candidate.sdpMLineIndex is not null, run the following
steps:
1. If candidate.sdpMLineIndex is equal to or larger than the
number of media descriptions in remoteDescription , reject p
with a newly created OperationError and abort these steps.
*/
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() =>
promise_rejects(t, 'OperationError',
pc.addIceCandidate({
candidate: candidateStr1,
sdpMLineIndex: 2,
ufrag
})));
}, 'Add candidate with invalid sdpMLineIndex should reject with OperationError');
// There is an "Else" for the statement:
// "Else, if candidate.sdpMLineIndex is not null, ..."
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() => pc.addIceCandidate({
candidate: candidateStr1,
sdpMid,
sdpMLineIndex: 2,
ufrag
}));
}, 'Invalid sdpMLineIndex should be ignored if valid sdpMid is provided');
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() => pc.addIceCandidate({
candidate: candidateStr2,
sdpMid: sdpMid2,
sdpMLineIndex: sdpMLineIndex2,
ufrag: null
}))
.then(() => {
assert_candidate_line_after(pc.remoteDescription.sdp,
mediaLine2, candidateLine2);
});
}, 'Add candidate for media stream 2 with null ufrag should succeed');
/*
4.3.2. addIceCandidate
4.5. If candidate.ufrag is neither undefined nor null, and is not equal
to any ufrag present in the corresponding media description of an
applied remote description, reject p with a newly created
OperationError and abort these steps.
*/
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() =>
promise_rejects(t, 'OperationError',
pc.addIceCandidate({
candidate: candidateStr1,
sdpMid, sdpMLineIndex,
ufrag: 'invalid'
})));
}, 'Add candidate with invalid ufrag should reject with OperationError');
/*
4.3.2. addIceCandidate
4.6.1. If candidate could not be successfully added the user agent MUST
queue a task that runs the following steps:
2. Reject p with a DOMException object whose name attribute has
the value OperationError and abort these steps.
*/
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() =>
promise_rejects(t, 'OperationError',
pc.addIceCandidate({
candidate: invalidCandidateStr,
sdpMid, sdpMLineIndex, ufrag
})));
}, 'Add candidate with invalid candidate string should reject with OperationError');
promise_test(t => {
const pc = new RTCPeerConnection();
t.add_cleanup(() => pc.close());
return pc.setRemoteDescription(sessionDesc)
.then(() =>
promise_rejects(t, 'OperationError',
pc.addIceCandidate({
candidate: candidateStr2,
sdpMid: sdpMid2,
sdpMLineIndex: sdpMLineIndex2,
ufrag: ufrag
})));
}, 'Add candidate with sdpMid belonging to different ufrag should reject with OperationError');
/*
TODO
4.3.2. addIceCandidate
4.6. In parallel, add the ICE candidate candidate as described in [JSEP]
(section 4.1.17.). Use candidate.ufrag to identify the ICE generation;
If the ufrag is null, process the candidate for the most recent ICE
generation.
- Call with candidate string containing partial malformed syntax, i.e. malformed IP.
Some browsers may ignore the syntax error and add it to the SDP regardless.
Non-Testable
4.3.2. addIceCandidate
4.6. (The steps are non-testable because the abort step in enqueue operation
steps in before they can reach here):
1. If candidate could not be successfully added the user agent MUST
queue a task that runs the following steps:
1. If connection's [[isClosed]] slot is true, then abort
these steps.
2. If candidate is applied successfully, the user agent MUST queue
a task that runs the following steps:
1. If connection's [[isClosed]] slot is true, then abort these steps.
Issues
w3c/webrtc-pc#1213
addIceCandidate end of candidates woes
w3c/webrtc-pc#1216
Clarify addIceCandidate behavior when adding candidate after end of candidate
w3c/webrtc-pc#1227
addIceCandidate may add ice candidate to the wrong remote description
w3c/webrtc-pc#1345
Make promise rejection/enqueing consistent
Coverage Report
Total: 23
Tested: 19
Not Tested: 2
Non-Testable: 2
*/
</script>