@limrun/ui 0.13.3 → 0.13.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -140,6 +140,157 @@ interface RemoteControlProps {
140
140
  * @default 2000
141
141
  */
142
142
  axMaxBackoffMs?: number;
143
+
144
+ /**
145
+ * Fires whenever the iOS simulator's camera demand state changes —
146
+ * i.e. an app inside the sim called
147
+ * `[AVCaptureSession startRunning]` or `[stopRunning]`. The
148
+ * component handles the `navigator.mediaDevices.getUserMedia` prompt
149
+ * and SDP plumbing internally; this callback is purely so the host
150
+ * UI can render a status indicator ("simulator is using your
151
+ * camera", etc.).
152
+ *
153
+ * `active` reflects whether the sim is currently asking for
154
+ * frames. `granted` is set only on the call that follows a
155
+ * `getUserMedia` attempt: `true` when the user accepted the
156
+ * browser prompt, `false` when they denied or the call failed
157
+ * (in which case the limulator side switches to a black-frame
158
+ * fallback so the app keeps ticking).
159
+ *
160
+ * `camera` (optional, only meaningful when `granted === true`)
161
+ * carries what `MediaStreamTrack.getSettings()` reported for the
162
+ * active capture: resolution, framerate, device label, facing
163
+ * mode. Use it to render a richer status indicator (e.g.
164
+ * "Camera · 1920×1080 · 30 fps · FaceTime HD").
165
+ *
166
+ * Only iOS instances ever fire this callback; Android instances
167
+ * have no camera-injector path and stay silent.
168
+ */
169
+ onCameraDemandChange?: (active: boolean, granted?: boolean, camera?: CameraCaptureInfo) => void;
170
+
171
+ /**
172
+ * Periodically (~1Hz) fires with a snapshot of the outbound
173
+ * camera stream's live WebRTC stats — codec, encoder
174
+ * implementation, hardware acceleration, encoded fps, bitrate,
175
+ * round-trip-time, and the encoder's
176
+ * `qualityLimitationReason` (which is what Meet/Zoom use to
177
+ * decide whether to show "Bandwidth limited" or "CPU limited"
178
+ * banners).
179
+ *
180
+ * Only fires while the simulator is actively pulling the
181
+ * camera AND `getUserMedia` was granted. `null` is emitted
182
+ * once when the stream goes back to idle so consumers can
183
+ * clear their UI without having to maintain their own
184
+ * timeout. The host app does not need to poll `getStats()`
185
+ * itself — this is the canonical place.
186
+ */
187
+ onCameraStats?: (stats: CameraStreamStats | null) => void;
188
+
189
+ /**
190
+ * Optional resolution cap for outbound camera capture.
191
+ *
192
+ * - `'auto'` (default): no extra constraint. The browser captures
193
+ * at its webcam's native max; WebRTC's quality scaler may step
194
+ * down resolution on the encoder side under bandwidth pressure
195
+ * while still feeding the simulator at the pool's native size.
196
+ * - `'1080p'` / `'720p'` / `'480p'`: hard cap applied via
197
+ * `getUserMedia` constraints (for new captures) and
198
+ * `track.applyConstraints` (for the currently-active track),
199
+ * matching the way Meet/Zoom expose a "Send resolution" picker.
200
+ *
201
+ * Bumping or lowering the cap mid-stream is supported; the
202
+ * change takes effect within a frame or two as the webcam
203
+ * re-negotiates.
204
+ */
205
+ cameraResolutionCap?: CameraResolutionCap;
206
+ /**
207
+ * Aspect ratio the simulator's virtual camera should report to apps.
208
+ * Picking a value here triggers a `cameraAspect` WS message to the
209
+ * host, which rebuilds its IOSurface ring at the matching dimensions
210
+ * (16:9 → 1920×1080, 4:3 → 1440×1080, 1:1 → 1080×1080, 9:16 →
211
+ * 1080×1920) and signals the in-sim dylib to re-handshake. iOS apps
212
+ * see CMSampleBuffers at the new dimensions within a frame or two.
213
+ *
214
+ * The browser still captures whatever the webcam offers; the host
215
+ * aspect-fills (cover, center-crop) into the new pool. Switching
216
+ * aspect at runtime is intentionally cheap so users can A/B preview
217
+ * styles without restarting the simulator.
218
+ *
219
+ * `undefined` leaves the pool untouched (the host's boot default —
220
+ * 16:9 / 1920×1080 — applies).
221
+ */
222
+ cameraAspect?: CameraAspect;
223
+ }
224
+
225
+ /**
226
+ * Resolution caps a host app can request on the outbound camera.
227
+ * `'auto'` is "let the browser decide" (no constraints beyond the
228
+ * 30 fps ceiling); the other options clamp width/height to the
229
+ * named target. Aspect ratio is preserved.
230
+ */
231
+ export type CameraResolutionCap = 'auto' | '1080p' | '720p' | '480p';
232
+
233
+ /**
234
+ * Aspect ratios exposed to the operator for the simulated camera.
235
+ * The host maps each label to concrete IOSurface dimensions; values
236
+ * the host doesn't recognise are silently ignored.
237
+ */
238
+ export type CameraAspect = '16:9' | '4:3' | '1:1' | '9:16';
239
+
240
+ /**
241
+ * Snapshot of the browser's webcam capture, mirrored from
242
+ * `MediaStreamTrack.getSettings()`. Forwarded to the host alongside
243
+ * `cameraResult` and exposed to the host app via
244
+ * `onCameraDemandChange` so it can render a status indicator without
245
+ * having to call `getStats()` itself.
246
+ */
247
+ export interface CameraCaptureInfo {
248
+ width?: number;
249
+ height?: number;
250
+ frameRate?: number;
251
+ deviceId?: string;
252
+ label?: string;
253
+ facingMode?: string;
254
+ }
255
+
256
+ /**
257
+ * Live outbound-camera quality snapshot. Derived from
258
+ * `RTCPeerConnection.getStats()` and rate-derived deltas, sampled
259
+ * once per second while the camera is sending. All fields optional:
260
+ * some browsers omit fields (Safari rarely reports
261
+ * `encoderImplementation`), and the first sample after camera start
262
+ * has no delta-derived numbers yet.
263
+ */
264
+ export interface CameraStreamStats {
265
+ /** "H264", "HEVC"/"H265", "VP9", "VP8", "AV1", etc. (uppercased). */
266
+ codec?: string;
267
+ /** e.g. "VideoToolbox" (hw), "OpenH264" (sw), "ExternalEncoder". */
268
+ encoderImplementation?: string;
269
+ /**
270
+ * Browser-reported hardware-acceleration hint. Some Chromium
271
+ * versions expose this via `powerEfficientEncoder`; we mirror it
272
+ * here so consumers don't have to know the spec quirk.
273
+ */
274
+ hardwareAccelerated?: boolean;
275
+ /** Outbound encoded fps over the last sample window. */
276
+ framesPerSecond?: number;
277
+ /** Cumulative encoded frame count. */
278
+ framesEncoded?: number;
279
+ /** Outbound encoded bitrate (bits/s) over the last window. */
280
+ bitrateBps?: number;
281
+ /** Width/height the encoder is currently producing. */
282
+ width?: number;
283
+ height?: number;
284
+ /**
285
+ * One of `'none' | 'cpu' | 'bandwidth' | 'other'`. Mirrors
286
+ * Meet/Zoom's "limited by …" banners. Anything other than
287
+ * `'none'` means the encoder dropped resolution to keep up.
288
+ */
289
+ qualityLimitationReason?: string;
290
+ /** Round-trip time in milliseconds, from the matching RTCP. */
291
+ rttMs?: number;
292
+ /** Packet-loss percentage over the last sample window (0..100). */
293
+ packetsLostPct?: number;
143
294
  }
