Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / tab_capture / performance.js
blobbfb0c15eb7bf1cf24a06045505970a0fa046542a
1 // Copyright 2014 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 // The tests here cover the end-to-end functionality of tab capturing and
6 // playback as video. The page generates a test patter (moving balls) and
7 // the rendering output of the tab is captured into a LocalMediaStream. Then,
8 // the LocalMediaStream is plugged into a video element for playback.
9 //
11 // Global to prevent gc from eating the video tag.
12 var video = null;
13 var capture_stream = null;
15 function TestStream(stream) {
16 // Create video and canvas elements, but no need to append them to the
17 // DOM.
18 video = document.createElement("video");
19 video.width = 1920;
20 video.height = 1080;
21 video.addEventListener("error", chrome.test.fail);
23 var canvas = document.createElement("canvas");
24 canvas.width = video.width;
25 canvas.height = video.height;
26 var context = canvas.getContext("2d");
27 document.body.appendChild(canvas);
28 var start_time = new Date().getTime();
30 // Play the LocalMediaStream in the video element.
31 video.src = URL.createObjectURL(stream);
32 video.play();
34 var frame = 0;
35 function draw() {
36 // Run for 10 seconds.
37 if (new Date().getTime() - start_time > 10000) {
38 chrome.test.succeed();
39 // Note that the API testing framework might not terminate if we keep
40 // animating and capturing, so we have to make sure that we stop doing
41 // that here.
42 if (capture_stream) {
43 capture_stream.stop();
45 stream.stop();
46 return;
48 requestAnimationFrame(draw);
49 frame = frame + 1;
50 context.fillStyle = 'rgb(255,255,255)';
51 context.fillRect( 0, 0, canvas.width, canvas.height );
52 for (var j = 0; j < 200; j++) {
53 var i = (j + frame) % 200;
54 var t = (frame + 3000) * (0.01 + i / 8000.0);
55 var x = (Math.sin( t ) * 0.45 + 0.5) * canvas.width;
56 var y = (Math.cos( t * 0.9 ) * 0.45 + 0.5) * canvas.height;
57 context.fillStyle = 'rgb(' + (255 - i) + ',' + (155 +i) + ', ' + i + ')';
58 context.beginPath();
59 context.arc(x, y, 50, 0, Math.PI * 2, true);
60 context.closePath();
61 context.fill();
65 // Kick it off.
66 draw();
69 // Set up a WebRTC connection and pipe |stream| through it.
70 function testThroughWebRTC(stream) {
71 capture_stream = stream;
72 console.log("Testing through webrtc.");
73 var sender = new webkitRTCPeerConnection(null);
74 var receiver = new webkitRTCPeerConnection(null);
75 sender.onicecandidate = function (event) {
76 if (event.candidate) {
77 receiver.addIceCandidate(new RTCIceCandidate(event.candidate));
80 receiver.onicecandidate = function (event) {
81 if (event.candidate) {
82 sender.addIceCandidate(new RTCIceCandidate(event.candidate));
85 receiver.onaddstream = function (event) {
86 TestStream(event.stream);
88 sender.addStream(stream);
89 sender.createOffer(function (sender_description) {
90 sender.setLocalDescription(sender_description);
91 receiver.setRemoteDescription(sender_description);
92 receiver.createAnswer(function (receiver_description) {
93 receiver.setLocalDescription(receiver_description);
94 sender.setRemoteDescription(receiver_description);
95 });
96 });
99 function tabCapturePerformanceTest() {
100 var f = TestStream;
101 if (parseInt(window.location.href.split('?WebRTC=')[1])) {
102 f = testThroughWebRTC;
104 var fps = parseInt(window.location.href.split('&fps=')[1]);
105 chrome.tabCapture.capture(
106 { video: true, audio: false,
107 videoConstraints: {
108 mandatory: {
109 minWidth: 1920,
110 minHeight: 1080,
111 maxWidth: 1920,
112 maxHeight: 1080,
113 maxFrameRate: fps,
120 chrome.test.runTests([ tabCapturePerformanceTest ]);
122 // TODO(hubbe): Consider capturing audio as well, need to figure out how to
123 // capture relevant statistics for that though.