Skip to content

Commit

Permalink
Don't hang on audio context trying to resume (#1379)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO authored Jan 22, 2025
1 parent dee51bc commit 7aa7453
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-pandas-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Don't hang on audio context trying to resume
19 changes: 10 additions & 9 deletions src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import {
isRemotePub,
isSafari,
isWeb,
sleep,
supportsSetSinkId,
toHttpUrl,
unpackStreamId,
Expand Down Expand Up @@ -1774,24 +1775,24 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
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;
Expand Down

0 comments on commit 7aa7453

Please sign in to comment.