@kubohiroya/turbowarp-camera-source 0.4.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 +30 -2
- package/README.md +96 -2
- package/dist/camera-source.js +198 -10
- package/dist/extension-manifest.json +74 -0
- package/package.json +2 -1
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.
|
|
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.
|
|
35
|
+
pnpm add @kubohiroya/turbowarp-camera-source@0.6.0
|
|
36
36
|
```
|
|
37
37
|
|
|
38
38
|
## Quick start
|
|
@@ -41,6 +41,10 @@ pnpm add @kubohiroya/turbowarp-camera-source@0.4.0
|
|
|
41
41
|
|
|
42
42
|
```text
|
|
43
43
|
start shared camera [pose] with device ID []
|
|
44
|
+
show shared camera [pose] preview mirrored [true]
|
|
45
|
+
shared camera [pose] frame width
|
|
46
|
+
shared camera [pose] frame rate
|
|
47
|
+
hide shared camera [pose] preview
|
|
44
48
|
start shared camera [qr] with device ID []
|
|
45
49
|
shared camera [pose] is running?
|
|
46
50
|
stop shared camera [pose]
|
|
@@ -51,7 +55,14 @@ stop shared camera [pose]
|
|
|
51
55
|
- `start shared camera [CAMERA_ID] with device ID [DEVICE_ID]`: 名前付き共有カメラを開始します。
|
|
52
56
|
- `stop shared camera [CAMERA_ID]`: 名前付き共有カメラを停止し、MediaStreamTrackを解放します。
|
|
53
57
|
- `shared camera [CAMERA_ID] is running?`: 指定した共有カメラが起動中かを返します。
|
|
58
|
+
- `shared camera [CAMERA_ID] error code`: 直近の開始失敗コードを返します。開始成功後は空文字列です。
|
|
59
|
+
- `shared camera [CAMERA_ID] error`: 直近の開始失敗メッセージを返します。開始成功後は空文字列です。
|
|
54
60
|
- `shared camera [CAMERA_ID] device ID`: 指定した共有カメラのdevice IDを返します。
|
|
61
|
+
- `show shared camera [CAMERA_ID] preview mirrored [MIRRORED]`: GPU-backed stage previewを表示します。
|
|
62
|
+
- `hide shared camera [CAMERA_ID] preview`: ブロックが所有するpreview leaseだけを解放して非表示にします。
|
|
63
|
+
- `shared camera [CAMERA_ID] frame width`: 実際のframe幅をpixel単位で返します。
|
|
64
|
+
- `shared camera [CAMERA_ID] frame height`: 実際のframe高さをpixel単位で返します。
|
|
65
|
+
- `shared camera [CAMERA_ID] frame rate`: 有効なvideo trackのframe rateを返します。
|
|
55
66
|
- `refresh camera devices`: カメラデバイス一覧を更新します。
|
|
56
67
|
- `camera device count`: 更新済みカメラデバイス数を返します。
|
|
57
68
|
- `camera device ID at [INDEX]`: 1始まりの位置でカメラdevice IDを返します。
|
|
@@ -91,6 +102,17 @@ await poseLease.release();
|
|
|
91
102
|
複数のpreview leaseが同じカメラを共有する場合、1つ以上の有効なleaseが`mirrored: true`を要求している間は
|
|
92
103
|
previewを左右反転します。
|
|
93
104
|
|
|
105
|
+
TurboWarp作品からは`show shared camera ... preview`と`hide shared camera ... preview`ブロックで同じ
|
|
106
|
+
表示経路を操作できます。showブロックが所有するpreview leaseはcamera IDごとに1つだけです。同じ設定で
|
|
107
|
+
繰り返し実行してもleaseやdrawableは増えず、`MIRRORED`を変更した場合は既存camera sessionを維持したまま
|
|
108
|
+
leaseを置換します。`MIRRORED`には`true`または`false`など、TurboWarpがbooleanとして解釈できる値を指定します。
|
|
109
|
+
hideはブロック所有の表示leaseだけを解放するため、ほかのブロックや拡張が所有する処理用leaseは継続します。
|
|
110
|
+
名前付きcameraの停止、作品の停止・再読込、runtime disposeではブロック所有resourceをすべて解放します。
|
|
111
|
+
|
|
112
|
+
frame widthとheightは、video elementが現在受信している寸法を優先し、最初のframeより前はvideo track設定を
|
|
113
|
+
fallbackとして使います。frame rateは有効なvideo track設定を返します。cameraが未起動の場合、またはブラウザが
|
|
114
|
+
値を提供しない場合、これら3つのreporterは`0`を返します。
|
|
115
|
+
|
|
94
116
|
`frame.element`はpreviewを左右反転した場合も未加工の`HTMLVideoElement`です。そのためWebGPUや
|
|
95
117
|
WebCodecsのconsumerは、CPU canvasやpreviewを経由せず、同じsourceを
|
|
96
118
|
`GPUQueue.copyExternalImageToTexture()`や短時間だけ保持する`VideoFrame`へ渡せます。consumerが生成した
|
|
@@ -104,6 +126,12 @@ Scratchのtouchingまたはcolor sensing用の画像sourceとしては扱いま
|
|
|
104
126
|
|
|
105
127
|
## 互換性
|
|
106
128
|
|
|
129
|
+
0.6.0ではcamera IDごとの開始失敗情報を公開し、inactive streamまたは終了したvideo trackを停止状態として
|
|
130
|
+
扱います。既存のcamera ID、lease ownership、preview動作は変更しません。
|
|
131
|
+
|
|
132
|
+
0.5.0ではGPU-backed previewの表示・非表示・mirror設定と、実際のframe width/height/rateを
|
|
133
|
+
TurboWarpブロックとして公開します。既定動作は変更しません。
|
|
134
|
+
|
|
107
135
|
0.4.0ではopt-inのGPU-backed video previewを追加します。Extension ID、ブロックopcode、camera lease
|
|
108
136
|
ownership、device selection、release semanticsは変更しません。`preview: true`を指定しないconsumerは、
|
|
109
137
|
従来のcamera-source動作を維持します。
|
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.
|
|
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.
|
|
39
|
+
pnpm add @kubohiroya/turbowarp-camera-source@0.6.0
|
|
40
40
|
```
|
|
41
41
|
|
|
42
42
|
## Quick Start
|
|
@@ -45,6 +45,10 @@ Use role names for camera streams. Consumers that use the same `cameraId` share
|
|
|
45
45
|
|
|
46
46
|
```text
|
|
47
47
|
start shared camera [pose] with device ID []
|
|
48
|
+
show shared camera [pose] preview mirrored [true]
|
|
49
|
+
shared camera [pose] frame width
|
|
50
|
+
shared camera [pose] frame rate
|
|
51
|
+
hide shared camera [pose] preview
|
|
48
52
|
start shared camera [qr] with device ID []
|
|
49
53
|
shared camera [pose] is running?
|
|
50
54
|
stop shared camera [pose]
|
|
@@ -85,6 +89,26 @@ Reports whether a named shared camera stream is active.
|
|
|
85
89
|
| Opcode | `isCameraRunning` |
|
|
86
90
|
| `CAMERA_ID` | String, default: `default` |
|
|
87
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
|
+
|
|
88
112
|
### `shared camera [CAMERA_ID] device ID`
|
|
89
113
|
|
|
90
114
|
Returns the active device ID for a named shared camera when available.
|
|
@@ -95,6 +119,57 @@ Returns the active device ID for a named shared camera when available.
|
|
|
95
119
|
| Opcode | `cameraDeviceIdReporter` |
|
|
96
120
|
| `CAMERA_ID` | String, default: `default` |
|
|
97
121
|
|
|
122
|
+
### `show shared camera [CAMERA_ID] preview mirrored [MIRRORED]`
|
|
123
|
+
|
|
124
|
+
Shows the named shared camera with the GPU-backed stage preview.
|
|
125
|
+
|
|
126
|
+
| Property | Value |
|
|
127
|
+
|---|---|
|
|
128
|
+
| Type | Command |
|
|
129
|
+
| Opcode | `showCameraPreview` |
|
|
130
|
+
| `CAMERA_ID` | String, default: `default` |
|
|
131
|
+
| `MIRRORED` | String, default: `true` |
|
|
132
|
+
|
|
133
|
+
### `hide shared camera [CAMERA_ID] preview`
|
|
134
|
+
|
|
135
|
+
Hides the block-owned preview without stopping leases owned by other consumers.
|
|
136
|
+
|
|
137
|
+
| Property | Value |
|
|
138
|
+
|---|---|
|
|
139
|
+
| Type | Command |
|
|
140
|
+
| Opcode | `hideCameraPreview` |
|
|
141
|
+
| `CAMERA_ID` | String, default: `default` |
|
|
142
|
+
|
|
143
|
+
### `shared camera [CAMERA_ID] frame width`
|
|
144
|
+
|
|
145
|
+
Returns the active video frame width in pixels, or zero while the camera is not running.
|
|
146
|
+
|
|
147
|
+
| Property | Value |
|
|
148
|
+
|---|---|
|
|
149
|
+
| Type | Reporter |
|
|
150
|
+
| Opcode | `cameraFrameWidth` |
|
|
151
|
+
| `CAMERA_ID` | String, default: `default` |
|
|
152
|
+
|
|
153
|
+
### `shared camera [CAMERA_ID] frame height`
|
|
154
|
+
|
|
155
|
+
Returns the active video frame height in pixels, or zero while the camera is not running.
|
|
156
|
+
|
|
157
|
+
| Property | Value |
|
|
158
|
+
|---|---|
|
|
159
|
+
| Type | Reporter |
|
|
160
|
+
| Opcode | `cameraFrameHeight` |
|
|
161
|
+
| `CAMERA_ID` | String, default: `default` |
|
|
162
|
+
|
|
163
|
+
### `shared camera [CAMERA_ID] frame rate`
|
|
164
|
+
|
|
165
|
+
Returns the active video track frame rate, or zero when it is unavailable.
|
|
166
|
+
|
|
167
|
+
| Property | Value |
|
|
168
|
+
|---|---|
|
|
169
|
+
| Type | Reporter |
|
|
170
|
+
| Opcode | `cameraFrameRate` |
|
|
171
|
+
| `CAMERA_ID` | String, default: `default` |
|
|
172
|
+
|
|
98
173
|
### `refresh camera devices`
|
|
99
174
|
|
|
100
175
|
Refreshes the browser camera device list.
|
|
@@ -174,6 +249,19 @@ released.
|
|
|
174
249
|
If multiple preview leases share a camera, the preview is mirrored while any active preview lease
|
|
175
250
|
requests `mirrored: true`.
|
|
176
251
|
|
|
252
|
+
TurboWarp projects can manage the same display path with the `show shared camera ... preview`
|
|
253
|
+
and `hide shared camera ... preview` blocks. The show block owns exactly one preview lease per
|
|
254
|
+
camera ID. Calling it repeatedly with the same setting is idempotent; changing `MIRRORED` replaces
|
|
255
|
+
that lease without creating another drawable. `MIRRORED` accepts TurboWarp boolean text such as
|
|
256
|
+
`true` and `false`. Hiding the preview releases only the block-owned display lease, so processing
|
|
257
|
+
leases held by other blocks or extensions continue running. Stopping the named camera, stopping or
|
|
258
|
+
loading a project, and disposing the runtime release the block-owned camera and preview resources.
|
|
259
|
+
|
|
260
|
+
The frame width and height reporters prefer the dimensions currently delivered by the video
|
|
261
|
+
element and fall back to the active track settings before its first frame. The frame-rate reporter
|
|
262
|
+
uses the active track setting. All three reporters return `0` when the camera is not running or the
|
|
263
|
+
browser does not provide the value.
|
|
264
|
+
|
|
177
265
|
`frame.element` is the original, unmodified `HTMLVideoElement`, even when the preview is mirrored.
|
|
178
266
|
WebGPU and WebCodecs consumers can therefore use that same source without going through the preview
|
|
179
267
|
or a CPU canvas, for example with `GPUQueue.copyExternalImageToTexture()` or a short-lived
|
|
@@ -185,6 +273,12 @@ noninteractive; it is intended for display, not Scratch touching or color-sensin
|
|
|
185
273
|
|
|
186
274
|
## Compatibility
|
|
187
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
|
+
|
|
279
|
+
Version 0.5.0 exposes GPU-backed preview visibility, mirroring, and actual frame width, height,
|
|
280
|
+
and rate as TurboWarp blocks. The default behavior remains unchanged.
|
|
281
|
+
|
|
188
282
|
Version 0.4.0 adds the opt-in GPU-backed video preview without changing the extension ID,
|
|
189
283
|
block opcodes, camera lease ownership, device selection, or release semantics. Consumers that do
|
|
190
284
|
not request `preview: true` retain the existing camera-source behavior.
|
package/dist/camera-source.js
CHANGED
|
@@ -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",
|
|
@@ -66,6 +86,62 @@
|
|
|
66
86
|
"defaultValue": "default"
|
|
67
87
|
} }
|
|
68
88
|
},
|
|
89
|
+
{
|
|
90
|
+
"opcode": "showCameraPreview",
|
|
91
|
+
"blockType": "COMMAND",
|
|
92
|
+
"text": "show shared camera [CAMERA_ID] preview mirrored [MIRRORED]",
|
|
93
|
+
"description": "Shows the named shared camera with the GPU-backed stage preview.",
|
|
94
|
+
"arguments": {
|
|
95
|
+
"CAMERA_ID": {
|
|
96
|
+
"type": "STRING",
|
|
97
|
+
"defaultValue": "default"
|
|
98
|
+
},
|
|
99
|
+
"MIRRORED": {
|
|
100
|
+
"type": "STRING",
|
|
101
|
+
"defaultValue": "true"
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"opcode": "hideCameraPreview",
|
|
107
|
+
"blockType": "COMMAND",
|
|
108
|
+
"text": "hide shared camera [CAMERA_ID] preview",
|
|
109
|
+
"description": "Hides the block-owned preview without stopping leases owned by other consumers.",
|
|
110
|
+
"arguments": { "CAMERA_ID": {
|
|
111
|
+
"type": "STRING",
|
|
112
|
+
"defaultValue": "default"
|
|
113
|
+
} }
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
"opcode": "cameraFrameWidth",
|
|
117
|
+
"blockType": "REPORTER",
|
|
118
|
+
"text": "shared camera [CAMERA_ID] frame width",
|
|
119
|
+
"description": "Returns the active video frame width in pixels, or zero while the camera is not running.",
|
|
120
|
+
"arguments": { "CAMERA_ID": {
|
|
121
|
+
"type": "STRING",
|
|
122
|
+
"defaultValue": "default"
|
|
123
|
+
} }
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
"opcode": "cameraFrameHeight",
|
|
127
|
+
"blockType": "REPORTER",
|
|
128
|
+
"text": "shared camera [CAMERA_ID] frame height",
|
|
129
|
+
"description": "Returns the active video frame height in pixels, or zero while the camera is not running.",
|
|
130
|
+
"arguments": { "CAMERA_ID": {
|
|
131
|
+
"type": "STRING",
|
|
132
|
+
"defaultValue": "default"
|
|
133
|
+
} }
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
"opcode": "cameraFrameRate",
|
|
137
|
+
"blockType": "REPORTER",
|
|
138
|
+
"text": "shared camera [CAMERA_ID] frame rate",
|
|
139
|
+
"description": "Returns the active video track frame rate, or zero when it is unavailable.",
|
|
140
|
+
"arguments": { "CAMERA_ID": {
|
|
141
|
+
"type": "STRING",
|
|
142
|
+
"defaultValue": "default"
|
|
143
|
+
} }
|
|
144
|
+
},
|
|
69
145
|
{
|
|
70
146
|
"opcode": "refreshCameraDevices",
|
|
71
147
|
"blockType": "COMMAND",
|
|
@@ -298,12 +374,37 @@
|
|
|
298
374
|
video: options.video ?? true
|
|
299
375
|
};
|
|
300
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
|
+
}
|
|
301
387
|
var CameraSourceExtension = class {
|
|
302
388
|
constructor() {
|
|
303
389
|
this.sessions = /* @__PURE__ */ new Map();
|
|
304
390
|
this.blockLeases = /* @__PURE__ */ new Map();
|
|
391
|
+
this.blockPreviewLeases = /* @__PURE__ */ new Map();
|
|
392
|
+
this.blockPreviewRevisions = /* @__PURE__ */ new Map();
|
|
393
|
+
this.cameraFailures = /* @__PURE__ */ new Map();
|
|
305
394
|
this.devices = [];
|
|
395
|
+
this.dispose = () => {
|
|
396
|
+
this.stopAllCameras();
|
|
397
|
+
Scratch.vm.runtime.off?.("PROJECT_STOP_ALL", this.handleProjectBoundary);
|
|
398
|
+
Scratch.vm.runtime.off?.("PROJECT_LOADED", this.handleProjectBoundary);
|
|
399
|
+
Scratch.vm.runtime.off?.("RUNTIME_DISPOSED", this.dispose);
|
|
400
|
+
};
|
|
401
|
+
this.handleProjectBoundary = () => {
|
|
402
|
+
this.stopAllCameras();
|
|
403
|
+
};
|
|
306
404
|
Scratch.vm.runtime.ext_kubohiroyacamerasource = this;
|
|
405
|
+
Scratch.vm.runtime.on?.("PROJECT_STOP_ALL", this.handleProjectBoundary);
|
|
406
|
+
Scratch.vm.runtime.on?.("PROJECT_LOADED", this.handleProjectBoundary);
|
|
407
|
+
Scratch.vm.runtime.on?.("RUNTIME_DISPOSED", this.dispose);
|
|
307
408
|
}
|
|
308
409
|
getInfo() {
|
|
309
410
|
return {
|
|
@@ -314,11 +415,31 @@
|
|
|
314
415
|
}
|
|
315
416
|
isCameraRunning(args = {}) {
|
|
316
417
|
const session = this.sessions.get(normalizeId(args.CAMERA_ID));
|
|
317
|
-
return
|
|
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 ?? "";
|
|
318
425
|
}
|
|
319
426
|
cameraDeviceIdReporter(args = {}) {
|
|
320
427
|
return this.sessions.get(normalizeId(args.CAMERA_ID))?.activeDeviceId ?? "";
|
|
321
428
|
}
|
|
429
|
+
cameraFrameWidth(args = {}) {
|
|
430
|
+
const session = this.sessions.get(normalizeId(args.CAMERA_ID));
|
|
431
|
+
if (!session?.stream) return 0;
|
|
432
|
+
return session.video?.videoWidth || this.trackSetting(session, "width");
|
|
433
|
+
}
|
|
434
|
+
cameraFrameHeight(args = {}) {
|
|
435
|
+
const session = this.sessions.get(normalizeId(args.CAMERA_ID));
|
|
436
|
+
if (!session?.stream) return 0;
|
|
437
|
+
return session.video?.videoHeight || this.trackSetting(session, "height");
|
|
438
|
+
}
|
|
439
|
+
cameraFrameRate(args = {}) {
|
|
440
|
+
const session = this.sessions.get(normalizeId(args.CAMERA_ID));
|
|
441
|
+
return session?.stream ? this.trackSetting(session, "frameRate") : 0;
|
|
442
|
+
}
|
|
322
443
|
async startSharedCamera(args = {}) {
|
|
323
444
|
const cameraId = normalizeId(args.CAMERA_ID);
|
|
324
445
|
if (this.blockLeases.has(cameraId)) return;
|
|
@@ -348,6 +469,7 @@
|
|
|
348
469
|
session.leases.delete(token);
|
|
349
470
|
session.previewLeases.delete(token);
|
|
350
471
|
if (session.leases.size === 0) this.stopCameraSession(session.cameraId);
|
|
472
|
+
this.cameraFailures.set(cameraId, cameraFailure(error));
|
|
351
473
|
throw error;
|
|
352
474
|
}
|
|
353
475
|
let released = false;
|
|
@@ -366,6 +488,36 @@
|
|
|
366
488
|
}
|
|
367
489
|
});
|
|
368
490
|
}
|
|
491
|
+
async showCameraPreview(args = {}) {
|
|
492
|
+
const cameraId = normalizeId(args.CAMERA_ID);
|
|
493
|
+
const mirrored = Scratch.Cast.toBoolean(args.MIRRORED ?? true);
|
|
494
|
+
if (this.blockPreviewLeases.get(cameraId)?.mirrored === mirrored) return;
|
|
495
|
+
const revision = this.nextPreviewBlockRevision(cameraId);
|
|
496
|
+
const lease = await this.acquireCamera({
|
|
497
|
+
owner: "camera-source-preview-block",
|
|
498
|
+
cameraId,
|
|
499
|
+
preview: true,
|
|
500
|
+
mirrored
|
|
501
|
+
});
|
|
502
|
+
if (this.blockPreviewRevisions.get(cameraId) !== revision) {
|
|
503
|
+
await lease.release();
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
const current = this.blockPreviewLeases.get(cameraId);
|
|
507
|
+
this.blockPreviewLeases.set(cameraId, {
|
|
508
|
+
lease,
|
|
509
|
+
mirrored
|
|
510
|
+
});
|
|
511
|
+
await current?.lease.release();
|
|
512
|
+
}
|
|
513
|
+
async hideCameraPreview(args = {}) {
|
|
514
|
+
const cameraId = normalizeId(args.CAMERA_ID);
|
|
515
|
+
this.nextPreviewBlockRevision(cameraId);
|
|
516
|
+
const existing = this.blockPreviewLeases.get(cameraId);
|
|
517
|
+
if (!existing) return;
|
|
518
|
+
this.blockPreviewLeases.delete(cameraId);
|
|
519
|
+
await existing.lease.release();
|
|
520
|
+
}
|
|
369
521
|
stopSharedCamera(args = {}) {
|
|
370
522
|
this.stopCameraSession(normalizeId(args.CAMERA_ID));
|
|
371
523
|
}
|
|
@@ -384,6 +536,7 @@
|
|
|
384
536
|
stopAllCameras() {
|
|
385
537
|
for (const cameraId of [...this.sessions.keys()]) this.stopCameraSession(cameraId);
|
|
386
538
|
this.blockLeases.clear();
|
|
539
|
+
this.blockPreviewLeases.clear();
|
|
387
540
|
}
|
|
388
541
|
session(cameraId) {
|
|
389
542
|
const existing = this.sessions.get(cameraId);
|
|
@@ -392,6 +545,7 @@
|
|
|
392
545
|
cameraId,
|
|
393
546
|
leases: /* @__PURE__ */ new Set(),
|
|
394
547
|
previewLeases: /* @__PURE__ */ new Map(),
|
|
548
|
+
active: true,
|
|
395
549
|
stream: null,
|
|
396
550
|
video: null,
|
|
397
551
|
preview: null,
|
|
@@ -406,26 +560,57 @@
|
|
|
406
560
|
session.mirrored = options.mirrored === true;
|
|
407
561
|
session.startPromise = (async () => {
|
|
408
562
|
const stream = await mediaDevices().getUserMedia(videoConstraints(options));
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
563
|
+
let video = null;
|
|
564
|
+
try {
|
|
565
|
+
if (!session.active) throw new Error("Camera acquisition was cancelled.");
|
|
566
|
+
video = document.createElement("video");
|
|
567
|
+
video.muted = true;
|
|
568
|
+
video.playsInline = true;
|
|
569
|
+
video.srcObject = stream;
|
|
570
|
+
await video.play();
|
|
571
|
+
if (!session.active) throw new Error("Camera acquisition was cancelled.");
|
|
572
|
+
session.stream = stream;
|
|
573
|
+
session.video = video;
|
|
574
|
+
this.watchStreamEnd(session, stream);
|
|
575
|
+
this.updateActiveDevice(session);
|
|
576
|
+
this.cameraFailures.delete(session.cameraId);
|
|
577
|
+
} catch (error) {
|
|
578
|
+
stream.getTracks().forEach((track) => track.stop());
|
|
579
|
+
if (video) video.srcObject = null;
|
|
580
|
+
throw error;
|
|
581
|
+
}
|
|
417
582
|
})();
|
|
418
583
|
try {
|
|
419
584
|
await session.startPromise;
|
|
420
585
|
} catch (error) {
|
|
421
|
-
this.stopCameraSession(session.cameraId);
|
|
586
|
+
if (this.sessions.get(session.cameraId) === session) this.stopCameraSession(session.cameraId);
|
|
587
|
+
this.cameraFailures.set(session.cameraId, cameraFailure(error));
|
|
422
588
|
throw error;
|
|
423
589
|
}
|
|
424
590
|
}
|
|
591
|
+
nextPreviewBlockRevision(cameraId) {
|
|
592
|
+
const revision = (this.blockPreviewRevisions.get(cameraId) ?? 0) + 1;
|
|
593
|
+
this.blockPreviewRevisions.set(cameraId, revision);
|
|
594
|
+
return revision;
|
|
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
|
+
}
|
|
425
606
|
updateActiveDevice(session) {
|
|
426
607
|
const settings = (session.stream?.getVideoTracks()[0] ?? null)?.getSettings();
|
|
427
608
|
session.activeDeviceId = typeof settings?.deviceId === "string" ? settings.deviceId : "";
|
|
428
609
|
}
|
|
610
|
+
trackSetting(session, name) {
|
|
611
|
+
const value = session.stream?.getVideoTracks()[0]?.getSettings()[name];
|
|
612
|
+
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
613
|
+
}
|
|
429
614
|
ensurePreview(session) {
|
|
430
615
|
if (session.preview || !session.video) return;
|
|
431
616
|
const runtime = Scratch.vm.runtime;
|
|
@@ -448,6 +633,8 @@
|
|
|
448
633
|
stopCameraSession(cameraId) {
|
|
449
634
|
const session = this.sessions.get(cameraId);
|
|
450
635
|
if (!session) return;
|
|
636
|
+
session.active = false;
|
|
637
|
+
this.nextPreviewBlockRevision(cameraId);
|
|
451
638
|
session.preview?.dispose();
|
|
452
639
|
session.stream?.getTracks().forEach((track) => track.stop());
|
|
453
640
|
if (session.video) session.video.srcObject = null;
|
|
@@ -460,6 +647,7 @@
|
|
|
460
647
|
session.previewLeases.clear();
|
|
461
648
|
this.sessions.delete(cameraId);
|
|
462
649
|
this.blockLeases.delete(cameraId);
|
|
650
|
+
this.blockPreviewLeases.delete(cameraId);
|
|
463
651
|
}
|
|
464
652
|
toScratchBlock(block) {
|
|
465
653
|
return {
|
|
@@ -37,6 +37,66 @@
|
|
|
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
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"opcode": "cameraFrameHeight",
|
|
62
|
+
"blockType": "REPORTER",
|
|
63
|
+
"arguments": [
|
|
64
|
+
{
|
|
65
|
+
"id": "CAMERA_ID",
|
|
66
|
+
"type": "STRING"
|
|
67
|
+
}
|
|
68
|
+
]
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"opcode": "cameraFrameRate",
|
|
72
|
+
"blockType": "REPORTER",
|
|
73
|
+
"arguments": [
|
|
74
|
+
{
|
|
75
|
+
"id": "CAMERA_ID",
|
|
76
|
+
"type": "STRING"
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"opcode": "cameraFrameWidth",
|
|
82
|
+
"blockType": "REPORTER",
|
|
83
|
+
"arguments": [
|
|
84
|
+
{
|
|
85
|
+
"id": "CAMERA_ID",
|
|
86
|
+
"type": "STRING"
|
|
87
|
+
}
|
|
88
|
+
]
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
"opcode": "hideCameraPreview",
|
|
92
|
+
"blockType": "COMMAND",
|
|
93
|
+
"arguments": [
|
|
94
|
+
{
|
|
95
|
+
"id": "CAMERA_ID",
|
|
96
|
+
"type": "STRING"
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
},
|
|
40
100
|
{
|
|
41
101
|
"opcode": "isCameraRunning",
|
|
42
102
|
"blockType": "BOOLEAN",
|
|
@@ -52,6 +112,20 @@
|
|
|
52
112
|
"blockType": "COMMAND",
|
|
53
113
|
"arguments": []
|
|
54
114
|
},
|
|
115
|
+
{
|
|
116
|
+
"opcode": "showCameraPreview",
|
|
117
|
+
"blockType": "COMMAND",
|
|
118
|
+
"arguments": [
|
|
119
|
+
{
|
|
120
|
+
"id": "CAMERA_ID",
|
|
121
|
+
"type": "STRING"
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
"id": "MIRRORED",
|
|
125
|
+
"type": "STRING"
|
|
126
|
+
}
|
|
127
|
+
]
|
|
128
|
+
},
|
|
55
129
|
{
|
|
56
130
|
"opcode": "startSharedCamera",
|
|
57
131
|
"blockType": "COMMAND",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-camera-source",
|
|
3
|
-
"version": "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",
|
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
"typecheck": "tsc --noEmit",
|
|
56
56
|
"build": "vite build",
|
|
57
57
|
"lint": "eslint src tests vite.config.ts",
|
|
58
|
+
"format": "eslint src tests vite.config.ts --fix",
|
|
58
59
|
"test": "vitest run",
|
|
59
60
|
"docs": "node scripts/generate-readme.ts",
|
|
60
61
|
"docs:check": "pnpm run docs && git diff --exit-code -- README.md",
|