使用 WebAPI 流式传输 MJPEG 始终缓冲
Using WebAPI to stream MJPEG always buffers
我正在使用 HttpSelfHostConfiguration 创建 WebAPI(服务)。我的目标是让一条路线从安全源流式传输 mjpeg 视频,并让其他路线可用于配置和 Web 界面。
我遇到的问题是,我遇到的每个示例都需要已知数量的图像才能设置主要响应的内容长度。我没有这个,刷新流也没有做任何事情。
这是响应的当前代码。如果我将相同的代码与原始套接字一起使用,而不是通过 ApiController,我可以很好地流式传输它,但是从头开始为我需要的一切创建一个网络服务器似乎并不是很有趣。
[HttpGet]
public HttpResponseMessage Stream(int channel)
{
var response = Request.CreateResponse();
response.Content = new PushStreamContent((outputStream, content, context) =>
{
StreamWriter writer = new StreamWriter(outputStream);
while (true)
{
using (MemoryStream ms = new MemoryStream())
{
ReadMemoryMappedFile(channel);
ms.SetLength(0);
this.Image.Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] buffer = ms.GetBuffer();
writer.WriteLine("--boundary");
writer.WriteLine("Content-Type: image/jpeg");
writer.WriteLine(string.Format("Content-length: {0}", buffer.Length));
writer.WriteLine();
writer.Write(buffer);
writer.Flush();
}
}
});
response.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=--boundary");
return response;
}
我找不到任何明确说明这一点的地方,但我假设 HttpSelfHostConfiguration 不支持我正在寻找的功能,并且总是需要在释放缓冲区之前关闭流.
我用 OWIN.SelfHost 交换了 HttpSelfHostConfiguration,它按预期工作。
我希望我迟到的回答能有所帮助,因为我最近 运行 遇到了同样的问题,我花了一些时间才弄明白...
我的解决方案是指定不带“--”的 ContentType 边界(但在流中写入时需要保留它们)。
尝试像这样配置 Headers:
response.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=boundary");
然后像这样在流中写入边界:
writer.WriteLine("--boundary");
像这样对我有用。
我正在使用 HttpSelfHostConfiguration 创建 WebAPI(服务)。我的目标是让一条路线从安全源流式传输 mjpeg 视频,并让其他路线可用于配置和 Web 界面。
我遇到的问题是,我遇到的每个示例都需要已知数量的图像才能设置主要响应的内容长度。我没有这个,刷新流也没有做任何事情。
这是响应的当前代码。如果我将相同的代码与原始套接字一起使用,而不是通过 ApiController,我可以很好地流式传输它,但是从头开始为我需要的一切创建一个网络服务器似乎并不是很有趣。
[HttpGet]
public HttpResponseMessage Stream(int channel)
{
var response = Request.CreateResponse();
response.Content = new PushStreamContent((outputStream, content, context) =>
{
StreamWriter writer = new StreamWriter(outputStream);
while (true)
{
using (MemoryStream ms = new MemoryStream())
{
ReadMemoryMappedFile(channel);
ms.SetLength(0);
this.Image.Bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] buffer = ms.GetBuffer();
writer.WriteLine("--boundary");
writer.WriteLine("Content-Type: image/jpeg");
writer.WriteLine(string.Format("Content-length: {0}", buffer.Length));
writer.WriteLine();
writer.Write(buffer);
writer.Flush();
}
}
});
response.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=--boundary");
return response;
}
我找不到任何明确说明这一点的地方,但我假设 HttpSelfHostConfiguration 不支持我正在寻找的功能,并且总是需要在释放缓冲区之前关闭流.
我用 OWIN.SelfHost 交换了 HttpSelfHostConfiguration,它按预期工作。
我希望我迟到的回答能有所帮助,因为我最近 运行 遇到了同样的问题,我花了一些时间才弄明白...
我的解决方案是指定不带“--”的 ContentType 边界(但在流中写入时需要保留它们)。
尝试像这样配置 Headers:
response.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/x-mixed-replace; boundary=boundary");
然后像这样在流中写入边界:
writer.WriteLine("--boundary");
像这样对我有用。