.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to build an Onvif IP camera to SIP video call gateway in C#.NET

Posted By:Simon Robert VoIP       Posted Date: October 03, 2014    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

In this tutorial I will show you how to build an Onvif IP camera to SIP video call gateway in C#.NET that can be used to check out your camera image through a simple video phone call - even remotely.
 

List of contents

1. Introduction

1.1. Example for field of use

2. The Onvif specification

3. The implementation of an Onvif IP camera to SIP video call gateway in C#

3.1. Before coding

3.2. Implementing the C# application

3.3. Implementing the GUI

4. Conclusion

5. Reviewed literature

 

1. Introduction

In addition to the popularity of VoIP (Voice over Internet Protocol) communication networks, IP-based security (or surveillance) systems are becoming more widely used as well. There is a substantial gain of benefits when switching from an analog to an IP-based system. It ensures the followings: unification of existing security systems; integration of devices into existing networks; enhanced service standards; leveraging of existing network assets.

If you want to make your security system more effective, there is a great way to achieve this goal: connect it to your VoIP telecommunication network. This allows you to view the camera image even remotely through a video phone call. And its reverse is also possible: you can make alarm calls to a SIP video phone and broadcast the camera image.

In this article I will demonstrate the former one. That is, I will show how to create a C# software that enables you to make a video phone call, in which the initiating party is you (and your physical or software telephone that supports video calling) and the endpoint is your IP camera that sends you the live camera image:

1.1. Example for field of use

Let's see a simple case as an example. Imagine that you are out of the office because of a conference. You are in another city. You are drinking your coffee between two presentations, when a telephone alarm arrives from your security alarm system to your mobile phone. Knowing that some pets have generated alarms earlier, you want to make sure whether there is a real problem or not. In this situation it is a very convenient option to make a video call by using your smartphone or your laptop during which you can view what your security cameras show. It sounds good, right? So let's see how it can be implemented.


2. The Onvif specification

In the title of this article, the 'Onvif' phrase may sounds new, as - although it first appeared in 2008 - it just started to spread in the past few years. 'Onvif' is the abbreviation for Open Network Video Interface Forum that is an organization started on 25 November, 2008. It has been established by Axis Communications, Bosch Security Systems and Sony with the goal to facilitate the development and use of a global open standard for the interface of IP-based physical security products.

There are 23 full members of Onvif - highlighting some of them without completeness: Bosch, Canon, Honeywell, Huawei, LG, Panasonic, Siemens, Sony, etc. - offering IP cameras and other security devices that are Onvif-compliant to guarantee full interoperability with other branded network video solutions for enhanced system integration and scalability:

Concerning that Onvif specification aims to standardize the network interface of network video products, it defines a common protocol for the exchange of information between network video devices. It covers areas such as IP configuration, device discovery, device management, media configuration, real time viewing, event handling, PTZ camera control, video analytics, security, etc.


3. The implementation of an Onvif IP camera to SIP video call gateway in C#


3.1. Before coding

Before starting the implementation of this C# application, make sure that your system complies with the minimum requirements. You need to have a VoIP phone system (IP PBX) and some IP cameras connected to the same network. It assumes that you have broadband Internet connection as well. For programming you need a Visual Studio (www.microsoft.com) and an Onvif-compliant camera SDK (www.camera-sdk.com). (At this point I would like to draw your attention to make sure that .NET Framework 4.0 has been installed on your PC, and the SDK's DLL file has been added to your references.) If all the necessary hardware and software are ready to use, start the development by creating a new project. You will need a new Windows Forms Application.

I will demonstrate my solution in two parts. First of all, you can study the Form1.cs that presents all functions of the application that will be used to connect to your camera and to implement the IP camera to SIP video call gateway. After this you can study the Form1.Designer.cs that contains all the GUI elements.


3.2. Implementing the C# application

In order to be able to connect to your camera in C# you need to use the classes of the SDK. Their objects have been declared at the beginning of the Form1 class. After initializing these objects, you need to call the SetVideoViewer() method and to add the VideoViewerFM object to the control collection of the panel. You can see below the statements and methods that are used in this project:

