How to stream video to multiple locations simultaneously (video stream duplication) in C#

This example demonstrates how you can stream video to multiple locations simultaneously in C#. To implement this example, you need to have Ozeki Camera SDK installed, and a reference to OzekiSDK.dll should be added to your Visual Studio project.

Stream video in C#

To establish the connection properly between your application and an IP camera you should apply the same code snippet what you have used in the example (How to connect to an IP camera device using C#?). Important: you should study this article in order to find out how to setup your Console Application correctly.

Getting started

To get started it is recomended to Download and Install the latest version of Ozeki Camera SDK. After installation you can find the example code discussed in this page with full source code in the following location on your harddisk:

Download Ozeki Camera SDK: https://www.camera-sdk.com/p_6513-download-onvif-ozeki-camera-sdk-for-c-sharp.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Onvif_IP_Camera_Server_Console\Onvif_IP_Camera_Server_Console.sln

To compile this example you will need Microsoft Visual Studio installed on your computer.

In the following you will be shown how to implement an IP camera live streaming service, which will involve multiple IP camera feeds to be presented to users across the web. There is a way that it could be installed on a local server on site. This way it can stream videos only after the authentication of the user.

This solution is easy-to-use and deliver high quality, reliable video from a main location to secondary locations anywhere in the world. The video over IP solution allows users to stream live or file-based video decoders over any network connections.

This option allows users to stream video from one main site to multiple recipient sites at the same time by using Server. The Server is a centralized secure distribution tool that manages large amounts of video for live playout or on-demand video scheduling.

Videos can be played out for mobile phones and tablets, or live streamed to your computer. Video over IP products and applications are easy to use, and saves time. This technology allows us to send video reliably over public Internet connections at low data rates without using bandwith-consuming Forward Error Correction (FEC).

An average camera lets one or two clients at the same time but this problem is easy to solve with Ozeki Camera SDK. You just have to connect it to the server and then numerous clients can connect at the same time. In this way we can stream videos to multiple locations simultaneously.

This example is about a Console Application instead of a Windows Forms Application. You will not need the image of the camera in the application now so the console interface is just enough.

Stream video to multiple locations simultaneously in C#

With the MyServer class your application is going to be able to establish a suitable connection between the C# Console Application server and clients. It implements the IPCameraServer interface which helps to create your own server. When your application is running we can check "who" is connected to the server

The _server object has a

  • start() method which will start your server.
  • stop() method which will stop your server.
  • server_ClientCountChange event which will write the list of clients to the console.

Program.cs

using System;
using Ozeki.Media;
using Ozeki.Camera;
using Ozeki.VoIP;

namespace Onvif_IP_Camera_Server_04
{
    public class MyServer : CameraServer
    {
        private MediaConnector _connector;
        private IIPCamera _camera;
        private ICameraClient _client;

        public string IpAddress { get; set; }
        public int Port { get; set; }

        public MyServer()
        {
            _connector = new MediaConnector();
            _camera = new IPCamera("192.168.112.109:8080", "user", "qwe123");

            if (_camera != null)
                _camera.Start();
        }

        protected override void OnClientConnected(ICameraClient client)
        {
            _client = client;
            _connector.Connect(_camera.AudioChannel, _client.AudioChannel);
            _connector.Connect(_camera.VideoChannel, _client.VideoChannel);

            base.OnClientConnected(_client);
        }

        protected override void OnClientDisconnected(ICameraClient client)
        {
            _connector.Disconnect(_camera.AudioChannel, _client.AudioChannel);
            _connector.Disconnect(_camera.VideoChannel, _client.VideoChannel);
            _connector.Dispose();

       
            base.OnClientDisconnected(client);
        }
    }

    class Program
    {
        static MyServer _server;

        static void Main(string[] args)
        {
            _server = new MyServer();
            _server.Start();
            _server.SetListenAddress("192.168.115.60", 554);
            _server.ClientCountChanged += _server_ClientCountChanged;
            Console.WriteLine("Started");
            Console.Read();
        }

        static void _server_ClientCountChanged(object sender, EventArgs e)
        {
            _server.ConnectedClients.ForEach(x => Console.WriteLine(x.TransportInfo.RemoteEndPoint.ToString()));
        }
    }
}
	
Code 1 - Example to stream a video to multiple locations simultaneously in C#

Please note that none of the cancel and disconnect methods are included in the example because of the demonstrating intent and briefness of the article. You may need to replace the first attribute of _server.SetListenAddress to your own IP-address.

Console output

console output
Figure 1 - Your console output

DISCLAIMER: Please note that the following features will only work if your IP camera supports the given function. You should check the user manual of your IP camera to make sure it supports the feature that you wish to implement in C#.

Related Pages

FAQ

Below you can find the answers for the most frequently asked questions related to this topic:

  1. How can I get the URL of the camera?

    You can get the URL from the producer of the camera. (In the 10th tutorial you can find information on how to create an own IP camera discoverer program.)

  2. I have not managed to build the solution. How to solve it?

    • Please set the Target framework property of the project to .NET 4.0.
    • You should add the OzekiSDK.dll to the references of the solution.
    • Please set first attribue of _server.SetListenAddress to your own IP-address.
    • Please import the missing classes.

More information