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