Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(chrome-extension): harden camera stream acquisition and add smooth hover reveal with generous hitbox grace period

ManthanNimodiya4d70061b5865
diff --git a/apps/chrome-extension/src/content/overlay.css b/apps/chrome-extension/src/content/overlay.css
index 662c4be7e..2202cb47c 100644
--- a/apps/chrome-extension/src/content/overlay.css
+++ b/apps/chrome-extension/src/content/overlay.css
@@ -1015,6 +1015,17 @@
color: #e5484d;
}
+.cap-extension-recording-badge::before {
+ content: "";
+ position: absolute;
+ top: -8px;
+ bottom: -8px;
+ left: -8px;
+ right: -8px;
+ z-index: -1;
+ pointer-events: auto;
+}
+
.cap-extension-vertical-dock {
position: absolute;
left: 0;
@@ -1035,6 +1046,17 @@
animation: cap-extension-dock-slide-up 220ms cubic-bezier(0.16, 1, 0.3, 1);
}
+.cap-extension-vertical-dock::before {
+ content: "";
+ position: absolute;
+ top: -12px;
+ bottom: -12px;
+ left: -12px;
+ right: -12px;
+ z-index: -1;
+ pointer-events: auto;
+}
+
.cap-extension-active-recording-container.opens-up
.cap-extension-vertical-dock {
bottom: calc(100% + 8px);
diff --git a/apps/chrome-extension/src/content/recording-bar.tsx b/apps/chrome-extension/src/content/recording-bar.tsx
index 781e2cde1..3367398c8 100644
--- a/apps/chrome-extension/src/content/recording-bar.tsx
+++ b/apps/chrome-extension/src/content/recording-bar.tsx
@@ -133,6 +133,7 @@ export function RecordingBarOverlay({
const positionModeRef = useRef<"active" | "ready" | null>(null);
const recorderPanelOpenRef = useRef(false);
const countdownIntervalRef = useRef<number | null>(null);
+ const hoverTimeoutRef = useRef<number | null>(null);
useEffect(() => {
planRef.current = plan;
@@ -462,6 +463,34 @@ export function RecordingBarOverlay({
};
}, [clampToViewport]);
+ useEffect(() => {
+ return () => {
+ if (hoverTimeoutRef.current !== null) {
+ window.clearTimeout(hoverTimeoutRef.current);
+ }
+ };
+ }, []);
+
+ const handleMouseEnter = useCallback(() => {
+ if (isDragging) return;
+ if (hoverTimeoutRef.current !== null) {
+ window.clearTimeout(hoverTimeoutRef.current);
+ hoverTimeoutRef.current = null;
+ }
+ setIsExpanded(true);
+ }, [isDragging]);
+
+ const handleMouseLeave = useCallback(() => {
+ if (isDragging) return;
+ if (hoverTimeoutRef.current !== null) {
+ window.clearTimeout(hoverTimeoutRef.current);
+ }
+ hoverTimeoutRef.current = window.setTimeout(() => {
+ setIsExpanded(false);
+ hoverTimeoutRef.current = null;
+ }, 450);
+ }, [isDragging]);
+
const sendControl = useCallback(
(type: BarControl) => {
setBusy(true);
@@ -656,6 +685,8 @@ export function RecordingBarOverlay({
<>
<div
ref={barRef}
+ role="toolbar"
+ aria-label="Active recording controls"
className={classNames(
"cap-extension-active-recording-container",
isDragging && "is-dragging",
@@ -669,6 +700,8 @@ export function RecordingBarOverlay({
visibility: position ? "visible" : "hidden",
}}
onPointerDown={handlePointerDown}
+ onMouseEnter={handleMouseEnter}
+ onMouseLeave={handleMouseLeave}
>
{isExpanded && status !== null ? (
<div
diff --git a/apps/chrome-extension/src/offscreen/recorder.ts b/apps/chrome-extension/src/offscreen/recorder.ts
index 340d029ea..6173df096 100644
--- a/apps/chrome-extension/src/offscreen/recorder.ts
+++ b/apps/chrome-extension/src/offscreen/recorder.ts
@@ -294,8 +294,11 @@ const disconnectCameraPreviews = () => {
};
const getCameraPreviewStream = async (settings: WebcamSettings) => {
+ const videoTrack = cameraPreviewStream?.getVideoTracks()[0];
if (
cameraPreviewStream?.active &&
+ videoTrack &&
+ videoTrack.readyState === "live" &&
cameraPreviewDeviceId === settings.deviceId
) {
return cameraPreviewStream;
@@ -377,38 +380,39 @@ const broadcastCaptureSource = (source: RecordingCaptureSource) => {
);
};
-const getVideoConstraint = (webcam: WebcamSettings) =>
- webcam.deviceId && webcam.deviceId !== DEFAULT_CAMERA_DEVICE_ID
- ? {
- deviceId: { exact: webcam.deviceId },
- }
- : true;
-
-const shouldRetryDefaultCamera = (webcam: WebcamSettings, error: unknown) =>
- webcam.deviceId !== null &&
- webcam.deviceId !== DEFAULT_CAMERA_DEVICE_ID &&
- error instanceof DOMException &&
- (error.name === "NotFoundError" || error.name === "OverconstrainedError");
-
const getCameraMediaStream = async (
webcam: WebcamSettings,
audio: boolean | MediaTrackConstraints,
) => {
- try {
- return await navigator.mediaDevices.getUserMedia({
- video: getVideoConstraint(webcam),
+ const constraints: MediaStreamConstraints[] = [];
+ if (webcam.deviceId && webcam.deviceId !== DEFAULT_CAMERA_DEVICE_ID) {
+ constraints.push({
+ video: { deviceId: { exact: webcam.deviceId } },
audio,
});
- } catch (error) {
- if (!shouldRetryDefaultCamera(webcam, error)) {
- throw error;
- }
-
- return navigator.mediaDevices.getUserMedia({
- video: true,
+ constraints.push({
+ video: { deviceId: { ideal: webcam.deviceId } },
audio,
});
}
+ constraints.push({
+ video: true,
+ audio,
+ });
+
+ let lastError: unknown = null;
+ for (const constraint of constraints) {
+ try {
+ const stream = await navigator.mediaDevices.getUserMedia(constraint);
+ const videoTrack = stream.getVideoTracks()[0];
+ if (videoTrack && videoTrack.readyState === "live") {
+ return stream;
+ }
+ } catch (error) {
+ lastError = error;
+ }
+ }
+ throw lastError ?? new Error("Failed to acquire camera stream");
};
const tabCaptureConstraints = (streamId: string, includeAudio: boolean) =>
diff --git a/apps/chrome-extension/src/preview/camera-preview.tsx b/apps/chrome-extension/src/preview/camera-preview.tsx
index 3d15920e9..da7741383 100644
--- a/apps/chrome-extension/src/preview/camera-preview.tsx
+++ b/apps/chrome-extension/src/preview/camera-preview.tsx
@@ -486,6 +486,13 @@ function App() {
streamRef.current = stream;
activeDeviceRef.current = settings.deviceId;
+ for (const track of stream.getVideoTracks()) {
+ track.addEventListener("unmute", () => {
+ void videoRef.current?.play().catch(() => undefined);
+ publishPreviewFrame();
+ });
+ }
+
if (videoRef.current) {
videoRef.current.srcObject = stream;
if (autoPictureInPictureEnabledRef.current) {
@@ -621,6 +628,10 @@ function App() {
}
}}
onLoadedData={publishPreviewFrame}
+ onCanPlay={() => {
+ void videoRef.current?.play().catch(() => undefined);
+ publishPreviewFrame();
+ }}
onPlaying={publishPreviewFrame}
/>
{isPictureInPictureSupported && !isInPictureInPicture ? (