Skip to main content

Calls

Markdown

Calls between two people and group calls, in cloud.wordplus:realtime-calls, on the same relay and media servers as browsers: a phone and a browser call each other, and either may add someone. The Better Messages call screen, in cloud.wordplus:realtime-ui, draws them with no screen code of yours. Both libraries are optional.

  • Calls between two people ring on every page and app the callee has open. They take one of three paths, tried in turn: directly between the two devices, through our relays (TURN) on the ports of the web, 443 and 80, when a network blocks that, and in a room on our media servers when even the relays cannot reach. A call moves on its own, without hanging up, and takes no credits on any path.
  • Group calls run on the WordPlus Cloud media servers, with no limit on people in a call. Up to 32 people on video at once take no credits with 1,000 connections, and 12 more for each 1,000 after them, a person on audio counting half. Each one above takes a credit a minute from your account.
  • Encrypted end to end, with keys only your server gives out: a call between two people on every path, and the group call it becomes. A group call your server names is encrypted end to end when its rule says encrypt (what WordPlus Cloud sees).
  • Calls ring a locked phone and a closed app, with a call's notification and Telecom, as phone calls do, once the app starts SystemCalls (below).
  • User ids are yours: digits or text such as user_2abc, up to 64 characters with no space. The SDK takes them as strings ("34", "user_2abc").

How a call starts, runs and ends, for the app and for your server: how calls work. What people build with them, from a support call to a meeting: use cases.

Who may call​

Your server declares calls by pattern, as it declares rooms (calls in depth). Here a support call between two people is named support: and their two ids, smaller first: support:34:42. A meeting is named meeting: and its team: meeting:daily. The app asks your calls route ({endpoint}/calls) with its headers each time it places, answers or joins a call, as the browser does, and your rule answers. These rules decide:

  1. A support call is only between the two people its name holds: whoever calls and whoever is called are both in it.
  2. Your server asks twice, as the caller places the call (side.role is caller) and as the callee answers (callee). Here anyone called may answer.
  3. Only placing the call is checked further: one of the two is staff, so a customer calls an agent and an agent calls a customer, but two customers never call each other.
  4. A meeting is for the members of its team alone.
  5. Its lead is its host (admin), who mutes and removes people from the web call screen (the host).
  6. A meeting is encrypted end to end (encrypt).
realtime.calls( 'support:{a}:{b}', async ( user, withUser, { a, b }, call, side ) => {
// 1. Both people are in the name, as text: user 34 is "34" there.
const pair = [ a, b ];
if ( ! pair.includes( String( user ) ) ) return false;
if ( ! pair.includes( String( withUser ) ) ) return false;

// 2. Anyone called may answer.
if ( side.role === 'callee' ) return true;

// 3. Placing it: one of the two is staff.
const callerIsStaff = await isStaff( user );
const calleeIsStaff = await isStaff( withUser );
return callerIsStaff || calleeIsStaff;
} );

realtime.groupCalls( 'meeting:{id}', async ( user, { id } ) => {
// 4. Members of the team only.
if ( ! await isMemberOf( id, user ) ) return false;

// 5. Its lead is its host.
return { admin: await isLeadOf( id, user ) };
}, { encrypt: true } ); // 6. Encrypted end to end.
  • isStaff, isMemberOf and isLeadOf are your own lookups, in your own data.
  • What a rule may answer: false, true, or an object with info (what the others see of the user, your profile name and avatar by default), and in a group call publish and subscribe (true unless said), admin and hidden (false unless said) (who may call).

The call screen​

import cloud.wordplus.realtime.calls.calls
import cloud.wordplus.realtime.ui.CallScreen

val calls = rt.calls(context)
CallScreen.attach(context, calls) // rings for every call coming in, and shows it once answered
CallScreen.onAnswered = { answered ->
when (answered) {
is IncomingCall.Answered.ToCall -> follow(answered.call) // the call a ring became
is IncomingCall.Answered.ToGroup -> follow(answered.group) // or the group call it rang into
}
}

