Content-Range working in Safari but not in Chrome

2019-06-13 05:37发布

I'm streaming audio files from a Node.js Express server with Content-Range headers plus no caching headers. This works ok in latest Safari instead, but it does not in Chrome.

While when streaming the full audio file with HTTP 200, my headers were

{ 'Content-Length': 4724126,
  'Content-Type': 'audio/mpeg',
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
  'Access-Control-Allow-Headers': 'POST, GET, OPTIONS',
  Expires: 0,
  Pragma: 'no-cache',
  'Cache-Control': 'no-cache, no-store, must-revalidate' }

and it works on both Chrome and Safari <audio> tag.

When streaming partial contents with HTTP 206 the headers were

{ 'Content-Length': 4724126,
  'Content-Type': 'audio/mpeg',
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
  'Access-Control-Allow-Headers': 'POST, GET, OPTIONS',
  Expires: 0,
  Pragma: 'no-cache',
  'Cache-Control': 'no-cache, no-store, must-revalidate',
  'Accept-Ranges': 'bytes',
  'Content-Range': 'bytes 120515-240260/4724126' }

This led to an error of the Chrome page where the <audio> or <video> tag was.

Even the embedded media tag created by Chrome when using the streaming url right in the browser it is not working, and led to this

enter image description here

The audio file is served reading a local file and creating a file read stream via Node.js the createReadStream api:

var file = fs.createReadStream(path, {start: range[0], end: range[1]});

I have posted the code for the server here.

[UPDATE]

It turns not that Chrome is more strict about Range and Content-Range requests/responses. So any Content-Range response must have a Range previous request. The typical case is scrubbing a player bar to the second S. The api will send a "Range" request, the server will respond with a "Content-Range" header. In my case I was sending a Content-Range response over an ordinary request without "Range". Safari works because is more street-html complaint i.e. it does not strictly require a "Range" request before a "Content-Range" response. So, I removed the "Content-Range" header from my response, and not it works. End of the story (!).

To follow up this issue I have posted in Chromium net-dev forum too in Chrome bad Range header: Range: bytes=0-

0条回答
登录 后发表回答