144
295
 
145
296
  interface ScreenshotData {
@@ -338,6 +489,10 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
338
489
  onAxStatusChange,
339
490
  axPollIntervalMs,
340
491
  axMaxBackoffMs,
492
+ onCameraDemandChange,
493
+ onCameraStats,
494
+ cameraResolutionCap = 'auto',
495
+ cameraAspect,
341
496
  }: RemoteControlProps,
342
497
  ref,
343
498
  ) => {
@@ -363,6 +518,58 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
363
518
  // Mirrored to a ref so stale closures in event handlers see the latest value.
364
519
  const autoReconnectRef = useRef(autoReconnect);
365
520
  autoReconnectRef.current = autoReconnect;
521
+ // Demand-driven outbound camera state.
522
+ //
523
+ // The limulator side broadcasts a `cameraRequest` WS message whenever
524
+ // an app inside the simulator opens/closes an `AVCaptureSession`.
525
+ // We respond by calling `navigator.mediaDevices.getUserMedia(...)`
526
+ // and `replaceTrack`ing the result onto a pre-allocated sendonly
527
+ // video transceiver. Pre-allocating the transceiver lets us
528
+ // attach/detach the local camera without renegotiating the SDP
529
+ // (the slot is already in the answer's a=video block, just with
530
+ // `inactive`/empty until we install the track).
531
+ //
532
+ // Refs (not state) because all callers live inside event-handler
533
+ // closures and the ref read happens during message processing, not
534
+ // during render. We keep both the sender and the active local
535
+ // stream so teardown can stop tracks without leaking the camera
536
+ // green light.
537
+ const outboundCameraSenderRef = useRef<RTCRtpSender | null>(null);
538
+ const outboundLocalStreamRef = useRef<MediaStream | null>(null);
539
+ // Bumped on every `cameraRequest` so a handler suspended on an
540
+ // await (e.g. the getUserMedia prompt) can detect it was superseded
541
+ // by a newer request and bail instead of re-attaching the camera.
542
+ const cameraRequestGenerationRef = useRef(0);
543
+ const cameraResolutionCapRef = useRef(cameraResolutionCap);
544
+ cameraResolutionCapRef.current = cameraResolutionCap;
545
+ // The aspect prop also rides a ref so the WS `onopen` reconnect
546
+ // path can replay the operator's last pick without depending on
547
+ // an in-flight render cycle. We mutate it inline (same render-
548
+ // time pattern as the cap ref) so a parent prop change is visible
549
+ // to closures captured during the next render; the useEffect
550
+ // below handles the actual "send to host" side-effect.
551
+ const cameraAspectRef = useRef<CameraAspect | undefined>(cameraAspect);
552
+ cameraAspectRef.current = cameraAspect;
553
+ // Mirror the demand-change callback into a ref so the WS message
554
+ // handler always sees the freshest customer callback even when the
555
+ // parent re-renders mid-session.
556
+ const onCameraDemandChangeRef = useRef(onCameraDemandChange);
557
+ onCameraDemandChangeRef.current = onCameraDemandChange;
558
+ const onCameraStatsRef = useRef(onCameraStats);
559
+ onCameraStatsRef.current = onCameraStats;
560
+ // Active outbound-camera stats poller. While the camera is
561
+ // sending we sample `RTCPeerConnection.getStats()` once per
562
+ // second, derive deltas (bitrate, fps, packet loss) from the
563
+ // previous sample, and push the result through `onCameraStats`.
564
+ // Cleared on teardown so we never leak the timer.
565
+ const cameraStatsTimerRef = useRef<ReturnType<typeof setInterval> | null>(null);
566
+ const cameraStatsPrevRef = useRef<{
567
+ timestamp: number;
568
+ framesEncoded?: number;
569
+ bytesSent?: number;
570
+ packetsSent?: number;
571
+ packetsLost?: number;
572
+ } | null>(null);
366
573
  const firstFrameShownRef = useRef(false);
367
574
  const pendingScreenshotResolversRef = useRef<
368
575
  Map<string, (value: ScreenshotData | PromiseLike<ScreenshotData>) => void>
@@ -1435,6 +1642,169 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
1435
1642
  setVideoLoaded(true);
1436
1643
  };