The following method initializes the camera that has been declared as a private member. The arguments that are used as follows: IP address of the camera, username, password:

_camera = IPCamera.GetCamera("192.168.115.198:8080", "admin", "admin")

 

This method allows you to create connection between the IP camera image and the image provider object that displays the image on the user interface:

_connector.Connect(_camera.VideoChannel, _imageProvider)


The Start() method is used to get the IP camera image from your camera:

_camera.Start()


You also need to start the viewer in order to display the camera image on the user interface:

_videoViewerWF1.Start()


You need to create a softphone object from the ISoftPhone interface:

ISoftPhone _softphone


You need to create a phone line object as well from the IPhoneLine interface:

IPhoneLine _phoneLine

 

You also need to initialize this softphone by using the default parameters. (You need to set the port range, indicated by the first two paramerters, as the minimum port number and maximum port number. This is the port's interval:

_softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000)

 

To be able to communicate (and to register to your phone system) you need to create a phone line. As the necessary SIP account has already been created you can establish the phone line using that:

var account = new SIPAccount(registrationRequired, displayName, userName,
authenticationId, registerPassword, domainHost);
_phoneLine = _softphone.CreatePhoneLine(account);

Since the state of the phone line can change during the running of the application, it is worth to follow these changes. For this purpose you need to listen to this change event:

_phoneLine.RegistrationStateChanged += phoneLine_RegistrationStateChanged;


When the phone line has been created, you need to call the RegisterPhoneLine() method to register it to the softphone. If the registration is set to 'required', this method will also send the SIP REGISTER command. You only need to use the following line:

_softphone.RegisterPhoneLine(_phoneLine);


In order to listen to the incoming calls, first of all you need to be notified when there is an incoming call. For this purpose, you need to set an event at the initialization of the softphone:

_softphone.IncomingCall += softphone_IncomingCall;


When the event notifies whether there is a call waiting to be accepted, it sets the call object, and subscribes to the events of the call by using the previously written method. After this, you can call the ConnectToCall() method and answer the call:

_call = e.Item;
_call.CallStateChanged += call_CallStateChanged;
ConnectToCall();
_call.Answer();


You need to connect the media handlers to the call by using the connector object. This way, the _camera.VideoChannel will be connected to the _videoSender, and the _camera.AudioChannel will be connected to the _audioSender:

public void ConnectToCall()
{
    _videoSender.AttachToCall(_call);
    _audioSender.AttachToCall(_call);
    _connector.Connect(_camera.VideoChannel, _videoSender);
    _connector.Connect(_camera.AudioChannel, _audioSender);
}

 

And finally the call will be completed. At this point, you need to unsubscribe from the event:

_call.CallStateChanged -= call_CallStateChanged;
_connector.Disconnect(_camera.VideoChannel, _videoSender);
_connector.Disconnect(_camera.AudioChannel, _audioSender);

Below you can see the full Form1.cs class, that contains all functions of the application that can be used to connect to your camera and to implement the IP camera to SIP video call gateway:

using System;
using System.Drawing;
using System.Windows.Forms;
using Ozeki.Media.MediaHandlers;
using Ozeki.Media.MediaHandlers.IPCamera;
using Ozeki.Media.MediaHandlers.Video;
using Ozeki.Media.Video;
using Ozeki.Media.Video.Controls;
using Ozeki.Media.IPCamera;
using Ozeki.VoIP;
using Ozeki.VoIP.SDK;

