Ozeki Camera Recorder Stat

How to handle alarms, by making a VoIP phone call to an alarm monitoring center with text to speech in C#

In this guide you can find information on how to connect an IP camera to motion detector with your Windows Forms/WPF Application written in C# and register your program to a PBX and make a VoIP phone call to an alarm monitoring center with text to speech. To implement this example, you must have OZEKI Camera SDK installed, and a reference to OzekiSDK.dll should be added to your Visual Studio project. To make the SIP functions working you must have a PBX and you will have to modify the programs connection data to the proper IP-adress.

How to handle alarms, by making a VoIP phone call to an alarm monitoring center with text to speech 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. It's also recommended to visit the How to setup viewer side motion detection 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-camera-onvif.html
Windows forms version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Motion_Detection_Text_To_Speech_WF\Motion_Detection_Text_To_Speech_WF.sln
WPF version: C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\
Motion_Detection_Text_To_Speech_WPF\Motion_Detection_Text_To_Speech_WPF.sln

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:

ISoftPhone _softphone: We need to create a softphone object from the ISoftPhone interface
IPhoneLine _phoneLine: We need to create a phone line object from the IPhoneLine interface
private TextToSpeech _textToSpeech: The C# written TextToSpeech class is available with the OZEKI Camera SDK. With the help of the TextToSpeech class you can create instances which are able to convert text - which is given as a parameter (or the path of it is given as a parameter) - to voice data. This instance can be attached to the call through the correct sender object, so the stream of voice data can be sent to the destination as in the case of the usage of a microphone.

We need to set the port range, indicated by the first two parameters as the minimum and the maximum number of the port, this is the interval of the port. If we have any firewall rule which restricts the usable ports, we can set the usable port range here, which will be used during the calls. The followings are examples are showing sample ports, but a softphone with these parameters can be used in the most of the cases:

To make your example working you have to modify the parameters of the Register function in this method.

private void button_SIPRegister_Click(object sender, EventArgs e)
        {
            Register(true, "885", "885", "885", "885", "192.168.115.100");
            _motionDetector.Start();
        }
Set the last parameter to your PBX IP address and if necessary, modify the other parameters to fit your PBX settings.

_softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);

we also initialize this softphone with default parameters.

To communicate (and to register to the PBX) we need to create a phone line. We've already created the necessary SIP account so with this we can create it:

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

When the application is running, the phone line's state can change. To follow these changes, we need to listen to this change event:

_phoneLine.RegistrationStateChanged += phoneLine_RegistrationStateChanged;

When the phone line has been created, we have to 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. We just need to use the following line:

_softphone.RegisterPhoneLine(_phoneLine);

When a motion is detected the program calls the CreateCall() method. In this example the dialed number is 886 and initialize the _call object:

var dial = new DialParameters("886") { CallType = CallType.AudioVideo };
_call = _softphone.CreateCallObject(_phoneLine, dial);

You have to attach _audioSender to call and connect the _textToSpeech object to
_audiosender:

_audioSender.AttachToCall(_call);
_connector.Connect(_textToSpeech, _audioSender);

After this you can start the _call:

_call.Start();

If the call's state will be answered, you can play a text with AddandStartText() method:

_textToSpeech.AddAndStartText("Motion detected");

If the call's state will be completed, you should detach and disconnect the devices:

_call.CallStateChanged -= call_CallStateChanged;
_audioSender.Detach();
_connector.Disconnect(_textToSpeech, _audioSender);

Making a VoIP phone call to an alarm monitoring center example in C#

Windows Form WPF  

