Principle:78 Xiaozhi esp32 Binary Audio Framing
| Knowledge Sources | |
|---|---|
| Domains | Network_Protocols, Audio_Streaming |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
A binary framing protocol for embedding audio payloads and metadata (timestamps, message types) into network transport frames, enabling efficient real-time audio streaming with versioned header formats that support both legacy and compact encodings.
Description
Binary audio framing defines how Opus audio packets are serialized for transmission over WebSocket or MQTT/UDP transports. The XiaoZhi firmware supports three protocol versions:
- Version 1 (raw): No header. Opus payload is sent directly as a WebSocket binary frame. No timestamp or metadata.
- Version 2 (BinaryProtocol2): A 16-byte packed header with version, type (0=Opus, 1=JSON), reserved field, 32-bit timestamp (for server-side AEC), and 32-bit payload size. All multi-byte fields are network-byte-order (big-endian via
htonl/htons). - Version 3 (BinaryProtocol3): A compact 4-byte header with 1-byte type, 1-byte reserved, and 2-byte payload size. No timestamp (for transports where server-side AEC is not used).
The protocol version is negotiated during the server hello handshake: the device sends a hello JSON with its supported version, and the server responds with the version to use. All subsequent audio frames use the negotiated version.
The AudioStreamPacket struct is the transport-neutral representation:
struct AudioStreamPacket {
int sample_rate; // e.g., 16000 (capture) or 24000 (playback)
int frame_duration; // e.g., 60 (ms)
uint32_t timestamp; // milliseconds, for server-side AEC alignment
uint32_t playback_id; // identifies the playback session
uint32_t media_position_ms; // current position in media
std::vector<uint8_t> payload; // Opus-encoded audio bytes
};
Usage
Use this principle when:
- Audio data must be sent over a text-or-binary transport (WebSocket)
- Timestamps are needed for server-side AEC alignment
- Backward compatibility with multiple protocol versions is required
- Bandwidth efficiency matters (compact headers for cellular)
Theoretical Basis
The framing uses Type-Length-Value (TLV) encoding:
BinaryProtocol2 (16 bytes header + payload):
┌─────────┬──────┬─────────┬───────────┬──────────────┬──────────┐
│ version │ type │ reserved│ timestamp │ payload_size │ payload │
│ 2 bytes│ 2 B │ 4 bytes│ 4 bytes │ 4 bytes │ N bytes │
└─────────┴──────┴─────────┴───────────┴──────────────┴──────────┘
All fields network-byte-order (big-endian)
BinaryProtocol3 (4 bytes header + payload):
┌──────┬─────────┬──────────────┬──────────┐
│ type │ reserved│ payload_size │ payload │
│ 1 B │ 1 B │ 2 bytes │ N bytes │
└──────┴─────────┴──────────────┴──────────┘
Compact format, no timestamp
The version negotiation during hello:
Device → Server: {"type":"hello","version":3,...}
Server → Device: {"type":"hello","version":2,...}
→ version_ = 2
→ all SendAudio() calls use BinaryProtocol2 format
Practical Guide
When implementing binary audio framing:
- Use big-endian for multi-byte fields:
htonl()/htons()for cross-platform compatibility. - Pack the struct: use
__attribute__((packed))to avoid padding. - Reserve a type field: allows multiplexing audio (0) and JSON (1) on the same binary channel.
- Support version downgrade: the server may select an older protocol version.
- Encrypt on MQTT/UDP: use AES-CTR for audio sent over UDP (unlike WebSocket, UDP is not encrypted).