Skip to content

🎥 Make videos programmatically with React

fix: require selected recorder devices

Gaurav Saxena9855d0dd17f0
diff --git a/packages/template-recorder/src/Stream.tsx b/packages/template-recorder/src/Stream.tsx
index b4309afa9b..b8841bf970 100644
--- a/packages/template-recorder/src/Stream.tsx
+++ b/packages/template-recorder/src/Stream.tsx
@@ -26,7 +26,7 @@ export const Stream: React.FC<{
setResolution: React.Dispatch<React.SetStateAction<ResolutionAndFps | null>>;
recordAudio: boolean;
selectedVideoSource: SelectedSource | null;
- selectedAudioSource: ConstrainDOMString | null;
+ selectedAudioSource: string | null;
preferPortrait: boolean;
mirror: boolean;
clear: () => void;
diff --git a/packages/template-recorder/src/helpers/get-video-stream.ts b/packages/template-recorder/src/helpers/get-video-stream.ts
index 4d236d41d8..89392ade31 100644
--- a/packages/template-recorder/src/helpers/get-video-stream.ts
+++ b/packages/template-recorder/src/helpers/get-video-stream.ts
@@ -31,7 +31,9 @@ export const getCameraStreamConstraints = (
return null;
}
const video: MediaTrackConstraints = {
- deviceId: selectedVideoSource.deviceId,
+ // A string constraint is only an "ideal" preference. Use an exact
+ // constraint so Chrome cannot silently substitute the default webcam.
+ deviceId: { exact: selectedVideoSource.deviceId },
width: preferPortrait
? undefined
: selectedVideoSource.maxWidth
@@ -58,7 +60,7 @@ const getCameraStram = ({
selectedVideoSource: SelectedSource;
preferPortrait: boolean;
recordAudio: boolean;
- selectedAudioSource: ConstrainDOMString | null;
+ selectedAudioSource: string | null;
}): Promise<MediaStream> => {
if (selectedVideoSource.type !== "camera") {
throw new Error("Unknown video source type");
@@ -69,7 +71,7 @@ const getCameraStram = ({
video: video ?? undefined,
audio:
recordAudio && selectedAudioSource
- ? { deviceId: selectedAudioSource }
+ ? { deviceId: { exact: selectedAudioSource } }
: undefined,
};
@@ -85,7 +87,7 @@ export const getVideoStream = async ({
selectedVideoSource: SelectedSource;
preferPortrait: boolean;
recordAudio: boolean;
- selectedAudioSource: ConstrainDOMString | null;
+ selectedAudioSource: string | null;
}): Promise<MediaStream> => {
if (selectedVideoSource.type === "display-with-audio") {
return getDisplayStream(selectedVideoSource);
@@ -94,7 +96,7 @@ export const getVideoStream = async ({
const displayStream = await getDisplayStream(selectedVideoSource);
if (recordAudio && selectedAudioSource) {
const audioStream = await window.navigator.mediaDevices.getUserMedia({
- audio: { deviceId: selectedAudioSource },
+ audio: { deviceId: { exact: 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
new file mode 100644
index 0000000000..c259aa7fc9
--- /dev/null
+++ b/packages/template-recorder/tests/get-video-stream.test.ts
@@ -0,0 +1,19 @@
+import { expect, test } from "bun:test";
+import { getCameraStreamConstraints } from "../src/helpers/get-video-stream";
+
+test("requires the selected camera instead of accepting the browser default", () => {
+ expect(
+ getCameraStreamConstraints(
+ {
+ type: "camera",
+ deviceId: "camo-camera-id",
+ maxWidth: 1920,
+ maxHeight: 1080,
+ minFps: 30,
+ },
+ false,
+ ),
+ ).toMatchObject({
+ deviceId: { exact: "camo-camera-id" },
+ });
+});