File: mpris.py

package info (click to toggle)
playerctl 2.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704 kB
  • sloc: ansic: 5,157; python: 1,107; xml: 198; sh: 133; makefile: 62
file content (274 lines) | stat: -rw-r--r-- 7,429 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from dbus_next.service import ServiceInterface, dbus_property, method, signal, Variant
from dbus_next import PropertyAccess, RequestNameReply, BusType
from dbus_next.aio import MessageBus

import asyncio


async def setup_mpris(*names, bus_address=None, system=False):
    # TODO maybe they should all share a bus for speed
    async def setup(name):
        if system:
            bus_type = BusType.SYSTEM
        else:
            bus_type = BusType.SESSION
        bus = await MessageBus(bus_type=bus_type,
                               bus_address=bus_address).connect()
        player = MprisPlayer(bus)
        bus.export('/org/mpris/MediaPlayer2', player)
        bus.export('/org/mpris/MediaPlayer2', MprisRoot())
        reply = await bus.request_name(f'org.mpris.MediaPlayer2.{name}')
        assert reply == RequestNameReply.PRIMARY_OWNER
        return player

    players = await asyncio.gather(*(setup(name) for name in names))
    await asyncio.gather(*(p.ping() for p in players))
    return players


async def setup_playerctld(bus_address=None):
    bus = await MessageBus(bus_address=bus_address).connect()
    playerctld = PlayerctldInterface(bus)
    bus.export('/org/mpris/MediaPlayer2', playerctld)
    reply = await bus.request_name('org.mpris.MediaPlayer2.playerctld')
    assert reply == RequestNameReply.PRIMARY_OWNER
    return playerctld


class MprisRoot(ServiceInterface):
    def __init__(self):
        super().__init__('org.mpris.MediaPlayer2')

    @method()
    def Raise(self):
        return

    @method()
    def Quit(self):
        return

    @dbus_property(access=PropertyAccess.READ)
    def CanRaise(self) -> 'b':
        return False

    @dbus_property(access=PropertyAccess.READ)
    def HasTrackList(self) -> 'b':
        return False

    @dbus_property(access=PropertyAccess.READ)
    def Identity(self) -> 's':
        return 'playerctl test client'

    @dbus_property(access=PropertyAccess.READ)
    def SupportedUriSchemes(self) -> 'as':
        return ['file']

    @dbus_property(access=PropertyAccess.READ)
    def SupportedMimeTypes(self) -> 'as':
        return ['audio/mp3']


class MprisPlayer(ServiceInterface):
    def __init__(self, bus):
        super().__init__('org.mpris.MediaPlayer2.Player')
        self.counter = 0
        self.reset()
        self.bus = bus

    def reset(self):
        # method calls
        self.next_called = False
        self.previous_called = False
        self.pause_called = False
        self.play_pause_called = False
        self.stop_called = False
        self.play_called = False
        self.seek_called_with = None
        self.set_position_called_with = None
        self.open_uri_called_with = None

        # properties
        self.playback_status = 'Playing'
        self.loop_status = 'None'
        self.rate = 1.0
        self.shuffle = False
        self.metadata = {}
        self.volume = 1.0
        self.position = 0
        self.minimum_rate = 1.0
        self.maximum_rate = 1.0
        self.can_go_next = True
        self.can_go_previous = True
        self.can_play = True
        self.can_pause = True
        self.can_seek = True
        self.can_control = True

        # signals
        self.seeked_value = 0

    async def ping(self):
        await self.bus.introspect('org.freedesktop.DBus',
                                  '/org/freedesktop/DBus')

    async def set_artist_title(self, artist, title, track_id=None):
        if track_id is None:
            self.counter += 1
            track_id = '/' + str(self.counter)

        self.metadata = {
            'xesam:title': Variant('s', title),
            'xesam:artist': Variant('as', [artist]),
            'mpris:trackid': Variant('o', track_id),
        }

        self.emit_properties_changed({
            'Metadata': self.metadata,
        })
        await self.ping()

    async def clear_metadata(self):
        self.counter += 1
        self.metadata = {
            'mpris:trackid': Variant('o', '/' + str(self.counter)),
        }
        self.emit_properties_changed({
            'Metadata': self.metadata,
        })
        await self.ping()

    async def disconnect(self):
        self.bus.disconnect()
        await self.bus.wait_for_disconnect()

    @method()
    def Next(self):
        self.next_called = True

    @method()
    def Previous(self):
        self.previous_called = True

    @method()
    def Pause(self):
        self.pause_called = True

    @method()
    def PlayPause(self):
        self.play_pause_called = True

    @method()
    def Stop(self):
        self.stop_called = True

    @method()
    def Play(self):
        self.play_called = True

    @method()
    def Seek(self, offset: 'x'):
        self.seek_called_with = offset

    @method()
    def SetPosition(self, track_id: 'o', position: 'x'):
        self.set_position_called_with = (track_id, position)

    @method()
    def OpenUri(self, uri: 's'):
        self.open_uri_called_with = uri

    @signal()
    def Seeked(self) -> 'x':
        return self.seeked_value

    @dbus_property(access=PropertyAccess.READ)
    def PlaybackStatus(self) -> 's':
        return self.playback_status

    @dbus_property()
    def LoopStatus(self) -> 's':
        return self.loop_status

    @LoopStatus.setter
    def LoopStatus(self, status: 's'):
        self.loop_status = status

    @dbus_property()
    def Rate(self) -> 'd':
        return self.rate

    @Rate.setter
    def Rate(self, rate: 'd'):
        self.rate = rate

    @dbus_property()
    def Shuffle(self) -> 'b':
        return self.shuffle

    @Shuffle.setter
    def Shuffle(self, shuffle: 'b'):
        self.shuffle = shuffle

    @dbus_property(access=PropertyAccess.READ)
    def Metadata(self) -> 'a{sv}':
        return self.metadata

    @dbus_property()
    def Volume(self) -> 'd':
        return self.volume

    @Volume.setter
    def Volume(self, volume: 'd'):
        self.volume = volume

    @dbus_property(access=PropertyAccess.READ)
    def Position(self) -> 'x':
        return self.position

    @dbus_property(access=PropertyAccess.READ)
    def MinimumRate(self) -> 'd':
        return self.minimum_rate

    @dbus_property(access=PropertyAccess.READ)
    def MaximumRate(self) -> 'd':
        return self.maximum_rate

    @dbus_property(access=PropertyAccess.READ)
    def CanGoNext(self) -> 'b':
        return self.can_go_next

    @dbus_property(access=PropertyAccess.READ)
    def CanGoPrevious(self) -> 'b':
        return self.can_go_previous

    @dbus_property(access=PropertyAccess.READ)
    def CanPlay(self) -> 'b':
        return self.can_play

    @dbus_property(access=PropertyAccess.READ)
    def CanPause(self) -> 'b':
        return self.can_pause

    @dbus_property(access=PropertyAccess.READ)
    def CanSeek(self) -> 'b':
        return self.can_seek

    @dbus_property(access=PropertyAccess.READ)
    def CanControl(self) -> 'b':
        return self.can_control


class PlayerctldInterface(ServiceInterface):
    '''just enough of playerctld for testing'''
    def __init__(self, bus):
        super().__init__('com.github.altdesktop.playerctld')
        self.bus = bus
        self.player_names = []

    @dbus_property(access=PropertyAccess.READ)
    def PlayerNames(self) -> 'as':
        return self.player_names

    async def disconnect(self):
        self.bus.disconnect()
        await self.bus.wait_for_disconnect()