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 as
28 * 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 in
33 * a flexible an extensible way. A #GstStructure does not have a refcount because it
34 * usually is part of a higher level object such as #GstCaps. It provides a means to
35 * enforce mutability using the refcount of the parent with the
36 * gst_structure_set_parent_refcount() method.
38 * A #GstStructure can be created with gst_structure_empty_new() or gst_structure_new(),
39 * which both take a name and an optional set of key/value pairs along with the types
42 * Field values can be changed with gst_structure_set_value() or gst_structure_set().
44 * Field values can be retrieved with gst_structure_get_value() or the more convenient
45 * gst_structure_get_*() functions.
47 * Fields can be removed with gst_structure_remove_field() or gst_structure_remove_fields().
49 * Last reviewed on 2005-11-09 (0.9.4)
58 #include "gst_private.h"
60 #include <gobject/gvaluecollector.h>
62 typedef struct _GstStructureField GstStructureField;
64 struct _GstStructureField
70 #define GST_STRUCTURE_FIELD(structure, index) \
71 &g_array_index((structure)->fields, GstStructureField, (index))
73 #define IS_MUTABLE(structure) \
74 (!(structure)->parent_refcount || \
75 g_atomic_int_get ((structure)->parent_refcount) == 1)
77 static void gst_structure_set_field (GstStructure * structure,
78 GstStructureField * field);
79 static GstStructureField *gst_structure_get_field (const GstStructure *
80 structure, const gchar * fieldname);
81 static GstStructureField *gst_structure_id_get_field (const GstStructure *
82 structure, GQuark field);
83 static void gst_structure_transform_to_string (const GValue * src_value,
85 static GstStructure *gst_structure_copy_conditional (const GstStructure *
87 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
88 GValue * value, GType default_type);
89 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
92 gst_structure_get_type (void)
94 static GType gst_structure_type = 0;
96 if (G_UNLIKELY (gst_structure_type == 0)) {
97 gst_structure_type = g_boxed_type_register_static ("GstStructure",
98 (GBoxedCopyFunc) gst_structure_copy_conditional,
99 (GBoxedFreeFunc) gst_structure_free);
101 g_value_register_transform_func (gst_structure_type, G_TYPE_STRING,
102 gst_structure_transform_to_string);
105 return gst_structure_type;
108 static GstStructure *
109 gst_structure_id_empty_new_with_size (GQuark quark, guint prealloc)
111 GstStructure *structure;
113 structure = g_new0 (GstStructure, 1);
114 structure->type = gst_structure_get_type ();
115 structure->name = quark;
117 g_array_sized_new (FALSE, TRUE, sizeof (GstStructureField), prealloc);
123 * gst_structure_id_empty_new:
124 * @quark: name of new structure
126 * Creates a new, empty #GstStructure with the given name as a GQuark.
128 * Returns: a new, empty #GstStructure
131 gst_structure_id_empty_new (GQuark quark)
133 g_return_val_if_fail (quark != 0, NULL);
135 return gst_structure_id_empty_new_with_size (quark, 0);
139 * gst_structure_empty_new:
140 * @name: name of new structure
142 * Creates a new, empty #GstStructure with the given name.
144 * Returns: a new, empty #GstStructure
147 gst_structure_empty_new (const gchar * name)
149 g_return_val_if_fail (name != NULL, NULL);
151 return gst_structure_id_empty_new_with_size (g_quark_from_string (name), 0);
156 * @name: name of new structure
157 * @firstfield: name of first field to set
158 * @...: additional arguments
160 * Creates a new #GstStructure with the given name. Parses the
161 * list of variable arguments and sets fields to the values listed.
162 * Variable arguments should be passed as field name, field type,
163 * and value. Last variable argument should be NULL.
165 * Returns: a new #GstStructure
168 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
170 GstStructure *structure;
173 g_return_val_if_fail (name != NULL, NULL);
175 va_start (varargs, firstfield);
177 structure = gst_structure_new_valist (name, firstfield, varargs);
185 * gst_structure_new_valist:
186 * @name: name of new structure
187 * @firstfield: name of first field to set
188 * @varargs: variable argument list
190 * Creates a new #GstStructure with the given name. Structure fields
191 * are set according to the varargs in a manner similar to
192 * @gst_structure_new.
194 * Returns: a new #GstStructure
197 gst_structure_new_valist (const gchar * name,
198 const gchar * firstfield, va_list varargs)
200 GstStructure *structure;
202 g_return_val_if_fail (name != NULL, NULL);
204 structure = gst_structure_empty_new (name);
205 gst_structure_set_valist (structure, firstfield, varargs);
211 * gst_structure_set_parent_refcount:
212 * @structure: a #GstStructure
213 * @refcount: a pointer to the parent's refcount
215 * Sets the parent_refcount field of #GstStructure. This field is used to
216 * determine whether a structure is mutable or not. This function should only be
217 * called by code implementing parent objects of #GstStructure, as described in
218 * the MT Refcounting section of the design documents.
221 gst_structure_set_parent_refcount (GstStructure * structure, int *refcount)
223 g_return_if_fail (structure != NULL);
225 /* if we have a parent_refcount already, we can only clear
226 * if with a NULL refcount */
227 if (structure->parent_refcount)
228 g_return_if_fail (refcount == NULL);
230 g_return_if_fail (refcount != NULL);
232 structure->parent_refcount = refcount;
236 * gst_structure_copy:
237 * @structure: a #GstStructure to duplicate
239 * Duplicates a #GstStructure and all its fields and values.
241 * Returns: a new #GstStructure.
244 gst_structure_copy (const GstStructure * structure)
246 GstStructure *new_structure;
247 GstStructureField *field;
250 g_return_val_if_fail (structure != NULL, NULL);
253 gst_structure_id_empty_new_with_size (structure->name,
254 structure->fields->len);
256 for (i = 0; i < structure->fields->len; i++) {
257 GstStructureField new_field = { 0 };
259 field = GST_STRUCTURE_FIELD (structure, i);
261 new_field.name = field->name;
262 gst_value_init_and_copy (&new_field.value, &field->value);
263 g_array_append_val (new_structure->fields, new_field);
266 return new_structure;
270 * gst_structure_free:
271 * @structure: the #GstStructure to free
273 * Frees a #GstStructure and all its fields and values. The structure must not
274 * have a parent when this function is called.
277 gst_structure_free (GstStructure * structure)
279 GstStructureField *field;
282 g_return_if_fail (structure != NULL);
283 g_return_if_fail (structure->parent_refcount == NULL);
285 for (i = 0; i < structure->fields->len; i++) {
286 field = GST_STRUCTURE_FIELD (structure, i);
288 if (G_IS_VALUE (&field->value)) {
289 g_value_unset (&field->value);
292 g_array_free (structure->fields, TRUE);
294 memset (structure, 0xff, sizeof (GstStructure));
300 * gst_structure_get_name:
301 * @structure: a #GstStructure
303 * Get the name of @structure as a string.
305 * Returns: the name of the structure.
308 gst_structure_get_name (const GstStructure * structure)
310 g_return_val_if_fail (structure != NULL, NULL);
312 return g_quark_to_string (structure->name);
316 * gst_structure_has_name:
317 * @structure: a #GstStructure
318 * @name: structure name to check for
320 * Checks if the structure has the given name
322 * Returns: TRUE if @name matches the name of the structure.
325 gst_structure_has_name (const GstStructure * structure, const gchar * name)
327 const gchar *structure_name;
329 g_return_val_if_fail (structure != NULL, FALSE);
330 g_return_val_if_fail (name != NULL, FALSE);
332 structure_name = g_quark_to_string (structure->name);
334 return (structure_name && strcmp (structure_name, name) == 0);
338 * gst_structure_get_name_id:
339 * @structure: a #GstStructure
341 * Get the name of @structure as a GQuark.
343 * Returns: the quark representing the name of the structure.
346 gst_structure_get_name_id (const GstStructure * structure)
348 g_return_val_if_fail (structure != NULL, 0);
350 return structure->name;
354 * gst_structure_set_name:
355 * @structure: a #GstStructure
356 * @name: the new name of the structure
358 * Sets the name of the structure to the given name. The string
359 * provided is copied before being used.
362 gst_structure_set_name (GstStructure * structure, const gchar * name)
364 g_return_if_fail (structure != NULL);
365 g_return_if_fail (name != NULL);
366 g_return_if_fail (IS_MUTABLE (structure));
368 structure->name = g_quark_from_string (name);
372 * gst_structure_id_set_value:
373 * @structure: a #GstStructure
374 * @field: a #GQuark representing a field
375 * @value: the new value of the field
377 * Sets the field with the given GQuark @field to @value. If the field
378 * does not exist, it is created. If the field exists, the previous
379 * value is replaced and freed.
382 gst_structure_id_set_value (GstStructure * structure,
383 GQuark field, const GValue * value)
385 GstStructureField gsfield = { 0, {0,} };
387 g_return_if_fail (structure != NULL);
388 g_return_if_fail (G_IS_VALUE (value));
389 g_return_if_fail (IS_MUTABLE (structure));
391 gsfield.name = field;
392 gst_value_init_and_copy (&gsfield.value, value);
394 gst_structure_set_field (structure, &gsfield);
398 * gst_structure_set_value:
399 * @structure: a #GstStructure
400 * @fieldname: the name of the field to set
401 * @value: the new value of the field
403 * Sets the field with the given name @field to @value. If the field
404 * does not exist, it is created. If the field exists, the previous
405 * value is replaced and freed.
408 gst_structure_set_value (GstStructure * structure,
409 const gchar * fieldname, const GValue * value)
411 g_return_if_fail (structure != NULL);
412 g_return_if_fail (fieldname != NULL);
413 g_return_if_fail (G_IS_VALUE (value));
414 g_return_if_fail (IS_MUTABLE (structure));
416 gst_structure_id_set_value (structure, g_quark_from_string (fieldname),
422 * @structure: a #GstStructure
423 * @fieldname: the name of the field to set
424 * @...: variable arguments
426 * Parses the variable arguments and sets fields accordingly.
427 * Variable arguments should be in the form field name, field type
428 * (as a GType), value(s). The last variable argument should be NULL.
431 gst_structure_set (GstStructure * structure, const gchar * field, ...)
435 g_return_if_fail (structure != NULL);
437 va_start (varargs, field);
439 gst_structure_set_valist (structure, field, varargs);
445 * gst_structure_set_valist:
446 * @structure: a #GstStructure
447 * @fieldname: the name of the field to set
448 * @varargs: variable arguments
450 * va_list form of gst_structure_set().
453 gst_structure_set_valist (GstStructure * structure,
454 const gchar * fieldname, va_list varargs)
459 g_return_if_fail (structure != NULL);
460 g_return_if_fail (IS_MUTABLE (structure));
463 GstStructureField field = { 0 };
465 field.name = g_quark_from_string (fieldname);
467 type = va_arg (varargs, GType);
469 #if GLIB_CHECK_VERSION(2,8,0)
470 if (type == G_TYPE_DATE) {
471 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
472 type = GST_TYPE_DATE;
476 g_value_init (&field.value, type);
477 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
479 g_critical ("%s", err);
482 gst_structure_set_field (structure, &field);
484 fieldname = va_arg (varargs, gchar *);
488 /* If the structure currently contains a field with the same name, it is
489 * replaced with the provided field. Otherwise, the field is added to the
490 * structure. The field's value is not deeply copied.
493 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
495 GstStructureField *f;
498 for (i = 0; i < structure->fields->len; i++) {
499 f = GST_STRUCTURE_FIELD (structure, i);
501 if (f->name == field->name) {
502 g_value_unset (&f->value);
503 memcpy (f, field, sizeof (GstStructureField));
508 g_array_append_val (structure->fields, *field);
511 /* If there is no field with the given ID, NULL is returned.
513 static GstStructureField *
514 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
516 GstStructureField *field;
519 g_return_val_if_fail (structure != NULL, NULL);
521 for (i = 0; i < structure->fields->len; i++) {
522 field = GST_STRUCTURE_FIELD (structure, i);
524 if (field->name == field_id)
531 /* If there is no field with the given ID, NULL is returned.
533 static GstStructureField *
534 gst_structure_get_field (const GstStructure * structure,
535 const gchar * fieldname)
537 g_return_val_if_fail (structure != NULL, NULL);
538 g_return_val_if_fail (fieldname != NULL, NULL);
540 return gst_structure_id_get_field (structure,
541 g_quark_from_string (fieldname));
545 * gst_structure_get_value:
546 * @structure: a #GstStructure
547 * @fieldname: the name of the field to get
549 * Get the value of the field with name @fieldname.
551 * Returns: the #GValue corresponding to the field with the given name.
554 gst_structure_get_value (const GstStructure * structure,
555 const gchar * fieldname)
557 GstStructureField *field;
559 g_return_val_if_fail (structure != NULL, NULL);
560 g_return_val_if_fail (fieldname != NULL, NULL);
562 field = gst_structure_get_field (structure, fieldname);
566 return &field->value;
570 * gst_structure_id_get_value:
571 * @structure: a #GstStructure
572 * @field: the #GQuark of the field to get
574 * Get the value of the field with GQuark @field.
576 * Returns: the #GValue corresponding to the field with the given name
580 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
582 GstStructureField *gsfield;
584 g_return_val_if_fail (structure != NULL, NULL);
586 gsfield = gst_structure_id_get_field (structure, field);
590 return &gsfield->value;
594 * gst_structure_remove_field:
595 * @structure: a #GstStructure
596 * @fieldname: the name of the field to remove
598 * Removes the field with the given name. If the field with the given
599 * name does not exist, the structure is unchanged.
602 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
604 GstStructureField *field;
608 g_return_if_fail (structure != NULL);
609 g_return_if_fail (fieldname != NULL);
610 g_return_if_fail (IS_MUTABLE (structure));
612 id = g_quark_from_string (fieldname);
614 for (i = 0; i < structure->fields->len; i++) {
615 field = GST_STRUCTURE_FIELD (structure, i);
617 if (field->name == id) {
618 if (G_IS_VALUE (&field->value)) {
619 g_value_unset (&field->value);
621 structure->fields = g_array_remove_index (structure->fields, i);
628 * gst_structure_remove_fields:
629 * @structure: a #GstStructure
630 * @fieldname: the name of the field to remove
631 * @...: NULL-terminated list of more fieldnames to remove
633 * Removes the fields with the given names. If a field does not exist, the
634 * argument is ignored.
637 gst_structure_remove_fields (GstStructure * structure,
638 const gchar * fieldname, ...)
642 g_return_if_fail (structure != NULL);
643 g_return_if_fail (fieldname != NULL);
644 /* mutability checked in remove_field */
646 va_start (varargs, fieldname);
648 gst_structure_remove_fields_valist (structure, fieldname, varargs);
654 * gst_structure_remove_fields_valist:
655 * @structure: a #GstStructure
656 * @fieldname: the name of the field to remove
657 * @varargs: NULL-terminated list of more fieldnames to remove
659 * va_list form of gst_structure_remove_fields().
662 gst_structure_remove_fields_valist (GstStructure * structure,
663 const gchar * fieldname, va_list varargs)
665 gchar *field = (gchar *) fieldname;
667 g_return_if_fail (structure != NULL);
668 g_return_if_fail (fieldname != NULL);
669 /* mutability checked in remove_field */
672 gst_structure_remove_field (structure, field);
673 field = va_arg (varargs, char *);
678 * gst_structure_remove_all_fields:
679 * @structure: a #GstStructure
681 * Removes all fields in a GstStructure.
684 gst_structure_remove_all_fields (GstStructure * structure)
686 GstStructureField *field;
689 g_return_if_fail (structure != NULL);
690 g_return_if_fail (IS_MUTABLE (structure));
692 for (i = structure->fields->len - 1; i >= 0; i--) {
693 field = GST_STRUCTURE_FIELD (structure, i);
695 if (G_IS_VALUE (&field->value)) {
696 g_value_unset (&field->value);
698 structure->fields = g_array_remove_index (structure->fields, i);
703 * gst_structure_get_field_type:
704 * @structure: a #GstStructure
705 * @fieldname: the name of the field
707 * Finds the field with the given name, and returns the type of the
708 * value it contains. If the field is not found, G_TYPE_INVALID is
711 * Returns: the #GValue of the field
714 gst_structure_get_field_type (const GstStructure * structure,
715 const gchar * fieldname)
717 GstStructureField *field;
719 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
720 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
722 field = gst_structure_get_field (structure, fieldname);
724 return G_TYPE_INVALID;
726 return G_VALUE_TYPE (&field->value);
730 * gst_structure_n_fields:
731 * @structure: a #GstStructure
733 * Get the number of fields in the structure.
735 * Returns: the number of fields in the structure
738 gst_structure_n_fields (const GstStructure * structure)
740 g_return_val_if_fail (structure != NULL, 0);
742 return structure->fields->len;
746 * gst_structure_nth_field_name:
747 * @structure: a #GstStructure
748 * @index: the index to get the name of
750 * Get the name of the given field number, counting from 0 onwards.
752 * Returns: the name of the given field number
755 gst_structure_nth_field_name (const GstStructure * structure, guint index)
757 GstStructureField *field;
759 field = GST_STRUCTURE_FIELD (structure, index);
760 return g_quark_to_string (field->name);
764 * gst_structure_foreach:
765 * @structure: a #GstStructure
766 * @func: a function to call for each field
767 * @user_data: private data
769 * Calls the provided function once for each field in the #GstStructure. The
770 * function must not modify the fields. Also see gst_structure_map_in_place().
772 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
776 gst_structure_foreach (const GstStructure * structure,
777 GstStructureForeachFunc func, gpointer user_data)
780 GstStructureField *field;
783 g_return_val_if_fail (structure != NULL, FALSE);
785 for (i = 0; i < structure->fields->len; i++) {
786 field = GST_STRUCTURE_FIELD (structure, i);
788 ret = func (field->name, &field->value, user_data);
797 * gst_structure_map_in_place:
798 * @structure: a #GstStructure
799 * @func: a function to call for each field
800 * @user_data: private data
802 * Calls the provided function once for each field in the #GstStructure. In
803 * contrast to gst_structure_foreach(), the function may modify the fields. The
804 * structure must be mutable.
806 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
810 gst_structure_map_in_place (GstStructure * structure,
811 GstStructureMapFunc func, gpointer user_data)
814 GstStructureField *field;
817 g_return_val_if_fail (structure != NULL, FALSE);
818 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
820 for (i = 0; i < structure->fields->len; i++) {
821 field = GST_STRUCTURE_FIELD (structure, i);
823 ret = func (field->name, &field->value, user_data);
832 * gst_structure_has_field:
833 * @structure: a #GstStructure
834 * @fieldname: the name of a field
836 * Check if @structure contains a field named @fieldname.
838 * Returns: TRUE if the structure contains a field with the given name
841 gst_structure_has_field (const GstStructure * structure,
842 const gchar * fieldname)
844 GstStructureField *field;
846 g_return_val_if_fail (structure != NULL, 0);
847 g_return_val_if_fail (fieldname != NULL, 0);
849 field = gst_structure_get_field (structure, fieldname);
851 return (field != NULL);
855 * gst_structure_has_field_typed:
856 * @structure: a #GstStructure
857 * @fieldname: the name of a field
858 * @type: the type of a value
860 * Check if @structure contains a field named @fieldname and with GType @type.
862 * Returns: TRUE if the structure contains a field with the given name and type
865 gst_structure_has_field_typed (const GstStructure * structure,
866 const gchar * fieldname, GType type)
868 GstStructureField *field;
870 g_return_val_if_fail (structure != NULL, 0);
871 g_return_val_if_fail (fieldname != NULL, 0);
873 field = gst_structure_get_field (structure, fieldname);
877 return (G_VALUE_TYPE (&field->value) == type);
881 /* utility functions */
884 * gst_structure_get_boolean:
885 * @structure: a #GstStructure
886 * @fieldname: the name of a field
887 * @value: a pointer to a #gboolean to set
889 * Sets the boolean pointed to by @value corresponding to the value of the
890 * given field. Caller is responsible for making sure the field exists
891 * and has the correct type.
893 * Returns: TRUE if the value could be set correctly. If there was no field
894 * with @fieldname or the existing field did not contain a boolean, this function
898 gst_structure_get_boolean (const GstStructure * structure,
899 const gchar * fieldname, gboolean * value)
901 GstStructureField *field;
903 g_return_val_if_fail (structure != NULL, FALSE);
904 g_return_val_if_fail (fieldname != NULL, FALSE);
906 field = gst_structure_get_field (structure, fieldname);
910 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
913 *value = g_value_get_boolean (&field->value);
919 * gst_structure_get_int:
920 * @structure: a #GstStructure
921 * @fieldname: the name of a field
922 * @value: a pointer to an int to set
924 * Sets the int pointed to by @value corresponding to the value of the
925 * given field. Caller is responsible for making sure the field exists
926 * and has the correct type.
928 * Returns: TRUE if the value could be set correctly. If there was no field
929 * with @fieldname or the existing field did not contain an int, this function
933 gst_structure_get_int (const GstStructure * structure,
934 const gchar * fieldname, gint * value)
936 GstStructureField *field;
938 g_return_val_if_fail (structure != NULL, FALSE);
939 g_return_val_if_fail (fieldname != NULL, FALSE);
940 g_return_val_if_fail (value != NULL, FALSE);
942 field = gst_structure_get_field (structure, fieldname);
946 if (!G_VALUE_HOLDS_INT (&field->value))
949 *value = g_value_get_int (&field->value);
955 * gst_structure_get_fourcc:
956 * @structure: a #GstStructure
957 * @fieldname: the name of a field
958 * @value: a pointer to a #GstFourcc to set
960 * Sets the #GstFourcc pointed to by @value corresponding to the value of the
961 * given field. Caller is responsible for making sure the field exists
962 * and has the correct type.
964 * Returns: TRUE if the value could be set correctly. If there was no field
965 * with @fieldname or the existing field did not contain a fourcc, this function
969 gst_structure_get_fourcc (const GstStructure * structure,
970 const gchar * fieldname, guint32 * value)
972 GstStructureField *field;
974 g_return_val_if_fail (structure != NULL, FALSE);
975 g_return_val_if_fail (fieldname != NULL, FALSE);
976 g_return_val_if_fail (value != NULL, FALSE);
978 field = gst_structure_get_field (structure, fieldname);
982 if (!GST_VALUE_HOLDS_FOURCC (&field->value))
985 *value = gst_value_get_fourcc (&field->value);
991 * gst_structure_get_date:
992 * @structure: a #GstStructure
993 * @fieldname: the name of a field
994 * @value: a pointer to a #GDate to set
996 * Sets the date pointed to by @value corresponding to the date of the
997 * given field. Caller is responsible for making sure the field exists
998 * and has the correct type.
1000 * Returns: TRUE if the value could be set correctly. If there was no field
1001 * with @fieldname or the existing field did not contain a data, this function
1005 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1008 GstStructureField *field;
1010 g_return_val_if_fail (structure != NULL, FALSE);
1011 g_return_val_if_fail (fieldname != NULL, FALSE);
1012 g_return_val_if_fail (value != NULL, FALSE);
1014 field = gst_structure_get_field (structure, fieldname);
1018 if (!GST_VALUE_HOLDS_DATE (&field->value))
1021 *value = g_value_dup_boxed (&field->value);
1027 * gst_structure_get_clock_time:
1028 * @structure: a #GstStructure
1029 * @fieldname: the name of a field
1030 * @value: a pointer to a #GstClockTime to set
1032 * Sets the clock time pointed to by @value corresponding to the clock time
1033 * of the given field. Caller is responsible for making sure the field exists
1034 * and has the correct type.
1036 * Returns: TRUE if the value could be set correctly. If there was no field
1037 * with @fieldname or the existing field did not contain a #GstClockTime, this
1038 * function returns FALSE.
1041 gst_structure_get_clock_time (const GstStructure * structure,
1042 const gchar * fieldname, GstClockTime * value)
1044 GstStructureField *field;
1046 g_return_val_if_fail (structure != NULL, FALSE);
1047 g_return_val_if_fail (fieldname != NULL, FALSE);
1048 g_return_val_if_fail (value != NULL, FALSE);
1050 field = gst_structure_get_field (structure, fieldname);
1054 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1057 *value = g_value_get_uint64 (&field->value);
1063 * gst_structure_get_double:
1064 * @structure: a #GstStructure
1065 * @fieldname: the name of a field
1066 * @value: a pointer to a #GstFourcc to set
1068 * Sets the double pointed to by @value corresponding to the value of the
1069 * given field. Caller is responsible for making sure the field exists
1070 * and has the correct type.
1072 * Returns: TRUE if the value could be set correctly. If there was no field
1073 * with @fieldname or the existing field did not contain a double, this
1074 * function returns FALSE.
1077 gst_structure_get_double (const GstStructure * structure,
1078 const gchar * fieldname, gdouble * value)
1080 GstStructureField *field;
1082 g_return_val_if_fail (structure != NULL, FALSE);
1083 g_return_val_if_fail (fieldname != NULL, FALSE);
1084 g_return_val_if_fail (value != NULL, FALSE);
1086 field = gst_structure_get_field (structure, fieldname);
1090 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1093 *value = g_value_get_double (&field->value);
1099 * gst_structure_get_string:
1100 * @structure: a #GstStructure
1101 * @fieldname: the name of a field
1103 * Finds the field corresponding to @fieldname, and returns the string
1104 * contained in the field's value. Caller is responsible for making
1105 * sure the field exists and has the correct type.
1107 * The string should not be modified, and remains valid until the next
1108 * call to a gst_structure_*() function with the given structure.
1110 * Returns: a pointer to the string or NULL when the field did not exist
1111 * or did not contain a string.
1114 gst_structure_get_string (const GstStructure * structure,
1115 const gchar * fieldname)
1117 GstStructureField *field;
1119 g_return_val_if_fail (structure != NULL, NULL);
1120 g_return_val_if_fail (fieldname != NULL, NULL);
1122 field = gst_structure_get_field (structure, fieldname);
1126 if (!G_VALUE_HOLDS_STRING (&field->value))
1129 return g_value_get_string (&field->value);
1133 * gst_structure_get_enum:
1134 * @structure: a #GstStructure
1135 * @fieldname: the name of a field
1136 * @enumtype: the enum type of a field
1137 * @value: a pointer to an int to set
1139 * Sets the int pointed to by @value corresponding to the value of the
1140 * given field. Caller is responsible for making sure the field exists,
1141 * has the correct type and that the enumtype is correct.
1143 * Returns: TRUE if the value could be set correctly. If there was no field
1144 * with @fieldname or the existing field did not contain an enum of the given
1145 * type, this function returns FALSE.
1148 gst_structure_get_enum (const GstStructure * structure,
1149 const gchar * fieldname, GType enumtype, gint * value)
1151 GstStructureField *field;
1153 g_return_val_if_fail (structure != NULL, FALSE);
1154 g_return_val_if_fail (fieldname != NULL, FALSE);
1155 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1156 g_return_val_if_fail (value != NULL, FALSE);
1158 field = gst_structure_get_field (structure, fieldname);
1162 if (!G_VALUE_HOLDS_ENUM (&field->value))
1164 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1167 *value = g_value_get_enum (&field->value);
1173 * gst_structure_get_fraction:
1174 * @structure: a #GstStructure
1175 * @fieldname: the name of a field
1176 * @value_numerator: a pointer to an int to set
1177 * @value_denominator: a pointer to an int to set
1179 * Sets the integers pointed to by @value_numerator and @value_denominator
1180 * corresponding to the value of the given field. Caller is responsible
1181 * for making sure the field exists and has the correct type.
1183 * Returns: TRUE if the values could be set correctly. If there was no field
1184 * with @fieldname or the existing field did not contain a GstFraction, this
1185 * function returns FALSE.
1188 gst_structure_get_fraction (const GstStructure * structure,
1189 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1191 GstStructureField *field;
1193 g_return_val_if_fail (structure != NULL, FALSE);
1194 g_return_val_if_fail (fieldname != NULL, FALSE);
1195 g_return_val_if_fail (value_numerator != NULL, FALSE);
1196 g_return_val_if_fail (value_denominator != NULL, FALSE);
1198 field = gst_structure_get_field (structure, fieldname);
1202 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1205 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1206 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1211 typedef struct _GstStructureAbbreviation
1216 GstStructureAbbreviation;
1218 static GstStructureAbbreviation *
1219 gst_structure_get_abbrs (gint * n_abbrs)
1221 static GstStructureAbbreviation *abbrs = NULL;
1222 static gint num = 0;
1224 if (abbrs == NULL) {
1225 /* dynamically generate the array */
1226 GstStructureAbbreviation dyn_abbrs[] = {
1231 {"float", G_TYPE_FLOAT}
1235 {"double", G_TYPE_DOUBLE}
1237 {"d", G_TYPE_DOUBLE}
1239 {"buffer", GST_TYPE_BUFFER}
1241 {"fourcc", GST_TYPE_FOURCC}
1243 {"4", GST_TYPE_FOURCC}
1245 {"fraction", GST_TYPE_FRACTION}
1247 {"boolean", G_TYPE_BOOLEAN}
1249 {"bool", G_TYPE_BOOLEAN}
1251 {"b", G_TYPE_BOOLEAN}
1253 {"string", G_TYPE_STRING}
1255 {"str", G_TYPE_STRING}
1257 {"s", G_TYPE_STRING}
1259 num = G_N_ELEMENTS (dyn_abbrs);
1260 /* permanently allocate and copy the array now */
1261 abbrs = g_new0 (GstStructureAbbreviation, num);
1262 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * num);
1270 gst_structure_from_abbr (const char *type_name)
1273 GstStructureAbbreviation *abbrs;
1276 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1278 abbrs = gst_structure_get_abbrs (&n_abbrs);
1280 for (i = 0; i < n_abbrs; i++) {
1281 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1282 return abbrs[i].type;
1286 /* this is the fallback */
1287 return g_type_from_name (type_name);
1291 gst_structure_to_abbr (GType type)
1294 GstStructureAbbreviation *abbrs;
1297 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1299 abbrs = gst_structure_get_abbrs (&n_abbrs);
1301 for (i = 0; i < n_abbrs; i++) {
1302 if (type == abbrs[i].type) {
1303 return abbrs[i].type_name;
1307 return g_type_name (type);
1311 gst_structure_value_get_generic_type (GValue * val)
1313 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1314 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1315 GArray *array = g_value_peek_pointer (val);
1317 if (array->len > 0) {
1318 GValue *value = &g_array_index (array, GValue, 0);
1320 return gst_structure_value_get_generic_type (value);
1324 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1326 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1327 return G_TYPE_DOUBLE;
1328 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1329 return GST_TYPE_FRACTION;
1331 return G_VALUE_TYPE (val);
1334 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1335 ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1339 * gst_structure_to_string:
1340 * @structure: a #GstStructure
1342 * Converts @structure to a human-readable string representation.
1344 * Returns: a pointer to string allocated by g_malloc(). g_free() after
1348 gst_structure_to_string (const GstStructure * structure)
1350 GstStructureField *field;
1354 /* NOTE: This function is potentially called by the debug system,
1355 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1356 * should be careful to avoid recursion. This includes any functions
1357 * called by gst_structure_to_string. In particular, calls should
1358 * not use the GST_PTR_FORMAT extension. */
1360 g_return_val_if_fail (structure != NULL, NULL);
1362 s = g_string_new ("");
1363 /* FIXME this string may need to be escaped */
1364 g_string_append_printf (s, "%s", g_quark_to_string (structure->name));
1365 for (i = 0; i < structure->fields->len; i++) {
1369 field = GST_STRUCTURE_FIELD (structure, i);
1371 t = gst_value_serialize (&field->value);
1372 type = gst_structure_value_get_generic_type (&field->value);
1374 g_string_append_printf (s, ", %s=(%s)%s", g_quark_to_string (field->name),
1375 gst_structure_to_abbr (type), GST_STR_NULL (t));
1378 return g_string_free (s, FALSE);
1382 * r will still point to the string. if end == next, the string will not be
1383 * null-terminated. In all other cases it will be.
1384 * end = pointer to char behind end of string, next = pointer to start of
1386 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1389 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next)
1399 ret = gst_structure_parse_simple_string (s, end);
1428 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1431 GValue value1 = { 0 };
1432 GValue value2 = { 0 };
1441 ret = gst_structure_parse_value (s, &s, &value1, type);
1445 while (g_ascii_isspace (*s))
1452 while (g_ascii_isspace (*s))
1455 ret = gst_structure_parse_value (s, &s, &value2, type);
1459 while (g_ascii_isspace (*s))
1466 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1469 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1470 range_type = GST_TYPE_DOUBLE_RANGE;
1471 g_value_init (value, range_type);
1472 gst_value_set_double_range (value, g_value_get_double (&value1),
1473 g_value_get_double (&value2));
1474 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1475 range_type = GST_TYPE_INT_RANGE;
1476 g_value_init (value, range_type);
1477 gst_value_set_int_range (value, g_value_get_int (&value1),
1478 g_value_get_int (&value2));
1479 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1480 range_type = GST_TYPE_FRACTION_RANGE;
1481 g_value_init (value, range_type);
1482 gst_value_set_fraction_range (value, &value1, &value2);
1492 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1493 GType type, GType list_type, char begin, char end)
1495 GValue list_value = { 0 };
1499 g_value_init (value, list_type);
1500 array = g_value_peek_pointer (value);
1506 while (g_ascii_isspace (*s))
1514 ret = gst_structure_parse_value (s, &s, &list_value, type);
1518 g_array_append_val (array, list_value);
1520 while (g_ascii_isspace (*s))
1528 while (g_ascii_isspace (*s))
1531 memset (&list_value, 0, sizeof (list_value));
1532 ret = gst_structure_parse_value (s, &s, &list_value, type);
1536 g_array_append_val (array, list_value);
1537 while (g_ascii_isspace (*s))
1548 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
1550 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
1555 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
1558 return gst_structure_parse_any_list (s, after, value, type,
1559 GST_TYPE_ARRAY, '<', '>');
1563 gst_structure_parse_simple_string (gchar * str, gchar ** end)
1567 while (GST_ASCII_IS_STRING (*s)) {
1577 gst_structure_parse_field (gchar * str,
1578 gchar ** after, GstStructureField * field)
1587 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1590 if (!gst_structure_parse_simple_string (s, &name_end))
1594 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1603 field->name = g_quark_from_string (name);
1606 if (!gst_structure_parse_value (s, &s, &field->value, G_TYPE_INVALID))
1614 gst_structure_parse_value (gchar * str,
1615 gchar ** after, GValue * value, GType default_type)
1624 GType type = default_type;
1628 while (g_ascii_isspace (*s))
1633 type = G_TYPE_INVALID;
1636 while (g_ascii_isspace (*s))
1639 if (!gst_structure_parse_simple_string (s, &type_end))
1642 while (g_ascii_isspace (*s))
1647 while (g_ascii_isspace (*s))
1652 type = gst_structure_from_abbr (type_name);
1655 if (type == G_TYPE_INVALID)
1659 while (g_ascii_isspace (*s))
1662 ret = gst_structure_parse_range (s, &s, value, type);
1663 } else if (*s == '{') {
1664 ret = gst_structure_parse_list (s, &s, value, type);
1665 } else if (*s == '<') {
1666 ret = gst_structure_parse_array (s, &s, value, type);
1669 if (!gst_structure_parse_string (s, &value_end, &s))
1674 if (type == G_TYPE_INVALID) {
1676 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_STRING };
1679 for (i = 0; i < 4; i++) {
1680 g_value_init (value, try_types[i]);
1681 ret = gst_value_deserialize (value, value_s);
1684 g_value_unset (value);
1687 g_value_init (value, type);
1689 ret = gst_value_deserialize (value, value_s);
1700 * gst_structure_from_string:
1701 * @string: a string representation of a #GstStructure.
1702 * @end: pointer to store the end of the string in.
1704 * Creates a #GstStructure from a string representation.
1705 * If end is not NULL, a pointer to the place inside the given string
1706 * where parsing ended will be returned.
1708 * Returns: a new #GstStructure or NULL when the string could not
1709 * be parsed. Free after usage.
1712 gst_structure_from_string (const gchar * string, gchar ** end)
1719 GstStructure *structure = NULL;
1720 GstStructureField field = { 0 };
1722 g_return_val_if_fail (string != NULL, NULL);
1724 copy = g_strdup (string);
1728 if (!gst_structure_parse_string (r, &w, &r))
1731 while (g_ascii_isspace (*r) || (r[0] == '\\' && g_ascii_isspace (r[1])))
1733 if (*r != 0 && *r != ';' && *r != ',')
1738 structure = gst_structure_empty_new (name);
1741 while (*r && (*r != ';')) {
1745 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1746 && g_ascii_isspace (r[1]))))
1749 memset (&field, 0, sizeof (field));
1750 if (!gst_structure_parse_field (r, &r, &field))
1752 gst_structure_set_field (structure, &field);
1753 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1754 && g_ascii_isspace (r[1]))))
1759 *end = (char *) string + (r - copy);
1766 gst_structure_free (structure);
1772 gst_structure_transform_to_string (const GValue * src_value,
1773 GValue * dest_value)
1775 g_return_if_fail (src_value != NULL);
1776 g_return_if_fail (dest_value != NULL);
1778 dest_value->data[0].v_pointer =
1779 gst_structure_to_string (src_value->data[0].v_pointer);
1782 static GstStructure *
1783 gst_structure_copy_conditional (const GstStructure * structure)
1786 return gst_structure_copy (structure);
1790 /* fixate utility functions */
1793 * gst_structure_fixate_field_nearest_int:
1794 * @structure: a #GstStructure
1795 * @field_name: a field in @structure
1796 * @target: the target value of the fixation
1798 * Fixates a #GstStructure by changing the given field to the nearest
1799 * integer to @target that is a subset of the existing field.
1801 * Returns: TRUE if the structure could be fixated
1804 gst_structure_fixate_field_nearest_int (GstStructure * structure,
1805 const char *field_name, int target)
1807 const GValue *value;
1809 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1810 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1812 value = gst_structure_get_value (structure, field_name);
1814 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
1817 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
1820 x = gst_value_get_int_range_min (value);
1823 x = gst_value_get_int_range_max (value);
1826 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
1828 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1829 const GValue *list_value;
1832 int best_index = -1;
1834 n = gst_value_list_get_size (value);
1835 for (i = 0; i < n; i++) {
1836 list_value = gst_value_list_get_value (value, i);
1837 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
1838 int x = g_value_get_int (list_value);
1840 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
1846 if (best_index != -1) {
1847 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
1857 * gst_structure_fixate_field_nearest_double:
1858 * @structure: a #GstStructure
1859 * @field_name: a field in @structure
1860 * @target: the target value of the fixation
1862 * Fixates a #GstStructure by changing the given field to the nearest
1863 * double to @target that is a subset of the existing field.
1865 * Returns: TRUE if the structure could be fixated
1868 gst_structure_fixate_field_nearest_double (GstStructure * structure,
1869 const char *field_name, double target)
1871 const GValue *value;
1873 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1874 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1876 value = gst_structure_get_value (structure, field_name);
1878 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
1881 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
1884 x = gst_value_get_double_range_min (value);
1887 x = gst_value_get_double_range_max (value);
1890 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
1892 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1893 const GValue *list_value;
1896 int best_index = -1;
1898 n = gst_value_list_get_size (value);
1899 for (i = 0; i < n; i++) {
1900 list_value = gst_value_list_get_value (value, i);
1901 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
1902 double x = g_value_get_double (list_value);
1904 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
1910 if (best_index != -1) {
1911 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
1922 * gst_structure_fixate_field_boolean:
1923 * @structure: a #GstStructure
1924 * @field_name: a field in @structure
1925 * @target: the target value of the fixation
1927 * Fixates a #GstStructure by changing the given @field_name field to the given
1928 * @target boolean if that field is not fixed yet.
1930 * Returns: TRUE if the structure could be fixated
1933 gst_structure_fixate_field_boolean (GstStructure * structure,
1934 const char *field_name, gboolean target)
1936 const GValue *value;
1938 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1939 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1941 value = gst_structure_get_value (structure, field_name);
1943 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
1946 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1947 const GValue *list_value;
1950 int best_index = -1;
1952 n = gst_value_list_get_size (value);
1953 for (i = 0; i < n; i++) {
1954 list_value = gst_value_list_get_value (value, i);
1955 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
1956 gboolean x = g_value_get_boolean (list_value);
1958 if (best_index == -1 || x == target) {
1964 if (best_index != -1) {
1965 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
1975 * gst_structure_fixate_field_nearest_fraction:
1976 * @structure: a #GstStructure
1977 * @field_name: a field in @structure
1978 * @target_numerator: The numerator of the target value of the fixation
1979 * @target_denominator: The denominator of the target value of the fixation
1981 * Fixates a #GstStructure by changing the given field to the nearest
1982 * fraction to @target_numerator/@target_denominator that is a subset
1983 * of the existing field.
1985 * Returns: TRUE if the structure could be fixated
1988 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
1989 const char *field_name, const gint target_numerator,
1990 const gint target_denominator)
1992 const GValue *value;
1994 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1995 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1997 value = gst_structure_get_value (structure, field_name);
1999 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2002 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2003 const GValue *x, *new_value;
2004 GValue target = { 0 };
2005 g_value_init (&target, GST_TYPE_FRACTION);
2006 gst_value_set_fraction (&target, target_numerator, target_denominator);
2008 new_value = ⌖
2009 x = gst_value_get_fraction_range_min (value);
2010 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2012 x = gst_value_get_fraction_range_max (value);
2013 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2016 gst_structure_set_value (structure, field_name, new_value);
2017 g_value_unset (&target);
2019 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2020 const GValue *list_value;
2022 const GValue *best = NULL;
2023 GValue best_diff = { 0 };
2024 GValue cur_diff = { 0 };
2025 GValue target = { 0 };
2026 gboolean res = FALSE;
2028 g_value_init (&best_diff, GST_TYPE_FRACTION);
2029 g_value_init (&cur_diff, GST_TYPE_FRACTION);
2030 g_value_init (&target, GST_TYPE_FRACTION);
2032 gst_value_set_fraction (&target, target_numerator, target_denominator);
2034 n = gst_value_list_get_size (value);
2035 for (i = 0; i < n; i++) {
2036 list_value = gst_value_list_get_value (value, i);
2037 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2040 gst_value_set_fraction (&best_diff, 0, 1);
2042 if (gst_value_compare (list_value, &target) == GST_VALUE_LESS_THAN)
2043 gst_value_fraction_subtract (&cur_diff, &target, list_value);
2045 gst_value_fraction_subtract (&cur_diff, list_value, &target);
2047 if (gst_value_compare (&cur_diff, &best_diff) == GST_VALUE_LESS_THAN) {
2049 g_value_copy (&cur_diff, &best_diff);
2055 gst_structure_set_value (structure, field_name, best);
2058 g_value_unset (&best_diff);
2059 g_value_unset (&cur_diff);
2060 g_value_unset (&target);