1 /* GStreamer
2 * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
3 *
4 * gstpreset.c: helper interface for element presets
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21 /**
22 * SECTION:gstpreset
23 * @title: GstPreset
24 * @short_description: helper interface for element presets
25 *
26 * This interface offers methods to query and manipulate parameter preset sets.
27 * A preset is a bunch of property settings, together with meta data and a name.
28 * The name of a preset serves as key for subsequent method calls to manipulate
29 * single presets.
30 * All instances of one type will share the list of presets. The list is created
31 * on demand, if presets are not used, the list is not created.
32 *
33 * The interface comes with a default implementation that serves most plugins.
34 * Wrapper plugins will override most methods to implement support for the
35 * native preset format of those wrapped plugins.
36 * One method that is useful to be overridden is gst_preset_get_property_names().
37 * With that one can control which properties are saved and in which order.
38 * When implementing support for read-only presets, one should set the vmethods
39 * for gst_preset_save_preset() and gst_preset_delete_preset() to %NULL.
40 * Applications can use gst_preset_is_editable() to check for that.
41 *
42 * The default implementation supports presets located in a system directory,
43 * application specific directory and in the users home directory. When getting
44 * a list of presets individual presets are read and overlaid in 1) system,
45 * 2) application and 3) user order. Whenever an earlier entry is newer, the
46 * later entries will be updated. Since 1.8 you can also provide extra paths
47 * where to find presets through the GST_PRESET_PATH environment variable.
48 * Presets found in those paths will be considered as "app presets".
49 */
50 /* FIXME:
51 * - non racyness
52 * - we need to avoid two instances writing the preset file
53 * -> flock(fileno()), http://www.ecst.csuchico.edu/~beej/guide/ipc/flock.html
54 * -> open exclusive
55 * -> better save the new file to a tempfile and then rename?
56 * - we like to know when any other instance makes changes to the keyfile
57 * - then ui can be updated
58 * - and we make sure that we don't lose edits
59 * -> its the same problem actually, once for inside a process, once system-
60 * wide
61 * - can we use a lock inside a names shared memory segment?
62 *
63 * - should there be a 'preset-list' property to get the preset list
64 * (and to connect a notify:: to to listen for changes)
65 * we could use gnome_vfs_monitor_add() to monitor the user preset_file.
66 *
67 * - should there be a 'preset-name' property so that we can set a preset via
68 * gst-launch, or should we handle this with special syntax in gst-launch:
69 * gst-launch element preset:<preset-name> property=value ...
70 * - this would allow to have preset-bundles too (a preset on bins that
71 * specifies presets for children
72 */
73
74 #include "gst_private.h"
75
76 #include "gstpreset.h"
77 #include "gstchildproxy.h"
78 #include "gstinfo.h"
79 #include "gstvalue.h"
80
81 #ifdef HAVE_UNISTD_H
82 #include <unistd.h>
83 #endif
84 #include <glib/gstdio.h>
85
86 #ifdef G_OS_WIN32
87 #define WIN32_LEAN_AND_MEAN
88 #include <windows.h>
89
90 extern HMODULE _priv_gst_dll_handle;
91 #endif
92
93 #define GST_CAT_DEFAULT preset_debug
94 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
95
96 /* defines for keyfile usage, this group contains the element type name and
97 * version these presets belong to. */
98 #define PRESET_HEADER "_presets_"
99
100 /* keys of the preset header section */
101 #define PRESET_HEADER_ELEMENT_NAME "element-name"
102 #define PRESET_HEADER_VERSION "version"
103
104 static GQuark preset_user_path_quark = 0;
105 static GQuark preset_app_path_quark = 0;
106 static GQuark preset_system_path_quark = 0;
107 static GQuark preset_quark = 0;
108
109 /* the application can set a custom path that is checked in addition to standard
110 * system and user dirs. This helps to develop new presets first local to the
111 * application.
112 */
113 static gchar *preset_app_dir = NULL;
114
115 /* default iface implementation */
116
117 static gboolean gst_preset_default_save_presets_file (GstPreset * preset);
118
119 /*
120 * preset_get_paths:
121 * @preset: a #GObject that implements #GstPreset
122 * @preset_user_path: (out) (allow-none): location for path or %NULL
123 * @preset_app_path: (out) (allow-none): location for path or %NULL
124 * @preset_system_path: (out) (allow-none): location for path or %NULL
125 *
126 * Fetch the preset_path for user local, application specific and system wide
127 * settings. Don't free after use.
128 *
129 * Returns: %FALSE if no paths could be found.
130 */
131 static gboolean
preset_get_paths(GstPreset * preset,const gchar ** preset_user_path,const gchar ** preset_app_path,const gchar ** preset_system_path)132 preset_get_paths (GstPreset * preset, const gchar ** preset_user_path,
133 const gchar ** preset_app_path, const gchar ** preset_system_path)
134 {
135 GType type = G_TYPE_FROM_INSTANCE (preset);
136 gchar *preset_path;
137 const gchar *element_name;
138
139 /* we use the element name when we must construct the paths */
140 element_name = G_OBJECT_TYPE_NAME (preset);
141 GST_INFO_OBJECT (preset, "element_name: '%s'", element_name);
142
143 if (preset_user_path) {
144 /* preset user path requested, see if we have it cached in the qdata */
145 if (!(preset_path = g_type_get_qdata (type, preset_user_path_quark))) {
146 gchar *preset_dir;
147
148 /* user presets go in user's XDG data directory. */
149 preset_dir = g_build_filename (g_get_user_data_dir (),
150 "gstreamer-" GST_API_VERSION, "presets", NULL);
151 GST_INFO_OBJECT (preset, "user_preset_dir: '%s'", preset_dir);
152 preset_path =
153 g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs", preset_dir,
154 element_name);
155 GST_INFO_OBJECT (preset, "user_preset_path: '%s'", preset_path);
156 /* create dirs */
157 g_mkdir_with_parents (preset_dir, 0755);
158 g_free (preset_dir);
159
160 /* cache the preset path to the type */
161 g_type_set_qdata (type, preset_user_path_quark, preset_path);
162 }
163 *preset_user_path = preset_path;
164 }
165
166 if (preset_app_path) {
167 if (preset_app_dir) {
168 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
169 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
170 preset_app_dir, element_name);
171 GST_INFO_OBJECT (preset, "application_preset_path: '%s'", preset_path);
172
173 /* cache the preset path to the type */
174 g_type_set_qdata (type, preset_app_path_quark, preset_path);
175 }
176 *preset_app_path = preset_path;
177 } else {
178 *preset_app_path = NULL;
179 }
180 }
181
182 if (preset_system_path) {
183 /* preset system path requested, see if we have it cached in the qdata */
184 if (!(preset_path = g_type_get_qdata (type, preset_system_path_quark))) {
185 gchar *preset_dir;
186
187 /* system presets in '$GST_DATADIR/gstreamer-1.0/presets/GstAudioPanorama.prs' */
188 #ifdef G_OS_WIN32
189 gchar *basedir =
190 g_win32_get_package_installation_directory_of_module
191 (_priv_gst_dll_handle);
192 preset_dir =
193 g_build_filename (basedir, "share", "gstreamer-" GST_API_VERSION,
194 "presets", NULL);
195 g_free (basedir);
196 #else
197 preset_dir = g_build_filename (GST_DATADIR, "gstreamer-" GST_API_VERSION,
198 "presets", NULL);
199 #endif
200 GST_INFO_OBJECT (preset, "system_preset_dir: '%s'", preset_dir);
201 preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
202 preset_dir, element_name);
203 GST_INFO_OBJECT (preset, "system_preset_path: '%s'", preset_path);
204 /* create dirs */
205 g_mkdir_with_parents (preset_dir, 0755);
206 g_free (preset_dir);
207
208 /* cache the preset path to the type */
209 g_type_set_qdata (type, preset_system_path_quark, preset_path);
210 }
211 *preset_system_path = preset_path;
212 }
213 return TRUE;
214 }
215
216 static gboolean
preset_skip_property(GParamSpec * property)217 preset_skip_property (GParamSpec * property)
218 {
219 if (((property->flags & G_PARAM_READWRITE) != G_PARAM_READWRITE) ||
220 (property->flags & G_PARAM_CONSTRUCT_ONLY))
221 return TRUE;
222 /* FIXME: skip GST_PARAM_NOT_PRESETABLE, see #522205 */
223 return FALSE;
224 }
225
226 static guint64
preset_parse_version(const gchar * str_version)227 preset_parse_version (const gchar * str_version)
228 {
229 guint major, minor, micro, nano;
230 gint num;
231
232 major = minor = micro = nano = 0;
233
234 /* parse version (e.g. 0.10.15.1) to guint64 */
235 num = sscanf (str_version, "%u.%u.%u.%u", &major, &minor, µ, &nano);
236 /* make sure we have at least "major.minor" */
237 if (num > 1) {
238 guint64 version;
239
240 version = ((((major << 8 | minor) << 8) | micro) << 8) | nano;
241 GST_DEBUG ("version %s -> %" G_GUINT64_FORMAT, str_version, version);
242 return version;
243 }
244 return G_GUINT64_CONSTANT (0);
245 }
246
247 static GKeyFile *
preset_open_and_parse_header(GstPreset * preset,const gchar * preset_path,guint64 * preset_version)248 preset_open_and_parse_header (GstPreset * preset, const gchar * preset_path,
249 guint64 * preset_version)
250 {
251 GKeyFile *in;
252 GError *error = NULL;
253 gboolean res;
254 const gchar *element_name;
255 gchar *name;
256
257 in = g_key_file_new ();
258
259 res = g_key_file_load_from_file (in, preset_path,
260 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error);
261 if (!res || error != NULL)
262 goto load_error;
263
264 /* element type name and preset name must match or we are dealing with a wrong
265 * preset file */
266 element_name = G_OBJECT_TYPE_NAME (preset);
267 name =
268 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
269 NULL);
270
271 if (!name || strcmp (name, element_name))
272 goto wrong_name;
273
274 g_free (name);
275
276 /* get the version now so that the caller can check it */
277 if (preset_version) {
278 gchar *str =
279 g_key_file_get_value (in, PRESET_HEADER, PRESET_HEADER_VERSION, NULL);
280 *preset_version = preset_parse_version (str);
281 g_free (str);
282 }
283
284 return in;
285
286 /* ERRORS */
287 load_error:
288 {
289 GST_INFO_OBJECT (preset, "Unable to read preset file %s: %s",
290 preset_path, error->message);
291 g_error_free (error);
292 g_key_file_free (in);
293 return NULL;
294 }
295 wrong_name:
296 {
297 GST_WARNING_OBJECT (preset,
298 "Wrong element name in preset file %s. Expected %s, got %s",
299 preset_path, element_name, GST_STR_NULL (name));
300 g_free (name);
301 g_key_file_free (in);
302 return NULL;
303 }
304 }
305
306 static void
preset_merge(GKeyFile * system,GKeyFile * user)307 preset_merge (GKeyFile * system, GKeyFile * user)
308 {
309 gchar *str;
310 gchar **groups, **keys;
311 gsize i, j, num_groups, num_keys;
312
313 /* copy file comment if there is any */
314 if ((str = g_key_file_get_comment (user, NULL, NULL, NULL))) {
315 g_key_file_set_comment (system, NULL, NULL, str, NULL);
316 g_free (str);
317 }
318
319 /* get groups in user and copy into system */
320 groups = g_key_file_get_groups (user, &num_groups);
321 for (i = 0; i < num_groups; i++) {
322 /* copy group comment if there is any */
323 if ((str = g_key_file_get_comment (user, groups[i], NULL, NULL))) {
324 g_key_file_set_comment (system, groups[i], NULL, str, NULL);
325 g_free (str);
326 }
327
328 /* ignore private groups */
329 if (groups[i][0] == '_')
330 continue;
331
332 /* if group already exists in system, remove and re-add keys from user */
333 if (g_key_file_has_group (system, groups[i])) {
334 g_key_file_remove_group (system, groups[i], NULL);
335 }
336
337 keys = g_key_file_get_keys (user, groups[i], &num_keys, NULL);
338 for (j = 0; j < num_keys; j++) {
339 /* copy key comment if there is any */
340 if ((str = g_key_file_get_comment (user, groups[i], keys[j], NULL))) {
341 g_key_file_set_comment (system, groups[i], keys[j], str, NULL);
342 g_free (str);
343 }
344 str = g_key_file_get_value (user, groups[i], keys[j], NULL);
345 g_key_file_set_value (system, groups[i], keys[j], str);
346 g_free (str);
347 }
348 g_strfreev (keys);
349 }
350 g_strfreev (groups);
351 }
352
353 typedef struct
354 {
355 GKeyFile *preset;
356 guint64 version;
357 } PresetAndVersion;
358
359 static gint
compare_preset_and_version(gconstpointer a,gconstpointer b,gpointer user_data)360 compare_preset_and_version (gconstpointer a, gconstpointer b,
361 gpointer user_data)
362 {
363 const PresetAndVersion *pa = a, *pb = b;
364
365 if (pa->version > pb->version)
366 return -1;
367 if (pa->version < pb->version)
368 return 1;
369 else
370 return 0;
371 }
372
373 /* reads the user and system presets files and merges them together. This
374 * function caches the GKeyFile on the element type. If there is no existing
375 * preset file, a new in-memory GKeyFile will be created. */
376 static GKeyFile *
preset_get_keyfile(GstPreset * preset)377 preset_get_keyfile (GstPreset * preset)
378 {
379 GKeyFile *presets;
380 GType type = G_TYPE_FROM_INSTANCE (preset);
381
382 /* first see if the have a cached version for the type */
383 if (!(presets = g_type_get_qdata (type, preset_quark))) {
384 const gchar *preset_user_path, *preset_app_path, *preset_system_path;
385 guint64 version_system = G_GUINT64_CONSTANT (0);
386 guint64 version_app = G_GUINT64_CONSTANT (0);
387 guint64 version_user = G_GUINT64_CONSTANT (0);
388 guint64 version = G_GUINT64_CONSTANT (0);
389 gboolean merged = FALSE;
390 GKeyFile *in_user, *in_app = NULL, *in_system;
391 GQueue in_env = G_QUEUE_INIT;
392 gboolean have_env = FALSE;
393 const gchar *envvar;
394
395 /* try to load the user, app and system presets, we do this to get the
396 * versions of all files. */
397 preset_get_paths (preset, &preset_user_path, &preset_app_path,
398 &preset_system_path);
399 in_user = preset_open_and_parse_header (preset, preset_user_path,
400 &version_user);
401
402 if (preset_app_path) {
403 in_app = preset_open_and_parse_header (preset, preset_app_path,
404 &version_app);
405 }
406
407 envvar = g_getenv ("GST_PRESET_PATH");
408 if (envvar) {
409 gint i;
410 gchar **preset_dirs = g_strsplit (envvar, G_SEARCHPATH_SEPARATOR_S, -1);
411
412 for (i = 0; preset_dirs[i]; i++) {
413 gchar *preset_path = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "%s.prs",
414 preset_dirs[i], G_OBJECT_TYPE_NAME (preset));
415 GKeyFile *env_file;
416 guint64 env_version;
417
418 env_file = preset_open_and_parse_header (preset, preset_path,
419 &env_version);
420 g_free (preset_path);
421 if (env_file) {
422 PresetAndVersion *pv = g_new (PresetAndVersion, 1);
423 pv->preset = env_file;
424 pv->version = env_version;
425 g_queue_push_tail (&in_env, pv);
426 have_env = TRUE;
427 }
428 }
429 g_strfreev (preset_dirs);
430 }
431
432 in_system = preset_open_and_parse_header (preset, preset_system_path,
433 &version_system);
434
435 /* compare version to check for merge */
436 if (in_system) {
437 presets = in_system;
438 version = version_system;
439 }
440
441 if (have_env) {
442 GList *l;
443
444 /* merge the ones from the environment paths. If any of them has a
445 * higher version, take that as the "master" version. Lower versions are
446 * then just merged in. */
447 g_queue_sort (&in_env, compare_preset_and_version, NULL);
448 /* highest version to lowest */
449 for (l = in_env.head; l; l = l->next) {
450 PresetAndVersion *pv = l->data;
451
452 if (version > pv->version) {
453 preset_merge (presets, pv->preset);
454 g_key_file_free (pv->preset);
455 } else {
456 if (presets)
457 g_key_file_free (presets);
458 presets = pv->preset;
459 version = pv->version;
460 }
461 g_free (pv);
462 }
463 g_queue_clear (&in_env);
464 }
465
466 if (in_app) {
467 /* if system/env version is higher, merge */
468 if (version > version_app) {
469 preset_merge (presets, in_app);
470 g_key_file_free (in_app);
471 } else {
472 if (presets)
473 g_key_file_free (presets);
474 presets = in_app;
475 version = version_app;
476 }
477 }
478 if (in_user) {
479 /* if system/env or app version is higher, merge */
480 if (version > version_user) {
481 preset_merge (presets, in_user);
482 g_key_file_free (in_user);
483 merged = TRUE;
484 } else {
485 if (presets)
486 g_key_file_free (presets);
487 presets = in_user;
488 }
489 }
490
491 if (!presets) {
492 /* we did not load a user, app or system presets file, create a new one */
493 presets = g_key_file_new ();
494 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_ELEMENT_NAME,
495 G_OBJECT_TYPE_NAME (preset));
496 }
497
498 /* attach the preset to the type */
499 g_type_set_qdata (type, preset_quark, (gpointer) presets);
500
501 if (merged) {
502 gst_preset_default_save_presets_file (preset);
503 }
504 }
505 return presets;
506 }
507
508 static gint
compare_strings(gchar ** a,gchar ** b,gpointer user_data)509 compare_strings (gchar ** a, gchar ** b, gpointer user_data)
510 {
511 return g_strcmp0 (*a, *b);
512 }
513
514 /* get a list of all supported preset names for an element */
515 static gchar **
gst_preset_default_get_preset_names(GstPreset * preset)516 gst_preset_default_get_preset_names (GstPreset * preset)
517 {
518 GKeyFile *presets;
519 gsize i, num_groups;
520 gchar **groups;
521
522 /* get the presets from the type */
523 if (!(presets = preset_get_keyfile (preset)))
524 goto no_presets;
525
526 /* get the groups, which are also the preset names */
527 if (!(groups = g_key_file_get_groups (presets, &num_groups)))
528 goto no_groups;
529
530 /* remove all private group names starting with '_' from the array */
531 for (i = 0; i < num_groups; i++) {
532 if (groups[i][0] == '_') {
533 /* free private group */
534 g_free (groups[i]);
535 /* move last element of list down */
536 num_groups--;
537 /* move last element into removed element */
538 groups[i] = groups[num_groups];
539 groups[num_groups] = NULL;
540 }
541 }
542 if (!num_groups) {
543 GST_INFO_OBJECT (preset, "Empty preset file");
544 g_strfreev (groups);
545 return NULL;
546 }
547
548 /* sort the array now */
549 g_qsort_with_data (groups, num_groups, sizeof (gchar *),
550 (GCompareDataFunc) compare_strings, NULL);
551
552 return groups;
553
554 /* ERRORS */
555 no_presets:
556 {
557 GST_WARNING_OBJECT (preset, "Could not load presets");
558 return NULL;
559 }
560 no_groups:
561 {
562 GST_WARNING_OBJECT (preset, "Could not find preset groups");
563 return NULL;
564 }
565 }
566
567 /* get a list of all property names that are used for presets */
568 static gchar **
gst_preset_default_get_property_names(GstPreset * preset)569 gst_preset_default_get_property_names (GstPreset * preset)
570 {
571 GParamSpec **props;
572 guint i, j = 0, n_props;
573 GObjectClass *gclass;
574 gboolean is_child_proxy;
575 gchar **result = NULL;
576
577 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
578 is_child_proxy = GST_IS_CHILD_PROXY (preset);
579
580 /* get a list of object properties */
581 props = g_object_class_list_properties (gclass, &n_props);
582 if (props) {
583 /* allocate array big enough to hold the worst case, including a terminating
584 * NULL pointer. */
585 result = g_new (gchar *, n_props + 1);
586
587 /* now filter out the properties that we can use for presets */
588 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
589 for (i = 0; i < n_props; i++) {
590 if (preset_skip_property (props[i]))
591 continue;
592 GST_DEBUG_OBJECT (preset, " using: %s", props[i]->name);
593 result[j++] = g_strdup (props[i]->name);
594 }
595 g_free (props);
596 }
597
598 if (is_child_proxy) {
599 guint c, n_children;
600 GObject *child;
601
602 n_children = gst_child_proxy_get_children_count ((GstChildProxy *) preset);
603 for (c = 0; c < n_children; c++) {
604 child = gst_child_proxy_get_child_by_index ((GstChildProxy *) preset, c);
605 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (child));
606
607 props = g_object_class_list_properties (gclass, &n_props);
608 if (props) {
609 /* resize property name array */
610 result = g_renew (gchar *, result, j + n_props + 1);
611
612 /* now filter out the properties that we can use for presets */
613 GST_DEBUG_OBJECT (preset, " filtering properties: %u", n_props);
614 for (i = 0; i < n_props; i++) {
615 if (preset_skip_property (props[i]))
616 continue;
617 GST_DEBUG_OBJECT (preset, " using: %s::%s",
618 GST_OBJECT_NAME (child), props[i]->name);
619 result[j++] = g_strdup_printf ("%s::%s", GST_OBJECT_NAME (child),
620 props[i]->name);
621 }
622 g_free (props);
623 }
624
625 gst_object_unref (child);
626 }
627 }
628 if (!result) {
629 GST_INFO_OBJECT (preset, "object has no properties");
630 } else {
631 result[j] = NULL;
632 }
633 return result;
634 }
635
636 /* load the presets of @name for the instance @preset. Returns %FALSE if something
637 * failed. */
638 static gboolean
gst_preset_default_load_preset(GstPreset * preset,const gchar * name)639 gst_preset_default_load_preset (GstPreset * preset, const gchar * name)
640 {
641 GKeyFile *presets;
642 gchar **props;
643 guint i;
644 GObjectClass *gclass;
645 gboolean is_child_proxy;
646
647 /* get the presets from the type */
648 if (!(presets = preset_get_keyfile (preset)))
649 goto no_presets;
650
651 /* get the preset name */
652 if (!g_key_file_has_group (presets, name))
653 goto no_group;
654
655 GST_DEBUG_OBJECT (preset, "loading preset : '%s'", name);
656
657 /* get the properties that we can configure in this element */
658 if (!(props = gst_preset_get_property_names (preset)))
659 goto no_properties;
660
661 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
662 is_child_proxy = GST_IS_CHILD_PROXY (preset);
663
664 /* for each of the property names, find the preset parameter and try to
665 * configure the property with its value */
666 for (i = 0; props[i]; i++) {
667 gchar *str;
668 GValue gvalue = { 0, };
669 GParamSpec *property = NULL;
670
671 /* check if we have a settings for this element property */
672 if (!(str = g_key_file_get_value (presets, name, props[i], NULL))) {
673 /* the element has a property but the parameter is not in the keyfile */
674 GST_INFO_OBJECT (preset, "parameter '%s' not in preset", props[i]);
675 continue;
676 }
677
678 GST_DEBUG_OBJECT (preset, "setting value '%s' for property '%s'", str,
679 props[i]);
680
681 if (is_child_proxy) {
682 gst_child_proxy_lookup ((GstChildProxy *) preset, props[i], NULL,
683 &property);
684 } else {
685 property = g_object_class_find_property (gclass, props[i]);
686 }
687 if (!property) {
688 /* the parameter was in the keyfile, the element said it supported it but
689 * then the property was not found in the element. This should not happen. */
690 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
691 g_free (str);
692 continue;
693 }
694
695 /* try to deserialize the property value from the keyfile and set it as
696 * the object property */
697 g_value_init (&gvalue, property->value_type);
698 if (gst_value_deserialize (&gvalue, str)) {
699 if (is_child_proxy) {
700 gst_child_proxy_set_property ((GstChildProxy *) preset, props[i],
701 &gvalue);
702 } else {
703 g_object_set_property ((GObject *) preset, props[i], &gvalue);
704 }
705 } else {
706 GST_WARNING_OBJECT (preset,
707 "deserialization of value '%s' for property '%s' failed", str,
708 props[i]);
709 }
710 g_value_unset (&gvalue);
711 g_free (str);
712 }
713 g_strfreev (props);
714
715 return TRUE;
716
717 /* ERRORS */
718 no_presets:
719 {
720 GST_WARNING_OBJECT (preset, "no presets");
721 return FALSE;
722 }
723 no_group:
724 {
725 GST_WARNING_OBJECT (preset, "no preset named '%s'", name);
726 return FALSE;
727 }
728 no_properties:
729 {
730 GST_INFO_OBJECT (preset, "no properties");
731 return FALSE;
732 }
733 }
734
735 /* save the presets file. A copy of the existing presets file is stored in a
736 * .bak file */
737 static gboolean
gst_preset_default_save_presets_file(GstPreset * preset)738 gst_preset_default_save_presets_file (GstPreset * preset)
739 {
740 GKeyFile *presets;
741 const gchar *preset_path;
742 GError *error = NULL;
743 gchar *bak_file_name;
744 gboolean backup = TRUE;
745 gchar *data;
746 gsize data_size;
747
748 preset_get_paths (preset, &preset_path, NULL, NULL);
749
750 /* get the presets from the type */
751 if (!(presets = preset_get_keyfile (preset)))
752 goto no_presets;
753
754 GST_DEBUG_OBJECT (preset, "saving preset file: '%s'", preset_path);
755
756 /* create backup if possible */
757 bak_file_name = g_strdup_printf ("%s.bak", preset_path);
758 if (g_file_test (bak_file_name, G_FILE_TEST_EXISTS)) {
759 if (g_unlink (bak_file_name)) {
760 backup = FALSE;
761 GST_INFO_OBJECT (preset, "cannot remove old backup file : %s",
762 bak_file_name);
763 }
764 }
765 if (backup) {
766 if (g_rename (preset_path, bak_file_name)) {
767 GST_INFO_OBJECT (preset, "cannot backup file : %s -> %s", preset_path,
768 bak_file_name);
769 }
770 }
771 g_free (bak_file_name);
772
773 /* update gstreamer version */
774 g_key_file_set_string (presets, PRESET_HEADER, PRESET_HEADER_VERSION,
775 PACKAGE_VERSION);
776
777 /* get new contents, wee need this to save it */
778 if (!(data = g_key_file_to_data (presets, &data_size, &error)))
779 goto convert_failed;
780
781 /* write presets */
782 if (!g_file_set_contents (preset_path, data, data_size, &error))
783 goto write_failed;
784
785 g_free (data);
786
787 return TRUE;
788
789 /* ERRORS */
790 no_presets:
791 {
792 GST_WARNING_OBJECT (preset,
793 "no presets, trying to unlink possibly existing preset file: '%s'",
794 preset_path);
795 g_unlink (preset_path);
796 return FALSE;
797 }
798 convert_failed:
799 {
800 GST_WARNING_OBJECT (preset, "can not get the keyfile contents: %s",
801 error->message);
802 g_error_free (error);
803 g_free (data);
804 return FALSE;
805 }
806 write_failed:
807 {
808 GST_WARNING_OBJECT (preset, "Unable to store preset file %s: %s",
809 preset_path, error->message);
810 g_error_free (error);
811 g_free (data);
812 return FALSE;
813 }
814 }
815
816 /* save the preset with the given name */
817 static gboolean
gst_preset_default_save_preset(GstPreset * preset,const gchar * name)818 gst_preset_default_save_preset (GstPreset * preset, const gchar * name)
819 {
820 GKeyFile *presets;
821 gchar **props;
822 guint i;
823 GObjectClass *gclass;
824 gboolean is_child_proxy;
825
826 GST_INFO_OBJECT (preset, "saving new preset: %s", name);
827
828 /* get the presets from the type */
829 if (!(presets = preset_get_keyfile (preset)))
830 goto no_presets;
831
832 /* take copies of current gobject properties from preset */
833 if (!(props = gst_preset_get_property_names (preset)))
834 goto no_properties;
835
836 gclass = G_OBJECT_CLASS (GST_ELEMENT_GET_CLASS (preset));
837 is_child_proxy = GST_IS_CHILD_PROXY (preset);
838
839 /* loop over the object properties and store the property value in the
840 * keyfile */
841 for (i = 0; props[i]; i++) {
842 GValue gvalue = { 0, };
843 gchar *str;
844 GParamSpec *property = NULL;
845
846 if (is_child_proxy) {
847 gst_child_proxy_lookup ((GstChildProxy *) preset, props[i], NULL,
848 &property);
849 } else {
850 property = g_object_class_find_property (gclass, props[i]);
851 }
852 if (!property) {
853 /* the element said it supported the property but then it does not have
854 * that property. This should not happen. */
855 GST_WARNING_OBJECT (preset, "property '%s' not in object", props[i]);
856 continue;
857 }
858
859 if (property->flags & G_PARAM_DEPRECATED) {
860 GST_INFO_OBJECT (preset, "Not saving property %s as it is deprecated",
861 property->name);
862 continue;
863 }
864
865 g_value_init (&gvalue, property->value_type);
866 if (is_child_proxy) {
867 gst_child_proxy_get_property ((GstChildProxy *) preset, props[i],
868 &gvalue);
869 } else {
870 g_object_get_property ((GObject *) preset, props[i], &gvalue);
871 }
872
873 if ((str = gst_value_serialize (&gvalue))) {
874 g_key_file_set_string (presets, name, props[i], (gpointer) str);
875 g_free (str);
876 } else {
877 GST_WARNING_OBJECT (preset, "serialization for property '%s' failed",
878 props[i]);
879 }
880 g_value_unset (&gvalue);
881 }
882 GST_INFO_OBJECT (preset, " saved");
883 g_strfreev (props);
884
885 /* save updated version */
886 return gst_preset_default_save_presets_file (preset);
887
888 /* ERRORS */
889 no_presets:
890 {
891 GST_WARNING_OBJECT (preset, "no presets");
892 return FALSE;
893 }
894 no_properties:
895 {
896 GST_INFO_OBJECT (preset, "no properties");
897 return FALSE;
898 }
899 }
900
901 /* copies all keys and comments from one group to another, deleting the old
902 * group. */
903 static gboolean
gst_preset_default_rename_preset(GstPreset * preset,const gchar * old_name,const gchar * new_name)904 gst_preset_default_rename_preset (GstPreset * preset, const gchar * old_name,
905 const gchar * new_name)
906 {
907 GKeyFile *presets;
908 gchar *str;
909 gchar **keys;
910 gsize i, num_keys;
911
912 /* get the presets from the type */
913 if (!(presets = preset_get_keyfile (preset)))
914 goto no_presets;
915
916 if (!g_key_file_has_group (presets, old_name))
917 goto no_group;
918
919 /* copy group comment if there is any */
920 if ((str = g_key_file_get_comment (presets, old_name, NULL, NULL))) {
921 g_key_file_set_comment (presets, new_name, NULL, str, NULL);
922 g_free (str);
923 }
924
925 /* get all keys from the old group and copy them in the new group */
926 keys = g_key_file_get_keys (presets, old_name, &num_keys, NULL);
927 for (i = 0; i < num_keys; i++) {
928 /* copy key comment if there is any */
929 if ((str = g_key_file_get_comment (presets, old_name, keys[i], NULL))) {
930 g_key_file_set_comment (presets, new_name, keys[i], str, NULL);
931 g_free (str);
932 }
933 /* copy key value */
934 str = g_key_file_get_value (presets, old_name, keys[i], NULL);
935 g_key_file_set_value (presets, new_name, keys[i], str);
936 g_free (str);
937 }
938 g_strfreev (keys);
939
940 /* remove old group */
941 g_key_file_remove_group (presets, old_name, NULL);
942
943 /* save updated version */
944 return gst_preset_default_save_presets_file (preset);
945
946 /* ERRORS */
947 no_presets:
948 {
949 GST_WARNING_OBJECT (preset, "no presets");
950 return FALSE;
951 }
952 no_group:
953 {
954 GST_WARNING_OBJECT (preset, "no preset named %s", old_name);
955 return FALSE;
956 }
957 }
958
959 /* delete a group from the keyfile */
960 static gboolean
gst_preset_default_delete_preset(GstPreset * preset,const gchar * name)961 gst_preset_default_delete_preset (GstPreset * preset, const gchar * name)
962 {
963 GKeyFile *presets;
964
965 /* get the presets from the type */
966 if (!(presets = preset_get_keyfile (preset)))
967 goto no_presets;
968
969 /* get the group */
970 if (!g_key_file_has_group (presets, name))
971 goto no_group;
972
973 /* remove the group */
974 g_key_file_remove_group (presets, name, NULL);
975
976 /* save updated version */
977 return gst_preset_default_save_presets_file (preset);
978
979 /* ERRORS */
980 no_presets:
981 {
982 GST_WARNING_OBJECT (preset, "no presets");
983 return FALSE;
984 }
985 no_group:
986 {
987 GST_WARNING_OBJECT (preset, "no preset named %s", name);
988 return FALSE;
989 }
990 }
991
992 static gboolean
gst_preset_default_set_meta(GstPreset * preset,const gchar * name,const gchar * tag,const gchar * value)993 gst_preset_default_set_meta (GstPreset * preset, const gchar * name,
994 const gchar * tag, const gchar * value)
995 {
996 GKeyFile *presets;
997 gchar *key;
998
999 /* get the presets from the type */
1000 if (!(presets = preset_get_keyfile (preset)))
1001 goto no_presets;
1002
1003 key = g_strdup_printf ("_meta/%s", tag);
1004 if (value && *value) {
1005 g_key_file_set_value (presets, name, key, value);
1006 } else {
1007 g_key_file_remove_key (presets, name, key, NULL);
1008 }
1009 g_free (key);
1010
1011 /* save updated keyfile */
1012 return gst_preset_default_save_presets_file (preset);
1013
1014 /* ERRORS */
1015 no_presets:
1016 {
1017 GST_WARNING_OBJECT (preset, "no presets");
1018 return FALSE;
1019 }
1020 }
1021
1022 /* the caller must free @value after usage */
1023 static gboolean
gst_preset_default_get_meta(GstPreset * preset,const gchar * name,const gchar * tag,gchar ** value)1024 gst_preset_default_get_meta (GstPreset * preset, const gchar * name,
1025 const gchar * tag, gchar ** value)
1026 {
1027 GKeyFile *presets;
1028 gchar *key;
1029
1030 /* get the presets from the type */
1031 if (!(presets = preset_get_keyfile (preset)))
1032 goto no_presets;
1033
1034 key = g_strdup_printf ("_meta/%s", tag);
1035 *value = g_key_file_get_value (presets, name, key, NULL);
1036 g_free (key);
1037
1038 return TRUE;
1039
1040 /* ERRORS */
1041 no_presets:
1042 {
1043 GST_WARNING_OBJECT (preset, "no presets");
1044 *value = NULL;
1045 return FALSE;
1046 }
1047 }
1048
1049 /* wrapper */
1050
1051 /**
1052 * gst_preset_get_preset_names:
1053 * @preset: a #GObject that implements #GstPreset
1054 *
1055 * Get a copy of preset names as a %NULL terminated string array.
1056 *
1057 * Returns: (transfer full) (array zero-terminated=1) (element-type utf8):
1058 * list with names, use g_strfreev() after usage.
1059 */
1060 gchar **
gst_preset_get_preset_names(GstPreset * preset)1061 gst_preset_get_preset_names (GstPreset * preset)
1062 {
1063 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
1064
1065 return (GST_PRESET_GET_INTERFACE (preset)->get_preset_names (preset));
1066 }
1067
1068 /**
1069 * gst_preset_get_property_names:
1070 * @preset: a #GObject that implements #GstPreset
1071 *
1072 * Get a the names of the GObject properties that can be used for presets.
1073 *
1074 * Returns: (transfer full) (array zero-terminated=1) (element-type utf8): an
1075 * array of property names which should be freed with g_strfreev() after use.
1076 */
1077 gchar **
gst_preset_get_property_names(GstPreset * preset)1078 gst_preset_get_property_names (GstPreset * preset)
1079 {
1080 g_return_val_if_fail (GST_IS_PRESET (preset), NULL);
1081
1082 return (GST_PRESET_GET_INTERFACE (preset)->get_property_names (preset));
1083 }
1084
1085 /**
1086 * gst_preset_load_preset:
1087 * @preset: a #GObject that implements #GstPreset
1088 * @name: preset name to load
1089 *
1090 * Load the given preset.
1091 *
1092 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1093 */
1094 gboolean
gst_preset_load_preset(GstPreset * preset,const gchar * name)1095 gst_preset_load_preset (GstPreset * preset, const gchar * name)
1096 {
1097 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1098 g_return_val_if_fail (name, FALSE);
1099
1100 return (GST_PRESET_GET_INTERFACE (preset)->load_preset (preset, name));
1101 }
1102
1103 /**
1104 * gst_preset_save_preset:
1105 * @preset: a #GObject that implements #GstPreset
1106 * @name: preset name to save
1107 *
1108 * Save the current object settings as a preset under the given name. If there
1109 * is already a preset by this @name it will be overwritten.
1110 *
1111 * Returns: %TRUE for success, %FALSE
1112 */
1113 gboolean
gst_preset_save_preset(GstPreset * preset,const gchar * name)1114 gst_preset_save_preset (GstPreset * preset, const gchar * name)
1115 {
1116 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1117 g_return_val_if_fail (name, FALSE);
1118
1119 return (GST_PRESET_GET_INTERFACE (preset)->save_preset (preset, name));
1120 }
1121
1122 /**
1123 * gst_preset_rename_preset:
1124 * @preset: a #GObject that implements #GstPreset
1125 * @old_name: current preset name
1126 * @new_name: new preset name
1127 *
1128 * Renames a preset. If there is already a preset by the @new_name it will be
1129 * overwritten.
1130 *
1131 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with @old_name
1132 */
1133 gboolean
gst_preset_rename_preset(GstPreset * preset,const gchar * old_name,const gchar * new_name)1134 gst_preset_rename_preset (GstPreset * preset, const gchar * old_name,
1135 const gchar * new_name)
1136 {
1137 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1138 g_return_val_if_fail (old_name, FALSE);
1139 g_return_val_if_fail (new_name, FALSE);
1140
1141 return (GST_PRESET_GET_INTERFACE (preset)->rename_preset (preset, old_name,
1142 new_name));
1143 }
1144
1145 /**
1146 * gst_preset_delete_preset:
1147 * @preset: a #GObject that implements #GstPreset
1148 * @name: preset name to remove
1149 *
1150 * Delete the given preset.
1151 *
1152 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1153 */
1154 gboolean
gst_preset_delete_preset(GstPreset * preset,const gchar * name)1155 gst_preset_delete_preset (GstPreset * preset, const gchar * name)
1156 {
1157 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1158 g_return_val_if_fail (name, FALSE);
1159
1160 return (GST_PRESET_GET_INTERFACE (preset)->delete_preset (preset, name));
1161 }
1162
1163 /**
1164 * gst_preset_set_meta:
1165 * @preset: a #GObject that implements #GstPreset
1166 * @name: preset name
1167 * @tag: meta data item name
1168 * @value: (allow-none): new value
1169 *
1170 * Sets a new @value for an existing meta data item or adds a new item. Meta
1171 * data @tag names can be something like e.g. "comment". Supplying %NULL for the
1172 * @value will unset an existing value.
1173 *
1174 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1175 */
1176 gboolean
gst_preset_set_meta(GstPreset * preset,const gchar * name,const gchar * tag,const gchar * value)1177 gst_preset_set_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1178 const gchar * value)
1179 {
1180 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1181 g_return_val_if_fail (name, FALSE);
1182 g_return_val_if_fail (tag, FALSE);
1183
1184 return GST_PRESET_GET_INTERFACE (preset)->set_meta (preset, name, tag, value);
1185 }
1186
1187 /**
1188 * gst_preset_get_meta:
1189 * @preset: a #GObject that implements #GstPreset
1190 * @name: preset name
1191 * @tag: meta data item name
1192 * @value: (out callee-allocates): value
1193 *
1194 * Gets the @value for an existing meta data @tag. Meta data @tag names can be
1195 * something like e.g. "comment". Returned values need to be released when done.
1196 *
1197 * Returns: %TRUE for success, %FALSE if e.g. there is no preset with that @name
1198 * or no value for the given @tag
1199 */
1200 gboolean
gst_preset_get_meta(GstPreset * preset,const gchar * name,const gchar * tag,gchar ** value)1201 gst_preset_get_meta (GstPreset * preset, const gchar * name, const gchar * tag,
1202 gchar ** value)
1203 {
1204 g_return_val_if_fail (GST_IS_PRESET (preset), FALSE);
1205 g_return_val_if_fail (name, FALSE);
1206 g_return_val_if_fail (tag, FALSE);
1207 g_return_val_if_fail (value, FALSE);
1208
1209 return GST_PRESET_GET_INTERFACE (preset)->get_meta (preset, name, tag, value);
1210 }
1211
1212 /**
1213 * gst_preset_set_app_dir:
1214 * @app_dir: (type filename): the application specific preset dir
1215 *
1216 * Sets an extra directory as an absolute path that should be considered when
1217 * looking for presets. Any presets in the application dir will shadow the
1218 * system presets.
1219 *
1220 * Returns: %TRUE for success, %FALSE if the dir already has been set
1221 */
1222 gboolean
gst_preset_set_app_dir(const gchar * app_dir)1223 gst_preset_set_app_dir (const gchar * app_dir)
1224 {
1225 g_return_val_if_fail (app_dir, FALSE);
1226
1227 if (!preset_app_dir) {
1228 preset_app_dir = g_strdup (app_dir);
1229 return TRUE;
1230 }
1231 return FALSE;
1232 }
1233
1234 /**
1235 * gst_preset_get_app_dir:
1236 *
1237 * Gets the directory for application specific presets if set by the
1238 * application.
1239 *
1240 * Returns: (nullable) (type filename): the directory or %NULL, don't free or modify
1241 * the string
1242 */
1243 const gchar *
gst_preset_get_app_dir(void)1244 gst_preset_get_app_dir (void)
1245 {
1246 return preset_app_dir;
1247 }
1248
1249 /**
1250 * gst_preset_is_editable:
1251 * @preset: a #GObject that implements #GstPreset
1252 *
1253 * Check if one can add new presets, change existing ones and remove presets.
1254 *
1255 * Returns: %TRUE if presets are editable or %FALSE if they are static
1256 *
1257 * Since: 1.6
1258 */
1259 gboolean
gst_preset_is_editable(GstPreset * preset)1260 gst_preset_is_editable (GstPreset * preset)
1261 {
1262 GstPresetInterface *iface = GST_PRESET_GET_INTERFACE (preset);
1263
1264 return iface->save_preset && iface->delete_preset;
1265 }
1266
1267 /* class internals */
1268
1269 static void
gst_preset_class_init(GstPresetInterface * iface)1270 gst_preset_class_init (GstPresetInterface * iface)
1271 {
1272 iface->get_preset_names = gst_preset_default_get_preset_names;
1273 iface->get_property_names = gst_preset_default_get_property_names;
1274
1275 iface->load_preset = gst_preset_default_load_preset;
1276 iface->save_preset = gst_preset_default_save_preset;
1277 iface->rename_preset = gst_preset_default_rename_preset;
1278 iface->delete_preset = gst_preset_default_delete_preset;
1279
1280 iface->set_meta = gst_preset_default_set_meta;
1281 iface->get_meta = gst_preset_default_get_meta;
1282 }
1283
1284 static void
gst_preset_base_init(gpointer g_class)1285 gst_preset_base_init (gpointer g_class)
1286 {
1287 static gboolean initialized = FALSE;
1288
1289 if (!initialized) {
1290 /* init default implementation */
1291 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "preset",
1292 GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLACK, "preset interface");
1293
1294 /* create quarks for use with g_type_{g,s}et_qdata() */
1295 preset_quark = g_quark_from_static_string ("GstPreset::presets");
1296 preset_user_path_quark =
1297 g_quark_from_static_string ("GstPreset::user_path");
1298 preset_app_path_quark = g_quark_from_static_string ("GstPreset::app_path");
1299 preset_system_path_quark =
1300 g_quark_from_static_string ("GstPreset::system_path");
1301
1302 #if 0
1303 /* create interface properties, each element would need to override this
1304 * g_object_class_override_property(gobject_class, PROP_PRESET_NAME, "preset-name");
1305 * and in _set_property() do
1306 * case PROP_PRESET_NAME: {
1307 * gchar *name = g_value_get_string (value);
1308 * if (name)
1309 * gst_preset_load_preset(preset, name);
1310 * } break;
1311 */
1312 g_object_interface_install_property (g_class,
1313 g_param_spec_string ("preset-name",
1314 "preset-name property",
1315 "load given preset", NULL, G_PARAM_WRITABLE));
1316 #endif
1317
1318 initialized = TRUE;
1319 }
1320 }
1321
1322 GType
gst_preset_get_type(void)1323 gst_preset_get_type (void)
1324 {
1325 static gsize type = 0;
1326
1327 if (g_once_init_enter (&type)) {
1328 GType _type;
1329 const GTypeInfo info = {
1330 sizeof (GstPresetInterface),
1331 (GBaseInitFunc) gst_preset_base_init, /* base_init */
1332 NULL, /* base_finalize */
1333 (GClassInitFunc) gst_preset_class_init, /* class_init */
1334 NULL, /* class_finalize */
1335 NULL, /* class_data */
1336 0,
1337 0, /* n_preallocs */
1338 NULL /* instance_init */
1339 };
1340 _type = g_type_register_static (G_TYPE_INTERFACE, "GstPreset", &info, 0);
1341 g_once_init_leave (&type, _type);
1342 }
1343 return type;
1344 }
1345