Heuristic:78 Xiaozhi esp32 Binary Protocol Versioning
| Knowledge Sources | |
|---|---|
| Domains | Embedded, Protocol_Design |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Binary audio frames use a negotiated version (1/2/3) with backward-compatible framing: v1 is raw payload, v2 adds timestamps for server AEC, v3 is a compact header.
Description
The XiaoZhi protocol supports three binary audio frame formats, negotiated via the `Protocol-Version` HTTP header during WebSocket connection:
- Version 1: Raw Opus payload bytes with no header. Simplest format, used when AEC and timestamps are not needed.
- Version 2: `BinaryProtocol2` struct with `version`, `type`, `reserved`, `timestamp`, `payload_size`, and `payload[]`. The timestamp enables server-side AEC by correlating playback audio with microphone input. Uses network byte order (htons/htonl).
- Version 3: `BinaryProtocol3` compact struct with `type`, `reserved`, `payload_size`, and `payload[]`. Removes the timestamp and version fields for minimal overhead when AEC is not used.
The version is stored per-connection and applied to both send and receive paths. The server selects the version in its `hello` response or via the `version` setting in NVS.
Usage
Use this heuristic when adding new binary frame fields or modifying the audio framing protocol. Always add a new version number rather than changing an existing struct layout — old servers and old firmware must continue to work with their negotiated version. Version negotiation happens once at connection time via the `Protocol-Version` header.
The Insight (Rule of Thumb)
- Action: Define each protocol version as a separate `__attribute__((packed))` struct. Use `htons`/`htonl`/`ntohs`/`ntohl` for multi-byte fields. Branch on `version_` in both send and receive paths.
- Value: v1 = raw bytes. v2 = 14-byte header + payload (with timestamp). v3 = 4-byte header + payload.
- Trade-off: v2 has the largest overhead (14 bytes per frame) but is required for server-side AEC. v3 is the most compact but drops timestamps. v1 is the compatibility fallback.
- Compatibility: All three versions work over both WebSocket and MQTT/UDP transports. The MQTT/UDP transport uses AES-CTR encryption on top of the binary framing.
Reasoning
Binary framing is more efficient than JSON for audio data — a 60ms Opus frame is 60-80 bytes, and wrapping it in JSON would double the size with base64 encoding and key overhead. The versioned structs allow the protocol to evolve without breaking older deployments: a server that only understands v1 will negotiate v1, while newer servers can offer v2 (for AEC timestamp support) or v3 (for minimal overhead).
The `__attribute__((packed))` ensures no padding bytes are inserted, which is critical for binary protocol compatibility across different compiler architectures. Network byte order (big-endian) for multi-byte fields ensures interoperability regardless of the ESP32's native endianness.
Code Evidence
Protocol structs from `main/protocols/protocol.h:19-33`:
struct BinaryProtocol2 {
uint16_t version;
uint16_t type; // Message type (0: OPUS, 1: JSON)
uint32_t reserved; // Reserved for future use
uint32_t timestamp; // Timestamp in milliseconds (used for server-side AEC)
uint32_t payload_size; // Payload size in bytes
uint8_t payload[]; // Payload data
} __attribute__((packed));
struct BinaryProtocol3 {
uint8_t type;
uint8_t reserved;
uint16_t payload_size;
uint8_t payload[];
} __attribute__((packed));
Version-based send from `main/protocols/websocket_protocol.cc:24-53`:
bool WebsocketProtocol::SendAudio(std::unique_ptr<AudioStreamPacket> packet) {
if (websocket_ == nullptr || !websocket_->IsConnected()) {
return false;
}
if (version_ == 2) {
// ... BinaryProtocol2 with htons/htonl ...
} else if (version_ == 3) {
// ... BinaryProtocol3 with htons ...
} else {
return websocket_->Send(packet->payload.data(), packet->payload.size(), true);
}
}
Version negotiation via HTTP header from `main/protocols/websocket_protocol.cc:83-86,104`:
int version = settings.GetInt("version");
if (version != 0) {
version_ = version;
}
// ...
websocket_->SetHeader("Protocol-Version", std::to_string(version_).c_str());