Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 980
CryShana Avatar asked Feb 09 '18 17:02

CryShana


People also ask

How do I create a partial view in .NET Core?

The method name contains Async will be rendered for the asynchronous code. The Render methods result need to be written directly to the response. If we want to Create Partial View, then we need to right click on the view folder > click on the folder > select Add > click on View.

Which is best .NET Core or MVC?

1. Enhanced Performance. An ASP.NET Development Company considers how well an application performs as a primary factor while selecting a framework for app development. And in this case, ASP.NET Core is much faster than ASP.NET MVC and has shown great results compared to other frameworks.

What is the main reason of using .NET Core rather than MVC?

The main reason of . Net core is cross platform support that enables the application to run on Windows, Mac and Linux OS whereas MVC application can be run only on Windows. build web application, IoT (Internet of things) apps, services and mobile Backends. You can do your development on Linux, Windows and MacOS.

What is stream in ASP.NET Core?

ASP.NET Core SignalR supports streaming from client to server and from server to client. This is useful for scenarios where fragments of data arrive over time.


2 Answers

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.

like image 91
Paulo Neves Avatar answered Sep 22 '22 00:09

Paulo Neves


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.

like image 21
Yuli Bonner Avatar answered Sep 18 '22 00:09

Yuli Bonner