@kubohiroya/turbowarp-camera-source 0.19.3 → 0.20.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/dist/runtime.d.ts CHANGED
@@ -116,8 +116,26 @@ export interface CameraFrameTime {
116
116
  }
117
117
  export interface CameraLease {
118
118
  getFrameSource(): CameraFrameSource;
119
+ /**
120
+ * The frame the camera is presenting, encoded as a still image.
121
+ *
122
+ * For a consumer that wants a file rather than pixels: an asset store, a download, a server
123
+ * publishing the latest frame. One that is going to process the image itself should read
124
+ * `getFrameSource().element` instead and skip the encode.
125
+ *
126
+ * It draws from the stream this lease already holds, so it never opens a second camera, and it
127
+ * changes nothing about the session: the lease is released the same way whether or not anything
128
+ * was captured.
129
+ *
130
+ * Optional for the same reason as `adoptCamera`: this surface is narrowed by shape rather than by
131
+ * a version, so a consumer compiled against these declarations can still meet a build that
132
+ * predates the method. Feature detect it with `typeof lease.captureFrame === 'function'`.
133
+ */
134
+ captureFrame?(options?: CameraCaptureOptions): Promise<CameraSnapshot>;
119
135
  release(): Promise<void>;
120
136
  }
137
+ export type { CameraCaptureOptions, CameraSnapshot, CameraSnapshotType } from './snapshot.js';
138
+ import type { CameraCaptureOptions, CameraSnapshot } from './snapshot.js';
121
139
  /**
122
140
  * What `Scratch.vm.runtime[cameraSourceRuntimeKey]` offers.
123
141
  *
@@ -1 +1 @@
1
- {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,0FAA0F;AAC1F,eAAO,MAAM,uBAAuB,2BAA2B,CAAC;AAEhE;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,+BAA+B,CAAC;AAEnE;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,qCAAqC,CAAC;AAE5E,wFAAwF;AACxF,eAAO,MAAM,6BAA6B,EAAG,CAAU,CAAC;AAExD;;;;;;GAMG;AACH,MAAM,MAA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,CAAC;IACtE,MAAM,SAAS,GAAI,OAAmC,CAAC,yBAAyB,CAAC,CAAC;IAClF,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IAC1E,MAAM,EAAC,cAAc,EAAC,GAAG,SAAuC,CAAC;IACjE,OAAO,OAAO,cAAc,KAAK,UAAU,CAAC,CAAC,CAAE,SAAsC,CAAC,CAAC,CAAC,SAAS,CAAC;AACpG,CAAC"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Taking one still image out of a running camera.
3
+ *
4
+ * A consumer can already read `CameraFrameSource.element` and draw it itself, and the ones doing
5
+ * computer vision should: they want the pixels, not a file. This is for the other kind of consumer,
6
+ * which wants an encoded image to hand to something that speaks files -- an asset store, a download,
7
+ * an HTTP server publishing the latest frame. Each of those would otherwise write the same canvas,
8
+ * the same encode, the same resize, and get the mirroring and the upscaling wrong in its own way.
9
+ *
10
+ * Nothing here knows about HTTP, assets or storage. It returns bytes and the metadata needed to
11
+ * describe them, and what happens to the bytes is the caller's business.
12
+ */
13
+ import { type Flip } from './flip.js';
14
+ export type CameraSnapshotType = 'image/jpeg' | 'image/png' | 'image/webp';
15
+ export interface CameraCaptureOptions {
16
+ /** Defaults to `image/jpeg`. */
17
+ type?: CameraSnapshotType;
18
+ /** 0 to 1, for `image/jpeg` and `image/webp`. Out-of-range values are clamped. */
19
+ quality?: number;
20
+ /** Upper bounds on the encoded image. Aspect ratio is kept, and the frame is never enlarged. */
21
+ maxWidth?: number;
22
+ maxHeight?: number;
23
+ /**
24
+ * How to turn the image over, defaulting to `none`: the camera's own capture.
25
+ *
26
+ * Not taken from the session's `previewFlip`, and this is the one decision here worth arguing
27
+ * about. The preview's flip is a drawing choice, and the frames behind it are always the ones the
28
+ * camera captured -- a profile's intrinsics describe that image, so a snapshot that silently
29
+ * mirrored itself would disagree with every calibration solved against the same camera, and
30
+ * disagree invisibly: a mirrored photograph of a room looks like a photograph of a room.
31
+ *
32
+ * A caller that wants what the operator was looking at -- a photo booth, a snapshot shown back to
33
+ * the person in frame -- asks for it, and can read `previewFlip` off the frame source to find out
34
+ * what that was.
35
+ */
36
+ flip?: Flip;
37
+ }
38
+ export interface CameraSnapshot {
39
+ readonly type: CameraSnapshotType;
40
+ readonly bytes: Uint8Array;
41
+ readonly width: number;
42
+ readonly height: number;
43
+ /** `Date.now()` when the frame was drawn, not when the encode finished. */
44
+ readonly capturedAt: number;
45
+ }
46
+ /** What this module needs of a video element, so a caller can pass anything that presents frames. */
47
+ export interface SnapshotSource {
48
+ readonly videoWidth: number;
49
+ readonly videoHeight: number;
50
+ readonly readyState: number;
51
+ }
52
+ /**
53
+ * Draws the frame the element is presenting and returns it encoded.
54
+ *
55
+ * No object URL is created and no canvas is attached to the document, so there is nothing to revoke
56
+ * and nothing to leak: the canvas and the blob are unreachable once the bytes have been copied out.
57
+ */
58
+ export declare function captureSnapshot(video: SnapshotSource, options?: CameraCaptureOptions): Promise<CameraSnapshot>;
59
+ //# sourceMappingURL=snapshot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAqC,KAAK,IAAI,EAAC,MAAM,WAAW,CAAC;AAExE,MAAM,MAAM,kBAAkB,GAAG,YAAY,GAAG,WAAW,GAAG,YAAY,CAAC;AAI3E,MAAM,WAAW,oBAAoB;IACnC,gCAAgC;IAChC,IAAI,CAAC,EAAE,kBAAkB,CAAC;IAC1B,kFAAkF;IAClF,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,gGAAgG;IAChG,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;;;;;;;;;OAYG;IACH,IAAI,CAAC,EAAE,IAAI,CAAC;CACb;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,IAAI,EAAE,kBAAkB,CAAC;IAClC,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,2EAA2E;IAC3E,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAED,qGAAqG;AACrG,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AA8GD;;;;;GAKG;AACH,wBAAsB,eAAe,CACnC,KAAK,EAAE,cAAc,EACrB,OAAO,GAAE,oBAAyB,GACjC,OAAO,CAAC,cAAc,CAAC,CA6CzB"}
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Taking one still image out of a running camera.
3
+ *
4
+ * A consumer can already read `CameraFrameSource.element` and draw it itself, and the ones doing
5
+ * computer vision should: they want the pixels, not a file. This is for the other kind of consumer,
6
+ * which wants an encoded image to hand to something that speaks files -- an asset store, a download,
7
+ * an HTTP server publishing the latest frame. Each of those would otherwise write the same canvas,
8
+ * the same encode, the same resize, and get the mirroring and the upscaling wrong in its own way.
9
+ *
10
+ * Nothing here knows about HTTP, assets or storage. It returns bytes and the metadata needed to
11
+ * describe them, and what happens to the bytes is the caller's business.
12
+ */
13
+ import { flipsHorizontally, flipsVertically } from './flip.js';
14
+ const snapshotTypes = ['image/jpeg', 'image/png', 'image/webp'];
15
+ const haveCurrentData = 2;
16
+ function requestedType(value) {
17
+ if (value === undefined)
18
+ return 'image/jpeg';
19
+ if (!snapshotTypes.includes(value)) {
20
+ throw new TypeError(`Camera snapshot type must be one of ${snapshotTypes.join(', ')}.`);
21
+ }
22
+ return value;
23
+ }
24
+ /**
25
+ * Clamps quality, but refuses a value that was never a quality.
26
+ *
27
+ * Out of range is a caller saying "as good as possible" in the wrong units, and clamping says the
28
+ * same thing. `NaN` or a string is a bug, and silently encoding at the browser's default would hide
29
+ * it behind an image that looks fine.
30
+ */
31
+ function requestedQuality(value) {
32
+ if (value === undefined)
33
+ return undefined;
34
+ if (typeof value !== 'number' || !Number.isFinite(value)) {
35
+ throw new TypeError('Camera snapshot quality must be a finite number between 0 and 1.');
36
+ }
37
+ return Math.min(1, Math.max(0, value));
38
+ }
39
+ function requestedBound(value, name) {
40
+ if (value === undefined)
41
+ return undefined;
42
+ if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
43
+ throw new TypeError(`Camera snapshot ${name} must be a positive finite number.`);
44
+ }
45
+ return value;
46
+ }
47
+ /**
48
+ * The encoded size: inside both bounds, same shape, never larger than the frame.
49
+ *
50
+ * Enlarging is left out rather than offered, because a caller asking for 4000 px of a 640 px frame
51
+ * is asking for detail that does not exist, and a file that claims to have it is worse than one that
52
+ * says 640. A future option can add it where somebody genuinely needs the pixels padded out.
53
+ */
54
+ function encodedSize(width, height, maxWidth, maxHeight) {
55
+ const scale = Math.min(1, maxWidth === undefined ? 1 : maxWidth / width, maxHeight === undefined ? 1 : maxHeight / height);
56
+ return {
57
+ width: Math.max(1, Math.round(width * scale)),
58
+ height: Math.max(1, Math.round(height * scale))
59
+ };
60
+ }
61
+ function createCanvas(width, height) {
62
+ const offscreen = globalThis
63
+ .OffscreenCanvas;
64
+ if (typeof offscreen === 'function')
65
+ return new offscreen(width, height);
66
+ const createElement = globalThis.document
67
+ ?.createElement;
68
+ if (typeof createElement !== 'function') {
69
+ throw new Error('Camera snapshots need either OffscreenCanvas or a document to make a canvas in.');
70
+ }
71
+ const canvas = createElement.call(globalThis.document, 'canvas');
72
+ canvas.width = width;
73
+ canvas.height = height;
74
+ return canvas;
75
+ }
76
+ async function encode(canvas, type, quality) {
77
+ if (typeof canvas.convertToBlob === 'function') {
78
+ return canvas.convertToBlob({ type, ...(quality === undefined ? {} : { quality }) });
79
+ }
80
+ if (typeof canvas.toBlob === 'function') {
81
+ return new Promise((resolve, reject) => {
82
+ canvas.toBlob((blob) => (blob ? resolve(blob) : reject(new Error('The camera frame could not be encoded.'))), type, quality);
83
+ });
84
+ }
85
+ throw new Error('This canvas cannot encode an image.');
86
+ }
87
+ /**
88
+ * Draws the frame the element is presenting and returns it encoded.
89
+ *
90
+ * No object URL is created and no canvas is attached to the document, so there is nothing to revoke
91
+ * and nothing to leak: the canvas and the blob are unreachable once the bytes have been copied out.
92
+ */
93
+ export async function captureSnapshot(video, options = {}) {
94
+ const type = requestedType(options.type);
95
+ const quality = requestedQuality(options.quality);
96
+ const maxWidth = requestedBound(options.maxWidth, 'maxWidth');
97
+ const maxHeight = requestedBound(options.maxHeight, 'maxHeight');
98
+ const flip = options.flip ?? 'none';
99
+ if (video.readyState < haveCurrentData) {
100
+ throw new Error('The shared camera has not presented a frame yet.');
101
+ }
102
+ const sourceWidth = video.videoWidth;
103
+ const sourceHeight = video.videoHeight;
104
+ if (!(sourceWidth > 0) || !(sourceHeight > 0)) {
105
+ throw new Error('The shared camera is presenting frames of no size.');
106
+ }
107
+ const size = encodedSize(sourceWidth, sourceHeight, maxWidth, maxHeight);
108
+ const canvas = createCanvas(size.width, size.height);
109
+ canvas.width = size.width;
110
+ canvas.height = size.height;
111
+ const context = canvas.getContext('2d');
112
+ if (!context)
113
+ throw new Error('Camera snapshots need a 2d canvas context.');
114
+ const horizontal = flipsHorizontally(flip);
115
+ const vertical = flipsVertically(flip);
116
+ context.save();
117
+ try {
118
+ if (horizontal || vertical) {
119
+ context.translate(horizontal ? size.width : 0, vertical ? size.height : 0);
120
+ context.scale(horizontal ? -1 : 1, vertical ? -1 : 1);
121
+ }
122
+ context.drawImage(video, 0, 0, size.width, size.height);
123
+ }
124
+ finally {
125
+ context.restore();
126
+ }
127
+ const capturedAt = Date.now();
128
+ const blob = await encode(canvas, type, quality);
129
+ // A browser that cannot encode the type asked for falls back to PNG rather than failing, and a
130
+ // caller told `image/webp` while handed PNG bytes will write them under the wrong name.
131
+ if (blob.type !== type) {
132
+ throw new Error(`This browser encoded ${blob.type || 'an unknown type'} rather than ${type}.`);
133
+ }
134
+ const bytes = new Uint8Array(await blob.arrayBuffer());
135
+ return Object.freeze({ type, bytes, width: size.width, height: size.height, capturedAt });
136
+ }
137
+ //# sourceMappingURL=snapshot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshot.js","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAC,iBAAiB,EAAE,eAAe,EAAY,MAAM,WAAW,CAAC;AAIxE,MAAM,aAAa,GAAkC,CAAC,YAAY,EAAE,WAAW,EAAE,YAAY,CAAC,CAAC;AA0D/F,MAAM,eAAe,GAAG,CAAC,CAAC;AAE1B,SAAS,aAAa,CAAC,KAAc;IACnC,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,YAAY,CAAC;IAC7C,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,KAA2B,CAAC,EAAE,CAAC;QACzD,MAAM,IAAI,SAAS,CAAC,uCAAuC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC1F,CAAC;IACD,OAAO,KAA2B,CAAC;AACrC,CAAC;AAED;;;;;;GAMG;AACH,SAAS,gBAAgB,CAAC,KAAc;IACtC,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC1C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACzD,MAAM,IAAI,SAAS,CAAC,kEAAkE,CAAC,CAAC;IAC1F,CAAC;IACD,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;AACzC,CAAC;AAED,SAAS,cAAc,CAAC,KAAc,EAAE,IAAY;IAClD,IAAI,KAAK,KAAK,SAAS;QAAE,OA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C,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAC3E,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACxD,CAAC;QACD,OAAO,CAAC,SAAS,CAAC,KAAc,EAAE,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IACnE,CAAC;YAAS,CAAC;QACT,OAAO,CAAC,OAAO,EAAE,CAAC;IACpB,CAAC;IACD,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAE9B,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IACjD,+FAA+F;IAC/F,wFAAwF;IACxF,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,CAAC,IAAI,IAAI,iBAAiB,gBAAgB,IAAI,GAAG,CAAC,CAAC;IACjG,CAAC;IACD,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;IACvD,OAAO,MAAM,CAAC,MAAM,CAAC,EAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,UAAU,EAAC,CAAC,CAAC;AAC1F,CAAC"}
@@ -6,4 +6,8 @@ export declare const extensionConfig: {
6
6
  readonly author: "Hiroya Kubo";
7
7
  readonly license: "MPL-2.0";
8
8
  readonly unsandboxed: true;
9
+ readonly blockIconURI: `data:image/svg+xml,${string}`;
10
+ };
11
+ export declare const extensionDescriptions: {
12
+ readonly ja: "1つのカメラ映像を複数のTurboWarp機能拡張で共有します。";
9
13
  };
