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 * We need a STUN server for some API calls. | 8 * We need a STUN server for some API calls. |
9 * @private | 9 * @private |
10 */ | 10 */ |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 * @param {!Object} constraints Any createOffer constraints. | 52 * @param {!Object} constraints Any createOffer constraints. |
53 */ | 53 */ |
54 function createLocalOffer(constraints) { | 54 function createLocalOffer(constraints) { |
55 peerConnection_().createOffer( | 55 peerConnection_().createOffer( |
56 function(localOffer) { | 56 function(localOffer) { |
57 success_('createOffer'); | 57 success_('createOffer'); |
58 setLocalDescription(peerConnection, localOffer); | 58 setLocalDescription(peerConnection, localOffer); |
59 | 59 |
60 returnToTest('ok-' + JSON.stringify(localOffer)); | 60 returnToTest('ok-' + JSON.stringify(localOffer)); |
61 }, | 61 }, |
62 function() { failure_('createOffer'); }, | 62 function(error) { failure_('createOffer', error); }, |
63 constraints); | 63 constraints); |
64 } | 64 } |
65 | 65 |
66 /** | 66 /** |
67 * Asks this page to accept an offer and generate an answer. | 67 * Asks this page to accept an offer and generate an answer. |
68 * | 68 * |
69 * Returns a string on the format ok-(JSON encoded session description). | 69 * Returns a string on the format ok-(JSON encoded session description). |
70 * | 70 * |
71 * @param {!string} sessionDescJson A JSON-encoded session description of type | 71 * @param {!string} sessionDescJson A JSON-encoded session description of type |
72 * 'offer'. | 72 * 'offer'. |
73 * @param {!Object} constraints Any createAnswer constraints. | 73 * @param {!Object} constraints Any createAnswer constraints. |
74 */ | 74 */ |
75 function receiveOfferFromPeer(sessionDescJson, constraints) { | 75 function receiveOfferFromPeer(sessionDescJson, constraints) { |
76 offer = parseJson_(sessionDescJson); | 76 offer = parseJson_(sessionDescJson); |
77 if (!offer.type) | 77 if (!offer.type) |
78 failTest('Got invalid session description from peer: ' + sessionDescJson); | 78 failTest('Got invalid session description from peer: ' + sessionDescJson); |
79 if (offer.type != 'offer') | 79 if (offer.type != 'offer') |
80 failTest('Expected to receive offer from peer, got ' + offer.type); | 80 failTest('Expected to receive offer from peer, got ' + offer.type); |
81 | 81 |
82 var sessionDescription = new RTCSessionDescription(offer); | 82 var sessionDescription = new RTCSessionDescription(offer); |
83 peerConnection_().setRemoteDescription( | 83 peerConnection_().setRemoteDescription( |
84 sessionDescription, | 84 sessionDescription, |
85 function() { success_('setRemoteDescription'); }, | 85 function() { success_('setRemoteDescription'); }, |
86 function() { failure_('setRemoteDescription'); }); | 86 function(error) { failure_('setRemoteDescription', error); }); |
87 | 87 |
88 peerConnection_().createAnswer( | 88 peerConnection_().createAnswer( |
89 function(answer) { | 89 function(answer) { |
90 success_('createAnswer'); | 90 success_('createAnswer'); |
91 setLocalDescription(peerConnection, answer); | 91 setLocalDescription(peerConnection, answer); |
92 returnToTest('ok-' + JSON.stringify(answer)); | 92 returnToTest('ok-' + JSON.stringify(answer)); |
93 }, | 93 }, |
94 function() { failure_('createAnswer'); }, | 94 function(error) { failure_('createAnswer', error); }, |
95 constraints); | 95 constraints); |
96 } | 96 } |
97 | 97 |
98 /** | 98 /** |
99 * Asks this page to accept an answer generated by the peer in response to a | 99 * Asks this page to accept an answer generated by the peer in response to a |
100 * previous offer by this page | 100 * previous offer by this page |
101 * | 101 * |
102 * Returns a string ok-accepted-answer on success. | 102 * Returns a string ok-accepted-answer on success. |
103 * | 103 * |
104 * @param {!string} sessionDescJson A JSON-encoded session description of type | 104 * @param {!string} sessionDescJson A JSON-encoded session description of type |
105 * 'answer'. | 105 * 'answer'. |
106 */ | 106 */ |
107 function receiveAnswerFromPeer(sessionDescJson) { | 107 function receiveAnswerFromPeer(sessionDescJson) { |
108 answer = parseJson_(sessionDescJson); | 108 answer = parseJson_(sessionDescJson); |
109 if (!answer.type) | 109 if (!answer.type) |
110 failTest('Got invalid session description from peer: ' + sessionDescJson); | 110 failTest('Got invalid session description from peer: ' + sessionDescJson); |
111 if (answer.type != 'answer') | 111 if (answer.type != 'answer') |
112 failTest('Expected to receive answer from peer, got ' + answer.type); | 112 failTest('Expected to receive answer from peer, got ' + answer.type); |
113 | 113 |
114 var sessionDescription = new RTCSessionDescription(answer); | 114 var sessionDescription = new RTCSessionDescription(answer); |
115 peerConnection_().setRemoteDescription( | 115 peerConnection_().setRemoteDescription( |
116 sessionDescription, | 116 sessionDescription, |
117 function() { | 117 function() { |
118 success_('setRemoteDescription'); | 118 success_('setRemoteDescription'); |
119 returnToTest('ok-accepted-answer'); | 119 returnToTest('ok-accepted-answer'); |
120 }, | 120 }, |
121 function() { failure_('setRemoteDescription'); }); | 121 function(error) { failure_('setRemoteDescription', error); }); |
122 } | 122 } |
123 | 123 |
124 /** | 124 /** |
125 * Adds the local stream to the peer connection. You will have to re-negotiate | 125 * Adds the local stream to the peer connection. You will have to re-negotiate |
126 * the call for this to take effect in the call. | 126 * the call for this to take effect in the call. |
127 */ | 127 */ |
128 function addLocalStream() { | 128 function addLocalStream() { |
129 addLocalStreamToPeerConnection(peerConnection_()); | 129 addLocalStreamToPeerConnection(peerConnection_()); |
130 returnToTest('ok-added'); | 130 returnToTest('ok-added'); |
131 } | 131 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 * Receives ICE candidates from the peer. | 203 * Receives ICE candidates from the peer. |
204 * | 204 * |
205 * Returns ok-received-candidates to the test on success. | 205 * Returns ok-received-candidates to the test on success. |
206 * | 206 * |
207 * @param iceCandidatesJson a JSON-encoded array of RTCIceCandidate instances. | 207 * @param iceCandidatesJson a JSON-encoded array of RTCIceCandidate instances. |
208 */ | 208 */ |
209 function receiveIceCandidates(iceCandidatesJson) { | 209 function receiveIceCandidates(iceCandidatesJson) { |
210 var iceCandidates = parseJson_(iceCandidatesJson); | 210 var iceCandidates = parseJson_(iceCandidatesJson); |
211 if (!iceCandidates.length) | 211 if (!iceCandidates.length) |
212 throw failTest('Received invalid ICE candidate list from peer: ' + | 212 throw failTest('Received invalid ICE candidate list from peer: ' + |
213 iceCandidatesJson); | 213 iceCandidatesJson); |
214 | 214 |
215 iceCandidates.forEach(function(iceCandidate) { | 215 iceCandidates.forEach(function(iceCandidate) { |
216 if (!iceCandidate.candidate) | 216 if (!iceCandidate.candidate) |
217 failTest('Received invalid ICE candidate from peer: ' + | 217 failTest('Received invalid ICE candidate from peer: ' + |
218 iceCandidatesJson); | 218 iceCandidatesJson); |
219 | 219 |
220 peerConnection_().addIceCandidate(new RTCIceCandidate(iceCandidate)); | 220 peerConnection_().addIceCandidate(new RTCIceCandidate(iceCandidate, |
221 }); | 221 function() { success_('addIceCandidate success'); }, |
| 222 function(error) { failure_('addIceCandidate failed', error); })); |
| 223 } |
222 | 224 |
223 returnToTest('ok-received-candidates'); | 225 returnToTest('ok-received-candidates'); |
224 } | 226 } |
225 | 227 |
226 /** | 228 /** |
227 * Returns | 229 * Returns |
228 */ | 230 */ |
229 function hasSeenCryptoInSdp() { | 231 function hasSeenCryptoInSdp() { |
230 returnToTest(gHasSeenCryptoInSdp); | 232 returnToTest(gHasSeenCryptoInSdp); |
231 } | 233 } |
(...skipping 21 matching lines...) Expand all Loading... |
253 return gPeerConnection; | 255 return gPeerConnection; |
254 } | 256 } |
255 | 257 |
256 /** @private */ | 258 /** @private */ |
257 function success_(method) { | 259 function success_(method) { |
258 debug(method + '(): success.'); | 260 debug(method + '(): success.'); |
259 } | 261 } |
260 | 262 |
261 /** @private */ | 263 /** @private */ |
262 function failure_(method, error) { | 264 function failure_(method, error) { |
263 throw failTest(method + '() failed: ' + error); | 265 throw failTest(method + '() failed: ' + JSON.stringify(error)); |
264 } | 266 } |
265 | 267 |
266 /** @private */ | 268 /** @private */ |
267 function iceCallback_(event) { | 269 function iceCallback_(event) { |
268 if (event.candidate) | 270 if (event.candidate) |
269 gIceCandidates.push(event.candidate); | 271 gIceCandidates.push(event.candidate); |
270 } | 272 } |
271 | 273 |
272 /** @private */ | 274 /** @private */ |
273 function setLocalDescription(peerConnection, sessionDescription) { | 275 function setLocalDescription(peerConnection, sessionDescription) { |
274 if (sessionDescription.sdp.search('a=crypto') != -1 || | 276 if (sessionDescription.sdp.search('a=crypto') != -1 || |
275 sessionDescription.sdp.search('a=fingerprint') != -1) | 277 sessionDescription.sdp.search('a=fingerprint') != -1) |
276 gHasSeenCryptoInSdp = 'crypto-seen'; | 278 gHasSeenCryptoInSdp = 'crypto-seen'; |
277 | 279 |
278 peerConnection.setLocalDescription( | 280 peerConnection.setLocalDescription( |
279 sessionDescription, | 281 sessionDescription, |
280 function() { success_('setLocalDescription'); }, | 282 function() { success_('setLocalDescription'); }, |
281 function() { failure_('setLocalDescription'); }); | 283 function(error) { failure_('setLocalDescription', error); }); |
282 } | 284 } |
283 | 285 |
284 /** @private */ | 286 /** @private */ |
285 function addStreamCallback_(event) { | 287 function addStreamCallback_(event) { |
286 debug('Receiving remote stream...'); | 288 debug('Receiving remote stream...'); |
287 var videoTag = document.getElementById('remote-view'); | 289 var videoTag = document.getElementById('remote-view'); |
288 attachMediaStream(videoTag, event.stream); | 290 attachMediaStream(videoTag, event.stream); |
289 } | 291 } |
290 | 292 |
291 /** @private */ | 293 /** @private */ |
292 function removeStreamCallback_(event) { | 294 function removeStreamCallback_(event) { |
293 debug('Call ended.'); | 295 debug('Call ended.'); |
294 document.getElementById('remote-view').src = ''; | 296 document.getElementById('remote-view').src = ''; |
295 } | 297 } |
296 | 298 |
297 /** | 299 /** |
298 * Parses JSON-encoded session descriptions and ICE candidates. | 300 * Parses JSON-encoded session descriptions and ICE candidates. |
299 * @private | 301 * @private |
300 */ | 302 */ |
301 function parseJson_(json) { | 303 function parseJson_(json) { |
302 // Escape since the \r\n in the SDP tend to get unescaped. | 304 // Escape since the \r\n in the SDP tend to get unescaped. |
303 jsonWithEscapedLineBreaks = json.replace(/\r\n/g, '\\r\\n'); | 305 jsonWithEscapedLineBreaks = json.replace(/\r\n/g, '\\r\\n'); |
304 try { | 306 try { |
305 return JSON.parse(jsonWithEscapedLineBreaks); | 307 return JSON.parse(jsonWithEscapedLineBreaks); |
306 } catch (exception) { | 308 } catch (exception) { |
307 failTest('Failed to parse JSON: ' + jsonWithEscapedLineBreaks + ', got ' + | 309 failTest('Failed to parse JSON: ' + jsonWithEscapedLineBreaks + ', got ' + |
308 exception); | 310 exception); |
309 } | 311 } |
310 } | 312 } |
OLD | NEW |