How to call an Onvif IP camera from a SIP video phone in C#

This example demonstrates a simple method for how to register a SIP account to a PBX with your Windows Forms/WPF Application written in C#, that can answer the incoming calls and send the camera picture and voice to a SIP video phone automatically. 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.

How to call an Onvif IP camera from a SIP video phone using 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 Windows Forms/WPF 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://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\
SIP_Call_Camera_WF\SIP_Call_Camera_WF.sln
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
SIP_Call_Camera_WPF\SIP_Call_Camera_WPF.sln

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

The additional statements and methods of this example are the following:

ISoftPhone _softphone: Please create a softphone object from the ISoftPhone interface.
IPhoneLine _phoneLine: You should create a phone line object from the IPhoneLine interface.

_softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000): You should also initialize this softphone with the default parameters.

Please set the port range indicated by the first two parameters as the minimum port's and the maximum port's number, this is the port's interval. If there is any firewall rule which restricts the usable ports, you can set the usable port range here, which will be used during the calls. These are sample ports only in the example, but a softphone with these parameters can be used in most of the cases.

In order to communicate (and to register to the PBX) you should create a phone line. In this example the necessary SIP account is already created so with this you can establish the phone line:

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

When the application is running, the state of the phone line can change. To follow these changes, you should listen to the following change event:

_phoneLine.RegistrationStateChanged += phoneLine_RegistrationStateChanged;

When the phone line has been created, please call the RegisterPhoneLine() method to register the phone line 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);

How to listen to incoming calls?
If you would like to be notified when there is an incoming call, you should set an event at the initialization of the softphone for this purpose:

_softphone.IncomingCall += softphone_IncomingCall;

When the event indicates that there is a call waiting to be received, it sets the call object, and subscribes to the call's events with help of the previously written method. Then you should call the ConnectToCall() method and answer the call:

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

You should attach the media handlers to the call. With the help of the connector object 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);
}
	

Finally the call will be completed. Then you need to unsubscribe from the event. This works similarly to the subscriber (with the "-=" operator) and "disconnector" method, which close the connection between the two media handlers with the Disconnect() method.

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

Calling an Onvif camera from a SIP video phone example in C#

Windows Form WPF  

Windows forms version

Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;
using Ozeki.Media;
using Ozeki.VoIP;
using Ozeki.Camera;

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_Phoneline.Text = ex.Message;
            }
        }

        private void phoneLine_RegistrationStateChanged(object sender, RegistrationStateChangedArgs e)
        {
            InvokeGuiThread(() => label_Phoneline.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_Call.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=new IPCamera("192.168.112.109:8080","user","qwe123");
            _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);
        }
    }
}
		
Code 1 - Calling an Onvif camera from a SIP video phone example 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.

GUI

graphical user interface
Figure 1 - The graphical user interface of your application

After the successful implementation of the functions and the GUI elements, the application will work properly. Pressing the connect button will load in the image of the IP camera device connected to your PC into the panel that you can see on the picture. Besides this you can register to a PBX and receive incoming calls to send the picture and voice of the camera.

Below you can find the code that belongs to the interface of the previously presented application. With the help of this section your Windows Forms Application will be able to work properly.

Form1.Designer.cs

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_Phoneline = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.button_SIPRegister = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.label_Call = new System.Windows.Forms.Label();
            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, 95);
            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, 124);
            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_Call);
            this.groupBox13.Controls.Add(this.label2);
            this.groupBox13.Controls.Add(this.label_Phoneline);
            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, 95);
            this.groupBox13.TabIndex = 4;
            this.groupBox13.TabStop = false;
            this.groupBox13.Text = "SIP account settings";
            // 
            // label_Phoneline
            // 
            this.label_Phoneline.AutoSize = true;
            this.label_Phoneline.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label_Phoneline.Location = new System.Drawing.Point(85, 50);
            this.label_Phoneline.Name = "label_Phoneline";
            this.label_Phoneline.Size = new System.Drawing.Size(2, 15);
            this.label_Phoneline.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);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(10, 74);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(53, 13);
            this.label2.TabIndex = 15;
            this.label2.Text = "Call state:";
            // 
            // label_Call
            // 
            this.label_Call.AutoSize = true;
            this.label_Call.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label_Call.Location = new System.Drawing.Point(85, 74);
            this.label_Call.Name = "label_Call";
            this.label_Call.Size = new System.Drawing.Size(2, 15);
            this.label_Call.TabIndex = 16;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(354, 366);
            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_Phoneline;
        private System.Windows.Forms.Label label_Call;
        private System.Windows.Forms.Label label2;

    }
}
		
