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