Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create NPM package #7

Merged
merged 23 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/.vscode/c_cpp_properties.json
/ui/libapi.*
!/ui/libapi.mjs.d.ts
/node_modules
/deps/*
!/deps/libgphoto2
Expand Down
121 changes: 118 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,124 @@
This is a [demo app](https://web.dev/porting-libusb-to-webusb/) running gPhoto2 on the Web:

![A picture of DSLR camera connected via a USB cable to a laptop. The laptop is running the Web demo mentioned in the article, which mirrors a live video feed from the camera as well as allows to tweak its settings via form controls.](https://web-dev.imgix.net/image/9oK23mr86lhFOwKaoYZ4EySNFp02/MR4YGRvl0Z9AWT6vv3sQ.jpg?auto=format&w=1600)
# Web-gPhoto2
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/).

# NPM
## Installation
```bash
npm install web-gphoto2
// or
yarn add web-gphoto2
```

## Usage
**Note: This package ONLY runs in the browser context**. It will not work in NodeJS. If you are using a build-tool make sure to dynamically import this package so it is only imported in the browser context.

```ts
let initModule: any;

if (typeof window !== 'undefined') {
icheered marked this conversation as resolved.
Show resolved Hide resolved
import('web-gphoto2').then((module) => {
initModule = module.default;
}).catch((error) => {
console.error("Error loading web-gphoto2", error);
});
} else {
console.warn("web-gphoto2 is only available in the browser");
}
```

A short example on how to use this package to obtain the camera config:
RReverser marked this conversation as resolved.
Show resolved Hide resolved
```ts
// After importing the initModule from web-gphoto2
const ModulePromise = initModule();

export function rethrowIfCritical(err) {
if (err.constructor !== Error) throw err;
}

export async function connect() {
const Module = await ModulePromise;

let context = await new Module.Context();
let supportedOps = await context.supportedOps();

let queue = Promise.resolve();

function schedule(op) {
let res = queue.then(() => op(context));
queue = res.catch(rethrowIfCritical);
return res;
}

return {
supportedOps,
schedule,
disconnect() {
context.delete();
}
};
}

// Select the camera, connect, and obtain the config. Error handling is omitted for brevity but should be included, check the example for details.
await navigator.usb.requestDevice({
filters: [
{
classCode: 6, // PTP
subclassCode: 1 // MTP
}
]
});
let connection = await connect();
let config = await connection.schedule((context: any) => context.configToJS());
console.log('Obtained config: ', config);
```

More information can be found in the example included in the Github repository.

## Common Issues
### 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:
```
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
```

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
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.
icheered marked this conversation as resolved.
Show resolved Hide resolved

In vite, both of the above mentioned issues are solved by adding the following to your vite.config.js:
```ts
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

/** @type {import('vite').Plugin} */
const viteServerConfig = {
name: 'add headers',
configureServer: (server) => {
server.middlewares.use((req, res, next) => {
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
next();
});
}
};

