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);
184 ESP_LOGE(TAG,
"%s source error",
215 vTaskDelay(pdMS_TO_TICKS(timeout_ms));
221 ps.
pending_frames.fetch_add(reserved_frames, std::memory_order_relaxed);
222 size_t bytes_written = ps.
speaker->
play(data,
length, pdMS_TO_TICKS(timeout_ms));
224 if (unused_frames > 0) {
228 while (!ps.
pending_frames.compare_exchange_weak(pending, pending - std::min(pending, unused_frames),
229 std::memory_order_relaxed)) {
232 return bytes_written;
236 vTaskDelay(pdMS_TO_TICKS(timeout_ms));
243 if (source !=
nullptr) {
281 if (announcement_source !=
nullptr) {
285 switch (announcement_state) {
315 if (this->
state != old_state) {
324 if (source !=
nullptr) {
334 for (
auto &binding : ps.
sources) {
335 if (binding->source->can_handle(uri)) {
338 return binding->source;
340 if (first_match ==
nullptr) {
341 first_match = binding->source;
351 if (target_source ==
nullptr) {
352 ESP_LOGW(TAG,
"No source for URI");
353 ESP_LOGV(TAG,
"URI: %s", uri.c_str());
362 if (active_source !=
nullptr) {
367 ESP_LOGV(TAG,
"Pipeline %u: stopping active source", pipeline);
381 ESP_LOGV(TAG,
"Pipeline %u: target source busy, stopping", pipeline);
408 if (!target_source->
play_uri(uri)) {
409 ESP_LOGE(TAG,
"Pipeline %u: Failed to play URI: %s", pipeline, uri.c_str());
424 cmd.pipeline = pipeline;
426 ESP_LOGE(TAG,
"Queue full, command dropped");
433 this->
pipelines_[pipeline].playlist_delay_ms = delay_ms;
441 this->
set_timeout(PIPELINE_TIMEOUT_IDS[pipeline], delay_ms,
457 bool command_executed =
false;
458 uint8_t pipeline = control_command.
pipeline;
464 switch (control_command.type) {
471 ps.
playlist.push_back(*control_command.data.uri);
475 command_executed =
true;
481 ps.
playlist.push_back(*control_command.data.uri);
489 bool nothing_playing =
495 command_executed =
true;
515 command_executed =
true;
525 command_executed =
true;
532 command_executed =
true;
538 if (command_executed) {
544 delete control_command.data.uri;
558 if (active_source !=
nullptr) {
559 target_source = active_source;
566 switch (player_command) {
570 if (target_source !=
nullptr) {
573 }
else if (!has_internal_playlist && active_source ==
nullptr && !ps.
playlist.empty()) {
575 if (last_has_internal_playlist) {
584 if (target_source !=
nullptr) {
592 if (!has_internal_playlist && active_source ==
nullptr && !ps.
playlist.empty()) {
594 if (last_has_internal_playlist) {
602 }
else if (target_source !=
nullptr) {
609 if (target_source !=
nullptr) {
621 if (target_source !=
nullptr) {
636 if (!has_internal_playlist) {
645 }
else if (target_source !=
nullptr) {
652 if (!has_internal_playlist) {
661 }
else if (target_source !=
nullptr) {
668 if (!has_internal_playlist) {
670 }
else if (target_source !=
nullptr) {
676 if (!has_internal_playlist) {
678 }
else if (target_source !=
nullptr) {
684 if (!has_internal_playlist) {
686 }
else if (target_source !=
nullptr) {
692 if (!has_internal_playlist) {
704 }
else if (target_source !=
nullptr) {
711 if (!has_internal_playlist) {
713 }
else if (target_source !=
nullptr) {
719 if (!has_internal_playlist) {
721 }
else if (target_source !=
nullptr) {
744 auto announcement =
call.get_announcement();
745 if (announcement.has_value() && announcement.value()) {
759 const auto &media_url =
call.get_media_url();
760 if (media_url.has_value()) {
761 auto command =
call.get_command();
768 control_command.data.uri =
new std::string(media_url.value());
770 delete control_command.data.uri;
771 ESP_LOGE(TAG,
"Queue full, URI dropped");
783 auto cmd =
call.get_command();
784 if (cmd.has_value()) {
785 switch (cmd.value()) {
801 control_command.data.command = cmd.value();
803 ESP_LOGE(TAG,
"Queue full, command dropped");
816 traits.set_supports_pause(
true);
819 if (ps.format.has_value()) {
820 traits.get_supported_formats().push_back(ps.format.value());
840 if (ps.is_configured()) {
841 ps.speaker->set_mute_state(mute_state);
852 for (
auto &ps : this->pipelines_) {
853 for (
auto &binding : ps.sources) {
854 binding->source->notify_mute_changed(mute_state);
876 for (
auto &binding : ps.sources) {
877 binding->source->notify_volume_changed(
volume);
891 float bounded_volume = (
volume < speaker::SILENT_VOLUME_THRESHOLD)
896 if (ps.is_configured()) {
897 ps.speaker->set_volume(bounded_volume);
929 for (
size_t i = 0; i < ps.
playlist.size(); i++) {
void defer(const char *name, SchedulerCallback &&f)
Defer a callback to the next loop() call with a const char* name.
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function 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