@kubohiroya/turbowarp-camera-source 0.14.0 → 0.16.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.14.0/dist/camera-source.js
29
+ https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-source@0.16.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.14.0
35
+ pnpm add @kubohiroya/turbowarp-camera-source@0.16.0
36
36
  ```
37
37
 
38
38
  ## Quick start
@@ -56,6 +56,9 @@ stop shared camera [pose]
56
56
 
57
57
  - `start shared camera [CAMERA_ID] with device ID [DEVICE_ID]`: 名前付き共有カメラを開始します。
58
58
  - `start shared camera [CAMERA_ID] requested by this page`: ページのクエリパラメータが指定するカメラを開始します。`cameraDeviceId`は厳密に、`cameraWidth`・`cameraHeight`・`cameraFrameRate`は理想値として要求します。指定がなければ、デバイスを指定しない`start shared camera`と同じです。複数のUSBカメラを使うアプリが、同じoriginの別のアプリ(レンズ校正アプリなど)をそのうち1台のために開くときの指定方法です。
59
+ - `start shared camera [CAMERA_ID] from the browser's camera button`: ブラウザ自身のカメラボタンをステージに置き、操作者が答えるまで待って、返ってきたstreamをこのカメラIDで共有します(`getUserMedia`は呼びません)。エディタで開いた`.sb3`のように、作品の上に要素を置くhostが居ない場合のためのブロックです。そのカメラIDがすでにstreamを持つとき——hostが採用したpackaged作品——は、訊き直さずそのstreamに合流します。拒否されても作品は動き続けます。カメラは起動せず、`shared camera error code`は`usermedia-cancelled`になり、`start shared camera`も使えます。要素を持たないブラウザでは`usermedia-unsupported`を報告します。
60
+ - `hide the camera button for shared camera [CAMERA_ID]`: まだ答えを待っているカメラボタンを取り除きます。待っていたスクリプトは続行し、カメラのエラーコードは`usermedia-cancelled`になります。停止ボタンや別の作品の読み込みでも取り除かれます。
61
+ - `browser's camera button available?`: このブラウザがカメラボタンの要素(`<usermedia>`、Chrome 151以降)を持つかどうかです。ボタンを出す前に確認し、偽のときは`start shared camera`に落としてください。
59
62
  - `stop shared camera [CAMERA_ID]`: 名前付き共有カメラを停止し、MediaStreamTrackを解放します。
60
63
  - `shared camera [CAMERA_ID] is running?`: 指定した共有カメラが起動中かを返します。
61
64
  - `shared camera [CAMERA_ID] error code`: 直近の開始失敗コードを返します。開始成功後は空文字列です。
@@ -279,6 +282,19 @@ if <(stored camera profile result for [pose]) = [restored]> then
279
282
 
280
283
  ## 互換性
281
284
 
285
+ 0.16.0では、ブラウザ自身のカメラボタンをステージに置く3つのブロックを追加した。
286
+ `start shared camera [CAMERA_ID] from the browser's camera button`、`hide the camera button for
287
+ shared camera [CAMERA_ID]`、`browser's camera button available?`である。エディタで開いた`.sb3`の
288
+ ように、ドキュメントがTurboWarpのもので、作品の上にcapability elementを置くhostが居ない場合の
289
+ ためのものである。streamは`getUserMedia`を呼ばずに`adoptCamera`を通って届き、すでにstreamを持つ
290
+ カメラIDは訊き直さずそのstreamに合流する。拒否、要素を持たないブラウザ、取得失敗はいずれも
291
+ `shared camera error code`に載るので、通常の`start shared camera`の経路は残る。既存のブロックと
292
+ capabilityのメンバーは変えていない。
293
+
294
+ 0.15.0では`getInfo()`に`docsURI`と`blockIconURI`を宣言した。この拡張を同梱するホストが、
295
+ パレットのアイコンとドキュメントボタンを描けるようになる。既存のブロックとcapabilityのメンバーは
296
+ 変えていない。
297
+
282
298
  0.14.0では呼び出し側が持っているstreamを共有する`adoptCamera`と、`./runtime`に`CameraAdoptOptions`・
283
299
  `CameraLeaseOptions`の宣言を追加した。`CameraAcquireOptions`のメンバーは変えていない。既存のブロックと
