Skip to content

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

  1. Negotiate version 3 or 4 on the control socket, claim a call, and wait for call.state with "state": "connected". Keep the owning control socket open.

  2. Open /v1/accounts/{account}/calls/{callId}/media with a calls bearer token, or with a ticket from POST /v1/accounts/{account}/calls/tickets ("socket": "media" and the callId). Within five seconds, send:

    {
    "type": "media.attach",
    "supportedProtocolVersions": [3, 4],
    "clientId": "CLIENT_ID",
    "ownerToken": "OWNER_TOKEN"
    }

    Use the clientId from the control socket’s hello, and the ownerToken from the command.result that 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 to 1000). Stamp matching audio and video frames with the same monotonic microsecond clock in timestampMicros. Leave mediaSync out for live microphone and camera calls, so frames go out at once with no added delay.

  3. Wait for media.ready. Its sendTracks and receiveTracks list 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 directVideo or conferenceVideo before you advertise video codecs.

  4. 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.

OffsetSizeFieldMeaning
04magicASCII WXCM (57 58 43 4d)
41version3
51kind1 = pcm_s16le, 2 = h265, 3 = h264, 4 = jpeg
62flagsBit 0x0001 keyframe, 0x0002 codec config (VPS/SPS/PPS), bits 0x000c clockwise rotation (0x0000 = 0°, 0x0004 = 90°, 0x0008 = 180°, 0x000c = 270°)
84trackIdTrack ID from media.ready / track.added
124generationActive track generation
168sequencePer-track monotonic frame sequence (0 allowed on client TX)
248timestampMicrosMicroseconds on the track timeline (for PCM receive, sampleOffset * 1_000_000 / 16_000)
324payloadLengthByte length of payload (must equal remaining frame length)
36NpayloadRaw PCM16LE samples, Annex-B H.265/H.264 access unit, or standalone JPEG
PropertyLimit
AudioPCM16 little-endian, 16,000 Hz mono, 20 ms daemon frames (640 bytes)
Maximum audio payload65,536 bytes
Outgoing audio queueEight seconds (256,000 bytes). See Pace outgoing audio
Maximum video payload512 KiB (524,288 bytes) per frame
Maximum WebSocket frame524,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:

  1. Keep the reply in your own buffer.
  2. Send the first chunk as soon as you have it.
  3. Send the rest as it plays, staying a few hundred milliseconds ahead.
  4. 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.

CallWhat is asked of the other side while you don’t watch
DirectA lower bitrate, about 30% lower in our tests. The other side keeps sending
ConferenceThe 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.