Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobile Safari makes multiple video requests

I am designing a web application for iPad which makes use of HTML5 in mobile safari. I am transmitting the file manually through an ASP.NET .ashx file hosted on IIS 7 running .NET Framework v2.0.

The essential code looks partly like this:

// If we receive range header only transmit partial file
if (context.Request.Headers["Range"] != null)
{
    var fi = new FileInfo(filePath);
    long fileSize = fi.Length;

    // Read start/end index
    string headerRange = context.Request.Headers["Range"].Replace("bytes=", "");
    string[] range = headerRange.Split('-');
    int startIndex = Convert.ToInt32(range[0]);
    int endIndex = Convert.ToInt32(range[1]);

    // Add header Content-Range,Last-Modified
    context.Response.StatusCode = (int)HttpStatusCode.PartialContent;
    context.Response.AddHeader(HttpWorkerRequest.GetKnownResponseHeaderName(HttpWorkerRequest.HeaderContentRange), String.Format("bytes {0}-{1}/{2}", startIndex, endIndex, fileSize));
    context.Response.AddHeader(HttpWorkerRequest.GetKnownResponseHeaderName(HttpWorkerRequest.HeaderLastModified), String.Format("{0:r}", fi.CreationTime));

    long length = (endIndex - startIndex) + 1;
    context.Response.TransmitFile(filePath, startIndex, length);
}
else
    context.Response.TransmitFile(filePath);

Now what confuses me to no end is the the protocols for requesting that safari seems to use. From proxying the requests through fiddler i get the following for an aprox 2MB file.

Fiddler requests

NOTE: When requesting an mp4 file, directly served through IIS 7, the protocol and amount of request are the same

  1. First it requests 2 bytes which allows it to read the 'Content-Range' header.
  2. Now it request the entire content (?)
  3. -
  4. It proceeds to do step 1. & 2. again (??)
  5. -
  6. It now requests only parts of the file (???)

If the file is larger the last steps will be many more. I have tested up to 99 request where each request contains a part of the file equally split. This makes sense and is what would be expected I think. What I cannot make sense of is why it makes 2 initial request for the first 2 bytes as well as 2 requests for the entire file before it finally requests the file in different parts.

As I conclude this results in the file being downloaded between 2 - 3 times, depending on the length of the file and whether the user watches it long enough.

Can anybody make sense of this behavior and maybe explain what I can do to prevent multiple downloads. Thanks.

like image 662
Jacob T. Nielsen Avatar asked May 23 '11 08:05

Jacob T. Nielsen


1 Answers

Per my comment to your question, I've had a similar issue in the past. One thing you could try if you have control of the server (I did not) is to disable either gzip or identity encoding of the file. I believe that in the first request for the entire content (#2 in your list) it asks for the content with gzip encoding (compressed). Perhaps you can configure your IIS to not to serve the file for a gzip-encoding request.

Here is my original (unanswered) question on the subject:

https://stackoverflow.com/questions/4855485/mpmovieplayercontroller-not-playing-full-length-mp3

like image 56
TomSwift Avatar answered Nov 07 '22 07:11

TomSwift