284
300
  capabilityのメンバーは変えていない。
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.14.0/dist/camera-source.js
33
+ https://cdn.jsdelivr.net/npm/@kubohiroya/turbowarp-camera-source@0.16.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.14.0
39
+ pnpm add @kubohiroya/turbowarp-camera-source@0.16.0
40
40
  ```
41
41
 
42
42
  ## Quick Start
@@ -79,6 +79,35 @@ Starts the camera the page's query parameters name: `cameraDeviceId` exactly, an
79
79
  | Opcode | `startRequestedSharedCamera` |
80
80
  | `CAMERA_ID` | String, default: `default` |
81
81
 
82
+ ### `start shared camera [CAMERA_ID] from the browser's camera button`
83
+
84
+ Places the browser's own camera button on the stage and waits for the operator to answer it, then shares the stream it returns under this camera id without calling getUserMedia. For a .sb3 opened in the editor, where no host above the project can place the element. When the camera id already runs a stream -- a packaged work whose host adopted one -- this joins that stream instead of asking again. A refusal leaves the work running: the camera does not start, `shared camera error code` reads `usermedia-cancelled`, and `start shared camera` is still available. Browsers without the element report `usermedia-unsupported`.
85
+
86
+ | Property | Value |
87
+ |---|---|
88
+ | Type | Command |
89
+ | Opcode | `startSharedCameraFromCameraButton` |
90
+ | `CAMERA_ID` | String, default: `default` |
91
+
92
+ ### `hide the camera button for shared camera [CAMERA_ID]`
93
+
94
+ Removes a camera button still waiting for an answer. The script waiting on it continues, with `usermedia-cancelled` as the camera's error code. The stop sign and loading another project remove it too.
95
+
96
+ | Property | Value |
97
+ |---|---|
98
+ | Type | Command |
99
+ | Opcode | `hideCameraButton` |
100
+ | `CAMERA_ID` | String, default: `default` |
101
+
102
+ ### `browser's camera button available?`
103
+
104
+ Whether this browser has the camera button element (`<usermedia>`, Chrome 151 and later). Check it before offering the button, and fall back to `start shared camera` where it is false.
105
+
106
+ | Property | Value |
107
+ |---|---|
108
+ | Type | Boolean |
109
+ | Opcode | `cameraButtonAvailable` |
110
+
82
111
  ### `stop shared camera [CAMERA_ID]`
83
112
 
84
113
  Stops a named shared camera stream and releases its tracks.
@@ -680,6 +709,19 @@ profile), and save it. An app that opens the calibration app for one camera name
680
709
 
681
710
  ## Compatibility
682
711
 
712
+ Version 0.16.0 adds three blocks that place the browser's own camera button on the stage:
713
+ `start shared camera [CAMERA_ID] from the browser's camera button`, `hide the camera button for
714
+ shared camera [CAMERA_ID]` and `browser's camera button available?`. They exist for a `.sb3` opened
715
+ in the editor, where the document belongs to TurboWarp and no host above the project can place a
716
+ capability element. The stream arrives through `adoptCamera` without `getUserMedia` being called,
717
+ and a camera id that already runs a stream joins it rather than asking again. A refusal, a browser
718
+ without the element and a failed open all land in `shared camera error code`, so the ordinary
719
+ `start shared camera` route stays available. No existing block or capability member changes.
720
+
721
+ Version 0.15.0 declares `docsURI` and `blockIconURI` in `getInfo()`, so a host that
722
+ bundles this extension can draw its palette icon and its documentation button. No existing block
723
+ or capability member changes.
724
+
683
725
  Version 0.14.0 adds `adoptCamera` for sharing a stream the caller already holds, and the
684
726
  `CameraAdoptOptions` and `CameraLeaseOptions` declarations to `./runtime`. `CameraAcquireOptions`
685
727
  keeps every member it had. No existing block or capability member changes.
@@ -44,6 +44,30 @@
44
44
  "defaultValue": "default"
45
45
  } }
46
46
  },