export default defineConfig({
plugins: [sveltekit(), viteServerConfig],
optimizeDeps: {
exclude: ['web-gphoto2']
}
});
```


# Demo
This repository also contains a [demo app](https://web.dev/porting-libusb-to-webusb/) running gPhoto2 on the Web:
![A picture of DSLR camera connected via a USB cable to a laptop. The laptop is running the Web demo mentioned in the article, which mirrors a live video feed from the camera as well as allows to tweak its settings via form controls.](https://web-dev.imgix.net/image/9oK23mr86lhFOwKaoYZ4EySNFp02/MR4YGRvl0Z9AWT6vv3sQ.jpg?auto=format&w=1600)

For the detailed technical write-up, see [the official blog post](https://web.dev/porting-libusb-to-webusb/). To see the demo in action, visit the hosted version [here](https://web-gphoto2.rreverser.com/) (but make sure to read the [cross-platform compatibility notes](https://web.dev/porting-libusb-to-webusb/#important-cross-platform-compatibility-notes) first).

If you don't have a DSLR, you can check out a recording of the demo below:
Expand Down
29 changes: 28 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
{
"name": "web-gphoto2",
"version": "0.1.0",
"description": "WebAssembly implementation of gphoto2 and libusb to control DSLR cameras over USB on the Web",
"main": "ui/camera.js",
"types": "ui/libapi.mjs.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/GoogleChromeLabs/web-gphoto2"
},
"keywords": [
"gphoto2",
"libusb",
"webassembly"
],
"author": "Ingvar Stepanyan - RReverser - [email protected]",
icheered marked this conversation as resolved.
Show resolved Hide resolved
"license": "LGPLv2.1",
"bugs": {
"url": "https://github.com/GoogleChromeLabs/web-gphoto2/issues"
},
"homepage": "https://github.com/GoogleChromeLabs/web-gphoto2#readme",
"files": [
"ui/libapi.*",
"ui/camera.js"
],
"devDependencies": {
"@types/emscripten": "^1.39.5",
"@types/requestidlecallback": "^0.3.4",
"@types/stats.js": "^0.17.0",
"preact": "^10.5.14"
}
}
}
114 changes: 114 additions & 0 deletions ui/camera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2021 Google LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/


/** @typedef {import('./libapi.mjs').Context} Context */

// To avoid errors for users who use SSR or Hybrid rendering (e.g. Nuxt.js), we need to check if we're in the browser.
let initModule;

async function initializeModule() {
if (typeof window !== 'undefined') {
const module = await import('./libapi.mjs');
initModule = module.default;
return initModule;
} else {
console.warn("web-gphoto2 is only available in the browser");
return null;
}
}

const ModulePromise = initializeModule().then(initModule => {
if (initModule) {
return initModule();
} else {
return null;
}
});

class Camera {
constructor() {
/** @type {Promise<unknown>} */
this.queue = Promise.resolve();
this.Module = null;
this.context = null;
}

rethrowIfCritical(err) {
icheered marked this conversation as resolved.
Show resolved Hide resolved
// If it's precisely Error, it's a custom error; anything else - SyntaxError,
// WebAssembly.RuntimeError, TypeError, etc. - is treated as critical here.
if (err.constructor !== Error) {
throw err;
}
}

async connect() {
this.Module = await ModulePromise;
icheered marked this conversation as resolved.
Show resolved Hide resolved
this.context = await new this.Module.Context();
}

/** Schedules an exclusive async operation on the global context.
* @template T
* @param {(ctx: Context) => Promise<T>} op
* @returns {Promise<T>}
*/
async schedule(op) {
let res = this.queue.then(() => op(this.context));
this.queue = res.catch(this.rethrowIfCritical);
return res;
}

async disconnect() {
if (!this.context.isDeleted()) {
this.context.delete();
}
}

async getConfig() {
return this.schedule((context) => context.configToJS());
}

async getSupportedOps() {
if (this.context) {
return await this.context.supportedOps();
}
throw new Error("You need to connect to the camera first");
}

async setConfigValue(name, value) {
const uiTimeout = new Promise((resolve) => setTimeout(resolve, 800));
const setResult = this.schedule((context) => context.setConfigValue(name, value));
// wait for both the config set operation and the timeout to complete
return Promise.all([setResult, uiTimeout]);
}


async capturePreviewAsBlob() {
return this.schedule((context) => context.capturePreviewAsBlob());
}

async captureImageAsFile() {
return this.schedule((context) => context.captureImageAsFile());
}

async consumeEvents() {
return this.schedule((context) => context.consumeEvents());
}
}

export default Camera;
16 changes: 16 additions & 0 deletions ui/libapi.mjs

Large diffs are not rendered by default.

39 changes: 31 additions & 8 deletions ui/libapi.mjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export type Config = {
label: string;
readonly: boolean;
} & (
| { type: 'range'; value: number; min: number; max: number; step: number }
| { type: 'menu' | 'radio'; value: string; choices: string[] }
| { type: 'toggle'; value: boolean }
| { type: 'text'; value: string }
| { type: 'window'; children: Record<string, Config> }
| { type: 'section'; children: Record<string, Config> }
| { type: 'datetime'; value: number }
);
| { type: 'range'; value: number; min: number; max: number; step: number }
| { type: 'menu' | 'radio'; value: string; choices: string[] }
| { type: 'toggle'; value: boolean }
| { type: 'text'; value: string }
| { type: 'window'; children: Record<string, Config> }
| { type: 'section'; children: Record<string, Config> }
| { type: 'datetime'; value: number }
);

declare interface SupportedOps {
captureImage: boolean;
Expand All @@ -41,6 +41,9 @@ declare interface SupportedOps {
}

declare class Context {
context(): Promise<any> {
throw new Error('Method not implemented.');
}
configToJS(): Promise<Config & { type: 'window' }>;
setConfigValue(
name: string,
Expand All @@ -55,6 +58,26 @@ declare class Context {
isDeleted(): boolean;
}

export declare class Camera {
constructor();

connect(): Promise<void>;
disconnect(): Promise<void>;

getConfig(): Promise<any>; // replace "any" with the actual type of the config if you have it

getSupportedOps(): SupportedOps;

setConfigValue(name: string, value: number | string | boolean): Promise<void>;

capturePreviewAsBlob(): Promise<Blob>;
captureImageAsFile(): Promise<File>;
consumeEvents(): Promise<boolean>;

private rethrowIfCritical(err: any): void; // any error type can be replaced by more specific if available
private schedule<T>(op: (ctx: any) => Promise<T>): Promise<T>; // ctx and T types can be replaced by more specific if available
}

export interface Module extends EmscriptenModule {
Context: typeof Context;
}
Expand Down
Binary file added ui/libapi.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions ui/libapi.worker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.