论坛 产品库 视频 专题 CIO俱乐部 Windows8 实验室 CMO俱乐部 案例

C#开源 实现MJPEG流传输

发布时间:2014-12-09 16:42:00 来源:论坛 作者:译名
关键字:C语言

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

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

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

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

  什么是 MJPEG?

  看这里:

  http://en.wikipedia.org/wiki/Motion_JPEG

  http://zh.wikipedia.org/wiki/Motion_JPEG

  http://baike.baidu.com/view/2098077.htm

  当然,我主要关注 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= informs the client to expect several parts (frames) as an answer delimited by . 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 来区分,每个图片前都有自己的数据头来描述图片数据长度。

  MJPEG数据头定义

  ///

/// 流头部 /// public string StreamHeader { get { return "HTTP/1.1 200 OK" + "\r\n" + "Content-Type: multipart/x-mixed-replace; boundary=" + this.Boundary + "\r\n"; } }

  ///

/// 图片头部 /// public string PayloadHeader { get { return "\r\n" + this.Boundary + "\r\n" + "Content-Type: image/jpeg" + "\r\n" + "Content-Length: " + _contentLengthString + "\r\n\r\n"; } }

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

  服务器端实现

  Http 服务器其实就是个支持 Tcp 连接的服务器。

  private AsyncTcpServer _server; _server = new AsyncTcpServer(Port); _server.Encoding = Encoding.ASCII;

  public void Start() { _server.Start(10); _server.ClientConnected += new EventHandler(OnClientConnected); _server.ClientDisconnected += new EventHandler(OnClientDisconnected); } public void Stop() { _server.Stop(); _server.ClientConnected -= new EventHandler(OnClientConnected); _server.ClientDisconnected -= new EventHandler(OnClientDisconnected); } private void OnClientConnected(object sender, TcpClientConnectedEventArgs e) { _clients.AddOrUpdate(e.TcpClient.Client.RemoteEndPoint.ToString(), e.TcpClient, (n, o) => { return e.TcpClient; }); } private void OnClientDisconnected(object sender, TcpClientDisconnectedEventArgs e) { TcpClient clientToBeThrowAway; _clients.TryRemove(e.TcpClient.Client.RemoteEndPoint.ToString(), out clientToBeThrowAway); }

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

  C#异步TCP服务器完整实现

  C#对TCP客户端的状态封装

  发送图片数据

  首先要保证,对一个HTTP连接只能发一次流头,因为后面是接连不断的图片数据。当然,发点别的数据客户端也不会解码。

  private void WriteStreamHeader() { if (_clients.Count > 0) { foreach (var item in _clients) { Logger.Debug(string.Format(CultureInfo.InvariantCulture, "Writing stream header, {0}, {1}{2}", item.Key, Environment.NewLine, StreamHeader)); _server.SyncSend(item.Value, StreamHeader); TcpClient clientToBeThrowAway; _clients.TryRemove(item.Key, out clientToBeThrowAway); } } }

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

  private void WritePayload(byte[] payload) { string payloadHeader = this.PayloadHeader.Replace(_contentLengthString, payload.Length.ToString()); string payloadTail = "\r\n"; Logger.Debug(string.Format(CultureInfo.InvariantCulture, "Writing payload header, {0}{1}", Environment.NewLine, payloadHeader)); byte[] payloadHeaderBytes = _server.Encoding.GetBytes(payloadHeader); byte[] payloadTailBytes = _server.Encoding.GetBytes(payloadTail); byte[] packet = new byte[payloadHeaderBytes.Length + payload.Length + payloadTail.Length]; Buffer.BlockCopy(payloadHeaderBytes, 0, packet, 0, payloadHeaderBytes.Length); Buffer.BlockCopy(payload, 0, packet, payloadHeaderBytes.Length, payload.Length); Buffer.BlockCopy(payloadTailBytes, 0, packet, payloadHeaderBytes.Length + payload.Length, payloadTailBytes.Length); _server.SendToAll(packet); }

  结果演示

  在可以成功发送流信息和图片信息后,就可以在浏览器上查看视频了。当然,我用的 Google Chrome 。IE10 好奇葩,它会把流当成文件不停的下载,搞不懂。

  远程访问

  局域网内的无线设备,只要浏览器支持 MJPEG ,均可以查看视频。我测试了 iPad 上的 Safari 是可以的,但 Chrome 却直接解析成乱码。

  当然,如果在路由器上配置转发规则,就可以在外网访问了。

  完整代码

  public class MJpegStreamingServer { private static string _contentLengthString = "__PayloadHeaderContentLength__"; private AsyncTcpServer _server; private ConcurrentDictionary _clients; public MJpegStreamingServer(int listenPort) : this(listenPort, "--dennisgao") { } public MJpegStreamingServer(int listenPort, string boundary) { Port = listenPort; Boundary = boundary; _server = new AsyncTcpServer(Port); _server.Encoding = Encoding.ASCII; _clients = new ConcurrentDictionary(); } ///

/// 监听的端口 /// public int Port { get; private set; } /// /// 分隔符 /// public string Boundary { get; private set; } /// /// 流头部 /// public string StreamHeader { get { return "HTTP/1.1 200 OK" + "\r\n" + "Content-Type: multipart/x-mixed-replace; boundary=" + this.Boundary + "\r\n"; } } /// /// 图片头部 /// public string PayloadHeader { get { return "\r\n" + this.Boundary + "\r\n" + "Content-Type: image/jpeg" + "\r\n" + "Content-Length: " + _contentLengthString + "\r\n\r\n"; } } public void Start() { _server.Start(10); _server.ClientConnected += new EventHandler(OnClientConnected); _server.ClientDisconnected += new EventHandler(OnClientDisconnected); } public void Stop() { _server.Stop(); _server.ClientConnected -= new EventHandler(OnClientConnected); _server.ClientDisconnected -= new EventHandler(OnClientDisconnected); } private void OnClientConnected(object sender, TcpClientConnectedEventArgs e) { _clients.AddOrUpdate(e.TcpClient.Client.RemoteEndPoint.ToString(), e.TcpClient, (n, o) => { return e.TcpClient; }); } private void OnClientDisconnected(object sender, TcpClientDisconnectedEventArgs e) { TcpClient clientToBeThrowAway; _clients.TryRemove(e.TcpClient.Client.RemoteEndPoint.ToString(), out clientToBeThrowAway); } public void Write(Image image) { if (_server.IsRunning) { byte[] payload = BytesOf(image); WriteStreamHeader(); WritePayload(payload); } } private void WriteStreamHeader() { if (_clients.Count > 0) { foreach (var item in _clients) { Logger.Debug(string.Format(CultureInfo.InvariantCulture, "Writing stream header, {0}, {1}{2}", item.Key, Environment.NewLine, StreamHeader)); _server.SyncSend(item.Value, StreamHeader); TcpClient clientToBeThrowAway; _clients.TryRemove(item.Key, out clientToBeThrowAway); } } } private void WritePayload(byte[] payload) { string payloadHeader = this.PayloadHeader.Replace(_contentLengthString, payload.Length.ToString()); string payloadTail = "\r\n"; Logger.Debug(string.Format(CultureInfo.InvariantCulture, "Writing payload header, {0}{1}", Environment.NewLine, payloadHeader)); byte[] payloadHeaderBytes = _server.Encoding.GetBytes(payloadHeader); byte[] payloadTailBytes = _server.Encoding.GetBytes(payloadTail); byte[] packet = new byte[payloadHeaderBytes.Length + payload.Length + payloadTail.Length]; Buffer.BlockCopy(payloadHeaderBytes, 0, packet, 0, payloadHeaderBytes.Length); Buffer.BlockCopy(payload, 0, packet, payloadHeaderBytes.Length, payload.Length); Buffer.BlockCopy(payloadTailBytes, 0, packet, payloadHeaderBytes.Length + payload.Length, payloadTailBytes.Length); _server.SendToAll(packet); } private byte[] BytesOf(Image image) { MemoryStream ms = new MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] payload = ms.ToArray(); return payload; } }


比特微信账号
比特微信账号

微信扫一扫
关注Chinabyte

返回首页 长微博 返回顶部