Skip to content

🎥 Make videos programmatically with React

ok, works better

Jonny Burger1ea2b78f79bd
diff --git a/packages/media/src/audio-iterator-manager.ts b/packages/media/src/audio-iterator-manager.ts
index ed5d996751..fc1b37f274 100644
--- a/packages/media/src/audio-iterator-manager.ts
+++ b/packages/media/src/audio-iterator-manager.ts
@@ -181,6 +181,17 @@ export const audioIteratorManager = ({
scheduleAudioNode,
});
}
+
+ await iterator.bufferAsFarAsPossible((buffer) => {
+ if (!nonce.isStale()) {
+ onAudioChunk({
+ getIsPlaying,
+ buffer,
+ playbackRate,
+ scheduleAudioNode,
+ });
+ }
+ }, startFromSecond + MAX_BUFFER_AHEAD_SECONDS);
} catch (e) {
if (e instanceof InputDisposedError) {
// iterator was disposed by a newer startAudioIterator call
@@ -246,7 +257,6 @@ export const audioIteratorManager = ({
if (!currentTimeIsAlreadyQueued) {
const audioSatisfyResult = await audioBufferIterator.tryToSatisfySeek(
newTime,
- null,
(buffer) => {
if (!nonce.isStale()) {
onAudioChunk({
@@ -268,12 +278,6 @@ export const audioIteratorManager = ({
}
if (audioSatisfyResult.type === 'not-satisfied') {
- console.log(
- 'not satisfied currentTime',
- newTime,
- queuedPeriod,
- audioSatisfyResult.reason,
- );
await startAudioIterator({
nonce,
playbackRate,
@@ -291,7 +295,6 @@ export const audioIteratorManager = ({
await audioBufferIterator.bufferAsFarAsPossible((buffer) => {
if (!nonce.isStale()) {
- console.log('buffering', buffer.timestamp);
onAudioChunk({
getIsPlaying,
buffer,
diff --git a/packages/media/src/audio/allow-wait.ts b/packages/media/src/audio/allow-wait.ts
deleted file mode 100644
index ada6728842..0000000000
--- a/packages/media/src/audio/allow-wait.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import type {WrappedAudioBuffer} from 'mediabunny';
-
-export type AllowWait = {
- type: 'allow-wait';
- waitCallback: () => () => void;
-};
-
-export const allowWaitRoutine = async (
- next: Promise<IteratorResult<WrappedAudioBuffer, void>>,
- waitFn: AllowWait,
-) => {
- const result = await Promise.race([
- next,
- new Promise<void>((resolve) => {
- Promise.resolve().then(() => resolve());
- }),
- ]);
-
- if (!result) {
- const unblock = waitFn.waitCallback();
- const newRes = await next;
- unblock();
-
- return newRes;
- }
-
- return result;
-};
diff --git a/packages/media/src/audio/audio-preview-iterator.ts b/packages/media/src/audio/audio-preview-iterator.ts
index f10cf0f1bd..7c2d62c067 100644
--- a/packages/media/src/audio/audio-preview-iterator.ts
+++ b/packages/media/src/audio/audio-preview-iterator.ts
@@ -1,7 +1,6 @@
import type {WrappedAudioBuffer} from 'mediabunny';
import {roundTo4Digits} from '../helpers/round-to-4-digits';
import type {PrewarmedAudioIteratorCache} from '../prewarm-iterator-for-looping';
-import {allowWaitRoutine, type AllowWait} from './allow-wait';
export const HEALTHY_BUFFER_THRESHOLD_SECONDS = 1;
@@ -34,22 +33,25 @@ export const makeAudioIterator = (
queuedAudioNodes.length = 0;
};
- const getNextOrNullIfNotAvailable = async (allowWait: AllowWait | null) => {
- const next = pendingNext ?? iterator.next();
+ const getNextOrNullIfNotAvailable = async () => {
+ let next = pendingNext;
+
+ if (!next) {
+ next = iterator.next();
+ }
+
pendingNext = null;
- const result = allowWait
- ? await allowWaitRoutine(next, allowWait)
- : await Promise.race([
- next,
- new Promise<void>((resolve) => {
- Promise.resolve().then(() => resolve());
- }),
- ]);
+ 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,
- pendingPromise: next,
waitPromise: async () => {
const res = await next;
return res.value;
@@ -62,6 +64,8 @@ export const makeAudioIterator = (
mostRecentTimestamp,
result.value.timestamp + result.value.duration,
);
+ // preload next already
+ pendingNext = iterator.next();
return {
type: 'got-buffer' as const,
buffer: result.value,
@@ -76,7 +80,6 @@ export const makeAudioIterator = (
const tryToSatisfySeek = async (
time: number,
- allowWait: AllowWait | null,
onBufferScheduled: (buffer: WrappedAudioBuffer) => void,
): Promise<
| {
@@ -98,7 +101,7 @@ export const makeAudioIterator = (
}
while (true) {
- const buffer = await getNextOrNullIfNotAvailable(allowWait);
+ const buffer = await getNextOrNullIfNotAvailable();
if (buffer.type === 'need-to-wait-for-it') {
return {
type: 'not-satisfied' as const,
@@ -159,9 +162,8 @@ export const makeAudioIterator = (
return {type: 'max-reached'};
}
- const buffer = await getNextOrNullIfNotAvailable(null);
+ const buffer = await getNextOrNullIfNotAvailable();
if (buffer.type === 'need-to-wait-for-it') {
- pendingNext = buffer.pendingPromise;
return {type: 'waiting'};
}
diff --git a/packages/media/src/debug-overlay/preview-overlay.ts b/packages/media/src/debug-overlay/preview-overlay.ts
index 063fe04864..f0016fbd3a 100644
--- a/packages/media/src/debug-overlay/preview-overlay.ts
+++ b/packages/media/src/debug-overlay/preview-overlay.ts
@@ -37,16 +37,12 @@ export const drawPreviewOverlay = ({
.getAudioBufferIterator()
?.getQueuedPeriod();
- const numberOfChunksAfterResuming = audioIteratorManager
- ?.getAudioBufferIterator()
- ?.getNumberOfChunksAfterResuming();
- if (queuedPeriod && audioTime) {
+ if (queuedPeriod) {
+ const aheadText = audioTime
+ ? ` (${(queuedPeriod.until - (audioTime - audioSyncAnchor) * playbackRate).toFixed(3)}s ahead)`
+ : '';
lines.push(
- `Audio queued until ${queuedPeriod.until.toFixed(3)}s (${(queuedPeriod.until - (audioTime - audioSyncAnchor) * playbackRate).toFixed(3)}s ahead)`,
- );
- } else if (numberOfChunksAfterResuming) {
- lines.push(
- `Audio chunks for after resuming: ${numberOfChunksAfterResuming}`,
+ `Audio queued until ${queuedPeriod.until.toFixed(3)}s${aheadText}`,
);
}