| 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 |
from gi.repository import GLib |
| 29 |
from gi.repository import Gst |
| 30 |
|
| 31 |
import logging |
| 32 |
import os |
| 33 |
import urllib.parse |
| 34 |
import copy |
| 35 |
import time |
| 36 |
|
| 37 |
from xl import common |
| 38 |
from xl import event |
| 39 |
|
| 40 |
from xl.nls import gettext as _ |
| 41 |
|
| 42 |
from . import gst_utils |
| 43 |
from .dynamic_sink import DynamicAudioSink |
| 44 |
from .sink import create_device, priority_boost |
| 45 |
|
| 46 |
from xl.player.engine import ExaileEngine |
| 47 |
from xl.player.track_fader import TrackFader |
| 48 |
from xl.player.gst import missing_plugin |
| 49 |
|
| 50 |
|
| 51 |
logger = logging.getLogger(__name__) |
| 52 |
|
| 53 |
|
| 54 |
class ExaileGstEngine(ExaileEngine): |
| 55 |
""" |
| 56 |
Super shiny GStreamer-based engine that does all the things! |
| 57 |
|
| 58 |
* Audio plugins to modify the output stream |
| 59 |
* gapless playback |
| 60 |
* crossfading (requires gst-plugins-bad) |
| 61 |
* Dynamic audio device switching at runtime |
| 62 |
|
| 63 |
Notes about crossfading: |
| 64 |
|
| 65 |
The big change from previous attempts at crossfading is that unlike |
| 66 |
the former unified engine, this tries to depend solely on the playbin |
| 67 |
element to play audio files. The reason for this is that playbin |
| 68 |
is 5000+ lines of battle-hardened C code that handles all of the |
| 69 |
weird edges cases in gstreamer, and we don't wish to duplicate that in |
| 70 |
Exaile if we can avoid it. |
| 71 |
|
| 72 |
Instead, we can use multiple playbin instances that have duplicate |
| 73 |
output audio devices. This makes crossfading a significantly simpler |
| 74 |
proposition. |
| 75 |
|
| 76 |
There are two modes for this thing: |
| 77 |
|
| 78 |
* One is normal/gapless mode (no crossfade), and it uses a normal |
| 79 |
playbin element and controls that directly. The playbin is wrapped |
| 80 |
by the AudioStream object, and it's audio sink is a DynamicAudioSink |
| 81 |
element with the |
| 82 |
|
| 83 |
* The other is crossfading mode (which requires gst-plugins-bad to be |
| 84 |
installed). Create multiple AudioStream objects, and they have a |
| 85 |
DynamicAudioSink object hooked up to an interaudiosink. |
| 86 |
|
| 87 |
You can register plugins to modify the output audio via the following |
| 88 |
providers: |
| 89 |
|
| 90 |
* gst_audio_filter: Multiple instances of this can be created, as they |
| 91 |
get applied to each stream. It is recommended that |
| 92 |
plugins inherit from :class:`.ElementBin` |
| 93 |
""" |
| 94 |
|
| 95 |
def __init__(self, name, player, disable_autoswitch): |
| 96 |
ExaileEngine.__init__(self, name, player) |
| 97 |
self.logger = logging.getLogger('%s [%s]' % (__name__, name)) |
| 98 |
|
| 99 |
# Default settings |
| 100 |
self.crossfade_enabled = False |
| 101 |
self.crossfade_duration = 3000 |
| 102 |
|
| 103 |
self.audiosink_device = None |
| 104 |
self.audiosink = None |
| 105 |
self.custom_sink_pipe = None |
| 106 |
|
| 107 |
# If True, then playback will be stopped if the audio sink changes without |
| 108 |
# the user asking for it |
| 109 |
self.disable_autoswitch = disable_autoswitch |
| 110 |
|
| 111 |
# This means to fade in when the user plays a track, only enabled |
| 112 |
# when crossfade isn't enabled |
| 113 |
self.user_fade_enabled = False |
| 114 |
self.user_fade_duration = 1000 |
| 115 |
|
| 116 |
# Key: option name; value: attribute on self |
| 117 |
options = { |
| 118 |
'%s/crossfading' % self.name: 'crossfade_enabled', |
| 119 |
'%s/crossfade_duration' % self.name: 'crossfade_duration', |
| 120 |
'%s/audiosink_device' % self.name: 'audiosink_device', |
| 121 |
'%s/audiosink' % self.name: 'audiosink', |
| 122 |
'%s/custom_sink_pipe' % self.name: 'custom_sink_pipe', |
| 123 |
'%s/user_fade_enabled' % self.name: 'user_fade_enabled', |
| 124 |
'%s/user_fade' % self.name: 'user_fade_duration', |
| 125 |
} |
| 126 |
|
| 127 |
self.settings_unsubscribe = common.subscribe_for_settings( |
| 128 |
self.name, options, self |
| 129 |
) |
| 130 |
|
| 131 |
# |
| 132 |
# Dynamic properties |
| 133 |
# |
| 134 |
|
| 135 |
def __setattr__(self, name, value): |
| 136 |
object.__setattr__(self, name, value) |
| 137 |
|
| 138 |
if not getattr(self, 'initialized', False): |
| 139 |
return |
| 140 |
|
| 141 |
if name in ['crossfade_enabled', 'crossfade_duration']: |
| 142 |
self._reconfigure_crossfader() |
| 143 |
|
| 144 |
if name in ['audiosink_device', 'audiosink', 'custom_sink_pipe']: |
| 145 |
self._reconfigure_sink() |
| 146 |
|
| 147 |
# |
| 148 |
# API |
| 149 |
# |
| 150 |
|
| 151 |
def initialize(self): |
| 152 |
object.__setattr__(self, 'initialized', True) |
| 153 |
|
| 154 |
self.main_stream = AudioStream(self) |
| 155 |
self.other_stream = None |
| 156 |
self.crossfade_out = None |
| 157 |
|
| 158 |
self.player.engine_load_volume() |
| 159 |
|
| 160 |
self._reconfigure_crossfader() |
| 161 |
|
| 162 |
def _reconfigure_crossfader(self): |
| 163 |
self.logger.info("Reconfiguring crossfading") |
| 164 |
|
| 165 |
cf_duration = None |
| 166 |
if self.crossfade_enabled: |
| 167 |
cf_duration = self.crossfade_duration / 1000.0 |
| 168 |
|
| 169 |
if self.other_stream is None: |
| 170 |
self.other_stream = AudioStream(self) |
| 171 |
self.other_stream.set_user_volume(self.main_stream.get_user_volume()) |
| 172 |
|
| 173 |
self.other_stream.reconfigure_fader(cf_duration, cf_duration) |
| 174 |
self.logger.info("Crossfade: enabled (%sms)", self.crossfade_duration) |
| 175 |
else: |
| 176 |
self.logger.info("Crossfade: disabled") |
| 177 |
if self.other_stream is not None: |
| 178 |
self.other_stream.destroy() |
| 179 |
|
| 180 |
self.main_stream.reconfigure_fader(cf_duration, cf_duration) |
| 181 |
|
| 182 |
def _reconfigure_sink(self): |
| 183 |
self.logger.info("Reconfiguring audiosinks") |
| 184 |
|
| 185 |
self.main_stream.reconfigure_sink() |
| 186 |
if self.other_stream is not None: |
| 187 |
self.other_stream.reconfigure_sink() |
| 188 |
|
| 189 |
def destroy(self, permanent=True): |
| 190 |
self.main_stream.destroy() |
| 191 |
|
| 192 |
if self.other_stream is not None: |
| 193 |
self.other_stream.destroy() |
| 194 |
|
| 195 |
if permanent: |
| 196 |
self.settings_unsubscribe() |
| 197 |
|
| 198 |
object.__setattr__(self, 'initialized', False) |
| 199 |
|
| 200 |
self.needs_sink = True |
| 201 |
self.audiosink = None |
| 202 |
self.audiosink_device = None |
| 203 |
|
| 204 |
# |
| 205 |
# Engine API |
| 206 |
# |
| 207 |
|
| 208 |
def get_current_track(self): |
| 209 |
return self.main_stream.current_track |
| 210 |
|
| 211 |
def get_position(self): |
| 212 |
return self.main_stream.get_position() |
| 213 |
|
| 214 |
def get_state(self): |
| 215 |
state = self.main_stream.get_gst_state() |
| 216 |
if state == Gst.State.PLAYING: |
| 217 |
return 'playing' |
| 218 |
elif state == Gst.State.PAUSED: |
| 219 |
return 'paused' |
| 220 |
else: |
| 221 |
return 'stopped' |
| 222 |
|
| 223 |
def get_volume(self): |
| 224 |
return self.main_stream.get_user_volume() |
| 225 |
|
| 226 |
def on_track_stopoffset_changed(self, track): |
| 227 |
for stream in [self.main_stream, self.other_stream]: |
| 228 |
if stream is None or stream.current_track != track: |
| 229 |
continue |
| 230 |
|
| 231 |
# The fader executes the stop offset, so reconfigure it |
| 232 |
|
| 233 |
if self.crossfade_enabled: |
| 234 |
stream.reconfigure_fader( |
| 235 |
self.crossfade_duration, self.crossfade_duration |
| 236 |
) |
| 237 |
else: |
| 238 |
stream.reconfigure_fader(None, None) |
| 239 |
|
| 240 |
def pause(self): |
| 241 |
self.main_stream.pause() |
| 242 |
|
| 243 |
if self.other_stream is not None: |
| 244 |
self.other_stream.stop() |
| 245 |
|
| 246 |
def play(self, track, start_at, paused, stopped): |
| 247 |
self._next_track(track, start_at, paused, stopped, False, False) |
| 248 |
|
| 249 |
def seek(self, value): |
| 250 |
return self.main_stream.seek(value) |
| 251 |
|
| 252 |
def set_volume(self, volume): |
| 253 |
self.main_stream.set_user_volume(volume) |
| 254 |
if self.other_stream is not None: |
| 255 |
self.other_stream.set_user_volume(volume) |
| 256 |
|
| 257 |
def stop(self): |
| 258 |
if self.other_stream is not None: |
| 259 |
self.other_stream.stop() |
| 260 |
|
| 261 |
prior_track = self.main_stream.stop(emit_eos=False) |
| 262 |
self.player.engine_notify_track_end(prior_track, True) |
| 263 |
|
| 264 |
def unpause(self): |
| 265 |
self.main_stream.unpause() |
| 266 |
|
| 267 |
# |
| 268 |
# Engine private functions |
| 269 |
# |
| 270 |
|
| 271 |
def _autoadvance_track(self, still_fading=False): |
| 272 |
track = self.player.engine_autoadvance_get_next_track() |
| 273 |
|
| 274 |
if track: |
| 275 |
play_args = self.player.engine_autoadvance_notify_next(track) + ( |
| 276 |
False, |
| 277 |
True, |
| 278 |
) |
| 279 |
self._next_track(*play_args) |
| 280 |
|
| 281 |
# If still fading, don't stop |
| 282 |
elif not still_fading: |
| 283 |
self.stop() |
| 284 |
|
| 285 |
@common.idle_add() |
| 286 |
def _eos_func(self, stream): |
| 287 |
if stream == self.main_stream: |
| 288 |
self._autoadvance_track() |
| 289 |
|
| 290 |
def _error_func(self, stream, msg): |
| 291 |
# Destroy the streams, and create a new one, just in case |
| 292 |
|
| 293 |
self.player.engine_notify_error(msg) |
| 294 |
self.destroy(permanent=False) |
| 295 |
self.initialize() |
| 296 |
|
| 297 |
def _next_track( |
| 298 |
self, track, start_at, paused, stopped, already_queued, autoadvance |
| 299 |
): |
| 300 |
prior_track = self.main_stream.current_track |
| 301 |
|
| 302 |
# Notify that the track is done |
| 303 |
if prior_track is not None: |
| 304 |
self.player.engine_notify_track_end(prior_track, False) |
| 305 |
|
| 306 |
if self.crossfade_enabled: |
| 307 |
self.main_stream, self.other_stream = self.other_stream, self.main_stream |
| 308 |
self.main_stream.play( |
| 309 |
track, |
| 310 |
start_at, |
| 311 |
paused, |
| 312 |
stopped, |
| 313 |
already_queued, |
| 314 |
self.crossfade_duration / 1000.0, |
| 315 |
self.crossfade_duration / 1000.0, |
| 316 |
) |
| 317 |
self.other_stream.fader.fade_out_on_play() |
| 318 |
elif self.user_fade_enabled and not autoadvance: |
| 319 |
self.main_stream.play( |
| 320 |
track, |
| 321 |
start_at, |
| 322 |
paused, |
| 323 |
stopped, |
| 324 |
already_queued, |
| 325 |
self.user_fade_duration / 1000.0, |
| 326 |
) |
| 327 |
else: |
| 328 |
self.main_stream.play(track, start_at, paused, stopped, already_queued) |
| 329 |
|
| 330 |
if stopped: |
| 331 |
event.log_event('playlist_track_next', self.player, track) |
| 332 |
else: |
| 333 |
self.player.engine_notify_track_start(track) |
| 334 |
|
| 335 |
|
| 336 |
class AudioStream: |
| 337 |
""" |
| 338 |
An object that can play one or more tracks |
| 339 |
""" |
| 340 |
|
| 341 |
idx = 0 |
| 342 |
|
| 343 |
def __init__(self, engine): |
| 344 |
AudioStream.idx += 1 |
| 345 |
self.name = '%s-audiostream-%s' % ( |
| 346 |
engine.name, |
| 347 |
str(self.idx) + '-' + str(time.time()), |
| 348 |
) |
| 349 |
self.engine = engine |
| 350 |
|
| 351 |
self.logger = logging.getLogger( |
| 352 |
'%s [%s-a%s]' % (__name__, engine.name, self.idx) |
| 353 |
) |
| 354 |
|
| 355 |
# track being played by this stream |
| 356 |
self.current_track = None |
| 357 |
self.buffered_track = None |
| 358 |
|
| 359 |
# This exists because if there is a sink error, it doesn't |
| 360 |
# really make sense to recreate the sink -- it'll just fail |
| 361 |
# again. Instead, wait for the user to try to play a track, |
| 362 |
# and maybe the issue has resolved itself (plugged device in?) |
| 363 |
self.needs_sink = True |
| 364 |
self.last_position = 0 |
| 365 |
|
| 366 |
self.audio_filters = gst_utils.ProviderBin( |
| 367 |
'gst_audio_filter', '%s-filters' % self.name |
| 368 |
) |
| 369 |
|
| 370 |
self.playbin = Gst.ElementFactory.make("playbin", "%s-playbin" % self.name) |
| 371 |
if self.playbin is None: |
| 372 |
raise TypeError("gstreamer 1.x base plugins not installed!") |
| 373 |
|
| 374 |
gst_utils.disable_video_text(self.playbin) |
| 375 |
|
| 376 |
self.playbin.connect("about-to-finish", self.on_about_to_finish) |
| 377 |
|
| 378 |
video = Gst.ElementFactory.make("fakesink", '%s-fakevideo' % self.name) |
| 379 |
video.set_property('sync', True) |
| 380 |
self.playbin.set_property('video-sink', video) |
| 381 |
|
| 382 |
self.audio_sink = DynamicAudioSink('%s-sink' % self.name) |
| 383 |
self.playbin.set_property('audio-sink', self.audio_sink) |
| 384 |
|
| 385 |
# Setup the bus |
| 386 |
bus = self.playbin.get_bus() |
| 387 |
bus.add_signal_watch() |
| 388 |
bus.connect('message', self.on_message) |
| 389 |
|
| 390 |
# priority boost hack if needed |
| 391 |
priority_boost(self.playbin) |
| 392 |
|
| 393 |
# Pulsesink changes volume behind our back, track it |
| 394 |
self.playbin.connect('notify::volume', self.on_volume_change) |
| 395 |
|
| 396 |
self.fader = TrackFader( |
| 397 |
self, self.on_fade_out_begin, '%s-fade-%s' % (engine.name, self.idx) |
| 398 |
) |
| 399 |
|
| 400 |
def destroy(self): |
| 401 |
self.fader.stop() |
| 402 |
self.playbin.set_state(Gst.State.NULL) |
| 403 |
self.playbin.get_bus().remove_signal_watch() |
| 404 |
|
| 405 |
def reconfigure_sink(self): |
| 406 |
self.needs_sink = False |
| 407 |
sink = create_device(self.engine.name) |
| 408 |
|
| 409 |
# Works for pulsesink, but not other sinks |
| 410 |
# -> Not a perfect solution, still some audio blip is heard. Unfortunately, |
| 411 |
# can't do better without direct support from gstreamer |
| 412 |
if self.engine.disable_autoswitch and hasattr(sink.props, 'current_device'): |
| 413 |
self.selected_sink = sink.props.device |
| 414 |
sink.connect('notify::current-device', self._on_sink_change_notify) |
| 415 |
|
| 416 |
self.audio_sink.reconfigure(sink) |
| 417 |
|
| 418 |
def _on_sink_change_notify(self, sink, param): |
| 419 |
if self.selected_sink != sink.props.current_device: |
| 420 |
domain = GLib.quark_from_string("g-exaile-error") |
| 421 |
err = GLib.Error.new_literal(domain, "Audio device disconnected", 0) |
| 422 |
self.playbin.get_bus().post( |
| 423 |
Gst.Message.new_error(None, err, "Disconnected") |
| 424 |
) |
| 425 |
self.logger.info("Detected device disconnect, stopping playback") |
| 426 |
|
| 427 |
def reconfigure_fader(self, fade_in_duration, fade_out_duration): |
| 428 |
if self.get_gst_state() != Gst.State.NULL: |
| 429 |
self.fader.setup_track( |
| 430 |
self.current_track, fade_in_duration, fade_out_duration, is_update=True |
| 431 |
) |
| 432 |
|
| 433 |
def get_gst_state(self): |
| 434 |
return self.playbin.get_state(timeout=50 * Gst.MSECOND)[1] |
| 435 |
|
| 436 |
def get_position(self): |
| 437 |
# TODO: This only works when pipeline is prerolled/ready? |
| 438 |
if not self.get_gst_state() == Gst.State.PAUSED: |
| 439 |
res, self.last_position = self.playbin.query_position(Gst.Format.TIME) |
| 440 |
|
| 441 |
if res is False: |
| 442 |
self.last_position = 0 |
| 443 |
|
| 444 |
return self.last_position |
| 445 |
|
| 446 |
def get_volume(self): |
| 447 |
return self.playbin.props.volume |
| 448 |
|
| 449 |
def get_user_volume(self): |
| 450 |
return self.fader.get_user_volume() |
| 451 |
|
| 452 |
def pause(self): |
| 453 |
# This caches the current last position before pausing |
| 454 |
self.get_position() |
| 455 |
self.playbin.set_state(Gst.State.PAUSED) |
| 456 |
self.fader.pause() |
| 457 |
|
| 458 |
def play( |
| 459 |
self, |
| 460 |
track, |
| 461 |
start_at, |
| 462 |
paused, |
| 463 |
stopped, |
| 464 |
already_queued, |
| 465 |
fade_in_duration=None, |
| 466 |
fade_out_duration=None, |
| 467 |
): |
| 468 |
'''fade duration is in seconds''' |
| 469 |
|
| 470 |
if not already_queued: |
| 471 |
self.stop(emit_eos=False) |
| 472 |
|
| 473 |
# For the moment, the only safe time to add/remove elements |
| 474 |
# is when the playbin is NULL, so do that here.. |
| 475 |
if self.audio_filters.setup_elements(): |
| 476 |
self.logger.debug("Applying audio filters") |
| 477 |
self.playbin.props.audio_filter = self.audio_filters |
| 478 |
else: |
| 479 |
self.logger.debug("Not applying audio filters") |
| 480 |
self.playbin.props.audio_filter = None |
| 481 |
|
| 482 |
if self.needs_sink: |
| 483 |
self.reconfigure_sink() |
| 484 |
|
| 485 |
self.current_track = track |
| 486 |
self.last_position = 0 |
| 487 |
self.buffered_track = None |
| 488 |
|
| 489 |
uri = track.get_loc_for_io() |
| 490 |
self.logger.info("Playing %s", common.sanitize_url(uri)) |
| 491 |
|
| 492 |
# This is only set for gapless playback |
| 493 |
if not already_queued: |
| 494 |
self.playbin.set_property("uri", uri) |
| 495 |
if urllib.parse.urlsplit(uri)[0] == "cdda": |
| 496 |
self.notify_id = self.playbin.connect( |
| 497 |
'source-setup', self.on_source_setup, track |
| 498 |
) |
| 499 |
|
| 500 |
if stopped: |
| 501 |
self.playbin.set_state(Gst.State.NULL) |
| 502 |
self.engine.player.engine_notify_track_end(self.current_track, True) |
| 503 |
# Start in paused mode if we need to seek |
| 504 |
elif paused or start_at is not None: |
| 505 |
self.playbin.set_state(Gst.State.PAUSED) |
| 506 |
elif not already_queued: |
| 507 |
self.playbin.set_state(Gst.State.PLAYING) |
| 508 |
|
| 509 |
self.fader.setup_track(track, fade_in_duration, fade_out_duration, now=0) |
| 510 |
|
| 511 |
if start_at is not None: |
| 512 |
self.seek(start_at) |
| 513 |
if not paused: |
| 514 |
self.playbin.set_state(Gst.State.PLAYING) |
| 515 |
|
| 516 |
if paused: |
| 517 |
self.fader.pause() |
| 518 |
|
| 519 |
def seek(self, value): |
| 520 |
'''value is in seconds''' |
| 521 |
|
| 522 |
# TODO: Make sure that we're in a valid seekable state before seeking? |
| 523 |
|
| 524 |
# wait up to 1s for the state to switch, else this fails |
| 525 |
if ( |
| 526 |
self.playbin.get_state(timeout=1000 * Gst.MSECOND)[0] |
| 527 |
!= Gst.StateChangeReturn.SUCCESS |
| 528 |
): |
| 529 |
# TODO: This error message is misleading, when does this ever happen? |
| 530 |
# TODO: if the sink is incorrectly specified, this error happens first. |
| 531 |
# self.engine._error_func(self, "Could not start at specified offset") |
| 532 |
self.logger.warning("Error seeking to specified offset") |
| 533 |
return False |
| 534 |
|
| 535 |
new_position = int(Gst.SECOND * value) |
| 536 |
seek_event = Gst.Event.new_seek( |
| 537 |
1.0, |
| 538 |
Gst.Format.TIME, |
| 539 |
Gst.SeekFlags.FLUSH, |
| 540 |
Gst.SeekType.SET, |
| 541 |
new_position, |
| 542 |
Gst.SeekType.NONE, |
| 543 |
0, |
| 544 |
) |
| 545 |
|
| 546 |
self.last_position = new_position |
| 547 |
self.fader.seek(value) |
| 548 |
|
| 549 |
return self.playbin.send_event(seek_event) |
| 550 |
|
| 551 |
def set_volume(self, volume): |
| 552 |
# self.logger.debug("Set playbin volume: %.2f", volume) |
| 553 |
# TODO: strange issue where pulse sets the system audio volume |
| 554 |
# when exaile starts up... |
| 555 |
self.playbin.props.volume = volume |
| 556 |
|
| 557 |
def set_user_volume(self, volume): |
| 558 |
self.logger.debug("Set user volume: %.2f", volume) |
| 559 |
self.fader.set_user_volume(volume) |
| 560 |
|
| 561 |
def stop(self, emit_eos=True): |
| 562 |
prior_track = self.current_track |
| 563 |
self.current_track = None |
| 564 |
self.playbin.set_state(Gst.State.NULL) |
| 565 |
self.fader.stop() |
| 566 |
|
| 567 |
if emit_eos: |
| 568 |
self.engine._eos_func(self) |
| 569 |
|
| 570 |
return prior_track |
| 571 |
|
| 572 |
def unpause(self): |
| 573 |
# gstreamer does not buffer paused network streams, so if the user |
| 574 |
# is unpausing a stream, just restart playback |
| 575 |
current = self.current_track |
| 576 |
if not (current.is_local() or current.get_tag_raw('__length')): |
| 577 |
self.playbin.set_state(Gst.State.READY) |
| 578 |
|
| 579 |
self.playbin.set_state(Gst.State.PLAYING) |
| 580 |
self.fader.unpause() |
| 581 |
|
| 582 |
# |
| 583 |
# Events |
| 584 |
# |
| 585 |
|
| 586 |
def on_about_to_finish(self, *args): |
| 587 |
""" |
| 588 |
This function exists solely to allow gapless playback for audio |
| 589 |
formats that support it. Setting the URI property of the playbin |
| 590 |
will queue the track for playback immediately after the previous |
| 591 |
track. |
| 592 |
|
| 593 |
.. note:: This is called from the gstreamer thread |
| 594 |
""" |
| 595 |
|
| 596 |
if self.engine.crossfade_enabled: |
| 597 |
return |
| 598 |
|
| 599 |
track = self.engine.player.engine_autoadvance_get_next_track(gapless=True) |
| 600 |
if track: |
| 601 |
uri = track.get_loc_for_io() |
| 602 |
self.playbin.set_property('uri', uri) |
| 603 |
self.buffered_track = track |
| 604 |
|
| 605 |
self.logger.debug( |
| 606 |
"Gapless transition: queuing %s", common.sanitize_url(uri) |
| 607 |
) |
| 608 |
|
| 609 |
def on_fade_out_begin(self): |
| 610 |
if self.engine.crossfade_enabled: |
| 611 |
self.engine._autoadvance_track(still_fading=True) |
| 612 |
|
| 613 |
def on_message(self, bus, message): |
| 614 |
""" |
| 615 |
This is called on the main thread |
| 616 |
""" |
| 617 |
|
| 618 |
if message.type == Gst.MessageType.BUFFERING: |
| 619 |
percent = message.parse_buffering() |
| 620 |
if not percent < 100: |
| 621 |
self.logger.info('Buffering complete') |
| 622 |
if percent % 5 == 0: |
| 623 |
event.log_event('playback_buffering', self.engine.player, percent) |
| 624 |
|
| 625 |
elif message.type == Gst.MessageType.TAG: |
| 626 |
"""Update track length and optionally metadata from gstreamer's parser. |
| 627 |
Useful for streams and files mutagen doesn't understand.""" |
| 628 |
|
| 629 |
current = self.current_track |
| 630 |
remote_newsong = False |
| 631 |
if not current.is_local(): |
| 632 |
prior_track = copy.deepcopy(current) |
| 633 |
remote_newsong = gst_utils.parse_stream_tags( |
| 634 |
current, message.parse_tag() |
| 635 |
) |
| 636 |
|
| 637 |
if current and not current.get_tag_raw('__length'): |
| 638 |
res, raw_duration = self.playbin.query_duration(Gst.Format.TIME) |
| 639 |
if not res: |
| 640 |
self.logger.error("Couldn't query duration") |
| 641 |
raw_duration = 0 |
| 642 |
duration = float(raw_duration) / Gst.SECOND |
| 643 |
if duration > 0: |
| 644 |
current.set_tag_raw('__length', duration) |
| 645 |
|
| 646 |
if remote_newsong: |
| 647 |
self.engine.player.engine_notify_track_end(prior_track, False) |
| 648 |
self.engine.player.engine_notify_track_start(current) |
| 649 |
|
| 650 |
elif ( |
| 651 |
message.type == Gst.MessageType.EOS |
| 652 |
and not self.get_gst_state() == Gst.State.PAUSED |
| 653 |
): |
| 654 |
self.engine._eos_func(self) |
| 655 |
|
| 656 |
elif ( |
| 657 |
message.type == Gst.MessageType.STREAM_START |
| 658 |
and message.src == self.playbin |
| 659 |
and self.buffered_track is not None |
| 660 |
): |
| 661 |
# This handles starting the next track during gapless transition |
| 662 |
buffered_track = self.buffered_track |
| 663 |
self.buffered_track = None |
| 664 |
play_args = self.engine.player.engine_autoadvance_notify_next( |
| 665 |
buffered_track |
| 666 |
) + (True, True) |
| 667 |
self.engine._next_track(*play_args) |
| 668 |
|
| 669 |
elif message.type == Gst.MessageType.STATE_CHANGED: |
| 670 |
# This idea from quodlibet: pulsesink will not notify us when |
| 671 |
# volume changes if the stream is paused, so do it when the |
| 672 |
# state changes. |
| 673 |
if message.src == self.audio_sink: |
| 674 |
self.playbin.notify("volume") |
| 675 |
|
| 676 |
elif message.type == Gst.MessageType.ERROR: |
| 677 |
self.__handle_error_message(message) |
| 678 |
|
| 679 |
elif message.type == Gst.MessageType.ELEMENT: |
| 680 |
if not missing_plugin.handle_message(message, self.engine): |
| 681 |
logger.debug( |
| 682 |
"Unexpected element-specific GstMessage received from %s: %s", |
| 683 |
message.src, |
| 684 |
message, |
| 685 |
) |
| 686 |
|
| 687 |
elif message.type == Gst.MessageType.WARNING: |
| 688 |
# TODO there might be some useful warnings we ignore for now. |
| 689 |
gerror, debug_text = Gst.Message.parse_warning(message) |
| 690 |
logger.warning( |
| 691 |
"Unhandled GStreamer warning received:\n\tGError: %s\n\tDebug text: %s", |
| 692 |
gerror, |
| 693 |
debug_text, |
| 694 |
) |
| 695 |
|
| 696 |
else: |
| 697 |
# TODO there might be some useful messages we ignore for now. |
| 698 |
logger.debug( |
| 699 |
"Unhandled GstMessage of type %s received: %s", message.type, message |
| 700 |
) |
| 701 |
|
| 702 |
def __handle_error_message(self, message): |
| 703 |
# Error handling code is from quodlibet |
| 704 |
gerror, debug_info = message.parse_error() |
| 705 |
message_text = "" |
| 706 |
if gerror: |
| 707 |
message_text = gerror.message.rstrip(".") |
| 708 |
|
| 709 |
if message_text == "": |
| 710 |
# The most readable part is always the last.. |
| 711 |
message_text = debug_info[debug_info.rfind(':') + 1 :] |
| 712 |
|
| 713 |
# .. unless there's nothing in it. |
| 714 |
if ' ' not in message_text: |
| 715 |
if debug_info.startswith('playsink'): |
| 716 |
message_text += _( |
| 717 |
': Possible audio device error, is it plugged in?' |
| 718 |
) |
| 719 |
|
| 720 |
self.logger.error("Playback error: %s", message_text) |
| 721 |
self.logger.debug("- Extra error info: %s", debug_info) |
| 722 |
|
| 723 |
envname = 'GST_DEBUG_DUMP_DOT_DIR' |
| 724 |
if envname not in os.environ: |
| 725 |
import xl.xdg |
| 726 |
|
| 727 |
os.environ[envname] = xl.xdg.get_logs_dir() |
| 728 |
|
| 729 |
Gst.debug_bin_to_dot_file(self.playbin, Gst.DebugGraphDetails.ALL, self.name) |
| 730 |
self.logger.debug( |
| 731 |
"- Pipeline debug info written to file '%s/%s.dot'", |
| 732 |
os.environ[envname], |
| 733 |
self.name, |
| 734 |
) |
| 735 |
|
| 736 |
self.engine._error_func(self, message_text) |
| 737 |
|
| 738 |
def on_source_setup(self, playbin, source, track): |
| 739 |
# this is for handling multiple CD devices properly |
| 740 |
device = track.get_loc_for_io().split("#")[-1] |
| 741 |
source.props.device = device |
| 742 |
playbin.disconnect(self.notify_id) |
| 743 |
|
| 744 |
def on_volume_change(self, e, p): |
| 745 |
real = self.playbin.props.volume |
| 746 |
vol, is_same = self.fader.calculate_user_volume(real) |
| 747 |
if not is_same: |
| 748 |
GLib.idle_add(self.engine.player.engine_notify_user_volume_change, vol) |