How to handle alarms, by sending a Contact ID message to an alarm monitoring center 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 more information on how to register your program to a PBX and send a Contact ID message to an alarm monitoring center. 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 handle alarms, by sending a Contact ID message to an alarm monitoring center 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:
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\\Examples\Other\
WPF version: C:\Program Files\Ozeki\Ozeki SDK\\Examples\Other\

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

You should also visit the How to setup viewer side motion detection and the previous How to handle alarms, by making a VoIP phone call to an alarm monitoring center with text to speech article before you begin to study this function. You can find a detailed guide about the Contact ID protocol at the Contact ID protocol page. The official documentation of the Contact ID protocol can also be found here: Contact ID standards.

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

private ContactIdHandler _contactIdHandler: ContactIdHandler object

If you do not use a codec applying lossless compression, the DTMF frequencies may be distorted during the communication with the Contact ID protocol. Therefore you should use PCMA or PCMU codecs. In this program we call the EnablePCMA() method to use only the PCMA codec:


It is also necessary to create a ContactIdHandler type object that needs to subscribe to the ContactIdSendFailed and ContactIdSendSuccessful events. When the ContactIdSendFailed event occurs, it means that the sending of the alarm (that is the contact ID) is failed. For instance, the kissoff signal is not received. When the ContactIdSendSuccessful event occurs, it means that the sending of the alarm has been successful. In this event the call will be hung up.

When the call is established the _contactIdHandler object should be attached to the call and connected to the _audioSender and _audioReceiver objects with the help of the _connector. After this you should call the Start() method:

_connector.Connect(_contactIdHandler, _audioSender);
_connector.Connect(_audioReceiver, _contactIdHandler);

In the InitializeContactIdHandler() method you should subscribe for the events and call the SendNotification() method:

_contactIdHandler.SendNotification(886, ContactIdEventQualifier.NewEvent, 130, 1, 22);
// send the Contact ID alarm notification

This example program will send DTMF Contact ID sequences according to their function: account number, event qualifier, event code, partition number, zone number.

When the call is finished you should disconnect the established connections and stop the contactIdHandler by calling the Stop() method:

_connector.Disconnect(_contactIdHandler, _audioSender);
_connector.Disconnect(_audioReceiver, _contactIdHandler);
_contactIdHandler.ContactIdSendSuccessful -=_contactIdHandler_ContactIdSendSuccessful;
_contactIdHandler.ContactIdSendFailed -= _contactIdHandler_ContactIdSendFailed;

If you wish to receive Contact ID messages, please study the tutorial that describes. how to receive a Contact ID alert in C#.

