Skip to main content

Camera sample

This guide will help you create a simple camera viewer application using the Media Blocks SDK .Net.

Required media blocks

To create a camera viewer, you need to add the following blocks to the pipeline:

Pipeline creation

First, we need to create a pipeline.

using VisioForge.Core.MediaBlocks;

var pipeline = new MediaBlocksPipeline();

We are working with live streams and must set the live parameter to true.

Add error handling to the pipeline.

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

Adding blocks

System video source

We must enumerate available devices to add the camera source and select the desired one.

You can enumerate selected camera formats, select the desired one, and set the frame rate from the list.

VideoCaptureDeviceSourceSettings videoSourceSettings = null;
var videoDevices = await DeviceEnumerator.Shared.VideoSourcesAsync();
var device = videoDevices[0];

var formatItem = device.VideoFormats[0];
if (formatItem != null)
{
videoSourceSettings = new VideoCaptureDeviceSourceSettings(device)
{
Format = formatItem.ToFormat()
};

videoSourceSettings.Format.FrameRate = formatItem.FrameRateList[0];
}

var videoSource = new SystemVideoSourceBlock(videoSourceSettings);

Video renderer

Now, let's add the video renderer.

var videoRenderer = new VideoRendererBlock(pipeline, VideoView1);

Connecting blocks

To connect the blocks, add the following code:

pipeline.Connect(videoSource.Output, videoRenderer.Input);

Start the pipeline

To start the pipeline, call the StartAsync method.

await pipeline.StartAsync();

You'll see the video from the camera on the screen.

If you have low-quality video, select another format and frame rate. We use the first available format and frame rate in this example.

Stop the pipeline

To stop the pipeline, call the StopAsync method.

await pipeline.StopAsync();

Please check the complete source code on GitHub.