Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: `navigator.mediaDevices` undefined in macOS

mooyg40a1ea3d8daa
diff --git a/apps/desktop/src-tauri/Info.plist b/apps/desktop/src-tauri/Info.plist
new file mode 100644
index 000000000..fe253ec7b
--- /dev/null
+++ b/apps/desktop/src-tauri/Info.plist
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>NSCameraUsageDescription</key>
+ <string>Request camera access for WebRTC</string>
+ <key>NSMicrophoneUsageDescription</key>
+ <string>Request microphone access for WebRTC</string>
+</dict>
+</plist>
diff --git a/apps/desktop/src-tauri/Release.entitlements b/apps/desktop/src-tauri/Release.entitlements
new file mode 100644
index 000000000..f918e8d17
--- /dev/null
+++ b/apps/desktop/src-tauri/Release.entitlements
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+ <dict>
+ <key>com.apple.security.cs.allow-jit</key>
+ <true/>
+ <key>com.apple.security.cs.allow-unsigned-executable-memory</key>
+ <true/>
+ <key>com.apple.security.cs.disable-library-validation</key>
+ <true/>
+ <key>com.apple.security.cs.allow-dyld-environment-variables</key>
+ <true/>
+ <key>com.apple.security.automation.apple-events</key>
+ <true/>
+ <key>com.apple.security.device.usb</key>
+ <true/>
+ <key>com.apple.security.device.camera</key>
+ <true/>
+ <key>com.apple.security.network.server</key>
+ <true/>
+ <key>com.apple.security.network.client</key>
+ <true/>
+ <key>com.apple.security.inherit</key>
+ <true/>
+ </dict>
+</plist>
diff --git a/apps/desktop/src-tauri/src/main.rs b/apps/desktop/src-tauri/src/main.rs
index eff663fec..ada8c877b 100644
--- a/apps/desktop/src-tauri/src/main.rs
+++ b/apps/desktop/src-tauri/src/main.rs
@@ -7,3 +7,5 @@ fn main() {
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
+
+
diff --git a/apps/desktop/src-tauri/tauri.conf.json b/apps/desktop/src-tauri/tauri.conf.json
index bee6ae715..8d5abad29 100644
--- a/apps/desktop/src-tauri/tauri.conf.json
+++ b/apps/desktop/src-tauri/tauri.conf.json
@@ -34,6 +34,11 @@
"bundle": {
"active": true,
+ "macOS": {
+ "entitlements": "Release.entitlements",
+ "exceptionDomain": "",
+ "frameworks": []
+ },
"targets": "all",
"identifier": "com.cap.so",
"icon": [
diff --git a/apps/desktop/src/components/FloatingOptions.tsx b/apps/desktop/src/components/FloatingOptions.tsx
index 48930f510..dd5d8c602 100644
--- a/apps/desktop/src/components/FloatingOptions.tsx
+++ b/apps/desktop/src/components/FloatingOptions.tsx
@@ -1,6 +1,5 @@
import { useEffect, useState } from "react";
import { ReactMediaRecorder } from "@/utils/recording/client";
-import { showMenu } from "tauri-plugin-context-menu";
import { useMediaDevices } from "@/utils/recording/MediaDeviceContext";
import { Video } from "@/components/icons/Video";
import { Microphone } from "@/components/icons/Microphone";
@@ -10,6 +9,7 @@ import { Button } from "@/components/Button";
import { Logo } from "@/components/icons/Logo";
// import { Settings } from "@/components/icons/Settings";
import { emit } from "@tauri-apps/api/event";
+import { showMenu } from "tauri-plugin-context-menu";
export const FloatingOptions = () => {
const [foundDevices, setFoundDevices] = useState<MediaDeviceInfo[]>([]);
@@ -63,7 +63,7 @@ export const FloatingOptions = () => {
}
}
} catch (error) {
- console.error(error);
+ console.error("Couldn't fetch devices", error);
}
};
@@ -71,29 +71,37 @@ export const FloatingOptions = () => {
}, []);
const handleContextClick = async (option: string) => {
+ const filteredDevices = foundDevices
+ .filter((device) =>
+ option === "video"
+ ? device.kind === "videoinput"
+ : device.kind === "audioinput"
+ )
+ .map((device) => ({
+ label: device.label,
+ disabled:
+ option === "video"
+ ? device.deviceId === selectedVideoDevice?.deviceId
+ : device.deviceId === selectedAudioDevice?.deviceId,
+ event: async () => {
+ try {
+ await emit("change-device", { type: option, device });
+ } catch (error) {
+ console.error("Failed to emit change-device event:", error);
+ }
+ },
+ }));
+
+ //TODO: Some default item maybe. OR Show an error when filteredDevices length === 0
showMenu({
- items: [
- ...foundDevices
- .filter((device) =>
- option === "video"
- ? device.kind === "videoinput"
- : device.kind === "audioinput"
- )
- .map((device) => ({
- label: device.label,
- disabled:
- option === "video"
- ? device.deviceId === selectedVideoDevice?.deviceId
- : device.deviceId === selectedAudioDevice?.deviceId,
- event: async () => {
- try {
- await emit("change-device", { type: option, device });
- } catch (error) {
- console.error("Failed to emit change-device event:", error);
- }
- },
- })),
- ],
+ items: [...filteredDevices],
+ ...(filteredDevices.length === 0 && {
+ items: [
+ {
+ label: "Nothing found.",
+ },
+ ],
+ }),
});
};
diff --git a/apps/desktop/src/hooks/useDidMount.ts b/apps/desktop/src/hooks/useDidMount.ts
new file mode 100644
index 000000000..0170be79c
--- /dev/null
+++ b/apps/desktop/src/hooks/useDidMount.ts
@@ -0,0 +1,19 @@
+import { DependencyList, useEffect, useRef } from "react";
+
+// USE ONLY IF YOU DON'T HAVE A WAY TO UNMOUNT
+export const useDidMountEffect = (
+ func: () => void,
+ deps: DependencyList | undefined
+) => {
+ const didMount = useRef(false);
+
+ useEffect(() => {
+ if (didMount.current) {
+ func();
+ }
+ return () => {
+ didMount.current = true;
+ };
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, deps);
+};
diff --git a/apps/desktop/src/utils/recording/client.ts b/apps/desktop/src/utils/recording/client.ts
index 6d80b5b80..e10e1225b 100644
--- a/apps/desktop/src/utils/recording/client.ts
+++ b/apps/desktop/src/utils/recording/client.ts
@@ -6,6 +6,7 @@ import {
import { ReactElement, useCallback, useEffect, useRef, useState } from "react";
import { connect } from "extendable-media-recorder-wav-encoder";
import { useMediaDevices } from "./MediaDeviceContext";
+import { useDidMountEffect } from "@/hooks/useDidMount";
export type ReactMediaRecorderRenderProps = {
error: string;
@@ -90,7 +91,8 @@ export function useReactMediaRecorder({
const { selectedVideoDevice, selectedAudioDevice } = useMediaDevices();
- useEffect(() => {
+ // We don't have a unmount / unregister for this so we need to use `useDidMountEffect`
+ useDidMountEffect(() => {
const setup = async () => {
await register(await connect());
};