From 7aa74532927d6380a2263ac8324b176884208ea6 Mon Sep 17 00:00:00 2001 From: lukasIO Date: Wed, 22 Jan 2025 21:15:09 +0100 Subject: [PATCH] Don't hang on audio context trying to resume (#1379) --- .changeset/weak-pandas-run.md | 5 +++++ src/room/Room.ts | 19 ++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 .changeset/weak-pandas-run.md diff --git a/.changeset/weak-pandas-run.md b/.changeset/weak-pandas-run.md new file mode 100644 index 0000000000..8bf95f5020 --- /dev/null +++ b/.changeset/weak-pandas-run.md @@ -0,0 +1,5 @@ +--- +"livekit-client": patch +--- + +Don't hang on audio context trying to resume diff --git a/src/room/Room.ts b/src/room/Room.ts index 8e544edb26..9987a5a61a 100644 --- a/src/room/Room.ts +++ b/src/room/Room.ts @@ -91,6 +91,7 @@ import { isRemotePub, isSafari, isWeb, + sleep, supportsSetSinkId, toHttpUrl, unpackStreamId, @@ -1774,24 +1775,24 @@ class Room extends (EventEmitter as new () => TypedEmitter) this.audioContext = getNewAudioContext() ?? undefined; } + if (this.options.webAudioMix) { + this.remoteParticipants.forEach((participant) => + participant.setAudioContext(this.audioContext), + ); + } + + this.localParticipant.setAudioContext(this.audioContext); + if (this.audioContext && this.audioContext.state === 'suspended') { // for iOS a newly created AudioContext is always in `suspended` state. // we try our best to resume the context here, if that doesn't work, we just continue with regular processing try { - await this.audioContext.resume(); + await Promise.race([this.audioContext.resume(), sleep(200)]); } catch (e: any) { this.log.warn('Could not resume audio context', { ...this.logContext, error: e }); } } - if (this.options.webAudioMix) { - this.remoteParticipants.forEach((participant) => - participant.setAudioContext(this.audioContext), - ); - } - - this.localParticipant.setAudioContext(this.audioContext); - const newContextIsRunning = this.audioContext?.state === 'running'; if (newContextIsRunning !== this.canPlaybackAudio) { this.audioEnabled = newContextIsRunning;