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 * [_gstencodingtarget_]
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 "_gstencodingtarget_"
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, GST_TYPE_MINI_OBJECT);
91 gst_encoding_target_init (GstEncodingTarget * target)
93 /* Nothing to initialize */
97 gst_encoding_target_finalize (GstEncodingTarget * target)
99 GST_DEBUG ("Finalizing");
102 g_free (target->name);
103 if (target->category)
104 g_free (target->category);
105 if (target->description)
106 g_free (target->description);
108 g_list_foreach (target->profiles, (GFunc) gst_mini_object_unref, NULL);
109 g_list_free (target->profiles);
113 gst_encoding_target_class_init (GstMiniObjectClass * klass)
116 (GstMiniObjectFinalizeFunction) gst_encoding_target_finalize;
120 * gst_encoding_target_get_name:
121 * @target: a #GstEncodingTarget
125 * Returns: (transfer none): The name of the @target.
128 gst_encoding_target_get_name (GstEncodingTarget * target)
134 * gst_encoding_target_get_category:
135 * @target: a #GstEncodingTarget
139 * Returns: (transfer none): The category of the @target. For example:
140 * #GST_ENCODING_CATEGORY_DEVICE.
143 gst_encoding_target_get_category (GstEncodingTarget * target)
145 return target->category;
149 * gst_encoding_target_get_description:
150 * @target: a #GstEncodingTarget
154 * Returns: (transfer none): The description of the @target.
157 gst_encoding_target_get_description (GstEncodingTarget * target)
159 return target->description;
163 * gst_encoding_target_get_profiles:
164 * @target: a #GstEncodingTarget
168 * Returns: (transfer none) (element-type Gst.EncodingProfile): A list of
169 * #GstEncodingProfile(s) this @target handles.
172 gst_encoding_target_get_profiles (GstEncodingTarget * target)
174 return target->profiles;
178 * gst_encoding_target_get_profile:
179 * @target: a #GstEncodingTarget
180 * @name: the name of the profile to retrieve
184 * Returns: (transfer full): The matching #GstEncodingProfile, or %NULL.
187 gst_encoding_target_get_profile (GstEncodingTarget * target, const gchar * name)
191 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), NULL);
192 g_return_val_if_fail (name != NULL, NULL);
194 for (tmp = target->profiles; tmp; tmp = tmp->next) {
195 GstEncodingProfile *tprof = (GstEncodingProfile *) tmp->data;
197 if (!g_strcmp0 (gst_encoding_profile_get_name (tprof), name)) {
198 gst_encoding_profile_ref (tprof);
206 static inline gboolean
207 validate_name (const gchar * name)
215 /* First character can only be a lower case ASCII character */
216 if (!g_ascii_isalpha (name[0]) || !g_ascii_islower (name[0]))
219 /* All following characters can only by:
220 * either a lower case ASCII character
223 for (i = 1; i < len; i++) {
224 /* if uppercase ASCII letter, return */
225 if (g_ascii_isupper (name[i]))
227 /* if a digit, continue */
228 if (g_ascii_isdigit (name[i]))
230 /* if an hyphen, continue */
233 /* remaining should only be ascii letters */
234 if (!g_ascii_isalpha (name[i]))
242 * gst_encoding_target_new:
243 * @name: The name of the target.
244 * @category: (transfer none): The name of the category to which this @target
245 * belongs. For example: #GST_ENCODING_CATEGORY_DEVICE.
246 * @description: (transfer none): A description of #GstEncodingTarget in the
248 * @profiles: (transfer none) (element-type Gst.EncodingProfile): A #GList of
249 * #GstEncodingProfile.
251 * Creates a new #GstEncodingTarget.
253 * The name and category can only consist of lowercase ASCII letters for the
254 * first character, followed by either lowercase ASCII letters, digits or
257 * The @category <emphasis>should</emphasis> be one of the existing
258 * well-defined categories, like #GST_ENCODING_CATEGORY_DEVICE, but it
259 * <emphasis>can</emphasis> be a application or user specific category if
264 * Returns: (transfer full): The newly created #GstEncodingTarget or %NULL if
265 * there was an error.
269 gst_encoding_target_new (const gchar * name, const gchar * category,
270 const gchar * description, const GList * profiles)
272 GstEncodingTarget *res;
274 g_return_val_if_fail (name != NULL, NULL);
275 g_return_val_if_fail (category != NULL, NULL);
276 g_return_val_if_fail (description != NULL, NULL);
279 if (!validate_name (name))
281 if (!validate_name (category))
282 goto invalid_category;
284 res = (GstEncodingTarget *) gst_mini_object_new (GST_TYPE_ENCODING_TARGET);
285 res->name = g_strdup (name);
286 res->category = g_strdup (category);
287 res->description = g_strdup (description);
290 GstEncodingProfile *prof = (GstEncodingProfile *) profiles->data;
293 g_list_append (res->profiles, gst_encoding_profile_ref (prof));
294 profiles = profiles->next;
301 GST_ERROR ("Invalid name for encoding target : '%s'", name);
307 GST_ERROR ("Invalid name for encoding category : '%s'", category);
313 * gst_encoding_target_add_profile:
314 * @target: the #GstEncodingTarget to add a profile to
315 * @profile: (transfer full): the #GstEncodingProfile to add
317 * Adds the given @profile to the @target. Each added profile must have
318 * a unique name within the profile.
320 * The @target will steal a reference to the @profile. If you wish to use
321 * the profile after calling this method, you should increase its reference
326 * Returns: %TRUE if the profile was added, else %FALSE.
330 gst_encoding_target_add_profile (GstEncodingTarget * target,
331 GstEncodingProfile * profile)
335 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), FALSE);
336 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
338 /* Make sure profile isn't already controlled by this target */
339 for (tmp = target->profiles; tmp; tmp = tmp->next) {
340 GstEncodingProfile *prof = (GstEncodingProfile *) tmp->data;
342 if (!g_strcmp0 (gst_encoding_profile_get_name (profile),
343 gst_encoding_profile_get_name (prof))) {
344 GST_WARNING ("Profile already present in target");
349 target->profiles = g_list_append (target->profiles, profile);
355 serialize_stream_profiles (GKeyFile * out, GstEncodingProfile * sprof,
356 const gchar * profilename, guint id)
358 gchar *sprofgroupname;
360 const GstCaps *format, *restriction;
361 const gchar *preset, *name, *description;
363 sprofgroupname = g_strdup_printf ("streamprofile-%s-%d", profilename, id);
365 /* Write the parent profile */
366 g_key_file_set_value (out, sprofgroupname, "parent", profilename);
368 g_key_file_set_value (out, sprofgroupname, "type",
369 gst_encoding_profile_get_type_nick (sprof));
371 format = gst_encoding_profile_get_format (sprof);
373 tmpc = gst_caps_to_string (format);
374 g_key_file_set_value (out, sprofgroupname, "format", tmpc);
378 name = gst_encoding_profile_get_name (sprof);
380 g_key_file_set_string (out, sprofgroupname, "name", name);
382 description = gst_encoding_profile_get_description (sprof);
384 g_key_file_set_string (out, sprofgroupname, "description", description);
386 preset = gst_encoding_profile_get_preset (sprof);
388 g_key_file_set_string (out, sprofgroupname, "preset", preset);
390 restriction = gst_encoding_profile_get_restriction (sprof);
392 tmpc = gst_caps_to_string (restriction);
393 g_key_file_set_value (out, sprofgroupname, "restriction", tmpc);
396 g_key_file_set_integer (out, sprofgroupname, "presence",
397 gst_encoding_profile_get_presence (sprof));
399 if (GST_IS_ENCODING_VIDEO_PROFILE (sprof)) {
400 GstEncodingVideoProfile *vp = (GstEncodingVideoProfile *) sprof;
402 g_key_file_set_integer (out, sprofgroupname, "pass",
403 gst_encoding_video_profile_get_pass (vp));
404 g_key_file_set_boolean (out, sprofgroupname, "variableframerate",
405 gst_encoding_video_profile_get_variableframerate (vp));
408 g_free (sprofgroupname);
412 /* Serialize the top-level profiles
413 * Note: They don't have to be containerprofiles */
415 serialize_encoding_profile (GKeyFile * out, GstEncodingProfile * prof)
417 gchar *profgroupname;
420 const gchar *profname, *profdesc, *profpreset, *proftype;
421 const GstCaps *profformat, *profrestriction;
423 profname = gst_encoding_profile_get_name (prof);
424 profdesc = gst_encoding_profile_get_description (prof);
425 profformat = gst_encoding_profile_get_format (prof);
426 profpreset = gst_encoding_profile_get_preset (prof);
427 proftype = gst_encoding_profile_get_type_nick (prof);
428 profrestriction = gst_encoding_profile_get_restriction (prof);
430 profgroupname = g_strdup_printf ("profile-%s", profname);
432 g_key_file_set_string (out, profgroupname, "name", profname);
434 g_key_file_set_value (out, profgroupname, "type",
435 gst_encoding_profile_get_type_nick (prof));
438 g_key_file_set_locale_string (out, profgroupname, "description",
439 setlocale (LC_ALL, NULL), profdesc);
441 gchar *tmpc = gst_caps_to_string (profformat);
442 g_key_file_set_string (out, profgroupname, "format", tmpc);
446 g_key_file_set_string (out, profgroupname, "preset", profpreset);
448 /* stream profiles */
449 if (GST_IS_ENCODING_CONTAINER_PROFILE (prof)) {
451 gst_encoding_container_profile_get_profiles
452 (GST_ENCODING_CONTAINER_PROFILE (prof)), i = 0; tmp;
453 tmp = tmp->next, i++) {
454 GstEncodingProfile *sprof = (GstEncodingProfile *) tmp->data;
456 if (!serialize_stream_profiles (out, sprof, profname, i))
460 g_free (profgroupname);
465 serialize_target (GKeyFile * out, GstEncodingTarget * target)
469 g_key_file_set_string (out, GST_ENCODING_TARGET_HEADER, "name", target->name);
470 g_key_file_set_string (out, GST_ENCODING_TARGET_HEADER, "category",
472 g_key_file_set_string (out, GST_ENCODING_TARGET_HEADER, "description",
473 target->description);
475 for (tmp = target->profiles; tmp; tmp = tmp->next) {
476 GstEncodingProfile *prof = (GstEncodingProfile *) tmp->data;
477 if (!serialize_encoding_profile (out, prof))
485 * parse_encoding_profile:
487 * @parentprofilename: the parent profile name (including 'profile-' or 'streamprofile-' header)
488 * @profilename: the profile name group to parse
489 * @nbgroups: the number of top-level groups
490 * @groups: the top-level groups
492 static GstEncodingProfile *
493 parse_encoding_profile (GKeyFile * in, gchar * parentprofilename,
494 gchar * profilename, gsize nbgroups, gchar ** groups)
496 GstEncodingProfile *sprof = NULL;
498 gchar *proftype, *format, *preset, *restriction, *pname, *description;
499 GstCaps *formatcaps = NULL;
500 GstCaps *restrictioncaps = NULL;
501 gboolean variableframerate;
503 gsize i, nbencprofiles;
505 GST_DEBUG ("parentprofilename : %s , profilename : %s",
506 parentprofilename, profilename);
508 if (parentprofilename) {
509 gboolean found = FALSE;
512 g_key_file_get_string_list (in, profilename, "parent",
513 &nbencprofiles, NULL);
514 if (!parent || !nbencprofiles) {
518 /* Check if this streamprofile is used in <profilename> */
519 for (i = 0; i < nbencprofiles; i++) {
520 if (!g_strcmp0 (parent[i], parentprofilename)) {
528 GST_DEBUG ("Stream profile '%s' isn't used in profile '%s'",
529 profilename, parentprofilename);
534 pname = g_key_file_get_value (in, profilename, "name", NULL);
536 /* First try to get localized description */
538 g_key_file_get_locale_string (in, profilename, "description",
539 setlocale (LC_ALL, NULL), NULL);
540 if (description == NULL)
541 description = g_key_file_get_value (in, profilename, "description", NULL);
543 /* Parse the remaining fields */
544 proftype = g_key_file_get_value (in, profilename, "type", NULL);
546 GST_WARNING ("Missing 'type' field for streamprofile %s", profilename);
550 format = g_key_file_get_value (in, profilename, "format", NULL);
552 formatcaps = gst_caps_from_string (format);
556 preset = g_key_file_get_value (in, profilename, "preset", NULL);
558 restriction = g_key_file_get_value (in, profilename, "restriction", NULL);
560 restrictioncaps = gst_caps_from_string (restriction);
561 g_free (restriction);
564 presence = g_key_file_get_integer (in, profilename, "presence", NULL);
565 pass = g_key_file_get_integer (in, profilename, "pass", NULL);
567 g_key_file_get_boolean (in, profilename, "variableframerate", NULL);
569 /* Build the streamprofile ! */
570 if (!g_strcmp0 (proftype, "container")) {
571 GstEncodingProfile *pprof;
574 (GstEncodingProfile *) gst_encoding_container_profile_new (pname,
575 description, formatcaps, preset);
576 /* Now look for the stream profiles */
577 for (i = 0; i < nbgroups; i++) {
578 if (!g_ascii_strncasecmp (groups[i], "streamprofile-", 13)) {
579 pprof = parse_encoding_profile (in, pname, groups[i], nbgroups, groups);
581 gst_encoding_container_profile_add_profile (
582 (GstEncodingContainerProfile *) sprof, pprof);
586 } else if (!g_strcmp0 (proftype, "video")) {
588 (GstEncodingProfile *) gst_encoding_video_profile_new (formatcaps,
589 preset, restrictioncaps, presence);
590 gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile
591 *) sprof, variableframerate);
592 gst_encoding_video_profile_set_pass ((GstEncodingVideoProfile *) sprof,
594 } else if (!g_strcmp0 (proftype, "audio")) {
596 (GstEncodingProfile *) gst_encoding_audio_profile_new (formatcaps,
597 preset, restrictioncaps, presence);
599 GST_ERROR ("Unknown profile format '%s'", proftype);
602 gst_caps_unref (restrictioncaps);
604 gst_caps_unref (formatcaps);
609 g_free (description);
618 static GstEncodingTarget *
619 parse_keyfile (GKeyFile * in, gchar * targetname, gchar * categoryname,
622 GstEncodingTarget *res = NULL;
623 GstEncodingProfile *prof;
627 res = gst_encoding_target_new (targetname, categoryname, description, NULL);
629 /* Figure out the various profiles */
630 groups = g_key_file_get_groups (in, &nbgroups);
631 for (i = 0; i < nbgroups; i++) {
632 if (!g_ascii_strncasecmp (groups[i], "profile-", 8)) {
633 prof = parse_encoding_profile (in, NULL, groups[i], nbgroups, groups);
635 gst_encoding_target_add_profile (res, prof);
644 g_free (categoryname);
646 g_free (description);
652 load_file_and_read_header (const gchar * path, gchar ** targetname,
653 gchar ** categoryname, gchar ** description, GError ** error)
658 in = g_key_file_new ();
660 GST_DEBUG ("path:%s", path);
663 g_key_file_load_from_file (in, path,
664 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, error);
665 if (!res || error != NULL)
669 g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "name", error);
674 g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "category", NULL);
676 g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "description",
683 GST_WARNING ("Unable to read GstEncodingTarget file %s: %s",
684 path, (*error)->message);
685 g_key_file_free (in);
691 GST_WARNING ("Wrong header in file %s: %s", path, (*error)->message);
692 g_key_file_free (in);
698 * gst_encoding_target_load_from_file:
699 * @filepath: The file location to load the #GstEncodingTarget from
700 * @error: If an error occured, this field will be filled in.
702 * Opens the provided file and returns the contained #GstEncodingTarget.
706 * Returns: (transfer full): The #GstEncodingTarget contained in the file, else
711 gst_encoding_target_load_from_file (const gchar * filepath, GError ** error)
714 gchar *targetname, *categoryname, *description;
715 GstEncodingTarget *res = NULL;
717 in = load_file_and_read_header (filepath, &targetname, &categoryname,
718 &description, error);
722 res = parse_keyfile (in, targetname, categoryname, description);
724 g_key_file_free (in);
731 * returned list contents must be freed
734 get_matching_filenames (gchar * path, gchar * filename)
738 const gchar *subdirname;
740 topdir = g_dir_open (path, 0, NULL);
741 if (G_UNLIKELY (topdir == NULL))
744 while ((subdirname = g_dir_read_name (topdir))) {
745 gchar *ltmp = g_build_filename (path, subdirname, NULL);
747 if (g_file_test (ltmp, G_FILE_TEST_IS_DIR)) {
748 gchar *tmp = g_build_filename (path, subdirname, filename, NULL);
749 /* Test to see if we have a file named like that in that directory */
750 if (g_file_test (tmp, G_FILE_TEST_EXISTS))
751 res = g_list_append (res, tmp);
758 g_dir_close (topdir);
763 static GstEncodingTarget *
764 gst_encoding_target_subload (gchar * path, const gchar * category,
765 gchar * lfilename, GError ** error)
767 GstEncodingTarget *target = NULL;
772 filename = g_build_filename (path, category, lfilename, NULL);
773 target = gst_encoding_target_load_from_file (filename, error);
776 GList *tmp, *tries = get_matching_filenames (path, lfilename);
778 /* Try to find a file named %s.gstprofile in any subdirectories */
779 for (tmp = tries; tmp; tmp = tmp->next) {
780 target = gst_encoding_target_load_from_file ((gchar *) tmp->data, NULL);
784 g_list_foreach (tries, (GFunc) g_free, NULL);
793 * gst_encoding_target_load:
794 * @name: the name of the #GstEncodingTarget to load.
795 * @category: (allow-none): the name of the target category, like
796 * #GST_ENCODING_CATEGORY_DEVICE. Can be %NULL
797 * @error: If an error occured, this field will be filled in.
799 * Searches for the #GstEncodingTarget with the given name, loads it
802 * If the category name is specified only targets from that category will be
807 * Returns: (transfer full): The #GstEncodingTarget if available, else %NULL.
810 gst_encoding_target_load (const gchar * name, const gchar * category,
813 gchar *lfilename, *tldir;
814 GstEncodingTarget *target = NULL;
816 g_return_val_if_fail (name != NULL, NULL);
818 if (!validate_name (name))
821 if (category && !validate_name (category))
822 goto invalid_category;
824 lfilename = g_strdup_printf ("%s" GST_ENCODING_TARGET_SUFFIX, name);
826 /* Try from local profiles */
828 g_build_filename (g_get_home_dir (), ".gstreamer-" GST_MAJORMINOR,
829 GST_ENCODING_TARGET_DIRECTORY, NULL);
830 target = gst_encoding_target_subload (tldir, category, lfilename, error);
833 if (target == NULL) {
834 /* Try from system-wide profiles */
836 g_build_filename (GST_DATADIR, "gstreamer-" GST_MAJORMINOR,
837 GST_ENCODING_TARGET_DIRECTORY, NULL);
838 target = gst_encoding_target_subload (tldir, category, lfilename, error);
848 GST_ERROR ("Invalid name for encoding target : '%s'", name);
853 GST_ERROR ("Invalid name for encoding category : '%s'", category);
859 * gst_encoding_target_save_to_file:
860 * @target: a #GstEncodingTarget
861 * @filepath: the location to store the @target at.
862 * @error: If an error occured, this field will be filled in.
864 * Saves the @target to the provided file location.
868 * Returns: %TRUE if the target was correctly saved, else %FALSE.
872 gst_encoding_target_save_to_file (GstEncodingTarget * target,
873 const gchar * filepath, GError ** error)
879 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), FALSE);
880 g_return_val_if_fail (filepath != NULL, FALSE);
882 /* FIXME : Check filepath is valid and writable
883 * FIXME : Strip out profiles already present in system target */
885 /* Get unique name... */
887 /* Create output GKeyFile */
888 out = g_key_file_new ();
890 if (!serialize_target (out, target))
891 goto serialize_failure;
893 if (!(data = g_key_file_to_data (out, &data_size, error)))
896 if (!g_file_set_contents (filepath, data, data_size, error))
899 g_key_file_free (out);
906 GST_ERROR ("Failure serializing target");
907 g_key_file_free (out);
913 GST_ERROR ("Failure converting keyfile: %s", (*error)->message);
914 g_key_file_free (out);
921 GST_ERROR ("Unable to write file %s: %s", filepath, (*error)->message);
922 g_key_file_free (out);
929 * gst_encoding_target_save:
930 * @target: a #GstEncodingTarget
931 * @error: If an error occured, this field will be filled in.
933 * Saves the @target to a default user-local directory.
937 * Returns: %TRUE if the target was correctly saved, else %FALSE.
941 gst_encoding_target_save (GstEncodingTarget * target, GError ** error)
947 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), FALSE);
948 g_return_val_if_fail (target->category != NULL, FALSE);
950 lfilename = g_strdup_printf ("%s" GST_ENCODING_TARGET_SUFFIX, target->name);
952 g_build_filename (g_get_home_dir (), ".gstreamer-" GST_MAJORMINOR,
953 GST_ENCODING_TARGET_DIRECTORY, target->category, lfilename, NULL);
956 res = gst_encoding_target_save_to_file (target, filename, error);
963 get_categories (gchar * path)
967 const gchar *subdirname;
969 topdir = g_dir_open (path, 0, NULL);
970 if (G_UNLIKELY (topdir == NULL))
973 while ((subdirname = g_dir_read_name (topdir))) {
974 gchar *ltmp = g_build_filename (path, subdirname, NULL);
976 if (g_file_test (ltmp, G_FILE_TEST_IS_DIR)) {
977 res = g_list_append (res, (gpointer) g_strdup (subdirname));
982 g_dir_close (topdir);
988 * gst_encoding_list_available_categories:
990 * Lists all #GstEncodingTarget categories present on disk.
992 * Returns: (transfer full) (element-type gchar*): A list
993 * of #GstEncodingTarget categories.
996 gst_encoding_list_available_categories (void)
1002 /* First try user-local categories */
1003 topdir = g_build_filename (g_get_home_dir (), ".gstreamer-" GST_MAJORMINOR,
1004 GST_ENCODING_TARGET_DIRECTORY, NULL);
1005 res = get_categories (topdir);
1008 /* Extend with system-wide categories */
1009 topdir = g_build_filename (GST_DATADIR, "gstreamer-" GST_MAJORMINOR,
1010 GST_ENCODING_TARGET_DIRECTORY, NULL);
1011 tmp1 = get_categories (topdir);
1014 for (tmp2 = tmp1; tmp2; tmp2 = tmp2->next) {
1015 gchar *name = (gchar *) tmp2->data;
1016 if (!g_list_find_custom (res, name, (GCompareFunc) g_strcmp0))
1017 res = g_list_append (res, (gpointer) name);
1026 static inline GList *
1027 sub_get_all_targets (gchar * subdir)
1030 const gchar *filename;
1032 GstEncodingTarget *target;
1034 dir = g_dir_open (subdir, 0, NULL);
1035 if (G_UNLIKELY (dir == NULL))
1038 while ((filename = g_dir_read_name (dir))) {
1041 /* Only try files ending with .gstprofile */
1042 if (!g_str_has_suffix (filename, GST_ENCODING_TARGET_SUFFIX))
1045 fullname = g_build_filename (subdir, filename, NULL);
1046 target = gst_encoding_target_load_from_file (fullname, NULL);
1048 res = g_list_append (res, target);
1050 GST_WARNING ("Failed to get a target from %s", fullname);
1058 static inline GList *
1059 get_all_targets (gchar * topdir, const gchar * categoryname)
1064 gchar *subdir = g_build_filename (topdir, categoryname, NULL);
1065 /* Try to open the directory */
1066 res = sub_get_all_targets (subdir);
1069 const gchar *subdirname;
1070 GDir *dir = g_dir_open (topdir, 0, NULL);
1072 if (G_UNLIKELY (dir == NULL))
1075 while ((subdirname = g_dir_read_name (dir))) {
1076 gchar *ltmp = g_build_filename (topdir, subdirname, NULL);
1078 if (g_file_test (ltmp, G_FILE_TEST_IS_DIR)) {
1079 res = g_list_concat (res, sub_get_all_targets (ltmp));
1090 compare_targets (const GstEncodingTarget * ta, const GstEncodingTarget * tb)
1092 if (!g_strcmp0 (ta->name, tb->name)
1093 && !g_strcmp0 (ta->category, tb->category))
1100 * gst_encoding_list_all_targets:
1101 * @categoryname: (allow-none): The category, for ex: #GST_ENCODING_CATEGORY_DEVICE.
1104 * List all available #GstEncodingTarget for the specified category, or all categories
1105 * if @categoryname is %NULL.
1107 * Returns: (transfer full) (element-type GstEncodingTarget): The list of #GstEncodingTarget
1110 gst_encoding_list_all_targets (const gchar * categoryname)
1116 /* Get user-locals */
1117 topdir = g_build_filename (g_get_home_dir (), ".gstreamer-" GST_MAJORMINOR,
1118 GST_ENCODING_TARGET_DIRECTORY, NULL);
1119 res = get_all_targets (topdir, categoryname);
1122 /* Get system-wide */
1123 topdir = g_build_filename (GST_DATADIR, "gstreamer-" GST_MAJORMINOR,
1124 GST_ENCODING_TARGET_DIRECTORY, NULL);
1125 tmp1 = get_all_targets (topdir, categoryname);
1128 /* Merge system-wide targets */
1129 /* FIXME : We should merge the system-wide profiles into the user-locals
1130 * instead of stopping at identical target names */
1131 for (tmp2 = tmp1; tmp2; tmp2 = tmp2->next) {
1132 GstEncodingTarget *target = (GstEncodingTarget *) tmp2->data;
1133 if (g_list_find_custom (res, target, (GCompareFunc) compare_targets))
1134 gst_encoding_target_unref (target);
1136 res = g_list_append (res, target);