How a call works
What call.Play does from the offer to the hang-up, and which package does each part.
Your Go code calls call.Play with a whatsmeow client, a phone number and the clip. Inside go-whatsapp-call, the call package signals over whatsmeow (offer, accept, hang-up), the mlow, rtp and srtp packages turn the clip into encrypted 60 ms MLow frames, and the relay package reaches a WhatsApp call relay over a WebRTC DataChannel. Stanzas and encrypted media go through WhatsApp's servers and relays to the callee's phone, which rings, answers and hears the clip.
A call has two halves. Signaling is a handful of <call> stanzas that go through the normal whatsmeow connection. Media is RTP that goes to a WhatsApp call relay over a WebRTC DataChannel, encrypted end to end with keys only the two phones (and this library) know. The protocol reference has every byte. This page is the short version.
Encode the clip
Before anything goes out, call.Play encodes the whole clip, the lead-in sound and the outro into MLow frames of 60 ms each, plus one silent frame. An empty clip fails here, before the phone rings.
Send the offer
It looks up the callee's LID and device list, makes a random call ID and a random 32-byte call key, and sends an <offer>. The call key goes to every one of the callee's devices, each copy encrypted with that device's Signal session.
Connect to a relay
The server's ack for the offer carries a relay allocation: relay addresses, tokens and a key. The library opens a WebRTC DataChannel to each IPv4 relay and asks it for an allocation over STUN. Media goes to the first relay that answers.
Ring
Each ringing device sends a <preaccept>, and the library answers with a <relaylatency> so both sides settle on a relay. Silent frames go out every 60 ms from the moment a relay is ready.
Play
When the <accept> comes in, the library remembers which device answered and sends mute_v2 (unmuted). For LeadIn it sends the lead-in sound on a loop, or silence, then the clip, then the outro.
Hang up
After Tail, it sends a <terminate> with the call length in ms and no reason, the way the phone app does, and waits for the server's ack. Without that ack the phone shows Reconnecting and "call failed".
Signaling
The call package sends stanzas through whatsmeow's DangerousInternals().SendNode and reads the replies as whatsmeow call events. The call key is encrypted per device with EncryptMessageForDevices, which pads the plaintext itself, so the library doesn't pad it again. Padding twice makes the phone reject the call with reason="enc".
After the answer, only the device that answered counts. Other devices stop ringing with accepted_elsewhere, and a linked device that can't decrypt the call key declines with enc while the others keep ringing.
Relays
The relay package runs one controller per relay. It sends a private STUN allocate (with the relay token, the stream subscription, a connection ID and a message integrity tag) every 500 ms until the relay answers, answers the relay's binding requests, and sends a keepalive every 1.1 s.
The transport is a pre-negotiated WebRTC DataChannel built with pion. The library makes its own offer, then edits a copy into the relay's answer: the auth token as the ICE username, the relay key as the password, and a fixed relay certificate fingerprint. One DataChannel message carries one STUN, RTP or RTCP packet. ISP-hosted relays answered on port 3480 in tests, and Meta's edge relays only on their listed port 3478, so the library tries both.
Audio
WhatsApp calls carry MLow, Meta's codec built on libopus. The mlow package runs a WebAssembly build of that libopus fork through wazero, so it needs no cgo and no native library. It encodes 16 kHz mono, 60 ms frames, and its output matches the Node encoder byte for byte.
mlow/libopus.wasm is a build of a libopus fork with Meta's MLow extension. Its source and license are unclear, so don't redistribute that file. Plain Opus played nothing in a live test. Whether plain Opus frames with the TOC configs WhatsApp advertises would play is still open.
Media
The rtp package wraps each frame in RTP: payload type 120, sequence from 1, timestamp +2880 per frame on a 48 kHz clock, and the empty WARP header extension (de be 00 00). RTCP rides along on a fixed cadence: SDES before the first packet, transport feedback after the first and every 8th, stream hello after the 4th, application feedback every 25th.
Packets go out on an absolute 60 ms schedule, so delivery doesn't drift.
Keys
Each direction has its own keys. The srtp package derives them with HKDF-SHA256 from the call key, using the sender's device-qualified LID (<user>:<device>@lid) as the info, then runs the RFC 3711 AES-CM key derivation. The payload is AES-128 counter mode with no SRTP auth tag. Integrity comes from a 4-byte WARP MI tag instead: HMAC-SHA1 over the packet and its rollover counter.
The ssrc package derives the stream IDs the same way from the call ID, so both sides compute them on their own. Your uplink uses slot 0, and the relay subscription lists nine slots derived from your own LID.
srtp, ssrc, rtp and relay were each checked byte for byte against a captured call before the first live one.
What isn't done yet
- Downlink decryption. The callee's audio is received but not decoded, which one-way playback doesn't need. Two-way calls will.
- Plain Opus instead of MLow, which would let stock libopus (BSD) replace the wasm build.