天天看點

C#開源實作MJPEG流傳輸

目前已經實作了 UDP+RTP 方式在不同實體機之間的媒體流傳輸。當然,由于沒有基于 .NET 的媒體流壓縮實作,是以直接傳輸的裸圖 Bitmap。不過要求不高,幀率低一些,機器性能強一些,看着也很流暢。

C#開源實作MJPEG流傳輸

能在桌面用戶端上看到視訊圖像的功能已經完成了。下面需要考慮,如何通過浏覽器來檢視視訊。

在不考慮使用 Flash、ActiveX 的條件下,貌似隻能選擇 MJPEG 方式。目前還沒有研究在 HTML5 下視訊是如何處理的,以後有時間可以探索。

<a href="http://www.cnblogs.com/gaochundong/p/csharp_mjpeg_streaming.html#what_is_mjpeg">什麼是 MJPEG?</a>

<a href="http://www.cnblogs.com/gaochundong/p/csharp_mjpeg_streaming.html#data_header_of_mjpeg">MJPEG資料頭定義</a>

<a href="http://www.cnblogs.com/gaochundong/p/csharp_mjpeg_streaming.html#server_implementation">伺服器端實作</a>

<a href="http://www.cnblogs.com/gaochundong/p/csharp_mjpeg_streaming.html#send_image_data">發送圖檔資料</a>

<a href="http://www.cnblogs.com/gaochundong/p/csharp_mjpeg_streaming.html#show_demo">結果示範</a>

<a href="http://www.cnblogs.com/gaochundong/p/csharp_mjpeg_streaming.html#remote_access">遠端通路</a>

<a href="http://www.cnblogs.com/gaochundong/p/csharp_mjpeg_streaming.html#full_source_code">完整代碼</a>

看這裡:

<a href="http://en.wikipedia.org/wiki/Motion_JPEG">http://en.wikipedia.org/wiki/Motion_JPEG</a>

<a href="http://zh.wikipedia.org/wiki/Motion_JPEG">http://zh.wikipedia.org/wiki/Motion_JPEG</a>

<a href="http://baike.baidu.com/view/2098077.htm">http://baike.baidu.com/view/2098077.htm</a>

當然,我主要關注 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=&lt;boundary-name&gt; informs the client to expect several parts (frames) as an answer delimited by &lt;boundary-name&gt;. 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 來區分,每個圖檔前都有自己的資料頭來描述圖檔資料長度。

C#開源實作MJPEG流傳輸
C#開源實作MJPEG流傳輸
C#開源實作MJPEG流傳輸
C#開源實作MJPEG流傳輸

這裡的 Boundary 可以是任意字元串,隻要你覺得唯一并能區分即可,比如我可以設定為“--dennisgao”。

Http 伺服器其實就是個支援 Tcp 連接配接的伺服器。

C#開源實作MJPEG流傳輸
C#開源實作MJPEG流傳輸

這裡可以參考兩篇文章中的實作。

<a href="http://www.cnblogs.com/gaochundong/archive/2013/04/14/csharp_async_tcp_server.html">C#異步TCP伺服器完整實作</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/04/14/csharp_async_tcp_client.html">C#對TCP用戶端的狀态封裝</a>

首先要保證,對一個HTTP連接配接隻能發一次流頭,因為後面是接連不斷的圖檔資料。當然,發點别的資料用戶端也不會解碼。

C#開源實作MJPEG流傳輸
C#開源實作MJPEG流傳輸

發送圖檔資料時,要保證圖檔的前面是圖檔頭和長度資訊,資料尾部要有換行符。

C#開源實作MJPEG流傳輸
C#開源實作MJPEG流傳輸

在可以成功發送流資訊和圖檔資訊後,就可以在浏覽器上檢視視訊了。當然,我用的 Google Chrome 。IE10 好奇葩,它會把流當成檔案不停的下載下傳,搞不懂。

C#開源實作MJPEG流傳輸

區域網路内的無線裝置,隻要浏覽器支援 MJPEG ,均可以檢視視訊。我測試了 iPad 上的 Safari 是可以的,但 Chrome 卻直接解析成亂碼。

C#開源實作MJPEG流傳輸

當然,如果在路由器上配置轉發規則,就可以在外網通路了。

C#開源實作MJPEG流傳輸
C#開源實作MJPEG流傳輸
C#開源實作MJPEG流傳輸
C#開源實作MJPEG流傳輸

<a></a>

<a>本文轉自匠心十年部落格園部落格,原文連結:http://www.cnblogs.com/gaochundong/p/csharp_mjpeg_streaming.html,如需轉載請自行聯系原作者</a>