Skip to Content
go-whatsapp-call

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.

  • cli is a connected whatsmeow client with a linked session. The quick start shows one.
  • peer is a phone number JID (types.NewJID("6281234567890", types.DefaultUserServer)) or a LID JID.
  • pcm is 16 kHz mono float samples in [-1, 1], the same for LeadInSound and OutroSound. mlow.SampleRate is 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.

FieldDefaultWhat it does
RingTimeout60 sHow long to wait for an answer
LeadIn1.5 sThe pause after the answer, before the clip, so the callee has the phone at their ear
LeadInSoundsilencePlays on a loop during LeadIn (a soft loading sound, say). 16 kHz mono
OutroSoundnonePlays once right after the clip, before Tail and the hang-up. 16 kHz mono
Tail1 sHow long to stay on after the clip (and outro) ends
LoggerdiscardA *slog.Logger that receives progress: ringing, relay ready, answered, clip played, hung up
DialPassed to the relay transport. For tests only

Result

FieldWhat it holds
CallIDThe call ID: 32 uppercase hex characters
AnsweredWhether the callee picked up
PlayedHow 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
ReasonThe 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:

ErrorWhy
call: empty clippcm has no samples
call: session isn't logged in or has no LIDThe 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 dialThe 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 Tail has passed. It sends a terminate with 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 with enc while 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:

PackageWhat it exports
mlowNewEncoder, Encoder.Encode for one 60 ms frame (FrameSamples samples), SampleRate
relaySTUN message builders and Parse, StartController for one relay, DialDataChannel
rtpThe RTP sender with the WARP extension and the RTCP cadence
srtpDeriveE2E for per-direction keys, AES-CM, the WARP MI tag and SRTCP
ssrcSSRC derivation and RelayPlan, the 9-entry stream plan

On this page