1437
1644
 
1645
+ // Stop every track on a MediaStream and release the device handle.
1646
+ // The browser keeps the camera "on" indicator lit until at least
1647
+ // one track on the underlying source is stopped, so we have to
1648
+ // call stop() explicitly — closing the peer connection alone
1649
+ // won't do it.
1650
+ const stopMediaStream = (stream: MediaStream) => {
1651
+ for (const track of stream.getTracks()) {
1652
+ try {
1653
+ track.stop();
1654
+ } catch (err) {
1655
+ debugWarn('track.stop() failed:', err);
1656
+ }
1657
+ }
1658
+ };
1659
+
1660
+ // Stop and forget any currently-attached outbound camera stream.
1661
+ // Safe to call when nothing is attached. Does not touch the
1662
+ // sender — the caller is responsible for `replaceTrack(null)`
1663
+ // separately when it wants the SDP slot itself empty.
1664
+ const stopOutboundLocalStream = () => {
1665
+ const stream = outboundLocalStreamRef.current;
1666
+ if (!stream) return;
1667
+ outboundLocalStreamRef.current = null;
1668
+ stopMediaStream(stream);
1669
+ };
1670
+
1671
+ // Translate the user-facing resolution cap into a
1672
+ // MediaTrackConstraints fragment we can feed `getUserMedia`
1673
+ // or `applyConstraints`. Always returns a 30 fps ceiling so
1674
+ // a 60 fps webcam doesn't double our encoder cost for no
1675
+ // visible win (the simulator pool is paced at 30).
1676
+ //
1677
+ // We use `ideal` rather than `max` so that a webcam with a
1678
+ // native 720p mode still hands us 720p when the user picks
1679
+ // 1080p — instead of refusing the constraint outright. The
1680
+ // browser's NotReadableError on a too-strict `max` is the
1681
+ // most common camera-permission gotcha in the wild.
1682
+ const cameraCapToConstraints = (cap: CameraResolutionCap): MediaTrackConstraints => {
1683
+ const base: MediaTrackConstraints = {
1684
+ frameRate: { ideal: 30, max: 30 },
1685
+ };
1686
+ switch (cap) {
1687
+ case '1080p':
1688
+ return { ...base, width: { ideal: 1920 }, height: { ideal: 1080 } };
1689
+ case '720p':
1690
+ return { ...base, width: { ideal: 1280 }, height: { ideal: 720 } };
1691
+ case '480p':
1692
+ return { ...base, width: { ideal: 854 }, height: { ideal: 480 } };
1693
+ case 'auto':
1694
+ default:
1695
+ return base;
1696
+ }
1697
+ };
1698
+
1699
+ // Convert the codec mime type (e.g. "video/H264" or "video/HEVC")
1700
+ // into a short uppercase label suitable for a status badge.
1701
+ const shortenCodecMime = (mime: string | undefined): string | undefined => {
1702
+ if (!mime) return undefined;
1703
+ const slash = mime.indexOf('/');
1704
+ const tail = slash >= 0 ? mime.slice(slash + 1) : mime;
1705
+ const upper = tail.toUpperCase();
1706
+ if (upper === 'HEVC') return 'H265';
1707
+ return upper;
1708
+ };
1709
+
1710
+ // Sample outbound-camera stats once and push a normalised
1711
+ // snapshot through `onCameraStats`. Skips silently if the sender
1712
+ // has been torn down between intervals.
1713
+ const sampleOutboundCameraStats = async () => {
1714
+ const sender = outboundCameraSenderRef.current;
1715
+ const handler = onCameraStatsRef.current;
1716
+ if (!sender || !handler) return;
1717
+ let report: RTCStatsReport;
1718
+ try {
1719
+ report = await sender.getStats();
1720
+ } catch {
1721
+ return;
1722
+ }
1723
+ let outbound: any | undefined;
1724
+ let codecMime: string | undefined;
1725
+ let remoteInbound: any | undefined;
1726
+ report.forEach((entry: any) => {
1727
+ if (entry.type === 'outbound-rtp' && entry.kind === 'video') {
1728
+ outbound = entry;
1729
+ } else if (entry.type === 'remote-inbound-rtp' && entry.kind === 'video') {
1730
+ remoteInbound = entry;
1731
+ }
1732
+ });
1733
+ if (outbound?.codecId) {
1734
+ const codec = report.get(outbound.codecId);
1735
+ if (codec) codecMime = (codec as any).mimeType;
1736
+ }
1737
+ if (!outbound) return;
1738
+ const now = (outbound.timestamp as number) ?? Date.now();
1739
+ const prev = cameraStatsPrevRef.current;
1740
+ let fps: number | undefined;
1741
+ let bitrate: number | undefined;
1742
+ let lossPct: number | undefined;
1743
+ if (prev && now > prev.timestamp) {
1744
+ const dt = (now - prev.timestamp) / 1000;
1745
+ if (typeof outbound.framesEncoded === 'number' && typeof prev.framesEncoded === 'number') {
1746
+ fps = Math.max(0, (outbound.framesEncoded - prev.framesEncoded) / dt);
1747
+ }
1748
+ if (typeof outbound.bytesSent === 'number' && typeof prev.bytesSent === 'number') {
1749
+ bitrate = Math.max(0, ((outbound.bytesSent - prev.bytesSent) * 8) / dt);
1750
+ }
1751
+ if (
1752
+ typeof outbound.packetsSent === 'number' &&
1753
+ typeof prev.packetsSent === 'number' &&
1754
+ remoteInbound &&
1755
+ typeof remoteInbound.packetsLost === 'number' &&
1756
+ typeof prev.packetsLost === 'number'
1757
+ ) {
1758
+ const sent = outbound.packetsSent - prev.packetsSent;
1759
+ const lost = remoteInbound.packetsLost - prev.packetsLost;
1760
+ if (sent > 0) lossPct = Math.max(0, Math.min(100, (lost / sent) * 100));
1761
+ }
1762
+ }
1763
+ cameraStatsPrevRef.current = {
1764
+ timestamp: now,
1765
+ framesEncoded: outbound.framesEncoded,
1766
+ bytesSent: outbound.bytesSent,
1767
+ packetsSent: outbound.packetsSent,
1768
+ packetsLost: remoteInbound?.packetsLost,
1769
+ };
1770
+ const stats: CameraStreamStats = {
1771
+ codec: shortenCodecMime(codecMime),
1772
+ encoderImplementation: outbound.encoderImplementation,
1773
+ hardwareAccelerated:
1774
+ typeof outbound.powerEfficientEncoder === 'boolean' ? outbound.powerEfficientEncoder : undefined,
1775
+ framesPerSecond: fps,
1776
+ framesEncoded: outbound.framesEncoded,
1777
+ bitrateBps: bitrate,
1778
+ width: outbound.frameWidth,
1779
+ height: outbound.frameHeight,
1780
+ qualityLimitationReason: outbound.qualityLimitationReason,
1781
+ rttMs:
1782
+ typeof remoteInbound?.roundTripTime === 'number' ? remoteInbound.roundTripTime * 1000 : undefined,
1783
+ packetsLostPct: lossPct,
1784
+ };
1785
+ safeInvoke('onCameraStats', onCameraStatsRef.current, stats);
1786
+ };
1787
+
1788
+ const startCameraStatsPoller = () => {
1789
+ if (cameraStatsTimerRef.current !== null) return;
1790
+ cameraStatsPrevRef.current = null;
1791
+ // First sample fires after one interval — the very first
1792
+ // sample has no deltas to compare against, which is fine: the
1793
+ // codec/encoder identity is still useful on its own.
1794
+ cameraStatsTimerRef.current = setInterval(() => {
1795
+ void sampleOutboundCameraStats();
1796
+ }, 1000);
1797
+ };
1798
+
1799
+ const stopCameraStatsPoller = () => {
1800
+ if (cameraStatsTimerRef.current !== null) {
1801
+ clearInterval(cameraStatsTimerRef.current);
1802
+ cameraStatsTimerRef.current = null;
1803
+ }
1804
+ cameraStatsPrevRef.current = null;
1805
+ safeInvoke('onCameraStats', onCameraStatsRef.current, null);
1806
+ };
1807
+
1438
1808
  const teardownConnection = () => {
1439
1809
  clearConnectionSuccessTimeout();
1440
1810
  clearIceDisconnectedGrace();
@@ -1443,6 +1813,12 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
1443
1813
  axFetcherRef.current.stop();
1444
1814
  axFetcherRef.current = null;
1445
1815
  }