namespace OnvifIpcameraSipVideoCall01
{
    public partial class Form1 : Form
    {
        private IIPCamera _camera;
        private DrawingImageProvider _imageProvider;
        private MediaConnector _connector;
        private VideoViewerWF _videoViewerWf;
        private ISoftPhone _softphone;
        private IPhoneLine _phoneLine;
        private IPhoneCall _call;
        private PhoneCallAudioSender _audioSender;
        private PhoneCallVideoSender _videoSender;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);
            _softphone.IncomingCall += softphone_IncomingCall;
            _connector = new MediaConnector();
            _videoSender = new PhoneCallVideoSender();
            _audioSender = new PhoneCallAudioSender();
            _imageProvider = new DrawingImageProvider();
            _videoViewerWf = new VideoViewerWF();
            SetVideoViewer();
        }

        private void SetVideoViewer()
        {
            CameraBox.Controls.Add(_videoViewerWf);
            _videoViewerWf.Size = new Size(260, 180);
            _videoViewerWf.BackColor = Color.Black;
            _videoViewerWf.TabStop = false;
            _videoViewerWf.FlipMode = FlipMode.None;
            _videoViewerWf.Location = new Point(35, 30);
            _videoViewerWf.Name = "_videoViewerWf";
        }

        private void Register(bool registrationRequired, string displayName, string userName, string authenticationId, string registerPassword, string domainHost)
        {
            try
            {
                var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost);
                _phoneLine = _softphone.CreatePhoneLine(account);
                _phoneLine.RegistrationStateChanged += phoneLine_RegistrationStateChanged;
                _softphone.RegisterPhoneLine(_phoneLine);
            }
            catch (Exception ex)
            {
                label_Status.Text = ex.Message;
            }
        }

        private void phoneLine_RegistrationStateChanged(object sender, RegistrationStateChangedArgs e)
        {
            InvokeGuiThread(() => label_Status.Text = e.State.ToString());
        }

        private void softphone_IncomingCall(object sender, VoIPEventArgs<IPhoneCall> e)
        {
            if (_camera != null)
            {
                _call = e.Item;
                _call.CallStateChanged += call_CallStateChanged;
                ConnectToCall();
                _call.Answer();
            }
        }

        private void ConnectToCall()
        {
            _videoSender.AttachToCall(_call);
            _audioSender.AttachToCall(_call);
            _connector.Connect(_camera.VideoChannel, _videoSender);
            _connector.Connect(_camera.AudioChannel, _audioSender);
        }

        private void call_CallStateChanged(object sender, CallStateChangedArgs e)
        {
            InvokeGuiThread(() => label_Status.Text = e.State.ToString());

            if (e.State == CallState.Completed)
                if (_call != null)
                {
                    _call.CallStateChanged -= call_CallStateChanged;
                    _connector.Disconnect(_camera.VideoChannel, _videoSender);
                    _connector.Disconnect(_camera.AudioChannel, _audioSender);
                }
        }

        private void button_Connect_Click(object sender, EventArgs e)
        {
            _camera = IPCameraFactory.GetCamera("192.168.115.175:8080", "admin", "admin");
            _connector.Connect(_camera.VideoChannel, _imageProvider);
            _videoViewerWf.SetImageProvider(_imageProvider);
            _videoViewerWf.Start();
            _camera.Start();
        }

        private void button_SIPRegister_Click(object sender, EventArgs e)
        {
            Register(true, "885", "885", "885", "885", "192.168.115.100");
        }

        private void InvokeGuiThread(Action action)
        {
            BeginInvoke(action);
        }
    }
}

3.3. Implementing the GUI

It is time to implement the elements of the Graphical User Interface. The following code spippet demonstrates the Form1.Designer.cs class that contains the full source code related to the GUI:

