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