.NET Core MVC中的部分内容(用于视频/音频流) [英] Partial content in .NET Core MVC (for video/audio streaming)
问题描述
我正在尝试在我的网站上实现视频和音频流传输(以便在Chrome中进行搜索),最近我发现.NET Core 2.0显然提供了一种相对简单且推荐的使用FileStreamResult
的方式来实现此功能. >
这是返回FileStreamResult的Action的简化实现:
public IActionResult GetFileDirect(string f)
{
var path = Path.Combine(Defaults.StorageLocation, f);
return File(System.IO.File.OpenRead(path), "video/mp4");
}
File
方法具有以下(简短的)描述:
以指定的contentType作为Content-Type返回指定的fileStream(Status200OK)中的文件.这支持范围请求(如果范围不能满足,则为Statusus206PartialContent或Status416RangeNotSatisfiable)
但是由于某些原因,服务器仍然无法正确响应范围请求.
我想念什么吗?
更新
Chrome发送的请求看起来像这样
GET https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4 HTTP/1.1
Host: myserver.com
Connection: keep-alive
Accept-Encoding: identity;q=1, *;q=0
User-Agent: ...
Accept: */*
Accept-Language: ...
Cookie: ...
Range: bytes=0-
响应如下:
HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Fri, 09 Feb 2018 17:57:45 GMT
Content-Type: video/mp4
Content-Length: 5418689
Connection: keep-alive
[... content ... ]
还尝试使用以下命令:
curl -H Range:bytes=16- -I https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4
并返回相同的响应.
HTML也非常简单.
<video controls autoplay>
<source src="https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
视频开始播放-用户仅无法观看视频.
在版本2.1中,将在文件方法中添加一个enableRangeProcessing参数.现在,您需要设置一个开关.您可以通过以下两种方式之一进行操作:
在runtimeconfig.json中:
{
// Set the switch here to affect .NET Core apps
"configProperties": {
"Switch.Microsoft.AspNetCore.Mvc.EnableRangeProcessing": "true"
}
}
或:
//Enable 206 Partial Content responses to enable Video Seeking from
//api/videos/{id}/file,
//as per, https://github.com/aspnet/Mvc/pull/6895#issuecomment-356477675.
//Should be able to remove this switch and use the enableRangeProcessing
//overload of File once
// ASP.NET Core 2.1 released
AppContext.SetSwitch("Switch.Microsoft.AspNetCore.Mvc.EnableRangeProcessing",
true);
有关详细信息,请参见 ASP.NET Core GitHub存储库.
I am trying to implement video and audio streaming on my website (to enable seeking in Chrome) and I recently found out that .NET Core 2.0 apparently provides a relatively simple and recommended way of implementing this using FileStreamResult
.
This is my simplified implementation of the Action that returns the FileStreamResult:
public IActionResult GetFileDirect(string f)
{
var path = Path.Combine(Defaults.StorageLocation, f);
return File(System.IO.File.OpenRead(path), "video/mp4");
}
The File
method has the following (shortened) description:
Returns a file in the specified fileStream (Status200OK), with the specified contentType as the Content-Type. This supports range requests (Status206PartialContent or Status416RangeNotSatisfiable if the range is not satisfiable)
But for some reason, the server still does not respond correctly to range requests.
Am I missing something?
Update
Request sent from Chrome looks like this
GET https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4 HTTP/1.1
Host: myserver.com
Connection: keep-alive
Accept-Encoding: identity;q=1, *;q=0
User-Agent: ...
Accept: */*
Accept-Language: ...
Cookie: ...
Range: bytes=0-
Response looks like:
HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Fri, 09 Feb 2018 17:57:45 GMT
Content-Type: video/mp4
Content-Length: 5418689
Connection: keep-alive
[... content ... ]
Also tried using the following command:
curl -H Range:bytes=16- -I https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4
and it returns the same response.
The HTML is pretty straightforward too.
<video controls autoplay>
<source src="https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
The video DOES start playing - the user is only unable to seek the video.
There will be an enableRangeProcessing parameter added to the File method in version 2.1. For now, you need to set a switch. You can do this one of two ways:
In runtimeconfig.json :
{
// Set the switch here to affect .NET Core apps
"configProperties": {
"Switch.Microsoft.AspNetCore.Mvc.EnableRangeProcessing": "true"
}
}
or:
//Enable 206 Partial Content responses to enable Video Seeking from
//api/videos/{id}/file,
//as per, https://github.com/aspnet/Mvc/pull/6895#issuecomment-356477675.
//Should be able to remove this switch and use the enableRangeProcessing
//overload of File once
// ASP.NET Core 2.1 released
AppContext.SetSwitch("Switch.Microsoft.AspNetCore.Mvc.EnableRangeProcessing",
true);
See ASP.NET Core GitHub Repo for details.
这篇关于.NET Core MVC中的部分内容(用于视频/音频流)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!