| 1 |
# This program is free software; you can redistribute it and/or modify |
| 2 |
# it under the terms of the GNU General Public License as published by |
| 3 |
# the Free Software Foundation; either version 2, or (at your option) |
| 4 |
# any later version. |
| 5 |
# |
| 6 |
# This program is distributed in the hope that it will be useful, |
| 7 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 |
# GNU General Public License for more details. |
| 10 |
# |
| 11 |
# You should have received a copy of the GNU General Public License |
| 12 |
# along with this program; if not, write to the Free Software |
| 13 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 14 |
# |
| 15 |
# |
| 16 |
# The developers of the Exaile media player hereby grant permission |
| 17 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 18 |
# distributed together with GStreamer and Exaile. This permission is |
| 19 |
# above and beyond the permissions granted by the GPL license by which |
| 20 |
# Exaile is covered. If you modify this code, you may extend this |
| 21 |
# exception to your version of the code, but you are not obligated to |
| 22 |
# do so. If you do not wish to do so, delete this exception statement |
| 23 |
# from your version. |
| 24 |
""" |
| 25 |
This module serves as frontend for GStreamer Plugin Base Utils (GstPbutils) for |
| 26 |
installing missing plugins. |
| 27 |
|
| 28 |
See also: |
| 29 |
* https://github.com/quodlibet/quodlibet/blob/master/quodlibet/quodlibet/player/gstbe/player.py#L548 |
| 30 |
* https://git.gnome.org/browse/totem/tree/src/backend/bacon-video-widget-gst-missing-plugins.c |
| 31 |
|
| 32 |
This module must be a singleton, i.e. no instances are allowed. Thus, there is |
| 33 |
no class. |
| 34 |
|
| 35 |
Created on 25.04.2017 |
| 36 |
|
| 37 |
@author: Christian Stadelmann |
| 38 |
""" |
| 39 |
|
| 40 |
import logging |
| 41 |
|
| 42 |
import gi |
| 43 |
|
| 44 |
gi.require_version('GstPbutils', '1.0') |
| 45 |
from gi.repository import GstPbutils |
| 46 |
|
| 47 |
from xl import event |
| 48 |
from xl.nls import gettext as _ |
| 49 |
|
| 50 |
LOGGER = logging.getLogger(__name__) |
| 51 |
|
| 52 |
MISSING_PLUGIN_URL = "https://exaile.readthedocs.io/en/stable/user/faq.html" |
| 53 |
|
| 54 |
# how to test: |
| 55 |
# the helper binary is located at /usr/libexec/gstreamer/gst-install-plugins-helper |
| 56 |
|
| 57 |
|
| 58 |
def handle_message(message, engine): |
| 59 |
""" |
| 60 |
Handles `message` by checking whether it is a "missing plugin" message. |
| 61 |
If it is, takes all required steps to make sure that |
| 62 |
* playback is paused |
| 63 |
* the user is notified of the issue |
| 64 |
* the user gets a hint on which software to install |
| 65 |
|
| 66 |
@param message: a Gst.Message of type Gst.MessageType.Element |
| 67 |
@param engine: an instance of xl.player.gst.engine.ExaileGstEngine |
| 68 |
|
| 69 |
@return: True if the message was a "missing plugin" message and was |
| 70 |
being handled. This does not mean that the plugin installed |
| 71 |
successfully. |
| 72 |
False if the message should be handled by some other code |
| 73 |
because it is not related to a missing plugin. |
| 74 |
""" |
| 75 |
if not GstPbutils.is_missing_plugin_message(message): |
| 76 |
return False |
| 77 |
|
| 78 |
__handle_plugin_missing_message(message, engine) |
| 79 |
return True |
| 80 |
|
| 81 |
|
| 82 |
def __handle_plugin_missing_message(message, engine): |
| 83 |
desc = GstPbutils.missing_plugin_message_get_description(message) |
| 84 |
installer_details = GstPbutils.missing_plugin_message_get_installer_detail(message) |
| 85 |
LOGGER.warning("A plugin for %s is missing, stopping playback", desc) |
| 86 |
|
| 87 |
user_message = _( |
| 88 |
"A GStreamer 1.x plugin for %s is missing. " |
| 89 |
"Without this software installed, Exaile will not be able to play the current file. " |
| 90 |
"Please install the required software on your computer. See %s for details." |
| 91 |
) % (desc, MISSING_PLUGIN_URL) |
| 92 |
# TODO make URL clickable by utilizing xlgui.widgets.dialogs.MessageBar |
| 93 |
|
| 94 |
engine.stop() |
| 95 |
__notify_user_on_error(user_message, engine) |
| 96 |
if GstPbutils.install_plugins_supported(): |
| 97 |
if __run_installer_helper(installer_details): |
| 98 |
return |
| 99 |
LOGGER.warning("Installation of GStreamer plugins not supported on this platform.") |
| 100 |
|
| 101 |
|
| 102 |
def __notify_user_on_error(message_text, engine): |
| 103 |
event.log_event('playback_error', engine.player, message_text) |
| 104 |
|
| 105 |
|
| 106 |
def __run_installer_helper(installer_details): |
| 107 |
""" |
| 108 |
|
| 109 |
@return True if the helper might have run. False if did not run for |
| 110 |
sure. |
| 111 |
""" |
| 112 |
cntxt = __create_context() |
| 113 |
|
| 114 |
LOGGER.info("Prompting user to install missing codec(s): %s", installer_details) |
| 115 |
|
| 116 |
start_result = GstPbutils.install_plugins_async( |
| 117 |
[installer_details], cntxt, __installer_finished_callback |
| 118 |
) |
| 119 |
LOGGER.debug( |
| 120 |
"GstPbutils.install_plugins_async() return value: %s", |
| 121 |
GstPbutils.InstallPluginsReturn.get_name(start_result), |
| 122 |
) |
| 123 |
if start_result == GstPbutils.InstallPluginsReturn.INTERNAL_FAILURE: |
| 124 |
# should only happen when there is a bug in Exaile or its libs: |
| 125 |
LOGGER.error("Internal failure starting assisted GStreamer plugin installation") |
| 126 |
return False |
| 127 |
elif start_result == GstPbutils.InstallPluginsReturn.HELPER_MISSING: |
| 128 |
# we expect that to happen on some platforms |
| 129 |
LOGGER.warning("Helper missing for assisted installation of Gstreamer plugins") |
| 130 |
return False |
| 131 |
elif start_result == GstPbutils.InstallPluginsReturn.INSTALL_IN_PROGRESS: |
| 132 |
LOGGER.warning("Another assisted plugin installation is already in progress") |
| 133 |
return False |
| 134 |
elif start_result == GstPbutils.InstallPluginsReturn.STARTED_OK: |
| 135 |
LOGGER.info("Successfully started assisted GStreamer plugin installation") |
| 136 |
return True |
| 137 |
else: |
| 138 |
LOGGER.error( |
| 139 |
"Code should not be reached. " |
| 140 |
"Unexpected return value from install_plugins_async: %s", |
| 141 |
GstPbutils.InstallPluginsReturn.get_name(start_result), |
| 142 |
) |
| 143 |
return False |
| 144 |
|
| 145 |
|
| 146 |
def __installer_finished_callback(result): |
| 147 |
# due to a bug in PackageKit, this function will be called immediately |
| 148 |
# after starting the helper: https://bugs.freedesktop.org/show_bug.cgi?id=100791 |
| 149 |
LOGGER.debug( |
| 150 |
"GstPbutils.install_plugins_async() helper process exit code: %s", |
| 151 |
GstPbutils.InstallPluginsReturn.get_name(result), |
| 152 |
) |
| 153 |
|
| 154 |
if ( |
| 155 |
result == GstPbutils.InstallPluginsReturn.SUCCESS |
| 156 |
or result == GstPbutils.InstallPluginsReturn.PARTIAL_SUCCESS |
| 157 |
): |
| 158 |
# TODO notify user that installation of plugins was successful and ask |
| 159 |
# the user whether we may restart GStreamer engine to apply plugin updates, |
| 160 |
# because the user might have resumed playback in the meantime, and we do |
| 161 |
# not want to interrupt. |
| 162 |
# |
| 163 |
# engine.stop() |
| 164 |
# engine.destroy(permanent=False) |
| 165 |
# Gst.update_registry() |
| 166 |
# engine.initialize() |
| 167 |
pass |
| 168 |
elif result == GstPbutils.InstallPluginsReturn.NOT_FOUND: |
| 169 |
LOGGER.warning("GStreamer helper was unable to install missing plugin.") |
| 170 |
# we do not care about these, the user already got a notification how |
| 171 |
# to install plugins. There is nothing more we can do. |
| 172 |
elif ( |
| 173 |
result == GstPbutils.InstallPluginsReturn.ERROR |
| 174 |
or result == GstPbutils.InstallPluginsReturn.CRASHED |
| 175 |
or result == GstPbutils.InstallPluginsReturn.INVALID |
| 176 |
or result == GstPbutils.InstallPluginsReturn.INTERNAL_FAILURE |
| 177 |
): |
| 178 |
LOGGER.error( |
| 179 |
"GStreamer plugin helper failed with %s", |
| 180 |
GstPbutils.InstallPluginsReturn.get_name(result), |
| 181 |
) |
| 182 |
elif result == GstPbutils.InstallPluginsReturn.USER_ABORT: |
| 183 |
LOGGER.info("User aborted the GStreamer plugin installation.") |
| 184 |
# the user decided not to install any software, which might have been on purpose, |
| 185 |
# so we do not want to ask again immediately. |
| 186 |
else: |
| 187 |
LOGGER.error( |
| 188 |
"Code should not be reached. " |
| 189 |
"Unexpected return value from install_plugins_async callback: %s", |
| 190 |
GstPbutils.InstallPluginsReturn.get_name(result), |
| 191 |
) |
| 192 |
|
| 193 |
|
| 194 |
def __create_context(): |
| 195 |
LOGGER.info("Initializing connector for GstPbutils...") |
| 196 |
cntxt = GstPbutils.InstallPluginsContext() |
| 197 |
cntxt.set_confirm_search(True) |
| 198 |
|
| 199 |
# See https://standards.freedesktop.org/desktop-entry-spec/latest/ape.html |
| 200 |
cntxt.set_desktop_id("exaile.desktop") |
| 201 |
# TODO |
| 202 |
# cntxt.set_startup_notification_id() |
| 203 |
# cntxt.set_xid() |
| 204 |
return cntxt |