Ozeki Camera Recorder Stat

How to detect if a camera goes offline, and how to make an alarm phone call using prerecorded voice in C#

This example demonstrates how you can connect your Windows Forms or WPF Application written in C# to an IP camera with motion detector. On this webpage you can also find detailed information on how you can register your program to a PBX and make an alarm phone call using prerecorded voice if the camera goes offline. 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 detect if a camera goes offline, and how to make an alarm phone call with prerecorded voice 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. Please visit the How to handle alarms, by making a VoIP phone call article before you begin to study this function.

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: http://www.camera-sdk.com/p_13-download-onvif-standard-ozeki-camera-sdk-for-webcam-and-ip-camera-developments-onvif.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\

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

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

First of all, please create a WaveStreamPlayback object and initialize it with a .wav file which is available at your project bin/debug folder location:

private WaveStreamPlayback _playback = new WaveStreamPlayback("alarm.wav");

You should subscribe for the camera ErrorOccured event:

_camera.CameraErrorOccured += _camera_CameraErrorOccured;

If the camera error is ConnectionLost, please make a call as in the previous example:

if(e.Error == IPCameraError.ConnectionLost)

Finally, if the call has been answered please play the prerecorded .wav file:

if (e.State == CallState.Answered)

Detecting if a camera goes offline and making an alarm phone call using prerecorded voice example in C#

Windows Form WPF  


	using System;
	using System.Drawing;
	using System.Windows.Forms;
	using Ozeki.Media;
	using Ozeki.Camera;
	using Ozeki.VoIP;
	namespace OnvifIPCameraMotionDetection12
    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 WaveStreamPlayback _playback;

        public Form1()
            _softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);
            _connector = new MediaConnector();
            _audioSender = new PhoneCallAudioSender();
            _playback = new WaveStreamPlayback("alarm.wav");
            _imageProvider = new DrawingImageProvider();

        private void SetVideoViewer()
            _videoViewerWf = new VideoViewerWF
                Size = new Size(260, 180),
                BackColor = Color.Black,
                TabStop = false,
                FlipMode = FlipMode.None,
                Location = new Point(35, 25),
                Name = "_videoViewerWf"

        //Registering the account to a PBX
        private void Register(bool registrationRequired, string displayName, string userName, string authenticationId, string registerPassword, string domainHost)
            var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost);
            _phoneLine = _softphone.CreatePhoneLine(account);
            _phoneLine.RegistrationStateChanged += phoneLine_RegistrationStateChanged;

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

        private void _camera_CameraStateChanged(object sender, CameraStateEventArgs e)
            InvokeGuiThread(() => label_Camera.Text = e.State.ToString());

        private void _camera_CameraErrorOccured(object sender, CameraErrorEventArgs e)
            InvokeGuiThread(() => label_Error.Text = e.Error.ToString());

            if (e.Error == IPCameraError.ConnectionLost)

        private void CreateCall()
            if (_phoneLine == null) return;
            var dial = new DialParameters("886") { CallType = CallType.AudioVideo };

            _call = _softphone.CreateCallObject(_phoneLine, dial);

            _call.CallStateChanged += call_CallStateChanged;
            _connector.Connect(_playback, _audioSender);

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

            if (e.State == CallState.Answered)

            if (e.State != CallState.Completed && e.State != CallState.Busy) return;
            if (_call == null) return;
            _call.CallStateChanged -= call_CallStateChanged;
            _connector.Disconnect(_playback, _audioSender);
            _call = null;

        // Connecting the camera video channel to the image provider and starting it
        private void button_Connect_Click(object sender, EventArgs e)
            _camera=new IPCamera("","user","qwe123");

            _camera.CameraStateChanged += _camera_CameraStateChanged;
            _camera.CameraErrorOccurred += _camera_CameraErrorOccured;
            _connector.Connect(_camera.VideoChannel, _imageProvider);

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

        private void button_Disconnect_Click(object sender, EventArgs e)
            if (_camera == null) return;
            _camera = null;

        private void InvokeGuiThread(Action action)

Code 1 - Detecting if a camera goes offline and making an alarm phone call using prerecorded voice 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.


