Skip to content

🎥 Make videos programmatically with React

fix tests

JonnyBurger02fbc6bf6d4d
diff --git a/packages/media/src/audio-iterator-manager.ts b/packages/media/src/audio-iterator-manager.ts
index 9fc2e65f24..467a454a9a 100644
--- a/packages/media/src/audio-iterator-manager.ts
+++ b/packages/media/src/audio-iterator-manager.ts
@@ -33,14 +33,12 @@ export const audioIteratorManager = ({
audioTrack,
delayPlaybackHandleIfNotPremounting,
sharedAudioContext,
- getIsLooping,
getSequenceEndTimestamp,
getSequenceDurationInSeconds,
getMediaEndTimestamp,
getStartTime,
initialMuted,
drawDebugOverlay,
- initialTime,
initialPlaybackRate,
initialTrimBefore,
initialTrimAfter,
@@ -52,14 +50,12 @@ export const audioIteratorManager = ({
audioTrack: InputAudioTrack;
delayPlaybackHandleIfNotPremounting: () => DelayPlaybackIfNotPremounting;
sharedAudioContext: SharedAudioContextForMediaPlayer;
- getIsLooping: () => boolean;
getSequenceEndTimestamp: () => number;
getSequenceDurationInSeconds: () => number;
getMediaEndTimestamp: () => number;
getStartTime: () => number;
initialMuted: boolean;
drawDebugOverlay: () => void;
- initialTime: number;
initialPlaybackRate: number;
initialTrimBefore: number | undefined;
initialTrimAfter: number | undefined;
@@ -71,7 +67,8 @@ export const audioIteratorManager = ({
let muted = initialMuted;
let currentVolume = 1;
let currentSeek = {
- time: initialTime,
+ // do not prevent first seek
+ time: -1,
playbackRate: initialPlaybackRate,
trimBefore: initialTrimBefore,
trimAfter: initialTrimAfter,
@@ -81,14 +78,6 @@ export const audioIteratorManager = ({
fps: initialFps,
};
- // TODO: do something with looping
- const _looping = getIsLooping();
- Internals.Log.trace(
- {logLevel: 'info', tag: 'audio-iterator-manager'},
- 'looping: %s',
- _looping,
- );
-
const gainNode = sharedAudioContext.audioContext.createGain();
gainNode.connect(sharedAudioContext.gainNode);
@@ -242,6 +231,7 @@ export const audioIteratorManager = ({
onDone,
logLevel,
currentTime,
+ getAudioContextCurrentTimeMockedInTest,
}: {
iterator: AudioIterator;
nonce: Nonce;
@@ -256,6 +246,7 @@ export const audioIteratorManager = ({
onDestroyed: () => void;
logLevel: LogLevel;
currentTime: number;
+ getAudioContextCurrentTimeMockedInTest: () => number;
}) => {
waitForTurn({
getPriority: () => {
@@ -277,7 +268,7 @@ export const audioIteratorManager = ({
sequenceStartTime: getStartTime(),
});
- return scheduledTime - sharedAudioContext.audioContext.currentTime;
+ return scheduledTime - getAudioContextCurrentTimeMockedInTest();
},
fn: () => iterator.getNextFn(),
onDone: (result, next) => {
@@ -317,6 +308,7 @@ export const audioIteratorManager = ({
onDone,
logLevel,
currentTime,
+ getAudioContextCurrentTimeMockedInTest,
});
next();
},
@@ -346,6 +338,7 @@ export const audioIteratorManager = ({
logLevel,
loop,
unscheduleAudioNode,
+ getAudioContextCurrentTimeMockedInTest,
}: {
startFromSecond: number;
nonce: Nonce;
@@ -358,6 +351,7 @@ export const audioIteratorManager = ({
logLevel: LogLevel;
loop: boolean;
unscheduleAudioNode: (node: AudioBufferSourceNode) => void;
+ getAudioContextCurrentTimeMockedInTest: () => number;
}) => {
if (muted) {
return;
@@ -402,6 +396,7 @@ export const audioIteratorManager = ({
},
logLevel,
currentTime: sharedAudioContext.audioContext.currentTime,
+ getAudioContextCurrentTimeMockedInTest,
});
};
@@ -418,6 +413,7 @@ export const audioIteratorManager = ({
sequenceOffset,
sequenceDurationInFrames,
fps,
+ getAudioContextCurrentTimeMockedInTest,
}: {
newTime: number;
nonce: Nonce;
@@ -434,6 +430,7 @@ export const audioIteratorManager = ({
sequenceOffset: number;
sequenceDurationInFrames: number;
fps: number;
+ getAudioContextCurrentTimeMockedInTest: () => number;
}) => {
if (
currentSeek.time === newTime &&
@@ -507,6 +504,7 @@ export const audioIteratorManager = ({
logLevel,
loop,
unscheduleAudioNode: sharedAudioContext.unscheduleAudioNode,
+ getAudioContextCurrentTimeMockedInTest,
});
// Not further scheduling, initial iterator is already running
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index f843ff66fd..671382f654 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -311,13 +311,11 @@ export class MediaPlayer {
delayPlaybackHandleIfNotPremounting:
this.delayPlaybackHandleIfNotPremounting,
sharedAudioContext: this.sharedAudioContext,
- getIsLooping: () => this.loop,
getMediaEndTimestamp: () => this.getMediaEndTimestamp(),
getSequenceEndTimestamp: () => this.getSequenceEndTimestamp(),
getStartTime: () => this.getStartTime(),
initialMuted,
drawDebugOverlay: this.drawDebugOverlay,
- initialTime: startTime,
initialPlaybackRate: this.playbackRate * this.globalPlaybackRate,
getSequenceDurationInSeconds: () =>
this.getSequenceDurationInSeconds(),
@@ -345,6 +343,8 @@ export class MediaPlayer {
loop: this.loop,
unscheduleAudioNode:
this.sharedAudioContext!.unscheduleAudioNode,
+ getAudioContextCurrentTimeMockedInTest: () =>
+ this.sharedAudioContext!.audioContext.currentTime,
})
: Promise.resolve(),
this.videoIteratorManager
@@ -430,6 +430,8 @@ export class MediaPlayer {
sequenceDurationInFrames: this.sequenceDurationInFrames,
fps: this.fps,
scheduleAudioNode: this.scheduleAudioNode,
+ getAudioContextCurrentTimeMockedInTest: () =>
+ this.sharedAudioContext!.audioContext.currentTime,
}),
]);
} catch (error) {
diff --git a/packages/media/src/test/audio-cleanup-on-seek.test.ts b/packages/media/src/test/audio-cleanup-on-seek.test.ts
index 4328cac002..a65959bbfc 100644
--- a/packages/media/src/test/audio-cleanup-on-seek.test.ts
+++ b/packages/media/src/test/audio-cleanup-on-seek.test.ts
@@ -37,47 +37,6 @@ const makeMockBuffer = (duration: number) => {
} as unknown as AudioBuffer;
};
-test('destroy should NOT stop nodes that are already playing with the same anchor', async () => {
- const audioSink = await makeCache();
- const iterator = makeAudioIterator({
- startFromSecond: 0,
- maximumTimestamp: Infinity,
- audioSink,
- logLevel: 'info',
- loop: false,
- playbackRate: 1,
- sequenceDurationInSeconds: 10,
- unscheduleAudioNode: () => {},
- });
-
- const mock1 = makeMockNode();
- const mock2 = makeMockNode();
-
- // Add nodes scheduled at anchor 0, with scheduledTime in the past
- iterator.addQueuedAudioNode({
- node: mock1.node,
- timestamp: 0,
- buffer: makeMockBuffer(0.021),
- scheduledTime: 0.1, // in the past relative to currentTime=1.0
- scheduledAtAnchor: 0,
- });
- iterator.addQueuedAudioNode({
- node: mock2.node,
- timestamp: 0.021,
- buffer: makeMockBuffer(0.021),
- scheduledTime: 0.121,
- scheduledAtAnchor: 0,
- });
-
- // Destroy with same anchor (0) and currentTime well past scheduledTime
- iterator.destroy();
-
- // Nodes should NOT have been stopped because they are already playing
- // and were scheduled for the current anchor
- expect(mock1.wasStopped()).toBe(false);
- expect(mock2.wasStopped()).toBe(false);
-});
-
test('destroy should stop nodes when the audio anchor changed (seek to different position)', async () => {
const audioBufferSink = await makeCache();
const iterator = makeAudioIterator({
diff --git a/packages/media/src/test/audio-iterator.test.ts b/packages/media/src/test/audio-iterator.test.ts
index 3205d7a04c..b2b20e0efb 100644
--- a/packages/media/src/test/audio-iterator.test.ts
+++ b/packages/media/src/test/audio-iterator.test.ts
@@ -4,8 +4,18 @@ import {expect, test} from 'vitest';
import {audioIteratorManager} from '../audio-iterator-manager';
import {makeNonceManager} from '../nonce-manager';
-const prepare = async (options?: {fps?: number; playbackRate?: number}) => {
- const {fps = 30, playbackRate = 1} = options ?? {};
+const prepare = async (options?: {
+ fps?: number;
+ playbackRate?: number;
+ mediaEndTimestamp?: number;
+ sequenceEndTimestamp?: number;
+}) => {
+ const {
+ fps = 30,
+ playbackRate = 1,
+ mediaEndTimestamp = Infinity,
+ sequenceEndTimestamp = Infinity,
+ } = options ?? {};
const input = new Input({
source: new UrlSource('https://remotion.media/video.mp4'),
formats: ALL_FORMATS,
@@ -17,6 +27,10 @@ const prepare = async (options?: {fps?: number; playbackRate?: number}) => {
const audioContext = new AudioContext();
+ const audioContextCurrentTime = {
+ current: 0,
+ };
+
const scheduleSharedAudioNode = (): ScheduleAudioNodeResult => {
return {
type: 'started',
@@ -25,6 +39,7 @@ const prepare = async (options?: {fps?: number; playbackRate?: number}) => {
};
const unscheduleAudioNode = () => {};
+ const audioSyncAnchor = {value: 0};
const manager = audioIteratorManager({
audioTrack,
@@ -35,18 +50,16 @@ const prepare = async (options?: {fps?: number; playbackRate?: number}) => {
sharedAudioContext: {
audioContext,
gainNode: audioContext.createGain(),
- audioSyncAnchor: {value: 0},
+ audioSyncAnchor,
scheduleAudioNode: scheduleSharedAudioNode,
unscheduleAudioNode,
},
- getIsLooping: () => false,
- getMediaEndTimestamp: () => Infinity,
- getSequenceEndTimestamp: () => Infinity,
+ getMediaEndTimestamp: () => mediaEndTimestamp,
+ getSequenceEndTimestamp: () => sequenceEndTimestamp,
getSequenceDurationInSeconds: () => 10,
getStartTime: () => 0,
initialMuted: false,
drawDebugOverlay: () => {},
- initialTime: 0,
initialPlaybackRate: 1,
initialTrimBefore: undefined,
initialTrimAfter: undefined,
@@ -91,6 +104,8 @@ const prepare = async (options?: {fps?: number; playbackRate?: number}) => {
sequenceOffset: 0,
sequenceDurationInFrames: 10,
fps,
+ getAudioContextCurrentTimeMockedInTest: () =>
+ audioContextCurrentTime.current,
});
};
@@ -99,13 +114,16 @@ const prepare = async (options?: {fps?: number; playbackRate?: number}) => {
fps,
scheduledChunks,
seek,
+ audioContextCurrentTime,
};
};
test('media player should work', async () => {
- const {manager, scheduledChunks, seek} = await prepare();
+ const {manager, scheduledChunks, seek, audioContextCurrentTime} =
+ await prepare();
- await seek({
+ audioContextCurrentTime.current = 9.96;
+ seek({
time: 9.96,
});
@@ -115,9 +133,10 @@ test('media player should work', async () => {
]);
scheduledChunks.length = 0;
- await seek({
+ seek({
time: 0,
});
+
await manager.waitForNScheduledNodes(3);
expect(scheduledChunks).toEqual([
@@ -125,7 +144,7 @@ test('media player should work', async () => {
]);
scheduledChunks.length = 0;
- await seek({
+ seek({
time: 0.04,
});
@@ -141,14 +160,16 @@ test('media player should work', async () => {
});
test('should not create too many iterators when the audio ends', async () => {
- const {manager, seek, scheduledChunks} = await prepare();
+ const {manager, seek, scheduledChunks, audioContextCurrentTime} =
+ await prepare();
- await seek({
+ audioContextCurrentTime.current = 9.97;
+ seek({
time: 9.97,
});
await manager.waitForNScheduledNodes(0);
- await seek({
+ seek({
time: 9.98,
});
await manager.waitForNScheduledNodes(2);
@@ -160,14 +181,16 @@ test('should not create too many iterators when the audio ends', async () => {
});
test('should not create too many iterators when the audio ends (variant)', async () => {
- const {manager, seek, scheduledChunks} = await prepare();
+ const {manager, seek, scheduledChunks, audioContextCurrentTime} =
+ await prepare();
- await seek({
+ audioContextCurrentTime.current = 9.97;
+ seek({
time: 9.97,
});
await manager.waitForNScheduledNodes(2);
- await seek({
+ seek({
time: 9.98,
});
await manager.waitForNScheduledNodes(0);
@@ -179,9 +202,10 @@ test('should not create too many iterators when the audio ends (variant)', async
});
test('should create more iterators when seeking ', async () => {
- const {manager, seek, scheduledChunks} = await prepare();
+ const {manager, seek, scheduledChunks, audioContextCurrentTime} =
+ await prepare();
- await seek({
+ seek({
time: 0,
});
await manager.waitForNScheduledNodes(6);
@@ -190,7 +214,8 @@ test('should create more iterators when seeking ', async () => {
0.10666666666666667,
]);
scheduledChunks.length = 0;
- await seek({
+ audioContextCurrentTime.current = 2;
+ seek({
time: 2,
});
await manager.waitForNScheduledNodes(6);
@@ -232,7 +257,7 @@ test('should not schedule duplicate chunks with playbackRate=0.5', async () => {
for (let frame = 0; frame < 30; frame++) {
const mediaTime = frame * (1 / fps) * playbackRate;
- await seek({
+ seek({
time: mediaTime,
});
await manager.waitForNScheduledNodes(1);
@@ -247,93 +272,27 @@ test('should not decode + schedule audio chunks beyond the end time', async () =
const endTime = 0.5;
const fps = 30;
- const input = new Input({
- source: new UrlSource('https://remotion.media/video.mp4'),
- formats: ALL_FORMATS,
- });
- const audioTrack = await input.getPrimaryAudioTrack();
- if (!audioTrack) {
- throw new Error('No audio track found');
- }
-
- const audioContext = new AudioContext();
-
- const manager = audioIteratorManager({
- audioTrack,
- delayPlaybackHandleIfNotPremounting: () => ({
- unblock: () => {},
- [Symbol.dispose]: () => {},
- }),
- sharedAudioContext: {
- audioContext,
- gainNode: audioContext.createGain(),
- audioSyncAnchor: {value: 0},
- scheduleAudioNode: () => ({
- type: 'started',
- scheduledTime: 0,
- }),
- unscheduleAudioNode: () => {},
- },
- getIsLooping: () => false,
- getMediaEndTimestamp: () => endTime,
- getSequenceEndTimestamp: () => endTime,
- getSequenceDurationInSeconds: () => 10,
- getStartTime: () => 0,
- initialMuted: false,
- drawDebugOverlay: () => {},
- initialTime: 0,
- initialPlaybackRate: 1,
- initialTrimBefore: undefined,
- initialTrimAfter: undefined,
- initialSequenceOffset: 0,
- initialSequenceDurationInFrames: 10,
- initialLoop: false,
- initialFps: 30,
- });
-
- const scheduledChunks: {
- timestamp: number;
- }[] = [];
- const scheduleAudioNode = (
- _node: AudioBufferSourceNode,
- mediaTimestamp: number,
- ): ScheduleAudioNodeResult => {
- scheduledChunks.push({
- timestamp: mediaTimestamp,
+ const {seek, manager, scheduledChunks, audioContextCurrentTime} =
+ await prepare({
+ fps,
+ mediaEndTimestamp: endTime,
+ sequenceEndTimestamp: endTime,
});
- return {
- type: 'started',
- scheduledTime: mediaTimestamp,
- };
- };
- const prom = manager.waitForNScheduledNodes(24);
+ const prom = manager.waitForNScheduledNodes(23);
// Simulate playback frame by frame, seeking past the end time
for (let frame = 0; frame < 30; frame++) {
const mediaTime = frame / fps;
-
- manager.seek({
- newTime: mediaTime,
- scheduleAudioNode,
- nonce: makeNonceManager().createAsyncOperation(),
- playbackRate: 1,
- getTargetTime: (mediaTimestamp: number) => mediaTimestamp,
- logLevel: 'info',
- loop: false,
- trimBefore: undefined,
- trimAfter: undefined,
- sequenceOffset: 0,
- sequenceDurationInFrames: 10,
- fps,
- });
+ audioContextCurrentTime.current = mediaTime;
+ seek({time: mediaTime});
}
await prom;
- for (const chunk of scheduledChunks) {
- expect(chunk.timestamp).toBeLessThanOrEqual(endTime);
+ for (const timestamp of scheduledChunks) {
+ expect(timestamp).toBeLessThanOrEqual(endTime);
}
- expect(scheduledChunks.length).toBe(24);
+ expect(scheduledChunks.length).toBe(23);
});
diff --git a/packages/media/src/test/video-with-no-frames-in-beginning.test.ts b/packages/media/src/test/video-with-no-frames-in-beginning.test.ts
index bf21ac61df..5d1e816df0 100644
--- a/packages/media/src/test/video-with-no-frames-in-beginning.test.ts
+++ b/packages/media/src/test/video-with-no-frames-in-beginning.test.ts
@@ -102,14 +102,12 @@ test('same goes for audio', async () => {
unscheduleAudioNode: () => {},
};
})(),
- getIsLooping: () => false,
getMediaEndTimestamp: () => Infinity,
getSequenceEndTimestamp: () => Infinity,
getSequenceDurationInSeconds: () => 10,
getStartTime: () => 0,
initialMuted: false,
drawDebugOverlay: () => {},
- initialTime: 0,
initialPlaybackRate: 1,
initialTrimBefore: undefined,
initialTrimAfter: undefined,
@@ -133,6 +131,7 @@ test('same goes for audio', async () => {
logLevel: 'info',
loop: false,
unscheduleAudioNode: () => {},
+ getAudioContextCurrentTimeMockedInTest: () => 0,
});
await manager.waitForNScheduledNodes(2);
@@ -152,6 +151,7 @@ test('same goes for audio', async () => {
sequenceOffset: 0,
sequenceDurationInFrames: 10,
fps: 30,
+ getAudioContextCurrentTimeMockedInTest: () => 0,
});
const iterators = manager.getAudioIteratorsCreated();