namespace OnvifIpcameraSipVideoCall01
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.button_Connect = new System.Windows.Forms.Button();
            this.CameraBox = new System.Windows.Forms.GroupBox();
            this.groupBox13 = new System.Windows.Forms.GroupBox();
            this.label_Status = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.button_SIPRegister = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.groupBox13.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.button_Connect);
            this.groupBox1.Location = new System.Drawing.Point(10, 10);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(110, 70);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Connect";
            // 
            // button_Connect
            // 
            this.button_Connect.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.button_Connect.ForeColor = System.Drawing.Color.Black;
            this.button_Connect.Location = new System.Drawing.Point(10, 25);
            this.button_Connect.Name = "button_Connect";
            this.button_Connect.Size = new System.Drawing.Size(75, 25);
            this.button_Connect.TabIndex = 6;
            this.button_Connect.Text = "Connect";
            this.button_Connect.UseVisualStyleBackColor = true;
            this.button_Connect.Click += new System.EventHandler(this.button_Connect_Click);
            // 
            // CameraBox
            // 
            this.CameraBox.Location = new System.Drawing.Point(10, 90);
            this.CameraBox.Name = "CameraBox";
            this.CameraBox.Size = new System.Drawing.Size(330, 230);
            this.CameraBox.TabIndex = 3;
            this.CameraBox.TabStop = false;
            this.CameraBox.Text = "Live camera ";
            // 
            // groupBox13
            // 
            this.groupBox13.Controls.Add(this.label_Status);
            this.groupBox13.Controls.Add(this.label1);
            this.groupBox13.Controls.Add(this.button_SIPRegister);
            this.groupBox13.Location = new System.Drawing.Point(130, 10);
            this.groupBox13.Name = "groupBox13";
            this.groupBox13.Size = new System.Drawing.Size(210, 70);
            this.groupBox13.TabIndex = 4;
            this.groupBox13.TabStop = false;
            this.groupBox13.Text = "SIP account settings";
            // 
            // label_Status
            // 
            this.label_Status.AutoSize = true;
            this.label_Status.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label_Status.Location = new System.Drawing.Point(85, 50);
            this.label_Status.Name = "label_Status";
            this.label_Status.Size = new System.Drawing.Size(2, 15);
            this.label_Status.TabIndex = 14;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(10, 50);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(40, 13);
            this.label1.TabIndex = 13;
            this.label1.Text = "Status:";
            // 
            // button_SIPRegister
            // 
            this.button_SIPRegister.Location = new System.Drawing.Point(10, 20);
            this.button_SIPRegister.Name = "button_SIPRegister";
            this.button_SIPRegister.Size = new System.Drawing.Size(75, 23);
            this.button_SIPRegister.TabIndex = 12;
            this.button_SIPRegister.Text = "Register";
            this.button_SIPRegister.UseVisualStyleBackColor = true;
            this.button_SIPRegister.Click += new System.EventHandler(this.button_SIPRegister_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(354, 329);
            this.Controls.Add(this.groupBox13);
            this.Controls.Add(this.CameraBox);
            this.Controls.Add(this.groupBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Onvif IP Camera Called by SIP Video Phone";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox13.ResumeLayout(false);
            this.groupBox13.PerformLayout();
            this.ResumeLayout(false);
        }

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Button button_Connect;
        private System.Windows.Forms.GroupBox CameraBox;
        private System.Windows.Forms.GroupBox groupBox13;
        private System.Windows.Forms.Button button_SIPRegister;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label_Status;

    }
}

After you have successfully implemented the functions and the GUI elements, take a closer look at the user interface (the following figure illustrates the GUI). You can connect to the camera by clicking on the 'Connect' button:



After this the camera image will be displayed automatically in the 'Live camera' section. Now let your new C# application be able to receive your phone calls. For this purpose you need to register to your phone system by clicking on the 'Register' button. After the registration the 'RegistrationSucceeded' message will appear:



To test the SIP video call, grap a phone (a physical one or a softphone) that is connected to your phone system, and dial the extension (that is the phone number) of the camera that has been specified during the SIP account settings. Your new application will accept the call automatically and send the camera image as a video image. You can see below that the camera image can be seen in the softphone:



In the meantime it can be seen that your new C# application also shows that the current status is 'InCall':


 

4. Conclusion

In this tutorial I tried to give you a comprehensive guide on how to build an Onvif IP camera to SIP video call gateway in C#.NET. If you use my source code you can easily build a C# application that allows you to check out your camera image through a simple video phone call. To implement this application I have used a Camera SDK that facilitates the whole process by providing prewritten components based on Onvif technology.

 

5. Reviewed literature

About Onvif at the Wikipedia: http://en.wikipedia.org/wiki/Open_Network_Video_Interface_Forum

About Onvif at its official website: http://www.onvif.org/

About the usage of the Camera SDK: http://www.camera-sdk.com/

About the Bria softphone: http://www.counterpath.com/bria


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend