Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(MediaStream): cleanup unused media stream (#106)

RiNeba8bd95af25
diff --git a/apps/desktop/src/routes/camera.tsx b/apps/desktop/src/routes/camera.tsx
index b9f54d405..08a11ec83 100644
--- a/apps/desktop/src/routes/camera.tsx
+++ b/apps/desktop/src/routes/camera.tsx
@@ -10,7 +10,6 @@ import {
Show,
Suspense,
createEffect,
- createResource,
createSignal,
on,
onCleanup,
@@ -33,7 +32,7 @@ namespace CameraWindow {
};
}
-export default function () {
+export default function() {
const options = createOptionsQuery();
const camera = createCameraForLabel(() => options.data?.cameraLabel ?? "");
@@ -254,16 +253,19 @@ function createCameraPreview(deviceId: () => string | undefined) {
} else if (ref) {
ref.srcObject = null;
}
- });
- // Cleanup effect
- onCleanup(() => {
- const stream = cameraStream();
- if (stream) {
- stream.getTracks().forEach((track) => track.stop());
- }
+ // Cleanup effect
+ onCleanup(() => {
+ if (stream) {
+ stream.getTracks().forEach((track) => track.stop());
+ }
+ if (ref?.srcObject) {
+ ref.srcObject = null;
+ }
+ });
});
+
return [setCameraRef, () => cameraStream()] as const;
}
diff --git a/apps/desktop/src/utils/media.ts b/apps/desktop/src/utils/media.ts
index 0dadee447..375e63022 100644
--- a/apps/desktop/src/utils/media.ts
+++ b/apps/desktop/src/utils/media.ts
@@ -11,8 +11,12 @@ import { commands } from "./tauri";
export function createDevices() {
const [devices, { refetch }] = createResource(async () => {
try {
- await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
- return await navigator.mediaDevices.enumerateDevices();
+ const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
+ const result = await navigator.mediaDevices.enumerateDevices();
+ // this is not a better solution, since `navigator.mediaDevices.getUserMedia` is always return a new instance of the stream
+ // we need to clean up the stream because we just want to get the devices.
+ stream.getTracks().forEach(track => track.stop())
+ return result
} catch (error) {
console.error("Error accessing media devices:", error);
return [];
@@ -75,8 +79,8 @@ export async function stopMediaStream(kind: "audio" | "video" | "both") {
streams.getTracks().forEach((track) => {
if ((kind === "audio" && track.kind === "audio") ||
- (kind === "video" && track.kind === "video") ||
- kind === "both") {
+ (kind === "video" && track.kind === "video") ||
+ kind === "both") {
track.stop();
}
});
diff --git a/apps/desktop/src/utils/queries.ts b/apps/desktop/src/utils/queries.ts
index a26fdb247..0f19939f3 100644
--- a/apps/desktop/src/utils/queries.ts
+++ b/apps/desktop/src/utils/queries.ts
@@ -70,12 +70,16 @@ export function createVideoDevicesQuery() {
const options = () => queryOptions({
queryKey: ["videoDevices"] as const,
queryFn: async () => {
- await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
+ const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
const browserDevices = await navigator.mediaDevices.enumerateDevices();
const rustCameras = await commands.listCameras();
- return browserDevices.filter(device => device.kind === "videoinput"
+ const result = browserDevices.filter(device => device.kind === "videoinput"
&& rustCameras.some(rDevice => rDevice === device.label)
)
+ // this is not a better solution, since `navigator.mediaDevices.getUserMedia` is always return a new instance of the stream
+ // we need to clean up the stream because we just want to get the devices.
+ stream.getTracks().forEach(track => track.stop())
+ return result
},
initialData: [],
reconcile: (oldData, newData) => permissions?.data?.camera === 'granted' ? newData