[英]C# AForge.Video decoding MJPEG stream over websockets
我正在嘗試在我的 WPF C# 應用程序上顯示 MJPEG 視頻 stream。 我使用的 MJPEG 解碼器庫是AForge.Video 。 到目前為止,我已經設法顯示通過 http 流式傳輸的視頻:
using System.IO;
using System.Windows;
using System.Drawing;
using System.Windows.Media.Imaging;
using AForge.Video;
namespace VideoControl
{
public partial class MainWindow : Window
{
MJPEGStream stream;
public MainWindow()
{
InitializeComponent();
stream = new MJPEGStream("http://mylocaladdress:1220/video"); //Only works over http: and not ws:.
stream.NewFrame += GetNewFrame;
stream.Start();
}
void GetNewFrame(object sender, NewFrameEventArgs e)
{
Bitmap bmp = (Bitmap)e.Frame.Clone();
Dispatcher.Invoke(() => {imgStream.Source = BitmapToImageSource(bmp);}); //Convert bitmap to image source first before applying to image.
}
BitmapImage BitmapToImageSource(Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}
}
}
但是,我的目標是通過 websockets (ws) 而不是 http 獲取視頻 stream。 我將如何 go 為這個庫實現 websocket (ws) 協議? 簡單地用 websocket 地址替換 http 地址是行不通的,因為庫不理解 websocket 協議。
我使用這個FFMPEG 包裝器在 Node-RED 服務器上托管我的 stream。 視頻 stream 在瀏覽器中顯示正常,但在 C# 中顯示不正常。 我在 http 上進行的早期成功測試是使用streameye流式傳輸的。 是否有任何替代AForge.Video的 MJPEG 解碼器支持開箱即用的 websockets?
MJpeg 旨在通過 HTTP 請求進行廣播。 它的解析方法會根據內容類型 header 發生變化,而 WebSockets 中沒有這種方法。 我認為沒有任何庫支持這個開箱即用,但你可以創建一個像 jsmpeg(感謝@jdweng)那樣的解決方法。
或者
您可以基於 AForge 自己的MJPegStream (GitHub) stream 閱讀器編寫自己的 stream 解碼器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.