锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / 流媒体开源 / C#实现基于IP摄像头的视频监控系统
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。

C#实现基于IP摄像头的视频监控系统

Introduction

Looking at today’s tendency of video surveillance systems, it can be easily noticed that popularity of IP based solutions is growing really fast. There are quite a lot of manufacturers, who provide great range of IP video cameras and video servers, meant to serve in a professional IP-enabled video surveillance system. More than that, many companies provide solutions aimed at converting current CCTV video surveillance systems to IP based systems, keeping current equipment and infrastructure. In addition to these companies who provide not just hardware solutions, but software as well, there are a lot of other companies who focus on the software part of IP video surveillance systems mostly, providing complete solutions for small or enterprise business, as well as for personal use.

In the article, I would like to share some of my experience in working with different IP video cameras and video servers from different manufacturers. The provided information is related mostly to accessing these cameras from your own application, which may be a simple application for your personal needs, or something more sophisticated and even close to some sort of video surveillance application.

纵观当今视频监控系统的发展趋势,可以很容易地发现基于IP的解决方案的普及正在快速增长。有很多制造商提供各种IP视频摄像头和视频服务器,旨在提供专业的IP视频监控系统。更重要的是,许多公司提供的解决方案旨在将当前的CCTV视频监控系统转换为基于IP的系统,从而保留当前的设备和基础设施。除了这些公司不仅提供硬件解决方案,还提供软件,还有很多其他公司主要关注IP视频监控系统的软件部分,为小型或企业业务提供完整的解决方案,

在文章中,我想分享一些使用不同制造商的不同IP摄像机和视频服务器的经验。提供的信息主要与从您自己的应用程序访问这些摄像头有关,这可能是一个简单的应用程序,以满足您的个人需求,或更复杂,甚至接近某种视频监控应用程序。

As a demo application to the article, I am providing a C# application, which allows single camera viewing as well as multiple camera viewing simultaneously. The application allows simultaneous view of not only several cameras from a single video server, but allows many different cameras of different manufacturers at a time. The range of supported video sources by the application is:作为文章的演示应用程序,我提供了一个C#应用程序,它允许单个摄像机查看以及同时多个摄像机查看。该应用程序不仅可以同时查看来自单个视频服务器的多个摄像机,而且可以同时允许不同制造商的许多不同摄像机。应用程序支持的视频源范围是:

  • Continuously updating JPEG source;
  • MJPEG (motion JPEG) streams;
  • Some Axis cameras and video servers (205, 206, 2100, 2110, 2120, 2130R, 2400, 2401, 2460);
  • D-Link cameras (JPEG support only);
  • Panasonic cameras;
  • PiXORD cameras;
  • StarDot cameras (NetCam, Express 6);
  • Local devices, which support the DirectShow interface;
  • MMS (Microsoft Media Services) streams.
  • 不断更新JPEG源码;
  • MJPEG(动态JPEG)流;
  • 一些安讯士摄像机和视频服务器(205,206,2100,2110,2120,2130R,2400,2401,2460);
  • D-Link相机(仅支持JPEG);
  • 松下相机;
  • PiXORD相机;
  • StarDot相机(NetCam,Express 6);
  • 本地设备,支持DirectShow接口;
  • MMS(Microsoft媒体服务)流。

What are IP cameras ?什么是IP摄像头?

