skip to content

Media Delivery

Stream encodes and delivers video, Images resizes and optimizes pictures, and Realtime relays WebRTC audio and video. Interviewers ask how to protect paid media and serve the right size.

on this pageshow

explore

questions

17

With the Cloudflare Realtime SFU API, how does a backend publish a participant's camera and let another participant pull it?

level: middleimportance: must knowfreq 30%

answer

  1. one session per PeerConnection
  2. local versus remote track entries
  3. who sends the offer each way
  4. a second call to finish pulling

basics

~20 s

Publish by calling sessions/new, then tracks/new with the browser's SDP offer and a local entry naming the track. Pull by calling tracks/new on the viewer's own session with a remote entry, then submit the browser's answer to /renegotiate.

solid answer

~40 s

Every call goes from the backend to `https://rtc.live.cloudflare.com/v1/apps/{appId}` with `Authorization: Bearer <APP_SECRET>`. To **publish**, the browser adds a `sendonly` transceiver and sets its local offer; the backend calls `POST /sessions/new`, then `POST /sessions/{sessionId}/tracks/new` with that offer and an entry `{ location: "local", mid, trackName: "camera" }`, and the browser applies the returned answer. To **pull**, the viewer gets its own session and the backend calls `tracks/new` with `{ location: "remote", sessionId: <publisher's>, trackName: "camera" }`; the SFU returns an offer, the browser answers, and the backend sends that answer to `PUT /sessions/{sessionId}/renegotiate`. The SFU announces nothing: your app shares session IDs and track names with authorized viewers.

code

json · 14 lines
json
{
  "tracks": [
    {
      "location": "remote",
      "sessionId": "<CLINICIAN_PUBLISH_SESSION_ID>",
      "trackName": "camera"
    },
    {
      "location": "remote",
      "sessionId": "<CLINICIAN_PUBLISH_SESSION_ID>",
      "trackName": "microphone"
    }
  ]
}

go deeper

for a junior

Know the nouns: app, session as one PeerConnection, track named by trackName, and that the App Secret stays on the server.

for a middle

Walk through both directions and say who sends the offer each way, and why pulling needs the extra /renegotiate call.

for a senior

Bring up per-item errors, per-session serialization, separate publish and receive sessions, and the 30-second media inactivity rule.

for a principal

Point out that rooms, discovery and permissions are your code on the SFU, which is exactly what RealtimeKit would otherwise provide.

