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 21 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
112 changes: 108 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,65 @@
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() {
// Capture a frame while in live view mode
const blob = await camera.capturePreviewAsBlob();
imageUrl = URL.createObjectURL(blob);
// Set the imageUrl as the src of an image element in your HTML
}

async function captureImageAsFile() {
// Capture an image
const file = await camera.captureImageAsFile();
imageUrl = URL.createObjectURL(file);
// Set the imageUrl as the src of an image element in your HTML
}
```

# 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 +72,58 @@ 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:3000/ in Chrome.

## Common Issues
<details>
<summary>
SharedArrayBuffer can not be found
</summary>
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)
</details>

<details>
<summary>
Error: Not found: /node_modules/.vite/deps/libapi.wasm
</summary>
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:

Then, navigate to http://localhost:5000/ in Chrome.
```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"],
},
});
```
</details>

## 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
2 changes: 1 addition & 1 deletion 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 Down
10 changes: 5 additions & 5 deletions ui/preview.js → examples/preact/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const isDebug = new URLSearchParams(location.search).has('debug');

const Stats = isDebug
? await import('stats.js').then(
res => /** @type {typeof import('stats.js')} */ (res['default'])
)
res => /** @type {typeof import('stats.js')} */(res['default'])
)
: null;

/** @extends Component<{ getPreview?: () => Promise<Blob> }, { error?: string }> */
Expand Down Expand Up @@ -96,9 +96,9 @@ export class Preview extends Component {
blob,
ratio
? {
resizeWidth: canvas.width,
resizeHeight: canvas.height
}
resizeWidth: canvas.width,
resizeHeight: canvas.height
}
: {}
);
if (!ratio) {
Expand Down
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/types.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": {
"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"
}
}
}
105 changes: 105 additions & 0 deletions src/camera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2023 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
*/

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

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

let ModulePromise = null;

class Camera {
constructor() {
this.queue = Promise.resolve();
this.Module = null;
this.context = 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 (!ModulePromise) {
ModulePromise = initModule()
}
this.Module = await ModulePromise;
this.context = await new this.Module.Context();
}

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.

);
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 { Camera };
icheered marked this conversation as resolved.
Show resolved Hide resolved
Loading