The main difference and advantage of IP cameras is that they provide output in digital form, and can be plugged directly to an Ethernet switch and accessed over an IP network. To achieve this, IP cameras not only have the camera, but also a small computer on board, which usually run embedded Linux. The purpose of this computer is to:IP摄像机的主要区别和优势在于它们以数字形式提供输出,并且可以直接插入以太网交换机并通过IP网络进行访问。为实现这一目标,IP摄像机不仅拥有摄像头,还拥有一台小型计算机,通常运行嵌入式Linux。这台电脑的目的是:

  1. convert an analogue image to a compressed digital image (some cameras/servers have an additional compression microprocessor in addition to the main CPU for the purpose);
  2. provide access to the image via IP network (usually these cameras run a web server, which provide the ability not only for accessing digital images, but also camera configuration information through the HTTP protocol).
  3. 将模拟图像转换为压缩数字图像(某些相机/服务器除此之外还有一个额外的压缩微处理器用于此目的);
  4. 通过IP网络提供对图像的访问(通常这些摄像机运行Web服务器,不仅可以访问数字图像,还可以通过HTTP协议访问摄像机配置信息)。
  5. Video servers are much more sophisticated devices, and usually come without cameras. Instead of this, they are equipped with several video input connectors (usually from 1 to 6), and the user may plug any analogue camera he would like to these connectors. Like IP cameras, video servers also convert image from analogue cameras to digital form and provide access to it through an IP network, but they also provide additional options for video archive creation (for this reason, video servers are equipped with hard drives).

The fact that IP cameras and servers can be accessed over an IP network is a great benefit. It allows monitoring not only from the actual location of these cameras, where you have specially equipped monitoring systems, it also allows doing it from any other IP-enabled point of the world using special video surveillance applications, like web browsers (see image below). And do it as from a usual workstation, or from PDAs and cell phones. The range of applications for IP-enabled video solutions is far away from just doing monitoring and storing video archives. The digital output of these cameras/servers allows them to be easily integrated with hundreds of applications:

视频服务器是更复杂的设备,通常没有摄像头。取而代之的是,它们配备了几个视频输入连接器(通常从1到6),用户可以将他想要的任何模拟摄像头插入这些连接器。与IP摄像机一样,视频服务器也将图像从模拟摄像机转换为数字形式,并通过IP网络提供对它的访问,但它们还为视频存档创建提供了额外的选项(因此,视频服务器配备了硬盘驱动器)。

可以通过IP网络访问IP摄像机和服务器的事实是一个很大的好处。它不仅可以监控这些摄像机的实际位置,也可以监控系统,还可以使用特殊的视频监控应用程序(如下图所示)从世界上任何其他具有IP功能的地方进行监控。 。并且可以从通常的工作站,或从PDA和手机进行。支持IP的视频解决方案的应用范围远不仅仅是监控和存储视频档案。这些摄像机/服务器的数字输出使它们可以轻松地与数百种应用程序集成:

  • motion detection/tracking (in the whole video frame, or in specified areas of interest);
  • traffic control and plate numbers recognition;
  • people tracking with ability for person identification;
  • etc.
  • 运动检测/跟踪(在整个视频帧中,或在指定的感兴趣区域);
  • 交通管制和车牌号码识别;
  • 跟踪人员识别能力的人;
  • 等等

Camera access from web browser

Video formats视频格式

The simplest video format, which is supported by almost all IP video cameras/servers, cannot be even called a video format. The format is just usual JPEG. Most cameras allow retrieving a single image from them by accessing a special URL (should be documented by the camera manufacturer). For example, the following URL allows retrieving an image from an Axis camera: http://webcam.mmhk.cz/axis-cgi/jpg/image.cgi .

This approach has advantages and disadvantages. The disadvantage is that it is required to send a new HTTP request to the camera’s web server each time you need to retrieve a new image, which adds some speed loss because of the extra data (HTTP headers) being sent/received. The advantage is that a monitoring application can easily control the maximum frame rate on its own – it will access the URL to get the next frame with any arbitrary speed (once per minute or 15 times per second, if the network and camera speed allows it).

几乎所有IP视频摄像机/服务器都支持最简单的视频格式,甚至不能称为视频格式。格式通常是JPEG。大多数相机允许通过访问特殊URL从中检索单个图像(应由相机制造商记录)。例如,以下URL允许从Axis相机检索图像:http//webcam.mmhk.cz/axis-cgi/jpg/image.cgi 。

这种方法有优点和缺点。缺点是每次需要检索新映像时都需要向摄像机的Web服务器发送新的HTTP请求,这会因为发送/接收额外数据(HTTP标头)而增加一些速度损失。优点是监控应用程序可以轻松控制自身的最大帧速率 - 它将访问URL以任意速度获取下一帧(每分钟一次或每秒15次,如果网络和摄像机速度允许的话) )。

