@ksp-gonogo/kerbcast-react 1.11.0-rc.4 → 1.11.0
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/README.md +19 -4
- package/dist/CameraFeed.js +77 -39
- package/dist/KerbalFaceFeed.js +2 -2
- package/dist/hooks/useReportDisplaySize.d.ts +46 -5
- package/dist/hooks/useReportDisplaySize.js +186 -31
- package/package.json +2 -2
- package/src/CameraFeed.test.tsx +247 -6
- package/src/CameraFeed.tsx +337 -289
- package/src/KerbalFaceFeed.tsx +2 -2
- package/src/hooks/useReportDisplaySize.loop.test.tsx +290 -0
- package/src/hooks/useReportDisplaySize.test.tsx +14 -11
- package/src/hooks/useReportDisplaySize.ts +230 -32
|
@@ -1,30 +1,108 @@
|
|
|
1
|
-
import { useEffect, useId, useLayoutEffect, useState } from "react";
|
|
1
|
+
import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from "react";
|
|
2
2
|
import { useKerbcastDisplaySizes } from "../context";
|
|
3
3
|
|
|
4
4
|
/*
|
|
5
5
|
* Debounce + bucket for self-measured display-size reporting. The first
|
|
6
6
|
* measurement after mount reports promptly (so a feed never sits at a stale
|
|
7
|
-
* resolution until it is resized); later churn is debounced.
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
7
|
+
* resolution until it is resized); later churn is debounced. Auto sets the
|
|
8
|
+
* resolution, never the shape: the request keeps the camera's configured
|
|
9
|
+
* aspect and is sized to the picture of that aspect that fits the box (the
|
|
10
|
+
* feed letterboxes with object-fit: contain). The width is ceiled to a
|
|
11
|
+
* BUCKET_PX multiple: that meets-the-minimum-need (never under-serve) and
|
|
12
|
+
* gives a material-change threshold so a feed re-reports only when a resize
|
|
13
|
+
* crosses a bucket boundary. The height follows at the aspect, rounded to
|
|
14
|
+
* even for H.264.
|
|
13
15
|
*/
|
|
14
16
|
const DEBOUNCE_MS = 200;
|
|
15
17
|
const BUCKET_PX = 16;
|
|
16
18
|
|
|
19
|
+
/*
|
|
20
|
+
* A resolution change must never cause another one. A box sized from the
|
|
21
|
+
* stream (a frame kept at the video's aspect, a `<video>` at its intrinsic
|
|
22
|
+
* size) would otherwise close a loop: the stream sets the box, the box sets
|
|
23
|
+
* the request, the request sets the stream. A stream that arrives smaller
|
|
24
|
+
* than asked (adaptive quality, a sidecar cap) would then shrink the next
|
|
25
|
+
* request, and so on down.
|
|
26
|
+
*
|
|
27
|
+
* FOLLOW_MS: a box change that starts this soon after the stream changed
|
|
28
|
+
* shape, and matches what that change explains, is the stream's doing. It is
|
|
29
|
+
* adopted as the new baseline and not reported. Layout reacts to a stream
|
|
30
|
+
* change within a frame or two; this leaves room for a slow phone.
|
|
31
|
+
*
|
|
32
|
+
* SUSPECT_MS / HOLD_MS: the backstop for a follow the check above misses (a
|
|
33
|
+
* very late layout, or one that does not keep the stream's shape). A box
|
|
34
|
+
* change this close to a stream change may still be a loop, so only one such
|
|
35
|
+
* report is let through per HOLD_MS; further ones are refused. Box changes
|
|
36
|
+
* away from any stream change always report.
|
|
37
|
+
*/
|
|
38
|
+
const FOLLOW_MS = 1000;
|
|
39
|
+
const SUSPECT_MS = 2500;
|
|
40
|
+
const HOLD_MS = 5000;
|
|
41
|
+
const ASPECT_TOLERANCE = 0.06;
|
|
42
|
+
const MAGNITUDE_SLACK = 1.15;
|
|
43
|
+
|
|
17
44
|
function bucket(px: number): number {
|
|
18
45
|
const b = Math.ceil(px / BUCKET_PX) * BUCKET_PX;
|
|
19
46
|
return b < BUCKET_PX ? BUCKET_PX : b;
|
|
20
47
|
}
|
|
21
48
|
|
|
22
|
-
/**
|
|
23
|
-
export
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
49
|
+
/** Shape of a camera whose configured capture size is not known. */
|
|
50
|
+
export const DEFAULT_FEED_ASPECT = 16 / 9;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Report size for a `width` x `height` box: the `aspect` picture that fits it,
|
|
54
|
+
* width bucketed. Never the box's own shape.
|
|
55
|
+
*/
|
|
56
|
+
export function displayReportSize(
|
|
57
|
+
width: number,
|
|
58
|
+
height: number,
|
|
59
|
+
aspect: number = DEFAULT_FEED_ASPECT,
|
|
60
|
+
): { width: number; height: number } {
|
|
61
|
+
const a = aspect > 0 && Number.isFinite(aspect) ? aspect : DEFAULT_FEED_ASPECT;
|
|
62
|
+
const fit = width > 0 && height > 0 ? Math.min(width, height * a) : width;
|
|
63
|
+
const w = bucket(fit);
|
|
64
|
+
return { width: w, height: Math.max(2, Math.round(w / a / 2) * 2) };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface Size {
|
|
68
|
+
width: number;
|
|
69
|
+
height: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The stream changing from `from` (null when nothing was known before) to `to`. */
|
|
73
|
+
export interface StreamChange {
|
|
74
|
+
from: Size | null;
|
|
75
|
+
to: Size;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function spread(a: number, b: number): number {
|
|
79
|
+
return a > b ? a / b : b / a;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Whether a box going from `prev` to `box` is explained by `change`: the new
|
|
84
|
+
* box has the new stream's aspect, and it grew or shrank no more than the
|
|
85
|
+
* stream's own change in size or aspect accounts for. A layout that fits the
|
|
86
|
+
* stream into a fixed tile changes with the aspect; one that shows the video
|
|
87
|
+
* at its own size changes with the size. A box that jumps further (entering
|
|
88
|
+
* fullscreen at the same aspect) is a layout change.
|
|
89
|
+
*/
|
|
90
|
+
export function followsStream(prev: Size, box: Size, change: StreamChange): boolean {
|
|
91
|
+
const { to, from } = change;
|
|
92
|
+
if (!(to.width > 0) || !(to.height > 0)) return false;
|
|
93
|
+
if (!(prev.width > 0) || !(prev.height > 0)) return false;
|
|
94
|
+
const aspect = to.width / to.height;
|
|
95
|
+
const heightFits =
|
|
96
|
+
Math.abs(box.height - box.width / aspect) <= Math.max(2, ASPECT_TOLERANCE * box.height);
|
|
97
|
+
const widthFits =
|
|
98
|
+
Math.abs(box.width - box.height * aspect) <= Math.max(2, ASPECT_TOLERANCE * box.width);
|
|
99
|
+
if (!heightFits && !widthFits) return false;
|
|
100
|
+
if (!from) return true;
|
|
101
|
+
const explained = Math.max(
|
|
102
|
+
spread(to.width * to.height, from.width * from.height),
|
|
103
|
+
spread(aspect, from.width / from.height),
|
|
104
|
+
);
|
|
105
|
+
return spread(box.width * box.height, prev.width * prev.height) <= explained * MAGNITUDE_SLACK;
|
|
28
106
|
}
|
|
29
107
|
|
|
30
108
|
export interface ReportDisplaySizeOptions {
|
|
@@ -35,10 +113,29 @@ export interface ReportDisplaySizeOptions {
|
|
|
35
113
|
*/
|
|
36
114
|
enabled?: boolean;
|
|
37
115
|
/**
|
|
38
|
-
* Report a square (side x side, the min edge) rather than
|
|
39
|
-
* Face cams are square; part cams are not.
|
|
116
|
+
* Report a square (side x side, the min edge) rather than a picture at
|
|
117
|
+
* `aspect`. Face cams are square; part cams are not.
|
|
40
118
|
*/
|
|
41
119
|
square?: boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Width over height of the picture to ask for, normally the camera's
|
|
122
|
+
* configured capture size. Default 16:9. Ignored when `square` is set.
|
|
123
|
+
*/
|
|
124
|
+
aspect?: number;
|
|
125
|
+
/**
|
|
126
|
+
* The `<video>` playing this camera. Its resolution changes are watched so a
|
|
127
|
+
* box resize that only follows the stream is not reported back.
|
|
128
|
+
*/
|
|
129
|
+
videoRef?: React.RefObject<HTMLVideoElement | null>;
|
|
130
|
+
/**
|
|
131
|
+
* The size the sidecar says it is rendering this camera at, for the same
|
|
132
|
+
* purpose: a layout may size itself from this rather than from the video.
|
|
133
|
+
*/
|
|
134
|
+
streamSize?: Size | null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
interface TimedChange extends StreamChange {
|
|
138
|
+
at: number;
|
|
42
139
|
}
|
|
43
140
|
|
|
44
141
|
/**
|
|
@@ -50,26 +147,92 @@ export interface ReportDisplaySizeOptions {
|
|
|
50
147
|
* mount lifecycle: on unmount / `flightId` change / `enabled` flip it
|
|
51
148
|
* deregisters, and the sidecar's clear-on-departure (unsubscribe) does the
|
|
52
149
|
* server-side clear.
|
|
150
|
+
*
|
|
151
|
+
* A box resize that follows a change in the incoming stream is not reported,
|
|
152
|
+
* so a layout sized from the stream cannot feed it back into its own request.
|
|
53
153
|
*/
|
|
54
154
|
export function useReportDisplaySize(
|
|
55
155
|
flightId: number | null,
|
|
56
156
|
ref: React.RefObject<Element | null>,
|
|
57
|
-
{
|
|
157
|
+
{
|
|
158
|
+
enabled = true,
|
|
159
|
+
square = false,
|
|
160
|
+
aspect = DEFAULT_FEED_ASPECT,
|
|
161
|
+
videoRef,
|
|
162
|
+
streamSize,
|
|
163
|
+
}: ReportDisplaySizeOptions = {},
|
|
58
164
|
): void {
|
|
59
165
|
const displaySizes = useKerbcastDisplaySizes();
|
|
60
166
|
const instanceId = useId();
|
|
61
167
|
|
|
62
168
|
/*
|
|
63
|
-
* Track the
|
|
64
|
-
* swapped after the first render (conditional render, a feed that appears
|
|
65
|
-
* once a stream arrives) is observed. Checked after every commit of the
|
|
66
|
-
*
|
|
169
|
+
* Track the elements behind the refs as state so an element that mounts or
|
|
170
|
+
* is swapped after the first render (conditional render, a feed that appears
|
|
171
|
+
* once a stream arrives) is observed. Checked after every commit of the host
|
|
172
|
+
* component; the state only changes when the element does.
|
|
67
173
|
*/
|
|
68
174
|
const [el, setEl] = useState<Element | null>(null);
|
|
175
|
+
const [video, setVideo] = useState<HTMLVideoElement | null>(null);
|
|
69
176
|
useLayoutEffect(() => {
|
|
70
177
|
if (ref.current !== el) setEl(ref.current);
|
|
178
|
+
const v = videoRef?.current ?? null;
|
|
179
|
+
if (v !== video) setVideo(v);
|
|
71
180
|
});
|
|
72
181
|
|
|
182
|
+
const changes = useRef<TimedChange[]>([]);
|
|
183
|
+
const lastStream = useRef<Size | null>(null);
|
|
184
|
+
const lastBySource = useRef<Record<"video" | "render", Size | null>>({ video: null, render: null });
|
|
185
|
+
|
|
186
|
+
/*
|
|
187
|
+
* Each source is compared with its own last value, so the video and the
|
|
188
|
+
* sidecar's render size reporting the same new shape at different moments
|
|
189
|
+
* are both noted. The first render size is only a baseline: a layout built
|
|
190
|
+
* from it was already in place when the first measurement went out.
|
|
191
|
+
*/
|
|
192
|
+
const noteStream = useCallback((source: "video" | "render", width: number, height: number) => {
|
|
193
|
+
if (!(width > 0) || !(height > 0)) return;
|
|
194
|
+
const seen = lastBySource.current[source];
|
|
195
|
+
if (seen && seen.width === width && seen.height === height) return;
|
|
196
|
+
const to = { width, height };
|
|
197
|
+
lastBySource.current[source] = to;
|
|
198
|
+
const from = lastStream.current;
|
|
199
|
+
lastStream.current = to;
|
|
200
|
+
if (!from && source === "render") return;
|
|
201
|
+
const now = Date.now();
|
|
202
|
+
changes.current = changes.current.filter((c) => now - c.at <= SUSPECT_MS + DEBOUNCE_MS);
|
|
203
|
+
changes.current.push({ at: now, from, to });
|
|
204
|
+
}, []);
|
|
205
|
+
|
|
206
|
+
useEffect(() => {
|
|
207
|
+
if (!video) return;
|
|
208
|
+
const sync = () => noteStream("video", video.videoWidth, video.videoHeight);
|
|
209
|
+
sync();
|
|
210
|
+
video.addEventListener("loadedmetadata", sync);
|
|
211
|
+
video.addEventListener("resize", sync);
|
|
212
|
+
return () => {
|
|
213
|
+
video.removeEventListener("loadedmetadata", sync);
|
|
214
|
+
video.removeEventListener("resize", sync);
|
|
215
|
+
};
|
|
216
|
+
}, [video, noteStream]);
|
|
217
|
+
|
|
218
|
+
/* A layout effect, so it is noted before the observer sees the box this commit lays out. */
|
|
219
|
+
const streamW = streamSize?.width ?? 0;
|
|
220
|
+
const streamH = streamSize?.height ?? 0;
|
|
221
|
+
useLayoutEffect(() => {
|
|
222
|
+
noteStream("render", streamW, streamH);
|
|
223
|
+
}, [streamW, streamH, noteStream]);
|
|
224
|
+
|
|
225
|
+
/*
|
|
226
|
+
* The aspect is read through a ref so the camera's configured size arriving
|
|
227
|
+
* after mount re-sizes the current request without a clear and re-register.
|
|
228
|
+
*/
|
|
229
|
+
const aspectRef = useRef(aspect);
|
|
230
|
+
const reapply = useRef<(() => void) | null>(null);
|
|
231
|
+
useEffect(() => {
|
|
232
|
+
aspectRef.current = aspect;
|
|
233
|
+
reapply.current?.();
|
|
234
|
+
}, [aspect]);
|
|
235
|
+
|
|
73
236
|
useEffect(() => {
|
|
74
237
|
if (flightId === null || !enabled) return;
|
|
75
238
|
if (!el) return;
|
|
@@ -78,33 +241,68 @@ export function useReportDisplaySize(
|
|
|
78
241
|
let lastW = -1;
|
|
79
242
|
let lastH = -1;
|
|
80
243
|
let firstReported = false;
|
|
244
|
+
let baseline: Size | null = null;
|
|
245
|
+
let lastSuspectAt = Number.NEGATIVE_INFINITY;
|
|
81
246
|
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
247
|
+
const sizeFor = (box: Size): Size => {
|
|
248
|
+
if (!square) return displayReportSize(box.width, box.height, aspectRef.current);
|
|
249
|
+
const side = bucket(Math.min(box.width, box.height));
|
|
250
|
+
return { width: side, height: side };
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const send = ({ width, height }: Size) => {
|
|
254
|
+
lastW = width;
|
|
255
|
+
lastH = height;
|
|
256
|
+
displaySizes.report(flightId, instanceId, width, height);
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
/*
|
|
260
|
+
* Judged when the debounce settles, so a stream change noted a little after
|
|
261
|
+
* the observer fired (a host that commits the stream and the box in a
|
|
262
|
+
* different order) still counts.
|
|
263
|
+
*/
|
|
264
|
+
const settle = (box: Size, seenAt: number) => {
|
|
265
|
+
const prev = baseline;
|
|
266
|
+
baseline = box;
|
|
267
|
+
const now = Date.now();
|
|
268
|
+
const near = changes.current.filter((c) => c.at <= now && c.at >= seenAt - SUSPECT_MS);
|
|
269
|
+
if (prev && near.some((c) => c.at >= seenAt - FOLLOW_MS && followsStream(prev, box, c))) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
const next = sizeFor(box);
|
|
273
|
+
if (next.width === lastW && next.height === lastH) return;
|
|
274
|
+
if (near.length > 0) {
|
|
275
|
+
const held = now - lastSuspectAt < HOLD_MS;
|
|
276
|
+
lastSuspectAt = now;
|
|
277
|
+
if (held) return;
|
|
278
|
+
}
|
|
279
|
+
send(next);
|
|
91
280
|
};
|
|
92
281
|
|
|
93
282
|
const observer = new ResizeObserver((entries) => {
|
|
94
283
|
const entry = entries[0];
|
|
95
284
|
if (!entry) return;
|
|
96
|
-
const { width, height
|
|
285
|
+
const box = { width: entry.contentRect.width, height: entry.contentRect.height };
|
|
97
286
|
if (!firstReported) {
|
|
98
287
|
firstReported = true;
|
|
99
|
-
|
|
288
|
+
baseline = box;
|
|
289
|
+
const next = sizeFor(box);
|
|
290
|
+
if (next.width !== lastW || next.height !== lastH) send(next);
|
|
100
291
|
return;
|
|
101
292
|
}
|
|
293
|
+
const seenAt = Date.now();
|
|
102
294
|
if (timer !== null) clearTimeout(timer);
|
|
103
|
-
timer = setTimeout(() =>
|
|
295
|
+
timer = setTimeout(() => settle(box, seenAt), DEBOUNCE_MS);
|
|
104
296
|
});
|
|
105
297
|
observer.observe(el);
|
|
298
|
+
reapply.current = () => {
|
|
299
|
+
if (!baseline) return;
|
|
300
|
+
const next = sizeFor(baseline);
|
|
301
|
+
if (next.width !== lastW || next.height !== lastH) send(next);
|
|
302
|
+
};
|
|
106
303
|
|
|
107
304
|
return () => {
|
|
305
|
+
reapply.current = null;
|
|
108
306
|
observer.disconnect();
|
|
109
307
|
if (timer !== null) clearTimeout(timer);
|
|
110
308
|
displaySizes.clear(flightId, instanceId);
|