viewModelScope.launch {
val call = calls.start("support:34:42", "34", CallType.VIDEO)
CallScreen.show(context, call, name = "Ann")

val meeting = calls.group("meeting:daily", CallType.VIDEO)
CallScreen.show(context, meeting, title = "Daily meeting")
}
  • It is the Better Messages screen itself, the same files as its app: the ring with Answer and Decline, the call between two with the picture of the other side and your own in the corner, the grid of a group call, and the controls (microphone, camera, turning the camera round, the speaker and audio route, picture in picture, the people of a group call, hanging up).
  • It is an activity of its own over the app, which the library declares. Its chevron puts it away and the call goes on, as CallScreen.hide() does from a button of your own: show a bar of your own meanwhile (CallScreen.active says a call is on), and CallScreen.restore() brings it back.
  • The microphone and camera: it asks for them when a call is answered, if the app has not been allowed them yet.
  • A call ringing during another is answered busy.
  • A call that becomes a group call comes back on the screen, as the group call.
  • Its words and colours: CallScreen.configure(CallScreen.Appearance(strings = mapOf(…), tokens = mapOf(…), tokensDark = mapOf(…), darkMode = false)), the strings by the Better Messages keys (answer, reject, end_call, calling, optimizing_video over a camera changing codec …), the colours by its design tokens ("--bm-color-accent" to "#21759b"). The English is the default for any string left out (customizing).
  • CallScreen.log hands you the lines the screen writes, for your own log.

The call screen on an Android phone, from its preview with no call: on the left a video call from Ann ringing in, with Answer and Decline. On the right a call to Ann ringing out, with the camera, microphone, speaker and hang up

Ringing a locked phone​

SystemCalls makes the app's calls phone calls to Android, as the Better Messages app's are. A call rings a locked phone and a closed app with a call's notification, Answer and Decline, and a ring screen over the lock screen. Telecom carries its audio routes, a cellular call holds it, and a car or a watch answers it. It goes on with the app in the background, with a call's foreground service and Hang up in the notification.

Start it in your Application, because a call can start the process with nothing else running:

class ShopApplication : Application() {
override fun onCreate() {
super.onCreate()
SystemCalls.start(this, SystemCalls.Options(name = "Shop", screen = MainActivity::class.java)) {
Session.realtime() // the signed-in user's connection, or null when nobody is signed in
}
}
}

And hand the call's messages over from your Firebase messaging service:

class Messaging : FirebaseMessagingService() {
override fun onNewToken(token: String) = SystemCalls.pushToken(token)

override fun onMessageReceived(message: RemoteMessage) {
if (SystemCalls.push(this, message.data)) return // a call, or a call called off
// your app's own messages
}
}
  • How a closed app rings: the phone goes with the user's line, so their calls ring it. A call sends it a high-priority message, the notification rings at once, and your lambda gives a connection. The SDK holds the line on it, and the call goes on as if it had come in while the app was open.
  • Answer on the notification opens screen, your activity, with the call: it arrives in SystemCalls.onAnswered { answered -> … }, and the call screen (realtime-ui) shows it by itself.
  • With the app open, a call rings on the app's own screen, as before, and the notification stays quiet under it. Answered on either, it is answered once.
  • The key: WordPlus Cloud sends the message with your Firebase service account, which you add to the app on the account's Apps page, under Phone calls (shipping). WordPlus Cloud keeps it, and the realtime servers get only short-lived tokens made from it.
  • The caller's name on the ring is the name in the caller's info from your server. The message carries that info sealed, and the phone opens it with the day's key, which the SDK keeps on the phone for this, encrypted with a key of Android's keystore (where the keys are kept). Google sees the call's id and name, the caller's user id and the call type, never the name of the caller.
  • Over a locked screen, from Android 14, is an access the person grants: SystemCalls.canRingFullScreen(context) says whether the app has it, and SystemCalls.openFullScreenSettings(context) opens the page where they allow it. Without it a locked phone rings with nothing on its screen to answer from.
  • Signing out: rt.calls(context).stopRinging() takes the phone off the user's line, so a closed app no longer rings for them.
  • A ring during a call is answered busy.
  • Not without Telecom: on Android 7 (API 24 and 25), or a device that refuses the app's registration, calls still ring with the notification, with no system audio routes. SystemCalls.isStarted says whether start was called.
  • Its words are string resources: wordplus_calls_answer, wordplus_calls_decline, wordplus_calls_incoming_video, wordplus_calls_incoming_audio, wordplus_calls_channel, wordplus_calls_ongoing_channel. Give them in your app's resources to translate or change them.