Code 2 - GUI example in C#

WPF version

MainWindow.xaml.cs

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

namespace OnvifIpcameraSipVideoCall01Wpf
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        private IIPCamera _camera;
        private DrawingImageProvider _drawingImageProvider;
        private MediaConnector _connector;
        private ISoftPhone _softphone;
        private IPhoneLine _phoneLine;
        private IPhoneCall _call;
        private PhoneCallAudioSender _audioSender;
        private PhoneCallVideoSender _videoSender;

        public MainWindow()
        {
            InitializeComponent();

            _drawingImageProvider = new DrawingImageProvider();
            _softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);
            _softphone.IncomingCall += softphone_IncomingCall;
            _connector = new MediaConnector();
            _videoSender = new PhoneCallVideoSender();
            _audioSender = new PhoneCallAudioSender();
            videoViewer.SetImageProvider(_drawingImageProvider);
        }

        private void Connect_Click(object sender, RoutedEventArgs e)
        {
            _camera = new IPCamera("192.168.112.109:8080", "user", "qwe123");

            _connector.Connect(_camera.VideoChannel, _drawingImageProvider);
            videoViewer.Start();
            _camera.Start();
        }

        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_Phoneline.Content = ex.Message;
            }
        }

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

        private void softphone_IncomingCall(object sender, VoIPEventArgs 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_Call.Content = 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_SIPRegister_Click(object sender, EventArgs e)
        {
            Register(true, "885", "885", "885", "885", "192.168.115.100");
        }

        private void InvokeGuiThread(Action action)
        {
            Dispatcher.BeginInvoke(action);
        }
    }
}
		
Code 1 - Calling an Onvif camera from a SIP video phone example 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.

GUI

graphical user interface
Figure 1 - The graphical user interface of your application

After the successful implementation of the functions and the GUI elements, the application will work properly. Pressing the connect button will load in the image of the IP camera device connected to your PC into the panel that you can see on the picture. Besides this you can register to a PBX and receive incoming calls to send the picture and voice of the camera.

Below you can find the code that belongs to the interface of the previously presented application. With the help of this section your WPF Application will be able to work properly.

MainWindow.xaml

<Window x:Class="OnvifIpcameraSipVideoCall01Wpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:Ozeki.Media;assembly=OzekiSDK"
    Title="Onvif IP Camera Called by SIP Video Phone" Height="411" Width="343" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen">
<Grid>
    <GroupBox Header="Live camera" HorizontalAlignment="Left" Margin="12,141,0,0" VerticalAlignment="Top" Height="226" Width="308">
        <Grid HorizontalAlignment="Left" Height="204" VerticalAlignment="Top" Width="296">
            <controls:VideoViewerWPF Name="videoViewer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Black"/>
        </Grid>
    </GroupBox>
    <Button Content="Connect" HorizontalAlignment="Left" Margin="10,21,0,0" VerticalAlignment="Top" Width="75" Click="Connect_Click"/>
    <GroupBox Header="SIP account settings" HorizontalAlignment="Left" Margin="92,12,0,0" VerticalAlignment="Top" Height="124" Width="226">
        <Grid HorizontalAlignment="Left" Height="102" VerticalAlignment="Top" Width="216" Margin="0,0,-2,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <Label Content="Status:" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1"/>
            <Label x:Name="label_Phoneline" Content="" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="1"/>
            <Label Content="Call state:" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="2"/>
            <Label x:Name="label_Call" Content="" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="2"/>
            <Button Content="Register" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Click="button_SIPRegister_Click"/>
        </Grid>
    </GroupBox>
</Grid>
</Window>
		
Code 2 - GUI example in C#

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 and the System.Drawing.dll to the references of the solution.
    • Please import the missing classes.
  3. The registration was unsuccessful. Why?

    • Please make sure your PBX is configured properly.
       You should check whether the dialed number is valid.
    • Please make sure your registration data are valid.

More information