Skip to content

🎥 Make videos programmatically with React

fix(media): poll for AudioContext contextTime in tests instead of fixed 50ms wait

pullfrog[bot]a153fac0d386
diff --git a/packages/media/src/test/audio-iterator.test.ts b/packages/media/src/test/audio-iterator.test.ts
index 236a28ab37..04d5f60b4f 100644
--- a/packages/media/src/test/audio-iterator.test.ts
+++ b/packages/media/src/test/audio-iterator.test.ts
@@ -4,6 +4,26 @@ import {expect, test} from 'vitest';
import {audioIteratorManager} from '../audio-iterator-manager';
import {makeNonceManager} from '../nonce-manager';
+const waitForContextTime = async (audioContext: AudioContext) => {
+ await audioContext.resume();
+ const start = Date.now();
+ await new Promise<void>((resolve, reject) => {
+ const check = () => {
+ if ((audioContext.getOutputTimestamp().contextTime ?? 0) > 0) {
+ resolve();
+ } else if (Date.now() - start > 5000) {
+ reject(
+ new Error('AudioContext contextTime did not advance above 0'),
+ );
+ } else {
+ setTimeout(check, 10);
+ }
+ };
+
+ check();
+ });
+};
+
const prepare = async () => {
const input = new Input({
source: new UrlSource('https://remotion.media/video.mp4'),
@@ -15,11 +35,7 @@ const prepare = async () => {
}
const audioContext = new AudioContext();
- await audioContext.resume();
- // Wait for contextTime to advance above 0
- await new Promise((r) => {
- setTimeout(r, 50);
- });
+ await waitForContextTime(audioContext);
const manager = audioIteratorManager({
audioTrack,
@@ -202,11 +218,7 @@ test('should not schedule duplicate chunks with playbackRate=0.5', async () => {
}
const audioContext = new AudioContext();
- await audioContext.resume();
- // Wait for contextTime to advance above 0
- await new Promise((r) => {
- setTimeout(r, 50);
- });
+ await waitForContextTime(audioContext);
const manager = audioIteratorManager({
audioTrack,
@@ -276,11 +288,7 @@ test('should not decode + schedule audio chunks beyond the end time', async () =
}
const audioContext = new AudioContext();
- await audioContext.resume();
- // Wait for contextTime to advance above 0
- await new Promise((r) => {
- setTimeout(r, 50);
- });
+ await waitForContextTime(audioContext);
const manager = audioIteratorManager({
audioTrack,