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
3 // found in the LICENSE file.
5 // These must match with how the video and canvas tags are declared in html.
6 const VIDEO_TAG_WIDTH = 320;
7 const VIDEO_TAG_HEIGHT = 240;
9 // Fake video capture background green is of value 135.
10 const COLOR_BACKGROUND_GREEN = 135;
12 // Number of test events to occur before the test pass. When the test pass,
13 // the function gAllEventsOccured is called.
14 var gNumberOfExpectedEvents = 0;
16 // Number of events that currently have occurred.
17 var gNumberOfEvents = 0;
19 var gAllEventsOccured = function () {};
21 // Use this function to set a function that will be called once all expected
22 // events has occurred.
23 function setAllEventsOccuredHandler(handler) {
24 gAllEventsOccured = handler;
27 // Tells the C++ code we succeeded, which will generally exit the test.
28 function reportTestSuccess() {
29 window.domAutomationController.send('OK');
32 // Returns a custom return value to the test.
33 function sendValueToTest(value) {
34 window.domAutomationController.send(value);
37 // Immediately fails the test on the C++ side.
38 function failTest(reason) {
39 var error = new Error(reason);
40 window.domAutomationController.send(error.stack);
43 function detectVideoPlaying(videoElementName, callback) {
44 detectVideo(videoElementName, isVideoPlaying, callback);
47 function detectVideoStopped(videoElementName, callback) {
48 detectVideo(videoElementName,
49 function (pixels, previous_pixels) {
50 return !isVideoPlaying(pixels, previous_pixels);
55 function detectBlackVideo(videoElementName, callback) {
56 detectVideo(videoElementName,
57 function (pixels, previous_pixels) {
58 return isVideoBlack(pixels);
63 function detectVideo(videoElementName, predicate, callback) {
64 console.log('Looking at video in element ' + videoElementName);
66 var width = VIDEO_TAG_WIDTH;
67 var height = VIDEO_TAG_HEIGHT;
68 var videoElement = $(videoElementName);
70 var startTimeMs = new Date().getTime();
71 var waitVideo = setInterval(function() {
72 var canvas = $(videoElementName + '-canvas');
74 console.log('Waiting for ' + videoElementName + '-canvas' + ' to appear');
77 var context = canvas.getContext('2d');
78 context.drawImage(videoElement, 0, 0, width, height);
79 var pixels = context.getImageData(0, 0 , width, height / 3).data;
80 // Check that there is an old and a new picture with the same size to
81 // compare and use the function |predicate| to detect the video state in
83 if (oldPixels.length == pixels.length &&
84 predicate(pixels, oldPixels)) {
85 console.log('Done looking at video in element ' + videoElementName);
86 clearInterval(waitVideo);
87 callback(videoElement.videoWidth, videoElement.videoHeight);
90 var elapsedTime = new Date().getTime() - startTimeMs;
91 if (elapsedTime > 3000) {
92 startTimeMs = new Date().getTime();
93 console.log('Still waiting for video to satisfy ' + predicate.toString());
98 function waitForVideoWithResolution(element, expected_width, expected_height) {
100 detectVideoPlaying(element,
101 function (width, height) {
102 assertEquals(expected_width, width);
103 assertEquals(expected_height, height);
108 function waitForVideo(videoElement) {
110 detectVideoPlaying(videoElement, function () { eventOccured(); });
113 function waitForVideoToStop(videoElement) {
115 detectVideoStopped(videoElement, function () { eventOccured(); });
118 function waitForBlackVideo(videoElement) {
120 detectBlackVideo(videoElement, function () { eventOccured(); });
123 // Calculates the current frame rate and compares to |expected_frame_rate|
124 // |callback| is triggered with value |true| if the calculated frame rate
125 // is +-1 the expected or |false| if five calculations fail to match
126 // |expected_frame_rate|. Calls back with OK if the check passed, otherwise
128 function validateFrameRate(videoElementName, expected_frame_rate, callback) {
129 var videoElement = $(videoElementName);
130 var startTime = new Date().getTime();
131 var decodedFrames = videoElement.webkitDecodedFrameCount;
134 if (videoElement.readyState <= HTMLMediaElement.HAVE_CURRENT_DATA ||
135 videoElement.paused || videoElement.ended) {
136 failTest("getFrameRate - " + videoElementName + " is not plaing.");
140 var waitVideo = setInterval(function() {
142 currentTime = new Date().getTime();
143 deltaTime = (currentTime - startTime) / 1000;
144 startTime = currentTime;
146 // Calculate decoded frames per sec.
148 (videoElement.webkitDecodedFrameCount - decodedFrames) / deltaTime;
149 decodedFrames = videoElement.webkitDecodedFrameCount;
151 console.log('FrameRate in ' + videoElementName + ' is ' + fps);
152 if (fps < expected_frame_rate + 1 && fps > expected_frame_rate - 1) {
153 clearInterval(waitVideo);
155 } else if (attempts == 5) {
156 clearInterval(waitVideo);
157 callback('Expected frame rate ' + expected_frame_rate + ' for ' +
158 'element ' + videoElementName + ', but got ' + fps);
163 function waitForConnectionToStabilize(peerConnection, callback) {
164 peerConnection.onsignalingstatechange = function(event) {
165 if (peerConnection.signalingState == 'stable') {
166 peerConnection.onsignalingstatechange = null;
172 // Adds an expected event. You may call this function many times to add more
173 // expected events. Each expected event must later be matched by a call to
174 // eventOccurred. When enough events have occurred, the "all events occurred
175 // handler" will be called.
176 function addExpectedEvent() {
177 ++gNumberOfExpectedEvents;
180 // See addExpectedEvent.
181 function eventOccured() {
183 if (gNumberOfEvents == gNumberOfExpectedEvents) {
188 // This very basic video verification algorithm will be satisfied if any
189 // pixels are changed.
190 function isVideoPlaying(pixels, previousPixels) {
191 for (var i = 0; i < pixels.length; i++) {
192 if (pixels[i] != previousPixels[i]) {
199 // Checks if the frame is black. |pixels| is in RGBA (i.e. pixels[0] is the R
200 // value for the first pixel).
201 function isVideoBlack(pixels) {
203 var accumulatedLuma = 0;
204 for (var i = 0; i < pixels.length; i += 4) {
205 // Ignore the alpha channel.
206 accumulatedLuma += rec702Luma_(pixels[i], pixels[i + 1], pixels[i + 2]);
207 if (accumulatedLuma > threshold * (i / 4 + 1))
213 // Use Luma as in Rec. 709: Y′709 = 0.2126R + 0.7152G + 0.0722B;
214 // See http://en.wikipedia.org/wiki/Rec._709.
215 function rec702Luma_(r, g, b) {
216 return 0.2126 * r + 0.7152 * g + 0.0722 * b;
219 // This function matches |left| and |right| and fails the test if the
220 // values don't match using normal javascript equality (i.e. the hard
221 // types of the operands aren't checked).
222 function assertEquals(expected, actual) {
223 if (actual != expected) {
224 failTest("expected '" + expected + "', got '" + actual + "'.");
228 function assertNotEquals(expected, actual) {
229 if (actual === expected) {
230 failTest("expected '" + expected + "', got '" + actual + "'.");
234 // Returns has-video-input-device to the test if there's a webcam available on
236 function hasVideoInputDeviceOnSystem() {
237 MediaStreamTrack.getSources(function(devices) {
238 var hasVideoInputDevice = false;
239 devices.forEach(function(device) {
240 if (device.kind == 'video')
241 hasVideoInputDevice = true;
244 if (hasVideoInputDevice)
245 sendValueToTest('has-video-input-device');
247 sendValueToTest('no-video-input-devices');