Skip to content

Commit

Permalink
Emit AudioPlaybackFailed event in experimental web audio mix mode (#468)
Browse files Browse the repository at this point in the history
* Emit AudioPlaybackFailed event also in experimental web audio mix mode

* only resume if not running

* disconnect if no more attached elements

* fix startAudio for web audio mix
  • Loading branch information
lukasIO committed Oct 13, 2022
1 parent fcad243 commit 91541ab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-rats-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Emit AudioPlaybackFailed event also in experimental web audio mix mode"
3 changes: 3 additions & 0 deletions src/room/participant/RemoteParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ export default class RemoteParticipant extends Participant {
*/
setAudioContext(ctx: AudioContext | undefined) {
this.audioContext = ctx;
this.tracks.forEach(
(track) => track.track instanceof RemoteAudioTrack && track.track.setAudioContext(ctx),
);
}

/** @internal */
Expand Down
29 changes: 26 additions & 3 deletions src/room/track/RemoteAudioTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AudioReceiverStats, computeBitrate } from '../stats';
import RemoteTrack from './RemoteTrack';
import { Track } from './Track';
import log from '../../logger';
import { TrackEvent } from '../events';

export default class RemoteAudioTrack extends RemoteTrack {
private prevStats?: AudioReceiverStats;
Expand Down Expand Up @@ -96,9 +97,12 @@ export default class RemoteAudioTrack extends RemoteTrack {
} else {
detached = super.detach(element);
// if there are still any attached elements after detaching, connect webaudio to the first element that's left
if (this.audioContext && this.attachedElements.length > 0) {
// disconnect webaudio otherwise
if (this.audioContext) {
if (this.attachedElements.length > 0) {
this.connectWebAudio(this.audioContext, this.attachedElements[0]);
} else {
this.disconnectWebAudio();
}
}
}
Expand All @@ -109,10 +113,12 @@ export default class RemoteAudioTrack extends RemoteTrack {
* @internal
* @experimental
*/
setAudioContext(audioContext: AudioContext) {
setAudioContext(audioContext: AudioContext | undefined) {
this.audioContext = audioContext;
if (this.attachedElements.length > 0) {
if (audioContext && this.attachedElements.length > 0) {
this.connectWebAudio(audioContext, this.attachedElements[0]);
} else if (!audioContext) {
this.disconnectWebAudio();
}
}

Expand Down Expand Up @@ -144,6 +150,23 @@ export default class RemoteAudioTrack extends RemoteTrack {
if (this.elementVolume) {
this.gainNode.gain.setTargetAtTime(this.elementVolume, 0, 0.1);
}

// try to resume the context if it isn't running already
if (context.state !== 'running') {
context
.resume()
.then(() => {
if (context.state !== 'running') {
this.emit(
TrackEvent.AudioPlaybackFailed,
new Error("Audio Context couldn't be started automatically"),
);
}
})
.catch((e) => {
this.emit(TrackEvent.AudioPlaybackFailed, e);
});
}
}

private disconnectWebAudio() {
Expand Down

0 comments on commit 91541ab

Please sign in to comment.