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.
32 * #GstStructure is used by various GStreamer subsystems to store information
33 * in a flexible and extensible way. A #GstStructure does not have a refcount
34 * because it usually is part of a higher level object such as #GstCaps. It
35 * provides a means to enforce mutability using the refcount of the parent
36 * with the gst_structure_set_parent_refcount() method.
38 * A #GstStructure can be created with gst_structure_empty_new() or
39 * gst_structure_new(), which both take a name and an optional set of
40 * key/value pairs along with the types of the values.
42 * Field values can be changed with gst_structure_set_value() or
43 * gst_structure_set().
45 * Field values can be retrieved with gst_structure_get_value() or the more
46 * convenient gst_structure_get_*() functions.
48 * Fields can be removed with gst_structure_remove_field() or
49 * gst_structure_remove_fields().
51 * Last reviewed on 2005-11-09 (0.9.4)
60 #include "gst_private.h"
62 #include <gobject/gvaluecollector.h>
64 typedef struct _GstStructureField GstStructureField;
66 struct _GstStructureField
72 #define GST_STRUCTURE_FIELD(structure, index) \
73 &g_array_index((structure)->fields, GstStructureField, (index))
75 #define IS_MUTABLE(structure) \
76 (!(structure)->parent_refcount || \
77 g_atomic_int_get ((structure)->parent_refcount) == 1)
79 static void gst_structure_set_field (GstStructure * structure,
80 GstStructureField * field);
81 static GstStructureField *gst_structure_get_field (const GstStructure *
82 structure, const gchar * fieldname);
83 static GstStructureField *gst_structure_id_get_field (const GstStructure *
84 structure, GQuark field);
85 static void gst_structure_transform_to_string (const GValue * src_value,
87 static GstStructure *gst_structure_copy_conditional (const GstStructure *
89 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
90 GValue * value, GType default_type);
91 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
94 gst_structure_get_type (void)
96 static GType gst_structure_type = 0;
98 if (G_UNLIKELY (gst_structure_type == 0)) {
99 gst_structure_type = g_boxed_type_register_static ("GstStructure",
100 (GBoxedCopyFunc) gst_structure_copy_conditional,
101 (GBoxedFreeFunc) gst_structure_free);
103 g_value_register_transform_func (gst_structure_type, G_TYPE_STRING,
104 gst_structure_transform_to_string);
107 return gst_structure_type;
110 static GstStructure *
111 gst_structure_id_empty_new_with_size (GQuark quark, guint prealloc)
113 GstStructure *structure;
115 structure = g_new0 (GstStructure, 1);
116 structure->type = gst_structure_get_type ();
117 structure->name = quark;
119 g_array_sized_new (FALSE, TRUE, sizeof (GstStructureField), prealloc);
125 * gst_structure_id_empty_new:
126 * @quark: name of new structure
128 * Creates a new, empty #GstStructure with the given name as a GQuark.
130 * Returns: a new, empty #GstStructure
133 gst_structure_id_empty_new (GQuark quark)
135 g_return_val_if_fail (quark != 0, NULL);
137 return gst_structure_id_empty_new_with_size (quark, 0);
141 * gst_structure_empty_new:
142 * @name: name of new structure
144 * Creates a new, empty #GstStructure with the given name.
146 * Returns: a new, empty #GstStructure
149 gst_structure_empty_new (const gchar * name)
151 g_return_val_if_fail (name != NULL, NULL);
153 return gst_structure_id_empty_new_with_size (g_quark_from_string (name), 0);
158 * @name: name of new structure
159 * @firstfield: name of first field to set
160 * @...: additional arguments
162 * Creates a new #GstStructure with the given name. Parses the
163 * list of variable arguments and sets fields to the values listed.
164 * Variable arguments should be passed as field name, field type,
165 * and value. Last variable argument should be NULL.
167 * Returns: a new #GstStructure
170 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
172 GstStructure *structure;
175 g_return_val_if_fail (name != NULL, NULL);
177 va_start (varargs, firstfield);
179 structure = gst_structure_new_valist (name, firstfield, varargs);
187 * gst_structure_new_valist:
188 * @name: name of new structure
189 * @firstfield: name of first field to set
190 * @varargs: variable argument list
192 * Creates a new #GstStructure with the given name. Structure fields
193 * are set according to the varargs in a manner similar to
194 * @gst_structure_new.
196 * Returns: a new #GstStructure
199 gst_structure_new_valist (const gchar * name,
200 const gchar * firstfield, va_list varargs)
202 GstStructure *structure;
204 g_return_val_if_fail (name != NULL, NULL);
206 structure = gst_structure_empty_new (name);
207 gst_structure_set_valist (structure, firstfield, varargs);
213 * gst_structure_set_parent_refcount:
214 * @structure: a #GstStructure
215 * @refcount: a pointer to the parent's refcount
217 * Sets the parent_refcount field of #GstStructure. This field is used to
218 * determine whether a structure is mutable or not. This function should only be
219 * called by code implementing parent objects of #GstStructure, as described in
220 * the MT Refcounting section of the design documents.
223 gst_structure_set_parent_refcount (GstStructure * structure, int *refcount)
225 g_return_if_fail (structure != NULL);
227 /* if we have a parent_refcount already, we can only clear
228 * if with a NULL refcount */
229 if (structure->parent_refcount)
230 g_return_if_fail (refcount == NULL);
232 g_return_if_fail (refcount != NULL);
234 structure->parent_refcount = refcount;
238 * gst_structure_copy:
239 * @structure: a #GstStructure to duplicate
241 * Duplicates a #GstStructure and all its fields and values.
243 * Returns: a new #GstStructure.
246 gst_structure_copy (const GstStructure * structure)
248 GstStructure *new_structure;
249 GstStructureField *field;
252 g_return_val_if_fail (structure != NULL, NULL);
255 gst_structure_id_empty_new_with_size (structure->name,
256 structure->fields->len);
258 for (i = 0; i < structure->fields->len; i++) {
259 GstStructureField new_field = { 0 };
261 field = GST_STRUCTURE_FIELD (structure, i);
263 new_field.name = field->name;
264 gst_value_init_and_copy (&new_field.value, &field->value);
265 g_array_append_val (new_structure->fields, new_field);
268 return new_structure;
272 * gst_structure_free:
273 * @structure: the #GstStructure to free
275 * Frees a #GstStructure and all its fields and values. The structure must not
276 * have a parent when this function is called.
279 gst_structure_free (GstStructure * structure)
281 GstStructureField *field;
284 g_return_if_fail (structure != NULL);
285 g_return_if_fail (structure->parent_refcount == NULL);
287 for (i = 0; i < structure->fields->len; i++) {
288 field = GST_STRUCTURE_FIELD (structure, i);
290 if (G_IS_VALUE (&field->value)) {
291 g_value_unset (&field->value);
294 g_array_free (structure->fields, TRUE);
296 memset (structure, 0xff, sizeof (GstStructure));
302 * gst_structure_get_name:
303 * @structure: a #GstStructure
305 * Get the name of @structure as a string.
307 * Returns: the name of the structure.
310 gst_structure_get_name (const GstStructure * structure)
312 g_return_val_if_fail (structure != NULL, NULL);
314 return g_quark_to_string (structure->name);
318 * gst_structure_has_name:
319 * @structure: a #GstStructure
320 * @name: structure name to check for
322 * Checks if the structure has the given name
324 * Returns: TRUE if @name matches the name of the structure.
327 gst_structure_has_name (const GstStructure * structure, const gchar * name)
329 const gchar *structure_name;
331 g_return_val_if_fail (structure != NULL, FALSE);
332 g_return_val_if_fail (name != NULL, FALSE);
334 structure_name = g_quark_to_string (structure->name);
336 return (structure_name && strcmp (structure_name, name) == 0);
340 * gst_structure_get_name_id:
341 * @structure: a #GstStructure
343 * Get the name of @structure as a GQuark.
345 * Returns: the quark representing the name of the structure.
348 gst_structure_get_name_id (const GstStructure * structure)
350 g_return_val_if_fail (structure != NULL, 0);
352 return structure->name;
356 * gst_structure_set_name:
357 * @structure: a #GstStructure
358 * @name: the new name of the structure
360 * Sets the name of the structure to the given name. The string
361 * provided is copied before being used.
364 gst_structure_set_name (GstStructure * structure, const gchar * name)
366 g_return_if_fail (structure != NULL);
367 g_return_if_fail (name != NULL);
368 g_return_if_fail (IS_MUTABLE (structure));
370 structure->name = g_quark_from_string (name);
374 * gst_structure_id_set_value:
375 * @structure: a #GstStructure
376 * @field: a #GQuark representing a field
377 * @value: the new value of the field
379 * Sets the field with the given GQuark @field to @value. If the field
380 * does not exist, it is created. If the field exists, the previous
381 * value is replaced and freed.
384 gst_structure_id_set_value (GstStructure * structure,
385 GQuark field, const GValue * value)
387 GstStructureField gsfield = { 0, {0,} };
389 g_return_if_fail (structure != NULL);
390 g_return_if_fail (G_IS_VALUE (value));
391 g_return_if_fail (IS_MUTABLE (structure));
393 /* if someones disables GST_DEBUG output, they probably do it for
394 * performance reasons, so skip the UTF-8 check here as well then */
395 #ifndef GST_DISABLE_GST_DEBUG
396 if (G_VALUE_HOLDS_STRING (value)) {
399 s = g_value_get_string (value);
400 if (s != NULL && !g_utf8_validate (s, -1, NULL)) {
401 g_warning ("Trying to set string field '%s' on structure, but string is "
402 "not valid UTF-8. Please file a bug.", g_quark_to_string (field));
407 gsfield.name = field;
408 gst_value_init_and_copy (&gsfield.value, value);
410 gst_structure_set_field (structure, &gsfield);
414 * gst_structure_set_value:
415 * @structure: a #GstStructure
416 * @fieldname: the name of the field to set
417 * @value: the new value of the field
419 * Sets the field with the given name @field to @value. If the field
420 * does not exist, it is created. If the field exists, the previous
421 * value is replaced and freed.
424 gst_structure_set_value (GstStructure * structure,
425 const gchar * fieldname, const GValue * value)
427 g_return_if_fail (structure != NULL);
428 g_return_if_fail (fieldname != NULL);
429 g_return_if_fail (G_IS_VALUE (value));
430 g_return_if_fail (IS_MUTABLE (structure));
432 gst_structure_id_set_value (structure, g_quark_from_string (fieldname),
438 * @structure: a #GstStructure
439 * @fieldname: the name of the field to set
440 * @...: variable arguments
442 * Parses the variable arguments and sets fields accordingly.
443 * Variable arguments should be in the form field name, field type
444 * (as a GType), value(s). The last variable argument should be NULL.
447 gst_structure_set (GstStructure * structure, const gchar * field, ...)
451 g_return_if_fail (structure != NULL);
453 va_start (varargs, field);
455 gst_structure_set_valist (structure, field, varargs);
461 * gst_structure_set_valist:
462 * @structure: a #GstStructure
463 * @fieldname: the name of the field to set
464 * @varargs: variable arguments
466 * va_list form of gst_structure_set().
469 gst_structure_set_valist (GstStructure * structure,
470 const gchar * fieldname, va_list varargs)
475 g_return_if_fail (structure != NULL);
476 g_return_if_fail (IS_MUTABLE (structure));
479 GstStructureField field = { 0 };
481 field.name = g_quark_from_string (fieldname);
483 type = va_arg (varargs, GType);
485 if (type == G_TYPE_DATE) {
486 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
487 type = GST_TYPE_DATE;
490 g_value_init (&field.value, type);
491 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
493 g_critical ("%s", err);
496 gst_structure_set_field (structure, &field);
498 fieldname = va_arg (varargs, gchar *);
503 * gst_structure_id_set:
504 * @structure: a #GstStructure
505 * @fieldname: the GQuark for the name of the field to set
506 * @...: variable arguments
508 * Identical to gst_structure_set, except that field names are
509 * passed using the GQuark for the field name. This allows more efficient
510 * setting of the structure if the caller already knows the associated
512 * The last variable argument must be NULL.
517 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
521 g_return_if_fail (structure != NULL);
523 va_start (varargs, field);
524 gst_structure_id_set_valist (structure, field, varargs);
529 * gst_structure_id_set_valist:
530 * @structure: a #GstStructure
531 * @fieldname: the name of the field to set
532 * @varargs: variable arguments
534 * va_list form of gst_structure_id_set().
539 gst_structure_id_set_valist (GstStructure * structure,
540 GQuark fieldname, va_list varargs)
545 g_return_if_fail (structure != NULL);
546 g_return_if_fail (IS_MUTABLE (structure));
549 GstStructureField field = { 0 };
551 field.name = fieldname;
553 type = va_arg (varargs, GType);
555 if (type == G_TYPE_DATE) {
556 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
557 type = GST_TYPE_DATE;
560 g_value_init (&field.value, type);
561 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
563 g_critical ("%s", err);
566 gst_structure_set_field (structure, &field);
568 fieldname = va_arg (varargs, GQuark);
572 /* If the structure currently contains a field with the same name, it is
573 * replaced with the provided field. Otherwise, the field is added to the
574 * structure. The field's value is not deeply copied.
577 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
579 GstStructureField *f;
582 for (i = 0; i < structure->fields->len; i++) {
583 f = GST_STRUCTURE_FIELD (structure, i);
585 if (f->name == field->name) {
586 g_value_unset (&f->value);
587 memcpy (f, field, sizeof (GstStructureField));
592 g_array_append_val (structure->fields, *field);
595 /* If there is no field with the given ID, NULL is returned.
597 static GstStructureField *
598 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
600 GstStructureField *field;
603 g_return_val_if_fail (structure != NULL, NULL);
605 for (i = 0; i < structure->fields->len; i++) {
606 field = GST_STRUCTURE_FIELD (structure, i);
608 if (field->name == field_id)
615 /* If there is no field with the given ID, NULL is returned.
617 static GstStructureField *
618 gst_structure_get_field (const GstStructure * structure,
619 const gchar * fieldname)
621 g_return_val_if_fail (structure != NULL, NULL);
622 g_return_val_if_fail (fieldname != NULL, NULL);
624 return gst_structure_id_get_field (structure,
625 g_quark_from_string (fieldname));
629 * gst_structure_get_value:
630 * @structure: a #GstStructure
631 * @fieldname: the name of the field to get
633 * Get the value of the field with name @fieldname.
635 * Returns: the #GValue corresponding to the field with the given name.
638 gst_structure_get_value (const GstStructure * structure,
639 const gchar * fieldname)
641 GstStructureField *field;
643 g_return_val_if_fail (structure != NULL, NULL);
644 g_return_val_if_fail (fieldname != NULL, NULL);
646 field = gst_structure_get_field (structure, fieldname);
650 return &field->value;
654 * gst_structure_id_get_value:
655 * @structure: a #GstStructure
656 * @field: the #GQuark of the field to get
658 * Get the value of the field with GQuark @field.
660 * Returns: the #GValue corresponding to the field with the given name
664 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
666 GstStructureField *gsfield;
668 g_return_val_if_fail (structure != NULL, NULL);
670 gsfield = gst_structure_id_get_field (structure, field);
674 return &gsfield->value;
678 * gst_structure_remove_field:
679 * @structure: a #GstStructure
680 * @fieldname: the name of the field to remove
682 * Removes the field with the given name. If the field with the given
683 * name does not exist, the structure is unchanged.
686 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
688 GstStructureField *field;
692 g_return_if_fail (structure != NULL);
693 g_return_if_fail (fieldname != NULL);
694 g_return_if_fail (IS_MUTABLE (structure));
696 id = g_quark_from_string (fieldname);
698 for (i = 0; i < structure->fields->len; i++) {
699 field = GST_STRUCTURE_FIELD (structure, i);
701 if (field->name == id) {
702 if (G_IS_VALUE (&field->value)) {
703 g_value_unset (&field->value);
705 structure->fields = g_array_remove_index (structure->fields, i);
712 * gst_structure_remove_fields:
713 * @structure: a #GstStructure
714 * @fieldname: the name of the field to remove
715 * @...: NULL-terminated list of more fieldnames to remove
717 * Removes the fields with the given names. If a field does not exist, the
718 * argument is ignored.
721 gst_structure_remove_fields (GstStructure * structure,
722 const gchar * fieldname, ...)
726 g_return_if_fail (structure != NULL);
727 g_return_if_fail (fieldname != NULL);
728 /* mutability checked in remove_field */
730 va_start (varargs, fieldname);
732 gst_structure_remove_fields_valist (structure, fieldname, varargs);
738 * gst_structure_remove_fields_valist:
739 * @structure: a #GstStructure
740 * @fieldname: the name of the field to remove
741 * @varargs: NULL-terminated list of more fieldnames to remove
743 * va_list form of gst_structure_remove_fields().
746 gst_structure_remove_fields_valist (GstStructure * structure,
747 const gchar * fieldname, va_list varargs)
749 gchar *field = (gchar *) fieldname;
751 g_return_if_fail (structure != NULL);
752 g_return_if_fail (fieldname != NULL);
753 /* mutability checked in remove_field */
756 gst_structure_remove_field (structure, field);
757 field = va_arg (varargs, char *);
762 * gst_structure_remove_all_fields:
763 * @structure: a #GstStructure
765 * Removes all fields in a GstStructure.
768 gst_structure_remove_all_fields (GstStructure * structure)
770 GstStructureField *field;
773 g_return_if_fail (structure != NULL);
774 g_return_if_fail (IS_MUTABLE (structure));
776 for (i = structure->fields->len - 1; i >= 0; i--) {
777 field = GST_STRUCTURE_FIELD (structure, i);
779 if (G_IS_VALUE (&field->value)) {
780 g_value_unset (&field->value);
782 structure->fields = g_array_remove_index (structure->fields, i);
787 * gst_structure_get_field_type:
788 * @structure: a #GstStructure
789 * @fieldname: the name of the field
791 * Finds the field with the given name, and returns the type of the
792 * value it contains. If the field is not found, G_TYPE_INVALID is
795 * Returns: the #GValue of the field
798 gst_structure_get_field_type (const GstStructure * structure,
799 const gchar * fieldname)
801 GstStructureField *field;
803 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
804 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
806 field = gst_structure_get_field (structure, fieldname);
808 return G_TYPE_INVALID;
810 return G_VALUE_TYPE (&field->value);
814 * gst_structure_n_fields:
815 * @structure: a #GstStructure
817 * Get the number of fields in the structure.
819 * Returns: the number of fields in the structure
822 gst_structure_n_fields (const GstStructure * structure)
824 g_return_val_if_fail (structure != NULL, 0);
826 return structure->fields->len;
830 * gst_structure_nth_field_name:
831 * @structure: a #GstStructure
832 * @index: the index to get the name of
834 * Get the name of the given field number, counting from 0 onwards.
836 * Returns: the name of the given field number
839 gst_structure_nth_field_name (const GstStructure * structure, guint index)
841 GstStructureField *field;
843 g_return_val_if_fail (structure != NULL, NULL);
844 g_return_val_if_fail (index < structure->fields->len, NULL);
846 field = GST_STRUCTURE_FIELD (structure, index);
848 return g_quark_to_string (field->name);
852 * gst_structure_foreach:
853 * @structure: a #GstStructure
854 * @func: a function to call for each field
855 * @user_data: private data
857 * Calls the provided function once for each field in the #GstStructure. The
858 * function must not modify the fields. Also see gst_structure_map_in_place().
860 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
864 gst_structure_foreach (const GstStructure * structure,
865 GstStructureForeachFunc func, gpointer user_data)
868 GstStructureField *field;
871 g_return_val_if_fail (structure != NULL, FALSE);
872 g_return_val_if_fail (func != NULL, FALSE);
874 for (i = 0; i < structure->fields->len; i++) {
875 field = GST_STRUCTURE_FIELD (structure, i);
877 ret = func (field->name, &field->value, user_data);
886 * gst_structure_map_in_place:
887 * @structure: a #GstStructure
888 * @func: a function to call for each field
889 * @user_data: private data
891 * Calls the provided function once for each field in the #GstStructure. In
892 * contrast to gst_structure_foreach(), the function may modify the fields. The
893 * structure must be mutable.
895 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
899 gst_structure_map_in_place (GstStructure * structure,
900 GstStructureMapFunc func, gpointer user_data)
903 GstStructureField *field;
906 g_return_val_if_fail (structure != NULL, FALSE);
907 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
908 g_return_val_if_fail (func != NULL, FALSE);
910 for (i = 0; i < structure->fields->len; i++) {
911 field = GST_STRUCTURE_FIELD (structure, i);
913 ret = func (field->name, &field->value, user_data);
922 * gst_structure_has_field:
923 * @structure: a #GstStructure
924 * @fieldname: the name of a field
926 * Check if @structure contains a field named @fieldname.
928 * Returns: TRUE if the structure contains a field with the given name
931 gst_structure_has_field (const GstStructure * structure,
932 const gchar * fieldname)
934 GstStructureField *field;
936 g_return_val_if_fail (structure != NULL, 0);
937 g_return_val_if_fail (fieldname != NULL, 0);
939 field = gst_structure_get_field (structure, fieldname);
941 return (field != NULL);
945 * gst_structure_has_field_typed:
946 * @structure: a #GstStructure
947 * @fieldname: the name of a field
948 * @type: the type of a value
950 * Check if @structure contains a field named @fieldname and with GType @type.
952 * Returns: TRUE if the structure contains a field with the given name and type
955 gst_structure_has_field_typed (const GstStructure * structure,
956 const gchar * fieldname, GType type)
958 GstStructureField *field;
960 g_return_val_if_fail (structure != NULL, 0);
961 g_return_val_if_fail (fieldname != NULL, 0);
963 field = gst_structure_get_field (structure, fieldname);
967 return (G_VALUE_TYPE (&field->value) == type);
971 /* utility functions */
974 * gst_structure_get_boolean:
975 * @structure: a #GstStructure
976 * @fieldname: the name of a field
977 * @value: a pointer to a #gboolean to set
979 * Sets the boolean pointed to by @value corresponding to the value of the
980 * given field. Caller is responsible for making sure the field exists
981 * and has the correct type.
983 * Returns: TRUE if the value could be set correctly. If there was no field
984 * with @fieldname or the existing field did not contain a boolean, this
985 * function returns FALSE.
988 gst_structure_get_boolean (const GstStructure * structure,
989 const gchar * fieldname, gboolean * value)
991 GstStructureField *field;
993 g_return_val_if_fail (structure != NULL, FALSE);
994 g_return_val_if_fail (fieldname != NULL, FALSE);
996 field = gst_structure_get_field (structure, fieldname);
1000 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1003 *value = g_value_get_boolean (&field->value);
1009 * gst_structure_get_int:
1010 * @structure: a #GstStructure
1011 * @fieldname: the name of a field
1012 * @value: a pointer to an int to set
1014 * Sets the int pointed to by @value corresponding to the value of the
1015 * given field. Caller is responsible for making sure the field exists
1016 * and has the correct type.
1018 * Returns: TRUE if the value could be set correctly. If there was no field
1019 * with @fieldname or the existing field did not contain an int, this function
1023 gst_structure_get_int (const GstStructure * structure,
1024 const gchar * fieldname, gint * value)
1026 GstStructureField *field;
1028 g_return_val_if_fail (structure != NULL, FALSE);
1029 g_return_val_if_fail (fieldname != NULL, FALSE);
1030 g_return_val_if_fail (value != NULL, FALSE);
1032 field = gst_structure_get_field (structure, fieldname);
1036 if (!G_VALUE_HOLDS_INT (&field->value))
1039 *value = g_value_get_int (&field->value);
1045 * gst_structure_get_fourcc:
1046 * @structure: a #GstStructure
1047 * @fieldname: the name of a field
1048 * @value: a pointer to a #GstFourcc to set
1050 * Sets the #GstFourcc pointed to by @value corresponding to the value of the
1051 * given field. Caller is responsible for making sure the field exists
1052 * and has the correct type.
1054 * Returns: TRUE if the value could be set correctly. If there was no field
1055 * with @fieldname or the existing field did not contain a fourcc, this function
1059 gst_structure_get_fourcc (const GstStructure * structure,
1060 const gchar * fieldname, guint32 * value)
1062 GstStructureField *field;
1064 g_return_val_if_fail (structure != NULL, FALSE);
1065 g_return_val_if_fail (fieldname != NULL, FALSE);
1066 g_return_val_if_fail (value != NULL, FALSE);
1068 field = gst_structure_get_field (structure, fieldname);
1072 if (!GST_VALUE_HOLDS_FOURCC (&field->value))
1075 *value = gst_value_get_fourcc (&field->value);
1081 * gst_structure_get_date:
1082 * @structure: a #GstStructure
1083 * @fieldname: the name of a field
1084 * @value: a pointer to a #GDate to set
1086 * Sets the date pointed to by @value corresponding to the date of the
1087 * given field. Caller is responsible for making sure the field exists
1088 * and has the correct type.
1090 * Returns: TRUE if the value could be set correctly. If there was no field
1091 * with @fieldname or the existing field did not contain a data, this function
1095 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1098 GstStructureField *field;
1100 g_return_val_if_fail (structure != NULL, FALSE);
1101 g_return_val_if_fail (fieldname != NULL, FALSE);
1102 g_return_val_if_fail (value != NULL, FALSE);
1104 field = gst_structure_get_field (structure, fieldname);
1108 if (!GST_VALUE_HOLDS_DATE (&field->value))
1111 *value = g_value_dup_boxed (&field->value);
1117 * gst_structure_get_clock_time:
1118 * @structure: a #GstStructure
1119 * @fieldname: the name of a field
1120 * @value: a pointer to a #GstClockTime to set
1122 * Sets the clock time pointed to by @value corresponding to the clock time
1123 * of the given field. Caller is responsible for making sure the field exists
1124 * and has the correct type.
1126 * Returns: TRUE if the value could be set correctly. If there was no field
1127 * with @fieldname or the existing field did not contain a #GstClockTime, this
1128 * function returns FALSE.
1131 gst_structure_get_clock_time (const GstStructure * structure,
1132 const gchar * fieldname, GstClockTime * value)
1134 GstStructureField *field;
1136 g_return_val_if_fail (structure != NULL, FALSE);
1137 g_return_val_if_fail (fieldname != NULL, FALSE);
1138 g_return_val_if_fail (value != NULL, FALSE);
1140 field = gst_structure_get_field (structure, fieldname);
1144 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1147 *value = g_value_get_uint64 (&field->value);
1153 * gst_structure_get_double:
1154 * @structure: a #GstStructure
1155 * @fieldname: the name of a field
1156 * @value: a pointer to a #GstFourcc to set
1158 * Sets the double pointed to by @value corresponding to the value of the
1159 * given field. Caller is responsible for making sure the field exists
1160 * and has the correct type.
1162 * Returns: TRUE if the value could be set correctly. If there was no field
1163 * with @fieldname or the existing field did not contain a double, this
1164 * function returns FALSE.
1167 gst_structure_get_double (const GstStructure * structure,
1168 const gchar * fieldname, gdouble * value)
1170 GstStructureField *field;
1172 g_return_val_if_fail (structure != NULL, FALSE);
1173 g_return_val_if_fail (fieldname != NULL, FALSE);
1174 g_return_val_if_fail (value != NULL, FALSE);
1176 field = gst_structure_get_field (structure, fieldname);
1180 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1183 *value = g_value_get_double (&field->value);
1189 * gst_structure_get_string:
1190 * @structure: a #GstStructure
1191 * @fieldname: the name of a field
1193 * Finds the field corresponding to @fieldname, and returns the string
1194 * contained in the field's value. Caller is responsible for making
1195 * sure the field exists and has the correct type.
1197 * The string should not be modified, and remains valid until the next
1198 * call to a gst_structure_*() function with the given structure.
1200 * Returns: a pointer to the string or NULL when the field did not exist
1201 * or did not contain a string.
1204 gst_structure_get_string (const GstStructure * structure,
1205 const gchar * fieldname)
1207 GstStructureField *field;
1209 g_return_val_if_fail (structure != NULL, NULL);
1210 g_return_val_if_fail (fieldname != NULL, NULL);
1212 field = gst_structure_get_field (structure, fieldname);
1216 if (!G_VALUE_HOLDS_STRING (&field->value))
1219 return g_value_get_string (&field->value);
1223 * gst_structure_get_enum:
1224 * @structure: a #GstStructure
1225 * @fieldname: the name of a field
1226 * @enumtype: the enum type of a field
1227 * @value: a pointer to an int to set
1229 * Sets the int pointed to by @value corresponding to the value of the
1230 * given field. Caller is responsible for making sure the field exists,
1231 * has the correct type and that the enumtype is correct.
1233 * Returns: TRUE if the value could be set correctly. If there was no field
1234 * with @fieldname or the existing field did not contain an enum of the given
1235 * type, this function returns FALSE.
1238 gst_structure_get_enum (const GstStructure * structure,
1239 const gchar * fieldname, GType enumtype, gint * value)
1241 GstStructureField *field;
1243 g_return_val_if_fail (structure != NULL, FALSE);
1244 g_return_val_if_fail (fieldname != NULL, FALSE);
1245 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1246 g_return_val_if_fail (value != NULL, FALSE);
1248 field = gst_structure_get_field (structure, fieldname);
1252 if (!G_VALUE_HOLDS_ENUM (&field->value))
1254 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1257 *value = g_value_get_enum (&field->value);
1263 * gst_structure_get_fraction:
1264 * @structure: a #GstStructure
1265 * @fieldname: the name of a field
1266 * @value_numerator: a pointer to an int to set
1267 * @value_denominator: a pointer to an int to set
1269 * Sets the integers pointed to by @value_numerator and @value_denominator
1270 * corresponding to the value of the given field. Caller is responsible
1271 * for making sure the field exists and has the correct type.
1273 * Returns: TRUE if the values could be set correctly. If there was no field
1274 * with @fieldname or the existing field did not contain a GstFraction, this
1275 * function returns FALSE.
1278 gst_structure_get_fraction (const GstStructure * structure,
1279 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1281 GstStructureField *field;
1283 g_return_val_if_fail (structure != NULL, FALSE);
1284 g_return_val_if_fail (fieldname != NULL, FALSE);
1285 g_return_val_if_fail (value_numerator != NULL, FALSE);
1286 g_return_val_if_fail (value_denominator != NULL, FALSE);
1288 field = gst_structure_get_field (structure, fieldname);
1292 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1295 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1296 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1301 typedef struct _GstStructureAbbreviation
1306 GstStructureAbbreviation;
1308 /* return a copy of an array of GstStructureAbbreviation containing all the
1309 * known type_string, GType maps, including abbreviations for common types */
1310 static GstStructureAbbreviation *
1311 gst_structure_get_abbrs (gint * n_abbrs)
1313 static GstStructureAbbreviation *abbrs = NULL;
1314 static gint num = 0;
1316 if (abbrs == NULL) {
1317 /* dynamically generate the array */
1318 GstStructureAbbreviation dyn_abbrs[] = {
1323 {"float", G_TYPE_FLOAT}
1327 {"double", G_TYPE_DOUBLE}
1329 {"d", G_TYPE_DOUBLE}
1331 {"buffer", GST_TYPE_BUFFER}
1333 {"fourcc", GST_TYPE_FOURCC}
1335 {"4", GST_TYPE_FOURCC}
1337 {"fraction", GST_TYPE_FRACTION}
1339 {"boolean", G_TYPE_BOOLEAN}
1341 {"bool", G_TYPE_BOOLEAN}
1343 {"b", G_TYPE_BOOLEAN}
1345 {"string", G_TYPE_STRING}
1347 {"str", G_TYPE_STRING}
1349 {"s", G_TYPE_STRING}
1351 num = G_N_ELEMENTS (dyn_abbrs);
1352 /* permanently allocate and copy the array now */
1353 abbrs = g_new0 (GstStructureAbbreviation, num);
1354 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * num);
1361 /* given a type_name that could be a type abbreviation or a registered GType,
1362 * return a matching GType */
1364 gst_structure_gtype_from_abbr (const char *type_name)
1367 GstStructureAbbreviation *abbrs;
1370 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1372 abbrs = gst_structure_get_abbrs (&n_abbrs);
1374 for (i = 0; i < n_abbrs; i++) {
1375 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1376 return abbrs[i].type;
1380 /* this is the fallback */
1381 return g_type_from_name (type_name);
1385 gst_structure_to_abbr (GType type)
1388 GstStructureAbbreviation *abbrs;
1391 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1393 abbrs = gst_structure_get_abbrs (&n_abbrs);
1395 for (i = 0; i < n_abbrs; i++) {
1396 if (type == abbrs[i].type) {
1397 return abbrs[i].type_name;
1401 return g_type_name (type);
1405 gst_structure_value_get_generic_type (GValue * val)
1407 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1408 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1409 GArray *array = g_value_peek_pointer (val);
1411 if (array->len > 0) {
1412 GValue *value = &g_array_index (array, GValue, 0);
1414 return gst_structure_value_get_generic_type (value);
1418 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1420 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1421 return G_TYPE_DOUBLE;
1422 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1423 return GST_TYPE_FRACTION;
1425 return G_VALUE_TYPE (val);
1428 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1429 ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1433 * gst_structure_to_string:
1434 * @structure: a #GstStructure
1436 * Converts @structure to a human-readable string representation.
1438 * Returns: a pointer to string allocated by g_malloc(). g_free() after
1442 gst_structure_to_string (const GstStructure * structure)
1444 GstStructureField *field;
1448 /* NOTE: This function is potentially called by the debug system,
1449 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1450 * should be careful to avoid recursion. This includes any functions
1451 * called by gst_structure_to_string. In particular, calls should
1452 * not use the GST_PTR_FORMAT extension. */
1454 g_return_val_if_fail (structure != NULL, NULL);
1456 s = g_string_new ("");
1457 /* FIXME this string may need to be escaped */
1458 g_string_append_printf (s, "%s", g_quark_to_string (structure->name));
1459 for (i = 0; i < structure->fields->len; i++) {
1463 field = GST_STRUCTURE_FIELD (structure, i);
1465 t = gst_value_serialize (&field->value);
1466 type = gst_structure_value_get_generic_type (&field->value);
1468 g_string_append_printf (s, ", %s=(%s)%s", g_quark_to_string (field->name),
1469 gst_structure_to_abbr (type), GST_STR_NULL (t));
1472 return g_string_free (s, FALSE);
1476 * r will still point to the string. if end == next, the string will not be
1477 * null-terminated. In all other cases it will be.
1478 * end = pointer to char behind end of string, next = pointer to start of
1480 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1483 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next)
1493 ret = gst_structure_parse_simple_string (s, end);
1522 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1525 GValue value1 = { 0 };
1526 GValue value2 = { 0 };
1535 ret = gst_structure_parse_value (s, &s, &value1, type);
1539 while (g_ascii_isspace (*s))
1546 while (g_ascii_isspace (*s))
1549 ret = gst_structure_parse_value (s, &s, &value2, type);
1553 while (g_ascii_isspace (*s))
1560 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1563 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1564 range_type = GST_TYPE_DOUBLE_RANGE;
1565 g_value_init (value, range_type);
1566 gst_value_set_double_range (value, g_value_get_double (&value1),
1567 g_value_get_double (&value2));
1568 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1569 range_type = GST_TYPE_INT_RANGE;
1570 g_value_init (value, range_type);
1571 gst_value_set_int_range (value, g_value_get_int (&value1),
1572 g_value_get_int (&value2));
1573 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1574 range_type = GST_TYPE_FRACTION_RANGE;
1575 g_value_init (value, range_type);
1576 gst_value_set_fraction_range (value, &value1, &value2);
1586 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1587 GType type, GType list_type, char begin, char end)
1589 GValue list_value = { 0 };
1593 g_value_init (value, list_type);
1594 array = g_value_peek_pointer (value);
1600 while (g_ascii_isspace (*s))
1608 ret = gst_structure_parse_value (s, &s, &list_value, type);
1612 g_array_append_val (array, list_value);
1614 while (g_ascii_isspace (*s))
1622 while (g_ascii_isspace (*s))
1625 memset (&list_value, 0, sizeof (list_value));
1626 ret = gst_structure_parse_value (s, &s, &list_value, type);
1630 g_array_append_val (array, list_value);
1631 while (g_ascii_isspace (*s))
1642 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
1644 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
1649 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
1652 return gst_structure_parse_any_list (s, after, value, type,
1653 GST_TYPE_ARRAY, '<', '>');
1657 gst_structure_parse_simple_string (gchar * str, gchar ** end)
1661 while (GST_ASCII_IS_STRING (*s)) {
1671 gst_structure_parse_field (gchar * str,
1672 gchar ** after, GstStructureField * field)
1681 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1684 if (!gst_structure_parse_simple_string (s, &name_end))
1688 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1697 field->name = g_quark_from_string (name);
1700 if (!gst_structure_parse_value (s, &s, &field->value, G_TYPE_INVALID))
1708 gst_structure_parse_value (gchar * str,
1709 gchar ** after, GValue * value, GType default_type)
1718 GType type = default_type;
1722 while (g_ascii_isspace (*s))
1725 /* check if there's a (type_name) 'cast' */
1728 type = G_TYPE_INVALID;
1731 while (g_ascii_isspace (*s))
1734 if (!gst_structure_parse_simple_string (s, &type_end))
1737 while (g_ascii_isspace (*s))
1742 while (g_ascii_isspace (*s))
1747 type = gst_structure_gtype_from_abbr (type_name);
1750 if (type == G_TYPE_INVALID)
1754 while (g_ascii_isspace (*s))
1757 ret = gst_structure_parse_range (s, &s, value, type);
1758 } else if (*s == '{') {
1759 ret = gst_structure_parse_list (s, &s, value, type);
1760 } else if (*s == '<') {
1761 ret = gst_structure_parse_array (s, &s, value, type);
1764 if (!gst_structure_parse_string (s, &value_end, &s))
1769 if (type == G_TYPE_INVALID) {
1771 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_STRING };
1774 for (i = 0; i < 4; i++) {
1775 g_value_init (value, try_types[i]);
1776 ret = gst_value_deserialize (value, value_s);
1779 g_value_unset (value);
1782 g_value_init (value, type);
1784 ret = gst_value_deserialize (value, value_s);
1795 * gst_structure_from_string:
1796 * @string: a string representation of a #GstStructure.
1797 * @end: pointer to store the end of the string in.
1799 * Creates a #GstStructure from a string representation.
1800 * If end is not NULL, a pointer to the place inside the given string
1801 * where parsing ended will be returned.
1803 * Returns: a new #GstStructure or NULL when the string could not
1804 * be parsed. Free after usage.
1807 gst_structure_from_string (const gchar * string, gchar ** end)
1814 GstStructure *structure = NULL;
1815 GstStructureField field = { 0 };
1817 g_return_val_if_fail (string != NULL, NULL);
1819 copy = g_strdup (string);
1823 if (!gst_structure_parse_string (r, &w, &r))
1826 while (g_ascii_isspace (*r) || (r[0] == '\\' && g_ascii_isspace (r[1])))
1828 if (*r != 0 && *r != ';' && *r != ',')
1833 structure = gst_structure_empty_new (name);
1836 while (*r && (*r != ';')) {
1840 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1841 && g_ascii_isspace (r[1]))))
1844 memset (&field, 0, sizeof (field));
1845 if (!gst_structure_parse_field (r, &r, &field))
1847 gst_structure_set_field (structure, &field);
1848 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1849 && g_ascii_isspace (r[1]))))
1854 *end = (char *) string + (r - copy);
1861 gst_structure_free (structure);
1867 gst_structure_transform_to_string (const GValue * src_value,
1868 GValue * dest_value)
1870 g_return_if_fail (src_value != NULL);
1871 g_return_if_fail (dest_value != NULL);
1873 dest_value->data[0].v_pointer =
1874 gst_structure_to_string (src_value->data[0].v_pointer);
1877 static GstStructure *
1878 gst_structure_copy_conditional (const GstStructure * structure)
1881 return gst_structure_copy (structure);
1885 /* fixate utility functions */
1888 * gst_structure_fixate_field_nearest_int:
1889 * @structure: a #GstStructure
1890 * @field_name: a field in @structure
1891 * @target: the target value of the fixation
1893 * Fixates a #GstStructure by changing the given field to the nearest
1894 * integer to @target that is a subset of the existing field.
1896 * Returns: TRUE if the structure could be fixated
1899 gst_structure_fixate_field_nearest_int (GstStructure * structure,
1900 const char *field_name, int target)
1902 const GValue *value;
1904 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1905 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1907 value = gst_structure_get_value (structure, field_name);
1909 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
1912 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
1915 x = gst_value_get_int_range_min (value);
1918 x = gst_value_get_int_range_max (value);
1921 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
1923 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1924 const GValue *list_value;
1927 int best_index = -1;
1929 n = gst_value_list_get_size (value);
1930 for (i = 0; i < n; i++) {
1931 list_value = gst_value_list_get_value (value, i);
1932 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
1933 int x = g_value_get_int (list_value);
1935 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
1941 if (best_index != -1) {
1942 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
1952 * gst_structure_fixate_field_nearest_double:
1953 * @structure: a #GstStructure
1954 * @field_name: a field in @structure
1955 * @target: the target value of the fixation
1957 * Fixates a #GstStructure by changing the given field to the nearest
1958 * double to @target that is a subset of the existing field.
1960 * Returns: TRUE if the structure could be fixated
1963 gst_structure_fixate_field_nearest_double (GstStructure * structure,
1964 const char *field_name, double target)
1966 const GValue *value;
1968 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1969 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1971 value = gst_structure_get_value (structure, field_name);
1973 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
1976 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
1979 x = gst_value_get_double_range_min (value);
1982 x = gst_value_get_double_range_max (value);
1985 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
1987 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1988 const GValue *list_value;
1991 int best_index = -1;
1993 n = gst_value_list_get_size (value);
1994 for (i = 0; i < n; i++) {
1995 list_value = gst_value_list_get_value (value, i);
1996 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
1997 double x = g_value_get_double (list_value);
1999 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2005 if (best_index != -1) {
2006 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2017 * gst_structure_fixate_field_boolean:
2018 * @structure: a #GstStructure
2019 * @field_name: a field in @structure
2020 * @target: the target value of the fixation
2022 * Fixates a #GstStructure by changing the given @field_name field to the given
2023 * @target boolean if that field is not fixed yet.
2025 * Returns: TRUE if the structure could be fixated
2028 gst_structure_fixate_field_boolean (GstStructure * structure,
2029 const char *field_name, gboolean target)
2031 const GValue *value;
2033 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2034 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2036 value = gst_structure_get_value (structure, field_name);
2038 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2041 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2042 const GValue *list_value;
2045 int best_index = -1;
2047 n = gst_value_list_get_size (value);
2048 for (i = 0; i < n; i++) {
2049 list_value = gst_value_list_get_value (value, i);
2050 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2051 gboolean x = g_value_get_boolean (list_value);
2053 if (best_index == -1 || x == target) {
2059 if (best_index != -1) {
2060 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2070 * gst_structure_fixate_field_nearest_fraction:
2071 * @structure: a #GstStructure
2072 * @field_name: a field in @structure
2073 * @target_numerator: The numerator of the target value of the fixation
2074 * @target_denominator: The denominator of the target value of the fixation
2076 * Fixates a #GstStructure by changing the given field to the nearest
2077 * fraction to @target_numerator/@target_denominator that is a subset
2078 * of the existing field.
2080 * Returns: TRUE if the structure could be fixated
2083 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2084 const char *field_name, const gint target_numerator,
2085 const gint target_denominator)
2087 const GValue *value;
2089 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2090 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2092 value = gst_structure_get_value (structure, field_name);
2094 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2097 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2098 const GValue *x, *new_value;
2099 GValue target = { 0 };
2100 g_value_init (&target, GST_TYPE_FRACTION);
2101 gst_value_set_fraction (&target, target_numerator, target_denominator);
2103 new_value = ⌖
2104 x = gst_value_get_fraction_range_min (value);
2105 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2107 x = gst_value_get_fraction_range_max (value);
2108 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2111 gst_structure_set_value (structure, field_name, new_value);
2112 g_value_unset (&target);
2114 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2115 const GValue *list_value;
2117 const GValue *best = NULL;
2118 GValue best_diff = { 0 };
2119 GValue cur_diff = { 0 };
2120 GValue target = { 0 };
2121 gboolean res = FALSE;
2123 g_value_init (&best_diff, GST_TYPE_FRACTION);
2124 g_value_init (&cur_diff, GST_TYPE_FRACTION);
2125 g_value_init (&target, GST_TYPE_FRACTION);
2127 gst_value_set_fraction (&target, target_numerator, target_denominator);
2129 n = gst_value_list_get_size (value);
2130 for (i = 0; i < n; i++) {
2131 list_value = gst_value_list_get_value (value, i);
2132 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2135 gst_value_set_fraction (&best_diff, 0, 1);
2137 if (gst_value_compare (list_value, &target) == GST_VALUE_LESS_THAN)
2138 gst_value_fraction_subtract (&cur_diff, &target, list_value);
2140 gst_value_fraction_subtract (&cur_diff, list_value, &target);
2142 if (gst_value_compare (&cur_diff, &best_diff) == GST_VALUE_LESS_THAN) {
2144 g_value_copy (&cur_diff, &best_diff);
2150 gst_structure_set_value (structure, field_name, best);
2153 g_value_unset (&best_diff);
2154 g_value_unset (&cur_diff);
2155 g_value_unset (&target);