ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
audio_http_media_source.h
Go to the documentation of this file.
1#pragma once
2
4
5#ifdef USE_ESP32
6
10
11#include <micro_decoder/decoder_source.h>
12#include <micro_decoder/types.h>
13
14#include <atomic>
15#include <memory>
16#include <string>
17
18namespace esphome::audio_http {
19
20// Inherits from two unrelated listener-style interfaces:
21// - media_source::MediaSource: this source reports state and writes audio *to* an orchestrator
22// (the orchestrator calls set_listener() on us with a MediaSourceListener*).
23// - micro_decoder::DecoderListener: the underlying decoder calls back *into* us with decoded
24// audio and state changes (we call decoder_->set_listener(this) in setup()).
25// The two set_listener() methods live on different base classes and serve opposite directions.
26class AudioHTTPMediaSource : public Component, public media_source::MediaSource, public micro_decoder::DecoderListener {
27 public:
28 void setup() override;
29 void loop() override;
30 void dump_config() override;
31
32 void set_buffer_size(size_t buffer_size) { this->buffer_size_ = buffer_size; }
33 void set_task_stack_in_psram(bool task_stack_in_psram) { this->decoder_task_stack_in_psram_ = task_stack_in_psram; }
34
35 // MediaSource interface implementation
36 bool play_uri(const std::string &uri) override;
38 bool can_handle(const std::string &uri) const override;
39
40 // DecoderListener interface implementation
41 size_t on_audio_write(const uint8_t *data, size_t length, uint32_t timeout_ms) override;
42 void on_stream_info(const micro_decoder::AudioStreamInfo &info) override;
43 void on_state_change(micro_decoder::DecoderState state) override;
44
45 protected:
46 std::unique_ptr<micro_decoder::DecoderSource> decoder_;
48
49 size_t buffer_size_{50000};
50
51 // Written from the main loop in handle_command(), read from the decoder task in
52 // on_audio_write(). Must be atomic to avoid a data race.
53 std::atomic<bool> pause_{false};
55};
56
57} // namespace esphome::audio_http
58
59#endif // USE_ESP32
void set_task_stack_in_psram(bool task_stack_in_psram)
size_t on_audio_write(const uint8_t *data, size_t length, uint32_t timeout_ms) override
bool can_handle(const std::string &uri) const override
bool play_uri(const std::string &uri) override
std::unique_ptr< micro_decoder::DecoderSource > decoder_
void handle_command(media_source::MediaSourceCommand command) override
void on_state_change(micro_decoder::DecoderState state) override
void on_stream_info(const micro_decoder::AudioStreamInfo &info) override
Abstract base class for media sources MediaSource provides audio data to an orchestrator via the Medi...
bool state
Definition fan.h:2
MediaSourceCommand
Commands that are sent from the orchestrator to a media source.
static void uint32_t
uint16_t length
Definition tt21100.cpp:0