Commit 3a2a5fb9 authored by perkj@chromium.org's avatar perkj@chromium.org

Add simple test for VideoTrack.stop().

BUG= 293304

Review URL: https://codereview.chromium.org/68263016

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@235698 0039d316-1c4b-4281-b951-d872f2087c98
parent fd0bf122
...@@ -5,19 +5,19 @@ ...@@ -5,19 +5,19 @@
$ = function(id) { $ = function(id) {
return document.getElementById(id); return document.getElementById(id);
}; };
var gLocalStream = null; var gLocalStream = null;
setAllEventsOccuredHandler(function() { setAllEventsOccuredHandler(function() {
gLocalStream.stop(); gLocalStream.stop();
document.title = 'OK'; document.title = 'OK';
}); });
// This test that a MediaStream can be created and a local preview // This test that a MediaStream can be created and a local preview
// rendered. // rendered.
function getUserMedia(constraints) { function getUserMedia(constraints) {
navigator.webkitGetUserMedia(constraints, displayAndWaitForVideo, navigator.webkitGetUserMedia(constraints,
failedCallback); displayAndWaitForVideoToStartAndStop, failedCallback);
} }
function getUserMediaWithAnalysis(constraints) { function getUserMediaWithAnalysis(constraints) {
...@@ -25,22 +25,25 @@ ...@@ -25,22 +25,25 @@
constraints, displayAndWaitForAndAnalyzeVideo, failedCallback); constraints, displayAndWaitForAndAnalyzeVideo, failedCallback);
} }
// This test that a MediaStream can be cloned and that the clone can // This test that a MediaStream can be cloned and that the clone can
// be rendered. // be rendered.
function getUserMediaAndClone() { function getUserMediaAndClone() {
navigator.webkitGetUserMedia({video: true, audio: true}, navigator.webkitGetUserMedia({video: true, audio: true},
createAndRenderClone, failedCallback); createAndRenderClone, failedCallback);
} }
function failedCallback(error) { function failedCallback(error) {
document.title = 'GetUserMedia call failed with code ' + error.code; document.title = 'GetUserMedia call failed with code ' + error.code;
} }
function displayAndWaitForVideo(stream) { function displayAndWaitForVideoToStartAndStop(stream) {
gLocalStream = stream; gLocalStream = stream;
var localStreamUrl = webkitURL.createObjectURL(stream); var localStreamUrl = webkitURL.createObjectURL(stream);
$('local-view').src = localStreamUrl; $('local-view').src = localStreamUrl;
waitForVideo('local-view'); document.title = 'Waiting for video...';
// Wait for video to play, then stop the track and wait for video to stop
// playing.
detectVideoPlaying('local-view', stopVideoTrack);
} }
function displayAndWaitForAndAnalyzeVideo(stream) { function displayAndWaitForAndAnalyzeVideo(stream) {
...@@ -53,7 +56,7 @@ ...@@ -53,7 +56,7 @@
function createAndRenderClone(stream) { function createAndRenderClone(stream) {
gLocalStream = stream; gLocalStream = stream;
// TODO(perkj): --use-fake-device-for-media-stream do not currently // TODO(perkj): --use-fake-device-for-media-stream do not currently
// work with audio devices and not all bots has a microphone. // work with audio devices and not all bots has a microphone.
new_stream = new webkitMediaStream(); new_stream = new webkitMediaStream();
new_stream.addTrack(stream.getVideoTracks()[0]); new_stream.addTrack(stream.getVideoTracks()[0]);
expectEquals(new_stream.getVideoTracks().length, 1); expectEquals(new_stream.getVideoTracks().length, 1);
...@@ -63,12 +66,17 @@ ...@@ -63,12 +66,17 @@
new_stream.removeTrack(new_stream.getAudioTracks()[0]); new_stream.removeTrack(new_stream.getAudioTracks()[0]);
expectEquals(new_stream.getAudioTracks().length, 0); expectEquals(new_stream.getAudioTracks().length, 0);
} }
var newStreamUrl = webkitURL.createObjectURL(new_stream); var newStreamUrl = webkitURL.createObjectURL(new_stream);
$('local-view').src = newStreamUrl; $('local-view').src = newStreamUrl;
waitForVideo('local-view'); waitForVideo('local-view');
} }
function stopVideoTrack() {
gLocalStream.getVideoTracks()[0].stop();
waitForVideoToStop('local-view');
}
function analyzeVideo() { function analyzeVideo() {
document.title = 'Waiting for video...'; document.title = 'Waiting for video...';
addExpectedEvent(); addExpectedEvent();
...@@ -78,7 +86,7 @@ ...@@ -78,7 +86,7 @@
}); });
} }
</script> </script>
</head> </head>
<body> <body>
<table border="0"> <table border="0">
...@@ -87,7 +95,7 @@ ...@@ -87,7 +95,7 @@
</tr> </tr>
<tr> <tr>
<td><video width="320" height="240" id="local-view" <td><video width="320" height="240" id="local-view"
autoplay="autoplay"></video></td> autoplay="autoplay"></video></td>
<!-- Canvases are named after their corresponding video elements. --> <!-- Canvases are named after their corresponding video elements. -->
<td><canvas width="320" height="240" id="local-view-canvas" <td><canvas width="320" height="240" id="local-view-canvas"
style="display:none"></canvas></td> style="display:none"></canvas></td>
......
...@@ -102,8 +102,8 @@ ...@@ -102,8 +102,8 @@
} }
// Do the forwarding after we have received video. // Do the forwarding after we have received video.
detectVideoIn('remote-view-1', onRemoteStream1); detectVideoPlaying('remote-view-1', onRemoteStream1);
detectVideoIn('remote-view-2', onRemoteStream2); detectVideoPlaying('remote-view-2', onRemoteStream2);
} }
// Test that we can setup call with an audio and video track and // Test that we can setup call with an audio and video track and
...@@ -212,7 +212,7 @@ ...@@ -212,7 +212,7 @@
} }
// Do the DTMF test after we have received video. // Do the DTMF test after we have received video.
detectVideoIn('remote-view-2', onCallEstablished); detectVideoPlaying('remote-view-2', onCallEstablished);
} }
// Test call with a new Video MediaStream that has been created based on a // Test call with a new Video MediaStream that has been created based on a
......
...@@ -24,27 +24,50 @@ function setAllEventsOccuredHandler(handler) { ...@@ -24,27 +24,50 @@ function setAllEventsOccuredHandler(handler) {
gAllEventsOccured = handler; gAllEventsOccured = handler;
} }
function detectVideoIn(videoElementName, callback) { function detectVideoPlaying(videoElementName, callback) {
detectVideo(videoElementName, isVideoPlaying, callback);
}
function detectVideoStopped(videoElementName, callback) {
detectVideo(videoElementName,
function (pixels, previous_pixels) {
return !isVideoPlaying(pixels, previous_pixels);
},
callback);
}
function detectVideo(videoElementName, predicate, callback) {
var width = VIDEO_TAG_WIDTH; var width = VIDEO_TAG_WIDTH;
var height = VIDEO_TAG_HEIGHT; var height = VIDEO_TAG_HEIGHT;
var videoElement = $(videoElementName); var videoElement = $(videoElementName);
var canvas = $(videoElementName + '-canvas'); var canvas = $(videoElementName + '-canvas');
var old_pixels = [];
var waitVideo = setInterval(function() { var waitVideo = setInterval(function() {
var context = canvas.getContext('2d'); var context = canvas.getContext('2d');
context.drawImage(videoElement, 0, 0, width, height); context.drawImage(videoElement, 0, 0, width, height);
var pixels = context.getImageData(0, 0, width, height).data; var pixels = context.getImageData(0, 0 , width, height / 3).data;
// Check that there is an old and a new picture with the same size to
if (isVideoPlaying(pixels, width, height)) { // compare and use the function |predicate| to detect the video state in
// that case.
if (old_pixels.length == pixels.length &&
predicate(pixels, old_pixels)) {
clearInterval(waitVideo); clearInterval(waitVideo);
callback(); callback();
} }
}, 100); old_pixels = pixels;
}, 200);
} }
function waitForVideo(videoElement) { function waitForVideo(videoElement) {
document.title = 'Waiting for video...'; document.title = 'Waiting for video...';
addExpectedEvent(); addExpectedEvent();
detectVideoIn(videoElement, function () { eventOccured(); }); detectVideoPlaying(videoElement, function () { eventOccured(); });
}
function waitForVideoToStop(videoElement) {
document.title = 'Waiting for video to stop...';
addExpectedEvent();
detectVideoStopped(videoElement, function () { eventOccured(); });
} }
function waitForConnectionToStabilize(peerConnection) { function waitForConnectionToStabilize(peerConnection) {
...@@ -69,13 +92,10 @@ function eventOccured() { ...@@ -69,13 +92,10 @@ function eventOccured() {
} }
// This very basic video verification algorithm will be satisfied if any // 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 // pixels are changed.
// assumption that a video element with null source just presents zeroes. function isVideoPlaying(pixels, previous_pixels) {
function isVideoPlaying(pixels, width, height) { for (var i = 0; i < pixels.length; i++) {
// Sample somewhere near the middle of the image. if (pixels[i] != previous_pixels[i]) {
var middle = width * height / 2;
for (var i = 0; i < 20; i++) {
if (pixels[middle + i] > 0) {
return true; return true;
} }
} }
......
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