| 1 |
# Copyright (C) 2008-2010 Adam Olsen |
| 2 |
# Copyright (C) 2014-2015 Dustin Spicuzza |
| 3 |
# |
| 4 |
# This program is free software; you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation; either version 2, or (at your option) |
| 7 |
# any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
# |
| 18 |
# |
| 19 |
# The developers of the Exaile media player hereby grant permission |
| 20 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 21 |
# distributed together with GStreamer and Exaile. This permission is |
| 22 |
# above and beyond the permissions granted by the GPL license by which |
| 23 |
# Exaile is covered. If you modify this code, you may extend this |
| 24 |
# exception to your version of the code, but you are not obligated to |
| 25 |
# do so. If you do not wish to do so, delete this exception statement |
| 26 |
# from your version. |
| 27 |
|
| 28 |
|
| 29 |
import time |
| 30 |
|
| 31 |
from gi.repository import GLib |
| 32 |
|
| 33 |
from xl import common |
| 34 |
from xl import event |
| 35 |
from xl import settings |
| 36 |
|
| 37 |
import logging |
| 38 |
|
| 39 |
logger = logging.getLogger(__name__) |
| 40 |
|
| 41 |
|
| 42 |
class ExailePlayer: |
| 43 |
""" |
| 44 |
This is the player object that everything in Exaile interacts with |
| 45 |
to control audio playback. The player object controls a playback |
| 46 |
engine, which actually controls audio playback. Nothing in this |
| 47 |
object should be specific to a particular engine. Examples of engines |
| 48 |
could be GStreamer, Xine, etc. Currently only the GStreamer engine |
| 49 |
is actually implemented. |
| 50 |
|
| 51 |
All public functions are assumed to be called from the Glib main |
| 52 |
thread, or bad things will happen. This includes most engine functions, |
| 53 |
with one or two noted exceptions. |
| 54 |
""" |
| 55 |
|
| 56 |
def __init__(self, name, disable_autoswitch=False): |
| 57 |
self.queue = None |
| 58 |
self._name = name |
| 59 |
|
| 60 |
self._playtime_stamp = None |
| 61 |
|
| 62 |
self._delay_id = None |
| 63 |
self._stop_id = None |
| 64 |
self._engine = None |
| 65 |
|
| 66 |
self._auto_advance_delay = 0 |
| 67 |
self._auto_advance = True |
| 68 |
self._gapless_enabled = True |
| 69 |
self.__volume = 1.0 |
| 70 |
|
| 71 |
options = { |
| 72 |
'%s/auto_advance_delay' % name: '_auto_advance_delay', |
| 73 |
'%s/auto_advance' % name: '_auto_advance', |
| 74 |
'%s/gapless_playback' % name: '_gapless_enabled', |
| 75 |
'%s/volume' % name: '_volume', |
| 76 |
} |
| 77 |
|
| 78 |
self._settings_unsub = common.subscribe_for_settings(name, options, self) |
| 79 |
|
| 80 |
self._setup_engine(disable_autoswitch) |
| 81 |
|
| 82 |
event.add_callback(self._on_track_end, 'playback_track_end', self) |
| 83 |
event.add_callback(self._on_track_tags_changed, 'track_tags_changed') |
| 84 |
|
| 85 |
def _setup_engine(self, disable_autoswitch): |
| 86 |
if self._engine is not None: |
| 87 |
self._engine.destroy() |
| 88 |
|
| 89 |
# allows building docs |
| 90 |
engine_name = settings.get_option("player/engine", "normal") |
| 91 |
|
| 92 |
if engine_name == 'rtfd_hack': |
| 93 |
return None |
| 94 |
|
| 95 |
# TODO: support other engines |
| 96 |
from .gst.engine import ExaileGstEngine |
| 97 |
|
| 98 |
self._engine = ExaileGstEngine(self._name, self, disable_autoswitch) |
| 99 |
self._engine.initialize() |
| 100 |
|
| 101 |
@property |
| 102 |
def _volume(self): |
| 103 |
return self.__volume |
| 104 |
|
| 105 |
@_volume.setter |
| 106 |
def _volume(self, value): |
| 107 |
self.__volume = value |
| 108 |
if self._engine is not None: |
| 109 |
self.engine_load_volume() |
| 110 |
|
| 111 |
def _on_track_end(self, name, obj, track): |
| 112 |
if not track: |
| 113 |
return |
| 114 |
try: |
| 115 |
i = int(track.get_tag_raw('__playcount')) |
| 116 |
except Exception: |
| 117 |
i = 0 |
| 118 |
track.set_tags(__playcount=i + 1, __last_played=time.time()) |
| 119 |
|
| 120 |
@common.idle_add() |
| 121 |
def _on_track_tags_changed(self, eventtype, track, tags): |
| 122 |
if '__stopoffset' in tags: |
| 123 |
self._engine.on_track_stopoffset_changed(track) |
| 124 |
|
| 125 |
def destroy(self): |
| 126 |
""" |
| 127 |
Destroys the engine and other resources for this player object. |
| 128 |
Unless you own the object, you probably should never call this. |
| 129 |
""" |
| 130 |
if self._settings_unsub is not None: |
| 131 |
self._settings_unsub() |
| 132 |
self._settings_unsub = None |
| 133 |
|
| 134 |
if self._engine is not None: |
| 135 |
self._engine.destroy() |
| 136 |
self._engine = None |
| 137 |
|
| 138 |
def get_volume(self): |
| 139 |
""" |
| 140 |
Gets the current user volume |
| 141 |
|
| 142 |
:returns: the volume percentage |
| 143 |
:type: int |
| 144 |
""" |
| 145 |
return self._volume * 100 |
| 146 |
|
| 147 |
def set_volume(self, volume): |
| 148 |
""" |
| 149 |
Sets the current user volume |
| 150 |
|
| 151 |
:param volume: the volume percentage |
| 152 |
:type volume: int |
| 153 |
""" |
| 154 |
volume = common.clamp(volume, 0, 100) |
| 155 |
settings.set_option("%s/volume" % self._name, volume / 100) |
| 156 |
|
| 157 |
def modify_volume(self, diff): |
| 158 |
""" |
| 159 |
Changes the current user volume |
| 160 |
|
| 161 |
:param diff: the volume difference (pos or neg) percentage units |
| 162 |
:type volume: int |
| 163 |
""" |
| 164 |
v = self.get_volume() |
| 165 |
self.set_volume(v + diff) |
| 166 |
|
| 167 |
@property |
| 168 |
def current(self): |
| 169 |
return self._engine.get_current_track() |
| 170 |
|
| 171 |
def play(self, track, start_at=None, paused=False): |
| 172 |
""" |
| 173 |
Starts the playback with the provided track |
| 174 |
or stops the playback it immediately if none |
| 175 |
|
| 176 |
:param track: the track to play or None |
| 177 |
:type track: :class:`xl.trax.Track` |
| 178 |
:param start_at: The offset to start playback at, in seconds |
| 179 |
:param paused: If True, start the track in 'paused' mode |
| 180 |
|
| 181 |
.. note:: The following :doc:`events </xl/event>` will be emitted by this method: |
| 182 |
|
| 183 |
* `playback_player_start`: indicates the start of playback overall |
| 184 |
* `playback_track_start`: indicates playback start of a track |
| 185 |
""" |
| 186 |
if track is None: |
| 187 |
self.stop() |
| 188 |
else: |
| 189 |
if self.is_stopped(): |
| 190 |
event.log_event('playback_player_start', self, track) |
| 191 |
|
| 192 |
play_args = self._get_play_params(track, start_at, paused, False, False) |
| 193 |
self._engine.play(*play_args) |
| 194 |
if play_args[2]: |
| 195 |
event.log_event('playback_player_pause', self, track) |
| 196 |
event.log_event("playback_toggle_pause", self, track) |
| 197 |
|
| 198 |
def stop(self): |
| 199 |
""" |
| 200 |
Stops the playback |
| 201 |
|
| 202 |
.. note:: The following :doc:`events </xl/event>` will be emitted by this method: |
| 203 |
|
| 204 |
* `playback_player_end`: indicates the end of playback overall |
| 205 |
* `playback_track_end`: indicates playback end of a track |
| 206 |
""" |
| 207 |
state = self.get_state() |
| 208 |
|
| 209 |
if state == 'playing' or state == 'paused': |
| 210 |
self._engine.stop() |
| 211 |
return True |
| 212 |
else: |
| 213 |
logger.debug("Stop ignored when state == %s", state) |
| 214 |
|
| 215 |
return False |
| 216 |
|
| 217 |
def pause(self): |
| 218 |
""" |
| 219 |
Pauses the playback if playing, does not toggle it |
| 220 |
|
| 221 |
:returns: True if paused, False otherwise |
| 222 |
|
| 223 |
.. note:: The following :doc:`events </xl/event>` will be emitted by this method: |
| 224 |
|
| 225 |
* `playback_player_pause`: indicates that the playback has been paused |
| 226 |
* `playback_toggle_pause`: indicates that the playback has been paused or resumed |
| 227 |
""" |
| 228 |
self._cancel_delayed_start() |
| 229 |
if self.is_playing(): |
| 230 |
current = self.current |
| 231 |
self._update_playtime(current) |
| 232 |
self._engine.pause() |
| 233 |
self._reset_playtime_stamp() |
| 234 |
|
| 235 |
event.log_event('playback_player_pause', self, current) |
| 236 |
event.log_event("playback_toggle_pause", self, current) |
| 237 |
return True |
| 238 |
|
| 239 |
return False |
| 240 |
|
| 241 |
def unpause(self): |
| 242 |
""" |
| 243 |
Resumes the playback if it is paused, does not toggle it |
| 244 |
|
| 245 |
:returns: True if paused, False otherwise |
| 246 |
|
| 247 |
.. note:: The following :doc:`events </xl/event>` will be emitted by this method: |
| 248 |
|
| 249 |
* `playback_player_resume`: indicates that the playback has been resumed |
| 250 |
* `playback_toggle_pause`: indicates that the playback has been paused or resumed |
| 251 |
""" |
| 252 |
self._cancel_delayed_start() |
| 253 |
if self.is_paused(): |
| 254 |
self._reset_playtime_stamp() |
| 255 |
self._engine.unpause() |
| 256 |
|
| 257 |
current = self.current |
| 258 |
event.log_event('playback_player_resume', self, current) |
| 259 |
event.log_event("playback_toggle_pause", self, current) |
| 260 |
return True |
| 261 |
return False |
| 262 |
|
| 263 |
def toggle_pause(self): |
| 264 |
""" |
| 265 |
Toggles between playing and paused state. Only valid when playback |
| 266 |
is not stopped. |
| 267 |
|
| 268 |
:returns: True if toggled, false otherwise |
| 269 |
|
| 270 |
.. note:: The following :doc:`events </xl/event>` will be emitted by this method: |
| 271 |
|
| 272 |
* `playback_toggle_pause`: indicates that the playback has been paused or resumed |
| 273 |
""" |
| 274 |
state = self.get_state() |
| 275 |
if state == 'paused': |
| 276 |
return self.unpause() |
| 277 |
elif state == 'playing': |
| 278 |
return self.pause() |
| 279 |
else: |
| 280 |
return False |
| 281 |
|
| 282 |
def seek(self, value): |
| 283 |
""" |
| 284 |
Seek to a position in the currently playing stream |
| 285 |
|
| 286 |
:param value: the position in seconds |
| 287 |
:type value: int |
| 288 |
""" |
| 289 |
if self._engine.seek(value): |
| 290 |
event.log_event('playback_seeked', self, value) |
| 291 |
|
| 292 |
def get_position(self): |
| 293 |
""" |
| 294 |
Gets the current playback position of the playing track |
| 295 |
|
| 296 |
:returns: the playback position in nanoseconds |
| 297 |
:rtype: int |
| 298 |
""" |
| 299 |
return self._engine.get_position() |
| 300 |
|
| 301 |
def get_time(self): |
| 302 |
""" |
| 303 |
Gets the current playback time |
| 304 |
|
| 305 |
:returns: the playback time in seconds |
| 306 |
:rtype: float |
| 307 |
""" |
| 308 |
return self.get_position() / 1e9 |
| 309 |
|
| 310 |
def get_progress(self) -> float: |
| 311 |
""" |
| 312 |
Gets the current playback progress |
| 313 |
|
| 314 |
:returns: the playback progress as [0..1] |
| 315 |
""" |
| 316 |
try: |
| 317 |
progress = self.get_time() / self.current.get_tag_raw("__length") |
| 318 |
except TypeError: # track doesn't have duration info |
| 319 |
progress = 0 |
| 320 |
except AttributeError: # no current track |
| 321 |
progress = 0 |
| 322 |
else: |
| 323 |
if progress < 0: |
| 324 |
progress = 0 |
| 325 |
elif progress > 1: |
| 326 |
progress = 1 |
| 327 |
return progress |
| 328 |
|
| 329 |
def set_progress(self, progress): |
| 330 |
""" |
| 331 |
Seeks to the progress position |
| 332 |
|
| 333 |
:param progress: value ranged at [0..1] |
| 334 |
:type progress: float |
| 335 |
""" |
| 336 |
seek_position = 0 |
| 337 |
|
| 338 |
try: |
| 339 |
length = self.current.get_tag_raw('__length') |
| 340 |
seek_position = length * progress |
| 341 |
except (TypeError, AttributeError): |
| 342 |
pass |
| 343 |
|
| 344 |
self.seek(seek_position) |
| 345 |
|
| 346 |
def modify_time(self, diff): |
| 347 |
""" |
| 348 |
Modifies the current position backwards or forwards. |
| 349 |
|
| 350 |
If position ends up after the end or before the start of the track |
| 351 |
it is truncated to lay inside the track. |
| 352 |
|
| 353 |
:param diff: value in seconds |
| 354 |
:type diff: int |
| 355 |
""" |
| 356 |
try: |
| 357 |
length = self.current.get_tag_raw('__length') |
| 358 |
except (TypeError, AttributeError): |
| 359 |
return |
| 360 |
|
| 361 |
if length is None: |
| 362 |
return |
| 363 |
|
| 364 |
pos = self.get_time() |
| 365 |
seek_pos = pos + diff |
| 366 |
|
| 367 |
# Make sure we don't seek outside the current track. Subtract a little |
| 368 |
# from length, player seems to hang sometimes if we seek to the end of |
| 369 |
# a track. |
| 370 |
seek_pos = max(0, min(seek_pos, length - 2)) |
| 371 |
|
| 372 |
self.seek(seek_pos) |
| 373 |
|
| 374 |
def get_state(self): |
| 375 |
""" |
| 376 |
Gets the player state |
| 377 |
|
| 378 |
:returns: one of *playing*, *paused* or *stopped* |
| 379 |
:rtype: string |
| 380 |
""" |
| 381 |
return self._engine.get_state() |
| 382 |
|
| 383 |
def is_playing(self): |
| 384 |
""" |
| 385 |
Convenience method to find out if the player is currently playing |
| 386 |
|
| 387 |
:returns: whether the player is currently playing |
| 388 |
:rtype: bool |
| 389 |
""" |
| 390 |
return self._engine.get_state() == 'playing' |
| 391 |
|
| 392 |
def is_paused(self): |
| 393 |
""" |
| 394 |
Convenience method to find out if the player is currently paused |
| 395 |
|
| 396 |
:returns: whether the player is currently paused |
| 397 |
:rtype: bool |
| 398 |
""" |
| 399 |
return self._engine.get_state() == 'paused' |
| 400 |
|
| 401 |
def is_stopped(self): |
| 402 |
""" |
| 403 |
Convenience method to find out if the player is currently stopped |
| 404 |
|
| 405 |
:returns: whether the player is currently stopped |
| 406 |
:rtype: bool |
| 407 |
""" |
| 408 |
return self._engine.get_state() == 'stopped' |
| 409 |
|
| 410 |
# |
| 411 |
# Engine-only API |
| 412 |
# |
| 413 |
|
| 414 |
def engine_load_volume(self): |
| 415 |
""" |
| 416 |
Load volume from settings, this function will call set_volume |
| 417 |
on the engine |
| 418 |
|
| 419 |
.. note:: Only to be called from engine |
| 420 |
""" |
| 421 |
self._engine.set_volume(self._volume) |
| 422 |
|
| 423 |
def engine_notify_user_volume_change(self, vol): |
| 424 |
""" |
| 425 |
Engine calls this when something inside the engine changes |
| 426 |
the user volume. |
| 427 |
|
| 428 |
.. note:: Only to be called from engine |
| 429 |
""" |
| 430 |
settings.set_option('%s/volume' % self._name, vol) |
| 431 |
|
| 432 |
def engine_notify_track_start(self, track): |
| 433 |
""" |
| 434 |
Called when a track has just entered the playing state |
| 435 |
|
| 436 |
:param track: Track that is being played now |
| 437 |
|
| 438 |
.. note:: Only to be called from engine |
| 439 |
""" |
| 440 |
|
| 441 |
self._reset_playtime_stamp() |
| 442 |
event.log_event('playback_track_start', self, track) |
| 443 |
|
| 444 |
def engine_notify_track_end(self, track, done): |
| 445 |
""" |
| 446 |
Called when a track has been stopped. Either: |
| 447 |
|
| 448 |
- stop() was called |
| 449 |
- play() was called and the prior track was stopped |
| 450 |
|
| 451 |
:param track: Must be the track that was just playing, and must |
| 452 |
never be None |
| 453 |
|
| 454 |
:param done: If True, no further tracks will be played |
| 455 |
|
| 456 |
.. note:: Only to be called from engine |
| 457 |
""" |
| 458 |
|
| 459 |
self._update_playtime(track) |
| 460 |
event.log_event('playback_track_end', self, track) |
| 461 |
|
| 462 |
if done: |
| 463 |
self._cancel_delayed_start() |
| 464 |
event.log_event('playback_player_end', self, track) |
| 465 |
|
| 466 |
@common.idle_add() |
| 467 |
def engine_notify_error(self, msg): |
| 468 |
""" |
| 469 |
Notification that some kind of error has occurred. If the error |
| 470 |
is not recoverable, the engine is expected to stop playback and |
| 471 |
reset itself to a state where playback can begin again. |
| 472 |
|
| 473 |
.. note:: Only to be called from engine |
| 474 |
""" |
| 475 |
event.log_event('playback_error', self, msg) |
| 476 |
|
| 477 |
def engine_autoadvance_get_next_track(self, gapless=False): |
| 478 |
""" |
| 479 |
Engine calls this when it wants to see what the next track |
| 480 |
to play is. The track may or may not actually get played. |
| 481 |
If the track gets played, then the engine must call |
| 482 |
engine_autoadvance_notify_next, engine_notify_playback_stop, |
| 483 |
and engine_notify_playback_start |
| 484 |
|
| 485 |
May be called on another thread. |
| 486 |
|
| 487 |
:param gapless: Set to True if the autoadvance is part of a gapless |
| 488 |
playback attempt |
| 489 |
|
| 490 |
:returns: The next track, or None if the engine should not try |
| 491 |
to play another track immediately |
| 492 |
|
| 493 |
.. note:: Only to be called from engine |
| 494 |
""" |
| 495 |
|
| 496 |
if not self._auto_advance: |
| 497 |
return |
| 498 |
|
| 499 |
if gapless: |
| 500 |
if self._auto_advance_delay != 0 or not self._gapless_enabled: |
| 501 |
return |
| 502 |
|
| 503 |
return self.queue.get_next() |
| 504 |
|
| 505 |
def engine_autoadvance_notify_next(self, buffered_track): |
| 506 |
""" |
| 507 |
Engine calls this when preparing to play the next track as |
| 508 |
part of an auto advance action. |
| 509 |
|
| 510 |
:param track: The track that will be played |
| 511 |
|
| 512 |
:returns: (track, start_at, paused) parameters that can be passed |
| 513 |
to the 'play' function of the engine. |
| 514 |
|
| 515 |
.. note:: Only to be called from engine |
| 516 |
""" |
| 517 |
|
| 518 |
next = self.queue.next(autoplay=False) |
| 519 |
|
| 520 |
stopped = bool(next is None) |
| 521 |
|
| 522 |
return self._get_play_params(buffered_track, None, False, stopped, True) |
| 523 |
|
| 524 |
# |
| 525 |
# Playtime related stuffs |
| 526 |
# |
| 527 |
|
| 528 |
def _get_play_params(self, track, start_at, paused, stopped, autoadvance): |
| 529 |
if start_at is None or start_at <= 0: |
| 530 |
start_at = None |
| 531 |
start_offset = track.get_tag_raw('__startoffset') or 0 |
| 532 |
if start_offset > 0: |
| 533 |
start_at = start_offset |
| 534 |
|
| 535 |
# Once playback has started, if there's a delay, pause the stream |
| 536 |
# for delay number of seconds |
| 537 |
self._cancel_delayed_start() |
| 538 |
|
| 539 |
if not paused and autoadvance and self._auto_advance_delay > 0: |
| 540 |
delay = int(self._auto_advance_delay) |
| 541 |
logger.debug("Delaying start for %sms", delay) |
| 542 |
self._delay_id = GLib.timeout_add(delay, self._delayed_start) |
| 543 |
paused = True |
| 544 |
|
| 545 |
return track, start_at, paused, stopped |
| 546 |
|
| 547 |
def _delayed_start(self): |
| 548 |
logger.debug("Resuming playback after delayed start") |
| 549 |
self.unpause() |
| 550 |
|
| 551 |
def _update_playtime(self, track): |
| 552 |
""" |
| 553 |
updates the total playtime for the currently playing track |
| 554 |
|
| 555 |
.. should be called whenever a pause/stop event occurs |
| 556 |
""" |
| 557 |
if track and self._playtime_stamp: |
| 558 |
last = track.get_tag_raw('__playtime') |
| 559 |
if isinstance(last, str): |
| 560 |
try: |
| 561 |
last = int(last) |
| 562 |
except Exception: |
| 563 |
last = 0 |
| 564 |
elif not isinstance(last, int): |
| 565 |
last = 0 |
| 566 |
track.set_tag_raw( |
| 567 |
'__playtime', last + int(time.time() - self._playtime_stamp) |
| 568 |
) |
| 569 |
self._playtime_stamp = None |
| 570 |
|
| 571 |
def _reset_playtime_stamp(self): |
| 572 |
self._playtime_stamp = int(time.time()) |
| 573 |
|
| 574 |
# |
| 575 |
# Delayed start stuff |
| 576 |
# |
| 577 |
|
| 578 |
def _cancel_delayed_start(self): |
| 579 |
if self._delay_id is not None: |
| 580 |
GLib.source_remove(self._delay_id) |
| 581 |
self._delay_id = None |