I am experimenting/building a website with streaming functionality, where the user could upload the video (.mp4
) and view the video on the page (video contents are returned partially/incrementally). I am using the Azure Blob Storage
and the dotnet
technology stack.
html:
<video src="https://localhost:5001/Files/DownloadFileStream?fileName=VID_20210719_110859.mp4" controls="true" />
controller:
[HttpGet]
public async Task<IActionResult> DownloadFileStream([FromQuery] string fileName)
{
var range = Request.Headers["range"].ToString();
Console.WriteLine(range);
var container = new BlobContainerClient("UseDevelopmentStorage=true", "sample-container");
await container.CreateIfNotExistsAsync();
BlobClient blobClient = container.GetBlobClient(fileName);
var response = await blobClient.GetPropertiesAsync();
string contentType = response.Value.ContentType;
long contentLength = response.Value.ContentLength;
Azure.Response<BlobDownloadStreamingResult> result = await blobClient.DownloadStreamingAsync();
return new FileStreamResult(result.Value.Content, contentType)
{
EnableRangeProcessing = true,
};
// following approach supports video Seek functionality:
/*
using var memory = new MemoryStream();
await blobClient.DownloadToAsync(memory);
return new FileContentResult(memory.ToArray(), contentType)
{
EnableRangeProcessing = true,
};*/
}
What I have noticed is, when I stream the contents - in Chrome's Inspect mode
we can see the media file being downloaded. In fact, if you use the seek functionality - multiple request records will appear:
My current problem is, after the file got fully downloaded/played until the end of stream - if you start playing it again - the browser will start to download the contents again, instead of using already cached/downloaded content.
So I went to investigate how the huge market leaders like YouTube and Instagram handle the video streaming, but what I noticed that upon video playback - not a single "media" type of content/request appears.
In fact, from the inspector
's perspective - it looks like nothing is being downloaded at all.. So this raises some questions.
- How do market leaders like YouTube and Instagram stream videos to clients, without exposing to
inspector
any media traffic? Is this behavior replicable by.Net 5
? - Why does my application keep re-downloading the same file over and over again and how to prevent it from happening?
- Why does the seek functionality not work when
DownloadStreamingAsync()
method is used and how to fix it? I use this approach mainly because the application has smaller memory footprint in this case. We don't have to download the whole stream into memory before returning contents to the client.
While I have listed 3 questions here - my main concern is the first question, so any answers on that topic are very welcomed. :)