ESPHome 2026.3.0
Loading...
Searching...
No Matches
speaker_source_media_player.cpp
Go to the documentation of this file.
2
3#ifdef USE_ESP32
4
6#include "esphome/core/log.h"
7
8#include <algorithm>
9
11
12static constexpr uint32_t MEDIA_CONTROLS_QUEUE_LENGTH = 20;
13
14static const char *const TAG = "speaker_source_media_player";
15
16// SourceBinding method implementations (defined here because SpeakerSourceMediaPlayer is forward-declared in the
17// header)
18
19// THREAD CONTEXT: Called from media source decode task thread
20size_t SourceBinding::write_audio(const uint8_t *data, size_t length, uint32_t timeout_ms,
22 return this->player->handle_media_output_(this->pipeline, this->source, data, length, timeout_ms, stream_info);
23}
24
25// THREAD CONTEXT: Called from main loop (media source's loop() calls set_state_ which calls report_state)
29
30// THREAD CONTEXT: Called from media source task thread; uses defer() to marshal to main loop
32 this->player->defer([this, volume]() { this->player->handle_volume_request_(volume); });
33}
34
35// THREAD CONTEXT: Called from media source task thread; uses defer() to marshal to main loop
36void SourceBinding::request_mute(bool is_muted) {
37 this->player->defer([this, is_muted]() { this->player->handle_mute_request_(is_muted); });
38}
39
40// THREAD CONTEXT: Called from media source task thread; uses defer() to marshal to main loop
41void SourceBinding::request_play_uri(const std::string &uri) {
42 this->player->defer([this, uri]() { this->player->handle_play_uri_request_(this->pipeline, uri); });
43}
44
45// THREAD CONTEXT: Called during code generation setup (main loop)
47 auto &binding =
48 this->pipelines_[pipeline].sources.emplace_back(std::make_unique<SourceBinding>(this, media_source, pipeline));
49 media_source->set_listener(binding.get());
50}
51
53 ESP_LOGCONFIG(TAG,
54 "Speaker Source Media Player:\n"
55 " Volume Increment: %.2f\n"
56 " Volume Min: %.2f\n"
57 " Volume Max: %.2f",
58 this->volume_increment_, this->volume_min_, this->volume_max_);
59}
60
63
64 this->media_control_command_queue_ = xQueueCreate(MEDIA_CONTROLS_QUEUE_LENGTH, sizeof(MediaPlayerControlCommand));
65
67
68 VolumeRestoreState volume_restore_state;
69 if (this->pref_.load(&volume_restore_state)) {
70 this->set_volume_(volume_restore_state.volume);
71 this->set_mute_state_(volume_restore_state.is_muted);
72 } else {
73 this->set_volume_(this->volume_initial_);
74 this->set_mute_state_(false);
75 }
76
77 // Register callbacks to receive playback notifications from speakers
78 for (size_t i = 0; i < this->pipelines_.size(); i++) {
79 if (this->pipelines_[i].is_configured()) {
80 this->pipelines_[i].speaker->add_audio_output_callback([this, i](uint32_t frames, int64_t timestamp) {
82 });
83 }
84 }
85}
86
87// THREAD CONTEXT: Called from the speaker's playback callback task (not main loop)
89 PipelineContext &ps = this->pipelines_[pipeline];
90
91 // Load once so the null check and use below are consistent
92 media_source::MediaSource *active_source = ps.active_source.load(std::memory_order_relaxed);
93 if (active_source == nullptr) {
94 return;
95 }
96
97 // CAS loop to safely subtract frames without underflow. If pending_frames is reset to 0 (new source
98 // starting) between the load and the subtract, compare_exchange_weak will fail and reload the current value.
99 uint32_t current = ps.pending_frames.load(std::memory_order_relaxed);
100 uint32_t source_frames;
101 do {
102 source_frames = std::min(frames, current);
103 } while (source_frames > 0 &&
104 !ps.pending_frames.compare_exchange_weak(current, current - source_frames, std::memory_order_relaxed));
105
106 if (source_frames > 0) {
107 // Notify the source about the played audio
108 active_source->notify_audio_played(source_frames, timestamp);
109 }
110}
111
112// THREAD CONTEXT: Called from main loop via defer()
114 // Update the media player's volume
115 this->set_volume_(volume);
116 this->publish_state();
117}
118
119// THREAD CONTEXT: Called from main loop via defer()
121 // Update the media player's mute state
122 this->set_mute_state_(is_muted);
123 this->publish_state();
124}
125
126// THREAD CONTEXT: Called from main loop via defer()
127void SpeakerSourceMediaPlayer::handle_play_uri_request_(uint8_t pipeline, const std::string &uri) {
128 // Smart source is requesting the player to play a different URI
129 auto call = this->make_call();
130 call.set_media_url(uri);
131 call.set_announcement(pipeline == ANNOUNCEMENT_PIPELINE);
132 call.perform();
133}
134
135// THREAD CONTEXT: Called from main loop (media source's loop() calls set_state_ which calls report_state)
138 PipelineContext &ps = this->pipelines_[pipeline];
139
141 // Track whether this IDLE was from an orchestrator-initiated stop (e.g., NEXT/PREV/PLAY_URI)
142 // so we can suppress spurious PLAYLIST_ADVANCE below
143 bool was_stopping = (ps.stopping_source == source);
144
145 // Source went idle - clear stopping flag if this was the source we asked to stop
146 if (was_stopping) {
147 ps.stopping_source = nullptr;
148 }
149
150 // Clear pending flag if this was the source we asked to play
151 if (ps.pending_source == source) {
152 ps.pending_source = nullptr;
153 }
154
155 // Source went idle - clear it if it's the active source
156 if (ps.active_source == source) {
158 ps.active_source = nullptr;
159
160 // Finish the speaker to ensure it's ready for the next playback
161 ps.speaker->finish();
162
163 // Only advance the playlist if the track finished naturally (not stopped by the orchestrator)
164 if (!was_stopping) {
166 }
167 }
169 // Source started playing - make it the active source if no one else is active
170 if (ps.active_source == nullptr) {
172 ps.last_source = nullptr;
173
174 // Clear pending flag now that the source is active
175 if (ps.pending_source == source) {
176 ps.pending_source = nullptr;
177 }
178 }
179 }
180}
181
182// THREAD CONTEXT: Called from media source decode task thread (not main loop).
183// Reads ps.active_source (atomic), writes ps.pending_frames (atomic), and calls
184// ps.speaker methods (speaker pointer is immutable after setup).
186 const uint8_t *data, size_t length, uint32_t timeout_ms,
188 PipelineContext &ps = this->pipelines_[pipeline];
189
190 // Single read; the if-body only uses ps.speaker (immutable after setup) and the source parameter.
191 if (ps.active_source == source) {
192 // This source is active - play the audio
194 // Setup the speaker to play this stream
196 vTaskDelay(pdMS_TO_TICKS(timeout_ms));
197 return 0;
198 }
199 size_t bytes_written = ps.speaker->play(data, length, pdMS_TO_TICKS(timeout_ms));
200 if (bytes_written > 0) {
201 // Track frames sent to speaker for this source
202 ps.pending_frames.fetch_add(stream_info.bytes_to_frames(bytes_written), std::memory_order_relaxed);
203 }
204 return bytes_written;
205 }
206
207 // Not the active source - wait for state callback to set us as active when we transition to PLAYING
208 vTaskDelay(pdMS_TO_TICKS(timeout_ms));
209 return 0;
210}
211
212// THREAD CONTEXT: Called from main loop (loop)
214 media_source::MediaSource *source, bool playlist_active, media_player::MediaPlayerState old_state) const {
215 if (source != nullptr) {
216 switch (source->get_state()) {
222 ESP_LOGE(TAG, "Media source error");
225 default:
227 }
228 }
229
230 // No active source. Stay PLAYING during playlist transitions
231 if (playlist_active && old_state == media_player::MEDIA_PLAYER_STATE_PLAYING) {
233 }
235}
236
238 // Process queued control commands
240
241 // Update state based on active sources - announcement pipeline takes priority
242 media_player::MediaPlayerState old_state = this->state;
243
245 PipelineContext &media_ps = this->pipelines_[MEDIA_PIPELINE];
246
247 // Check playlist state to detect transitions between items
248 bool announcement_playlist_active = (ann_ps.playlist_index < ann_ps.playlist.size()) ||
249 (ann_ps.repeat_mode != REPEAT_OFF && !ann_ps.playlist.empty());
250 bool media_playlist_active = (media_ps.playlist_index < media_ps.playlist.size()) ||
251 (media_ps.repeat_mode != REPEAT_OFF && !media_ps.playlist.empty());
252
253 // Check announcement pipeline first
254 media_source::MediaSource *announcement_source = ann_ps.active_source;
255 if (announcement_source != nullptr) {
256 media_source::MediaSourceState announcement_state = announcement_source->get_state();
257 if (announcement_state != media_source::MediaSourceState::IDLE) {
258 // Announcement is active - announcements take priority and never report PAUSED
259 switch (announcement_state) {
261 case media_source::MediaSourceState::PAUSED: // Treat paused announcements as announcing
263 break;
265 ESP_LOGE(TAG, "Announcement source error");
266 // Fall through to media pipeline state
267 this->state = this->get_source_state_(media_ps.active_source, media_playlist_active, old_state);
268 break;
269 default:
270 break;
271 }
272 } else {
273 // Announcement source is idle, fall through to media pipeline
274 this->state = this->get_source_state_(media_ps.active_source, media_playlist_active, old_state);
275 }
276 } else if (announcement_playlist_active && old_state == media_player::MEDIA_PLAYER_STATE_ANNOUNCING) {
278 } else {
279 // No active announcement, check media pipeline
280 this->state = this->get_source_state_(media_ps.active_source, media_playlist_active, old_state);
281 }
282
283 if (this->state != old_state) {
284 this->publish_state();
285 ESP_LOGD(TAG, "State changed to %s", media_player::media_player_state_to_string(this->state));
286 }
287}
288
290 PipelineContext &ps = this->pipelines_[pipeline];
291 media_source::MediaSource *first_match = nullptr;
292 for (auto &binding : ps.sources) {
293 if (binding->source->can_handle(uri)) {
294 // Prefer an idle source; otherwise remember the first match (will be stopped by try_execute_play_uri_)
295 if (binding->source->get_state() == media_source::MediaSourceState::IDLE) {
296 return binding->source;
297 }
298 if (first_match == nullptr) {
299 first_match = binding->source;
300 }
301 }
302 }
303 return first_match;
304}
305
306bool SpeakerSourceMediaPlayer::try_execute_play_uri_(const std::string &uri, uint8_t pipeline) {
307 // Find target source
308 media_source::MediaSource *target_source = this->find_source_for_uri_(uri, pipeline);
309 if (target_source == nullptr) {
310 ESP_LOGW(TAG, "No source for URI");
311 ESP_LOGV(TAG, "URI: %s", uri.c_str());
312 return true; // Remove from queue (unrecoverable)
313 }
314
315 PipelineContext &ps = this->pipelines_[pipeline];
316
317 media_source::MediaSource *active_source = ps.active_source;
318
319 // If active source exists and is not IDLE, stop it and wait
320 if (active_source != nullptr) {
321 media_source::MediaSourceState active_state = active_source->get_state();
322 if (active_state != media_source::MediaSourceState::IDLE) {
323 // Only send END command once per source - check if we've already asked this source to stop
324 if (ps.stopping_source != active_source) {
325 ESP_LOGV(TAG, "Pipeline %u: stopping active source", pipeline);
326 ps.stopping_source = active_source;
328 ps.speaker->stop();
329 }
330 return false; // Leave in queue, retry next loop
331 }
332 }
333
334 // Also check target source directly - handles case where source errored before PLAYING state
335 media_source::MediaSourceState target_state = target_source->get_state();
336 if (target_state != media_source::MediaSourceState::IDLE) {
337 // Only send STOP command once per source
338 if (ps.stopping_source != target_source) {
339 ESP_LOGV(TAG, "Pipeline %u: target source busy, stopping", pipeline);
340 ps.stopping_source = target_source;
342 ps.speaker->stop();
343 }
344 return false; // Leave in queue, retry next loop
345 }
346
347 // Clear stopping flag since we're past the stopping phase
348 ps.stopping_source = nullptr;
349
350 // Check if speaker is ready
351 if (!ps.speaker->is_stopped()) {
352 return false; // Speaker not ready yet, retry later
353 }
354
355 // Set pending source so handle_media_state_changed_ can recognize it when the source transitions to PLAYING
356 ps.pending_source = target_source;
357
358 // Speaker is ready, try to play
359 if (!target_source->play_uri(uri)) {
360 ESP_LOGE(TAG, "Pipeline %u: Failed to play URI: %s", pipeline, uri.c_str());
361 ps.pending_source = nullptr;
363 }
364
365 // Reset pending frame counter for this pipeline since we're starting a new source
366 ps.pending_frames.store(0, std::memory_order_relaxed);
367
368 return true; // Remove from queue
369}
370
371// THREAD CONTEXT: Called from main loop (process_control_queue_, queue_play_current_, handle_media_state_changed_)
374 cmd.type = type;
375 cmd.pipeline = pipeline;
376 if (xQueueSend(this->media_control_command_queue_, &cmd, 0) != pdTRUE) {
377 ESP_LOGE(TAG, "Queue full, command dropped");
378 }
379}
380
381// THREAD CONTEXT: Called from main loop via automation commands (direct)
383 if (pipeline < this->pipelines_.size()) {
384 this->pipelines_[pipeline].playlist_delay_ms = delay_ms;
385 }
386}
387
388// THREAD CONTEXT: Called from main loop (process_control_queue_).
389// The timeout callback also runs on the main loop.
391 if (delay_ms > 0) {
392 this->set_timeout(PIPELINE_TIMEOUT_IDS[pipeline], delay_ms,
393 [this, pipeline]() { this->queue_command_(MediaPlayerControlCommand::PLAY_CURRENT, pipeline); });
394 } else {
396 }
397}
398
399// THREAD CONTEXT: Called from main loop (loop)
401 MediaPlayerControlCommand control_command{};
402
403 // Use peek to check command without removing it
404 if (xQueuePeek(this->media_control_command_queue_, &control_command, 0) != pdTRUE) {
405 return;
406 }
407
408 bool command_executed = false;
409 uint8_t pipeline = control_command.pipeline;
410
411 // Get pipeline state
412 PipelineContext &ps = this->pipelines_[pipeline];
413 media_source::MediaSource *active_source = ps.active_source;
414
415 switch (control_command.type) {
417 // Always use our local playlist to start playback
418 this->cancel_timeout(PIPELINE_TIMEOUT_IDS[pipeline]);
419 ps.playlist.clear();
420 ps.shuffle_indices.clear(); // Clear shuffle when starting fresh playlist
421 ps.playlist_index = 0; // Reset index
422 ps.playlist.push_back(*control_command.data.uri);
423
424 // Queue PLAY_CURRENT to initiate playback
426 command_executed = true;
427 break;
428 }
429
431 // Always add to our local playlist
432 ps.playlist.push_back(*control_command.data.uri);
433
434 // If shuffle is active, add the new item to the end of the shuffle order
435 if (!ps.shuffle_indices.empty()) {
436 ps.shuffle_indices.push_back(ps.playlist.size() - 1);
437 }
438
439 // If nothing is playing and no upcoming items are queued, start the new item.
440 bool nothing_playing =
441 (active_source == nullptr) || (active_source->get_state() == media_source::MediaSourceState::IDLE);
442 if (nothing_playing && ps.playlist_index >= ps.playlist.size() - 1) {
443 ps.playlist_index = ps.playlist.size() - 1; // Point to newly added item
445 }
446 command_executed = true;
447 break;
448 }
449
451 // Internal message: a track finished, advance to next
452 if (ps.repeat_mode != REPEAT_ONE) {
453 ps.playlist_index++;
454 }
455
456 // Check if we should continue playback
457 if (ps.playlist_index < ps.playlist.size()) {
458 this->queue_play_current_(pipeline, ps.playlist_delay_ms);
459 } else if (ps.repeat_mode == REPEAT_ALL && !ps.playlist.empty()) {
460 ps.playlist_index = 0;
461 this->queue_play_current_(pipeline, ps.playlist_delay_ms);
462 }
463 command_executed = true;
464 break;
465 }
466
468 // Play the item at current playlist index (mapped through shuffle if active)
469 if (ps.playlist_index < ps.playlist.size()) {
470 size_t actual_position = this->get_playlist_position_(pipeline);
471 command_executed = this->try_execute_play_uri_(ps.playlist[actual_position], pipeline);
472 } else {
473 command_executed = true; // Index out of bounds or empty playlist
474 }
475 break;
476 }
477
479 this->handle_player_command_(control_command.data.command, pipeline);
480 command_executed = true;
481 break;
482 }
483 }
484
485 // Only remove from queue if successfully executed
486 if (command_executed) {
487 xQueueReceive(this->media_control_command_queue_, &control_command, 0);
488
489 // Delete the allocated string for PLAY_URI and ENQUEUE_URI commands
490 if (control_command.type == MediaPlayerControlCommand::PLAY_URI ||
491 control_command.type == MediaPlayerControlCommand::ENQUEUE_URI) {
492 delete control_command.data.uri;
493 }
494 }
495}
496
497// THREAD CONTEXT: Called from main loop only (via process_control_queue_)
499 uint8_t pipeline) {
500 PipelineContext &ps = this->pipelines_[pipeline];
501 media_source::MediaSource *active_source = ps.active_source;
502 bool has_internal_playlist = (active_source != nullptr) && active_source->has_internal_playlist();
503
504 // Determine target source: prefer active, fall back to last
505 media_source::MediaSource *target_source = nullptr;
506 if (active_source != nullptr) {
507 target_source = active_source;
508 } else if (ps.last_source != nullptr) {
509 target_source = ps.last_source;
510 }
511
512 switch (player_command) {
514 // Convert TOGGLE to PLAY or PAUSE based on current state
515 if ((active_source != nullptr) && (active_source->get_state() == media_source::MediaSourceState::PLAYING)) {
516 if (target_source != nullptr) {
518 }
519 } else if (!has_internal_playlist && active_source == nullptr && !ps.playlist.empty()) {
520 bool last_has_internal_playlist = (ps.last_source != nullptr) && ps.last_source->has_internal_playlist();
521 if (last_has_internal_playlist) {
523 } else {
524 if (ps.playlist_index >= ps.playlist.size()) {
525 ps.playlist_index = 0;
526 }
528 }
529 } else {
530 if (target_source != nullptr) {
532 }
533 }
534 break;
535 }
536
538 if (!has_internal_playlist && active_source == nullptr && !ps.playlist.empty()) {
539 bool last_has_internal_playlist = (ps.last_source != nullptr) && ps.last_source->has_internal_playlist();
540 if (last_has_internal_playlist) {
542 } else {
543 if (ps.playlist_index >= ps.playlist.size()) {
544 ps.playlist_index = 0;
545 }
547 }
548 } else if (target_source != nullptr) {
550 }
551 break;
552 }
553
555 if (target_source != nullptr) {
557 }
558 break;
559 }
560
562 if (!has_internal_playlist) {
563 this->cancel_timeout(PIPELINE_TIMEOUT_IDS[pipeline]);
564 ps.playlist.clear();
565 ps.shuffle_indices.clear();
566 ps.playlist_index = 0;
567 }
568 if (target_source != nullptr) {
570 }
571 break;
572 }
573
575 if (!has_internal_playlist) {
576 this->cancel_timeout(PIPELINE_TIMEOUT_IDS[pipeline]);
577 if (ps.playlist_index + 1 < ps.playlist.size()) {
578 ps.playlist_index++;
580 } else if (ps.repeat_mode == REPEAT_ALL && !ps.playlist.empty()) {
581 ps.playlist_index = 0;
583 }
584 } else if (target_source != nullptr) {
586 }
587 break;
588 }
589
591 if (!has_internal_playlist) {
592 this->cancel_timeout(PIPELINE_TIMEOUT_IDS[pipeline]);
593 if (ps.playlist_index > 0) {
594 ps.playlist_index--;
596 } else if (ps.repeat_mode == REPEAT_ALL && !ps.playlist.empty()) {
597 ps.playlist_index = ps.playlist.size() - 1;
599 }
600 } else if (target_source != nullptr) {
602 }
603 break;
604 }
605
607 if (!has_internal_playlist) {
609 } else if (target_source != nullptr) {
611 }
612 break;
613
615 if (!has_internal_playlist) {
617 } else if (target_source != nullptr) {
619 }
620 break;
621
623 if (!has_internal_playlist) {
625 } else if (target_source != nullptr) {
627 }
628 break;
629
631 if (!has_internal_playlist) {
632 this->cancel_timeout(PIPELINE_TIMEOUT_IDS[pipeline]);
633 if (ps.playlist_index < ps.playlist.size()) {
634 size_t actual_position = this->get_playlist_position_(pipeline);
635 ps.playlist[0] = std::move(ps.playlist[actual_position]);
636 ps.playlist.resize(1);
637 ps.playlist_index = 0;
638 } else {
639 ps.playlist.clear();
640 ps.playlist_index = 0;
641 }
642 ps.shuffle_indices.clear();
643 } else if (target_source != nullptr) {
645 }
646 break;
647 }
648
650 if (!has_internal_playlist) {
651 this->shuffle_playlist_(pipeline);
652 } else if (target_source != nullptr) {
654 }
655 break;
656
658 if (!has_internal_playlist) {
659 this->unshuffle_playlist_(pipeline);
660 } else if (target_source != nullptr) {
662 }
663 break;
664
665 default:
666 // TURN_ON, TURN_OFF, ENQUEUE (handled separately with URL) are no-ops
667 break;
668 }
669}
670
671// THREAD CONTEXT: Called from main loop only. Entry points:
672// - HA/automation commands (direct)
673// - handle_play_uri_request_() via make_call().perform() (deferred from source tasks)
675 if (!this->is_ready()) {
676 return;
677 }
678
679 MediaPlayerControlCommand control_command{};
680
681 // Determine which pipeline to use based on announcement flag, falling back if the preferred pipeline
682 // is not configured
683 auto announcement = call.get_announcement();
684 if (announcement.has_value() && announcement.value()) {
685 if (this->pipelines_[ANNOUNCEMENT_PIPELINE].is_configured()) {
686 control_command.pipeline = ANNOUNCEMENT_PIPELINE;
687 } else {
688 control_command.pipeline = MEDIA_PIPELINE;
689 }
690 } else {
691 if (this->pipelines_[MEDIA_PIPELINE].is_configured()) {
692 control_command.pipeline = MEDIA_PIPELINE;
693 } else {
694 control_command.pipeline = ANNOUNCEMENT_PIPELINE;
695 }
696 }
697
698 auto media_url = call.get_media_url();
699 if (media_url.has_value()) {
700 auto command = call.get_command();
701 bool enqueue = command.has_value() && command.value() == media_player::MEDIA_PLAYER_COMMAND_ENQUEUE;
702
703 if (enqueue) {
704 control_command.type = MediaPlayerControlCommand::ENQUEUE_URI;
705 } else {
706 control_command.type = MediaPlayerControlCommand::PLAY_URI;
707 }
708 // Heap allocation is unavoidable: URIs from Home Assistant are arbitrary-length (media URLs with tokens
709 // can easily exceed 500 bytes). Deleted in process_control_queue_() after the command is consumed. FreeRTOS queues
710 // require items to be copyable, so we store a pointer to the string in the queue rather than the string itself.
711 control_command.data.uri = new std::string(media_url.value());
712 if (xQueueSend(this->media_control_command_queue_, &control_command, 0) != pdTRUE) {
713 delete control_command.data.uri;
714 ESP_LOGE(TAG, "Queue full, URI dropped");
715 }
716 return;
717 }
718
719 auto volume = call.get_volume();
720 if (volume.has_value()) {
721 this->set_volume_(volume.value());
722 this->publish_state();
723 return;
724 }
725
726 auto cmd = call.get_command();
727 if (cmd.has_value()) {
728 switch (cmd.value()) {
730 this->set_mute_state_(true);
731 break;
733 this->set_mute_state_(false);
734 break;
736 this->set_volume_(std::min(1.0f, this->volume + this->volume_increment_));
737 break;
739 this->set_volume_(std::max(0.0f, this->volume - this->volume_increment_));
740 break;
741 default:
742 // Queue command for processing in loop()
743 control_command.type = MediaPlayerControlCommand::SEND_COMMAND;
744 control_command.data.command = cmd.value();
745 if (xQueueSend(this->media_control_command_queue_, &control_command, 0) != pdTRUE) {
746 ESP_LOGE(TAG, "Queue full, command dropped");
747 }
748 return;
749 }
750 this->publish_state();
751 }
752}
753
755 // This media player supports more traits like playlists, repeat, and shuffle, but the ESPHome API currently (March
756 // 2026) doesn't support those commands, so we only report pause support for now since that's used by the frontend and
757 // supported by our player.
758 auto traits = media_player::MediaPlayerTraits();
759 traits.set_supports_pause(true);
760
761 for (const auto &ps : this->pipelines_) {
762 if (ps.format.has_value()) {
763 traits.get_supported_formats().push_back(ps.format.value());
764 }
765 }
766
767 return traits;
768}
769
771 VolumeRestoreState volume_restore_state;
772 volume_restore_state.volume = this->volume;
773 volume_restore_state.is_muted = this->is_muted_;
774 this->pref_.save(&volume_restore_state);
775}
776
777void SpeakerSourceMediaPlayer::set_mute_state_(bool mute_state, bool publish) {
778 if (this->is_muted_ == mute_state) {
779 return;
780 }
781
782 for (auto &ps : this->pipelines_) {
783 if (ps.is_configured()) {
784 ps.speaker->set_mute_state(mute_state);
785 }
786 }
787
788 this->is_muted_ = mute_state;
789
790 if (publish) {
792 }
793
794 // Notify all media sources about the mute state change
795 for (auto &ps : this->pipelines_) {
796 for (auto &binding : ps.sources) {
797 binding->source->notify_mute_changed(mute_state);
798 }
799 }
800
801 if (mute_state) {
802 this->defer([this]() { this->mute_trigger_.trigger(); });
803 } else {
804 this->defer([this]() { this->unmute_trigger_.trigger(); });
805 }
806}
807
808void SpeakerSourceMediaPlayer::set_volume_(float volume, bool publish) {
809 // Remap the volume to fit within the configured limits
810 float bounded_volume = remap<float, float>(volume, 0.0f, 1.0f, this->volume_min_, this->volume_max_);
811
812 for (auto &ps : this->pipelines_) {
813 if (ps.is_configured()) {
814 ps.speaker->set_volume(bounded_volume);
815 }
816 }
817
818 if (publish) {
819 this->volume = volume;
820 }
821
822 // Notify all media sources about the volume change
823 for (auto &ps : this->pipelines_) {
824 for (auto &binding : ps.sources) {
825 binding->source->notify_volume_changed(volume);
826 }
827 }
828
829 // Turn on the mute state if the volume is effectively zero, off otherwise.
830 // Pass publish=false to avoid saving twice.
831 if (volume < 0.001) {
832 this->set_mute_state_(true, false);
833 } else {
834 this->set_mute_state_(false, false);
835 }
836
837 // Save after mute mutation so the restored state has the correct is_muted_ value
838 if (publish) {
840 }
841
842 this->defer([this, volume]() { this->volume_trigger_.trigger(volume); });
843}
844
846 const PipelineContext &ps = this->pipelines_[pipeline];
847
848 if (ps.shuffle_indices.empty() || ps.playlist_index >= ps.shuffle_indices.size()) {
849 return ps.playlist_index;
850 }
851 return ps.shuffle_indices[ps.playlist_index];
852}
853
855 PipelineContext &ps = this->pipelines_[pipeline];
856
857 if (ps.playlist.size() <= 1) {
858 ps.shuffle_indices.clear();
859 return;
860 }
861
862 // Capture current actual position BEFORE modifying shuffle_indices
863 size_t current_actual = this->get_playlist_position_(pipeline);
864
865 // Build indices vector
866 ps.shuffle_indices.resize(ps.playlist.size());
867 for (size_t i = 0; i < ps.playlist.size(); i++) {
868 ps.shuffle_indices[i] = i;
869 }
870
871 // Fisher-Yates shuffle using ESPHome's random helper
872 for (size_t i = ps.shuffle_indices.size() - 1; i > 0; i--) {
873 size_t j = random_uint32() % (i + 1);
874 std::swap(ps.shuffle_indices[i], ps.shuffle_indices[j]);
875 }
876
877 // Move current track to current position (so playback continues seamlessly)
878 if (ps.playlist_index < ps.shuffle_indices.size()) {
879 for (size_t i = 0; i < ps.shuffle_indices.size(); i++) {
880 if (ps.shuffle_indices[i] == current_actual) {
881 std::swap(ps.shuffle_indices[i], ps.shuffle_indices[ps.playlist_index]);
882 break;
883 }
884 }
885 }
886}
887
889 PipelineContext &ps = this->pipelines_[pipeline];
890
891 if (!ps.shuffle_indices.empty() && ps.playlist_index < ps.shuffle_indices.size()) {
893 }
894 ps.shuffle_indices.clear();
895}
896
897} // namespace esphome::speaker_source
898
899#endif // USE_ESP32
media_source::MediaSource * source
audio::AudioStreamInfo stream_info
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:501
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:451
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:473
bool save(const T *src)
Definition preferences.h:21
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:325
Abstract base class for media sources MediaSource provides audio data to an orchestrator via the Medi...
virtual void notify_audio_played(uint32_t frames, int64_t timestamp)
Notify the source about audio that has been played Called when the speaker reports that audio frames ...
virtual bool has_internal_playlist() const
Whether this source manages its own playlist internally Smart sources that handle next/previous/repea...
void set_listener(MediaSourceListener *listener)
Set the listener that receives callbacks from this source.
virtual void handle_command(MediaSourceCommand command)=0
Handle playback commands; e.g., pause, stop, next, etc.
MediaSourceState get_state() const
Get current playback state.
virtual bool play_uri(const std::string &uri)=0
Start playing the given URI Sources should validate the URI and state, returning false if the source ...
virtual size_t play(const uint8_t *data, size_t length)=0
Plays the provided audio data.
void set_audio_stream_info(const audio::AudioStreamInfo &audio_stream_info)
Definition speaker.h:99
audio::AudioStreamInfo & get_audio_stream_info()
Definition speaker.h:103
virtual void finish()
Definition speaker.h:58
bool is_stopped() const
Definition speaker.h:67
virtual void stop()=0
void shuffle_playlist_(uint8_t pipeline)
Generates shuffled indices for the playlist, keeping current track at current position.
void set_playlist_delay_ms(uint8_t pipeline, uint32_t delay_ms)
void queue_play_current_(uint8_t pipeline, uint32_t delay_ms=0)
void set_mute_state_(bool mute_state, bool publish=true)
Sets the mute state.
void queue_command_(MediaPlayerControlCommand::Type type, uint8_t pipeline)
media_source::MediaSource * find_source_for_uri_(const std::string &uri, uint8_t pipeline)
void unshuffle_playlist_(uint8_t pipeline)
Clears shuffle indices and adjusts playlist_index to maintain current track.
media_player::MediaPlayerState get_source_state_(media_source::MediaSource *media_source, bool playlist_active, media_player::MediaPlayerState old_state) const
Determine media player state from a pipeline's active source.
void handle_play_uri_request_(uint8_t pipeline, const std::string &uri)
void control(const media_player::MediaPlayerCall &call) override
void handle_player_command_(media_player::MediaPlayerCommand player_command, uint8_t pipeline)
size_t get_playlist_position_(uint8_t pipeline) const
Maps playlist_index through shuffle indices if shuffle is active.
void add_media_source(uint8_t pipeline, media_source::MediaSource *media_source)
Adds a media source to a pipeline and registers this player as its listener.
bool try_execute_play_uri_(const std::string &uri, uint8_t pipeline)
void set_volume_(float volume, bool publish=true)
Updates this->volume and saves volume/mute state to flash for restoration if publish is true.
void handle_media_state_changed_(uint8_t pipeline, media_source::MediaSource *source, media_source::MediaSourceState state)
media_player::MediaPlayerTraits get_traits() override
void handle_speaker_playback_callback_(uint32_t frames, int64_t timestamp, uint8_t pipeline)
void save_volume_restore_state_()
Saves the current volume and mute state to the flash for restoration.
size_t handle_media_output_(uint8_t pipeline, media_source::MediaSource *source, const uint8_t *data, size_t length, uint32_t timeout_ms, const audio::AudioStreamInfo &stream_info)
uint16_t type
bool state
Definition fan.h:2
const char * media_player_state_to_string(MediaPlayerState state)
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:17
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:605
static void uint32_t
std::vector< std::unique_ptr< SourceBinding > > sources
std::atomic< media_source::MediaSource * > active_source
void request_play_uri(const std::string &uri) override
void report_state(media_source::MediaSourceState state) override
size_t write_audio(const uint8_t *data, size_t length, uint32_t timeout_ms, const audio::AudioStreamInfo &stream_info) override
uint16_t length
Definition tt21100.cpp:0
uint16_t timestamp
Definition tt21100.cpp:2