Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:78 Xiaozhi esp32 Bounded Queue Backpressure

From Leeroopedia
Knowledge Sources
Domains Embedded, Audio, Optimization
Last Updated 2026-10-17 00:00 GMT

Overview

Fixed-capacity ring buffer queues with drop-newest backpressure prevent unbounded memory growth in the audio pipeline without blocking real-time capture.

Description

The audio pipeline uses `FixedQueue<T, Capacity>`, a compile-time fixed-capacity ring buffer with no dynamic allocation. When the queue is full, `push_back()` returns `false` — the newest item is dropped rather than blocking the producer or growing the buffer. The encode queue tracks `encode_drop_count` for diagnostics. Queue capacities are calibrated to the Opus frame duration (60ms): the decode queue holds 20 frames (1200ms of audio), the send queue holds 40 frames (2400ms), and the encode/playback queues hold just 2 tasks each.

This prevents the audio pipeline from accumulating unbounded latency when the consumer (opus encoder, network sender, or speaker output) falls behind. Dropping the newest packet is preferred over dropping the oldest because the oldest data represents the most time-sensitive audio — dropping it would cause a gap in the ongoing stream, while dropping new data just shortens it.

Usage

Use this heuristic when designing any audio or real-time data queue in the firmware. Always use `FixedQueue` with a compile-time capacity, never `std::vector` or unbounded `std::queue`. Check `push_back()` return value and log drops. Tune capacity based on the frame duration and the maximum acceptable latency.

The Insight (Rule of Thumb)

  • Action: Use `FixedQueue<T, Capacity>` with compile-time capacity for all audio queues.
  • Value: Encode/playback queues: capacity 2. Decode queue: 1200ms / frame_duration. Send queue: 2400ms / frame_duration.
  • Trade-off: Drop-newest loses the most recent audio when congested, but preserves continuity of older audio and prevents OOM. A ~20-30% drop rate under load is preferable to unbounded growth.
  • Compatibility: `FixedQueue` uses `std::optional<T>` internally with no heap allocation after construction. Works on all targets including those without PSRAM.

Reasoning

On an ESP32 with limited RAM (and PSRAM on some boards), the audio pipeline processes 16kHz 16-bit mono PCM. Each 60ms Opus frame is ~60-80 bytes compressed, but the PCM is 1920 samples × 2 bytes = 3840 bytes. An unbounded queue would exhaust memory in seconds if the network or encoder stalls. The `FixedQueue` provides O(1) push/pop with zero allocation. The drop-newest strategy means that if the encoder can't keep up, the input simply skips frames — the user hears silence or a brief dropout, not a crash.

The queue capacities are derived from the Opus frame duration constant `OPUS_FRAME_DURATION_MS = 60`, making them self-documenting and easy to adjust.

Code Evidence

FixedQueue implementation from `main/audio/fixed_queue.h:39-46`:

    bool push_back(T value) {
        if (full()) {
            return false;
        }
        storage_[(head_ + size_) % Capacity].emplace(std::move(value));
        ++size_;
        return true;
    }

Queue capacity constants from `main/audio/audio_service.h:40-46`:

#define OPUS_FRAME_DURATION_MS 60
#define MAX_ENCODE_TASKS_IN_QUEUE 2
#define MAX_PLAYBACK_TASKS_IN_QUEUE 2
#define MAX_DECODE_PACKETS_IN_QUEUE (1200 / OPUS_FRAME_DURATION_MS)
#define MAX_SEND_PACKETS_IN_QUEUE (2400 / OPUS_FRAME_DURATION_MS)
#define AUDIO_TESTING_MAX_DURATION_MS 10000
#define MAX_TIMESTAMPS_IN_QUEUE 3

Queue declarations from `main/audio/audio_service.h:188-193`:

    FixedQueue<std::unique_ptr<AudioStreamPacket>, kAudioTestingPacketCapacity> audio_decode_queue_;
    FixedQueue<std::unique_ptr<AudioStreamPacket>, MAX_SEND_PACKETS_IN_QUEUE> audio_send_queue_;
    FixedQueue<std::unique_ptr<AudioStreamPacket>, kAudioTestingPacketCapacity>
        audio_testing_queue_;
    FixedQueue<AudioTask, MAX_ENCODE_TASKS_IN_QUEUE> audio_encode_queue_;
    FixedQueue<AudioTask, MAX_PLAYBACK_TASKS_IN_QUEUE> audio_playback_queue_;

Drop tracking from `main/audio/audio_service.h:111`:

    uint32_t encode_drop_count = 0;

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment