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; |
11 | 11 |
12 // Number of test events to occur before the test pass. When the test pass, | 12 // Number of test events to occur before the test pass. When the test pass, |
13 // the function gAllEventsOccured is called. | 13 // the function gAllEventsOccured is called. |
14 var gNumberOfExpectedEvents = 0; | 14 var gNumberOfExpectedEvents = 0; |
15 | 15 |
16 // Number of events that currently have occurred. | 16 // Number of events that currently have occurred. |
17 var gNumberOfEvents = 0; | 17 var gNumberOfEvents = 0; |
18 | 18 |
19 var gAllEventsOccured = function () {}; | 19 var gAllEventsOccured = function () {}; |
20 | 20 |
21 // Use this function to set a function that will be called once all expected | 21 // Use this function to set a function that will be called once all expected |
22 // events has occurred. | 22 // events has occurred. |
23 function setAllEventsOccuredHandler(handler) { | 23 function setAllEventsOccuredHandler(handler) { |
24 gAllEventsOccured = handler; | 24 gAllEventsOccured = handler; |
25 } | 25 } |
26 | 26 |
27 function detectVideoIn(videoElementName, callback) { | 27 function detectVideoPlaying(videoElementName, callback) { |
| 28 detectVideo(videoElementName, isVideoPlaying, callback); |
| 29 } |
| 30 |
| 31 function detectVideoStopped(videoElementName, callback) { |
| 32 detectVideo(videoElementName, |
| 33 function (pixels, previous_pixels) { |
| 34 return !isVideoPlaying(pixels, previous_pixels); |
| 35 }, |
| 36 callback); |
| 37 } |
| 38 |
| 39 function detectVideo(videoElementName, predicate, callback) { |
28 var width = VIDEO_TAG_WIDTH; | 40 var width = VIDEO_TAG_WIDTH; |
29 var height = VIDEO_TAG_HEIGHT; | 41 var height = VIDEO_TAG_HEIGHT; |
30 var videoElement = $(videoElementName); | 42 var videoElement = $(videoElementName); |
31 var canvas = $(videoElementName + '-canvas'); | 43 var canvas = $(videoElementName + '-canvas'); |
| 44 var old_pixels = []; |
32 var waitVideo = setInterval(function() { | 45 var waitVideo = setInterval(function() { |
33 var context = canvas.getContext('2d'); | 46 var context = canvas.getContext('2d'); |
34 context.drawImage(videoElement, 0, 0, width, height); | 47 context.drawImage(videoElement, 0, 0, width, height); |
35 var pixels = context.getImageData(0, 0, width, height).data; | 48 var pixels = context.getImageData(0, 0 , width, height / 3).data; |
36 | 49 // Check that there is an old and a new picture with the same size to |
37 if (isVideoPlaying(pixels, width, height)) { | 50 // compare and use the function |predicate| to detect the video state in |
| 51 // that case. |
| 52 if (old_pixels.length == pixels.length && |
| 53 predicate(pixels, old_pixels)) { |
38 clearInterval(waitVideo); | 54 clearInterval(waitVideo); |
39 callback(); | 55 callback(); |
40 } | 56 } |
41 }, 100); | 57 old_pixels = pixels; |
| 58 }, 200); |
42 } | 59 } |
43 | 60 |
44 function waitForVideo(videoElement) { | 61 function waitForVideo(videoElement) { |
45 document.title = 'Waiting for video...'; | 62 document.title = 'Waiting for video...'; |
46 addExpectedEvent(); | 63 addExpectedEvent(); |
47 detectVideoIn(videoElement, function () { eventOccured(); }); | 64 detectVideoPlaying(videoElement, function () { eventOccured(); }); |
| 65 } |
| 66 |
| 67 function waitForVideoToStop(videoElement) { |
| 68 document.title = 'Waiting for video to stop...'; |
| 69 addExpectedEvent(); |
| 70 detectVideoStopped(videoElement, function () { eventOccured(); }); |
48 } | 71 } |
49 | 72 |
50 function waitForConnectionToStabilize(peerConnection) { | 73 function waitForConnectionToStabilize(peerConnection) { |
51 addExpectedEvent(); | 74 addExpectedEvent(); |
52 var waitForStabilization = setInterval(function() { | 75 var waitForStabilization = setInterval(function() { |
53 if (peerConnection.signalingState == 'stable') { | 76 if (peerConnection.signalingState == 'stable') { |
54 clearInterval(waitForStabilization); | 77 clearInterval(waitForStabilization); |
55 eventOccured(); | 78 eventOccured(); |
56 } | 79 } |
57 }, 100); | 80 }, 100); |
58 } | 81 } |
59 | 82 |
60 function addExpectedEvent() { | 83 function addExpectedEvent() { |
61 ++gNumberOfExpectedEvents; | 84 ++gNumberOfExpectedEvents; |
62 } | 85 } |
63 | 86 |
64 function eventOccured() { | 87 function eventOccured() { |
65 ++gNumberOfEvents; | 88 ++gNumberOfEvents; |
66 if (gNumberOfEvents == gNumberOfExpectedEvents) { | 89 if (gNumberOfEvents == gNumberOfExpectedEvents) { |
67 gAllEventsOccured(); | 90 gAllEventsOccured(); |
68 } | 91 } |
69 } | 92 } |
70 | 93 |
71 // This very basic video verification algorithm will be satisfied if any | 94 // This very basic video verification algorithm will be satisfied if any |
72 // pixels are nonzero in a small sample area in the middle. It relies on the | 95 // pixels are changed. |
73 // assumption that a video element with null source just presents zeroes. | 96 function isVideoPlaying(pixels, previous_pixels) { |
74 function isVideoPlaying(pixels, width, height) { | 97 for (var i = 0; i < pixels.length; i++) { |
75 // Sample somewhere near the middle of the image. | 98 if (pixels[i] != previous_pixels[i]) { |
76 var middle = width * height / 2; | |
77 for (var i = 0; i < 20; i++) { | |
78 if (pixels[middle + i] > 0) { | |
79 return true; | 99 return true; |
80 } | 100 } |
81 } | 101 } |
82 return false; | 102 return false; |
83 } | 103 } |
84 | 104 |
85 // This function matches |left| and |right| and throws an exception if the | 105 // This function matches |left| and |right| and throws an exception if the |
86 // values don't match. | 106 // values don't match. |
87 function expectEquals(left, right) { | 107 function expectEquals(left, right) { |
88 if (left != right) { | 108 if (left != right) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 detectedAspectRatioString += " cropped"; | 191 detectedAspectRatioString += " cropped"; |
172 } else | 192 } else |
173 detectedAspectRatioString += " letterbox"; | 193 detectedAspectRatioString += " letterbox"; |
174 | 194 |
175 console.log("Original image is: " + detectedAspectRatioString); | 195 console.log("Original image is: " + detectedAspectRatioString); |
176 callback(detectedAspectRatioString); | 196 callback(detectedAspectRatioString); |
177 } | 197 } |
178 }, | 198 }, |
179 50); | 199 50); |
180 } | 200 } |
OLD | NEW |