The second popular format is MJPEG (Motion JPEG). This format allows to download not just a single JPEG image, but a stream of JPEGs. As in the case of the normal JPEG format, the client application does an HTTP request to a special camera’s URL, like this one: http://146.176.65.10/axis-cgi/mjpg/video.cgi . But the camera replies to this request with not just a single JPEG, but with a stream of JPEGs delimited with a special delimiter, which is specified in one of the HTTP headers. When the client application does not want to receive video data any more, it closes the connection with the camera.

第二种流行的格式是MJPEG(Motion JPEG)。此格式不仅可以下载单个JPEG图像,还可以下载JPEG流。与普通JPEG格式的情况一样,客户端应用程序对特殊摄像机的URL执行HTTP请求,如下所示:http//146.176.65.10/axis-cgi/mjpg/video.cgi。但是摄像机不仅使用单个JPEG来回复此请求,而是使用特殊分隔符分隔的JPEG流,该分隔符在其中一个HTTP标头中指定。当客户端应用程序不再需要接收视频数据时,它会关闭与摄像头的连接。

The MJPEG approach seems to be much better, because it has one obvious advantage - it requires sending an HTTP request only once and then receiving JPEG from the camera continuously. But in this approach, you cannot control the frame rate so easily. Accessing such an MJPEG URL, your camera will feed you with some sort of predefined frame rate. In case you would like to change it, you will need to add some extra parameters to the URL. This sounds not so problematic, but in reality, it may lead to some problems. I’ll try to describe the most common one. Suppose you requested (or it was the default setting) 15 frames per second from a certain camera. But, it so happened, that somewhere on the way between you and your camera network speed went down and you receive only 5 frames per second. Suppose your camera has buffer for 30 frames, for example. So, your camera generated 30 frames per 2 seconds, but you consumed them only per 6 seconds. That means that you will see the last frame with a 4 seconds delay – which will be too late in most cases. Of course it is just a sample, cameras will flush their buffers from time to time and do something else to avoid such sort of delays. But, here is a real sample I saw. One guy once entered a room monitored by some camera, spent there a short period of time, and then he went to another room and saw himself walking in that previous room in the camera monitoring application (the application provided by the camera manufacturer).

Many cameras from many different manufacturers support much more than just JPEG or MJPEJ formats. There are cameras which support MPEG-2, and some others that support MPEG-4. Also, some cameras support not only video, but sound transmission as well, and even bidirectional.

Further in this article, I will talk a little bit more about accessing some cameras - accessing single JPEG frames, and MJPEG streams (the MPEG format is not covered by the article). Most camera manufacturers provide APIs and SDKs on their sites, so these information could be learned in more detail.

MJPEG方法似乎要好得多,因为它有一个明显的优势 - 它只需要发送一次HTTP请求,然后连续从相机接收JPEG。但在这种方法中,您无法轻松控制帧速率。访问这样的MJPEG URL,您的相机将为您提供某种预定义的帧速率。如果您想要更改它,您需要在URL中添加一些额外的参数。这听起来不是那么成问题,但实际上,它可能会导致一些问题。我将尝试描述最常见的一个。假设您从某个摄像头请求(或者是默认设置)每秒15帧。但是,它发生了,你和你的相机网络速度之间的某个地方的速度下降,你每秒只收到5帧。例如,假设您的相机有30帧的缓冲区。因此,您的相机每2秒产生30帧,但您每6秒仅消耗一帧。这意味着您将看到延迟4秒的最后一帧 - 在大多数情况下这将为时已晚。当然它只是一个样本,摄像机会不时刷新缓冲区并做其他事情以避免这种延迟。但是,这是我看到的真实样本。一个人曾经进入一个由一些相机监控的房间,在那里呆了很短的时间,

许多不同制造商的许多相机支持的不仅仅是JPEG或MJPEJ格式。有些摄像机支持MPEG-2,还有一些支持MPEG-4。此外,有些相机不仅支持视频,还支持声音​​传输,甚至双向。

