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 14 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: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/* binary
build/* linguist-generated=true
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export LDFLAGS += -L$(SYSROOT)/lib
export ACLOCAL_PATH := $(SYSROOT)/share/aclocal:$(ACLOCAL_PATH)

# Common linking flags for all targets.
export LDFLAGS += -s DYNAMIC_EXECUTION=0 -s AUTO_JS_LIBRARIES=0 -s AUTO_NATIVE_LIBRARIES=0
export LDFLAGS += -s DYNAMIC_EXECUTION=0 -s AUTO_JS_LIBRARIES=0 -s AUTO_NATIVE_LIBRARIES=0 -s ENVIRONMENT=web,worker

# Common compilation & linking flags for all langs and targets.
COMMON_FLAGS = -Os -flto
Expand All @@ -15,7 +15,7 @@ export LDFLAGS += $(COMMON_FLAGS)

## Main API module

ui/libapi.mjs: api.o $(SYSROOT)/lib/libltdl.la $(SYSROOT)/lib/libgphoto2.la
build/libapi.mjs: api.o $(SYSROOT)/lib/libltdl.la $(SYSROOT)/lib/libgphoto2.la
libtool --verbose --mode=link $(LD) $(LDFLAGS) -o $@ $+ \
-fexceptions --bind -s ASYNCIFY -s ALLOW_MEMORY_GROWTH \
-dlpreopen $(SYSROOT)/lib/libgphoto2/2.5.28.1/ptp2.la \
Expand Down
105 changes: 101 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,106 @@
This is a [demo app](https://web.dev/porting-libusb-to-webusb/) running gPhoto2 on the Web:
# Web-gPhoto2

![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)
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

A short example on how to use this package:

```ts
import Camera from "web-gphoto2";

let camera = new Camera();

async function connectCamera() {
await camera.showCameraPicker();
await camera.connect();
}

async function getSupportedOps() {
const ops = await camera.getSupportedOps();
console.log("Supported Ops:", ops);
}

async function getCameraConfig() {
const config = await camera.getConfig();
console.log("Config:", config);
}

async function updateConfig() {
await camera.setConfigValue("iso", "800");
}

async function capturePreviewAsBlob() {
const blob = await camera.capturePreviewAsBlob();
console.log("Blob:", blob);
icheered marked this conversation as resolved.
Show resolved Hide resolved
}

async function captureImageAsFile() {
const file = await camera.captureImageAsFile();
console.log("File:", file);
}
```

## 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 All @@ -16,10 +113,10 @@ To build, you'll need Docker. Then:

```bash
./build.sh # runs build in Docker
npx serve ui # starts a local server with COOP/COEP
npx serve examples/preact # starts a local server with COOP/COEP
```

Then, navigate to http://localhost:5000/ in Chrome.
Then, navigate to http://localhost:3000/ in Chrome.

## See also

Expand Down
16 changes: 16 additions & 0 deletions build/libapi.mjs

Large diffs are not rendered by default.

Binary file added build/libapi.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions build/libapi.worker.js

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

File renamed without changes.
1 change: 1 addition & 0 deletions examples/preact/build
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions ui/index.js → examples/preact/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import { connect, rethrowIfCritical } from './ops.js';
import { Preview } from './preview.js';
import { Widget } from './widget.js';

/** @typedef {import('../libapi.mjs').Context} Context */
/** @typedef {import('../libapi.mjs').Config} Config */
/** @typedef {import('./ops').Connection} Connection */
/** @typedef {import('./build/libapi.mjs').Context} Context */
/** @typedef {import('./build/libapi.mjs').Config} Config */
/** @typedef {import('./ops.js').Connection} Connection */

export const isDebug = new URLSearchParams(location.search).has('debug');

Expand Down
8 changes: 5 additions & 3 deletions ui/ops.js → examples/preact/ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

import initModule from '../libapi.mjs';
import initModule from './build/libapi.mjs';

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

Expand All @@ -25,7 +25,8 @@ const ModulePromise = initModule();
export function rethrowIfCritical(err) {
// 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) {
if (err.constructor !== Error)
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: what happened to formatting here and below with braces moving onto new lines? Can you fix manually or run them through Prettier please?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also looks like those functions are now duplicated with camera class? Can we reuse them instead? (by updating the demo)

Copy link
Contributor Author

@icheered icheered Aug 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pulled the files through prettier.

We can update the preact demo (and probably should), but since I'm very much not experienced with React and thoroughly dislike working with it I kind of wanted to leave that up to you ;)

I made the changes to the repo in such a way that the demo wouldn't be broken precisely for this reason

throw err;
}
}
Expand All @@ -44,7 +45,8 @@ export async function connect() {
* @param {(ctx: Context) => Promise<T>} op
* @returns {Promise<T>}
*/
function schedule(op) {
function schedule(op)
{
let res = queue.then(() => op(context));
queue = res.catch(rethrowIfCritical);
return res;
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion ui/widget.js → examples/preact/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { h, Component, createRef } from 'preact';

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

/**
* @param {Config} config
Expand Down
41 changes: 40 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
{
"name": "web-gphoto2",
"version": "1.0.0",
"description": "WebAssembly implementation of gphoto2 and libusb to control DSLR cameras over USB on the Web",
"type": "module",
"main": "src/camera.js",
"types": "src/libapi.mjs.d.ts",
icheered marked this conversation as resolved.
Show resolved Hide resolved
"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": {
"name": "Ingvar Stepanyan",
"email": "[email protected]",
"url": "https://rreverser.com"
},
"contributors": [
{
"name": "Tjeerd Bakker",
"email": "[email protected]",
"url": "https://icheered.com"
}
],
"license": "LGPL-2.1-or-later",
RReverser marked this conversation as resolved.
Show resolved Hide resolved
"bugs": {
"url": "https://github.com/GoogleChromeLabs/web-gphoto2/issues"
},
"homepage": "https://github.com/GoogleChromeLabs/web-gphoto2#readme",
"files": [
"src/*",
"build/*"
],
"devDependencies": {
"@types/emscripten": "^1.39.5",
"@types/requestidlecallback": "^0.3.4",
"@types/stats.js": "^0.17.0",
"preact": "^10.5.14"
}
}
}
131 changes: 131 additions & 0 deletions src/camera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright 2021 Google LLC
icheered marked this conversation as resolved.
Show resolved Hide resolved
*
* 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.d.ts').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") {
icheered marked this conversation as resolved.
Show resolved Hide resolved
const module = await import("../build/libapi.mjs");
initModule = module.default;
return initModule;
} else {
console.warn("web-gphoto2 is only available in the browser");
return null;
}
}

export function rethrowIfCritical(err) {
// 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;
}
}

const INTERFACE_CLASS = 6; // PTP
const INTERFACE_SUBCLASS = 1; // MTP

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

async showCameraPicker() {
icheered marked this conversation as resolved.
Show resolved Hide resolved
// @ts-ignore
await navigator.usb.requestDevice({
filters: [
{
classCode: INTERFACE_CLASS,
subclassCode: INTERFACE_SUBCLASS,
},
],
});
}

async connect() {
if (!this.ModulePromise) {
this.ModulePromise = initializeModule().then((initModule) => {
if (initModule) {
return initModule();
} else {
return null;
}
});
}
this.Module = await this.ModulePromise;
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(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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also only just noticed that this got broken when migrating. Previously uiTimeout would only start when the scheduled operation is reached (inside the callback), but now it's assigned outside the callback, which makes it start much earlier so it can timeout too early as well.

);
// 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;
Loading