@limrun/ui 0.9.0-rc.13 → 0.9.0-rc.14
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.
- package/dist/components/remote-control.d.ts +16 -24
- package/dist/index.cjs +1 -1
- package/dist/index.js +804 -755
- package/package.json +1 -1
- package/src/components/remote-control.tsx +167 -58
package/package.json
CHANGED
|
@@ -142,33 +142,25 @@ interface RemoteControlProps {
|
|
|
142
142
|
axMaxBackoffMs?: number;
|
|
143
143
|
|
|
144
144
|
/**
|
|
145
|
-
*
|
|
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.).
|
|
146
152
|
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
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).
|
|
153
159
|
*
|
|
154
|
-
*
|
|
155
|
-
* camera
|
|
156
|
-
*
|
|
157
|
-
* Lifecycle expectations:
|
|
158
|
-
* - Pass `null`/`undefined` to skip outbound camera (default).
|
|
159
|
-
* - Pass a `MediaStream` obtained from
|
|
160
|
-
* `navigator.mediaDevices.getUserMedia({ video: true })` (or any
|
|
161
|
-
* other source — screen capture, virtual camera, etc.).
|
|
162
|
-
* - Changing the `MediaStream.id` triggers a connection restart so
|
|
163
|
-
* the new track is included in the SDP offer. The reference itself
|
|
164
|
-
* is allowed to be unstable across renders — we key the reconnect
|
|
165
|
-
* on `id`, not object identity, so memoization isn't required.
|
|
166
|
-
* - Stopping the tracks (e.g. `track.stop()`) on the parent side is
|
|
167
|
-
* sufficient to cut the outbound feed without restarting; the peer
|
|
168
|
-
* connection just stops getting frames on that track. To fully
|
|
169
|
-
* detach the track from the SDP, swap the prop to `null`.
|
|
160
|
+
* Only iOS instances ever fire this callback; Android instances
|
|
161
|
+
* have no camera-injector path and stay silent.
|
|
170
162
|
*/
|
|
171
|
-
|
|
163
|
+
onCameraDemandChange?: (active: boolean, granted?: boolean) => void;
|
|
172
164
|
}
|
|
173
165
|
|
|
174
166
|
interface ScreenshotData {
|
|
@@ -367,7 +359,7 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
|
|
|
367
359
|
onAxStatusChange,
|
|
368
360
|
axPollIntervalMs,
|
|
369
361
|
axMaxBackoffMs,
|
|
370
|
-
|
|
362
|
+
onCameraDemandChange,
|
|
371
363
|
}: RemoteControlProps,
|
|
372
364
|
ref,
|
|
373
365
|
) => {
|
|
@@ -393,13 +385,29 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
|
|
|
393
385
|
// Mirrored to a ref so stale closures in event handlers see the latest value.
|
|
394
386
|
const autoReconnectRef = useRef(autoReconnect);
|
|
395
387
|
autoReconnectRef.current = autoReconnect;
|
|
396
|
-
//
|
|
397
|
-
//
|
|
398
|
-
//
|
|
399
|
-
//
|
|
400
|
-
//
|
|
401
|
-
|
|
402
|
-
|
|
388
|
+
// Demand-driven outbound camera state.
|
|
389
|
+
//
|
|
390
|
+
// The limulator side broadcasts a `cameraRequest` WS message whenever
|
|
391
|
+
// an app inside the simulator opens/closes an `AVCaptureSession`.
|
|
392
|
+
// We respond by calling `navigator.mediaDevices.getUserMedia(...)`
|
|
393
|
+
// and `replaceTrack`ing the result onto a pre-allocated sendonly
|
|
394
|
+
// video transceiver. Pre-allocating the transceiver lets us
|
|
395
|
+
// attach/detach the local camera without renegotiating the SDP
|
|
396
|
+
// (the slot is already in the answer's a=video block, just with
|
|
397
|
+
// `inactive`/empty until we install the track).
|
|
398
|
+
//
|
|
399
|
+
// Refs (not state) because all callers live inside event-handler
|
|
400
|
+
// closures and the ref read happens during message processing, not
|
|
401
|
+
// during render. We keep both the sender and the active local
|
|
402
|
+
// stream so teardown can stop tracks without leaking the camera
|
|
403
|
+
// green light.
|
|
404
|
+
const outboundCameraSenderRef = useRef<RTCRtpSender | null>(null);
|
|
405
|
+
const outboundLocalStreamRef = useRef<MediaStream | null>(null);
|
|
406
|
+
// Mirror the demand-change callback into a ref so the WS message
|
|
407
|
+
// handler always sees the freshest customer callback even when the
|
|
408
|
+
// parent re-renders mid-session.
|
|
409
|
+
const onCameraDemandChangeRef = useRef(onCameraDemandChange);
|
|
410
|
+
onCameraDemandChangeRef.current = onCameraDemandChange;
|
|
403
411
|
const firstFrameShownRef = useRef(false);
|
|
404
412
|
const pendingScreenshotResolversRef = useRef<
|
|
405
413
|
Map<string, (value: ScreenshotData | PromiseLike<ScreenshotData>) => void>
|
|
@@ -1472,6 +1480,32 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
|
|
|
1472
1480
|
setVideoLoaded(true);
|
|
1473
1481
|
};
|
|
1474
1482
|
|
|
1483
|
+
// Stop every track on a MediaStream and release the device handle.
|
|
1484
|
+
// The browser keeps the camera "on" indicator lit until at least
|
|
1485
|
+
// one track on the underlying source is stopped, so we have to
|
|
1486
|
+
// call stop() explicitly — closing the peer connection alone
|
|
1487
|
+
// won't do it.
|
|
1488
|
+
const stopMediaStream = (stream: MediaStream) => {
|
|
1489
|
+
for (const track of stream.getTracks()) {
|
|
1490
|
+
try {
|
|
1491
|
+
track.stop();
|
|
1492
|
+
} catch (err) {
|
|
1493
|
+
debugWarn('track.stop() failed:', err);
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
};
|
|
1497
|
+
|
|
1498
|
+
// Stop and forget any currently-attached outbound camera stream.
|
|
1499
|
+
// Safe to call when nothing is attached. Does not touch the
|
|
1500
|
+
// sender — the caller is responsible for `replaceTrack(null)`
|
|
1501
|
+
// separately when it wants the SDP slot itself empty.
|
|
1502
|
+
const stopOutboundLocalStream = () => {
|
|
1503
|
+
const stream = outboundLocalStreamRef.current;
|
|
1504
|
+
if (!stream) return;
|
|
1505
|
+
outboundLocalStreamRef.current = null;
|
|
1506
|
+
stopMediaStream(stream);
|
|
1507
|
+
};
|
|
1508
|
+
|
|
1475
1509
|
const teardownConnection = () => {
|
|
1476
1510
|
clearConnectionSuccessTimeout();
|
|
1477
1511
|
clearIceDisconnectedGrace();
|
|
@@ -1480,6 +1514,11 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
|
|
|
1480
1514
|
axFetcherRef.current.stop();
|
|
1481
1515
|
axFetcherRef.current = null;
|
|
1482
1516
|
}
|
|
1517
|
+
// Drop any active outbound camera before the PC dies so the
|
|
1518
|
+
// browser doesn't leave the camera indicator lit between
|
|
1519
|
+
// reconnects.
|
|
1520
|
+
stopOutboundLocalStream();
|
|
1521
|
+
outboundCameraSenderRef.current = null;
|
|
1483
1522
|
// A scheduled cursor flush would otherwise call setState on a
|
|
1484
1523
|
// teardown component once the next frame runs.
|
|
1485
1524
|
if (cursorRafIdRef.current !== undefined) {
|
|
@@ -1681,26 +1720,18 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
|
|
|
1681
1720
|
peerConnection.addTransceiver('audio', { direction: 'recvonly' });
|
|
1682
1721
|
const videoTransceiver = peerConnection.addTransceiver('video', { direction: 'recvonly' });
|
|
1683
1722
|
|
|
1684
|
-
//
|
|
1685
|
-
//
|
|
1686
|
-
//
|
|
1687
|
-
//
|
|
1688
|
-
//
|
|
1689
|
-
//
|
|
1690
|
-
//
|
|
1691
|
-
//
|
|
1692
|
-
const
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
}
|
|
1697
|
-
debugLog(
|
|
1698
|
-
'Attached outbound camera tracks:',
|
|
1699
|
-
outboundCamera.getVideoTracks().length,
|
|
1700
|
-
'streamId:',
|
|
1701
|
-
outboundCamera.id,
|
|
1702
|
-
);
|
|
1703
|
-
}
|
|
1723
|
+
// Pre-allocate a sendonly video slot for the user's camera.
|
|
1724
|
+
// The track stays `null` until the limulator side asks for one
|
|
1725
|
+
// (via the `cameraRequest` WS message), at which point we call
|
|
1726
|
+
// `replaceTrack` with a `getUserMedia` result. Allocating
|
|
1727
|
+
// up-front means we don't have to renegotiate the SDP when the
|
|
1728
|
+
// simulator app actually opens its `AVCaptureSession` — the
|
|
1729
|
+
// codec/SSRC negotiation happens once, here, and turning the
|
|
1730
|
+
// camera on/off later is just a track-replace.
|
|
1731
|
+
const outboundCameraTransceiver = peerConnection.addTransceiver('video', {
|
|
1732
|
+
direction: 'sendonly',
|
|
1733
|
+
});
|
|
1734
|
+
outboundCameraSenderRef.current = outboundCameraTransceiver.sender;
|
|
1704
1735
|
|
|
1705
1736
|
// As hardware encoder, we use H265 for iOS and VP9 for Android.
|
|
1706
1737
|
// We make sure these two are the first ones in the list.
|
|
@@ -2030,6 +2061,85 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
|
|
|
2030
2061
|
pendingScreenshotResolversRef.current.delete(message.id);
|
|
2031
2062
|
pendingScreenshotRejectersRef.current.delete(message.id);
|
|
2032
2063
|
break;
|
|
2064
|
+
case 'cameraRequest': {
|
|
2065
|
+
const active = message.active === true;
|
|
2066
|
+
if (!active) {
|
|
2067
|
+
// Sim no longer wants frames. Drop our local track and
|
|
2068
|
+
// shut the browser's camera green light off.
|
|
2069
|
+
const sender = outboundCameraSenderRef.current;
|
|
2070
|
+
if (sender) {
|
|
2071
|
+
try {
|
|
2072
|
+
await sender.replaceTrack(null);
|
|
2073
|
+
} catch (err) {
|
|
2074
|
+
debugWarn('replaceTrack(null) on camera detach failed:', err);
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
stopOutboundLocalStream();
|
|
2078
|
+
safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, false);
|
|
2079
|
+
break;
|
|
2080
|
+
}
|
|
2081
|
+
// Sim is asking for camera. Ask the browser; the user's
|
|
2082
|
+
// prompt response is reported back to the host via a
|
|
2083
|
+
// `cameraResult` message so it can swap to a
|
|
2084
|
+
// black-frame fallback on denial.
|
|
2085
|
+
safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true);
|
|
2086
|
+
let stream: MediaStream | null = null;
|
|
2087
|
+
try {
|
|
2088
|
+
stream = await navigator.mediaDevices.getUserMedia({
|
|
2089
|
+
video: true,
|
|
2090
|
+
audio: false,
|
|
2091
|
+
});
|
|
2092
|
+
} catch (err) {
|
|
2093
|
+
debugWarn('getUserMedia denied/failed:', err);
|
|
2094
|
+
}
|
|
2095
|
+
if (!isCurrentAttempt() || wsRef.current !== ws) {
|
|
2096
|
+
if (stream) stopMediaStream(stream);
|
|
2097
|
+
return;
|
|
2098
|
+
}
|
|
2099
|
+
if (!stream) {
|
|
2100
|
+
ws.send(JSON.stringify({ type: 'cameraResult', granted: false }));
|
|
2101
|
+
safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true, false);
|
|
2102
|
+
break;
|
|
2103
|
+
}
|
|
2104
|
+
// Replace any previous local stream (e.g. an earlier
|
|
2105
|
+
// cameraRequest that resolved with a different device)
|
|
2106
|
+
// before we install the new tracks.
|
|
2107
|
+
stopOutboundLocalStream();
|
|
2108
|
+
outboundLocalStreamRef.current = stream;
|
|
2109
|
+
const sender = outboundCameraSenderRef.current;
|
|
2110
|
+
const videoTrack = stream.getVideoTracks()[0] ?? null;
|
|
2111
|
+
if (!sender || !videoTrack) {
|
|
2112
|
+
if (stream) stopMediaStream(stream);
|
|
2113
|
+
outboundLocalStreamRef.current = null;
|
|
2114
|
+
ws.send(JSON.stringify({ type: 'cameraResult', granted: false }));
|
|
2115
|
+
safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true, false);
|
|
2116
|
+
break;
|
|
2117
|
+
}
|
|
2118
|
+
try {
|
|
2119
|
+
await sender.replaceTrack(videoTrack);
|
|
2120
|
+
} catch (err) {
|
|
2121
|
+
debugWarn('replaceTrack(videoTrack) failed:', err);
|
|
2122
|
+
stopMediaStream(stream);
|
|
2123
|
+
outboundLocalStreamRef.current = null;
|
|
2124
|
+
ws.send(JSON.stringify({ type: 'cameraResult', granted: false }));
|
|
2125
|
+
safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true, false);
|
|
2126
|
+
break;
|
|
2127
|
+
}
|
|
2128
|
+
// If the browser revokes the track later (extension,
|
|
2129
|
+
// user clicks Stop in the camera tab UI, etc.), notify
|
|
2130
|
+
// the host so it can switch to black frames.
|
|
2131
|
+
videoTrack.onended = () => {
|
|
2132
|
+
if (outboundLocalStreamRef.current !== stream) return;
|
|
2133
|
+
stopOutboundLocalStream();
|
|
2134
|
+
if (wsRef.current === ws && ws.readyState === WebSocket.OPEN) {
|
|
2135
|
+
ws.send(JSON.stringify({ type: 'cameraResult', granted: false }));
|
|
2136
|
+
}
|
|
2137
|
+
safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true, false);
|
|
2138
|
+
};
|
|
2139
|
+
ws.send(JSON.stringify({ type: 'cameraResult', granted: true }));
|
|
2140
|
+
safeInvoke('onCameraDemandChange', onCameraDemandChangeRef.current, true, true);
|
|
2141
|
+
break;
|
|
2142
|
+
}
|
|
2033
2143
|
case 'terminateAppResult':
|
|
2034
2144
|
if (typeof message.id !== 'string') {
|
|
2035
2145
|
debugWarn('Received invalid terminateApp result message:', message);
|
|
@@ -2138,13 +2248,12 @@ export const RemoteControl = forwardRef<RemoteControlHandle, RemoteControlProps>
|
|
|
2138
2248
|
stop();
|
|
2139
2249
|
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
2140
2250
|
};
|
|
2141
|
-
//
|
|
2142
|
-
//
|
|
2143
|
-
//
|
|
2144
|
-
//
|
|
2145
|
-
// wrapping object don't trigger churn.
|
|
2251
|
+
// Camera attach/detach happens entirely inside the WS message
|
|
2252
|
+
// loop now (sendonly transceiver + `replaceTrack`), so the
|
|
2253
|
+
// connection effect doesn't need to bounce when the camera
|
|
2254
|
+
// turns on/off — no SDP-affecting change.
|
|
2146
2255
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
2147
|
-
}, [url, token, propSessionId
|
|
2256
|
+
}, [url, token, propSessionId]);
|
|
2148
2257
|
|
|
2149
2258
|
// Recompute the inspect-overlay geometry (container-local pixel rect of
|
|
2150
2259
|
// the actually-rendered video content) from the current mapping context.
|