Skip to content

🎥 Make videos programmatically with React

remove complexity

Jonny Burgerdb3c78d89395
diff --git a/packages/example/src/NewVideo.tsx b/packages/example/src/NewVideo.tsx
index b0b236152f..d385c4d633 100644
--- a/packages/example/src/NewVideo.tsx
+++ b/packages/example/src/NewVideo.tsx
@@ -19,7 +19,7 @@ export const calculateMetadataFn: CalculateMetadataFunction<
};
export const Component = () => {
- return <Video src={src} />;
+ return <Video src={src} debugAudioScheduling />;
};
export const NewVideoComp = () => {
diff --git a/packages/media/src/audio-iterator-manager.ts b/packages/media/src/audio-iterator-manager.ts
index 8f1cccb4a8..43b8fcca95 100644
--- a/packages/media/src/audio-iterator-manager.ts
+++ b/packages/media/src/audio-iterator-manager.ts
@@ -13,8 +13,6 @@ import {makePrewarmedAudioIteratorCache} from './prewarm-iterator-for-looping';
import {ALLOWED_GLOBAL_TIME_ANCHOR_SHIFT} from './set-global-time-anchor';
import type {SharedAudioContextForMediaPlayer} from './shared-audio-context-for-media-player';
-const MAX_BUFFER_AHEAD_SECONDS = 2;
-
type ScheduleAudioNode = (
node: AudioBufferSourceNode,
mediaTimestamp: number,
@@ -437,61 +435,16 @@ export const audioIteratorManager = ({
);
if (!currentTimeIsAlreadyQueued) {
- const audioSatisfyResult = await audioBufferIterator.tryToSatisfySeek(
- newTime,
- (buffer) => {
- if (!nonce.isStale()) {
- onAudioChunk({
- getIsPlaying,
- buffer,
- playbackRate,
- scheduleAudioNode,
- debugAudioScheduling,
- });
- }
- },
- );
-
- if (nonce.isStale()) {
- return;
- }
-
- if (audioSatisfyResult.type === 'ended') {
- return;
- }
-
- if (audioSatisfyResult.type === 'not-satisfied') {
- await startAudioIterator({
- nonce,
- playbackRate,
- startFromSecond: newTime,
- getIsPlaying,
- scheduleAudioNode,
- debugAudioScheduling,
- getTargetTime,
- });
- return;
- }
-
- if (audioSatisfyResult.type === 'satisfied') {
- // fall through
- }
+ startAudioIterator({
+ nonce,
+ playbackRate,
+ startFromSecond: newTime,
+ getIsPlaying,
+ scheduleAudioNode,
+ debugAudioScheduling,
+ getTargetTime,
+ });
}
-
- await audioBufferIterator.bufferAsFarAsPossible(
- (buffer) => {
- if (!nonce.isStale()) {
- onAudioChunk({
- getIsPlaying,
- buffer,
- playbackRate,
- scheduleAudioNode,
- debugAudioScheduling,
- });
- }
- },
- Math.min(newTime + MAX_BUFFER_AHEAD_SECONDS, getEndTime()),
- );
};
return {
diff --git a/packages/media/src/audio/audio-preview-iterator.ts b/packages/media/src/audio/audio-preview-iterator.ts
index 504fef0853..adc09de954 100644
--- a/packages/media/src/audio/audio-preview-iterator.ts
+++ b/packages/media/src/audio/audio-preview-iterator.ts
@@ -1,6 +1,4 @@
-import type {WrappedAudioBuffer} from 'mediabunny';
import {Internals} from 'remotion';
-import {roundTo4Digits} from '../helpers/round-to-4-digits';
import type {PrewarmedAudioIteratorCache} from '../prewarm-iterator-for-looping';
import {ALLOWED_GLOBAL_TIME_ANCHOR_SHIFT} from '../set-global-time-anchor';
import type {SharedAudioContextForMediaPlayer} from '../shared-audio-context-for-media-player';
@@ -43,8 +41,6 @@ export const makeAudioIterator = ({
timestamp: number;
}[] = [];
let mostRecentTimestamp = -Infinity;
- let pendingNext: Promise<IteratorResult<WrappedAudioBuffer, void>> | null =
- null;
const cleanupAudioQueue = (
audioContext: SharedAudioContextForMediaPlayer,
@@ -86,153 +82,6 @@ export const makeAudioIterator = ({
queuedAudioNodes.length = 0;
};
- const getNextOrNullIfNotAvailable = async () => {
- let next = pendingNext;
-
- if (!next) {
- next = iterator.next();
- }
-
- pendingNext = null;
- const result = await Promise.race([
- next,
- new Promise<void>((resolve) => {
- Promise.resolve().then(() => resolve());
- }),
- ]);
-
- if (!result) {
- pendingNext = next;
- return {
- type: 'need-to-wait-for-it' as const,
- waitPromise: async () => {
- const res = await next;
- return res.value;
- },
- };
- }
-
- if (result.value) {
- mostRecentTimestamp = Math.max(
- mostRecentTimestamp,
- result.value.timestamp + result.value.duration,
- );
- // preload next already
- pendingNext = iterator.next();
- return {
- type: 'got-buffer' as const,
- buffer: result.value,
- };
- }
-
- return {
- type: 'got-end' as const,
- mostRecentTimestamp,
- };
- };
-
- const tryToSatisfySeek = async (
- time: number,
- onBufferScheduled: (buffer: WrappedAudioBuffer) => void,
- ): Promise<
- | {
- type: 'not-satisfied';
- reason: string;
- }
- | {
- type: 'ended';
- }
- | {
- type: 'satisfied';
- }
- > => {
- if (time < startFromSecond) {
- return {
- type: 'not-satisfied' as const,
- reason: `time requested is before the start of the iterator`,
- };
- }
-
- while (true) {
- const buffer = await getNextOrNullIfNotAvailable();
- if (buffer.type === 'need-to-wait-for-it') {
- return {
- type: 'not-satisfied' as const,
- reason: 'iterator did not have buffer ready',
- };
- }
-
- if (buffer.type === 'got-end') {
- if (time >= mostRecentTimestamp) {
- return {
- type: 'ended' as const,
- };
- }
-
- return {
- type: 'not-satisfied' as const,
- reason: `iterator ended before the requested time`,
- };
- }
-
- if (buffer.type === 'got-buffer') {
- const bufferTimestamp = roundTo4Digits(buffer.buffer.timestamp);
- const bufferEndTimestamp = roundTo4Digits(
- buffer.buffer.timestamp + buffer.buffer.duration,
- );
-
- const timestamp = roundTo4Digits(time);
-
- if (timestamp < bufferTimestamp) {
- return {
- type: 'not-satisfied' as const,
- reason: `iterator is too far, most recently returned ${bufferTimestamp}-${bufferEndTimestamp}, requested ${timestamp}`,
- };
- }
-
- if (bufferTimestamp <= timestamp && bufferEndTimestamp >= timestamp) {
- onBufferScheduled(buffer.buffer);
- return {
- type: 'satisfied' as const,
- };
- }
-
- onBufferScheduled(buffer.buffer);
-
- continue;
- }
-
- throw new Error('Unreachable');
- }
- };
-
- const bufferAsFarAsPossible = async (
- onBufferScheduled: (buffer: WrappedAudioBuffer) => void,
- maxTimestamp: number,
- ): Promise<{type: 'ended'} | {type: 'waiting'} | {type: 'max-reached'}> => {
- while (true) {
- if (mostRecentTimestamp >= maxTimestamp) {
- return {type: 'max-reached'};
- }
-
- const buffer = await getNextOrNullIfNotAvailable();
- if (buffer.type === 'need-to-wait-for-it') {
- return {type: 'waiting'};
- }
-
- if (buffer.type === 'got-end') {
- return {type: 'ended'};
- }
-
- if (buffer.type === 'got-buffer') {
- onBufferScheduled(buffer.buffer);
- continue;
- }
-
- throw new Error('Unreachable');
- }
- };
-
const removeAndReturnAllQueuedAudioNodes = () => {
const nodes = queuedAudioNodes.slice();
for (const node of nodes) {
@@ -361,8 +210,6 @@ export const makeAudioIterator = ({
until,
};
},
- tryToSatisfySeek,
- bufferAsFarAsPossible,
addChunkForAfterResuming,
moveQueuedChunksToPauseQueue,
getNumberOfChunksAfterResuming,