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 = updateVideoTagSize_('local-view'); |
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 | |
148 * tag, if width or height is 0, size will be taken from | |
149 * videoTag.videoWidth. | |
150 * @param {string} height The height of the video to update the video tag, if | |
151 * width or height is 0 size will be taken from the videoTag.videoHeight. | |
146 */ | 152 */ |
147 function updateVideoTagSize_(videoTagId) { | 153 function updateVideoTagSize_(videoTagId, width, height) { |
148 var videoTag = $(videoTagId); | 154 var videoTag = $(videoTagId); |
149 // Don't update if sizes are 0 (happens for Chrome M23). | 155 if (width > 0 || height > 0) { |
150 if (videoTag.videoWidth > 0 && videoTag.videoHeight > 0) { | 156 videoTag.width = width; |
151 debug('Set video tag "' + videoTagId + '" width and height to ' + | 157 videoTag.height = height; |
152 videoTag.videoWidth + 'x' + videoTag.videoHeight); | |
153 videoTag.width = videoTag.videoWidth; | |
154 videoTag.height = videoTag.videoHeight; | |
155 } | 158 } |
159 else { | |
160 if (videoTag.videoWidth > 0 || videoTag.videoHeight > 0) { | |
161 videoTag.width = videoTag.videoWidth; | |
162 videoTag.height = videoTag.videoHeight; | |
163 } | |
164 else { | |
165 return debug('"' + videoTagId + '" video stream size is 0, skipping ' + | |
166 'resize'); | |
kjellander_chromium
2013/04/12 09:58:18
nit: Indent to match above parenthesis.
jansson
2013/04/16 14:25:17
Done.
| |
167 } | |
168 } | |
169 debug('Set video tag "' + videoTagId + '" size to ' + videoTag.width + 'x' + | |
170 videoTag.height); | |
171 displayVideoSize_(videoTag); | |
156 } | 172 } |
157 | 173 |
158 /** | 174 /** |
175 * @private | |
176 * @param {string} videoTag The ID of the video tag + stream to | |
177 * write the size to a HTML tag based on id. | |
178 */ | |
179 function displayVideoSize_(videoTag) { | |
180 if (videoTag.videoWidth > 0 || videoTag.videoHeight > 0) { | |
181 $(videoTag.id + '-stream-size').innerHTML = '(stream size: ' + | |
182 videoTag.videoWidth + 'x' + | |
183 videoTag.videoHeight + ')'; | |
184 } | |
185 $(videoTag.id + '-size').innerHTML = videoTag.width + 'x' + videoTag.height; | |
186 } | |
187 | |
188 /** | |
159 * @private | 189 * @private |
160 * @param {NavigatorUserMediaError} error Error containing details. | 190 * @param {NavigatorUserMediaError} error Error containing details. |
161 */ | 191 */ |
162 function getUserMediaFailedCallback_(error) { | 192 function getUserMediaFailedCallback_(error) { |
163 debug('GetUserMedia FAILED: Maybe the camera is in use by another process?'); | 193 debug('GetUserMedia FAILED: Maybe the camera is in use by another process?'); |
164 gRequestWebcamAndMicrophoneResult = 'failed-with-error-' + error.code; | 194 gRequestWebcamAndMicrophoneResult = 'failed-with-error-' + error.code; |
195 debug(gRequestWebcamAndMicrophoneResult); | |
165 } | 196 } |
166 | 197 |
167 $ = function(id) { | 198 $ = function(id) { |
168 return document.getElementById(id); | 199 return document.getElementById(id); |
169 }; | 200 }; |
OLD | NEW |