17611538698
webmaster@21cto.com

C#开源实现MJPEG流传输

资讯 0 12691 2017-04-27 12:01:16
本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载。

许久以前写了篇文章《基于.NET打造IP智能网络视频监控系统》,记录和介绍了自己几年来积累和演练的一个系统。发现几个月过去了,没有任何进展。

目前已经实现了 UDP+RTP 方式在不同物理机之间的媒体流传输。当然,由于没有基于 .NET 的媒体流压缩实现,所以直接传输的裸图 Bitmap。不过要求不高,帧率低一些,机器性能强一些,看着也很流畅。




能在桌面客户端上看到视频图像的功能已经完成了。下面需要考虑,如何通过浏览器来查看视频。

在不考虑使用 Flash、ActiveX 的条件下,貌似只能选择 MJPEG 方式。目前还没有研究在 HTML5 下视频是如何处理的,以后有时间可以探索。

[h1]目录[/h1]

http://en.wikipedia.org/wiki/Motion_JPEG
[*]http://zh.wikipedia.org/wiki/Motion_JPEG[/*]
[*]http://baike.baidu.com/view/2098077.htm[/*]
[/list]
当然,我主要关注 MJPEG over HTTP 这段。

M-JPEG over HTTP
HTTP streaming separates each image into individual HTTP replies on a specified marker. RTP streaming creates packets of a sequence of JPEG images that can be received by clients such as QuickTime or VLC.
In response to a GET request for a MJPEG file or stream, the server streams the sequence of JPEG frames over HTTP. A special mime-type content type multipart/x-mixed-replace;boundary=<boundary-name> informs the client to expect several parts (frames) as an answer delimited by <boundary-name>. This boundary name is expressly disclosed within the MIME-type declaration itself. The TCP connection is not closed as long as the client wants to receive new frames and the server wants to provide new frames. Two basic implementations of a M-JPEG streaming server are cambozola and MJPG-Streamer. The more robust ffmpeg-server also provides M-JPEG streaming support.


也就是说,建立 HTTP 连接后,服务端在 Response 消息中先发一个数据头 Header 告诉客户端,我后面的都是 JPEG 图片。图片之间使用 boundary-name 来区分,每个图片前都有自己的数据头来描述图片数据长度。



1 /// <summary>
2 /// 流头部
3 /// </summary>
4 public string StreamHeader
5 {
6 get
7 {
8 return "HTTP/1.1 200 OK" +
9 " " +
10 "Content-Type: multipart/x-mixed-replace; boundary=" + this.Boundary +
11 " ";
12 }
13 }




1 /// <summary>
2 /// 图片头部
3 /// </summary>
4 public string PayloadHeader
5 {
6 get
7 {
8 return " " +
9 this.Boundary +
10 " " +
11 "Content-Type: image/jpeg" +
12 " " +
13 "Content-Length: " + _contentLengthString +
14 " ";
15 }
16 }


这里的 Boundary 可以是任意字符串,只要你觉得唯一并能区分即可,比如我可以设置为“--dennisgao”。



1 public void Start()
2 {
3 _server.Start(10);
4 _server.ClientConnected += new EventHandler<TcpClientConnectedEventArgs>(OnClientConnected);
5 _server.ClientDisconnected += new EventHandler<TcpClientDisconnectedEventArgs>(OnClientDisconnected);
6 }
7
8 public void Stop()
9 {
10 _server.Stop();
11 _server.ClientConnected -= new EventHandler<TcpClientConnectedEventArgs>(OnClientConnected);
12 _server.ClientDisconnected -= new EventHandler<TcpClientDisconnectedEventArgs>(OnClientDisconnected);
13 }
14
15 private void OnClientConnected(object sender, TcpClientConnectedEventArgs e)
16 {
17 _clients.AddOrUpdate(e.TcpClient.Client.RemoteEndPoint.ToString(), e.TcpClient, (n, o) => { return e.TcpClient; });
18 }
19
20 private void OnClientDisconnected(object sender, TcpClientDisconnectedEventArgs e)
21 {
22 TcpClient clientToBeThrowAway;
23 _clients.TryRemove(e.TcpClient.Client.RemoteEndPoint.ToString(), out clientToBeThrowAway);
24 }


这里可以参考两篇文章中的实现。




1 private void WriteStreamHeader()
2 {
3 if (_clients.Count > 0)
4 {
5 foreach (var item in _clients)
6 {
7 Logger.Debug(string.Format(CultureInfo.InvariantCulture,
8 "Writing stream header, {0}, {1}{2}", item.Key, Environment.NewLine, StreamHeader));
9
10 _server.SyncSend(item.Value, StreamHeader);
11
12 TcpClient clientToBeThrowAway;
13 _clients.TryRemove(item.Key, out clientToBeThrowAway);
14 }
15 }
16 }


发送图片数据时,要保证图片的前面是图片头和长度信息,数据尾部要有换行符。



1 private void WritePayload(byte[] payload)
2 {
3 string payloadHeader = this.PayloadHeader.Replace(_contentLengthString, payload.Length.ToString());
4 string payloadTail = " ";
5
6 Logger.Debug(string.Format(CultureInfo.InvariantCulture,
7 "Writing payload header, {0}{1}", Environment.NewLine, payloadHeader));
8
9 byte[] payloadHeaderBytes = _server.Encoding.GetBytes(payloadHeader);
10 byte[] payloadTailBytes = _server.Encoding.GetBytes(payloadTail);
11 byte[] packet = new byte[payloadHeaderBytes.Length + payload.Length + payloadTail.Length];
12 Buffer.BlockCopy(payloadHeaderBytes, 0, packet, 0, payloadHeaderBytes.Length);
13 Buffer.BlockCopy(payload, 0, packet, payloadHeaderBytes.Length, payload.Length);
14 Buffer.BlockCopy(payloadTailBytes, 0, packet, payloadHeaderBytes.Length + payload.Length, payloadTailBytes.Length);
15
16 _server.SendToAll(packet);
17 }


Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载。[/b]

评论