🎥 Make videos programmatically with React
`@remotion/media`: Fix rapid block/unblock cycles during video seek
Igor Samokhovets82432e18a74a
diff --git a/packages/media/src/test/video-iterator-manager.test.ts b/packages/media/src/test/video-iterator-manager.test.tsnew file mode 100644index 0000000000..4e09341e19--- /dev/null+++ b/packages/media/src/test/video-iterator-manager.test.ts@@ -0,0 +1,102 @@+import {ALL_FORMATS, Input, UrlSource} from 'mediabunny';+import {expect, test} from 'vitest';+import {makeNonceManager} from '../nonce-manager';+import {videoIteratorManager} from '../video-iterator-manager';++const prepare = async () => {+ const input = new Input({+ source: new UrlSource('https://remotion.media/video.mp4'),+ formats: ALL_FORMATS,+ });+ const videoTrack = await input.getPrimaryVideoTrack();+ if (!videoTrack) {+ throw new Error('No video track found');+ }++ return {videoTrack};+};++test('seek should not cause overlapping block/unblock cycles', async () => {+ const {videoTrack} = await prepare();++ let activeBlocks = 0;+ let maxConcurrentBlocks = 0;++ const manager = videoIteratorManager({+ videoTrack,+ delayPlaybackHandleIfNotPremounting: () => {+ activeBlocks++;+ maxConcurrentBlocks = Math.max(maxConcurrentBlocks, activeBlocks);+ return {+ unblock: () => {+ activeBlocks--;+ },+ };+ },+ context: null,+ canvas: null,+ getOnVideoFrameCallback: () => null,+ logLevel: 'error',+ drawDebugOverlay: () => {},+ });++ const nonceManager = makeNonceManager();++ // Initialize the iterator first+ await manager.startVideoIterator(0, nonceManager.createAsyncOperation());++ // Perform seeks that will trigger 'not-satisfied' (jumping around)+ // These should NOT cause overlapping blocks+ await manager.seek({newTime: 5, nonce: nonceManager.createAsyncOperation()});+ await manager.seek({newTime: 0, nonce: nonceManager.createAsyncOperation()});+ await manager.seek({newTime: 8, nonce: nonceManager.createAsyncOperation()});++ // With the fix, max concurrent blocks should be 1+ // Before the fix (fire-and-forget), it could be > 1+ expect(maxConcurrentBlocks).toBe(1);+ expect(activeBlocks).toBe(0); // All blocks should be released+});++test('rapid sequential seeks should not cause overlapping blocks', async () => {+ const {videoTrack} = await prepare();++ let activeBlocks = 0;+ let maxConcurrentBlocks = 0;++ const manager = videoIteratorManager({+ videoTrack,+ delayPlaybackHandleIfNotPremounting: () => {+ activeBlocks++;+ maxConcurrentBlocks = Math.max(maxConcurrentBlocks, activeBlocks);+ return {+ unblock: () => {+ activeBlocks--;+ },+ };+ },+ context: null,+ canvas: null,+ getOnVideoFrameCallback: () => null,+ logLevel: 'error',+ drawDebugOverlay: () => {},+ });++ const nonceManager = makeNonceManager();++ // Initialize the iterator first+ await manager.startVideoIterator(0, nonceManager.createAsyncOperation());++ // Perform many rapid seeks - simulating scrubbing through video+ for (let i = 0; i < 10; i++) {+ // Alternate between distant positions to force iterator recreation+ const time = i % 2 === 0 ? i * 0.5 : 9 - i * 0.5;+ await manager.seek({+ newTime: time,+ nonce: nonceManager.createAsyncOperation(),+ });+ }++ // With the fix, max concurrent blocks should be 1+ expect(maxConcurrentBlocks).toBe(1);+ expect(activeBlocks).toBe(0);+});diff --git a/packages/media/src/video-iterator-manager.ts b/packages/media/src/video-iterator-manager.tsindex de05bf4838..70e5ee6eb7 100644--- a/packages/media/src/video-iterator-manager.ts+++ b/packages/media/src/video-iterator-manager.ts@@ -60,7 +60,7 @@ export const videoIteratorManager = ({);};- const startVideoIterator = async (+ const startVideoIteratorWithoutDelayPlayback = async (timeToSeek: number,nonce: Nonce,): Promise<void> => {@@ -69,9 +69,7 @@ export const videoIteratorManager = ({videoIteratorsCreated++;videoFrameIterator = iterator;- const delayHandle = delayPlaybackHandleIfNotPremounting();const frameResult = await iterator.getNext();- delayHandle.unblock();if (iterator.isDestroyed()) {return;@@ -93,6 +91,18 @@ export const videoIteratorManager = ({drawFrame(frameResult.value);};+ const startVideoIterator = async (+ timeToSeek: number,+ nonce: Nonce,+ ): Promise<void> => {+ const delayHandle = delayPlaybackHandleIfNotPremounting();+ try {+ await startVideoIteratorWithoutDelayPlayback(timeToSeek, nonce);+ } finally {+ delayHandle.unblock();+ }+ };+const seek = async ({newTime, nonce}: {newTime: number; nonce: Nonce}) => {if (!videoFrameIterator) {return;@@ -114,10 +124,14 @@ export const videoIteratorManager = ({return;}- // Intentionally not awaited, letting audio start as well- startVideoIterator(newTime, nonce).catch(() => {- // Ignore errors, might be stale or disposed- });+ // Block at the seek level, not inside startVideoIterator+ // This ensures only one block/unblock cycle per seek operation+ const delayHandle = delayPlaybackHandleIfNotPremounting();+ try {+ await startVideoIteratorWithoutDelayPlayback(newTime, nonce);+ } finally {+ delayHandle.unblock();+ }};return {