17

I'm trying to modify a custom web server app to work with HTML5 video.

It serves a HTML5 page with a basic <video> tag and then it needs to handle the requests for actual content.

The only way I could get it to work so far is to load the entire video file into the memory and then send it back in a single response. It's not a practical option. I want to serve it piece by piece: send back, say, 100 kb, and wait for the browser to request more.

I see a request with the following headers:

http_version = 1.1
request_method = GET

Host = ###.###.###.###:##
User-Agent = Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0
Accept = video/webm,video/ogg,video/*;q=0.9,application/ogg;q=0.7,audio/*;q=0.6,*/*;q=0.5
Accept-Language = en-US,en;q=0.5
Connection = keep-alive
Range = bytes=0-

I tried to send back a partial content response:

HTTP/1.1 206 Partial content
Content-Type: video/mp4
Content-Range: bytes 0-99999 / 232725251
Content-Length: 100000

I get a few more GET requests, as follows

Cache-Control = no-cache
Connection = Keep-Alive
Pragma = getIfoFileURI.dlna.org
Accept = */*
User-Agent = NSPlayer/12.00.7601.17514 WMFSDK/12.00.7601.17514
GetContentFeatures.DLNA.ORG = 1
Host = ###.###.###.###:##

(with no indication that the browser wants any specific part of the file.) No matter what I send back to the browser, the video does not play.

As stated above, the same video will play correctly if I try to send the entire 230 MB file at once in the same HTTP packet.

Is there any way to get this all working nicely through partial content requests? I'm using Firefox for testing purposes, but it needs to work with all browsers eventually.

7
  • I find it odd that you're using Firefox for testing and sending an MP4 file which Firefox doesn't support, so it should never work.
    – Ian Devlin
    Commented Oct 24, 2012 at 7:43
  • So far I tried Firefox (with H264 plugin) and IE9 on the browser side, and MP4 and WebM on the server side. I think I'm missing something. Commented Oct 24, 2012 at 10:00
  • Why do you want to get partial range requests from clients? Just allow them to ask what they want, and send it to them in most convenient way. I worked with streaming audio (requested from Chrome), and a custom server - it sent sounds in real time. I think this is default and acceptable way. If you bother about network load, use throttling down to a speed sufficient for smooth video playback.
    – Stan
    Commented Oct 24, 2012 at 10:05
  • I want partial range requests, because I'll be doing realtime transcoding, I can't have the file completely transcoded and available upon request. Commented Oct 24, 2012 at 10:50
  • 1
    @Stan and what is the way that most useragents implement HTML5 media tags? Can't find any good documentation anywhere. Commented Oct 29, 2012 at 23:21

2 Answers 2

8

I know this is an old question, but if it helps you can try the following "Model" that we use in our code base.

class Model_DownloadableFile {
private $full_path;

function __construct($full_path) {
    $this->full_path = $full_path;
}

public function get_full_path() {
    return $this->full_path;
}

// Function borrowed from (been cleaned up and modified slightly): http://stackoverflow.com/questions/157318/resumable-downloads-when-using-php-to-send-the-file/4451376#4451376
// Allows for resuming paused downloads etc
public function download_file_in_browser() {
    // Avoid sending unexpected errors to the client - we should be serving a file,
    // we don't want to corrupt the data we send
    @error_reporting(0);

    // Make sure the files exists, otherwise we are wasting our time
    if (!file_exists($this->full_path)) {
        header('HTTP/1.1 404 Not Found');
        exit;
    }

    // Get the 'Range' header if one was sent
    if (isset($_SERVER['HTTP_RANGE'])) {
        $range = $_SERVER['HTTP_RANGE']; // IIS/Some Apache versions
    } else if ($apache = apache_request_headers()) { // Try Apache again
        $headers = array();
        foreach ($apache as $header => $val) {
            $headers[strtolower($header)] = $val;
        }
        if (isset($headers['range'])) {
            $range = $headers['range'];
        } else {
            $range = false; // We can't get the header/there isn't one set
        }
    } else {
        $range = false; // We can't get the header/there isn't one set
    }

    // Get the data range requested (if any)
    $filesize = filesize($this->full_path);
    $length = $filesize;
    if ($range) {
        $partial = true;
        list($param, $range) = explode('=', $range);
        if (strtolower(trim($param)) != 'bytes') { // Bad request - range unit is not 'bytes'
            header("HTTP/1.1 400 Invalid Request");
            exit;
        }
        $range = explode(',', $range);
        $range = explode('-', $range[0]); // We only deal with the first requested range
        if (count($range) != 2) { // Bad request - 'bytes' parameter is not valid
            header("HTTP/1.1 400 Invalid Request");
            exit;
        }
        if ($range[0] === '') { // First number missing, return last $range[1] bytes
            $end = $filesize - 1;
            $start = $end - intval($range[0]);
        } else if ($range[1] === '') { // Second number missing, return from byte $range[0] to end
            $start = intval($range[0]);
            $end = $filesize - 1;
        } else { // Both numbers present, return specific range
            $start = intval($range[0]);
            $end = intval($range[1]);
            if ($end >= $filesize || (!$start && (!$end || $end == ($filesize - 1)))) {
                $partial = false;
            } // Invalid range/whole file specified, return whole file
        }
        $length = $end - $start + 1;
    } else {
        $partial = false; // No range requested
    }

    // Determine the content type
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $contenttype = finfo_file($finfo, $this->full_path);
    finfo_close($finfo);

    // Send standard headers
    header("Content-Type: $contenttype");
    header("Content-Length: $length");
    header('Content-Disposition: attachment; filename="' . basename($this->full_path) . '"');
    header('Accept-Ranges: bytes');

    // if requested, send extra headers and part of file...
    if ($partial) {
        header('HTTP/1.1 206 Partial Content');
        header("Content-Range: bytes $start-$end/$filesize");
        if (!$fp = fopen($this->full_path, 'r')) { // Error out if we can't read the file
            header("HTTP/1.1 500 Internal Server Error");
            exit;
        }
        if ($start) {
            fseek($fp, $start);
        }
        while ($length) { // Read in blocks of 8KB so we don't chew up memory on the server
            $read = ($length > 8192) ? 8192 : $length;
            $length -= $read;
            print(fread($fp, $read));
        }
        fclose($fp);
    } else {
        readfile($this->full_path); // ...otherwise just send the whole file
    }

    // Exit here to avoid accidentally sending extra content on the end of the file
    exit;
}
}

You then use it like this:

(new Model_DownloadableFile('FULL/PATH/TO/FILE'))->download_file_in_browser();

It will deal with sending part of the file or the full file etc and works well for us in this and lots of other situations. Hope it helps.

5
  • 1
    The line that reads header("Content-Length: $filesize"); should actually be header("Content-Length: $length"); as it is the total bytes delivered, not the size of the file. Chrome's video player barfs on this if not set correctly. Commented Mar 4, 2014 at 16:33
  • Good spot, I've corrected the original post. $length is only set if the $range is true, so I've made sure it is set to $filesize first and gets overwritten if $range is true. Commented Mar 4, 2014 at 16:54
  • My problem is that php delivers Content-Type: text/html even if header_remove('Conten-Type');
    – B.F.
    Commented Nov 1, 2014 at 13:16
  • @B.F. it shouldn't be. How do you know that it is? If you are getting that header, do you know that it is php and not something else such as Apache or Nginx? Why do you want to remove the header in the first place? Why not just set the header you do want with the header() function? Commented Nov 2, 2014 at 17:18
  • @Luke Cousins Thanks for reply! I removed the default header from php.ini but a lot other things. So I dont know where the mistake was. To your script: you should answer a miss formed range request with header('HTTP/1.1 416 Requested Range Not Satisfiable1');And if you like to give user a second chance: header('Content-Range: bytes */'.filesize($file),true);
    – B.F.
    Commented Nov 4, 2014 at 7:40
