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