Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range request and HTML5 Audio

In the context of HTML5 audio, if the server is sent a request with the following header:

Range:bytes=0-

Will responding with

Content-Range: bytes 0-499/1234

Be handled correctly by all modern browsers? When byte[500] is needed, will the browser automatically know to request the next chunk? Am I interpreting the following spec correctly?

If all of the preconditions are true, the server supports the Range header field for the target resource, and the specified range(s) are
valid and satisfiable (as defined in Section 2.1), the server SHOULD
send a 206 (Partial Content) response with a payload containing one
or more partial representations that correspond to the satisfiable
ranges requested, as defined in Section 4.

I've been reading from the following spec: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-p5-range-22

like image 409
jpatapoff Avatar asked Jun 25 '13 19:06

jpatapoff


People also ask

What is range request?

An HTTP range request asks the server to send only a portion of an HTTP message back to a client. Range requests are useful for clients like media players that support random access, data tools that know they need only part of a large file, and download managers that let the user pause and resume the download.

Can multiple audio sources can be played in HTML?

The <audio> HTML element is used to embed sound content in documents. It may contain one or more audio sources, represented using the src attribute or the <source> element: the browser will choose the most suitable one.

What is byte request range?

Byte-range requests occur when a client asks the server for only a portion of the requested file. The purpose of this is essentially to conserve bandwidth usage by avoiding the need to download a complete file when all that is required is a small section.


2 Answers

If that request was generated internally by the browser by assigning the src attribute of an audio of video tag, then yes, the media element knows how to handle byte range requests and make subsequent requests for the next segments. No additional logic needed.

like image 89
Nick Desaulniers Avatar answered Oct 16 '22 20:10

Nick Desaulniers


It sounds like you're interpreting the spec correctly. However, examples of ServiceWorkers responding to Range Requests suggests that the expected response to open-ended requests like 0- is the entire byte range. Browsers then stream the response up to some heuristic, and if the user seeks to a range outside of what has been streamed the initiate a subsequent open-ended request.

In order words, if there's a file with 1000 bytes: the first request is always Range: bytes=0-. The browser decides to load 100 bytes. The user seeks toward the end, and the browser sends another request Range: bytes=900-.

I've actually never seen instances where browsers request explicit, closed ranges like Range: bytes=0-100. They also don't seem to use the range size, i.e. 1234 in Content-Range: bytes 0-499/1234. I'm guessing this is because some servers may send * for files of unknown duration (or continuous, real-time playback).

Word of caution: this is based on my observations, predominantly of Firefox, Chrome, and iOS Safari. Behavior may vary based on browser vendor, version, etc.

like image 39
Tom Avatar answered Oct 16 '22 19:10

Tom