node-av / lib / HardwareDeviceContext
Class: HardwareDeviceContext
Defined in: src/lib/hardware-device-context.ts:51
Hardware device context for GPU-accelerated processing.
Manages hardware acceleration devices for video encoding, decoding, and filtering. Provides access to GPU resources like CUDA, VAAPI, VideoToolbox, and other hardware acceleration APIs. Essential for high-performance video processing and reduced CPU usage.
Direct mapping to FFmpeg's AVHWDeviceContext.
Example
import { HardwareDeviceContext, FFmpegError } from 'node-av';
import { AV_HWDEVICE_TYPE_CUDA, AV_HWDEVICE_TYPE_VIDEOTOOLBOX } from 'node-av/constants';
// Create hardware device
const device = new HardwareDeviceContext();
const ret = device.create(AV_HWDEVICE_TYPE_CUDA);
FFmpegError.throwIfError(ret, 'create');
// List available hardware types
const types = HardwareDeviceContext.iterateTypes();
for (const type of types) {
const name = HardwareDeviceContext.getTypeName(type);
console.log(`Available: ${name}`);
}
// Use with decoder
const codecContext = new CodecContext();
codecContext.hwDeviceCtx = device;
// Create derived context
const derived = new HardwareDeviceContext();
const ret2 = derived.createDerived(device, AV_HWDEVICE_TYPE_CUDA);
FFmpegError.throwIfError(ret2, 'createDerived');
// Cleanup
device.free();
See
- AVHWDeviceContext - FFmpeg Doxygen
- HardwareFramesContext For hardware frame allocation
- CodecContext For hardware codec usage
Implements
Disposable
NativeWrapper
<NativeHardwareDeviceContext
>
Constructors
Constructor
new HardwareDeviceContext():
HardwareDeviceContext
Defined in: src/lib/hardware-device-context.ts:54
Returns
HardwareDeviceContext
Accessors
hwctx
Get Signature
get hwctx():
null
|bigint
Defined in: src/lib/hardware-device-context.ts:151
Hardware context pointer.
Opaque pointer to the underlying hardware-specific context. Type depends on the hardware device type.
Direct mapping to AVHWDeviceContext->hwctx.
Returns
null
| bigint
type
Get Signature
get type():
AVHWDeviceType
Defined in: src/lib/hardware-device-context.ts:139
Hardware device type.
The type of hardware acceleration in use.
Direct mapping to AVHWDeviceContext->type.
Returns
Methods
[dispose]()
[dispose]():
void
Defined in: src/lib/hardware-device-context.ts:406
Dispose of the hardware device context.
Implements the Disposable interface for automatic cleanup. Equivalent to calling free().
Returns
void
Example
{
using device = new HardwareDeviceContext();
device.create(AV_HWDEVICE_TYPE_CUDA);
// Use device...
} // Automatically freed when leaving scope
Implementation of
Disposable.[dispose]
alloc()
alloc(
type
):void
Defined in: src/lib/hardware-device-context.ts:180
Allocate hardware device context.
Allocates memory for the specified hardware device type. Must be followed by init() to initialize the device.
Direct mapping to av_hwdevice_ctx_alloc().
Parameters
type
Hardware device type to allocate
Returns
void
Throws
If allocation fails (ENOMEM)
Example
import { AV_HWDEVICE_TYPE_CUDA } from 'node-av/constants';
const device = new HardwareDeviceContext();
device.alloc(AV_HWDEVICE_TYPE_CUDA);
const ret = device.init();
FFmpegError.throwIfError(ret, 'init');
See
create()
create(
type
,device
,options
):number
Defined in: src/lib/hardware-device-context.ts:256
Create and initialize hardware device.
Combined allocation and initialization of a hardware device. This is the preferred method for creating hardware contexts.
Direct mapping to av_hwdevice_ctx_create().
Parameters
type
Hardware device type
device
Device name/path (null for default)
null
| string
options
Device-specific options
null
| Dictionary
Returns
number
0 on success, negative AVERROR on error:
- AVERROR_EINVAL: Invalid type or parameters
- AVERROR_ENOMEM: Memory allocation failure
- AVERROR_ENOSYS: Type not supported
- Device-specific errors
Example
import { FFmpegError, Dictionary } from 'node-av';
import { AV_HWDEVICE_TYPE_CUDA } from 'node-av/constants';
// Create with default device
const device = new HardwareDeviceContext();
let ret = device.create(AV_HWDEVICE_TYPE_CUDA);
FFmpegError.throwIfError(ret, 'create');
// Create with specific device
const device2 = new HardwareDeviceContext();
ret = device2.create(AV_HWDEVICE_TYPE_VAAPI, '/dev/dri/renderD128');
FFmpegError.throwIfError(ret, 'create');
// Create with options
const opts = Dictionary.fromObject({ 'device_idx': '1' });
ret = device.create(AV_HWDEVICE_TYPE_CUDA, null, opts);
FFmpegError.throwIfError(ret, 'create');
See
createDerived To create from existing device
createDerived()
createDerived(
source
,type
):number
Defined in: src/lib/hardware-device-context.ts:293
Create derived hardware device.
Creates a new device context derived from an existing one. Used for interoperability between different hardware APIs.
Direct mapping to av_hwdevice_ctx_create_derived().
Parameters
source
HardwareDeviceContext
Source device context to derive from
type
Target hardware device type
Returns
number
0 on success, negative AVERROR on error:
- AVERROR_EINVAL: Invalid parameters
- AVERROR_ENOSYS: Derivation not supported
- AVERROR_ENOMEM: Memory allocation failure
Example
import { FFmpegError } from 'node-av';
import { AV_HWDEVICE_TYPE_CUDA, AV_HWDEVICE_TYPE_VULKAN } from 'node-av/constants';
// Create CUDA device from Vulkan
const vulkan = new HardwareDeviceContext();
vulkan.create(AV_HWDEVICE_TYPE_VULKAN);
const cuda = new HardwareDeviceContext();
const ret = cuda.createDerived(vulkan, AV_HWDEVICE_TYPE_CUDA);
FFmpegError.throwIfError(ret, 'createDerived');
See
create For creating independent device
free()
free():
void
Defined in: src/lib/hardware-device-context.ts:313
Free hardware device context.
Releases all resources associated with the hardware device. The context becomes invalid after calling this.
Direct mapping to av_buffer_unref() on device context.
Returns
void
Example
device.free();
// Device is now invalid
See
Symbol.dispose For automatic cleanup
getHwframeConstraints()
getHwframeConstraints(
hwconfig?
):null
| {maxHeight
:number
;maxWidth
:number
;minHeight
:number
;minWidth
:number
;validHwFormats?
:number
[];validSwFormats?
:number
[]; }
Defined in: src/lib/hardware-device-context.ts:369
Get hardware frame constraints.
Returns the constraints on frames that can be allocated with this hardware device.
Direct mapping to av_hwdevice_get_hwframe_constraints().
Parameters
hwconfig?
bigint
Optional hardware configuration
Returns
null
| { maxHeight
: number
; maxWidth
: number
; minHeight
: number
; minWidth
: number
; validHwFormats?
: number
[]; validSwFormats?
: number
[]; }
Frame constraints, or null if not available
Example
const constraints = device.getHwframeConstraints();
if (constraints) {
console.log(`Min size: ${constraints.minWidth}x${constraints.minHeight}`);
console.log(`Max size: ${constraints.maxWidth}x${constraints.maxHeight}`);
if (constraints.validSwFormats) {
console.log('Software formats:', constraints.validSwFormats);
}
if (constraints.validHwFormats) {
console.log('Hardware formats:', constraints.validHwFormats);
}
}
getNative()
getNative():
NativeHardwareDeviceContext
Defined in: src/lib/hardware-device-context.ts:387
Internal
Get the underlying native HardwareDeviceContext object.
Returns
The native HardwareDeviceContext binding object
Implementation of
hwconfigAlloc()
hwconfigAlloc():
null
|bigint
Defined in: src/lib/hardware-device-context.ts:338
Allocate hardware configuration.
Allocates a hardware-specific configuration structure. Used for codec configuration with hardware acceleration.
Direct mapping to av_hwdevice_hwconfig_alloc().
Returns
null
| bigint
Configuration pointer, or null on failure
Example
const hwconfig = device.hwconfigAlloc();
if (hwconfig) {
// Use with codec context
codecContext.hwConfig = hwconfig;
}
See
getHwframeConstraints To get constraints
init()
init():
number
Defined in: src/lib/hardware-device-context.ts:209
Initialize allocated hardware device.
Initializes a previously allocated hardware device context. Must be called after alloc() and before using the device.
Direct mapping to av_hwdevice_ctx_init().
Returns
number
0 on success, negative AVERROR on error:
- AVERROR_EINVAL: Invalid parameters
- AVERROR_ENOMEM: Memory allocation failure
- Device-specific errors
Example
import { FFmpegError } from 'node-av';
device.alloc(type);
const ret = device.init();
FFmpegError.throwIfError(ret, 'init');
See
findTypeByName()
static
findTypeByName(name
):AVHWDeviceType
Defined in: src/lib/hardware-device-context.ts:128
Find hardware device type by name.
Converts a string name to the corresponding hardware device type enum.
Direct mapping to av_hwdevice_find_type_by_name().
Parameters
name
string
Hardware type name (e.g., 'cuda', 'vaapi', 'videotoolbox')
Returns
Hardware device type enum, or AV_HWDEVICE_TYPE_NONE if not found
Example
const type = HardwareDeviceContext.findTypeByName('cuda');
if (type !== AV_HWDEVICE_TYPE_NONE) {
console.log('CUDA is available');
}
See
getTypeName For type to name conversion
getTypeName()
static
getTypeName(type
):null
|string
Defined in: src/lib/hardware-device-context.ts:79
Get human-readable name for hardware device type.
Converts a hardware device type enum to its string representation.
Direct mapping to av_hwdevice_get_type_name().
Parameters
type
Hardware device type
Returns
null
| string
Type name string, or null if invalid
Example
import { AV_HWDEVICE_TYPE_CUDA } from 'node-av/constants';
const name = HardwareDeviceContext.getTypeName(AV_HWDEVICE_TYPE_CUDA);
console.log(name); // "cuda"
See
findTypeByName For reverse lookup
iterateTypes()
static
iterateTypes():AVHWDeviceType
[]
Defined in: src/lib/hardware-device-context.ts:103
List all supported hardware device types.
Returns an array of all hardware acceleration types available in the current FFmpeg build.
Direct mapping to av_hwdevice_iterate_types().
Returns
Array of available hardware device types
Example
const types = HardwareDeviceContext.iterateTypes();
console.log('Available hardware acceleration:');
for (const type of types) {
const name = HardwareDeviceContext.getTypeName(type);
console.log(` - ${name}`);
}