@kubohiroya/turbowarp-camera-source 0.5.0 → 0.6.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.ja.md CHANGED
@@ -26,13 +26,13 @@ TurboWarp-Camera-Sourceは、MediaDevicesのカメラストリームを名前付
26
26
  次のURLをunsandboxed custom extensionとして読み込みます。
27
27
 
28
28
  ```text
29
- https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-source@0.5.0/dist/camera-source.js
29
+ https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-source@0.6.0/dist/camera-source.js
30
30
  ```
31
31
 
32
32
  npm hostでは次を使います。
33
33
 
34
34
  ```bash
35
- pnpm add @kubohiroya/turbowarp-camera-source@0.5.0
35
+ pnpm add @kubohiroya/turbowarp-camera-source@0.6.0
36
36
  ```
37
37
 
38
38
  ## Quick start
@@ -55,6 +55,8 @@ stop shared camera [pose]
55
55
  - `start shared camera [CAMERA_ID] with device ID [DEVICE_ID]`: 名前付き共有カメラを開始します。
56
56
  - `stop shared camera [CAMERA_ID]`: 名前付き共有カメラを停止し、MediaStreamTrackを解放します。
57
57
  - `shared camera [CAMERA_ID] is running?`: 指定した共有カメラが起動中かを返します。
58
+ - `shared camera [CAMERA_ID] error code`: 直近の開始失敗コードを返します。開始成功後は空文字列です。
59
+ - `shared camera [CAMERA_ID] error`: 直近の開始失敗メッセージを返します。開始成功後は空文字列です。
58
60
  - `shared camera [CAMERA_ID] device ID`: 指定した共有カメラのdevice IDを返します。
59
61
  - `show shared camera [CAMERA_ID] preview mirrored [MIRRORED]`: GPU-backed stage previewを表示します。
60
62
  - `hide shared camera [CAMERA_ID] preview`: ブロックが所有するpreview leaseだけを解放して非表示にします。
@@ -124,6 +126,9 @@ Scratchのtouchingまたはcolor sensing用の画像sourceとしては扱いま
124
126
 
125
127
  ## 互換性
126
128
 
129
+ 0.6.0ではcamera IDごとの開始失敗情報を公開し、inactive streamまたは終了したvideo trackを停止状態として
130
+ 扱います。既存のcamera ID、lease ownership、preview動作は変更しません。
131
+
127
132
  0.5.0ではGPU-backed previewの表示・非表示・mirror設定と、実際のframe width/height/rateを
128
133
  TurboWarpブロックとして公開します。既定動作は変更しません。
129
134
 
package/README.md CHANGED
@@ -30,13 +30,13 @@ or select separate cameras for separate roles.
30
30
  Load this URL as an unsandboxed custom extension:
31
31
 
32
32
  ```text
33
- https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-source@0.5.0/dist/camera-source.js
33
+ https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-source@0.6.0/dist/camera-source.js
34
34
  ```
35
35
 
36
36
  For npm hosts:
37
37
 
38
38
  ```bash
39
- pnpm add @kubohiroya/turbowarp-camera-source@0.5.0
39
+ pnpm add @kubohiroya/turbowarp-camera-source@0.6.0
40
40
  ```
41
41
 
42
42
  ## Quick Start
@@ -89,6 +89,26 @@ Reports whether a named shared camera stream is active.
89
89
  | Opcode | `isCameraRunning` |
90
90
  | `CAMERA_ID` | String, default: `default` |
91
91
 
92
+ ### `shared camera [CAMERA_ID] error code`
93
+
94
+ Returns the latest camera failure code, or an empty string after a successful start.
95
+
96
+ | Property | Value |
97
+ |---|---|
98
+ | Type | Reporter |
99
+ | Opcode | `cameraErrorCode` |
100
+ | `CAMERA_ID` | String, default: `default` |
101
+
102
+ ### `shared camera [CAMERA_ID] error`
103
+
104
+ Returns the latest camera failure message, or an empty string after a successful start.
105
+
106
+ | Property | Value |
107
+ |---|---|
108
+ | Type | Reporter |
109
+ | Opcode | `cameraError` |
110
+ | `CAMERA_ID` | String, default: `default` |
111
+
92
112
  ### `shared camera [CAMERA_ID] device ID`
93
113
 
94
114
  Returns the active device ID for a named shared camera when available.
@@ -253,6 +273,9 @@ noninteractive; it is intended for display, not Scratch touching or color-sensin
253
273
 
254
274
  ## Compatibility
255
275
 
