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|.
117 function validateFrameRate(videoElementName, expected_frame_rate, callback) {
118 var videoElement = $(videoElementName);
119 var startTime = new Date().getTime();
120 var decodedFrames = videoElement.webkitDecodedFrameCount;
123 if (videoElement.readyState <= HTMLMediaElement.HAVE_CURRENT_DATA ||
124 videoElement.paused || videoElement.ended) {
125 failTest("getFrameRate - " + videoElementName + " is not plaing.");
129 var waitVideo = setInterval(function() {
131 currentTime = new Date().getTime();
132 deltaTime = (currentTime - startTime) / 1000;
133 startTime = currentTime;
135 // Calculate decoded frames per sec.
137 (videoElement.webkitDecodedFrameCount - decodedFrames) / deltaTime;
138 decodedFrames = videoElement.webkitDecodedFrameCount;
140 console.log('FrameRate in ' + videoElementName + ' is ' + fps);
141 if (fps < expected_frame_rate + 1 && fps > expected_frame_rate - 1) {
142 clearInterval(waitVideo);
144 } else if (attempts == 5) {
145 clearInterval(waitVideo);
151 function waitForConnectionToStabilize(peerConnection, callback) {
152 peerConnection.onsignalingstatechange = function(event) {
153 if (peerConnection.signalingState == 'stable') {
154 peerConnection.onsignalingstatechange = null;
160 // Adds an expected event. You may call this function many times to add more
161 // expected events. Each expected event must later be matched by a call to
162 // eventOccurred. When enough events have occurred, the "all events occurred
163 // handler" will be called.
164 function addExpectedEvent() {
165 ++gNumberOfExpectedEvents;
168 // See addExpectedEvent.
169 function eventOccured() {
171 if (gNumberOfEvents == gNumberOfExpectedEvents) {
176 // This very basic video verification algorithm will be satisfied if any
177 // pixels are changed.
178 function isVideoPlaying(pixels, previousPixels) {
179 for (var i = 0; i < pixels.length; i++) {
180 if (pixels[i] != previousPixels[i]) {
187 function isVideoBlack(pixels) {
188 for (var i = 0; i < pixels.length; i++) {
189 // |pixels| is in RGBA. Ignore the alpha channel.
190 if (pixels[i] != 0 && (i + 1) % 4 != 0) {
197 // This function matches |left| and |right| and fails the test if the
198 // values don't match using normal javascript equality (i.e. the hard
199 // types of the operands aren't checked).
200 function assertEquals(expected, actual) {
201 if (actual != expected) {
202 failTest("expected '" + expected + "', got '" + actual + "'.");
206 function assertNotEquals(expected, actual) {
207 if (actual === expected) {
208 failTest("expected '" + expected + "', got '" + actual + "'.");