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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
|
<html>
<head>
<script type="text/javascript" src="webrtc_test_utilities.js"></script>
<script type="text/javascript" src="webrtc_test_audio.js"></script>
<script type="text/javascript">
$ = function(id) {
return document.getElementById(id);
};
window.onerror = function(errorMsg, url, lineNumber, column, errorObj) {
failTest('Error: ' + errorMsg + '\nScript: ' + url +
'\nLine: ' + lineNumber + '\nColumn: ' + column +
'\nStackTrace: ' + errorObj);
}
var gFirstConnection = null;
var gSecondConnection = null;
var gTestWithoutMsid = false;
var gLocalStream = null;
var gSentTones = '';
var gRemoteStreams = {};
// Default transform functions, overridden by some test cases.
var transformSdp = function(sdp) { return sdp; };
var transformRemoteSdp = function(sdp) { return sdp; };
var onLocalDescriptionError = function(error) { failTest(error); };
var onRemoteDescriptionError = function(error) { failTest(error); };
// When using external SDES, the crypto key is chosen by javascript.
var EXTERNAL_SDES_LINES = {
'audio': 'a=crypto:1 AES_CM_128_HMAC_SHA1_80 ' +
'inline:PS1uQCVeeCFCanVmcjkpPywjNWhcYD0mXXtxaVBR',
'video': 'a=crypto:1 AES_CM_128_HMAC_SHA1_80 ' +
'inline:d0RmdmcmVCspeEc3QGZiNWpVLFJhQX1cfHAwJSoj',
'data': 'a=crypto:1 AES_CM_128_HMAC_SHA1_80 ' +
'inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj'
};
setAllEventsOccuredHandler(reportTestSuccess);
// Test that we can setup a call with an audio and video track (must request
// video in this call since we expect video to be playing).
function call(constraints) {
createConnections(null);
navigator.webkitGetUserMedia(constraints,
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
waitForVideo('remote-view-1');
waitForVideo('remote-view-2');
}
// Test that we can setup a call with a video track and that the remote peer
// receives black frames if the local video track is disabled.
function callAndDisableLocalVideo(constraints) {
createConnections(null);
navigator.webkitGetUserMedia(constraints,
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
detectVideoPlaying('remote-view-1',
function () {
assertEquals(gLocalStream.getVideoTracks().length, 1);
gLocalStream.getVideoTracks()[0].enabled = false;
waitForBlackVideo('remote-view-1');
});
}
// Test that we can setup call with an audio and video track and check that
// the video resolution is as expected.
function callAndExpectResolution(constraints,
expected_width,
expected_height) {
createConnections(null);
navigator.webkitGetUserMedia(constraints,
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
waitForVideoWithResolution('remote-view-1',
expected_width,
expected_height);
waitForVideoWithResolution('remote-view-2',
expected_width,
expected_height);
}
// First calls without streams on any connections, and then adds a stream
// to peer connection 1 which gets sent to peer connection 2. We must wait
// for the first negotiation to complete before starting the second one, which
// is why we wait until the connection is stable before re-negotiating.
function callEmptyThenAddOneStreamAndRenegotiate(constraints) {
createConnections(null);
negotiate();
waitForConnectionToStabilize(gFirstConnection, function() {
navigator.webkitGetUserMedia(constraints,
addStreamToTheFirstConnectionAndNegotiate, printGetUserMediaError);
// Only the first connection is sending here.
waitForVideo('remote-view-2');
});
}
// The second set of constraints should request video (e.g. video:true) since
// we expect video to be playing after the second renegotiation.
function callAndRenegotiateToVideo(constraints, renegotiationConstraints) {
createConnections(null);
navigator.webkitGetUserMedia(constraints,
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
waitForConnectionToStabilize(gFirstConnection, function() {
gFirstConnection.removeStream(gLocalStream);
gSecondConnection.removeStream(gLocalStream);
navigator.webkitGetUserMedia(renegotiationConstraints,
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
waitForVideo('remote-view-1');
waitForVideo('remote-view-2');
});
}
// The second set of constraints should request audio (e.g. audio:true) since
// we expect audio to be playing after the second renegotiation.
function callAndRenegotiateToAudio(constraints, renegotiationConstraints) {
createConnections(null);
navigator.webkitGetUserMedia(constraints,
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
waitForConnectionToStabilize(gFirstConnection, function() {
gFirstConnection.removeStream(gLocalStream);
gSecondConnection.removeStream(gLocalStream);
navigator.webkitGetUserMedia(renegotiationConstraints,
addStreamToTheFirstConnectionAndNegotiate, printGetUserMediaError);
var onCallEstablished = function() {
ensureAudioPlaying(gSecondConnection);
};
waitForConnectionToStabilize(gFirstConnection, onCallEstablished);
});
}
// First makes a call between pc1 and pc2 where a stream is sent from pc1 to
// pc2. The stream sent from pc1 to pc2 is cloned from the stream received on
// pc2 to test that cloning of remote video tracks works as intended and is
// sent back to pc1.
function callAndForwardRemoteStream(constraints) {
createConnections(null);
navigator.webkitGetUserMedia(constraints,
addStreamToTheFirstConnectionAndNegotiate,
printGetUserMediaError);
var onRemoteStream2 = function() {
// Video has been detected to be playing in pc2. Clone the received
// stream and send it back to pc1.
gSecondConnection.addStream(gRemoteStreams['remote-view-2'].clone());
negotiate();
}
// Wait for remove video to be playing in pc2. Once video is playing,
// forward the remove stream from pc2 to pc1.
detectVideoPlaying('remote-view-2', onRemoteStream2);
// Wait for video to be forwarded back to connection 1.
waitForVideo('remote-view-1');
}
// First makes a call between pc1 and pc2, and then construct a new media
// stream using the remote audio and video tracks, connect the new media
// stream to a video element. These operations should not crash Chrome.
function ConnectChromiumSinkToRemoteAudioTrack() {
createConnections(null);
navigator.webkitGetUserMedia({audio: true, video: true},
addStreamToBothConnectionsAndNegotiate,
printGetUserMediaError);
detectVideoPlaying('remote-view-2', function() {
// Construct a new media stream with remote tracks.
var newStream = new webkitMediaStream();
newStream.addTrack(
gSecondConnection.getRemoteStreams()[0].getAudioTracks()[0]);
newStream.addTrack(
gSecondConnection.getRemoteStreams()[0].getVideoTracks()[0]);
var videoElement = document.createElement('video');
// No crash for this operation.
videoElement.src = URL.createObjectURL(newStream);
waitForVideo('remote-view-2');
});
}
// Test that we can setup call with an audio and video track and
// simulate that the remote peer don't support MSID.
function callWithoutMsidAndBundle() {
createConnections(null);
transformSdp = removeBundle;
transformRemoteSdp = removeMsid;
gTestWithoutMsid = true;
navigator.webkitGetUserMedia({audio: true, video: true},
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
waitForVideo('remote-view-1');
waitForVideo('remote-view-2');
}
// Test that we can't setup a call with an unsupported video codec
function negotiateUnsupportedVideoCodec() {
createConnections(null);
transformSdp = removeVideoCodec;
onLocalDescriptionError = function(error) {
var expectedMsg = 'Failed to set local offer sdp:' +
' Session error code: ERROR_CONTENT. Session error description:' +
' Failed to set local video description recv parameters..';
assertEquals(expectedMsg, error);
reportTestSuccess();
};
navigator.webkitGetUserMedia({audio: true, video: true},
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
}
// Test that we can't setup a call if one peer does not support encryption
function negotiateNonCryptoCall() {
createConnections(null);
transformSdp = removeCrypto;
onLocalDescriptionError = function(error) {
var expectedMsg = 'Failed to set local offer sdp:' +
' Called with SDP without DTLS fingerprint.';
assertEquals(expectedMsg, error);
reportTestSuccess();
};
navigator.webkitGetUserMedia({audio: true, video: true},
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
}
// Test that we can negotiate a call with an SDP offer that includes a
// b=AS:XX line to control audio and video bandwidth
function negotiateOfferWithBLine() {
createConnections(null);
transformSdp = addBandwithControl;
navigator.webkitGetUserMedia({audio: true, video: true},
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
waitForVideo('remote-view-1');
waitForVideo('remote-view-2');
}
// Test that we can setup call with legacy settings.
function callWithLegacySdp() {
transformSdp = function(sdp) {
return removeBundle(useGice(useExternalSdes(sdp)));
};
createConnections({
'mandatory': {'RtpDataChannels': true, 'DtlsSrtpKeyAgreement': false}
});
setupDataChannel({reliable: false});
navigator.webkitGetUserMedia({audio: true, video: true},
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
waitForVideo('remote-view-1');
waitForVideo('remote-view-2');
}
// Test only a data channel.
function callWithDataOnly() {
createConnections({optional:[{RtpDataChannels: true}]});
setupDataChannel({reliable: false});
negotiate();
}
function callWithSctpDataOnly() {
createConnections({optional: [{DtlsSrtpKeyAgreement: true}]});
setupSctpDataChannel({reliable: true});
negotiate();
}
// Test call with audio, video and a data channel.
function callWithDataAndMedia() {
createConnections({optional:[{RtpDataChannels: true}]});
setupDataChannel({reliable: false});
navigator.webkitGetUserMedia({audio: true, video: true},
addStreamToBothConnectionsAndNegotiate,
printGetUserMediaError);
waitForVideo('remote-view-1');
waitForVideo('remote-view-2');
}
function callWithSctpDataAndMedia() {
createConnections({optional: [{DtlsSrtpKeyAgreement: true}]});
setupSctpDataChannel({reliable: true});
navigator.webkitGetUserMedia({audio: true, video: true},
addStreamToBothConnectionsAndNegotiate,
printGetUserMediaError);
waitForVideo('remote-view-1');
waitForVideo('remote-view-2');
}
// Test call with a data channel and later add audio and video.
function callWithDataAndLaterAddMedia() {
createConnections({optional:[{RtpDataChannels: true}]});
setupDataChannel({reliable: false});
negotiate();
// Set an event handler for when the data channel has been closed.
setAllEventsOccuredHandler(function() {
// When the video is flowing the test is done.
setAllEventsOccuredHandler(reportTestSuccess);
navigator.webkitGetUserMedia({audio: true, video: true},
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
waitForVideo('remote-view-1');
waitForVideo('remote-view-2');
});
}
// Test that we can setup call and send DTMF.
function callAndSendDtmf(tones) {
createConnections(null);
navigator.webkitGetUserMedia({audio: true, video: true},
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
var onCallEstablished = function() {
// Send DTMF tones. Allocate the sender in the window to keep it from
// being garbage collected. https://crbug.com/486654.
var localAudioTrack = gLocalStream.getAudioTracks()[0];
window.dtmfSender = gFirstConnection.createDTMFSender(localAudioTrack);
window.dtmfSender.ontonechange = onToneChange;
window.dtmfSender.insertDTMF(tones);
// Wait for the DTMF tones callback.
addExpectedEvent();
var waitDtmf = setInterval(function() {
if (gSentTones == tones) {
clearInterval(waitDtmf);
eventOccured();
}
}, 100);
}
// Do the DTMF test after we have received video.
detectVideoPlaying('remote-view-2', onCallEstablished);
}
function testCreateOfferOptions() {
createConnections(null);
var offerOptions = {
'offerToReceiveAudio': false,
'offerToReceiveVideo': true
};
gFirstConnection.createOffer(
function(offer) {
assertEquals(-1, offer.sdp.search('m=audio'));
assertNotEquals(-1, offer.sdp.search('m=video'));
reportTestSuccess();
},
function(error) { failTest(error); },
offerOptions);
}
function callAndEnsureAudioIsPlaying(constraints) {
createConnections(null);
// Add the local stream to gFirstConnection to play one-way audio.
navigator.webkitGetUserMedia(constraints,
addStreamToTheFirstConnectionAndNegotiate, printGetUserMediaError);
var onCallEstablished = function() {
ensureAudioPlaying(gSecondConnection);
};
waitForConnectionToStabilize(gFirstConnection, onCallEstablished);
}
function callWithIsac16KAndEnsureAudioIsPlaying(constraints) {
transformSdp = function(sdp) {
sdp = sdp.replace(/m=audio (\d+) RTP\/SAVPF.*\r\n/g,
'm=audio $1 RTP/SAVPF 103 126\r\n');
sdp = sdp.replace('a=fmtp:111 minptime=10', 'a=fmtp:103 minptime=10');
if (sdp.search('a=rtpmap:103 ISAC/16000') == -1)
failTest('Missing iSAC 16K codec on Android; cannot force codec.');
return sdp;
}
callAndEnsureAudioIsPlaying(constraints);
}
function enableRemoteVideo(peerConnection, enabled) {
remoteStream = peerConnection.getRemoteStreams()[0];
remoteStream.getVideoTracks()[0].enabled = enabled;
}
function enableRemoteAudio(peerConnection, enabled) {
remoteStream = peerConnection.getRemoteStreams()[0];
remoteStream.getAudioTracks()[0].enabled = enabled;
}
function enableLocalVideo(peerConnection, enabled) {
localStream = peerConnection.getLocalStreams()[0];
localStream.getVideoTracks()[0].enabled = enabled;
}
function enableLocalAudio(peerConnection, enabled) {
localStream = peerConnection.getLocalStreams()[0];
localStream.getAudioTracks()[0].enabled = enabled;
}
function callAndEnsureRemoteAudioTrackMutingWorks() {
callAndEnsureAudioIsPlaying({audio: true, video: true});
setAllEventsOccuredHandler(function() {
setAllEventsOccuredHandler(reportTestSuccess);
// Call is up, now mute the remote track and check we stop playing out
// audio (after a small delay, we don't expect it to happen instantly).
enableRemoteAudio(gSecondConnection, false);
ensureSilence(gSecondConnection);
});
}
function callAndEnsureLocalAudioTrackMutingWorks() {
callAndEnsureAudioIsPlaying({audio: true, video: true});
setAllEventsOccuredHandler(function() {
setAllEventsOccuredHandler(reportTestSuccess);
// Call is up, now mute the local track of the sending side and ensure
// the receiving side stops receiving audio.
enableLocalAudio(gFirstConnection, false);
ensureSilence(gSecondConnection);
});
}
function callAndEnsureAudioTrackUnmutingWorks() {
callAndEnsureAudioIsPlaying({audio: true, video: true});
setAllEventsOccuredHandler(function() {
setAllEventsOccuredHandler(reportTestSuccess);
// Mute, wait a while, unmute, verify audio gets back up.
// (Also, ensure video muting doesn't affect audio).
enableRemoteAudio(gSecondConnection, false);
enableRemoteVideo(gSecondConnection, false);
setTimeout(function() {
enableRemoteAudio(gSecondConnection, true);
}, 500);
setTimeout(function() {
ensureAudioPlaying(gSecondConnection);
}, 1500);
});
}
function callAndEnsureLocalVideoMutingDoesntMuteAudio() {
callAndEnsureAudioIsPlaying({audio: true, video: true});
setAllEventsOccuredHandler(function() {
setAllEventsOccuredHandler(reportTestSuccess);
enableLocalVideo(gFirstConnection, false);
ensureAudioPlaying(gSecondConnection);
});
}
function callAndEnsureRemoteVideoMutingDoesntMuteAudio() {
callAndEnsureAudioIsPlaying({audio: true, video: true});
setAllEventsOccuredHandler(function() {
setAllEventsOccuredHandler(reportTestSuccess);
enableRemoteVideo(gSecondConnection, false);
ensureAudioPlaying(gSecondConnection);
});
}
function callAndEnsureVideoTrackMutingWorks() {
createConnections(null);
navigator.webkitGetUserMedia({audio: true, video: true},
addStreamToBothConnectionsAndNegotiate, printGetUserMediaError);
addExpectedEvent();
detectVideoPlaying('remote-view-2', function() {
// Disable the receiver's remote media stream. Video should stop.
// (Also, ensure muting audio doesn't affect video).
enableRemoteVideo(gSecondConnection, false);
enableRemoteAudio(gSecondConnection, false);
detectVideoStopped('remote-view-2', function() {
// Video has stopped: unmute and succeed if it starts playing again.
enableRemoteVideo(gSecondConnection, true);
detectVideoPlaying('remote-view-2', eventOccured);
})
});
}
// Test call with a new Video MediaStream that has been created based on a
// stream generated by getUserMedia.
function callWithNewVideoMediaStream() {
createConnections(null);
navigator.webkitGetUserMedia({audio: true, video: true},
createNewVideoStreamAndAddToBothConnections, printGetUserMediaError);
waitForVideo('remote-view-1');
waitForVideo('remote-view-2');
}
// Test call with a new Video MediaStream that has been created based on a
// stream generated by getUserMedia. When Video is flowing, an audio track
// is added to the sent stream and the video track is removed. This
// is to test that adding and removing of remote tracks on an existing
// mediastream works.
function callWithNewVideoMediaStreamLaterSwitchToAudio() {
createConnections(null);
navigator.webkitGetUserMedia({audio: true, video: true},
createNewVideoStreamAndAddToBothConnections, printGetUserMediaError);
waitForVideo('remote-view-1');
waitForVideo('remote-view-2');
// Set an event handler for when video is playing.
setAllEventsOccuredHandler(function() {
// Add an audio track to the local stream and remove the video track and
// then renegotiate. But first - setup the expectations.
var localStream = gFirstConnection.getLocalStreams()[0];
var remoteStream1 = gFirstConnection.getRemoteStreams()[0];
// Add an expected event that onaddtrack will be called on the remote
// mediastream received on gFirstConnection when the audio track is
// received.
addExpectedEvent();
remoteStream1.onaddtrack = function(){
assertEquals(remoteStream1.getAudioTracks()[0].id,
localStream.getAudioTracks()[0].id);
eventOccured();
}
// Add an expectation that the received video track is removed from
// gFirstConnection.
addExpectedEvent();
remoteStream1.onremovetrack = function() {
eventOccured();
}
// Add an expected event that onaddtrack will be called on the remote
// mediastream received on gSecondConnection when the audio track is
// received.
remoteStream2 = gSecondConnection.getRemoteStreams()[0];
addExpectedEvent();
remoteStream2.onaddtrack = function() {
assertEquals(remoteStream2.getAudioTracks()[0].id,
localStream.getAudioTracks()[0].id);
eventOccured();
}
// Add an expectation that the received video track is removed from
// gSecondConnection.
addExpectedEvent();
remoteStream2.onremovetrack = function() {
eventOccured();
}
// When all the above events have occurred- the test pass.
setAllEventsOccuredHandler(reportTestSuccess);
localStream.addTrack(gLocalStream.getAudioTracks()[0]);
localStream.removeTrack(localStream.getVideoTracks()[0]);
negotiate();
});
}
// Loads this page inside itself using an iframe, and ensures we can make a
// successful getUserMedia + peerconnection call inside the iframe.
function callInsideIframe(constraints) {
runInsideIframe(function(iframe) {
// Run a regular webrtc call inside the iframe.
iframe.contentWindow.call(constraints);
});
}
// Func should accept an iframe as its first argument.
function runInsideIframe(func) {
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.onload = onIframeLoaded;
iframe.src = window.location;
function onIframeLoaded() {
var iframe = window.document.querySelector('iframe');
// Propagate test success out of the iframe.
iframe.contentWindow.setAllEventsOccuredHandler(
window.parent.reportTestSuccess);
func(iframe);
}
}
// This function is used for setting up a test that:
// 1. Creates a data channel on |gFirstConnection| and sends data to
// |gSecondConnection|.
// 2. When data is received on |gSecondConnection| a message
// is sent to |gFirstConnection|.
// 3. When data is received on |gFirstConnection|, the data
// channel is closed. The test passes when the state transition completes.
function setupDataChannel(params) {
var sendDataString = "send some text on a data channel."
firstDataChannel = gFirstConnection.createDataChannel(
"sendDataChannel", params);
assertEquals('connecting', firstDataChannel.readyState);
// When |firstDataChannel| transition to open state, send a text string.
firstDataChannel.onopen = function() {
assertEquals('open', firstDataChannel.readyState);
firstDataChannel.send(sendDataString);
}
// When |firstDataChannel| receive a message, close the channel and
// initiate a new offer/answer exchange to complete the closure.
firstDataChannel.onmessage = function(event) {
assertEquals(event.data, sendDataString);
firstDataChannel.close();
negotiate();
}
// When |firstDataChannel| transition to closed state, the test pass.
addExpectedEvent();
firstDataChannel.onclose = function() {
assertEquals('closed', firstDataChannel.readyState);
eventOccured();
}
// Event handler for when |gSecondConnection| receive a new dataChannel.
gSecondConnection.ondatachannel = function (event) {
// Make secondDataChannel global to make sure it's not gc'd.
secondDataChannel = event.channel;
// When |secondDataChannel| receive a message, send a message back.
secondDataChannel.onmessage = function(event) {
assertEquals(event.data, sendDataString);
console.log("gSecondConnection received data");
assertEquals('open', secondDataChannel.readyState);
secondDataChannel.send(sendDataString);
}
}
}
// SCTP data channel setup is slightly different then RTP based
// channels. Due to a bug in libjingle, we can't send data immediately
// after channel becomes open. So for that reason in SCTP,
// we are sending data from second channel, when ondatachannel event is
// received. So data flow happens 2 -> 1 -> 2.
function setupSctpDataChannel(params) {
var sendDataString = "send some text on a data channel."
firstDataChannel = gFirstConnection.createDataChannel(
"sendDataChannel", params);
assertEquals('connecting', firstDataChannel.readyState);
// When |firstDataChannel| transition to open state, send a text string.
firstDataChannel.onopen = function() {
assertEquals('open', firstDataChannel.readyState);
}
// When |firstDataChannel| receive a message, send message back.
// initiate a new offer/answer exchange to complete the closure.
firstDataChannel.onmessage = function(event) {
assertEquals('open', firstDataChannel.readyState);
assertEquals(event.data, sendDataString);
firstDataChannel.send(sendDataString);
}
// Event handler for when |gSecondConnection| receive a new dataChannel.
gSecondConnection.ondatachannel = function (event) {
// Make secondDataChannel global to make sure it's not gc'd.
secondDataChannel = event.channel;
secondDataChannel.onopen = function() {
secondDataChannel.send(sendDataString);
}
// When |secondDataChannel| receive a message, close the channel and
// initiate a new offer/answer exchange to complete the closure.
secondDataChannel.onmessage = function(event) {
assertEquals(event.data, sendDataString);
assertEquals('open', secondDataChannel.readyState);
secondDataChannel.close();
negotiate();
}
// When |secondDataChannel| transition to closed state, the test pass.
addExpectedEvent();
secondDataChannel.onclose = function() {
assertEquals('closed', secondDataChannel.readyState);
eventOccured();
}
}
}
// Test call with a stream that has been created by getUserMedia, clone
// the stream to a cloned stream, send them via the same peer connection.
function addTwoMediaStreamsToOneConnection() {
createConnections(null);
navigator.webkitGetUserMedia({audio: true, video: true},
cloneStreamAndAddTwoStreamsToOneConnection, printGetUserMediaError);
}
function onToneChange(tone) {
gSentTones += tone.tone;
}
function createConnections(constraints) {
gFirstConnection = createConnection(constraints, 'remote-view-1');
assertEquals('stable', gFirstConnection.signalingState);
gSecondConnection = createConnection(constraints, 'remote-view-2');
assertEquals('stable', gSecondConnection.signalingState);
}
function createConnection(constraints, remoteView) {
var pc = new webkitRTCPeerConnection(null, constraints);
pc.onaddstream = function(event) {
onRemoteStream(event, remoteView);
}
return pc;
}
function displayAndRemember(localStream) {
var localStreamUrl = URL.createObjectURL(localStream);
$('local-view').src = localStreamUrl;
gLocalStream = localStream;
}
// Called if getUserMedia fails.
function printGetUserMediaError(error) {
var message = 'getUserMedia request unexpectedly failed:';
if (error.constraintName)
message += ' could not satisfy constraint ' + error.constraintName;
else
message += ' devices not working/user denied access.';
failTest(message);
}
// Called if getUserMedia succeeds and we want to send from both connections.
function addStreamToBothConnectionsAndNegotiate(localStream) {
displayAndRemember(localStream);
gFirstConnection.addStream(localStream);
gSecondConnection.addStream(localStream);
negotiate();
}
// Called if getUserMedia succeeds when we want to send from one connection.
function addStreamToTheFirstConnectionAndNegotiate(localStream) {
displayAndRemember(localStream);
gFirstConnection.addStream(localStream);
negotiate();
}
function verifyHasOneAudioAndVideoTrack(stream) {
assertEquals(1, stream.getAudioTracks().length);
assertEquals(1, stream.getVideoTracks().length);
}
// Called if getUserMedia succeeds, then clone the stream, send two streams
// from one peer connection.
function cloneStreamAndAddTwoStreamsToOneConnection(localStream) {
displayAndRemember(localStream);
var clonedStream = null;
if (typeof localStream.clone === "function") {
clonedStream = localStream.clone();
} else {
clonedStream = new webkitMediaStream(localStream);
}
gFirstConnection.addStream(localStream);
gFirstConnection.addStream(clonedStream);
// Verify the local streams are correct.
assertEquals(2, gFirstConnection.getLocalStreams().length);
verifyHasOneAudioAndVideoTrack(gFirstConnection.getLocalStreams()[0]);
verifyHasOneAudioAndVideoTrack(gFirstConnection.getLocalStreams()[1]);
// The remote side should receive two streams. After that, verify the
// remote side has the correct number of streams and tracks.
addExpectedEvent();
addExpectedEvent();
gSecondConnection.onaddstream = function(event) {
eventOccured();
}
setAllEventsOccuredHandler(function() {
// Negotiation complete, verify remote streams on the receiving side.
assertEquals(2, gSecondConnection.getRemoteStreams().length);
verifyHasOneAudioAndVideoTrack(gSecondConnection.getRemoteStreams()[0]);
verifyHasOneAudioAndVideoTrack(gSecondConnection.getRemoteStreams()[1]);
reportTestSuccess();
});
negotiate();
}
// A new MediaStream is created with video track from |localStream| and is
// added to both peer connections.
function createNewVideoStreamAndAddToBothConnections(localStream) {
displayAndRemember(localStream);
var newStream = new webkitMediaStream();
newStream.addTrack(localStream.getVideoTracks()[0]);
gFirstConnection.addStream(newStream);
gSecondConnection.addStream(newStream);
negotiate();
}
function negotiate() {
negotiateBetween(gFirstConnection, gSecondConnection);
}
function negotiateBetween(caller, callee) {
console.log("Negotiating call...");
// Not stable = negotiation is ongoing. The behavior of re-negotiating while
// a negotiation is ongoing is more or less undefined, so avoid this.
if (caller.signalingState != 'stable' || callee.signalingState != 'stable')
throw 'You can only negotiate when the connection is stable!';
connectOnIceCandidate(caller, callee);
caller.createOffer(
function (offer) {
onOfferCreated(offer, caller, callee);
});
}
function iceCandidateIsLoopback(candidate) {
return candidate.candidate.indexOf("127.0.0.1") > -1 ||
candidate.candidate.indexOf("::1") > -1;
}
// Helper function to invoke |callback| when gathering is completed.
function gatherIceCandidates(pc, callback) {
var candidates = [];
pc.createDataChannel("");
pc.onicecandidate = function(event) {
// null candidate indicates the gathering has completed.
if (event.candidate == null) {
callback(candidates);
} else {
candidates.push(event.candidate);
}
}
pc.createOffer(
function(offer) {
pc.setLocalDescription(offer);
},
function(error) { failTest(error); }
);
}
function callWithDevicePermissionGranted() {
var pc = new webkitRTCPeerConnection(null, null);
gatherIceCandidates(pc, function(candidates) {
assertEquals(candidates.length > 0, true);
for (i = 0; i < candidates.length; i++) {
assertEquals(iceCandidateIsLoopback(candidates[i]), false);
}
reportTestSuccess();
});
}
function callWithDevicePermissionDeniedAndEmptyIceServers() {
var pc = new webkitRTCPeerConnection({iceServers:[]}, null);
gatherIceCandidates(pc, function(candidates) {
assertEquals(candidates.length, 0);
reportTestSuccess();
});
}
function callAndExpectLoopbackCandidates() {
var pc = new webkitRTCPeerConnection(null, null);
gatherIceCandidates(pc, function(candidates) {
assertEquals(candidates.length > 0, true);
for (i = 0; i < candidates.length; i++) {
assertEquals(iceCandidateIsLoopback(candidates[i]), true);
}
reportTestSuccess();
});
}
function callWithDevicePermissionDeniedAndUndefinedIceServers() {
callAndExpectLoopbackCandidates();
}
function callWithMultipleRoutesDisabledAndUndefinedIceServers() {
callAndExpectLoopbackCandidates();
}
function onOfferCreated(offer, caller, callee) {
offer.sdp = transformSdp(offer.sdp);
console.log('Offer:\n' + offer.sdp);
caller.setLocalDescription(offer, function() {
assertEquals('have-local-offer', caller.signalingState);
receiveOffer(offer.sdp, caller, callee);
}, onLocalDescriptionError);
}
function receiveOffer(offerSdp, caller, callee) {
console.log("Receiving offer...");
offerSdp = transformRemoteSdp(offerSdp);
var parsedOffer = new RTCSessionDescription({ type: 'offer',
sdp: offerSdp });
callee.setRemoteDescription(parsedOffer,
function() {
assertEquals('have-remote-offer',
callee.signalingState);
callee.createAnswer(
function (answer) {
onAnswerCreated(answer, caller, callee);
});
},
onRemoteDescriptionError);
}
function removeMsid(offerSdp) {
offerSdp = offerSdp.replace(/a=msid-semantic.*\r\n/g, '');
offerSdp = offerSdp.replace('a=mid:audio\r\n', '');
offerSdp = offerSdp.replace('a=mid:video\r\n', '');
offerSdp = offerSdp.replace(/a=ssrc.*\r\n/g, '');
return offerSdp;
}
function removeVideoCodec(offerSdp) {
offerSdp = offerSdp.replace('a=rtpmap:100 VP8/90000\r\n',
'a=rtpmap:100 XVP8/90000\r\n');
return offerSdp;
}
function removeCrypto(offerSdp) {
offerSdp = offerSdp.replace(/a=crypto.*\r\n/g, 'a=Xcrypto\r\n');
offerSdp = offerSdp.replace(/a=fingerprint.*\r\n/g, '');
return offerSdp;
}
function addBandwithControl(offerSdp) {
offerSdp = offerSdp.replace('a=mid:audio\r\n', 'a=mid:audio\r\n'+
'b=AS:16\r\n');
offerSdp = offerSdp.replace('a=mid:video\r\n', 'a=mid:video\r\n'+
'b=AS:512\r\n');
return offerSdp;
}
function removeBundle(sdp) {
return sdp.replace(/a=group:BUNDLE .*\r\n/g, '');
}
function useGice(sdp) {
sdp = sdp.replace(/t=.*\r\n/g, function(subString) {
return subString + 'a=ice-options:google-ice\r\n';
});
return sdp;
}
function useExternalSdes(sdp) {
// Remove current crypto specification.
sdp = sdp.replace(/a=crypto.*\r\n/g, '');
sdp = sdp.replace(/a=fingerprint.*\r\n/g, '');
// Add external crypto. This is not compatible with |removeMsid|.
sdp = sdp.replace(/a=mid:(\w+)\r\n/g, function(subString, group) {
return subString + EXTERNAL_SDES_LINES[group] + '\r\n';
});
return sdp;
}
function onAnswerCreated(answer, caller, callee) {
answer.sdp = transformSdp(answer.sdp);
console.log('Answer:\n' + answer.sdp);
callee.setLocalDescription(answer,
function () {
assertEquals('stable', callee.signalingState);
},
onLocalDescriptionError);
receiveAnswer(answer.sdp, caller);
}
function receiveAnswer(answerSdp, caller) {
console.log("Receiving answer...");
answerSdp = transformRemoteSdp(answerSdp);
var parsedAnswer = new RTCSessionDescription({ type: 'answer',
sdp: answerSdp });
caller.setRemoteDescription(parsedAnswer,
function() {
assertEquals('stable', caller.signalingState);
},
onRemoteDescriptionError);
}
function connectOnIceCandidate(caller, callee) {
caller.onicecandidate = function(event) { onIceCandidate(event, callee); }
callee.onicecandidate = function(event) { onIceCandidate(event, caller); }
}
function onIceCandidate(event, target) {
if (event.candidate) {
var candidate = new RTCIceCandidate(event.candidate);
target.addIceCandidate(candidate);
}
}
function onRemoteStream(e, target) {
console.log("Receiving remote stream...");
if (gTestWithoutMsid && e.stream.id != "default") {
failTest('a default remote stream was expected but instead ' +
e.stream.id + ' was received.');
}
gRemoteStreams[target] = e.stream;
var remoteStreamUrl = URL.createObjectURL(e.stream);
var remoteVideo = $(target);
remoteVideo.src = remoteStreamUrl;
}
</script>
</head>
<body>
<table border="0">
<tr>
<td><video width="320" height="240" id="local-view" style="display:none"
autoplay muted></video></td>
<td><video width="320" height="240" id="remote-view-1"
style="display:none" autoplay></video></td>
<td><video width="320" height="240" id="remote-view-2"
style="display:none" autoplay></video></td>
<td><video width="320" height="240" id="remote-view-3"
style="display:none" autoplay></video></td>
<td><video width="320" height="240" id="remote-view-4"
style="display:none" autoplay></video></td>
<!-- Canvases are named after their corresponding video elements. -->
<td><canvas width="320" height="240" id="remote-view-1-canvas"
style="display:none"></canvas></td>
<td><canvas width="320" height="240" id="remote-view-2-canvas"
style="display:none"></canvas></td>
<td><canvas width="320" height="240" id="remote-view-3-canvas"
style="display:none"></canvas></td>
<td><canvas width="320" height="240" id="remote-view-4-canvas"
style="display:none"></canvas></td>
</tr>
</table>
</body>
</html>
|