Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"deps": "bun run --cwd ../api build && bun run --cwd ../moq/js/libav build",
"fix": "biome check --fix",
"tauri": "tauri",
"test": "bun test --isolate --conditions=browser --preload ./test/setup.ts src"
"test": "bun test --isolate --conditions=browser --preload ./test/setup.ts src",
"test:media": "bun run deps && tsc --project test/tsconfig.json && bun test ./test/media.browser.ts"
},
"license": "MIT",
"dependencies": {
Expand Down Expand Up @@ -63,6 +64,7 @@
"babel-preset-solid": "1.9.15",
"globals": "^17.4.0",
"madge": "^8.0.0",
"playwright": "1.64.1",
"rolldown": "^1.2.15",
"ts-expect": "^1.3.0",
"tsx": "^4.20.5",
Expand Down
74 changes: 74 additions & 0 deletions app/src/capture.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as Test from "bun:test";
import * as Signals from "@moq/signals";
import * as Capture from "./capture";

// Drain signal notifications without wall-clock sleeps or production deadlines.
async function flush(): Promise<void> {
for (let i = 0; i < 20; i++) await Promise.resolve();
}

function fixture() {
const enabled = new Signals.Signal(true);
const error = new Signals.Signal<Error | undefined>(new Error("Permission denied"));
const device = new Signals.Signal("first");
const constraints = new Signals.Signal({ width: 640 });
const retry = new Capture.Retry({ enabled, error, settings: [device, constraints] });
return { enabled, error, device, constraints, retry };
}

Test.test("retry waits for the capture to process disabled before enabling", async () => {
const capture = fixture();
try {
capture.retry.start();
await flush();
Test.expect(capture.enabled.peek()).toBe(false);
capture.error.set(undefined);
await flush();
Test.expect(capture.enabled.peek()).toBe(true);
} finally {
capture.retry.close();
}
});

Test.test("closed and superseded retries never re-enable a late acknowledgement", async () => {
for (const supersede of ["close", "enable", "device", "constraints"] as const) {
const capture = fixture();
try {
capture.retry.start();
await flush();
if (supersede === "close") capture.retry.close();
if (supersede === "enable") capture.enabled.set(true);
if (supersede === "device") capture.device.set("second");
if (supersede === "constraints") capture.constraints.set({ width: 1280 });
await flush();
capture.enabled.set(false);
capture.error.set(undefined);
await flush();
Test.expect(capture.enabled.peek()).toBe(false);
} finally {
capture.retry.close();
}
}
});

Test.test("a stale retry click cannot restart a recovered or disabled capture", async () => {
const capture = fixture();
try {
capture.error.set(undefined);
capture.retry.start();
Test.expect(capture.enabled.peek()).toBe(true);
capture.enabled.set(false);
capture.error.set(new Error("Old refusal"));
capture.retry.start();
capture.error.set(undefined);
await flush();
Test.expect(capture.enabled.peek()).toBe(false);
capture.retry.close();
capture.enabled.set(true);
capture.error.set(new Error("Old refusal"));
capture.retry.start();
Test.expect(capture.enabled.peek()).toBe(true);
} finally {
capture.retry.close();
}
});
46 changes: 46 additions & 0 deletions app/src/capture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as Signals from "@moq/signals";

// A failed capture refunds its retry budget only after processing enabled=false.
// Wait for its cleared error as that acknowledgement before enabling it again.
interface Props {
enabled: Signals.Signal<boolean>;
error: Signals.Getter<Error | undefined>;
settings: readonly Signals.Getter<unknown>[];
}

export class Retry {
#attempt: Signals.Effect | undefined;
#closed = false;
#props: Props;

constructor(props: Props) {
this.#props = props;
}

start(): void {
this.#attempt?.close();
const { enabled, error, settings } = this.#props;
if (this.#closed || !enabled.peek() || !error.peek()) return;

const attempt = new Signals.Effect();
this.#attempt = attempt;
const requested = settings.map((setting) => setting.peek());
for (const setting of settings) attempt.cleanup(setting.subscribe(() => attempt.close()));
attempt.cleanup(enabled.subscribe((value) => value && attempt.close()));
attempt.cleanup(
error.subscribe((value) => {
if (value || attempt.abort.aborted) return;
const current =
!enabled.peek() && settings.every((setting, index) => setting.peek() === requested[index]);
attempt.close();
if (current) enabled.set(true);
}),
);
enabled.set(false);
}

close(): void {
this.#closed = true;
this.#attempt?.close();
}
}
141 changes: 124 additions & 17 deletions app/src/controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {
createMemo,
createSelector,
createSignal,
createUniqueId,
Match,
onCleanup,
onMount,
Show,
Switch,
} from "solid-js";
import type { JSX } from "solid-js/jsx-runtime";
import * as Capture from "./capture";
import { MemeSelector } from "./components/meme-selector";
import Tooltip from "./components/tooltip";
import { Tutorial } from "./components/tutorial";
Expand Down Expand Up @@ -250,11 +252,60 @@ export function Controls(props: { room: Room; local: Local; canvas: Canvas }): J
);
}

