Skip to main content

Live streams

Markdown

One person presents live to everyone watching: the picture, a chat beside it, reactions, and the count of who watches. In cloud.wordplus:realtime-calls, on the same media servers as the browser: an app watches a stream a browser presents, and goes live itself.

How a stream starts, who is who, how your server runs it and how it ends: how live streams work. What people build with them: live streaming, interviews and questions on stage and members-only streams.

Who is who​

Your server declares streams by pattern, as it declares rooms and calls (live streams in depth). Here the show of a shop is named show: and the show: show:mugs. The app asks the calls route of your server when it opens a stream, as for calls, and your rule answers with the place of the user in it. This rule decides:

  1. A show that does not exist opens for nobody.
  2. Its presenter is its host: goes live, brings guests on stage, keeps the chat and ends it.
  3. Everyone else watches, signed in or not. A visitor who is not signed in comes with no user, reads the chat and signs in to write.
  4. Viewers send reactions.
  5. Links in the chat come only from the host and those who keep the chat.
realtime.streams( 'show:{id}', async ( user, { id } ) => {
// 1. A show that does not exist opens for nobody.
const show = await findShow( id );
if ( ! show ) return false;

// 2. Its presenter is its host.
if ( user === show.hostId ) return 'host';

// 3. Everyone else watches, signed in or not.
return 'viewer';
}, {
reactions: true, // 4. Viewers send reactions.
chat: { links: false }, // 5. Links only from those who keep the chat.
} );
  • The answer: false, a role (host or viewer), or the role with more: info (the name and https picture everyone sees, your profile by default), chat (write or read), mod (true for someone who keeps the chat without hosting), src (what the host may send: camera, microphone, screen) and quality (the best picture the host sends, 720p or 1080p).
  • A visitor who is not signed in may only be a viewer. A rule that gives them more is refused with no_user.
  • The settings of the stream, the third argument, are each a value or a function of the parts of its name: stage (guests on stage, on unless false), reactions, people (who sees who is watching, mods or all), and chat: offline (open or closed while it is not live), links and fresh (the chat emptied each time it goes live) (permissions).

Drawn ready​

LiveStreamScreen of cloud.wordplus:realtime-ui is the stream as the web player draws it: the picture, Live and how many watch over it, its title, the chat with a field to write in, and a heart for reactions, which float up the picture as everyone sends them.

import cloud.wordplus.realtime.ui.LiveStreamScreen

val show = rt.calls(context).stream("show:mugs")
LiveStreamScreen.open(activity, show, title = "The new mugs") // full screen, over the activity
  • Closing it, or Back, leaves the stream and dismisses it, and onClose hears it: LiveStreamScreen.open(activity, show, title, onClose = { … }).
  • As a view of your own layout: LiveStreamScreen(context, show, title), with leavesOnClose = false to keep the stream for the app.
  • Its words are English by default, and yours by key in the language of the user: LiveStreamScreen.Appearance(strings = mapOf("live" to "Live", "watching" to "%d schauen zu")). The keys are live, watching, starts_soon, ended, say_something, send, read_only, pinned, react, close, picture, no_picture, not_sent, and for the stage ask_stage, lower_hand, invited_title, invited_text, not_now, join_stage, leave_stage, camera, microphone and stage_failed.
  • For viewers: the host goes live from the studio on the web, or with the calls of the stream below.
  • In Compose, open it from a DisposableEffect while the app is in the stream, as the API demo does.

Watching​

A screen of your own, on the stream itself:

import cloud.wordplus.realtime.calls.LiveStreamVideoView
import cloud.wordplus.realtime.calls.calls
import cloud.wordplus.realtime.calls.stream

// In a coroutine on the main thread, such as one of lifecycleScope.
val show = rt.calls(context).stream("show:mugs")

val player = LiveStreamVideoView(context)
player.show(show) // the main picture of the stream as it changes, its sound plays by itself

