| OLD | NEW |
| 1 /** | 1 /** |
| 2 * Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 * Copyright (c) 2012 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 * See http://dev.w3.org/2011/webrtc/editor/getusermedia.html for more | 8 * See http://dev.w3.org/2011/webrtc/editor/getusermedia.html for more |
| 9 * information on getUserMedia. | 9 * information on getUserMedia. |
| 10 */ | 10 */ |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 */ | 28 */ |
| 29 var gRequestWebcamAndMicrophoneResult = 'not-called-yet'; | 29 var gRequestWebcamAndMicrophoneResult = 'not-called-yet'; |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * This function asks permission to use the webcam and mic from the browser. It | 32 * This function asks permission to use the webcam and mic from the browser. It |
| 33 * will return ok-requested to PyAuto. This does not mean the request was | 33 * will return ok-requested to PyAuto. This does not mean the request was |
| 34 * approved though. The test will then have to click past the dialog that | 34 * approved though. The test will then have to click past the dialog that |
| 35 * appears in Chrome, which will run either the OK or failed callback as a | 35 * appears in Chrome, which will run either the OK or failed callback as a |
| 36 * a result. To see which callback was called, use obtainGetUserMediaResult(). | 36 * a result. To see which callback was called, use obtainGetUserMediaResult(). |
| 37 * | 37 * |
| 38 * @param{string} constraints Defines what to be requested, with mandatory | 38 * @param {string} constraints Defines what to be requested, with mandatory |
| 39 * and optional constraints defined. The contents of this parameter depends | 39 * and optional constraints defined. The contents of this parameter depends |
| 40 * on the WebRTC version. This should be JavaScript code that we eval(). | 40 * on the WebRTC version. This should be JavaScript code that we eval(). |
| 41 */ | 41 */ |
| 42 function getUserMedia(constraints) { | 42 function getUserMedia(constraints) { |
| 43 if (!navigator.webkitGetUserMedia) { | 43 if (!navigator.webkitGetUserMedia) { |
| 44 returnToTest('Browser does not support WebRTC.'); | 44 returnToTest('Browser does not support WebRTC.'); |
| 45 return; | 45 return; |
| 46 } | 46 } |
| 47 try { | 47 try { |
| 48 var evaluatedConstraints; | 48 var evaluatedConstraints; |
| 49 eval('evaluatedConstraints = ' + constraints); | 49 eval('evaluatedConstraints = ' + constraints); |
| 50 } catch (exception) { | 50 } catch (exception) { |
| 51 throw failTest('Not valid JavaScript expression: ' + constraints); | 51 throw failTest('Not valid JavaScript expression: ' + constraints); |
| 52 } | 52 } |
| 53 debug('Requesting getUserMedia: constraints: ' + constraints); | 53 debug('Requesting getUserMedia: constraints: ' + constraints); |
| 54 navigator.webkitGetUserMedia(evaluatedConstraints, | 54 navigator.webkitGetUserMedia(evaluatedConstraints, |
| 55 getUserMediaOkCallback_, | 55 getUserMediaOkCallback_, |
| 56 getUserMediaFailedCallback_); | 56 getUserMediaFailedCallback_); |
| 57 returnToTest('ok-requested'); | 57 returnToTest('ok-requested'); |
| 58 } | 58 } |
| 59 | 59 |
| 60 /** | 60 /** |
| 61 * Must be called after calling getUserMedia. Returns not-called-yet if we have | 61 * Must be called after calling getUserMedia. |
| 62 * not yet been called back by WebRTC. Otherwise it returns either ok-got-stream | 62 * @return {string} Returns not-called-yet if we have not yet been called back |
| 63 * or failed-with-error-x (where x is the error code from the error callback) | 63 * by WebRTC. Otherwise it returns either ok-got-stream or |
| 64 * depending on which callback got called by WebRTC. | 64 * failed-with-error-x (where x is the error code from the error |
| 65 * callback) depending on which callback got called by WebRTC. |
| 65 */ | 66 */ |
| 66 function obtainGetUserMediaResult() { | 67 function obtainGetUserMediaResult() { |
| 67 returnToTest(gRequestWebcamAndMicrophoneResult); | 68 returnToTest(gRequestWebcamAndMicrophoneResult); |
| 68 return gRequestWebcamAndMicrophoneResult; | 69 return gRequestWebcamAndMicrophoneResult; |
| 69 } | 70 } |
| 70 | 71 |
| 71 /** | 72 /** |
| 72 * Stops the local stream. | 73 * Stops the local stream. |
| 73 */ | 74 */ |
| 74 function stopLocalStream() { | 75 function stopLocalStream() { |
| 75 if (gLocalStream == null) | 76 if (gLocalStream == null) |
| 76 throw failTest('Tried to stop local stream, ' + | 77 throw failTest('Tried to stop local stream, ' + |
| 77 'but media access is not granted.'); | 78 'but media access is not granted.'); |
| 78 | 79 |
| 79 gLocalStream.stop(); | 80 gLocalStream.stop(); |
| 80 returnToTest('ok-stopped'); | 81 returnToTest('ok-stopped'); |
| 81 } | 82 } |
| 82 | 83 |
| 83 // Functions callable from other JavaScript modules. | 84 // Functions callable from other JavaScript modules. |
| 84 | 85 |
| 85 /** | 86 /** |
| 86 * Adds the current local media stream to a peer connection. | 87 * Adds the current local media stream to a peer connection. |
| 87 * @param{RTCPeerConnection} peerConnection | 88 * @param {RTCPeerConnection} peerConnection |
| 88 */ | 89 */ |
| 89 function addLocalStreamToPeerConnection(peerConnection) { | 90 function addLocalStreamToPeerConnection(peerConnection) { |
| 90 if (gLocalStream == null) | 91 if (gLocalStream == null) |
| 91 throw failTest('Tried to add local stream to peer connection, ' + | 92 throw failTest('Tried to add local stream to peer connection, ' + |
| 92 'but there is no stream yet.'); | 93 'but there is no stream yet.'); |
| 93 try { | 94 try { |
| 94 peerConnection.addStream(gLocalStream, gAddStreamConstraints); | 95 peerConnection.addStream(gLocalStream, gAddStreamConstraints); |
| 95 } catch (exception) { | 96 } catch (exception) { |
| 96 throw failTest('Failed to add stream with constraints ' + | 97 throw failTest('Failed to add stream with constraints ' + |
| 97 gAddStreamConstraints + ': ' + exception); | 98 gAddStreamConstraints + ': ' + exception); |
| 98 } | 99 } |
| 99 debug('Added local stream.'); | 100 debug('Added local stream.'); |
| 100 } | 101 } |
| 101 | 102 |
| 102 /** | 103 /** |
| 103 * Removes the local stream from the peer connection. | 104 * Removes the local stream from the peer connection. |
| 104 * @param{rtcpeerconnection} peerconnection | 105 * @param {rtcpeerconnection} peerConnection |
| 105 */ | 106 */ |
| 106 function removeLocalStreamFromPeerConnection(peerConnection) { | 107 function removeLocalStreamFromPeerConnection(peerConnection) { |
| 107 if (gLocalStream == null) | 108 if (gLocalStream == null) |
| 108 throw failTest('Tried to remove local stream from peer connection, ' + | 109 throw failTest('Tried to remove local stream from peer connection, ' + |
| 109 'but there is no stream yet.'); | 110 'but there is no stream yet.'); |
| 110 try { | 111 try { |
| 111 peerConnection.removeStream(gLocalStream); | 112 peerConnection.removeStream(gLocalStream); |
| 112 } catch (exception) { | 113 } catch (exception) { |
| 113 throw failTest('Could not remove stream: ' + exception); | 114 throw failTest('Could not remove stream: ' + exception); |
| 114 } | 115 } |
| 115 debug('Removed local stream.'); | 116 debug('Removed local stream.'); |
| 116 } | 117 } |
| 117 | 118 |
| 118 /** | 119 /** |
| 119 * Returns the current local stream - |gLocalStream|. | 120 * @return {string} Returns the current local stream - |gLocalStream|. |
| 120 */ | 121 */ |
| 121 function getLocalStream() { | 122 function getLocalStream() { |
| 122 return gLocalStream; | 123 return gLocalStream; |
| 123 } | 124 } |
| 124 | 125 |
| 125 // Internals. | 126 // Internals. |
| 126 | 127 |
| 127 /** | 128 /** |
| 128 * @private | 129 * @private |
| 129 * @param {MediaStream} stream Media stream. | 130 * @param {MediaStream} stream Media stream. |
| 130 */ | 131 */ |
| 131 function getUserMediaOkCallback_(stream) { | 132 function getUserMediaOkCallback_(stream) { |
| 132 gLocalStream = stream; | 133 gLocalStream = stream; |
| 133 var videoTag = $('local-view'); | 134 var videoTag = $('local-view'); |
| 134 videoTag.src = webkitURL.createObjectURL(stream); | 135 videoTag.src = webkitURL.createObjectURL(stream); |
| 135 | 136 |
| 136 // Due to crbug.com/110938 the size is 0 when onloadedmetadata fires. | 137 // Due to crbug.com/110938 the size is 0 when onloadedmetadata fires. |
| 137 // videoTag.onloadedmetadata = updateVideoTagSize_('local-view'); | 138 // videoTag.onloadedmetadata = displayVideoSize_(videoTag);. |
| 138 // Use setTimeout as a workaround for now. | 139 // Use setTimeout as a workaround for now. |
| 139 setTimeout(function() {updateVideoTagSize_('local-view')}, 500); | |
| 140 gRequestWebcamAndMicrophoneResult = 'ok-got-stream'; | 140 gRequestWebcamAndMicrophoneResult = 'ok-got-stream'; |
| 141 setTimeout(function() {displayVideoSize_(videoTag);}, 500); |
| 141 } | 142 } |
| 142 | 143 |
| 143 /** | 144 /** |
| 144 * @private | 145 * @private |
| 145 * @param {string} videoTagId The ID of the video tag to update. | 146 * @param {string} videoTagId The ID of the video tag to update. |
| 147 * @param {string} width The width of the video to update the video tag, if |
| 148 * width or height is 0, size will be taken from videoTag.videoWidth. |
| 149 * @param {string} height The height of the video to update the video tag, if |
| 150 * width or height is 0 size will be taken from the videoTag.videoHeight. |
| 146 */ | 151 */ |
| 147 function updateVideoTagSize_(videoTagId) { | 152 function updateVideoTagSize_(videoTagId, width, height) { |
| 148 var videoTag = $(videoTagId); | 153 var videoTag = $(videoTagId); |
| 149 // Don't update if sizes are 0 (happens for Chrome M23). | 154 if (width > 0 || height > 0) { |
| 150 if (videoTag.videoWidth > 0 && videoTag.videoHeight > 0) { | 155 videoTag.width = width; |
| 151 debug('Set video tag "' + videoTagId + '" width and height to ' + | 156 videoTag.height = height; |
| 152 videoTag.videoWidth + 'x' + videoTag.videoHeight); | |
| 153 videoTag.width = videoTag.videoWidth; | |
| 154 videoTag.height = videoTag.videoHeight; | |
| 155 } | 157 } |
| 158 else { |
| 159 if (videoTag.videoWidth > 0 || videoTag.videoHeight > 0) { |
| 160 videoTag.width = videoTag.videoWidth; |
| 161 videoTag.height = videoTag.videoHeight; |
| 162 } |
| 163 else { |
| 164 return debug('"' + videoTagId + '" video stream size is 0, skipping ' + |
| 165 'resize'); |
| 166 } |
| 167 } |
| 168 debug('Set video tag "' + videoTagId + '" size to ' + videoTag.width + 'x' + |
| 169 videoTag.height); |
| 170 displayVideoSize_(videoTag); |
| 156 } | 171 } |
| 157 | 172 |
| 158 /** | 173 /** |
| 174 * @private |
| 175 * @param {string} videoTag The ID of the video tag + stream to |
| 176 * write the size to a HTML tag based on id. |
| 177 */ |
| 178 function displayVideoSize_(videoTag) { |
| 179 if (videoTag.videoWidth > 0 || videoTag.videoHeight > 0) { |
| 180 $(videoTag.id + '-stream-size').innerHTML = '(stream size: ' + |
| 181 videoTag.videoWidth + 'x' + |
| 182 videoTag.videoHeight + ')'; |
| 183 } |
| 184 $(videoTag.id + '-size').innerHTML = videoTag.width + 'x' + videoTag.height; |
| 185 } |
| 186 |
| 187 /** |
| 159 * @private | 188 * @private |
| 160 * @param {NavigatorUserMediaError} error Error containing details. | 189 * @param {NavigatorUserMediaError} error Error containing details. |
| 161 */ | 190 */ |
| 162 function getUserMediaFailedCallback_(error) { | 191 function getUserMediaFailedCallback_(error) { |
| 163 debug('GetUserMedia FAILED: Maybe the camera is in use by another process?'); | 192 debug('GetUserMedia FAILED: Maybe the camera is in use by another process?'); |
| 164 gRequestWebcamAndMicrophoneResult = 'failed-with-error-' + error.code; | 193 gRequestWebcamAndMicrophoneResult = 'failed-with-error-' + error.code; |
| 194 debug(gRequestWebcamAndMicrophoneResult); |
| 165 } | 195 } |
| 166 | 196 |
| 167 $ = function(id) { | 197 $ = function(id) { |
| 168 return document.getElementById(id); | 198 return document.getElementById(id); |
| 169 }; | 199 }; |
| OLD | NEW |