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