@@ -5,5 +5,10 @@ export const extensionConfig = {
5
5
  description: 'Share one MediaDevices camera stream between TurboWarp extensions.',
6
6
  author: 'Hiroya Kubo',
7
7
  license: 'MPL-2.0',
8
- unsandboxed: true
8
+ unsandboxed: true,
9
+ blockIconURI: `data:image/svg+xml,${encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><circle cx="17" cy="24" r="9" fill="none" stroke="#4C97FF" stroke-width="3"/><circle cx="17" cy="24" r="3.4" fill="#4C97FF"/><path d="M30 24h10M29 17l9-5M29 31l9 5" fill="none" stroke="#59C059" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/></svg>' +
10
+ 'stroke-width="5" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h24a4 4 0 0 1 ' +
11
+ '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"/>' +
12
+ '</g></svg>')}`,
9
13
  };
14
+ export const extensionDescriptions = { ja: '1つのカメラ映像を複数のTurboWarp機能拡張で共有します。' };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-camera-source",
3
- "version": "0.19.3",
3
+ "version": "0.20.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",
@@ -34,6 +34,7 @@
34
34
  }
35
35
  },
36
36
  "files": [
37
+ "assets/",
37
38
  "dist/",
38
39
  "schemas/",
39
40
  "README.md",
@@ -71,10 +72,10 @@
71
72
  "prepare": "npm run build"
72
73
  },
73
74
  "devDependencies": {
74
- "@kubohiroya/turbowarp-app-shell": "0.3.1",
75
+ "@kubohiroya/turbowarp-app-shell": "^0.4.0",
75
76
  "@eslint/js": "^9.39.5",
76
- "@kubohiroya/turbowarp-extension-manifest": "0.3.2",
77
- "@kubohiroya/vite-plugin-turbowarp-extension": "0.6.1",
77
+ "@kubohiroya/turbowarp-extension-manifest": "^0.3.2",
78
+ "@kubohiroya/vite-plugin-turbowarp-extension": "^0.8.0",
78
79
  "@types/node": "^24.13.3",
79
80
  "ajv": "^8.20.0",
80
81
  "eslint": "^9.39.5",
@@ -83,7 +84,7 @@
83
84
  "vite": "^8.2.2",
84
85
  "vitest": "^4.1.10",
85
86
  "react-dom": "18.3.1",
86
- "@kubohiroya/turbowarp-extension-data-editor": "0.3.0"
87
+ "@kubohiroya/turbowarp-extension-data-editor": "^0.3.4"
87
88
  },
88
89
  "keywords": [
89
90
  "turbowarp",