| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2008 Bastien Nocera <[email protected]> |
| 4 |
* Copyright (C) 2008 Richard Hughes <[email protected]> |
| 5 |
* |
| 6 |
* Licensed under the GNU General Public License Version 2 |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <gst/gst.h> |
| 24 |
#include <gst/pbutils/install-plugins.h> |
| 25 |
#include <string.h> |
| 26 |
#include <sys/utsname.h> |
| 27 |
#include <packagekit-glib2/packagekit.h> |
| 28 |
|
| 29 |
typedef struct { |
| 30 |
GstStructure *structure; |
| 31 |
gchar *type_name; |
| 32 |
gchar *codec_name; |
| 33 |
gchar *app_name; |
| 34 |
gchar *gstreamer_version; |
| 35 |
} PkGstCodecInfo; |
| 36 |
|
| 37 |
enum { |
| 38 |
FIELD_VERSION = 0, |
| 39 |
FIELD_LAYER, |
| 40 |
FIELD_VARIANT, |
| 41 |
FIELD_SYSTEMSTREAM |
| 42 |
}; |
| 43 |
|
| 44 |
static PkGstCodecInfo * |
| 45 |
pk_gst_parse_codec (const gchar *codec) |
| 46 |
{ |
| 47 |
GstStructure *s; |
| 48 |
PkGstCodecInfo *info = NULL; |
| 49 |
g_autofree gchar *caps = NULL; |
| 50 |
g_autofree gchar *type_name = NULL; |
| 51 |
g_auto(GStrv) split = NULL; |
| 52 |
g_auto(GStrv) ss = NULL; |
| 53 |
|
| 54 |
split = g_strsplit (codec, "|", -1); |
| 55 |
if (split == NULL || g_strv_length (split) != 5) { |
| 56 |
g_message ("PackageKit: not a GStreamer codec line"); |
| 57 |
return NULL; |
| 58 |
} |
| 59 |
if (g_strcmp0 (split[0], "gstreamer") != 0) { |
| 60 |
g_message ("PackageKit: not a GStreamer codec request"); |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
if (g_strcmp0 (split[1], "0.10") != 0 && |
| 64 |
g_strcmp0 (split[1], "1.0") != 0) { |
| 65 |
g_message ("PackageKit: not recognised GStreamer version"); |
| 66 |
return NULL; |
| 67 |
} |
| 68 |
|
| 69 |
if (g_str_has_prefix (split[4], "uri") != FALSE) { |
| 70 |
/* split uri */ |
| 71 |
ss = g_strsplit (split[4], " ", 2); |
| 72 |
info = g_new0 (PkGstCodecInfo, 1); |
| 73 |
info->app_name = g_strdup (split[2]); |
| 74 |
info->codec_name = g_strdup (split[3]); |
| 75 |
info->type_name = g_strdup (ss[0]); |
| 76 |
return info; |
| 77 |
} |
| 78 |
|
| 79 |
/* split */ |
| 80 |
ss = g_strsplit (split[4], "-", 2); |
| 81 |
type_name = g_strdup (ss[0]); |
| 82 |
caps = g_strdup (ss[1]); |
| 83 |
|
| 84 |
s = gst_structure_from_string (caps, NULL); |
| 85 |
if (s == NULL) { |
| 86 |
g_message ("PackageKit: failed to parse caps: %s", caps); |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
|
| 90 |
/* remove fields that are almost always just MIN-MAX of some sort |
| 91 |
* in order to make the caps look less messy */ |
| 92 |
gst_structure_remove_field (s, "pixel-aspect-ratio"); |
| 93 |
gst_structure_remove_field (s, "framerate"); |
| 94 |
gst_structure_remove_field (s, "channels"); |
| 95 |
gst_structure_remove_field (s, "width"); |
| 96 |
gst_structure_remove_field (s, "height"); |
| 97 |
gst_structure_remove_field (s, "rate"); |
| 98 |
gst_structure_remove_field (s, "depth"); |
| 99 |
gst_structure_remove_field (s, "clock-rate"); |
| 100 |
gst_structure_remove_field (s, "bitrate"); |
| 101 |
|
| 102 |
info = g_new0 (PkGstCodecInfo, 1); |
| 103 |
info->gstreamer_version = g_strdup (split[1]); |
| 104 |
info->app_name = g_strdup (split[2]); |
| 105 |
info->codec_name = g_strdup (split[3]); |
| 106 |
info->type_name = g_strdup (type_name); |
| 107 |
info->structure = s; |
| 108 |
return info; |
| 109 |
} |
| 110 |
|
| 111 |
static int |
| 112 |
pk_gst_field_get_type (const gchar *field_name) |
| 113 |
{ |
| 114 |
if (g_strrstr (field_name, "version") != NULL) |
| 115 |
return FIELD_VERSION; |
| 116 |
if (g_strcmp0 (field_name, "layer") == 0) |
| 117 |
return FIELD_LAYER; |
| 118 |
if (g_strcmp0 (field_name, "systemstream") == 0) |
| 119 |
return FIELD_SYSTEMSTREAM; |
| 120 |
if (g_strcmp0 (field_name, "variant") == 0) |
| 121 |
return FIELD_VARIANT; |
| 122 |
return -1; |
| 123 |
} |
| 124 |
|
| 125 |
static gint |
| 126 |
pk_gst_fields_type_compare (const gchar *a, const gchar *b) |
| 127 |
{ |
| 128 |
gint a_type, b_type; |
| 129 |
|
| 130 |
a_type = pk_gst_field_get_type (a); |
| 131 |
b_type = pk_gst_field_get_type (b); |
| 132 |
if (a_type < b_type) |
| 133 |
return -1; |
| 134 |
if (b_type < a_type) |
| 135 |
return 1; |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
static gchar * |
| 140 |
pk_gst_structure_to_provide (GstStructure *s) |
| 141 |
{ |
| 142 |
GString *string; |
| 143 |
guint i, num_fields; |
| 144 |
GList *l; |
| 145 |
g_autoptr(GList) fields = NULL; |
| 146 |
|
| 147 |
num_fields = gst_structure_n_fields (s); |
| 148 |
fields = NULL; |
| 149 |
|
| 150 |
for (i = 0; i < num_fields; i++) { |
| 151 |
const gchar *field_name; |
| 152 |
|
| 153 |
field_name = gst_structure_nth_field_name (s, i); |
| 154 |
if (pk_gst_field_get_type (field_name) < 0) { |
| 155 |
g_message ("PackageKit: ignoring field named %s", field_name); |
| 156 |
continue; |
| 157 |
} |
| 158 |
|
| 159 |
fields = g_list_insert_sorted (fields, g_strdup (field_name), (GCompareFunc) pk_gst_fields_type_compare); |
| 160 |
} |
| 161 |
|
| 162 |
string = g_string_new(""); |
| 163 |
for (l = fields; l != NULL; l = l->next) { |
| 164 |
gchar *field_name; |
| 165 |
GType type; |
| 166 |
|
| 167 |
field_name = l->data; |
| 168 |
|
| 169 |
type = gst_structure_get_field_type (s, field_name); |
| 170 |
g_message ("PackageKit: field is: %s, type: %s", field_name, g_type_name (type)); |
| 171 |
|
| 172 |
if (type == G_TYPE_INT) { |
| 173 |
int value; |
| 174 |
|
| 175 |
gst_structure_get_int (s, field_name, &value); |
| 176 |
g_string_append_printf (string, "(%s=%d)", field_name, value); |
| 177 |
} else if (type == G_TYPE_BOOLEAN) { |
| 178 |
int value; |
| 179 |
|
| 180 |
gst_structure_get_boolean (s, field_name, &value); |
| 181 |
g_string_append_printf (string, "(%s=%s)", field_name, value ? "true" : "false"); |
| 182 |
} else if (type == G_TYPE_STRING) { |
| 183 |
const gchar *value; |
| 184 |
|
| 185 |
value = gst_structure_get_string (s, field_name); |
| 186 |
g_string_append_printf (string, "(%s=%s)", field_name, value); |
| 187 |
} else { |
| 188 |
g_warning ("PackageKit: unhandled type! %s", g_type_name (type)); |
| 189 |
} |
| 190 |
|
| 191 |
g_free (field_name); |
| 192 |
} |
| 193 |
return g_string_free (string, FALSE); |
| 194 |
} |
| 195 |
|
| 196 |
static void |
| 197 |
pk_gst_codec_free (PkGstCodecInfo *codec) |
| 198 |
{ |
| 199 |
if (codec->structure) |
| 200 |
gst_structure_free (codec->structure); |
| 201 |
g_free (codec->type_name); |
| 202 |
g_free (codec->codec_name); |
| 203 |
g_free (codec->app_name); |
| 204 |
g_free (codec->gstreamer_version); |
| 205 |
g_free (codec); |
| 206 |
} |
| 207 |
|
| 208 |
static const gchar * |
| 209 |
pk_gst_get_arch_suffix (void) |
| 210 |
{ |
| 211 |
gint retval; |
| 212 |
const gchar *suffix = ""; |
| 213 |
struct utsname buf; |
| 214 |
|
| 215 |
retval = uname (&buf); |
| 216 |
|
| 217 |
/* did we get valid value? */ |
| 218 |
if (retval != 0 || buf.machine[0] == '\0') { |
| 219 |
g_warning ("PackageKit: cannot get machine type"); |
| 220 |
goto out; |
| 221 |
} |
| 222 |
|
| 223 |
/* 32 bit machines */ |
| 224 |
if (g_strcmp0 (buf.machine, "i386") == 0 || |
| 225 |
g_strcmp0 (buf.machine, "i586") == 0 || |
| 226 |
g_strcmp0 (buf.machine, "i686") == 0) |
| 227 |
goto out; |
| 228 |
|
| 229 |
/* 64 bit machines */ |
| 230 |
if (g_strcmp0 (buf.machine, "x86_64") == 0) { |
| 231 |
suffix = "()(64bit)"; |
| 232 |
goto out; |
| 233 |
} |
| 234 |
|
| 235 |
/* fall back */ |
| 236 |
if (sizeof (void *) == 8) { |
| 237 |
suffix = "()(64bit)"; |
| 238 |
g_debug ("PackageKit: assume 64-bit suffix for machine type: '%s'", buf.machine); |
| 239 |
} else { |
| 240 |
g_debug ("PackageKit: assume no suffix for machine type: '%s'", buf.machine); |
| 241 |
} |
| 242 |
|
| 243 |
out: |
| 244 |
return suffix; |
| 245 |
} |
| 246 |
|
| 247 |
static GVariant * |
| 248 |
make_platform_data (const gchar *startup_id) |
| 249 |
{ |
| 250 |
GVariantBuilder builder; |
| 251 |
|
| 252 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}")); |
| 253 |
|
| 254 |
if (startup_id && g_utf8_validate (startup_id, -1, NULL)) { |
| 255 |
g_variant_builder_add (&builder, "{sv}", |
| 256 |
"desktop-startup-id", g_variant_new_string (startup_id)); |
| 257 |
} |
| 258 |
|
| 259 |
return g_variant_builder_end (&builder); |
| 260 |
} |
| 261 |
|
| 262 |
static gboolean |
| 263 |
pk_gst_dbus_install_resources (gchar **resources, const gchar *desktop_id, const gchar *startup_id, const gchar *interaction, GError **error) |
| 264 |
{ |
| 265 |
g_autoptr(GDBusProxy) proxy = NULL; |
| 266 |
g_autoptr(GVariant) value = NULL; |
| 267 |
|
| 268 |
/* get proxy */ |
| 269 |
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, |
| 270 |
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 271 |
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 272 |
NULL, |
| 273 |
"org.freedesktop.PackageKit", |
| 274 |
"/org/freedesktop/PackageKit", |
| 275 |
"org.freedesktop.PackageKit.Modify2", |
| 276 |
NULL, |
| 277 |
error); |
| 278 |
if (proxy != NULL) { |
| 279 |
/* invoke the method */ |
| 280 |
value = g_dbus_proxy_call_sync (proxy, |
| 281 |
"InstallGStreamerResources", |
| 282 |
g_variant_new ("(^a&sss@a{sv})", |
| 283 |
resources, |
| 284 |
interaction ? interaction : "", |
| 285 |
desktop_id ? desktop_id : "", |
| 286 |
make_platform_data (startup_id)), |
| 287 |
G_DBUS_CALL_FLAGS_NONE, |
| 288 |
60 * 60 * 1000, /* 1 hour */ |
| 289 |
NULL, |
| 290 |
error); |
| 291 |
if (value != NULL) |
| 292 |
return TRUE; |
| 293 |
} |
| 294 |
|
| 295 |
return FALSE; |
| 296 |
} |
| 297 |
|
| 298 |
static gboolean |
| 299 |
pk_gst_dbus_install_resources_compat (gchar **resources, gint xid, GError **error) |
| 300 |
{ |
| 301 |
g_autoptr(GDBusProxy) proxy = NULL; |
| 302 |
g_autoptr(GVariant) value = NULL; |
| 303 |
|
| 304 |
/* get proxy */ |
| 305 |
proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, |
| 306 |
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
| 307 |
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, |
| 308 |
NULL, |
| 309 |
"org.freedesktop.PackageKit", |
| 310 |
"/org/freedesktop/PackageKit", |
| 311 |
"org.freedesktop.PackageKit.Modify", |
| 312 |
NULL, |
| 313 |
error); |
| 314 |
if (proxy != NULL) { |
| 315 |
/* invoke the method */ |
| 316 |
value = g_dbus_proxy_call_sync (proxy, |
| 317 |
"InstallGStreamerResources", |
| 318 |
g_variant_new ("(u^a&ss)", |
| 319 |
xid, |
| 320 |
resources, |
| 321 |
"hide-finished"), |
| 322 |
G_DBUS_CALL_FLAGS_NONE, |
| 323 |
60 * 60 * 1000, /* 1 hour */ |
| 324 |
NULL, |
| 325 |
error); |
| 326 |
if (value != NULL) |
| 327 |
return TRUE; |
| 328 |
} |
| 329 |
|
| 330 |
return FALSE; |
| 331 |
} |
| 332 |
|
| 333 |
int |
| 334 |
main (int argc, gchar **argv) |
| 335 |
{ |
| 336 |
GOptionContext *context; |
| 337 |
guint i; |
| 338 |
guint len; |
| 339 |
gboolean ret; |
| 340 |
gchar **codecs = NULL; |
| 341 |
gint xid = 0; |
| 342 |
const gchar *suffix; |
| 343 |
gchar *resource; |
| 344 |
g_autoptr(GError) error = NULL; |
| 345 |
g_autofree gchar *desktop_id = NULL; |
| 346 |
g_autofree gchar *interaction = NULL; |
| 347 |
g_autofree gchar *startup_id = NULL; |
| 348 |
g_autoptr(GPtrArray) array = NULL; |
| 349 |
g_auto(GStrv) resources = NULL; |
| 350 |
|
| 351 |
const GOptionEntry options[] = { |
| 352 |
{ "transient-for", '\0', 0, G_OPTION_ARG_INT, &xid, "The XID of the parent window", NULL }, |
| 353 |
{ "desktop-id", '\0', 0, G_OPTION_ARG_STRING, &desktop_id, "The desktop ID of the calling application", NULL }, |
| 354 |
{ "interaction", '\0', 0, G_OPTION_ARG_STRING, &interaction, "Interaction mode specifying which UI elements should be shown", NULL }, |
| 355 |
{ "startup-notification-id", '\0', 0, G_OPTION_ARG_STRING, &startup_id, "The startup notification ID for focus stealing prevention", NULL }, |
| 356 |
{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &codecs, "GStreamer install infos", NULL }, |
| 357 |
G_OPTION_ENTRY_NULL |
| 358 |
}; |
| 359 |
|
| 360 |
gst_init (&argc, &argv); |
| 361 |
|
| 362 |
context = g_option_context_new ("Install missing codecs"); |
| 363 |
g_option_context_add_main_entries (context, options, NULL); |
| 364 |
|
| 365 |
if (!g_option_context_parse (context, &argc, &argv, &error)) { |
| 366 |
g_print ("%s\nRun '%s --help' to see a full list of available command line options.\n", |
| 367 |
error->message, argv[0]); |
| 368 |
return GST_INSTALL_PLUGINS_ERROR; |
| 369 |
} |
| 370 |
if (codecs == NULL) { |
| 371 |
g_print ("Missing codecs information\n"); |
| 372 |
g_print ("Run 'with --help' to see a full list of available command line options.\n"); |
| 373 |
return GST_INSTALL_PLUGINS_ERROR; |
| 374 |
} |
| 375 |
|
| 376 |
/* this is our parent window */ |
| 377 |
g_message ("PackageKit: xid = %i", xid); |
| 378 |
g_message ("PackageKit: desktop_id = %s", desktop_id); |
| 379 |
|
| 380 |
/* use a ()(64bit) suffix for 64 bit */ |
| 381 |
suffix = pk_gst_get_arch_suffix (); |
| 382 |
|
| 383 |
array = g_ptr_array_new_with_free_func (g_free); |
| 384 |
len = g_strv_length (codecs); |
| 385 |
|
| 386 |
/* process argv */ |
| 387 |
for (i = 0; i < len; i++) { |
| 388 |
PkGstCodecInfo *info; |
| 389 |
gchar *type; |
| 390 |
const gchar *gstreamer_version; |
| 391 |
|
| 392 |
info = pk_gst_parse_codec (codecs[i]); |
| 393 |
if (info == NULL) { |
| 394 |
g_message ("skipping %s", codecs[i]); |
| 395 |
continue; |
| 396 |
} |
| 397 |
|
| 398 |
/* gstreamer1 is the provide name used for the |
| 399 |
* first version of the new release */ |
| 400 |
if (g_strcmp0 (info->gstreamer_version, "1.0") == 0) |
| 401 |
gstreamer_version = "1"; |
| 402 |
else |
| 403 |
gstreamer_version = info->gstreamer_version; |
| 404 |
|
| 405 |
g_message ("PackageKit: Codec nice name: %s", info->codec_name); |
| 406 |
if (info->structure != NULL) { |
| 407 |
g_autofree gchar *s = NULL; |
| 408 |
s = pk_gst_structure_to_provide (info->structure); |
| 409 |
type = g_strdup_printf ("gstreamer%s(%s-%s)%s%s", |
| 410 |
gstreamer_version, |
| 411 |
info->type_name, |
| 412 |
gst_structure_get_name (info->structure), |
| 413 |
s, suffix); |
| 414 |
g_message ("PackageKit: structure: %s", type); |
| 415 |
} else { |
| 416 |
type = g_strdup_printf ("gstreamer%s(%s)%s", |
| 417 |
gstreamer_version, |
| 418 |
info->type_name, |
| 419 |
suffix); |
| 420 |
g_message ("PackageKit: non-structure: %s", type); |
| 421 |
} |
| 422 |
|
| 423 |
/* "encode" */ |
| 424 |
resource = g_strdup_printf ("%s|%s", info->codec_name, type); |
| 425 |
g_ptr_array_add (array, resource); |
| 426 |
|
| 427 |
/* free codec structure */ |
| 428 |
pk_gst_codec_free (info); |
| 429 |
} |
| 430 |
|
| 431 |
/* nothing parsed */ |
| 432 |
if (array->len == 0) { |
| 433 |
g_message ("no codec lines could be parsed"); |
| 434 |
return GST_INSTALL_PLUGINS_ERROR; |
| 435 |
} |
| 436 |
|
| 437 |
/* convert to a GStrv */ |
| 438 |
resources = pk_ptr_array_to_strv (array); |
| 439 |
|
| 440 |
/* first try the new interface */ |
| 441 |
ret = pk_gst_dbus_install_resources (resources, desktop_id, startup_id, interaction, &error); |
| 442 |
if (g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) { |
| 443 |
/* ... and if that fails, fall back to the compat interface */ |
| 444 |
g_clear_error (&error); |
| 445 |
g_message ("PackageKit: falling back to compat dbus interface"); |
| 446 |
ret = pk_gst_dbus_install_resources_compat (resources, xid, &error); |
| 447 |
} |
| 448 |
if (!ret) { |
| 449 |
/* use the error string to return a good GStreamer exit code */ |
| 450 |
g_message ("PackageKit: Did not install codec: %s", error->message); |
| 451 |
if (g_strrstr (error->message, "did not agree to search") != NULL) |
| 452 |
return GST_INSTALL_PLUGINS_USER_ABORT; |
| 453 |
if (g_strrstr (error->message, "not all codecs were installed") != NULL) |
| 454 |
return GST_INSTALL_PLUGINS_PARTIAL_SUCCESS; |
| 455 |
return GST_INSTALL_PLUGINS_NOT_FOUND; |
| 456 |
} |
| 457 |
return GST_INSTALL_PLUGINS_SUCCESS; |
| 458 |
} |
| 459 |
|