I'm making a small application which sent a part of video file to client to play on <video>
element.
This is the code I'm having:
[RoutePrefix("api/video")]
public class VideoApiController : ApiController
{
[Route("")]
[HttpGet]
public async Task<HttpResponseMessage> GetVideoAsync([FromUri] GetVideoViewModel model)
{
var path = System.Web.Hosting.HostingEnvironment.MapPath($"~/wwwroot/{model.FileName}");
if (path == null || !File.Exists(path))
return new HttpResponseMessage(HttpStatusCode.NotFound);
using (var fileStream = File.OpenRead(path))
{
var bytes = new byte[model.To];
fileStream.Read(bytes, 0, bytes.Length - 1);
var memoryStream = new MemoryStream(bytes);
memoryStream.Seek(0, SeekOrigin.Begin);
var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.PartialContent);
httpResponseMessage.Content = new ByteRangeStreamContent(memoryStream, new RangeHeaderValue(model.From, model.To), "video/webm");
return httpResponseMessage;
}
}
}
Then, I tested my end-point with postman. If I selected byte range from 0 to 100000(http://localhost:64186/api/video?fileName=elephants-dream.webm&from=0&to=100000), video could be displayed on result panel:
However, when I selected byte range from 100000 to 200000(http://localhost:64186/api/video?fileName=elephants-dream.webm&from=100000&to=200000) at the first tiem, video was blank:
As I understand, video/webm
uses a codec, metadata is included at some first bytes of stream.
If I want to play a part of video without play it from the beginning. What should I do ?
Thank you.