276
+ Version 0.6.0 exposes per-camera failure details and treats inactive or ended video tracks as
277
+ stopped. Existing camera IDs, lease ownership, and preview behavior remain unchanged.
278
+
256
279
  Version 0.5.0 exposes GPU-backed preview visibility, mirroring, and actual frame width, height,
257
280
  and rate as TurboWarp blocks. The default behavior remains unchanged.
258
281
 
@@ -56,6 +56,26 @@
56
56
  "defaultValue": "default"
57
57
  } }
58
58
  },
59
+ {
60
+ "opcode": "cameraErrorCode",
61
+ "blockType": "REPORTER",
62
+ "text": "shared camera [CAMERA_ID] error code",
63
+ "description": "Returns the latest camera failure code, or an empty string after a successful start.",
64
+ "arguments": { "CAMERA_ID": {
65
+ "type": "STRING",
66
+ "defaultValue": "default"
67
+ } }
68
+ },
69
+ {
70
+ "opcode": "cameraError",
71
+ "blockType": "REPORTER",
72
+ "text": "shared camera [CAMERA_ID] error",
73
+ "description": "Returns the latest camera failure message, or an empty string after a successful start.",
74
+ "arguments": { "CAMERA_ID": {
75
+ "type": "STRING",
76
+ "defaultValue": "default"
77
+ } }
78
+ },
59
79
  {
60
80
  "opcode": "cameraDeviceIdReporter",
61
81
  "blockType": "REPORTER",
@@ -354,12 +374,23 @@
354
374
  video: options.video ?? true
355
375
  };
356
376
  }
377
+ function cameraFailure(error) {
378
+ if (error instanceof Error) return {
379
+ code: error.name || "Error",
380
+ message: error.message
381
+ };
382
+ return {
383
+ code: "Error",
384
+ message: String(error)
385
+ };
386
+ }
357
387
  var CameraSourceExtension = class {
358
388
  constructor() {
359
389
  this.sessions = /* @__PURE__ */ new Map();
360
390
  this.blockLeases = /* @__PURE__ */ new Map();
361
391
  this.blockPreviewLeases = /* @__PURE__ */ new Map();
362
392
  this.blockPreviewRevisions = /* @__PURE__ */ new Map();
393
+ this.cameraFailures = /* @__PURE__ */ new Map();
363
394
  this.devices = [];
364
395
  this.dispose = () => {
365
396
  this.stopAllCameras();
@@ -384,7 +415,13 @@
384
415
  }
385
416
  isCameraRunning(args = {}) {
386
417
  const session = this.sessions.get(normalizeId(args.CAMERA_ID));
387
- return Boolean(session?.stream);
418
+ return session?.stream ? this.isStreamRunning(session.stream) : false;
419
+ }
420
+ cameraErrorCode(args = {}) {
421
+ return this.cameraFailures.get(normalizeId(args.CAMERA_ID))?.code ?? "";
422
+ }
423
+ cameraError(args = {}) {
424
+ return this.cameraFailures.get(normalizeId(args.CAMERA_ID))?.message ?? "";
388
425
  }
389
426
  cameraDeviceIdReporter(args = {}) {
390
427
  return this.sessions.get(normalizeId(args.CAMERA_ID))?.activeDeviceId ?? "";
@@ -432,6 +469,7 @@
432
469
  session.leases.delete(token);
433
470
  session.previewLeases.delete(token);
434
471
  if (session.leases.size === 0) this.stopCameraSession(session.cameraId);
472
+ this.cameraFailures.set(cameraId, cameraFailure(error));
435
473
  throw error;
436
474
  }
437
475
  let released = false;
@@ -533,7 +571,9 @@
533
571
  if (!session.active) throw new Error("Camera acquisition was cancelled.");
534
572
  session.stream = stream;
535
573
  session.video = video;
574
+ this.watchStreamEnd(session, stream);
536
575
  this.updateActiveDevice(session);
576
+ this.cameraFailures.delete(session.cameraId);
537
577
  } catch (error) {
538
578
  stream.getTracks().forEach((track) => track.stop());
539
579
  if (video) video.srcObject = null;
@@ -544,6 +584,7 @@
544
584
  await session.startPromise;
545
585
  } catch (error) {
546
586
  if (this.sessions.get(session.cameraId) === session) this.stopCameraSession(session.cameraId);
587
+ this.cameraFailures.set(session.cameraId, cameraFailure(error));
547
588
  throw error;
548
589
  }
549
590
  }
@@ -552,6 +593,16 @@
552
593
  this.blockPreviewRevisions.set(cameraId, revision);
553
594
  return revision;
554
595
  }
