Skip to content

🎥 Make videos programmatically with React

rip out the stuff we don't need

JonnyBurgera4d2852ab37c
diff --git a/packages/media/src/audio/audio-preview-iterator.ts b/packages/media/src/audio/audio-preview-iterator.ts
index 9282f338ec..1059e973fa 100644
--- a/packages/media/src/audio/audio-preview-iterator.ts
+++ b/packages/media/src/audio/audio-preview-iterator.ts
@@ -8,8 +8,6 @@ export const makeAudioIterator = (
) => {
let destroyed = false;
const iterator = audioSink.buffers(startFromSecond);
- let audioIteratorStarted = false;
- let audioBufferHealth = 0;
const queuedAudioNodes: Set<AudioBufferSourceNode> = new Set();
const cleanupAudioQueue = () => {
@@ -27,18 +25,9 @@ export const makeAudioIterator = (
destroyed = true;
iterator.return().catch(() => undefined);
},
- isReadyToPlay: () => {
- return audioIteratorStarted && audioBufferHealth > 0;
- },
- setAudioIteratorStarted: (started: boolean) => {
- audioIteratorStarted = started;
- },
getNext: () => {
return iterator.next();
},
- setAudioBufferHealth: (health: number) => {
- audioBufferHealth = health;
- },
isDestroyed: () => {
return destroyed;
},
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index ac03532b50..0228fc3c68 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -1,4 +1,4 @@
-import type {WrappedAudioBuffer, WrappedCanvas} from 'mediabunny';
+import type {WrappedCanvas} from 'mediabunny';
import {
ALL_FORMATS,
AudioBufferSink,
@@ -9,7 +9,6 @@ import {
import type {LogLevel, useBufferState} from 'remotion';
import {Internals} from 'remotion';
import {
- HEALTHY_BUFFER_THRESHOLD_SECONDS,
makeAudioIterator,
type AudioIterator,
} from './audio/audio-preview-iterator';
@@ -17,7 +16,6 @@ import type {DebugStats} from './debug-overlay/preview-overlay';
import {drawPreviewOverlay} from './debug-overlay/preview-overlay';
import {getTimeInSeconds} from './get-time-in-seconds';
import {isNetworkError} from './is-network-error';
-import {sleep, TimeoutError, withTimeout} from './video/timeout-utils';
import {
createVideoIterator,
type VideoIterator,
@@ -155,10 +153,6 @@ export class MediaPlayer {
return Boolean(this.audioSink && this.sharedAudioContext && this.gainNode);
}
- private isCurrentlyBuffering(): boolean {
- return this.isBuffering && Boolean(this.bufferingStartedAtMs);
- }
-
private isDisposalError(): boolean {
return this.input?.disposed === true;
}
@@ -615,42 +609,6 @@ export class MediaPlayer {
}
};
- private bufferingStartedAtMs: number | null = null;
- private minBufferingTimeoutMs: number = 500;
-
- private setBufferingState(isBuffering: boolean): void {
- if (this.isBuffering !== isBuffering) {
- this.isBuffering = isBuffering;
-
- if (isBuffering) {
- this.bufferingStartedAtMs = performance.now();
- this.onBufferingChangeCallback?.(true);
- } else {
- this.bufferingStartedAtMs = null;
- this.onBufferingChangeCallback?.(false);
- }
- }
- }
-
- private maybeResumeFromBuffering(currentBufferDuration: number): void {
- if (!this.isCurrentlyBuffering()) return;
-
- const now = performance.now();
- const bufferingDuration = now - this.bufferingStartedAtMs!;
-
- const minTimeElapsed = bufferingDuration >= this.minBufferingTimeoutMs;
- const bufferHealthy =
- currentBufferDuration >= HEALTHY_BUFFER_THRESHOLD_SECONDS;
-
- if (minTimeElapsed && bufferHealthy) {
- Internals.Log.trace(
- {logLevel: this.logLevel, tag: '@remotion/media'},
- `[MediaPlayer] Resuming from buffering after ${bufferingDuration}ms - buffer recovered`,
- );
- this.setBufferingState(false);
- }
- }
-
private runAudioIterator = async (
startFromSecond: number,
audioIterator: AudioIterator,
@@ -658,32 +616,14 @@ export class MediaPlayer {
if (!this.hasAudio()) return;
try {
- let totalBufferDuration = 0;
let isFirstBuffer = true;
- audioIterator.setAudioIteratorStarted(true);
while (true) {
if (audioIterator.isDestroyed()) {
return;
}
- const BUFFERING_TIMEOUT_MS = 50;
-
- let result: IteratorResult<WrappedAudioBuffer, void>;
- try {
- result = await withTimeout(
- audioIterator.getNext(),
- BUFFERING_TIMEOUT_MS,
- 'Iterator timeout',
- );
- } catch (error) {
- if (error instanceof TimeoutError && !this.mediaEnded) {
- this.setBufferingState(true);
- }
-
- await sleep(10);
- continue;
- }
+ const result = await audioIterator.getNext();
// media has ended
if (result.done || !result.value) {
@@ -691,15 +631,7 @@ export class MediaPlayer {
break;
}
- const {buffer, timestamp, duration} = result.value;
-
- totalBufferDuration += duration;
-
- audioIterator.setAudioBufferHealth(
- Math.max(0, totalBufferDuration / this.playbackRate),
- );
-
- this.maybeResumeFromBuffering(totalBufferDuration / this.playbackRate);
+ const {buffer, timestamp} = result.value;
if (this.playing) {
if (isFirstBuffer) {
diff --git a/packages/media/src/video/timeout-utils.ts b/packages/media/src/video/timeout-utils.ts
deleted file mode 100644
index 126141a587..0000000000
--- a/packages/media/src/video/timeout-utils.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-/* eslint-disable no-promise-executor-return */
-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,
- errorMessage: string = 'Operation timed out',
-): Promise<T> {
- let timeoutId: number | null = null;
-
- const timeoutPromise = new Promise<never>((_, reject) => {
- timeoutId = window.setTimeout(() => {
- reject(new TimeoutError(errorMessage));
- }, timeoutMs);
- });
-
- return Promise.race([
- promise.finally(() => {
- if (timeoutId) {
- clearTimeout(timeoutId);
- }
- }),
- timeoutPromise,
- ]);
-}