47
+ {
48
+ "opcode": "startSharedCameraFromCameraButton",
49
+ "blockType": "COMMAND",
50
+ "text": "start shared camera [CAMERA_ID] from the browser's camera button",
51
+ "arguments": { "CAMERA_ID": {
52
+ "type": "STRING",
53
+ "defaultValue": "default"
54
+ } }
55
+ },
56
+ {
57
+ "opcode": "hideCameraButton",
58
+ "blockType": "COMMAND",
59
+ "text": "hide the camera button for shared camera [CAMERA_ID]",
60
+ "arguments": { "CAMERA_ID": {
61
+ "type": "STRING",
62
+ "defaultValue": "default"
63
+ } }
64
+ },
65
+ {
66
+ "opcode": "cameraButtonAvailable",
67
+ "blockType": "BOOLEAN",
68
+ "text": "browser's camera button available?",
69
+ "arguments": {}
70
+ },
47
71
  {
48
72
  "opcode": "stopSharedCamera",
49
73
  "blockType": "COMMAND",
@@ -2052,6 +2076,72 @@
2052
2076
  return flip === "vertical" || flip === "both";
2053
2077
  }
2054
2078
  //#endregion
2079
+ //#region src/user-media.ts
2080
+ /** Whether this browser has the capability element at all. */
2081
+ function supportsUserMediaElement(view = globalThis) {
2082
+ return "HTMLUserMediaElement" in view;
2083
+ }
2084
+ /**
2085
+ * Places `<usermedia>` in `container` and resolves once the operator has answered.
2086
+ *
2087
+ * No styling is applied beyond taking the element out of the document flow: the element enforces
2088
+ * its own contrast and size rules, and a stylesheet fighting them is how a prompt stops opening.
2089
+ */
2090
+ function requestUserMedia(options) {
2091
+ const element = options.document.createElement("usermedia");
2092
+ const button = options.document.createElement("button");
2093
+ button.type = "button";
2094
+ button.textContent = options.label;
2095
+ element.append(button);
2096
+ element.style.position = "absolute";
2097
+ element.style.left = "50%";
2098
+ element.style.top = "50%";
2099
+ element.style.transform = "translate(-50%, -50%)";
2100
+ element.style.zIndex = "2";
2101
+ try {
2102
+ element.setConstraints?.({ video: {} });
2103
+ } catch {}
2104
+ let settle = () => {};
2105
+ const settled = new Promise((resolve) => {
2106
+ settle = resolve;
2107
+ });
2108
+ let done = false;
2109
+ const finish = (outcome) => {
2110
+ if (done) return;
2111
+ done = true;
2112
+ element.remove();
2113
+ settle(outcome);
2114
+ };
2115
+ element.addEventListener("stream", () => {
2116
+ const stream = element.stream;
2117
+ if (stream && typeof stream.getTracks === "function") finish({
2118
+ kind: "stream",
2119
+ stream
2120
+ });
2121
+ else finish({
2122
+ kind: "error",
2123
+ code: "usermedia-empty",
2124
+ message: "The camera button returned no stream."
2125
+ });
2126
+ });
2127
+ element.addEventListener("cancel", () => finish({ kind: "cancelled" }));
2128
+ element.addEventListener("error", (event) => {
2129
+ const detail = event.error;
2130
+ const code = typeof detail?.name === "string" && detail.name.length > 0 ? detail.name : "usermedia-error";
2131
+ const message = typeof detail?.message === "string" && detail.message.length > 0 ? detail.message : "The camera button could not open the camera.";
2132
+ finish({
2133
+ kind: "error",
2134
+ code,
2135
+ message
2136
+ });
2137
+ });
2138
+ options.container.append(element);
2139
+ return Object.freeze({
2140
+ dismiss: () => finish({ kind: "cancelled" }),
2141
+ settled
2142
+ });
2143
+ }
2144
+ //#endregion
2055
2145
  //#region src/video-preview.ts
2056
2146
  var videoLayer = "video";
2057
2147
  var haveCurrentData = 2;
@@ -2288,6 +2378,13 @@
2288
2378
  //#region src/extension.ts
2289
2379
  var blockDefinitions = block_definitions_default.blocks;
2290
2380
  var menuDefinitions = block_definitions_default.menus;
2381
+ /**
2382
+ * The documentation site this extension's palette button opens, and the icon the palette draws
2383
+ * on every block. Hosts that bundle several extensions read both out of `getInfo()`, so a
2384
+ * bundled Camera Source without them loses its docs button in the host's palette.
2385
+ */
2386
+ var EXTENSION_DOCS_URI = "https://kubohiroya.github.io/turbowarp-camera-source/";
2387
+ var BLOCK_ICON_URI = `data:image/svg+xml,${encodeURIComponent("<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 64 64\"><g fill=\"none\" stroke=\"#fff\" stroke-width=\"5\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path d=\"M12 20h24a4 4 0 0 1 4 4v16a4 4 0 0 1-4 4H12a4 4 0 0 1-4-4V24a4 4 0 0 1 4-4Z\"/><path d=\"m40 29 14-9v24l-14-9\"/></g></svg>")}`;
2291
2388
  var defaultCameraId = "default";