## The model: applications, sessions and tracks Cloudflare's **Realtime SFU** forwards WebRTC media between endpoints, but it has no rooms and no presence. You get three building blocks: - An **SFU application**, with an **App ID** and **App Secret**. Separate apps keep development and production apart. - A **session**, which is one endpoint's WebRTC connection to the SFU; in a browser, one `RTCPeerConnection` maps to one `sessionId`. - A **track**, which is a published audio or video stream named by a `trackName` inside its publishing session. A subscriber locates it by the publisher's `sessionId` plus that `trackName`. Each participant uploads its camera once and the SFU forwards a copy to every session that asks for it. The general case for an SFU over a mesh is covered in the WebRTC topology lessons. What is specific here is that nothing is forwarded until a session explicitly pulls a track. ## Who calls what | Layer | Owns | | --- | --- | | Browser | Captures media, owns the `RTCPeerConnection`, creates and applies SDP | | Your backend (for example a Worker) | Authenticates users, holds the App Secret, calls the SFU API | | Application state (for example one Durable Object per visit) | Membership, which tracks exist, who may pull them | | Realtime SFU | Establishes WebRTC sessions and forwards the selected tracks | Every SFU API call carries `Authorization: Bearer <APP_SECRET>`. The secret stays on the backend: a browser holding it could open, pull or close any session in the app. ## Publishing a camera 1. In the browser, call `getUserMedia()`, add each track with `pc.addTransceiver(track, { direction: "sendonly" })`, then `createOffer()` and `setLocalDescription()`. Once ICE candidates are gathered, read each transceiver's `mid`. 2. The backend calls `POST /apps/{appId}/sessions/new` with no body and stores the `sessionId`. Doing this only when the browser is ready matters, because an unconnected session can expire. 3. The backend calls `POST /sessions/{sessionId}/tracks/new` with `sessionDescription` set to the browser's offer and `tracks: [{ location: "local", mid, trackName: "camera" }]`. Camera and microphone can go in one request, each with its own `mid` and `trackName`. 4. The browser applies the returned **answer** with `setRemoteDescription()`. No `/renegotiate` call is needed in this direction. 5. When the connection reports `connected`, the app records the publisher's `sessionId` and track names in its visit state. ## Pulling a track 1. The viewer creates a new `RTCPeerConnection` with a `track` handler, and the backend opens a second SFU session for it. 2. The backend calls `tracks/new` on the **viewer's** session with `tracks: [{ location: "remote", sessionId: "<publisher session>", trackName: "camera" }]` and no offer. One request can pull several tracks, even from different publishers. 3. This time the SFU returns an **offer**, flagged `requiresImmediateRenegotiation: true`. The browser applies it, creates and sets an answer, and returns it. 4. The backend submits the answer to `PUT /sessions/{sessionId}/renegotiate`. The pull is finished only when that call succeeds. A browser reaching `signalingState: "stable"` does not prove the SFU has the answer. 5. In the `track` handler, `event.transceiver.mid` tells the viewer which publication arrived. The receiving `mid` can differ from the publisher's. ## What it looks like for a ten-person visit - Each participant opens a **publishing session** carrying two local tracks, `camera` and `microphone`, in one `tracks/new` request. - Each participant also opens a **receiving session** and pulls the other nine people's tracks: up to 18 remote entries, which fits in one request under the 64-track cap. - When someone new joins, every existing receiving session pulls two more tracks, and each of those pulls is its own offer/answer exchange. - When someone leaves, the app removes their tracks from discovery and closes the matching subscriptions on every receiving session. ## Details that bite - **Check every item.** A `200` response can still carry per-track `errorCode` values; record a track as ready only when its own result succeeded. - **One mutation at a time per session.** Track creation, closure and renegotiation on one session must be serialized; different sessions can run in parallel. - **Limits.** The API allows 50 requests per second per session and up to 64 tracks in one request. A track that receives no media packets for 30 seconds is garbage-collected. - **Two sessions per participant is fine.** Cloudflare's video-room example gives each browser separate publishing and receiving sessions so their negotiations never collide. - **Closing.** `PUT /sessions/{sessionId}/tracks/close` takes the `mid`s to stop. With `force: true` it stops forwarding without an SDP exchange. - **NAT traversal is still yours.** The SFU path does not hand the browser TURN servers, so a team building on the SFU directly adds Cloudflare TURN credentials to its `iceServers` itself.

  • Why might a team give each participant two SFU sessions instead of one?
    Each session has one PeerConnection and one offer/answer state machine. With separate publishing and receiving sessions, a new pull never has to wait for, or collide with, the participant's own publish negotiation. One bidirectional session also works if every mutation on it goes through the same queue.
  • What happens to a published track when the clinician's camera stops sending for a while?
    A track that receives no incoming media packets for 30 seconds is garbage-collected. The app must publish it again and rebuild the subscriptions that pointed at it. Session reuse after a connectivity loss is a separate 30-second window.
  • How does a viewer pick a lower video quality from a simulcast publisher?
    The remote track entry in `tracks/new` can carry a `simulcast` object with `preferredRid` and fallback policies, and `PUT .../tracks/update` changes `preferredRid` on an existing subscription by its receiving `mid`. Both `priorityOrdering` and `ridNotAvailable` default to `none`, so nothing falls back unless you set them.

  • The SFU automatically forwards every participant's tracks to everyone in the room.
  • The browser can call the SFU API itself with the App Secret embedded in the page.
  • Pulling a track is finished once the browser's signalingState is stable.
  • A subscriber locates a track by its mid alone.
  • An HTTP 200 from tracks/new means every requested track succeeded.
open as a page

In Cloudflare Stream, how do direct creator uploads let instructors upload long lectures from the browser without exposing your API token?

level: middleimportance: must knowfreq 32%

basics

~20 s

Your backend, which holds the API token, asks Stream for a one-time upload URL and hands only that URL to the browser, which uploads straight to Stream. Lectures over 200 MB need the tus form of direct creator uploads.

open as a page

In Cloudflare Stream, how do requireSignedURLs and signed tokens keep paid lessons playable only by enrolled students?

level: middleimportance: must knowfreq 33%

basics

~20 s

Setting requireSignedURLs on a Stream video stops its bare ID from playing anywhere. Your backend checks enrolment, then issues a signed token that replaces the video ID in the player or manifest URL until its exp passes.

open as a page

In Cloudflare Images, how does a /cdn-cgi/image/ URL resize and convert a remote image, and what must the zone enable first?

level: juniorimportance: should knowfreq 30%

basics

~10 s

A /cdn-cgi/image/<OPTIONS>/<SOURCE> URL asks Cloudflare to fetch the original, apply comma-separated options such as width=640,format=auto, and cache the result. It works only after transformations are enabled for that zone under Images > Transformations.

open as a page

In Cloudflare RealtimeKit, what does the backend create so a patient can join a video visit, and what does the client SDK receive?

level: juniorimportance: should knowfreq 24%

basics

~20 s

The backend uses a Cloudflare API token to create a reusable meeting and add each person as a participant with a preset. That returns a participant auth token, the only credential the client SDK receives to join.

open as a page

In Cloudflare Stream, what are the ways to upload a video, and when must an upload use the tus protocol?

level: juniorimportance: should knowfreq 28%

basics

~10 s