在本文中,我将更多地讨论访问一些相机 - 访问单个JPEG帧和MJPEG流(文章未涵盖MPEG格式)。大多数相机制造商在其网站上提供API和SDK,因此可以更详细地了解这些信息。

Accessing JPEGs and MJPEGs访问JPEG和MJPEG

Displaying data from any JPEG source (camera) is really simple – you just need to continuously create HTTP requests to the source, download response data, and extract a bitmap from them. Here is a quick sample of retrieving a single JPEG frame from an IP camera:显示来自任何JPEG源(摄像头)的数据非常简单 - 您只需要不断创建对源的HTTP请求,下载响应数据,并从中提取位图。以下是从IP摄像头检索单个JPEG帧的快速示例:

string sourceURL = "http://webcam.mmhk.cz/axis-cgi/jpg/image.cgi";
byte[] buffer = new byte[100000];
int read, total = 0;
// create HTTP request
HttpWebRequest req = (HttpWebRequest) WebRequest.Create( sourceURL );
// get response
WebResponse resp = req.GetResponse( );
// get response stream
Stream stream = resp.GetResponseStream( );
// read data from stream
while ( ( read = stream.Read( buffer, total, 1000 ) ) != 0 )
{
total += read;
}
// get bitmap
Bitmap bmp = (Bitmap) Bitmap.FromStream(
new MemoryStream( buffer, 0, total ) );

But, don't forget that most cameras are not free with open access like in the sample above. Most probably, you will want to protect your camera with a password, which should be specified somehow:但是,不要忘记大多数相机都不是免费的,可以像上面的示例那样使用开放式访问。最有可能的是,您需要使用密码保护您的相机,密码应以某种方式指定:

// create HTTP request  
HttpWebRequest req =      (HttpWebRequest) WebRequest.Create( sourceURL );
  // set login and password
  req.Credentials = new NetworkCredential( login, password );  ...

Accessing MJPEG sources is much more complicated. First of all, let's take a look at the response content type. It should look something like this:访问MJPEG源要复杂得多。首先,我们来看看响应内容类型。它应该看起来像这样:

multipart/x-mixed-replace; boundary=--myboundary

Maybe, it will not look exactly the same, but its type will be multipart/x-mixed-replace for sure, followed by a certain boundary. In this case, the boundary value is "--myboundary" . Now, let's take a look at an actual stream data:也许,它看起来不会完全一样,但它的类型multipart/x-mixed-replace肯定会是 ,然后是一定的边界。在这种情况下,边界值是"--myboundary" 。现在,我们来看看实际的流数据:

--myboundary
Content-Type: image/jpeg

... image binary data ...

--myboundary
Content-Type: image/jpeg

... image binary data ...

--myboundary
Content-Type: image/jpeg

...

 

 

Summarizing this all together, the algorithm of accessing an MJPEG source becomes clear:

  1. Parse response content type to extract the actual boundary value;
  2. Read the initial portion of the stream, searching for the first boundary;
  3. Read binary data until the next boundary;
  4. Extract an image from the read buffer;
  5. Process the image (display, do whatever else);
  6. Continue with steps 3-5 in a loop.

Actually, the idea of accessing an MJPEG source does not look so complicated as I stated before, but it is for sure, that its implementation will be not so trivial as in the case of the JPEG source.

总而言之,访问MJPEG源的算法变得清晰:

  1. 解析响应内容类型以提取实际边界值;
  2. 读取流的初始部分,搜索第一个边界;
  3. 读取二进制数据直到下一个边界;
  4. 从读缓冲区中提取图像;
  5. 处理图像(显示,做其他事情);
  6. 继续循环中的步骤3-5。

实际上,访问MJPEG源代码的想法并不像我之前所说的那么复杂,但可以肯定的是,它的实现并不像JPEG源代码那样简单。

Axis cameras and video servers安讯士摄像机和视频服务器

