Class H264EncoderBlock
- Assembly
- VisioForge.Core.dll
H.264/AVC encoder block for industry-standard video compression with broad compatibility. This block provides comprehensive H.264 encoding capabilities with support for multiple hardware acceleration platforms including NVIDIA NVENC, AMD AMF, Intel QSV, and Apple VideoToolbox. Features adaptive bitrate control, multiple profile support (Baseline, Main, High), and optimized encoding for various use cases from streaming to broadcast. Essential for universal video compatibility across devices, platforms, and streaming services. Implements the VisioForge.Core.MediaBlocks.MediaBlock. Implements the VisioForge.Core.MediaBlocks.IMediaBlockInternals. Implements the IDisposable.
public class H264EncoderBlock : MediaBlock, IMediaBlock, IDisposable, IMediaBlockInternalsInheritance
Implements
Inherited Members
Extension Methods
Constructors
H264EncoderBlock(IH264EncoderSettings)
Initializes a new instance of the VisioForge.Core.MediaBlocks.VideoEncoders.H264EncoderBlock class with specified H.264 encoder settings.
public H264EncoderBlock(IH264EncoderSettings settings)Parameters
settingsIH264EncoderSettings-
The H.264 encoder configuration settings.
H264EncoderBlock()
Initializes a new instance of the VisioForge.Core.MediaBlocks.VideoEncoders.H264EncoderBlock class with automatically selected optimal encoder settings based on available hardware acceleration.
public H264EncoderBlock()Properties
Input
Gets the input.
public override MediaBlockPad Input { get; }Property Value
Inputs
Gets the inputs.
public override MediaBlockPad[] Inputs { get; }Property Value
Output
Gets the output.
public override MediaBlockPad Output { get; }Property Value
Outputs
Gets the outputs.
public override MediaBlockPad[] Outputs { get; }Property Value
Settings
Gets or sets the H.264 encoder settings. Contains comprehensive configuration parameters for H.264 encoding including profile selection, bitrate control, quality settings, and hardware acceleration options.
public IH264EncoderSettings Settings { get; set; }Property Value
Type
Gets the type.
public override MediaBlockType Type { get; }Property Value
Methods
Build()
Constructs and initializes the H.264 encoder and its associated GStreamer elements. This method creates the encoder with the specified settings, establishes input and output pads, and prepares the block for industry-standard video compression with broad compatibility.
public override bool Build()Returns
- bool
-
trueif the encoder was successfully built and configured;falseif initialization failed.
CanEncodeAsync(IH264EncoderSettings)
Encodes a few test frames with the encoder these settings select, to find out whether it can really encode on this machine. VisioForge.Core.MediaBlocks.VideoEncoders.H264EncoderBlock.IsAvailable(VisioForge.Core.Types.X.VideoEncoders.IH264EncoderSettings) only says that its element exists.
public static Task<bool> CanEncodeAsync(IH264EncoderSettings settings)Parameters
settingsIH264EncoderSettings-
Selects the encoder. Its other values, such as the bitrate, are not applied.
Returns
Remarks
The answer holds for this moment and for a small frame. A session limit reached later, or a
frame size the encoder refuses, can still fail a real encode. A check that runs out of its
15 seconds returns false and leaves its thread and pipeline behind, so don't repeat
it in a loop: an encoder that wedged can keep hold of what a recording would need.
Exceptions
- ArgumentNullException
-
settingsis null. - InvalidOperationException
-
The SDK is not initialized yet.
CleanUp()
Releases all resources and performs cleanup operations for this H.264 encoder block. This method disposes of the GStreamer elements and resets the build state so the block can be safely rebuilt with a new pipeline.
public void CleanUp()Dispose(bool)
Releases unmanaged and - optionally - managed resources.
protected override void Dispose(bool disposing)Parameters
disposingbool-
trueto release both managed and unmanaged resources;falseto release only unmanaged resources.
GetCore()
Gets the core BaseElement wrapper that provides additional functionality around the GStreamer H.264 encoder element.
public BaseElement GetCore()Returns
- BaseElement
-
The BaseElement wrapper instance that encapsulates the GStreamer H.264 encoder.
GetDefaultSettings()
Gets the optimal default H.264 encoder settings based on available hardware acceleration. Automatically selects the best available encoder: hardware-accelerated encoders are preferred over software implementations for optimal performance.
public static IH264EncoderSettings GetDefaultSettings()Returns
- IH264EncoderSettings
-
The optimal H.264 encoder settings for the current system.
Remarks
On Android and the Apple platforms the platform encoder is assumed when GStreamer is not initialized yet: the probe behind it is ElementFactory.Find, which throws before InitSDK, and an output constructed that early has always worked on these platforms. The same assumption AACEncoderBlock.GetDefaultSettings() makes about AVENC, for the same reason.
The consequence is that the OpenH264 fallback is only reachable after InitSDK. An output constructed before it latches the platform encoder unprobed, and on a runtime that genuinely has none - a stripped framework, an Android image with no MediaCodec H.264 - the pipeline then fails to build rather than falling back. That is the behaviour macOS has always shipped, where the MP4 default was a hardcoded AppleMediaH264EncoderSettings with no probe at all; it is not new, only wider. Construct outputs after InitSDK to get the probe.
On the other platforms a call before InitSDK returns OpenH264: there is no element registry to read yet, and with the redist in its deployed folder the lookup itself would throw. A warning goes to the trace log. The Windows output types call this on the first read of their Video property, so an output constructed before InitSDK still gets the GPU encoder if that read comes after it. VisioForge.Core.MediaBlocks.VideoEncoders.H264EncoderBlock.#ctor calls it in the constructor, so it stays on OpenH264 when constructed before InitSDK.
A GPU encoder is chosen because its GStreamer element exists. That does not prove it can encode right now: all NVENC sessions can be taken, or the driver can refuse. Call VisioForge.Core.MediaBlocks.VideoEncoders.H264EncoderBlock.CanEncodeAsync(VisioForge.Core.Types.X.VideoEncoders.IH264EncoderSettings) to find out, or set OpenH264 explicitly.
NVENC, AMF and QSV come back with a keyframe every 25 frames rather than their own defaults, the cadence OpenH264 has, so the interval does not depend on which of those four gets picked.
GetElement()
Gets the underlying GStreamer element that performs the H.264 video encoding.
public Element GetElement()Returns
- Element
-
The GStreamer Element instance representing the H.264 encoder.
IsAvailable(IH264EncoderSettings)
Determines whether H.264 encoding is available with the specified settings. Checks for required codec libraries and hardware acceleration support.
public static bool IsAvailable(IH264EncoderSettings settings)Parameters
settingsIH264EncoderSettings-
The H.264 encoder settings to validate.
Returns
- bool
-
trueif H.264 encoding is available; otherwise,false.
IMediaBlockInternals.SetContext(MediaBlocksPipeline)
Associates this H.264 encoder block with a pipeline and initializes its internal context. This method is called internally when the block is added to a pipeline.
void IMediaBlockInternals.SetContext(MediaBlocksPipeline pipeline)Parameters
pipelineMediaBlocksPipeline-
The MediaBlocksPipeline instance that will manage this block.