Skip to main content

Pipeline usage

The MediaBlocksPipeline is the core of the Media Blocks SDK .Net.

Using the pipeline, you can create a video player, a camera viewer, a video recorder, or any other application that processes video and audio streams.

SDK contains many blocks, each performing one small function, such as reading a video file, displaying video, playing audio, encoding video, and more.

Pipeline creation

First, we need to create a pipeline.

using VisioForge.Core.MediaBlocks;

var pipeline = new MediaBlocksPipeline();

Error handling

Add error handling to the pipeline.

pipeline.OnError += (sender, args) =>
{
Debug.WriteLine(args.Message);
};

Duration and position

You can get the duration and position of the pipeline.

The duration is unknown for live streams, and the position is the current time.

var duration = await _pipeline.DurationAsync();

var position = await _pipeline.Position_GetAsync();

For seeking, you can set the position.

await _pipeline.Position_SetAsync(TimeSpan.FromSeconds(10));

Start and stop

To start the pipeline, call the StartAsync method.

await _pipeline.StartAsync();

The StartAsync method has an optional onlyPreload parameter. Set it to true if you want to preload the pipeline without starting it.

await _pipeline.StartAsync(onlyPreload: true);

Use the ResumeAsync method to start the preloaded pipeline.

To stop the pipeline, call the StopAsync method.

await _pipeline.StopAsync();

State

You can get the pipeline state.

var state = _pipeline.State;

The pipeline state can be Free, Pause, or Play.

Also, you can subscribe to the OnStart and OnStop events.

_pipeline.OnStart += (sender, args) =>
{
Debug.WriteLine("Pipeline started");
};

_pipeline.OnStop += (sender, args) =>
{
Debug.WriteLine("Pipeline stopped");
};

Use the PauseAsync method to pause the pipeline.

await _pipeline.PauseAsync();

Use the ResumeAsync method to resume the pipeline.

await _pipeline.ResumeAsync();

Connecting blocks

To connect the blocks, add the following code:

pipeline.Connect(block1.Output, block2.Input);

Each block has an Output and an Input property. You can connect the blocks using the Connect method.

Depending on the block type, it may have multiple inputs and outputs.

Some blocks have additional independent video and audio outputs, with AudioOutput and VideoOutput names.

Sink blocks with inputs by request

Some sink blocks have input by request. These blocks implement the IMediaBlockDynamicInputs interface — for example, the MP4SinkBlock.

Sample usage:

// create video input
var videoInput = mp4Muxer.CreateNewInput(MediaBlockPadMediaType.Video);

// connect (some) video source to video input
pipeline.Connect(videoSource.Output, videoInput);

You need to set the input media type to request a new input.

Disposing

Don't forget to dispose of the pipeline when you finish using it.

pipeline.Dispose();

If possible, use the DisposeAsync method to prevent UI freezes.

await pipeline.DisposeAsync();