Skip to content

🎥 Make videos programmatically with React

Pass SharedAudioContextForMediaPlayer to audio iterator manager

Jonny Burger97710b1652fb
diff --git a/packages/media/src/audio-iterator-manager.ts b/packages/media/src/audio-iterator-manager.ts
index ad6c1acaed..cf8d734094 100644
--- a/packages/media/src/audio-iterator-manager.ts
+++ b/packages/media/src/audio-iterator-manager.ts
@@ -10,6 +10,7 @@ import type {DelayPlaybackIfNotPremounting} from './delay-playback-if-not-premou
import type {Nonce} from './nonce-manager';
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 = 8;
@@ -30,7 +31,7 @@ export const audioIteratorManager = ({
}: {
audioTrack: InputAudioTrack;
delayPlaybackHandleIfNotPremounting: () => DelayPlaybackIfNotPremounting;
- sharedAudioContext: AudioContext;
+ sharedAudioContext: SharedAudioContextForMediaPlayer;
getIsLooping: () => boolean;
getEndTime: () => number;
getStartTime: () => number;
@@ -40,8 +41,8 @@ export const audioIteratorManager = ({
let muted = initialMuted;
let currentVolume = 1;
- const gainNode = sharedAudioContext.createGain();
- gainNode.connect(sharedAudioContext.destination);
+ const gainNode = sharedAudioContext.audioContext.createGain();
+ gainNode.connect(sharedAudioContext.audioContext.destination);
const audioSink = new AudioBufferSink(audioTrack);
const prewarmedAudioIteratorCache =
@@ -67,7 +68,7 @@ export const audioIteratorManager = ({
throw new Error('Audio buffer iterator not found');
}
- if (sharedAudioContext.state !== 'running') {
+ if (sharedAudioContext.audioContext.state !== 'running') {
throw new Error(
'Tried to schedule node while audio context is not running',
);
@@ -77,7 +78,7 @@ export const audioIteratorManager = ({
return;
}
- const node = sharedAudioContext.createBufferSource();
+ const node = sharedAudioContext.audioContext.createBufferSource();
node.buffer = buffer;
node.playbackRate.value = playbackRate;
node.connect(gainNode);
@@ -106,6 +107,7 @@ export const audioIteratorManager = ({
buffer,
scheduledTime: started.scheduledTime,
playbackRate,
+ scheduledAtAnchor: sharedAudioContext.audioSyncAnchor.value,
});
node.onended = () => {
// Some leniancy is needed as we find that sometimes onended is fired a bit too early
@@ -174,8 +176,9 @@ export const audioIteratorManager = ({
if (
getIsPlaying() &&
- sharedAudioContext.state === 'running' &&
- (sharedAudioContext.getOutputTimestamp().contextTime ?? 0) > 0
+ sharedAudioContext.audioContext.state === 'running' &&
+ (sharedAudioContext.audioContext.getOutputTimestamp().contextTime ?? 0) >
+ 0
) {
resumeScheduledAudioChunks({
playbackRate,
@@ -354,8 +357,8 @@ export const audioIteratorManager = ({
from:
queuedPeriod.from -
ALLOWED_GLOBAL_TIME_ANCHOR_SHIFT -
- sharedAudioContext.baseLatency -
- sharedAudioContext.outputLatency,
+ sharedAudioContext.audioContext.baseLatency -
+ sharedAudioContext.audioContext.outputLatency,
until: queuedPeriod.until,
}
: null;
diff --git a/packages/media/src/audio/audio-preview-iterator.ts b/packages/media/src/audio/audio-preview-iterator.ts
index 5189145020..a770f5500c 100644
--- a/packages/media/src/audio/audio-preview-iterator.ts
+++ b/packages/media/src/audio/audio-preview-iterator.ts
@@ -3,6 +3,7 @@ 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';
export const HEALTHY_BUFFER_THRESHOLD_SECONDS = 1;
@@ -12,6 +13,7 @@ export type QueuedNode = {
buffer: AudioBuffer;
scheduledTime: number;
playbackRate: number;
+ scheduledAtAnchor: number;
};
export type QueuedPeriod = {
@@ -39,27 +41,34 @@ export const makeAudioIterator = (
let pendingNext: Promise<IteratorResult<WrappedAudioBuffer, void>> | null =
null;
- const cleanupAudioQueue = (audioContext: AudioContext) => {
+ const cleanupAudioQueue = (
+ audioContext: SharedAudioContextForMediaPlayer,
+ ) => {
for (const node of queuedAudioNodes) {
try {
- const currentlyHearing = audioContext.getOutputTimestamp().contextTime!;
- const nodeEndTime =
- node.scheduledTime + node.buffer.duration / node.playbackRate;
-
+ // 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.currentTime;
+ audioContext.audioContext.currentTime;
- const shouldKeep = isAlreadyPlaying;
+ // except for when the audio anchor changed (e.g. through a seek)
+ const wasScheduledForThisAnchor =
+ node.scheduledAtAnchor === audioContext.audioSyncAnchor.value;
- if (shouldKeep) {
+ if (isAlreadyPlaying && wasScheduledForThisAnchor) {
continue;
}
if (debugAudioScheduling) {
+ const currentlyHearing =
+ audioContext.audioContext.getOutputTimestamp().contextTime!;
+ const nodeEndTime =
+ node.scheduledTime + node.buffer.duration / node.playbackRate;
+
Internals.Log.info(
{logLevel: 'trace', tag: 'audio-scheduling'},
- `Stopping node ${node.timestamp.toFixed(3)}, currently hearing = ${currentlyHearing.toFixed(3)} currentTime = ${audioContext.currentTime.toFixed(3)} nodeEndTime = ${nodeEndTime.toFixed(3)} scheduledTime = ${node.scheduledTime.toFixed(3)}`,
+ `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)}`,
);
}
@@ -259,7 +268,7 @@ export const makeAudioIterator = (
};
return {
- destroy: (audioContext: AudioContext) => {
+ destroy: (audioContext: SharedAudioContextForMediaPlayer) => {
cleanupAudioQueue(audioContext);
destroyed = true;
iterator.return().catch(() => undefined);
@@ -286,12 +295,14 @@ export const makeAudioIterator = (
buffer,
scheduledTime,
playbackRate,
+ scheduledAtAnchor,
}: {
node: AudioBufferSourceNode;
timestamp: number;
buffer: AudioBuffer;
scheduledTime: number;
playbackRate: number;
+ scheduledAtAnchor: number;
}) => {
queuedAudioNodes.push({
node,
@@ -299,6 +310,7 @@ export const makeAudioIterator = (
buffer,
scheduledTime,
playbackRate,
+ scheduledAtAnchor,
});
},
removeQueuedAudioNode: (node: AudioBufferSourceNode) => {
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index 0bed0e2113..2b49de61be 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -292,7 +292,7 @@ export class MediaPlayer {
audioTrack,
delayPlaybackHandleIfNotPremounting:
this.delayPlaybackHandleIfNotPremounting,
- sharedAudioContext: this.sharedAudioContext.audioContext,
+ sharedAudioContext: this.sharedAudioContext,
getIsLooping: () => this.loop,
getEndTime: () => this.getEndTime(),
getStartTime: () => this.getStartTime(),
diff --git a/packages/media/src/test/audio-iterator.test.ts b/packages/media/src/test/audio-iterator.test.ts
index e81336e4fb..4483769f14 100644
--- a/packages/media/src/test/audio-iterator.test.ts
+++ b/packages/media/src/test/audio-iterator.test.ts
@@ -22,7 +22,14 @@ const prepare = async () => {
unblock: () => {},
[Symbol.dispose]: () => {},
}),
- sharedAudioContext: audioContext,
+ sharedAudioContext: {
+ audioContext,
+ audioSyncAnchor: {value: 0},
+ scheduleAudioNode: () => ({
+ type: 'started',
+ scheduledTime: 0,
+ }),
+ },
getIsLooping: () => false,
getEndTime: () => Infinity,
getStartTime: () => 0,
@@ -229,7 +236,14 @@ test('should not schedule duplicate chunks with playbackRate=0.5', async () => {
unblock: () => {},
[Symbol.dispose]: () => {},
}),
- sharedAudioContext: audioContext,
+ sharedAudioContext: {
+ audioContext,
+ audioSyncAnchor: {value: 0},
+ scheduleAudioNode: () => ({
+ type: 'started',
+ scheduledTime: 0,
+ }),
+ },
getIsLooping: () => false,
getEndTime: () => Infinity,
getStartTime: () => 0,
@@ -298,7 +312,14 @@ test('should not decode + schedule audio chunks beyond the end time', async () =
unblock: () => {},
[Symbol.dispose]: () => {},
}),
- sharedAudioContext: audioContext,
+ sharedAudioContext: {
+ audioContext,
+ audioSyncAnchor: {value: 0},
+ scheduleAudioNode: () => ({
+ type: 'started',
+ scheduledTime: 0,
+ }),
+ },
getIsLooping: () => false,
getEndTime: () => endTime,
getStartTime: () => 0,
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 b9205f7707..352575413f 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
@@ -89,7 +89,14 @@ test('same goes for audio', async () => {
unblock: () => {},
[Symbol.dispose]: () => {},
}),
- sharedAudioContext: new AudioContext(),
+ sharedAudioContext: {
+ audioContext: new AudioContext(),
+ audioSyncAnchor: {value: 0},
+ scheduleAudioNode: () => ({
+ type: 'started',
+ scheduledTime: 0,
+ }),
+ },
getIsLooping: () => false,
getEndTime: () => Infinity,
getStartTime: () => 0,