function CaptureStatus(props: {
kind: "Camera" | "Microphone";
pending: boolean;
unavailable: boolean;
error: Error | undefined;
retry: () => void;
}): JSX.Element {
const status = createUniqueId();
return (
<>
<span id={status} role="status" class="sr-only" aria-atomic="true">
{props.error
? `${props.kind} could not start. ${props.error.message}`
: props.unavailable
? `No ${props.kind.toLowerCase()} found.`
: props.pending
? `Starting ${props.kind.toLowerCase()}…`
: ""}
</span>
<Show when={props.error}>
{(error) => (
<button
type="button"
onClick={props.retry}
title={`${props.kind} could not start. ${error().message}`}
class="self-center text-xs text-red-300 underline cursor-pointer"
aria-label={`Retry ${props.kind.toLowerCase()}`}
aria-describedby={status}
>
Retry
</button>
)}
</Show>
</>
);
}

export function Microphone(props: { local: Local }): JSX.Element {
const retry = new Capture.Retry({
enabled: props.local.core.microphoneEnabled,
error: props.local.microphone.out.error,
settings: [
Settings.microphone.device,
props.local.microphone.device.preferred,
props.local.microphone.constraints,
],
});
onCleanup(() => retry.close());
const toggle = () => {
props.local.core.microphoneEnabled.update((prev: boolean) => !prev);
};
const root = solid(props.local.camera.audio.root);
const source = solid(props.local.microphone.out.source);
const error = solid(props.local.microphone.out.error);

const [showMenu, setShowMenu] = createSignal(false);
const [deviceChangeIndicator, setDeviceChangeIndicator] = createSignal(false);
Expand All @@ -265,6 +316,17 @@ export function Microphone(props: { local: Local }): JSX.Element {
const device = props.local.microphone.device;
const enabled = solid(props.local.core.microphoneEnabled);
const available = solid(device.out.available);
const state = createMemo(() =>
!enabled()
? "off"
: error()
? "failed"
: source()
? "live"
: available()?.length === 0
? "unavailable"
: "pending",
);
const requested = createSelector(solid(device.out.requested));
const active = createSelector(solid(device.out.active));

Expand Down Expand Up @@ -315,8 +377,8 @@ export function Microphone(props: { local: Local }): JSX.Element {

// Handle device selection
const selectDevice = (deviceId: string) => {
if (root() && deviceId === Settings.microphone.device.peek()) {
// Same device selected and mic is enabled - disable it
if (enabled() && deviceId === Settings.microphone.device.peek()) {
// Selecting the requested device again disables even a pending capture.
props.local.core.microphoneEnabled.set(false);
Settings.microphone.device.set(undefined);
} else {
Expand All @@ -328,13 +390,22 @@ export function Microphone(props: { local: Local }): JSX.Element {

return (
<div class="flex items-start pointer-events-auto relative">
<Tooltip content={root() ? "Disable microphone" : "Enable microphone"} position="top">
<CaptureStatus
kind="Microphone"
pending={state() === "pending"}
unavailable={state() === "unavailable"}
error={enabled() ? error() : undefined}
retry={() => retry.start()}
/>
<Tooltip content={enabled() ? "Disable microphone" : "Enable microphone"} position="top">
<button
type="button"
onClick={toggle}
class="relative border hover:bg-gray-700 transition-all cursor-pointer p-2 backdrop-blur-sm bg-transparent rounded"
role="switch"
aria-checked={!!root()}
aria-checked={enabled()}
aria-busy={state() === "pending"}
data-state={state()}
aria-label="Toggle microphone"
classList={{
"border-white": !!root(),
Expand All @@ -343,10 +414,20 @@ export function Microphone(props: { local: Local }): JSX.Element {
}}
>
<Visualize audio={props.local.camera.audio} />
<span class={root() ? "icon-[mdi--microphone]" : "icon-[mdi--microphone-off]"} />
<span
class={
state() === "pending"
? "icon-[mdi--loading] animate-spin"
: state() === "failed"
? "icon-[mdi--microphone-message]"
: source()
? "icon-[mdi--microphone]"
: "icon-[mdi--microphone-off]"
}
/>
</button>
</Tooltip>
<Show when={root()}>
<Show when={enabled()}>
<Tooltip
content={deviceChangeMessage() || "Microphone settings"}
position="top"
Expand Down Expand Up @@ -462,11 +543,18 @@ export function Microphone(props: { local: Local }): JSX.Element {
}

export function Camera(props: { local: Local; room?: Room }): JSX.Element {
const retry = new Capture.Retry({
enabled: props.local.core.cameraEnabled,
error: props.local.webcam.out.error,
settings: [Settings.camera.device, props.local.webcam.device.preferred, props.local.webcam.constraints],
});
onCleanup(() => retry.close());
const toggle = () => {
props.local.core.cameraEnabled.update((prev: boolean) => !prev);
};
const media = solid(props.local.webcam.out.source);
const enabled = solid(props.local.core.cameraEnabled);
const error = solid(props.local.webcam.out.error);

const [showMenu, setShowMenu] = createSignal(false);
const [deviceChangeIndicator, setDeviceChangeIndicator] = createSignal(false);
Expand All @@ -476,6 +564,9 @@ export function Camera(props: { local: Local; room?: Room }): JSX.Element {
const device = props.local.webcam.device;
const available = solid(device.out.available);
const noCamera = createMemo(() => enabled() && available()?.length === 0 && !media());
const state = createMemo(() =>
!enabled() ? "off" : error() ? "failed" : noCamera() ? "unavailable" : media() ? "live" : "pending",
);
const requested = createSelector(solid(device.out.requested));
const active = createSelector(solid(device.out.active));

Expand Down Expand Up @@ -527,8 +618,8 @@ export function Camera(props: { local: Local; room?: Room }): JSX.Element {

// Handle device selection
const selectDevice = (deviceId: string) => {
if (media() && deviceId === Settings.camera.device.peek()) {
// Same device selected and camera is enabled - disable it
if (enabled() && deviceId === Settings.camera.device.peek()) {
// Selecting the requested device again disables even a pending capture.
props.local.core.cameraEnabled.set(false);
Settings.camera.device.set(undefined);
} else {
Expand All @@ -540,34 +631,50 @@ export function Camera(props: { local: Local; room?: Room }): JSX.Element {

return (
<div class="flex items-start pointer-events-auto relative">
<Show when={noCamera()}>
<div
role="status"
class="absolute bottom-full mb-2 left-0 w-56 rounded border border-white/30 bg-black/90 p-2 text-sm leading-snug text-white shadow-lg"
>
<Show when={noCamera() && !error()}>
<div class="absolute bottom-full mb-2 left-0 w-56 rounded border border-white/30 bg-black/90 p-2 text-sm leading-snug text-white shadow-lg">
No camera found. Connect a webcam to turn on video.
</div>
</Show>
<CaptureStatus
kind="Camera"
pending={state() === "pending" && !noCamera()}
unavailable={state() === "unavailable"}
error={enabled() ? error() : undefined}
retry={() => retry.start()}
/>
<Tooltip
content={noCamera() ? "No camera found" : media() ? "Disable camera" : "Enable camera"}
content={noCamera() ? "No camera found" : enabled() ? "Disable camera" : "Enable camera"}
position="top"
>
<button
type="button"
onClick={toggle}
class="relative border hover:bg-gray-700 transition-all cursor-pointer p-2 backdrop-blur-sm bg-transparent rounded"
role="switch"
aria-checked={!!media()}
aria-checked={enabled()}
aria-busy={state() === "pending"}
data-state={state()}
aria-label="Toggle camera"
classList={{
"border-white": !!media(),
"border-transparent": !media(),
}}
>
<span class={media() ? "icon-[mdi--camera]" : "icon-[mdi--camera-off]"} />
<span
class={
state() === "pending"
? "icon-[mdi--loading] animate-spin"
: state() === "failed"
? "icon-[mdi--camera-off-outline]"
: media()
? "icon-[mdi--camera]"
: "icon-[mdi--camera-off]"
}
/>
</button>
</Tooltip>
<Show when={media() && (available()?.length ?? 0) > 1}>
<Show when={enabled() && (available()?.length ?? 0) > 1}>
<Tooltip
content={deviceChangeMessage() || "Camera settings"}
position="top"
Expand Down
Loading