12static constexpr uint32_t MEDIA_CONTROLS_QUEUE_LENGTH = 20;
48 this->
pipelines_[pipeline].sources.emplace_back(std::make_unique<SourceBinding>(
this, media_source, pipeline));
54 "Speaker Source Media Player:\n"
55 " Volume Increment: %.2f\n"
72 if (this->
pref_.
load(&volume_restore_state)) {
81 for (
size_t i = 0; i < this->
pipelines_.size(); i++) {
96 if (active_source ==
nullptr) {
105 source_frames = std::min(frames, current);
106 }
while (source_frames > 0 &&
107 !ps.
pending_frames.compare_exchange_weak(current, current - source_frames, std::memory_order_relaxed));
109 if (source_frames > 0) {
133 call.set_media_url(uri);
199 vTaskDelay(pdMS_TO_TICKS(timeout_ms));
205 ps.
pending_frames.fetch_add(reserved_frames, std::memory_order_relaxed);
206 size_t bytes_written = ps.
speaker->
play(data,
length, pdMS_TO_TICKS(timeout_ms));
208 if (unused_frames > 0) {
212 while (!ps.
pending_frames.compare_exchange_weak(pending, pending - std::min(pending, unused_frames),
213 std::memory_order_relaxed)) {
216 return bytes_written;
220 vTaskDelay(pdMS_TO_TICKS(timeout_ms));
227 if (source !=
nullptr) {
234 ESP_LOGE(TAG,
"Media source error");
267 if (announcement_source !=
nullptr) {
271 switch (announcement_state) {
277 ESP_LOGE(TAG,
"Announcement source error");
302 if (this->
state != old_state) {
311 if (source !=
nullptr) {
321 for (
auto &binding : ps.
sources) {
322 if (binding->source->can_handle(uri)) {
325 return binding->source;
327 if (first_match ==
nullptr) {
328 first_match = binding->source;
338 if (target_source ==
nullptr) {
339 ESP_LOGW(TAG,
"No source for URI");
340 ESP_LOGV(TAG,
"URI: %s", uri.c_str());
349 if (active_source !=
nullptr) {
354 ESP_LOGV(TAG,
"Pipeline %u: stopping active source", pipeline);
368 ESP_LOGV(TAG,
"Pipeline %u: target source busy, stopping", pipeline);
395 if (!target_source->
play_uri(uri)) {
396 ESP_LOGE(TAG,
"Pipeline %u: Failed to play URI: %s", pipeline, uri.c_str());
411 cmd.pipeline = pipeline;
413 ESP_LOGE(TAG,
"Queue full, command dropped");
420 this->
pipelines_[pipeline].playlist_delay_ms = delay_ms;
428 this->
set_timeout(PIPELINE_TIMEOUT_IDS[pipeline], delay_ms,
444 bool command_executed =
false;
445 uint8_t pipeline = control_command.
pipeline;
451 switch (control_command.type) {
458 ps.
playlist.push_back(*control_command.data.uri);
462 command_executed =
true;
468 ps.
playlist.push_back(*control_command.data.uri);
476 bool nothing_playing =
482 command_executed =
true;
502 command_executed =
true;
512 command_executed =
true;
519 command_executed =
true;
525 if (command_executed) {
531 delete control_command.data.uri;
545 if (active_source !=
nullptr) {
546 target_source = active_source;
553 switch (player_command) {
557 if (target_source !=
nullptr) {
560 }
else if (!has_internal_playlist && active_source ==
nullptr && !ps.
playlist.empty()) {
562 if (last_has_internal_playlist) {
571 if (target_source !=
nullptr) {
579 if (!has_internal_playlist && active_source ==
nullptr && !ps.
playlist.empty()) {
581 if (last_has_internal_playlist) {
589 }
else if (target_source !=
nullptr) {
596 if (target_source !=
nullptr) {
608 if (target_source !=
nullptr) {
623 if (!has_internal_playlist) {
632 }
else if (target_source !=
nullptr) {
639 if (!has_internal_playlist) {
648 }
else if (target_source !=
nullptr) {
655 if (!has_internal_playlist) {
657 }
else if (target_source !=
nullptr) {
663 if (!has_internal_playlist) {
665 }
else if (target_source !=
nullptr) {
671 if (!has_internal_playlist) {
673 }
else if (target_source !=
nullptr) {
679 if (!has_internal_playlist) {
691 }
else if (target_source !=
nullptr) {
698 if (!has_internal_playlist) {
700 }
else if (target_source !=
nullptr) {
706 if (!has_internal_playlist) {
708 }
else if (target_source !=
nullptr) {
731 auto announcement =
call.get_announcement();
732 if (announcement.has_value() && announcement.value()) {
746 const auto &media_url =
call.get_media_url();
747 if (media_url.has_value()) {
748 auto command =
call.get_command();
759 control_command.data.uri =
new std::string(media_url.value());
761 delete control_command.data.uri;
762 ESP_LOGE(TAG,
"Queue full, URI dropped");
774 auto cmd =
call.get_command();
775 if (cmd.has_value()) {
776 switch (cmd.value()) {
792 control_command.data.command = cmd.value();
794 ESP_LOGE(TAG,
"Queue full, command dropped");
807 traits.set_supports_pause(
true);
810 if (ps.format.has_value()) {
811 traits.get_supported_formats().push_back(ps.format.value());
831 if (ps.is_configured()) {
832 ps.speaker->set_mute_state(mute_state);
843 for (
auto &ps : this->pipelines_) {
844 for (
auto &binding : ps.sources) {
845 binding->source->notify_mute_changed(mute_state);
867 for (
auto &binding : ps.sources) {
868 binding->source->notify_volume_changed(
volume);
882 float bounded_volume = (
volume < speaker::SILENT_VOLUME_THRESHOLD)
887 if (ps.is_configured()) {
888 ps.speaker->set_volume(bounded_volume);
920 for (
size_t i = 0; i < ps.
playlist.size(); i++) {
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
void defer(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call with a const char* name.
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.
uint32_t bytes_to_frames(size_t bytes) const
Convert bytes to frames.
void cancel()
The media player volume was set and already sent to the speakers; it applies until the next announcem...
void begin(bool other_audio_playing, F &&apply)
Called when an announcement starts.
void set_volume(float volume, F &&apply)
Stores a new announcement volume and applies it right away if it is in use.
bool is_configured() const
void end(float media_volume, F &&apply)
Switches back to the media player volume if the announcement volume is in use.
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)
audio::AudioStreamInfo & get_audio_stream_info()
ESPHOME_LOG_TAG(TAG, "speaker_source_media_player")
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
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).
speaker::Speaker * speaker
std::vector< size_t > shuffle_indices
uint32_t playlist_delay_ms
bool is_configured() const
Check if this pipeline is configured (has a speaker assigned)
media_source::MediaSource * last_source
media_source::MediaSource * pending_source
std::atomic< uint32_t > pending_frames
std::vector< std::unique_ptr< SourceBinding > > sources
std::vector< std::string > playlist
std::atomic< media_source::MediaSource * > active_source
media_source::MediaSource * stopping_source
void request_play_uri(const std::string &uri) override
void request_mute(bool is_muted) override
SpeakerSourceMediaPlayer * player
void request_volume(float volume) 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
media_source::MediaSource * source