Crate mpris_server
source ·Expand description
MPRIS Server
Implement MPRIS D-Bus interface in your application.
This library provides the essential functionalities for implementing the MPRIS D-Bus interface. This enables your application to become discoverable and controllable by other MPRIS-compatible media controllers, including but not limited to GNOME Shell, KDE Plasma, and other libraries such as mpris.
This library supports all the following interfaces as defined in the specification:
- org.mpris.MediaPlayer2
- org.mpris.MediaPlayer2.Player
- org.mpris.MediaPlayer2.TrackList
- org.mpris.MediaPlayer2.Playlists
To implement these interfaces, this crate offers two flavors: you can either create your own struct and implement RootInterface and PlayerInterface (or with optional TrackListInterface and PlaylistsInterface), or you can use the ready-to-use mutable Player struct.
Examples
For more detailed examples, see also the examples directory.
Manual Implementation (via Server or LocalServer)
use std::future;
use mpris_server::{
export::{
async_trait::async_trait,
zbus::{fdo, Result},
},
Metadata, PlayerInterface, Property, RootInterface, Server, Time, Volume,
};
pub struct MyPlayer;
#[async_trait]
impl RootInterface for MyPlayer {
async fn identity(&self) -> fdo::Result<String> {
Ok("MyPlayer".into())
}
// Other methods...
}
#[async_trait]
impl PlayerInterface for MyPlayer {
async fn set_volume(&self, volume: Volume) -> Result<()> {
self.volume.set(volume);
Ok(())
}
async fn metadata(&self) -> fdo::Result<Metadata> {
let metadata = Metadata::builder()
.title("My Song")
.artist(["My Artist"])
.album("My Album")
.length(Time::from_micros(123))
.build();
Ok(metadata)
}
// Other methods...
}
#[async_std::main]
async fn main() {
let server = Server::new("com.my.Application", MyPlayer).unwrap();
// Initialize server's connection to the session bus
server.init().await.unwrap();
// Emit `PropertiesChanged` signal for `CanSeek` and `Metadata` properties
server.properties_changed(Property::CanSeek | Property::Metadata).await.unwrap();
// Emit `Seeked` signal
server.seeked(Time::from_micros(124)).await.unwrap();
// Prevent the program from exiting.
future::pending::<()>().await;
}Ready-to-use Implementation (via Player)
If you want to create a simple player without having to implement the interfaces, you can use the ready-to-use Player struct that implements those interfaces internally. This struct is mutable, automatically emits properties changed signal, and allows you to connect to method and property setter calls.
However, Player currently only supports the more commonly used org.mpris.MediaPlayer2 and org.mpris.MediaPlayer2.Player interfaces.
use mpris_server::{Player, Time};
#[async_std::main]
async fn main() {
let player = Player::builder("com.my.Application")
.can_play(true)
.can_pause(true)
.build()
.unwrap();
// Handle `PlayPause` method call
player.connect_play_pause(|| {
println!("PlayPause");
});
// Update `CanPlay` property and emit `PropertiesChanged` signal for it
player.set_can_play(false).await.unwrap();
// Emit `Seeked` signal
player.emit_seeked(Time::from_millis(1000)).await.unwrap();
player.init_and_run().await.unwrap();
}License
Copyright 2023 Dave Patrick Caberto
This software is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at this site.
Modules
- This contains the definitions of builder-pattern structs.
- This contains libraries that are used alongside with this crate.
Structs
- A data structure describing a playlist, or nothing.
- A mapping from metadata attribute names to values.
- Ready-to-use mutable object that internally implements
LocalRootInterfaceandLocalPlayerInterface. - A data structure describing a playlist.
- Thin wrapper around
zbus::Connectionthat calls toT’s implementation ofRootInterface,PlayerInterface,TrackListInterface, andPlaylistsInterfaceto implementorg.mpris.MediaPlayer2and its sub-interfaces. - A time which can be negative.
- Unique track identifier.
Enums
- A repeat / loop status
- A playback state.
- Specifies the ordering of returned playlists.
- Used for emitting
PropertiesChangedsignals onServerviaServer::playlists_properties_changed, ifTimplementsPlaylistsInterface. - Used for emitting
PropertiesChangedsignals onServerviaServer::track_list_properties_changed, ifTimplementsTrackListInterface.
Traits
- Local version of
PlayerInterfaceto be used withLocalServer. - Local version of
PlaylistsInterfaceto be used withLocalServer. - Local version of
RootInterfaceto be used withLocalServer. - Local version of
TrackListInterfaceto be used withLocalServer. - Used to implement org.mpris.MediaPlayer2.Player interface, which implements the methods for querying and providing basic control over what is currently playing.
- Used to implement org.mpris.MediaPlayer2.Playlists interface, which provides access to the media player’s playlists.
- Used to implement org.mpris.MediaPlayer2 interface.
- Used to implement org.mpris.MediaPlayer2.TrackList interface, which provides access to a short list of tracks which were recently played or will be played shortly. This is intended to provide context to the currently-playing track, rather than giving complete access to the media player’s playlist.
Type Aliases
- Combined date and time.
- A playback rate.
- Unique playlist identifier.
- A unique resource identifier.
- Audio volume level.