Skip to content

Commit 078395a

Browse files
committed
webrtc: add advanced audio settings
autoGainControl, echoCancellation, and noiseSuppression are audio processing options that are usually enabled by default on WebRTC input tracks. This commit adds the possibility to enable/disable them, as they can be undesirable in some cases (audiophile use cases). For example, one might want to stream electronic dance music, which is basically noise, so it should not be suppressed in that specific case. Note that these are not exact settings, they are set as "ideal" in order not to break anything on devices where those constraints are not implemented. Signed-off-by: László Várady <laszlo.varady@protonmail.com>
1 parent 059b07c commit 078395a

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

spec/unit/webrtc/mediaHandler.spec.ts

Lines changed: 17 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
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[] = [];
@@ -72,6 +79,17 @@ export class MediaHandler extends TypedEventEmitter<
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("LOG 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
@@ -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)