The Graphical User Interface of an application for detecting if a camera goes offline in C#
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. When the motion detector detects motion, the motion label's text will become true. If the SIP account registration was successful the program will call an alarm monitoring center.

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.


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

        protected override void Dispose(bool disposing)
            if (disposing && (components != null))

        private void InitializeComponent()
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.button_Disconnect = new System.Windows.Forms.Button();
            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.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label_Camera = new System.Windows.Forms.Label();
            this.label_Error = new System.Windows.Forms.Label();
            this.label_Call = new System.Windows.Forms.Label();
            // groupBox1
            this.groupBox1.Location = new System.Drawing.Point(10, 10);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(156, 104);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Connect";
            // button_Disconnect
            this.button_Disconnect.Location = new System.Drawing.Point(80, 19);
            this.button_Disconnect.Name = "button_Disconnect";
            this.button_Disconnect.Size = new System.Drawing.Size(70, 23);
            this.button_Disconnect.TabIndex = 7;
            this.button_Disconnect.Text = "Disconnect";
            this.button_Disconnect.UseVisualStyleBackColor = true;
            this.button_Disconnect.Click += new System.EventHandler(this.button_Disconnect_Click);
            // 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(6, 19);
            this.button_Connect.Name = "button_Connect";
            this.button_Connect.Size = new System.Drawing.Size(60, 23);
            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, 120);
            this.CameraBox.Name = "CameraBox";
            this.CameraBox.Size = new System.Drawing.Size(335, 220);
            this.CameraBox.TabIndex = 3;
            this.CameraBox.TabStop = false;
            this.CameraBox.Text = "Live camera ";
            // groupBox13
            this.groupBox13.Location = new System.Drawing.Point(172, 10);
            this.groupBox13.Name = "groupBox13";
            this.groupBox13.Size = new System.Drawing.Size(173, 104);
            this.groupBox13.TabIndex = 4;
            this.groupBox13.TabStop = false;
            this.groupBox13.Text = "SIP account settings";
            // label_Phoneline
            this.label_Phoneline.AutoSize = true;
            this.label_Phoneline.Location = new System.Drawing.Point(48, 55);
            this.label_Phoneline.Name = "label_Phoneline";
            this.label_Phoneline.Size = new System.Drawing.Size(0, 13);
            this.label_Phoneline.TabIndex = 14;
            // label1
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(7, 55);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(35, 13);
            this.label1.TabIndex = 13;
            this.label1.Text = "State:";
            // 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(3, 55);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(72, 13);
            this.label2.TabIndex = 8;
            this.label2.Text = "Camera state:";
            // label3
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(3, 79);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(70, 13);
            this.label3.TabIndex = 9;
            this.label3.Text = "Camera error:";
            // label4
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(7, 79);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(53, 13);
            this.label4.TabIndex = 15;
            this.label4.Text = "Call state:";
            // label_Camera
            this.label_Camera.AutoSize = true;
            this.label_Camera.Location = new System.Drawing.Point(80, 55);
            this.label_Camera.Name = "label_Camera";
            this.label_Camera.Size = new System.Drawing.Size(0, 13);
            this.label_Camera.TabIndex = 10;
            // label_Error
            this.label_Error.AutoSize = true;
            this.label_Error.Location = new System.Drawing.Point(80, 79);
            this.label_Error.Name = "label_Error";
            this.label_Error.Size = new System.Drawing.Size(0, 13);
            this.label_Error.TabIndex = 11;
            // label_Call
            this.label_Call.AutoSize = true;
            this.label_Call.Location = new System.Drawing.Point(73, 79);
            this.label_Call.Name = "label_Call";
            this.label_Call.Size = new System.Drawing.Size(0, 13);
            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, 351);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Alarm call if camera goes offline";


        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.Button button_Disconnect;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label_Error;
        private System.Windows.Forms.Label label_Camera;
        private System.Windows.Forms.Label label_Call;
        private System.Windows.Forms.Label label4;

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


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 System.Drawing.dll and the OzekiSDK.dll to the references of the solution.
    • Please import the missing classes.
    • Make sure that you have a file named "alarm.wav" on the proper location.
  3. My audio is not played. Why?

    Make sure you added a .wav file.

  4. At camera error I did not receive the call. Why?

    • Please make sure that your PBX is configured properly.
    • You should check whether the dialed number is valid.
    • Please make sure your registration data are valid.
    • You should make sure that the camera is not covered and whether the room is too dark.