How to implement ONVIF IP camera service and RTSP streaming to smartphones / remote PCs in C#

Posted by in C# category on for Intermediate level | Points: 250 | Views : 135676 red flag
Rating: 5 out of 5  
 1 vote(s)

In this tutorial, we look how to demonstrate a C#-based solution to increase the efficiency of your IP-based video surveillance system by building an ONVIF IP camera server. We will see how to implement RTSP streaming to multiple locations in C# by using prewritten Onvif-based components.

 Download source code for How to implement ONVIF IP camera service and RTSP streaming to smartphones / remote PCs in C#


In the past couple of weeks (or rather months), I became more and more interested in IP camera surveillance systems and I am thinking more and more about how to get more out of our IP-based security system. When the most of the leadership were out of the office, there was a need for streaming the camera video to multiple locations simultaneously. For this reason a C# application was built (more specifically an IP camera server) that can be used to stream the video and audio of an IP camera to remote computers or even smartphones. The clients only need a video player that supports RTSP. It can be said that this software is able to provide a sense of ONVIF IP camera service to multiple locations. I believe my solution could be useful any developers or system integrators who want to improve their security/surveillance system. However, I also trust that we can exchange further ides related to IP camera programming. So, wishing you good reading, have fun and do not hesitate to share your ideas!

Technical background - Glossary

For the sake of completeness, before describing the code lets get clarification on the most important terms and definitions related to this topic. Take a moment to study the following brief glossary to learn more about camera server services and RTSP camera streaming:

