Fixes for review
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,6 +4,7 @@
|
|||||||
!/deps/libgphoto2
|
!/deps/libgphoto2
|
||||||
!/deps/libusb
|
!/deps/libusb
|
||||||
/api.o
|
/api.o
|
||||||
|
examples/svelte
|
||||||
|
|
||||||
# Local Netlify folder
|
# Local Netlify folder
|
||||||
.netlify
|
.netlify
|
||||||
|
|||||||
54
README.md
54
README.md
@@ -7,7 +7,6 @@ Powered by a [custom fork](https://github.com/RReverser/libgphoto2) of [libgphot
|
|||||||
# NPM
|
# NPM
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install web-gphoto2
|
npm install web-gphoto2
|
||||||
// or
|
// or
|
||||||
@@ -15,7 +14,6 @@ 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
|
||||||
@@ -43,16 +41,42 @@ async function updateConfig() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function capturePreviewAsBlob() {
|
async function capturePreviewAsBlob() {
|
||||||
|
// Capture a frame while in live view mode
|
||||||
const blob = await camera.capturePreviewAsBlob();
|
const blob = await camera.capturePreviewAsBlob();
|
||||||
console.log("Blob:", blob);
|
imageUrl = URL.createObjectURL(blob);
|
||||||
|
// Set the imageUrl as the src of an image element in your HTML
|
||||||
}
|
}
|
||||||
|
|
||||||
async function captureImageAsFile() {
|
async function captureImageAsFile() {
|
||||||
|
// Capture an image
|
||||||
const file = await camera.captureImageAsFile();
|
const file = await camera.captureImageAsFile();
|
||||||
console.log("File:", file);
|
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:
|
||||||
|

|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
https://user-images.githubusercontent.com/557590/152155035-a1664656-a7d9-411f-8cb3-5f04320f1391.mp4
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
To build, you'll need Docker. Then:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./build.sh # runs build in Docker
|
||||||
|
npx serve examples/preact # starts a local server with COOP/COEP
|
||||||
|
```
|
||||||
|
|
||||||
|
Then, navigate to http://localhost:3000/ in Chrome.
|
||||||
|
|
||||||
## Common Issues
|
## Common Issues
|
||||||
|
|
||||||
### SharedArrayBuffer can not be found
|
### SharedArrayBuffer can not be found
|
||||||
@@ -96,28 +120,6 @@ export default defineConfig({
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
# Demo
|
|
||||||
|
|
||||||
This repository also contains a [demo app](https://web.dev/porting-libusb-to-webusb/) running gPhoto2 on the Web:
|
|
||||||

|
|
||||||
|
|
||||||
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:
|
|
||||||
|
|
||||||
https://user-images.githubusercontent.com/557590/152155035-a1664656-a7d9-411f-8cb3-5f04320f1391.mp4
|
|
||||||
|
|
||||||
## Building
|
|
||||||
|
|
||||||
To build, you'll need Docker. Then:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
./build.sh # runs build in Docker
|
|
||||||
npx serve examples/preact # starts a local server with COOP/COEP
|
|
||||||
```
|
|
||||||
|
|
||||||
Then, navigate to http://localhost:3000/ in Chrome.
|
|
||||||
|
|
||||||
## See also
|
## See also
|
||||||
|
|
||||||
[RReverser/eos-remote-web](https://github.com/RReverser/eos-remote-web) - my other project for controlling Canon cameras over Web Bluetooth.
|
[RReverser/eos-remote-web](https://github.com/RReverser/eos-remote-web) - my other project for controlling Canon cameras over Web Bluetooth.
|
||||||
|
|||||||
@@ -25,8 +25,7 @@ const ModulePromise = initModule();
|
|||||||
export function rethrowIfCritical(err) {
|
export function rethrowIfCritical(err) {
|
||||||
// If it's precisely Error, it's a custom error; anything else - SyntaxError,
|
// If it's precisely Error, it's a custom error; anything else - SyntaxError,
|
||||||
// WebAssembly.RuntimeError, TypeError, etc. - is treated as critical here.
|
// WebAssembly.RuntimeError, TypeError, etc. - is treated as critical here.
|
||||||
if (err.constructor !== Error)
|
if (err.constructor !== Error) {
|
||||||
{
|
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,8 +44,7 @@ export async function connect() {
|
|||||||
* @param {(ctx: Context) => Promise<T>} op
|
* @param {(ctx: Context) => Promise<T>} op
|
||||||
* @returns {Promise<T>}
|
* @returns {Promise<T>}
|
||||||
*/
|
*/
|
||||||
function schedule(op)
|
function schedule(op) {
|
||||||
{
|
|
||||||
let res = queue.then(() => op(context));
|
let res = queue.then(() => op(context));
|
||||||
queue = res.catch(rethrowIfCritical);
|
queue = res.catch(rethrowIfCritical);
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ export const isDebug = new URLSearchParams(location.search).has('debug');
|
|||||||
|
|
||||||
const Stats = isDebug
|
const Stats = isDebug
|
||||||
? await import('stats.js').then(
|
? await import('stats.js').then(
|
||||||
res => /** @type {typeof import('stats.js')} */ (res['default'])
|
res => /** @type {typeof import('stats.js')} */(res['default'])
|
||||||
)
|
)
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
/** @extends Component<{ getPreview?: () => Promise<Blob> }, { error?: string }> */
|
/** @extends Component<{ getPreview?: () => Promise<Blob> }, { error?: string }> */
|
||||||
@@ -96,9 +96,9 @@ export class Preview extends Component {
|
|||||||
blob,
|
blob,
|
||||||
ratio
|
ratio
|
||||||
? {
|
? {
|
||||||
resizeWidth: canvas.width,
|
resizeWidth: canvas.width,
|
||||||
resizeHeight: canvas.height
|
resizeHeight: canvas.height
|
||||||
}
|
}
|
||||||
: {}
|
: {}
|
||||||
);
|
);
|
||||||
if (!ratio) {
|
if (!ratio) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "web-gphoto2",
|
"name": "web-gphoto2-npm-test",
|
||||||
"version": "1.0.0",
|
"version": "0.14.0",
|
||||||
"description": "WebAssembly implementation of gphoto2 and libusb to control DSLR cameras over USB on the Web",
|
"description": "WebAssembly implementation of gphoto2 and libusb to control DSLR cameras over USB on the Web",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/camera.js",
|
"main": "src/camera.js",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2021 Google LLC
|
* Copyright 2023 Google LLC
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or
|
* This library is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
@@ -16,21 +16,12 @@
|
|||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** @typedef {import('./libapi.mjs.d.ts').Context} Context */
|
/**
|
||||||
|
* @typedef {import('./libapi.mjs').Config} Config
|
||||||
// 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.
|
* @typedef {import('./libapi.mjs').SupportedOps} SupportedOps
|
||||||
let initModule;
|
* @typedef {import('./libapi.mjs').Context} Context
|
||||||
|
*/
|
||||||
async function initializeModule() {
|
import initModule from "../build/libapi.mjs";
|
||||||
if (typeof window !== "undefined") {
|
|
||||||
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) {
|
export function rethrowIfCritical(err) {
|
||||||
// If it's precisely Error, it's a custom error; anything else - SyntaxError,
|
// If it's precisely Error, it's a custom error; anything else - SyntaxError,
|
||||||
@@ -43,6 +34,9 @@ export function rethrowIfCritical(err) {
|
|||||||
const INTERFACE_CLASS = 6; // PTP
|
const INTERFACE_CLASS = 6; // PTP
|
||||||
const INTERFACE_SUBCLASS = 1; // MTP
|
const INTERFACE_SUBCLASS = 1; // MTP
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class provides methods for interacting with the camera.
|
||||||
|
*/
|
||||||
class Camera {
|
class Camera {
|
||||||
constructor() {
|
constructor() {
|
||||||
/** @type {Promise<unknown>} */
|
/** @type {Promise<unknown>} */
|
||||||
@@ -52,6 +46,10 @@ class Camera {
|
|||||||
this.ModulePromise = null;
|
this.ModulePromise = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method shows the camera picker.
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
async showCameraPicker() {
|
async showCameraPicker() {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
await navigator.usb.requestDevice({
|
await navigator.usb.requestDevice({
|
||||||
@@ -64,24 +62,24 @@ class Camera {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method connects to the camera.
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
async connect() {
|
async connect() {
|
||||||
if (!this.ModulePromise) {
|
if (!this.ModulePromise) {
|
||||||
this.ModulePromise = initializeModule().then((initModule) => {
|
this.ModulePromise = initModule()
|
||||||
if (initModule) {
|
|
||||||
return initModule();
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
this.Module = await this.ModulePromise;
|
this.Module = await this.ModulePromise;
|
||||||
this.context = await new this.Module.Context();
|
this.context = await new this.Module.Context();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Schedules an exclusive async operation on the global context.
|
/**
|
||||||
|
* This method schedules an exclusive async operation on the global context.
|
||||||
* @template T
|
* @template T
|
||||||
* @param {(ctx: Context) => Promise<T>} op
|
* @param {(ctx: Context) => Promise<T>} op
|
||||||
* @returns {Promise<T>}
|
* @returns {Promise<T>}
|
||||||
|
* @private
|
||||||
*/
|
*/
|
||||||
async schedule(op) {
|
async schedule(op) {
|
||||||
let res = this.queue.then(() => op(this.context));
|
let res = this.queue.then(() => op(this.context));
|
||||||
@@ -89,16 +87,28 @@ class Camera {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method disconnects from the camera.
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
async disconnect() {
|
async disconnect() {
|
||||||
if (!this.context.isDeleted()) {
|
if (!this.context.isDeleted()) {
|
||||||
this.context.delete();
|
this.context.delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets the camera configuration.
|
||||||
|
* @returns {Promise<Config>}
|
||||||
|
*/
|
||||||
async getConfig() {
|
async getConfig() {
|
||||||
return this.schedule((context) => context.configToJS());
|
return this.schedule((context) => context.configToJS());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets the supported operations of the camera.
|
||||||
|
* @returns {Promise<SupportedOps>}
|
||||||
|
*/
|
||||||
async getSupportedOps() {
|
async getSupportedOps() {
|
||||||
if (this.context) {
|
if (this.context) {
|
||||||
return await this.context.supportedOps();
|
return await this.context.supportedOps();
|
||||||
@@ -106,23 +116,40 @@ class Camera {
|
|||||||
throw new Error("You need to connect to the camera first");
|
throw new Error("You need to connect to the camera first");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets a configuration value on the camera.
|
||||||
|
* @param {string} name
|
||||||
|
* @param {number | string | boolean} value
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
async setConfigValue(name, value) {
|
async setConfigValue(name, value) {
|
||||||
const uiTimeout = new Promise((resolve) => setTimeout(resolve, 800));
|
const uiTimeout = new Promise((resolve) => setTimeout(resolve, 800));
|
||||||
const setResult = this.schedule((context) =>
|
const setResult = this.schedule((context) =>
|
||||||
context.setConfigValue(name, value)
|
context.setConfigValue(name, value)
|
||||||
);
|
);
|
||||||
// wait for both the config set operation and the timeout to complete
|
|
||||||
return Promise.all([setResult, uiTimeout]);
|
return Promise.all([setResult, uiTimeout]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method captures a preview as a Blob.
|
||||||
|
* @returns {Promise<Blob>}
|
||||||
|
*/
|
||||||
async capturePreviewAsBlob() {
|
async capturePreviewAsBlob() {
|
||||||
return this.schedule((context) => context.capturePreviewAsBlob());
|
return this.schedule((context) => context.capturePreviewAsBlob());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method captures an image as a File.
|
||||||
|
* @returns {Promise<File>}
|
||||||
|
*/
|
||||||
async captureImageAsFile() {
|
async captureImageAsFile() {
|
||||||
return this.schedule((context) => context.captureImageAsFile());
|
return this.schedule((context) => context.captureImageAsFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method consumes camera events.
|
||||||
|
* @returns {Promise<boolean>}
|
||||||
|
*/
|
||||||
async consumeEvents() {
|
async consumeEvents() {
|
||||||
return this.schedule((context) => context.consumeEvents());
|
return this.schedule((context) => context.consumeEvents());
|
||||||
}
|
}
|
||||||
|
|||||||
25
src/libapi.mjs.d.ts
vendored
25
src/libapi.mjs.d.ts
vendored
@@ -16,7 +16,7 @@
|
|||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export type Config = {
|
type Config = {
|
||||||
name: string;
|
name: string;
|
||||||
info: string;
|
info: string;
|
||||||
label: string;
|
label: string;
|
||||||
@@ -58,32 +58,11 @@ declare class Context {
|
|||||||
isDeleted(): boolean;
|
isDeleted(): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export declare class Camera {
|
|
||||||
constructor();
|
|
||||||
|
|
||||||
showCameraPicker(): Promise<void>;
|
|
||||||
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 {
|
export interface Module extends EmscriptenModule {
|
||||||
Context: typeof Context;
|
Context: typeof Context;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type { Context };
|
export type { Config, Context, SupportedOps };
|
||||||
|
|
||||||
declare const initModule: EmscriptenModuleFactory<Module>;
|
declare const initModule: EmscriptenModuleFactory<Module>;
|
||||||
export default initModule;
|
export default initModule;
|
||||||
|
|||||||
Reference in New Issue
Block a user