Answer the question
In order to leave comments, you need to log in
Chrome. Asp.net/PushStreamContent. Why does video streaming stop?
API:
public HttpResponseMessage Get(string id)
{
var video = new VideoStream(id);
var response = Request.CreateResponse();
response.Content = new PushStreamContent((Action<Stream, HttpContent, TransportContext>)video.WriteToStream, new MediaTypeHeaderValue("video/webm"));
response.Content.LoadIntoBufferAsync();
return response;
}
public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
{
try
{
byte[] buffer;
var f = File.Open(_filename, FileMode.Open, FileAccess.Read);
if (f != null)
buffer = new byte[f.Length];
else
buffer = new byte[Int32.MaxValue];
f.Close();
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;
}
video.Close();
video.Dispose();
}
}
catch
{
throw;
}
finally
{
outputStream.Close();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question