Call Media
The voice of a call travels between its two clients through the relay, which forwards each packet to the other party and looks at no more than the first 5 bytes of it. All integers are big-endian.
Versions
What this section describes is version 1 of call media, that of
call.incoming. A later version comes as a new type, call.incoming.v2, as
Protocol Evolution has it: the server rings each of the
callee's clients with the newest version that client and the caller's both
declared, and gives media for it. Every version keeps the first 5 bytes of a
packet, the registration and the
WebSocket transport, so that one relay carries all of them, and
every client decodes IMA-ADPCM. New codecs, control subtypes and transports
are not versions: caps bits, subtypes and media fields that a client does
not know are ignored.
Transports
| Transport | Endpoint | Unit |
|---|---|---|
| UDP | media.udp |
One datagram per packet |
| WebSocket | media.ws |
One binary message per packet |
A client tries UDP first, where it is offered, and moves to WebSocket for the rest of the call when the relay has not answered its registration within 2 seconds: when it starts, and each time it moves to another network while on UDP. The two parties of a call may use different transports.
Packets
Every packet starts with an 11-byte header:
0 version_type u8 0x60 | type: 0 audio, 2 control, 3 registration (1 is reserved)
1..4 session_id u32 media.session_id
5..8 seq u32 see below
9..10 send_time u16 sender's monotonic clock in milliseconds, modulo 65536
Audio and control packets are header ‖ AES-256-GCM(send_key, nonce, plaintext, aad = header), the 16-byte tag after the ciphertext.
seqis one counter per direction for both types: it starts at 0 and goes up by exactly 1 for every encrypted packet. A sender takes the number and encrypts under it at once, so that no two packets share one. A sender that reaches 2^32 − 1 ends the call.- The nonce is
0x00 × 8 ‖ seq. Each call's keys are new, so a key never meets a nonce twice. - A receiver drops a packet that does not authenticate, and keeps a window
of the last 1024
seqto drop repeats, after authentication. - A packet is at most 1200 bytes.
Registration
A registration tells the relay where a client is. It is not encrypted:
header (type 3, seq = registration counter, flags = 0 UDP / 1 WebSocket) ‖ cookie (16 bytes) ‖ HMAC-SHA256(media.token, header ‖ cookie)
59 bytes in all; flags sits in the header's send_time bytes (9..10) so that
the HMAC binds the registration to its transport and a UDP registration cannot
be replayed over WebSocket. Over UDP the relay checks that the address can be
answered before it sends anything there:
- The client sends a registration (
flags = 0) with an all-zero cookie. - From the address it is bound to already, the relay takes it as a keepalive and answers nothing.
- From another address it answers with a challenge: a registration of the
same layout (
flags = 0), authenticated with the same token, whoseseqechoes the client's and whose cookie only the relay can make for that participant, address and minute. Clients treat the cookie as opaque. - The client sends its registration again at once with that cookie. Valid for that address in the current or the previous minute, it binds the client there.
A client keeps one strictly increasing registration counter for the whole call
across transports; once bound, the relay refuses any re-binding (UDP or
WebSocket, including after disconnect) whose seq is not greater than the
highest seq bound so far. A client registers when it starts, after every
change of network, every 10 seconds while it hears its peer, and every 2
seconds once it has heard nothing for 3 seconds. Over WebSocket the first
message is one registration (flags = 1) with an all-zero cookie; the
connection is the binding, and no challenge is used. The relay lets each
participant send 4 Mbit/s with a burst of 256 KiB and drops what goes past it.
Control
The plaintext of a control packet is subtype (u8) ‖ body. Subtypes
0x01–0x0F belong to the call, 0x10–0x1F to audio. Unknown
subtypes are ignored.
| Subtype | Name | Body |
|---|---|---|
0x01 |
Feedback | target_bps u32 ‖ loss_q8 u8 ‖ echo_send_time u16 ‖ hold_ms u16 |
0x02 |
Hangup | Empty. Sent 3 times 40 ms apart, besides call.hangup |
0x03 |
Hold | on u8: 1 while the sender holds the call, 0 once it is back |
0x11 |
Silence | noise_level u8, dBov 0..127 |
A client holds the call while its user takes another call, such as a phone call: it sends no voice and plays none, and goes on registering and sending Feedback. It sends Hold when that changes and every 2 seconds while it holds. The other client shows the call held, and does not count the silence as lost; voice from the holding client ends it there too, should its Hold off be lost.
Rate Control
Each receiver works out how much its peer may send and tells it in Feedback. Over the last second of arrivals it measures:
loss: (expected − received) / expected overseq;rx_bps: bytes received × 8 per second, each packet counted with 28 bytes more;queue_delay:d − min(d over the last 10 s), whered = arrival_ms − unwrap(send_time), averaged over the second.
Every 250 ms it updates target_bps:
| Condition | New target |
|---|---|
loss > 10% or queue_delay > 150 ms |
max(MIN, 0.85 × rx_bps) |
loss < 2% and queue_delay < 50 ms |
max(target, min(MAX, cap, 1.05 × target)) |
| otherwise | unchanged |
cap is 1.5 × rx_bps + 20000, or 200000 when that is more and the target
has not gone down in the last 30 seconds. MIN is 10000, MAX 2500000, and
the target starts at 64000, which the sender uses until the first Feedback
comes. A received target_bps is clamped to [MIN, MAX]. Feedback goes every
500 ms, and at once when the target goes down. loss_q8 is loss × 256,
at most 255. On Feedback the sender takes rtt = now − echo_send_time − hold_ms on the 16-bit clock, sign-extended and at least 0: echo_send_time
is the send_time of the newest packet received, hold_ms how long ago it
came.
Codecs
caps bits: 0x01 decodes Opus, 0x02 decodes IMA-ADPCM, always set. Other
bits are zero. A client sends Opus where it can encode it and the peer's
caps include it, and IMA-ADPCM otherwise; the two directions may differ.
Audio Payload
The plaintext of an audio packet:
0 codec u8 0x01 Opus (48 kHz, 20 ms frames), 0x02 IMA-ADPCM (8 kHz, 20 ms frames)
1..4 first_frame_seq u32 sender's 20 ms capture frame counter of the first primary frame
5 primary_count u8 bit 7: interleaved (stride 2); bits 0..6: count 1..6 (2..6 when interleaved)
6 red_count u8 0..4
primary frames primary_count × (length, bytes); frame i has seq first_frame_seq + i × (interleaved ? 2 : 1)
red frames red_count × (offset u8 1..32, length, bytes); seq = first_frame_seq − offset
length is 1 byte for 0..127, otherwise 2 bytes,
0x80 | (len >> 8), len & 0xFF. A packet carries one codec. The frame counter
goes up by 1 every 20 ms of capture, silence included.
An IMA-ADPCM frame is 20 ms at 8 kHz, 160 samples, in 83 bytes:
0..1 predictor i16 decoder state before the first sample
2 step_index u8 0..88
3..82 samples 160 × 4 bits: sample 2k in the low nibble of byte 3+k, sample 2k+1 in the high nibble
Encoding and decoding follow the IMA/DVI ADPCM reference tables. Each frame carries its own state, so that a lost frame spoils none after it. Capture is resampled from 48 kHz to 8 kHz and playback back, with a Kaiser-windowed sinc low-pass (β = 8), 48 taps at 8 kHz, cut off at 3.5 kHz.
Sending
Opus levels:
| Level | Bit rate | Frames per packet |
|---|---|---|
| 0 | 6000 | 5 |
| 1 | 8000 | 4 |
| 2 | 12000 | 3 |
| 3 | 16000 | 2 |
| 4 | 24000 | 1 |
wire(level, red) = bitrate × (red ? 2 : 1) + 8 × 62 × 50 / frames, where 62
bytes are what each packet carries besides its frames. red is on when
loss ≥ 2%. The sender takes the highest level whose wire fits
target_bps, or level 0. It goes up at most once every 5 seconds, and only
when the higher level fit for all of them; it goes down at once. With red on,
each packet repeats the frames of the one before as red frames, the newest 4
at most.
IMA-ADPCM always sends 4 frames a packet, 39400 bit/s, and with red, 72600
bit/s, when red is on and the target allows it.
Silence
Voice activity is detected on 20 ms frames: a frame is speech when its RMS is at least 2.5 times the noise floor and at least −50 dBFS. The floor follows frames that are not speech, falling at once to a quieter frame and rising by 3 dB a second at most. Speech lasts 600 ms after its last frame, and the 2 frames before it begins are sent with it. In silence nothing is encoded, and the sender sends Silence every 500 ms; the receiver plays comfort noise at that level and does not count the missing frames as lost.
Playout
- Jitter.
Jis the RFC 3550 interarrival jitter of frames againstseq × 20 ms. The target delay isT = span + 3 × Jwithin 40 to 400 ms,spanbeing the duration of the newest packet's primary frames. - Decoding. Frames are decoded in
seqorder. A frame missing when it is due is concealed by Opus PLC, a frame of zero length, for 100 ms at most, then by comfort noise; IMA-ADPCM gaps are comfort noise. - Time stretching. Decoded audio plays through WSOLA: at 1.08 times speed
while the buffered delay is more than 20 ms above
T, at 0.92 while it is more than 20 ms below, else at 1. No frame is dropped for it. - Talk spurts. The first frame after silence plays
Tafter it arrived. A frame older than the last played is late and dropped, unless it is more than twice the greatest delay behind: then the peer started its counter over, and playout starts from it. Late frames do not changeJ.
Audio and video are not synchronised; there is no video in version 1.
Test Vectors
protocol/calls/vectors/ holds byte-exact examples of packets, registrations,
audio payloads and IMA-ADPCM frames, which every implementation reproduces.