structure: Add getters for int64 and uint64 values
[platform/upstream/gstreamer.git] / gst / gststructure.c
1 /* GStreamer
2  * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
3  *
4  * gststructure.c: lists of { GQuark, GValue } tuples
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:gststructure
24  * @short_description: Generic structure containing fields of names and values
25  * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
26  *
27  * A #GstStructure is a collection of key/value pairs. The keys are expressed
28  * as GQuarks and the values can be of any GType.
29  *
30  * In addition to the key/value pairs, a #GstStructure also has a name. The name
31  * starts with a letter and can be filled by letters, numbers and any of "/-_.:".
32  *
33  * #GstStructure is used by various GStreamer subsystems to store information
34  * in a flexible and extensible way. A #GstStructure does not have a refcount
35  * because it usually is part of a higher level object such as #GstCaps,
36  * #GstMessage, #GstEvent, #GstQuery. It provides a means to enforce mutability
37  * using the refcount of the parent with the gst_structure_set_parent_refcount()
38  * method.
39  *
40  * A #GstStructure can be created with gst_structure_new_empty() or
41  * gst_structure_new(), which both take a name and an optional set of
42  * key/value pairs along with the types of the values.
43  *
44  * Field values can be changed with gst_structure_set_value() or
45  * gst_structure_set().
46  *
47  * Field values can be retrieved with gst_structure_get_value() or the more
48  * convenient gst_structure_get_*() functions.
49  *
50  * Fields can be removed with gst_structure_remove_field() or
51  * gst_structure_remove_fields().
52  *
53  * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are
54  * not allowed. Strings may be NULL however.
55  *
56  * Be aware that the current #GstCaps / #GstStructure serialization into string
57  * has limited support for nested #GstCaps / #GstStructure fields. It can only
58  * support one level of nesting. Using more levels will lead to unexpected
59  * behavior when using serialization features, such as gst_caps_to_string() or
60  * gst_value_serialize() and their counterparts.
61  *
62  * Last reviewed on 2012-03-29 (0.11.3)
63  */
64
65 #ifdef HAVE_CONFIG_H
66 #include "config.h"
67 #endif
68
69 #include <string.h>
70
71 #include "gst_private.h"
72 #include "gstquark.h"
73 #include <gst/gst.h>
74 #include <gobject/gvaluecollector.h>
75
76 GST_DEBUG_CATEGORY_STATIC (gst_structure_debug);
77 #define GST_CAT_DEFAULT gst_structure_debug
78
79 typedef struct _GstStructureField GstStructureField;
80
81 struct _GstStructureField
82 {
83   GQuark name;
84   GValue value;
85 };
86
87 typedef struct
88 {
89   GstStructure s;
90
91   /* owned by parent structure, NULL if no parent */
92   gint *parent_refcount;
93
94   GArray *fields;
95 } GstStructureImpl;
96
97 #define GST_STRUCTURE_REFCOUNT(s) (((GstStructureImpl*)(s))->parent_refcount)
98 #define GST_STRUCTURE_FIELDS(s) (((GstStructureImpl*)(s))->fields)
99
100 #define GST_STRUCTURE_FIELD(structure, index) \
101     &g_array_index(GST_STRUCTURE_FIELDS(structure), GstStructureField, (index))
102
103 #define IS_MUTABLE(structure) \
104     (!GST_STRUCTURE_REFCOUNT(structure) || \
105      g_atomic_int_get (GST_STRUCTURE_REFCOUNT(structure)) == 1)
106
107 #define IS_TAGLIST(structure) \
108     (structure->name == GST_QUARK (TAGLIST))
109
110 static void gst_structure_set_field (GstStructure * structure,
111     GstStructureField * field);
112 static GstStructureField *gst_structure_get_field (const GstStructure *
113     structure, const gchar * fieldname);
114 static GstStructureField *gst_structure_id_get_field (const GstStructure *
115     structure, GQuark field);
116 static void gst_structure_transform_to_string (const GValue * src_value,
117     GValue * dest_value);
118 static GstStructure *gst_structure_copy_conditional (const GstStructure *
119     structure);
120 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
121     GValue * value, GType default_type);
122 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
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.
625  * Variable arguments should be in the form field name, field type
626  * (as a GType), value(s).  The last variable argument should be NULL.
627  */
628 void
629 gst_structure_set (GstStructure * structure, const gchar * field, ...)
630 {
631   va_list varargs;
632
633   g_return_if_fail (structure != NULL);
634   g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
635
636   va_start (varargs, field);
637   gst_structure_set_valist_internal (structure, field, varargs);
638   va_end (varargs);
639 }
640
641 /**
642  * gst_structure_set_valist:
643  * @structure: a #GstStructure
644  * @fieldname: the name of the field to set
645  * @varargs: variable arguments
646  *
647  * va_list form of gst_structure_set().
648  */
649 void
650 gst_structure_set_valist (GstStructure * structure,
651     const gchar * fieldname, va_list varargs)
652 {
653   g_return_if_fail (structure != NULL);
654   g_return_if_fail (IS_MUTABLE (structure));
655
656   gst_structure_set_valist_internal (structure, fieldname, varargs);
657 }
658
659 static void
660 gst_structure_id_set_valist_internal (GstStructure * structure,
661     GQuark fieldname, va_list varargs)
662 {
663   gchar *err = NULL;
664   GType type;
665
666   while (fieldname) {
667     GstStructureField field = { 0 };
668
669     field.name = fieldname;
670
671     type = va_arg (varargs, GType);
672
673 #ifndef G_VALUE_COLLECT_INIT
674     g_value_init (&field.value, type);
675     G_VALUE_COLLECT (&field.value, varargs, 0, &err);
676 #else
677     G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
678 #endif
679     if (G_UNLIKELY (err)) {
680       g_critical ("%s", err);
681       return;
682     }
683     gst_structure_set_field (structure, &field);
684
685     fieldname = va_arg (varargs, GQuark);
686   }
687 }
688
689 /**
690  * gst_structure_id_set:
691  * @structure: a #GstStructure
692  * @fieldname: the GQuark for the name of the field to set
693  * @...: variable arguments
694  *
695  * Identical to gst_structure_set, except that field names are
696  * passed using the GQuark for the field name. This allows more efficient
697  * setting of the structure if the caller already knows the associated
698  * quark values.
699  * The last variable argument must be NULL.
700  */
701 void
702 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
703 {
704   va_list varargs;
705
706   g_return_if_fail (structure != NULL);
707
708   va_start (varargs, field);
709   gst_structure_id_set_valist_internal (structure, field, varargs);
710   va_end (varargs);
711 }
712
713 /**
714  * gst_structure_id_set_valist:
715  * @structure: a #GstStructure
716  * @fieldname: the name of the field to set
717  * @varargs: variable arguments
718  *
719  * va_list form of gst_structure_id_set().
720  */
721 void
722 gst_structure_id_set_valist (GstStructure * structure,
723     GQuark fieldname, va_list varargs)
724 {
725   g_return_if_fail (structure != NULL);
726   g_return_if_fail (IS_MUTABLE (structure));
727
728   gst_structure_id_set_valist_internal (structure, fieldname, varargs);
729 }
730
731 /**
732  * gst_structure_new_id:
733  * @name_quark: name of new structure
734  * @field_quark: the GQuark for the name of the field to set
735  * @...: variable arguments
736  *
737  * Creates a new #GstStructure with the given name as a GQuark, followed by
738  * fieldname quark, GType, argument(s) "triplets" in the same format as
739  * gst_structure_id_set(). Basically a convenience wrapper around
740  * gst_structure_new_id_empty() and gst_structure_id_set().
741  *
742  * The last variable argument must be NULL (or 0).
743  *
744  * Free-function: gst_structure_free
745  *
746  * Returns: (transfer full): a new #GstStructure
747  */
748 GstStructure *
749 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
750 {
751   GstStructure *s;
752   va_list varargs;
753
754   g_return_val_if_fail (name_quark != 0, NULL);
755   g_return_val_if_fail (field_quark != 0, NULL);
756
757   s = gst_structure_new_id_empty (name_quark);
758
759   va_start (varargs, field_quark);
760   gst_structure_id_set_valist_internal (s, field_quark, varargs);
761   va_end (varargs);
762
763   return s;
764 }
765
766 #if GST_VERSION_NANO == 1
767 #define GIT_G_WARNING g_warning
768 #else
769 #define GIT_G_WARNING GST_WARNING
770 #endif
771
772 /* If the structure currently contains a field with the same name, it is
773  * replaced with the provided field. Otherwise, the field is added to the
774  * structure. The field's value is not deeply copied.
775  */
776 static void
777 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
778 {
779   GstStructureField *f;
780   guint i, len = GST_STRUCTURE_FIELDS (structure)->len;
781
782   if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
783     const gchar *s;
784
785     s = g_value_get_string (&field->value);
786     /* only check for NULL strings in taglists, as they are allowed in message
787      * structs, e.g. error message debug strings */
788     if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
789       if (s == NULL) {
790         GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
791             "Please file a bug.", g_quark_to_string (field->name));
792         g_value_unset (&field->value);
793         return;
794       } else {
795         /* empty strings never make sense */
796         GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
797             "Please file a bug.", g_quark_to_string (field->name));
798         g_value_unset (&field->value);
799         return;
800       }
801     } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
802       g_warning ("Trying to set string on %s field '%s', but string is not "
803           "valid UTF-8. Please file a bug.",
804           IS_TAGLIST (structure) ? "taglist" : "structure",
805           g_quark_to_string (field->name));
806       g_value_unset (&field->value);
807       return;
808     }
809   } else if (G_UNLIKELY (G_VALUE_HOLDS (&field->value, G_TYPE_DATE))) {
810     const GDate *d;
811
812     d = g_value_get_boxed (&field->value);
813     /* only check for NULL GDates in taglists, as they might make sense
814      * in other, generic structs */
815     if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
816       GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
817           "Please file a bug.", g_quark_to_string (field->name));
818       g_value_unset (&field->value);
819       return;
820     } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
821       g_warning
822           ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
823           IS_TAGLIST (structure) ? "taglist" : "structure",
824           g_quark_to_string (field->name));
825       g_value_unset (&field->value);
826       return;
827     }
828   }
829
830   for (i = 0; i < len; i++) {
831     f = GST_STRUCTURE_FIELD (structure, i);
832
833     if (G_UNLIKELY (f->name == field->name)) {
834       g_value_unset (&f->value);
835       memcpy (f, field, sizeof (GstStructureField));
836       return;
837     }
838   }
839
840   g_array_append_val (GST_STRUCTURE_FIELDS (structure), *field);
841 }
842
843 /* If there is no field with the given ID, NULL is returned.
844  */
845 static GstStructureField *
846 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
847 {
848   GstStructureField *field;
849   guint i, len;
850
851   len = GST_STRUCTURE_FIELDS (structure)->len;
852
853   for (i = 0; i < len; i++) {
854     field = GST_STRUCTURE_FIELD (structure, i);
855
856     if (G_UNLIKELY (field->name == field_id))
857       return field;
858   }
859
860   return NULL;
861 }
862
863 /* If there is no field with the given ID, NULL is returned.
864  */
865 static GstStructureField *
866 gst_structure_get_field (const GstStructure * structure,
867     const gchar * fieldname)
868 {
869   g_return_val_if_fail (structure != NULL, NULL);
870   g_return_val_if_fail (fieldname != NULL, NULL);
871
872   return gst_structure_id_get_field (structure,
873       g_quark_from_string (fieldname));
874 }
875
876 /**
877  * gst_structure_get_value:
878  * @structure: a #GstStructure
879  * @fieldname: the name of the field to get
880  *
881  * Get the value of the field with name @fieldname.
882  *
883  * Returns: the #GValue corresponding to the field with the given 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: the #GValue corresponding to the field with the given name
909  *          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  *
1109  * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1110  * FALSE otherwise.
1111  */
1112 gboolean
1113 gst_structure_foreach (const GstStructure * structure,
1114     GstStructureForeachFunc func, gpointer user_data)
1115 {
1116   guint i, len;
1117   GstStructureField *field;
1118   gboolean ret;
1119
1120   g_return_val_if_fail (structure != NULL, FALSE);
1121   g_return_val_if_fail (func != NULL, FALSE);
1122
1123   len = GST_STRUCTURE_FIELDS (structure)->len;
1124
1125   for (i = 0; i < len; i++) {
1126     field = GST_STRUCTURE_FIELD (structure, i);
1127
1128     ret = func (field->name, &field->value, user_data);
1129     if (G_UNLIKELY (!ret))
1130       return FALSE;
1131   }
1132
1133   return TRUE;
1134 }
1135
1136 /**
1137  * gst_structure_map_in_place:
1138  * @structure: a #GstStructure
1139  * @func: (scope call): a function to call for each field
1140  * @user_data: (closure): private data
1141  *
1142  * Calls the provided function once for each field in the #GstStructure. In
1143  * contrast to gst_structure_foreach(), the function may modify but not delete the
1144  * fields. The structure must be mutable.
1145  *
1146  * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1147  * FALSE otherwise.
1148  */
1149 gboolean
1150 gst_structure_map_in_place (GstStructure * structure,
1151     GstStructureMapFunc func, gpointer user_data)
1152 {
1153   guint i, len;
1154   GstStructureField *field;
1155   gboolean ret;
1156
1157   g_return_val_if_fail (structure != NULL, FALSE);
1158   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1159   g_return_val_if_fail (func != NULL, FALSE);
1160   len = GST_STRUCTURE_FIELDS (structure)->len;
1161
1162   for (i = 0; i < len; i++) {
1163     field = GST_STRUCTURE_FIELD (structure, i);
1164
1165     ret = func (field->name, &field->value, user_data);
1166     if (!ret)
1167       return FALSE;
1168   }
1169
1170   return TRUE;
1171 }
1172
1173 /**
1174  * gst_structure_id_has_field:
1175  * @structure: a #GstStructure
1176  * @field: #GQuark of the field name
1177  *
1178  * Check if @structure contains a field named @field.
1179  *
1180  * Returns: TRUE if the structure contains a field with the given name
1181  */
1182 gboolean
1183 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1184 {
1185   GstStructureField *f;
1186
1187   g_return_val_if_fail (structure != NULL, FALSE);
1188   g_return_val_if_fail (field != 0, FALSE);
1189
1190   f = gst_structure_id_get_field (structure, field);
1191
1192   return (f != NULL);
1193 }
1194
1195 /**
1196  * gst_structure_has_field:
1197  * @structure: a #GstStructure
1198  * @fieldname: the name of a field
1199  *
1200  * Check if @structure contains a field named @fieldname.
1201  *
1202  * Returns: TRUE if the structure contains a field with the given name
1203  */
1204 gboolean
1205 gst_structure_has_field (const GstStructure * structure,
1206     const gchar * fieldname)
1207 {
1208   g_return_val_if_fail (structure != NULL, FALSE);
1209   g_return_val_if_fail (fieldname != NULL, FALSE);
1210
1211   return gst_structure_id_has_field (structure,
1212       g_quark_from_string (fieldname));
1213 }
1214
1215 /**
1216  * gst_structure_id_has_field_typed:
1217  * @structure: a #GstStructure
1218  * @field: #GQuark of the field name
1219  * @type: the type of a value
1220  *
1221  * Check if @structure contains a field named @field and with GType @type.
1222  *
1223  * Returns: TRUE if the structure contains a field with the given name and type
1224  */
1225 gboolean
1226 gst_structure_id_has_field_typed (const GstStructure * structure,
1227     GQuark field, GType type)
1228 {
1229   GstStructureField *f;
1230
1231   g_return_val_if_fail (structure != NULL, FALSE);
1232   g_return_val_if_fail (field != 0, FALSE);
1233
1234   f = gst_structure_id_get_field (structure, field);
1235   if (f == NULL)
1236     return FALSE;
1237
1238   return (G_VALUE_TYPE (&f->value) == type);
1239 }
1240
1241 /**
1242  * gst_structure_has_field_typed:
1243  * @structure: a #GstStructure
1244  * @fieldname: the name of a field
1245  * @type: the type of a value
1246  *
1247  * Check if @structure contains a field named @fieldname and with GType @type.
1248  *
1249  * Returns: TRUE if the structure contains a field with the given name and type
1250  */
1251 gboolean
1252 gst_structure_has_field_typed (const GstStructure * structure,
1253     const gchar * fieldname, GType type)
1254 {
1255   g_return_val_if_fail (structure != NULL, FALSE);
1256   g_return_val_if_fail (fieldname != NULL, FALSE);
1257
1258   return gst_structure_id_has_field_typed (structure,
1259       g_quark_from_string (fieldname), type);
1260 }
1261
1262 /* utility functions */
1263
1264 /**
1265  * gst_structure_get_boolean:
1266  * @structure: a #GstStructure
1267  * @fieldname: the name of a field
1268  * @value: (out): a pointer to a #gboolean to set
1269  *
1270  * Sets the boolean pointed to by @value corresponding to the value of the
1271  * given field.  Caller is responsible for making sure the field exists
1272  * and has the correct type.
1273  *
1274  * Returns: TRUE if the value could be set correctly. If there was no field
1275  * with @fieldname or the existing field did not contain a boolean, this
1276  * function returns FALSE.
1277  */
1278 gboolean
1279 gst_structure_get_boolean (const GstStructure * structure,
1280     const gchar * fieldname, gboolean * value)
1281 {
1282   GstStructureField *field;
1283
1284   g_return_val_if_fail (structure != NULL, FALSE);
1285   g_return_val_if_fail (fieldname != NULL, FALSE);
1286
1287   field = gst_structure_get_field (structure, fieldname);
1288
1289   if (field == NULL)
1290     return FALSE;
1291   if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1292     return FALSE;
1293
1294   *value = gst_g_value_get_boolean_unchecked (&field->value);
1295
1296   return TRUE;
1297 }
1298
1299 /**
1300  * gst_structure_get_int:
1301  * @structure: a #GstStructure
1302  * @fieldname: the name of a field
1303  * @value: (out): a pointer to an int to set
1304  *
1305  * Sets the int pointed to by @value corresponding to the value of the
1306  * given field.  Caller is responsible for making sure the field exists
1307  * and has the correct type.
1308  *
1309  * Returns: %TRUE if the value could be set correctly. If there was no field
1310  * with @fieldname or the existing field did not contain an int, this function
1311  * returns %FALSE.
1312  */
1313 gboolean
1314 gst_structure_get_int (const GstStructure * structure,
1315     const gchar * fieldname, gint * value)
1316 {
1317   GstStructureField *field;
1318
1319   g_return_val_if_fail (structure != NULL, FALSE);
1320   g_return_val_if_fail (fieldname != NULL, FALSE);
1321   g_return_val_if_fail (value != NULL, FALSE);
1322
1323   field = gst_structure_get_field (structure, fieldname);
1324
1325   if (field == NULL)
1326     return FALSE;
1327   if (!G_VALUE_HOLDS_INT (&field->value))
1328     return FALSE;
1329
1330   *value = gst_g_value_get_int_unchecked (&field->value);
1331
1332   return TRUE;
1333 }
1334
1335 /**
1336  * gst_structure_get_uint:
1337  * @structure: a #GstStructure
1338  * @fieldname: the name of a field
1339  * @value: (out): a pointer to a uint to set
1340  *
1341  * Sets the uint pointed to by @value corresponding to the value of the
1342  * given field.  Caller is responsible for making sure the field exists
1343  * and has the correct type.
1344  *
1345  * Returns: %TRUE if the value could be set correctly. If there was no field
1346  * with @fieldname or the existing field did not contain a uint, this function
1347  * returns %FALSE.
1348  */
1349 gboolean
1350 gst_structure_get_uint (const GstStructure * structure,
1351     const gchar * fieldname, guint * value)
1352 {
1353   GstStructureField *field;
1354
1355   g_return_val_if_fail (structure != NULL, FALSE);
1356   g_return_val_if_fail (fieldname != NULL, FALSE);
1357   g_return_val_if_fail (value != NULL, FALSE);
1358
1359   field = gst_structure_get_field (structure, fieldname);
1360
1361   if (field == NULL)
1362     return FALSE;
1363   if (!G_VALUE_HOLDS_UINT (&field->value))
1364     return FALSE;
1365
1366   *value = gst_g_value_get_uint_unchecked (&field->value);
1367
1368   return TRUE;
1369 }
1370
1371 /**
1372  * gst_structure_get_int64:
1373  * @structure: a #GstStructure
1374  * @fieldname: the name of a field
1375  * @value: (out): a pointer to an int64 to set
1376  *
1377  * Sets the int64 pointed to by @value corresponding to the value of the
1378  * given field. Caller is responsible for making sure the field exists
1379  * and has the correct type.
1380  *
1381  * Returns: %TRUE if the value could be set correctly. If there was no field
1382  * with @fieldname or the existing field did not contain an int64, this function
1383  * returns %FALSE.
1384  *
1385  * Since: 1.4
1386  */
1387 gboolean
1388 gst_structure_get_int64 (const GstStructure * structure,
1389     const gchar * fieldname, gint64 * value)
1390 {
1391   GstStructureField *field;
1392
1393   g_return_val_if_fail (structure != NULL, FALSE);
1394   g_return_val_if_fail (fieldname != NULL, FALSE);
1395   g_return_val_if_fail (value != NULL, FALSE);
1396
1397   field = gst_structure_get_field (structure, fieldname);
1398
1399   if (field == NULL)
1400     return FALSE;
1401   if (!G_VALUE_HOLDS_INT64 (&field->value))
1402     return FALSE;
1403
1404   *value = gst_g_value_get_int64_unchecked (&field->value);
1405
1406   return TRUE;
1407 }
1408
1409 /**
1410  * gst_structure_get_uint64:
1411  * @structure: a #GstStructure
1412  * @fieldname: the name of a field
1413  * @value: (out): a pointer to a uint64 to set
1414  *
1415  * Sets the uint64 pointed to by @value corresponding to the value of the
1416  * given field. Caller is responsible for making sure the field exists
1417  * and has the correct type.
1418  *
1419  * Returns: %TRUE if the value could be set correctly. If there was no field
1420  * with @fieldname or the existing field did not contain a uint64, this function
1421  * returns %FALSE.
1422  *
1423  * Since: 1.4
1424  */
1425 gboolean
1426 gst_structure_get_uint64 (const GstStructure * structure,
1427     const gchar * fieldname, guint64 * value)
1428 {
1429   GstStructureField *field;
1430
1431   g_return_val_if_fail (structure != NULL, FALSE);
1432   g_return_val_if_fail (fieldname != NULL, FALSE);
1433   g_return_val_if_fail (value != NULL, FALSE);
1434
1435   field = gst_structure_get_field (structure, fieldname);
1436
1437   if (field == NULL)
1438     return FALSE;
1439   if (!G_VALUE_HOLDS_UINT64 (&field->value))
1440     return FALSE;
1441
1442   *value = gst_g_value_get_uint64_unchecked (&field->value);
1443
1444   return TRUE;
1445 }
1446
1447 /**
1448  * gst_structure_get_date:
1449  * @structure: a #GstStructure
1450  * @fieldname: the name of a field
1451  * @value: (out callee-allocates): a pointer to a #GDate to set
1452  *
1453  * Sets the date pointed to by @value corresponding to the date of the
1454  * given field.  Caller is responsible for making sure the field exists
1455  * and has the correct type.
1456  *
1457  * On success @value will point to a newly-allocated copy of the date which
1458  * should be freed with g_date_free() when no longer needed (note: this is
1459  * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1460  * copy of the string).
1461  *
1462  * Returns: TRUE if the value could be set correctly. If there was no field
1463  * with @fieldname or the existing field did not contain a data, this function
1464  * returns FALSE.
1465  */
1466 gboolean
1467 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1468     GDate ** value)
1469 {
1470   GstStructureField *field;
1471
1472   g_return_val_if_fail (structure != NULL, FALSE);
1473   g_return_val_if_fail (fieldname != NULL, FALSE);
1474   g_return_val_if_fail (value != NULL, FALSE);
1475
1476   field = gst_structure_get_field (structure, fieldname);
1477
1478   if (field == NULL)
1479     return FALSE;
1480   if (!G_VALUE_HOLDS (&field->value, G_TYPE_DATE))
1481     return FALSE;
1482
1483   /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1484   *value = g_value_dup_boxed (&field->value);
1485
1486   return TRUE;
1487 }
1488
1489 /**
1490  * gst_structure_get_date_time:
1491  * @structure: a #GstStructure
1492  * @fieldname: the name of a field
1493  * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1494  *
1495  * Sets the datetime pointed to by @value corresponding to the datetime of the
1496  * given field. Caller is responsible for making sure the field exists
1497  * and has the correct type.
1498  *
1499  * On success @value will point to a reference of the datetime which
1500  * should be unreffed with gst_date_time_unref() when no longer needed
1501  * (note: this is inconsistent with e.g. gst_structure_get_string()
1502  * which doesn't return a copy of the string).
1503  *
1504  * Returns: TRUE if the value could be set correctly. If there was no field
1505  * with @fieldname or the existing field did not contain a data, this function
1506  * returns FALSE.
1507  */
1508 gboolean
1509 gst_structure_get_date_time (const GstStructure * structure,
1510     const gchar * fieldname, GstDateTime ** value)
1511 {
1512   GstStructureField *field;
1513
1514   g_return_val_if_fail (structure != NULL, FALSE);
1515   g_return_val_if_fail (fieldname != NULL, FALSE);
1516   g_return_val_if_fail (value != NULL, FALSE);
1517
1518   field = gst_structure_get_field (structure, fieldname);
1519
1520   if (field == NULL)
1521     return FALSE;
1522   if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1523     return FALSE;
1524
1525   /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1526   *value = g_value_dup_boxed (&field->value);
1527
1528   return TRUE;
1529 }
1530
1531 /**
1532  * gst_structure_get_clock_time:
1533  * @structure: a #GstStructure
1534  * @fieldname: the name of a field
1535  * @value: (out): a pointer to a #GstClockTime to set
1536  *
1537  * Sets the clock time pointed to by @value corresponding to the clock time
1538  * of the given field.  Caller is responsible for making sure the field exists
1539  * and has the correct type.
1540  *
1541  * Returns: TRUE if the value could be set correctly. If there was no field
1542  * with @fieldname or the existing field did not contain a #GstClockTime, this
1543  * function returns FALSE.
1544  */
1545 gboolean
1546 gst_structure_get_clock_time (const GstStructure * structure,
1547     const gchar * fieldname, GstClockTime * value)
1548 {
1549   GstStructureField *field;
1550
1551   g_return_val_if_fail (structure != NULL, FALSE);
1552   g_return_val_if_fail (fieldname != NULL, FALSE);
1553   g_return_val_if_fail (value != NULL, FALSE);
1554
1555   field = gst_structure_get_field (structure, fieldname);
1556
1557   if (field == NULL)
1558     return FALSE;
1559   if (!G_VALUE_HOLDS_UINT64 (&field->value))
1560     return FALSE;
1561
1562   *value = gst_g_value_get_uint64_unchecked (&field->value);
1563
1564   return TRUE;
1565 }
1566
1567 /**
1568  * gst_structure_get_double:
1569  * @structure: a #GstStructure
1570  * @fieldname: the name of a field
1571  * @value: (out): a pointer to a gdouble to set
1572  *
1573  * Sets the double pointed to by @value corresponding to the value of the
1574  * given field.  Caller is responsible for making sure the field exists
1575  * and has the correct type.
1576  *
1577  * Returns: TRUE if the value could be set correctly. If there was no field
1578  * with @fieldname or the existing field did not contain a double, this
1579  * function returns FALSE.
1580  */
1581 gboolean
1582 gst_structure_get_double (const GstStructure * structure,
1583     const gchar * fieldname, gdouble * value)
1584 {
1585   GstStructureField *field;
1586
1587   g_return_val_if_fail (structure != NULL, FALSE);
1588   g_return_val_if_fail (fieldname != NULL, FALSE);
1589   g_return_val_if_fail (value != NULL, FALSE);
1590
1591   field = gst_structure_get_field (structure, fieldname);
1592
1593   if (field == NULL)
1594     return FALSE;
1595   if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1596     return FALSE;
1597
1598   *value = gst_g_value_get_double_unchecked (&field->value);
1599
1600   return TRUE;
1601 }
1602
1603 /**
1604  * gst_structure_get_string:
1605  * @structure: a #GstStructure
1606  * @fieldname: the name of a field
1607  *
1608  * Finds the field corresponding to @fieldname, and returns the string
1609  * contained in the field's value.  Caller is responsible for making
1610  * sure the field exists and has the correct type.
1611  *
1612  * The string should not be modified, and remains valid until the next
1613  * call to a gst_structure_*() function with the given structure.
1614  *
1615  * Returns: a pointer to the string or NULL when the field did not exist
1616  * or did not contain a string.
1617  */
1618 const gchar *
1619 gst_structure_get_string (const GstStructure * structure,
1620     const gchar * fieldname)
1621 {
1622   GstStructureField *field;
1623
1624   g_return_val_if_fail (structure != NULL, NULL);
1625   g_return_val_if_fail (fieldname != NULL, NULL);
1626
1627   field = gst_structure_get_field (structure, fieldname);
1628
1629   if (field == NULL)
1630     return NULL;
1631   if (!G_VALUE_HOLDS_STRING (&field->value))
1632     return NULL;
1633
1634   return gst_g_value_get_string_unchecked (&field->value);
1635 }
1636
1637 /**
1638  * gst_structure_get_enum:
1639  * @structure: a #GstStructure
1640  * @fieldname: the name of a field
1641  * @enumtype: the enum type of a field
1642  * @value: (out): a pointer to an int to set
1643  *
1644  * Sets the int pointed to by @value corresponding to the value of the
1645  * given field.  Caller is responsible for making sure the field exists,
1646  * has the correct type and that the enumtype is correct.
1647  *
1648  * Returns: TRUE if the value could be set correctly. If there was no field
1649  * with @fieldname or the existing field did not contain an enum of the given
1650  * type, this function returns FALSE.
1651  */
1652 gboolean
1653 gst_structure_get_enum (const GstStructure * structure,
1654     const gchar * fieldname, GType enumtype, gint * value)
1655 {
1656   GstStructureField *field;
1657
1658   g_return_val_if_fail (structure != NULL, FALSE);
1659   g_return_val_if_fail (fieldname != NULL, FALSE);
1660   g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1661   g_return_val_if_fail (value != NULL, FALSE);
1662
1663   field = gst_structure_get_field (structure, fieldname);
1664
1665   if (field == NULL)
1666     return FALSE;
1667   if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1668     return FALSE;
1669
1670   *value = g_value_get_enum (&field->value);
1671
1672   return TRUE;
1673 }
1674
1675 /**
1676  * gst_structure_get_fraction:
1677  * @structure: a #GstStructure
1678  * @fieldname: the name of a field
1679  * @value_numerator: (out): a pointer to an int to set
1680  * @value_denominator: (out): a pointer to an int to set
1681  *
1682  * Sets the integers pointed to by @value_numerator and @value_denominator
1683  * corresponding to the value of the given field.  Caller is responsible
1684  * for making sure the field exists and has the correct type.
1685  *
1686  * Returns: TRUE if the values could be set correctly. If there was no field
1687  * with @fieldname or the existing field did not contain a GstFraction, this
1688  * function returns FALSE.
1689  */
1690 gboolean
1691 gst_structure_get_fraction (const GstStructure * structure,
1692     const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1693 {
1694   GstStructureField *field;
1695
1696   g_return_val_if_fail (structure != NULL, FALSE);
1697   g_return_val_if_fail (fieldname != NULL, FALSE);
1698   g_return_val_if_fail (value_numerator != NULL, FALSE);
1699   g_return_val_if_fail (value_denominator != NULL, FALSE);
1700
1701   field = gst_structure_get_field (structure, fieldname);
1702
1703   if (field == NULL)
1704     return FALSE;
1705   if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1706     return FALSE;
1707
1708   *value_numerator = gst_value_get_fraction_numerator (&field->value);
1709   *value_denominator = gst_value_get_fraction_denominator (&field->value);
1710
1711   return TRUE;
1712 }
1713
1714 typedef struct _GstStructureAbbreviation
1715 {
1716   const gchar *type_name;
1717   GType type;
1718 }
1719 GstStructureAbbreviation;
1720
1721 /* return a copy of an array of GstStructureAbbreviation containing all the
1722  * known type_string, GType maps, including abbreviations for common types */
1723 static GstStructureAbbreviation *
1724 gst_structure_get_abbrs (gint * n_abbrs)
1725 {
1726   static GstStructureAbbreviation *abbrs = NULL;
1727   static volatile gsize num = 0;
1728
1729   if (g_once_init_enter (&num)) {
1730     /* dynamically generate the array */
1731     gsize _num;
1732     GstStructureAbbreviation dyn_abbrs[] = {
1733       {"int", G_TYPE_INT}
1734       ,
1735       {"i", G_TYPE_INT}
1736       ,
1737       {"uint", G_TYPE_UINT}
1738       ,
1739       {"u", G_TYPE_UINT}
1740       ,
1741       {"float", G_TYPE_FLOAT}
1742       ,
1743       {"f", G_TYPE_FLOAT}
1744       ,
1745       {"double", G_TYPE_DOUBLE}
1746       ,
1747       {"d", G_TYPE_DOUBLE}
1748       ,
1749       {"buffer", GST_TYPE_BUFFER}
1750       ,
1751       {"fraction", GST_TYPE_FRACTION}
1752       ,
1753       {"boolean", G_TYPE_BOOLEAN}
1754       ,
1755       {"bool", G_TYPE_BOOLEAN}
1756       ,
1757       {"b", G_TYPE_BOOLEAN}
1758       ,
1759       {"string", G_TYPE_STRING}
1760       ,
1761       {"str", G_TYPE_STRING}
1762       ,
1763       {"s", G_TYPE_STRING}
1764       ,
1765       {"structure", GST_TYPE_STRUCTURE}
1766       ,
1767       {"date", G_TYPE_DATE}
1768       ,
1769       {"datetime", GST_TYPE_DATE_TIME}
1770       ,
1771       {"bitmask", GST_TYPE_BITMASK}
1772       ,
1773       {"sample", GST_TYPE_SAMPLE}
1774       ,
1775       {"taglist", GST_TYPE_TAG_LIST}
1776     };
1777     _num = G_N_ELEMENTS (dyn_abbrs);
1778     /* permanently allocate and copy the array now */
1779     abbrs = g_new0 (GstStructureAbbreviation, _num);
1780     memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1781     g_once_init_leave (&num, _num);
1782   }
1783   *n_abbrs = num;
1784
1785   return abbrs;
1786 }
1787
1788 /* given a type_name that could be a type abbreviation or a registered GType,
1789  * return a matching GType */
1790 static GType
1791 gst_structure_gtype_from_abbr (const char *type_name)
1792 {
1793   int i;
1794   GstStructureAbbreviation *abbrs;
1795   gint n_abbrs;
1796
1797   g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1798
1799   abbrs = gst_structure_get_abbrs (&n_abbrs);
1800
1801   for (i = 0; i < n_abbrs; i++) {
1802     if (strcmp (type_name, abbrs[i].type_name) == 0) {
1803       return abbrs[i].type;
1804     }
1805   }
1806
1807   /* this is the fallback */
1808   return g_type_from_name (type_name);
1809 }
1810
1811 static const char *
1812 gst_structure_to_abbr (GType type)
1813 {
1814   int i;
1815   GstStructureAbbreviation *abbrs;
1816   gint n_abbrs;
1817
1818   g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1819
1820   abbrs = gst_structure_get_abbrs (&n_abbrs);
1821
1822   for (i = 0; i < n_abbrs; i++) {
1823     if (type == abbrs[i].type) {
1824       return abbrs[i].type_name;
1825     }
1826   }
1827
1828   return g_type_name (type);
1829 }
1830
1831 static GType
1832 gst_structure_value_get_generic_type (GValue * val)
1833 {
1834   if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1835       || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1836     GArray *array = g_value_peek_pointer (val);
1837
1838     if (array->len > 0) {
1839       GValue *value = &g_array_index (array, GValue, 0);
1840
1841       return gst_structure_value_get_generic_type (value);
1842     } else {
1843       return G_TYPE_INT;
1844     }
1845   } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1846     return G_TYPE_INT;
1847   } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1848     return G_TYPE_INT64;
1849   } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1850     return G_TYPE_DOUBLE;
1851   } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1852     return GST_TYPE_FRACTION;
1853   }
1854   return G_VALUE_TYPE (val);
1855 }
1856
1857 gboolean
1858 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1859     GString * s)
1860 {
1861   GstStructureField *field;
1862   guint i, len;
1863
1864   g_return_val_if_fail (s != NULL, FALSE);
1865
1866   len = GST_STRUCTURE_FIELDS (structure)->len;
1867   for (i = 0; i < len; i++) {
1868     char *t;
1869     GType type;
1870
1871     field = GST_STRUCTURE_FIELD (structure, i);
1872
1873     t = gst_value_serialize (&field->value);
1874     type = gst_structure_value_get_generic_type (&field->value);
1875
1876     g_string_append_len (s, ", ", 2);
1877     /* FIXME: do we need to escape fieldnames? */
1878     g_string_append (s, g_quark_to_string (field->name));
1879     g_string_append_len (s, "=(", 2);
1880     g_string_append (s, gst_structure_to_abbr (type));
1881     g_string_append_c (s, ')');
1882     g_string_append (s, t == NULL ? "NULL" : t);
1883     g_free (t);
1884   }
1885
1886   g_string_append_c (s, ';');
1887   return TRUE;
1888 }
1889
1890 /**
1891  * gst_structure_to_string:
1892  * @structure: a #GstStructure
1893  *
1894  * Converts @structure to a human-readable string representation.
1895  *
1896  * For debugging purposes its easier to do something like this:
1897  * |[
1898  * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1899  * ]|
1900  * This prints the structure in human readable form.
1901  *
1902  * The current implementation of serialization will lead to unexpected results
1903  * when there are nested #GstCaps / #GstStructure deeper than one level.
1904  *
1905  * Free-function: g_free
1906  *
1907  * Returns: (transfer full): a pointer to string allocated by g_malloc().
1908  *     g_free() after usage.
1909  */
1910 gchar *
1911 gst_structure_to_string (const GstStructure * structure)
1912 {
1913   GString *s;
1914
1915   /* NOTE:  This function is potentially called by the debug system,
1916    * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1917    * should be careful to avoid recursion.  This includes any functions
1918    * called by gst_structure_to_string.  In particular, calls should
1919    * not use the GST_PTR_FORMAT extension.  */
1920
1921   g_return_val_if_fail (structure != NULL, NULL);
1922
1923   /* we estimate a minimum size based on the number of fields in order to
1924    * avoid unnecessary reallocs within GString */
1925   s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1926   g_string_append (s, g_quark_to_string (structure->name));
1927   priv_gst_structure_append_to_gstring (structure, s);
1928   return g_string_free (s, FALSE);
1929 }
1930
1931 /*
1932  * r will still point to the string. if end == next, the string will not be
1933  * null-terminated. In all other cases it will be.
1934  * end = pointer to char behind end of string, next = pointer to start of
1935  * unread data.
1936  * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1937  */
1938 static gboolean
1939 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1940     gboolean unescape)
1941 {
1942   gchar *w;
1943
1944   if (*s == 0)
1945     return FALSE;
1946
1947   if (*s != '"') {
1948     int ret;
1949
1950     ret = gst_structure_parse_simple_string (s, end);
1951     *next = *end;
1952
1953     return ret;
1954   }
1955
1956   if (unescape) {
1957     w = s;
1958     s++;
1959     while (*s != '"') {
1960       if (G_UNLIKELY (*s == 0))
1961         return FALSE;
1962       if (G_UNLIKELY (*s == '\\'))
1963         s++;
1964       *w = *s;
1965       w++;
1966       s++;
1967     }
1968     s++;
1969   } else {
1970     /* Find the closing quotes */
1971     s++;
1972     while (*s != '"') {
1973       if (G_UNLIKELY (*s == 0))
1974         return FALSE;
1975       if (G_UNLIKELY (*s == '\\'))
1976         s++;
1977       s++;
1978     }
1979     s++;
1980     w = s;
1981   }
1982
1983   *end = w;
1984   *next = s;
1985
1986   return TRUE;
1987 }
1988
1989 static gboolean
1990 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1991     GType type)
1992 {
1993   GValue value1 = { 0 };
1994   GValue value2 = { 0 };
1995   GValue value3 = { 0 };
1996   GType range_type;
1997   gboolean ret, have_step = FALSE;
1998
1999   if (*s != '[')
2000     return FALSE;
2001   s++;
2002
2003   ret = gst_structure_parse_value (s, &s, &value1, type);
2004   if (ret == FALSE)
2005     return FALSE;
2006
2007   while (g_ascii_isspace (*s))
2008     s++;
2009
2010   if (*s != ',')
2011     return FALSE;
2012   s++;
2013
2014   while (g_ascii_isspace (*s))
2015     s++;
2016
2017   ret = gst_structure_parse_value (s, &s, &value2, type);
2018   if (ret == FALSE)
2019     return FALSE;
2020
2021   while (g_ascii_isspace (*s))
2022     s++;
2023
2024   /* optional step for int and int64 */
2025   if (G_VALUE_TYPE (&value1) == G_TYPE_INT
2026       || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2027     if (*s == ',') {
2028       s++;
2029
2030       while (g_ascii_isspace (*s))
2031         s++;
2032
2033       ret = gst_structure_parse_value (s, &s, &value3, type);
2034       if (ret == FALSE)
2035         return FALSE;
2036
2037       while (g_ascii_isspace (*s))
2038         s++;
2039
2040       have_step = TRUE;
2041     }
2042   }
2043
2044   if (*s != ']')
2045     return FALSE;
2046   s++;
2047
2048   if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
2049     return FALSE;
2050   if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
2051     return FALSE;
2052
2053   if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
2054     range_type = GST_TYPE_DOUBLE_RANGE;
2055     g_value_init (value, range_type);
2056     gst_value_set_double_range (value,
2057         gst_g_value_get_double_unchecked (&value1),
2058         gst_g_value_get_double_unchecked (&value2));
2059   } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
2060     range_type = GST_TYPE_INT_RANGE;
2061     g_value_init (value, range_type);
2062     if (have_step)
2063       gst_value_set_int_range_step (value,
2064           gst_g_value_get_int_unchecked (&value1),
2065           gst_g_value_get_int_unchecked (&value2),
2066           gst_g_value_get_int_unchecked (&value3));
2067     else
2068       gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
2069           gst_g_value_get_int_unchecked (&value2));
2070   } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2071     range_type = GST_TYPE_INT64_RANGE;
2072     g_value_init (value, range_type);
2073     if (have_step)
2074       gst_value_set_int64_range_step (value,
2075           gst_g_value_get_int64_unchecked (&value1),
2076           gst_g_value_get_int64_unchecked (&value2),
2077           gst_g_value_get_int64_unchecked (&value3));
2078     else
2079       gst_value_set_int64_range (value,
2080           gst_g_value_get_int64_unchecked (&value1),
2081           gst_g_value_get_int64_unchecked (&value2));
2082   } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
2083     range_type = GST_TYPE_FRACTION_RANGE;
2084     g_value_init (value, range_type);
2085     gst_value_set_fraction_range (value, &value1, &value2);
2086   } else {
2087     return FALSE;
2088   }
2089
2090   *after = s;
2091   return TRUE;
2092 }
2093
2094 static gboolean
2095 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
2096     GType type, GType list_type, char begin, char end)
2097 {
2098   GValue list_value = { 0 };
2099   gboolean ret;
2100   GArray *array;
2101
2102   g_value_init (value, list_type);
2103   array = g_value_peek_pointer (value);
2104
2105   if (*s != begin)
2106     return FALSE;
2107   s++;
2108
2109   while (g_ascii_isspace (*s))
2110     s++;
2111   if (*s == end) {
2112     s++;
2113     *after = s;
2114     return TRUE;
2115   }
2116
2117   ret = gst_structure_parse_value (s, &s, &list_value, type);
2118   if (ret == FALSE)
2119     return FALSE;
2120
2121   g_array_append_val (array, list_value);
2122
2123   while (g_ascii_isspace (*s))
2124     s++;
2125
2126   while (*s != end) {
2127     if (*s != ',')
2128       return FALSE;
2129     s++;
2130
2131     while (g_ascii_isspace (*s))
2132       s++;
2133
2134     memset (&list_value, 0, sizeof (list_value));
2135     ret = gst_structure_parse_value (s, &s, &list_value, type);
2136     if (ret == FALSE)
2137       return FALSE;
2138
2139     g_array_append_val (array, list_value);
2140     while (g_ascii_isspace (*s))
2141       s++;
2142   }
2143
2144   s++;
2145
2146   *after = s;
2147   return TRUE;
2148 }
2149
2150 static gboolean
2151 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2152 {
2153   return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2154       '{', '}');
2155 }
2156
2157 static gboolean
2158 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2159     GType type)
2160 {
2161   return gst_structure_parse_any_list (s, after, value, type,
2162       GST_TYPE_ARRAY, '<', '>');
2163 }
2164
2165 static gboolean
2166 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2167 {
2168   char *s = str;
2169
2170   while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2171     s++;
2172   }
2173
2174   *end = s;
2175
2176   return (s != str);
2177 }
2178
2179 static gboolean
2180 gst_structure_parse_field (gchar * str,
2181     gchar ** after, GstStructureField * field)
2182 {
2183   gchar *name;
2184   gchar *name_end;
2185   gchar *s;
2186   gchar c;
2187
2188   s = str;
2189
2190   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2191     s++;
2192   name = s;
2193   if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2194     GST_WARNING ("failed to parse simple string, str=%s", str);
2195     return FALSE;
2196   }
2197
2198   s = name_end;
2199   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2200     s++;
2201
2202   if (G_UNLIKELY (*s != '=')) {
2203     GST_WARNING ("missing assignment operator in the field, str=%s", str);
2204     return FALSE;
2205   }
2206   s++;
2207
2208   c = *name_end;
2209   *name_end = '\0';
2210   field->name = g_quark_from_string (name);
2211   GST_DEBUG ("trying field name '%s'", name);
2212   *name_end = c;
2213
2214   if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2215               G_TYPE_INVALID))) {
2216     GST_WARNING ("failed to parse value %s", str);
2217     return FALSE;
2218   }
2219
2220   *after = s;
2221   return TRUE;
2222 }
2223
2224 static gboolean
2225 gst_structure_parse_value (gchar * str,
2226     gchar ** after, GValue * value, GType default_type)
2227 {
2228   gchar *type_name;
2229   gchar *type_end;
2230   gchar *value_s;
2231   gchar *value_end;
2232   gchar *s;
2233   gchar c;
2234   int ret = 0;
2235   GType type = default_type;
2236
2237   s = str;
2238   while (g_ascii_isspace (*s))
2239     s++;
2240
2241   /* check if there's a (type_name) 'cast' */
2242   type_name = NULL;
2243   if (*s == '(') {
2244     s++;
2245     while (g_ascii_isspace (*s))
2246       s++;
2247     type_name = s;
2248     if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2249       return FALSE;
2250     s = type_end;
2251     while (g_ascii_isspace (*s))
2252       s++;
2253     if (G_UNLIKELY (*s != ')'))
2254       return FALSE;
2255     s++;
2256     while (g_ascii_isspace (*s))
2257       s++;
2258
2259     c = *type_end;
2260     *type_end = 0;
2261     type = gst_structure_gtype_from_abbr (type_name);
2262     GST_DEBUG ("trying type name '%s'", type_name);
2263     *type_end = c;
2264
2265     if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2266       GST_WARNING ("invalid type");
2267       return FALSE;
2268     }
2269   }
2270
2271   while (g_ascii_isspace (*s))
2272     s++;
2273   if (*s == '[') {
2274     ret = gst_structure_parse_range (s, &s, value, type);
2275   } else if (*s == '{') {
2276     ret = gst_structure_parse_list (s, &s, value, type);
2277   } else if (*s == '<') {
2278     ret = gst_structure_parse_array (s, &s, value, type);
2279   } else {
2280     value_s = s;
2281
2282     if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2283       GType try_types[] =
2284           { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2285         G_TYPE_STRING
2286       };
2287       int i;
2288
2289       if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2290         return FALSE;
2291       /* Set NULL terminator for deserialization */
2292       c = *value_end;
2293       *value_end = '\0';
2294
2295       for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2296         g_value_init (value, try_types[i]);
2297         ret = gst_value_deserialize (value, value_s);
2298         if (ret)
2299           break;
2300         g_value_unset (value);
2301       }
2302     } else {
2303       g_value_init (value, type);
2304
2305       if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2306                   (type != G_TYPE_STRING))))
2307         return FALSE;
2308       /* Set NULL terminator for deserialization */
2309       c = *value_end;
2310       *value_end = '\0';
2311
2312       ret = gst_value_deserialize (value, value_s);
2313       if (G_UNLIKELY (!ret))
2314         g_value_unset (value);
2315     }
2316     *value_end = c;
2317   }
2318
2319   *after = s;
2320
2321   return ret;
2322 }
2323
2324 gboolean
2325 priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end,
2326     gchar ** next)
2327 {
2328   char *w;
2329   char *r;
2330
2331   r = str;
2332
2333   /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2334   while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2335               && g_ascii_isspace (r[1]))))
2336     r++;
2337
2338   *start = r;
2339
2340   if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2341     GST_WARNING ("Failed to parse structure string '%s'", str);
2342     return FALSE;
2343   }
2344
2345   *end = w;
2346   *next = r;
2347
2348   return TRUE;
2349 }
2350
2351 gboolean
2352 priv_gst_structure_parse_fields (gchar * str, gchar ** end,
2353     GstStructure * structure)
2354 {
2355   gchar *r;
2356   GstStructureField field;
2357
2358   r = str;
2359
2360   do {
2361     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2362                 && g_ascii_isspace (r[1]))))
2363       r++;
2364     if (*r == ';') {
2365       /* end of structure, get the next char and finish */
2366       r++;
2367       break;
2368     }
2369     if (*r == '\0') {
2370       /* accept \0 as end delimiter */
2371       break;
2372     }
2373     if (G_UNLIKELY (*r != ',')) {
2374       GST_WARNING ("Failed to find delimiter, r=%s", r);
2375       return FALSE;
2376     }
2377     r++;
2378     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2379                 && g_ascii_isspace (r[1]))))
2380       r++;
2381
2382     memset (&field, 0, sizeof (field));
2383     if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2384       GST_WARNING ("Failed to parse field, r=%s", r);
2385       return FALSE;
2386     }
2387     gst_structure_set_field (structure, &field);
2388   } while (TRUE);
2389
2390   *end = r;
2391
2392   return TRUE;
2393 }
2394
2395 /**
2396  * gst_structure_new_from_string:
2397  * @string: a string representation of a #GstStructure
2398  *
2399  * Creates a #GstStructure from a string representation.
2400  * If end is not NULL, a pointer to the place inside the given string
2401  * where parsing ended will be returned.
2402  *
2403  * The current implementation of serialization will lead to unexpected results
2404  * when there are nested #GstCaps / #GstStructure deeper than one level.
2405  *
2406  * Free-function: gst_structure_free
2407  *
2408  * Returns: (transfer full): a new #GstStructure or NULL when the string could
2409  *     not be parsed. Free with gst_structure_free() after use.
2410  *
2411  * Since: 1.2
2412  */
2413 GstStructure *
2414 gst_structure_new_from_string (const gchar * string)
2415 {
2416   return gst_structure_from_string (string, NULL);
2417 }
2418
2419 /**
2420  * gst_structure_from_string:
2421  * @string: a string representation of a #GstStructure.
2422  * @end: (out) (allow-none) (transfer none) (skip): pointer to store the end of the string in.
2423  *
2424  * Creates a #GstStructure from a string representation.
2425  * If end is not NULL, a pointer to the place inside the given string
2426  * where parsing ended will be returned.
2427  *
2428  * Free-function: gst_structure_free
2429  *
2430  * Returns: (transfer full): a new #GstStructure or NULL when the string could
2431  *     not be parsed. Free with gst_structure_free() after use.
2432  */
2433 GstStructure *
2434 gst_structure_from_string (const gchar * string, gchar ** end)
2435 {
2436   char *name;
2437   char *copy;
2438   char *w;
2439   char *r;
2440   char save;
2441   GstStructure *structure = NULL;
2442
2443   g_return_val_if_fail (string != NULL, NULL);
2444
2445   copy = g_strdup (string);
2446   r = copy;
2447
2448   if (!priv_gst_structure_parse_name (r, &name, &w, &r))
2449     goto error;
2450
2451   save = *w;
2452   *w = '\0';
2453   structure = gst_structure_new_empty (name);
2454   *w = save;
2455
2456   if (G_UNLIKELY (structure == NULL))
2457     goto error;
2458
2459   if (!priv_gst_structure_parse_fields (r, &r, structure))
2460     goto error;
2461
2462   if (end)
2463     *end = (char *) string + (r - copy);
2464   else if (*r)
2465     g_warning ("gst_structure_from_string did not consume whole string,"
2466         " but caller did not provide end pointer (\"%s\")", string);
2467
2468   g_free (copy);
2469   return structure;
2470
2471 error:
2472   if (structure)
2473     gst_structure_free (structure);
2474   g_free (copy);
2475   return NULL;
2476 }
2477
2478 static void
2479 gst_structure_transform_to_string (const GValue * src_value,
2480     GValue * dest_value)
2481 {
2482   g_return_if_fail (src_value != NULL);
2483   g_return_if_fail (dest_value != NULL);
2484
2485   dest_value->data[0].v_pointer =
2486       gst_structure_to_string (src_value->data[0].v_pointer);
2487 }
2488
2489 static GstStructure *
2490 gst_structure_copy_conditional (const GstStructure * structure)
2491 {
2492   if (structure)
2493     return gst_structure_copy (structure);
2494   return NULL;
2495 }
2496
2497 /* fixate utility functions */
2498
2499 /**
2500  * gst_structure_fixate_field_nearest_int:
2501  * @structure: a #GstStructure
2502  * @field_name: a field in @structure
2503  * @target: the target value of the fixation
2504  *
2505  * Fixates a #GstStructure by changing the given field to the nearest
2506  * integer to @target that is a subset of the existing field.
2507  *
2508  * Returns: TRUE if the structure could be fixated
2509  */
2510 gboolean
2511 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2512     const char *field_name, int target)
2513 {
2514   const GValue *value;
2515
2516   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2517   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2518
2519   value = gst_structure_get_value (structure, field_name);
2520
2521   if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2522     /* already fixed */
2523     return FALSE;
2524   } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2525     int x;
2526
2527     x = gst_value_get_int_range_min (value);
2528     if (target < x)
2529       target = x;
2530     x = gst_value_get_int_range_max (value);
2531     if (target > x)
2532       target = x;
2533     gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2534     return TRUE;
2535   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2536     const GValue *list_value;
2537     int i, n;
2538     int best = 0;
2539     int best_index = -1;
2540
2541     n = gst_value_list_get_size (value);
2542     for (i = 0; i < n; i++) {
2543       list_value = gst_value_list_get_value (value, i);
2544       if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2545         int x = gst_g_value_get_int_unchecked (list_value);
2546
2547         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2548           best_index = i;
2549           best = x;
2550         }
2551       }
2552     }
2553     if (best_index != -1) {
2554       gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2555       return TRUE;
2556     }
2557     return FALSE;
2558   }
2559
2560   return FALSE;
2561 }
2562
2563 /**
2564  * gst_structure_fixate_field_nearest_double:
2565  * @structure: a #GstStructure
2566  * @field_name: a field in @structure
2567  * @target: the target value of the fixation
2568  *
2569  * Fixates a #GstStructure by changing the given field to the nearest
2570  * double to @target that is a subset of the existing field.
2571  *
2572  * Returns: TRUE if the structure could be fixated
2573  */
2574 gboolean
2575 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2576     const char *field_name, double target)
2577 {
2578   const GValue *value;
2579
2580   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2581   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2582
2583   value = gst_structure_get_value (structure, field_name);
2584
2585   if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2586     /* already fixed */
2587     return FALSE;
2588   } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2589     double x;
2590
2591     x = gst_value_get_double_range_min (value);
2592     if (target < x)
2593       target = x;
2594     x = gst_value_get_double_range_max (value);
2595     if (target > x)
2596       target = x;
2597     gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2598     return TRUE;
2599   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2600     const GValue *list_value;
2601     int i, n;
2602     double best = 0;
2603     int best_index = -1;
2604
2605     n = gst_value_list_get_size (value);
2606     for (i = 0; i < n; i++) {
2607       list_value = gst_value_list_get_value (value, i);
2608       if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2609         double x = gst_g_value_get_double_unchecked (list_value);
2610
2611         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2612           best_index = i;
2613           best = x;
2614         }
2615       }
2616     }
2617     if (best_index != -1) {
2618       gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2619       return TRUE;
2620     }
2621     return FALSE;
2622   }
2623
2624   return FALSE;
2625
2626 }
2627
2628 /**
2629  * gst_structure_fixate_field_boolean:
2630  * @structure: a #GstStructure
2631  * @field_name: a field in @structure
2632  * @target: the target value of the fixation
2633  *
2634  * Fixates a #GstStructure by changing the given @field_name field to the given
2635  * @target boolean if that field is not fixed yet.
2636  *
2637  * Returns: TRUE if the structure could be fixated
2638  */
2639 gboolean
2640 gst_structure_fixate_field_boolean (GstStructure * structure,
2641     const char *field_name, gboolean target)
2642 {
2643   const GValue *value;
2644
2645   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2646   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2647
2648   value = gst_structure_get_value (structure, field_name);
2649
2650   if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2651     /* already fixed */
2652     return FALSE;
2653   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2654     const GValue *list_value;
2655     int i, n;
2656     int best = 0;
2657     int best_index = -1;
2658
2659     n = gst_value_list_get_size (value);
2660     for (i = 0; i < n; i++) {
2661       list_value = gst_value_list_get_value (value, i);
2662       if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2663         gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2664
2665         if (best_index == -1 || x == target) {
2666           best_index = i;
2667           best = x;
2668         }
2669       }
2670     }
2671     if (best_index != -1) {
2672       gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2673       return TRUE;
2674     }
2675     return FALSE;
2676   }
2677
2678   return FALSE;
2679 }
2680
2681 /**
2682  * gst_structure_fixate_field_string:
2683  * @structure: a #GstStructure
2684  * @field_name: a field in @structure
2685  * @target: the target value of the fixation
2686  *
2687  * Fixates a #GstStructure by changing the given @field_name field to the given
2688  * @target string if that field is not fixed yet.
2689  *
2690  * Returns: TRUE if the structure could be fixated
2691  */
2692 gboolean
2693 gst_structure_fixate_field_string (GstStructure * structure,
2694     const gchar * field_name, const gchar * target)
2695 {
2696   const GValue *value;
2697
2698   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2699   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2700
2701   value = gst_structure_get_value (structure, field_name);
2702
2703   if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2704     /* already fixed */
2705     return FALSE;
2706   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2707     const GValue *list_value;
2708     int i, n;
2709     const gchar *best = NULL;
2710     int best_index = -1;
2711
2712     n = gst_value_list_get_size (value);
2713     for (i = 0; i < n; i++) {
2714       list_value = gst_value_list_get_value (value, i);
2715       if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2716         const gchar *x = g_value_get_string (list_value);
2717
2718         if (best_index == -1 || g_str_equal (x, target)) {
2719           best_index = i;
2720           best = x;
2721         }
2722       }
2723     }
2724     if (best_index != -1) {
2725       gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2726       return TRUE;
2727     }
2728     return FALSE;
2729   }
2730
2731   return FALSE;
2732 }
2733
2734 /**
2735  * gst_structure_fixate_field_nearest_fraction:
2736  * @structure: a #GstStructure
2737  * @field_name: a field in @structure
2738  * @target_numerator: The numerator of the target value of the fixation
2739  * @target_denominator: The denominator of the target value of the fixation
2740  *
2741  * Fixates a #GstStructure by changing the given field to the nearest
2742  * fraction to @target_numerator/@target_denominator that is a subset
2743  * of the existing field.
2744  *
2745  * Returns: TRUE if the structure could be fixated
2746  */
2747 gboolean
2748 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2749     const char *field_name, const gint target_numerator,
2750     const gint target_denominator)
2751 {
2752   const GValue *value;
2753
2754   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2755   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2756
2757   value = gst_structure_get_value (structure, field_name);
2758
2759   if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2760     /* already fixed */
2761     return FALSE;
2762   } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2763     const GValue *x, *new_value;
2764     GValue target = { 0 };
2765     g_value_init (&target, GST_TYPE_FRACTION);
2766     gst_value_set_fraction (&target, target_numerator, target_denominator);
2767
2768     new_value = &target;
2769     x = gst_value_get_fraction_range_min (value);
2770     if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2771       new_value = x;
2772     x = gst_value_get_fraction_range_max (value);
2773     if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2774       new_value = x;
2775
2776     gst_structure_set_value (structure, field_name, new_value);
2777     g_value_unset (&target);
2778     return TRUE;
2779   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2780     const GValue *list_value;
2781     int i, n;
2782     const GValue *best = NULL;
2783     gdouble target;
2784     gdouble cur_diff;
2785     gdouble best_diff = G_MAXDOUBLE;
2786
2787     target = (gdouble) target_numerator / (gdouble) target_denominator;
2788
2789     GST_DEBUG ("target %g, best %g", target, best_diff);
2790
2791     best = NULL;
2792
2793     n = gst_value_list_get_size (value);
2794     for (i = 0; i < n; i++) {
2795       list_value = gst_value_list_get_value (value, i);
2796       if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2797         gint num, denom;
2798         gdouble list_double;
2799
2800         num = gst_value_get_fraction_numerator (list_value);
2801         denom = gst_value_get_fraction_denominator (list_value);
2802
2803         list_double = ((gdouble) num / (gdouble) denom);
2804         cur_diff = target - list_double;
2805
2806         GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2807
2808         if (cur_diff < 0)
2809           cur_diff = -cur_diff;
2810
2811         if (!best || cur_diff < best_diff) {
2812           GST_DEBUG ("new best %g", list_double);
2813           best = list_value;
2814           best_diff = cur_diff;
2815         }
2816       }
2817     }
2818     if (best != NULL) {
2819       gst_structure_set_value (structure, field_name, best);
2820       return TRUE;
2821     }
2822   }
2823
2824   return FALSE;
2825 }
2826
2827 static gboolean
2828 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2829 {
2830   GstStructure *s = data;
2831   GValue v = { 0 };
2832
2833   if (gst_value_fixate (&v, value)) {
2834     gst_structure_id_take_value (s, field_id, &v);
2835   }
2836   return TRUE;
2837 }
2838
2839 /**
2840  * gst_structure_fixate_field:
2841  * @structure: a #GstStructure
2842  * @field_name: a field in @structure
2843  *
2844  * Fixates a #GstStructure by changing the given field with its fixated value.
2845  *
2846  * Returns: TRUE if the structure field could be fixated
2847  */
2848 gboolean
2849 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2850 {
2851   GstStructureField *field;
2852
2853   g_return_val_if_fail (structure != NULL, FALSE);
2854   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2855
2856   if (!(field = gst_structure_get_field (structure, field_name)))
2857     return FALSE;
2858
2859   return default_fixate (field->name, &field->value, structure);
2860 }
2861
2862 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2863  * (useful for message parsing functions where the return location is user
2864  * supplied and the user may pass NULL if the value isn't of interest) */
2865 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname)           \
2866 G_STMT_START {                                                                \
2867   const GValue *_value = (value);                                             \
2868   guint _flags = (flags);                                                     \
2869   GType _value_type = G_VALUE_TYPE (_value);                                  \
2870   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);           \
2871   const gchar *_lcopy_format = _vtable->lcopy_format;                         \
2872   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };      \
2873   guint _n_values = 0;                                                        \
2874                                                                               \
2875   while (*_lcopy_format != '\0') {                                            \
2876     g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER);                     \
2877     _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer);          \
2878     _lcopy_format++;                                                          \
2879   }                                                                           \
2880   if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2881     *(__error) = g_strdup_printf ("either all or none of the return "         \
2882         "locations for field '%s' need to be NULL", fieldname);               \
2883   } else if (_cvalues[0].v_pointer != NULL) {                                 \
2884     *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags);  \
2885   }                                                                           \
2886 } G_STMT_END
2887
2888 /**
2889  * gst_structure_get_valist:
2890  * @structure: a #GstStructure
2891  * @first_fieldname: the name of the first field to read
2892  * @args: variable arguments
2893  *
2894  * Parses the variable arguments and reads fields from @structure accordingly.
2895  * valist-variant of gst_structure_get(). Look at the documentation of
2896  * gst_structure_get() for more details.
2897  *
2898  * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2899  */
2900 gboolean
2901 gst_structure_get_valist (const GstStructure * structure,
2902     const char *first_fieldname, va_list args)
2903 {
2904   const char *field_name;
2905   GType expected_type = G_TYPE_INVALID;
2906
2907   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2908   g_return_val_if_fail (first_fieldname != NULL, FALSE);
2909
2910   field_name = first_fieldname;
2911   while (field_name) {
2912     const GValue *val = NULL;
2913     gchar *err = NULL;
2914
2915     expected_type = va_arg (args, GType);
2916
2917     val = gst_structure_get_value (structure, field_name);
2918
2919     if (val == NULL)
2920       goto no_such_field;
2921
2922     if (G_VALUE_TYPE (val) != expected_type)
2923       goto wrong_type;
2924
2925     GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2926     if (err) {
2927       g_warning ("%s: %s", G_STRFUNC, err);
2928       g_free (err);
2929       return FALSE;
2930     }
2931
2932     field_name = va_arg (args, const gchar *);
2933   }
2934
2935   return TRUE;
2936
2937 /* ERRORS */
2938 no_such_field:
2939   {
2940     GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2941         field_name, structure);
2942     return FALSE;
2943   }
2944 wrong_type:
2945   {
2946     GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
2947         "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2948         GST_STR_NULL (g_type_name (expected_type)),
2949         G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2950         structure);
2951     return FALSE;
2952   }
2953 }
2954
2955 /**
2956  * gst_structure_id_get_valist:
2957  * @structure: a #GstStructure
2958  * @first_field_id: the quark of the first field to read
2959  * @args: variable arguments
2960  *
2961  * Parses the variable arguments and reads fields from @structure accordingly.
2962  * valist-variant of gst_structure_id_get(). Look at the documentation of
2963  * gst_structure_id_get() for more details.
2964  *
2965  * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2966  */
2967 gboolean
2968 gst_structure_id_get_valist (const GstStructure * structure,
2969     GQuark first_field_id, va_list args)
2970 {
2971   GQuark field_id;
2972   GType expected_type = G_TYPE_INVALID;
2973
2974   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2975   g_return_val_if_fail (first_field_id != 0, FALSE);
2976
2977   field_id = first_field_id;
2978   while (field_id) {
2979     const GValue *val = NULL;
2980     gchar *err = NULL;
2981
2982     expected_type = va_arg (args, GType);
2983
2984     val = gst_structure_id_get_value (structure, field_id);
2985
2986     if (val == NULL)
2987       goto no_such_field;
2988
2989     if (G_VALUE_TYPE (val) != expected_type)
2990       goto wrong_type;
2991
2992     GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2993     if (err) {
2994       g_warning ("%s: %s", G_STRFUNC, err);
2995       g_free (err);
2996       return FALSE;
2997     }
2998
2999     field_id = va_arg (args, GQuark);
3000   }
3001
3002   return TRUE;
3003
3004 /* ERRORS */
3005 no_such_field:
3006   {
3007     GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
3008         GST_STR_NULL (g_quark_to_string (field_id)), structure);
3009     return FALSE;
3010   }
3011 wrong_type:
3012   {
3013     GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
3014         "field was of type '%s': %" GST_PTR_FORMAT,
3015         g_quark_to_string (field_id),
3016         GST_STR_NULL (g_type_name (expected_type)),
3017         G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
3018         structure);
3019     return FALSE;
3020   }
3021 }
3022
3023 /**
3024  * gst_structure_get:
3025  * @structure: a #GstStructure
3026  * @first_fieldname: the name of the first field to read
3027  * @...: variable arguments
3028  *
3029  * Parses the variable arguments and reads fields from @structure accordingly.
3030  * Variable arguments should be in the form field name, field type
3031  * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3032  * The last variable argument should be NULL.
3033  *
3034  * For refcounted (mini)objects you will receive a new reference which
3035  * you must release with a suitable _unref() when no longer needed. For
3036  * strings and boxed types you will receive a copy which you will need to
3037  * release with either g_free() or the suitable function for the boxed type.
3038  *
3039  * Returns: FALSE if there was a problem reading any of the fields (e.g.
3040  *     because the field requested did not exist, or was of a type other
3041  *     than the type specified), otherwise TRUE.
3042  */
3043 gboolean
3044 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
3045     ...)
3046 {
3047   gboolean ret;
3048   va_list args;
3049
3050   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3051   g_return_val_if_fail (first_fieldname != NULL, FALSE);
3052
3053   va_start (args, first_fieldname);
3054   ret = gst_structure_get_valist (structure, first_fieldname, args);
3055   va_end (args);
3056
3057   return ret;
3058 }
3059
3060 /**
3061  * gst_structure_id_get:
3062  * @structure: a #GstStructure
3063  * @first_field_id: the quark of the first field to read
3064  * @...: variable arguments
3065  *
3066  * Parses the variable arguments and reads fields from @structure accordingly.
3067  * Variable arguments should be in the form field id quark, field type
3068  * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3069  * The last variable argument should be NULL (technically it should be a
3070  * 0 quark, but we require NULL so compilers that support it can check for
3071  * the NULL terminator and warn if it's not there).
3072  *
3073  * This function is just like gst_structure_get() only that it is slightly
3074  * more efficient since it saves the string-to-quark lookup in the global
3075  * quark hashtable.
3076  *
3077  * For refcounted (mini)objects you will receive a new reference which
3078  * you must release with a suitable _unref() when no longer needed. For
3079  * strings and boxed types you will receive a copy which you will need to
3080  * release with either g_free() or the suitable function for the boxed type.
3081  *
3082  * Returns: FALSE if there was a problem reading any of the fields (e.g.
3083  *     because the field requested did not exist, or was of a type other
3084  *     than the type specified), otherwise TRUE.
3085  */
3086 gboolean
3087 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
3088     ...)
3089 {
3090   gboolean ret;
3091   va_list args;
3092
3093   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3094   g_return_val_if_fail (first_field_id != 0, FALSE);
3095
3096   va_start (args, first_field_id);
3097   ret = gst_structure_id_get_valist (structure, first_field_id, args);
3098   va_end (args);
3099
3100   return ret;
3101 }
3102
3103 static gboolean
3104 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
3105     gpointer data)
3106 {
3107   const GstStructure *struct1 = (const GstStructure *) data;
3108   const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
3109
3110   if (G_UNLIKELY (val1 == NULL))
3111     return FALSE;
3112   if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
3113     return TRUE;
3114   }
3115
3116   return FALSE;
3117 }
3118
3119 /**
3120  * gst_structure_is_equal:
3121  * @structure1: a #GstStructure.
3122  * @structure2: a #GstStructure.
3123  *
3124  * Tests if the two #GstStructure are equal.
3125  *
3126  * Returns: TRUE if the two structures have the same name and field.
3127  **/
3128 gboolean
3129 gst_structure_is_equal (const GstStructure * structure1,
3130     const GstStructure * structure2)
3131 {
3132   g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
3133   g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
3134
3135   if (G_UNLIKELY (structure1 == structure2))
3136     return TRUE;
3137
3138   if (structure1->name != structure2->name) {
3139     return FALSE;
3140   }
3141   if (GST_STRUCTURE_FIELDS (structure1)->len !=
3142       GST_STRUCTURE_FIELDS (structure2)->len) {
3143     return FALSE;
3144   }
3145
3146   return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
3147       (gpointer) structure2);
3148 }
3149
3150
3151 typedef struct
3152 {
3153   GstStructure *dest;
3154   const GstStructure *intersect;
3155 }
3156 IntersectData;
3157
3158 static gboolean
3159 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
3160 {
3161   IntersectData *idata = (IntersectData *) data;
3162   const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3163
3164   if (G_UNLIKELY (val2 == NULL)) {
3165     gst_structure_id_set_value (idata->dest, id, val1);
3166   } else {
3167     GValue dest_value = { 0 };
3168     if (gst_value_intersect (&dest_value, val1, val2)) {
3169       gst_structure_id_take_value (idata->dest, id, &dest_value);
3170     } else {
3171       return FALSE;
3172     }
3173   }
3174   return TRUE;
3175 }
3176
3177 static gboolean
3178 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
3179 {
3180   IntersectData *idata = (IntersectData *) data;
3181   const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3182
3183   if (G_UNLIKELY (val2 == NULL)) {
3184     gst_structure_id_set_value (idata->dest, id, val1);
3185   }
3186   return TRUE;
3187 }
3188
3189 /**
3190  * gst_structure_intersect:
3191  * @struct1: a #GstStructure
3192  * @struct2: a #GstStructure
3193  *
3194  * Intersects @struct1 and @struct2 and returns the intersection.
3195  *
3196  * Returns: Intersection of @struct1 and @struct2
3197  */
3198 GstStructure *
3199 gst_structure_intersect (const GstStructure * struct1,
3200     const GstStructure * struct2)
3201 {
3202   IntersectData data;
3203
3204   g_assert (struct1 != NULL);
3205   g_assert (struct2 != NULL);
3206
3207   if (G_UNLIKELY (struct1->name != struct2->name))
3208     return NULL;
3209
3210   /* copy fields from struct1 which we have not in struct2 to target
3211    * intersect if we have the field in both */
3212   data.dest = gst_structure_new_id_empty (struct1->name);
3213   data.intersect = struct2;
3214   if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3215               gst_structure_intersect_field1, &data)))
3216     goto error;
3217
3218   /* copy fields from struct2 which we have not in struct1 to target */
3219   data.intersect = struct1;
3220   if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3221               gst_structure_intersect_field2, &data)))
3222     goto error;
3223
3224   return data.dest;
3225
3226 error:
3227   gst_structure_free (data.dest);
3228   return NULL;
3229 }
3230
3231 static gboolean
3232 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3233     gpointer data)
3234 {
3235   GstStructure *other = (GstStructure *) data;
3236   const GValue *val2 = gst_structure_id_get_value (other, id);
3237
3238   if (G_LIKELY (val2)) {
3239     if (!gst_value_can_intersect (val1, val2)) {
3240       return FALSE;
3241     } else {
3242       gint eq = gst_value_compare (val1, val2);
3243
3244       if (eq == GST_VALUE_UNORDERED) {
3245         /* we need to try interseting */
3246         if (!gst_value_intersect (NULL, val1, val2)) {
3247           return FALSE;
3248         }
3249       } else if (eq != GST_VALUE_EQUAL) {
3250         return FALSE;
3251       }
3252     }
3253   }
3254   return TRUE;
3255 }
3256
3257 /**
3258  * gst_structure_can_intersect:
3259  * @struct1: a #GstStructure
3260  * @struct2: a #GstStructure
3261  *
3262  * Tries intersecting @struct1 and @struct2 and reports whether the result
3263  * would not be empty.
3264  *
3265  * Returns: %TRUE if intersection would not be empty
3266  */
3267 gboolean
3268 gst_structure_can_intersect (const GstStructure * struct1,
3269     const GstStructure * struct2)
3270 {
3271   g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3272   g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3273
3274   if (G_UNLIKELY (struct1->name != struct2->name))
3275     return FALSE;
3276
3277   /* tries to intersect if we have the field in both */
3278   return gst_structure_foreach ((GstStructure *) struct1,
3279       gst_caps_structure_can_intersect_field, (gpointer) struct2);
3280 }
3281
3282 static gboolean
3283 gst_caps_structure_is_superset_field (GQuark field_id, const GValue * value,
3284     gpointer user_data)
3285 {
3286   GstStructure *subset = user_data;
3287   const GValue *other;
3288   int comparison;
3289
3290   if (!(other = gst_structure_id_get_value (subset, field_id)))
3291     /* field is missing in the subset => no subset */
3292     return FALSE;
3293
3294   comparison = gst_value_compare (value, other);
3295
3296   /* equal values are subset */
3297   if (comparison == GST_VALUE_EQUAL)
3298     return TRUE;
3299
3300   /* ordered, but unequal, values are not */
3301   if (comparison != GST_VALUE_UNORDERED)
3302     return FALSE;
3303
3304   return gst_value_is_subset (other, value);
3305 }
3306
3307 /**
3308  * gst_structure_is_subset:
3309  * @subset: a #GstStructure
3310  * @superset: a potentially greater #GstStructure
3311  *
3312  * Checks if @subset is a subset of @superset, i.e. has the same
3313  * structure name and for all fields that are existing in @superset,
3314  * @subset has a value that is a subset of the value in @superset.
3315  *
3316  * Returns: %TRUE if @subset is a subset of @superset
3317  */
3318 gboolean
3319 gst_structure_is_subset (const GstStructure * subset,
3320     const GstStructure * superset)
3321 {
3322   if ((superset->name != subset->name) ||
3323       (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3324     return FALSE;
3325
3326   return gst_structure_foreach ((GstStructure *) superset,
3327       gst_caps_structure_is_superset_field, (gpointer) subset);
3328 }
3329
3330
3331 /**
3332  * gst_structure_fixate:
3333  * @structure: a #GstStructure
3334  *
3335  * Fixate all values in @structure using gst_value_fixate().
3336  * @structure will be modified in-place and should be writable.
3337  */
3338 void
3339 gst_structure_fixate (GstStructure * structure)
3340 {
3341   g_return_if_fail (GST_IS_STRUCTURE (structure));
3342
3343   gst_structure_foreach (structure, default_fixate, structure);
3344 }