Boyfinn
Boyfinn

Reputation: 311

C# AForge.Video decoding MJPEG stream over websockets

I'm trying to display an MJPEG video stream on my WPF C# application. The MJPEG decoder library I'm using is AForge.Video. So far I've managed to display video streamed over http with this:

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;
            }
        }
    }
}

However, my goal is to get the video stream over websockets (ws) instead of http. How would i go about implementing the websocket (ws) protocol for this library? Simply replacing the http address with a websocket address doesn't work, since the library doesn't understand the websocket protocol.

I'm hosting my stream on a Node-RED server using this FFMPEG wrapper. The video stream shows up fine in browser, but not in C#. The earlier successful tests i conducted over http were streamed using streameye. Are there any alternative MJPEG decoders to AForge.Video that support websockets out of the box?

Upvotes: 4

Views: 891

Answers (1)

Erdogan Kurtur
Erdogan Kurtur

Reputation: 3685

MJpeg is designed to broadcast over an HTTP request. Its parsing methodology changes according to the content type header, which you will not have in WebSockets. I do not think any library support this out of box, but you can create a workaround like jsmpeg (thanks @jdweng) did.

  • Your application creates a simple HTTP server that serves MJpeg stream for the AForge.Video ()
  • You connect to your video source over WebSockets and stream that result to your HTTP endpoint
  • Connect AForge.Video instance to your local web server, voila everything works.

Or

you can write your own stream decoder based on AForge's own MJPegStream (GitHub) stream reader.

Upvotes: 2

Related Questions