2

We are trying to play large videos (30MB) using asp.net. The code we are using is:

System.IO.Stream iStream = null;
byte[] buffer = new Byte[10000];
int length;
long dataToRead;


context.Response.Buffer = false;
context.Response.BufferOutput = false;
context.Response.Clear();
iStream = new System.IO.FileStream(myPath, System.IO.FileMode.Open,  System.IO.FileAccess.Read, System.IO.FileShare.Read);
dataToRead = iStream.Length;
context.Response.ContentType = "video/mp4";
context.Response.AddHeader("Content-Length", iStream.Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
                               //Write the file.
while (dataToRead > 0)
{
   // Verify that the client is connected.
   if (context.Response.IsClientConnected)
   {
       // Read the data in buffer.
       length = iStream.Read(buffer, 0, 10000);

       // Write the data to the current output stream.
       context.Response.OutputStream.Write(buffer, 0, length);

       // Flush the data to the HTML output.

       context.Response.Flush();

       buffer = new Byte[10000];
       dataToRead = dataToRead - length;
   }
   else
   {
       //prevent infinite loop if user disconnects
       dataToRead = -1;
   }`enter code here`
}
context.Response.OutputStream.Flush();

The issue is, it will take long time to play the video. The response is not quick. Please help us how to resolve this.

icebat
  • 4,696
  • 4
  • 22
  • 36
veena m
  • 21
  • 3

2 Answers2

0

Have you used ffmpeg, -movflags +faststart to move 'moov atom' metadata in the beginning of the MP4 file? maybe it plays better .

Sara N
  • 1,079
  • 5
  • 17
  • 45
0

One reason this is slow is that you aren't overlapping the read and the send. There is no concurrency between the two so every read has to wait for the send to finish and every send has to wait for a read.

Any reason you are doing your own stream copying instead of (a) just using Stream.CopyTo or (b) just returning a FileStreamResult or (c) using WebAPI and a StreamContent result.

See What's the difference between the four File Results in ASP.NET MVC

Community
  • 1
  • 1
Ian Mercer
  • 38,490
  • 8
  • 97
  • 133