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 'use strict'; |
| 7 |
| 8 var getMediaButton = document.querySelector('button#getMedia'); |
| 9 var connectButton = document.querySelector('button#connect'); |
| 10 var hangupButton = document.querySelector('button#hangup'); |
| 11 |
| 12 getMediaButton.onclick = getMedia; |
| 13 connectButton.onclick = createPeerConnection; |
| 14 hangupButton.onclick = hangup; |
| 15 |
| 16 var minWidthInput = document.querySelector('div#minWidth input'); |
| 17 var maxWidthInput = document.querySelector('div#maxWidth input'); |
| 18 var minHeightInput = document.querySelector('div#minHeight input'); |
| 19 var maxHeightInput = document.querySelector('div#maxHeight input'); |
| 20 var minFramerateInput = document.querySelector('div#minFramerate input'); |
| 21 var maxFramerateInput = document.querySelector('div#maxFramerate input'); |
| 22 |
| 23 minWidthInput.onchange = maxWidthInput.onchange = |
| 24 minHeightInput.onchange = maxHeightInput.onchange = |
| 25 minFramerateInput.onchange = maxFramerateInput.onchange = displayRangeValue; |
| 26 |
| 27 var getUserMediaConstraintsDiv = |
| 28 document.querySelector('div#getUserMediaConstraints'); |
| 29 var bitrateDiv = document.querySelector('div#bitrate'); |
| 30 var peerDiv = document.querySelector('div#peer'); |
| 31 var senderStatsDiv = document.querySelector('div#senderStats'); |
| 32 var receiverStatsDiv = document.querySelector('div#receiverStats'); |
| 33 |
| 34 var localVideo = document.querySelector('div#localVideo video'); |
| 35 var remoteVideo = document.querySelector('div#remoteVideo video'); |
| 36 var localVideoStatsDiv = document.querySelector('div#localVideo div'); |
| 37 var remoteVideoStatsDiv = document.querySelector('div#remoteVideo div'); |
| 38 |
| 39 var localPeerConnection; |
| 40 var remotePeerConnection; |
| 41 var localStream; |
| 42 var bytesPrev; |
| 43 var timestampPrev; |
| 44 |
| 45 main(); |
| 46 |
| 47 function main() { |
| 48 displayGetUserMediaConstraints(); |
| 49 } |
| 50 |
| 51 function hangup() { |
| 52 trace('Ending call'); |
| 53 localPeerConnection.close(); |
| 54 remotePeerConnection.close(); |
| 55 localPeerConnection = null; |
| 56 remotePeerConnection = null; |
| 57 |
| 58 localStream.getTracks().forEach(function(track) { |
| 59 track.stop(); |
| 60 }); |
| 61 localStream = null; |
| 62 |
| 63 hangupButton.disabled = true; |
| 64 getMediaButton.disabled = false; |
| 65 } |
| 66 |
| 67 function getMedia() { |
| 68 getMediaButton.disabled = true; |
| 69 if (localStream) { |
| 70 localStream.getTracks().forEach(function(track) { |
| 71 track.stop(); |
| 72 }); |
| 73 var videoTracks = localStream.getVideoTracks(); |
| 74 for (var i = 0; i !== videoTracks.length; ++i) { |
| 75 videoTracks[i].stop(); |
| 76 } |
| 77 } |
| 78 navigator.mediaDevices.getUserMedia(getUserMediaConstraints()) |
| 79 .then(gotStream) |
| 80 .catch(function(e) { |
| 81 var message = 'getUserMedia error: ' + e.name + '\n' + |
| 82 'PermissionDeniedError may mean invalid constraints.'; |
| 83 alert(message); |
| 84 console.log(message); |
| 85 getMediaButton.disabled = false; |
| 86 }); |
| 87 } |
| 88 |
| 89 function gotStream(stream) { |
| 90 connectButton.disabled = false; |
| 91 console.log('GetUserMedia succeeded'); |
| 92 localStream = stream; |
| 93 localVideo.srcObject = stream; |
| 94 } |
| 95 |
| 96 function getUserMediaConstraints() { |
| 97 var constraints = {}; |
| 98 constraints.audio = true; |
| 99 constraints.video = {}; |
| 100 if (minWidthInput.value !== '0') { |
| 101 constraints.video.width = {}; |
| 102 constraints.video.width.min = minWidthInput.value; |
| 103 } |
| 104 if (maxWidthInput.value !== '0') { |
| 105 constraints.video.width = constraints.video.width || {}; |
| 106 constraints.video.width.max = maxWidthInput.value; |
| 107 } |
| 108 if (minHeightInput.value !== '0') { |
| 109 constraints.video.height = {}; |
| 110 constraints.video.height.min = minHeightInput.value; |
| 111 } |
| 112 if (maxHeightInput.value !== '0') { |
| 113 constraints.video.height = constraints.video.height || {}; |
| 114 constraints.video.height.max = maxHeightInput.value; |
| 115 } |
| 116 if (minFramerateInput.value !== '0') { |
| 117 constraints.video.frameRate = {}; |
| 118 constraints.video.frameRate.min = minFramerateInput.value; |
| 119 } |
| 120 if (maxFramerateInput.value !== '0') { |
| 121 constraints.video.frameRate = constraints.video.frameRate || {}; |
| 122 constraints.video.frameRate.max = maxFramerateInput.value; |
| 123 } |
| 124 |
| 125 return constraints; |
| 126 } |
| 127 |
| 128 function displayGetUserMediaConstraints() { |
| 129 var constraints = getUserMediaConstraints(); |
| 130 console.log('getUserMedia constraints', constraints); |
| 131 getUserMediaConstraintsDiv.textContent = |
| 132 JSON.stringify(constraints, null, ' '); |
| 133 } |
| 134 |
| 135 function createPeerConnection() { |
| 136 connectButton.disabled = true; |
| 137 hangupButton.disabled = false; |
| 138 |
| 139 bytesPrev = 0; |
| 140 timestampPrev = 0; |
| 141 localPeerConnection = new RTCPeerConnection(null); |
| 142 remotePeerConnection = new RTCPeerConnection(null); |
| 143 localPeerConnection.addStream(localStream); |
| 144 console.log('localPeerConnection creating offer'); |
| 145 localPeerConnection.onnegotiationeeded = function() { |
| 146 console.log('Negotiation needed - localPeerConnection'); |
| 147 }; |
| 148 remotePeerConnection.onnegotiationeeded = function() { |
| 149 console.log('Negotiation needed - remotePeerConnection'); |
| 150 }; |
| 151 localPeerConnection.onicecandidate = function(e) { |
| 152 console.log('Candidate localPeerConnection'); |
| 153 if (e.candidate) { |
| 154 remotePeerConnection.addIceCandidate(e.candidate) |
| 155 .then( |
| 156 onAddIceCandidateSuccess, |
| 157 onAddIceCandidateError |
| 158 ); |
| 159 } |
| 160 }; |
| 161 remotePeerConnection.onicecandidate = function(e) { |
| 162 console.log('Candidate remotePeerConnection'); |
| 163 if (e.candidate) { |
| 164 localPeerConnection.addIceCandidate(e.candidate) |
| 165 .then( |
| 166 onAddIceCandidateSuccess, |
| 167 onAddIceCandidateError |
| 168 ); |
| 169 } |
| 170 }; |
| 171 remotePeerConnection.onaddstream = function(e) { |
| 172 console.log('remotePeerConnection got stream'); |
| 173 remoteVideo.srcObject = e.stream; |
| 174 }; |
| 175 localPeerConnection.createOffer().then( |
| 176 function(desc) { |
| 177 console.log('localPeerConnection offering'); |
| 178 localPeerConnection.setLocalDescription(desc); |
| 179 remotePeerConnection.setRemoteDescription(desc); |
| 180 remotePeerConnection.createAnswer().then( |
| 181 function(desc2) { |
| 182 console.log('remotePeerConnection answering'); |
| 183 remotePeerConnection.setLocalDescription(desc2); |
| 184 localPeerConnection.setRemoteDescription(desc2); |
| 185 }, |
| 186 function(err) { |
| 187 console.log(err); |
| 188 } |
| 189 ); |
| 190 }, |
| 191 function(err) { |
| 192 console.log(err); |
| 193 } |
| 194 ); |
| 195 } |
| 196 |
| 197 function onAddIceCandidateSuccess() { |
| 198 trace('AddIceCandidate success.'); |
| 199 } |
| 200 |
| 201 function onAddIceCandidateError(error) { |
| 202 trace('Failed to add Ice Candidate: ' + error.toString()); |
| 203 } |
| 204 |
| 205 // Display statistics |
| 206 setInterval(function() { |
| 207 if (remotePeerConnection && remotePeerConnection.getRemoteStreams()[0]) { |
| 208 remotePeerConnection.getStats(null) |
| 209 .then(function(results) { |
| 210 var statsString = dumpStats(results); |
| 211 receiverStatsDiv.innerHTML = '<h2>Receiver stats</h2>' + statsString; |
| 212 // calculate video bitrate |
| 213 results.forEach(function(report) { |
| 214 var now = report.timestamp; |
| 215 |
| 216 var bitrate; |
| 217 if (report.type === 'inboundrtp' && report.mediaType === 'video') { |
| 218 // firefox calculates the bitrate for us |
| 219 // https://bugzilla.mozilla.org/show_bug.cgi?id=951496 |
| 220 bitrate = Math.floor(report.bitrateMean / 1024); |
| 221 } else if (report.type === 'ssrc' && report.bytesReceived && |
| 222 report.googFrameHeightReceived) { |
| 223 // chrome does not so we need to do it ourselves |
| 224 var bytes = report.bytesReceived; |
| 225 if (timestampPrev) { |
| 226 bitrate = 8 * (bytes - bytesPrev) / (now - timestampPrev); |
| 227 bitrate = Math.floor(bitrate); |
| 228 } |
| 229 bytesPrev = bytes; |
| 230 timestampPrev = now; |
| 231 } |
| 232 if (bitrate) { |
| 233 bitrate += ' kbits/sec'; |
| 234 bitrateDiv.innerHTML = '<strong>Bitrate:</strong> ' + bitrate; |
| 235 } |
| 236 }); |
| 237 |
| 238 // figure out the peer's ip |
| 239 var activeCandidatePair = null; |
| 240 var remoteCandidate = null; |
| 241 |
| 242 // search for the candidate pair |
| 243 results.forEach(function(report) { |
| 244 if (report.type === 'candidatepair' && report.selected || |
| 245 report.type === 'googCandidatePair' && |
| 246 report.googActiveConnection === 'true') { |
| 247 activeCandidatePair = report; |
| 248 } |
| 249 }); |
| 250 if (activeCandidatePair && activeCandidatePair.remoteCandidateId) { |
| 251 remoteCandidate = results[activeCandidatePair.remoteCandidateId]; |
| 252 } |
| 253 if (remoteCandidate && remoteCandidate.ipAddress && |
| 254 remoteCandidate.portNumber) { |
| 255 peerDiv.innerHTML = '<strong>Connected to:</strong> ' + |
| 256 remoteCandidate.ipAddress + |
| 257 ':' + remoteCandidate.portNumber; |
| 258 } |
| 259 }, function(err) { |
| 260 console.log(err); |
| 261 }); |
| 262 localPeerConnection.getStats(null) |
| 263 .then(function(results) { |
| 264 var statsString = dumpStats(results); |
| 265 senderStatsDiv.innerHTML = '<h2>Sender stats</h2>' + statsString; |
| 266 }, function(err) { |
| 267 console.log(err); |
| 268 }); |
| 269 } else { |
| 270 console.log('Not connected yet'); |
| 271 } |
| 272 // Collect some stats from the video tags. |
| 273 if (localVideo.videoWidth) { |
| 274 localVideoStatsDiv.innerHTML = '<strong>Video dimensions:</strong> ' + |
| 275 localVideo.videoWidth + 'x' + localVideo.videoHeight + 'px'; |
| 276 } |
| 277 if (remoteVideo.videoWidth) { |
| 278 remoteVideoStatsDiv.innerHTML = '<strong>Video dimensions:</strong> ' + |
| 279 remoteVideo.videoWidth + 'x' + remoteVideo.videoHeight + 'px'; |
| 280 } |
| 281 }, 1000); |
| 282 |
| 283 // Dumping a stats variable as a string. |
| 284 // might be named toString? |
| 285 function dumpStats(results) { |
| 286 var statsString = ''; |
| 287 results.forEach(function(res) { |
| 288 statsString += '<h3>Report type='; |
| 289 statsString += res.type; |
| 290 statsString += '</h3>\n'; |
| 291 statsString += 'id ' + res.id + '<br>\n'; |
| 292 statsString += 'time ' + res.timestamp + '<br>\n'; |
| 293 Object.keys(res).forEach(function(k) { |
| 294 if (k !== 'timestamp' && k !== 'type' && k !== 'id') { |
| 295 statsString += k + ': ' + res[k] + '<br>\n'; |
| 296 } |
| 297 }); |
| 298 }); |
| 299 return statsString; |
| 300 } |
| 301 |
| 302 // Utility to show the value of a range in a sibling span element |
| 303 function displayRangeValue(e) { |
| 304 var span = e.target.parentElement.querySelector('span'); |
| 305 span.textContent = e.target.value; |
| 306 displayGetUserMediaConstraints(); |
| 307 } |
OLD | NEW |