1816
+ // Drop any active outbound camera before the PC dies so the
1817
+ // browser doesn't leave the camera indicator lit between
1818
+ // reconnects.
1819
+ stopCameraStatsPoller();
1820
+ stopOutboundLocalStream();
1821
+ outboundCameraSenderRef.current = null;
1446
1822
  // A scheduled cursor flush would otherwise call setState on a
1447
1823
  // teardown component once the next frame runs.
1448
1824
  if (cursorRafIdRef.current !== undefined) {
@@ -1568,6 +1944,21 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
1568
1944
  if (!isCurrentAttempt() || wsRef.current !== ws) {
1569
1945
  return;
1570
1946
  }
1947
+ // Replay the saved camera aspect for fresh connections
1948
+ // (initial mount, autoreconnect, sim reboot). The host's
1949
+ // streamer starts at its boot default (16:9 / 1920×1080)
1950
+ // and a missing message would mean the operator's pick
1951
+ // silently reverts on reconnect. We always send something
1952
+ // when the prop is set, even if it matches the host
1953
+ // default — the host short-circuits no-op rebuilds.
1954
+ const initialAspect = cameraAspectRef.current;
1955
+ if (initialAspect) {
1956
+ try {
1957
+ ws.send(JSON.stringify({ type: 'cameraAspect', aspect: initialAspect }));
1958
+ } catch (err) {
1959
+ debugWarn('initial cameraAspect send failed:', err);
1960
+ }
1961
+ }
1571
1962
  settle(resolve);
1572
1963
  };