Axis cameras and video servers seem to be the best IP video cameras I managed to work. From the user's perspective, these cameras provide good video quality and frame rate, as well as it is very easy to install them and configure. From the programmer's perspective, these devices are even better – the company provides the best developer's documentation I’ve ever seen for IP cameras. The company provides complete documentation on how to access these cameras over HTTP as well as provides an SDK [^].

The following are URL formats to access JPEG and MJPEG stream of Axis IP cameras/servers:

安讯士摄像机和视频服务器似乎是我设法工作的最好的IP摄像机。从用户的角度来看,这些摄像机提供了良好的视频质量和帧速率,并且非常容易安装和配置。从程序员的角度来看,这些设备甚至更好 - 该公司提供了我见过的最好的开发人员的IP摄像机文档。该公司提供了有关如何通过HTTP访问这些摄像机的完整文档,并提供了SDK [ ^ ]。

以下是访问Axis IP摄像机/服务器的JPEG和MJPEG流的URL格式:

JPEG:
  http://<servername>/axis-cgi/jpg/image.cgi
MJPEG:
  http://<servername>/axis-cgi/mjpg/video.cgi

Both of these URLs may accept some parameters, which may make impact on the result. The most popular parameters are resolution (to specify the desired size of the video output), camera (to specify the camera’s number in the case of a video server), and the desired frame rate (only for MJPEG sources):这两个URL都可能接受一些参数,这些参数可能会对结果产生影响。最常用的参数是分辨率(指定视频输出的所需大小),摄像机(在视频服务器的情况下指定摄像机的编号)和所需的帧速率(仅适用于MJPEG信号源):

Samples:
  http://<servername>/axis-cgi/jpg/image.cgi?resolution=320x240
  http://<servername>/axis-cgi/mjpg/video.cgi?camera=2
  http://<servername>/axis-cgi/mjpg/video.cgi?camera=2&des_fps=5

To get the complete HTTP API and the list of all supported parameters, please, refer the Axis support web site.要获取完整的HTTP API和所有支持的参数列表,请参阅Axis支持网站。

StarDot cameras and video servers StarDot相机和视频服务器

It looks like StarDot does not have a great range of IP cameras and video servers, and the range did not change for the past two years. All they have for now is one model of IP camera and one model of video server. As to me, they have the only benefit – their video server supports up to 6 analogue cameras. But everything else does not make them a competitor to such companies like Axis. For example, the frame rate of their IP cameras is really small (not acceptable for security) and these cameras do not support MJPEG. The amount of information for developers also seems to be poor.

URL formats to access their products are really simple:

看起来StarDot没有大量的IP摄像机和视频服务器,而且这个范围在过去两年里没有变化。他们现在拥有的只有一款IP摄像头和一款视频服务器。至于我,他们有唯一的好处 - 他们的视频服务器最多支持6个模拟摄像头。但其他一切并不能使他们成为像Axis这样的公司的竞争对手。例如,他们的IP摄像机的帧速率非常小(安全性不可接受),并且这些摄像机不支持MJPEG。开发人员的信息量似乎也很差。

访问其产品的URL格式非常简单:

StarDot NetCam:
http://<servername>/netcam.jpg
StarDot Express 6 (video server)
http://<servername>/jpeg.cgi?<cameranumber>
http://<servername>/jpeg.cgi?3

PiXORD cameras

The product range of PiXORD mostly consists of different models of IP cameras, which seem to be rather nice cameras providing good quality and frame rate and supporting MJPEG streams. The company provides SDK for their products, but it becomes accessible only after the registration procedure.

Here are URL formats to access their IP cameras:

的产品系列主要由不同型号的IP摄像机组成,这些摄像机似乎是相当不错的摄像机,可提供良好的质量和帧速率,并支持MJPEG流。该公司为其产品提供SDK,但只有在注册程序后才能访问。

以下是访问其IP摄像头的URL格式:

JPEG:
http://<servername>/images<channel><resolution>
http://<servername>/images1sif
MJPEG:
http://<servername>/getimage?camera=<channel>[&fmt=<resolution>][&delay=<delay>]
http://<servername>/getimage?camera=1&fmt=sif&delay=10

 

D-Link cameras