2292
2389
  function mediaDevices() {
2293
2390
  const devices = globalThis.navigator?.mediaDevices;
@@ -2424,6 +2521,7 @@
2424
2521
  this.blockPreviewLeases = /* @__PURE__ */ new Map();
2425
2522
  this.blockPreviewRevisions = /* @__PURE__ */ new Map();
2426
2523
  this.cameraFailures = /* @__PURE__ */ new Map();
2524
+ this.cameraButtons = /* @__PURE__ */ new Map();
2427
2525
  this.profiles = new CameraProfileRegistry();
2428
2526
  this.generations = /* @__PURE__ */ new Map();
2429
2527
  this.lastConditions = /* @__PURE__ */ new Map();
@@ -2469,6 +2567,8 @@
2469
2567
  return {
2470
2568
  id: extensionConfig.id,
2471
2569
  name: Scratch.translate(block_definitions_default.extensionName),
2570
+ docsURI: EXTENSION_DOCS_URI,
2571
+ blockIconURI: BLOCK_ICON_URI,
2472
2572
  blocks: blockDefinitions.map((block) => this.toScratchBlock(block)),
2473
2573
  menus: Object.fromEntries(Object.entries(menuDefinitions).map(([id, menu]) => [id, {
2474
2574
  acceptReporters: menu.acceptReporters,
@@ -2619,6 +2719,8 @@
2619
2719
  return this.devices[indexFrom(args.INDEX)]?.label ?? "";
2620
2720
  }
2621
2721
  stopAllCameras() {
2722
+ for (const request of [...this.cameraButtons.values()]) request.dismiss();
2723
+ this.cameraButtons.clear();
2622
2724
  for (const cameraId of [...this.sessions.keys()]) this.stopCameraSession(cameraId);
2623
2725
  this.blockLeases.clear();
2624
2726
  this.blockPreviewLeases.clear();
@@ -2765,6 +2867,105 @@
2765
2867
  });
2766
2868
  this.blockLeases.set(cameraId, lease);
2767
2869
  }
2870
+ /** Whether this browser has the camera button element at all. */
2871
+ cameraButtonAvailable() {
2872
+ return supportsUserMediaElement();
2873
+ }
2874
+ /**
2875
+ * Starts a camera from the browser's own camera button, rather than from `getUserMedia`.
2876
+ *
2877
+ * For a `.sb3` opened in the editor. The document belongs to TurboWarp there, so no host above
2878
+ * the project can place a capability element -- and without one, the browser-managed handover is
2879
+ * closed to the work entirely. #39 kept this element out of the extension so that no extension
2880
+ * could manufacture an operator's intent; placing a block *is* that intent, and the browser still
2881
+ * collects the click and draws the prompt. What the block does is put the element where it can be
2882
+ * clicked.
2883
+ *
2884
+ * A camera id that already runs a stream joins it: a packaged work's host adopted one before the
2885
+ * project started, and asking again would only refuse (`adoptCamera` guards an id against a
2886
+ * second stream). So the same project runs in both distributions with this block in it.
2887
+ *
2888
+ * Nothing here throws into the project. A refusal, an unsupported browser and a failed open all
2889
+ * land in `shared camera error code`, and `start shared camera` remains available.
2890
+ */
2891
+ async startSharedCameraFromCameraButton(args = {}) {
2892
+ const cameraId = normalizeId(args.CAMERA_ID);
2893
+ if (this.blockLeases.has(cameraId)) return;
2894
+ if (this.sessions.get(cameraId)?.stream) {
2895
+ this.blockLeases.set(cameraId, await this.acquireCamera({
2896
+ owner: "camera-source-block",
2897
+ cameraId
2898
+ }));
2899
+ return;
2900
+ }
2901
+ if (!supportsUserMediaElement()) {
2902
+ this.cameraFailures.set(cameraId, {
2903
+ code: "usermedia-unsupported",
2904
+ message: "This browser has no camera button element."
2905
+ });
2906
+ return;
2907
+ }
2908
+ const container = this.cameraButtonContainer();
2909
+ if (!container) {
2910
+ this.cameraFailures.set(cameraId, {
2911
+ code: "usermedia-no-stage",
2912
+ message: "The stage the camera button would be placed on was not found."
2913
+ });
2914
+ return;
2915
+ }
2916
+ this.cameraButtons.get(cameraId)?.dismiss();
2917
+ const request = requestUserMedia({
2918
+ document,
2919
+ container,
2920
+ label: Scratch.translate("Allow the camera")
2921
+ });
2922
+ this.cameraButtons.set(cameraId, request);
2923
+ const outcome = await request.settled;
2924
+ if (this.cameraButtons.get(cameraId) === request) this.cameraButtons.delete(cameraId);
2925
+ if (outcome.kind === "cancelled") {
2926
+ this.cameraFailures.set(cameraId, {
2927
+ code: "usermedia-cancelled",
2928
+ message: "The camera was not allowed."
2929
+ });
2930
+ return;
2931
+ }
2932
+ if (outcome.kind === "error") {
2933
+ this.cameraFailures.set(cameraId, {
2934
+ code: outcome.code,
2935
+ message: outcome.message
2936
+ });
2937
+ return;
2938
+ }
2939
+ try {
2940
+ this.blockLeases.set(cameraId, await this.adoptCamera({
2941
+ owner: "camera-source-block",
2942
+ cameraId,
2943
+ stream: outcome.stream
2944
+ }));
2945
+ } catch (error) {
2946
+ outcome.stream.getTracks().forEach((track) => track.stop());
2947
+ this.cameraFailures.set(cameraId, cameraFailure(error));
2948
+ }
2949
+ }
2950
+ /** Removes a camera button still waiting for an answer; the script waiting on it continues. */
2951
+ hideCameraButton(args = {}) {
2952
+ this.cameraButtons.get(normalizeId(args.CAMERA_ID))?.dismiss();
2953
+ }
2954
+ /**
2955
+ * Where the camera button goes: the element that holds the stage canvas.
2956
+ *
2957
+ * The preview is a renderer skin, so the extension has had no DOM of its own to hang this on. A
2958
+ * capability element cannot be drawn into WebGL, and the editor's own layout is not ours to
2959
+ * restructure, so it is placed over the canvas and centred, with the element keeping its own look.
2960
+ */
2961
+ cameraButtonContainer() {
2962
+ const parent = (Scratch.vm.runtime.renderer?.gl?.canvas)?.parentElement ?? null;
2963
+ if (!parent) return null;
2964
+ const style = parent.style;
2965
+ const computed = globalThis.getComputedStyle?.(parent);
2966
+ if (style && computed?.position === "static") style.position = "relative";
2967
+ return parent;
2968
+ }
2768
2969
  async restoreStoredProfile(cameraId, sameDevice) {
2769
2970
  let records;
2770
2971
  try {
@@ -12,6 +12,11 @@
12
12
  }
13
13
  ]
14
14
  },
15
+ {
16
+ "opcode": "cameraButtonAvailable",
17
+ "blockType": "BOOLEAN",
18
+ "arguments": []
19
+ },
15
20
  {
16
21
  "opcode": "cameraConditionsGeneration",
17
22
  "blockType": "REPORTER",
@@ -217,6 +222,16 @@
217
222
  }
218
223
  ]
219
224
  },
225
+ {
226
+ "opcode": "hideCameraButton",
227
+ "blockType": "COMMAND",
228
+ "arguments": [
229
+ {
230
+ "id": "CAMERA_ID",
231
+ "type": "STRING"
232
+ }
233
+ ]
234
+ },
220
235
  {
221
236
  "opcode": "hideCameraPreview",
222
237
  "blockType": "COMMAND",
@@ -335,6 +350,16 @@
335
350
  }
336
351
  ]
337
352
  },
353
+ {
354
+ "opcode": "startSharedCameraFromCameraButton",
355
+ "blockType": "COMMAND",
356
+ "arguments": [
357
+ {
358
+ "id": "CAMERA_ID",
359
+ "type": "STRING"
360
+ }
361
+ ]
362
+ },
338
363
  {
339
364
  "opcode": "stopSharedCamera",
340
365
  "blockType": "COMMAND",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-camera-source",
3
- "version": "0.14.0",
3
+ "version": "0.16.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",