Sending a Contact ID message to an alarm monitoring center 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 OnvifIPCameraMotionDetection11
	    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 PhoneCallAudioReceiver _audioReceiver;
	        private MotionDetector _motionDetector;
	        private ContactIdHandler _contactIdHandler;
	        public Form1()
	            _softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);
	            _connector = new MediaConnector();
	            _audioSender = new PhoneCallAudioSender();
	            _audioReceiver = new PhoneCallAudioReceiver();
	            _motionDetector = new MotionDetector();
	            _motionDetector.MotionDetection += _motionDetector_MotionDetection;
	            _contactIdHandler = new ContactIdHandler();
	            _imageProvider = new DrawingImageProvider();
	        private void Form1_Load(object sender, EventArgs e)
	        private void SetVideoViewer()
	            _videoViewerWf = new VideoViewerWF
	                Size = new Size(260, 180),
	                BackColor = Color.Black,
	                TabStop = false,
	                FlipMode = FlipMode.None,
	                Location = new Point(50, 30),
	                Name = "_videoViewerWf"
	        private void EnablePCMA()
	            foreach (var s in _softphone.Codecs)
	            _softphone.EnableCodec(8); //Only the PCMA codec is enabled
	        private void InitializeContactIdHandler()
	            _contactIdHandler.SendNotification(886, ContactIdEventQualifier.NewEvent, 130, 1, 22); // Sending the Contact ID alarm notification
	            _contactIdHandler.ContactIdSendSuccessful += _contactIdHandler_ContactIdSendSuccessful;
	            _contactIdHandler.ContactIdSendFailed += _contactIdHandler_ContactIdSendFailed;
	        private void _contactIdHandler_ContactIdSendSuccessful(object sender, EventArgs e)
	            InvokeGuiThread(() =>
	                label_ContactID.Text = "Success!";
	        private void _contactIdHandler_ContactIdSendFailed(object sender, EventArgs e)
	            InvokeGuiThread(() =>
	                label_ContactID.Text = "Failed!";
	        //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;
	            catch (Exception ex)
	                label_Status.Text = ex.Message;
	        private void phoneLine_RegistrationStateChanged(object sender, RegistrationStateChangedArgs e)
	            InvokeGuiThread(() =>
	                label_Status.Text = e.State.ToString();
	            if (e.State == RegState.RegistrationSucceeded)
	        private void _motionDetector_MotionDetection(object sender, MotionDetectionEvent e)
	            InvokeGuiThread(() =>
	                label_Motion.Text = e.Detection.ToString();
	            if (e.Detection && _call == null)
	        private void CreateCall()
	            if (_phoneLine == null) return;
	            var numberToDial = "886";
	            _call = _softphone.CreateCallObject(_phoneLine, numberToDial);
	            _call.CallStateChanged += call_CallStateChanged;
	            _call.Start(); //SIP INVITE request has been sent
	        private void call_CallStateChanged(object sender, CallStateChangedArgs e)
	            InvokeGuiThread(() =>
	                label_Status.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;
	            _call = null;
	            _connector.Disconnect(_contactIdHandler, _audioSender);
	            _connector.Disconnect(_audioReceiver, _contactIdHandler);
	            _contactIdHandler.ContactIdSendSuccessful -= _contactIdHandler_ContactIdSendSuccessful;
	            _contactIdHandler.ContactIdSendFailed -= _contactIdHandler_ContactIdSendFailed;
	            _contactIdHandler.Stop(); //Stopping the contactIdHandler                  
	        private void SetupDevices()
	            //Attaching the phoneCallAudioSender and the phoneCallAudioReceiver to the call
	            _contactIdHandler.Start(); //Starting the contactIdHandler
	            _connector.Connect(_contactIdHandler, _audioSender); //Connecting the contactIdHandler to the phoneCallAudioSender object
	            _connector.Connect(_audioReceiver, _contactIdHandler); //Connecting the phoneCallAudioReceiver to the contactIdHandler object 
	        //Connecting the camera video channel to the image provider and start
	        private void button_Connect_Click(object sender, EventArgs e)
	            _camera = new IPCamera("", "user", "qwe123");
	            _connector.Connect(_camera.VideoChannel, _motionDetector);
	            _connector.Connect(_motionDetector, _imageProvider);
	        private void button_SIPRegister_Click(object sender, EventArgs e)
	            Register(true, "885", "885", "885", "885", "");
	        private void InvokeGuiThread(Action action)
	        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
	            if (_call != null)

Code 1 - Sending a Contact ID message to an alarm monitoring center 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 that handles alarms by sending a Contact ID message to an alarm monitoring center 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. By pressing the Register button you can register the SIP account to a PBX. If the registration was successful and motion was detected, the program sends a Contact ID message to an alarm monitoring center. It will be indicated in label's text.

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 OnvifIPCameraMotionDetection11
	    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_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.label2 = new System.Windows.Forms.Label();
	            this.label_Motion = new System.Windows.Forms.Label();
	            this.label3 = new System.Windows.Forms.Label();
	            this.label_ContactID = 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(125, 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(20, 25);
	            this.button_Connect.Name = "button_Connect";
	            this.button_Connect.Size = new System.Drawing.Size(80, 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, 85);
	            this.CameraBox.Name = "CameraBox";
	            this.CameraBox.Size = new System.Drawing.Size(360, 230);
	            this.CameraBox.TabIndex = 3;
	            this.CameraBox.TabStop = false;
	            this.CameraBox.Text = "Live camera ";
	            // groupBox13
	            this.groupBox13.Location = new System.Drawing.Point(150, 10);
	            this.groupBox13.Name = "groupBox13";
	            this.groupBox13.Size = new System.Drawing.Size(220, 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(60, 45);
	            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, 45);
	            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, 15);
	            this.button_SIPRegister.Name = "button_SIPRegister";
	            this.button_SIPRegister.Size = new System.Drawing.Size(80, 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(15, 325);
	            this.label2.Name = "label2";
	            this.label2.Size = new System.Drawing.Size(42, 13);
	            this.label2.TabIndex = 5;
	            this.label2.Text = "Motion:";
	            // label_Motion
	            this.label_Motion.AutoSize = true;
	            this.label_Motion.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
	            this.label_Motion.Location = new System.Drawing.Point(70, 325);
	            this.label_Motion.Name = "label_Motion";
	            this.label_Motion.Size = new System.Drawing.Size(2, 15);
	            this.label_Motion.TabIndex = 6;
	            // label3
	            this.label3.AutoSize = true;
	            this.label3.Location = new System.Drawing.Point(140, 325);
	            this.label3.Name = "label3";
	            this.label3.Size = new System.Drawing.Size(61, 13);
	            this.label3.TabIndex = 7;
	            this.label3.Text = "Contact ID:";
	            // label_ContactID
	            this.label_ContactID.AutoSize = true;
	            this.label_ContactID.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
	            this.label_ContactID.Location = new System.Drawing.Point(210, 325);
	            this.label_ContactID.Name = "label_ContactID";
	            this.label_ContactID.Size = new System.Drawing.Size(2, 15);
	            this.label_ContactID.TabIndex = 8;
	            // Form1
	            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
	            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
	            this.ClientSize = new System.Drawing.Size(384, 364);
	            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
	            this.MaximizeBox = false;
	            this.Name = "Form1";
	            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
	            this.Text = "Onvif Camera Motion Detection Contact ID Message";
	            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
	            this.Load += new System.EventHandler(this.Form1_Load);
	        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;
	        private System.Windows.Forms.Label label2;
	        private System.Windows.Forms.Label label_Motion;
	        private System.Windows.Forms.Label label3;
	        private System.Windows.Forms.Label label_ContactID;

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 OzekiSDK.dll to the references of the solution.
    • Please import the missing classes.
  3. My alarm monitoring center did not get the Contact ID message. 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.

Ozeki Camera Recorder

If you are not a developer, but you need a great software to view or record your camera, we recommend you to use Ozeki Camera Recorder. This is a great end user product, that allows you to control your cameras from a webbrowser. It's easy and great. You can download it from the following link: Download Ozeki Camera Recorder.

Download link:
Download Ozeki Camera Recorder for Windows

Copyright © 2000- - Ozeki Ltd |
Home > Online manual > Motion detection and alarms > Send Contact ID alarms
Legal | Privacy | Terms of use | 6567 | | Login