Commit dffd2b76 authored by phoglund@chromium.org's avatar phoglund@chromium.org

Added a basic WebRTC peerconnection browser test with video verification.


BUG=


Review URL: https://chromiumcodereview.appspot.com/11428136

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@170989 0039d316-1c4b-4281-b951-d872f2087c98
parent 21f65c05
......@@ -2,8 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/command_line.h"
#include "base/utf_string_conversions.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test_utils.h"
#include "content/shell/shell.h"
#include "content/test/content_browser_test.h"
......@@ -12,26 +14,26 @@
namespace content {
// Tests The WebRTC getUserMedia call.
// Note: Requires --use-fake-device-for-media-stream (this flag is set by
// default in content_browsertests).
class WebrtcGetUserMediaTest: public ContentBrowserTest {
class WebrtcBrowserTest: public ContentBrowserTest {
public:
WebrtcGetUserMediaTest() {}
protected:
void TestGetUserMediaWithConstraints(const std::string& constraints) {
ASSERT_TRUE(test_server()->Start());
GURL empty_url(test_server()->GetURL(
"files/media/getusermedia_and_stop.html"));
NavigateToURL(shell(), empty_url);
WebrtcBrowserTest() {}
virtual ~WebrtcBrowserTest() {}
virtual void SetUp() OVERRIDE {
// We need fake devices in this test since we want to run on naked VMs. We
// assume this switch is set by default in content_browsertests.
ASSERT_TRUE(CommandLine::ForCurrentProcess()->HasSwitch(
switches::kUseFakeDeviceForMediaStream));
ASSERT_TRUE(test_server()->Start());
ContentBrowserTest::SetUp();
}
protected:
bool ExecuteJavascript(const std::string& javascript) {
RenderViewHost* render_view_host =
shell()->web_contents()->GetRenderViewHost();
EXPECT_TRUE(ExecuteJavaScript(render_view_host, L"",
ASCIIToWide(constraints)));
ExpectTitle("OK");
return ExecuteJavaScript(render_view_host, L"", ASCIIToWide(javascript));
}
void ExpectTitle(const std::string& expected_title) const {
......@@ -44,17 +46,40 @@ class WebrtcGetUserMediaTest: public ContentBrowserTest {
// These tests will all make a getUserMedia call with different constraints and
// see that the success callback is called. If the error callback is called or
// none of the callbacks are called the tests will simply time out and fail.
IN_PROC_BROWSER_TEST_F(WebrtcGetUserMediaTest, GetVideoStreamAndStop) {
TestGetUserMediaWithConstraints("getUserMedia({video: true});");
IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, GetVideoStreamAndStop) {
GURL url(test_server()->GetURL("files/media/getusermedia_and_stop.html"));
NavigateToURL(shell(), url);
EXPECT_TRUE(ExecuteJavascript("getUserMedia({video: true});"));
ExpectTitle("OK");
}
IN_PROC_BROWSER_TEST_F(WebrtcGetUserMediaTest, GetAudioAndVideoStreamAndStop) {
TestGetUserMediaWithConstraints("getUserMedia({video: true, audio: true});");
IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, GetAudioAndVideoStreamAndStop) {
GURL url(test_server()->GetURL("files/media/getusermedia_and_stop.html"));
NavigateToURL(shell(), url);
EXPECT_TRUE(ExecuteJavascript("getUserMedia({video: true, audio: true});"));
ExpectTitle("OK");
}
// TODO(phoglund): enable once we have fake audio device support.
IN_PROC_BROWSER_TEST_F(WebrtcGetUserMediaTest, DISABLED_GetAudioStreamAndStop) {
TestGetUserMediaWithConstraints("getUserMedia({audio: true});");
// These tests will make a complete PeerConnection-based call and verify that
// video is playing for the call.
IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, CanSetupVideoCall) {
GURL url(test_server()->GetURL("files/media/peerconnection-call.html"));
NavigateToURL(shell(), url);
EXPECT_TRUE(ExecuteJavascript("call({video: true});"));
ExpectTitle("OK");
}
IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, CanSetupAudioAndVideoCall) {
GURL url(test_server()->GetURL("files/media/peerconnection-call.html"));
NavigateToURL(shell(), url);
EXPECT_TRUE(ExecuteJavascript("call({video: true, audio: true});"));
ExpectTitle("OK");
}
} // namespace content
......
......@@ -795,7 +795,7 @@
}],
['enable_webrtc==1', {
'sources': [
'browser/webrtc_getusermedia_browsertest.cc',
'browser/webrtc_browsertest.cc',
],
}],
],
......
<html>
<head>
<script type="text/javascript">
$ = function(id) {
return document.getElementById(id);
};
var gFirstConnection = null;
var gSecondConnection = null;
function call(constraints) {
navigator.webkitGetUserMedia(constraints, okCallback, failedCallback);
}
function failedCallback(error) {
document.title = 'getUserMedia request failed with code ' + error.code;
}
function okCallback(localStream) {
var localStreamUrl = webkitURL.createObjectURL(localStream);
$('local-view').src = localStreamUrl;
callUsingStream(localStream);
}
function callUsingStream(localStream) {
gFirstConnection = new webkitRTCPeerConnection(null, null);
gFirstConnection.onicecandidate = onIceCandidateToFirst;
gFirstConnection.addStream(localStream);
gFirstConnection.createOffer(onOfferCreated);
}
function onOfferCreated(offer) {
gFirstConnection.setLocalDescription(offer);
receiveCall(offer.sdp);
}
function receiveCall(offerSdp) {
gSecondConnection = new webkitRTCPeerConnection(null, null);
gSecondConnection.onicecandidate = onIceCandidateToSecond;
gSecondConnection.onaddstream = onRemoteStream;
var parsedOffer = new RTCSessionDescription({ type: 'offer',
sdp: offerSdp });
gSecondConnection.setRemoteDescription(parsedOffer);
gSecondConnection.createAnswer(onAnswerCreated);
}
function onAnswerCreated(answer) {
gSecondConnection.setLocalDescription(answer);
handleAnswer(answer.sdp);
}
function handleAnswer(answerSdp) {
var parsedAnswer = new RTCSessionDescription({ type: 'answer',
sdp: answerSdp });
gFirstConnection.setRemoteDescription(parsedAnswer);
}
function onIceCandidateToFirst(event) {
if (event.candidate) {
var candidate = new RTCIceCandidate(event.candidate);
gSecondConnection.addIceCandidate(candidate);
}
}
function onIceCandidateToSecond(event) {
if (event.candidate) {
var candidate = new RTCIceCandidate(event.candidate);
gFirstConnection.addIceCandidate(candidate);
}
}
function onRemoteStream(e) {
var remoteStreamUrl = webkitURL.createObjectURL(e.stream);
var remoteVideo = $('remote-view');
remoteVideo.src = remoteStreamUrl;
waitForVideo(remoteVideo, 320, 240);
}
// TODO(phoglund): perhaps use the video detector in chrome/test/data/webrtc/?
function waitForVideo(videoElement, width, height) {
document.title = 'Waiting for video...';
var canvas = $('canvas');
setInterval(function() {
var context = canvas.getContext('2d');
context.drawImage(videoElement, 0, 0, width, height);
var pixels = context.getImageData(0, 0, width, height).data;
if (isVideoPlaying(pixels, width, height))
testSuccessful();
}, 100);
}
// This very basic video verification algorithm will be satisfied if any
// pixels are nonzero in a small sample area in the middle. It relies on the
// assumption that a video element with null source just presents zeroes.
function isVideoPlaying(pixels, width, height) {
// Sample somewhere near the middle of the image.
var middle = width * height / 2;
for (var i = 0; i < 20; i++) {
if (pixels[middle + i] > 0) {
return true;
}
}
return false;
}
function testSuccessful() {
document.title = 'OK';
}
</script>
</head>
<body>
<table border="0">
<tr>
<td>Local Preview</td>
<td>Remote Stream</td>
<td>Capturing Canvas</td>
</tr>
<tr>
<td><video width="320" height="240" id="local-view"
autoplay="autoplay"></video></td>
<td><video width="320" height="240" id="remote-view"
autoplay="autoplay"></video></td>
<td><canvas width="320" height="240" id="canvas"></canvas></td>
</tr>
<tr>
<td colspan="3">You should see the same animated feed in all three
displays (the canvas will lag a bit).
</td>
</table>
</body>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment