-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathplayer.py
534 lines (450 loc) · 15.6 KB
/
player.py
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import subprocess
import time
import os
import signal
import logging
import threading
import math
import urlparse
from decorator import decorator
from glob import glob
from dbus import DBusException, Int64, String, ObjectPath
from omxplayer.bus_finder import BusFinder
from omxplayer.dbus_connection import DBusConnection, \
DBusConnectionError
from evento import Event
#### CONSTANTS ####
RETRY_DELAY = 0.05
#### FILE GLOBAL OBJECTS ####
logger = logging.getLogger(__name__)
#### CLASSES ####
class FileNotFoundError(Exception):
pass
class OMXPlayer(object):
"""
OMXPlayer controller
This works by speaking to OMXPlayer over DBus sending messages.
Args:
source (str): Path to the file (as ~/Videos/my-video.mp4) or URL you wish to play
args (list): used to pass option parameters to omxplayer. see: https://github.com/popcornmix/omxplayer#synopsis
Multiple argument example:
>>> OMXPlayer('path.mp4', args=['--no-osd', '--no-keys', '-b'])
"""
def __init__(self, source,
args=[],
bus_address_finder=None,
Connection=None,
pause=False):
logger.debug('Instantiating OMXPlayer')
self.args = args
self.tries = 0
self._is_playing = True
self._source = source
self._Connection = Connection if Connection else DBusConnection
self._bus_address_finder = bus_address_finder if bus_address_finder else BusFinder()
#: Event called on pause ``callback(player)``
self.pauseEvent = Event()
#: Event called on play ``callback(player)``
self.playEvent = Event()
#: Event called on stop ``callback(player)``
self.stopEvent = Event()
#: Event called on seek ``callback(player, relative_position)``
self.seekEvent = Event()
#: Event called on setting position ``callback(player, absolute_position)``
self.positionEvent = Event()
self._process = None
self._connection = None
self.load(source, pause=pause)
def _load_source(self, source):
if self._process:
self.quit()
self._process = self._setup_omxplayer_process(source)
self._connection = self._setup_dbus_connection(self._Connection, self._bus_address_finder)
def _run_omxplayer(self, source, devnull):
def on_exit():
logger.info("OMXPlayer process is dead, all DBus calls from here "
"will fail")
def monitor(process, on_exit):
process.wait()
on_exit()
command = ['omxplayer'] + self.args + [source]
logger.debug("Opening omxplayer with the command: %s" % command)
process = subprocess.Popen(command,
stdin=devnull,
stdout=devnull,
preexec_fn=os.setsid)
self._process_monitor = threading.Thread(target=monitor,
args=(process, on_exit))
self._process_monitor.start()
return process
def _setup_omxplayer_process(self, source):
logger.debug('Setting up OMXPlayer process')
source_url = urlparse.urlsplit(source)
if not source_url.scheme and not os.path.isfile(source):
raise FileNotFoundError("Could not find: {}".format(source))
with open(os.devnull, 'w') as devnull:
process = self._run_omxplayer(source, devnull)
logger.debug('Process opened with PID %s' % process.pid)
return process
def _setup_dbus_connection(self, Connection, bus_address_finder):
logger.debug('Trying to connect to OMXPlayer via DBus')
while self.tries < 50:
logger.debug('DBus connect attempt: {}'.format(self.tries))
try:
connection = Connection(bus_address_finder.get_address())
logger.debug(
'Connected to OMXPlayer at DBus address: %s' % connection)
return connection
except (DBusConnectionError, IOError):
logger.debug('Failed to connect to OMXPlayer DBus address')
self.tries += 1
time.sleep(RETRY_DELAY)
raise SystemError('DBus cannot connect to the OMXPlayer process')
""" Utilities """
def _check_player_is_active(fn):
# wraps is a decorator that improves debugging wrapped methods
def wrapped(fun, self, *args, **kwargs):
logger.debug('Checking if process is still alive')
# poll determines whether the process has terminated,
# if it hasn't it returns None.
if self._process.poll() is None:
logger.debug('OMXPlayer is running, so execute %s' %
fn.__name__)
return fn(self, *args, **kwargs)
else:
logger.info('Process is no longer alive, can\'t run command')
return decorator(wrapped, fn)
def load(self, source, pause=False):
"""
Loads a new source (as a file) from ``source`` (a file path or URL)
by killing the current ``omxplayer`` process and forking a new one.
Args:
source (string): Path to the file to play or URL
"""
self._source = source
self._load_source(source)
if pause:
time.sleep(0.5) # Wait for the DBus interface to be initialised
self.pause()
""" ROOT INTERFACE METHODS """
@_check_player_is_active
def can_quit(self):
"""
Returns:
bool: """
return bool(self._get_root_interface().CanQuit())
@_check_player_is_active
def can_set_fullscreen(self):
"""
Returns:
bool: """
return bool(self._get_root_interface().CanSetFullscreen())
@_check_player_is_active
def identity(self):
"""
Get the ID of the media player
Returns:
bool:
"""
return str(self._get_root_interface().Identity())
""" PLAYER INTERFACE PROPERTIES """
@_check_player_is_active
def can_go_next(self):
"""
Returns:
bool: Whether the player can move to the next item in the playlist
"""
return bool(self._get_properties_interface().CanGoNext())
@_check_player_is_active
def can_go_previous(self):
"""
Returns:
bool: Whether the player can move to the previous item in the
playlist
"""
return bool(self._get_properties_interface().CanGoPrevious())
@_check_player_is_active
def can_seek(self):
"""
Returns:
bool: Whether the player can seek """
return bool(self._get_properties_interface().CanSeek())
@_check_player_is_active
def can_control(self):
"""
Returns:
bool: """
return bool(self._get_properties_interface().CanControl())
@_check_player_is_active
def can_play(self):
"""
Returns:
bool: """
return bool(self._get_properties_interface().CanPlay())
@_check_player_is_active
def can_pause(self):
"""
Returns:
bool: """
return bool(self._get_properties_interface().CanPause())
@_check_player_is_active
def playback_status(self):
"""
Returns:
str: One of ("Playing" | "Paused" | "Stopped")
"""
return str(self._get_properties_interface().PlaybackStatus())
@_check_player_is_active
def volume(self):
"""
Returns:
volume (float): Volume in millibels
"""
vol = float(self._get_properties_interface().Volume())
return 2000 * math.log(vol, 10)
@_check_player_is_active
def set_volume(self, volume):
"""
Args:
volume (float): Volume in millibels
"""
return float(self._get_properties_interface().Volume(
10**(volume/2000.0)
))
@_check_player_is_active
def mute(self):
"""
Turns mute on, if the audio is already muted, then this does not do
anything
Returns:
None:
"""
self._get_properties_interface().Mute()
@_check_player_is_active
def unmute(self):
"""
Unmutes the video, if the audio is already unmuted, then this does
not do anything
Returns:
None:
"""
self._get_properties_interface().Unmute()
@_check_player_is_active
def position(self):
"""
Returns:
float: The position in seconds
"""
return self._get_properties_interface().Position() / (1000 * 1000.0)
@_check_player_is_active
def _duration_us(self):
"""
Returns:
long: The duration in microseconds
"""
return long(self._get_properties_interface().Duration())
@_check_player_is_active
def duration(self):
"""
Returns:
float: The duration in seconds
"""
return self._duration_us() / (1000 * 1000.0)
@_check_player_is_active
def minimum_rate(self):
"""
Returns:
str: The minimum playback rate
"""
return float(self._get_properties_interface().MinimumRate())
@_check_player_is_active
def maximum_rate(self):
"""
Returns:
str: The maximum playback rate
"""
return float(self._get_properties_interface().MaximumRate())
""" PLAYER INTERFACE METHODS """
@_check_player_is_active
def pause(self):
"""
Return:
None:
"""
self._get_player_interface().Pause()
self._is_playing = False
self.pauseEvent(self)
@_check_player_is_active
def play_pause(self):
"""
Return:
None:
"""
self._get_player_interface().PlayPause()
self._is_playing = not self._is_playing
if self._is_playing:
self.playEvent(self)
else:
self.pauseEvent(self)
@_check_player_is_active
def stop(self):
self._get_player_interface().Stop()
self.stopEvent(self)
@_check_player_is_active
def seek(self, relative_position):
"""
Args:
relative_position (float): The position in seconds to seek to.
"""
self._get_player_interface().Seek(Int64(relative_position))
self.seekEvent(self, relative_position)
@_check_player_is_active
def set_position(self, position):
"""
Args:
position (float): The position in seconds.
"""
self._get_player_interface().SetPosition(ObjectPath("/not/used"), Int64(position*1000*1000))
self.positionEvent(self, position)
@_check_player_is_active
def set_alpha(self, alpha):
"""
Args:
alpha (float): The transparency (0..255)
"""
self._get_player_interface().SetAlpha(ObjectPath('/not/used'), Int64(alpha))
@_check_player_is_active
def set_aspect_mode(self, mode):
"""
Args:
mode (str): One of ("letterbox" | "fill" | "stretch")
"""
self._get_player_interface().SetAspectMode(ObjectPath('/not/used'), String(mode))
@_check_player_is_active
def set_video_pos(self, x1, y1, x2, y2):
"""
Args:
Image position (int, int, int, int):
"""
position = "%s %s %s %s" % (str(x1),str(y1),str(x2),str(y2))
self._get_player_interface().VideoPos(ObjectPath('/not/used'), String(position))
@_check_player_is_active
def set_video_crop(self, x1, y1, x2, y2):
"""
Args:
Image position (int, int, int, int):
"""
crop = "%s %s %s %s" % (str(x1),str(y1),str(x2),str(y2))
self._get_player_interface().SetVideoCropPos(ObjectPath('/not/used'), String(crop))
@_check_player_is_active
def list_video(self):
"""
Returns:
[str]: A list of all known video streams, each item is in the
format: ``<index>:<language>:<name>:<codec>:<active>``
"""
return map(str, self._get_player_interface().ListVideo())
@_check_player_is_active
def list_audio(self):
"""
Returns:
[str]: A list of all known audio streams, each item is in the
format: ``<index>:<language>:<name>:<codec>:<active>``
"""
return map(str, self._get_player_interface().ListAudio())
@_check_player_is_active
def list_subtitles(self):
"""
Returns:
[str]: A list of all known subtitles, each item is in the
format: ``<index>:<language>:<name>:<codec>:<active>``
"""
return map(str, self._get_player_interface().ListSubtitles())
@_check_player_is_active
def action(self, code):
"""
Executes a keyboard command via a code
Args:
code (int): The key code you wish to emulate
refer to ``keys.py`` for the possible keys
Returns:
None:
"""
self._get_player_interface().Action(code)
@_check_player_is_active
def is_playing(self):
"""
Returns:
bool: Whether the player is playing
"""
self._is_playing = (self.playback_status() == "Playing")
logger.info("Playing?: %s" % self._is_playing)
return self._is_playing
@_check_player_is_active
def play_sync(self):
"""
Returns:
None:
"""
self.play()
logger.info("Playing synchronously")
try:
time.sleep(0.05)
logger.debug("Wait for playing to start")
while self.is_playing():
time.sleep(0.05)
except DBusException:
logger.error(
"Cannot play synchronously any longer as DBus calls timed out."
)
@_check_player_is_active
def play(self):
"""
Returns:
None:
"""
if not self.is_playing():
self.play_pause()
self._is_playing = True
self.playEvent(self)
def _get_root_interface(self):
return self._connection.root_interface
def _get_player_interface(self):
return self._connection.player_interface
def _get_properties_interface(self):
return self._connection.properties_interface
def quit(self):
try:
logger.debug('Quitting OMXPlayer')
process_group_id = os.getpgid(self._process.pid)
os.killpg(process_group_id, signal.SIGTERM)
logger.debug('SIGTERM Sent to pid: %s' % process_group_id)
self._process_monitor.join()
except OSError:
logger.error('Could not find the process to kill')
self._process = None
self._process = None
@_check_player_is_active
def get_source(self):
"""
Returns:
str: source currently playing
"""
return self._source
# For backward compatibility
@_check_player_is_active
def get_filename(self):
"""
Returns:
str: source currently playing
.. deprecated:: 0.2.0
Use: :func:`get_source` instead.
"""
return self.get_source()
# MediaPlayer2.Player types:
# Track_Id: DBus ID of track
# Plaback_Rate: Multiplier for playback speed (1 = normal speed)
# Volume: 0--1, 0 is muted and 1 is full volume
# Time_In_Us: Time in microseconds
# Playback_Status: Playing|Paused|Stopped
# Loop_Status: None|Track|Playlist