D-Link has a wide range of IP video cameras, and is known as one of the first companies who started to use MPEG-4 in their cameras. Actually, these cameras have MPEG video as their primary goal – they don’t support anything else, like MJPEG. Most of their cameras also have audio support, and some models even have bidirectional audio support. From the user's perspective, it is rather simple to install and configure these cameras, which support a lot of different settings. From the developer's perspective, these cameras are not so easy. The company does not want to share lots of development information, and it is really hard to find any developer's information on their site. This fact makes these cameras not so nice in case you want to develop your own surveillance software instead of using their own (which is buggy). By the way, that story I told before about a guy who viewed himself walking in another room was about a D-Link camera.

At this moment, I know only one way to access D-Link cameras (JPEG format):

d-Link的拥有广泛的IP摄像机,被称为最早开始在相机中使用MPEG-4的公司之一。实际上,这些相机以MPEG视频为主要目标 - 他们不支持任何其他内容,如MJPEG。他们的大多数相机也有音频支持,有些型号甚至还支持双向音频。从用户的角度来看,安装和配置这些相机非常简单,这些相机支持许多不同的设置。从开发人员的角度来看,这些相机并不那么容易。该公司不希望分享大量的开发信息,而且很难在其网站上找到任何开发人员的信息。这个事实使得这些相机不太好,以防你想要开发自己的监控软件而不是使用自己的(这是有缺陷的)。顺便说一句,我之前讲过的一个关于一个看着自己走在另一个房间的人的故事是关于D-Link相机的。

此刻,我只知道一种访问D-Link相机的方式(JPEG格式):

http://<servername>/cgi-bin/video.jpg

Some other video sources其他一些视频来源

Many other video sources can be accessed using other approaches than HTTP. For example, you can easily access such video sources like local web cameras connected to your PC through an USB port, or you can access remote video streams over MMS (Microsoft Media Services). One of the most common approaches to access these two types of video sources is to use DirectShow. The sample application demonstrates the technique, and you can study more about it using several other articles on CodeProject dedicated to the topic.

可以使用除HTTP之外的其他方法来访问许多其他视频源。例如,您可以轻松访问此类视频源,例如通过USB端口连接到PC的本地网络摄像头,或者您可以通过MMS(Microsoft媒体服务)访问远程视频流。访问这两种类型的视频源的最常用方法之一是使用DirectShow。示例应用程序演示了该技术,您可以使用专门针对该主题的CodeProject上的其他几篇文章来研究它。

The application code应用程序代码

The main goal of the application was to make it flexible and extensible. The application itself can communicate with any video source – it may be an IP video camera or a server, it may be a local camera attached to USB, it may be an MMS stream from a remote server, or it may be any other video source. And more of it, the application can work with all these video sources simultaneously, displaying them all on a single screen.

Another main feature of the application is that it can be easily extended on the fly. The main application module knows nothing about any video sources and how to configure them; it knows only how to display them. All the logic of communication with a particular video source is hidden in separate modules, and the application is not tightly coupled with them. If you have a new video source and you want the application to work with it, you don’t need to change any line of code in the application itself. You just need to create a new module which is responsible for communication with your custom video source, and place the module to the application’s folder.

该应用程序的主要目标是使其灵活和可扩展。应用程序本身可以与任何视频源通信 - 它可以是IP视频摄像机或服务器,它可以是连接到USB的本地摄像机,它可以是来自远程服务器的MMS流,或者它可以是任何其他视频源。更重要的是,该应用程序可以同时处理所有这些视频源,并将它们全部显示在一个屏幕上。

该应用程序的另一个主要特点是它可以在运行中轻松扩展。主应用程序模块对任何视频源以及如何配置它们一无所知; 它只知道如何显示它们。与特定视频源通信的所有逻辑都隐藏在单独的模块中,并且应用程序不与它们紧密耦合。如果您有新的视频源,并且希望应用程序使用它,则无需更改应用程序本身中的任何代码行。您只需创建一个负责与自定义视频源通信的新模块,并将模块放置到应用程序的文件夹中。

