Sheraff
Sheraff

Reputation: 6772

Mixing request origins in <audio> stream of 206 ranges causes playback to stop

TL;DR When an <audio> element receives 206 Content-Range chunks of data, switching the origin of these chunks mid-stream through a service-worker stops the playback, and it cannot be resumed. Why and how can I prevent it from stopping?


I have a server on my network and this server knows how to respond to Content-Range audio streaming requests with the correct 206 chunks.

On the client, there is an <audio src="..."> tag and a service-worker that intercepts the fetch requests emitted by that audio element.

If I leave my home with an audio file currently streaming, here's what happens

You can see this happen in the devtools: enter image description here The first request is that of the client, the second (red) the one to local.example.com, and the last one the one to example.com

Except... that the audio playback gets interrupted and cannot be resumed.

To diagnose this issue:

Can some explain why the audio playback gets interrupted when it started playing as a stream of 206 responses from an origin, and in the middle of streaming, starts receiving 206 responses from another origin ?

The response header received by the audio element are identical, whether it's from local.example.com or from example.com:

Request URL: https://example.com/api/file/clh2jwi0s030kyqjx2i45ct6w
Request Method: GET
Status Code: 206  (from service worker)
Referrer Policy: strict-origin-when-cross-origin

accept-ch: Sec-CH-DPR, Sec-CH-Viewport-Width, Downlink
access-control-allow-origin: https://example.com
access-control-expose-headers: Content-Range
cache-control: public, max-age=31536000
Content-Length: 524288
Content-Range: bytes 3145728-3670015/6044646
content-type: audio/MPEG
date: Thu, 04 May 2023 14:15:14 GMT
server: Apache
service-worker-allowed: /

Upvotes: 3

Views: 119

Answers (1)

Sheraff
Sheraff

Reputation: 6772

The answer is that for the <audio> to be able to receive chunks from multiple origins when requesting a single source, you need to give it the crossorigin attribute.

Upvotes: 1

Related Questions