garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/notes/calls.md

raw


topic: calls triggers: [calls, meeting.rs, meeting.ts, "core::meeting", "meeting record", poll, agenda, "voice call", "video call", str0m, sframe, SFrame, SFU, opus, opus-pure, VP8, VP9, WebRTC, webrtc, Galene, "video on a prim", calls.rs, call_screen.rs, call_tones.rs, systems::call_screen, platform/camera.rs, platform/screen.rs, platform/screen, wayland.rs, hw_video.rs, web_call.rs, webcall.js, call-worker.js, call.ts, call-tones.ts, webcall.d.ts, web_call_test.ts, web-push.ts, crates/atlas/web/src/sw.js, crates/calls, cartopolis_calls] updated: 2026-10-17

Calls: encrypted voice and video, forwarded by the simulator

Private-chat calls (one-to-one and group, voice and video) are end-to-end encrypted and travel over UDP through the simulator, which forwards frames it cannot open. Proximity voice (voice-chat.md) is a different thing and stays as it is: it is public, like nearby chat.

Decided (2026-09-29), and why

Measured

2026-09-29, a throwaway crate outside the workspace:

Hardware encoding on the phone (2026-10-04)

platform::hw_video encodes a call's video with the phone's own codecs, MediaCodec through its C API (libmediandk, as the camera uses libcamera2ndk): no Java, no Gradle, no C of ours. It takes the first of: the system's VP9 encoder if it is hardware, the system's VP8 encoder if hardware, Google's software VP9 (c2.android.vp9.encoder, the system's libvpx), else the pure-Rust encoder. "Hardware" is decided by name: Google's codecs are c2.android.* / OMX.google.*. NV12 input is asked for first, I420 second, and the input format's stride / slice-height are honoured. Keyframe requests become request-sync; a turn of the phone reopens the codec at the new size. It sits behind video::WithFallback, so its first error hands the stream to the Rust encoder from a keyframe for the rest of the call.

