Curt Copeland

Curt Copeland

  • NA
  • 1
  • 5.2k

Onvif IP camera remote configuration in C#

Oct 17 2014 8:36 AM
Hi there,

A couple of weeks ago we have installed a new security IP camera (EasyN 187V). In addition to the basic functionalities, it supports Onvif and it has some special features (PTZ, built-in microphone, night vision, motion detection, alarm notification, etc.).

The long and short of it, this is a smart device, but we need some further functionalities, because my boss travels a lot and he wants more control over the surveillance system. I'm in charge of finding out a solution that enables remote configuration.

I've done some research and I came across a Codeproject guide that informed me that an Onvif-compliant camera SDK would be the most effective solution. (I'm not secure in my knowledge, so I need some confirmation.)

So the point is that I checked out the camera SDK that was recommended by the article, and I found a good Onvif C# example on how to configure the IP camera's network settings.
I thought it's worth to share the code snippet related to this issue, because it can be useful for others as well, so if you'are also interested in the remote configuration, take a look at the code below:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Ozeki.Media.MediaHandlers;
using Ozeki.Media.MediaHandlers.IPCamera;
using Ozeki.Media.MediaHandlers.IPCamera.Types;
using Ozeki.Media.MediaHandlers.Video;
using Ozeki.Media.IPCamera;
using Ozeki.Media.IPCamera.Network;
using Ozeki.Media.Video;
using Ozeki.Media.Video.Controls;
 
namespace ConfigureOnvifCameraRemotely07
{
    public partial class Form1 : Form
    {
        private IIPCamera _camera;
        private DrawingImageProvider _imageProvider;
        private MediaConnector _connector;
        private VideoViewerWF _videoViewerWf;
 
        private List<int> _ports;
 
        public Form1()
        {
            InitializeComponent();
            _ports = new List<int>();
            _connector = new MediaConnector();
            _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(30, 30);
            _videoViewerWf.Name = "_videoViewerWf";
        }
 
        private void button_Connect_Click(object sender, EventArgs e)
        {
            _camera = IPCameraFactory.GetCamera("192.168.115.175:8080", "admin", "admin");
 
            _camera.CameraStateChanged += _camera_CameraStateChanged;
 
            _connector.Connect(_camera.VideoChannel, _imageProvider);
            _videoViewerWf.SetImageProvider(_imageProvider);
            _videoViewerWf.Start();
 
            _camera.Start();
        }
 
        private void _camera_CameraStateChanged(object sender, CameraStateEventArgs e)
        {
            if (e.State == IPCameraState.Streaming)
            {
                InvokeGuiThread(() => groupBox_Network.Enabled = true);
                ClearGUI();
                GetNetworkSettings();
                if (!_camera.NetworkManager.DefaultConfig.UseDHCP)
                    InvokeGuiThread(() => radioButton_Manual.Checked = true );
            }
        }
 
        private void ClearGUI()
        {
            InvokeGuiThread(() =>
            {
                textBox_IP.Text = String.Empty;
                textBox_Host.Text = String.Empty;
                textBox_Netmask.Text = String.Empty;
                textBox_Gateway.Text = String.Empty;
                textBox_DNS.Text = String.Empty;
                textBox_NTP_IP.Text = String.Empty;
 
                textBox_HTTP.Text = String.Empty;
                textBox_HTTPS.Text = String.Empty;
                textBox_RTSP.Text = String.Empty;
                comboBox_HTTP.Items.Clear();
                comboBox_HTTPS.Items.Clear();
                comboBox_RTSP.Items.Clear();
            });
        }
 
        private void GetNetworkSettings()
        {
            InvokeGuiThread(() =>
            {
                textBox_IP.Text = _camera.NetworkManager.DefaultConfig.IPAddress;
                textBox_Host.Text = _camera.NetworkManager.DefaultConfig.HostName;
                textBox_Netmask.Text = _camera.NetworkManager.DefaultConfig.Netmask;
                textBox_Gateway.Text = _camera.NetworkManager.DefaultConfig.DefaultGateway;
                textBox_DNS.Text = _camera.NetworkManager.DefaultConfig.DNS;
                textBox_NTP_IP.Text = _camera.NetworkManager.DefaultConfig.NTP.IPAddress;
 
                if (_camera.NetworkManager.HttpPort == null)
                    textBox_HTTP.Enabled = false;
                else
                {
                    foreach (var ports in _camera.NetworkManager.HttpPort.Port)
                    {
                        comboBox_HTTP.Items.Add(ports);
                    }
                }
 
                if (_camera.NetworkManager.HttpsPort == null)
                    textBox_HTTPS.Enabled = false;
                else
                {
                    foreach (var ports in _camera.NetworkManager.HttpsPort.Port)
                    {
                        comboBox_HTTPS.Items.Add(ports);
                    }
                }
 
                if (_camera.NetworkManager.RtspPort == null)
                    textBox_RTSP.Enabled = false;
                else
                {
                    foreach (var ports in _camera.NetworkManager.RtspPort.Port)
                    {
                        comboBox_RTSP.Items.Add(ports);
                    }
                }
            });
        }
 
