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: 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: 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: 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: A list of #GstEncodingProfile(s) this @target handles.
149 gst_encoding_target_get_profiles (GstEncodingTarget * target)
151 return target->profiles;
154 static inline gboolean
155 validate_name (const gchar * name)
163 /* First character can only be a lower case ASCII character */
164 if (!g_ascii_isalpha (name[0]) || !g_ascii_islower (name[0]))
167 /* All following characters can only by:
168 * either a lower case ASCII character
171 for (i = 1; i < len; i++) {
172 /* if uppercase ASCII letter, return */
173 if (g_ascii_isupper (name[i]))
175 /* if a digit, continue */
176 if (g_ascii_isdigit (name[i]))
178 /* if an hyphen, continue */
181 /* remaining should only be ascii letters */
182 if (!g_ascii_isalpha (name[i]))
190 * gst_encoding_target_new:
191 * @name: The name of the target.
192 * @category: The name of the category to which this @target belongs.
193 * @description: A description of #GstEncodingTarget in the current locale.
194 * @profiles: A #GList of #GstEncodingProfile.
196 * Creates a new #GstEncodingTarget.
198 * The name and category can only consist of lowercase ASCII letters for the
199 * first character, followed by either lowercase ASCII letters, digits or
204 * Returns: The newly created #GstEncodingTarget or %NULL if there was an
209 gst_encoding_target_new (const gchar * name, const gchar * category,
210 const gchar * description, const GList * profiles)
212 GstEncodingTarget *res;
214 g_return_val_if_fail (name != NULL, NULL);
215 g_return_val_if_fail (category != NULL, NULL);
216 g_return_val_if_fail (description != NULL, NULL);
219 if (!validate_name (name))
221 if (!validate_name (category))
222 goto invalid_category;
224 res = (GstEncodingTarget *) gst_mini_object_new (GST_TYPE_ENCODING_TARGET);
225 res->name = g_strdup (name);
226 res->category = g_strdup (category);
227 res->description = g_strdup (description);
230 GstEncodingProfile *prof = (GstEncodingProfile *) profiles->data;
233 g_list_append (res->profiles, gst_encoding_profile_ref (prof));
234 profiles = profiles->next;
241 GST_ERROR ("Invalid name for encoding category : '%s'", name);
247 GST_ERROR ("Invalid category for encoding category : '%s'", category);
253 * gst_encoding_target_add_profile:
254 * @target: the #GstEncodingTarget to add a profile to
255 * @profile: the #GstEncodingProfile to add
257 * Adds the given @profile to the @target.
259 * The @target will steal a reference to the @profile. If you wish to use
260 * the profile after calling this method, you should increase its reference
265 * Returns: %TRUE if the profile was added, else %FALSE.
269 gst_encoding_target_add_profile (GstEncodingTarget * target,
270 GstEncodingProfile * profile)
274 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), FALSE);
275 g_return_val_if_fail (GST_IS_ENCODING_PROFILE (profile), FALSE);
277 /* Make sure profile isn't already controlled by this target */
278 for (tmp = target->profiles; tmp; tmp = tmp->next) {
279 GstEncodingProfile *prof = (GstEncodingProfile *) tmp->data;
281 if (!g_strcmp0 (gst_encoding_profile_get_name (profile),
282 gst_encoding_profile_get_name (prof))) {
283 GST_WARNING ("Profile already present in target");
288 target->profiles = g_list_append (target->profiles, profile);
294 serialize_stream_profiles (GKeyFile * out, GstEncodingProfile * sprof,
295 const gchar * profilename, guint id)
297 gchar *sprofgroupname;
299 const GstCaps *format, *restriction;
300 const gchar *preset, *name, *description;
302 sprofgroupname = g_strdup_printf ("streamprofile-%s-%d", profilename, id);
304 /* Write the parent profile */
305 g_key_file_set_value (out, sprofgroupname, "parent", profilename);
307 g_key_file_set_value (out, sprofgroupname, "type",
308 gst_encoding_profile_get_type_nick (sprof));
310 format = gst_encoding_profile_get_format (sprof);
312 tmpc = gst_caps_to_string (format);
313 g_key_file_set_value (out, sprofgroupname, "format", tmpc);
317 name = gst_encoding_profile_get_name (sprof);
319 g_key_file_set_string (out, sprofgroupname, "name", name);
321 description = gst_encoding_profile_get_description (sprof);
323 g_key_file_set_string (out, sprofgroupname, "description", description);
325 preset = gst_encoding_profile_get_preset (sprof);
327 g_key_file_set_string (out, sprofgroupname, "preset", preset);
329 restriction = gst_encoding_profile_get_restriction (sprof);
331 tmpc = gst_caps_to_string (restriction);
332 g_key_file_set_value (out, sprofgroupname, "restriction", tmpc);
335 g_key_file_set_integer (out, sprofgroupname, "presence",
336 gst_encoding_profile_get_presence (sprof));
338 if (GST_IS_ENCODING_VIDEO_PROFILE (sprof)) {
339 GstEncodingVideoProfile *vp = (GstEncodingVideoProfile *) sprof;
341 g_key_file_set_integer (out, sprofgroupname, "pass",
342 gst_encoding_video_profile_get_pass (vp));
343 g_key_file_set_boolean (out, sprofgroupname, "variableframerate",
344 gst_encoding_video_profile_get_variableframerate (vp));
347 g_free (sprofgroupname);
351 /* Serialize the top-level profiles
352 * Note: They don't have to be containerprofiles */
354 serialize_encoding_profile (GKeyFile * out, GstEncodingProfile * prof)
356 gchar *profgroupname;
359 const gchar *profname, *profdesc, *profpreset, *proftype;
360 const GstCaps *profformat, *profrestriction;
362 profname = gst_encoding_profile_get_name (prof);
363 profdesc = gst_encoding_profile_get_description (prof);
364 profformat = gst_encoding_profile_get_format (prof);
365 profpreset = gst_encoding_profile_get_preset (prof);
366 proftype = gst_encoding_profile_get_type_nick (prof);
367 profrestriction = gst_encoding_profile_get_restriction (prof);
369 profgroupname = g_strdup_printf ("profile-%s", profname);
371 g_key_file_set_string (out, profgroupname, "name", profname);
373 g_key_file_set_value (out, profgroupname, "type",
374 gst_encoding_profile_get_type_nick (prof));
377 g_key_file_set_locale_string (out, profgroupname, "description",
378 setlocale (LC_ALL, NULL), profdesc);
380 gchar *tmpc = gst_caps_to_string (profformat);
381 g_key_file_set_string (out, profgroupname, "format", tmpc);
385 g_key_file_set_string (out, profgroupname, "preset", profpreset);
387 /* stream profiles */
388 if (GST_IS_ENCODING_CONTAINER_PROFILE (prof)) {
390 gst_encoding_container_profile_get_profiles
391 (GST_ENCODING_CONTAINER_PROFILE (prof)), i = 0; tmp;
392 tmp = tmp->next, i++) {
393 GstEncodingProfile *sprof = (GstEncodingProfile *) tmp->data;
395 if (!serialize_stream_profiles (out, sprof, profname, i))
399 g_free (profgroupname);
404 serialize_target (GKeyFile * out, GstEncodingTarget * target)
408 g_key_file_set_string (out, GST_ENCODING_TARGET_HEADER, "name", target->name);
409 g_key_file_set_string (out, GST_ENCODING_TARGET_HEADER, "category",
411 g_key_file_set_string (out, GST_ENCODING_TARGET_HEADER, "description",
412 target->description);
414 for (tmp = target->profiles; tmp; tmp = tmp->next) {
415 GstEncodingProfile *prof = (GstEncodingProfile *) tmp->data;
416 if (!serialize_encoding_profile (out, prof))
424 * parse_encoding_profile:
426 * @parentprofilename: the parent profile name (including 'profile-' or 'streamprofile-' header)
427 * @profilename: the profile name group to parse
428 * @nbgroups: the number of top-level groups
429 * @groups: the top-level groups
431 static GstEncodingProfile *
432 parse_encoding_profile (GKeyFile * in, gchar * parentprofilename,
433 gchar * profilename, gsize nbgroups, gchar ** groups)
435 GstEncodingProfile *sprof = NULL;
437 gchar *proftype, *format, *preset, *restriction, *pname, *description;
438 GstCaps *formatcaps = NULL;
439 GstCaps *restrictioncaps = NULL;
440 gboolean variableframerate;
442 gsize i, nbencprofiles;
444 GST_DEBUG ("parentprofilename : %s , profilename : %s",
445 parentprofilename, profilename);
447 if (parentprofilename) {
448 gboolean found = FALSE;
451 g_key_file_get_string_list (in, profilename, "parent",
452 &nbencprofiles, NULL);
453 if (!parent || !nbencprofiles) {
457 /* Check if this streamprofile is used in <profilename> */
458 for (i = 0; i < nbencprofiles; i++) {
459 if (!g_strcmp0 (parent[i], parentprofilename)) {
467 GST_DEBUG ("Stream profile '%s' isn't used in profile '%s'",
468 profilename, parentprofilename);
473 pname = g_key_file_get_value (in, profilename, "name", NULL);
475 /* First try to get localized description */
477 g_key_file_get_locale_string (in, profilename, "description",
478 setlocale (LC_ALL, NULL), NULL);
479 if (description == NULL)
480 description = g_key_file_get_value (in, profilename, "description", NULL);
482 /* Parse the remaining fields */
483 proftype = g_key_file_get_value (in, profilename, "type", NULL);
485 GST_WARNING ("Missing 'type' field for streamprofile %s", profilename);
489 format = g_key_file_get_value (in, profilename, "format", NULL);
491 formatcaps = gst_caps_from_string (format);
495 preset = g_key_file_get_value (in, profilename, "preset", NULL);
497 restriction = g_key_file_get_value (in, profilename, "restriction", NULL);
499 restrictioncaps = gst_caps_from_string (restriction);
500 g_free (restriction);
503 presence = g_key_file_get_integer (in, profilename, "presence", NULL);
504 pass = g_key_file_get_integer (in, profilename, "pass", NULL);
506 g_key_file_get_boolean (in, profilename, "variableframerate", NULL);
508 /* Build the streamprofile ! */
509 if (!g_strcmp0 (proftype, "container")) {
510 GstEncodingProfile *pprof;
513 (GstEncodingProfile *) gst_encoding_container_profile_new (pname,
514 description, formatcaps, preset);
515 /* Now look for the stream profiles */
516 for (i = 0; i < nbgroups; i++) {
517 if (!g_ascii_strncasecmp (groups[i], "streamprofile-", 13)) {
518 pprof = parse_encoding_profile (in, pname, groups[i], nbgroups, groups);
520 gst_encoding_container_profile_add_profile (
521 (GstEncodingContainerProfile *) sprof, pprof);
525 } else if (!g_strcmp0 (proftype, "video")) {
527 (GstEncodingProfile *) gst_encoding_video_profile_new (formatcaps,
528 preset, restrictioncaps, presence);
529 gst_encoding_video_profile_set_variableframerate ((GstEncodingVideoProfile
530 *) sprof, variableframerate);
531 gst_encoding_video_profile_set_pass ((GstEncodingVideoProfile *) sprof,
533 } else if (!g_strcmp0 (proftype, "audio")) {
535 (GstEncodingProfile *) gst_encoding_audio_profile_new (formatcaps,
536 preset, restrictioncaps, presence);
538 GST_ERROR ("Unknown profile format '%s'", proftype);
541 gst_caps_unref (restrictioncaps);
543 gst_caps_unref (formatcaps);
548 g_free (description);
557 static GstEncodingTarget *
558 parse_keyfile (GKeyFile * in, gchar * targetname, gchar * categoryname,
561 GstEncodingTarget *res = NULL;
562 GstEncodingProfile *prof;
566 res = gst_encoding_target_new (targetname, categoryname, description, NULL);
568 /* Figure out the various profiles */
569 groups = g_key_file_get_groups (in, &nbgroups);
570 for (i = 0; i < nbgroups; i++) {
571 if (!g_ascii_strncasecmp (groups[i], "profile-", 8)) {
572 prof = parse_encoding_profile (in, NULL, groups[i], nbgroups, groups);
574 gst_encoding_target_add_profile (res, prof);
583 g_free (categoryname);
585 g_free (description);
591 load_file_and_read_header (const gchar * path, gchar ** targetname,
592 gchar ** categoryname, gchar ** description, GError ** error)
597 in = g_key_file_new ();
599 GST_DEBUG ("path:%s", path);
602 g_key_file_load_from_file (in, path,
603 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, error);
604 if (!res || error != NULL)
608 g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "name", error);
613 g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "category", NULL);
615 g_key_file_get_value (in, GST_ENCODING_TARGET_HEADER, "description",
622 GST_WARNING ("Unable to read GstEncodingTarget file %s: %s",
623 path, (*error)->message);
624 g_key_file_free (in);
630 GST_WARNING ("Wrong header in file %s: %s", path, (*error)->message);
631 g_key_file_free (in);
637 * gst_encoding_target_load_from:
638 * @path: The file to load the #GstEncodingTarget from
639 * @error: If an error occured, this field will be filled in.
641 * Opens the provided file and returns the contained #GstEncodingTarget.
645 * Returns: The #GstEncodingTarget contained in the file, else %NULL
649 gst_encoding_target_load_from (const gchar * path, GError ** error)
652 gchar *targetname, *categoryname, *description;
653 GstEncodingTarget *res = NULL;
655 in = load_file_and_read_header (path, &targetname, &categoryname,
656 &description, error);
660 res = parse_keyfile (in, targetname, categoryname, description);
662 g_key_file_free (in);
669 * gst_encoding_target_load:
670 * @name: the name of the #GstEncodingTarget to load.
671 * @error: If an error occured, this field will be filled in.
673 * Searches for the #GstEncodingTarget with the given name, loads it
676 * Warning: NOT IMPLEMENTED.
680 * Returns: The #GstEncodingTarget if available, else %NULL
684 gst_encoding_target_load (const gchar * name, GError ** error)
686 /* FIXME : IMPLEMENT */
691 * gst_encoding_target_save:
692 * @target: a #GstEncodingTarget
693 * @error: If an error occured, this field will be filled in.
695 * Saves the @target to the default location.
697 * Warning: NOT IMPLEMENTED.
701 * Returns: %TRUE if the target was correctly saved, else %FALSE.
705 gst_encoding_target_save (GstEncodingTarget * target, GError ** error)
707 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), FALSE);
709 /* FIXME : IMPLEMENT */
714 * gst_encoding_target_save_to:
715 * @target: a #GstEncodingTarget
716 * @path: the location to store the @target at.
717 * @error: If an error occured, this field will be filled in.
719 * Saves the @target to the provided location.
723 * Returns: %TRUE if the target was correctly saved, else %FALSE.
727 gst_encoding_target_save_to (GstEncodingTarget * target, const gchar * path,
734 g_return_val_if_fail (GST_IS_ENCODING_TARGET (target), FALSE);
735 g_return_val_if_fail (path != NULL, FALSE);
737 /* FIXME : Check path is valid and writable
738 * FIXME : Strip out profiles already present in system target */
740 /* Get unique name... */
742 /* Create output GKeyFile */
743 out = g_key_file_new ();
745 if (!serialize_target (out, target))
746 goto serialize_failure;
748 if (!(data = g_key_file_to_data (out, &data_size, error)))
751 if (!g_file_set_contents (path, data, data_size, error))
754 g_key_file_free (out);
761 GST_ERROR ("Failure serializing target");
762 g_key_file_free (out);
768 GST_ERROR ("Failure converting keyfile: %s", (*error)->message);
769 g_key_file_free (out);
776 GST_ERROR ("Unable to write file %s: %s", path, (*error)->message);
777 g_key_file_free (out);