A hardware encoder sends 640×480 (video::HW_WIDTH/HW_HEIGHT), four times the pixels — the camera's own size, so the phone's picture is only turned, never shrunk. The encoder says its size (VideoEncode::picture_size), the session publishes it (Session::send_size) and the camera is told each frame (Camera::set_send_size), so a hardware encoder that fails mid-call and hands over to software also shrinks what the camera sends. The bitrate grows with pixels to the power 0.6: ~575 kbit/s VP9, ~1 Mbit/s VP8 — through a forwarder that does no congestion control yet, which is the real limit on going higher. Software encoders (Google's and ours) stay at 320×240. CARTO_VIDEO_SIZE=small keeps hardware at 320×240 too.

What a receiver pays for 640×480, measured in Rust on the VPS: ~7–8 ms to decode a frame (VP8 or VP9), on the decode thread, plus a 1.2 MB texture upload per picture at 15 fps on the main thread — fine one-to-one, worth watching in a group of several senders on a phone.

Only encoding: at 320×240 decoding is ~1.5 ms in Rust, and a hardware decoder would add a copy out of its buffers for nothing.

Compiled for aarch64 only, never run. The emulator here cannot run the app. The phone's log says what happened: call video: encoder chosen (name, codec, hardware) and call video: encoding every 5 s with the mean ms a picture took. CARTO_VIDEO_ENCODER=software skips it for an A/B. Which encoders a phone has: adb shell dumpsys media.codec | grep -iE "vp8|vp9".

Where the call's CPU goes (2026-10-04)

An audit of the media path found the main thread doing work for pictures nobody saw. What changed, so it is not undone:

And on the screen: the timer counts from the answer, not from dialling; "Reconnecting…" shows while the connection is in its 30 s grace (link::Output::Lost); a remote picture with nothing after it for 3 s goes back to the person's initial (a camera turned off used to freeze on its last frame); the in-call buttons carry labels; hanging up says "Call ended · 04:12"; and a private message that fails to send goes back into the message box.

Running it

calls_listen (UDP) turns calls on; the dev binary defaults it to 127.0.0.1:4749 as it does the HTTP port. calls_public_addr is the IP:port clients reach it at, and is required behind NAT or on 0.0.0.0: WebRTC puts that address in the SDP as the one candidate, so it must be an address, not a hostname. The port is UDP and goes straight to the container — Caddy carries none of it — so a deployment publishes it in Docker and opens it in any firewall. calls_addr on SimHandle says what was bound.

A chat whose hub is another server has its call on that server's forwarder; this one only forwards the signalling (chat-across-servers.md, "Calls").

Known gaps in the server

Not yet known

Echo cancellation on Android needs AAudio's VOICE_COMMUNICATION input preset, which cpal cannot set; the browser's encoded-transform worker glue against our wasm build; whether the Adreno 840 has a hardware VP8/VP9 encoder; how the phone's upload budget (upload-bursts.md) copes with a decoded video stream, which matters doubly for video on a prim.

The phone's camera (2026-09-30)

Camera2 through its C API (libcamera2ndk) into an AImageReader (platform::camera's phone module): no Java and no GPU surface, and the pictures arrive as 4:2:0 planes the call encodes directly. The front camera is chosen when there is one. Three decisions:

Not run on a device: the emulator on the VPS cannot run this app, so the permission prompt, the orientation and the picture itself are unverified.

Calls in the browser (2026-09-30)

The browser's own WebRTC, microphone, camera, Opus and VP9 (web/webcall.js, driven from platform::web_call), in the same shape as the native link: one connection up, one down, both to the forwarder. Every encoded frame passes through an encoded-transform worker (web/call-worker.js) that seals it on the way out and opens it on the way in, so the forwarder carries SFrame from a browser exactly as from the app. Decisions:

Browsers sending VP9 (2026-10-17)

The worker seals with a clear prefix whose length depends on the codec (VP8 10 or 3 bytes, VP9 10 or 1), so it must know every outgoing frame's codec, and a frame sealed with the wrong prefix opens for nobody.

Verified 2026-09-30, tools/web_call_test.ts: the web client in headless Chrome 154 (its fake camera and microphone) and the native client (stand-in camera and tone) in one call through a local simulator. After four minutes the app had decoded 3,391 pictures and ~11,300 audio frames from the browser; the browser's RTP statistics showed 3,063 pictures and 11.3 million audio samples decoded from the app. Every frame both ways was sealed and opened by the two SFrame implementations, so this is also the cross-check the vectors are for.

What the test found along the way, all fixed:

And what a test has to know:

On the chat page (2026-10-10)

The chat page (crates/atlas/web, src/lib/call.ts) calls with the same webcall.js, and the split is forced by where things can live: media (getUserMedia, RTCPeerConnection) exists in a page and not in a worker, while the keys and the connection live in the chat worker. So the page runs the call and the worker carries its signalling (call_join, call_answer, call_leave, call_decline) and answers call_keys — the chat's 16-byte SFrame key for its current epoch, from the MLS exporter — which the page asks for again every second so a commit mid-call moves the key with it. One call at a time, in the tab that placed or answered it; other tabs only see it ring. The rules are the app's: a roster without you rings, an empty one is the call over, a minute without an answer ends a call placed here. webcall.js gained an onStream option so a page with its own layout gets the streams instead of the viewer's elements, and deploy-web.yml watches the three files it shares with the viewer. chat-browser.ts checks it end to end: two headless Chromium with fake devices ring, answer, and each sees the other's video.

It is drawn as the app's call screen (2026-10-11). A call coming in is a dialog over the page — the caller large on a pulsing initial, Decline red, Video glass and Answer green, round keys with a label under each — and a banner with the same keys over a call already going on or once folded with the arrow. The call itself is a panel to the right of the chat, and one button (Swap) puts it in the middle with the chat as the panel; a phone has room for one, so there it is the whole screen or a green bar. The keys are theme::call_button's four tones in CSS, and the app's chat header now uses those keys too instead of a text button. The people are an even grid, not a masonry: every picture is a camera frame of one shape, so the only decision is the column count (gridColumns: the squarest grid in the middle, one column then two in the panel), and one other person fills the panel with your own picture in its corner. standInClient lets a preview mount the real page against a stand-in client with canvas.captureStream() cameras — how layouts with several people were looked at with nobody to call; nothing about a real multi-party call has run in the browser.

The page makes the app's call sounds too (src/lib/call-tones.ts): the ringtone, the 425 Hz ringback, the call-waiting beep and the join, leave and end cues, synthesised from the same notes and levels as call_tones.rs. A browser keeps audio suspended until the page has had a click or a key press, so a call arriving in a tab nobody has touched since it loaded rings silently.

Ringing a closed app (2026-09-30, protocol 29)

A phone with a UnifiedPush distributor installed (ntfy is the common one) is woken when a call starts in one of its chats. Push.java asks the distributor for an endpoint at startup, PushReceiver.java keeps it in the cartopolis_push preferences, and the running app hands it to the server (SetPushEndpoint, systems::push). When the first person joins a call, the server posts to every other member's endpoints (simulator::push), and the receiver shows a ringing "Incoming call" for 45 s. Opening it connects, and the roster rings as it does for any connected device. Decisions:

Ringing a closed browser (2026-10-11)

The web chat rings a browser whose chat page is closed through the browser's own push service (Web Push): src/lib/web-push.ts subscribes once notifications are allowed and registers the address with the same SetPushEndpoint as the app, marked webpush: (simulator::push::WEB_PREFIX); src/sw.js shows "Incoming call" and opens the chat on a tap, and the chat then rings with the caller's name as usual. Decisions:

A call that waits (2026-09-30)

Both ends used to create the down (receiving) connection at the join and tick it from then on, though nothing is negotiated on it until a second person joins. Its DTLS handshake timer ran from creation, so a caller alone for ~30 s was dropped ("DTLS error: timeout: handshake"), and one answered later still failed the moment it was used ("timeout: connect"). The down connection is now made at its first offer, on both sides. And the forwarder never renegotiates a receiving connection down to nothing: an offer stopping every track has no BUNDLE group, str0m refuses it, and that ended the call for the last person left in it. The departed tracks wait to be stopped by the next offer; the client drops their pictures on the roster. link::tests::a_caller_alone_for_a_minute_is_still_in_the_call covers all three.

Agents reach contacts through MCP (request_contact, answer_contact, contacts), because a call needs two people to be contacts first.

Ringing like a phone (2026-09-30, protocol 31)

A call rings with a synthesised ringtone and an incoming-call card (Decline red, Answer green); your own ringing plays the 425 Hz ringback and gives up after 60 s ("No answer"); cues mark joins, departures and the end; a call that stopped ringing unanswered leaves "Missed call from …". Which tone plays is call_tones::wanted_loop, a pure function of the call state. Declining is on the wire (CallDecline → CallDeclined to the chat's connected devices): in a one-to-one chat the caller's call ends at once with "… declined", in a group it is only said, and the decliner's other devices stop ringing without counting it as missed. Agents have decline_call.

The call screen, and coming back to it (2026-10-01)

systems::call_screen draws calls the way Signal and WhatsApp do: an incoming call takes the whole screen (Decline red left, Answer green right, at thumb height); in a call the other person fills the screen — their picture cropped to fill when it is the screen's way up, letterboxed when not — with your own picture in a corner, a grid for groups, and the controls on the bottom edge, fading over a picture after 4 s. The arrow, or Back, folds it to a green bar at the top so the chats and the map work during a call; it never ends a call. --ui callincoming|call|callvideo|callgroup|callfolded captures each.

Four faults behind "the call flow is broken", each with its cause:

When Android does stop the process (a swipe from recents, a reclaim), Chats or World and the open chat come back from user.json (UserData::screen), put back at each sign-in, including the one after a resume. A ringing call is drawn over whatever was restored. The map itself still streams again after a real cold start — from the disk cache, not the network — because the GPU state does not survive the process.

Not verified on a device: the emulator cannot run the app, so the back gesture, the restore and the call screen on a phone are unwatched.

A call is a meeting (2026-10-17, protocol 41)

Design: docs/mockups/meetings/ (v2). Every call has a host — the first person in; when the last host or co-host leaves, whoever has been there longest takes over (simulator::calls::succeed), so a call never loses its host. Decisions, so they are not undone:

On the chat page (src/lib/call.ts, chat.tsx's callStage): the stage, a floating dock, notices with their own button, a people island, Speaker and Gallery, M / V / H and Space held to talk. Who is talking, and talking into a muted microphone, are measured on the page from the sound it already plays (Web Audio analysers; webcall.js keeps an unmuted clone of the microphone for the second), so the server learns nothing from either. Verified 2026-10-17 by crates/atlas/web/meeting-browser.ts: three Chrome for Testing profiles on mac-2 against a real simulator — host role, a raised hand and its notice, a sealed reaction opened by another browser, a host's mute that the held browser cannot undo, Let talk, a shared screen as the stage, end for everyone. A canvas stands in for getDisplayMedia, which a headless browser has no screen for. Running it took the node: the VPS node has no browser and its glibc is newer than this container's, so its binaries do not run here.

What it found: the page's WebCall.close() threw on every hang-up — with onStream it marked a person's tracks as true and close then called .remove() on them — so the page never set "The call ended" or any other reason. Older than the meetings; fixed in webcall.js.

What a meeting leaves in the chat (2026-10-17)

Planning a meeting, its answers, polls and votes, notes taken in the call and the record afterwards are sealed chat messages, not call state: they must outlast the call, reach people who were not in it, and the server must read none of them. Each is an ActivityStreams object in Payload::Object, read and written by cartopolis_core::meeting (the reference) and the web's src/lib/meeting.ts, both tested against crates/core/testdata/meeting-vectors.json.

Verified 2026-10-17 by meeting-browser.ts (plan, answer, poll, vote, a decision in the notes panel, the agenda started by the host and seen by a member, and the record arriving after End for everyone). It found that a record card mixing keyed sections and empty holes threw on every redraw, which froze the page after the call; Mithril refuses that mix.

Guests, by link (2026-10-18)

Somebody with no account can join one meeting. Decided with the person steering, between two ways of giving a guest the call's key: the key is in the link — not "add the guest to the chat", which would show a guest the chat's messages for as long as they were in it.

Verified 2026-10-18 by meeting-browser.ts's fifth browser: the link, the door, the waiting room, and video both ways, which only works if the handed key opens both ways.

It found an older bug, from when the join started offering a screen track: the server dropped any call answer over 16 kB without a word. A browser's answer to a down offer has a media line per track of everybody else (about 2.4 kB each), so once a fourth person joined a browser meeting every answer was dropped and nobody received anything new. The limit is 256 kB now (calls::MAX_SDP_BYTES, with the measurement), and a dropped answer is logged.

The app shares its screen, and suppresses noise (2026-10-18)