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);
69 var canvas = $(videoElementName + '-canvas');
71 var waitVideo = setInterval(function() {
72 var context = canvas.getContext('2d');
73 context.drawImage(videoElement, 0, 0, width, height);
74 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
76 // compare and use the function |predicate| to detect the video state in
78 if (oldPixels.length == pixels.length &&
79 predicate(pixels, oldPixels)) {
80 console.log('Done looking at video in element ' + videoElementName);
81 clearInterval(waitVideo);
82 callback(videoElement.videoWidth, videoElement.videoHeight);
88 function waitForVideoWithResolution(element, expected_width, expected_height) {
90 detectVideoPlaying(element,
91 function (width, height) {
92 assertEquals(expected_width, width);
93 assertEquals(expected_height, height);
98 function waitForVideo(videoElement) {
100 detectVideoPlaying(videoElement, function () { eventOccured(); });
103 function waitForVideoToStop(videoElement) {
105 detectVideoStopped(videoElement, function () { eventOccured(); });
108 function waitForBlackVideo(videoElement) {
110 detectBlackVideo(videoElement, function () { eventOccured(); });
113 // Calculates the current frame rate and compares to |expected_frame_rate|
114 // |callback| is triggered with value |true| if the calculated frame rate
115 // is +-1 the expected or |false| if five calculations fail to match
116 // |expected_frame_rate|. Calls back with OK if the check passed, otherwise
118 function validateFrameRate(videoElementName, expected_frame_rate, callback) {
119 var videoElement = $(videoElementName);
120 var startTime = new Date().getTime();
121 var decodedFrames = videoElement.webkitDecodedFrameCount;
124 if (videoElement.readyState <= HTMLMediaElement.HAVE_CURRENT_DATA ||
125 videoElement.paused || videoElement.ended) {
126 failTest("getFrameRate - " + videoElementName + " is not plaing.");
130 var waitVideo = setInterval(function() {
132 currentTime = new Date().getTime();
133 deltaTime = (currentTime - startTime) / 1000;
134 startTime = currentTime;
136 // Calculate decoded frames per sec.
138 (videoElement.webkitDecodedFrameCount - decodedFrames) / deltaTime;
139 decodedFrames = videoElement.webkitDecodedFrameCount;
141 console.log('FrameRate in ' + videoElementName + ' is ' + fps);
142 if (fps < expected_frame_rate + 1 && fps > expected_frame_rate - 1) {
143 clearInterval(waitVideo);
145 } else if (attempts == 5) {
146 clearInterval(waitVideo);
147 callback('Expected frame rate ' + expected_frame_rate + ' for ' +
148 'element ' + videoElementName + ', but got ' + fps);
153 function waitForConnectionToStabilize(peerConnection, callback) {
154 peerConnection.onsignalingstatechange = function(event) {
155 if (peerConnection.signalingState == 'stable') {
156 peerConnection.onsignalingstatechange = null;
162 // Adds an expected event. You may call this function many times to add more
163 // expected events. Each expected event must later be matched by a call to
164 // eventOccurred. When enough events have occurred, the "all events occurred
165 // handler" will be called.
166 function addExpectedEvent() {
167 ++gNumberOfExpectedEvents;
170 // See addExpectedEvent.
171 function eventOccured() {
173 if (gNumberOfEvents == gNumberOfExpectedEvents) {
178 // This very basic video verification algorithm will be satisfied if any
179 // pixels are changed.
180 function isVideoPlaying(pixels, previousPixels) {
181 for (var i = 0; i < pixels.length; i++) {
182 if (pixels[i] != previousPixels[i]) {
189 function isVideoBlack(pixels) {
190 for (var i = 0; i < pixels.length; i++) {
191 // |pixels| is in RGBA. Ignore the alpha channel.
192 // We allow it to be off by 1, to account for rounding errors in YUV
194 if (pixels[i] != 0 && pixels[i] != 1 && (i + 1) % 4 != 0) {
201 // 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
203 // types of the operands aren't checked).
204 function assertEquals(expected, actual) {
205 if (actual != expected) {
206 failTest("expected '" + expected + "', got '" + actual + "'.");
210 function assertNotEquals(expected, actual) {
211 if (actual === expected) {
212 failTest("expected '" + expected + "', got '" + actual + "'.");
216 // Returns has-video-input-device to the test if there's a webcam available on
218 function hasVideoInputDeviceOnSystem() {
219 MediaStreamTrack.getSources(function(devices) {
220 var hasVideoInputDevice = false;
221 devices.forEach(function(device) {
222 if (device.kind == 'video')
223 hasVideoInputDevice = true;
226 if (hasVideoInputDevice)
227 sendValueToTest('has-video-input-device');
229 sendValueToTest('no-video-input-devices');