Partial content in .NET Core MVC (for video/audio streaming)

こ雲淡風輕ζ 提交于 2020-06-07 21:19:28

问题


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.


回答1:


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.




回答2:


My answer is based on Yuli Bonner, but with the adaptations so that it answers the question directly, and with Core 2.2

 public IActionResult GetFileDirect(string f)
{
   var path = Path.Combine(Defaults.StorageLocation, f);
   var res = File(System.IO.File.OpenRead(path), "video/mp4");
   res.EnableRangeProcessing = true;
   return res;
} 

This allowed for seeking in the browser.



来源:https://stackoverflow.com/questions/48711209/partial-content-in-net-core-mvc-for-video-audio-streaming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!