The key approach to implementing the idea was to create an interface which describes all the common functionality of all video sources. The interface is IVideoSource. Then, a set of classes were created which implement the above interface and encapsulate all the routines for the communication with the particular video source and extracting image data from it. Each such class is fully responsible for all the work required to provide the application with images to display. The code of these classes does not go to the application code, but it goes to separate assemblies, which represent those application modules, which can be easily added to the application to extend its functionality.

Each video source module may contain an arbitrary amount of video providers – classes, which provide access to the video source. Most of such modules contain only one video provider, but some of them have several – it may be preferred to group video providers somehow (all video providers to access cameras/servers of one manufacturer goes to the same module, for example).

实现该想法的关键方法是创建一个界面,描述所有视频源的所有常见功能。界面是IVideoSource。然后,创建一组类,这些类实现上述接口并封装用于与特定视频源通信并从中提取图像数据的所有例程。每个这样的类都完全负责为应用程序提供要显示的图像所需的所有工作。这些类的代码不会转到应用程序代码,而是转到单独的程序集,这些程序集代表那些应用程序模块,可以很容易地将它们添加到应用程序中以扩展其功能。

每个视频源模块可以包含任意数量的视频提供者 - 类,其提供对视频源的访问。大多数此类模块仅包含一个视频提供者,但其中一些具有多个 - 可能优先以某种方式对视频提供者进行分组(例如,所有视频提供者访问一个制造商的相机/服务器进入相同的模块)。

All these video providers can be used as complete classes to access different video sources from your application. But there are still two missing things, which are required to implement to make the application extensible and configurable. First of all, all our video providers should be self-describable and self-configurable. For this purpose, two more interfaces were added: IVideoSourceDescription and IVideoSourcePage. Each class, which implements the IVideoSourceDescriptioninterface, provides the name and description of the provider, which allows saving and loading its configuration and the creation of the configured video provider. Classes which implement the IVideoSourcePage interface represent a property page for the video provider configuration. These additional classes also go to the video provider’s modules. Combining all these together makes clear that a simplest module, which contains only one video provider, should contain three classes: provider description, provider configuration page, and the video provided itself.所有这些视频提供程序都可以用作完整的类来访问应用程序中的不同视频源。但是仍然有两个缺失的东西需要实现以使应用程序可扩展和可配置。首先,我们所有的视频提供商都应该是可自我描述和自我配置的。为此,增加了两个接口:IVideoSourceDescription和 IVideoSourcePage。每个实现IVideoSourceDescription接口的类都给出了提供程序的名称和描述,允许保存和加载其配置以及创建配置的视频提供程序。实现IVideoSourcePageinterface的类表示视频提供程序配置的属性页。这些附加类也会转到视频提供者的模块中。将所有这些组合在一起清楚地表明,最简单的模块(仅包含一个视频提供者)应包含三个类:提供者描述,提供者配置页面和提供的视频本身。

And the last thing to make the solution working should be implemented on the application side – the application should find all modules and collect all information about the video source providers which live there. This actually can be done very easily through reflection. First of all, the application searches for all DLL files in the application folder. Then, it tries to load each the file as an assembly and enumerate all types in the assembly, searching for types which implement theIVideoSourceDescription interface. Once such a type is found, it is instantiated and requested to provide the video provider’s name, description, and other information. This module's investigation procedure is called only once on the application startup, but the application can be easily modified to call the procedure by user request (it may be useful if the user added a new video provider module, but does not want to restart the application).使解决方案工作的最后一件事应该在应用程序端实现 - 应用程序应该找到所有模块并收集有关视频源提供者的所有信息。这实际上可以通过反射很容易地完成。首先,应用程序搜索应用程序文件夹中的所有DLL文件。然后,它尝试将每个文件作为程序集加载并枚举程序集中的所有类型,搜索实现该程序的类型IVideoSourceDescription接口。一旦找到这样的类型,就将其实例化并请求提供视频提供者的名称,描述和其他信息。该模块的调查过程仅在应用程序启动时调用一次,但可以轻松修改应用程序以通过用户请求调用该过程(如果用户添加了新的视频提供程序模块,但不想重新启动应用程序,则可能很有用)。

