Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /** | 1 /** |
| 2 * Copyright 2014 The Chromium Authors. All rights reserved. | 2 * Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * The one and only peer connection in this page. | 8 * The one and only peer connection in this page. |
| 9 * @private | 9 * @private |
| 10 */ | 10 */ |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 gPeerConnection = createPeerConnection_(); | 37 gPeerConnection = createPeerConnection_(); |
| 38 returnToTest('ok-peerconnection-created'); | 38 returnToTest('ok-peerconnection-created'); |
| 39 } | 39 } |
| 40 | 40 |
| 41 /** | 41 /** |
| 42 * Asks this page to create a local offer. | 42 * Asks this page to create a local offer. |
| 43 * | 43 * |
| 44 * Returns a string on the format ok-(JSON encoded session description). | 44 * Returns a string on the format ok-(JSON encoded session description). |
| 45 * | 45 * |
| 46 * @param {!Object} constraints Any createOffer constraints. | 46 * @param {!Object} constraints Any createOffer constraints. |
| 47 * @param {string} videoCodec If not null, promotes the specified codec to be | |
| 48 * the default video codec, e.g. the first one in the list on the 'm=video' | |
| 49 * SDP offer line. |videoCodec| is the case-sensitive codec name, e.g. | |
| 50 * 'VP8' or 'H264'. | |
| 47 */ | 51 */ |
| 48 function createLocalOffer(constraints) { | 52 function createLocalOffer(constraints, videoCodec = null) { |
| 49 peerConnection_().createOffer( | 53 peerConnection_().createOffer( |
| 50 function(localOffer) { | 54 function(localOffer) { |
| 51 success_('createOffer'); | 55 success('createOffer'); |
| 56 | |
| 52 setLocalDescription(peerConnection, localOffer); | 57 setLocalDescription(peerConnection, localOffer); |
| 58 if (videoCodec !== null) | |
| 59 localOffer.sdp = setSdpDefaultVideoCodec(localOffer.sdp, videoCodec); | |
| 53 | 60 |
| 54 returnToTest('ok-' + JSON.stringify(localOffer)); | 61 returnToTest('ok-' + JSON.stringify(localOffer)); |
| 55 }, | 62 }, |
| 56 function(error) { failure_('createOffer', error); }, | 63 function(error) { failure('createOffer', error); }, |
| 57 constraints); | 64 constraints); |
| 58 } | 65 } |
| 59 | 66 |
| 60 /** | 67 /** |
| 61 * Asks this page to accept an offer and generate an answer. | 68 * Asks this page to accept an offer and generate an answer. |
| 62 * | 69 * |
| 63 * Returns a string on the format ok-(JSON encoded session description). | 70 * Returns a string on the format ok-(JSON encoded session description). |
| 64 * | 71 * |
| 65 * @param {!string} sessionDescJson A JSON-encoded session description of type | 72 * @param {!string} sessionDescJson A JSON-encoded session description of type |
| 66 * 'offer'. | 73 * 'offer'. |
| 67 * @param {!Object} constraints Any createAnswer constraints. | 74 * @param {!Object} constraints Any createAnswer constraints. |
| 68 */ | 75 */ |
| 69 function receiveOfferFromPeer(sessionDescJson, constraints) { | 76 function receiveOfferFromPeer(sessionDescJson, constraints) { |
| 70 offer = parseJson_(sessionDescJson); | 77 offer = parseJson_(sessionDescJson); |
| 71 if (!offer.type) | 78 if (!offer.type) |
| 72 failTest('Got invalid session description from peer: ' + sessionDescJson); | 79 failTest('Got invalid session description from peer: ' + sessionDescJson); |
| 73 if (offer.type != 'offer') | 80 if (offer.type != 'offer') |
| 74 failTest('Expected to receive offer from peer, got ' + offer.type); | 81 failTest('Expected to receive offer from peer, got ' + offer.type); |
| 75 | 82 |
| 76 var sessionDescription = new RTCSessionDescription(offer); | 83 var sessionDescription = new RTCSessionDescription(offer); |
| 77 peerConnection_().setRemoteDescription( | 84 peerConnection_().setRemoteDescription( |
| 78 sessionDescription, | 85 sessionDescription, |
| 79 function() { success_('setRemoteDescription'); }, | 86 function() { success('setRemoteDescription'); }, |
| 80 function(error) { failure_('setRemoteDescription', error); }); | 87 function(error) { failure('setRemoteDescription', error); }); |
| 81 | 88 |
| 82 peerConnection_().createAnswer( | 89 peerConnection_().createAnswer( |
| 83 function(answer) { | 90 function(answer) { |
| 84 success_('createAnswer'); | 91 success('createAnswer'); |
| 85 setLocalDescription(peerConnection, answer); | 92 setLocalDescription(peerConnection, answer); |
| 86 returnToTest('ok-' + JSON.stringify(answer)); | 93 returnToTest('ok-' + JSON.stringify(answer)); |
| 87 }, | 94 }, |
| 88 function(error) { failure_('createAnswer', error); }, | 95 function(error) { failure('createAnswer', error); }, |
| 89 constraints); | 96 constraints); |
| 90 } | 97 } |
| 91 | 98 |
| 92 /** | 99 /** |
| 100 * Verifies that the specified codec is the default video codec, e.g. the first | |
| 101 * one in the list on the 'm=video' SDP answer line. If this is not the case, | |
| 102 * |failure_| occurs. | |
|
phoglund_chromium
2016/02/02 14:45:19
Nit: blank line before @param
phoglund_chromium
2016/02/02 14:45:19
Nit: |failure|
| |
| 103 * @param {!string} sessionDescJson A JSON-encoded session description. | |
| 104 * @param {!string} expectedVideoCodec The case-sensitive codec name, e.g. | |
| 105 * 'VP8' or 'H264'. | |
| 106 */ | |
| 107 function verifyDefaultVideoCodec(sessionDescJson, expectedVideoCodec) { | |
| 108 var sessionDesc = parseJson_(sessionDescJson); | |
| 109 if (!sessionDesc.type) { | |
| 110 failure('verifyDefaultVideoCodec', | |
| 111 'Invalid session description: ' + sessionDescJson); | |
| 112 } | |
| 113 var defaultVideoCodec = getSdpDefaultVideoCodec(sessionDesc.sdp); | |
| 114 if (defaultVideoCodec === null) { | |
| 115 failure('verifyDefaultVideoCodec', | |
| 116 'Could not determine default video codec.'); | |
| 117 } | |
| 118 if (expectedVideoCodec !== defaultVideoCodec) { | |
| 119 failure('verifyDefaultVideoCodec', | |
| 120 'Expected default video codec ' + expectedVideoCodec + | |
| 121 ', got ' + defaultVideoCodec + '.'); | |
| 122 } | |
| 123 returnToTest('ok-verified'); | |
|
hbos_chromium
2016/02/02 14:07:19
(Lol I wasted so much time debugging before I real
phoglund_chromium
2016/02/02 14:45:19
Yeah, the test just hangs in c++ land. I know, it'
| |
| 124 } | |
| 125 | |
| 126 /** | |
| 93 * Asks this page to accept an answer generated by the peer in response to a | 127 * Asks this page to accept an answer generated by the peer in response to a |
| 94 * previous offer by this page | 128 * previous offer by this page |
| 95 * | 129 * |
| 96 * Returns a string ok-accepted-answer on success. | 130 * Returns a string ok-accepted-answer on success. |
| 97 * | 131 * |
| 98 * @param {!string} sessionDescJson A JSON-encoded session description of type | 132 * @param {!string} sessionDescJson A JSON-encoded session description of type |
| 99 * 'answer'. | 133 * 'answer'. |
| 100 */ | 134 */ |
| 101 function receiveAnswerFromPeer(sessionDescJson) { | 135 function receiveAnswerFromPeer(sessionDescJson) { |
| 102 answer = parseJson_(sessionDescJson); | 136 answer = parseJson_(sessionDescJson); |
| 103 if (!answer.type) | 137 if (!answer.type) |
| 104 failTest('Got invalid session description from peer: ' + sessionDescJson); | 138 failTest('Got invalid session description from peer: ' + sessionDescJson); |
| 105 if (answer.type != 'answer') | 139 if (answer.type != 'answer') |
| 106 failTest('Expected to receive answer from peer, got ' + answer.type); | 140 failTest('Expected to receive answer from peer, got ' + answer.type); |
| 107 | 141 |
| 108 var sessionDescription = new RTCSessionDescription(answer); | 142 var sessionDescription = new RTCSessionDescription(answer); |
| 109 peerConnection_().setRemoteDescription( | 143 peerConnection_().setRemoteDescription( |
| 110 sessionDescription, | 144 sessionDescription, |
| 111 function() { | 145 function() { |
| 112 success_('setRemoteDescription'); | 146 success('setRemoteDescription'); |
| 113 returnToTest('ok-accepted-answer'); | 147 returnToTest('ok-accepted-answer'); |
| 114 }, | 148 }, |
| 115 function(error) { failure_('setRemoteDescription', error); }); | 149 function(error) { failure('setRemoteDescription', error); }); |
| 116 } | 150 } |
| 117 | 151 |
| 118 /** | 152 /** |
| 119 * Adds the local stream to the peer connection. You will have to re-negotiate | 153 * Adds the local stream to the peer connection. You will have to re-negotiate |
| 120 * the call for this to take effect in the call. | 154 * the call for this to take effect in the call. |
| 121 */ | 155 */ |
| 122 function addLocalStream() { | 156 function addLocalStream() { |
| 123 addLocalStreamToPeerConnection(peerConnection_()); | 157 addLocalStreamToPeerConnection(peerConnection_()); |
| 124 returnToTest('ok-added'); | 158 returnToTest('ok-added'); |
| 125 } | 159 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 if (!iceCandidates.length) | 221 if (!iceCandidates.length) |
| 188 throw failTest('Received invalid ICE candidate list from peer: ' + | 222 throw failTest('Received invalid ICE candidate list from peer: ' + |
| 189 iceCandidatesJson); | 223 iceCandidatesJson); |
| 190 | 224 |
| 191 iceCandidates.forEach(function(iceCandidate) { | 225 iceCandidates.forEach(function(iceCandidate) { |
| 192 if (!iceCandidate.candidate) | 226 if (!iceCandidate.candidate) |
| 193 failTest('Received invalid ICE candidate from peer: ' + | 227 failTest('Received invalid ICE candidate from peer: ' + |
| 194 iceCandidatesJson); | 228 iceCandidatesJson); |
| 195 | 229 |
| 196 peerConnection_().addIceCandidate(new RTCIceCandidate(iceCandidate, | 230 peerConnection_().addIceCandidate(new RTCIceCandidate(iceCandidate, |
| 197 function() { success_('addIceCandidate'); }, | 231 function() { success('addIceCandidate'); }, |
| 198 function(error) { failure_('addIceCandidate', error); } | 232 function(error) { failure('addIceCandidate', error); } |
| 199 )); | 233 )); |
| 200 }); | 234 }); |
| 201 | 235 |
| 202 returnToTest('ok-received-candidates'); | 236 returnToTest('ok-received-candidates'); |
| 203 } | 237 } |
| 204 | 238 |
| 205 /** | 239 /** |
| 206 * Sets the mute state of the selected media element. | 240 * Sets the mute state of the selected media element. |
| 207 * | 241 * |
| 208 * Returns ok-muted on success. | 242 * Returns ok-muted on success. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 241 } | 275 } |
| 242 | 276 |
| 243 /** @private */ | 277 /** @private */ |
| 244 function peerConnection_() { | 278 function peerConnection_() { |
| 245 if (gPeerConnection == null) | 279 if (gPeerConnection == null) |
| 246 throw failTest('Trying to use peer connection, but none was created.'); | 280 throw failTest('Trying to use peer connection, but none was created.'); |
| 247 return gPeerConnection; | 281 return gPeerConnection; |
| 248 } | 282 } |
| 249 | 283 |
| 250 /** @private */ | 284 /** @private */ |
| 251 function success_(method) { | |
| 252 debug(method + '(): success.'); | |
| 253 } | |
| 254 | |
| 255 /** @private */ | |
| 256 function failure_(method, error) { | |
| 257 throw failTest(method + '() failed: ' + JSON.stringify(error)); | |
| 258 } | |
| 259 | |
| 260 /** @private */ | |
| 261 function iceCallback_(event) { | 285 function iceCallback_(event) { |
| 262 if (event.candidate) | 286 if (event.candidate) |
| 263 gIceCandidates.push(event.candidate); | 287 gIceCandidates.push(event.candidate); |
| 264 } | 288 } |
| 265 | 289 |
| 266 /** @private */ | 290 /** @private */ |
| 267 function setLocalDescription(peerConnection, sessionDescription) { | 291 function setLocalDescription(peerConnection, sessionDescription) { |
| 268 if (sessionDescription.sdp.search('a=crypto') != -1 || | 292 if (sessionDescription.sdp.search('a=crypto') != -1 || |
| 269 sessionDescription.sdp.search('a=fingerprint') != -1) | 293 sessionDescription.sdp.search('a=fingerprint') != -1) |
| 270 gHasSeenCryptoInSdp = 'crypto-seen'; | 294 gHasSeenCryptoInSdp = 'crypto-seen'; |
| 271 | 295 |
| 272 peerConnection.setLocalDescription( | 296 peerConnection.setLocalDescription( |
| 273 sessionDescription, | 297 sessionDescription, |
| 274 function() { success_('setLocalDescription'); }, | 298 function() { success('setLocalDescription'); }, |
| 275 function(error) { failure_('setLocalDescription', error); }); | 299 function(error) { failure('setLocalDescription', error); }); |
| 276 } | 300 } |
| 277 | 301 |
| 278 /** @private */ | 302 /** @private */ |
| 279 function addStreamCallback_(event) { | 303 function addStreamCallback_(event) { |
| 280 debug('Receiving remote stream...'); | 304 debug('Receiving remote stream...'); |
| 281 var videoTag = document.getElementById('remote-view'); | 305 var videoTag = document.getElementById('remote-view'); |
| 282 attachMediaStream(videoTag, event.stream); | 306 attachMediaStream(videoTag, event.stream); |
| 283 } | 307 } |
| 284 | 308 |
| 285 /** @private */ | 309 /** @private */ |
| 286 function removeStreamCallback_(event) { | 310 function removeStreamCallback_(event) { |
| 287 debug('Call ended.'); | 311 debug('Call ended.'); |
| 288 document.getElementById('remote-view').src = ''; | 312 document.getElementById('remote-view').src = ''; |
| 289 } | 313 } |
| 290 | 314 |
| 291 /** | 315 /** |
| 292 * Parses JSON-encoded session descriptions and ICE candidates. | 316 * Parses JSON-encoded session descriptions and ICE candidates. |
| 293 * @private | 317 * @private |
| 294 */ | 318 */ |
| 295 function parseJson_(json) { | 319 function parseJson_(json) { |
| 296 // Escape since the \r\n in the SDP tend to get unescaped. | 320 // Escape since the \r\n in the SDP tend to get unescaped. |
| 297 jsonWithEscapedLineBreaks = json.replace(/\r\n/g, '\\r\\n'); | 321 jsonWithEscapedLineBreaks = json.replace(/\r\n/g, '\\r\\n'); |
| 298 try { | 322 try { |
| 299 return JSON.parse(jsonWithEscapedLineBreaks); | 323 return JSON.parse(jsonWithEscapedLineBreaks); |
| 300 } catch (exception) { | 324 } catch (exception) { |
| 301 failTest('Failed to parse JSON: ' + jsonWithEscapedLineBreaks + ', got ' + | 325 failTest('Failed to parse JSON: ' + jsonWithEscapedLineBreaks + ', got ' + |
| 302 exception); | 326 exception); |
| 303 } | 327 } |
| 304 } | 328 } |
| OLD | NEW |