🎥 Make videos programmatically with React
`@remotion/media`: Count silent ranges as buffered (#10659)
Jonny Burgerd8c9bcd8b5d9
diff --git a/packages/media/src/audio-iterator-manager.ts b/packages/media/src/audio-iterator-manager.tsindex d599d996fe..95f7f147f6 100644--- a/packages/media/src/audio-iterator-manager.ts+++ b/packages/media/src/audio-iterator-manager.ts@@ -283,7 +283,10 @@ export const audioIteratorManager = ({) => number | null;playbackRate: number;scheduleAudioNode: ScheduleAudioNode;- onScheduled: (sourceDurationInSeconds: number) => void;+ onScheduled: (+ sourceDurationInSeconds: number,+ timelineTimestamp: number,+ ) => void;onDone: () => void;onDestroyed: () => void;logLevel: LogLevel;@@ -331,7 +334,10 @@ export const audioIteratorManager = ({return;}- onScheduled(result.value.sourceDurationInSeconds);+ onScheduled(+ result.value.sourceDurationInSeconds,+ result.value.timelineTimestamp,+ );notifyNodeScheduled();onAudioChunk({@@ -443,7 +449,7 @@ export const audioIteratorManager = ({audioIteratorsCreated++;audioBufferIterator = iterator;- let bufferedDuration = 0;+ let bufferedUntil = startFromSecond;let hasUnblockedPlayback = false;const unblockPlayback = () => {if (hasUnblockedPlayback) {@@ -460,12 +466,17 @@ export const audioIteratorManager = ({getTargetTime,playbackRate,scheduleAudioNode,- onScheduled: (sourceDurationInSeconds) => {- bufferedDuration += sourceDurationInSeconds;+ onScheduled: (sourceDurationInSeconds, timelineTimestamp) => {+ bufferedUntil = Math.max(+ bufferedUntil,+ timelineTimestamp + sourceDurationInSeconds,+ );+ const bufferedDuration = bufferedUntil - startFromSecond;// Need to schedule a bit into the future to unblock the buffer state,// otherwise we might be scheduling too late. This must be based on- // duration, not chunk count, because large PCM chunks can exceed the- // scheduling horizon before enough chunks are queued:+ // timeline coverage, not audible duration or chunk count: silence is+ // already safe to play, and large PCM chunks can exceed the scheduling+ // horizon before enough chunks are queued:// https://github.com/remotion-dev/remotion/issues/9394if (hasEnoughAudioToStartPlayback(bufferedDuration)) {unblockPlayback();@@ -588,12 +599,19 @@ export const audioIteratorManager = ({}const currentIteratorTimestamp = audioBufferIterator.guessNextTimestamp();+ const iteratorHasAdvancedThroughSilence =+ loop &&+ currentAnchor !== null &&+ unloopedNewTime >= currentAnchor.unloopedStartInSeconds &&+ currentIteratorTimestamp >= timeToCheck;if (- currentIteratorTimestamp < timeToCheck &&- Math.abs(currentIteratorTimestamp - timeToCheck) < 1+ iteratorHasAdvancedThroughSilence ||+ (currentIteratorTimestamp < timeToCheck &&+ Math.abs(currentIteratorTimestamp - timeToCheck) < 1)) {processNext();- // iterator is less than 1 second behind, we will just let it run+ // The iterator has either advanced beyond the current time, meaning+ // the gap is known silence, or is less than 1 second behind. Let it run.return;}}diff --git a/packages/media/src/test/audio-iterator.test.ts b/packages/media/src/test/audio-iterator.test.tsindex b99e70d314..85946c56cf 100644--- a/packages/media/src/test/audio-iterator.test.ts+++ b/packages/media/src/test/audio-iterator.test.ts@@ -186,6 +186,33 @@ test('PCM chunks unblock playback based on duration before six chunks or EOF', amanager.destroyIterator();});+// https://github.com/remotion-dev/remotion/issues/10658+test('silence before the next loop pass counts toward startup buffering', async () => {+ const {+ manager,+ seek,+ scheduledChunks,+ audioContextCurrentTime,+ getPlaybackUnblocks,+ } = await prepare({+ src: 'https://remotion.media/audio-shorter-than-video.mp4',+ mediaEndTimestamp: 10,+ sequenceDurationInSeconds: 12,+ loop: true,+ });++ audioContextCurrentTime.current = 8;+ seek({time: 8});+ await manager.waitForNScheduledNodes(1);++ expect(scheduledChunks[0]).toBeCloseTo(10);+ expect(getPlaybackUnblocks()).toBe(1);++ seek({time: 8 + 1 / 30});+ expect(manager.getAudioIteratorsCreated()).toBe(1);+ manager.destroyIterator();+});+test('media player should work', async () => {const {manager, scheduledChunks, seek, audioContextCurrentTime} =await prepare();