How the system shows the calls:

SystemCalls.start(this, SystemCalls.Options(
name = "Shop", // shown for a caller whose name is not known yet
screen = MainActivity::class.java, // the activity a call answered from the notification opens
icon = R.drawable.ic_call, // the notification's small icon, the system's call icon by default
)) { Session.realtime() }

Calls of your own​

The same calls, for screens you draw:

calls.onIncoming { incoming ->
// incoming.from, incoming.info (name, avatar), incoming.type, incoming.group (a ring into a group call)
incoming.onOver { reason -> hideRing() } // answered elsewhere, the caller gave up …
viewModelScope.launch {
when (val answered = incoming.accept()) { // or incoming.decline(busy = true)
is IncomingCall.Answered.ToCall -> show(answered.call)
is IncomingCall.Answered.ToGroup -> show(answered.group)
}
}
}

val call = calls.start("support:34:42", "34", CallType.VIDEO)
call.onState { state -> label = state.wire } // ringing, connecting, connected, reconnecting, ended
call.onRemote { // what the other side sends changed
remoteView.show(call.remoteCamera)
cameraOffBadge.isVisible = call.remoteMedia["camera"] != true
}
call.onEnded { reason -> close(reason) } // hangup, declined, busy, cancelled, group …

val remoteView = CallVideoView(context) // a View
val ownView = CallVideoView(context).apply { mirrored = call.frontCamera }
ownView.show(call.localCamera)

call.microphone = false // mute
call.camera = false // stop sending the camera
call.flipCamera() // the front one to the back, and back
call.end() // hangs up, or calls it off while it rings
remoteView.release() // when the view goes

calls.onError { code -> log(code) } // the line refused, or a group call that could not join
  • Ask for RECORD_AUDIO and, for video, CAMERA before a call, as for any screen of your own. The library manifest declares them. One allowed only after the call began: call call.permissionsGranted(), and the call opens it then.
  • call.remoteMedia says what the other side has on: camera, microphone, screen_share and screen_share_audio, each true or false, read again at each onRemote.
  • call.path says what carries it: DIRECT, RELAY (TURN) or ROOM. call.peer and call.peerInfo who the other side is, call.role whether this app is its CALLER or CALLEE, call.frontCamera whether the camera sent is the front one.
  • calls.incoming lists the calls ringing for the user now.
  • calls.onError hears what went wrong away from any one call: your server refused the line its calls ring on (forbidden, expired …), or a group call could not join its room (room_failed, and the group call ends with it).
  • Where the sound goes: a video call starts on the speaker and an audio call at the earpiece. The call screen's speaker button moves it, and its settings list every output, a headset or a car included. With SystemCalls, Telecom carries the routes, so a car or a watch takes the call. A screen of your own moves it with Android's AudioManager, as for any call.
  • calls.stopRinging() lets go of this app line: its user calls no longer ring here.
  • start, group, accept and add switch to the main thread themselves. Call them from any coroutine.

Adding people​

Once a call is up, either side may ring someone else into it. Both sides go on in a group call of their own, without hanging up:

call.onGroup { group -> follow(group) }        // both sides, and the call then ends as `group`
val invite = call.add("43") // rings user 43, throws its code
  • Your rule decides, as for any call: the user calling the person about the call name, then the person taking a call from them.
  • invite is the CallInvite rung: invite.to, and invite.state as it was rung, ringing. The person comes into group.people once they answer.
  • The person rung in gets an IncomingCall whose group is set, and accept() answers ToGroup.
  • group.add(user) rings more people in, from anyone in it. A group call your server names (calls.group) takes nobody that way: not_a_call.

