Skip to content

Commit 2c095a8

Browse files
lukasIOsvajunas-budrys
authored andcommitted
Don't hang on audio context trying to resume (livekit#1379)
1 parent 55e7b82 commit 2c095a8

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

.changeset/weak-pandas-run.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"livekit-client": patch
3+
---
4+
5+
Don't hang on audio context trying to resume

src/room/Room.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import {
9191
isRemotePub,
9292
isSafari,
9393
isWeb,
94+
sleep,
9495
supportsSetSinkId,
9596
toHttpUrl,
9697
unpackStreamId,
@@ -1774,24 +1775,24 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
17741775
this.audioContext = getNewAudioContext() ?? undefined;
17751776
}
17761777

1778+
if (this.options.webAudioMix) {
1779+
this.remoteParticipants.forEach((participant) =>
1780+
participant.setAudioContext(this.audioContext),
1781+
);
1782+
}
1783+
1784+
this.localParticipant.setAudioContext(this.audioContext);
1785+
17771786
if (this.audioContext && this.audioContext.state === 'suspended') {
17781787
// for iOS a newly created AudioContext is always in `suspended` state.
17791788
// we try our best to resume the context here, if that doesn't work, we just continue with regular processing
17801789
try {
1781-
await this.audioContext.resume();
1790+
await Promise.race([this.audioContext.resume(), sleep(200)]);
17821791
} catch (e: any) {
17831792
this.log.warn('Could not resume audio context', { ...this.logContext, error: e });
17841793
}
17851794
}
17861795

1787-
if (this.options.webAudioMix) {
1788-
this.remoteParticipants.forEach((participant) =>
1789-
participant.setAudioContext(this.audioContext),
1790-
);
1791-
}
1792-
1793-
this.localParticipant.setAudioContext(this.audioContext);
1794-
17951796
const newContextIsRunning = this.audioContext?.state === 'running';
17961797
if (newContextIsRunning !== this.canPlaybackAudio) {
17971798
this.audioEnabled = newContextIsRunning;

0 commit comments

Comments
 (0)