Skip to content

🎥 Make videos programmatically with React

`@remotion/media`: do not trigger buffer state at the end of the media

Igor Samokhovets4197c10f889f
diff --git a/packages/media/src/video/media-player.ts b/packages/media/src/video/media-player.ts
index dce430a417..cd9abbaf31 100644
--- a/packages/media/src/video/media-player.ts
+++ b/packages/media/src/video/media-player.ts
@@ -10,7 +10,7 @@ import type {LogLevel} from 'remotion';
import {Internals} from 'remotion';
import {getTimeInSeconds} from '../get-time-in-seconds';
import {isNetworkError} from '../is-network-error';
-import {sleep, withTimeout} from './timeout-utils';
+import {sleep, TimeoutError, withTimeout} from './timeout-utils';
export const SEEK_THRESHOLD = 0.05;
const AUDIO_BUFFER_TOLERANCE_THRESHOLD = 0.1;
@@ -81,6 +81,8 @@ export class MediaPlayer {
private audioIteratorStarted = false;
private readonly HEALTHY_BUFER_THRESHOLD_SECONDS = 1;
+ private mediaEnded = false;
+
private onVideoFrameCallback?: (frame: CanvasImageSource) => void;
constructor({
@@ -319,6 +321,7 @@ export class MediaPlayer {
if (isSignificantSeek) {
this.nextFrame = null;
this.audioSyncAnchor = this.sharedAudioContext.currentTime - newTime;
+ this.mediaEnded = false;
if (this.audioSink) {
await this.cleanAudioIteratorAndNodes();
@@ -600,6 +603,7 @@ export class MediaPlayer {
(await this.videoFrameIterator.next()).value ?? null;
if (!newNextFrame) {
+ this.mediaEnded = true;
break;
}
@@ -706,13 +710,18 @@ export class MediaPlayer {
BUFFERING_TIMEOUT_MS,
'Iterator timeout',
);
- } catch {
- this.setBufferingState(true);
+ } catch (error) {
+ if (error instanceof TimeoutError && !this.mediaEnded) {
+ this.setBufferingState(true);
+ }
+
await sleep(10);
continue;
}
+ // media has ended
if (result.done || !result.value) {
+ this.mediaEnded = true;
break;
}
diff --git a/packages/media/src/video/timeout-utils.ts b/packages/media/src/video/timeout-utils.ts
index a6d3c0c534..126141a587 100644
--- a/packages/media/src/video/timeout-utils.ts
+++ b/packages/media/src/video/timeout-utils.ts
@@ -2,6 +2,13 @@
export const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
+export class TimeoutError extends Error {
+ constructor(message: string = 'Operation timed out') {
+ super(message);
+ this.name = 'TimeoutError';
+ }
+}
+
export function withTimeout<T>(
promise: Promise<T>,
timeoutMs: number,
@@ -11,7 +18,7 @@ export function withTimeout<T>(
const timeoutPromise = new Promise<never>((_, reject) => {
timeoutId = window.setTimeout(() => {
- reject(new Error(errorMessage));
+ reject(new TimeoutError(errorMessage));
}, timeoutMs);
});