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.
23 #include "encoding-target.h"
30 * [_gstencodingtarget_]
32 * category : <category>
33 * description : <description> #translatable
35 * [profile-<profile1name>]
37 * description : <description> #optional
41 * [streamprofile-<id>]
42 * parent : <encodingprofile.name>[,<encodingprofile.name>..]
43 * type : <type> # "audio", "video", "text"
46 * restriction : <restriction>
47 * presence : <presence>
49 * variableframerate : <variableframerate>
52 #define GST_ENCODING_TARGET_HEADER "_gstencodingtarget_"
54 struct _GstEncodingTarget
67 G_DEFINE_TYPE (GstEncodingTarget, gst_encoding_target, GST_TYPE_MINI_OBJECT);
70 gst_encoding_target_init (GstEncodingTarget * target)
72 /* Nothing to initialize */
76 gst_encoding_target_finalize (GstEncodingTarget * target)
78 GST_DEBUG ("Finalizing");
81 g_free (target->name);
83 g_free (target->category);
84 if (target->description)
85 g_free (target->description);
87 g_list_foreach (target->profiles, (GFunc) gst_mini_object_unref, NULL);
88 g_list_free (target->profiles);
92 gst_encoding_target_class_init (GstMiniObjectClass * klass)
95 (GstMiniObjectFinalizeFunction) gst_encoding_target_finalize;
99 * gst_encoding_target_get_name:
100 * @target: a #GstEncodingTarget
104 * Returns: (transfer none): The name of the @target.
107 gst_encoding_target_get_name (GstEncodingTarget * target)
113 * gst_encoding_target_get_category:
114 * @target: a #GstEncodingTarget
118 * Returns: (transfer none): The category of the @target.
121 gst_encoding_target_get_category (GstEncodingTarget * target)
123 return target->category;
127 * gst_encoding_target_get_description:
128 * @target: a #GstEncodingTarget
132 * Returns: (transfer none): The description of the @target.
135 gst_encoding_target_get_description (GstEncodingTarget * target)
137 return target->description;
141 * gst_encoding_target_get_profiles:
142 * @target: a #GstEncodingTarget
146 * Returns: (transfer none) (element-type Gst.EncodingProfile): A list of
147 * #GstEncodingProfile(s) this @target handles.
150 gst_encoding_target_get_profiles (GstEncodingTarget * target)
152 return target->profiles;
155 static inline gboolean
156 validate_name (const gchar * name)
164 /* First character can only be a lower case ASCII character */
165 if (!g_ascii_isalpha (name[0]) || !g_ascii_islower (name[0]))
168 /* All following characters can only by:
169 * either a lower case ASCII character
172 for (i = 1; i < len; i++) {
173 /* if uppercase ASCII letter, return */
174 if (g_ascii_isupper (name[i]))
176 /* if a digit, continue */
177 if (g_ascii_isdigit (name[i]))
179 /* if an hyphen, continue */
182 /* remaining should only be ascii letters */
183 if (!g_ascii_isalpha (name[i]))
191 * gst_encoding_target_new:
192 * @name: The name of the target.
193 * @category: (transfer none): The name of the category to which this @target
195 * @description: (transfer none): A description of #GstEncodingTarget in the
197 * @profiles: (transfer none) (element-type Gst.EncodingProfile): A #GList of
198 * #GstEncodingProfile.
200 * Creates a new #GstEncodingTarget.
202 * The name and category can only consist of lowercase ASCII letters for the
203 * first character, followed by either lowercase ASCII letters, digits or
208 * Returns: (transfer full): The newly created #GstEncodingTarget or %NULL if
209 * there was an error.
213 gst_encoding_target_new (const gchar * name, const gchar * category,
214 const gchar * description, const GList * profiles)
216 GstEncodingTarget *res;
218 g_return_val_if_fail (name != NULL, NULL);
219 g_return_val_if_fail (category != NULL, NULL);
220 g_return_val_if_fail (description != NULL, NULL);
223 if (!validate_name (name))
225 if (!validate_name (category))
226 goto invalid_category;
228 res = (GstEncodingTarget *) gst_mini_object_new (GST_TYPE_ENCODING_TARGET);
229 res->name = g_strdup (name);
230 res->category = g_strdup (category);
231 res->description = g_strdup (description);
234 GstEncodingProfile *prof = (GstEncodingProfile *) profiles->data;
237 g_list_append (res->profiles, gst_encoding_profile_ref (prof));
238 profiles = profiles->next;
245 GST_ERROR ("Invalid name for encoding target : '%s'", name);
251 GST_ERROR ("Invalid name for encoding category : '%s'", category);
257 * gst_encoding_target_add_profile:
258 * @target: the #GstEncodingTarget to add a profile to
259 * @profile: (transfer full): the #GstEncodingProfile to add
261 * Adds the given @profile to the @target.
263 * The @target will steal a reference to the @profile. If you wish to use
264 * the profile after calling this method, you should increase its reference
269 * Returns: %TRUE if the profile was added, else %FALSE.
273 gst_encoding_target_add_profile (GstEncodingTarget * target,
274 GstEncodingProfile * profile)
278 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), FALSE);
279 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
281 /* Make sure profile isn't already controlled by this target */
282 for (tmp = target->profiles; tmp; tmp = tmp->next) {
283 GstEncodingProfile *prof = (GstEncodingProfile *) tmp->data;
285 if (!g_strcmp0 (gst_encoding_profile_get_name (profile),
286 gst_encoding_profile_get_name (prof))) {
287 GST_WARNING ("Profile already present in target");
292 target->profiles = g_list_append (target->profiles, profile);
298 serialize_stream_profiles (GKeyFile * out, GstEncodingProfile * sprof,
299 const gchar * profilename, guint id)
301 gchar *sprofgroupname;
303 const GstCaps *format, *restriction;
304 const gchar *preset, *name, *description;
306 sprofgroupname = g_strdup_printf ("streamprofile-%s-%d", profilename, id);
308 /* Write the parent profile */
309 g_key_file_set_value (out, sprofgroupname, "parent", profilename);
311 g_key_file_set_value (out, sprofgroupname, "type",
312 gst_encoding_profile_get_type_nick (sprof));
314 format = gst_encoding_profile_get_format (sprof);
316 tmpc = gst_caps_to_string (format);
317 g_key_file_set_value (out, sprofgroupname, "format", tmpc);
321 name = gst_encoding_profile_get_name (sprof);
323 g_key_file_set_string (out, sprofgroupname, "name", name);
325 description = gst_encoding_profile_get_description (sprof);
327 g_key_file_set_string (out, sprofgroupname, "description", description);
329 preset = gst_encoding_profile_get_preset (sprof);
331 g_key_file_set_string (out, sprofgroupname, "preset", preset);
333 restriction = gst_encoding_profile_get_restriction (sprof);
335 tmpc = gst_caps_to_string (restriction);
336 g_key_file_set_value (out, sprofgroupname, "restriction", tmpc);
339 g_key_file_set_integer (out, sprofgroupname, "presence",
340 gst_encoding_profile_get_presence (sprof));
342 if (GST_IS_ENCODING_VIDEO_PROFILE (sprof)) {
343 GstEncodingVideoProfile *vp = (GstEncodingVideoProfile *) sprof;
345 g_key_file_set_integer (out, sprofgroupname, "pass",
346 gst_encoding_video_profile_get_pass (vp));
347 g_key_file_set_boolean (out, sprofgroupname, "variableframerate",
348 gst_encoding_video_profile_get_variableframerate (vp));
351 g_free (sprofgroupname);
355 /* Serialize the top-level profiles
356 * Note: They don't have to be containerprofiles */
358 serialize_encoding_profile (GKeyFile * out, GstEncodingProfile * prof)
360 gchar *profgroupname;
363 const gchar *profname, *profdesc, *profpreset, *proftype;
364 const GstCaps *profformat, *profrestriction;
366 profname = gst_encoding_profile_get_name (prof);
367 profdesc = gst_encoding_profile_get_description (prof);
368 profformat = gst_encoding_profile_get_format (prof);
369 profpreset = gst_encoding_profile_get_preset (prof);
370 proftype = gst_encoding_profile_get_type_nick (prof);
371 profrestriction = gst_encoding_profile_get_restriction (prof);
373 profgroupname = g_strdup_printf ("profile-%s", profname);
375 g_key_file_set_string (out, profgroupname, "name", profname);
377 g_key_file_set_value (out, profgroupname, "type",
378 gst_encoding_profile_get_type_nick (prof));
381 g_key_file_set_locale_string (out, profgroupname, "description",
382 setlocale (LC_ALL, NULL), profdesc);
384 gchar *tmpc = gst_caps_to_string (profformat);
385 g_key_file_set_string (out, profgroupname, "format", tmpc);
389 g_key_file_set_string (out, profgroupname, "preset", profpreset);
391 /* stream profiles */
392 if (GST_IS_ENCODING_CONTAINER_PROFILE (prof)) {
394 gst_encoding_container_profile_get_profiles
395 (GST_ENCODING_CONTAINER_PROFILE (prof)), i = 0; tmp;
396 tmp = tmp->next, i++) {
397 GstEncodingProfile *sprof = (GstEncodingProfile *) tmp->data;
399 if (!serialize_stream_profiles (out, sprof, profname, i))
403 g_free (profgroupname);
408 serialize_target (GKeyFile * out, GstEncodingTarget * target)
412 g_key_file_set_string (out, GST_ENCODING_TARGET_HEADER, "name", target->name);
413 g_key_file_set_string (out, GST_ENCODING_TARGET_HEADER, "category",
415 g_key_file_set_string (out, GST_ENCODING_TARGET_HEADER, "description",
416 target->description);
418 for (tmp = target->profiles; tmp; tmp = tmp->next) {
419 GstEncodingProfile *prof = (GstEncodingProfile *) tmp->data;
420 if (!serialize_encoding_profile (out, prof))
428 * parse_encoding_profile:
430 * @parentprofilename: the parent profile name (including 'profile-' or 'streamprofile-' header)
431 * @profilename: the profile name group to parse
432 * @nbgroups: the number of top-level groups
433 * @groups: the top-level groups
435 static GstEncodingProfile *
436 parse_encoding_profile (GKeyFile * in, gchar * parentprofilename,
437 gchar * profilename, gsize nbgroups, gchar ** groups)
439 GstEncodingProfile *sprof = NULL;
441 gchar *proftype, *format, *preset, *restriction, *pname, *description;
442 GstCaps *formatcaps = NULL;
443 GstCaps *restrictioncaps = NULL;
444 gboolean variableframerate;
446 gsize i, nbencprofiles;
448 GST_DEBUG ("parentprofilename : %s , profilename : %s",
449 parentprofilename, profilename);
451 if (parentprofilename) {
452 gboolean found = FALSE;
455 g_key_file_get_string_list (in, profilename, "parent",
456 &nbencprofiles, NULL);
457 if (!parent || !nbencprofiles) {
461 /* Check if this streamprofile is used in <profilename> */
462 for (i = 0; i < nbencprofiles; i++) {
463 if (!g_strcmp0 (parent[i], parentprofilename)) {
471 GST_DEBUG ("Stream profile '%s' isn't used in profile '%s'",
472 profilename, parentprofilename);
477 pname = g_key_file_get_value (in, profilename, "name", NULL);
479 /* First try to get localized description */
481 g_key_file_get_locale_string (in, profilename, "description",
482 setlocale (LC_ALL, NULL), NULL);
483 if (description == NULL)
484 description = g_key_file_get_value (in, profilename, "description", NULL);
486 /* Parse the remaining fields */
487 proftype = g_key_file_get_value (in, profilename, "type", NULL);
489 GST_WARNING ("Missing 'type' field for streamprofile %s", profilename);
493 format = g_key_file_get_value (in, profilename, "format", NULL);
495 formatcaps = gst_caps_from_string (format);
499 preset = g_key_file_get_value (in, profilename, "preset", NULL);
501 restriction = g_key_file_get_value (in, profilename, "restriction", NULL);
503 restrictioncaps = gst_caps_from_string (restriction);
504 g_free (restriction);
507 presence = g_key_file_get_integer (in, profilename, "presence", NULL);
508 pass = g_key_file_get_integer (in, profilename, "pass", NULL);
510 g_key_file_get_boolean (in, profilename, "variableframerate", NULL);
512 /* Build the streamprofile ! */
513 if (!g_strcmp0 (proftype, "container")) {
514 GstEncodingProfile *pprof;
517 (GstEncodingProfile *) gst_encoding_container_profile_new (pname,
518 description, formatcaps, preset);
519 /* Now look for the stream profiles */
520 for (i = 0; i < nbgroups; i++) {
521 if (!g_ascii_strncasecmp (groups[i], "streamprofile-", 13)) {
522 pprof = parse_encoding_profile (in, pname, groups[i], nbgroups, groups);
524 gst_encoding_container_profile_add_profile (
525 (GstEncodingContainerProfile *) sprof, pprof);
529 } else if (!g_strcmp0 (proftype, "video")) {
531 (GstEncodingProfile *) gst_encoding_video_profile_new (formatcaps,
532 preset, restrictioncaps, presence);
533 gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile
534 *) sprof, variableframerate);
535 gst_encoding_video_profile_set_pass ((GstEncodingVideoProfile *) sprof,
537 } else if (!g_strcmp0 (proftype, "audio")) {
539 (GstEncodingProfile *) gst_encoding_audio_profile_new (formatcaps,
540 preset, restrictioncaps, presence);
542 GST_ERROR ("Unknown profile format '%s'", proftype);
545 gst_caps_unref (restrictioncaps);
547 gst_caps_unref (formatcaps);
552 g_free (description);
561 static GstEncodingTarget *
562 parse_keyfile (GKeyFile * in, gchar * targetname, gchar * categoryname,
565 GstEncodingTarget *res = NULL;
566 GstEncodingProfile *prof;
570 res = gst_encoding_target_new (targetname, categoryname, description, NULL);
572 /* Figure out the various profiles */
573 groups = g_key_file_get_groups (in, &nbgroups);
574 for (i = 0; i < nbgroups; i++) {
575 if (!g_ascii_strncasecmp (groups[i], "profile-", 8)) {
576 prof = parse_encoding_profile (in, NULL, groups[i], nbgroups, groups);
578 gst_encoding_target_add_profile (res, prof);
587 g_free (categoryname);
589 g_free (description);
595 load_file_and_read_header (const gchar * path, gchar ** targetname,
596 gchar ** categoryname, gchar ** description, GError ** error)
601 in = g_key_file_new ();
603 GST_DEBUG ("path:%s", path);
606 g_key_file_load_from_file (in, path,
607 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, error);
608 if (!res || error != NULL)
612 g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "name", error);
617 g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "category", NULL);
619 g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "description",
626 GST_WARNING ("Unable to read GstEncodingTarget file %s: %s",
627 path, (*error)->message);
628 g_key_file_free (in);
634 GST_WARNING ("Wrong header in file %s: %s", path, (*error)->message);
635 g_key_file_free (in);
641 * gst_encoding_target_load_from:
642 * @path: The file to load the #GstEncodingTarget from
643 * @error: If an error occured, this field will be filled in.
645 * Opens the provided file and returns the contained #GstEncodingTarget.
649 * Returns: (transfer full): The #GstEncodingTarget contained in the file, else
654 gst_encoding_target_load_from (const gchar * path, GError ** error)
657 gchar *targetname, *categoryname, *description;
658 GstEncodingTarget *res = NULL;
660 in = load_file_and_read_header (path, &targetname, &categoryname,
661 &description, error);
665 res = parse_keyfile (in, targetname, categoryname, description);
667 g_key_file_free (in);
674 * gst_encoding_target_load:
675 * @name: the name of the #GstEncodingTarget to load.
676 * @error: If an error occured, this field will be filled in.
678 * Searches for the #GstEncodingTarget with the given name, loads it
681 * Warning: NOT IMPLEMENTED.
685 * Returns: (transfer full): The #GstEncodingTarget if available, else %NULL.
689 gst_encoding_target_load (const gchar * name, GError ** error)
691 /* FIXME : IMPLEMENT */
696 * gst_encoding_target_save:
697 * @target: a #GstEncodingTarget
698 * @error: If an error occured, this field will be filled in.
700 * Saves the @target to the default location.
702 * Warning: NOT IMPLEMENTED.
706 * Returns: %TRUE if the target was correctly saved, else %FALSE.
710 gst_encoding_target_save (GstEncodingTarget * target, GError ** error)
712 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), FALSE);
714 /* FIXME : IMPLEMENT */
719 * gst_encoding_target_save_to:
720 * @target: a #GstEncodingTarget
721 * @path: the location to store the @target at.
722 * @error: If an error occured, this field will be filled in.
724 * Saves the @target to the provided location.
728 * Returns: %TRUE if the target was correctly saved, else %FALSE.
732 gst_encoding_target_save_to (GstEncodingTarget * target, const gchar * path,
739 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), FALSE);
740 g_return_val_if_fail (path != NULL, FALSE);
742 /* FIXME : Check path is valid and writable
743 * FIXME : Strip out profiles already present in system target */
745 /* Get unique name... */
747 /* Create output GKeyFile */
748 out = g_key_file_new ();
750 if (!serialize_target (out, target))
751 goto serialize_failure;
753 if (!(data = g_key_file_to_data (out, &data_size, error)))
756 if (!g_file_set_contents (path, data, data_size, error))
759 g_key_file_free (out);
766 GST_ERROR ("Failure serializing target");
767 g_key_file_free (out);
773 GST_ERROR ("Failure converting keyfile: %s", (*error)->message);
774 g_key_file_free (out);
781 GST_ERROR ("Unable to write file %s: %s", path, (*error)->message);
782 g_key_file_free (out);