ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
router_speaker.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
8
9#include <freertos/FreeRTOS.h>
10
11#include <atomic>
12
13namespace esphome::router {
14
15class Router final : public Component, public speaker::Speaker {
16 public:
17 void setup() override;
18 void loop() override;
19 void dump_config() override;
20
21 size_t play(const uint8_t *data, size_t length) override { return this->play(data, length, 0); }
22 size_t play(const uint8_t *data, size_t length, TickType_t ticks_to_wait) override;
23
24 void start() override;
25 void stop() override;
26 void finish() override;
27
28 bool has_buffered_data() const override;
29
30 void set_pause_state(bool pause_state) override;
31 bool get_pause_state() const override { return this->cached_pause_; }
32
33 void set_volume(float volume) override;
34 float get_volume() override { return this->volume_; }
35
36 void set_mute_state(bool mute_state) override;
37 bool get_mute_state() override { return this->mute_state_; }
38
39 // Allocate the output list to its final size. Must be called before add_output().
40 void set_output_count(size_t count) { this->outputs_.init(count); }
41 void add_output(speaker::Speaker *spk) { this->outputs_.push_back(spk); }
42
46
47 // Always valid: active_output_idx_ stays within [0, outputs_.size()) and at least
48 // two outputs are required (validated in Python), so this never returns null.
50 return this->outputs_[this->active_output_idx_.load(std::memory_order_relaxed)];
51 }
52
53 protected:
54 // Frames written to the active output but not yet played: incremented in play() and decremented
55 // (clamped at zero) by the active output's audio_output_callback. Mirrors mixer_speaker's
56 // frames_in_pipeline_.
57 std::atomic<uint32_t> frames_in_pipeline_{0};
58
59 // Set when entering STATE_STARTING; used to time out a start the output never acts on
61
62 bool cached_pause_{false};
63
65
66 // Index of the previously-active output we're waiting on to fully stop before
67 // starting the new one. -1 means no pending start. Set by switch_to_output()
68 // when switching mid-playback; cleared by loop() once the old output reports
69 // is_stopped(). Required because shared-bus drivers (e.g. two i2s_audio
70 // speakers on one i2s_bus) reject start() until the previous user releases.
71 std::atomic<int8_t> pending_start_prev_idx_{-1};
72
73 private:
75 // Index into outputs_, always within [0, outputs_.size()). Defaults to the first
76 // configured output; updated by switch_to_output().
77 std::atomic<int8_t> active_output_idx_{0};
78};
79
80} // namespace esphome::router
81
82#endif // USE_ESP32
Fixed-capacity vector - sized once through init() or try_init(); push_back never reallocates This avo...
Definition helpers.h:669
void set_mute_state(bool mute_state) override
size_t play(const uint8_t *data, size_t length) override
void set_output_count(size_t count)
speaker::Speaker * get_active_output() const
bool get_mute_state() override
bool get_pause_state() const override
void set_pause_state(bool pause_state) override
void set_volume(float volume) override
void add_output(speaker::Speaker *spk)
bool has_buffered_data() const override
std::atomic< uint32_t > frames_in_pipeline_
bool switch_to_output(speaker::Speaker *target)
Switch the active output to the given speaker.
float get_volume() override
std::atomic< int8_t > pending_start_prev_idx_
unsigned count
Definition gif.cpp:51
static void uint32_t