Skip to content

Commit 1bec9ec

Browse files
committed
De-flake fromPixels test failure
`HTMLVideoElement.requestVideoFrameCallback()` provides a realiable way to guarantee the readiness of a video frame, whereas the browser may not update the ready state until a few frames later. In such cases, false alarms will be raised by the validation in `fromPixels()`. For where rVFC is unavailable, fallback to the old path based on the `loadeddata` event. This at least seems better than waiting on nothing. Fixes #6577
1 parent c5c5d9f commit 1bec9ec

File tree

3 files changed

+5
-19
lines changed

3 files changed

+5
-19
lines changed

tfjs-backend-webgpu/src/from_pixels_webgpu_test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ describeWebGPU('fromPixels', () => {
5656
if ('requestVideoFrameCallback' in video) {
5757
// tslint:disable-next-line:no-any
5858
await new Promise(go => (video as any).requestVideoFrameCallback(go));
59+
} else {
60+
// On mobile safari the ready state is ready immediately.
61+
if (video.readyState < HTMLVideoElement.prototype.HAVE_CURRENT_DATA) {
62+
await new Promise(go => video.addEventListener('loadeddata', go));
63+
}
5964
}
6065

6166
{

tfjs-core/src/ops/browser.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,6 @@ function fromPixels_(
107107
` or {data: Uint32Array, width: number, height: number}, ` +
108108
`but was ${(pixels as {}).constructor.name}`);
109109
}
110-
if (isVideo) {
111-
const HAVE_CURRENT_DATA_READY_STATE = 2;
112-
if (isVideo &&
113-
(pixels as HTMLVideoElement).readyState <
114-
HAVE_CURRENT_DATA_READY_STATE) {
115-
throw new Error(
116-
'The video element has not loaded data yet. Please wait for ' +
117-
'`loadeddata` event on the <video> element.');
118-
}
119-
}
120110
// If the current backend has 'FromPixels' registered, it has a more
121111
// efficient way of handling pixel uploads, so we call that.
122112
const kernel = getKernel(FromPixels, ENGINE.backendName);

tfjs-core/src/ops/from_pixels_test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,6 @@ describeWithFlags('fromPixels', BROWSER_ENVS, () => {
233233
document.body.removeChild(video);
234234
}, 30_000 /* 30 seconds */);
235235

236-
it('fromPixels for HTMLVideoElement throws without loadeddata', async () => {
237-
const video = document.createElement('video');
238-
video.width = 1;
239-
video.height = 1;
240-
video.src = 'data:image/gif;base64' +
241-
',R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==';
242-
expect(() => tf.browser.fromPixels(video)).toThrowError();
243-
});
244-
245236
it('throws when passed a primitive number', () => {
246237
const msg = /pixels passed to tf.browser.fromPixels\(\) must be either/;
247238
// tslint:disable-next-line:no-any

0 commit comments

Comments
 (0)