Skip to content

Commit

Permalink
impl mpris signals
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceZhang1993 committed Apr 11, 2024
1 parent 3eee584 commit 4357ad4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 11 deletions.
22 changes: 20 additions & 2 deletions aionowplaying/backend/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from abc import ABCMeta, abstractmethod
from typing import List

from aionowplaying.interface import MPInterface, MPPlayerInterface
from aionowplaying.interface import MPInterface, MPPlayerInterface, MPTrackListInterface
from aionowplaying.model import Metadata


class BaseNowPlayingBackend(metaclass=ABCMeta):
Expand All @@ -10,9 +11,26 @@ class BaseNowPlayingBackend(metaclass=ABCMeta):
def target_platforms() -> List[str]:
pass

def __init__(self, interface: MPInterface, player_interface: MPPlayerInterface):
def __init__(self, interface: MPInterface, player_interface: MPPlayerInterface,
tracklist_interface: MPTrackListInterface):
self._interface = interface
self._player_interface = player_interface
self._tracklist_interface = tracklist_interface

def seeked(self, position: int):
pass

def tracklist_replaced(self, tracks: List[str], current: str):
pass

def track_added(self, metadata: Metadata, after_track: str):
pass

def track_removed(self, track: str):
pass

def track_metadata_changed(self, track: str, metadata: Metadata):
pass

@abstractmethod
def run(self):
Expand Down
76 changes: 70 additions & 6 deletions aionowplaying/backend/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

from aionowplaying.backend.base import BaseNowPlayingBackend
from aionowplaying.enum import LoopStatus
from aionowplaying.interface import MPInterface, MPPlayerInterface
from aionowplaying.interface_old.base import PropertyName, PlaybackPropertyName, PlaybackProperties
from aionowplaying.interface import MPInterface, MPPlayerInterface, MPTrackListInterface
from aionowplaying.interface_old.base import PropertyName, PlaybackPropertyName, PlaybackProperties, \
TrackListPropertyName
from aionowplaying.model import Metadata


Expand Down Expand Up @@ -174,8 +175,8 @@ def can_control(self) -> 'b':
return self._it.canControl

@signal(name='Seeked')
def seeked(self, position: int) -> 'x':
return self._it.signalSeeked(position)
def seeked(self, position: 'x'):
pass

@method(name="Next")
def next(self):
Expand Down Expand Up @@ -222,13 +223,60 @@ def set_position(self, track_id: 'o', position: 'x'):
self._it.setPosition(track_id, position)


class MprisTracklistServiceInterface(ServiceInterface):
def __init__(self, bus_name: str, it: MPTrackListInterface = None):
super().__init__(bus_name)
self._it = it

@dbus_property(access=PropertyAccess.READ, name=TrackListPropertyName.CanEditTracks.value)
def can_edit_tracks(self) -> 'b':
return self._it.canEditTracks

@dbus_property(access=PropertyAccess.READ, name=TrackListPropertyName.Tracks.value)
def tracks(self) -> 'ao':
return self._it.tracks

@method(name="GetTracksMetadata")
def get_tracks_metadata(self, track_ids: 'ao') -> 'aa{sv}':
return self._it.getTracksMetadata(track_ids)

@method(name="AddTrack")
def add_track(self, uri: 's', after_track_id: 'o', set_as_current: 'b'):
self._it.addTrack(uri, after_track_id, set_as_current)

@method(name="RemoveTrack")
def remove_track(self, track_id: 'o'):
self._it.removeTrack(track_id)

@method(name="GoTo")
def goto(self, track_id: 'o'):
self._it.goTo(track_id)

@signal(name="TrackListReplaced")
def track_list_replaced(self, track_ids: 'ao', current_track_id: 'o'):
pass

@signal(name="TrackAdded")
def track_added(self, metadata: 'a{sv}', after_track_id: 'o'):
pass

@signal(name="TrackRemoved")
def track_removed(self, track_id: 'o'):
pass

@signal(name="TrackMetadataChanged")
def track_metadata_changed(self, track_id: 'o', metadata: 'a{sv}'):
pass


class LinuxNowPlayingBackend(BaseNowPlayingBackend):
@staticmethod
def target_platforms() -> List[str]:
return ['linux']

def __init__(self, interface: MPInterface, player_interface: MPPlayerInterface):
super().__init__(interface, player_interface)
def __init__(self, interface: MPInterface, player_interface: MPPlayerInterface,
tracklist_interface: MPTrackListInterface):
super().__init__(interface, player_interface, tracklist_interface)
self._dbus: Optional[MessageBus] = None
self._bus_name = f'org.mpris.MediaPlayer2.{interface.id}'
self._entry_name = 'org.mpris.MediaPlayer2'
Expand All @@ -237,6 +285,22 @@ def __init__(self, interface: MPInterface, player_interface: MPPlayerInterface):
self._object_path = '/org/mpris/MediaPlayer2'
self._bus = MprisServiceInterface(self._entry_name, interface)
self._player_bus = MprisPlayerServiceInterface(self._player_entry_name, player_interface)
self._tracklist_bus = MprisTracklistServiceInterface(self._player_tracklist_name, tracklist_interface)

def seeked(self, position: int):
self._player_bus.seeked(position)

def tracklist_replaced(self, tracks: List[str], current: str):
self._tracklist_bus.track_list_replaced(tracks, current)

def track_added(self, metadata: Metadata, after_track: str):
self._tracklist_bus.track_added(DBusBeanMapper.metadata(metadata), after_track)

def track_removed(self, track: str):
self._tracklist_bus.track_removed(track)

def track_metadata_changed(self, track: str, metadata: Metadata):
self._tracklist_bus.track_metadata_changed(track, DBusBeanMapper.metadata(metadata))

def run(self):
self._dbus = MessageBus()
Expand Down
12 changes: 9 additions & 3 deletions aionowplaying/interface.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from abc import ABCMeta, abstractmethod
from typing import List, Optional

from aionowplaying.backend import BaseNowPlayingBackend
from aionowplaying.enum import PlaybackStatus, LoopStatus
from aionowplaying.model import Metadata


# noinspection PyPep8Naming
class MPInterface(metaclass=ABCMeta):
def __init__(self):
self.backend: Optional[BaseNowPlayingBackend]

"""
Media player definitions of aionp
All media players using aionp should implement this interface
Expand Down Expand Up @@ -245,16 +249,18 @@ def metadata(self) -> Metadata:
def position(self) -> int:
pass

def signalSeeked(self, position: int):
pass


# noinspection PyPep8Naming
class MPTrackListInterface(metaclass=ABCMeta):
@abstractmethod
def getTracksMetadata(self, trackIds: List[str]) -> List[Metadata]:
pass

@property
@abstractmethod
def tracks(self) -> List[str]:
pass

@property
@abstractmethod
def canEditTracks(self) -> bool:
Expand Down

0 comments on commit 4357ad4

Please sign in to comment.