ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
esphome::media_source::MediaSource Class Referenceabstract

Abstract base class for media sources MediaSource provides audio data to an orchestrator via the MediaSourceListener interface. More...

#include <media_source.h>

Public Member Functions

virtual ~MediaSource ()=default
 
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 is busy.
 
virtual void handle_command (MediaSourceCommand command)=0
 Handle playback commands (pause, stop, next, etc.)
 
virtual bool has_internal_playlist () const
 Whether this source manages its own playlist internally Smart sources that handle next/previous/repeat/shuffle should override this to return true.
 
MediaSourceState get_state () const
 Get current playback state (must only be called from the main loop)
 
virtual bool can_handle (const std::string &uri) const =0
 Check if this source can handle the given URI Each source must override this to match its supported URI scheme(s).
 
void set_listener (MediaSourceListener *listener)
 Set the listener that receives callbacks from this source.
 
bool has_listener () const
 Check if a listener has been registered.
 
size_t write_output (const uint8_t *data, size_t length, uint32_t timeout_ms, const audio::AudioStreamInfo &stream_info)
 Write audio data to the listener.
 
virtual void notify_volume_changed (float volume)
 Notify the source that volume changed Simple sources ignore this.
 
virtual void notify_mute_changed (bool is_muted)
 Notify the source that mute state changed Simple sources ignore this.
 
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 have been written to the DAC.
 

Protected Member Functions

void set_state_ (MediaSourceState state)
 Update state and notify listener (must only be called from the main loop) This is the only way to change state_, ensuring listener notifications always fire.
 

Detailed Description

Abstract base class for media sources MediaSource provides audio data to an orchestrator via the MediaSourceListener interface.

It also receives commands from the orchestrator to control playback.

Definition at line 61 of file media_source.h.

Constructor & Destructor Documentation

◆ ~MediaSource()

virtual esphome::media_source::MediaSource::~MediaSource ( )
virtualdefault

Member Function Documentation

◆ can_handle()

virtual bool esphome::media_source::MediaSource::can_handle ( const std::string & uri) const
pure virtual

Check if this source can handle the given URI Each source must override this to match its supported URI scheme(s).

Parameters
uriURI to check
Returns
true if this source can handle the URI

◆ get_state()

MediaSourceState esphome::media_source::MediaSource::get_state ( ) const
inline

Get current playback state (must only be called from the main loop)

Returns
Current state of this source

Definition at line 86 of file media_source.h.

◆ handle_command()

virtual void esphome::media_source::MediaSource::handle_command ( MediaSourceCommand command)
pure virtual

Handle playback commands (pause, stop, next, etc.)

Parameters
commandCommand to execute

◆ has_internal_playlist()

virtual bool esphome::media_source::MediaSource::has_internal_playlist ( ) const
inlinevirtual

Whether this source manages its own playlist internally Smart sources that handle next/previous/repeat/shuffle should override this to return true.

Definition at line 80 of file media_source.h.

◆ has_listener()

bool esphome::media_source::MediaSource::has_listener ( ) const
inline

Check if a listener has been registered.

Definition at line 103 of file media_source.h.

◆ notify_audio_played()

virtual void esphome::media_source::MediaSource::notify_audio_played ( uint32_t frames,
int64_t timestamp )
inlinevirtual

Notify the source about audio that has been played Called when the speaker reports that audio frames have been written to the DAC.

Sources can override this to track playback progress for synchronization.

Parameters
framesNumber of audio frames that were played
timestampSystem time in microseconds when the frames finished writing to the DAC

Definition at line 136 of file media_source.h.

◆ notify_mute_changed()

virtual void esphome::media_source::MediaSource::notify_mute_changed ( bool is_muted)
inlinevirtual

Notify the source that mute state changed Simple sources ignore this.

Override for smart sources that track mute state.

Parameters
is_mutedNew mute state

Definition at line 129 of file media_source.h.

◆ notify_volume_changed()

virtual void esphome::media_source::MediaSource::notify_volume_changed ( float volume)
inlinevirtual

Notify the source that volume changed Simple sources ignore this.

Override for smart sources that track volume state.

Parameters
volumeNew volume level (0.0 to 1.0)

Definition at line 124 of file media_source.h.

◆ play_uri()

virtual bool esphome::media_source::MediaSource::play_uri ( const std::string & uri)
pure virtual

Start playing the given URI Sources should validate the URI and state, returning false if the source is busy.

The orchestrator is responsible for stopping active sources before starting a new one.

Parameters
uriURI to play; e.g., "http://stream_url"
Returns
true if playback started successfully, false otherwise

◆ set_listener()

void esphome::media_source::MediaSource::set_listener ( MediaSourceListener * listener)
inline

Set the listener that receives callbacks from this source.

Parameters
listenerPointer to the MediaSourceListener implementation

Definition at line 100 of file media_source.h.

◆ set_state_()

void esphome::media_source::MediaSource::set_state_ ( MediaSourceState state)
inlineprotected

Update state and notify listener (must only be called from the main loop) This is the only way to change state_, ensuring listener notifications always fire.

Sources running FreeRTOS tasks should signal via event groups and call this from loop().

Parameters
stateNew state to set

Definition at line 143 of file media_source.h.

◆ write_output()

size_t esphome::media_source::MediaSource::write_output ( const uint8_t * data,
size_t length,
uint32_t timeout_ms,
const audio::AudioStreamInfo & stream_info )
inline

Write audio data to the listener.

Parameters
dataPointer to audio data buffer (not modified by this method)
lengthNumber of bytes to write
timeout_msMilliseconds to wait if the listener can't accept data immediately
stream_infoAudio stream format information
Returns
Number of bytes written, or 0 if no listener is set

Definition at line 111 of file media_source.h.


The documentation for this class was generated from the following file: