How to query the audio/video stream parameters (frame rate, resolution, video codec, audio codec) of a broadcasted camera in Windows Console in C#

In this guide you will find detailed information on how to query all the details and information (frame rate, resolution, video codec, audio codec) about your IP camera. 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 get all the details and stream specifications from an IP camera device 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 Console 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: 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\
Camera_Viewer_Stream_Parameters_Console\Camera_Viewer_Stream_Parameters_Console.sln

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

Please note that this example is about a Console Application and not about a Windows Forms Application. The image of the camera is not needed, the console interface will be sufficient.

To create a new Console Application you should choose the Console Application option in the new project window. In this case you do not need to copy the contents of multiple files. The only necessary source code is the one that can be seen on this page. After creating a new Console Application with the same name that you can see right after the namespace keyword, the only thing you have to do is to copy all of the code from this page into the Visual Studio (besides the usual to-dos like adding the SDK reference and modifying the project properties).

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

Camera.CameraStateChanged += new
EventHandler<CameraStateEventArgs>(Camera_CameraStateChanged): with the help of this line you can subscribe to the events of the IP camera. When the state of the came changes, the application is going to react to the event.

static void Camera_CameraStateChanged(object sender, CameraStateEventArgs e): with the help of this method you can handle several events of the camera that can happen until the application is running. The first type of events is the handled event: after the camera has been successfully connected to the application the firmware and hardware details of the device can be listed. The second type of events is the streaming event: after the camera starts streaming both the audio and video stream specifications can be listed.

Console.WriteLine(GetDeviceInfo()),
Console.WriteLine(StreamInfoAudio()),
Console.WriteLine(StreamInfoVideo()): the methods inside the Console.WriteLine() calls return a formed string about the different categories we are listing the information about.

Query an IP camera's details and stream information in C#

Program.cs

using System;
using System.Text;
using Ozeki.Media;
using Ozeki.Camera;

namespace VideoCameraViewer06
{
    class Program
    {
        static IIPCamera Camera;

        static void Main(string[] args)
        {
            Camera = new IPCamera("192.168.112.109:8080", "user", "qwe123");
            if(Camera != null)
            {
                Camera.Start();
                Camera.CameraStateChanged += Camera_CameraStateChanged;
                Console.WriteLine("Connecting...\n");
            }

            Console.ReadLine();
        }

        static void Camera_CameraStateChanged(object sender, CameraStateEventArgs e)
        {
            switch (e.State)
            {
                case CameraState.Streaming:
                    {
                        Console.WriteLine(GetDeviceInfo());
                        Console.WriteLine(StreamInfoAudio());
                        Console.WriteLine(StreamInfoVideo());
                        break;
                    }
            }
        }

        public static string GetDeviceInfo()
        {
            var sb = new StringBuilder();

            sb.AppendLine(@"Firmware - " + Camera.CameraInfo.DeviceInfo.Firmware + "\n");
            sb.AppendLine(@"Hardware ID - " + Camera.CameraInfo.DeviceInfo.HardwareId + "\n");
            sb.AppendLine(@"Manufacture - " + Camera.CameraInfo.DeviceInfo.Manufacturer + "\n");
            sb.AppendLine(@"Model - " + Camera.CameraInfo.DeviceInfo.Model + "\n");
            sb.AppendLine(@"Serial number - " + Camera.CameraInfo.DeviceInfo.SerialNumber + "\n");

            return sb.ToString();
        }

        public static string StreamInfoAudio()
        {
            var sb = new StringBuilder();
            if (Camera.CurrentStream.AudioEncoding != null)
            {
                sb.AppendLine(" - Audio Encoding \n");
                sb.AppendLine("\t Bitrate - " + Camera.CurrentStream.AudioEncoding.Bitrate + "\n");
                sb.AppendLine("\t Encoding - " + Camera.CurrentStream.AudioEncoding.Encoding + "\n");
                sb.AppendLine("\t Samplerate - " + Camera.CurrentStream.AudioEncoding.SampleRate + "\n");
                sb.AppendLine("\t Session time out - " + Camera.CurrentStream.AudioEncoding.SessionTimeOut + "\n");
                sb.AppendLine("\t Use count - " + Camera.CurrentStream.AudioEncoding.UseCount + "\n");

                sb.AppendLine(" - Audio Source \n");
                sb.AppendLine("\t Channels - " + Camera.CurrentStream.AudioSource.Channels + "\n");
                sb.AppendLine("\t Use count - " + Camera.CurrentStream.AudioSource.UseCount + "\n");
            }
                return sb.ToString();
            
        }

        public static string StreamInfoVideo()
        {
            var sb = new StringBuilder();

            sb.AppendLine(" - Video Encoding \n");
            if (Camera.CurrentStream.VideoEncoding != null)
            {
                sb.AppendLine("\t Bitrate - " + Camera.CurrentStream.VideoEncoding.BitRate + "\n");
                sb.AppendLine("\t Encoding - " + Camera.CurrentStream.VideoEncoding.Encoding + "\n");
                sb.AppendLine("\t Encoding interval - " + Camera.CurrentStream.VideoEncoding.EncodingInterval + "\n");
                sb.AppendLine("\t Framerate - " + Camera.CurrentStream.VideoEncoding.FrameRate + "\n");
                sb.AppendLine("\t Quality - " + Camera.CurrentStream.VideoEncoding.Quality + "\n");
                sb.AppendLine("\t Resolution - " + Camera.CurrentStream.VideoEncoding.Resolution + "\n");
                sb.AppendLine("\t Session time out - " + Camera.CurrentStream.VideoEncoding.SessionTimeout + "\n");
                sb.AppendLine("\t Use count - " + Camera.CurrentStream.VideoEncoding.UseCount + "\n");
            }
            sb.AppendLine(" - Video Source \n");
            if (Camera.CurrentStream.VideoSource != null)
            {
                sb.AppendLine("\t Bounds - " + Camera.CurrentStream.VideoSource.Bounds + "\n");
                sb.AppendLine("\t Use count - " + Camera.CurrentStream.VideoSource.UseCount + "\n");
            }
            return sb.ToString();
        }
    }
}

Code 1 - Query an IP camera's details and stream information 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.

Console output

Figure 1 - In case of a successful query your console output

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 to the references of the solution.
    • Please import the missing classes.
  3. I see "unknown" in every fields.

    In this case your camera failed to provide any details to the program, but it works normally.

  4. Are there more informations about the device and the streams?

    Yes, there are. You can find them, if you write the following in Visual Studio

    • Camera.NetworkSettings.
    • Camera.CurrentStream.AudioEncoding.
    • Camera.CurrentStream.AudioSource.
    • Camera.CurrentStream.VideoEncoding.
    • Camera.CurrentStream.VideoSource.

    After this, the Visual Studio will show the properties automatically. For more information, please check the definition of the IPCamera class.

More information