Group calls​

How a group call starts, who gets in, how your server runs it and how it ends: how group calls work.

val group = calls.group("meeting:daily", CallType.VIDEO)
group.microphone = true // nothing is sent into a group call until asked
group.camera = true
group.flipCamera() // the front camera to the back, and back
group.onPeople { people = group.people } // List<GroupPerson>: user, info, camera, microphone, screen, speaking, publish, optimizing
group.onState { state -> label = state.wire }
group.onEnded { reason -> close(reason) } // left, disconnected, room_failed
group.leave()
  • group.encrypted says whether its media is encrypted end to end: always for the group call that comes from a call, and for one your server names when its rule says encrypt.
  • Drawing it yourself: a GroupVideoView shows anyone's camera, view.show(person.identity, group), or the screen they share from a browser with GroupVideoSource.SCREEN, and view.showOwn(group) this app's own camera. Audio plays by itself.
  • A camera changing codec: a browser in the call may move its camera to the codec its computer sends best, which takes the camera away for a moment. GroupPerson.optimizing is true from its word until the new camera arrives, the camera is turned off, or the media server's failsafe has passed (10 seconds unless it sets another): draw a placeholder such as "Optimizing video quality..." over their picture then, not a camera turned off. The call screen does it for you, and holds its placeholder until the new camera's first frame.
  • What this app sends: its camera and microphone. Sharing the screen and raising a hand are not in the Android SDK yet: a person shares their screen from a browser, and the app shows it.

The host​

The host of a group call is whom its rule answers admin: true. In a browser, the call screen gives the host buttons on the tile of each of the others: mute, let speak, make a listener, remove (permissions).

The Android SDK has no host controls yet. GroupCall has no way to mute, remove or let speak someone else, and the call screen shows none. A host does it from the web call screen, or your server does it from a route of yours that checks who asks, below (buttons for the host). The app hears each change: a person muted in onPeople, its own right to speak in onPermissions, and the app taken out in onEnded with disconnected.

Speakers and listeners​

A class, a town hall or an audio room: your rule makes the listeners (publish: false), and your server lets one speak while the call runs, with realtime.call( "class:7" ).permit( user, { publish: true } ) (speakers and listeners). The app hears it:

group.onPermissions { can ->
group.microphone = can.publish // let on: it speaks. Back to listening: the media server took it down already
}
val speakers = group.people.filter { it.publish }
  • group.can is what the app may do now, GroupPermissions(publish, subscribe): what your rule said, then what your server changed.
  • Back to listening, microphone and camera turn false, and nothing is sent again on a reconnect.
  • Everyone sees who may speak: GroupPerson.publish, with onPeople when it changes.
  • A listener asks to speak through a room of yours that carries hands, as the browser does: rt.join("class:7").emit("hand", buildJsonObject { put("up", true) }) from the app, when the room rule lists hand in emit.

From your server​

Your server sees the group calls going on and runs them, with no app open: who is in a call, muting someone, letting them speak or making them a listener, taking them out, and ending the call for everyone. Call these from a route of yours that checks the user asking is the host:

await realtime.listCalls();                     // [ { name, people, started_at } ]
const meeting = realtime.call( 'meeting:daily' );
await meeting.get(); // { people, started_at, list: [ { user, joined_at, camera, microphone, screen } ] }
await meeting.mute( 43, 'microphone' ); // 'camera' or 'screen' too
await meeting.permit( 43, { publish: false } ); // a listener now
await meeting.remove( 43 ); // out now
await meeting.end(); // out, everyone

A call between two people runs between their devices, so it is not in listCalls(). More in calls from your server.

Why a call ended​

call.endReason, and the reason onEnded gets: the same codes as the browser (why a call ended), plus connection_failed when no path connected. A group call ends with left when the app leaves it, disconnected when the media server lets the app go (your server removed it or ended the call, or its connection did not come back), and room_failed when it could not join. A call that could not start throws a CallException with the code: forbidden, no_user, not_included, auth_failed (errors).