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 19 matching lines...) Expand all Loading... |
30 | 30 |
31 function detectVideoStopped(videoElementName, callback) { | 31 function detectVideoStopped(videoElementName, callback) { |
32 detectVideo(videoElementName, | 32 detectVideo(videoElementName, |
33 function (pixels, previous_pixels) { | 33 function (pixels, previous_pixels) { |
34 return !isVideoPlaying(pixels, previous_pixels); | 34 return !isVideoPlaying(pixels, previous_pixels); |
35 }, | 35 }, |
36 callback); | 36 callback); |
37 } | 37 } |
38 | 38 |
39 function detectVideo(videoElementName, predicate, callback) { | 39 function detectVideo(videoElementName, predicate, callback) { |
| 40 console.log('Looking at video in element ' + videoElementName); |
| 41 |
40 var width = VIDEO_TAG_WIDTH; | 42 var width = VIDEO_TAG_WIDTH; |
41 var height = VIDEO_TAG_HEIGHT; | 43 var height = VIDEO_TAG_HEIGHT; |
42 var videoElement = $(videoElementName); | 44 var videoElement = $(videoElementName); |
43 var canvas = $(videoElementName + '-canvas'); | 45 var canvas = $(videoElementName + '-canvas'); |
44 var old_pixels = []; | 46 var oldPixels = []; |
45 var waitVideo = setInterval(function() { | 47 var waitVideo = setInterval(function() { |
46 var context = canvas.getContext('2d'); | 48 var context = canvas.getContext('2d'); |
47 context.drawImage(videoElement, 0, 0, width, height); | 49 context.drawImage(videoElement, 0, 0, width, height); |
48 var pixels = context.getImageData(0, 0 , width, height / 3).data; | 50 var pixels = context.getImageData(0, 0 , width, height / 3).data; |
49 // Check that there is an old and a new picture with the same size to | 51 // Check that there is an old and a new picture with the same size to |
50 // compare and use the function |predicate| to detect the video state in | 52 // compare and use the function |predicate| to detect the video state in |
51 // that case. | 53 // that case. |
52 if (old_pixels.length == pixels.length && | 54 if (oldPixels.length == pixels.length && |
53 predicate(pixels, old_pixels)) { | 55 predicate(pixels, oldPixels)) { |
| 56 console.log('Done looking at video in element ' + videoElementName); |
54 clearInterval(waitVideo); | 57 clearInterval(waitVideo); |
55 callback(); | 58 callback(); |
56 } | 59 } |
57 old_pixels = pixels; | 60 oldPixels = pixels; |
58 }, 200); | 61 }, 200); |
59 } | 62 } |
60 | 63 |
61 function waitForVideo(videoElement) { | 64 function waitForVideo(videoElement) { |
62 document.title = 'Waiting for video...'; | 65 document.title = 'Waiting for video...'; |
63 addExpectedEvent(); | 66 addExpectedEvent(); |
64 detectVideoPlaying(videoElement, function () { eventOccured(); }); | 67 detectVideoPlaying(videoElement, function () { eventOccured(); }); |
65 } | 68 } |
66 | 69 |
67 function waitForVideoToStop(videoElement) { | 70 function waitForVideoToStop(videoElement) { |
(...skipping 17 matching lines...) Expand all Loading... |
85 | 88 |
86 function eventOccured() { | 89 function eventOccured() { |
87 ++gNumberOfEvents; | 90 ++gNumberOfEvents; |
88 if (gNumberOfEvents == gNumberOfExpectedEvents) { | 91 if (gNumberOfEvents == gNumberOfExpectedEvents) { |
89 gAllEventsOccured(); | 92 gAllEventsOccured(); |
90 } | 93 } |
91 } | 94 } |
92 | 95 |
93 // This very basic video verification algorithm will be satisfied if any | 96 // This very basic video verification algorithm will be satisfied if any |
94 // pixels are changed. | 97 // pixels are changed. |
95 function isVideoPlaying(pixels, previous_pixels) { | 98 function isVideoPlaying(pixels, previousPixels) { |
96 for (var i = 0; i < pixels.length; i++) { | 99 for (var i = 0; i < pixels.length; i++) { |
97 if (pixels[i] != previous_pixels[i]) { | 100 if (pixels[i] != previousPixels[i]) { |
98 return true; | 101 return true; |
99 } | 102 } |
100 } | 103 } |
101 return false; | 104 return false; |
102 } | 105 } |
103 | 106 |
104 // This function matches |left| and |right| and throws an exception if the | 107 // This function matches |left| and |right| and throws an exception if the |
105 // values don't match. | 108 // values don't match. |
106 function expectEquals(left, right) { | 109 function expectEquals(left, right) { |
107 if (left != right) { | 110 if (left != right) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 detectedAspectRatioString += " cropped"; | 193 detectedAspectRatioString += " cropped"; |
191 } else | 194 } else |
192 detectedAspectRatioString += " letterbox"; | 195 detectedAspectRatioString += " letterbox"; |
193 | 196 |
194 console.log("Original image is: " + detectedAspectRatioString); | 197 console.log("Original image is: " + detectedAspectRatioString); |
195 callback(detectedAspectRatioString); | 198 callback(detectedAspectRatioString); |
196 } | 199 } |
197 }, | 200 }, |
198 50); | 201 50); |
199 } | 202 } |
OLD | NEW |