gststructure: Fix some memory leaks. Sprinkle G_LIKELY/UNLIKELY
[platform/upstream/gstreamer.git] / gst / gststructure.c
1 /* GStreamer
2  * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
3  *
4  * gststructure.c: lists of { GQuark, GValue } tuples
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:gststructure
24  * @short_description: Generic structure containing fields of names and values
25  * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
26  *
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.
29  *
30  * In addition to the key/value pairs, a #GstStructure also has a name. The name
31  * starts with a letter and can be folled by letters, numbers and any of "/-_.:".
32  * 
33  * #GstStructure is used by various GStreamer subsystems to store information
34  * in a flexible and extensible way. A #GstStructure does not have a refcount
35  * because it usually is part of a higher level object such as #GstCaps. It
36  * provides a means to enforce mutability using the refcount of the parent
37  * with the gst_structure_set_parent_refcount() method.
38  *
39  * A #GstStructure can be created with gst_structure_empty_new() or
40  * gst_structure_new(), which both take a name and an optional set of
41  * key/value pairs along with the types of the values.
42  * 
43  * Field values can be changed with gst_structure_set_value() or
44  * gst_structure_set().
45  *
46  * Field values can be retrieved with gst_structure_get_value() or the more
47  * convenient gst_structure_get_*() functions.
48  *
49  * Fields can be removed with gst_structure_remove_field() or
50  * gst_structure_remove_fields().
51  *
52  * Last reviewed on 2007-10-16 (0.10.15)
53  */
54
55 #ifdef HAVE_CONFIG_H
56 #include "config.h"
57 #endif
58
59 #include <string.h>
60
61 #include "gst_private.h"
62 #include "gstquark.h"
63 #include <gst/gst.h>
64 #include <gobject/gvaluecollector.h>
65
66 typedef struct _GstStructureField GstStructureField;
67
68 struct _GstStructureField
69 {
70   GQuark name;
71   GValue value;
72 };
73
74 #define GST_STRUCTURE_FIELD(structure, index) \
75     &g_array_index((structure)->fields, GstStructureField, (index))
76
77 #define IS_MUTABLE(structure) \
78     (!(structure)->parent_refcount || \
79      g_atomic_int_get ((structure)->parent_refcount) == 1)
80
81 #define IS_TAGLIST(structure) \
82     (structure->name == GST_QUARK (TAGLIST))
83
84 static void gst_structure_set_field (GstStructure * structure,
85     GstStructureField * field);
86 static GstStructureField *gst_structure_get_field (const GstStructure *
87     structure, const gchar * fieldname);
88 static GstStructureField *gst_structure_id_get_field (const GstStructure *
89     structure, GQuark field);
90 static void gst_structure_transform_to_string (const GValue * src_value,
91     GValue * dest_value);
92 static GstStructure *gst_structure_copy_conditional (const GstStructure *
93     structure);
94 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
95     GValue * value, GType default_type);
96 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
97
98 GType
99 gst_structure_get_type (void)
100 {
101   static GType gst_structure_type = 0;
102
103   if (G_UNLIKELY (gst_structure_type == 0)) {
104     gst_structure_type = g_boxed_type_register_static ("GstStructure",
105         (GBoxedCopyFunc) gst_structure_copy_conditional,
106         (GBoxedFreeFunc) gst_structure_free);
107
108     g_value_register_transform_func (gst_structure_type, G_TYPE_STRING,
109         gst_structure_transform_to_string);
110   }
111
112   return gst_structure_type;
113 }
114
115 static GstStructure *
116 gst_structure_id_empty_new_with_size (GQuark quark, guint prealloc)
117 {
118   GstStructure *structure;
119
120   structure = g_slice_new (GstStructure);
121   structure->type = gst_structure_get_type ();
122   structure->name = quark;
123   structure->parent_refcount = NULL;
124   structure->fields =
125       g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
126
127   return structure;
128 }
129
130 /**
131  * gst_structure_id_empty_new:
132  * @quark: name of new structure
133  *
134  * Creates a new, empty #GstStructure with the given name as a GQuark.
135  *
136  * Returns: a new, empty #GstStructure
137  */
138 GstStructure *
139 gst_structure_id_empty_new (GQuark quark)
140 {
141   g_return_val_if_fail (quark != 0, NULL);
142
143   return gst_structure_id_empty_new_with_size (quark, 0);
144 }
145
146 #ifndef G_DISABLE_CHECKS
147 static gboolean
148 gst_structure_validate_name (const gchar * name)
149 {
150   const gchar *s;
151
152   g_return_val_if_fail (name != NULL, FALSE);
153
154   /* FIXME 0.11: use g_ascii_isalpha() */
155   if (!g_ascii_isalnum (*name)) {
156     GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
157         *name, name);
158     return FALSE;
159   }
160
161   /* FIXME 0.11: don't allow spaces */
162   /* FIXME: test name string more */
163   s = &name[1];
164   while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+ ", *s) != NULL))
165     s++;
166   if (*s != '\0') {
167     GST_WARNING ("Invalid character '%c' at offset %lu in structure name: %s",
168         *s, ((gulong) s - (gulong) name), name);
169     return FALSE;
170   }
171
172   return TRUE;
173 }
174 #endif
175
176 /**
177  * gst_structure_empty_new:
178  * @name: name of new structure
179  *
180  * Creates a new, empty #GstStructure with the given @name.
181  *
182  * See gst_structure_set_name() for constraints on the @name parameter.
183  *
184  * Returns: a new, empty #GstStructure
185  */
186 GstStructure *
187 gst_structure_empty_new (const gchar * name)
188 {
189   g_return_val_if_fail (gst_structure_validate_name (name), NULL);
190
191   return gst_structure_id_empty_new_with_size (g_quark_from_string (name), 0);
192 }
193
194 /**
195  * gst_structure_new:
196  * @name: name of new structure
197  * @firstfield: name of first field to set
198  * @...: additional arguments
199  *
200  * Creates a new #GstStructure with the given name.  Parses the
201  * list of variable arguments and sets fields to the values listed.
202  * Variable arguments should be passed as field name, field type,
203  * and value.  Last variable argument should be NULL.
204  *
205  * Returns: a new #GstStructure
206  */
207 GstStructure *
208 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
209 {
210   GstStructure *structure;
211   va_list varargs;
212
213   g_return_val_if_fail (name != NULL, NULL);
214
215   va_start (varargs, firstfield);
216
217   structure = gst_structure_new_valist (name, firstfield, varargs);
218
219   va_end (varargs);
220
221   return structure;
222 }
223
224 /**
225  * gst_structure_new_valist:
226  * @name: name of new structure
227  * @firstfield: name of first field to set
228  * @varargs: variable argument list
229  *
230  * Creates a new #GstStructure with the given @name.  Structure fields
231  * are set according to the varargs in a manner similar to
232  * gst_structure_new().
233  *
234  * See gst_structure_set_name() for constraints on the @name parameter.
235  *
236  * Returns: a new #GstStructure
237  */
238 GstStructure *
239 gst_structure_new_valist (const gchar * name,
240     const gchar * firstfield, va_list varargs)
241 {
242   GstStructure *structure;
243
244   g_return_val_if_fail (name != NULL, NULL);
245
246   structure = gst_structure_empty_new (name);
247
248   if (structure)
249     gst_structure_set_valist (structure, firstfield, varargs);
250
251   return structure;
252 }
253
254 /**
255  * gst_structure_set_parent_refcount:
256  * @structure: a #GstStructure
257  * @refcount: a pointer to the parent's refcount
258  *
259  * Sets the parent_refcount field of #GstStructure. This field is used to
260  * determine whether a structure is mutable or not. This function should only be
261  * called by code implementing parent objects of #GstStructure, as described in
262  * the MT Refcounting section of the design documents.
263  */
264 void
265 gst_structure_set_parent_refcount (GstStructure * structure, int *refcount)
266 {
267   g_return_if_fail (structure != NULL);
268
269   /* if we have a parent_refcount already, we can only clear
270    * if with a NULL refcount */
271   if (structure->parent_refcount)
272     g_return_if_fail (refcount == NULL);
273   else
274     g_return_if_fail (refcount != NULL);
275
276   structure->parent_refcount = refcount;
277 }
278
279 /**
280  * gst_structure_copy:
281  * @structure: a #GstStructure to duplicate
282  *
283  * Duplicates a #GstStructure and all its fields and values.
284  *
285  * Returns: a new #GstStructure.
286  */
287 GstStructure *
288 gst_structure_copy (const GstStructure * structure)
289 {
290   GstStructure *new_structure;
291   GstStructureField *field;
292   guint i;
293
294   g_return_val_if_fail (structure != NULL, NULL);
295
296   new_structure =
297       gst_structure_id_empty_new_with_size (structure->name,
298       structure->fields->len);
299
300   for (i = 0; i < structure->fields->len; i++) {
301     GstStructureField new_field = { 0 };
302
303     field = GST_STRUCTURE_FIELD (structure, i);
304
305     new_field.name = field->name;
306     gst_value_init_and_copy (&new_field.value, &field->value);
307     g_array_append_val (new_structure->fields, new_field);
308   }
309
310   return new_structure;
311 }
312
313 /**
314  * gst_structure_free:
315  * @structure: the #GstStructure to free
316  *
317  * Frees a #GstStructure and all its fields and values. The structure must not
318  * have a parent when this function is called.
319  */
320 void
321 gst_structure_free (GstStructure * structure)
322 {
323   GstStructureField *field;
324   guint i;
325
326   g_return_if_fail (structure != NULL);
327   g_return_if_fail (structure->parent_refcount == NULL);
328
329   for (i = 0; i < structure->fields->len; i++) {
330     field = GST_STRUCTURE_FIELD (structure, i);
331
332     if (G_IS_VALUE (&field->value)) {
333       g_value_unset (&field->value);
334     }
335   }
336   g_array_free (structure->fields, TRUE);
337 #ifdef USE_POISONING
338   memset (structure, 0xff, sizeof (GstStructure));
339 #endif
340   g_slice_free (GstStructure, structure);
341 }
342
343 /**
344  * gst_structure_get_name:
345  * @structure: a #GstStructure
346  *
347  * Get the name of @structure as a string.
348  *
349  * Returns: the name of the structure.
350  */
351 const gchar *
352 gst_structure_get_name (const GstStructure * structure)
353 {
354   g_return_val_if_fail (structure != NULL, NULL);
355
356   return g_quark_to_string (structure->name);
357 }
358
359 /**
360  * gst_structure_has_name:
361  * @structure: a #GstStructure
362  * @name: structure name to check for
363  *
364  * Checks if the structure has the given name
365  *
366  * Returns: TRUE if @name matches the name of the structure.
367  */
368 gboolean
369 gst_structure_has_name (const GstStructure * structure, const gchar * name)
370 {
371   const gchar *structure_name;
372
373   g_return_val_if_fail (structure != NULL, FALSE);
374   g_return_val_if_fail (name != NULL, FALSE);
375
376   /* getting the string is cheap and comparing short strings is too
377    * should be faster than getting the quark for name and comparing the quarks
378    */
379   structure_name = g_quark_to_string (structure->name);
380
381   return (structure_name && strcmp (structure_name, name) == 0);
382 }
383
384 /**
385  * gst_structure_get_name_id:
386  * @structure: a #GstStructure
387  *
388  * Get the name of @structure as a GQuark.
389  *
390  * Returns: the quark representing the name of the structure.
391  */
392 GQuark
393 gst_structure_get_name_id (const GstStructure * structure)
394 {
395   g_return_val_if_fail (structure != NULL, 0);
396
397   return structure->name;
398 }
399
400 /**
401  * gst_structure_set_name:
402  * @structure: a #GstStructure
403  * @name: the new name of the structure
404  *
405  * Sets the name of the structure to the given @name.  The string
406  * provided is copied before being used. It must not be empty, start with a
407  * letter and can be followed by letters, numbers and any of "/-_.:".
408  */
409 void
410 gst_structure_set_name (GstStructure * structure, const gchar * name)
411 {
412   g_return_if_fail (structure != NULL);
413   g_return_if_fail (IS_MUTABLE (structure));
414   g_return_if_fail (gst_structure_validate_name (name));
415
416   structure->name = g_quark_from_string (name);
417 }
418
419 /**
420  * gst_structure_id_set_value:
421  * @structure: a #GstStructure
422  * @field: a #GQuark representing a field
423  * @value: the new value of the field
424  *
425  * Sets the field with the given GQuark @field to @value.  If the field
426  * does not exist, it is created.  If the field exists, the previous
427  * value is replaced and freed.
428  */
429 void
430 gst_structure_id_set_value (GstStructure * structure,
431     GQuark field, const GValue * value)
432 {
433   GstStructureField gsfield = { 0, {0,} };
434
435   g_return_if_fail (structure != NULL);
436   g_return_if_fail (G_IS_VALUE (value));
437   g_return_if_fail (IS_MUTABLE (structure));
438
439   gsfield.name = field;
440   gst_value_init_and_copy (&gsfield.value, value);
441
442   gst_structure_set_field (structure, &gsfield);
443 }
444
445 /**
446  * gst_structure_set_value:
447  * @structure: a #GstStructure
448  * @fieldname: the name of the field to set
449  * @value: the new value of the field
450  *
451  * Sets the field with the given name @field to @value.  If the field
452  * does not exist, it is created.  If the field exists, the previous
453  * value is replaced and freed.
454  */
455 void
456 gst_structure_set_value (GstStructure * structure,
457     const gchar * fieldname, const GValue * value)
458 {
459   g_return_if_fail (structure != NULL);
460   g_return_if_fail (fieldname != NULL);
461   g_return_if_fail (G_IS_VALUE (value));
462   g_return_if_fail (IS_MUTABLE (structure));
463
464   gst_structure_id_set_value (structure, g_quark_from_string (fieldname),
465       value);
466 }
467
468 /**
469  * gst_structure_set:
470  * @structure: a #GstStructure
471  * @fieldname: the name of the field to set
472  * @...: variable arguments
473  *
474  * Parses the variable arguments and sets fields accordingly.
475  * Variable arguments should be in the form field name, field type
476  * (as a GType), value(s).  The last variable argument should be NULL.
477  */
478 void
479 gst_structure_set (GstStructure * structure, const gchar * field, ...)
480 {
481   va_list varargs;
482
483   g_return_if_fail (structure != NULL);
484
485   va_start (varargs, field);
486
487   gst_structure_set_valist (structure, field, varargs);
488
489   va_end (varargs);
490 }
491
492 /**
493  * gst_structure_set_valist:
494  * @structure: a #GstStructure
495  * @fieldname: the name of the field to set
496  * @varargs: variable arguments
497  *
498  * va_list form of gst_structure_set().
499  */
500 void
501 gst_structure_set_valist (GstStructure * structure,
502     const gchar * fieldname, va_list varargs)
503 {
504   gchar *err = NULL;
505   GType type;
506
507   g_return_if_fail (structure != NULL);
508   g_return_if_fail (IS_MUTABLE (structure));
509
510   while (fieldname) {
511     GstStructureField field = { 0 };
512
513     field.name = g_quark_from_string (fieldname);
514
515     type = va_arg (varargs, GType);
516
517     if (type == G_TYPE_DATE) {
518       g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
519       type = GST_TYPE_DATE;
520     }
521
522     g_value_init (&field.value, type);
523     G_VALUE_COLLECT (&field.value, varargs, 0, &err);
524     if (err) {
525       g_critical ("%s", err);
526       return;
527     }
528     gst_structure_set_field (structure, &field);
529
530     fieldname = va_arg (varargs, gchar *);
531   }
532 }
533
534 /**
535  * gst_structure_id_set:
536  * @structure: a #GstStructure
537  * @fieldname: the GQuark for the name of the field to set
538  * @...: variable arguments
539  *
540  * Identical to gst_structure_set, except that field names are
541  * passed using the GQuark for the field name. This allows more efficient
542  * setting of the structure if the caller already knows the associated
543  * quark values.
544  * The last variable argument must be NULL.
545  *
546  * Since: 0.10.10
547  */
548 void
549 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
550 {
551   va_list varargs;
552
553   g_return_if_fail (structure != NULL);
554
555   va_start (varargs, field);
556   gst_structure_id_set_valist (structure, field, varargs);
557   va_end (varargs);
558 }
559
560 /**
561  * gst_structure_id_set_valist:
562  * @structure: a #GstStructure
563  * @fieldname: the name of the field to set
564  * @varargs: variable arguments
565  *
566  * va_list form of gst_structure_id_set().
567  *
568  * Since: 0.10.10
569  */
570 void
571 gst_structure_id_set_valist (GstStructure * structure,
572     GQuark fieldname, va_list varargs)
573 {
574   gchar *err = NULL;
575   GType type;
576
577   g_return_if_fail (structure != NULL);
578   g_return_if_fail (IS_MUTABLE (structure));
579
580   while (fieldname) {
581     GstStructureField field = { 0 };
582
583     field.name = fieldname;
584
585     type = va_arg (varargs, GType);
586
587     if (type == G_TYPE_DATE) {
588       g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
589       type = GST_TYPE_DATE;
590     }
591
592     g_value_init (&field.value, type);
593     G_VALUE_COLLECT (&field.value, varargs, 0, &err);
594     if (err) {
595       g_critical ("%s", err);
596       return;
597     }
598     gst_structure_set_field (structure, &field);
599
600     fieldname = va_arg (varargs, GQuark);
601   }
602 }
603
604 /**
605  * gst_structure_id_new:
606  * @name_quark: name of new structure
607  * @field_quark: the GQuark for the name of the field to set
608  * @...: variable arguments
609  *
610  * Creates a new #GstStructure with the given name as a GQuark, followed by
611  * fieldname quark, GType, argument(s) "triplets" in the same format as
612  * gst_structure_id_set(). Basically a convenience wrapper around
613  * gst_structure_id_empty_new() and gst_structure_id_set().
614  *
615  * The last variable argument must be NULL (or 0).
616  *
617  * Returns: a new #GstStructure
618  *
619  * Since: 0.10.24
620  */
621 GstStructure *
622 gst_structure_id_new (GQuark name_quark, GQuark field_quark, ...)
623 {
624   GstStructure *s;
625   va_list varargs;
626
627   g_return_val_if_fail (name_quark != 0, NULL);
628   g_return_val_if_fail (field_quark != 0, NULL);
629
630   s = gst_structure_id_empty_new (name_quark);
631
632   va_start (varargs, field_quark);
633   gst_structure_id_set_valist (s, field_quark, varargs);
634   va_end (varargs);
635
636   return s;
637 }
638
639 /* If the structure currently contains a field with the same name, it is
640  * replaced with the provided field. Otherwise, the field is added to the
641  * structure. The field's value is not deeply copied.
642  */
643 static void
644 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
645 {
646   GstStructureField *f;
647   guint i;
648
649   if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
650     const gchar *s;
651
652     s = g_value_get_string (&field->value);
653     /* only check for NULL strings in taglists, as they are allowed in message
654      * structs, e.g. error message debug strings */
655     if (G_UNLIKELY (s == NULL && IS_TAGLIST (structure))) {
656       g_warning ("Trying to set NULL string on field '%s' on taglist. "
657           "Please file a bug.", g_quark_to_string (field->name));
658       g_value_unset (&field->value);
659       return;
660     } else if (G_UNLIKELY (s != NULL && *s == '\0')) {
661       /* empty strings never make sense */
662       g_warning ("Trying to set empty string on %s field '%s'. Please file a "
663           "bug.", IS_TAGLIST (structure) ? "taglist" : "structure",
664           g_quark_to_string (field->name));
665       g_value_unset (&field->value);
666       return;
667     } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
668       g_warning ("Trying to set string on %s field '%s', but string is not "
669           "valid UTF-8. Please file a bug.",
670           IS_TAGLIST (structure) ? "taglist" : "structure",
671           g_quark_to_string (field->name));
672       g_value_unset (&field->value);
673       return;
674     }
675   }
676
677   for (i = 0; i < structure->fields->len; i++) {
678     f = GST_STRUCTURE_FIELD (structure, i);
679
680     if (f->name == field->name) {
681       g_value_unset (&f->value);
682       memcpy (f, field, sizeof (GstStructureField));
683       return;
684     }
685   }
686
687   g_array_append_val (structure->fields, *field);
688 }
689
690 /* If there is no field with the given ID, NULL is returned.
691  */
692 static GstStructureField *
693 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
694 {
695   GstStructureField *field;
696   guint i;
697
698   g_return_val_if_fail (structure != NULL, NULL);
699
700   for (i = 0; i < structure->fields->len; i++) {
701     field = GST_STRUCTURE_FIELD (structure, i);
702
703     if (field->name == field_id)
704       return field;
705   }
706
707   return NULL;
708 }
709
710 /* If there is no field with the given ID, NULL is returned.
711  */
712 static GstStructureField *
713 gst_structure_get_field (const GstStructure * structure,
714     const gchar * fieldname)
715 {
716   g_return_val_if_fail (structure != NULL, NULL);
717   g_return_val_if_fail (fieldname != NULL, NULL);
718
719   return gst_structure_id_get_field (structure,
720       g_quark_from_string (fieldname));
721 }
722
723 /**
724  * gst_structure_get_value:
725  * @structure: a #GstStructure
726  * @fieldname: the name of the field to get
727  *
728  * Get the value of the field with name @fieldname.
729  *
730  * Returns: the #GValue corresponding to the field with the given name.
731  */
732 const GValue *
733 gst_structure_get_value (const GstStructure * structure,
734     const gchar * fieldname)
735 {
736   GstStructureField *field;
737
738   g_return_val_if_fail (structure != NULL, NULL);
739   g_return_val_if_fail (fieldname != NULL, NULL);
740
741   field = gst_structure_get_field (structure, fieldname);
742   if (field == NULL)
743     return NULL;
744
745   return &field->value;
746 }
747
748 /**
749  * gst_structure_id_get_value:
750  * @structure: a #GstStructure
751  * @field: the #GQuark of the field to get
752  *
753  * Get the value of the field with GQuark @field.
754  *
755  * Returns: the #GValue corresponding to the field with the given name
756  *          identifier.
757  */
758 const GValue *
759 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
760 {
761   GstStructureField *gsfield;
762
763   g_return_val_if_fail (structure != NULL, NULL);
764
765   gsfield = gst_structure_id_get_field (structure, field);
766   if (gsfield == NULL)
767     return NULL;
768
769   return &gsfield->value;
770 }
771
772 /**
773  * gst_structure_remove_field:
774  * @structure: a #GstStructure
775  * @fieldname: the name of the field to remove
776  *
777  * Removes the field with the given name.  If the field with the given
778  * name does not exist, the structure is unchanged.
779  */
780 void
781 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
782 {
783   GstStructureField *field;
784   GQuark id;
785   guint i;
786
787   g_return_if_fail (structure != NULL);
788   g_return_if_fail (fieldname != NULL);
789   g_return_if_fail (IS_MUTABLE (structure));
790
791   id = g_quark_from_string (fieldname);
792
793   for (i = 0; i < structure->fields->len; i++) {
794     field = GST_STRUCTURE_FIELD (structure, i);
795
796     if (field->name == id) {
797       if (G_IS_VALUE (&field->value)) {
798         g_value_unset (&field->value);
799       }
800       structure->fields = g_array_remove_index (structure->fields, i);
801       return;
802     }
803   }
804 }
805
806 /**
807  * gst_structure_remove_fields:
808  * @structure: a #GstStructure
809  * @fieldname: the name of the field to remove
810  * @...: NULL-terminated list of more fieldnames to remove
811  *
812  * Removes the fields with the given names. If a field does not exist, the
813  * argument is ignored.
814  */
815 void
816 gst_structure_remove_fields (GstStructure * structure,
817     const gchar * fieldname, ...)
818 {
819   va_list varargs;
820
821   g_return_if_fail (structure != NULL);
822   g_return_if_fail (fieldname != NULL);
823   /* mutability checked in remove_field */
824
825   va_start (varargs, fieldname);
826
827   gst_structure_remove_fields_valist (structure, fieldname, varargs);
828
829   va_end (varargs);
830 }
831
832 /**
833  * gst_structure_remove_fields_valist:
834  * @structure: a #GstStructure
835  * @fieldname: the name of the field to remove
836  * @varargs: NULL-terminated list of more fieldnames to remove
837  *
838  * va_list form of gst_structure_remove_fields().
839  */
840 void
841 gst_structure_remove_fields_valist (GstStructure * structure,
842     const gchar * fieldname, va_list varargs)
843 {
844   gchar *field = (gchar *) fieldname;
845
846   g_return_if_fail (structure != NULL);
847   g_return_if_fail (fieldname != NULL);
848   /* mutability checked in remove_field */
849
850   while (field) {
851     gst_structure_remove_field (structure, field);
852     field = va_arg (varargs, char *);
853   }
854 }
855
856 /**
857  * gst_structure_remove_all_fields:
858  * @structure: a #GstStructure
859  *
860  * Removes all fields in a GstStructure.
861  */
862 void
863 gst_structure_remove_all_fields (GstStructure * structure)
864 {
865   GstStructureField *field;
866   int i;
867
868   g_return_if_fail (structure != NULL);
869   g_return_if_fail (IS_MUTABLE (structure));
870
871   for (i = structure->fields->len - 1; i >= 0; i--) {
872     field = GST_STRUCTURE_FIELD (structure, i);
873
874     if (G_IS_VALUE (&field->value)) {
875       g_value_unset (&field->value);
876     }
877     structure->fields = g_array_remove_index (structure->fields, i);
878   }
879 }
880
881 /**
882  * gst_structure_get_field_type:
883  * @structure: a #GstStructure
884  * @fieldname: the name of the field
885  *
886  * Finds the field with the given name, and returns the type of the
887  * value it contains.  If the field is not found, G_TYPE_INVALID is
888  * returned.
889  *
890  * Returns: the #GValue of the field
891  */
892 GType
893 gst_structure_get_field_type (const GstStructure * structure,
894     const gchar * fieldname)
895 {
896   GstStructureField *field;
897
898   g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
899   g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
900
901   field = gst_structure_get_field (structure, fieldname);
902   if (field == NULL)
903     return G_TYPE_INVALID;
904
905   return G_VALUE_TYPE (&field->value);
906 }
907
908 /**
909  * gst_structure_n_fields:
910  * @structure: a #GstStructure
911  *
912  * Get the number of fields in the structure.
913  *
914  * Returns: the number of fields in the structure
915  */
916 gint
917 gst_structure_n_fields (const GstStructure * structure)
918 {
919   g_return_val_if_fail (structure != NULL, 0);
920
921   return structure->fields->len;
922 }
923
924 /**
925  * gst_structure_nth_field_name:
926  * @structure: a #GstStructure
927  * @index: the index to get the name of
928  *
929  * Get the name of the given field number, counting from 0 onwards.
930  *
931  * Returns: the name of the given field number
932  */
933 const gchar *
934 gst_structure_nth_field_name (const GstStructure * structure, guint index)
935 {
936   GstStructureField *field;
937
938   g_return_val_if_fail (structure != NULL, NULL);
939   g_return_val_if_fail (index < structure->fields->len, NULL);
940
941   field = GST_STRUCTURE_FIELD (structure, index);
942
943   return g_quark_to_string (field->name);
944 }
945
946 /**
947  * gst_structure_foreach:
948  * @structure: a #GstStructure
949  * @func: a function to call for each field
950  * @user_data: private data
951  *
952  * Calls the provided function once for each field in the #GstStructure. The
953  * function must not modify the fields. Also see gst_structure_map_in_place().
954  *
955  * Returns: TRUE if the supplied function returns TRUE For each of the fields,
956  * FALSE otherwise.
957  */
958 gboolean
959 gst_structure_foreach (const GstStructure * structure,
960     GstStructureForeachFunc func, gpointer user_data)
961 {
962   guint i;
963   GstStructureField *field;
964   gboolean ret;
965
966   g_return_val_if_fail (structure != NULL, FALSE);
967   g_return_val_if_fail (func != NULL, FALSE);
968
969   for (i = 0; i < structure->fields->len; i++) {
970     field = GST_STRUCTURE_FIELD (structure, i);
971
972     ret = func (field->name, &field->value, user_data);
973     if (!ret)
974       return FALSE;
975   }
976
977   return TRUE;
978 }
979
980 /**
981  * gst_structure_map_in_place:
982  * @structure: a #GstStructure
983  * @func: a function to call for each field
984  * @user_data: private data
985  *
986  * Calls the provided function once for each field in the #GstStructure. In
987  * contrast to gst_structure_foreach(), the function may modify but not delete the
988  * fields. The structure must be mutable.
989  *
990  * Returns: TRUE if the supplied function returns TRUE For each of the fields,
991  * FALSE otherwise.
992  */
993 gboolean
994 gst_structure_map_in_place (GstStructure * structure,
995     GstStructureMapFunc func, gpointer user_data)
996 {
997   guint i;
998   GstStructureField *field;
999   gboolean ret;
1000
1001   g_return_val_if_fail (structure != NULL, FALSE);
1002   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1003   g_return_val_if_fail (func != NULL, FALSE);
1004
1005   for (i = 0; i < structure->fields->len; i++) {
1006     field = GST_STRUCTURE_FIELD (structure, i);
1007
1008     ret = func (field->name, &field->value, user_data);
1009     if (!ret)
1010       return FALSE;
1011   }
1012
1013   return TRUE;
1014 }
1015
1016 /**
1017  * gst_structure_has_field:
1018  * @structure: a #GstStructure
1019  * @fieldname: the name of a field
1020  *
1021  * Check if @structure contains a field named @fieldname.
1022  *
1023  * Returns: TRUE if the structure contains a field with the given name
1024  */
1025 gboolean
1026 gst_structure_has_field (const GstStructure * structure,
1027     const gchar * fieldname)
1028 {
1029   GstStructureField *field;
1030
1031   g_return_val_if_fail (structure != NULL, 0);
1032   g_return_val_if_fail (fieldname != NULL, 0);
1033
1034   field = gst_structure_get_field (structure, fieldname);
1035
1036   return (field != NULL);
1037 }
1038
1039 /**
1040  * gst_structure_has_field_typed:
1041  * @structure: a #GstStructure
1042  * @fieldname: the name of a field
1043  * @type: the type of a value
1044  *
1045  * Check if @structure contains a field named @fieldname and with GType @type.
1046  *
1047  * Returns: TRUE if the structure contains a field with the given name and type
1048  */
1049 gboolean
1050 gst_structure_has_field_typed (const GstStructure * structure,
1051     const gchar * fieldname, GType type)
1052 {
1053   GstStructureField *field;
1054
1055   g_return_val_if_fail (structure != NULL, 0);
1056   g_return_val_if_fail (fieldname != NULL, 0);
1057
1058   field = gst_structure_get_field (structure, fieldname);
1059   if (field == NULL)
1060     return FALSE;
1061
1062   return (G_VALUE_TYPE (&field->value) == type);
1063 }
1064
1065
1066 /* utility functions */
1067
1068 /**
1069  * gst_structure_get_boolean:
1070  * @structure: a #GstStructure
1071  * @fieldname: the name of a field
1072  * @value: a pointer to a #gboolean to set
1073  *
1074  * Sets the boolean pointed to by @value corresponding to the value of the
1075  * given field.  Caller is responsible for making sure the field exists
1076  * and has the correct type.
1077  *
1078  * Returns: TRUE if the value could be set correctly. If there was no field
1079  * with @fieldname or the existing field did not contain a boolean, this
1080  * function returns FALSE.
1081  */
1082 gboolean
1083 gst_structure_get_boolean (const GstStructure * structure,
1084     const gchar * fieldname, gboolean * value)
1085 {
1086   GstStructureField *field;
1087
1088   g_return_val_if_fail (structure != NULL, FALSE);
1089   g_return_val_if_fail (fieldname != NULL, FALSE);
1090
1091   field = gst_structure_get_field (structure, fieldname);
1092
1093   if (field == NULL)
1094     return FALSE;
1095   if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1096     return FALSE;
1097
1098   *value = g_value_get_boolean (&field->value);
1099
1100   return TRUE;
1101 }
1102
1103 /**
1104  * gst_structure_get_int:
1105  * @structure: a #GstStructure
1106  * @fieldname: the name of a field
1107  * @value: a pointer to an int to set
1108  *
1109  * Sets the int pointed to by @value corresponding to the value of the
1110  * given field.  Caller is responsible for making sure the field exists
1111  * and has the correct type.
1112  *
1113  * Returns: %TRUE if the value could be set correctly. If there was no field
1114  * with @fieldname or the existing field did not contain an int, this function
1115  * returns %FALSE.
1116  */
1117 gboolean
1118 gst_structure_get_int (const GstStructure * structure,
1119     const gchar * fieldname, gint * value)
1120 {
1121   GstStructureField *field;
1122
1123   g_return_val_if_fail (structure != NULL, FALSE);
1124   g_return_val_if_fail (fieldname != NULL, FALSE);
1125   g_return_val_if_fail (value != NULL, FALSE);
1126
1127   field = gst_structure_get_field (structure, fieldname);
1128
1129   if (field == NULL)
1130     return FALSE;
1131   if (!G_VALUE_HOLDS_INT (&field->value))
1132     return FALSE;
1133
1134   *value = g_value_get_int (&field->value);
1135
1136   return TRUE;
1137 }
1138
1139 /**
1140  * gst_structure_get_uint:
1141  * @structure: a #GstStructure
1142  * @fieldname: the name of a field
1143  * @value: a pointer to a uint to set
1144  *
1145  * Sets the uint pointed to by @value corresponding to the value of the
1146  * given field.  Caller is responsible for making sure the field exists
1147  * and has the correct type.
1148  *
1149  * Returns: %TRUE if the value could be set correctly. If there was no field
1150  * with @fieldname or the existing field did not contain a uint, this function
1151  * returns %FALSE.
1152  *
1153  * Since: 0.10.15
1154  */
1155 gboolean
1156 gst_structure_get_uint (const GstStructure * structure,
1157     const gchar * fieldname, guint * value)
1158 {
1159   GstStructureField *field;
1160
1161   g_return_val_if_fail (structure != NULL, FALSE);
1162   g_return_val_if_fail (fieldname != NULL, FALSE);
1163   g_return_val_if_fail (value != NULL, FALSE);
1164
1165   field = gst_structure_get_field (structure, fieldname);
1166
1167   if (field == NULL)
1168     return FALSE;
1169   if (!G_VALUE_HOLDS_UINT (&field->value))
1170     return FALSE;
1171
1172   *value = g_value_get_uint (&field->value);
1173
1174   return TRUE;
1175 }
1176
1177 /**
1178  * gst_structure_get_fourcc:
1179  * @structure: a #GstStructure
1180  * @fieldname: the name of a field
1181  * @value: a pointer to a #GstFourcc to set
1182  *
1183  * Sets the #GstFourcc pointed to by @value corresponding to the value of the
1184  * given field.  Caller is responsible for making sure the field exists
1185  * and has the correct type.
1186  *
1187  * Returns: TRUE if the value could be set correctly. If there was no field
1188  * with @fieldname or the existing field did not contain a fourcc, this function
1189  * returns FALSE.
1190  */
1191 gboolean
1192 gst_structure_get_fourcc (const GstStructure * structure,
1193     const gchar * fieldname, guint32 * value)
1194 {
1195   GstStructureField *field;
1196
1197   g_return_val_if_fail (structure != NULL, FALSE);
1198   g_return_val_if_fail (fieldname != NULL, FALSE);
1199   g_return_val_if_fail (value != NULL, FALSE);
1200
1201   field = gst_structure_get_field (structure, fieldname);
1202
1203   if (field == NULL)
1204     return FALSE;
1205   if (!GST_VALUE_HOLDS_FOURCC (&field->value))
1206     return FALSE;
1207
1208   *value = gst_value_get_fourcc (&field->value);
1209
1210   return TRUE;
1211 }
1212
1213 /**
1214  * gst_structure_get_date:
1215  * @structure: a #GstStructure
1216  * @fieldname: the name of a field
1217  * @value: a pointer to a #GDate to set
1218  *
1219  * Sets the date pointed to by @value corresponding to the date of the
1220  * given field.  Caller is responsible for making sure the field exists
1221  * and has the correct type.
1222  *
1223  * Returns: TRUE if the value could be set correctly. If there was no field
1224  * with @fieldname or the existing field did not contain a data, this function
1225  * returns FALSE.
1226  */
1227 gboolean
1228 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1229     GDate ** value)
1230 {
1231   GstStructureField *field;
1232
1233   g_return_val_if_fail (structure != NULL, FALSE);
1234   g_return_val_if_fail (fieldname != NULL, FALSE);
1235   g_return_val_if_fail (value != NULL, FALSE);
1236
1237   field = gst_structure_get_field (structure, fieldname);
1238
1239   if (field == NULL)
1240     return FALSE;
1241   if (!GST_VALUE_HOLDS_DATE (&field->value))
1242     return FALSE;
1243
1244   *value = g_value_dup_boxed (&field->value);
1245
1246   return TRUE;
1247 }
1248
1249 /**
1250  * gst_structure_get_clock_time:
1251  * @structure: a #GstStructure
1252  * @fieldname: the name of a field
1253  * @value: a pointer to a #GstClockTime to set
1254  *
1255  * Sets the clock time pointed to by @value corresponding to the clock time
1256  * of the given field.  Caller is responsible for making sure the field exists
1257  * and has the correct type.
1258  *
1259  * Returns: TRUE if the value could be set correctly. If there was no field
1260  * with @fieldname or the existing field did not contain a #GstClockTime, this 
1261  * function returns FALSE.
1262  */
1263 gboolean
1264 gst_structure_get_clock_time (const GstStructure * structure,
1265     const gchar * fieldname, GstClockTime * value)
1266 {
1267   GstStructureField *field;
1268
1269   g_return_val_if_fail (structure != NULL, FALSE);
1270   g_return_val_if_fail (fieldname != NULL, FALSE);
1271   g_return_val_if_fail (value != NULL, FALSE);
1272
1273   field = gst_structure_get_field (structure, fieldname);
1274
1275   if (field == NULL)
1276     return FALSE;
1277   if (!G_VALUE_HOLDS_UINT64 (&field->value))
1278     return FALSE;
1279
1280   *value = g_value_get_uint64 (&field->value);
1281
1282   return TRUE;
1283 }
1284
1285 /**
1286  * gst_structure_get_double:
1287  * @structure: a #GstStructure
1288  * @fieldname: the name of a field
1289  * @value: a pointer to a #GstFourcc to set
1290  *
1291  * Sets the double pointed to by @value corresponding to the value of the
1292  * given field.  Caller is responsible for making sure the field exists
1293  * and has the correct type.
1294  *
1295  * Returns: TRUE if the value could be set correctly. If there was no field
1296  * with @fieldname or the existing field did not contain a double, this 
1297  * function returns FALSE.
1298  */
1299 gboolean
1300 gst_structure_get_double (const GstStructure * structure,
1301     const gchar * fieldname, gdouble * value)
1302 {
1303   GstStructureField *field;
1304
1305   g_return_val_if_fail (structure != NULL, FALSE);
1306   g_return_val_if_fail (fieldname != NULL, FALSE);
1307   g_return_val_if_fail (value != NULL, FALSE);
1308
1309   field = gst_structure_get_field (structure, fieldname);
1310
1311   if (field == NULL)
1312     return FALSE;
1313   if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1314     return FALSE;
1315
1316   *value = g_value_get_double (&field->value);
1317
1318   return TRUE;
1319 }
1320
1321 /**
1322  * gst_structure_get_string:
1323  * @structure: a #GstStructure
1324  * @fieldname: the name of a field
1325  *
1326  * Finds the field corresponding to @fieldname, and returns the string
1327  * contained in the field's value.  Caller is responsible for making
1328  * sure the field exists and has the correct type.
1329  *
1330  * The string should not be modified, and remains valid until the next
1331  * call to a gst_structure_*() function with the given structure.
1332  *
1333  * Returns: a pointer to the string or NULL when the field did not exist
1334  * or did not contain a string.
1335  */
1336 const gchar *
1337 gst_structure_get_string (const GstStructure * structure,
1338     const gchar * fieldname)
1339 {
1340   GstStructureField *field;
1341
1342   g_return_val_if_fail (structure != NULL, NULL);
1343   g_return_val_if_fail (fieldname != NULL, NULL);
1344
1345   field = gst_structure_get_field (structure, fieldname);
1346
1347   if (field == NULL)
1348     return NULL;
1349   if (!G_VALUE_HOLDS_STRING (&field->value))
1350     return NULL;
1351
1352   return g_value_get_string (&field->value);
1353 }
1354
1355 /**
1356  * gst_structure_get_enum:
1357  * @structure: a #GstStructure
1358  * @fieldname: the name of a field
1359  * @enumtype: the enum type of a field
1360  * @value: a pointer to an int to set
1361  *
1362  * Sets the int pointed to by @value corresponding to the value of the
1363  * given field.  Caller is responsible for making sure the field exists,
1364  * has the correct type and that the enumtype is correct.
1365  *
1366  * Returns: TRUE if the value could be set correctly. If there was no field
1367  * with @fieldname or the existing field did not contain an enum of the given
1368  * type, this function returns FALSE.
1369  */
1370 gboolean
1371 gst_structure_get_enum (const GstStructure * structure,
1372     const gchar * fieldname, GType enumtype, gint * value)
1373 {
1374   GstStructureField *field;
1375
1376   g_return_val_if_fail (structure != NULL, FALSE);
1377   g_return_val_if_fail (fieldname != NULL, FALSE);
1378   g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1379   g_return_val_if_fail (value != NULL, FALSE);
1380
1381   field = gst_structure_get_field (structure, fieldname);
1382
1383   if (field == NULL)
1384     return FALSE;
1385   if (!G_VALUE_HOLDS_ENUM (&field->value))
1386     return FALSE;
1387   if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1388     return FALSE;
1389
1390   *value = g_value_get_enum (&field->value);
1391
1392   return TRUE;
1393 }
1394
1395 /**
1396  * gst_structure_get_fraction:
1397  * @structure: a #GstStructure
1398  * @fieldname: the name of a field
1399  * @value_numerator: a pointer to an int to set
1400  * @value_denominator: a pointer to an int to set
1401  *
1402  * Sets the integers pointed to by @value_numerator and @value_denominator 
1403  * corresponding to the value of the given field.  Caller is responsible 
1404  * for making sure the field exists and has the correct type.
1405  *
1406  * Returns: TRUE if the values could be set correctly. If there was no field
1407  * with @fieldname or the existing field did not contain a GstFraction, this 
1408  * function returns FALSE.
1409  */
1410 gboolean
1411 gst_structure_get_fraction (const GstStructure * structure,
1412     const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1413 {
1414   GstStructureField *field;
1415
1416   g_return_val_if_fail (structure != NULL, FALSE);
1417   g_return_val_if_fail (fieldname != NULL, FALSE);
1418   g_return_val_if_fail (value_numerator != NULL, FALSE);
1419   g_return_val_if_fail (value_denominator != NULL, FALSE);
1420
1421   field = gst_structure_get_field (structure, fieldname);
1422
1423   if (field == NULL)
1424     return FALSE;
1425   if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1426     return FALSE;
1427
1428   *value_numerator = gst_value_get_fraction_numerator (&field->value);
1429   *value_denominator = gst_value_get_fraction_denominator (&field->value);
1430
1431   return TRUE;
1432 }
1433
1434 typedef struct _GstStructureAbbreviation
1435 {
1436   gchar *type_name;
1437   GType type;
1438 }
1439 GstStructureAbbreviation;
1440
1441 /* return a copy of an array of GstStructureAbbreviation containing all the
1442  * known type_string, GType maps, including abbreviations for common types */
1443 static GstStructureAbbreviation *
1444 gst_structure_get_abbrs (gint * n_abbrs)
1445 {
1446   static GstStructureAbbreviation *abbrs = NULL;
1447   static gint num = 0;
1448
1449   if (abbrs == NULL) {
1450     /* dynamically generate the array */
1451     GstStructureAbbreviation dyn_abbrs[] = {
1452       {"int", G_TYPE_INT}
1453       ,
1454       {"i", G_TYPE_INT}
1455       ,
1456       {"float", G_TYPE_FLOAT}
1457       ,
1458       {"f", G_TYPE_FLOAT}
1459       ,
1460       {"double", G_TYPE_DOUBLE}
1461       ,
1462       {"d", G_TYPE_DOUBLE}
1463       ,
1464       {"buffer", GST_TYPE_BUFFER}
1465       ,
1466       {"fourcc", GST_TYPE_FOURCC}
1467       ,
1468       {"4", GST_TYPE_FOURCC}
1469       ,
1470       {"fraction", GST_TYPE_FRACTION}
1471       ,
1472       {"boolean", G_TYPE_BOOLEAN}
1473       ,
1474       {"bool", G_TYPE_BOOLEAN}
1475       ,
1476       {"b", G_TYPE_BOOLEAN}
1477       ,
1478       {"string", G_TYPE_STRING}
1479       ,
1480       {"str", G_TYPE_STRING}
1481       ,
1482       {"s", G_TYPE_STRING}
1483       ,
1484       {"structure", GST_TYPE_STRUCTURE}
1485     };
1486     num = G_N_ELEMENTS (dyn_abbrs);
1487     /* permanently allocate and copy the array now */
1488     abbrs = g_new0 (GstStructureAbbreviation, num);
1489     memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * num);
1490   }
1491   *n_abbrs = num;
1492
1493   return abbrs;
1494 }
1495
1496 /* given a type_name that could be a type abbreviation or a registered GType,
1497  * return a matching GType */
1498 static GType
1499 gst_structure_gtype_from_abbr (const char *type_name)
1500 {
1501   int i;
1502   GstStructureAbbreviation *abbrs;
1503   gint n_abbrs;
1504
1505   g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1506
1507   abbrs = gst_structure_get_abbrs (&n_abbrs);
1508
1509   for (i = 0; i < n_abbrs; i++) {
1510     if (strcmp (type_name, abbrs[i].type_name) == 0) {
1511       return abbrs[i].type;
1512     }
1513   }
1514
1515   /* this is the fallback */
1516   return g_type_from_name (type_name);
1517 }
1518
1519 static const char *
1520 gst_structure_to_abbr (GType type)
1521 {
1522   int i;
1523   GstStructureAbbreviation *abbrs;
1524   gint n_abbrs;
1525
1526   g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1527
1528   abbrs = gst_structure_get_abbrs (&n_abbrs);
1529
1530   for (i = 0; i < n_abbrs; i++) {
1531     if (type == abbrs[i].type) {
1532       return abbrs[i].type_name;
1533     }
1534   }
1535
1536   return g_type_name (type);
1537 }
1538
1539 static GType
1540 gst_structure_value_get_generic_type (GValue * val)
1541 {
1542   if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1543       || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1544     GArray *array = g_value_peek_pointer (val);
1545
1546     if (array->len > 0) {
1547       GValue *value = &g_array_index (array, GValue, 0);
1548
1549       return gst_structure_value_get_generic_type (value);
1550     } else {
1551       return G_TYPE_INT;
1552     }
1553   } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1554     return G_TYPE_INT;
1555   } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1556     return G_TYPE_DOUBLE;
1557   } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1558     return GST_TYPE_FRACTION;
1559   }
1560   return G_VALUE_TYPE (val);
1561 }
1562
1563 gboolean
1564 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1565     GString * s)
1566 {
1567   GstStructureField *field;
1568   guint i;
1569
1570   g_return_val_if_fail (s != NULL, FALSE);
1571
1572   g_string_append (s, g_quark_to_string (structure->name));
1573   for (i = 0; i < structure->fields->len; i++) {
1574     char *t;
1575     GType type;
1576
1577     field = GST_STRUCTURE_FIELD (structure, i);
1578
1579     t = gst_value_serialize (&field->value);
1580     type = gst_structure_value_get_generic_type (&field->value);
1581
1582     g_string_append_len (s, ", ", 2);
1583     /* FIXME: do we need to escape fieldnames? */
1584     g_string_append (s, g_quark_to_string (field->name));
1585     g_string_append_len (s, "=(", 2);
1586     g_string_append (s, gst_structure_to_abbr (type));
1587     g_string_append_c (s, ')');
1588     g_string_append (s, GST_STR_NULL (t));
1589     g_free (t);
1590   }
1591
1592   g_string_append_c (s, ';');
1593   return TRUE;
1594 }
1595
1596 /**
1597  * gst_structure_to_string:
1598  * @structure: a #GstStructure
1599  *
1600  * Converts @structure to a human-readable string representation.
1601  *
1602  * For debugging purposes its easier to do something like this:
1603  * |[
1604  * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1605  * ]|
1606  * This prints the structure in human readble form.
1607  *
1608  * Returns: a pointer to string allocated by g_malloc(). g_free() after
1609  * usage.
1610  */
1611 gchar *
1612 gst_structure_to_string (const GstStructure * structure)
1613 {
1614   GString *s;
1615
1616   /* NOTE:  This function is potentially called by the debug system,
1617    * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1618    * should be careful to avoid recursion.  This includes any functions
1619    * called by gst_structure_to_string.  In particular, calls should
1620    * not use the GST_PTR_FORMAT extension.  */
1621
1622   g_return_val_if_fail (structure != NULL, NULL);
1623
1624   /* we estimate a minimum size based on the number of fields in order to
1625    * avoid unnecessary reallocs within GString */
1626   s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1627   priv_gst_structure_append_to_gstring (structure, s);
1628   return g_string_free (s, FALSE);
1629 }
1630
1631 /*
1632  * r will still point to the string. if end == next, the string will not be
1633  * null-terminated. In all other cases it will be.
1634  * end = pointer to char behind end of string, next = pointer to start of
1635  * unread data.
1636  * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1637  */
1638 static gboolean
1639 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next)
1640 {
1641   gchar *w;
1642
1643   if (*s == 0)
1644     return FALSE;
1645
1646   if (*s != '"') {
1647     int ret;
1648
1649     ret = gst_structure_parse_simple_string (s, end);
1650     *next = *end;
1651
1652     return ret;
1653   }
1654
1655   w = s;
1656   s++;
1657   while (*s != '"') {
1658     if (*s == 0)
1659       return FALSE;
1660
1661     if (*s == '\\') {
1662       s++;
1663     }
1664
1665     *w = *s;
1666     w++;
1667     s++;
1668   }
1669   s++;
1670
1671   *end = w;
1672   *next = s;
1673
1674   return TRUE;
1675 }
1676
1677 static gboolean
1678 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1679     GType type)
1680 {
1681   GValue value1 = { 0 };
1682   GValue value2 = { 0 };
1683   GType range_type;
1684   gboolean ret;
1685
1686   if (*s != '[')
1687     return FALSE;
1688   s++;
1689
1690   ret = gst_structure_parse_value (s, &s, &value1, type);
1691   if (ret == FALSE)
1692     return FALSE;
1693
1694   while (g_ascii_isspace (*s))
1695     s++;
1696
1697   if (*s != ',')
1698     return FALSE;
1699   s++;
1700
1701   while (g_ascii_isspace (*s))
1702     s++;
1703
1704   ret = gst_structure_parse_value (s, &s, &value2, type);
1705   if (ret == FALSE)
1706     return FALSE;
1707
1708   while (g_ascii_isspace (*s))
1709     s++;
1710
1711   if (*s != ']')
1712     return FALSE;
1713   s++;
1714
1715   if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1716     return FALSE;
1717
1718   if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1719     range_type = GST_TYPE_DOUBLE_RANGE;
1720     g_value_init (value, range_type);
1721     gst_value_set_double_range (value, g_value_get_double (&value1),
1722         g_value_get_double (&value2));
1723   } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1724     range_type = GST_TYPE_INT_RANGE;
1725     g_value_init (value, range_type);
1726     gst_value_set_int_range (value, g_value_get_int (&value1),
1727         g_value_get_int (&value2));
1728   } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1729     range_type = GST_TYPE_FRACTION_RANGE;
1730     g_value_init (value, range_type);
1731     gst_value_set_fraction_range (value, &value1, &value2);
1732   } else {
1733     return FALSE;
1734   }
1735
1736   *after = s;
1737   return TRUE;
1738 }
1739
1740 static gboolean
1741 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1742     GType type, GType list_type, char begin, char end)
1743 {
1744   GValue list_value = { 0 };
1745   gboolean ret;
1746   GArray *array;
1747
1748   g_value_init (value, list_type);
1749   array = g_value_peek_pointer (value);
1750
1751   if (*s != begin)
1752     return FALSE;
1753   s++;
1754
1755   while (g_ascii_isspace (*s))
1756     s++;
1757   if (*s == end) {
1758     s++;
1759     *after = s;
1760     return TRUE;
1761   }
1762
1763   ret = gst_structure_parse_value (s, &s, &list_value, type);
1764   if (ret == FALSE)
1765     return FALSE;
1766
1767   g_array_append_val (array, list_value);
1768
1769   while (g_ascii_isspace (*s))
1770     s++;
1771
1772   while (*s != end) {
1773     if (*s != ',')
1774       return FALSE;
1775     s++;
1776
1777     while (g_ascii_isspace (*s))
1778       s++;
1779
1780     memset (&list_value, 0, sizeof (list_value));
1781     ret = gst_structure_parse_value (s, &s, &list_value, type);
1782     if (ret == FALSE)
1783       return FALSE;
1784
1785     g_array_append_val (array, list_value);
1786     while (g_ascii_isspace (*s))
1787       s++;
1788   }
1789
1790   s++;
1791
1792   *after = s;
1793   return TRUE;
1794 }
1795
1796 static gboolean
1797 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
1798 {
1799   return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
1800       '{', '}');
1801 }
1802
1803 static gboolean
1804 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
1805     GType type)
1806 {
1807   return gst_structure_parse_any_list (s, after, value, type,
1808       GST_TYPE_ARRAY, '<', '>');
1809 }
1810
1811 static gboolean
1812 gst_structure_parse_simple_string (gchar * str, gchar ** end)
1813 {
1814   char *s = str;
1815
1816   while (GST_ASCII_IS_STRING (*s)) {
1817     s++;
1818   }
1819
1820   *end = s;
1821
1822   return (s != str);
1823 }
1824
1825 static gboolean
1826 gst_structure_parse_field (gchar * str,
1827     gchar ** after, GstStructureField * field)
1828 {
1829   gchar *name;
1830   gchar *name_end;
1831   gchar *s;
1832   gchar c;
1833
1834   s = str;
1835
1836   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1837     s++;
1838   name = s;
1839   if (!gst_structure_parse_simple_string (s, &name_end))
1840     return FALSE;
1841
1842   s = name_end;
1843   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1844     s++;
1845
1846   if (*s != '=')
1847     return FALSE;
1848   s++;
1849
1850   c = *name_end;
1851   *name_end = '\0';
1852   field->name = g_quark_from_string (name);
1853   *name_end = c;
1854
1855   if (!gst_structure_parse_value (s, &s, &field->value, G_TYPE_INVALID))
1856     return FALSE;
1857
1858   *after = s;
1859   return TRUE;
1860 }
1861
1862 static gboolean
1863 gst_structure_parse_value (gchar * str,
1864     gchar ** after, GValue * value, GType default_type)
1865 {
1866   gchar *type_name;
1867   gchar *type_end;
1868   gchar *value_s;
1869   gchar *value_end;
1870   gchar *s;
1871   gchar c;
1872   int ret = 0;
1873   GType type = default_type;
1874
1875   s = str;
1876   while (g_ascii_isspace (*s))
1877     s++;
1878
1879   /* check if there's a (type_name) 'cast' */
1880   type_name = NULL;
1881   if (*s == '(') {
1882     s++;
1883     while (g_ascii_isspace (*s))
1884       s++;
1885     type_name = s;
1886     if (!gst_structure_parse_simple_string (s, &type_end))
1887       return FALSE;
1888     s = type_end;
1889     while (g_ascii_isspace (*s))
1890       s++;
1891     if (*s != ')')
1892       return FALSE;
1893     s++;
1894     while (g_ascii_isspace (*s))
1895       s++;
1896
1897     c = *type_end;
1898     *type_end = 0;
1899     type = gst_structure_gtype_from_abbr (type_name);
1900     *type_end = c;
1901
1902     if (G_UNLIKELY (type == G_TYPE_INVALID))
1903       return FALSE;
1904   }
1905
1906   while (g_ascii_isspace (*s))
1907     s++;
1908   if (*s == '[') {
1909     ret = gst_structure_parse_range (s, &s, value, type);
1910   } else if (*s == '{') {
1911     ret = gst_structure_parse_list (s, &s, value, type);
1912   } else if (*s == '<') {
1913     ret = gst_structure_parse_array (s, &s, value, type);
1914   } else {
1915     value_s = s;
1916     if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s)))
1917       return FALSE;
1918
1919     c = *value_end;
1920     *value_end = '\0';
1921     if (G_UNLIKELY (type == G_TYPE_INVALID)) {
1922       GType try_types[] =
1923           { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
1924         G_TYPE_STRING
1925       };
1926       int i;
1927
1928       for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
1929         g_value_init (value, try_types[i]);
1930         ret = gst_value_deserialize (value, value_s);
1931         if (ret)
1932           break;
1933         g_value_unset (value);
1934       }
1935     } else {
1936       g_value_init (value, type);
1937
1938       ret = gst_value_deserialize (value, value_s);
1939       if (G_UNLIKELY (!ret))
1940         g_value_unset (value);
1941     }
1942     *value_end = c;
1943   }
1944
1945   *after = s;
1946
1947   return ret;
1948 }
1949
1950 /**
1951  * gst_structure_from_string:
1952  * @string: a string representation of a #GstStructure.
1953  * @end: pointer to store the end of the string in.
1954  *
1955  * Creates a #GstStructure from a string representation.
1956  * If end is not NULL, a pointer to the place inside the given string
1957  * where parsing ended will be returned.
1958  *
1959  * Returns: a new #GstStructure or NULL when the string could not
1960  * be parsed. Free with gst_structure_free() after use.
1961  */
1962 GstStructure *
1963 gst_structure_from_string (const gchar * string, gchar ** end)
1964 {
1965   char *name;
1966   char *copy;
1967   char *w;
1968   char *r;
1969   char save;
1970   GstStructure *structure = NULL;
1971   GstStructureField field = { 0 };
1972
1973   g_return_val_if_fail (string != NULL, NULL);
1974
1975   copy = g_strdup (string);
1976   r = copy;
1977
1978   /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
1979   while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1980               && g_ascii_isspace (r[1]))))
1981     r++;
1982
1983   name = r;
1984   if (!gst_structure_parse_string (r, &w, &r)) {
1985     GST_WARNING ("Failed to parse structure string");
1986     goto error;
1987   }
1988
1989   save = *w;
1990   *w = '\0';
1991   structure = gst_structure_empty_new (name);
1992   *w = save;
1993
1994   if (G_UNLIKELY (structure == NULL))
1995     goto error;
1996
1997   do {
1998     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1999                 && g_ascii_isspace (r[1]))))
2000       r++;
2001     if (*r == ';') {
2002       /* end of structure, get the next char and finish */
2003       r++;
2004       break;
2005     }
2006     if (*r == '\0') {
2007       /* accept \0 as end delimiter */
2008       break;
2009     }
2010     if (*r != ',') {
2011       GST_WARNING ("Failed to find delimiter, r=%s", r);
2012       goto error;
2013     }
2014     r++;
2015     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2016                 && g_ascii_isspace (r[1]))))
2017       r++;
2018
2019     memset (&field, 0, sizeof (field));
2020     if (!gst_structure_parse_field (r, &r, &field))
2021       goto error;
2022     gst_structure_set_field (structure, &field);
2023   } while (TRUE);
2024
2025   if (end)
2026     *end = (char *) string + (r - copy);
2027   else if (*r)
2028     g_warning ("gst_structure_from_string did not consume whole string,"
2029         " but caller did not provide end pointer (\"%s\")", string);
2030
2031   g_free (copy);
2032   return structure;
2033
2034 error:
2035   if (structure)
2036     gst_structure_free (structure);
2037   g_free (copy);
2038   return NULL;
2039 }
2040
2041 static void
2042 gst_structure_transform_to_string (const GValue * src_value,
2043     GValue * dest_value)
2044 {
2045   g_return_if_fail (src_value != NULL);
2046   g_return_if_fail (dest_value != NULL);
2047
2048   dest_value->data[0].v_pointer =
2049       gst_structure_to_string (src_value->data[0].v_pointer);
2050 }
2051
2052 static GstStructure *
2053 gst_structure_copy_conditional (const GstStructure * structure)
2054 {
2055   if (structure)
2056     return gst_structure_copy (structure);
2057   return NULL;
2058 }
2059
2060 /* fixate utility functions */
2061
2062 /**
2063  * gst_structure_fixate_field_nearest_int:
2064  * @structure: a #GstStructure
2065  * @field_name: a field in @structure
2066  * @target: the target value of the fixation
2067  *
2068  * Fixates a #GstStructure by changing the given field to the nearest
2069  * integer to @target that is a subset of the existing field.
2070  *
2071  * Returns: TRUE if the structure could be fixated
2072  */
2073 gboolean
2074 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2075     const char *field_name, int target)
2076 {
2077   const GValue *value;
2078
2079   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2080   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2081
2082   value = gst_structure_get_value (structure, field_name);
2083
2084   if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2085     /* already fixed */
2086     return FALSE;
2087   } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2088     int x;
2089
2090     x = gst_value_get_int_range_min (value);
2091     if (target < x)
2092       target = x;
2093     x = gst_value_get_int_range_max (value);
2094     if (target > x)
2095       target = x;
2096     gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2097     return TRUE;
2098   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2099     const GValue *list_value;
2100     int i, n;
2101     int best = 0;
2102     int best_index = -1;
2103
2104     n = gst_value_list_get_size (value);
2105     for (i = 0; i < n; i++) {
2106       list_value = gst_value_list_get_value (value, i);
2107       if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2108         int x = g_value_get_int (list_value);
2109
2110         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2111           best_index = i;
2112           best = x;
2113         }
2114       }
2115     }
2116     if (best_index != -1) {
2117       gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2118       return TRUE;
2119     }
2120     return FALSE;
2121   }
2122
2123   return FALSE;
2124 }
2125
2126 /**
2127  * gst_structure_fixate_field_nearest_double:
2128  * @structure: a #GstStructure
2129  * @field_name: a field in @structure
2130  * @target: the target value of the fixation
2131  *
2132  * Fixates a #GstStructure by changing the given field to the nearest
2133  * double to @target that is a subset of the existing field.
2134  *
2135  * Returns: TRUE if the structure could be fixated
2136  */
2137 gboolean
2138 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2139     const char *field_name, double target)
2140 {
2141   const GValue *value;
2142
2143   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2144   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2145
2146   value = gst_structure_get_value (structure, field_name);
2147
2148   if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2149     /* already fixed */
2150     return FALSE;
2151   } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2152     double x;
2153
2154     x = gst_value_get_double_range_min (value);
2155     if (target < x)
2156       target = x;
2157     x = gst_value_get_double_range_max (value);
2158     if (target > x)
2159       target = x;
2160     gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2161     return TRUE;
2162   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2163     const GValue *list_value;
2164     int i, n;
2165     double best = 0;
2166     int best_index = -1;
2167
2168     n = gst_value_list_get_size (value);
2169     for (i = 0; i < n; i++) {
2170       list_value = gst_value_list_get_value (value, i);
2171       if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2172         double x = g_value_get_double (list_value);
2173
2174         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2175           best_index = i;
2176           best = x;
2177         }
2178       }
2179     }
2180     if (best_index != -1) {
2181       gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2182       return TRUE;
2183     }
2184     return FALSE;
2185   }
2186
2187   return FALSE;
2188
2189 }
2190
2191 /**
2192  * gst_structure_fixate_field_boolean:
2193  * @structure: a #GstStructure
2194  * @field_name: a field in @structure
2195  * @target: the target value of the fixation
2196  *
2197  * Fixates a #GstStructure by changing the given @field_name field to the given
2198  * @target boolean if that field is not fixed yet.
2199  *
2200  * Returns: TRUE if the structure could be fixated
2201  */
2202 gboolean
2203 gst_structure_fixate_field_boolean (GstStructure * structure,
2204     const char *field_name, gboolean target)
2205 {
2206   const GValue *value;
2207
2208   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2209   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2210
2211   value = gst_structure_get_value (structure, field_name);
2212
2213   if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2214     /* already fixed */
2215     return FALSE;
2216   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2217     const GValue *list_value;
2218     int i, n;
2219     int best = 0;
2220     int best_index = -1;
2221
2222     n = gst_value_list_get_size (value);
2223     for (i = 0; i < n; i++) {
2224       list_value = gst_value_list_get_value (value, i);
2225       if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2226         gboolean x = g_value_get_boolean (list_value);
2227
2228         if (best_index == -1 || x == target) {
2229           best_index = i;
2230           best = x;
2231         }
2232       }
2233     }
2234     if (best_index != -1) {
2235       gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2236       return TRUE;
2237     }
2238     return FALSE;
2239   }
2240
2241   return FALSE;
2242 }
2243
2244 /**
2245  * gst_structure_fixate_field_nearest_fraction:
2246  * @structure: a #GstStructure
2247  * @field_name: a field in @structure
2248  * @target_numerator: The numerator of the target value of the fixation
2249  * @target_denominator: The denominator of the target value of the fixation
2250  *
2251  * Fixates a #GstStructure by changing the given field to the nearest
2252  * fraction to @target_numerator/@target_denominator that is a subset 
2253  * of the existing field.
2254  *
2255  * Returns: TRUE if the structure could be fixated
2256  */
2257 gboolean
2258 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2259     const char *field_name, const gint target_numerator,
2260     const gint target_denominator)
2261 {
2262   const GValue *value;
2263
2264   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2265   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2266
2267   value = gst_structure_get_value (structure, field_name);
2268
2269   if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2270     /* already fixed */
2271     return FALSE;
2272   } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2273     const GValue *x, *new_value;
2274     GValue target = { 0 };
2275     g_value_init (&target, GST_TYPE_FRACTION);
2276     gst_value_set_fraction (&target, target_numerator, target_denominator);
2277
2278     new_value = &target;
2279     x = gst_value_get_fraction_range_min (value);
2280     if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2281       new_value = x;
2282     x = gst_value_get_fraction_range_max (value);
2283     if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2284       new_value = x;
2285
2286     gst_structure_set_value (structure, field_name, new_value);
2287     g_value_unset (&target);
2288     return TRUE;
2289   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2290     const GValue *list_value;
2291     int i, n;
2292     const GValue *best = NULL;
2293     gdouble target;
2294     gdouble cur_diff;
2295     gdouble best_diff = G_MAXDOUBLE;
2296
2297     target = (gdouble) target_numerator / (gdouble) target_denominator;
2298
2299     GST_DEBUG ("target %g, best %g", target, best_diff);
2300
2301     best = NULL;
2302
2303     n = gst_value_list_get_size (value);
2304     for (i = 0; i < n; i++) {
2305       list_value = gst_value_list_get_value (value, i);
2306       if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2307         gint num, denom;
2308         gdouble list_double;
2309
2310         num = gst_value_get_fraction_numerator (list_value);
2311         denom = gst_value_get_fraction_denominator (list_value);
2312
2313         list_double = ((gdouble) num / (gdouble) denom);
2314         cur_diff = target - list_double;
2315
2316         GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2317
2318         if (cur_diff < 0)
2319           cur_diff = -cur_diff;
2320
2321         if (!best || cur_diff < best_diff) {
2322           GST_DEBUG ("new best %g", list_double);
2323           best = list_value;
2324           best_diff = cur_diff;
2325         }
2326       }
2327     }
2328     if (best != NULL) {
2329       gst_structure_set_value (structure, field_name, best);
2330       return TRUE;
2331     }
2332   }
2333
2334   return FALSE;
2335 }