How to find not Onvif cameras on the network
This example demonstrates how you can implement multicamera server console in C#. 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.
Stream video in 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\Onvif_IP_Camera_Server_Console\Onvif_IP_Camera_Server_Console.sln |
To compile this example you will need Microsoft Visual Studio installed on your computer.
Architecture of Onvif IP camera server for RTSP camera
First let us clarify why it is needed to use an Onvif IP camera server instead of a simple application which can establish connection with RTSP cameras. RTSP cameras have just one main stream, which means they can provide streaming to just one client at a time. Onvif IP camera server helps you to reach the image of the RTSP camera as a client by RTSP (Real Time Streaming Protocol) and transmit it as a server to multiple viewers simultaneously. (Figure 1).
Aftre installing the architecture, your RTSP camera will work almost like an Onvif IP camera apart from the limits of most RTSP cameras have e.g. no PTZ control, unability to set the lighting, no authentication, etc. These feature problems can be resolved by building-in the Onvif IP camera server e.g. you need to authenticate to the server in orderto use the RTSP camera. The PTZ feature can be resolved by optical zooming and by installing a PTZ device, but it is not recommended because it's price is higher and the quality of optical zoom is worse than a new IP Camera's.
In the following you will be shown how to implement multicamera server console. This application is the most simple example what represents the complete app. It contains the elements:
- RTSP camera stream as Onvid camera.
- Connecting to an MJPEG stream and streaming it as Onvif camera
- Streaming USB camera as an Onvif camera
- ServerCount Gets the number of servers managed by the MultiCameraServer object.
- ServerList Gets the list of the servers.
- ConfigList Gets the list of the server configurations.
- EventHandler<CameraServerConnectionArgs> ClientConnected is raised when a client connects to a server managed by the MultiCameraServer object. This event must always be handled in order to connect the server's VideoSender and AudioSender with the client's VideoChannel and AudioChannel. In the handler you will be able to lay down conditions about the clients, the CameraServerConnectionArgs object will contain the server and client which wants to connect.
- EventHandler<CameraServerConnectionArgs> ClientDisconnected is raised when a client disconnects from a server managed by the MultiCameraServer. It is recommended to handle this event.
- EventHandler<MultiCameraServerArgs> ServerStarted raised when StartServer() method is called.
- EventHandler<MultiCameraServerArgs> ServerStopped is raised when StopServer() method is called .
- EventHandler<EventArgs> AllServersStarted is raised when StartAllServers() method is called.
- EventHandler<EventArgs> AllServersStopped is raised when StopAllServers() method is called.
- AddNewServer(CameraServerConfig config) Configuration for the new server.
- Config: configuration for the new server.
- CameraServer GetServer(CameraServerConfig conf) Gets the server definied by the given config.
- Conf: CameraServerConfiguration object which identifies the server based on the Ip Address and port number.
- RemoveServer(CameraServerConfig conf) Removes a server definied by the given config.
- Conf: CameraServerConfiguration object which identifies the server based on the Ip Address and port number.
- StartServer(CameraServerConfig conf) Starts the server definied by the given config.
- Conf: CameraServerConfiguration object which identifies the server based on the Ip Address and port number.
- StopServer(CameraServerConfig conf) Stops the server definied by the given config.
- Conf:CameraServerConfiguration object which identifies the server based on the Ip Address and port number.
- StopAllServers() Stops all servers.
- StartAllServers() Starts all servers.
- ClearAll() Removes all the servers managed by the MultiCameraServer object.
- ModifyServerConfig(CameraServerConfig oldconf, CameraServerConfig config) Modifies a server identified by the oldconf parameter. If the server was running it will be stopped and restared after the modification.
- Oldconf: CameraServerConfiguration object which identifies the server based on the Ip Address and port number.
- Config: New configuration which will replace the old.
-
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 OzekiSDK.dll to the references of the solution.
- Please import the missing classes.
- How to forward PTZ instructions in C#
- How to turn your USB camera into an Onvif IP webcam in C#
- How to turn your old RTSP ip camera into an Onvif IP webcam in C#
- Send video to Iphone Android mobile in C#
- C# stream video to multiple locations simultaneously
- How to make your software discoverable by other applications in C#
- Connect to RTSP camera stream, broadcasted by VLC media player
- How to find not Onvif cameras on the network
- How to compress h.263 and h.264 codec in C#
The program runs an Onvif protocol for every server.
For example we can connect to an MJPEG stream and we can also stream it disguised as an Onvif camera. It is quite useful, because it displays itself on the network, therefore everything that knows Onvif protocol will see it.
Properties
Events
Methods
Program.cs
using System; using Ozeki.Media; using Ozeki.Camera; namespace MulticameraServerDemo { class Program { static MediaConnector _connector; static MultiCameraServer _multiCameraServer; static IPCamera _rtspCamera; static MJPEGConnection _mjpegStream; static IWebCamera webCamera; static void Main(string[] args) { _connector = new MediaConnector(); _multiCameraServer = new MultiCameraServer(); var ip = Ozeki.Network.NetworkAddressHelper.GetLocalIP().ToString(); //Wire up the events _multiCameraServer.ClientConnected+=_multiCameraServer_ClientConnected; _multiCameraServer.ClientDisconnected +=_multiCameraServer_ClientDisconnected; // ------------------- RTSP EXAMPLE --------------------------- _rtspCamera = new IPCamera("rtsp://192.168.112.103:554/live.sdp", string.Empty, string.Empty, CameraTransportType.TCP); if(_rtspCamera != null) { _rtspCamera.Start(); Console.WriteLine("RTSP Camera connected"); int serverPort1 = 8554; int onvifServerPort1 = 8189; string rtspUrl1 = "rtsp://" + ip + ":" + serverPort1; OnvifConfig onvfif1 = new OnvifConfig(onvifServerPort1, ip, true, rtspUrl1); CameraServerConfig conf1 = new CameraServerConfig(ip, serverPort1, _rtspCamera.VideoChannel, null, onvfif1); _multiCameraServer.AddNewServer(conf1); } // ------------------- MJPEG Example ----------------------- _mjpegStream = new MJPEGConnection(); var connected = _mjpegStream.Connect("http://192.168.115.124:7777/vjpeg.v?user=admin&pwd=admin"); if (connected) { Console.WriteLine("MJPEG Stream connected"); int serverPort2 = 555; int onvifServerPort2 = 8289; string rtspUrl2 = "rtsp://" + ip + ":" + serverPort2; OnvifConfig onvfif2 = new OnvifConfig(onvifServerPort2, ip, true, rtspUrl2); CameraServerConfig conf2 = new CameraServerConfig(ip, serverPort2, _mjpegStream.VideoChannel, null, onvfif2); _multiCameraServer.AddNewServer(conf2); } // -------------------- USB Example ----------------------- webCamera = WebCameraFactory.GetDefaultDevice(); if (webCamera != null) { webCamera.Start(); Console.WriteLine("USB Camera connected"); int serverPort3 = 556; int onvifServerPort3 = 8389; string rtspUrl3 = "rtsp://" + ip + ":" + serverPort3; OnvifConfig onvfif3 = new OnvifConfig(onvifServerPort3, ip, true, rtspUrl3); CameraServerConfig conf3 = new CameraServerConfig(ip, serverPort3, webCamera.VideoChannel, null, onvfif3); _multiCameraServer.AddNewServer(conf3); } var all_started = _multiCameraServer.StartAllServers(); Console.WriteLine(all_started?"Servers started":"Some of the servers couldn't be started"); Console.ReadLine(); } //It is recommended to handle the disconnect event private static void _multiCameraServer_ClientDisconnected(object sender, CameraServerConnectionArgs e) { if (e.Server.VideoSender != null) _connector.Disconnect(e.Server.VideoSender, e.Client.VideoChannel); if (e.Server.AudioSender != null) _connector.Disconnect(e.Server.AudioSender, e.Client.AudioChannel); Console.WriteLine("Server: " + e.Server.ServerIPAddress + ":" + e.Server.Port + "-" + " Client disconnected " + e.Client.TransportInfo.RemoteEndPoint); } //with this event you can handle client connections, you can decide wether the client can connect and receive media or not static void _multiCameraServer_ClientConnected(object sender, CameraServerConnectionArgs e) { if (e.Server.VideoSender != null) _connector.Connect(e.Server.VideoSender, e.Client.VideoChannel); if (e.Server.AudioSender != null) _connector.Connect(e.Server.AudioSender, e.Client.AudioChannel); Console.WriteLine("Server: " + e.Server.ServerIPAddress + ":" + e.Server.Port + "-" + " New client: " + e.Client.TransportInfo.RemoteEndPoint); } } }
Console output
Related Pages
FAQ
Below you can find the answers for the most frequently asked questions related to this topic:
More information