views:

11282

answers:

8

I have images being sent to my database from a remote video source at about 5 frames per second as JPEG images. I am trying to figure out how to get those images into a video format so I can stream a live video feed to Silverlight.

It seems to make sense to create a MJPEG stream but I'm having a few problems. Firstly I was trying to stream via an HTTP request so I didn't have a deal with sockets but maybe this is breaking my code.

If I try surf to my stream from QT I get a video error, Media player shows the first frame image and Silverlight crashes :)

Here is the code that streams - since I content type used this way can only be sent once I know that it isn't ideal and might be the root cause. All images are coming in via a LINQ2SQL object.

I did already try simply updating the image source of an image control in Silverlight but the flicker isn't acceptable. If Silverlight doesn't support MJPEG then no point even continuing but it looks like it does. I do have access to the h.264 frames coming in but that seemed more complicated via MP4.

    Response.Clear();
    Response.ContentType = "multipart/x-mixed-replace; boundary=--myboundary";
    ASCIIEncoding ae = new ASCIIEncoding();
    HCData data = new HCData();
    var videos = (from v in data.Videos
                 select v).Take(50); // sample the first 50 frames
    foreach (Video frame in videos)
    {
        byte[] boundary = ae.GetBytes("\r\n--myboundary\r\nContent-Type: image/jpeg\r\nContent-Length:" + frame.VideoData.ToArray().Length + "\r\n\r\n");
        var mem = new MemoryStream(boundary);
        mem.WriteTo(Response.OutputStream);
        mem = new MemoryStream(frame.VideoData.ToArray());
        mem.WriteTo(Response.OutputStream);
        Response.Flush();
        Thread.Sleep(200);
    }

Thanks!

EDIT: I have the stream working in firefox so if I surf to the page I see video! but nothing else accepts the format. Not IE, SL, Media player - nothing.

A: 

First, write your mjpeg frames out to separate files. You should then be able to open these in Phototshop (this will independently verify that you are parsing the stream correctly). If this fails, by bet is that you have HTTP headers embedded in your image data.

dicroce
Yes I have done that - the images get used for other things independent of the Video so I can view them and they look good.
typemismatch
Does that mean you've viewed the frames after they've been handled by your program?
junkforce
Yes - I also have it working in Firefox and I can see perfect video but yes, I've also written the images as single jpg files to disk and they are fine.
typemismatch
A: 

Have you looked at various web cam setups that exist on the net? A lot of them do some sort of low res update without flicker. You should be able to reverse engineer these types of sites for additional clues to your problem.

Some sites create a GIF animation, maybe that is an option so that the user can see the past minute or so.

John Dyer
A: 

I'm far from being an expert in MJPEG streaming, but looking at the source of mjpg-streamer on sourcefourge I think you should send each frame separately, writing the boundary before and after each of them. You should of course not write the content-type in the closing boundary.

csgero
+2  A: 

I did MJPEG a long time ago (3-4 years ago) and I'm scratching my head trying to remember the details and I simply can't. But, if its possible, I would suggest finding some kind of web site that streams MJPEG content and fire up wireshark/ethereal and see what you get over the wire. My guess is you are missing some required HTTP headers that firefox is little more forgiving about.

If you can't find a sample MJPEG stream over the internet, a lot of web cams have software that give you an MJPEG stream. The app I worked on it with was a console for multiple security cameras, so I know that is a common implementation for cams of all types (if they support a web interface).

dviljoen
A: 

About your edit: MJPEG is supported by Firefox and Safari. However other applications do not, like Explorer or Silverlight depending on what you are doing with it.

JustSmith
A: 

This is very interesting. But what is HCData. I don't know any class containing that in net.framework. Is this a custom class made in c++ for socket video streaming? Best regards yours sincerely Axel Arnold Bangert -- Herzogenrath 2009

A: 

This is very interesting. But what is HCData. I don't know any class containing that in net.framework. Is this a custom class made in c++ for socket video streaming?

Best regards yours sincerely

Axel Arnold Bangert -- Herzogenrath 2009

A: 

Possibly it is something out of QT C# Library from sourceforge: http://prdownloads.sourceforge.net/qtcsharp/qtsharp-0.7.1.tar.bz2?download

Best regards yours sincerely Axel Arnold Bangert -- Herzogenrath 2009