Calls and audio processing
Calls connect the call screen, WhatsApp call events, local audio devices, and the call history. The system keeps the user-facing state tied to what the call service and media streams report. It also protects nearby media, reduces echo, and provides opt-in data for diagnosing poor audio or video. For the wider media model, see Media and calling, Media capture, preview, and editing, and Stickers and emoji.
Call experience
The call experience has two linked parts: the live call surface and the call history. The live surface lets the user answer, decline, mute, change devices, control video, hide the surface, or hang up. The history gives each call a durable record and a way to start or return to a similar call. The wider media model is described in Media and calling.
Call lifecycle
The call lifecycle is coordinated by a worker that owns one active call for an account. A call update is the shared snapshot that tells the UI the phase, outcome, devices, video state, and group roster. The UI may show an outgoing call immediately as placing, but later phases come from signaling and the media path rather than from the button action.
The main phases describe claims that become true in order:
- Placing means local setup is in progress and the peer has not been called.
- Dialing means the offer was sent.
- Ringing means the peer's device is ringing.
- Incoming means the user must accept or decline an offer.
- Connecting and Accepted mean the answer exists and media is being set up.
- Active means media is available. The duration starts here.
- Ended or Failed records the final result and why it happened.
The worker rejects a second active call. It also rejects offers that the client cannot handle, such as unsupported group-call cases, instead of leaving the other side waiting. When a call reaches a terminal phase, its media resources are released while the UI may keep a short farewell view.
This diagram shows how user actions and remote events meet in the call coordinator.
flowchart LR
U[User starts or answers] --> W[Call worker]
S[Remote signaling] --> W
M[Media events] --> W
W --> Q[Call update]
Q --> V[Call surface]
Q --> H[Call history]
U -->|mute, camera, device, hang up| W
References: src/calls.rs• L65-128 src/…/calls.rs• L154-179 src/…/calls.rs• L721-802 src/…/calls.rs• L255-367
Call surface
The call surface presents the current call without deciding its state. A one-to-one call shows the other person and the current status. A group call shows a roster of joined and invited people. Video replaces a person's avatar with a live picture only while the related video state says that the picture is still being sent.
The user can leave the full call surface without ending the call. The window then shows a bottom strip with the person, status, duration, and a way back. Full screen changes only the window view. It does not change the call. If a locked chat hides the call, the surface and its pictures stay behind the lock until the folder is opened.
References: src/…/call.rs• L35-160 src/…/call.rs• L162-283 src/…/call.rs• L285-356 src/…/calls.rs• L85-185 src/…/calls.rs• L212-230
Call history
The Calls page combines call records from the phone with calls taken by the desktop client. It folds adjacent calls with the same chat and broad result into one row. Each row still shows the newest call's type, direction, result, duration, and group participant count when that information exists.
The page supports all-call and missed-call views, search, frequent contacts, voice or video start actions, and call-back actions. A call-link record is shown as a standalone entry because it has no chat to open. Local records are reconciled with the phone's own call record so one call does not appear twice.
References: src/…/calls.rs• L19-36 src/…/calls.rs• L132-169 src/…/calls.rs• L188-249 src/…/calls.rs• L316-407 src/…/calls.rs• L985-1074 src/…/calls.rs• L1106-1176
Notifications and tones
An incoming call uses the chat's notification rules. Muted, archived, and locked chats do not disclose or announce a call in the normal way. When an announcement is allowed, the notification names the caller unless the app lock requires a generic message. A group call can name both the group and the person who started it.
The call phase controls the call-specific tone. An outgoing call plays ringback only while the peer is ringing. An incoming call plays the ringtone only while it waits for an answer. Ending, answering, declining, hiding, or dismissing the call stops the tone. Group calls that the desktop cannot join use a notification that tells the user to answer on the phone and do not start a local ringtone.
References: src/…/calls.rs• L455-475 src/…/calls.rs• L486-607 src/…/calls.rs• L634-671 src/call_tones.rs• L1-17
Media coordination
Calls share the desktop's media rules with voice messages, video playback, and recording. The goal is to avoid competing sounds while keeping the call itself alive when the user moves through the app.
Media pause behavior
When the setting to pause other media is enabled, a live call asks the shared media-pause service for quiet. The service pauses only players that were playing when the first request arrived. It resumes only those players after the last request ends, so overlapping recordings, playback, and calls do not pause twice or resume too early.
The pause work runs away from the UI and keeps requests ordered. Linux uses MPRIS and Windows uses system media controls. macOS has no public API for this feature, so the setting is not offered there.
References: src/app.rs• L10736-10759 src/media_pause.rs• L1-20 src/media_pause.rs• L29-53 src/media_pause.rs• L119-131 src/…/settings.rs• L450-456
Device selection and recovery
The call screen exposes microphone, speaker, and camera choices. A missing choice falls back to the system default for audio or the first available camera. The call keeps the user-facing warning about the device that was lost, so the selected label does not silently claim to be in use.
Device changes replace the stream behind the call's media channel instead of rebuilding the call. If a headset, speaker, microphone, or camera disappears during a call, the worker discovers the new device set and publishes a new call update. Audio can continue on a fallback device while the surface explains what changed.
References: src/calls.rs• L259-405 src/calls.rs• L434-500 src/…/call.rs• L593-656 src/…/calls.rs• L899-977
Audio path
The audio path turns the device's native stream into the fixed voice format used by the call engine, then performs echo and noise treatment before sending microphone frames. The speaker path reverses the format conversion and also supplies the reference needed by echo handling.
Audio chains
The call engine uses a 16 kHz mono voice stream. Devices may use another rate or channel count, so the input is mixed to mono and resampled before it is framed. The output is resampled to the speaker's format and expanded to its channel count. Partial device reads are carried forward so frame boundaries remain stable.
The microphone and speaker each run behind a stable call channel. A device switch or recovery replaces only the stream at that edge. This lets signaling and codec state continue while the local audio device changes. The speaker also watches for a stream that accepts data but stops playing, then restarts or falls back when recovery cannot restore sound.
References: src/call_audio.rs• L1-17 src/call_audio.rs• L33-56 src/call_audio.rs• L100-199 src/call_audio.rs• L201-226 src/call_audio.rs• L555-618 src/call_audio.rs• L868-970
Echo handling
The echo path removes the far-end voice that the microphone hears through room speakers. The speaker provides a live, mono copy of what the device actually played. The microphone side uses that copy as its reference and aligns it with the microphone signal before it sends the result to the call engine.
The processing chain handles echo, steady background noise, and quiet input level. It can follow a new speaker stream and discard stale reference audio after a device change. With headphones, the same chain detects that there is no useful speaker echo and avoids removing the user's voice.
References: src/…/echo.rs• L1-18 src/…/echo.rs• L44-95 src/…/echo.rs• L250-371 src/…/chain.rs• L1-10 src/…/chain.rs• L31-56
Video and diagnostics
Video uses the same call coordinator but has its own capture, transport, decode, and display signals. Diagnostics describe whether media is flowing without exposing private call data by default.
Video flow
An outgoing video call starts only when the camera and video path can be prepared. A camera failure does not silently turn a requested video call into a voice call. During a voice call, the camera control can request a video upgrade when the call service supports it.
Local and remote pictures move to the UI as frames. Video decode stays off the UI path. The UI uses the latest frame only while the call update says that the related camera is active. If a decoder needs a keyframe, the media side asks the peer for one instead of showing a stale picture as live.
References: src/calls.rs• L502-514 src/calls.rs• L520-549 src/calls.rs• L715-767 src/calls.rs• L1288-1424 src/…/calls.rs• L654-713
Diagnostic signals
The call surface reports conditions that need user action or help from support. These include a lost local device, missing peer audio, audio that arrives but cannot be decoded, and a failed connection. The worker also checks device availability and media health during its periodic call reconciliation.
Video diagnostics keep counts of sent frames, keyframes, decoded frames, undecodable frames, and the largest keyframe. These counts help distinguish a broken capture path from a transport or decode problem. Logs describe rate changes and peer video format without storing the call's destination.
References: src/calls.rs• L130-190 src/…/call.rs• L373-475 src/…/calls.rs• L909-977 src/call_diag.rs• L1-15 src/call_diag.rs• L92-159 src/calls.rs• L1427-1463
Diagnostic dumps
Diagnostic dumps are opt-in because they contain conversation audio or video. Setting ZAPFAST_CALL_AUDIO_DUMP writes the echo reference, raw microphone input, processed output, and replay order as 16 kHz mono WAV data. Setting ZAPFAST_CALL_VIDEO_DUMP writes sent and received raw H.264 data. Neither dump is enabled by default, and a write failure stops the dump rather than the call.
The audio dump supports offline replay of the exact processing order. The video dump can be opened by tools such as ffmpeg. Treat both dump types as sensitive recordings: enable them only with the other participant's agreement and remove them after diagnosis.
References: src/…/dump.rs• L1-19 src/…/dump.rs• L28-78 src/call_diag.rs• L7-18 src/call_diag.rs• L145-211
Change checklist
When these files or sections are changed, remember to consider these:
- Keep
CallPhase,CallOutcome, the call-screen status text, and history summaries aligned. A new result can otherwise be shown as a generic end state or filed with the wrong history label. - Keep device ids, picker values, fallback updates, and the audio pump's binding rules aligned. A changed device identity can make the UI report one device while the stream uses another.
- Keep the audio dump file set and order.csv replay contract aligned with the echo chain. A format change can make a diagnostic recording impossible to replay.
- Keep
ZAPFAST_CALL_AUDIO_DUMPandZAPFAST_CALL_VIDEO_DUMPopt-in behavior visible in both support guidance and privacy review. - Keep
call_live()andwants_quiet()aligned so a call does not resume other media while its audio is still active. - Keep remote-video flags and frame lifetime rules aligned. A frame must not remain visible after the peer stops sending or after a private call is hidden.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| No usable microphone or speaker before an outgoing call | The call fails before it rings the peer. | Check the devices selected for the call before signaling. |
| A selected device disappears during a call | Audio or video falls back to another device and the surface warns the user. | Reconcile device lists and publish lost-device state. |
| A speaker accepts frames but never plays them | The call can remain connected but silent. | Restart the output and move to the system default after repeated unplayed streams. |
| A peer's audio stalls or cannot be decoded | The user sees why the call is silent instead of only hearing silence. | Preserve the peer-audio diagnostic in the call update. |
| A requested video path cannot start | A video call is rejected instead of silently becoming voice. | Require a working video pipeline before signaling. |
| A video queue fills or a keyframe is lost | The remote picture pauses until the decoder can start again. | Drop late frames and request a new keyframe. |
| Another account or chat starts a second call | The new call is blocked or declined while the existing call stays intact. | Enforce one active call in the worker and route updates by generation. |
| Audio or video dump variables point to a folder | Participant media is written to local files. | Enable dumps only for agreed diagnosis and delete them after use. |
Key files
| File | Why to read it |
|---|---|
| src/…/calls.rs | Shows how call updates, user actions, notifications, privacy, tones, and account routing meet. |
| src/…/calls.rs | Shows call ownership, signaling, media events, device reconciliation, and history filing. |
| src/calls.rs | Defines the call phases, shared update model, media coordination, and video flow. |
| src/…/call.rs | Shows the live call surface, incoming prompt, group layout, controls, and user-visible diagnostics. |
| src/…/calls.rs | Shows the Calls page, history grouping, search, filters, and call-start actions. |
| src/call_audio.rs | Shows the stable microphone and speaker paths and local device recovery. |
| src/…/echo.rs | Shows how played speaker audio becomes the microphone's echo reference. |
| src/…/chain.rs | Shows the high-level voice processing stages used by calls. |
| src/media_pause.rs | Shows the shared pause-and-resume behavior for other applications' media. |
| src/call_diag.rs | Shows opt-in video counters and raw H.264 dump behavior. |
| src/…/dump.rs | Shows opt-in audio dump files and the replay contract. |