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: | 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\Motion_Detection_Camera_Offline_WF\Motion_Detection_Camera_Offline_WF.sln |
WPF version: | C:\Program Files\Ozeki\Ozeki SDK\examples.zip\Examples\Other\Motion_Detection_Camera_Offline_WPF\Motion_Detection_Camera_Offline_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:
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)
CreateCall();
Finally, if the call has been answered please play the prerecorded .wav file:
if (e.State == CallState.Answered)
_playback.Start();
Detecting if a camera goes offline and making an alarm phone call using prerecorded voice 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.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() { InitializeComponent(); _softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000); _connector = new MediaConnector(); _audioSender = new PhoneCallAudioSender(); _playback = new WaveStreamPlayback("alarm.wav"); _imageProvider = new DrawingImageProvider(); SetVideoViewer(); } 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" }; 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) { var account = new SIPAccount(registrationRequired, displayName, userName, authenticationId, registerPassword, domainHost); _phoneLine = _softphone.CreatePhoneLine(account); _phoneLine.RegistrationStateChanged += phoneLine_RegistrationStateChanged; _softphone.RegisterPhoneLine(_phoneLine); } 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) CreateCall(); } private void CreateCall() { if (_phoneLine == null) return; var dial = new DialParameters("886") { CallType = CallType.AudioVideo }; _call = _softphone.CreateCallObject(_phoneLine, dial); _call.CallStateChanged += call_CallStateChanged; _audioSender.AttachToCall(_call); _connector.Connect(_playback, _audioSender); _call.Start(); } private void call_CallStateChanged(object sender, CallStateChangedArgs e) { InvokeGuiThread(() => label_Call.Text = e.State.ToString()); if (e.State == CallState.Answered) _playback.Start(); if (e.State != CallState.Completed && e.State != CallState.Busy) return; if (_call == null) return; _call.CallStateChanged -= call_CallStateChanged; _audioSender.Detach(); _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("192.168.112.109:8080","user","qwe123"); _camera.CameraStateChanged += _camera_CameraStateChanged; _camera.CameraErrorOccurred += _camera_CameraErrorOccured; _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 button_Disconnect_Click(object sender, EventArgs e) { if (_camera == null) return; _camera.Disconnect(); _camera = null; _videoViewerWf.Stop(); } private void InvokeGuiThread(Action action) { BeginInvoke(action); } } }
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
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.
Form1.Designer.cs
namespace OnvifIPCameraMotionDetection12 { 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_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(); this.groupBox1.SuspendLayout(); this.groupBox13.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.label_Error); this.groupBox1.Controls.Add(this.label_Camera); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.button_Disconnect); 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(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.Controls.Add(this.label_Call); this.groupBox13.Controls.Add(this.label4); 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(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.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 = "Alarm call if camera goes offline"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); 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.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; } }
WPF version
MainWindow.xaml.cs
using System; using System.Windows; using Ozeki.Media; using Ozeki.Camera; using Ozeki.VoIP; namespace OnvifIPCameraMotionDetection12Wpf { ////// 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 WaveStreamPlayback _playback; public MainWindow() { InitializeComponent(); _drawingImageProvider = new DrawingImageProvider(); _softphone = SoftPhoneFactory.CreateSoftPhone(5000, 10000); _connector = new MediaConnector(); _audioSender = new PhoneCallAudioSender(); _playback = new WaveStreamPlayback("alarm.wav"); } private void Connect_Click(object sender, RoutedEventArgs e) { _camera=new IPCamera("192.168.112.109:8080","user","qwe123"); _camera.CameraStateChanged += _camera_CameraStateChanged; _camera.CameraErrorOccurred += _camera_CameraErrorOccured; _connector.Connect(_camera.VideoChannel, drawingImageProvider); videoViewer.SetImageProvider(_drawingImageProvider); videoViewer.Start(); _camera.Start(); } //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; _softphone.RegisterPhoneLine(_phoneLine); } private void phoneLine_RegistrationStateChanged(object sender, RegistrationStateChangedArgs e) { InvokeGuiThread(() => label_Phoneline.Content = e.State.ToString()); } private void _camera_CameraStateChanged(object sender, CameraStateEventArgs e) { InvokeGuiThread(() => label_Camera.Content = e.State.ToString()); } private void _camera_CameraErrorOccured(object sender, CameraErrorEventArgs e) { InvokeGuiThread(() => label_Error.Content = e.Error.ToString()); if (e.Error == IPCameraError.ConnectionLost) CreateCall(); } private void CreateCall() { if (_phoneLine == null) return; var dial = new DialParameters("886") { CallType = CallType.AudioVideo }; _call = _softphone.CreateCallObject(_phoneLine, dial); _call.CallStateChanged += call_CallStateChanged; _audioSender.AttachToCall(_call); _connector.Connect(_playback, _audioSender); _call.Start(); } private void call_CallStateChanged(object sender, CallStateChangedArgs e) { InvokeGuiThread(() => label_Call.Content = e.State.ToString()); if (e.State == CallState.Answered) _playback.Start(); if (e.State != CallState.Completed && e.State != CallState.Busy) return; if (_call == null) return; _call.CallStateChanged -= call_CallStateChanged; _audioSender.Detach(); _connector.Disconnect(_playback, _audioSender); _call = null; } private void button_SIPRegister_Click(object sender, EventArgs e) { Register(true, "885", "885", "885", "885", "192.168.115.100"); } private void button_Disconnect_Click(object sender, RoutedEventArgs e) { if (_camera == null) return; _camera.Disconnect(); _camera = null; videoViewer.Stop(); } private void InvokeGuiThread(Action action) { Dispatcher.BeginInvoke(action); } } }
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
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 WPF will be able to work properly.
MainWindow.xaml
<Window x:Class="OnvifIPCameraMotionDetection12Wpf.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="Alarm call if camera goes offline" Height="408" Width="399" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen"> <Grid Margin="0,0,4,0"> <GroupBox Header="Live camera" HorizontalAlignment="Left" Margin="12,141,0,0" VerticalAlignment="Top" Height="226" Width="367"> <Grid HorizontalAlignment="Left" Height="204" VerticalAlignment="Top" Width="296"> <controls:VideoViewerWPF Name="videoViewer" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Black" Margin="0,0,-57,0"/> </Grid> </GroupBox> <GroupBox Header="SIP account settings" HorizontalAlignment="Left" Margin="192,12,0,0" VerticalAlignment="Top" Height="124" Width="187"> <Grid HorizontalAlignment="Left" Height="102" VerticalAlignment="Top" Width="177" 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> <GroupBox Header="Connect" HorizontalAlignment="Left" Margin="10,12,0,0" VerticalAlignment="Top" Height="124" Width="177"> <Grid HorizontalAlignment="Left" Height="102" VerticalAlignment="Top" Width="167" Margin="0,0,-2,0"> <Grid.RowDefinitions> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> <RowDefinition Height="1*"/> </Grid.RowDefinitions> <Button Content="Connect" HorizontalAlignment="Left" VerticalAlignment="Center" Width="75" Click="Connect_Click"/> <Button Content="Disconnect" HorizontalAlignment="Right" VerticalAlignment="Center" Width="75" Click="button_Disconnect_Click"/> <Label Content="Camera state:" HorizontalAlignment="Left" Grid.Row="1" VerticalAlignment="Center"/> <Label Content="Camera error:" HorizontalAlignment="Left" Grid.Row="2" VerticalAlignment="Center"/> <Label x:Name="label_Camera" Content="" HorizontalAlignment="Right" Grid.Row="1" VerticalAlignment="Center"/> <Label x:Name="label_Error" Content="" HorizontalAlignment="Right" Grid.Row="2" VerticalAlignment="Center"/> </Grid> </GroupBox> </Grid> </Window>
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:
-
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.)
-
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.
-
My audio is not played. Why?
Make sure you added a .wav file.
-
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.
More information
- How to subscribe for camera events in C#
- How to use camera side motion detection in C#
- How to setup viewer side motion detection in C#
- How to set motion detection sensitivity in C#
- How to mask certain areas of the video image in C#
- C# alarm snapshot sending in e-mail
- C# camera snapshot picture upload to FTP
- C# video alarm sending in e-mail
- C# recording alarm video upload to ftp
- Make a VoIP phone alarm call with text to speech in C#
- C# Contact ID alarm message
- Detect if a camera goes offline using alarm phone call