        private void button_Apply_Click(object sender, EventArgs e)
        {
            if (radioButton_DHCP.Checked)
            {
                _camera.NetworkManager.DefaultConfig.UseDHCP = true;
            }
 
            if (radioButton_Manual.Checked)
            {
                _camera.NetworkManager.DefaultConfig.UseDHCP = false;
 
                _camera.NetworkManager.DefaultConfig.IPAddress = textBox_IP.Text;
                _camera.NetworkManager.DefaultConfig.HostName = textBox_Host.Text;
                _camera.NetworkManager.DefaultConfig.Netmask = textBox_Netmask.Text;
                _camera.NetworkManager.DefaultConfig.DefaultGateway = textBox_Gateway.Text;
                _camera.NetworkManager.DefaultConfig.DNS = textBox_DNS.Text;
 
 
                if (_camera.NetworkManager.HttpPort != null)
                {
                    _ports.Clear();
                    foreach (var item in comboBox_HTTP.Items)
                    {
                        var ports = Int32.Parse(item.ToString());
                        _ports.Add(ports);
                    }                 
                    _camera.NetworkManager.HttpPort = new CameraPort(CameraProtocolType.HTTP, true, _ports.ToArray());
                }
 
                if (_camera.NetworkManager.HttpsPort != null)
                {
                    _ports.Clear();
                    foreach (var item in comboBox_HTTPS.Items)
                    {
                        var ports = Int32.Parse(item.ToString());
                        _ports.Add(ports);
                    }
                    _camera.NetworkManager.HttpsPort = new CameraPort(CameraProtocolType.HTTPS, true, _ports.ToArray());
                }
 
                if (_camera.NetworkManager.RtspPort != null)
                {
                    _ports.Clear();
                    foreach (var item in comboBox_RTSP.Items)
                    {
                        var ports = Int32.Parse(item.ToString());
                        _ports.Add(ports);
                    }
                    _camera.NetworkManager.RtspPort = new CameraPort(CameraProtocolType.RTSP, true, _ports.ToArray());
                }
            }
 
            _camera.NetworkManager.ApplyConfig();
        }
 
        private void button_HTTP_Add_Click(object sender, EventArgs e)
        {
            if (!comboBox_HTTP.Items.Contains(textBox_HTTP.Text))
                comboBox_HTTP.Items.Add(textBox_HTTP.Text);
        }
 
        private void button_HTTPS_Add_Click(object sender, EventArgs e)
        {
            if (!comboBox_HTTPS.Items.Contains(textBox_HTTPS.Text))
                comboBox_HTTPS.Items.Add(textBox_HTTPS.Text);
        }
 
        private void button_RTSP_Add_Click(object sender, EventArgs e)
        {
            if (!comboBox_RTSP.Items.Contains(textBox_RTSP.Text))
                comboBox_RTSP.Items.Add(textBox_RTSP.Text);
        }
 
        private void button_HTTP_Delete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < comboBox_HTTP.Items.Count; i++)
            {
                if (comboBox_HTTP.Items[i].ToString() == textBox_HTTP.Text)
                    comboBox_HTTP.Items.Remove(comboBox_HTTP.Items[i]);
            }
        }
 
        private void button_HTTPS_Delete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < comboBox_HTTPS.Items.Count; i++)
            {
                if (comboBox_HTTPS.Items[i].ToString() == textBox_HTTPS.Text)
                    comboBox_HTTPS.Items.Remove(comboBox_HTTPS.Items[i]);
            }
        }
 
        private void button_RTSP_Delete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < comboBox_RTSP.Items.Count; i++)
            {
                if (comboBox_RTSP.Items[i].ToString() == textBox_RTSP.Text)
                    comboBox_RTSP.Items.Remove(comboBox_RTSP.Items[i]);
            }
        }
 
        private void radioButton_Manual_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_Basic.Enabled = true;
            groupBox_Ports.Enabled = true;
        }
 
        private void radioButton_DHCP_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_Basic.Enabled = false;
            groupBox_Ports.Enabled = false;
        }
 
        private void InvokeGuiThread(Action action)
        {
            BeginInvoke(action);
        }
    }
}
(Source: http://www.camera-sdk.com/p_22-how-to-configure-your-onvif-camera-remotely-onvif.html)

It works well, but unfortunately this SDK is not free. Now I use its free trial version, but it will expire soon. Can anybody recommend me an alternative SDK to this one? Or any other C#-based suggestion would be appreciated related to remote IP camera configuration.

I'm looking forward to your answers here or please send me an e-mail to [email protected]! Thank you so much in advance!

All the best,
Curt

Answers (1)