VideoDecoder
TheVideoDecoder class decodes compressed video data (EncodedVideoChunk) into raw video frames (VideoFrame). It supports H.264, VP8, VP9, HEVC, and AV1 codecs with optional hardware acceleration.
This class follows the W3C WebCodecs VideoDecoder specification.
Quick Example
Constructor
Creates a newVideoDecoder instance.
Initialization object containing callbacks for decoded frames and errors.
Example: Creating a decoder
Example: Creating a decoder
Properties
Current state of the decoder. Possible values:
Number of decode operations currently pending in the queue. Useful for implementing backpressure to avoid memory exhaustion when decoding faster than frames can be processed.
Static Methods
isConfigSupported()
Checks if a decoder configuration is supported by the current system.The decoder configuration to test for support.
Promise<VideoDecoderSupport> - Object indicating whether the config is supported.
Example: Checking codec support
Example: Checking codec support
Example: Testing hardware acceleration
Example: Testing hardware acceleration
Methods
configure()
Configures the decoder with the specified codec and parameters.Configuration object specifying codec and video parameters.
InvalidStateErrorif the decoder is closedNotSupportedErrorif the codec is not supported
Example: Basic H.264 configuration
Example: Basic H.264 configuration
Example: Configuration with extradata
Example: Configuration with extradata
Example: HDR video configuration
Example: HDR video configuration
decode()
Queues an encoded video chunk for decoding.The encoded video chunk to decode. Can be a keyframe (
type: 'key') or delta frame (type: 'delta').InvalidStateErrorif the decoder is not configuredDataErrorif the chunk data is malformed
Example: Decoding a stream of chunks
Example: Decoding a stream of chunks
flush()
Waits for all pending decode operations to complete.Promise<void> - Resolves when all queued chunks have been decoded and all output callbacks have been invoked.
Throws:
InvalidStateErrorif the decoder is not configured
Example: Ensuring all frames are decoded
Example: Ensuring all frames are decoded
reset()
Resets the decoder to the unconfigured state, discarding all pending work.'unconfigured' state and must be reconfigured before use.
Throws:
InvalidStateErrorif the decoder is closed
Example: Resetting for new stream
Example: Resetting for new stream
close()
Closes the decoder and releases all resources.close(), the decoder cannot be used. Any pending decode operations are aborted.
Example: Proper cleanup
Example: Proper cleanup
addEventListener()
Adds an event listener for decoder events.Event type. Currently only
'dequeue' is supported.Callback function to invoke when the event fires.
Event listener options.
Example: Backpressure using dequeue event
Example: Backpressure using dequeue event
removeEventListener()
Removes a previously added event listener.Event type (e.g.,
'dequeue').The callback function to remove.
Example: Removing a listener
Example: Removing a listener
Interfaces
VideoDecoderConfig
Configuration options for the video decoder.VideoDecoderInit
Initialization callbacks for the decoder constructor.VideoDecoderSupport
Result ofisConfigSupported().
Hardware Decoding
Hardware-accelerated decoding uses GPU decoders for improved performance and reduced CPU usage. This is especially beneficial for 4K+ video or when processing multiple streams.Hardware decoder availability depends on your system:
- macOS: VideoToolbox (all Macs)
- Windows/Linux: NVDEC (NVIDIA GPUs), VA-API (Intel/AMD)
- Raspberry Pi: V4L2 M2M
See Also
VideoFrame
Raw video frame data output by the decoder
VideoEncoder
Encode VideoFrames into compressed video
EncodedVideoChunk
Compressed video data input to the decoder
Hardware Acceleration
Guide to GPU-accelerated encoding/decoding