Cloudflare Stream takes videos from the dashboard, a basic multipart POST, a link Stream downloads itself (/stream/copy), or the resumable tus protocol. Any file over 200 MB must use tus.

open as a page

In Cloudflare Images hosted storage, how do named variants, flexible variants and signed URLs decide which versions of an image a browser can fetch?

level: middleimportance: should knowfreq 20%

basics

~20 s

A hosted image is served as imagedelivery.net/<ACCOUNT_HASH>/<IMAGE_ID>/<VARIANT>. Named variants fix the sizes on offer, flexible variants (off by default) allow any parameters in the URL, and requireSignedURLs makes an image need an expiring signed URL.

open as a page

With the Cloudflare Images binding, how does a Worker transform image bytes, and when is it a better fit than fetch()'s cf.image?

level: middleimportance: should knowfreq 22%

basics

~10 s

The Images binding takes raw bytes: env.IMAGES.input(stream).transform({...}).output({ format: "image/webp" }), then .response(). It suits images with no public URL, such as uploads or private storage, and custom operation order; results are not cached automatically.

open as a page

In a Cloudflare Worker, how do fetch()'s cf.image options resize images, and how do you keep the Worker from resizing its own source requests?

level: middleimportance: should knowfreq 25%

basics

~10 s

A Worker calls fetch(sourceURL, { cf: { image: {...} } }) and returns the result. Route it only on an image path, keep originals elsewhere, and pass through requests whose Via header contains image-resizing.

open as a page

With Cloudflare Realtime TURN, how does a backend turn a TURN key into short-lived credentials for browsers, and how long may they last?

level: middleimportance: should knowfreq 28%

basics

~20 s

A TURN key is a long-term server-side secret that cannot authenticate to turn.cloudflare.com. The backend calls the key's generate-ice-servers endpoint with a ttl, up to 48 hours, and hands the returned iceServers to the browser.

open as a page

In Cloudflare Stream, how does a live input take a weekly class over RTMPS, SRT or WHIP, and what decides whether it is recorded?

level: middleimportance: should knowfreq 22%

basics

~20 s

A Stream live input is a reusable ingest point the encoder pushes to over RTMPS, SRT or WHIP. With recording.mode set to automatic, each RTMPS or SRT broadcast plays over HLS and DASH and is recorded; WHIP broadcasts are not.

open as a page

A marketplace on Cloudflare finds strangers using its /cdn-cgi/image/ URLs to transform photos hosted on other sites; why is that possible, and how do you lock it down?

level: seniorimportance: should knowfreq 30%

basics

~20 s

The zone's transformation Sources were most likely set to Any origin, so any public image can be transformed on the marketplace's bill. Switch back to allowed origins, list only real hosts, close their open redirects, and cap sizes with presets.

open as a page

In a telehealth room on the Cloudflare Realtime SFU, subscriptions sometimes fail with HTTP 406 when two clinicians join at once; why, and how should the backend fix it?

level: seniorimportance: should knowfreq 15%

basics

~20 s

Two handlers sent tracks/new to the same receiving session before the first SFU offer had been answered through /renegotiate, so the SFU rejected the overlap. Route every mutation for a session through one queue that waits for the full exchange.

open as a page

Cloudflare Realtime telehealth calls fail on a hospital network that allows only outbound TCP 443, though the app hands out TURN URLs on its own custom TURN hostname; why, and what fixes it?

level: seniorimportance: should knowfreq 14%

basics

~10 s

On Cloudflare TURN, TCP 443 carries only TURN over TLS, and custom TURN hostnames do not support TLS. Hand out the turns:turn.cloudflare.com:443?transport=tcp URL, or have the firewall allow TURN's other ports.

open as a page

On a Cloudflare Stream course site where every paid lesson requires signed URLs, outsiders can still watch the weekly live class and its replays; what explains it?

level: seniorimportance: should knowfreq 16%

basics

~20 s

Playing a Stream live input by its ID uses the input's own recording.requireSignedURLs and allowedOrigins, not any video's. Left at their defaults, the class plays without a token anywhere, and its replays inherit the same open settings.

open as a page

As the lead for a marketplace's photo pipeline, how would you choose between Cloudflare Images and running your own resizing service behind Cloudflare?

level: principalimportance: should knowfreq 15%

basics

~20 s

Compare cost shape, control and operating burden: Cloudflare Images bills monthly unique transformations or stored and delivered images, a self-run resizer bills compute and engineers. Decide on catalogue size versus traffic, privacy, processing needs and exit cost.

open as a page

For a telehealth app adding ten-person video visits, how would you choose between Cloudflare RealtimeKit, building on the Realtime SFU directly, and running your own media server?

level: principalimportance: should knowfreq 18%

basics

~20 s

Pick RealtimeKit when you want meetings fast, paying per participant minute. Pick the Realtime SFU when you need control over tracks and own signaling, paying for egress. Self-host only when a hard requirement neither can meet justifies running media servers.

open as a page