Form1.cs

	using System;
	using System.Drawing;
	using System.Windows.Forms;
	using Ozeki.Media;
	using Ozeki.Camera;
	using Ozeki.VoIP;
	
	namespace OnvifIPCameraMotionDetection10
	{
	    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 MotionDetector _motionDetector;
	        private TextToSpeech _textToSpeech;
	
	        public Form1()
	        {
	            InitializeComponent();
	            _imageProvider = new DrawingImageProvider();
	            _softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000);
	            _connector = new MediaConnector();
	            _audioSender = new PhoneCallAudioSender();
	            _textToSpeech = new TextToSpeech();
	            _motionDetector = new MotionDetector();
	            _motionDetector.MotionDetection += _motionDetector_MotionDetection;
	            SetVideoViewer();
	        }
	
	        private void SetVideoViewer()
	        {
	            _videoViewerWf = new VideoViewerWF
	            {
	                Size = new Size(260, 180),
	                BackColor = Color.Black,
	                TabStop = false,
	                FlipMode = FlipMode.None,
	                Location = new Point(45, 30),
	                Name = "_videoViewerWf"
	            };
	            CameraBox.Controls.Add(_videoViewerWf);
	        }
	
	        //Registering the account to a PBX
	        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)
	            {
	                InvokeGuiThread(() => label_Status.Text = ex.Message );
	            }
	        }
	
	        private void phoneLine_RegistrationStateChanged(object sender, RegistrationStateChangedArgs e)
	        {
	            InvokeGuiThread(() => label_Status.Text = e.State.ToString() );
	        }
	
	        private void _motionDetector_MotionDetection(object sender, MotionDetectionEvent e)
	        {
	            InvokeGuiThread(() => label_Motion.Text = e.Detection.ToString() );
	
	            if (e.Detection && _call == null)
	                CreateCall();
	        }
	
	        private void CreateCall()
	        {
	            if (_phoneLine == null) return;
	            var dial = new DialParameters("886") { CallType = CallType.Audio };
	
	            _call = _softphone.CreateCallObject(_phoneLine, dial);
	
	            _call.CallStateChanged += call_CallStateChanged;
	            _audioSender.AttachToCall(_call);
	            _connector.Connect(_textToSpeech, _audioSender);
	            _call.Start();
	        }
	
	        private void call_CallStateChanged(object sender, CallStateChangedArgs e)
	        {
	            InvokeGuiThread(() => label_Status.Text = e.State.ToString() );
	
	            if (e.State == CallState.Answered)
	                _textToSpeech.AddAndStartText("Motion detected");
	
	            if (e.State != CallState.Completed && e.State != CallState.Busy) return;
	            if (_call == null) return;
	            _call.CallStateChanged -= call_CallStateChanged;
	            _audioSender.Detach();
	            _connector.Disconnect(_textToSpeech, _audioSender);
	            _call = null;
	        }
	
	        private void button_Connect_Click(object sender, EventArgs e)
	        {
	            _camera=new IPCamera("192.168.112.109:8080","user","qwe123");
	
	            _connector.Connect(_camera.VideoChannel, _motionDetector);
	            _connector.Connect(_motionDetector, _imageProvider);
	            _videoViewerWf.SetImageProvider(_imageProvider);
	            _videoViewerWf.Start();
	            _camera.Start();
	        }
	
	        //We'll call the register() method then start the motion detector
	        private void button_SIPRegister_Click(object sender, EventArgs e)
	        {
	            Register(true, "885", "885", "885", "885", "192.168.115.100");
	            _motionDetector.Start();
	        }
	
	        private void InvokeGuiThread(Action action)
	        {
	            BeginInvoke(action);
	        }
	    }
	}
	

Code 1 - Making a VoIP phone call 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.

GUI

The Graphical User Interface of an application that handles alarms by making a BoIP voice call 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. When the motion detector detects motion, motion label text will be true. If SIP account register is succeeded than the program call to 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.

Form1.Designer.cs

	namespace OnvifIPCameraMotionDetection10
	{
	    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.label2 = new System.Windows.Forms.Label();
	            this.label_Motion = 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(130, 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(25, 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(350, 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(150, 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(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;
	            // 
	            // Form1
	            // 
	            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
	            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
	            this.ClientSize = new System.Drawing.Size(374, 364);
	            this.Controls.Add(this.label_Motion);
	            this.Controls.Add(this.label2);
	            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 Camera Motion Detection SIP Call with TTS";
	            this.groupBox1.ResumeLayout(false);
	            this.groupBox13.ResumeLayout(false);
	            this.groupBox13.PerformLayout();
	            this.ResumeLayout(false);
	            this.PerformLayout();
	
	        }
	
	        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;
	    }
	}
	

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. My phone does not ring, when the camera detects a motion. Why?

    • Make sure your PBX is configured right.
    • Make sure the dialed number whether is valid.
    • Make sure your registration datas is valid.