Some underwater stones待补充细节

There is one known bug of the .NET 1.0 (and 1.1) framework, which is actually not a bug, but a feature. But the feature makes a great problem to communication with some cameras in MJPEG mode. The problem is that some MJPEG video sources don’t conform to the HTTP standard 100%. Or to say it in a little bit different way – Microsoft was too picky, and implemented the first version of their framework very strictly conforming to the HTTP standard. Some cameras have something very little missing in the HTTP header and .NET immediately refuses to work with them, generating a WebException with the following description:有一个已知的.NET 1.0(和1.1)框架的错误,它实际上不是一个错误,而是一个功能。但该功能在MJPEG模式下与某些相机的通信成为一个很大的问题。问题是一些MJPEG视频源不符合100%的HTTP标准。或者以稍微不同的方式说 - 微软过于挑剔,并且非常严格地遵守HTTP标准实现了他们框架的第一个版本。有些相机在HTTP标头中缺少一些东西,.NET立即拒绝使用它们,生成一个 WebException带有以下描述:

The underlying connection was closed:
       The server committed an HTTP protocol violation.

Fortunately, it is a known feature of .NET and it is possible to fix it. First of all, you will need to get at least the 1.1 version of the framework and install the first service pack for it. Then, you will need to create an application configuration file for your application, and place it in the application folder. Here is a minimal content of the file to make the MJPEG sources working:幸运的是,它是.NET的一个已知功能,可以修复它。首先,您需要至少获得1.1版本的框架并为其安装第一个Service Pack。然后,您需要为应用程序创建应用程序配置文件,并将其放在应用程序文件夹中。以下是使MJPEG源工作的文件的最小内容:

<configuration>
<startup>
<supportedRuntime version="v1.1.4322" />
</startup>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>

The second problem is that the HttpWebRequest class of .NET has such a feature as connection group. By default, all HTTP requests are created in the same connection group, but each connection group has a limit of simultaneously opened connections. So, this creates such a problem, that you cannot monitor many cameras at the same time. The good news is that the problem can also be solved easily – the HttpWebRequest class has a property called ConnectionGroupName, so you can manage connections grouping on your own.第二个问题是HttpWebRequest.NET类具有连接组这样的功能。默认情况下,所有HTTP请求都在同一连接组中创建,但每个连接组都具有同时打开的连接的限制。因此,这会产生这样的问题,即您无法同时监控多个摄像头。好消息是问题也可以轻松解决 - HttpWebRequest类有一个名为的属性 ConnectionGroupName,因此您可以自己管理连接分组。

Conclusion

The attached application demonstrates all of the techniques described in the article, and allows monitoring of many different cameras from different manufacturers. The application lets you monitor a single camera, or several cameras on a single screen at a time (full screen mode supported). Please don’t consider the application to be a complete video surveillance application, but treat it as a demo, as a proof-of-concept, as a starting point for your own software. However, the application may be useful for many personal reasons.

You can find one more interesting application here on CodeProject, which also works with video cameras, and is based on techniques I described in the article.

The demo application includes many free cameras from all over the world: Las Vegas, Stuttgart Airport, and many many more. You can easily find more freely accessible cameras on the Internet, add them to the application, and enjoy monitoring them.

附带的应用程序演示了本文中描述的所有技术,并允许监视来自不同制造商的许多不同相机。该应用程序允许您一次监视单个摄像机或单个屏幕上的多个摄像机(支持全屏模式)。请不要将该应用程序视为完整的视频监控应用程序,而是将其视为演示,作为概念验证,作为您自己软件的起点。但是,该应用程序可能因许多个人原因而有用。

您可以在CodeProject上找到一个更有趣的应用程序,它也适用于摄像机,并且基于我在文章中描述的技术。

演示应用程序包括来自世界各地的许多免费摄像头:拉斯维加斯,斯图加特机场等等。您可以轻松地在Internet上找到更多可自由访问的摄像头,将其添加到应用程序中,并享受监控。

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内