@kubohiroya/turbowarp-camera-calibration 0.8.0 → 0.8.1

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.ja.md CHANGED
@@ -101,7 +101,7 @@ bundleは約11 MBあります。
101
101
  検証済みのversionをexact pinします。
102
102
 
103
103
  ```bash
104
- pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.8.0
104
+ pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.8.1
105
105
  ```
106
106
 
107
107
  standalone bundle:
@@ -113,7 +113,7 @@ node_modules/@kubohiroya/turbowarp-camera-calibration/dist/camera-calibration.js
113
113
  version固定CDN URL:
114
114
 
115
115
  ```text
116
- https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.8.0/dist/camera-calibration.js
116
+ https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.8.1/dist/camera-calibration.js
117
117
  ```
118
118
 
119
119
  ## クイックスタート
package/README.md CHANGED
@@ -102,7 +102,7 @@ therefore about 11 MB.
102
102
  Install an exact version that you have reviewed:
103
103
 
104
104
  ```bash
105
- pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.8.0
105
+ pnpm add --save-exact @kubohiroya/turbowarp-camera-calibration@0.8.1
106
106
  ```
107
107
 
108
108
  Load the standalone bundle from:
@@ -114,7 +114,7 @@ node_modules/@kubohiroya/turbowarp-camera-calibration/dist/camera-calibration.js
114
114
  A version-pinned CDN URL is:
115
115
 
116
116
  ```text
117
- https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.8.0/dist/camera-calibration.js
117
+ https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-calibration@0.8.1/dist/camera-calibration.js
118
118
  ```
119
119
 
120
120
  ## Quick start
@@ -798,6 +798,28 @@
798
798
  }
799
799
  //#endregion
800
800
  //#region src/calibration/controller.ts
801
+ /**
802
+ * How long a just-acquired camera is given to produce its first frame.
803
+ *
804
+ * Camera Source reports a camera as started once `play()` resolves, and for a
805
+ * MediaStream that can happen before the metadata carrying the frame size has
806
+ * arrived -- so the very first look at the video can legitimately find a
807
+ * zero-sized element. Treating that as a dead camera ends a session the
808
+ * operator just started, on a camera that was about to work.
809
+ *
810
+ * Long enough for a slow USB camera to enumerate, short enough that a camera
811
+ * which really is producing nothing does not leave the operator waiting
812
+ * without an answer.
813
+ */
814
+ var FIRST_FRAME_TIMEOUT_MS = 4e3;
815
+ var FIRST_FRAME_POLL_MS = 30;
816
+ /**
817
+ * The same limit counted in looks rather than milliseconds.
818
+ *
819
+ * Both, because the clock is injected: a host that supplies a coarse or
820
+ * stopped one would otherwise turn this wait into a loop with no way out.
821
+ */
822
+ var FIRST_FRAME_ATTEMPTS = Math.ceil(FIRST_FRAME_TIMEOUT_MS / FIRST_FRAME_POLL_MS);
801
823
  /** The document a measured pose is reported as. */
802
824
  var BOARD_POSE_SCHEMA = "twcc/board-pose";
803
825
  var MINIMUM_SAMPLES = 8;
@@ -908,11 +930,15 @@
908
930
  }
909
931
  let frame;
910
932
  try {
911
- frame = requireVideoFrame(lease);
933
+ frame = await this.awaitFirstFrame(lease, operation);
912
934
  } catch (error) {
913
935
  await lease.release();
914
936
  this.fail("camera-ended", error);
915
937
  }
