1 /* GStreamer encoding profile registry
2 * Copyright (C) 2010 Edward Hervey <edward.hervey@collabora.co.uk>
3 * (C) 2010 Nokia Corporation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
27 #include "encoding-target.h"
34 * [GStreamer Encoding Target]
36 * category : <category>
37 * description : <description> #translatable
39 * [profile-<profile1name>]
41 * description : <description> #optional
45 * [streamprofile-<id>]
46 * parent : <encodingprofile.name>[,<encodingprofile.name>..]
47 * type : <type> # "audio", "video", "text"
50 * restriction : <restriction>
51 * presence : <presence>
53 * variableframerate : <variableframerate>
57 * Location of profile files
59 * $GST_DATADIR/gstreamer-GST_MAJORMINOR/encoding-profile
60 * $HOME/gstreamer-GST_MAJORMINOR/encoding-profile
63 * $(target.category)/$(target.name).gep
65 * Naming restrictions:
66 * lowercase ASCII letter for the first character
67 * Same for all other characters + numerics + hyphens
71 #define GST_ENCODING_TARGET_HEADER "GStreamer Encoding Target"
72 #define GST_ENCODING_TARGET_DIRECTORY "encoding-profiles"
73 #define GST_ENCODING_TARGET_SUFFIX ".gep"
75 struct _GstEncodingTarget
88 G_DEFINE_TYPE (GstEncodingTarget, gst_encoding_target, G_TYPE_OBJECT);
91 gst_encoding_target_init (GstEncodingTarget * target)
93 /* Nothing to initialize */
97 gst_encoding_target_finalize (GObject * object)
99 GstEncodingTarget *target = (GstEncodingTarget *) object;
101 GST_DEBUG ("Finalizing");
104 g_free (target->name);
105 if (target->category)
106 g_free (target->category);
107 if (target->description)
108 g_free (target->description);
110 g_list_foreach (target->profiles, (GFunc) gst_mini_object_unref, NULL);
111 g_list_free (target->profiles);
115 gst_encoding_target_class_init (GObjectClass * klass)
117 klass->finalize = gst_encoding_target_finalize;
121 * gst_encoding_target_get_name:
122 * @target: a #GstEncodingTarget
126 * Returns: (transfer none): The name of the @target.
129 gst_encoding_target_get_name (GstEncodingTarget * target)
135 * gst_encoding_target_get_category:
136 * @target: a #GstEncodingTarget
140 * Returns: (transfer none): The category of the @target. For example:
141 * #GST_ENCODING_CATEGORY_DEVICE.
144 gst_encoding_target_get_category (GstEncodingTarget * target)
146 return target->category;
150 * gst_encoding_target_get_description:
151 * @target: a #GstEncodingTarget
155 * Returns: (transfer none): The description of the @target.
158 gst_encoding_target_get_description (GstEncodingTarget * target)
160 return target->description;
164 * gst_encoding_target_get_profiles:
165 * @target: a #GstEncodingTarget
169 * Returns: (transfer none) (element-type Gst.EncodingProfile): A list of
170 * #GstEncodingProfile(s) this @target handles.
173 gst_encoding_target_get_profiles (GstEncodingTarget * target)
175 return target->profiles;
179 * gst_encoding_target_get_profile:
180 * @target: a #GstEncodingTarget
181 * @name: the name of the profile to retrieve
185 * Returns: (transfer full): The matching #GstEncodingProfile, or %NULL.
188 gst_encoding_target_get_profile (GstEncodingTarget * target, const gchar * name)
192 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), NULL);
193 g_return_val_if_fail (name != NULL, NULL);
195 for (tmp = target->profiles; tmp; tmp = tmp->next) {
196 GstEncodingProfile *tprof = (GstEncodingProfile *) tmp->data;
198 if (!g_strcmp0 (gst_encoding_profile_get_name (tprof), name)) {
199 gst_encoding_profile_ref (tprof);
207 static inline gboolean
208 validate_name (const gchar * name)
216 /* First character can only be a lower case ASCII character */
217 if (!g_ascii_isalpha (name[0]) || !g_ascii_islower (name[0]))
220 /* All following characters can only by:
221 * either a lower case ASCII character
224 for (i = 1; i < len; i++) {
225 /* if uppercase ASCII letter, return */
226 if (g_ascii_isupper (name[i]))
228 /* if a digit, continue */
229 if (g_ascii_isdigit (name[i]))
231 /* if an hyphen, continue */
234 /* remaining should only be ascii letters */
235 if (!g_ascii_isalpha (name[i]))
243 * gst_encoding_target_new:
244 * @name: The name of the target.
245 * @category: (transfer none): The name of the category to which this @target
246 * belongs. For example: #GST_ENCODING_CATEGORY_DEVICE.
247 * @description: (transfer none): A description of #GstEncodingTarget in the
249 * @profiles: (transfer none) (element-type Gst.EncodingProfile): A #GList of
250 * #GstEncodingProfile.
252 * Creates a new #GstEncodingTarget.
254 * The name and category can only consist of lowercase ASCII letters for the
255 * first character, followed by either lowercase ASCII letters, digits or
258 * The @category <emphasis>should</emphasis> be one of the existing
259 * well-defined categories, like #GST_ENCODING_CATEGORY_DEVICE, but it
260 * <emphasis>can</emphasis> be a application or user specific category if
265 * Returns: (transfer full): The newly created #GstEncodingTarget or %NULL if
266 * there was an error.
270 gst_encoding_target_new (const gchar * name, const gchar * category,
271 const gchar * description, const GList * profiles)
273 GstEncodingTarget *res;
275 g_return_val_if_fail (name != NULL, NULL);
276 g_return_val_if_fail (category != NULL, NULL);
277 g_return_val_if_fail (description != NULL, NULL);
280 if (!validate_name (name))
282 if (!validate_name (category))
283 goto invalid_category;
285 res = (GstEncodingTarget *) g_object_new (GST_TYPE_ENCODING_TARGET, NULL);
286 res->name = g_strdup (name);
287 res->category = g_strdup (category);
288 res->description = g_strdup (description);
291 GstEncodingProfile *prof = (GstEncodingProfile *) profiles->data;
294 g_list_append (res->profiles, gst_encoding_profile_ref (prof));
295 profiles = profiles->next;
302 GST_ERROR ("Invalid name for encoding target : '%s'", name);
308 GST_ERROR ("Invalid name for encoding category : '%s'", category);
314 * gst_encoding_target_add_profile:
315 * @target: the #GstEncodingTarget to add a profile to
316 * @profile: (transfer full): the #GstEncodingProfile to add
318 * Adds the given @profile to the @target. Each added profile must have
319 * a unique name within the profile.
321 * The @target will steal a reference to the @profile. If you wish to use
322 * the profile after calling this method, you should increase its reference
327 * Returns: %TRUE if the profile was added, else %FALSE.
331 gst_encoding_target_add_profile (GstEncodingTarget * target,
332 GstEncodingProfile * profile)
336 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), FALSE);
337 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
339 /* Make sure profile isn't already controlled by this target */
340 for (tmp = target->profiles; tmp; tmp = tmp->next) {
341 GstEncodingProfile *prof = (GstEncodingProfile *) tmp->data;
343 if (!g_strcmp0 (gst_encoding_profile_get_name (profile),
344 gst_encoding_profile_get_name (prof))) {
345 GST_WARNING ("Profile already present in target");
350 target->profiles = g_list_append (target->profiles, profile);
356 serialize_stream_profiles (GKeyFile * out, GstEncodingProfile * sprof,
357 const gchar * profilename, guint id)
359 gchar *sprofgroupname;
361 const GstCaps *format, *restriction;
362 const gchar *preset, *name, *description;
364 sprofgroupname = g_strdup_printf ("streamprofile-%s-%d", profilename, id);
366 /* Write the parent profile */
367 g_key_file_set_value (out, sprofgroupname, "parent", profilename);
369 g_key_file_set_value (out, sprofgroupname, "type",
370 gst_encoding_profile_get_type_nick (sprof));
372 format = gst_encoding_profile_get_format (sprof);
374 tmpc = gst_caps_to_string (format);
375 g_key_file_set_value (out, sprofgroupname, "format", tmpc);
379 name = gst_encoding_profile_get_name (sprof);
381 g_key_file_set_string (out, sprofgroupname, "name", name);
383 description = gst_encoding_profile_get_description (sprof);
385 g_key_file_set_string (out, sprofgroupname, "description", description);
387 preset = gst_encoding_profile_get_preset (sprof);
389 g_key_file_set_string (out, sprofgroupname, "preset", preset);
391 restriction = gst_encoding_profile_get_restriction (sprof);
393 tmpc = gst_caps_to_string (restriction);
394 g_key_file_set_value (out, sprofgroupname, "restriction", tmpc);
397 g_key_file_set_integer (out, sprofgroupname, "presence",
398 gst_encoding_profile_get_presence (sprof));
400 if (GST_IS_ENCODING_VIDEO_PROFILE (sprof)) {
401 GstEncodingVideoProfile *vp = (GstEncodingVideoProfile *) sprof;
403 g_key_file_set_integer (out, sprofgroupname, "pass",
404 gst_encoding_video_profile_get_pass (vp));
405 g_key_file_set_boolean (out, sprofgroupname, "variableframerate",
406 gst_encoding_video_profile_get_variableframerate (vp));
409 g_free (sprofgroupname);
416 const char *loc = NULL;
420 #if defined(LC_MESSAGES)
421 loc = setlocale (LC_MESSAGES, NULL);
422 GST_LOG ("LC_MESSAGES: %s", GST_STR_NULL (loc));
423 #elif defined(LC_ALL)
424 loc = setlocale (LC_ALL, NULL);
425 GST_LOG ("LC_ALL: %s", GST_STR_NULL (loc));
427 GST_LOG ("Neither LC_ALL nor LC_MESSAGES defined");
429 #else /* !ENABLE_NLS */
430 GST_LOG ("i18n disabled");
433 if (loc == NULL || g_ascii_strncasecmp (loc, "en", 2) == 0)
436 /* en_GB.UTF-8 => en */
437 ret = g_ascii_strdown (loc, -1);
438 ret = g_strcanon (ret, "abcdefghijklmnopqrstuvwxyz", '\0');
439 GST_LOG ("using locale: %s", ret);
443 /* Serialize the top-level profiles
444 * Note: They don't have to be containerprofiles */
446 serialize_encoding_profile (GKeyFile * out, GstEncodingProfile * prof)
448 gchar *profgroupname;
451 const gchar *profname, *profdesc, *profpreset, *proftype;
452 const GstCaps *profformat, *profrestriction;
454 profname = gst_encoding_profile_get_name (prof);
455 profdesc = gst_encoding_profile_get_description (prof);
456 profformat = gst_encoding_profile_get_format (prof);
457 profpreset = gst_encoding_profile_get_preset (prof);
458 proftype = gst_encoding_profile_get_type_nick (prof);
459 profrestriction = gst_encoding_profile_get_restriction (prof);
461 profgroupname = g_strdup_printf ("profile-%s", profname);
463 g_key_file_set_string (out, profgroupname, "name", profname);
465 g_key_file_set_value (out, profgroupname, "type",
466 gst_encoding_profile_get_type_nick (prof));
471 locale = get_locale ();
472 if (locale != NULL) {
473 g_key_file_set_locale_string (out, profgroupname, "description",
477 g_key_file_set_string (out, profgroupname, "description", profdesc);
481 gchar *tmpc = gst_caps_to_string (profformat);
482 g_key_file_set_string (out, profgroupname, "format", tmpc);
486 g_key_file_set_string (out, profgroupname, "preset", profpreset);
488 /* stream profiles */
489 if (GST_IS_ENCODING_CONTAINER_PROFILE (prof)) {
491 gst_encoding_container_profile_get_profiles
492 (GST_ENCODING_CONTAINER_PROFILE (prof)), i = 0; tmp;
493 tmp = tmp->next, i++) {
494 GstEncodingProfile *sprof = (GstEncodingProfile *) tmp->data;
496 if (!serialize_stream_profiles (out, sprof, profname, i))
500 g_free (profgroupname);
505 serialize_target (GKeyFile * out, GstEncodingTarget * target)
509 g_key_file_set_string (out, GST_ENCODING_TARGET_HEADER, "name", target->name);
510 g_key_file_set_string (out, GST_ENCODING_TARGET_HEADER, "category",
512 g_key_file_set_string (out, GST_ENCODING_TARGET_HEADER, "description",
513 target->description);
515 for (tmp = target->profiles; tmp; tmp = tmp->next) {
516 GstEncodingProfile *prof = (GstEncodingProfile *) tmp->data;
517 if (!serialize_encoding_profile (out, prof))
525 * parse_encoding_profile:
527 * @parentprofilename: the parent profile name (including 'profile-' or 'streamprofile-' header)
528 * @profilename: the profile name group to parse
529 * @nbgroups: the number of top-level groups
530 * @groups: the top-level groups
532 static GstEncodingProfile *
533 parse_encoding_profile (GKeyFile * in, gchar * parentprofilename,
534 gchar * profilename, gsize nbgroups, gchar ** groups)
536 GstEncodingProfile *sprof = NULL;
538 gchar *proftype, *format, *preset, *restriction, *pname, *description;
539 GstCaps *formatcaps = NULL;
540 GstCaps *restrictioncaps = NULL;
541 gboolean variableframerate;
543 gsize i, nbencprofiles;
545 GST_DEBUG ("parentprofilename : %s , profilename : %s",
546 parentprofilename, profilename);
548 if (parentprofilename) {
549 gboolean found = FALSE;
552 g_key_file_get_string_list (in, profilename, "parent",
553 &nbencprofiles, NULL);
554 if (!parent || !nbencprofiles) {
558 /* Check if this streamprofile is used in <profilename> */
559 for (i = 0; i < nbencprofiles; i++) {
560 if (!g_strcmp0 (parent[i], parentprofilename)) {
568 GST_DEBUG ("Stream profile '%s' isn't used in profile '%s'",
569 profilename, parentprofilename);
574 pname = g_key_file_get_value (in, profilename, "name", NULL);
576 /* First try to get localized description */
580 locale = get_locale ();
581 if (locale != NULL) {
582 /* will try to fall back to untranslated string if no translation found */
583 description = g_key_file_get_locale_string (in, profilename,
584 "description", locale, NULL);
588 g_key_file_get_string (in, profilename, "description", NULL);
592 /* Note: a missing description is normal for non-container profiles */
593 if (description == NULL) {
594 GST_LOG ("Missing 'description' field for streamprofile %s", profilename);
597 /* Parse the remaining fields */
598 proftype = g_key_file_get_value (in, profilename, "type", NULL);
600 GST_WARNING ("Missing 'type' field for streamprofile %s", profilename);
604 format = g_key_file_get_value (in, profilename, "format", NULL);
606 formatcaps = gst_caps_from_string (format);
610 preset = g_key_file_get_value (in, profilename, "preset", NULL);
612 restriction = g_key_file_get_value (in, profilename, "restriction", NULL);
614 restrictioncaps = gst_caps_from_string (restriction);
615 g_free (restriction);
618 presence = g_key_file_get_integer (in, profilename, "presence", NULL);
619 pass = g_key_file_get_integer (in, profilename, "pass", NULL);
621 g_key_file_get_boolean (in, profilename, "variableframerate", NULL);
623 /* Build the streamprofile ! */
624 if (!g_strcmp0 (proftype, "container")) {
625 GstEncodingProfile *pprof;
628 (GstEncodingProfile *) gst_encoding_container_profile_new (pname,
629 description, formatcaps, preset);
630 /* Now look for the stream profiles */
631 for (i = 0; i < nbgroups; i++) {
632 if (!g_ascii_strncasecmp (groups[i], "streamprofile-", 13)) {
633 pprof = parse_encoding_profile (in, pname, groups[i], nbgroups, groups);
635 gst_encoding_container_profile_add_profile (
636 (GstEncodingContainerProfile *) sprof, pprof);
640 } else if (!g_strcmp0 (proftype, "video")) {
642 (GstEncodingProfile *) gst_encoding_video_profile_new (formatcaps,
643 preset, restrictioncaps, presence);
644 gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile
645 *) sprof, variableframerate);
646 gst_encoding_video_profile_set_pass ((GstEncodingVideoProfile *) sprof,
648 } else if (!g_strcmp0 (proftype, "audio")) {
650 (GstEncodingProfile *) gst_encoding_audio_profile_new (formatcaps,
651 preset, restrictioncaps, presence);
653 GST_ERROR ("Unknown profile format '%s'", proftype);
656 gst_caps_unref (restrictioncaps);
658 gst_caps_unref (formatcaps);
663 g_free (description);
672 static GstEncodingTarget *
673 parse_keyfile (GKeyFile * in, gchar * targetname, gchar * categoryname,
676 GstEncodingTarget *res = NULL;
677 GstEncodingProfile *prof;
681 res = gst_encoding_target_new (targetname, categoryname, description, NULL);
683 /* Figure out the various profiles */
684 groups = g_key_file_get_groups (in, &nbgroups);
685 for (i = 0; i < nbgroups; i++) {
686 if (!g_ascii_strncasecmp (groups[i], "profile-", 8)) {
687 prof = parse_encoding_profile (in, NULL, groups[i], nbgroups, groups);
689 gst_encoding_target_add_profile (res, prof);
698 g_free (categoryname);
700 g_free (description);
706 load_file_and_read_header (const gchar * path, gchar ** targetname,
707 gchar ** categoryname, gchar ** description, GError ** error)
711 GError *key_error = NULL;
713 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
715 in = g_key_file_new ();
717 GST_DEBUG ("path:%s", path);
720 g_key_file_load_from_file (in, path,
721 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &key_error);
722 if (!res || key_error != NULL)
727 g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "name", &key_error);
732 g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "category", NULL);
734 g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "description",
741 GST_WARNING ("Unable to read GstEncodingTarget file %s: %s",
742 path, key_error->message);
743 g_propagate_error (error, key_error);
744 g_key_file_free (in);
750 GST_WARNING ("Wrong header in file %s: %s", path, key_error->message);
751 g_propagate_error (error, key_error);
752 g_key_file_free (in);
758 * gst_encoding_target_load_from_file:
759 * @filepath: The file location to load the #GstEncodingTarget from
760 * @error: If an error occured, this field will be filled in.
762 * Opens the provided file and returns the contained #GstEncodingTarget.
766 * Returns: (transfer full): The #GstEncodingTarget contained in the file, else
771 gst_encoding_target_load_from_file (const gchar * filepath, GError ** error)
774 gchar *targetname, *categoryname, *description;
775 GstEncodingTarget *res = NULL;
777 in = load_file_and_read_header (filepath, &targetname, &categoryname,
778 &description, error);
782 res = parse_keyfile (in, targetname, categoryname, description);
784 g_key_file_free (in);
791 * returned list contents must be freed
794 get_matching_filenames (gchar * path, gchar * filename)
798 const gchar *subdirname;
800 topdir = g_dir_open (path, 0, NULL);
801 if (G_UNLIKELY (topdir == NULL))
804 while ((subdirname = g_dir_read_name (topdir))) {
805 gchar *ltmp = g_build_filename (path, subdirname, NULL);
807 if (g_file_test (ltmp, G_FILE_TEST_IS_DIR)) {
808 gchar *tmp = g_build_filename (path, subdirname, filename, NULL);
809 /* Test to see if we have a file named like that in that directory */
810 if (g_file_test (tmp, G_FILE_TEST_EXISTS))
811 res = g_list_append (res, tmp);
818 g_dir_close (topdir);
823 static GstEncodingTarget *
824 gst_encoding_target_subload (gchar * path, const gchar * category,
825 gchar * lfilename, GError ** error)
827 GstEncodingTarget *target = NULL;
832 filename = g_build_filename (path, category, lfilename, NULL);
833 target = gst_encoding_target_load_from_file (filename, error);
836 GList *tmp, *tries = get_matching_filenames (path, lfilename);
838 /* Try to find a file named %s.gstprofile in any subdirectories */
839 for (tmp = tries; tmp; tmp = tmp->next) {
840 target = gst_encoding_target_load_from_file ((gchar *) tmp->data, NULL);
844 g_list_foreach (tries, (GFunc) g_free, NULL);
853 * gst_encoding_target_load:
854 * @name: the name of the #GstEncodingTarget to load.
855 * @category: (allow-none): the name of the target category, like
856 * #GST_ENCODING_CATEGORY_DEVICE. Can be %NULL
857 * @error: If an error occured, this field will be filled in.
859 * Searches for the #GstEncodingTarget with the given name, loads it
862 * If the category name is specified only targets from that category will be
867 * Returns: (transfer full): The #GstEncodingTarget if available, else %NULL.
870 gst_encoding_target_load (const gchar * name, const gchar * category,
873 gchar *lfilename, *tldir;
874 GstEncodingTarget *target = NULL;
876 g_return_val_if_fail (name != NULL, NULL);
878 if (!validate_name (name))
881 if (category && !validate_name (category))
882 goto invalid_category;
884 lfilename = g_strdup_printf ("%s" GST_ENCODING_TARGET_SUFFIX, name);
886 /* Try from local profiles */
888 g_build_filename (g_get_home_dir (), ".gstreamer-" GST_MAJORMINOR,
889 GST_ENCODING_TARGET_DIRECTORY, NULL);
890 target = gst_encoding_target_subload (tldir, category, lfilename, error);
893 if (target == NULL) {
894 /* Try from system-wide profiles */
896 g_build_filename (GST_DATADIR, "gstreamer-" GST_MAJORMINOR,
897 GST_ENCODING_TARGET_DIRECTORY, NULL);
898 target = gst_encoding_target_subload (tldir, category, lfilename, error);
908 GST_ERROR ("Invalid name for encoding target : '%s'", name);
913 GST_ERROR ("Invalid name for encoding category : '%s'", category);
919 * gst_encoding_target_save_to_file:
920 * @target: a #GstEncodingTarget
921 * @filepath: the location to store the @target at.
922 * @error: If an error occured, this field will be filled in.
924 * Saves the @target to the provided file location.
928 * Returns: %TRUE if the target was correctly saved, else %FALSE.
932 gst_encoding_target_save_to_file (GstEncodingTarget * target,
933 const gchar * filepath, GError ** error)
939 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), FALSE);
940 g_return_val_if_fail (filepath != NULL, FALSE);
942 /* FIXME : Check filepath is valid and writable
943 * FIXME : Strip out profiles already present in system target */
945 /* Get unique name... */
947 /* Create output GKeyFile */
948 out = g_key_file_new ();
950 if (!serialize_target (out, target))
951 goto serialize_failure;
953 if (!(data = g_key_file_to_data (out, &data_size, error)))
956 if (!g_file_set_contents (filepath, data, data_size, error))
959 g_key_file_free (out);
966 GST_ERROR ("Failure serializing target");
967 g_key_file_free (out);
973 GST_ERROR ("Failure converting keyfile: %s", (*error)->message);
974 g_key_file_free (out);
981 GST_ERROR ("Unable to write file %s: %s", filepath, (*error)->message);
982 g_key_file_free (out);
989 * gst_encoding_target_save:
990 * @target: a #GstEncodingTarget
991 * @error: If an error occured, this field will be filled in.
993 * Saves the @target to a default user-local directory.
997 * Returns: %TRUE if the target was correctly saved, else %FALSE.
1001 gst_encoding_target_save (GstEncodingTarget * target, GError ** error)
1007 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), FALSE);
1008 g_return_val_if_fail (target->category != NULL, FALSE);
1010 lfilename = g_strdup_printf ("%s" GST_ENCODING_TARGET_SUFFIX, target->name);
1012 g_build_filename (g_get_home_dir (), ".gstreamer-" GST_MAJORMINOR,
1013 GST_ENCODING_TARGET_DIRECTORY, target->category, lfilename, NULL);
1016 res = gst_encoding_target_save_to_file (target, filename, error);
1023 get_categories (gchar * path)
1027 const gchar *subdirname;
1029 topdir = g_dir_open (path, 0, NULL);
1030 if (G_UNLIKELY (topdir == NULL))
1033 while ((subdirname = g_dir_read_name (topdir))) {
1034 gchar *ltmp = g_build_filename (path, subdirname, NULL);
1036 if (g_file_test (ltmp, G_FILE_TEST_IS_DIR)) {
1037 res = g_list_append (res, (gpointer) g_strdup (subdirname));
1042 g_dir_close (topdir);
1048 * gst_encoding_list_available_categories:
1050 * Lists all #GstEncodingTarget categories present on disk.
1052 * Returns: (transfer full) (element-type gchar*): A list
1053 * of #GstEncodingTarget categories.
1058 gst_encoding_list_available_categories (void)
1064 /* First try user-local categories */
1065 topdir = g_build_filename (g_get_home_dir (), ".gstreamer-" GST_MAJORMINOR,
1066 GST_ENCODING_TARGET_DIRECTORY, NULL);
1067 res = get_categories (topdir);
1070 /* Extend with system-wide categories */
1071 topdir = g_build_filename (GST_DATADIR, "gstreamer-" GST_MAJORMINOR,
1072 GST_ENCODING_TARGET_DIRECTORY, NULL);
1073 tmp1 = get_categories (topdir);
1076 for (tmp2 = tmp1; tmp2; tmp2 = tmp2->next) {
1077 gchar *name = (gchar *) tmp2->data;
1078 if (!g_list_find_custom (res, name, (GCompareFunc) g_strcmp0))
1079 res = g_list_append (res, (gpointer) name);
1088 static inline GList *
1089 sub_get_all_targets (gchar * subdir)
1092 const gchar *filename;
1094 GstEncodingTarget *target;
1096 dir = g_dir_open (subdir, 0, NULL);
1097 if (G_UNLIKELY (dir == NULL))
1100 while ((filename = g_dir_read_name (dir))) {
1103 /* Only try files ending with .gstprofile */
1104 if (!g_str_has_suffix (filename, GST_ENCODING_TARGET_SUFFIX))
1107 fullname = g_build_filename (subdir, filename, NULL);
1108 target = gst_encoding_target_load_from_file (fullname, NULL);
1110 res = g_list_append (res, target);
1112 GST_WARNING ("Failed to get a target from %s", fullname);
1120 static inline GList *
1121 get_all_targets (gchar * topdir, const gchar * categoryname)
1126 gchar *subdir = g_build_filename (topdir, categoryname, NULL);
1127 /* Try to open the directory */
1128 res = sub_get_all_targets (subdir);
1131 const gchar *subdirname;
1132 GDir *dir = g_dir_open (topdir, 0, NULL);
1134 if (G_UNLIKELY (dir == NULL))
1137 while ((subdirname = g_dir_read_name (dir))) {
1138 gchar *ltmp = g_build_filename (topdir, subdirname, NULL);
1140 if (g_file_test (ltmp, G_FILE_TEST_IS_DIR)) {
1141 res = g_list_concat (res, sub_get_all_targets (ltmp));
1152 compare_targets (const GstEncodingTarget * ta, const GstEncodingTarget * tb)
1154 if (!g_strcmp0 (ta->name, tb->name)
1155 && !g_strcmp0 (ta->category, tb->category))
1162 * gst_encoding_list_all_targets:
1163 * @categoryname: (allow-none): The category, for ex: #GST_ENCODING_CATEGORY_DEVICE.
1166 * List all available #GstEncodingTarget for the specified category, or all categories
1167 * if @categoryname is %NULL.
1169 * Returns: (transfer full) (element-type GstEncodingTarget): The list of #GstEncodingTarget
1174 gst_encoding_list_all_targets (const gchar * categoryname)
1180 /* Get user-locals */
1181 topdir = g_build_filename (g_get_home_dir (), ".gstreamer-" GST_MAJORMINOR,
1182 GST_ENCODING_TARGET_DIRECTORY, NULL);
1183 res = get_all_targets (topdir, categoryname);
1186 /* Get system-wide */
1187 topdir = g_build_filename (GST_DATADIR, "gstreamer-" GST_MAJORMINOR,
1188 GST_ENCODING_TARGET_DIRECTORY, NULL);
1189 tmp1 = get_all_targets (topdir, categoryname);
1192 /* Merge system-wide targets */
1193 /* FIXME : We should merge the system-wide profiles into the user-locals
1194 * instead of stopping at identical target names */
1195 for (tmp2 = tmp1; tmp2; tmp2 = tmp2->next) {
1196 GstEncodingTarget *target = (GstEncodingTarget *) tmp2->data;
1197 if (g_list_find_custom (res, target, (GCompareFunc) compare_targets))
1198 gst_encoding_target_unref (target);
1200 res = g_list_append (res, target);