🎥 Make videos programmatically with React
get rid of destroy logic!
Jonny Burger48b985457b13
diff --git a/packages/media/src/audio-iterator-manager.ts b/packages/media/src/audio-iterator-manager.tsindex 0d5cfad727..b4691f4e2d 100644--- a/packages/media/src/audio-iterator-manager.ts+++ b/packages/media/src/audio-iterator-manager.ts@@ -310,7 +310,7 @@ export const audioIteratorManager = ({return;}- audioBufferIterator?.destroy(sharedAudioContext);+ audioBufferIterator?.destroy();const delayHandle = delayPlaybackHandleIfNotPremounting();currentDelayHandle = delayHandle;@@ -419,7 +419,7 @@ export const audioIteratorManager = ({startAudioIterator,getAudioBufferIterator: () => audioBufferIterator,destroyIterator: () => {- audioBufferIterator?.destroy(sharedAudioContext);+ audioBufferIterator?.destroy();audioBufferIterator = null;if (currentDelayHandle) {diff --git a/packages/media/src/audio/audio-preview-iterator.ts b/packages/media/src/audio/audio-preview-iterator.tsindex cea6fadc5a..dcd5cbbbdd 100644--- a/packages/media/src/audio/audio-preview-iterator.ts+++ b/packages/media/src/audio/audio-preview-iterator.ts@@ -1,8 +1,6 @@import type {AudioBufferSink} from 'mediabunny';import type {LogLevel} from 'remotion';-import {Internals} from 'remotion';import {makeIteratorWithPriming} from '../make-iterator-with-priming';-import type {SharedAudioContextForMediaPlayer} from '../shared-audio-context-for-media-player';export const HEALTHY_BUFFER_THRESHOLD_SECONDS = 1;export const ALLOWED_GLOBAL_TIME_ANCHOR_SHIFT = 0.1;@@ -24,7 +22,6 @@ export type QueuedPeriod = {export const makeAudioIterator = ({startFromSecond,maximumTimestamp,- logLevel,audioSink,}: {startFromSecond: number;@@ -41,36 +38,9 @@ export const makeAudioIterator = ({const queuedAudioNodes: QueuedNode[] = [];let mostRecentTimestamp = -Infinity;- const cleanupAudioQueue = (- audioContext: SharedAudioContextForMediaPlayer,- ) => {+ const cleanupAudioQueue = () => {for (const node of queuedAudioNodes) {try {- // When we unmount at the end of playback, we might not yet be done with audio anchors- // we should not stop the nodes- const isAlreadyPlaying =- node.scheduledTime - ALLOWED_GLOBAL_TIME_ANCHOR_SHIFT <- audioContext.audioContext.currentTime!;-- // except for when the audio anchor changed (e.g. through a seek)- const wasScheduledForThisAnchor =- node.scheduledAtAnchor === audioContext.audioSyncAnchor.value;-- if (isAlreadyPlaying && wasScheduledForThisAnchor) {- console.log('not cleaning up!');- continue;- }-- const currentlyHearing =- audioContext.audioContext.getOutputTimestamp().contextTime!;- const nodeEndTime =- node.scheduledTime + node.buffer.duration / node.playbackRate;-- Internals.Log.verbose(- {logLevel, tag: 'audio-scheduling'},- `Stopping node ${node.timestamp.toFixed(3)}, currently hearing = ${currentlyHearing.toFixed(3)} currentTime = ${audioContext.audioContext.currentTime.toFixed(3)} nodeEndTime = ${nodeEndTime.toFixed(3)} scheduledTime = ${node.scheduledTime.toFixed(3)}`,- );-node.node.stop();} catch {// Node may not have been started@@ -94,8 +64,8 @@ export const makeAudioIterator = ({};return {- destroy: (audioContext: SharedAudioContextForMediaPlayer) => {- cleanupAudioQueue(audioContext);+ destroy: () => {+ cleanupAudioQueue();destroyed = true;iterator.return().catch(() => undefined);},diff --git a/packages/media/src/test/audio-cleanup-on-seek.test.ts b/packages/media/src/test/audio-cleanup-on-seek.test.tsindex 17c1a70617..37f713a895 100644--- a/packages/media/src/test/audio-cleanup-on-seek.test.ts+++ b/packages/media/src/test/audio-cleanup-on-seek.test.ts@@ -1,7 +1,6 @@import {ALL_FORMATS, AudioBufferSink, Input, UrlSource} from 'mediabunny';import {expect, test} from 'vitest';import {makeAudioIterator} from '../audio/audio-preview-iterator';-import type {SharedAudioContextForMediaPlayer} from '../shared-audio-context-for-media-player';const makeCache = async () => {const input = new Input({@@ -38,23 +37,6 @@ const makeMockBuffer = (duration: number) => {} as unknown as AudioBuffer;};-const makeMockSharedAudioContext = ({- currentTime,- anchorValue,-}: {- currentTime: number;- anchorValue: number;-}): SharedAudioContextForMediaPlayer => {- return {- audioContext: {- currentTime,- getOutputTimestamp: () => ({contextTime: currentTime}),- } as unknown as AudioContext,- audioSyncAnchor: {value: anchorValue},- scheduleAudioNode: () => ({type: 'started', scheduledTime: 0}),- };-};-test('destroy should NOT stop nodes that are already playing with the same anchor', async () => {const audioSink = await makeCache();const iterator = makeAudioIterator({@@ -86,9 +68,7 @@ test('destroy should NOT stop nodes that are already playing with the same ancho});// Destroy with same anchor (0) and currentTime well past scheduledTime- iterator.destroy(- makeMockSharedAudioContext({currentTime: 1.0, anchorValue: 0}),- );+ iterator.destroy();// Nodes should NOT have been stopped because they are already playing// and were scheduled for the current anchor@@ -129,9 +109,7 @@ test('destroy should stop nodes when the audio anchor changed (seek to different// Destroy with a DIFFERENT anchor (simulating a seek happened)// Even though nodes are "already playing" (currentTime > scheduledTime),// they should be stopped because the anchor changed- iterator.destroy(- makeMockSharedAudioContext({currentTime: 1.0, anchorValue: 1}),- );+ iterator.destroy();// Nodes SHOULD be stopped because the anchor changed (seek happened)expect(mock1.wasStopped()).toBe(true);