Skip to content

Commit a584324

Browse files
authored
webrtc: add advanced audio settings (#2434)
1 parent 059b07c commit a584324

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

spec/unit/webrtc/mediaHandler.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ describe('Media Handler', function() {
7979
}));
8080
});
8181

82+
it("sets audio settings", async () => {
83+
await mediaHandler.setAudioSettings({
84+
autoGainControl: false,
85+
echoCancellation: true,
86+
noiseSuppression: false,
87+
});
88+
89+
await mediaHandler.getUserMediaStream(true, false);
90+
expect(mockMediaDevices.getUserMedia).toHaveBeenCalledWith(expect.objectContaining({
91+
audio: expect.objectContaining({
92+
autoGainControl: { ideal: false },
93+
echoCancellation: { ideal: true },
94+
noiseSuppression: { ideal: false },
95+
}),
96+
}));
97+
});
98+
8299
it("sets video device ID", async () => {
83100
await mediaHandler.setVideoInput(FAKE_VIDEO_INPUT_ID);
84101

src/webrtc/mediaHandler.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@ export interface IScreensharingOpts {
4040
throwOnFail?: boolean;
4141
}
4242

43+
export interface AudioSettings {
44+
autoGainControl: boolean;
45+
echoCancellation: boolean;
46+
noiseSuppression: boolean;
47+
}
48+
4349
export class MediaHandler extends TypedEventEmitter<
4450
MediaHandlerEvent.LocalStreamsChanged, MediaHandlerEventHandlerMap
4551
> {
4652
private audioInput?: string;
53+
private audioSettings?: AudioSettings;
4754
private videoInput?: string;
4855
private localUserMediaStream?: MediaStream;
4956
public userMediaStreams: MediaStream[] = [];
@@ -64,21 +71,32 @@ export class MediaHandler extends TypedEventEmitter<
6471
* undefined treated as unset
6572
*/
6673
public async setAudioInput(deviceId: string): Promise<void> {
67-
logger.info("LOG setting audio input to", deviceId);
74+
logger.info("Setting audio input to", deviceId);
6875

6976
if (this.audioInput === deviceId) return;
7077

7178
this.audioInput = deviceId;
7279
await this.updateLocalUsermediaStreams();
7380
}
7481

82+
/**
83+
* Set audio settings for MatrixCalls
84+
* @param {AudioSettings} opts audio options to set
85+
*/
86+
public async setAudioSettings(opts: AudioSettings): Promise<void> {
87+
logger.info("Setting audio settings to", opts);
88+
89+
this.audioSettings = Object.assign({}, opts) as AudioSettings;
90+
await this.updateLocalUsermediaStreams();
91+
}
92+
7593
/**
7694
* Set a video input device to use for MatrixCalls
7795
* @param {string} deviceId the identifier for the device
7896
* undefined treated as unset
7997
*/
8098
public async setVideoInput(deviceId: string): Promise<void> {
81-
logger.info("LOG setting video input to", deviceId);
99+
logger.info("Setting video input to", deviceId);
82100

83101
if (this.videoInput === deviceId) return;
84102

@@ -362,6 +380,9 @@ export class MediaHandler extends TypedEventEmitter<
362380
audio: audio
363381
? {
364382
deviceId: this.audioInput ? { ideal: this.audioInput } : undefined,
383+
autoGainControl: this.audioSettings ? { ideal: this.audioSettings.autoGainControl } : undefined,
384+
echoCancellation: this.audioSettings ? { ideal: this.audioSettings.echoCancellation } : undefined,
385+
noiseSuppression: this.audioSettings ? { ideal: this.audioSettings.noiseSuppression } : undefined,
365386
}
366387
: false,
367388
video: video

0 commit comments

Comments
 (0)