Skip to content

Commit

Permalink
Simplify startPlayingAndWaitForVideo with modern async/await. (#3319)
Browse files Browse the repository at this point in the history
* Simplify startPlayingAndWaitForVideo with modern async/await.
* [startPlayingAndWaitForVideo] Use requestVideoFrameCallback if supported.
  Chrome needs this currently.
* Make function async.

TODO: Update callsites to use async instead of callback.
  • Loading branch information
kdashg authored Aug 20, 2021
1 parent 17fa7af commit afcae42
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions sdk/tests/js/webgl-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2927,14 +2927,6 @@ var requestAnimFrame = function(callback) {
_requestAnimFrame.call(window, callback);
};

/**
* Provides video.requestVideoFrameCallback in a cross browser way.
* Returns a property, or undefined if unsuported.
*/
var getRequestVidFrameCallback = function() {
return HTMLVideoElement.prototype["requestVideoFrameCallback"];
};

var _cancelAnimFrame;

/**
Expand Down Expand Up @@ -3136,38 +3128,30 @@ var runSteps = function(steps) {
* @param {!function(!HTMLVideoElement): void} callback Function to call when
* video is ready.
*/
function startPlayingAndWaitForVideo(video, callback) {
async function startPlayingAndWaitForVideo(video, callback) {
if (video.error) {
testFailed('Video failed to load: ' + video.error);
return;
}

video.addEventListener(
'error', e => { testFailed('Video playback failed: ' + e.message); },
true);

var rvfc = getRequestVidFrameCallback();
if (rvfc === undefined) {
var timeWatcher = function() {
if (video.currentTime > 0) {
callback(video);
} else {
requestAnimFrame.call(window, timeWatcher);
}
};

timeWatcher();
} else {
// Calls video.requestVideoFrameCallback(_ => { callback(video) })
rvfc.call(video, _ => { callback(video) });
}

video.loop = true;
video.muted = true;
// See whether setting the preload flag de-flakes video-related tests.
video.preload = 'auto';
video.play();
};

try {
await video.play();
} catch (e) {
testFailed('video.play failed: ' + e);
return;
}

if (video.requestVideoFrameCallback) {
await new Promise(go => video.requestVideoFrameCallback(go));
}

callback(video);
}

var getHost = function(url) {
url = url.replace("\\", "/");
Expand Down

0 comments on commit afcae42

Please sign in to comment.