MCPcopy
hub / github.com/CapSoftware/Cap / useCapRecorder

Function useCapRecorder

packages/sdk-recorder/src/react/useCapRecorder.ts:5–90  ·  view source on GitHub ↗
(options: RecorderOptions)

Source from the content-addressed store, hash-verified

3import type { RecorderOptions, RecorderPhase, RecordingResult } from "../types";
4
5export function useCapRecorder(options: RecorderOptions) {
6 const { publicKey, apiBase, mode, userId, camera, microphone, systemAudio } =
7 options;
8 const cameraEnabled = camera?.enabled;
9 const cameraDeviceId = camera?.deviceId;
10 const microphoneEnabled = microphone?.enabled;
11 const microphoneDeviceId = microphone?.deviceId;
12 const recorderRef = useRef<CapRecorder | null>(null);
13 const [phase, setPhase] = useState<RecorderPhase>("idle");
14 const [videoId, setVideoId] = useState<string | null>(null);
15 const [durationMs, setDurationMs] = useState(0);
16
17 useEffect(() => {
18 const recorder = new CapRecorder({
19 publicKey,
20 apiBase,
21 mode,
22 userId,
23 camera:
24 cameraEnabled !== undefined || cameraDeviceId !== undefined
25 ? { enabled: cameraEnabled, deviceId: cameraDeviceId }
26 : undefined,
27 microphone:
28 microphoneEnabled !== undefined || microphoneDeviceId !== undefined
29 ? { enabled: microphoneEnabled, deviceId: microphoneDeviceId }
30 : undefined,
31 systemAudio,
32 });
33 recorderRef.current = recorder;
34
35 const unsubPhase = recorder.on("phasechange", (e) => {
36 setPhase(e.phase);
37 });
38 const unsubDuration = recorder.on("durationchange", (e) => {
39 setDurationMs(e.durationMs);
40 });
41 const unsubComplete = recorder.on("complete", (e) => {
42 setVideoId(e.videoId);
43 });
44
45 return () => {
46 unsubPhase();
47 unsubDuration();
48 unsubComplete();
49 recorder.destroy();
50 };
51 }, [
52 publicKey,
53 apiBase,
54 mode,
55 userId,
56 cameraEnabled,
57 cameraDeviceId,
58 microphoneEnabled,
59 microphoneDeviceId,
60 systemAudio,
61 ]);
62

Callers

nothing calls this directly

Calls 6

onMethod · 0.95
destroyMethod · 0.95
pauseMethod · 0.80
resumeMethod · 0.80
startMethod · 0.65
stopMethod · 0.65

Tested by

no test coverage detected