1573
1964
 
@@ -1644,6 +2035,23 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
1644
2035
  peerConnection.addTransceiver('audio', { direction: 'recvonly' });
1645
2036
  const videoTransceiver = peerConnection.addTransceiver('video', { direction: 'recvonly' });
1646
2037
 
2038
+ // Pre-allocate a sendonly slot for the user's camera so the
2039
+ // sim can pull frames later without renegotiating SDP.
2040
+ // Allocating up-front means we don't have to renegotiate the
2041
+ // SDP when the simulator app actually opens its
2042
+ // `AVCaptureSession` — the codec/SSRC negotiation happens
2043
+ // once, here, and turning the camera on/off later is just a
2044
+ // `replaceTrack`.
2045
+ // iOS only: Android has no camera consumer, and the extra
2046
+ // m=video line crashes Android 14's scrcpy/libwebrtc.
2047
+ const outboundCameraTransceiver =
2048
+ platform === 'ios'
2049
+ ? peerConnection.addTransceiver('video', {
2050
+ direction: 'sendonly',
2051
+ })
2052
+ : null;
2053
+ outboundCameraSenderRef.current = outboundCameraTransceiver?.sender ?? null;
2054
+
1647
2055
  // As hardware encoder, we use H265 for iOS and VP9 for Android.
1648
2056
  // We make sure these two are the first ones in the list.
1649
2057
  // If not, the fallback is H264 which is also hardware accelerated, although not as good,
@@ -1669,6 +2077,59 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
1669
2077
  }
1670
2078
  }
1671
2079
 