938
+ if (operation !== this.operation) {
939
+ await lease.release();
940
+ return;
941
+ }
916
942
  this.session = {
917
943
  calibrationId: normalized.calibrationId,
918
944
  board: normalized.board,
@@ -970,6 +996,30 @@
970
996
  guidance() {
971
997
  return this.guidanceCode;
972
998
  }
999
+ /**
1000
+ * Waits for the camera to hand over a frame with a size on it.
1001
+ *
1002
+ * The session's resolution is fixed from this frame and every later sample
1003
+ * is checked against it, so reading it one tick too early would fix the
1004
+ * session to a size the camera never had.
1005
+ */
1006
+ async awaitFirstFrame(lease, operation) {
1007
+ const deadline = this.nowMilliseconds() + FIRST_FRAME_TIMEOUT_MS;
1008
+ for (let attempt = 0;; attempt += 1) {
1009
+ const frame = lease.getFrameSource();
1010
+ if (frame.kind === "video" && frame.width >= 1 && frame.height >= 1) return frame;
1011
+ if (operation !== this.operation) throw new Error("The calibration was cancelled while the camera started.");
1012
+ if (this.nowMilliseconds() >= deadline || attempt >= FIRST_FRAME_ATTEMPTS) throw new Error(`The camera produced no frame within ${FIRST_FRAME_TIMEOUT_MS} ms of starting.`);
1013
+ await this.delay(FIRST_FRAME_POLL_MS);
1014
+ }
1015
+ }
1016
+ delay(milliseconds) {
1017
+ return new Promise((resolve) => {
1018
+ this.schedule(() => {
1019
+ resolve();
1020
+ }, milliseconds);
1021
+ });
1022
+ }
973
1023
  stopAutomatic() {
974
1024
  this.automatic = false;
975
1025
  this.cancelTick?.();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-camera-calibration",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "A TurboWarp extension that calibrates a shared Camera Source camera and publishes its intrinsic calibration profile.",
5
5
  "author": "Hiroya Kubo <hiroya@cuc.ac.jp>",
6
6
  "license": "MPL-2.0",
@@ -38,6 +38,29 @@ import type {
38
38
  CalibrationSolveResult
39
39
  } from './types.js';
40
40
 
41
+ /**
42
+ * How long a just-acquired camera is given to produce its first frame.
43
+ *
44
+ * Camera Source reports a camera as started once `play()` resolves, and for a
45
+ * MediaStream that can happen before the metadata carrying the frame size has
46
+ * arrived -- so the very first look at the video can legitimately find a
47
+ * zero-sized element. Treating that as a dead camera ends a session the
48
+ * operator just started, on a camera that was about to work.
49
+ *
50
+ * Long enough for a slow USB camera to enumerate, short enough that a camera
51
+ * which really is producing nothing does not leave the operator waiting
52
+ * without an answer.
53
+ */
54
+ const FIRST_FRAME_TIMEOUT_MS = 4000;
55
+ const FIRST_FRAME_POLL_MS = 30;
56
+ /**
57
+ * The same limit counted in looks rather than milliseconds.
58
+ *
59
+ * Both, because the clock is injected: a host that supplies a coarse or
60
+ * stopped one would otherwise turn this wait into a loop with no way out.
61
+ */
62
+ const FIRST_FRAME_ATTEMPTS = Math.ceil(FIRST_FRAME_TIMEOUT_MS / FIRST_FRAME_POLL_MS);
63
+
41
64
  /** The document a measured pose is reported as. */
42
65
  const BOARD_POSE_SCHEMA = 'twcc/board-pose';
43
66
 
@@ -197,11 +220,15 @@ class CameraCalibration {
197
220
  }
198
221
  let frame: CameraFrameSource;
199
222
  try {
200
- frame = requireVideoFrame(lease);
223
+ frame = await this.awaitFirstFrame(lease, operation);
201
224
  } catch (error) {
202
225
  await lease.release();
203
226
  this.fail('camera-ended', error);
204
227
  }
228
+ if (operation !== this.operation) {
229
+ await lease.release();
230
+ return;
231
+ }
205
232
  this.session = {
206
233
  calibrationId: normalized.calibrationId,
207
234
  board: normalized.board,
@@ -266,6 +293,50 @@ class CameraCalibration {
266
293
  return this.guidanceCode;
267
294
  }
268
295
 
296
+ /**
297
+ * Waits for the camera to hand over a frame with a size on it.
298
+ *
299
+ * The session's resolution is fixed from this frame and every later sample
300
+ * is checked against it, so reading it one tick too early would fix the
301
+ * session to a size the camera never had.
302
+ */
303
+ private async awaitFirstFrame(
304
+ lease: CameraLease,
305
+ operation: number
306
+ ): Promise<CameraFrameSource> {
307
+ const deadline = this.nowMilliseconds() + FIRST_FRAME_TIMEOUT_MS;
308
+ for (let attempt = 0; ; attempt += 1) {
309
+ // Deliberately not `requireVideoFrame`. A lease that throws is a lease
310
+ // that is gone -- Camera Source saying there is no camera here any more
311
+ // -- and waiting on that would only delay the answer. A lease that
312
+ // answers with a frame of no size is a camera still starting up, and
313
+ // that is the one worth waiting for.
314
+ const frame = lease.getFrameSource();
315
+ if (frame.kind === 'video' && frame.width >= 1 && frame.height >= 1) {
316
+ return frame;
317
+ }
318
+ // A cancel while waiting is not a camera failure, and the caller
319
+ // releases the lease either way.
320
+ if (operation !== this.operation) {
321
+ throw new Error('The calibration was cancelled while the camera started.');
322
+ }
323
+ if (this.nowMilliseconds() >= deadline || attempt >= FIRST_FRAME_ATTEMPTS) {
324
+ throw new Error(
325
+ `The camera produced no frame within ${FIRST_FRAME_TIMEOUT_MS} ms of starting.`
326
+ );
327
+ }
328
+ await this.delay(FIRST_FRAME_POLL_MS);
329
+ }
330
+ }
331
+
332
+ private delay(milliseconds: number): Promise<void> {
333
+ return new Promise((resolve) => {
334
+ this.schedule(() => {
335
+ resolve();
336
+ }, milliseconds);
337
+ });
338
+ }
339
+
269
340
  private stopAutomatic(): void {
270
341
  this.automatic = false;
271
342
  this.cancelTick?.();