Move navigator.usb.requestDevice to package
This commit is contained in:
68
README.md
68
README.md
@@ -1,10 +1,13 @@
|
|||||||
# Web-gPhoto2
|
# Web-gPhoto2
|
||||||
|
|
||||||
A gPhoto2 implementation using WebAssembly to control DSLR cameras from the browser.
|
A gPhoto2 implementation using WebAssembly to control DSLR cameras from the browser.
|
||||||
|
|
||||||
Powered by a [custom fork](https://github.com/RReverser/libgphoto2) of [libgphoto2](https://github.com/gphoto/libgphoto2), the [WebUSB](https://github.com/WICG/webusb) backend of [libusb](https://github.com/libusb/libusb), and WebAssembly via [Emscripten](https://emscripten.org/).
|
Powered by a [custom fork](https://github.com/RReverser/libgphoto2) of [libgphoto2](https://github.com/gphoto/libgphoto2), the [WebUSB](https://github.com/WICG/webusb) backend of [libusb](https://github.com/libusb/libusb), and WebAssembly via [Emscripten](https://emscripten.org/).
|
||||||
|
|
||||||
# NPM
|
# NPM
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install web-gphoto2
|
npm install web-gphoto2
|
||||||
// or
|
// or
|
||||||
@@ -14,47 +17,48 @@ yarn add web-gphoto2
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
A short example on how to use this package:
|
A short example on how to use this package:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import Camera from 'web-gphoto2';
|
import Camera from "web-gphoto2";
|
||||||
|
|
||||||
let camera = new Camera();
|
let camera = new Camera();
|
||||||
|
|
||||||
async function connectCamera() {
|
async function connectCamera() {
|
||||||
// @ts-ignore
|
await camera.showCameraPicker();
|
||||||
await navigator.usb.requestDevice({
|
await camera.connect();
|
||||||
filters: [{ classCode: 6, subclassCode: 1 }]
|
|
||||||
});
|
|
||||||
await camera.connect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getSupportedOps() {
|
async function getSupportedOps() {
|
||||||
const ops = await camera.getSupportedOps();
|
const ops = await camera.getSupportedOps();
|
||||||
console.log('Supported Ops:', ops);
|
console.log("Supported Ops:", ops);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getCameraConfig() {
|
async function getCameraConfig() {
|
||||||
const config = await camera.getConfig();
|
const config = await camera.getConfig();
|
||||||
console.log('Config:', config);
|
console.log("Config:", config);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateConfig() {
|
async function updateConfig() {
|
||||||
await camera.setConfigValue('iso', '800');
|
await camera.setConfigValue("iso", "800");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function capturePreviewAsBlob() {
|
async function capturePreviewAsBlob() {
|
||||||
const blob = await camera.capturePreviewAsBlob();
|
const blob = await camera.capturePreviewAsBlob();
|
||||||
console.log('Blob:', blob);
|
console.log("Blob:", blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function captureImageAsFile() {
|
async function captureImageAsFile() {
|
||||||
const file = await camera.captureImageAsFile();
|
const file = await camera.captureImageAsFile();
|
||||||
console.log('File:', file);
|
console.log("File:", file);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Common Issues
|
## Common Issues
|
||||||
### SharedArrayBuffer can not be found
|
|
||||||
|
### SharedArrayBuffer can not be found
|
||||||
|
|
||||||
SharedArrayBuffer has been disabled across all browsers due to the Spectre vulnerability. This package uses SharedArrayBuffer to communicate with the WebAssembly module. To work around this issue, you need to set two response headers for your document:
|
SharedArrayBuffer has been disabled across all browsers due to the Spectre vulnerability. This package uses SharedArrayBuffer to communicate with the WebAssembly module. To work around this issue, you need to set two response headers for your document:
|
||||||
|
|
||||||
```
|
```
|
||||||
Cross-Origin-Opener-Policy: same-origin
|
Cross-Origin-Opener-Policy: same-origin
|
||||||
Cross-Origin-Embedder-Policy: require-corp
|
Cross-Origin-Embedder-Policy: require-corp
|
||||||
@@ -63,35 +67,37 @@ Cross-Origin-Embedder-Policy: require-corp
|
|||||||
Information from [Stackoverflow](https://stackoverflow.com/questions/64650119/react-error-sharedarraybuffer-is-not-defined-in-firefox)
|
Information from [Stackoverflow](https://stackoverflow.com/questions/64650119/react-error-sharedarraybuffer-is-not-defined-in-firefox)
|
||||||
|
|
||||||
### Error: Not found: /node_modules/.vite/deps/libapi.wasm
|
### Error: Not found: /node_modules/.vite/deps/libapi.wasm
|
||||||
|
|
||||||
Vite tries to optimize the dependencies by default. This causes the WebAssembly module to be moved to a different location. To prevent this, you need to exclude the web-gphoto2 package from the optimization.
|
Vite tries to optimize the dependencies by default. This causes the WebAssembly module to be moved to a different location. To prevent this, you need to exclude the web-gphoto2 package from the optimization.
|
||||||
|
|
||||||
In vite, both of the above mentioned issues are solved by adding the following to your vite.config.js:
|
In vite, both of the above mentioned issues are solved by adding the following to your vite.config.js:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import { sveltekit } from '@sveltejs/kit/vite';
|
import { sveltekit } from "@sveltejs/kit/vite";
|
||||||
import { defineConfig } from 'vite';
|
import { defineConfig } from "vite";
|
||||||
|
|
||||||
/** @type {import('vite').Plugin} */
|
/** @type {import('vite').Plugin} */
|
||||||
const viteServerConfig = {
|
const viteServerConfig = {
|
||||||
name: 'add headers',
|
name: "add headers",
|
||||||
configureServer: (server) => {
|
configureServer: (server) => {
|
||||||
server.middlewares.use((req, res, next) => {
|
server.middlewares.use((req, res, next) => {
|
||||||
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
|
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
|
||||||
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
|
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [sveltekit(), viteServerConfig],
|
plugins: [sveltekit(), viteServerConfig],
|
||||||
optimizeDeps: {
|
optimizeDeps: {
|
||||||
exclude: ['web-gphoto2']
|
exclude: ["web-gphoto2"],
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
# Demo
|
# Demo
|
||||||
|
|
||||||
This repository also contains a [demo app](https://web.dev/porting-libusb-to-webusb/) running gPhoto2 on the Web:
|
This repository also contains a [demo app](https://web.dev/porting-libusb-to-webusb/) running gPhoto2 on the Web:
|
||||||

|

|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,9 @@ export function rethrowIfCritical(err) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const INTERFACE_CLASS = 6; // PTP
|
||||||
|
const INTERFACE_SUBCLASS = 1; // MTP
|
||||||
|
|
||||||
class Camera {
|
class Camera {
|
||||||
constructor() {
|
constructor() {
|
||||||
/** @type {Promise<unknown>} */
|
/** @type {Promise<unknown>} */
|
||||||
@@ -49,6 +52,18 @@ class Camera {
|
|||||||
this.ModulePromise = null;
|
this.ModulePromise = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async showCameraPicker() {
|
||||||
|
// @ts-ignore
|
||||||
|
await navigator.usb.requestDevice({
|
||||||
|
filters: [
|
||||||
|
{
|
||||||
|
classCode: INTERFACE_CLASS,
|
||||||
|
subclassCode: INTERFACE_SUBCLASS,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async connect() {
|
async connect() {
|
||||||
if (!this.ModulePromise) {
|
if (!this.ModulePromise) {
|
||||||
this.ModulePromise = initializeModule().then((initModule) => {
|
this.ModulePromise = initializeModule().then((initModule) => {
|
||||||
|
|||||||
1
src/libapi.mjs.d.ts
vendored
1
src/libapi.mjs.d.ts
vendored
@@ -61,6 +61,7 @@ declare class Context {
|
|||||||
export declare class Camera {
|
export declare class Camera {
|
||||||
constructor();
|
constructor();
|
||||||
|
|
||||||
|
showCameraPicker(): Promise<void>;
|
||||||
connect(): Promise<void>;
|
connect(): Promise<void>;
|
||||||
disconnect(): Promise<void>;
|
disconnect(): Promise<void>;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user