Index: tools/perf/page_sets/webrtc_cases/multiple-peerconnections.js |
diff --git a/tools/perf/page_sets/webrtc_cases/multiple-peerconnections.js b/tools/perf/page_sets/webrtc_cases/multiple-peerconnections.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4401d10bd24937f23ce03c23a8a19e15137b583a |
--- /dev/null |
+++ b/tools/perf/page_sets/webrtc_cases/multiple-peerconnections.js |
@@ -0,0 +1,112 @@ |
+/* |
+ * Copyright 2017 The Chromium Authors. All rights reserved. |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+/*jshint esversion: 6 */ |
+ |
+'use strict'; |
+ |
+var $ = document.getElementById.bind(document); |
+ |
+var testTable = $('test-table'); |
+var nPeerConnectionsInput = $('num-peerconnections'); |
+var startTestButton = $('start-test'); |
+var cpuOveruseDetectionCheckbox = $('cpuoveruse-detection'); |
+ |
+startTestButton.onclick = startTest; |
+ |
+function logError(err) { |
+ console.err(err); |
+} |
+ |
+function addNewVideoElement() { |
+ var newRow = testTable.insertRow(-1); |
+ var newCell = newRow.insertCell(-1); |
+ var video = document.createElement('video'); |
+ video.autoplay = true; |
+ newCell.appendChild(video); |
+ return video; |
+} |
+ |
+function PeerConnection(id, cpuOveruseDetection) { |
+ this.id = id; |
+ this.cpuOveruseDetection = cpuOveruseDetection; |
+ |
+ this.localConnection = null; |
+ this.remoteConnection = null; |
+ |
+ this.remoteView = addNewVideoElement(); |
+ |
+ this.start = function() { |
+ var onGetUserMediaSuccess = this.onGetUserMediaSuccess.bind(this); |
+ navigator.mediaDevices.getUserMedia({ |
+ audio: true, |
+ video: true |
+ }) |
+ .then(onGetUserMediaSuccess) |
+ .catch(logError); |
+ }; |
+ |
+ this.onGetUserMediaSuccess = function(stream) { |
+ // Create local peer connection. |
+ this.localConnection = new RTCPeerConnection(null, { |
+ 'optional': [{ |
+ 'googCpuOveruseDetection': this.cpuOveruseDetection |
+ }] |
+ }); |
+ this.localConnection.onicecandidate = (event) => { |
+ this.onIceCandidate(this.remoteConnection, event); |
+ }; |
+ this.localConnection.addStream(stream); |
+ |
+ // Create remote peer connection. |
+ this.remoteConnection = new RTCPeerConnection(null, { |
+ 'optional': [{ |
+ 'googCpuOveruseDetection': this.cpuOveruseDetection |
+ }] |
+ }); |
+ this.remoteConnection.onicecandidate = (event) => { |
+ this.onIceCandidate(this.localConnection, event); |
+ }; |
+ this.remoteConnection.onaddstream = (e) => { |
+ this.remoteView.srcObject = e.stream; |
+ }; |
+ |
+ // Initiate call. |
+ var onCreateOfferSuccess = this.onCreateOfferSuccess.bind(this); |
+ this.localConnection.createOffer({ |
+ offerToReceiveAudio: 1, |
+ offerToReceiveVideo: 1 |
+ }) |
+ .then(onCreateOfferSuccess, logError); |
+ }; |
+ |
+ this.onCreateOfferSuccess = function(desc) { |
+ this.localConnection.setLocalDescription(desc); |
+ this.remoteConnection.setRemoteDescription(desc); |
+ |
+ var onCreateAnswerSuccess = this.onCreateAnswerSuccess.bind(this); |
+ this.remoteConnection.createAnswer() |
+ .then(onCreateAnswerSuccess, logError); |
+ }; |
+ |
+ this.onCreateAnswerSuccess = function(desc) { |
+ this.remoteConnection.setLocalDescription(desc); |
+ this.localConnection.setRemoteDescription(desc); |
+ }; |
+ |
+ this.onIceCandidate = function(connection, event) { |
+ if (event.candidate) { |
+ connection.addIceCandidate(new RTCIceCandidate(event.candidate)); |
+ } |
+ }; |
+} |
+ |
+function startTest() { |
+ var cpuOveruseDetection = cpuOveruseDetectionCheckbox.checked; |
+ var nPeerConnections = nPeerConnectionsInput.value; |
+ for (var i = 0; i < nPeerConnections; ++i) { |
+ new PeerConnection(i, cpuOveruseDetection).start(); |
+ } |
+} |