2080
+ // Pin the outbound camera transceiver's codec order with
2081
+ // negotiation-time fallback. Preference order:
2082
+ // 1. H.265 / HEVC — VideoToolbox HW on M-series + recent
2083
+ // Chrome. ~30% less bitrate at equal quality. If the
2084
+ // answerer (limulator) supports HEVC, this wins.
2085
+ // 2. H.264 with VideoToolbox-friendly profile-level-ids
2086
+ // (42e01f / 42e028 / 640c1f). Universally HW-accelerated
2087
+ // on every Mac since 2009. SDP-time fallback when HEVC
2088
+ // isn't in the answer.
2089
+ // 3. Any other H.264 profile. Often resolves to OpenH264
2090
+ // (software) so we keep it explicitly last among H.264
2091
+ // to avoid Chrome's default pick.
2092
+ // 4. VP9 / VP8 (libvpx software on Mac). Last resort.
2093
+ //
2094
+ // Caveat: this is *negotiation* fallback. If HEVC HW encoder
2095
+ // init fails at session start or gets demoted mid-stream,
2096
+ // Chrome falls back to *software* HEVC, not H.264 — no spec
2097
+ // hook for runtime re-negotiation. The host-side stats
2098
+ // logger will surface this as decoder=ffmpeg / hw=false.
2099
+ if (outboundCameraTransceiver && RTCRtpSender.getCapabilities) {
2100
+ const senderCaps = RTCRtpSender.getCapabilities('video');
2101
+ if (senderCaps && senderCaps.codecs) {
2102
+ const vtH264Profiles = new Set(['42e01f', '42e028', '640c1f']);
2103
+ const score = (c: { mimeType: string; sdpFmtpLine?: string }): number => {
2104
+ const mime = c.mimeType.toLowerCase();
2105
+ const fmtp = (c.sdpFmtpLine || '').toLowerCase();
2106
+ const profileMatch = fmtp.match(/profile-level-id=([0-9a-f]{6})/);
2107
+ const profile = profileMatch ? profileMatch[1] : '';
2108
+ if (mime === 'video/h265' || mime === 'video/hevc') return 1;
2109
+ if (mime === 'video/h264' && vtH264Profiles.has(profile)) return 2;
2110
+ if (mime === 'video/h264') return 3;
2111
+ if (mime === 'video/vp9') return 4;
2112
+ if (mime === 'video/vp8') return 5;
2113
+ if (mime === 'video/rtx' || mime === 'video/red' || mime === 'video/ulpfec') {
2114
+ return 0; // Keep RTX/FEC available alongside everything else.
2115
+ }
2116
+ return 6;
2117
+ };
2118
+ const sortedSendCodecs = [...senderCaps.codecs].sort((a, b) => score(a) - score(b));
2119
+ try {
2120
+ outboundCameraTransceiver.setCodecPreferences(sortedSendCodecs);
2121
+ debugLog(
2122
+ 'Outbound camera codec preferences:',
2123
+ sortedSendCodecs
2124
+ .map((c) => `${c.mimeType}${c.sdpFmtpLine ? `[${c.sdpFmtpLine}]` : ''}`)
2125
+ .join(', '),
2126
+ );
2127
+ } catch (err) {
2128
+ debugWarn('Failed to set outbound camera codec preferences:', err);
2129
+ }
2130
+ }
2131
+ }
2132
+
1672
2133
  const dataChannel = peerConnection.createDataChannel('control', {
1673
2134
  ordered: true,
1674
2135
  negotiated: true,
@@ -1972,6 +2433,235 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
1972
2433
  pendingScreenshotResolversRef.current.delete(message.id);
1973
2434
  pendingScreenshotRejectersRef.current.delete(message.id);
1974
2435
  break;
2436
+ case 'cameraRequest': {
2437
+ const active = message.active === true;
2438
+ // Bump up front so any earlier in-flight handler bails.
2439
+ const cameraGeneration = ++cameraRequestGenerationRef.current;
2440
+ const isCurrentCameraRequest = () =>
2441
+ isCurrentAttempt() &&
2442
+ wsRef.current === ws &&
2443
+ cameraGeneration === cameraRequestGenerationRef.current;
2444
+ // Log unconditionally — this is one of the few places we
2445
+ // hand control to the browser's permission UI, and a
2446
+ // silent failure here looks identical to "limulator
2447
+ // never asked" from the user's point of view.
2448
+ // eslint-disable-next-line no-console
2449
+ console.info('[RemoteControl] cameraRequest received, active=', active);
2450
+ if (!active) {
2451
+ // Sim no longer wants frames. Drop our local track and
2452
+ // shut the browser's camera green light off.
2453
+ const sender = outboundCameraSenderRef.current;
2454
+ if (sender) {
2455
+ try {
2456
+ await sender.replaceTrack(null);
2457
+ } catch (err) {
2458
+ debugWarn('replaceTrack(null) on camera detach failed:', err);
2459
+ }
2460
+ }
2461
+ stopCameraStatsPoller();
2462
+ stopOutboundLocalStream();
2463
+ safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, false);
2464
+ break;
2465
+ }
2466
+ // Sim is asking for camera. Ask the browser; the user's
2467
+ // prompt response is reported back to the host via a
2468
+ // `cameraResult` message so it can swap to a
2469
+ // black-frame fallback on denial.
2470
+ safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true);
2471
+ if (!navigator.mediaDevices || typeof navigator.mediaDevices.getUserMedia !== 'function') {
2472
+ // Likely an insecure context (http on a non-localhost
2473
+ // origin) — Chrome strips `mediaDevices` off
2474
+ // `navigator` in that case and the only signal is
2475
+ // this undefined check.
2476
+ // eslint-disable-next-line no-console
2477
+ console.warn(
2478
+ '[RemoteControl] navigator.mediaDevices.getUserMedia unavailable. ' +
2479
+ 'getUserMedia requires a secure context (https or http://localhost). ' +
2480
+ 'Replying cameraResult granted=false so limulator falls back to black frames.',
2481
+ );
2482
+ ws.send(JSON.stringify({ type: 'cameraResult', granted: false }));
2483
+ safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true, false);
2484
+ break;
2485
+ }
2486
+ let stream: MediaStream | null = null;
2487
+ try {
2488
+ // Capture at the webcam's *native* resolution by
2489
+ // default (no width/height constraints). When the
2490
+ // host app has picked an explicit cap via
2491
+ // `cameraResolutionCap`, we honour it here. Frame
2492
+ // rate is always capped to 30 to match the
2493
+ // simulator pool's pacing.
2494
+ stream = await navigator.mediaDevices.getUserMedia({
2495
+ video: cameraCapToConstraints(cameraResolutionCapRef.current),
2496
+ audio: false,
2497
+ });
2498
+ } catch (err) {
2499
+ // Surface unconditionally — the user is the only one
2500
+ // who can fix this (permission denied, no device,
2501
+ // dismissed prompt, etc.). `debugWarn` alone would
2502
+ // hide it in normal builds.
2503
+ // eslint-disable-next-line no-console
2504
+ console.warn('[RemoteControl] getUserMedia denied/failed:', err);
2505
+ }
2506
+ if (!isCurrentCameraRequest()) {
2507
+ // Superseded during the prompt; drop the stream we got.
2508
+ if (stream) stopMediaStream(stream);
2509
+ return;
2510
+ }
2511
+ if (!stream) {
2512
+ ws.send(JSON.stringify({ type: 'cameraResult', granted: false }));
2513
+ safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true, false);
2514
+ break;
2515
+ }
2516
+ // Replace any previous local stream (e.g. an earlier
2517
+ // cameraRequest that resolved with a different device)
2518
+ // before we install the new tracks.
2519
+ stopOutboundLocalStream();
2520
+ outboundLocalStreamRef.current = stream;
2521
+ const sender = outboundCameraSenderRef.current;
2522
+ const videoTrack = stream.getVideoTracks()[0] ?? null;
2523
+ if (!sender || !videoTrack) {
2524
+ if (stream) stopMediaStream(stream);
2525
+ outboundLocalStreamRef.current = null;
2526
+ ws.send(JSON.stringify({ type: 'cameraResult', granted: false }));
2527
+ safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true, false);
2528
+ break;
2529
+ }
2530
+ try {
2531
+ await sender.replaceTrack(videoTrack);
2532
+ } catch (err) {
2533
+ debugWarn('replaceTrack(videoTrack) failed:', err);
2534
+ stopMediaStream(stream);
2535
+ outboundLocalStreamRef.current = null;
2536
+ ws.send(JSON.stringify({ type: 'cameraResult', granted: false }));
2537
+ safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true, false);
2538
+ break;
2539
+ }
2540
+ if (!isCurrentCameraRequest()) {
2541
+ // Superseded mid-attach; detach and skip the ACK/poller.
2542
+ try {
2543
+ await sender.replaceTrack(null);
2544
+ } catch (err) {
2545
+ debugWarn('replaceTrack(null) on stale camera attach failed:', err);
2546
+ }
2547
+ stopMediaStream(stream);
2548
+ if (outboundLocalStreamRef.current === stream) {
2549
+ outboundLocalStreamRef.current = null;
2550
+ }
2551
+ return;
2552
+ }
2553
+ // Tell the encoder this is real motion content (a
2554
+ // physical camera), not text/slides. With `'motion'`
2555
+ // VideoToolbox / libvpx pick latency-friendly tuning
2556
+ // (no extra B-frames, shorter GoP smoothing). Set
2557
+ // before the first frame so the encoder init reads it.
2558
+ try {
2559
+ videoTrack.contentHint = 'motion';
2560
+ } catch {
2561
+ /* older browsers don't support contentHint; ignore */
2562
+ }
2563
+ // Bound the outbound bitrate generously and let
2564
+ // WebRTC's congestion control (BWE) find the floor.
2565
+ // 8 Mbps is comfortable for native 1080p30 webcam
2566
+ // content over LAN — the encoder will use far less when
2567
+ // the scene is static, and BWE will throttle if a
2568
+ // hop is constrained. Pair with
2569
+ // `maintain-framerate` so the quality scaler steps
2570
+ // down resolution before dropping frames, matching how
2571
+ // Meet/Zoom degrade.
2572
+ try {
2573
+ const params = sender.getParameters();
2574
+ if (!params.encodings || params.encodings.length === 0) {
2575
+ params.encodings = [{}];
2576
+ }
2577
+ params.encodings[0].maxBitrate = 8_000_000;
2578
+ params.encodings[0].maxFramerate = 30;
2579
+ params.degradationPreference = 'maintain-framerate';
2580
+ await sender.setParameters(params);
2581
+ } catch (err) {
2582
+ debugWarn('setParameters on outbound camera failed:', err);
2583
+ }
2584
+ // Surface the actual captured geometry to the host so
2585
+ // it can size its IOSurface pool / picture-format
2586
+ // metadata to match. `getSettings()` returns what the
2587
+ // browser actually picked — which may differ from any
2588
+ // hints we sent in the constraints — including the
2589
+ // selected `deviceId` / `label` (useful when a user has
2590
+ // multiple cameras and we eventually expose a picker).
2591
+ let cameraMetadata: {
2592
+ width?: number;
2593
+ height?: number;
2594
+ frameRate?: number;
2595
+ deviceId?: string;
2596
+ label?: string;
2597
+ facingMode?: string;
2598
+ } = {};
2599
+ try {
2600
+ const settings = videoTrack.getSettings();
2601
+ cameraMetadata = {
2602
+ width: settings.width,
2603
+ height: settings.height,
2604
+ frameRate: settings.frameRate,
2605
+ deviceId: settings.deviceId,
2606
+ label: videoTrack.label || undefined,
2607
+ facingMode: settings.facingMode,
2608
+ };
2609
+ // eslint-disable-next-line no-console
2610
+ console.info(
2611
+ `[RemoteControl] camera capture: ${cameraMetadata.width}x${cameraMetadata.height}` +
2612
+ ` @ ${cameraMetadata.frameRate ?? '?'}fps` +
2613
+ (cameraMetadata.label ? ` — ${cameraMetadata.label}` : ''),
2614
+ );
2615
+ } catch (err) {
2616
+ debugWarn('getSettings() on outbound camera track failed:', err);
2617
+ }
2618
+ // If the browser revokes the track later (extension,
2619
+ // user clicks Stop in the camera tab UI, etc.), notify
2620
+ // the host so it can switch to black frames.
2621
+ videoTrack.onended = () => {
2622
+ if (outboundLocalStreamRef.current !== stream) return;
2623
+ stopCameraStatsPoller();
2624
+ stopOutboundLocalStream();
2625
+ if (wsRef.current === ws && ws.readyState === WebSocket.OPEN) {
2626
+ ws.send(JSON.stringify({ type: 'cameraResult', granted: false }));
2627
+ }
2628
+ safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true, false);
2629
+ };
2630
+ if (!isCurrentCameraRequest()) {
2631
+ // Superseded during setParameters; detach and bail.
2632
+ videoTrack.onended = null;
2633
+ try {
2634
+ await sender.replaceTrack(null);
2635
+ } catch (err) {
2636
+ debugWarn('replaceTrack(null) on stale camera finalize failed:', err);
2637
+ }
2638
+ stopMediaStream(stream);
2639
+ if (outboundLocalStreamRef.current === stream) {
2640
+ outboundLocalStreamRef.current = null;
2641
+ }
2642
+ return;
2643
+ }
2644
+ ws.send(
2645
+ JSON.stringify({
2646
+ type: 'cameraResult',
2647
+ granted: true,
2648
+ // Forward what the browser actually captured so the
2649
+ // host can size its IOSurface pool, log the device,
2650
+ // and (eventually) surface a status pill / picker.
2651
+ camera: cameraMetadata,
2652
+ }),
2653
+ );
2654
+ safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true, true, cameraMetadata);
2655
+ // Kick off the per-second outbound stats sampler. We do
2656
+ // this *after* `setParameters` so the first sample
2657
+ // already sees the encoder under its final bitrate /
2658
+ // degradation policy, and *after* the host-side
2659
+ // attachInboundTrack will have wired up (the
2660
+ // cameraResult ACK is what triggers it on the host),
2661
+ // so framesEncoded starts climbing immediately.
2662
+ startCameraStatsPoller();
2663
+ break;
2664
+ }
1975
2665
  case 'terminateAppResult':
