OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2017 The Chromium Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 /*jshint esversion: 6 */ |
| 7 |
| 8 'use strict'; |
| 9 |
| 10 var $ = document.getElementById.bind(document); |
| 11 |
| 12 var testTable = $('test-table'); |
| 13 var nPeerConnectionsInput = $('num-peerconnections'); |
| 14 var startTestButton = $('start-test'); |
| 15 var cpuOveruseDetectionCheckbox = $('cpuoveruse-detection'); |
| 16 |
| 17 startTestButton.onclick = startTest; |
| 18 |
| 19 function logError(err) { |
| 20 console.err(err); |
| 21 } |
| 22 |
| 23 function addNewVideoElement() { |
| 24 var newRow = testTable.insertRow(-1); |
| 25 var newCell = newRow.insertCell(-1); |
| 26 var video = document.createElement('video'); |
| 27 video.autoplay = true; |
| 28 newCell.appendChild(video); |
| 29 return video; |
| 30 } |
| 31 |
| 32 function PeerConnection(id, cpuOveruseDetection) { |
| 33 this.id = id; |
| 34 this.cpuOveruseDetection = cpuOveruseDetection; |
| 35 |
| 36 this.localConnection = null; |
| 37 this.remoteConnection = null; |
| 38 |
| 39 this.remoteView = addNewVideoElement(); |
| 40 |
| 41 this.start = function() { |
| 42 var onGetUserMediaSuccess = this.onGetUserMediaSuccess.bind(this); |
| 43 navigator.mediaDevices.getUserMedia({ |
| 44 audio: true, |
| 45 video: true |
| 46 }) |
| 47 .then(onGetUserMediaSuccess) |
| 48 .catch(logError); |
| 49 }; |
| 50 |
| 51 this.onGetUserMediaSuccess = function(stream) { |
| 52 // Create local peer connection. |
| 53 this.localConnection = new RTCPeerConnection(null, { |
| 54 'optional': [{ |
| 55 'googCpuOveruseDetection': this.cpuOveruseDetection |
| 56 }] |
| 57 }); |
| 58 this.localConnection.onicecandidate = (event) => { |
| 59 this.onIceCandidate(this.remoteConnection, event); |
| 60 }; |
| 61 this.localConnection.addStream(stream); |
| 62 |
| 63 // Create remote peer connection. |
| 64 this.remoteConnection = new RTCPeerConnection(null, { |
| 65 'optional': [{ |
| 66 'googCpuOveruseDetection': this.cpuOveruseDetection |
| 67 }] |
| 68 }); |
| 69 this.remoteConnection.onicecandidate = (event) => { |
| 70 this.onIceCandidate(this.localConnection, event); |
| 71 }; |
| 72 this.remoteConnection.onaddstream = (e) => { |
| 73 this.remoteView.srcObject = e.stream; |
| 74 }; |
| 75 |
| 76 // Initiate call. |
| 77 var onCreateOfferSuccess = this.onCreateOfferSuccess.bind(this); |
| 78 this.localConnection.createOffer({ |
| 79 offerToReceiveAudio: 1, |
| 80 offerToReceiveVideo: 1 |
| 81 }) |
| 82 .then(onCreateOfferSuccess, logError); |
| 83 }; |
| 84 |
| 85 this.onCreateOfferSuccess = function(desc) { |
| 86 this.localConnection.setLocalDescription(desc); |
| 87 this.remoteConnection.setRemoteDescription(desc); |
| 88 |
| 89 var onCreateAnswerSuccess = this.onCreateAnswerSuccess.bind(this); |
| 90 this.remoteConnection.createAnswer() |
| 91 .then(onCreateAnswerSuccess, logError); |
| 92 }; |
| 93 |
| 94 this.onCreateAnswerSuccess = function(desc) { |
| 95 this.remoteConnection.setLocalDescription(desc); |
| 96 this.localConnection.setRemoteDescription(desc); |
| 97 }; |
| 98 |
| 99 this.onIceCandidate = function(connection, event) { |
| 100 if (event.candidate) { |
| 101 connection.addIceCandidate(new RTCIceCandidate(event.candidate)); |
| 102 } |
| 103 }; |
| 104 } |
| 105 |
| 106 function startTest() { |
| 107 var cpuOveruseDetection = cpuOveruseDetectionCheckbox.checked; |
| 108 var nPeerConnections = nPeerConnectionsInput.value; |
| 109 for (var i = 0; i < nPeerConnections; ++i) { |
| 110 new PeerConnection(i, cpuOveruseDetection).start(); |
| 111 } |
| 112 } |
OLD | NEW |