Skip to main content

Broadcast to a Large Audience with HLS

Turn a stream into HLS so it plays on phones, TVs and browsers for audiences of any size. This guide uses an OBS stream sent over WHIP as the source.

Before you start​

  • A running room and its meetingID.
  • A video source: the sessionID of an active WHIP stream.

1. Start the broadcast​

From your server:

curl -X POST "https://mediasfu.com/v1/meetings/$MEETING_ID/broadcasts" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MEDIASFU_API_USERNAME:$MEDIASFU_API_KEY" \
-d "{\"sourceSessionID\":\"$SESSION_ID\"}"

The response includes:

{
"success": true,
"broadcastID": "…",
"state": "…",
"sourceQuality": "…",
"renditions": ["…"],
"playbackUrl": "https://…",
"playbackUrls": [],
"originPlaybackUrl": "https://…"
}

playbackUrl is the address viewers should use, and it may be served from a CDN. originPlaybackUrl is the same stream straight from the media server. Keep it as the fallback, so playback continues if the primary address fails.

Wait until the broadcast reports that it is active before you show the player. MediaSFU produces the source's quality and lower ones; it doesn't upscale an SD source to HD, so renditions tells you what viewers can get.

2. Play it​

Use hls.js wherever it is supported. iOS Safari, which can't run it, plays HLS natively. Try the addresses in order: playbackUrl, then playbackUrls, then originPlaybackUrl. Move on when one fails, but stop on a 401 or 403, because a refused token won't work anywhere else:

<video id="live" controls playsinline muted autoplay></video>
<script src="https://cdn.jsdelivr.net/npm/hls.js@1"></script>
<script>
// From your server: the broadcast's playbackUrl, playbackUrls and originPlaybackUrl, in that order.
const urls = ['PLAYBACK_URL', 'ORIGIN_PLAYBACK_URL'];
const video = document.getElementById('live');
let index = 0;
let hls = null;

function play() {
const url = urls[index];
if (window.Hls && Hls.isSupported()) {
if (hls) hls.destroy();
hls = new Hls();
hls.on(Hls.Events.MANIFEST_PARSED, () => video.play().catch(() => {}));
hls.on(Hls.Events.ERROR, (_event, data) => {
if (!data.fatal) return;
const status = data.response && data.response.code;
if (status === 401 || status === 403) return; // refused: do not try elsewhere
if (++index < urls.length) play();
});
hls.loadSource(url);
hls.attachMedia(video);
} else {
video.src = url; // iOS Safari
video.onerror = () => { if (++index < urls.length) play(); };
}
}
play();
</script>

With the MediaSFU web SDK, getHlsPlaybackCandidates(broadcast) builds that list. attachPlayback({ source, fallbackSources }) then plays it with the same rules, in SDK versions that include them.

Serve playbackUrl to viewers from your own backend. It carries a short-lived token, so fetch a fresh broadcast or URL when it expires. For large public audiences, put a CDN in front of the playback origin.

3. End the broadcast​

curl -X DELETE "https://mediasfu.com/v1/meetings/$MEETING_ID/broadcasts/$BROADCAST_ID" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MEDIASFU_API_USERNAME:$MEDIASFU_API_KEY" \
-d '{"reason":"event_finished"}'

Good to know​

  • HLS runs a few seconds behind the live room. Use WHEP when viewers need to interact in real time.
  • Broadcasts use complete CMAF segments, not partial-segment Low-Latency HLS.
  • A broadcast request is declined when streaming isn't enabled for the account, the room has ended, or the source has no video.

See the streaming HTTP reference for every field and route.