Async video streaming in ASP.Net Core Web Api not working

I used http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/ this method before and worked great for async streaming.

But for ASP.NET Core, this method does not work as expected.

By video streaming class:

public class VideoStream { private readonly string _filename; public VideoStream(string filename) { _filename = filename; } public async Task WriteToStream(Stream outputStream, HttpContent content, TransportContext context) { try { var buffer = new byte[65536]; using (var video = File.Open(_filename, FileMode.Open, FileAccess.Read)) { var length = (int)video.Length; var bytesRead = 1; while (length > 0 && bytesRead > 0) { bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length)); await outputStream.WriteAsync(buffer, 0, bytesRead); length -= bytesRead; } } } catch (Exception) { return; } finally { outputStream.Flush(); outputStream.Dispose(); } } } 

and I have the following action for streaming video requests:

  [HttpGet] [Route("[action]")] public IActionResult GetVideo(int id) { var fileName = GetVideoFileName(id); var video = new VideoStream(fileName); var response = new HttpResponseMessage { Content = new PushStreamContent(video.WriteToStream, new MediaTypeHeaderValue("video/mp4")) }; var objectResult = new ObjectResult(response); objectResult.ContentTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("video/mp4")); return objectResult; } 

Since Asp.Net Core does not have a built-in Media Formatter for video / mp 4 by default, I created the following custom Media Formatter

  public class VideoOutputFormatter : IOutputFormatter { public bool CanWriteResult(OutputFormatterCanWriteContext context) { if (context == null) throw new ArgumentNullException(nameof(context)); return true; } public async Task WriteAsync(OutputFormatterWriteContext context) { if (context == null) throw new ArgumentNullException(nameof(context)); var response = context.HttpContext.Response; response.ContentType = "video/mp4"; How to impelemnt ??? } } 

and added the following line to Startup.cs

  services.AddMvc(options => { options.OutputFormatters.Add(new VideoOutputFormatter()); }); 

It actually calls my custom formatter. I don’t know how to implement this custom media format for video / mp 4. Can anyone help me?

+7
asp.net-mvc video-streaming asp.net-core asp.net-core-webapi
source share
1 answer

A look at the source code for Asp.NET Core really helped me find the answer to this question. They have a StreamOutputFormatter class that is really close to what you want to use. I needed to change it to look for PushStreamContent , and it worked like a charm.

Here is my full VideoOutputFormatter:

 public class VideoOutputFormatter : IOutputFormatter { public bool CanWriteResult(OutputFormatterCanWriteContext context) { if (context == null) throw new ArgumentNullException(nameof(context)); if (context.Object is PushStreamContent) return true; return false; } public async Task WriteAsync(OutputFormatterWriteContext context) { if (context == null) throw new ArgumentNullException(nameof(context)); using (var stream = ((PushStreamContent)context.Object)) { var response = context.HttpContext.Response; if (context.ContentType != null) { response.ContentType = context.ContentType.ToString(); } await stream.CopyToAsync(response.Body); } } } 

Instead of wrapping the HttpResponseMessage in an ObjectResult in your controller, you just need to drag the PushStreamContent object into the ObjectResult . You still need to set MediaTypeHeaderValue to ObjectResult .

+3
source share

All Articles