Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webrtc: add advanced audio settings #2434

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
MrAnno marked this conversation as resolved.
Show resolved Hide resolved

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