Skip to content

🎥 Make videos programmatically with React

test(recorder): cover exact audio device constraints

Gaurav Saxena0fa257d1569c
diff --git a/packages/template-recorder/src/helpers/get-video-stream.ts b/packages/template-recorder/src/helpers/get-video-stream.ts
index 89392ade31..29584f2eb6 100644
--- a/packages/template-recorder/src/helpers/get-video-stream.ts
+++ b/packages/template-recorder/src/helpers/get-video-stream.ts
@@ -51,6 +51,22 @@ export const getCameraStreamConstraints = (
return video;
};
+export const getAudioStreamConstraints = ({
+ recordAudio,
+ selectedAudioSource,
+}: {
+ recordAudio: boolean;
+ selectedAudioSource: string | null;
+}): MediaTrackConstraints | undefined => {
+ if (!recordAudio || !selectedAudioSource) {
+ return undefined;
+ }
+
+ // A string constraint is only an "ideal" preference. Use an exact
+ // constraint so Chrome cannot silently substitute the default microphone.
+ return { deviceId: { exact: selectedAudioSource } };
+};
+
const getCameraStram = ({
selectedVideoSource,
preferPortrait,
@@ -69,10 +85,7 @@ const getCameraStram = ({
const mediaStreamConstraints: MediaStreamConstraints = {
video: video ?? undefined,
- audio:
- recordAudio && selectedAudioSource
- ? { deviceId: { exact: selectedAudioSource } }
- : undefined,
+ audio: getAudioStreamConstraints({ recordAudio, selectedAudioSource }),
};
return window.navigator.mediaDevices.getUserMedia(mediaStreamConstraints);
@@ -96,7 +109,7 @@ export const getVideoStream = async ({
const displayStream = await getDisplayStream(selectedVideoSource);
if (recordAudio && selectedAudioSource) {
const audioStream = await window.navigator.mediaDevices.getUserMedia({
- audio: { deviceId: { exact: selectedAudioSource } },
+ audio: getAudioStreamConstraints({ recordAudio, selectedAudioSource }),
});
return new MediaStream([
...displayStream.getVideoTracks(),
diff --git a/packages/template-recorder/tests/get-video-stream.test.ts b/packages/template-recorder/tests/get-video-stream.test.ts
index c259aa7fc9..7c29dd7324 100644
--- a/packages/template-recorder/tests/get-video-stream.test.ts
+++ b/packages/template-recorder/tests/get-video-stream.test.ts
@@ -1,5 +1,8 @@
import { expect, test } from "bun:test";
-import { getCameraStreamConstraints } from "../src/helpers/get-video-stream";
+import {
+ getAudioStreamConstraints,
+ getCameraStreamConstraints,
+} from "../src/helpers/get-video-stream";
test("requires the selected camera instead of accepting the browser default", () => {
expect(
@@ -17,3 +20,23 @@ test("requires the selected camera instead of accepting the browser default", ()
deviceId: { exact: "camo-camera-id" },
});
});
+
+test("requires the selected microphone instead of accepting the browser default", () => {
+ expect(
+ getAudioStreamConstraints({
+ recordAudio: true,
+ selectedAudioSource: "camo-microphone-id",
+ }),
+ ).toEqual({
+ deviceId: { exact: "camo-microphone-id" },
+ });
+});
+
+test("does not request a microphone when audio recording is disabled", () => {
+ expect(
+ getAudioStreamConstraints({
+ recordAudio: false,
+ selectedAudioSource: "camo-microphone-id",
+ }),
+ ).toBeUndefined();
+});