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 detectVideo(videoElementName, predicate, callback) {
56 console.log('Looking at video in element ' + videoElementName);
58 var width = VIDEO_TAG_WIDTH;
59 var height = VIDEO_TAG_HEIGHT;
60 var videoElement = $(videoElementName);
61 var canvas = $(videoElementName + '-canvas');
63 var waitVideo = setInterval(function() {
64 var context = canvas.getContext('2d');
65 context.drawImage(videoElement, 0, 0, width, height);
66 var pixels = context.getImageData(0, 0 , width, height / 3).data;
67 // Check that there is an old and a new picture with the same size to
68 // compare and use the function |predicate| to detect the video state in
70 if (oldPixels.length == pixels.length &&
71 predicate(pixels, oldPixels)) {
72 console.log('Done looking at video in element ' + videoElementName);
73 clearInterval(waitVideo);
74 callback(videoElement.videoWidth, videoElement.videoHeight);
80 function waitForVideoWithResolution(element, expected_width, expected_height) {
82 detectVideoPlaying(element,
83 function (width, height) {
84 assertEquals(expected_width, width);
85 assertEquals(expected_height, height);
90 function waitForVideo(videoElement) {
92 detectVideoPlaying(videoElement, function () { eventOccured(); });
95 function waitForVideoToStop(videoElement) {
97 detectVideoStopped(videoElement, function () { eventOccured(); });
100 // Calculates the current frame rate and compares to |expected_frame_rate|
101 // |callback| is triggered with value |true| if the calculated frame rate
102 // is +-1 the expected or |false| if five calculations fail to match
103 // |expected_frame_rate|.
104 function validateFrameRate(videoElementName, expected_frame_rate, callback) {
105 var videoElement = $(videoElementName);
106 var startTime = new Date().getTime();
107 var decodedFrames = videoElement.webkitDecodedFrameCount;
110 if (videoElement.readyState <= HTMLMediaElement.HAVE_CURRENT_DATA ||
111 videoElement.paused || videoElement.ended) {
112 failTest("getFrameRate - " + videoElementName + " is not plaing.");
116 var waitVideo = setInterval(function() {
118 currentTime = new Date().getTime();
119 deltaTime = (currentTime - startTime) / 1000;
120 startTime = currentTime;
122 // Calculate decoded frames per sec.
124 (videoElement.webkitDecodedFrameCount - decodedFrames) / deltaTime;
125 decodedFrames = videoElement.webkitDecodedFrameCount;
127 console.log('FrameRate in ' + videoElementName + ' is ' + fps);
128 if (fps < expected_frame_rate + 1 && fps > expected_frame_rate - 1) {
129 clearInterval(waitVideo);
131 } else if (attempts == 5) {
132 clearInterval(waitVideo);
138 function waitForConnectionToStabilize(peerConnection, callback) {
139 peerConnection.onsignalingstatechange = function(event) {
140 if (peerConnection.signalingState == 'stable') {
141 peerConnection.onsignalingstatechange = null;
147 // Adds an expected event. You may call this function many times to add more
148 // expected events. Each expected event must later be matched by a call to
149 // eventOccurred. When enough events have occurred, the "all events occurred
150 // handler" will be called.
151 function addExpectedEvent() {
152 ++gNumberOfExpectedEvents;
155 // See addExpectedEvent.
156 function eventOccured() {
158 if (gNumberOfEvents == gNumberOfExpectedEvents) {
163 // This very basic video verification algorithm will be satisfied if any
164 // pixels are changed.
165 function isVideoPlaying(pixels, previousPixels) {
166 for (var i = 0; i < pixels.length; i++) {
167 if (pixels[i] != previousPixels[i]) {
174 // This function matches |left| and |right| and fails the test if the
175 // values don't match using normal javascript equality (i.e. the hard
176 // types of the operands aren't checked).
177 function assertEquals(expected, actual) {
178 if (actual != expected) {
179 failTest("expected '" + expected + "', got '" + actual + "'.");
183 function assertNotEquals(expected, actual) {
184 if (actual === expected) {
185 failTest("expected '" + expected + "', got '" + actual + "'.");