Skip to content

🎥 Make videos programmatically with React

don't over-schedule beyond duration.

Jonny Burgerd45ac7de3bbf
diff --git a/packages/media/src/audio-iterator-manager.ts b/packages/media/src/audio-iterator-manager.ts
index 69a1a7cf4e..88ac1b10b3 100644
--- a/packages/media/src/audio-iterator-manager.ts
+++ b/packages/media/src/audio-iterator-manager.ts
@@ -50,6 +50,7 @@ export const audioIteratorManager = ({
mediaTimestamp,
playbackRate,
scheduleAudioNode,
+ maxDuration,
}: {
buffer: AudioBuffer;
mediaTimestamp: number;
@@ -57,7 +58,9 @@ export const audioIteratorManager = ({
scheduleAudioNode: (
node: AudioBufferSourceNode,
mediaTimestamp: number,
+ maxDuration: number | null,
) => void;
+ maxDuration: number | null;
}) => {
if (!audioBufferIterator) {
throw new Error('Audio buffer iterator not found');
@@ -72,11 +75,11 @@ export const audioIteratorManager = ({
node.playbackRate.value = playbackRate;
node.connect(gainNode);
- scheduleAudioNode(node, mediaTimestamp);
+ scheduleAudioNode(node, mediaTimestamp, maxDuration);
const iterator = audioBufferIterator;
- iterator.addQueuedAudioNode(node, mediaTimestamp, buffer);
+ iterator.addQueuedAudioNode(node, mediaTimestamp, buffer, maxDuration);
node.onended = () => {
// Some leniancy is needed as we find that sometimes onended is fired a bit too early
setTimeout(() => {
@@ -97,18 +100,30 @@ export const audioIteratorManager = ({
scheduleAudioNode: (
node: AudioBufferSourceNode,
mediaTimestamp: number,
+ maxDuration: number | null,
) => void;
}) => {
if (muted) {
return;
}
+ const endTime = getEndTime();
+ if (buffer.timestamp >= endTime) {
+ return;
+ }
+
+ const maxDuration =
+ buffer.timestamp + buffer.duration > endTime
+ ? endTime - buffer.timestamp
+ : null;
+
if (getIsPlaying()) {
scheduleAudioChunk({
buffer: buffer.buffer,
mediaTimestamp: buffer.timestamp,
playbackRate,
scheduleAudioNode,
+ maxDuration,
});
} else {
if (!audioBufferIterator) {
@@ -118,6 +133,7 @@ export const audioIteratorManager = ({
audioBufferIterator.addChunkForAfterResuming(
buffer.buffer,
buffer.timestamp,
+ maxDuration,
);
}
@@ -138,6 +154,7 @@ export const audioIteratorManager = ({
scheduleAudioNode: (
node: AudioBufferSourceNode,
mediaTimestamp: number,
+ maxDuration: number | null,
) => void;
}) => {
if (muted) {
@@ -191,7 +208,7 @@ export const audioIteratorManager = ({
scheduleAudioNode,
});
}
- }, startFromSecond + MAX_BUFFER_AHEAD_SECONDS);
+ }, Math.min(startFromSecond + MAX_BUFFER_AHEAD_SECONDS, getEndTime()));
} catch (e) {
if (e instanceof InputDisposedError) {
// iterator was disposed by a newer startAudioIterator call
@@ -225,6 +242,7 @@ export const audioIteratorManager = ({
scheduleAudioNode: (
node: AudioBufferSourceNode,
mediaTimestamp: number,
+ maxDuration: number | null,
) => void;
}) => {
if (muted) {
@@ -302,7 +320,7 @@ export const audioIteratorManager = ({
scheduleAudioNode,
});
}
- }, newTime + MAX_BUFFER_AHEAD_SECONDS);
+ }, Math.min(newTime + MAX_BUFFER_AHEAD_SECONDS, getEndTime()));
};
const resumeScheduledAudioChunks = ({
@@ -313,6 +331,7 @@ export const audioIteratorManager = ({
scheduleAudioNode: (
node: AudioBufferSourceNode,
mediaTimestamp: number,
+ maxDuration: number | null,
) => void;
}) => {
if (muted) {
@@ -329,6 +348,7 @@ export const audioIteratorManager = ({
mediaTimestamp: chunk.timestamp,
playbackRate,
scheduleAudioNode,
+ maxDuration: chunk.maxDuration,
});
}
};
diff --git a/packages/media/src/audio/audio-preview-iterator.ts b/packages/media/src/audio/audio-preview-iterator.ts
index 7c2d62c067..33d23d1ea0 100644
--- a/packages/media/src/audio/audio-preview-iterator.ts
+++ b/packages/media/src/audio/audio-preview-iterator.ts
@@ -8,6 +8,7 @@ export type QueuedNode = {
node: AudioBufferSourceNode;
timestamp: number;
buffer: AudioBuffer;
+ maxDuration: number | null;
};
export const makeAudioIterator = (
@@ -20,6 +21,7 @@ export const makeAudioIterator = (
const audioChunksForAfterResuming: {
buffer: AudioBuffer;
timestamp: number;
+ maxDuration: number | null;
}[] = [];
let mostRecentTimestamp = -Infinity;
let pendingNext: Promise<IteratorResult<WrappedAudioBuffer, void>> | null =
@@ -190,14 +192,18 @@ export const makeAudioIterator = (
return nodes;
};
- const addChunkForAfterResuming = (buffer: AudioBuffer, timestamp: number) => {
- audioChunksForAfterResuming.push({buffer, timestamp});
+ const addChunkForAfterResuming = (
+ buffer: AudioBuffer,
+ timestamp: number,
+ maxDuration: number | null,
+ ) => {
+ audioChunksForAfterResuming.push({buffer, timestamp, maxDuration});
};
const moveQueuedChunksToPauseQueue = () => {
const toQueue = removeAndReturnAllQueuedAudioNodes();
for (const chunk of toQueue) {
- addChunkForAfterResuming(chunk.buffer, chunk.timestamp);
+ addChunkForAfterResuming(chunk.buffer, chunk.timestamp, chunk.maxDuration);
}
};
@@ -230,8 +236,9 @@ export const makeAudioIterator = (
node: AudioBufferSourceNode,
timestamp: number,
buffer: AudioBuffer,
+ maxDuration: number | null,
) => {
- queuedAudioNodes.push({node, timestamp, buffer});
+ queuedAudioNodes.push({node, timestamp, buffer, maxDuration});
},
removeQueuedAudioNode: (node: AudioBufferSourceNode) => {
const index = queuedAudioNodes.findIndex((n) => n.node === node);
diff --git a/packages/media/src/test/audio-iterator.test.ts b/packages/media/src/test/audio-iterator.test.ts
index 4361715ed0..5fc8eb9dd6 100644
--- a/packages/media/src/test/audio-iterator.test.ts
+++ b/packages/media/src/test/audio-iterator.test.ts
@@ -21,12 +21,8 @@ const prepare = async () => {
}),
sharedAudioContext: new AudioContext(),
getIsLooping: () => false,
- getEndTime: () => {
- throw new Error('not implemented');
- },
- getStartTime: () => {
- throw new Error('not implemented');
- },
+ getEndTime: () => Infinity,
+ getStartTime: () => 0,
updatePlaybackTime: () => {},
initialMuted: false,
drawDebugOverlay: () => {},
@@ -49,13 +45,14 @@ test('media player should work', async () => {
const scheduleAudioNode = (
node: AudioBufferSourceNode,
mediaTimestamp: number,
+ maxDuration: number | null,
) => {
- node.start(mediaTimestamp);
+ node.start(mediaTimestamp, 0, maxDuration ?? undefined);
setTimeout(
() => {
node.stop();
},
- (node.buffer?.duration ?? 0) * 1000,
+ (maxDuration ?? node.buffer?.duration ?? 0) * 1000,
);
scheduledChunks.push(mediaTimestamp);
};
@@ -102,13 +99,14 @@ test('should not create too many iterators when the audio ends', async () => {
const scheduleAudioNode = (
node: AudioBufferSourceNode,
mediaTimestamp: number,
+ maxDuration: number | null,
) => {
- node.start(mediaTimestamp);
+ node.start(mediaTimestamp, 0, maxDuration ?? undefined);
setTimeout(
() => {
node.stop();
},
- (node.buffer?.duration ?? 0) * 1000,
+ (maxDuration ?? node.buffer?.duration ?? 0) * 1000,
);
scheduledChunks.push(mediaTimestamp);
};
@@ -148,13 +146,14 @@ test('should create more iterators when seeking ', async () => {
const scheduleAudioNode = (
node: AudioBufferSourceNode,
mediaTimestamp: number,
+ maxDuration: number | null,
) => {
- node.start(mediaTimestamp);
+ node.start(mediaTimestamp, 0, maxDuration ?? undefined);
setTimeout(
() => {
node.stop();
},
- (node.buffer?.duration ?? 0) * 1000,
+ (maxDuration ?? node.buffer?.duration ?? 0) * 1000,
);
scheduledChunks.push(mediaTimestamp);
};
@@ -203,12 +202,8 @@ test('should not schedule duplicate chunks with playbackRate=0.5', async () => {
}),
sharedAudioContext: new AudioContext(),
getIsLooping: () => false,
- getEndTime: () => {
- throw new Error('not implemented');
- },
- getStartTime: () => {
- throw new Error('not implemented');
- },
+ getEndTime: () => Infinity,
+ getStartTime: () => 0,
updatePlaybackTime: () => {},
initialMuted: false,
drawDebugOverlay: () => {},
@@ -218,13 +213,14 @@ test('should not schedule duplicate chunks with playbackRate=0.5', async () => {
const scheduleAudioNode = (
node: AudioBufferSourceNode,
mediaTimestamp: number,
+ maxDuration: number | null,
) => {
- node.start(mediaTimestamp);
+ node.start(mediaTimestamp, 0, maxDuration ?? undefined);
setTimeout(
() => {
node.stop();
},
- (node.buffer?.duration ?? 0) * 1000,
+ (maxDuration ?? node.buffer?.duration ?? 0) * 1000,
);
scheduledChunks.push(mediaTimestamp);
};
@@ -282,19 +278,28 @@ test('should not decode + schedule audio chunks beyond the end time', async () =
drawDebugOverlay: () => {},
});
- const scheduledChunks: number[] = [];
+ const scheduledChunks: {
+ timestamp: number;
+ maxDuration: number | null;
+ bufferDuration: number;
+ }[] = [];
const scheduleAudioNode = (
node: AudioBufferSourceNode,
mediaTimestamp: number,
+ maxDuration: number | null,
) => {
- node.start(mediaTimestamp);
+ node.start(mediaTimestamp, 0, maxDuration ?? undefined);
setTimeout(
() => {
node.stop();
},
- (node.buffer?.duration ?? 0) * 1000,
+ (maxDuration ?? node.buffer?.duration ?? 0) * 1000,
);
- scheduledChunks.push(mediaTimestamp);
+ scheduledChunks.push({
+ timestamp: mediaTimestamp,
+ maxDuration,
+ bufferDuration: node.buffer?.duration ?? 0,
+ });
};
// Simulate playback frame by frame, seeking past the end time
@@ -310,7 +315,11 @@ test('should not decode + schedule audio chunks beyond the end time', async () =
});
}
- for (const timestamp of scheduledChunks) {
- expect(timestamp).toBeLessThanOrEqual(endTime);
+ for (const chunk of scheduledChunks) {
+ expect(chunk.timestamp).toBeLessThanOrEqual(endTime);
+ const effectiveDuration = chunk.maxDuration ?? chunk.bufferDuration;
+ expect(chunk.timestamp + effectiveDuration).toBeLessThanOrEqual(
+ endTime + 1 / 48000,
+ );
}
});
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 f9a5bc75a6..1bdee6ad7e 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
@@ -91,12 +91,8 @@ test('same goes for audio', async () => {
}),
sharedAudioContext: new AudioContext(),
getIsLooping: () => false,
- getEndTime: () => {
- throw new Error('not implemented');
- },
- getStartTime: () => {
- throw new Error('not implemented');
- },
+ getEndTime: () => Infinity,
+ getStartTime: () => 0,
updatePlaybackTime: () => {},
initialMuted: false,
drawDebugOverlay: () => {},
@@ -109,8 +105,8 @@ test('same goes for audio', async () => {
playbackRate: 1,
startFromSecond: 0.06671494248275864,
getIsPlaying: () => true,
- scheduleAudioNode: (node, mediaTimestamp) => {
- node.start(mediaTimestamp);
+ scheduleAudioNode: (node, mediaTimestamp, maxDuration) => {
+ node.start(mediaTimestamp, 0, maxDuration ?? undefined);
},
});
@@ -119,8 +115,8 @@ test('same goes for audio', async () => {
nonce: nonceManager.createAsyncOperation(),
playbackRate: 1,
getIsPlaying: () => true,
- scheduleAudioNode: (node) => {
- node.start(1);
+ scheduleAudioNode: (node, _mediaTimestamp, maxDuration) => {
+ node.start(1, 0, maxDuration ?? undefined);
},
});