Node.js library for USB Video Class (UVC) devices (node-libuvc
)
Low-level Node.js library for USB Video Class (UVC) devices, wrapping the cross-platform libuvc
C library. Used to write software for webcams, camcorders, etcetera.
UVC-compliant devices include webcams, digital camcorders, transcoders, analog video converters and still-image cameras.
This is a experimental, proof-of-concept combining several technologies.
- Conan to download the
libuvc
C library, plus dependencies, for the current operating system/platform. - CMake.js for C compilation/build.
- @ffi-packager/ffi-generate to generate Foreign Function Interface (FFI) bindings for
libuvc
. ffi-napi
and related libraries, to dynamically load thelibuvc
bindings.
This proof-of-concept lead to the development of node-ffi-packager.
Features
- All functionality of
libuvc
available in Node.js. - Strictly follows the
libuvc.h
C header file. - Enables C-style programming.
- This style is different from the usual conveniences of modern Javascript.
- This low-level library was primarily created to enable building other libraries, where developer convenience can be a goal.
Requirements
- Node.js (
node
andnpm
commands) - Dependencies required by CMake.js:
- CMake
- A C/C++ compiler toolchain for your operating system/platform, such as:
- Conan
- A supported, and connected, UVC camera.
Installation
Use a package.json
reference to your preferred semantic version node-libuvc
git tag.
npm install --save github:joelpurra/node-libuvc#semver:^v1.0.0
Usage
See ./examples/
for ready-to-run code.
const libuvc = require("libuvc");
const ref = require("ref-napi");
// NOTE: C types in comments for reference.
const /* uvc_context_t * */ ctx = ref.alloc(libuvc.types.uvc_context_tPointer);
const /* uvc_device_t * */ dev = ref.alloc(libuvc.types.uvc_device_tPointer);
const /* uvc_device_handle_t * */ devh = ref.alloc(
libuvc.types.uvc_device_handle_tPointer
);
const /* uvc_stream_ctrl_t */ ctrl = ref.alloc(libuvc.types.uvc_stream_ctrl_t);
let /* uvc_error_t */ res;
res = libuvc.functions.uvc_init(/* & */ ctx, null);
res = libuvc.functions.uvc_find_device(ctx.deref(), /* & */ dev, 0, 0, null);
res = libuvc.functions.uvc_open(dev.deref(), /* & */ devh);
// NOTE: use the UVC device here, for example setting controls and streaming image frames.
libuvc.functions.uvc_close(devh.deref());
libuvc.functions.uvc_unref_device(dev.deref());
libuvc.functions.uvc_exit(ctx.deref());
Building
The package should be automatically built by npm install
. You can also manually (re)build the package dependencies.
npm run --silent build
Development
- Requires a UVC device, such as a compatible webcam.
- Get the source code from the
node-libuvc
repository. - Follow git-flow and use git-flow-avh.
- Make sure that all example code works by testing them manually.
# Make sure git-flow is initialized.
git flow init -d
npm run --silent rebuild
npm run --silent test
See also
@ffi-libraries/libuvc-v0.0.6
for prebuilt binaries.- Generated by node-ffi-packager, which automates the manual proof-of-concept steps in this repository.
node-uvc
which implements a developer-friendly Javascript layer on top of@ffi-libraries/libuvc-v0.0.6
.node-uvc-control
for a UVC controls implementation based onnode-usb
(npm packageusb
), which in turn is based onlibusb
(Wikipedia).uvcc
for a user-friendly command line interface (CLI) to change camera controls.- USB Video Class on Wikipedia.
- List of USB video class devices on Wikipedia.
- The
v4l-utils
for video4linux (Wikipedia), which includesv4l2-ctl
.
Acknowledgements
- Ken Tossel for creating
libuvc
.
node-libuvc
Copyright © 2020, 2021 Joel Purra. Released under GNU Lesser General Public License version 3.0 (LGPL-3.0). Your donations are appreciated!