ESPHome 2026.11.0-dev
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 final : public Component,
28 public micro_decoder::DecoderListener {
29 public:
30 void setup() override;
31 void loop() override;
32 void dump_config() override;
33
34 void set_buffer_size(size_t buffer_size) { this->buffer_size_ = buffer_size; }
35 void set_task_stack_in_psram(bool task_stack_in_psram) { this->decoder_task_stack_in_psram_ = task_stack_in_psram; }
36 void set_persistent_ring_buffer(bool persistent) { this->persistent_ring_buffer_ = persistent; }
37#ifdef USE_AUDIO_HTTP_CA_CERTIFICATE
40 void set_http_ca_certificate(const char *ca_certificate) { this->http_ca_certificate_ = ca_certificate; }
41#endif
42
43 // MediaSource interface implementation
44 bool play_uri(const std::string &uri) override;
46 bool can_handle(const std::string &uri) const override;
47
48 // DecoderListener interface implementation
49 size_t on_audio_write(const uint8_t *data, size_t length, uint32_t timeout_ms) override;
50 void on_stream_info(const micro_decoder::AudioStreamInfo &info) override;
51 void on_state_change(micro_decoder::DecoderState state) override;
52
53 protected:
54 std::unique_ptr<micro_decoder::DecoderSource> decoder_;
56
57 size_t buffer_size_{50000};
58
59 // Written from the main loop in handle_command(), read from the decoder task in
60 // on_audio_write(). Must be atomic to avoid a data race.
61 std::atomic<bool> pause_{false};
64#ifdef USE_AUDIO_HTTP_CA_CERTIFICATE
65 const char *http_ca_certificate_{nullptr};
66#endif
67};
68
69} // namespace esphome::audio_http
70
71#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 set_http_ca_certificate(const char *ca_certificate)
PEM-encoded CA certificate(s) used as the sole trust anchor for HTTPS playback URLs,...
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