show.onChange { // the state, the count, the chat, the pictures
state.text = if (show.state == LiveStreamState.LIVE) "Live now" else "Not live"
count.text = "${show.viewers} watching"
}
show.onMessage { message -> add(message.name, message.text) }
show.send("Hello!")
show.react("❤️")
show.leave()
  • Reactions are any one emoji: show.react("🔥"), or LiveStreamReaction.HEART, CLAP, LAUGH, WOW and FIRE for the five the players offer. show.onReactions hears the ones sent since the last, by emoji, about every half second, and LiveStreamScreen floats each up the picture.
  • calls.stream(name, video = false) joins without the picture, for a screen that shows the chat or the count. show.watch() fetches it later, as a LiveStreamVideoView does by itself.
  • With a route of your own in place of a rule: the route checks whatever it needs and answers realtime.streamToken( user, name, { role: 'host' } ) (user null for a visitor, who can only be a viewer). A token lasts ten minutes, so the app gives a function that gets one, asked again as each runs out, and your route may then say no: rt.joinStream(context) { fetchStreamToken() }, or rt.joinStream(context, video = false) { … }. rt.joinStream(context, token) takes one token, for a stay shorter than it: expired is what it comes to when it would need another.
  • state is IDLE, LIVE or ENDED. media is where the app is in the room of the stream: OFF, CONNECTING, LIVE, RECONNECTING. While a join waits for a place on the mesh, waiting is a CallWait, until when and reason why, and onWaiting hears it. media stays as it was, and the join goes on by itself. With as many watching as the stream takes (audience), a viewer waits the same way, with the reason full and until its next try.
  • role is the place your rule gave this user: HOST, GUEST or VIEWER. viewers and peak count those watching, startedAt is when it went live, in milliseconds.
  • The quality of the picture: it comes at the size of its view unless a viewer chooses. show.qualities() gives the layers they may choose, the best first, each HIGH, MEDIUM or LOW with its size, and show.setQuality(LiveStreamQuality.LOW) holds to one, AUTO going back. None above 720p may be chosen, and none shows before the picture arrives. The sizes are worked out from the size of the picture, so a sender with other layers may differ from them a little.
  • show.stats() says how the stream plays here, to debug it: the picture and the sound that arrive, the media server and the way they come, and the session so far (how long the picture took to start, stalls, reconnects), with the last things that happened. It is measured on the device once a second and sent nowhere.
  • The main picture is the one the host chose: a shared screen, the picture from OBS, else the camera of the host. show.tiles has each picture, and player.show(show, tile = identity) draws one of them. Call player.release() once the view leaves the screen for good.
  • The chat: messages (the history, oldest first), pinned, canWrite, slow. Each message has the app user id of who wrote it, null for a visitor.
  • onEnded says why the app is out of the stream: left, banned, kicked, or the code of what it could not go on with.

In Compose, put the player in an AndroidView, and read the state of the stream from a counter that onChange raises, as the example app does.

Going live​

The app of the host shows its camera, then goes live:

show.preview()                          // the camera and microphone, in the same LiveStreamVideoView
show.setCamera(false) // or setMicrophone(on)
show.goLive()
show.end()
  • The camera and microphone need CAMERA and RECORD_AUDIO, asked for at runtime before preview(), as for calls.
  • goLive() with nothing on turns the camera and microphone on first. A host may take an ended stream live again.
  • What a role may not do throws a CallException with its code: not_host, read_only, slow, timeout.
  • From OBS: val (url, key) = show.ingest(). ingest(renew = true) makes a new key and cuts off the old one. The picture from the encoder takes the place of the camera.
  • The app sends its camera, its microphone and its screen. show.setScreenShare(true, answer) takes the answer of the system, as for a call, and show.sharingScreen says it is shared, for a host or a guest on stage. LiveStreamScreen has no button for it: this is for a screen of your own. Choosing which picture the stream shows is done from the studio on the web.

Guests on stage​

A viewer raises a hand, the host invites them, and they come on stage beside the host with their camera and microphone, for everyone watching, on the web too. LiveStreamScreen draws what a viewer does: a signed-in viewer asks with Ask to join on stage, answers the invitation in a card under the title, and once on stage has their camera, their microphone, Leave stage, and their own camera in a corner of the picture. It asks for nothing a stream would refuse: show.canAskStage says whether this user may ask now. It asks the system for CAMERA and RECORD_AUDIO when they press Join stage, from the activity it is drawn in. What the host does, seeing the hands and inviting, is in the studio on the web or a screen of your own. In a screen of your own:

