OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // These must match with how the video and canvas tags are declared in html. | 5 // These must match with how the video and canvas tags are declared in html. |
6 const VIDEO_TAG_WIDTH = 320; | 6 const VIDEO_TAG_WIDTH = 320; |
7 const VIDEO_TAG_HEIGHT = 240; | 7 const VIDEO_TAG_HEIGHT = 240; |
8 | 8 |
9 // Fake video capture background green is of value 135. | 9 // Fake video capture background green is of value 135. |
10 const COLOR_BACKGROUND_GREEN = 135; | 10 const COLOR_BACKGROUND_GREEN = 135; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 }, | 59 }, |
60 callback); | 60 callback); |
61 } | 61 } |
62 | 62 |
63 function detectVideo(videoElementName, predicate, callback) { | 63 function detectVideo(videoElementName, predicate, callback) { |
64 console.log('Looking at video in element ' + videoElementName); | 64 console.log('Looking at video in element ' + videoElementName); |
65 | 65 |
66 var width = VIDEO_TAG_WIDTH; | 66 var width = VIDEO_TAG_WIDTH; |
67 var height = VIDEO_TAG_HEIGHT; | 67 var height = VIDEO_TAG_HEIGHT; |
68 var videoElement = $(videoElementName); | 68 var videoElement = $(videoElementName); |
69 var canvas = $(videoElementName + '-canvas'); | |
70 var oldPixels = []; | 69 var oldPixels = []; |
70 var startTimeMs = new Date().getTime(); | |
71 var waitVideo = setInterval(function() { | 71 var waitVideo = setInterval(function() { |
72 var canvas = $(videoElementName + '-canvas'); | |
73 if (canvas == null) { | |
74 console.log('Waiting for ' + videoElementName + '-canvas' + ' to appear'); | |
75 return; | |
76 } | |
72 var context = canvas.getContext('2d'); | 77 var context = canvas.getContext('2d'); |
73 context.drawImage(videoElement, 0, 0, width, height); | 78 context.drawImage(videoElement, 0, 0, width, height); |
74 var pixels = context.getImageData(0, 0 , width, height / 3).data; | 79 var pixels = context.getImageData(0, 0 , width, height / 3).data; |
75 // Check that there is an old and a new picture with the same size to | 80 // Check that there is an old and a new picture with the same size to |
76 // compare and use the function |predicate| to detect the video state in | 81 // compare and use the function |predicate| to detect the video state in |
77 // that case. | 82 // that case. |
78 if (oldPixels.length == pixels.length && | 83 if (oldPixels.length == pixels.length && |
79 predicate(pixels, oldPixels)) { | 84 predicate(pixels, oldPixels)) { |
80 console.log('Done looking at video in element ' + videoElementName); | 85 console.log('Done looking at video in element ' + videoElementName); |
81 clearInterval(waitVideo); | 86 clearInterval(waitVideo); |
82 callback(videoElement.videoWidth, videoElement.videoHeight); | 87 callback(videoElement.videoWidth, videoElement.videoHeight); |
83 } | 88 } |
84 oldPixels = pixels; | 89 oldPixels = pixels; |
90 | |
91 var elapsedTime = new Date().getTime() - startTimeMs; | |
92 if (elapsedTime > 3000) { | |
93 startTimeMs = new Date().getTime(); | |
94 console.log('Still waiting for video to satisfy ' + predicate.toString()); | |
95 } | |
85 }, 200); | 96 }, 200); |
86 } | 97 } |
87 | 98 |
88 function waitForVideoWithResolution(element, expected_width, expected_height) { | 99 function waitForVideoWithResolution(element, expected_width, expected_height) { |
89 addExpectedEvent(); | 100 addExpectedEvent(); |
90 detectVideoPlaying(element, | 101 detectVideoPlaying(element, |
91 function (width, height) { | 102 function (width, height) { |
92 assertEquals(expected_width, width); | 103 assertEquals(expected_width, width); |
93 assertEquals(expected_height, height); | 104 assertEquals(expected_height, height); |
94 eventOccured(); | 105 eventOccured(); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 // pixels are changed. | 190 // pixels are changed. |
180 function isVideoPlaying(pixels, previousPixels) { | 191 function isVideoPlaying(pixels, previousPixels) { |
181 for (var i = 0; i < pixels.length; i++) { | 192 for (var i = 0; i < pixels.length; i++) { |
182 if (pixels[i] != previousPixels[i]) { | 193 if (pixels[i] != previousPixels[i]) { |
183 return true; | 194 return true; |
184 } | 195 } |
185 } | 196 } |
186 return false; | 197 return false; |
187 } | 198 } |
188 | 199 |
200 // Pixels is an array where pixels[0] is the R value for the first pixel, | |
201 // pixels[1] is the G value, and so on. | |
189 function isVideoBlack(pixels) { | 202 function isVideoBlack(pixels) { |
190 for (var i = 0; i < pixels.length; i++) { | 203 for (var i = 0; i < pixels.length; i++) { |
191 // |pixels| is in RGBA. Ignore the alpha channel. | 204 if ((i + 1) % 4 == 0) { |
192 // We allow it to be off by 1, to account for rounding errors in YUV | 205 // Ignore the alpha channel. |
193 // conversion. | 206 continue; |
194 if (pixels[i] != 0 && pixels[i] != 1 && (i + 1) % 4 != 0) { | 207 } |
208 | |
209 // Should be == 0, but account for rounding errors in YUV conversion. | |
kjellander_chromium
2015/05/12 13:35:46
Clarify in the comment that a black pixel is == 0.
phoglund_chromium
2015/05/13 08:47:35
Done.
| |
210 if (pixels[i] > 3) { | |
211 console.log('Found nonblack pixel at ' + i + ', was ' + pixels[i]); | |
195 return false; | 212 return false; |
196 } | 213 } |
197 } | 214 } |
198 return true; | 215 return true; |
199 } | 216 } |
200 | 217 |
201 // This function matches |left| and |right| and fails the test if the | 218 // This function matches |left| and |right| and fails the test if the |
202 // values don't match using normal javascript equality (i.e. the hard | 219 // values don't match using normal javascript equality (i.e. the hard |
203 // types of the operands aren't checked). | 220 // types of the operands aren't checked). |
204 function assertEquals(expected, actual) { | 221 function assertEquals(expected, actual) { |
(...skipping 17 matching lines...) Expand all Loading... | |
222 if (device.kind == 'video') | 239 if (device.kind == 'video') |
223 hasVideoInputDevice = true; | 240 hasVideoInputDevice = true; |
224 }); | 241 }); |
225 | 242 |
226 if (hasVideoInputDevice) | 243 if (hasVideoInputDevice) |
227 sendValueToTest('has-video-input-device'); | 244 sendValueToTest('has-video-input-device'); |
228 else | 245 else |
229 sendValueToTest('no-video-input-devices'); | 246 sendValueToTest('no-video-input-devices'); |
230 }); | 247 }); |
231 } | 248 } |
OLD | NEW |