Exchange call media over WebSocket
Audio and video for a call travel on a media WebSocket, separate from the
control socket. JSON text messages manage tracks. Binary messages
carry the audio and video, each with a 36-byte WXCM header. Every message is
listed in the calls WebSocket reference.
Attach and exchange media
-
Negotiate version 3 or 4 on the control socket, claim a call, and wait for
call.statewith"state": "connected". Keep the owning control socket open. -
Open
/v1/accounts/{account}/calls/{callId}/mediawith acallsbearer token, or with a ticket fromPOST /v1/accounts/{account}/calls/tickets("socket": "media"and thecallId). Within five seconds, send:{"type": "media.attach","supportedProtocolVersions": [3, 4],"clientId": "CLIENT_ID","ownerToken": "OWNER_TOKEN"}Use the
clientIdfrom the control socket’shello, and theownerTokenfrom thecommand.resultthat claimed the call. Never log the token.For synthetic audio and video that must stay in lip sync (protocol version 4), add
"mediaSync": "shared_clock"and optionally"maxSyncSkewMs": 400(up to1000). Stamp matching audio and video frames with the same monotonic microsecond clock intimestampMicros. LeavemediaSyncout for live microphone and camera calls, so frames go out at once with no added delay. -
Wait for
media.ready. ItssendTracksandreceiveTrackslist the tracks you can use. Abridged, with one outgoing audio track:{"type": "media.ready","protocolVersion": 4,"sendTracks": [{"media": "audio","id": 1,"generation": 1,"direction": "send","format": {"sampleRate": 16000, "channels": 1, "frameMs": 20}}],"receiveTracks": []}Video calls also list video tracks. Check
directVideoorconferenceVideobefore you advertise video codecs. -
Start each outgoing track before you send its frames, then send binary frames:
{"type": "track.start", "trackId": 1, "generation": 1}Receiving does not need
track.start.
Binary WXCM v3 frame layout
Every binary message starts with a 36-byte big-endian header. PCM16 samples in the payload stay little-endian.
| Offset | Size | Field | Meaning |
|---|---|---|---|
0 | 4 | magic | ASCII WXCM (57 58 43 4d) |
4 | 1 | version | 3 |
5 | 1 | kind | 1 = pcm_s16le, 2 = h265, 3 = h264, 4 = jpeg |
6 | 2 | flags | Bit 0x0001 keyframe, 0x0002 codec config (VPS/SPS/PPS), bits 0x000c clockwise rotation (0x0000 = 0°, 0x0004 = 90°, 0x0008 = 180°, 0x000c = 270°) |
8 | 4 | trackId | Track ID from media.ready / track.added |
12 | 4 | generation | Active track generation |
16 | 8 | sequence | Per-track monotonic frame sequence (0 allowed on client TX) |
24 | 8 | timestampMicros | Microseconds on the track timeline (for PCM receive, sampleOffset * 1_000_000 / 16_000) |
32 | 4 | payloadLength | Byte length of payload (must equal remaining frame length) |
36 | N | payload | Raw PCM16LE samples, Annex-B H.265/H.264 access unit, or standalone JPEG |
| Property | Limit |
|---|---|
| Audio | PCM16 little-endian, 16,000 Hz mono, 20 ms daemon frames (640 bytes) |
| Maximum audio payload | 65,536 bytes |
| Outgoing audio queue | Eight seconds (256,000 bytes). See Pace outgoing audio |
| Maximum video payload | 512 KiB (524,288 bytes) per frame |
| Maximum WebSocket frame | 524,324 bytes (36 + 512 KiB) |
Pace outgoing audio
The daemon plays outgoing audio in real time and queues up to eight seconds of it. That covers network jitter, not a whole reply: text-to-speech often produces audio several times faster than it plays. A chunk that doesn’t fit ends the call. The daemon sends this on the media socket, then closes both sockets:
{"type": "call.error", "code": "capacity"}To play a reply of any length:
- Keep the reply in your own buffer.
- Send the first chunk as soon as you have it.
- Send the rest as it plays, staying a few hundred milliseconds ahead.
- To interrupt, empty your buffer and send
call.clear_audio. Then start the new generation before sending new audio.
The daemon doesn’t report what it has played, so your own pacing tells you how much of an interrupted reply the other person heard.
Receive tracks and video keyframes
A conference has one receive track per participant, with a participantId that
links it to the roster. Tracks come and go through track.added and
track.removed on both sockets. Apply those events in order to the binary frames.
To ask the remote sender for a fresh keyframe, for example after your decoder resets, send this on the media socket:
{"type": "video.request_keyframe", "trackId": 65538}When a remote peer asks for a keyframe from your camera, the daemon sends
video.keyframe_requested on the media socket with the trackId and
generation.
Stop watching
Watching is on when a call connects. After call.watch turns it off, the daemon neither decodes nor delivers new video; a few frames already on their way may still arrive. Audio, your camera and the track list stay as they are.
| Call | What is asked of the other side while you don’t watch |
|---|---|
| Direct | A lower bitrate, about 30% lower in our tests. The other side keeps sending |
| Conference | The daemon unsubscribes from each participant you don’t watch. The server may still send their video; in a two-person test it did |
Turning watching on again asks for a full picture. Until it arrives, no new frame is delivered.
Mute, camera toggle and interrupt
call.mute, call.camera, call.clear_audio and (on protocol version 4) call.clear_video go on the control socket. Each one that succeeds starts a new send generation and emits track.updated. call.clear_video keeps active unchanged and also sends video.keyframe_requested on the media socket:
{ "type": "track.updated", "callId": "CALL_ID", "track": { "media": "video", "id": 65537, "generation": 2, "direction": "send", "format": {"codec": "h265", "mode": "passthrough", "maxWidth": 640, "maxHeight": 480, "maxFps": 15}, "active": false }}To resume after a change, stop the old producer, send track.start for the new
generation if active is true, and then send fresh frames. The daemon discards
frames from older generations and rejects unknown track IDs.