// The app of a viewer:
show.hand(true) // show.hand(false) takes it down
show.onChange {
handButton.isVisible = show.canAskStage // signed in, not the host, live, and it takes guests
joinButton.isVisible = show.invited // the host asks them on
stageBadge.isVisible = show.onStage
}
show.joinStage() // on stage, the camera and microphone live, or show.declineStage()
show.leaveStage() // the cloud takes them off, and they watch again

// The app of the host:
show.onChange { drawHands(show.hands) } // List<LiveStreamHand>: user, name, at, the oldest first
show.invite(show.hands.first().user!!) // or anyone watching, by their user id
show.unstage("34") // off stage, back to watching
show.layout(LiveStreamLayout.Mode.SPEAKER) // GRID, SPEAKER, or SPOTLIGHT with a user

A guest sends the camera and microphone, the picture up to 720p. A visitor who is not signed in does not come on stage (anonymous), a stream whose settings say stage: false takes no hands (no_stage), and joinStage() with no invitation is not_invited.

Keeping the chat​

The host, and anyone the rule made mod: deleteMessage(id), pin(id), unpin(), timeout(user, seconds) (from a minute to a day, they go on watching), setSlow(seconds), clearChat(), remove(user) (out now, they may come back, and kick(user) is the same) and ban(user). isModerator says whether this user keeps the chat.

A message the chat does not take throws its code: read_only, slow, timeout, too_long, links, not_live.

The show card of the API demo opens the screen drawn ready on the show of Ann: the example app.

From your server​

Your server runs a stream as its host does, with no app open: it lists the streams live now, reads one and who watches it, writes in its chat as the shop, times out, kicks or bans someone, turns reactions off, and ends the stream. Each change reaches the app at once: a message in onMessage, someone put out in their onEnded (kicked, banned), the end in state.

await realtime.listStreams();                    // [ { name, started_at, viewers } ], those live now
const show = realtime.stream( 'show:mugs' );
await show.say( 'Free shipping until midnight', { name: 'Mug Shop' } );
await show.timeout( 77, 600 ); // no writing for 10 minutes
await show.ban( 88 );
await show.end();

Everything your server can do with a stream, in each language: live streams from your server, and the guide of your stack (Next.js, Express, Laravel, PHP, Django, FastAPI, Flask).

Webhooks​

Your server hears a stream go live and end, and each signed-in person who watched it, by webhook:

Event
stream.live, stream.ended{ id, at, stream, started_at, ended_at }: the stream went live, or ended, the times in milliseconds
stream.joined, stream.left{ id, at, stream, user, role, watch, joined_at }, and on leaving left_at and seconds: a signed-in person watching or on it while it was live. Visitors are not told

These handlers do three things:

  1. As the show goes live, its followers hear of it.
  2. As it ends, your server keeps how long it ran.
  3. As someone stops watching, your server adds the seconds they watched while it was live.
// 1. Live: tell its followers.
realtime.on( 'stream.live', ( { stream } ) => notifyFollowers( stream ) );
// 2. Ended: keep how long it ran.
realtime.on( 'stream.ended', ( { stream, started_at, ended_at } ) => saveShowLength( stream, started_at, ended_at ) );
// 3. Someone stopped watching: the seconds they watched.
realtime.on( 'stream.left', ( { stream, user, seconds } ) => addWatchTime( stream, user, seconds ) );

What WordPlus Cloud sees​

A live stream is not encrypted end to end. Its picture and chat are for everyone watching, so they are encrypted on the way, and the media servers of WordPlus Cloud see them, as a stream needs (encryption).

Refusals​

rt.calls(context).stream(name), rt.joinStream and the actions of the host throw a CallException with its code:

Code
forbiddenyour rule said no, or no pattern matches the name
bad_tokenrt.joinStream was given what is no stream's token
expiredrt.joinStream was given one token that ran out, and has no way to get another
no_useryour rule gave a visitor who is not signed in more than the place of a viewer
not_includedthe plan of your app does not include live streams
bannedthis user is banned from the stream
no_hoursthe account has used its credits this month, what it may borrow included: new viewers above the plan come in once credits are bought
not_hostan action of the host from someone who is not the host
publish_failedthe camera or microphone could not be sent into the stream