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