IP-based surveillance systems: Although in my previous article (How to build a C# IP camera software that enables Pan-Tilt-Zoom motion control) the IP surveillance systems was introduced briefly, this article has few sentences about it for better understanding. IP surveillance is a digitized and networked version of CCTV (Closed-circuit television). The CCTV cameras are commonly used for surveillance in many areas, but a growing branch in CCTV is IP cameras. An IP camera (that is also called ’network camera’) is a type of digital video cameras.  ’IP’ refers to the IP cameras’ characteristics that they can send and receive data over the Internet Protocol. Like a computer, an IP camera also has its own IP address, that is used to connect the device directly to a network. An IP camera connected to an IP surveillance system can be used to record video footage that will be transmitted through an IP-based network (Source [1], [2]).

IP camera / video server: In the surveillance context, an IP video server (that is also called 'IP camera server’) converts analog video signals into IP video streams. An IP video server enables to broadcast digitized video and audio from IP cameras over IP networks to multiple locations even simultaneously - at the same way that an IP camera can. As an IP video server uses IP protocols, it can stream video over any network that IP can use (via a modem for access over a phone or ISDN connection) (Source [3]).

Video streaming: The video streaming functionality makes it possible to stream video from one main site to multiple secondary locations all over the world at the same time by using a server. The video can be played out live for mobile phones and tablets, or streamed live to computers. While an average camera supports one or two clients at the same time, an IP camera server application provides a useful solution for this problem. You can connect several clients to the server at the same time and you can stream the camera video to multiple locations simultaneously.

ONVIF IP camera service: Due to an IP video / camera server connected to a camera, the video from an existing surveillance system can be converted and networked into a new IP surveillance system. This way it enables to provide IP camera service (Source [3]).

RTSP: RTSP is the acronym for Real-Time Streaming Protocol that is a network control protocol designed for use in entertainment and communications systems to control streaming media servers. The protocol is used for establishing and controlling media sessions between end points. The transmission of streaming data itself is not a task of the RTSP protocol. Most RTSP servers use the RTP (Real-time Transport Protocol) in conjunction with RTCP (Real-time Control Protocol) for media stream delivery (Source [4]).

ONVIF: In my previous article (How to build a C# IP camera software that enables Pan-Tilt-Zoom motion control)  ONVIF was covered , but take a look at the essence of this technology again. ONVIF is an open industry forum that is intended to facilitate the development and use of a global open standard for the interface of physical IP-based security products. The keystones of ONVIF are the following: standardization of communication between IP-based video devices; interoperability between IP-based security devices regardless of manufacturer; open to all companies and organizations (Source [5]).

What you need

After studying the technical background of ONVIF IP camera video streaming, take a look at the prerequisites that are needed for the implementation of your C#-based ONVIF IP camera server. Your system needs to meet the following hardware and software requirements:

Hardware requirements: A PC is essentially needed of course. You need at least one IP camera as well. (Before programming, make sure that the computer and the IP camera are connected to the same network.) To make a test broadcasting you will need an other PC or a smartphone/tablet as a client device. (An Android-based mobile phone for this purpose.)

Software requirements: To build a Console Application like mine, Visual Studio (Download link [7]) and .NET Framework 4 (Download link [8]) should be installed on your PC. You also need a software development kit that supports the ONVIF standardization. I used Ozeki Camera SDK (Download link [9]) that provides instant ONVIF components for IP camera developments. (It is important to know that you need to add the DLL file offered by the SDK to your references in Visual Studio, and you also need to change your target framework to ’.NET Framework 4.0’.) On the client device, whether it is a computer or a mobile device, a video player supporting RTSP streaming needs to be installed. For this purpose I used VLC media player (Download link [10]) .

Others: Due the IP networks’ characteristics, broadband Internet connection is also needed.

Code explanation

Since in case of this application it does not needed to display the IP camera image on the server PC, it is enough to create a Console Application. After you have created a new Visual C# Console Application in Visual Studio, let’s start the programming.

To be able to build a camera server application and stream the image of your IP camera to multiple locations in C#, you will need only one class. This is the Program.cs that will be described below.

For streaming the camera image, your application needs to be connected to the camera. This way the application can access the camera image then be able to broadcast it to the remote locations. Take a look at the methods allowing you to connect your application to the IP camera. 

The following method initializes the camera that has been declared as a private member of the class. Three arguments are used: IP address of the camera (, username (admin) and password (admin):
_camera = IPCameraFactory.GetCamera("", "admin", "admin")
With the next one you can establish connection between the camera image and the image provider object (the image provider object is used to display the camera image on the GUI):
_connector.Connect(_camera.VideoChannel, _imageProvider)
The following method can be used to receive the camera image:
And finally, use the line below to display the camera image on the GUI:
After you have created the connection successfully, let’s implement the broadcasting functionality. Concerning to the fact that this feature is based on a server-client model, there is a need for a class that is used to establish an appropriate connection between the C# Console Application server and the client. For this purpose the MyServer class is used that implements the IPCameraServer interface which helps to create your own server. During the running of the application, it can be seen ’who’ (that is which client) has been connected to the server. Now take a look at the methods and events of the _server object:
  • The start() method can be used to start the server.
  • The stop() method can be used to stop the server.
  • The server_ClientCountChange event writes the list of connected clients to the console.
The SetListenAddress method can be used to specify the IP address of the server PC and to define the port number on which you would like to monitor the incoming requests. That is, this method has two parameters: the IP address of the server ( and the port number (554):
_server.SetListenAddress("", 554)
For the sake of completeness, check out the full Program.cs class:
using System;
using Ozeki.Media.IPCamera;
using Ozeki.Media.IPCamera.Rtsp;
using Ozeki.Media.IPCamera.Server;
using Ozeki.Media.MediaHandlers;
using Ozeki.VoIP.PBX.Authentication;

namespace Onvif_IP_Camera_Server_04
    public class MyServer : IPCameraServer
        private MediaConnector _connector;
        private IIPCamera _camera;
        private IIPCameraClient _client;

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

        public event EventHandler ClientCountChange;

        public MyServer()
            _connector = new MediaConnector();
            _camera = IPCameraFactory.GetCamera("", "admin", "admin");

            if (_camera != null)

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

            var handler = ClientCountChange;
            if (handler != null)
                handler(null, new EventArgs());


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

            var handler = ClientCountChange;
            if (handler != null)
                handler(null, new EventArgs());


    class Program
        static MyServer _server = new MyServer();

        static void Main(string[] args)
            _server.SetListenAddress("", 554);
            _server.ClientCountChange += new EventHandler(server_ClientCountChange);

        static void server_ClientCountChange(object sender, EventArgs e)
            _server.ConnectedClients.ForEach(x => Console.WriteLine(x.TransportInfo.RemoteEndPoint.ToString()));
After you have implemented the program successfully, let’s run the application. The following console output will appear: 

Test broadcasting

For testing the video streaming, make sure that you have specified the appropriate arguments for your IP camera (IP address with port number, username, password) and proper parameters for the server (IP address, port number). If everything is all right, run the application and take a client device on which you have installed previously a video player that supports RTSP streaming.

Android-based Sony smartphone along with VLC media player was used in demos of this article. To connect to the server launch the VLC media player and touch the ’Open network stream’ button in the top right corner of the screen. A pop-up window will appear. Enter the correct media resource locator (MRL) into the textbox. (MRL is a URI used to uniquely identify and locate a multimedia resource.) In case of RTSP streaming you need to enter the MRL in the VLC media player as follows: 

rtsp://<camera username>:<camera password>@<server address>:<server port>

That is:


After entering the accurate MRL, touch the ’Open’ button (see the figure below):

After this, the camera image will be streamed to the mobile device and it can be seen in the VLC media player:

On the server PC the Console Application also shows that a remote client (that is connected to the same network) has been connected to the server:

Of course it is possible to connect more clients to the server. The number of clients is not limited. Due to this functionality, the camera image can be seen for those people as well who are currently out of the office - wherever they are in the World. They do not need any special camera application; a simple VLC media player (or an other one that support RTSP streaming) is enough to check out what is going on in the area that is under video surveillance. 


In this brief tutorial we looked into a smart solution on how to increase the efficiency of your IP-based video surveillance system. You could see above how simple to implement an ONVIF IP camera server application in C# by using prewritten Onvif-based components. Using this software, now your system is ready for RTSP streaming to multiple smartphones / remote PCs simultaneously. For further reading and getting the necessary software take a look at the following knowledge base that was used to compose this article:



Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Timothy Walker
Member Level: Starter
Member Status: Member
Member Since: 9/11/2014 4:18:57 AM
Country: United Kingdom

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)