Skip to content

Commit

Permalink
Allow specifying audio source for participant.setVolume API (#780)
Browse files Browse the repository at this point in the history
* Allow specifiyng audio source for participant.setVolume API

* remove microphone source check

* Create angry-bananas-matter.md
  • Loading branch information
lukasIO authored Jul 13, 2023
1 parent 1d60c4c commit db462ed
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-bananas-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Allow specifying audio source for participant.setVolume API
34 changes: 19 additions & 15 deletions src/room/participant/RemoteParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class RemoteParticipant extends Participant {

signalClient: SignalClient;

private volume?: number;
private volumeMap: Map<Track.Source, number>;

private audioContext?: AudioContext;

Expand All @@ -48,6 +48,7 @@ export default class RemoteParticipant extends Participant {
this.tracks = new Map();
this.audioTracks = new Map();
this.videoTracks = new Map();
this.volumeMap = new Map();
}

protected addTrackPublication(publication: RemoteTrackPublication) {
Expand Down Expand Up @@ -102,12 +103,17 @@ export default class RemoteParticipant extends Participant {
}

/**
* sets the volume on the participant's microphone track
* sets the volume on the participant's audio track
* by default, this affects the microphone publication
* a different source can be passed in as a second argument
* if no track exists the volume will be applied when the microphone track is added
*/
setVolume(volume: number) {
this.volume = volume;
const audioPublication = this.getTrack(Track.Source.Microphone);
setVolume(
volume: number,
source: Track.Source.Microphone | Track.Source.ScreenShareAudio = Track.Source.Microphone,
) {
this.volumeMap.set(source, volume);
const audioPublication = this.getTrack(source);
if (audioPublication && audioPublication.track) {
(audioPublication.track as RemoteAudioTrack).setVolume(volume);
}
Expand All @@ -116,12 +122,14 @@ export default class RemoteParticipant extends Participant {
/**
* gets the volume on the participant's microphone track
*/
getVolume() {
const audioPublication = this.getTrack(Track.Source.Microphone);
getVolume(
source: Track.Source.Microphone | Track.Source.ScreenShareAudio = Track.Source.Microphone,
) {
const audioPublication = this.getTrack(source);
if (audioPublication && audioPublication.track) {
return (audioPublication.track as RemoteAudioTrack).getVolume();
}
return this.volume;
return this.volumeMap.get(source);
}

/** @internal */
Expand Down Expand Up @@ -198,13 +206,9 @@ export default class RemoteParticipant extends Participant {
track.start();

publication.setTrack(track);
// set participant volume on new microphone tracks
if (
this.volume !== undefined &&
track instanceof RemoteAudioTrack &&
track.source === Track.Source.Microphone
) {
track.setVolume(this.volume);
// set participant volumes on new audio tracks
if (this.volumeMap.has(publication.source) && track instanceof RemoteAudioTrack) {
track.setVolume(this.volumeMap.get(publication.source)!);
}

return publication;
Expand Down

0 comments on commit db462ed

Please sign in to comment.