Skip to content

Commit

Permalink
webrtc: add advanced audio settings (#2434)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAnno authored Nov 8, 2022
1 parent 059b07c commit a584324
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
17 changes: 17 additions & 0 deletions spec/unit/webrtc/mediaHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ describe('Media Handler', function() {
}));
});

it("sets audio settings", async () => {
await mediaHandler.setAudioSettings({
autoGainControl: false,
echoCancellation: true,
noiseSuppression: false,
});

await mediaHandler.getUserMediaStream(true, false);
expect(mockMediaDevices.getUserMedia).toHaveBeenCalledWith(expect.objectContaining({
audio: expect.objectContaining({
autoGainControl: { ideal: false },
echoCancellation: { ideal: true },
noiseSuppression: { ideal: false },
}),
}));
});

it("sets video device ID", async () => {
await mediaHandler.setVideoInput(FAKE_VIDEO_INPUT_ID);

Expand Down
25 changes: 23 additions & 2 deletions src/webrtc/mediaHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ export interface IScreensharingOpts {
throwOnFail?: boolean;
}

export interface AudioSettings {
autoGainControl: boolean;
echoCancellation: boolean;
noiseSuppression: boolean;
}

export class MediaHandler extends TypedEventEmitter<
MediaHandlerEvent.LocalStreamsChanged, MediaHandlerEventHandlerMap
> {
private audioInput?: string;
private audioSettings?: AudioSettings;
private videoInput?: string;
private localUserMediaStream?: MediaStream;
public userMediaStreams: MediaStream[] = [];
Expand All @@ -64,21 +71,32 @@ export class MediaHandler extends TypedEventEmitter<
* undefined treated as unset
*/
public async setAudioInput(deviceId: string): Promise<void> {
logger.info("LOG setting audio input to", deviceId);
logger.info("Setting audio input to", deviceId);

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

this.audioInput = deviceId;
await this.updateLocalUsermediaStreams();
}

/**
* Set audio settings for MatrixCalls
* @param {AudioSettings} opts audio options to set
*/
public async setAudioSettings(opts: AudioSettings): Promise<void> {
logger.info("Setting audio settings to", opts);

this.audioSettings = Object.assign({}, opts) as AudioSettings;
await this.updateLocalUsermediaStreams();
}

/**
* Set a video input device to use for MatrixCalls
* @param {string} deviceId the identifier for the device
* undefined treated as unset
*/
public async setVideoInput(deviceId: string): Promise<void> {
logger.info("LOG setting video input to", deviceId);
logger.info("Setting video input to", deviceId);

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

Expand Down Expand Up @@ -362,6 +380,9 @@ export class MediaHandler extends TypedEventEmitter<
audio: audio
? {
deviceId: this.audioInput ? { ideal: this.audioInput } : undefined,
autoGainControl: this.audioSettings ? { ideal: this.audioSettings.autoGainControl } : undefined,
echoCancellation: this.audioSettings ? { ideal: this.audioSettings.echoCancellation } : undefined,
noiseSuppression: this.audioSettings ? { ideal: this.audioSettings.noiseSuppression } : undefined,
}
: false,
video: video
Expand Down

0 comments on commit a584324

Please sign in to comment.