2 * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
4 * gststructure.c: lists of { GQuark, GValue } tuples
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.
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.
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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:gststructure
24 * @short_description: Generic structure containing fields of names and values
25 * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
27 * A #GstStructure is a collection of key/value pairs. The keys are expressed
28 * as GQuarks and the values can be of any GType.
30 * In addition to the key/value pairs, a #GstStructure also has a name. The name
31 * starts with a letter and can be folled by letters, numbers and any of "/-_.:".
33 * #GstStructure is used by various GStreamer subsystems to store information
34 * in a flexible and extensible way. A #GstStructure does not have a refcount
35 * because it usually is part of a higher level object such as #GstCaps. It
36 * provides a means to enforce mutability using the refcount of the parent
37 * with the gst_structure_set_parent_refcount() method.
39 * A #GstStructure can be created with gst_structure_empty_new() or
40 * gst_structure_new(), which both take a name and an optional set of
41 * key/value pairs along with the types of the values.
43 * Field values can be changed with gst_structure_set_value() or
44 * gst_structure_set().
46 * Field values can be retrieved with gst_structure_get_value() or the more
47 * convenient gst_structure_get_*() functions.
49 * Fields can be removed with gst_structure_remove_field() or
50 * gst_structure_remove_fields().
52 * Last reviewed on 2007-10-16 (0.10.15)
61 #include "gst_private.h"
63 #include <gobject/gvaluecollector.h>
65 typedef struct _GstStructureField GstStructureField;
67 struct _GstStructureField
73 #define GST_STRUCTURE_FIELD(structure, index) \
74 &g_array_index((structure)->fields, GstStructureField, (index))
76 #define IS_MUTABLE(structure) \
77 (!(structure)->parent_refcount || \
78 g_atomic_int_get ((structure)->parent_refcount) == 1)
80 static void gst_structure_set_field (GstStructure * structure,
81 GstStructureField * field);
82 static GstStructureField *gst_structure_get_field (const GstStructure *
83 structure, const gchar * fieldname);
84 static GstStructureField *gst_structure_id_get_field (const GstStructure *
85 structure, GQuark field);
86 static void gst_structure_transform_to_string (const GValue * src_value,
88 static GstStructure *gst_structure_copy_conditional (const GstStructure *
90 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
91 GValue * value, GType default_type);
92 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
95 gst_structure_get_type (void)
97 static GType gst_structure_type = 0;
99 if (G_UNLIKELY (gst_structure_type == 0)) {
100 gst_structure_type = g_boxed_type_register_static ("GstStructure",
101 (GBoxedCopyFunc) gst_structure_copy_conditional,
102 (GBoxedFreeFunc) gst_structure_free);
104 g_value_register_transform_func (gst_structure_type, G_TYPE_STRING,
105 gst_structure_transform_to_string);
108 return gst_structure_type;
111 static GstStructure *
112 gst_structure_id_empty_new_with_size (GQuark quark, guint prealloc)
114 GstStructure *structure;
116 structure = g_slice_new (GstStructure);
117 structure->type = gst_structure_get_type ();
118 structure->name = quark;
119 structure->parent_refcount = NULL;
121 g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
127 * gst_structure_id_empty_new:
128 * @quark: name of new structure
130 * Creates a new, empty #GstStructure with the given name as a GQuark.
132 * Returns: a new, empty #GstStructure
135 gst_structure_id_empty_new (GQuark quark)
137 g_return_val_if_fail (quark != 0, NULL);
139 return gst_structure_id_empty_new_with_size (quark, 0);
142 #ifndef G_DISABLE_CHECKS
144 gst_structure_validate_name (const gchar * name)
148 g_return_val_if_fail (name != NULL, FALSE);
150 /* FIXME 0.11: use g_ascii_isalpha() */
151 if (!g_ascii_isalnum (*name)) {
152 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
157 /* FIXME 0.11: don't allow spaces */
158 /* FIXME: test name string more */
160 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+ ", *s) != NULL))
163 GST_WARNING ("Invalid character '%c' at offset %lu in structure name: %s",
164 *s, ((gulong) s - (gulong) name), name);
173 * gst_structure_empty_new:
174 * @name: name of new structure
176 * Creates a new, empty #GstStructure with the given @name.
178 * See gst_structure_set_name() for constraints on the @name parameter.
180 * Returns: a new, empty #GstStructure
183 gst_structure_empty_new (const gchar * name)
185 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
187 return gst_structure_id_empty_new_with_size (g_quark_from_string (name), 0);
192 * @name: name of new structure
193 * @firstfield: name of first field to set
194 * @...: additional arguments
196 * Creates a new #GstStructure with the given name. Parses the
197 * list of variable arguments and sets fields to the values listed.
198 * Variable arguments should be passed as field name, field type,
199 * and value. Last variable argument should be NULL.
201 * Returns: a new #GstStructure
204 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
206 GstStructure *structure;
209 g_return_val_if_fail (name != NULL, NULL);
211 va_start (varargs, firstfield);
213 structure = gst_structure_new_valist (name, firstfield, varargs);
221 * gst_structure_new_valist:
222 * @name: name of new structure
223 * @firstfield: name of first field to set
224 * @varargs: variable argument list
226 * Creates a new #GstStructure with the given @name. Structure fields
227 * are set according to the varargs in a manner similar to
228 * gst_structure_new().
230 * See gst_structure_set_name() for constraints on the @name parameter.
232 * Returns: a new #GstStructure
235 gst_structure_new_valist (const gchar * name,
236 const gchar * firstfield, va_list varargs)
238 GstStructure *structure;
240 g_return_val_if_fail (name != NULL, NULL);
242 structure = gst_structure_empty_new (name);
245 gst_structure_set_valist (structure, firstfield, varargs);
251 * gst_structure_set_parent_refcount:
252 * @structure: a #GstStructure
253 * @refcount: a pointer to the parent's refcount
255 * Sets the parent_refcount field of #GstStructure. This field is used to
256 * determine whether a structure is mutable or not. This function should only be
257 * called by code implementing parent objects of #GstStructure, as described in
258 * the MT Refcounting section of the design documents.
261 gst_structure_set_parent_refcount (GstStructure * structure, int *refcount)
263 g_return_if_fail (structure != NULL);
265 /* if we have a parent_refcount already, we can only clear
266 * if with a NULL refcount */
267 if (structure->parent_refcount)
268 g_return_if_fail (refcount == NULL);
270 g_return_if_fail (refcount != NULL);
272 structure->parent_refcount = refcount;
276 * gst_structure_copy:
277 * @structure: a #GstStructure to duplicate
279 * Duplicates a #GstStructure and all its fields and values.
281 * Returns: a new #GstStructure.
284 gst_structure_copy (const GstStructure * structure)
286 GstStructure *new_structure;
287 GstStructureField *field;
290 g_return_val_if_fail (structure != NULL, NULL);
293 gst_structure_id_empty_new_with_size (structure->name,
294 structure->fields->len);
296 for (i = 0; i < structure->fields->len; i++) {
297 GstStructureField new_field = { 0 };
299 field = GST_STRUCTURE_FIELD (structure, i);
301 new_field.name = field->name;
302 gst_value_init_and_copy (&new_field.value, &field->value);
303 g_array_append_val (new_structure->fields, new_field);
306 return new_structure;
310 * gst_structure_free:
311 * @structure: the #GstStructure to free
313 * Frees a #GstStructure and all its fields and values. The structure must not
314 * have a parent when this function is called.
317 gst_structure_free (GstStructure * structure)
319 GstStructureField *field;
322 g_return_if_fail (structure != NULL);
323 g_return_if_fail (structure->parent_refcount == NULL);
325 for (i = 0; i < structure->fields->len; i++) {
326 field = GST_STRUCTURE_FIELD (structure, i);
328 if (G_IS_VALUE (&field->value)) {
329 g_value_unset (&field->value);
332 g_array_free (structure->fields, TRUE);
334 memset (structure, 0xff, sizeof (GstStructure));
336 g_slice_free (GstStructure, structure);
340 * gst_structure_get_name:
341 * @structure: a #GstStructure
343 * Get the name of @structure as a string.
345 * Returns: the name of the structure.
348 gst_structure_get_name (const GstStructure * structure)
350 g_return_val_if_fail (structure != NULL, NULL);
352 return g_quark_to_string (structure->name);
356 * gst_structure_has_name:
357 * @structure: a #GstStructure
358 * @name: structure name to check for
360 * Checks if the structure has the given name
362 * Returns: TRUE if @name matches the name of the structure.
365 gst_structure_has_name (const GstStructure * structure, const gchar * name)
367 const gchar *structure_name;
369 g_return_val_if_fail (structure != NULL, FALSE);
370 g_return_val_if_fail (name != NULL, FALSE);
372 /* getting the string is cheap and comparing short strings is too
373 * should be faster than getting the quark for name and comparing the quarks
375 structure_name = g_quark_to_string (structure->name);
377 return (structure_name && strcmp (structure_name, name) == 0);
381 * gst_structure_get_name_id:
382 * @structure: a #GstStructure
384 * Get the name of @structure as a GQuark.
386 * Returns: the quark representing the name of the structure.
389 gst_structure_get_name_id (const GstStructure * structure)
391 g_return_val_if_fail (structure != NULL, 0);
393 return structure->name;
397 * gst_structure_set_name:
398 * @structure: a #GstStructure
399 * @name: the new name of the structure
401 * Sets the name of the structure to the given @name. The string
402 * provided is copied before being used. It must not be empty, start with a
403 * letter and can be followed by letters, numbers and any of "/-_.:".
406 gst_structure_set_name (GstStructure * structure, const gchar * name)
408 g_return_if_fail (structure != NULL);
409 g_return_if_fail (IS_MUTABLE (structure));
410 g_return_if_fail (gst_structure_validate_name (name));
412 structure->name = g_quark_from_string (name);
416 * gst_structure_id_set_value:
417 * @structure: a #GstStructure
418 * @field: a #GQuark representing a field
419 * @value: the new value of the field
421 * Sets the field with the given GQuark @field to @value. If the field
422 * does not exist, it is created. If the field exists, the previous
423 * value is replaced and freed.
426 gst_structure_id_set_value (GstStructure * structure,
427 GQuark field, const GValue * value)
429 GstStructureField gsfield = { 0, {0,} };
431 g_return_if_fail (structure != NULL);
432 g_return_if_fail (G_IS_VALUE (value));
433 g_return_if_fail (IS_MUTABLE (structure));
435 if (G_VALUE_HOLDS_STRING (value)) {
438 s = g_value_get_string (value);
439 if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
440 g_warning ("Trying to set string field '%s' on structure, but string is "
441 "not valid UTF-8. Please file a bug.", g_quark_to_string (field));
446 gsfield.name = field;
447 gst_value_init_and_copy (&gsfield.value, value);
449 gst_structure_set_field (structure, &gsfield);
453 * gst_structure_set_value:
454 * @structure: a #GstStructure
455 * @fieldname: the name of the field to set
456 * @value: the new value of the field
458 * Sets the field with the given name @field to @value. If the field
459 * does not exist, it is created. If the field exists, the previous
460 * value is replaced and freed.
463 gst_structure_set_value (GstStructure * structure,
464 const gchar * fieldname, const GValue * value)
466 g_return_if_fail (structure != NULL);
467 g_return_if_fail (fieldname != NULL);
468 g_return_if_fail (G_IS_VALUE (value));
469 g_return_if_fail (IS_MUTABLE (structure));
471 gst_structure_id_set_value (structure, g_quark_from_string (fieldname),
477 * @structure: a #GstStructure
478 * @fieldname: the name of the field to set
479 * @...: variable arguments
481 * Parses the variable arguments and sets fields accordingly.
482 * Variable arguments should be in the form field name, field type
483 * (as a GType), value(s). The last variable argument should be NULL.
486 gst_structure_set (GstStructure * structure, const gchar * field, ...)
490 g_return_if_fail (structure != NULL);
492 va_start (varargs, field);
494 gst_structure_set_valist (structure, field, varargs);
500 * gst_structure_set_valist:
501 * @structure: a #GstStructure
502 * @fieldname: the name of the field to set
503 * @varargs: variable arguments
505 * va_list form of gst_structure_set().
508 gst_structure_set_valist (GstStructure * structure,
509 const gchar * fieldname, va_list varargs)
514 g_return_if_fail (structure != NULL);
515 g_return_if_fail (IS_MUTABLE (structure));
518 GstStructureField field = { 0 };
520 field.name = g_quark_from_string (fieldname);
522 type = va_arg (varargs, GType);
524 if (type == G_TYPE_DATE) {
525 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
526 type = GST_TYPE_DATE;
529 g_value_init (&field.value, type);
530 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
532 g_critical ("%s", err);
535 gst_structure_set_field (structure, &field);
537 fieldname = va_arg (varargs, gchar *);
542 * gst_structure_id_set:
543 * @structure: a #GstStructure
544 * @fieldname: the GQuark for the name of the field to set
545 * @...: variable arguments
547 * Identical to gst_structure_set, except that field names are
548 * passed using the GQuark for the field name. This allows more efficient
549 * setting of the structure if the caller already knows the associated
551 * The last variable argument must be NULL.
556 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
560 g_return_if_fail (structure != NULL);
562 va_start (varargs, field);
563 gst_structure_id_set_valist (structure, field, varargs);
568 * gst_structure_id_set_valist:
569 * @structure: a #GstStructure
570 * @fieldname: the name of the field to set
571 * @varargs: variable arguments
573 * va_list form of gst_structure_id_set().
578 gst_structure_id_set_valist (GstStructure * structure,
579 GQuark fieldname, va_list varargs)
584 g_return_if_fail (structure != NULL);
585 g_return_if_fail (IS_MUTABLE (structure));
588 GstStructureField field = { 0 };
590 field.name = fieldname;
592 type = va_arg (varargs, GType);
594 if (type == G_TYPE_DATE) {
595 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
596 type = GST_TYPE_DATE;
599 g_value_init (&field.value, type);
600 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
602 g_critical ("%s", err);
605 gst_structure_set_field (structure, &field);
607 fieldname = va_arg (varargs, GQuark);
611 /* If the structure currently contains a field with the same name, it is
612 * replaced with the provided field. Otherwise, the field is added to the
613 * structure. The field's value is not deeply copied.
616 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
618 GstStructureField *f;
621 for (i = 0; i < structure->fields->len; i++) {
622 f = GST_STRUCTURE_FIELD (structure, i);
624 if (f->name == field->name) {
625 g_value_unset (&f->value);
626 memcpy (f, field, sizeof (GstStructureField));
631 g_array_append_val (structure->fields, *field);
634 /* If there is no field with the given ID, NULL is returned.
636 static GstStructureField *
637 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
639 GstStructureField *field;
642 g_return_val_if_fail (structure != NULL, NULL);
644 for (i = 0; i < structure->fields->len; i++) {
645 field = GST_STRUCTURE_FIELD (structure, i);
647 if (field->name == field_id)
654 /* If there is no field with the given ID, NULL is returned.
656 static GstStructureField *
657 gst_structure_get_field (const GstStructure * structure,
658 const gchar * fieldname)
660 g_return_val_if_fail (structure != NULL, NULL);
661 g_return_val_if_fail (fieldname != NULL, NULL);
663 return gst_structure_id_get_field (structure,
664 g_quark_from_string (fieldname));
668 * gst_structure_get_value:
669 * @structure: a #GstStructure
670 * @fieldname: the name of the field to get
672 * Get the value of the field with name @fieldname.
674 * Returns: the #GValue corresponding to the field with the given name.
677 gst_structure_get_value (const GstStructure * structure,
678 const gchar * fieldname)
680 GstStructureField *field;
682 g_return_val_if_fail (structure != NULL, NULL);
683 g_return_val_if_fail (fieldname != NULL, NULL);
685 field = gst_structure_get_field (structure, fieldname);
689 return &field->value;
693 * gst_structure_id_get_value:
694 * @structure: a #GstStructure
695 * @field: the #GQuark of the field to get
697 * Get the value of the field with GQuark @field.
699 * Returns: the #GValue corresponding to the field with the given name
703 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
705 GstStructureField *gsfield;
707 g_return_val_if_fail (structure != NULL, NULL);
709 gsfield = gst_structure_id_get_field (structure, field);
713 return &gsfield->value;
717 * gst_structure_remove_field:
718 * @structure: a #GstStructure
719 * @fieldname: the name of the field to remove
721 * Removes the field with the given name. If the field with the given
722 * name does not exist, the structure is unchanged.
725 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
727 GstStructureField *field;
731 g_return_if_fail (structure != NULL);
732 g_return_if_fail (fieldname != NULL);
733 g_return_if_fail (IS_MUTABLE (structure));
735 id = g_quark_from_string (fieldname);
737 for (i = 0; i < structure->fields->len; i++) {
738 field = GST_STRUCTURE_FIELD (structure, i);
740 if (field->name == id) {
741 if (G_IS_VALUE (&field->value)) {
742 g_value_unset (&field->value);
744 structure->fields = g_array_remove_index (structure->fields, i);
751 * gst_structure_remove_fields:
752 * @structure: a #GstStructure
753 * @fieldname: the name of the field to remove
754 * @...: NULL-terminated list of more fieldnames to remove
756 * Removes the fields with the given names. If a field does not exist, the
757 * argument is ignored.
760 gst_structure_remove_fields (GstStructure * structure,
761 const gchar * fieldname, ...)
765 g_return_if_fail (structure != NULL);
766 g_return_if_fail (fieldname != NULL);
767 /* mutability checked in remove_field */
769 va_start (varargs, fieldname);
771 gst_structure_remove_fields_valist (structure, fieldname, varargs);
777 * gst_structure_remove_fields_valist:
778 * @structure: a #GstStructure
779 * @fieldname: the name of the field to remove
780 * @varargs: NULL-terminated list of more fieldnames to remove
782 * va_list form of gst_structure_remove_fields().
785 gst_structure_remove_fields_valist (GstStructure * structure,
786 const gchar * fieldname, va_list varargs)
788 gchar *field = (gchar *) fieldname;
790 g_return_if_fail (structure != NULL);
791 g_return_if_fail (fieldname != NULL);
792 /* mutability checked in remove_field */
795 gst_structure_remove_field (structure, field);
796 field = va_arg (varargs, char *);
801 * gst_structure_remove_all_fields:
802 * @structure: a #GstStructure
804 * Removes all fields in a GstStructure.
807 gst_structure_remove_all_fields (GstStructure * structure)
809 GstStructureField *field;
812 g_return_if_fail (structure != NULL);
813 g_return_if_fail (IS_MUTABLE (structure));
815 for (i = structure->fields->len - 1; i >= 0; i--) {
816 field = GST_STRUCTURE_FIELD (structure, i);
818 if (G_IS_VALUE (&field->value)) {
819 g_value_unset (&field->value);
821 structure->fields = g_array_remove_index (structure->fields, i);
826 * gst_structure_get_field_type:
827 * @structure: a #GstStructure
828 * @fieldname: the name of the field
830 * Finds the field with the given name, and returns the type of the
831 * value it contains. If the field is not found, G_TYPE_INVALID is
834 * Returns: the #GValue of the field
837 gst_structure_get_field_type (const GstStructure * structure,
838 const gchar * fieldname)
840 GstStructureField *field;
842 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
843 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
845 field = gst_structure_get_field (structure, fieldname);
847 return G_TYPE_INVALID;
849 return G_VALUE_TYPE (&field->value);
853 * gst_structure_n_fields:
854 * @structure: a #GstStructure
856 * Get the number of fields in the structure.
858 * Returns: the number of fields in the structure
861 gst_structure_n_fields (const GstStructure * structure)
863 g_return_val_if_fail (structure != NULL, 0);
865 return structure->fields->len;
869 * gst_structure_nth_field_name:
870 * @structure: a #GstStructure
871 * @index: the index to get the name of
873 * Get the name of the given field number, counting from 0 onwards.
875 * Returns: the name of the given field number
878 gst_structure_nth_field_name (const GstStructure * structure, guint index)
880 GstStructureField *field;
882 g_return_val_if_fail (structure != NULL, NULL);
883 g_return_val_if_fail (index < structure->fields->len, NULL);
885 field = GST_STRUCTURE_FIELD (structure, index);
887 return g_quark_to_string (field->name);
891 * gst_structure_foreach:
892 * @structure: a #GstStructure
893 * @func: a function to call for each field
894 * @user_data: private data
896 * Calls the provided function once for each field in the #GstStructure. The
897 * function must not modify the fields. Also see gst_structure_map_in_place().
899 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
903 gst_structure_foreach (const GstStructure * structure,
904 GstStructureForeachFunc func, gpointer user_data)
907 GstStructureField *field;
910 g_return_val_if_fail (structure != NULL, FALSE);
911 g_return_val_if_fail (func != NULL, FALSE);
913 for (i = 0; i < structure->fields->len; i++) {
914 field = GST_STRUCTURE_FIELD (structure, i);
916 ret = func (field->name, &field->value, user_data);
925 * gst_structure_map_in_place:
926 * @structure: a #GstStructure
927 * @func: a function to call for each field
928 * @user_data: private data
930 * Calls the provided function once for each field in the #GstStructure. In
931 * contrast to gst_structure_foreach(), the function may modify but not delete the
932 * fields. The structure must be mutable.
934 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
938 gst_structure_map_in_place (GstStructure * structure,
939 GstStructureMapFunc func, gpointer user_data)
942 GstStructureField *field;
945 g_return_val_if_fail (structure != NULL, FALSE);
946 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
947 g_return_val_if_fail (func != NULL, FALSE);
949 for (i = 0; i < structure->fields->len; i++) {
950 field = GST_STRUCTURE_FIELD (structure, i);
952 ret = func (field->name, &field->value, user_data);
961 * gst_structure_has_field:
962 * @structure: a #GstStructure
963 * @fieldname: the name of a field
965 * Check if @structure contains a field named @fieldname.
967 * Returns: TRUE if the structure contains a field with the given name
970 gst_structure_has_field (const GstStructure * structure,
971 const gchar * fieldname)
973 GstStructureField *field;
975 g_return_val_if_fail (structure != NULL, 0);
976 g_return_val_if_fail (fieldname != NULL, 0);
978 field = gst_structure_get_field (structure, fieldname);
980 return (field != NULL);
984 * gst_structure_has_field_typed:
985 * @structure: a #GstStructure
986 * @fieldname: the name of a field
987 * @type: the type of a value
989 * Check if @structure contains a field named @fieldname and with GType @type.
991 * Returns: TRUE if the structure contains a field with the given name and type
994 gst_structure_has_field_typed (const GstStructure * structure,
995 const gchar * fieldname, GType type)
997 GstStructureField *field;
999 g_return_val_if_fail (structure != NULL, 0);
1000 g_return_val_if_fail (fieldname != NULL, 0);
1002 field = gst_structure_get_field (structure, fieldname);
1006 return (G_VALUE_TYPE (&field->value) == type);
1010 /* utility functions */
1013 * gst_structure_get_boolean:
1014 * @structure: a #GstStructure
1015 * @fieldname: the name of a field
1016 * @value: a pointer to a #gboolean to set
1018 * Sets the boolean pointed to by @value corresponding to the value of the
1019 * given field. Caller is responsible for making sure the field exists
1020 * and has the correct type.
1022 * Returns: TRUE if the value could be set correctly. If there was no field
1023 * with @fieldname or the existing field did not contain a boolean, this
1024 * function returns FALSE.
1027 gst_structure_get_boolean (const GstStructure * structure,
1028 const gchar * fieldname, gboolean * value)
1030 GstStructureField *field;
1032 g_return_val_if_fail (structure != NULL, FALSE);
1033 g_return_val_if_fail (fieldname != NULL, FALSE);
1035 field = gst_structure_get_field (structure, fieldname);
1039 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1042 *value = g_value_get_boolean (&field->value);
1048 * gst_structure_get_int:
1049 * @structure: a #GstStructure
1050 * @fieldname: the name of a field
1051 * @value: a pointer to an int to set
1053 * Sets the int pointed to by @value corresponding to the value of the
1054 * given field. Caller is responsible for making sure the field exists
1055 * and has the correct type.
1057 * Returns: %TRUE if the value could be set correctly. If there was no field
1058 * with @fieldname or the existing field did not contain an int, this function
1062 gst_structure_get_int (const GstStructure * structure,
1063 const gchar * fieldname, gint * value)
1065 GstStructureField *field;
1067 g_return_val_if_fail (structure != NULL, FALSE);
1068 g_return_val_if_fail (fieldname != NULL, FALSE);
1069 g_return_val_if_fail (value != NULL, FALSE);
1071 field = gst_structure_get_field (structure, fieldname);
1075 if (!G_VALUE_HOLDS_INT (&field->value))
1078 *value = g_value_get_int (&field->value);
1084 * gst_structure_get_uint:
1085 * @structure: a #GstStructure
1086 * @fieldname: the name of a field
1087 * @value: a pointer to a uint to set
1089 * Sets the uint pointed to by @value corresponding to the value of the
1090 * given field. Caller is responsible for making sure the field exists
1091 * and has the correct type.
1093 * Returns: %TRUE if the value could be set correctly. If there was no field
1094 * with @fieldname or the existing field did not contain a uint, this function
1100 gst_structure_get_uint (const GstStructure * structure,
1101 const gchar * fieldname, guint * value)
1103 GstStructureField *field;
1105 g_return_val_if_fail (structure != NULL, FALSE);
1106 g_return_val_if_fail (fieldname != NULL, FALSE);
1107 g_return_val_if_fail (value != NULL, FALSE);
1109 field = gst_structure_get_field (structure, fieldname);
1113 if (!G_VALUE_HOLDS_UINT (&field->value))
1116 *value = g_value_get_uint (&field->value);
1122 * gst_structure_get_fourcc:
1123 * @structure: a #GstStructure
1124 * @fieldname: the name of a field
1125 * @value: a pointer to a #GstFourcc to set
1127 * Sets the #GstFourcc pointed to by @value corresponding to the value of the
1128 * given field. Caller is responsible for making sure the field exists
1129 * and has the correct type.
1131 * Returns: TRUE if the value could be set correctly. If there was no field
1132 * with @fieldname or the existing field did not contain a fourcc, this function
1136 gst_structure_get_fourcc (const GstStructure * structure,
1137 const gchar * fieldname, guint32 * value)
1139 GstStructureField *field;
1141 g_return_val_if_fail (structure != NULL, FALSE);
1142 g_return_val_if_fail (fieldname != NULL, FALSE);
1143 g_return_val_if_fail (value != NULL, FALSE);
1145 field = gst_structure_get_field (structure, fieldname);
1149 if (!GST_VALUE_HOLDS_FOURCC (&field->value))
1152 *value = gst_value_get_fourcc (&field->value);
1158 * gst_structure_get_date:
1159 * @structure: a #GstStructure
1160 * @fieldname: the name of a field
1161 * @value: a pointer to a #GDate to set
1163 * Sets the date pointed to by @value corresponding to the date of the
1164 * given field. Caller is responsible for making sure the field exists
1165 * and has the correct type.
1167 * Returns: TRUE if the value could be set correctly. If there was no field
1168 * with @fieldname or the existing field did not contain a data, this function
1172 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1175 GstStructureField *field;
1177 g_return_val_if_fail (structure != NULL, FALSE);
1178 g_return_val_if_fail (fieldname != NULL, FALSE);
1179 g_return_val_if_fail (value != NULL, FALSE);
1181 field = gst_structure_get_field (structure, fieldname);
1185 if (!GST_VALUE_HOLDS_DATE (&field->value))
1188 *value = g_value_dup_boxed (&field->value);
1194 * gst_structure_get_clock_time:
1195 * @structure: a #GstStructure
1196 * @fieldname: the name of a field
1197 * @value: a pointer to a #GstClockTime to set
1199 * Sets the clock time pointed to by @value corresponding to the clock time
1200 * of the given field. Caller is responsible for making sure the field exists
1201 * and has the correct type.
1203 * Returns: TRUE if the value could be set correctly. If there was no field
1204 * with @fieldname or the existing field did not contain a #GstClockTime, this
1205 * function returns FALSE.
1208 gst_structure_get_clock_time (const GstStructure * structure,
1209 const gchar * fieldname, GstClockTime * value)
1211 GstStructureField *field;
1213 g_return_val_if_fail (structure != NULL, FALSE);
1214 g_return_val_if_fail (fieldname != NULL, FALSE);
1215 g_return_val_if_fail (value != NULL, FALSE);
1217 field = gst_structure_get_field (structure, fieldname);
1221 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1224 *value = g_value_get_uint64 (&field->value);
1230 * gst_structure_get_double:
1231 * @structure: a #GstStructure
1232 * @fieldname: the name of a field
1233 * @value: a pointer to a #GstFourcc to set
1235 * Sets the double pointed to by @value corresponding to the value of the
1236 * given field. Caller is responsible for making sure the field exists
1237 * and has the correct type.
1239 * Returns: TRUE if the value could be set correctly. If there was no field
1240 * with @fieldname or the existing field did not contain a double, this
1241 * function returns FALSE.
1244 gst_structure_get_double (const GstStructure * structure,
1245 const gchar * fieldname, gdouble * value)
1247 GstStructureField *field;
1249 g_return_val_if_fail (structure != NULL, FALSE);
1250 g_return_val_if_fail (fieldname != NULL, FALSE);
1251 g_return_val_if_fail (value != NULL, FALSE);
1253 field = gst_structure_get_field (structure, fieldname);
1257 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1260 *value = g_value_get_double (&field->value);
1266 * gst_structure_get_string:
1267 * @structure: a #GstStructure
1268 * @fieldname: the name of a field
1270 * Finds the field corresponding to @fieldname, and returns the string
1271 * contained in the field's value. Caller is responsible for making
1272 * sure the field exists and has the correct type.
1274 * The string should not be modified, and remains valid until the next
1275 * call to a gst_structure_*() function with the given structure.
1277 * Returns: a pointer to the string or NULL when the field did not exist
1278 * or did not contain a string.
1281 gst_structure_get_string (const GstStructure * structure,
1282 const gchar * fieldname)
1284 GstStructureField *field;
1286 g_return_val_if_fail (structure != NULL, NULL);
1287 g_return_val_if_fail (fieldname != NULL, NULL);
1289 field = gst_structure_get_field (structure, fieldname);
1293 if (!G_VALUE_HOLDS_STRING (&field->value))
1296 return g_value_get_string (&field->value);
1300 * gst_structure_get_enum:
1301 * @structure: a #GstStructure
1302 * @fieldname: the name of a field
1303 * @enumtype: the enum type of a field
1304 * @value: a pointer to an int to set
1306 * Sets the int pointed to by @value corresponding to the value of the
1307 * given field. Caller is responsible for making sure the field exists,
1308 * has the correct type and that the enumtype is correct.
1310 * Returns: TRUE if the value could be set correctly. If there was no field
1311 * with @fieldname or the existing field did not contain an enum of the given
1312 * type, this function returns FALSE.
1315 gst_structure_get_enum (const GstStructure * structure,
1316 const gchar * fieldname, GType enumtype, gint * value)
1318 GstStructureField *field;
1320 g_return_val_if_fail (structure != NULL, FALSE);
1321 g_return_val_if_fail (fieldname != NULL, FALSE);
1322 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1323 g_return_val_if_fail (value != NULL, FALSE);
1325 field = gst_structure_get_field (structure, fieldname);
1329 if (!G_VALUE_HOLDS_ENUM (&field->value))
1331 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1334 *value = g_value_get_enum (&field->value);
1340 * gst_structure_get_fraction:
1341 * @structure: a #GstStructure
1342 * @fieldname: the name of a field
1343 * @value_numerator: a pointer to an int to set
1344 * @value_denominator: a pointer to an int to set
1346 * Sets the integers pointed to by @value_numerator and @value_denominator
1347 * corresponding to the value of the given field. Caller is responsible
1348 * for making sure the field exists and has the correct type.
1350 * Returns: TRUE if the values could be set correctly. If there was no field
1351 * with @fieldname or the existing field did not contain a GstFraction, this
1352 * function returns FALSE.
1355 gst_structure_get_fraction (const GstStructure * structure,
1356 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1358 GstStructureField *field;
1360 g_return_val_if_fail (structure != NULL, FALSE);
1361 g_return_val_if_fail (fieldname != NULL, FALSE);
1362 g_return_val_if_fail (value_numerator != NULL, FALSE);
1363 g_return_val_if_fail (value_denominator != NULL, FALSE);
1365 field = gst_structure_get_field (structure, fieldname);
1369 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1372 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1373 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1378 typedef struct _GstStructureAbbreviation
1383 GstStructureAbbreviation;
1385 /* return a copy of an array of GstStructureAbbreviation containing all the
1386 * known type_string, GType maps, including abbreviations for common types */
1387 static GstStructureAbbreviation *
1388 gst_structure_get_abbrs (gint * n_abbrs)
1390 static GstStructureAbbreviation *abbrs = NULL;
1391 static gint num = 0;
1393 if (abbrs == NULL) {
1394 /* dynamically generate the array */
1395 GstStructureAbbreviation dyn_abbrs[] = {
1400 {"float", G_TYPE_FLOAT}
1404 {"double", G_TYPE_DOUBLE}
1406 {"d", G_TYPE_DOUBLE}
1408 {"buffer", GST_TYPE_BUFFER}
1410 {"fourcc", GST_TYPE_FOURCC}
1412 {"4", GST_TYPE_FOURCC}
1414 {"fraction", GST_TYPE_FRACTION}
1416 {"boolean", G_TYPE_BOOLEAN}
1418 {"bool", G_TYPE_BOOLEAN}
1420 {"b", G_TYPE_BOOLEAN}
1422 {"string", G_TYPE_STRING}
1424 {"str", G_TYPE_STRING}
1426 {"s", G_TYPE_STRING}
1428 {"structure", GST_TYPE_STRUCTURE}
1430 num = G_N_ELEMENTS (dyn_abbrs);
1431 /* permanently allocate and copy the array now */
1432 abbrs = g_new0 (GstStructureAbbreviation, num);
1433 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * num);
1440 /* given a type_name that could be a type abbreviation or a registered GType,
1441 * return a matching GType */
1443 gst_structure_gtype_from_abbr (const char *type_name)
1446 GstStructureAbbreviation *abbrs;
1449 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1451 abbrs = gst_structure_get_abbrs (&n_abbrs);
1453 for (i = 0; i < n_abbrs; i++) {
1454 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1455 return abbrs[i].type;
1459 /* this is the fallback */
1460 return g_type_from_name (type_name);
1464 gst_structure_to_abbr (GType type)
1467 GstStructureAbbreviation *abbrs;
1470 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1472 abbrs = gst_structure_get_abbrs (&n_abbrs);
1474 for (i = 0; i < n_abbrs; i++) {
1475 if (type == abbrs[i].type) {
1476 return abbrs[i].type_name;
1480 return g_type_name (type);
1484 gst_structure_value_get_generic_type (GValue * val)
1486 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1487 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1488 GArray *array = g_value_peek_pointer (val);
1490 if (array->len > 0) {
1491 GValue *value = &g_array_index (array, GValue, 0);
1493 return gst_structure_value_get_generic_type (value);
1497 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1499 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1500 return G_TYPE_DOUBLE;
1501 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1502 return GST_TYPE_FRACTION;
1504 return G_VALUE_TYPE (val);
1507 /* keep in sync with gstvalue.c */
1508 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1509 ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1513 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1516 GstStructureField *field;
1519 g_return_val_if_fail (s != NULL, FALSE);
1521 g_string_append (s, g_quark_to_string (structure->name));
1522 for (i = 0; i < structure->fields->len; i++) {
1526 field = GST_STRUCTURE_FIELD (structure, i);
1528 t = gst_value_serialize (&field->value);
1529 type = gst_structure_value_get_generic_type (&field->value);
1531 g_string_append_len (s, ", ", 2);
1532 /* FIXME: do we need to escape fieldnames? */
1533 g_string_append (s, g_quark_to_string (field->name));
1534 g_string_append_len (s, "=(", 2);
1535 g_string_append (s, gst_structure_to_abbr (type));
1536 g_string_append_c (s, ')');
1537 g_string_append (s, GST_STR_NULL (t));
1541 g_string_append_c (s, ';');
1546 * gst_structure_to_string:
1547 * @structure: a #GstStructure
1549 * Converts @structure to a human-readable string representation.
1551 * For debugging purposes its easier to do something like this:
1553 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1555 * This prints the structure in human readble form.
1557 * Returns: a pointer to string allocated by g_malloc(). g_free() after
1561 gst_structure_to_string (const GstStructure * structure)
1565 /* NOTE: This function is potentially called by the debug system,
1566 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1567 * should be careful to avoid recursion. This includes any functions
1568 * called by gst_structure_to_string. In particular, calls should
1569 * not use the GST_PTR_FORMAT extension. */
1571 g_return_val_if_fail (structure != NULL, NULL);
1573 /* we estimate a minimum size based on the number of fields in order to
1574 * avoid unnecessary reallocs within GString */
1575 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1576 priv_gst_structure_append_to_gstring (structure, s);
1577 return g_string_free (s, FALSE);
1581 * r will still point to the string. if end == next, the string will not be
1582 * null-terminated. In all other cases it will be.
1583 * end = pointer to char behind end of string, next = pointer to start of
1585 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1588 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next)
1598 ret = gst_structure_parse_simple_string (s, end);
1627 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1630 GValue value1 = { 0 };
1631 GValue value2 = { 0 };
1639 ret = gst_structure_parse_value (s, &s, &value1, type);
1643 while (g_ascii_isspace (*s))
1650 while (g_ascii_isspace (*s))
1653 ret = gst_structure_parse_value (s, &s, &value2, type);
1657 while (g_ascii_isspace (*s))
1664 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1667 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1668 range_type = GST_TYPE_DOUBLE_RANGE;
1669 g_value_init (value, range_type);
1670 gst_value_set_double_range (value, g_value_get_double (&value1),
1671 g_value_get_double (&value2));
1672 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1673 range_type = GST_TYPE_INT_RANGE;
1674 g_value_init (value, range_type);
1675 gst_value_set_int_range (value, g_value_get_int (&value1),
1676 g_value_get_int (&value2));
1677 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1678 range_type = GST_TYPE_FRACTION_RANGE;
1679 g_value_init (value, range_type);
1680 gst_value_set_fraction_range (value, &value1, &value2);
1690 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1691 GType type, GType list_type, char begin, char end)
1693 GValue list_value = { 0 };
1697 g_value_init (value, list_type);
1698 array = g_value_peek_pointer (value);
1704 while (g_ascii_isspace (*s))
1712 ret = gst_structure_parse_value (s, &s, &list_value, type);
1716 g_array_append_val (array, list_value);
1718 while (g_ascii_isspace (*s))
1726 while (g_ascii_isspace (*s))
1729 memset (&list_value, 0, sizeof (list_value));
1730 ret = gst_structure_parse_value (s, &s, &list_value, type);
1734 g_array_append_val (array, list_value);
1735 while (g_ascii_isspace (*s))
1746 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
1748 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
1753 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
1756 return gst_structure_parse_any_list (s, after, value, type,
1757 GST_TYPE_ARRAY, '<', '>');
1761 gst_structure_parse_simple_string (gchar * str, gchar ** end)
1765 while (GST_ASCII_IS_STRING (*s)) {
1775 gst_structure_parse_field (gchar * str,
1776 gchar ** after, GstStructureField * field)
1785 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1788 if (!gst_structure_parse_simple_string (s, &name_end))
1792 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1801 field->name = g_quark_from_string (name);
1804 if (!gst_structure_parse_value (s, &s, &field->value, G_TYPE_INVALID))
1812 gst_structure_parse_value (gchar * str,
1813 gchar ** after, GValue * value, GType default_type)
1822 GType type = default_type;
1826 while (g_ascii_isspace (*s))
1829 /* check if there's a (type_name) 'cast' */
1833 while (g_ascii_isspace (*s))
1836 if (!gst_structure_parse_simple_string (s, &type_end))
1839 while (g_ascii_isspace (*s))
1844 while (g_ascii_isspace (*s))
1849 type = gst_structure_gtype_from_abbr (type_name);
1852 if (type == G_TYPE_INVALID)
1856 while (g_ascii_isspace (*s))
1859 ret = gst_structure_parse_range (s, &s, value, type);
1860 } else if (*s == '{') {
1861 ret = gst_structure_parse_list (s, &s, value, type);
1862 } else if (*s == '<') {
1863 ret = gst_structure_parse_array (s, &s, value, type);
1866 if (!gst_structure_parse_string (s, &value_end, &s))
1871 if (type == G_TYPE_INVALID) {
1873 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
1878 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
1879 g_value_init (value, try_types[i]);
1880 ret = gst_value_deserialize (value, value_s);
1883 g_value_unset (value);
1886 g_value_init (value, type);
1888 ret = gst_value_deserialize (value, value_s);
1899 * gst_structure_from_string:
1900 * @string: a string representation of a #GstStructure.
1901 * @end: pointer to store the end of the string in.
1903 * Creates a #GstStructure from a string representation.
1904 * If end is not NULL, a pointer to the place inside the given string
1905 * where parsing ended will be returned.
1907 * Returns: a new #GstStructure or NULL when the string could not
1908 * be parsed. Free with gst_structure_free() after use.
1911 gst_structure_from_string (const gchar * string, gchar ** end)
1918 GstStructure *structure = NULL;
1919 GstStructureField field = { 0 };
1921 g_return_val_if_fail (string != NULL, NULL);
1923 copy = g_strdup (string);
1926 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
1927 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1928 && g_ascii_isspace (r[1]))))
1932 if (!gst_structure_parse_string (r, &w, &r)) {
1933 GST_WARNING ("Failed to parse structure string");
1939 structure = gst_structure_empty_new (name);
1942 if (structure == NULL)
1946 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1947 && g_ascii_isspace (r[1]))))
1950 /* end of structure, get the next char and finish */
1955 /* accept \0 as end delimiter */
1959 GST_WARNING ("Failed to find delimiter, r=%s", r);
1963 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1964 && g_ascii_isspace (r[1]))))
1967 memset (&field, 0, sizeof (field));
1968 if (!gst_structure_parse_field (r, &r, &field))
1970 gst_structure_set_field (structure, &field);
1975 *end = (char *) string + (r - copy);
1977 g_warning ("gst_structure_from_string did not consume whole string,"
1978 " but caller did not provide end pointer (\"%s\")", string);
1985 gst_structure_free (structure);
1991 gst_structure_transform_to_string (const GValue * src_value,
1992 GValue * dest_value)
1994 g_return_if_fail (src_value != NULL);
1995 g_return_if_fail (dest_value != NULL);
1997 dest_value->data[0].v_pointer =
1998 gst_structure_to_string (src_value->data[0].v_pointer);
2001 static GstStructure *
2002 gst_structure_copy_conditional (const GstStructure * structure)
2005 return gst_structure_copy (structure);
2009 /* fixate utility functions */
2012 * gst_structure_fixate_field_nearest_int:
2013 * @structure: a #GstStructure
2014 * @field_name: a field in @structure
2015 * @target: the target value of the fixation
2017 * Fixates a #GstStructure by changing the given field to the nearest
2018 * integer to @target that is a subset of the existing field.
2020 * Returns: TRUE if the structure could be fixated
2023 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2024 const char *field_name, int target)
2026 const GValue *value;
2028 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2029 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2031 value = gst_structure_get_value (structure, field_name);
2033 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2036 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2039 x = gst_value_get_int_range_min (value);
2042 x = gst_value_get_int_range_max (value);
2045 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2047 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2048 const GValue *list_value;
2051 int best_index = -1;
2053 n = gst_value_list_get_size (value);
2054 for (i = 0; i < n; i++) {
2055 list_value = gst_value_list_get_value (value, i);
2056 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2057 int x = g_value_get_int (list_value);
2059 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2065 if (best_index != -1) {
2066 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2076 * gst_structure_fixate_field_nearest_double:
2077 * @structure: a #GstStructure
2078 * @field_name: a field in @structure
2079 * @target: the target value of the fixation
2081 * Fixates a #GstStructure by changing the given field to the nearest
2082 * double to @target that is a subset of the existing field.
2084 * Returns: TRUE if the structure could be fixated
2087 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2088 const char *field_name, double target)
2090 const GValue *value;
2092 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2093 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2095 value = gst_structure_get_value (structure, field_name);
2097 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2100 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2103 x = gst_value_get_double_range_min (value);
2106 x = gst_value_get_double_range_max (value);
2109 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2111 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2112 const GValue *list_value;
2115 int best_index = -1;
2117 n = gst_value_list_get_size (value);
2118 for (i = 0; i < n; i++) {
2119 list_value = gst_value_list_get_value (value, i);
2120 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2121 double x = g_value_get_double (list_value);
2123 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2129 if (best_index != -1) {
2130 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2141 * gst_structure_fixate_field_boolean:
2142 * @structure: a #GstStructure
2143 * @field_name: a field in @structure
2144 * @target: the target value of the fixation
2146 * Fixates a #GstStructure by changing the given @field_name field to the given
2147 * @target boolean if that field is not fixed yet.
2149 * Returns: TRUE if the structure could be fixated
2152 gst_structure_fixate_field_boolean (GstStructure * structure,
2153 const char *field_name, gboolean target)
2155 const GValue *value;
2157 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2158 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2160 value = gst_structure_get_value (structure, field_name);
2162 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2165 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2166 const GValue *list_value;
2169 int best_index = -1;
2171 n = gst_value_list_get_size (value);
2172 for (i = 0; i < n; i++) {
2173 list_value = gst_value_list_get_value (value, i);
2174 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2175 gboolean x = g_value_get_boolean (list_value);
2177 if (best_index == -1 || x == target) {
2183 if (best_index != -1) {
2184 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2194 * gst_structure_fixate_field_nearest_fraction:
2195 * @structure: a #GstStructure
2196 * @field_name: a field in @structure
2197 * @target_numerator: The numerator of the target value of the fixation
2198 * @target_denominator: The denominator of the target value of the fixation
2200 * Fixates a #GstStructure by changing the given field to the nearest
2201 * fraction to @target_numerator/@target_denominator that is a subset
2202 * of the existing field.
2204 * Returns: TRUE if the structure could be fixated
2207 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2208 const char *field_name, const gint target_numerator,
2209 const gint target_denominator)
2211 const GValue *value;
2213 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2214 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2216 value = gst_structure_get_value (structure, field_name);
2218 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2221 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2222 const GValue *x, *new_value;
2223 GValue target = { 0 };
2224 g_value_init (&target, GST_TYPE_FRACTION);
2225 gst_value_set_fraction (&target, target_numerator, target_denominator);
2227 new_value = ⌖
2228 x = gst_value_get_fraction_range_min (value);
2229 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2231 x = gst_value_get_fraction_range_max (value);
2232 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2235 gst_structure_set_value (structure, field_name, new_value);
2236 g_value_unset (&target);
2238 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2239 const GValue *list_value;
2241 const GValue *best = NULL;
2244 gdouble best_diff = G_MAXDOUBLE;
2246 target = (gdouble) target_numerator / (gdouble) target_denominator;
2248 GST_DEBUG ("target %g, best %g", target, best_diff);
2252 n = gst_value_list_get_size (value);
2253 for (i = 0; i < n; i++) {
2254 list_value = gst_value_list_get_value (value, i);
2255 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2257 gdouble list_double;
2259 num = gst_value_get_fraction_numerator (list_value);
2260 denom = gst_value_get_fraction_denominator (list_value);
2262 list_double = ((gdouble) num / (gdouble) denom);
2263 cur_diff = target - list_double;
2265 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2268 cur_diff = -cur_diff;
2270 if (!best || cur_diff < best_diff) {
2271 GST_DEBUG ("new best %g", list_double);
2273 best_diff = cur_diff;
2278 gst_structure_set_value (structure, field_name, best);