| 1 |
# Copyright (C) 2013-2015 Dustin Spicuzza |
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2, or (at your option) |
| 6 |
# any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program; if not, write to the Free Software |
| 15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
# |
| 17 |
# |
| 18 |
# The developers of the Exaile media player hereby grant permission |
| 19 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 20 |
# distributed together with GStreamer and Exaile. This permission is |
| 21 |
# above and beyond the permissions granted by the GPL license by which |
| 22 |
# Exaile is covered. If you modify this code, you may extend this |
| 23 |
# exception to your version of the code, but you are not obligated to |
| 24 |
# do so. If you do not wish to do so, delete this exception statement |
| 25 |
# from your version. |
| 26 |
|
| 27 |
|
| 28 |
import logging |
| 29 |
|
| 30 |
import ctypes.wintypes as cwin |
| 31 |
import ctypes |
| 32 |
|
| 33 |
from gi.repository import Gst |
| 34 |
|
| 35 |
logger = logging.getLogger(__name__) |
| 36 |
|
| 37 |
|
| 38 |
def get_priority_booster(): |
| 39 |
""" |
| 40 |
This hack allows us to boost the priority of GStreamer task threads on |
| 41 |
Windows. See https://github.com/exaile/exaile/issues/76 and |
| 42 |
https://bugzilla.gnome.org/show_bug.cgi?id=781998 |
| 43 |
""" |
| 44 |
avrt_dll = ctypes.windll.LoadLibrary("avrt.dll") |
| 45 |
AvSetMmThreadCharacteristics = avrt_dll.AvSetMmThreadCharacteristicsW |
| 46 |
AvSetMmThreadCharacteristics.argtypes = [cwin.LPCWSTR, ctypes.POINTER(cwin.DWORD)] |
| 47 |
AvSetMmThreadCharacteristics.restype = cwin.HANDLE |
| 48 |
|
| 49 |
AvRevertMmThreadCharacteristics = avrt_dll.AvRevertMmThreadCharacteristics |
| 50 |
AvRevertMmThreadCharacteristics.argtypes = [cwin.HANDLE] |
| 51 |
AvRevertMmThreadCharacteristics.restype = cwin.BOOL |
| 52 |
|
| 53 |
def on_stream_status(bus, message): |
| 54 |
""" |
| 55 |
Called synchronously from GStreamer processing threads -- do what |
| 56 |
we need to do and then get out ASAP |
| 57 |
""" |
| 58 |
status = message.parse_stream_status() |
| 59 |
|
| 60 |
# A gstreamer thread starts |
| 61 |
if status.type == Gst.StreamStatusType.ENTER: |
| 62 |
obj = message.get_stream_status_object() |
| 63 |
|
| 64 |
# note that we use "Pro Audio" because it gives a higher priority, and |
| 65 |
# that's what Chrome does anyways... |
| 66 |
unused = cwin.DWORD() |
| 67 |
obj.task_handle = AvSetMmThreadCharacteristics( |
| 68 |
"Pro Audio", ctypes.byref(unused) |
| 69 |
) |
| 70 |
|
| 71 |
# A gstreamer thread ends |
| 72 |
elif status.type == Gst.StreamStatusType.LEAVE: |
| 73 |
obj = message.get_stream_status_object() |
| 74 |
task_handle = getattr(obj, 'task_handle', None) |
| 75 |
if task_handle: |
| 76 |
AvRevertMmThreadCharacteristics(task_handle) |
| 77 |
|
| 78 |
def attach_priority_hook(player): |
| 79 |
bus = player.get_bus() |
| 80 |
bus.connect('sync-message::stream-status', on_stream_status) |
| 81 |
bus.enable_sync_message_emission() |
| 82 |
|
| 83 |
return attach_priority_hook |