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.
11 // Global to prevent gc from eating the video tag.
13 var capture_stream = null;
15 function TestStream(stream) {
16 // Create video and canvas elements, but no need to append them to the
18 video = document.createElement("video");
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);
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
43 capture_stream.stop();
48 requestAnimationFrame(draw);
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 + ')';
59 context.arc(x, y, 50, 0, Math.PI * 2, true);
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);
99 function tabCapturePerformanceTest() {
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,
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.