Chromium Code Reviews| Index: content/test/data/media/getusermedia_and_stop.html |
| diff --git a/content/test/data/media/getusermedia_and_stop.html b/content/test/data/media/getusermedia_and_stop.html |
| index f1c43bef012ea30a1bfd607e34e4b080ea8b2e17..ba562f11b7210d5699a9a3d60ca11acb0d44564c 100644 |
| --- a/content/test/data/media/getusermedia_and_stop.html |
| +++ b/content/test/data/media/getusermedia_and_stop.html |
| @@ -1,6 +1,30 @@ |
| <html> |
| <head> |
| <script type="text/javascript"> |
| + $ = function(id) { |
| + return document.getElementById(id); |
| + }; |
| + |
| + // These must match with how the video and canvas tags are declared in html. |
| + const VIDEO_TAG_WIDTH = 320; |
| + const VIDEO_TAG_HEIGHT = 240; |
| + |
| + // Number of test events to occur before the test pass. When the test pass, |
| + // the document title change to OK. |
| + var gNumberOfExpectedEvents = 0; |
| + |
| + // Number of events that currently have occurred. |
| + var gNumberOfEvents = 0; |
| + |
| + var gLocalStream = null; |
| + |
| + // This test that a MediaStream can be cloned and that the clone can |
| + // be rendered. |
|
phoglund_chromium
2013/04/09 13:16:28
Maybe getUserMediaAndClone is a better name here.
perkj_chrome
2013/04/11 10:12:41
Done.
|
| + function createCloneStream() { |
| + navigator.webkitGetUserMedia({video: true, audio: true}, |
| + createAndRenderClone, failedCallback); |
| + } |
| + |
| function getUserMedia(constraints) { |
| navigator.webkitGetUserMedia(constraints, okCallback, failedCallback); |
| } |
| @@ -10,9 +34,101 @@ |
| } |
|
phoglund_chromium
2013/04/09 13:16:28
Rename to displayAndWaitForVideo?
perkj_chrome
2013/04/11 10:12:41
Done.
|
| function okCallback(stream) { |
|
phoglund_chromium
2013/04/09 13:16:28
We are no longer testing the stop() method. Can yo
perkj_chrome
2013/04/11 10:12:41
Humm- I did not really want to change the behavior
|
| - stream.stop(); |
| - document.title = 'OK'; |
| + gLocalStream = stream; |
| + waitForVideo('local-view'); |
|
phoglund_chromium
2013/04/09 13:16:28
So this wait function happens to be asynchronous,
perkj_chrome
2013/04/11 10:12:41
Done.
|
| + var localStreamUrl = webkitURL.createObjectURL(stream); |
| + $('local-view').src = localStreamUrl; |
| + } |
| + |
| + function createAndRenderClone(stream) { |
| + gLocalStream = stream; |
| + waitForVideo('local-view'); |
| + |
| + var new_stream = new webkitMediaStream(stream.getAudioTracks()); |
| + expectEquals(new_stream.getAudioTracks().length, 1); |
| + new_stream.addTrack(stream.getVideoTracks()[0]); |
| + expectEquals(new_stream.getVideoTracks().length, 1); |
| + new_stream.removeTrack(new_stream.getAudioTracks()[0]); |
| + expectEquals(new_stream.getAudioTracks().length, 0); |
| + |
| + var newStreamUrl = webkitURL.createObjectURL(new_stream); |
| + $('local-view').src = newStreamUrl; |
| + } |
| + |
| + |
|
phoglund_chromium
2013/04/09 13:16:28
You should be able to extract all code 59-116 to a
perkj_chrome
2013/04/11 10:12:41
Done.
|
| + function detectVideoIn(videoElementName, callback) { |
| + var width = VIDEO_TAG_WIDTH; |
| + var height = VIDEO_TAG_HEIGHT; |
| + var videoElement = $(videoElementName); |
| + var canvas = $(videoElementName + '-canvas'); |
| + var waitVideo = 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)) { |
| + clearInterval(waitVideo); |
| + callback(); |
| + } |
| + }, 100); |
| + } |
| + |
| + function waitForVideo(videoElement) { |
| + document.title = 'Waiting for video...'; |
| + addExpectedEvent(); |
| + detectVideoIn(videoElement, function () { eventOccured(); }); |
| + } |
| + |
| + function addExpectedEvent() { |
| + ++gNumberOfExpectedEvents; |
| + } |
| + |
| + function eventOccured() { |
| + ++gNumberOfEvents; |
| + if (gNumberOfEvents == gNumberOfExpectedEvents) { |
| + gLocalStream.stop(); |
| + document.title = 'OK'; |
| + } |
| + } |
| + |
| + // 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; |
| + } |
| + |
| + // This function matches |left| and |right| and throws an exception if the |
| + // values don't match. |
| + function expectEquals(left, right) { |
| + if (left != right) { |
| + var s = "expectEquals failed left: " + left + " right: " + right; |
| + document.title = s; |
| + throw s; |
| + } |
| } |
| - </script> |
| + |
| + </script> |
| </head> |
| +<body> |
| + <table border="0"> |
| + <tr> |
| + <td>Local Preview</td> |
| + </tr> |
| + <tr> |
| + <td><video width="320" height="240" id="local-view" |
| + autoplay="autoplay"></video></td> |
| + <!-- Canvases are named after their corresponding video elements. --> |
| + <td><canvas width="320" height="240" id="local-view-canvas" |
| + style="display:none"></canvas></td> |
| + </tr> |
| + </table> |
| +</body> |
| </html> |