1976
2666
  if (typeof message.id !== 'string') {
1977
2667
  debugWarn('Received invalid terminateApp result message:', message);
@@ -2059,6 +2749,41 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
2059
2749
  start();
2060
2750
  };
2061
2751
 
2752
+ // Re-apply the resolution cap on the currently-sending track
2753
+ // whenever the host app changes its preference. Skips when no
2754
+ // camera is active — the next `getUserMedia` will pick up the
2755
+ // new value via `cameraCapToConstraints` automatically.
2756
+ useEffect(() => {
2757
+ const stream = outboundLocalStreamRef.current;
2758
+ if (!stream) return;
2759
+ const track = stream.getVideoTracks()[0];
2760
+ if (!track) return;
2761
+ const constraints = cameraCapToConstraints(cameraResolutionCap);
2762
+ track.applyConstraints(constraints).catch((err) => {
2763
+ debugWarn('applyConstraints for new camera cap failed:', err);
2764
+ });
2765
+ // eslint-disable-next-line react-hooks/exhaustive-deps
2766
+ }, [cameraResolutionCap]);
2767
+
2768
+ // Push the aspect preference to the host whenever it changes.
2769
+ // The host rebuilds its IOSurface pool and bumps `pool_generation`
2770
+ // on its end; the dylib re-handshakes on next sem_wait. No
2771
+ // peer-connection renegotiation is needed — the aspect change is
2772
+ // purely about the pixel buffer dimensions iOS apps observe, not
2773
+ // about WebRTC track layout. The `cameraAspectRef` is mirrored
2774
+ // higher up so the WS `onopen` reconnect path can replay the
2775
+ // latest value on fresh connections.
2776
+ useEffect(() => {
2777
+ if (!cameraAspect) return;
2778
+ const ws = wsRef.current;
2779
+ if (!ws || ws.readyState !== WebSocket.OPEN) return;
2780
+ try {
2781
+ ws.send(JSON.stringify({ type: 'cameraAspect', aspect: cameraAspect }));
2782
+ } catch (err) {
2783
+ debugWarn('cameraAspect send failed:', err);
2784
+ }
2785
+ }, [cameraAspect]);
2786
+
2062
2787
  useEffect(() => {
2063
2788
  // Reset video loaded state when connection params change
2064
2789
  setVideoLoaded(false);
@@ -2080,6 +2805,11 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
2080
2805
  stop();
2081
2806
  document.removeEventListener('visibilitychange', handleVisibilityChange);
2082
2807
  };
2808
+ // Camera attach/detach happens entirely inside the WS message
2809
+ // loop now (sendonly transceiver + `replaceTrack`), so the
2810
+ // connection effect doesn't need to bounce when the camera
2811
+ // turns on/off — no SDP-affecting change.
2812
+ // eslint-disable-next-line react-hooks/exhaustive-deps
2083
2813
  }, [url, token, propSessionId]);
2084
2814
 
2085
2815
  // Recompute the inspect-overlay geometry (container-local pixel rect of