Audio API
The audio API is the interface we have built around GStreamer to support our specific use cases. Most backends should be able to get by with simply setting the URI of the resource they want to play, for these cases the default playback provider should be used.
For more advanced cases such as when the raw audio data is delivered outside of GStreamer or the backend needs to add metadata to the currently playing resource, developers should sub-class the base playback provider and implement the extra behaviour that is needed through the following API:
mopidy.audio
Modules:
Classes:
-
Audio–Audio output API.
-
AudioListener–Marker interface for recipients of events sent by the audio actor.
-
AudioProxy–Audio layer wrapped in a Pykka actor proxy.
-
GstAudio–Audio output through GStreamer.
Functions:
-
supported_uri_schemes–Determine which URIs we can actually support from provided whitelist.
Audio
Audio output API.
There is only a single implementation of this API, using GStreamer.
The primary motivation for defining this API separate from the implementation is to make it a bit easier to mock the audio layer in tests. If we ever add more implementations, changes to this API will probably be necessary.
Methods:
-
get_current_tags–Get the currently playing media's tags.
-
get_position–Get position in milliseconds.
-
pause_playback–Pause playback.
-
prepare_change–Notify audio layer that we are about to change playback state.
-
set_about_to_finish_callback–Configure audio to use an about-to-finish callback.
-
set_position–Set position in milliseconds.
-
set_source_setup_callback–Configure audio to use a source-setup callback.
-
set_uri–Set URI of audio to be played.
-
start_playback–Start playback.
-
stop_playback–Stop playback.
-
testing_gst__enable_sync_handler–Enable manual processing of messages from bus.
-
testing_gst__wait_for_state_change–Block until any pending playback state changes are complete.
Attributes:
-
state(PlaybackState) –The GStreamer state mapped to PlaybackState.
state
class-attribute
instance-attribute
state: PlaybackState = STOPPED
The GStreamer state mapped to PlaybackState.
get_current_tags
Get the currently playing media's tags.
If no tags have been found, or nothing is playing this returns an empty
dictionary. For each set of tags we collect a tags_changed event is
emitted with the keys of the changed tags. After such calls users may
call this function to get the updated values.
prepare_change
prepare_change() -> bool
Notify audio layer that we are about to change playback state.
This function MUST be called before changing URIs or doing changes like updating data that is being pushed.
set_about_to_finish_callback
Configure audio to use an about-to-finish callback.
This should be used to achieve gapless playback. For this to work the callback MUST call set_uri with the new URI to play and block until this call has been made. prepare_change is not needed before set_uri in this one special case.
Parameters:
-
(callbackCallable[[], None]) –Callback to run when we need the next URI.
set_source_setup_callback
Configure audio to use a source-setup callback.
This should be used to modify source-specific properties such as login details.
Parameters:
-
(callbackCallable[[Element], None]) –Callback to run when we set up the source.
set_uri
testing_gst__enable_sync_handler
testing_gst__enable_sync_handler() -> None
Enable manual processing of messages from bus.
Not part of the API. Only for testing of GstAudio.
testing_gst__wait_for_state_change
testing_gst__wait_for_state_change() -> None
Block until any pending playback state changes are complete.
Not part of the API. Only for testing of GstAudio.
AudioListener
Bases: Listener
Marker interface for recipients of events sent by the audio actor.
Any Pykka actor that mixes in this class will receive calls to the methods defined here when the corresponding events happen in the core actor. This interface is used both for looking up what actors to notify of the events, and for providing default implementations for those listeners that are not interested in all events.
Methods:
-
on_event–Called on all events.
-
position_changed–Called whenever the position of the stream changes.
-
reached_end_of_stream–Called whenever the end of the audio stream is reached.
-
send–Helper to allow calling of audio listener events.
-
state_changed–Called after the playback state have changed.
-
stream_changed–Called whenever the audio stream changes.
-
tags_changed–Called whenever the current audio stream's tags change.
on_event
position_changed
position_changed(position: DurationMs) -> None
Called whenever the position of the stream changes.
MAY be implemented by actor.
Parameters:
-
(positionDurationMs) –Position in milliseconds.
reached_end_of_stream
reached_end_of_stream() -> None
Called whenever the end of the audio stream is reached.
MAY be implemented by actor.
send
staticmethod
Helper to allow calling of audio listener events.
state_changed
state_changed(
old_state: PlaybackState,
new_state: PlaybackState,
target_state: PlaybackState | None,
) -> None
Called after the playback state have changed.
Will be called for both immediate and async state changes in GStreamer.
Target state is used to when we should be in the target state, but
temporarily need to switch to an other state. A typical example of this
is buffering. When this happens an event with
old=PLAYING, new=PAUSED, target=PLAYING will be emitted. Once we have
caught up a old=PAUSED, new=PLAYING, target=None event will be
be generated.
Regular state changes will not have target state set as they are final states which should be stable.
MAY be implemented by actor.
Parameters:
-
(old_statePlaybackState) –The state before the change.
-
(new_statePlaybackState) –The state after the change.
-
(target_statePlaybackState | None) –The intended state, or
Noneif this is a final state.
stream_changed
Called whenever the audio stream changes.
MAY be implemented by actor.
Parameters:
-
(uriUri) –URI the stream has started playing.
tags_changed
Called whenever the current audio stream's tags change.
This event signals that some track metadata has been updated. This can be metadata such as artists, titles, organization, or details about the actual audio such as bit-rates, numbers of channels etc.
For the available tag keys please refer to GStreamer documentation for tags.
MAY be implemented by actor.
Parameters:
AudioProxy
Bases: ActorMemberMixin, ActorProxy[AudioActor]
Audio layer wrapped in a Pykka actor proxy.
GstAudio
GstAudio(config: Config, mixer: MixerProxy | None)
Bases: Audio, ThreadingActor
Audio output through GStreamer.
Attributes:
-
state(PlaybackState) –The GStreamer state mapped to PlaybackState.
state
class-attribute
instance-attribute
state: PlaybackState = STOPPED
The GStreamer state mapped to PlaybackState.
mopidy.audio.scan
Classes:
-
Scanner–Helper to get tags and other relevant info from URIs.
Scanner
Scanner(timeout: int = 1000, proxy_config: ProxyConfig | None = None)
Helper to get tags and other relevant info from URIs.
Parameters:
-
(timeoutint, default:1000) –Timeout for scanning a URI in milliseconds.
-
(proxy_configProxyConfig | None, default:None) –Dictionary containing proxy config strings.
Methods:
-
scan–Scan the given URI collecting relevant metadata.
scan
Scan the given URI collecting relevant metadata.
Parameters:
-
(uristr) –URI of the resource to scan.
-
(timeoutfloat | None, default:None) –Timeout for scanning in milliseconds. Defaults to the
timeoutvalue used when creating the scanner.
Returns:
-
_Result–Named tuple:
uri,tags,duration,seekable,mime,playable.
mopidy.audio.tags
Functions:
-
convert_taglist–Convert a
Gst.TagListto plain Python types. -
convert_tags_to_track–Convert our normalized tags to a track.
-
repr_tags–Returns a printable representation of a
Gst.TagList.
convert_taglist
Convert a Gst.TagList to plain Python types.
Knows how to convert:
- Dates
- Buffers
- Numbers
- Strings
- Booleans
Unknown types will be ignored and trace logged. Tag keys are all strings defined as part of GStreamer's GstTagList.
Parameters:
-
(taglistTagList) –A GStreamer taglist to be converted.
convert_tags_to_track
convert_tags_to_track(
tags: dict[str, Any],
*,
uri: Uri,
length: DurationMs | None = None,
last_modified: int | None = None,
) -> Track
Convert our normalized tags to a track.
repr_tags
Returns a printable representation of a Gst.TagList.
Tag values of type bytes are truncated to the specified length to avoid large amounts of output when logging.
Parameters: