Skip to content
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
6 changes: 5 additions & 1 deletion apps/web/src/audio/VoiceRecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@
return !MediaDeviceHandler.getAudioNoiseSuppression();
}

private async makeRecorder(): Promise<void> {

Check failure on line 104 in apps/web/src/audio/VoiceRecording.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=element-web&issues=AZ0bMXuYRtam6H6tI0Sa&open=AZ0bMXuYRtam6H6tI0Sa&pullRequest=32887
try {
const requestedDeviceId = MediaDeviceHandler.getAudioInput();
const deviceIdConstraint =
requestedDeviceId && requestedDeviceId !== "default" ? { deviceId: { exact: requestedDeviceId } } : {};

this.recorderStream = await navigator.mediaDevices.getUserMedia({
audio: {
channelCount: CHANNELS,
deviceId: MediaDeviceHandler.getAudioInput(),
...deviceIdConstraint,
Comment thread
Half-Shot marked this conversation as resolved.
autoGainControl: { ideal: MediaDeviceHandler.getAudioAutoGainControl() },
echoCancellation: { ideal: MediaDeviceHandler.getAudioEchoCancellation() },
noiseSuppression: { ideal: MediaDeviceHandler.getAudioNoiseSuppression() },
Expand Down
23 changes: 23 additions & 0 deletions apps/web/test/unit-tests/audio/VoiceRecording-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ describe("VoiceRecording", () => {
}),
);
});

it("should request the selected microphone as an exact device constraint", async () => {
MediaDeviceHandlerMock.getAudioInput.mockReturnValue("selected-mic");
await recording.start();

expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith(
expect.objectContaining({
audio: expect.objectContaining({ deviceId: { exact: "selected-mic" } }),
}),
);
});

it("should not force an exact microphone when default device is selected", async () => {
MediaDeviceHandlerMock.getAudioInput.mockReturnValue("default");
await recording.start();

const constraints = mocked(navigator.mediaDevices.getUserMedia).mock.calls[0][0] as MediaStreamConstraints;
expect(constraints.audio).toEqual(
expect.not.objectContaining({
deviceId: expect.anything(),
}),
);
});
});

describe("when recording", () => {
Expand Down
Loading