596
+ isStreamRunning(stream) {
597
+ return stream.active !== false && stream.getVideoTracks().some((track) => track.readyState !== "ended");
598
+ }
599
+ watchStreamEnd(session, stream) {
600
+ const handleEnded = () => {
601
+ if (this.sessions.get(session.cameraId) !== session || session.stream !== stream) return;
602
+ if (!this.isStreamRunning(stream)) this.stopCameraSession(session.cameraId);
603
+ };
604
+ for (const track of stream.getVideoTracks()) track.addEventListener("ended", handleEnded, { once: true });
605
+ }
555
606
  updateActiveDevice(session) {
556
607
  const settings = (session.stream?.getVideoTracks()[0] ?? null)?.getSettings();
557
608
  session.activeDeviceId = typeof settings?.deviceId === "string" ? settings.deviceId : "";
@@ -37,6 +37,26 @@
37
37
  }
38
38
  ]
39
39
  },
40
+ {
41
+ "opcode": "cameraError",
42
+ "blockType": "REPORTER",
43
+ "arguments": [
44
+ {
45
+ "id": "CAMERA_ID",
46
+ "type": "STRING"
47
+ }
48
+ ]
49
+ },
50
+ {
51
+ "opcode": "cameraErrorCode",
52
+ "blockType": "REPORTER",
53
+ "arguments": [
54
+ {
55
+ "id": "CAMERA_ID",
56
+ "type": "STRING"
57
+ }
58
+ ]
59
+ },
40
60
  {
41
61
  "opcode": "cameraFrameHeight",
42
62
  "blockType": "REPORTER",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-camera-source",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "author": "Hiroya Kubo <hiroya@cuc.ac.jp>",
5
5
  "description": "A TurboWarp capability extension for sharing one MediaDevices camera stream across extensions.",
6
6
  "type": "module",
@@ -29,27 +29,9 @@
29
29
  "access": "public",
30
30
  "registry": "https://registry.npmjs.org/"
31
31
  },
32
- "packageManager": "pnpm@11.18.0",
33
32
  "engines": {
34
33
  "node": ">=22.18.0"
35
34
  },
36
- "scripts": {
37
- "dev": "vite build --watch",
38
- "typecheck": "tsc --noEmit",
39
- "build": "vite build",
40
- "lint": "eslint src tests vite.config.ts",
41
- "format": "eslint src tests vite.config.ts --fix",
42
- "test": "vitest run",
43
- "docs": "node scripts/generate-readme.ts",
44
- "docs:check": "pnpm run docs && git diff --exit-code -- README.md",
45
- "docs:pages:check": "node scripts/check-pages.ts",
46
- "repo:check": "node scripts/check-repo.ts",
47
- "pack:check": "npm pack --dry-run --ignore-scripts",
48
- "release:check": "pnpm publish --dry-run --access public --no-git-checks",
49
- "check": "pnpm run typecheck && pnpm run lint && pnpm run test && pnpm run docs:check && pnpm run docs:pages:check && pnpm run check:dist && pnpm run repo:check && pnpm run pack:check",
50
- "check:dist": "pnpm run build && node scripts/check-dist.ts",
51
- "prepack": "pnpm run check"
52
- },
53
35
  "devDependencies": {
54
36
  "@eslint/js": "^9.39.5",
55
37
  "@kubohiroya/vite-plugin-turbowarp-extension": "0.4.0",
@@ -67,5 +49,21 @@
67
49
  "camera",
68
50
  "media-devices",
69
51
  "getusermedia"
70
- ]
71
- }
52
+ ],
53
+ "scripts": {
54
+ "dev": "vite build --watch",
55
+ "typecheck": "tsc --noEmit",
56
+ "build": "vite build",
57
+ "lint": "eslint src tests vite.config.ts",
58
+ "format": "eslint src tests vite.config.ts --fix",
59
+ "test": "vitest run",
60
+ "docs": "node scripts/generate-readme.ts",
61
+ "docs:check": "pnpm run docs && git diff --exit-code -- README.md",
62
+ "docs:pages:check": "node scripts/check-pages.ts",
63
+ "repo:check": "node scripts/check-repo.ts",
64
+ "pack:check": "npm pack --dry-run --ignore-scripts",
65
+ "release:check": "pnpm publish --dry-run --access public --no-git-checks",
66
+ "check": "pnpm run typecheck && pnpm run lint && pnpm run test && pnpm run docs:check && pnpm run docs:pages:check && pnpm run check:dist && pnpm run repo:check && pnpm run pack:check",
67
+ "check:dist": "pnpm run build && node scripts/check-dist.ts"
68
+ }
69
+ }