C# 带有 MJPEG 和 multipart/x-mixed-replace 的 HttpWebResponse;边界=--来自安全摄像头的myboundary 响应内容类型不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2060953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:09:08  来源:igfitidea点击:

HttpWebResponse with MJPEG and multipart/x-mixed-replace; boundary=--myboundary response content type from security camera not working

c#asp.nethttpwebrequesthttpwebresponsemjpeg

提问by arri.io

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:

我有一个 ASP.NET 应用程序,我需要显示来自安全摄像头的视频源。视频提要的内容类型为“multipart/x-mixed-replace”;边界=--myboundary' 与边界之间的图像数据。我需要帮助将该数据流传递到我的页面,以便我拥有的客户端插件可以使用该流,就像我直接浏览到相机的 Web 界面一样。以下代码不起作用:

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

采纳答案by neuro

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 :

好吧,如果你想让你的客户端看到 mjpeg 流,你需要发送整个 http 响应。HTTP 客户端(如浏览器)或媒体播放器(如 VLC)需要一个 mjpeg 流,如下所示:

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.

注意:正如 Ergousha 在回答中所说,您必须在 Content-length 字段后面有一个空行。

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

顺便说一句,为什么不将您的客户端直接重定向到 mjpeg 流?

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

例如,您可以使用http://ipcam/mjpg/video.mjpgAXIS IP 摄像机。

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.

如果您只需要通过 HTTP 传输的图像,则必须设置正确的标头和 MIME 内容类型图像/jpeg。要解码图像,您必须获得字节数据,您将获得 jpeg 编码。然后,您将必须解码 jpeg 以获取特定格式的图像(我认为类似于 yuv420p)。我检查了我的网络摄像头,我认为它的流不是 base64 编码的。

Precise your needs, I will try to help more.

正是您的需求,我会尽力帮助更多。

my2c

我的2c

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 ...)

也就是说,您必须将正确的标头发送给您的客户。确保使用诸如wireshark之​​类的工具来监视数据包,并确保在您的客户端发出HTTP GET后,您将正确的MJPEG流发送给他(就像我在帖子开头所描述的那样......)

m2c

m2c

回答by feroze

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

图像数据的编码是什么?是 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.

基本上,您必须从响应中解析出图像数据,Base64 将其解码为字节,然后将图像发送到客户端。

回答by Ergousha

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.

很好地解释了 mjpeg 流的样子。我想添加一个提示;在实际数据之前总是有 2 个换行符。如果你把它做成一行,它就不起作用。

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