web: Fix hangs on Safari by avoiding getOutputTimestamp

`AudioContext.getOutputTimestamp` returns incorrect values in
Safari 15.1+, and this would cause movies with 'stream' sounds to
soft-lock because the calculated audio position would barely
progress.

See:
https://developer.apple.com/forums/thread/696356

Change the output timestamp calculation to use
`AudioContext.currentTime` instead.
This commit is contained in:
Mike Welsh 2022-03-19 12:58:47 -07:00
parent fa70cc07ab
commit dd45dc81a0
1 changed files with 4 additions and 5 deletions

View File

@ -28,13 +28,12 @@ export function copyToAudioBuffer(
} }
/** /**
* Returns `AudioContext.getOutputTimestamp`, defaulting to `context.currentTime` if * Returns the estimated output timestamp for the audio context.
* `getOutputTimestamp` is unavailable. This is necessary because `web-sys` does not yet export * This is necessary because web-sys does not export `AudioContext.baseLatency`.
* `AudioBuffer.copyToChannel`.
* *
* @internal * @internal
*/ */
export function getAudioOutputTimestamp(context: AudioContext): number { export function getAudioOutputTimestamp(context: AudioContext): number {
const timestamp = context.getOutputTimestamp?.(); // TODO: Ideally we'd use `context.getOutputTimestamp`, but this is broken as of Safari 15.4.
return timestamp?.contextTime ?? context.currentTime - context.baseLatency; return context.currentTime - context.baseLatency;
} }