Skip to content

🎥 Make videos programmatically with React

Fix stop() on never-started AudioScheduledSourceNode

Jonny Burgerfb93a7f536c4
diff --git a/packages/media/src/audio-iterator-manager.ts b/packages/media/src/audio-iterator-manager.ts
index 4d2bc01c0b..b81b7cb76b 100644
--- a/packages/media/src/audio-iterator-manager.ts
+++ b/packages/media/src/audio-iterator-manager.ts
@@ -57,7 +57,7 @@ export const audioIteratorManager = ({
node: AudioBufferSourceNode,
mediaTimestamp: number,
maxDuration: number | null,
- ) => void;
+ ) => boolean;
maxDuration: number | null;
}) => {
if (!audioBufferIterator) {
@@ -73,7 +73,11 @@ export const audioIteratorManager = ({
node.playbackRate.value = playbackRate;
node.connect(gainNode);
- scheduleAudioNode(node, mediaTimestamp, maxDuration);
+ const started = scheduleAudioNode(node, mediaTimestamp, maxDuration);
+ if (!started) {
+ node.disconnect();
+ return;
+ }
const iterator = audioBufferIterator;
@@ -99,7 +103,7 @@ export const audioIteratorManager = ({
node: AudioBufferSourceNode,
mediaTimestamp: number,
maxDuration: number | null,
- ) => void;
+ ) => boolean;
}) => {
if (muted) {
return;
@@ -153,7 +157,7 @@ export const audioIteratorManager = ({
node: AudioBufferSourceNode,
mediaTimestamp: number,
maxDuration: number | null,
- ) => void;
+ ) => boolean;
}) => {
if (muted) {
return;
@@ -243,7 +247,7 @@ export const audioIteratorManager = ({
node: AudioBufferSourceNode,
mediaTimestamp: number,
maxDuration: number | null,
- ) => void;
+ ) => boolean;
}) => {
if (muted) {
return;
@@ -335,7 +339,7 @@ export const audioIteratorManager = ({
node: AudioBufferSourceNode,
mediaTimestamp: number,
maxDuration: number | null,
- ) => void;
+ ) => boolean;
}) => {
if (muted) {
return;
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index 66327e162b..3d85e8a407 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -643,7 +643,7 @@ export class MediaPlayer {
node: AudioBufferSourceNode,
mediaTimestamp: number,
maxDuration: number | null,
- ) => {
+ ): boolean => {
const currentTime = this.getAudioPlaybackTime();
const delayWithoutPlaybackRate = mediaTimestamp - currentTime;
const delay =
@@ -662,7 +662,7 @@ export class MediaPlayer {
} else {
const offset = -delayWithoutPlaybackRate;
if (maxDuration !== null && maxDuration - offset <= 0) {
- return;
+ return false;
}
node.start(
@@ -671,6 +671,8 @@ export class MediaPlayer {
maxDuration !== null ? maxDuration - offset : undefined,
);
}
+
+ return true;
};
private getAudioPlaybackTime(): number {
diff --git a/packages/media/src/test/audio-iterator.test.ts b/packages/media/src/test/audio-iterator.test.ts
index a9b3833986..2e8f9a45b5 100644
--- a/packages/media/src/test/audio-iterator.test.ts
+++ b/packages/media/src/test/audio-iterator.test.ts
@@ -55,6 +55,7 @@ test('media player should work', async () => {
(maxDuration ?? node.buffer?.duration ?? 0) * 1000,
);
scheduledChunks.push(mediaTimestamp);
+ return true;
};
const {manager, playbackRate, getIsPlaying} = await prepare();
@@ -109,6 +110,7 @@ test('should not create too many iterators when the audio ends', async () => {
(maxDuration ?? node.buffer?.duration ?? 0) * 1000,
);
scheduledChunks.push(mediaTimestamp);
+ return true;
};
await manager.seek({
@@ -156,6 +158,7 @@ test('should create more iterators when seeking ', async () => {
(maxDuration ?? node.buffer?.duration ?? 0) * 1000,
);
scheduledChunks.push(mediaTimestamp);
+ return true;
};
await manager.seek({
@@ -223,6 +226,7 @@ test('should not schedule duplicate chunks with playbackRate=0.5', async () => {
(maxDuration ?? node.buffer?.duration ?? 0) * 1000,
);
scheduledChunks.push(mediaTimestamp);
+ return true;
};
const fps = 25;
@@ -299,6 +303,7 @@ test('should not decode + schedule audio chunks beyond the end time', async () =
maxDuration,
bufferDuration: node.buffer?.duration ?? 0,
});
+ return true;
};
// Simulate playback frame by frame, seeking past the end time
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 6387ed708f..f853d36d66 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
@@ -106,6 +106,7 @@ test('same goes for audio', async () => {
getIsPlaying: () => true,
scheduleAudioNode: (node, mediaTimestamp, maxDuration) => {
node.start(mediaTimestamp, 0, maxDuration ?? undefined);
+ return true;
},
});
@@ -116,6 +117,7 @@ test('same goes for audio', async () => {
getIsPlaying: () => true,
scheduleAudioNode: (node, _mediaTimestamp, maxDuration) => {
node.start(1, 0, maxDuration ?? undefined);
+ return true;
},
});