HttpWebResponse with MJPEG and multipart/x-mixed-r

2019-03-20 06:46发布

问题:

I have an ASP.NET application that I need to show a video feed from a security camera. The video feed has a content type of 'multipart/x-mixed-replace; boundary=--myboundary' with the image data between the boundaries. I need assistance with passing that stream of data through to my page so that the client side plugin I have can consume the stream just as it would if I browsed to the camera's web interface directly. The following code does not work:

//Get response data
byte[] data = HtmlParser.GetByteArrayFromStream(response.GetResponseStream());
if (data != null)
{
 HttpContext.Current.Response.OutputStream.Write(data, 0, data.Length);
}
return;

回答1:

Well, if you want your client to see the mjpeg stream, you need to send the whole http response. HTTP client like browser or media player like VLC need a mjpeg stream that looks like :

HTTP/1.1 200 OK
Content-Type: multipart/x-mixed-replace; boundary=myboundary

--myboundary
Content-Type: image/jpeg
Content-length: 12345

[image 1 encoded jpeg data]


--myboundary
Content-Type: image/jpeg
Content-length: 45678

[image 2 encoded jpeg data]

...

NOTE: As Ergousha said in an answer, you must have an empty line after the Content-length field.

By the way, why not redirect your client directly to the mjpeg stream ?

You can use http://ipcam/mjpg/video.mjpg AXIS IP cameras for example.

If you just need the image through HTTP, you have to set the correct header and the MIME content-type image/jpeg. To decode the image, you have to get the byte data and you will get jpeg encoding. Then you will have to decode jpeg to get an image in a specific format (something like yuv420p I think). I've check on my ip camera, and its stream is not base64 encoded I think.

Precise your needs, I will try to help more.

my2c

EDIT:

Well, I suppose you do something like :

client    : connect to proxy, 
            get example.com/camera1.mjpg,
            while not the end
                recv


yourproxy : wait connection
            connect to camera,
            get 10.0.0.123/camera1.mjpg
            while not the end
                recv buffer
                copy buffer
                send buffer to client

That to say that you must send the correct header to your client. To be sure use a tool like wireshark to spy on the packet and be sure that after your client has issued a HTTP GET you send to him the correct MJPEG stream (like the one I describe at the beginning of my post ...)

m2c



回答2:

Good explaination about how mjpeg stream looks like. I would like to add a tip that; there are always 2 new line character before the actual data. If you make it one line, it doesnt work.

        string header =
            "--myboundary\r\n" +
            "Content-Type:image/jpeg\r\n" +
            "Content-Length:" + length.ToString() + "\r\n\r\n";


回答3:

What is the encoding of the image data? Is it Base64?

Basically you will have to parse out the image data from the response, Base64 decode it into bytes, and then send the image to the client.