4

I want partial range requests, because I'll be doing realtime transcoding, I can't have the file completely transcoded and available upon request.

For response which you don't know the full body content yet (you can't guess the Content-Length, live encoding), use chunk encoding:

HTTP/1.1 200 OK
Content-Type: video/mp4
Transfer-Encoding: chunked
Trailer: Expires

1E; 1st chunk
...binary....data...chunk1..my
24; 2nd chunk
video..binary....data....chunk2..con
22; 3rd chunk
tent...binary....data....chunk3..a
2A; 4th chunk
nd...binary......data......chunk4...etc...
0
Expires: Wed, 21 Oct 2015 07:28:00 GMT

Each chunk is send when it's available: when few frames are encoded or when the output buffer is full, 100kB are generated, etc.

22; 3rd chunk
tent...binary....data....chunk3..a

Where 22 give the chunk byte length in hexa (0x22 = 34 bytes), ; 3rd chunk is extra chunk infos (optional) and tent...binary....data....chunk3..a is the content of the chunk.

Then, when the encoding is finished and all chunks are sent, end by:

0
Expires: Wed, 21 Oct 2015 07:28:00 GMT

Where 0 means there not more chunks, followed by zero or more trailer (allowed header fields) defined in the header (Trailer: Expires and Expires: Wed, 21 Oct 2015 07:28:00 GMT are not required) to provide checksums or digital signatures, etc.

Here is the equivalent of the server's response if the file was already generated (no live encoding):

HTTP/1.1 200 OK
Content-Type: video/mp4
Content-Length: 142
Expires: Wed, 21 Oct 2015 07:28:00 GMT

...binary....data...chunk1..myvideo..binary....data....chunk2..content...binary....data....chunk3..and...binary......data......chunk4...etc...

For more information: Chunked transfer encoding — Wikipedia, Trailer - HTTP | MDN

5
  • I don't think the video tag supports chunked encoding(at least in Chrome).
    – themihai
    Commented May 19, 2017 at 20:18
  • 1
    The chunked transport encoding is required for HTTP/1.1 support. Also I tried, Chrome and Firefox play well videos using chunked transport encoding. Safari (desktop and iOS) requires ranges support to play videos (not required for audio). Transport encoding and ranges are not incompatible, but bytes ranges require to know in advance the byte size. For that, you can use live streaming protocols (but require special treatment when encoding the video) like HLS or APIs like MSE. See hls.js
    – mems
    Commented May 22, 2017 at 14:44
  • Chrome makes a partial request regardless if the server supports it or not so I think chunked encoding alone without partial/ranges support is not enough to serve html5 video. If the server doesn't advertise the total length of the content(i.e provides a wildcard *) the player stops after the first partial request completes instead to request the next part so I guess that's a Chrome bug.
    – themihai
    Commented May 22, 2017 at 14:50
  • When you use chunked transport encoding you don't have to provide the Content-Length header (Note: "*" is an invalid value for this header). I made a test case, where the video load on Chrome, Firefox and IE (at least IE10). See gist.github.com/mems/b121bba11adc48dfd068bb7a8d113a8c (remove usleep(200000) if you want to load the video quicker)
    – mems
    Commented May 22, 2017 at 15:32
  • that doesn't work on Chrome(on Mac). No video is playing if the Range header is not provided.
    – themihai
    Commented May 24, 2017 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.