ESPHome 2026.5.3
Loading...
Searching...
No Matches
speaker_media_player.cpp
Go to the documentation of this file.
2
3#ifdef USE_ESP32
4
5#include "esphome/core/log.h"
6
8#ifdef USE_OTA
10#endif
11
12namespace esphome::speaker {
13
14// Framework:
15// - Media player that can handle two streams: one for media and one for announcements
16// - Each stream has an individual speaker component for output
17// - Each stream is handled by an ``AudioPipeline`` object with two parts/tasks
18// - ``AudioReader`` handles reading from an HTTP source or from a PROGMEM flash set at compile time
19// - ``AudioDecoder`` handles decoding the audio file. All formats are limited to two channels and 16 bits per
20// sample.
21// Each format is enabled independently at compile time:
22// - FLAC
23// - MP3 (based on the libhelix decoder)
24// - Ogg Opus
25// - WAV
26// - Each task runs until it is done processing the file or it receives a stop command
27// - Inter-task communication uses a FreeRTOS Event Group
28// - The ``AudioPipeline`` sets up a ring buffer between the reader and decoder tasks. The decoder task outputs audio
29// directly to a speaker component.
30// - The pipelines internal state needs to be processed by regularly calling ``process_state``.
31// - Generic media player commands are received by the ``control`` function. The commands are added to the
32// ``media_control_command_queue_`` to be processed in the component's loop
33// - Local file play back is initiatied with ``play_file`` and adds it to the ``media_control_command_queue_``
34// - Starting a stream intializes the appropriate pipeline or stops it if it is already running
35// - Volume and mute commands are achieved by the ``mute``, ``unmute``, ``set_volume`` functions.
36// - Volume commands are ignored if the media control queue is full to avoid crashing with rapid volume
37// increases/decreases.
38// - These functions all send the appropriate information to the speakers to implement.
39// - Pausing is implemented in the decoder task and is also sent directly to the media speaker component to decrease
40// latency.
41// - The components main loop performs housekeeping:
42// - It reads the media control queue and processes it directly
43// - It determines the overall state of the media player by considering the state of each pipeline
44// - announcement playback takes highest priority
45// - Handles playlists and repeating by starting the appropriate file when a previous file is finished
46// - Logging only happens in the main loop task to reduce task stack memory usage.
47
48static const uint32_t MEDIA_CONTROLS_QUEUE_LENGTH = 20;
49
50static const UBaseType_t MEDIA_PIPELINE_TASK_PRIORITY = 1;
51static const UBaseType_t ANNOUNCEMENT_PIPELINE_TASK_PRIORITY = 1;
52
53static const char *const TAG = "speaker_media_player";
54
56#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
58#else
60#endif
61
62 this->media_control_command_queue_ = xQueueCreate(MEDIA_CONTROLS_QUEUE_LENGTH, sizeof(MediaCallCommand));
63
65
66 VolumeRestoreState volume_restore_state;
67 if (this->pref_.load(&volume_restore_state)) {
68 this->set_volume_(volume_restore_state.volume);
69 this->set_mute_state_(volume_restore_state.is_muted);
70 } else {
71 this->set_volume_(this->volume_initial_);
72 this->set_mute_state_(false);
73 }
74
75#ifdef USE_OTA_STATE_LISTENER
77#endif
78
80 make_unique<AudioPipeline>(this->announcement_speaker_, this->buffer_size_, this->task_stack_in_psram_, "ann",
81 ANNOUNCEMENT_PIPELINE_TASK_PRIORITY);
82
83 if (this->announcement_pipeline_ == nullptr) {
84 ESP_LOGE(TAG, "Failed to create announcement pipeline");
85 this->mark_failed();
86 }
87
88 if (!this->single_pipeline_()) {
89 this->media_pipeline_ = make_unique<AudioPipeline>(this->media_speaker_, this->buffer_size_,
90 this->task_stack_in_psram_, "med", MEDIA_PIPELINE_TASK_PRIORITY);
91
92 if (this->media_pipeline_ == nullptr) {
93 ESP_LOGE(TAG, "Failed to create media pipeline");
94 this->mark_failed();
95 }
96 }
97
98 ESP_LOGI(TAG, "Set up speaker media player");
99}
100
102 switch (pipeline_type) {
104 this->announcement_playlist_delay_ms_ = delay_ms;
105 break;
107 this->media_playlist_delay_ms_ = delay_ms;
108 break;
109 }
110}
111
113 this->media_pipeline_->stop();
114 this->unpause_media_remaining_ = 3;
115 this->set_interval("unpause_med", 50, [this]() {
117 this->cancel_interval("unpause_med");
118 this->media_pipeline_->set_pause_state(false);
119 this->is_paused_ = false;
120 } else if (--this->unpause_media_remaining_ == 0) {
121 this->cancel_interval("unpause_med");
122 }
123 });
124}
125
127 if (!this->is_ready()) {
128 return;
129 }
130
131 MediaCallCommand media_command;
132
133 if (xQueueReceive(this->media_control_command_queue_, &media_command, 0) == pdTRUE) {
134 bool enqueue = media_command.enqueue.has_value() && media_command.enqueue.value();
135
136 if (media_command.url.has_value() || media_command.file.has_value()) {
137#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
141 }
142#endif
143 PlaylistItem playlist_item;
144 if (media_command.url.has_value()) {
145 playlist_item.url = *media_command.url.value();
146 delete media_command.url.value();
147 }
148 if (media_command.file.has_value()) {
149 playlist_item.file = media_command.file;
150 }
151
152 if (this->single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value())) {
153 if (!enqueue) {
154 // Ensure the loaded next item doesn't start playing, clear the queue, start the file, and unpause
155 this->cancel_timeout("next_ann");
156 this->announcement_playlist_.clear();
157 if (media_command.file.has_value()) {
158 this->announcement_pipeline_->start_file(playlist_item.file.value());
159 } else if (media_command.url.has_value()) {
160 this->announcement_pipeline_->start_url(playlist_item.url.value());
161 }
162 this->announcement_pipeline_->set_pause_state(false);
163 }
164 this->announcement_playlist_.push_back(playlist_item);
165 } else {
166 if (!enqueue) {
167 // Ensure the loaded next item doesn't start playing, clear the queue, start the file, and unpause
168 this->cancel_timeout("next_media");
169 this->media_playlist_.clear();
170 if (this->is_paused_) {
171 // If paused, stop the media pipeline and unpause it after confirming its stopped. This avoids playing a
172 // short segment of the paused file before starting the new one.
174 } else {
175 // Not paused, just directly start the file
176 if (media_command.file.has_value()) {
177 this->media_pipeline_->start_file(playlist_item.file.value());
178 } else if (media_command.url.has_value()) {
179 this->media_pipeline_->start_url(playlist_item.url.value());
180 }
181 this->media_pipeline_->set_pause_state(false);
182 this->is_paused_ = false;
183 }
184 }
185 this->media_playlist_.push_back(playlist_item);
186 }
187
188 return; // Don't process the new file play command further
189 }
190
191 if (media_command.volume.has_value()) {
192 this->set_volume_(media_command.volume.value());
193 this->publish_state();
194 }
195
196 if (media_command.command.has_value()) {
197 switch (media_command.command.value()) {
199#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
203 }
204#endif
205 if ((this->media_pipeline_ != nullptr) && (this->is_paused_)) {
206 this->media_pipeline_->set_pause_state(false);
207 }
208 this->is_paused_ = false;
209 break;
211 if ((this->media_pipeline_ != nullptr) && (!this->is_paused_)) {
212 this->media_pipeline_->set_pause_state(true);
213 }
214 this->is_paused_ = true;
215 break;
216#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
220 this->publish_state();
221 }
222 break;
224 this->is_turn_off_ = true;
225 // Intentional Fall-through
226#endif
228 // Pipelines do not stop immediately after calling the stop command, so confirm its stopped before unpausing.
229 // This avoids an audible short segment playing after receiving the stop command in a paused state.
230#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
231 if (this->single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value()) ||
232 (this->is_turn_off_ && this->announcement_pipeline_state_ != AudioPipelineState::STOPPED)) {
233#else
234 if (this->single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value())) {
235#endif
236 if (this->announcement_pipeline_ != nullptr) {
237 this->cancel_timeout("next_ann");
238 this->announcement_playlist_.clear();
239 this->announcement_pipeline_->stop();
241 this->set_interval("unpause_ann", 50, [this]() {
243 this->cancel_interval("unpause_ann");
244 this->announcement_pipeline_->set_pause_state(false);
245 } else if (--this->unpause_announcement_remaining_ == 0) {
246 this->cancel_interval("unpause_ann");
247 }
248 });
249 }
250 } else {
251 if (this->media_pipeline_ != nullptr) {
252 this->cancel_timeout("next_media");
253 this->media_playlist_.clear();
255 }
256 }
257
258 break;
260 if (this->media_pipeline_ != nullptr) {
261 if (this->is_paused_) {
262 this->media_pipeline_->set_pause_state(false);
263 this->is_paused_ = false;
264 } else {
265 this->media_pipeline_->set_pause_state(true);
266 this->is_paused_ = true;
267 }
268 }
269 break;
271 this->set_mute_state_(true);
272
273 this->publish_state();
274 break;
275 }
277 this->set_mute_state_(false);
278 this->publish_state();
279 break;
281 this->set_volume_(std::min(1.0f, this->volume + this->volume_increment_));
282 this->publish_state();
283 break;
285 this->set_volume_(std::max(0.0f, this->volume - this->volume_increment_));
286 this->publish_state();
287 break;
289 if (this->single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value())) {
290 this->announcement_repeat_one_ = true;
291 } else {
292 this->media_repeat_one_ = true;
293 }
294 break;
296 if (this->single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value())) {
297 this->announcement_repeat_one_ = false;
298 } else {
299 this->media_repeat_one_ = false;
300 }
301 break;
303 if (this->single_pipeline_() || (media_command.announce.has_value() && media_command.announce.value())) {
304 if (this->announcement_playlist_.empty()) {
305 this->announcement_playlist_.resize(1);
306 }
307 } else {
308 if (this->media_playlist_.empty()) {
309 this->media_playlist_.resize(1);
310 }
311 }
312 break;
313 default:
314 break;
315 }
316 }
317 }
318}
319
320#ifdef USE_OTA_STATE_LISTENER
321void SpeakerMediaPlayer::on_ota_global_state(ota::OTAState state, float progress, uint8_t error,
322 ota::OTAComponent *comp) {
323 if (state == ota::OTA_STARTED) {
324 if (this->media_pipeline_ != nullptr) {
325 this->media_pipeline_->suspend_tasks();
326 }
327 if (this->announcement_pipeline_ != nullptr) {
328 this->announcement_pipeline_->suspend_tasks();
329 }
330 } else if (state == ota::OTA_ERROR) {
331 if (this->media_pipeline_ != nullptr) {
332 this->media_pipeline_->resume_tasks();
333 }
334 if (this->announcement_pipeline_ != nullptr) {
335 this->announcement_pipeline_->resume_tasks();
336 }
337 }
338}
339#endif
340
342 this->watch_media_commands_();
343
344 // Determine state of the media player
345 media_player::MediaPlayerState old_state = this->state;
346
347 AudioPipelineState old_media_pipeline_state = this->media_pipeline_state_;
348 if (this->media_pipeline_ != nullptr) {
349 this->media_pipeline_state_ = this->media_pipeline_->process_state();
350 }
351
353 ESP_LOGE(TAG, "The media pipeline's file reader encountered an error.");
355 ESP_LOGE(TAG, "The media pipeline's audio decoder encountered an error.");
356 }
357
358 AudioPipelineState old_announcement_pipeline_state = this->announcement_pipeline_state_;
359 if (this->announcement_pipeline_ != nullptr) {
360 this->announcement_pipeline_state_ = this->announcement_pipeline_->process_state();
361 }
362
364 ESP_LOGE(TAG, "The announcement pipeline's file reader encountered an error.");
366 ESP_LOGE(TAG, "The announcement pipeline's audio decoder encountered an error.");
367 }
368
371 } else {
372 if (!this->announcement_playlist_.empty()) {
373 uint32_t timeout_ms = 0;
374 if (old_announcement_pipeline_state == AudioPipelineState::PLAYING) {
375 // Finished the current announcement file
376 if (!this->announcement_repeat_one_) {
377 // Pop item off the playlist if repeat is disabled
378 this->announcement_playlist_.pop_front();
379 }
380 // Only delay starting playback if moving on the next playlist item or repeating the current item
381 timeout_ms = this->announcement_playlist_delay_ms_;
382 }
383
384 if (!this->announcement_playlist_.empty()) {
385 // Start the next announcement file
386 PlaylistItem playlist_item = this->announcement_playlist_.front();
387 if (playlist_item.url.has_value()) {
388 this->announcement_pipeline_->start_url(playlist_item.url.value());
389 } else if (playlist_item.file.has_value()) {
390 this->announcement_pipeline_->start_file(playlist_item.file.value());
391 }
392
393 if (timeout_ms > 0) {
394 // Pause pipeline internally to facilitate the delay between items
395 this->announcement_pipeline_->set_pause_state(true);
396 // Internally unpause the pipeline after the delay between playlist items. Announcements do not follow the
397 // media player's pause state.
398 this->set_timeout("next_ann", timeout_ms, [this]() { this->announcement_pipeline_->set_pause_state(false); });
399 }
400 }
401 } else {
402 if (this->is_paused_) {
403#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
404 if (this->state != media_player::MEDIA_PLAYER_STATE_OFF) {
406 }
407#else
409#endif
413 if (!media_playlist_.empty()) {
414 uint32_t timeout_ms = 0;
415 if (old_media_pipeline_state == AudioPipelineState::PLAYING) {
416 // Finished the current media file
417 if (!this->media_repeat_one_) {
418 // Pop item off the playlist if repeat is disabled
419 this->media_playlist_.pop_front();
420 }
421 // Only delay starting playback if moving on the next playlist item or repeating the current item
422 timeout_ms = this->media_playlist_delay_ms_;
423 }
424 if (!this->media_playlist_.empty()) {
425 PlaylistItem playlist_item = this->media_playlist_.front();
426 if (playlist_item.url.has_value()) {
427 this->media_pipeline_->start_url(playlist_item.url.value());
428 } else if (playlist_item.file.has_value()) {
429 this->media_pipeline_->start_file(playlist_item.file.value());
430 }
431
432 if (timeout_ms > 0) {
433 // Pause pipeline internally to facilitate the delay between items
434 this->media_pipeline_->set_pause_state(true);
435 // Internally unpause the pipeline after the delay between playlist items, if the media player state is
436 // not paused.
437 this->set_timeout("next_media", timeout_ms,
438 [this]() { this->media_pipeline_->set_pause_state(this->is_paused_); });
439 }
440 }
441 } else {
442#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
443 if (this->state != media_player::MEDIA_PLAYER_STATE_OFF) {
445 }
446#else
448#endif
449 }
450 }
451 }
452 }
453
454 if (this->state != old_state) {
455 this->publish_state();
456 ESP_LOGD(TAG, "State changed to %s", media_player::media_player_state_to_string(this->state));
457 }
458#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
459 if (this->is_turn_off_ && (this->state == media_player::MEDIA_PLAYER_STATE_PAUSED ||
461 this->is_turn_off_ = false;
462 if (this->state == media_player::MEDIA_PLAYER_STATE_PAUSED) {
464 this->publish_state();
465 ESP_LOGD(TAG, "State changed to %s", media_player::media_player_state_to_string(this->state));
466 }
468 this->publish_state();
469 ESP_LOGD(TAG, "State changed to %s", media_player::media_player_state_to_string(this->state));
470 }
471#endif
472}
473
474void SpeakerMediaPlayer::play_file(audio::AudioFile *media_file, bool announcement, bool enqueue) {
475 if (!this->is_ready()) {
476 // Ignore any commands sent before the media player is setup
477 return;
478 }
479
480 MediaCallCommand media_command;
481
482 media_command.file = media_file;
483 if (this->single_pipeline_() || announcement) {
484 media_command.announce = true;
485 } else {
486 media_command.announce = false;
487 }
488 media_command.enqueue = enqueue;
489 xQueueSend(this->media_control_command_queue_, &media_command, portMAX_DELAY);
490}
491
492void SpeakerMediaPlayer::control(const media_player::MediaPlayerCall &call) {
493 if (!this->is_ready()) {
494 // Ignore any commands sent before the media player is setup
495 return;
496 }
497
498 MediaCallCommand media_command;
499
500 auto ann = call.get_announcement();
501 if (this->single_pipeline_() || (ann.has_value() && *ann)) {
502 media_command.announce = true;
503 } else {
504 media_command.announce = false;
505 }
506
507 const auto &media_url = call.get_media_url();
508 if (media_url.has_value()) {
509 media_command.url =
510 new std::string(*media_url); // Must be manually deleted after receiving media_command from a queue
511
512 auto cmd = call.get_command();
513 if (cmd.has_value()) {
515 media_command.enqueue = true;
516 }
517 }
518
519 xQueueSend(this->media_control_command_queue_, &media_command, portMAX_DELAY);
520 return;
521 }
522
523 auto vol = call.get_volume();
524 if (vol.has_value()) {
525 media_command.volume = vol;
526 // Wait 0 ticks for queue to be free, volume sets aren't that important!
527 xQueueSend(this->media_control_command_queue_, &media_command, 0);
528 return;
529 }
530
531 auto cmd = call.get_command();
532 if (cmd.has_value()) {
533 media_command.command = cmd;
534 TickType_t ticks_to_wait = portMAX_DELAY;
537 ticks_to_wait = 0; // Wait 0 ticks for queue to be free, volume sets aren't that important!
538 }
539 xQueueSend(this->media_control_command_queue_, &media_command, ticks_to_wait);
540 return;
541 }
542}
543
544media_player::MediaPlayerTraits SpeakerMediaPlayer::get_traits() {
545 auto traits = media_player::MediaPlayerTraits();
546 if (!this->single_pipeline_()) {
547 traits.set_supports_pause(true);
548 }
549#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
550 traits.set_supports_turn_off_on(true);
551#endif
552
553 if (this->announcement_format_.has_value()) {
554 traits.get_supported_formats().push_back(this->announcement_format_.value());
555 }
556 if (this->media_format_.has_value()) {
557 traits.get_supported_formats().push_back(this->media_format_.value());
558 } else if (this->single_pipeline_() && this->announcement_format_.has_value()) {
559 // Only one pipeline is defined, so use the announcement format (if configured) for the default purpose
560 media_player::MediaPlayerSupportedFormat media_format = this->announcement_format_.value();
562 traits.get_supported_formats().push_back(media_format);
563 }
564
565 return traits;
566};
567
569 VolumeRestoreState volume_restore_state;
570 volume_restore_state.volume = this->volume;
571 volume_restore_state.is_muted = this->is_muted_;
572 this->pref_.save(&volume_restore_state);
573}
574
575void SpeakerMediaPlayer::set_mute_state_(bool mute_state) {
576 if (this->media_speaker_ != nullptr) {
577 this->media_speaker_->set_mute_state(mute_state);
578 }
579 if (this->announcement_speaker_ != nullptr) {
580 this->announcement_speaker_->set_mute_state(mute_state);
581 }
582
583 bool old_mute_state = this->is_muted_;
584 this->is_muted_ = mute_state;
585
587
588 if (old_mute_state != mute_state) {
589 if (mute_state) {
590 this->defer([this]() { this->mute_trigger_.trigger(); });
591 } else {
592 this->defer([this]() { this->unmute_trigger_.trigger(); });
593 }
594 }
595}
596
597void SpeakerMediaPlayer::set_volume_(float volume, bool publish) {
598 // Remap the volume to fit with in the configured limits
599 float bounded_volume = remap<float, float>(volume, 0.0f, 1.0f, this->volume_min_, this->volume_max_);
600
601 if (this->media_speaker_ != nullptr) {
602 this->media_speaker_->set_volume(bounded_volume);
603 }
604
605 if (this->announcement_speaker_ != nullptr) {
606 this->announcement_speaker_->set_volume(bounded_volume);
607 }
608
609 if (publish) {
610 this->volume = volume;
612 }
613
614 // Turn on the mute state if the volume is effectively zero, off otherwise
615 if (volume < 0.001) {
616 this->set_mute_state_(true);
617 } else {
618 this->set_mute_state_(false);
619 }
620
621 this->defer([this, volume]() { this->volume_trigger_.trigger(volume); });
622}
623
624} // namespace esphome::speaker
625
626#endif
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition component.h:560
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:510
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_interval(const std voi set_interval)(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
Definition component.h:417
bool is_ready() const
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_timeout(const std boo cancel_timeout)(const char *name)
Cancel a timeout function.
Definition component.h:532
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_interval(const std boo cancel_interval)(const char *name)
Cancel an interval function.
Definition component.h:439
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:482
void add_global_state_listener(OTAGlobalStateListener *listener)
virtual void set_volume(float volume)
Definition speaker.h:70
virtual void set_mute_state(bool mute_state)
Definition speaker.h:80
optional< media_player::MediaPlayerSupportedFormat > announcement_format_
void save_volume_restore_state_()
Saves the current volume and mute state to the flash for restoration.
std::deque< PlaylistItem > announcement_playlist_
void set_volume_(float volume, bool publish=true)
Updates this->volume and saves volume/mute state to flash for restortation if publish is true.
void play_file(audio::AudioFile *media_file, bool announcement, bool enqueue)
void set_mute_state_(bool mute_state)
Sets the mute state.
std::deque< PlaylistItem > media_playlist_
std::unique_ptr< AudioPipeline > media_pipeline_
void stop_and_unpause_media_()
Stops the media pipeline and polls until stopped to unpause it, avoiding an audible glitch.
optional< media_player::MediaPlayerSupportedFormat > media_format_
void control(const media_player::MediaPlayerCall &call) override
void set_playlist_delay_ms(AudioPipelineType pipeline_type, uint32_t delay_ms)
void on_ota_global_state(ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) override
std::unique_ptr< AudioPipeline > announcement_pipeline_
media_player::MediaPlayerTraits get_traits() override
bool single_pipeline_()
Returns true if the media player has only the announcement pipeline defined, false if both the announ...
const char * media_player_state_to_string(MediaPlayerState state)
OTAGlobalCallback * get_global_ota_callback()
const char *const TAG
Definition spi.cpp:7
T remap(U value, U min, U max, T min_out, T max_out)
Remap value from the range (min, max) to (min_out, max_out).
Definition helpers.h:765
static void uint32_t