Go API
call.Play, its options and the result it returns.
res, err := call.Play(ctx, client, types.NewJID("6281234567890", types.DefaultUserServer), pcm, call.Options{
LeadIn: 3 * time.Second,
LeadInSound: loading, // optional, loops during LeadIn
OutroSound: chime, // optional, plays after the clip
})import "github.com/zakiego/go-whatsapp-call/call"
func Play(ctx context.Context, cli *whatsmeow.Client, peer types.JID, pcm []float32, opts Options) (*Result, error)Play calls peer, plays pcm once they answer, then hangs up. It blocks until the call is over.
cliis a connected whatsmeow client with a linked session. The quick start shows one.peeris a phone number JID (types.NewJID("6281234567890", types.DefaultUserServer)) or a LID JID.pcmis 16 kHz mono float samples in[-1, 1], the same forLeadInSoundandOutroSound.mlow.SampleRateis 16000.
The clip is encoded to MLow before the phone rings, so an empty clip fails right away with call: empty clip. Cancel ctx to end the call early: Play hangs up, then returns the result so far with ctx.Err().
Options
Zero values pick the defaults.
| Field | Default | What it does |
|---|---|---|
RingTimeout | 60 s | How long to wait for an answer |
LeadIn | 1.5 s | The pause after the answer, before the clip, so the callee has the phone at their ear |
LeadInSound | silence | Plays on a loop during LeadIn (a soft loading sound, say). 16 kHz mono |
OutroSound | none | Plays once right after the clip, before Tail and the hang-up. 16 kHz mono |
Tail | 1 s | How long to stay on after the clip (and outro) ends |
Logger | discard | A *slog.Logger that receives progress: ringing, relay ready, answered, clip played, hung up |
Dial | Passed to the relay transport. For tests only |
Result
| Field | What it holds |
|---|---|
CallID | The call ID: 32 uppercase hex characters |
Answered | Whether the callee picked up |
Played | How much went out after the answer, in 60 ms steps. Counts the outro too |
EndedBy | "us" when the library hung up after the clip, "callee" when they hung up or declined, "timeout" when nobody answered |
Reason | The callee's terminate or reject reason, if any |
A call nobody answered isn't an error: err is nil and EndedBy is "timeout". err is set when the call couldn't start:
| Error | Why |
|---|---|
call: empty clip | pcm has no samples |
call: session isn't logged in or has no LID | The whatsmeow client isn't linked, or its store has no LID yet |
call: no LID known for ... (message them first) | WhatsApp addresses calls by LID, and neither the store nor a device query knew the callee's. Message the number from the calling account first |
call: device list for ... | whatsmeow couldn't fetch the callee's devices |
call: no IPv4 relay to dial | The server offered no IPv4 relay, or none could be dialed |
Who can end the call
- The library, once the clip and the outro have played and
Tailhas passed. It sends aterminatewith the call length and waits for the server's ack, so the phone doesn't show "Reconnecting". - The callee, by hanging up or declining. 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 withencwhile the others keep ringing. RingTimeout, when nobody answers.- Your
ctx.
Lower-level packages
call is the one most code needs. The others are exported so they can be tested against captured bytes, and they follow the protocol reference:
| Package | What it exports |
|---|---|
mlow | NewEncoder, Encoder.Encode for one 60 ms frame (FrameSamples samples), SampleRate |
relay | STUN message builders and Parse, StartController for one relay, DialDataChannel |
rtp | The RTP sender with the WARP extension and the RTCP cadence |
srtp | DeriveE2E for per-direction keys, AES-CM, the WARP MI tag and SRTCP |
ssrc | SSRC derivation and RelayPlan, the 9-entry stream plan |