Skip to content

🎥 Make videos programmatically with React

sync to audio context

Jonny Burger39422a51e1b7
diff --git a/packages/media/src/audio-iterator-manager.ts b/packages/media/src/audio-iterator-manager.ts
index 22c07f9600..65bf1fcafb 100644
--- a/packages/media/src/audio-iterator-manager.ts
+++ b/packages/media/src/audio-iterator-manager.ts
@@ -145,49 +145,14 @@ export const audioIteratorManager = ({
};
};
- const resumeScheduledAudioChunks = ({
- playbackRate,
- scheduleAudioNode,
- debugAudioScheduling,
- getAudioContextState,
- }: {
- playbackRate: number;
- scheduleAudioNode: ScheduleAudioNode;
- debugAudioScheduling: boolean;
- getAudioContextState: () => AudioContextState;
- }) => {
- if (muted) {
- return;
- }
-
- if (!audioBufferIterator) {
- return;
- }
-
- for (const chunk of audioBufferIterator.getAndClearAudioChunksForAfterResuming()) {
- scheduleAudioChunk({
- buffer: chunk.buffer,
- mediaTimestamp: chunk.timestamp,
- playbackRate,
- scheduleAudioNode,
- debugAudioScheduling,
- getAudioContextState,
- });
- }
- };
-
const onAudioChunk = ({
- getIsPlaying,
getAudioContextState,
- getAudioContextOutputTimestamp,
buffer,
playbackRate,
scheduleAudioNode,
debugAudioScheduling,
}: {
- getIsPlaying: () => boolean;
getAudioContextState: () => AudioContextState;
- getAudioContextOutputTimestamp: () => number;
buffer: WrappedAudioBuffer;
playbackRate: number;
scheduleAudioNode: ScheduleAudioNode;
@@ -209,45 +174,14 @@ export const audioIteratorManager = ({
return;
}
- if (
- getIsPlaying() &&
- getAudioContextState() === 'running' &&
- getAudioContextOutputTimestamp() > 0
- ) {
- resumeScheduledAudioChunks({
- playbackRate,
- scheduleAudioNode,
- debugAudioScheduling,
- getAudioContextState,
- });
-
- scheduleAudioChunk({
- buffer: buffer.buffer,
- mediaTimestamp: buffer.timestamp,
- playbackRate,
- scheduleAudioNode,
- debugAudioScheduling,
- getAudioContextState,
- });
- } else {
- if (!audioBufferIterator) {
- throw new Error('Audio buffer iterator not found');
- }
-
- if (debugAudioScheduling) {
- Internals.Log.info(
- {logLevel: 'trace', tag: 'audio-scheduling'},
- 'not ready, added to queue: %s %s',
- buffer.timestamp.toFixed(3),
- buffer.duration.toFixed(3),
- );
- }
-
- audioBufferIterator.addChunkForAfterResuming(
- buffer.buffer,
- buffer.timestamp,
- );
- }
+ scheduleAudioChunk({
+ buffer: buffer.buffer,
+ mediaTimestamp: buffer.timestamp,
+ playbackRate,
+ scheduleAudioNode,
+ debugAudioScheduling,
+ getAudioContextState,
+ });
drawDebugOverlay();
};
@@ -322,9 +256,7 @@ export const audioIteratorManager = ({
notifyNodeScheduled();
onAudioChunk({
- getIsPlaying,
getAudioContextState,
- getAudioContextOutputTimestamp,
buffer: result.value,
playbackRate,
scheduleAudioNode,
@@ -417,14 +349,6 @@ export const audioIteratorManager = ({
});
};
- const pausePlayback = () => {
- if (!audioBufferIterator) {
- return;
- }
-
- audioBufferIterator.moveQueuedChunksToPauseQueue();
- };
-
const seek = async ({
newTime,
nonce,
@@ -513,8 +437,6 @@ export const audioIteratorManager = ({
return {
startAudioIterator,
- resumeScheduledAudioChunks,
- pausePlayback,
getAudioBufferIterator: () => audioBufferIterator,
destroyIterator: () => {
prewarmedAudioIteratorCache.destroy();
diff --git a/packages/media/src/audio/audio-preview-iterator.ts b/packages/media/src/audio/audio-preview-iterator.ts
index 3f50f5a9f1..d19461fb4d 100644
--- a/packages/media/src/audio/audio-preview-iterator.ts
+++ b/packages/media/src/audio/audio-preview-iterator.ts
@@ -36,10 +36,6 @@ export const makeAudioIterator = ({
maximumTimestamp,
);
const queuedAudioNodes: QueuedNode[] = [];
- const audioChunksForAfterResuming: {
- buffer: AudioBuffer;
- timestamp: number;
- }[] = [];
let mostRecentTimestamp = -Infinity;
const cleanupAudioQueue = (
@@ -82,45 +78,6 @@ export const makeAudioIterator = ({
queuedAudioNodes.length = 0;
};
- const removeAndReturnAllQueuedAudioNodes = () => {
- const nodes = queuedAudioNodes.slice();
- for (const node of nodes) {
- try {
- node.node.stop();
- } catch {
- // Node may not have been started
- }
- }
-
- queuedAudioNodes.length = 0;
- return nodes;
- };
-
- const addChunkForAfterResuming = (buffer: AudioBuffer, timestamp: number) => {
- audioChunksForAfterResuming.push({
- buffer,
- timestamp,
- });
- };
-
- const moveQueuedChunksToPauseQueue = () => {
- const toQueue = removeAndReturnAllQueuedAudioNodes();
- for (const chunk of toQueue) {
- addChunkForAfterResuming(chunk.buffer, chunk.timestamp);
- }
-
- if (debugAudioScheduling && toQueue.length > 0) {
- Internals.Log.trace(
- {logLevel: 'trace', tag: 'audio-scheduling'},
- `Moved ${toQueue.length} ${toQueue.length === 1 ? 'chunk' : 'chunks'} to pause queue (${toQueue[0].timestamp.toFixed(3)}-${toQueue[toQueue.length - 1].timestamp + toQueue[toQueue.length - 1].buffer.duration.toFixed(3)})`,
- );
- }
- };
-
- const getNumberOfChunksAfterResuming = () => {
- return audioChunksForAfterResuming.length;
- };
-
const getNextFn = async () => {
const next = await iterator.next();
@@ -139,7 +96,6 @@ export const makeAudioIterator = ({
cleanupAudioQueue(audioContext);
destroyed = true;
iterator.return().catch(() => undefined);
- audioChunksForAfterResuming.length = 0;
},
getNextFn,
isDestroyed: () => {
@@ -176,11 +132,6 @@ export const makeAudioIterator = ({
queuedAudioNodes.splice(index, 1);
}
},
- getAndClearAudioChunksForAfterResuming: () => {
- const chunks = audioChunksForAfterResuming.slice();
- audioChunksForAfterResuming.length = 0;
- return chunks;
- },
guessNextTimestamp: () => {
return !Number.isFinite(mostRecentTimestamp)
? startFromSecond
@@ -195,11 +146,6 @@ export const makeAudioIterator = ({
from = Math.min(from, node.timestamp);
}
- for (const chunk of audioChunksForAfterResuming) {
- until = Math.max(until, chunk.timestamp + chunk.buffer.duration);
- from = Math.min(from, chunk.timestamp);
- }
-
if (!Number.isFinite(from) || !Number.isFinite(until)) {
return null;
}
@@ -209,9 +155,6 @@ export const makeAudioIterator = ({
until,
};
},
- addChunkForAfterResuming,
- moveQueuedChunksToPauseQueue,
- getNumberOfChunksAfterResuming,
};
};
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index 81286a53ac..b7f648416b 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -436,27 +436,7 @@ export class MediaPlayer {
}
}
- public playAudio(): void {
- if (
- this.audioIteratorManager &&
- this.sharedAudioContext?.audioContext.state === 'running' &&
- (this.sharedAudioContext?.audioContext?.getOutputTimestamp()
- .contextTime ?? 0) > 0
- ) {
- this.audioIteratorManager.resumeScheduledAudioChunks({
- playbackRate: this.playbackRate * this.globalPlaybackRate,
- scheduleAudioNode: this.scheduleAudioNode,
- debugAudioScheduling: this.debugAudioScheduling,
- getAudioContextState: () =>
- this.sharedAudioContext?.audioContext.state ?? 'suspended',
- });
- }
- }
-
public play(): void {
- // This part is idempotent, will schedule audio chunks if needed
- this.playAudio();
-
if (this.playing) {
return;
}
@@ -490,8 +470,6 @@ export class MediaPlayer {
}
this.playing = false;
- this.audioIteratorManager?.pausePlayback();
-
this.drawDebugOverlay();
}
@@ -570,34 +548,7 @@ export class MediaPlayer {
}
private rescheduleAudioChunks(): void {
- if (!this.audioIteratorManager) {
- return;
- }
-
- if (!this.sharedAudioContext) {
- return;
- }
-
- const iterator = this.audioIteratorManager.getAudioBufferIterator();
- if (!iterator) {
- return;
- }
-
- iterator.moveQueuedChunksToPauseQueue();
- if (
- this.playing &&
- this.sharedAudioContext.audioContext.state === 'running' &&
- (this.sharedAudioContext.audioContext?.getOutputTimestamp().contextTime ??
- 0) > 0
- ) {
- this.audioIteratorManager.resumeScheduledAudioChunks({
- playbackRate: this.playbackRate * this.globalPlaybackRate,
- scheduleAudioNode: this.scheduleAudioNode,
- debugAudioScheduling: this.debugAudioScheduling,
- getAudioContextState: () =>
- this.sharedAudioContext?.audioContext.state ?? 'suspended',
- });
- }
+ // TODO: Reschedule
}
public async setPlaybackRate(
diff --git a/packages/media/src/use-common-effects.ts b/packages/media/src/use-common-effects.ts
index 72d115b6a1..e2016bec9c 100644
--- a/packages/media/src/use-common-effects.ts
+++ b/packages/media/src/use-common-effects.ts
@@ -85,10 +85,6 @@ export const useCommonEffects = ({
logLevel,
]);
- if (playingWhilePremounting) {
- mediaPlayerRef.current?.playAudio();
- }
-
useLayoutEffect(() => {
const mediaPlayer = mediaPlayerRef.current;
if (!mediaPlayer) return;