Table of Contents

Class SourceSwitchBlock

Namespace
VisioForge.Core.MediaBlocks.Special
Assembly
VisioForge.Core.dll

Provides dynamic switching between multiple input sources, allowing seamless source changes during pipeline operation.

public class SourceSwitchBlock : MediaBlock, IMediaBlock, IDisposable, IMediaBlockInternals

Inheritance

Implements

Inherited Members

Extension Methods

Examples

// Example 1: Switch between multiple cameras
var switchSettings = new SourceSwitchSettings { PadsCount = 3 };
var sourceSwitch = new SourceSwitchBlock(switchSettings);

// Connect three camera sources
pipeline.Connect(camera1.Output, sourceSwitch.Inputs[0]);
pipeline.Connect(camera2.Output, sourceSwitch.Inputs[1]);
pipeline.Connect(camera3.Output, sourceSwitch.Inputs[2]);

// Connect output to renderer
pipeline.Connect(sourceSwitch.Output, videoRenderer.Input);

// Switch to camera 2 during runtime
sourceSwitch.Switch(1);

// Example 2: Implement source failover
var switchSettings = new SourceSwitchSettings { PadsCount = 2 };
var sourceSwitch = new SourceSwitchBlock(switchSettings);

pipeline.Connect(primarySource.Output, sourceSwitch.Inputs[0]);
pipeline.Connect(backupSource.Output, sourceSwitch.Inputs[1]);

// Monitor primary source and switch to backup if needed
if (primarySourceFailed)
{
    sourceSwitch.Switch(1); // Switch to backup
}

Remarks

The VisioForge.Core.MediaBlocks.Special.SourceSwitchBlock enables real-time switching between multiple input streams without interrupting the pipeline. It accepts multiple inputs and routes one of them to the output based on the current selection.

Key features and use cases:

  • Live switching between camera feeds in surveillance systems
  • Multi-angle video playback with seamless transitions
  • Failover mechanisms for redundant media sources
  • A/B testing of different processing chains
  • Dynamic source selection in live streaming applications

The block supports:

  • Configurable number of input pads (set during construction)
  • Runtime switching via the VisioForge.Core.MediaBlocks.Special.SourceSwitchBlock.Switch(System.Int32) method
  • Automatic media type negotiation
  • Seamless transitions without frame drops or glitches

Important: All input sources should have compatible formats (resolution, framerate, etc.) for optimal switching performance. Format differences may cause brief interruptions during switches.

Constructors

SourceSwitchBlock(SourceSwitchSettings)

Initializes a new instance of the VisioForge.Core.MediaBlocks.Special.SourceSwitchBlock class.

public SourceSwitchBlock(SourceSwitchSettings settings)

Parameters

settings SourceSwitchSettings

The configuration settings for the source switch.

Remarks

The number of input pads is determined by VisioForge.Core.Types.X.Special.SourceSwitchSettings.PadsCount. Each input pad accepts any media type (auto-negotiation), and the output pad will adopt the format of the currently selected input.

After construction, connect all input sources before starting the pipeline. The first input (index 0) is selected by default.

Exceptions

ArgumentNullException

Thrown when settings is null.

ArgumentException

Thrown when settings contains invalid configuration.

Properties

Input

Gets the input.

public override MediaBlockPad Input { get; }

Property Value

MediaBlockPad

Inputs

Gets the inputs.

public override MediaBlockPad[] Inputs { get; }

Property Value

MediaBlockPad[]

Output

Gets the output.

public override MediaBlockPad Output { get; }

Property Value

MediaBlockPad

Outputs

Gets the outputs.

public override MediaBlockPad[] Outputs { get; }

Property Value

MediaBlockPad[]

Settings

Gets or sets the configuration settings for the source switch.

public SourceSwitchSettings Settings { get; set; }

Property Value

SourceSwitchSettings

Remarks

These settings must be configured before building the block and cannot be changed after initialization.

Type

Gets the type.

public override MediaBlockType Type { get; }

Property Value

MediaBlockType

Methods

Build()

Builds and initializes the source switch element within the pipeline.

public override bool Build()

Returns

bool

true if the element was successfully built and initialized; otherwise, false.

Remarks

This method performs the following operations:

  1. Creates the underlying GStreamer input-selector element
  2. Initializes it with the configured settings
  3. Creates the requested number of input pads
  4. Configures the single output pad

The method creates request pads for inputs, allowing dynamic pad management. All pads are configured for automatic media type negotiation.

CleanUp()

Performs cleanup of the source switch resources.

public void CleanUp()

Remarks

This method disposes of the underlying GStreamer element and resets the build state. It ensures proper cleanup of all dynamically created pads and internal resources.

After cleanup, the block cannot be used again and must be recreated if needed. This method is called automatically during disposal.

Dispose(bool)

Releases unmanaged and - optionally - managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

GetCore()

Gets the core GStreamer wrapper element.

public BaseElement GetCore()

Returns

BaseElement

The VisioForge.Core.GStreamer.Special.SourceSwitch wrapper that manages the underlying input-selector.

Remarks

This provides access to the wrapper class for advanced operations or direct manipulation of the GStreamer input-selector element.

GetElement()

Gets the underlying GStreamer element.

public Element GetElement()

Returns

Element

The native GStreamer input-selector element used for source switching.

Remarks

Direct access to the GStreamer element should be used carefully. Prefer using the block's public API for switching operations.

IsAvailable()

Determines whether the source switch functionality is available in the current environment.

public static bool IsAvailable()

Returns

bool

true if source switching is available; otherwise, false.

Remarks

The source switch requires specific GStreamer plugins to be installed. This method verifies that all necessary components are available.

If this returns false, ensure that:

  • GStreamer core plugins are properly installed
  • The input-selector element is available
  • The correct NuGet SDK redistribution packages are included

Switch(int)

Switches the active input to the specified source.

public void Switch(int switchIndex)

Parameters

switchIndex int

The zero-based index of the input to switch to.

Examples

// Switch to the second input (index 1)
sourceSwitch.Switch(1);

// Implement a rotation between sources
int currentSource = 0;
void RotateSources()
{
    currentSource = (currentSource + 1) % sourceSwitch.Settings.PadsCount;
    sourceSwitch.Switch(currentSource);
}

Remarks

This method performs a seamless switch to the specified input source. The switch happens immediately and should not cause frame drops or audio glitches when sources have compatible formats.

Valid indices range from 0 to (PadsCount - 1). Attempting to switch to an invalid index logs an error and returns without switching.

Thread Safety: This method can be called from any thread and is safe to use during active pipeline operation.

Exceptions

InvalidOperationException

Thrown when the element is not initialized.

ArgumentOutOfRangeException

Thrown when switchIndex is out of range.

IMediaBlockInternals.SetContext(MediaBlocksPipeline)

Sets the context.

void IMediaBlockInternals.SetContext(MediaBlocksPipeline pipeline)

Parameters

pipeline MediaBlocksPipeline

The pipeline.

See Also