Switch between cameras while sharing
The picture is swapped on the running stream, so viewers stay connected. The new camera is opened first where the device allows two at once; phones close the old one first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ export const ICON_PATHS: Record<string, string> = {
|
|||||||
'headphones-off': 'M3 14v-2a9 9 0 0 1 18 0v2M21 14v7h-4v-7zM3 14v7h4v-7zM3 3l18 18',
|
'headphones-off': 'M3 14v-2a9 9 0 0 1 18 0v2M21 14v7h-4v-7zM3 14v7h4v-7zM3 3l18 18',
|
||||||
settings: 'M4 21v-7M4 10V3M12 21v-9M12 8V3M20 21v-5M20 12V3M1 14h6M9 8h6M17 16h6',
|
settings: 'M4 21v-7M4 10V3M12 21v-9M12 8V3M20 21v-5M20 12V3M1 14h6M9 8h6M17 16h6',
|
||||||
plus: 'M12 5v14M5 12h14',
|
plus: 'M12 5v14M5 12h14',
|
||||||
|
switch: 'M4 8h13l-3-3M20 16H7l3 3',
|
||||||
camera: 'M2 6h14v12H2zM16 10l6-3v10l-6-3',
|
camera: 'M2 6h14v12H2zM16 10l6-3v10l-6-3',
|
||||||
screen: 'M3 4h18v12H3zM8 20h8M12 16v4',
|
screen: 'M3 4h18v12H3zM8 20h8M12 16v4',
|
||||||
popout: 'M14 4h6v6M20 4l-9 9M18 14v6H4V6h6',
|
popout: 'M14 4h6v6M20 4l-9 9M18 14v6H4V6h6',
|
||||||
|
|||||||
@@ -46,6 +46,9 @@ class ScreenShare {
|
|||||||
// Show our own camera mirrored to ourselves, as a mirror would. Not a camera pointing away
|
// Show our own camera mirrored to ourselves, as a mirror would. Not a camera pointing away
|
||||||
// from us (a phone's back camera), and never what the others get.
|
// from us (a phone's back camera), and never what the others get.
|
||||||
mirror = $state(false);
|
mirror = $state(false);
|
||||||
|
// How many cameras this device has; with more than one there is something to switch to
|
||||||
|
cameras = $state(0);
|
||||||
|
switching = $state(false);
|
||||||
viewers = $state(0);
|
viewers = $state(0);
|
||||||
// Streams we receive: one screen at a time, and any number of cameras next to it
|
// Streams we receive: one screen at a time, and any number of cameras next to it
|
||||||
watches = $state.raw<Watching[]>([]);
|
watches = $state.raw<Watching[]>([]);
|
||||||
@@ -146,6 +149,8 @@ class ScreenShare {
|
|||||||
this.usesVirtualMic = usesVirtualMic;
|
this.usesVirtualMic = usesVirtualMic;
|
||||||
this.kind = kind;
|
this.kind = kind;
|
||||||
this.mirror = kind === 'camera' && stream.getVideoTracks()[0]?.getSettings().facingMode !== 'environment';
|
this.mirror = kind === 'camera' && stream.getVideoTracks()[0]?.getSettings().facingMode !== 'environment';
|
||||||
|
this.cameras = 0;
|
||||||
|
if (kind === 'camera') navigator.mediaDevices.enumerateDevices().then(d => { this.cameras = d.filter(x => x.kind === 'videoinput').length; }).catch(() => {});
|
||||||
this.host = s;
|
this.host = s;
|
||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
this.send(s, this.others(s), SHARE.on, kind);
|
this.send(s, this.others(s), SHARE.on, kind);
|
||||||
@@ -153,6 +158,60 @@ class ScreenShare {
|
|||||||
sounds.play('shareStarted');
|
sounds.play('shareStarted');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The next camera of this device (front and back on a phone), without interrupting the stream
|
||||||
|
async switchCamera(): Promise<void> {
|
||||||
|
const stream = this.stream;
|
||||||
|
const old = stream?.getVideoTracks()[0];
|
||||||
|
if (!stream || !old || this.kind !== 'camera' || this.switching) return;
|
||||||
|
this.switching = true;
|
||||||
|
this.error = '';
|
||||||
|
const open = (deviceId: string) => navigator.mediaDevices.getUserMedia({
|
||||||
|
video: { deviceId: { exact: deviceId }, width: { ideal: 1280 }, height: { ideal: 720 }, frameRate: { ideal: QUALITY[store.settings.shareQuality].fps } }
|
||||||
|
}).catch(() => navigator.mediaDevices.getUserMedia({ video: { deviceId: { exact: deviceId } } }));
|
||||||
|
try {
|
||||||
|
const ids = (await navigator.mediaDevices.enumerateDevices()).filter(d => d.kind === 'videoinput').map(d => d.deviceId);
|
||||||
|
this.cameras = ids.length;
|
||||||
|
const current = old.getSettings().deviceId ?? '';
|
||||||
|
const next = ids[(ids.indexOf(current) + 1) % ids.length];
|
||||||
|
if (ids.length < 2 || !next || next === current) return;
|
||||||
|
let fresh: MediaStream;
|
||||||
|
try {
|
||||||
|
// Computers can have both open for a moment, which keeps the picture going
|
||||||
|
fresh = await open(next);
|
||||||
|
} catch {
|
||||||
|
// A phone opens one camera at a time, so the old one has to go first
|
||||||
|
old.stop();
|
||||||
|
try {
|
||||||
|
fresh = await open(next);
|
||||||
|
} catch (e) {
|
||||||
|
// Could not get the other one: back to the camera we had
|
||||||
|
fresh = await open(current).catch(() => { throw e; });
|
||||||
|
this.error = `Could not switch the camera: ${(e as Error).message}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Stopped sharing in the meantime
|
||||||
|
if (this.stream !== stream) return fresh.getTracks().forEach(t => t.stop());
|
||||||
|
const track = fresh.getVideoTracks()[0];
|
||||||
|
track.contentHint = 'motion';
|
||||||
|
track.addEventListener('ended', () => { if (this.stream?.getVideoTracks()[0] === track) this.stop(); });
|
||||||
|
// Viewers keep their connection; only the picture changes
|
||||||
|
for (const pc of this.peers.values()) {
|
||||||
|
for (const sender of pc.getSenders()) {
|
||||||
|
if (sender.track !== old) continue;
|
||||||
|
await sender.replaceTrack(track).catch(e => { this.error = `A viewer did not get the new camera: ${(e as Error).message}`; });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
old.stop();
|
||||||
|
this.stream = new MediaStream([track, ...stream.getAudioTracks()]);
|
||||||
|
this.mirror = track.getSettings().facingMode !== 'environment';
|
||||||
|
} catch (e) {
|
||||||
|
this.error = `The camera stopped: ${(e as Error).message}`;
|
||||||
|
this.stop();
|
||||||
|
} finally {
|
||||||
|
this.switching = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private release(): void {
|
private release(): void {
|
||||||
this.dropPeers();
|
this.dropPeers();
|
||||||
this.stream?.getTracks().forEach(t => t.stop());
|
this.stream?.getTracks().forEach(t => t.stop());
|
||||||
|
|||||||
@@ -237,6 +237,14 @@
|
|||||||
{#if voice.error && session.status === 'connected'}<div class="bar bad" role="alert"><span>Voice is not working: {voice.error}</span></div>{/if}
|
{#if voice.error && session.status === 'connected'}<div class="bar bad" role="alert"><span>Voice is not working: {voice.error}</span></div>{/if}
|
||||||
{#if share.error}<div class="bar bad" role="alert"><span>{share.error}</span><button class="btn" onclick={() => (share.error = '')}>Dismiss</button></div>{/if}
|
{#if share.error}<div class="bar bad" role="alert"><span>{share.error}</span><button class="btn" onclick={() => (share.error = '')}>Dismiss</button></div>{/if}
|
||||||
|
|
||||||
|
{#snippet switcher()}
|
||||||
|
{#if mine && share.kind === 'camera' && share.cameras > 1}
|
||||||
|
<button class="icon-btn" title="Switch to the next camera" aria-label="Switch camera" disabled={share.switching} onclick={() => share.switchCamera()}>
|
||||||
|
<Icon name="switch" size={18} />
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
{/snippet}
|
||||||
|
|
||||||
{#snippet spot()}
|
{#snippet spot()}
|
||||||
{#if self && focus != null && focused}
|
{#if self && focus != null && focused}
|
||||||
<div class="spot" role="region" aria-label="Screen share" bind:this={frame}>
|
<div class="spot" role="region" aria-label="Screen share" bind:this={frame}>
|
||||||
@@ -256,6 +264,7 @@
|
|||||||
{/if}
|
{/if}
|
||||||
{#if focus !== self.session}<button class="btn small" onclick={() => { if (focus != null) share.unwatch(focus); }}>Stop watching</button>
|
{#if focus !== self.session}<button class="btn small" onclick={() => { if (focus != null) share.unwatch(focus); }}>Stop watching</button>
|
||||||
{:else if videoOnly}
|
{:else if videoOnly}
|
||||||
|
{@render switcher()}
|
||||||
<span class="status" role="status" title="People watching your {share.kind}">{share.viewers} watching</span>
|
<span class="status" role="status" title="People watching your {share.kind}">{share.viewers} watching</span>
|
||||||
<button class="btn small" onclick={() => share.stop()}>Stop sharing</button>
|
<button class="btn small" onclick={() => share.stop()}>Stop sharing</button>
|
||||||
{/if}
|
{/if}
|
||||||
@@ -367,6 +376,7 @@
|
|||||||
</button>
|
</button>
|
||||||
{/if}
|
{/if}
|
||||||
{#if mine}
|
{#if mine}
|
||||||
|
{@render switcher()}
|
||||||
<span class="status" role="status" title="People watching your {share.kind}"><Icon name={share.kind} size={14} />{share.viewers} watching</span>
|
<span class="status" role="status" title="People watching your {share.kind}"><Icon name={share.kind} size={14} />{share.viewers} watching</span>
|
||||||
<button class="btn small" onclick={() => share.stop()}>Stop sharing</button>
|
<button class="btn small" onclick={() => share.stop()}>Stop sharing</button>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
+30
-1
@@ -22,7 +22,7 @@ async function launch() {
|
|||||||
const app = await electron.launch({
|
const app = await electron.launch({
|
||||||
executablePath: electronPath as unknown as string,
|
executablePath: electronPath as unknown as string,
|
||||||
args: [root, `--user-data-dir=${mkdtempSync(path.join(tmpdir(), 'mumh5-e2e-'))}`, '--ozone-platform=x11',
|
args: [root, `--user-data-dir=${mkdtempSync(path.join(tmpdir(), 'mumh5-e2e-'))}`, '--ozone-platform=x11',
|
||||||
'--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream'],
|
'--use-fake-device-for-media-stream=device-count=2', '--use-fake-ui-for-media-stream'],
|
||||||
env: env as Record<string, string>
|
env: env as Record<string, string>
|
||||||
});
|
});
|
||||||
const page = await app.firstWindow();
|
const page = await app.firstWindow();
|
||||||
@@ -311,6 +311,35 @@ try {
|
|||||||
await bob.page.locator('.stage .status', { hasText: '1 watching' }).waitFor();
|
await bob.page.locator('.stage .status', { hasText: '1 watching' }).waitFor();
|
||||||
if (process.env.SHOTS_DIR) await bob.page.screenshot({ path: `${process.env.SHOTS_DIR}/cams.png` });
|
if (process.env.SHOTS_DIR) await bob.page.screenshot({ path: `${process.env.SHOTS_DIR}/cams.png` });
|
||||||
|
|
||||||
|
// Switching cameras swaps the picture on the running stream. Chromium's second fake camera
|
||||||
|
// never delivers frames to an encoder (not even as the first camera), so the viewer is
|
||||||
|
// checked after switching there and back.
|
||||||
|
const ownCamera = () => alice.page.evaluate(() => ((document.querySelector('.stage .tile.self video') as HTMLVideoElement).srcObject as MediaStream).getVideoTracks()[0].label);
|
||||||
|
const firstCamera = await ownCamera();
|
||||||
|
const switchTo = async (different: boolean) => {
|
||||||
|
await alice.page.locator('.stage').getByRole('button', { name: 'Switch camera' }).click();
|
||||||
|
await alice.page.waitForFunction(([was, differs]) => {
|
||||||
|
const v = document.querySelector('.stage .tile.self video') as HTMLVideoElement | null;
|
||||||
|
const track = (v?.srcObject as MediaStream | null)?.getVideoTracks()[0];
|
||||||
|
return !!track && (track.label !== was) === differs && track.readyState === 'live' && v!.videoWidth > 0;
|
||||||
|
}, [firstCamera, different] as const, { timeout: 15000 });
|
||||||
|
await alice.page.getByRole('button', { name: 'Switch camera' }).and(alice.page.locator(':enabled')).waitFor();
|
||||||
|
};
|
||||||
|
await switchTo(true);
|
||||||
|
const secondCamera = await ownCamera();
|
||||||
|
await switchTo(false);
|
||||||
|
const fresh = await bob.page.evaluate(n => new Promise<number>(resolve => {
|
||||||
|
const v = document.querySelector(`.stage .tile[aria-label*="${n}"] video`) as HTMLVideoElement;
|
||||||
|
let count = 0;
|
||||||
|
const tick = () => { if (++count >= 8) resolve(count); else v.requestVideoFrameCallback(tick); };
|
||||||
|
v.requestVideoFrameCallback(tick);
|
||||||
|
setTimeout(() => resolve(count), 12000);
|
||||||
|
}), aliceName);
|
||||||
|
assert.ok(fresh >= 8, `viewer got ${fresh} new frames after switching cameras`);
|
||||||
|
await alice.page.locator('.stage .status', { hasText: '1 watching' }).waitFor();
|
||||||
|
assert.equal(await alice.page.locator('.bar.bad').count(), 0);
|
||||||
|
console.log(`ok: camera switched while sharing (${firstCamera} to ${secondCamera} and back), viewer kept the stream`);
|
||||||
|
|
||||||
// A click enlarges one camera; the statistics show it came through the relay
|
// A click enlarges one camera; the statistics show it came through the relay
|
||||||
await bob.page.locator('.stage .tile', { hasText: aliceName }).click();
|
await bob.page.locator('.stage .tile', { hasText: aliceName }).click();
|
||||||
await bob.page.waitForFunction(() => (document.querySelector('.spot video') as HTMLVideoElement | null)?.videoWidth! > 0, null, { timeout: 20000 });
|
await bob.page.waitForFunction(() => (document.querySelector('.spot video') as HTMLVideoElement | null)?.videoWidth! > 0, null, { timeout: 20000 });
|
||||||
|
|||||||
Reference in New Issue
Block a user