OLD | NEW |
---|---|
(Empty) | |
1 // These must match with how the video and canvas tags are declared in html. | |
2 const VIDEO_TAG_WIDTH = 320; | |
3 const VIDEO_TAG_HEIGHT = 240; | |
4 | |
5 // Number of test events to occur before the test pass. When the test pass, | |
6 // the function gAllEventsOccured is called. | |
7 var gNumberOfExpectedEvents = 0; | |
8 | |
9 // Number of events that currently have occurred. | |
10 var gNumberOfEvents = 0; | |
11 | |
phoglund_chromium
2013/04/11 11:16:48
Write a small accessor to overwrite this guy which
perkj_chrome
2013/04/11 11:50:48
Done.
| |
12 var gAllEventsOccured = function () {}; | |
13 | |
14 function detectVideoIn(videoElementName, callback) { | |
15 var width = VIDEO_TAG_WIDTH; | |
16 var height = VIDEO_TAG_HEIGHT; | |
17 var videoElement = $(videoElementName); | |
18 var canvas = $(videoElementName + '-canvas'); | |
19 var waitVideo = setInterval(function() { | |
20 var context = canvas.getContext('2d'); | |
21 context.drawImage(videoElement, 0, 0, width, height); | |
22 var pixels = context.getImageData(0, 0, width, height).data; | |
23 | |
24 if (isVideoPlaying(pixels, width, height)) { | |
25 clearInterval(waitVideo); | |
26 callback(); | |
27 } | |
28 }, 100); | |
29 } | |
30 | |
31 function waitForVideo(videoElement) { | |
32 document.title = 'Waiting for video...'; | |
33 addExpectedEvent(); | |
34 detectVideoIn(videoElement, function () { eventOccured(); }); | |
35 } | |
36 | |
37 function addExpectedEvent() { | |
38 ++gNumberOfExpectedEvents; | |
39 } | |
40 | |
41 function eventOccured() { | |
42 ++gNumberOfEvents; | |
43 if (gNumberOfEvents == gNumberOfExpectedEvents) { | |
44 gAllEventsOccured(); | |
45 } | |
46 } | |
47 | |
48 // This very basic video verification algorithm will be satisfied if any | |
49 // pixels are nonzero in a small sample area in the middle. It relies on the | |
50 // assumption that a video element with null source just presents zeroes. | |
51 function isVideoPlaying(pixels, width, height) { | |
52 // Sample somewhere near the middle of the image. | |
53 var middle = width * height / 2; | |
54 for (var i = 0; i < 20; i++) { | |
55 if (pixels[middle + i] > 0) { | |
56 return true; | |
57 } | |
58 } | |
59 return false; | |
60 } | |
61 | |
62 // This function matches |left| and |right| and throws an exception if the | |
63 // values don't match. | |
64 function expectEquals(left, right) { | |
65 if (left != right) { | |
66 var s = "expectEquals failed left: " + left + " right: " + right; | |
67 document.title = s; | |
68 throw s; | |
69 } | |
70 } | |
OLD | NEW |