]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - gst/gststructure.c
structure: Don't allow invalid GDates in all structures and don't allow NULL GDates...
[glsdk/gstreamer0-10.git] / gst / gststructure.c
1 /* GStreamer
2  * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
3  *
4  * gststructure.c: lists of { GQuark, GValue } tuples
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
22 /**
23  * SECTION:gststructure
24  * @short_description: Generic structure containing fields of names and values
25  * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
26  *
27  * A #GstStructure is a collection of key/value pairs. The keys are expressed
28  * as GQuarks and the values can be of any GType.
29  *
30  * In addition to the key/value pairs, a #GstStructure also has a name. The name
31  * starts with a letter and can be folled by letters, numbers and any of "/-_.:".
32  *
33  * #GstStructure is used by various GStreamer subsystems to store information
34  * in a flexible and extensible way. A #GstStructure does not have a refcount
35  * because it usually is part of a higher level object such as #GstCaps. It
36  * provides a means to enforce mutability using the refcount of the parent
37  * with the gst_structure_set_parent_refcount() method.
38  *
39  * A #GstStructure can be created with gst_structure_empty_new() or
40  * gst_structure_new(), which both take a name and an optional set of
41  * key/value pairs along with the types of the values.
42  *
43  * Field values can be changed with gst_structure_set_value() or
44  * gst_structure_set().
45  *
46  * Field values can be retrieved with gst_structure_get_value() or the more
47  * convenient gst_structure_get_*() functions.
48  *
49  * Fields can be removed with gst_structure_remove_field() or
50  * gst_structure_remove_fields().
51  *
52  * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are
53  * not allowed. Strings must not be empty either, but may be NULL.
54  *
55  * Last reviewed on 2009-06-08 (0.10.23)
56  */
58 #ifdef HAVE_CONFIG_H
59 #include "config.h"
60 #endif
62 #include <string.h>
64 #include "gst_private.h"
65 #include "gstquark.h"
66 #include <gst/gst.h>
67 #include <gobject/gvaluecollector.h>
69 typedef struct _GstStructureField GstStructureField;
71 struct _GstStructureField
72 {
73   GQuark name;
74   GValue value;
75 };
77 #define GST_STRUCTURE_FIELD(structure, index) \
78     &g_array_index((structure)->fields, GstStructureField, (index))
80 #define IS_MUTABLE(structure) \
81     (!(structure)->parent_refcount || \
82      g_atomic_int_get ((structure)->parent_refcount) == 1)
84 #define IS_TAGLIST(structure) \
85     (structure->name == GST_QUARK (TAGLIST))
87 static void gst_structure_set_field (GstStructure * structure,
88     GstStructureField * field);
89 static GstStructureField *gst_structure_get_field (const GstStructure *
90     structure, const gchar * fieldname);
91 static GstStructureField *gst_structure_id_get_field (const GstStructure *
92     structure, GQuark field);
93 static void gst_structure_transform_to_string (const GValue * src_value,
94     GValue * dest_value);
95 static GstStructure *gst_structure_copy_conditional (const GstStructure *
96     structure);
97 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
98     GValue * value, GType default_type);
99 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
101 GType
102 gst_structure_get_type (void)
104   static GType gst_structure_type = 0;
106   if (G_UNLIKELY (gst_structure_type == 0)) {
107     gst_structure_type = g_boxed_type_register_static ("GstStructure",
108         (GBoxedCopyFunc) gst_structure_copy_conditional,
109         (GBoxedFreeFunc) gst_structure_free);
111     g_value_register_transform_func (gst_structure_type, G_TYPE_STRING,
112         gst_structure_transform_to_string);
113   }
115   return gst_structure_type;
118 static GstStructure *
119 gst_structure_id_empty_new_with_size (GQuark quark, guint prealloc)
121   GstStructure *structure;
123   structure = g_slice_new (GstStructure);
124   structure->type = gst_structure_get_type ();
125   structure->name = quark;
126   structure->parent_refcount = NULL;
127   structure->fields =
128       g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
130   return structure;
133 /**
134  * gst_structure_id_empty_new:
135  * @quark: name of new structure
136  *
137  * Creates a new, empty #GstStructure with the given name as a GQuark.
138  *
139  * Free-function: gst_structure_free
140  *
141  * Returns: (transfer full): a new, empty #GstStructure
142  */
143 GstStructure *
144 gst_structure_id_empty_new (GQuark quark)
146   g_return_val_if_fail (quark != 0, NULL);
148   return gst_structure_id_empty_new_with_size (quark, 0);
151 #ifndef G_DISABLE_CHECKS
152 static gboolean
153 gst_structure_validate_name (const gchar * name)
155   const gchar *s;
157   g_return_val_if_fail (name != NULL, FALSE);
159   /* FIXME 0.11: use g_ascii_isalpha() */
160   if (G_UNLIKELY (!g_ascii_isalnum (*name))) {
161     GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
162         *name, name);
163     return FALSE;
164   }
166   /* FIXME 0.11: don't allow spaces */
167   /* FIXME: test name string more */
168   s = &name[1];
169   while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+ ", *s) != NULL))
170     s++;
171   if (G_UNLIKELY (*s != '\0')) {
172     GST_WARNING ("Invalid character '%c' at offset %lu in structure name: %s",
173         *s, ((gulong) s - (gulong) name), name);
174     return FALSE;
175   }
177   return TRUE;
179 #endif
181 /**
182  * gst_structure_empty_new:
183  * @name: name of new structure
184  *
185  * Creates a new, empty #GstStructure with the given @name.
186  *
187  * See gst_structure_set_name() for constraints on the @name parameter.
188  *
189  * Free-function: gst_structure_free
190  *
191  * Returns: (transfer full): a new, empty #GstStructure
192  */
193 GstStructure *
194 gst_structure_empty_new (const gchar * name)
196   g_return_val_if_fail (gst_structure_validate_name (name), NULL);
198   return gst_structure_id_empty_new_with_size (g_quark_from_string (name), 0);
201 /**
202  * gst_structure_new:
203  * @name: name of new structure
204  * @firstfield: name of first field to set
205  * @...: additional arguments
206  *
207  * Creates a new #GstStructure with the given name.  Parses the
208  * list of variable arguments and sets fields to the values listed.
209  * Variable arguments should be passed as field name, field type,
210  * and value.  Last variable argument should be NULL.
211  *
212  * Free-function: gst_structure_free
213  *
214  * Returns: (transfer full): a new #GstStructure
215  */
216 GstStructure *
217 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
219   GstStructure *structure;
220   va_list varargs;
222   va_start (varargs, firstfield);
223   structure = gst_structure_new_valist (name, firstfield, varargs);
224   va_end (varargs);
226   return structure;
229 /**
230  * gst_structure_new_valist:
231  * @name: name of new structure
232  * @firstfield: name of first field to set
233  * @varargs: variable argument list
234  *
235  * Creates a new #GstStructure with the given @name.  Structure fields
236  * are set according to the varargs in a manner similar to
237  * gst_structure_new().
238  *
239  * See gst_structure_set_name() for constraints on the @name parameter.
240  *
241  * Free-function: gst_structure_free
242  *
243  * Returns: (transfer full): a new #GstStructure
244  */
245 GstStructure *
246 gst_structure_new_valist (const gchar * name,
247     const gchar * firstfield, va_list varargs)
249   GstStructure *structure;
251   structure = gst_structure_empty_new (name);
253   if (structure)
254     gst_structure_set_valist (structure, firstfield, varargs);
256   return structure;
259 /**
260  * gst_structure_set_parent_refcount:
261  * @structure: a #GstStructure
262  * @refcount: (in): a pointer to the parent's refcount
263  *
264  * Sets the parent_refcount field of #GstStructure. This field is used to
265  * determine whether a structure is mutable or not. This function should only be
266  * called by code implementing parent objects of #GstStructure, as described in
267  * the MT Refcounting section of the design documents.
268  */
269 void
270 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
272   g_return_if_fail (structure != NULL);
274   /* if we have a parent_refcount already, we can only clear
275    * if with a NULL refcount */
276   if (structure->parent_refcount)
277     g_return_if_fail (refcount == NULL);
278   else
279     g_return_if_fail (refcount != NULL);
281   structure->parent_refcount = refcount;
284 /**
285  * gst_structure_copy:
286  * @structure: a #GstStructure to duplicate
287  *
288  * Duplicates a #GstStructure and all its fields and values.
289  *
290  * Free-function: gst_structure_free
291  *
292  * Returns: (transfer none): a new #GstStructure.
293  */
294 GstStructure *
295 gst_structure_copy (const GstStructure * structure)
297   GstStructure *new_structure;
298   GstStructureField *field;
299   guint i, len;
301   g_return_val_if_fail (structure != NULL, NULL);
303   len = structure->fields->len;
304   new_structure = gst_structure_id_empty_new_with_size (structure->name, len);
306   for (i = 0; i < len; i++) {
307     GstStructureField new_field = { 0 };
309     field = GST_STRUCTURE_FIELD (structure, i);
311     new_field.name = field->name;
312     gst_value_init_and_copy (&new_field.value, &field->value);
313     g_array_append_val (new_structure->fields, new_field);
314   }
316   return new_structure;
319 /**
320  * gst_structure_free:
321  * @structure: (in) (transfer full): the #GstStructure to free
322  *
323  * Frees a #GstStructure and all its fields and values. The structure must not
324  * have a parent when this function is called.
325  */
326 void
327 gst_structure_free (GstStructure * structure)
329   GstStructureField *field;
330   guint i, len;
332   g_return_if_fail (structure != NULL);
333   g_return_if_fail (structure->parent_refcount == NULL);
335   len = structure->fields->len;
336   for (i = 0; i < len; i++) {
337     field = GST_STRUCTURE_FIELD (structure, i);
339     if (G_IS_VALUE (&field->value)) {
340       g_value_unset (&field->value);
341     }
342   }
343   g_array_free (structure->fields, TRUE);
344 #ifdef USE_POISONING
345   memset (structure, 0xff, sizeof (GstStructure));
346 #endif
347   g_slice_free (GstStructure, structure);
350 /**
351  * gst_structure_get_name:
352  * @structure: a #GstStructure
353  *
354  * Get the name of @structure as a string.
355  *
356  * Returns: the name of the structure.
357  */
358 const gchar *
359 gst_structure_get_name (const GstStructure * structure)
361   g_return_val_if_fail (structure != NULL, NULL);
363   return g_quark_to_string (structure->name);
366 /**
367  * gst_structure_has_name:
368  * @structure: a #GstStructure
369  * @name: structure name to check for
370  *
371  * Checks if the structure has the given name
372  *
373  * Returns: TRUE if @name matches the name of the structure.
374  */
375 gboolean
376 gst_structure_has_name (const GstStructure * structure, const gchar * name)
378   const gchar *structure_name;
380   g_return_val_if_fail (structure != NULL, FALSE);
381   g_return_val_if_fail (name != NULL, FALSE);
383   /* getting the string is cheap and comparing short strings is too
384    * should be faster than getting the quark for name and comparing the quarks
385    */
386   structure_name = g_quark_to_string (structure->name);
388   return (structure_name && strcmp (structure_name, name) == 0);
391 /**
392  * gst_structure_get_name_id:
393  * @structure: a #GstStructure
394  *
395  * Get the name of @structure as a GQuark.
396  *
397  * Returns: the quark representing the name of the structure.
398  */
399 GQuark
400 gst_structure_get_name_id (const GstStructure * structure)
402   g_return_val_if_fail (structure != NULL, 0);
404   return structure->name;
407 /**
408  * gst_structure_set_name:
409  * @structure: a #GstStructure
410  * @name: the new name of the structure
411  *
412  * Sets the name of the structure to the given @name.  The string
413  * provided is copied before being used. It must not be empty, start with a
414  * letter and can be followed by letters, numbers and any of "/-_.:".
415  */
416 void
417 gst_structure_set_name (GstStructure * structure, const gchar * name)
419   g_return_if_fail (structure != NULL);
420   g_return_if_fail (IS_MUTABLE (structure));
421   g_return_if_fail (gst_structure_validate_name (name));
423   structure->name = g_quark_from_string (name);
426 static inline void
427 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
428     const GValue * value)
430   GstStructureField gsfield = { 0, {0,} };
432   gsfield.name = field;
433   gst_value_init_and_copy (&gsfield.value, value);
435   gst_structure_set_field (structure, &gsfield);
438 /**
439  * gst_structure_id_set_value:
440  * @structure: a #GstStructure
441  * @field: a #GQuark representing a field
442  * @value: the new value of the field
443  *
444  * Sets the field with the given GQuark @field to @value.  If the field
445  * does not exist, it is created.  If the field exists, the previous
446  * value is replaced and freed.
447  */
448 void
449 gst_structure_id_set_value (GstStructure * structure,
450     GQuark field, const GValue * value)
453   g_return_if_fail (structure != NULL);
454   g_return_if_fail (G_IS_VALUE (value));
455   g_return_if_fail (IS_MUTABLE (structure));
457   gst_structure_id_set_value_internal (structure, field, value);
460 /**
461  * gst_structure_set_value:
462  * @structure: a #GstStructure
463  * @fieldname: the name of the field to set
464  * @value: the new value of the field
465  *
466  * Sets the field with the given name @field to @value.  If the field
467  * does not exist, it is created.  If the field exists, the previous
468  * value is replaced and freed.
469  */
470 void
471 gst_structure_set_value (GstStructure * structure,
472     const gchar * fieldname, const GValue * value)
474   g_return_if_fail (structure != NULL);
475   g_return_if_fail (fieldname != NULL);
476   g_return_if_fail (G_IS_VALUE (value));
477   g_return_if_fail (IS_MUTABLE (structure));
479   gst_structure_id_set_value_internal (structure,
480       g_quark_from_string (fieldname), value);
483 static inline void
484 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
485     GValue * value)
487   GstStructureField gsfield = { 0, {0,} };
489   gsfield.name = field;
490   gsfield.value = *value;
492   gst_structure_set_field (structure, &gsfield);
494   /* we took ownership */
495 #ifdef USE_POISONING
496   memset (value, 0, sizeof (GValue));
497 #else
498   value->g_type = G_TYPE_INVALID;
499 #endif
502 /**
503  * gst_structure_id_take_value:
504  * @structure: a #GstStructure
505  * @field: a #GQuark representing a field
506  * @value: (transfer full): the new value of the field
507  *
508  * Sets the field with the given GQuark @field to @value.  If the field
509  * does not exist, it is created.  If the field exists, the previous
510  * value is replaced and freed.
511  *
512  * Since: 0.10.31
513  */
514 void
515 gst_structure_id_take_value (GstStructure * structure, GQuark field,
516     GValue * value)
518   g_return_if_fail (structure != NULL);
519   g_return_if_fail (G_IS_VALUE (value));
520   g_return_if_fail (IS_MUTABLE (structure));
522   gst_structure_id_take_value_internal (structure, field, value);
525 /**
526  * gst_structure_take_value:
527  * @structure: a #GstStructure
528  * @fieldname: the name of the field to set
529  * @value: (transfer full): the new value of the field
530  *
531  * Sets the field with the given name @field to @value.  If the field
532  * does not exist, it is created.  If the field exists, the previous
533  * value is replaced and freed. The function will take ownership of @value.
534  *
535  * Since: 0.10.31
536  */
537 void
538 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
539     GValue * value)
541   g_return_if_fail (structure != NULL);
542   g_return_if_fail (fieldname != NULL);
543   g_return_if_fail (G_IS_VALUE (value));
544   g_return_if_fail (IS_MUTABLE (structure));
546   gst_structure_id_take_value_internal (structure,
547       g_quark_from_string (fieldname), value);
550 static void
551 gst_structure_set_valist_internal (GstStructure * structure,
552     const gchar * fieldname, va_list varargs)
554   gchar *err = NULL;
555   GType type;
557   while (fieldname) {
558     GstStructureField field = { 0 };
560     field.name = g_quark_from_string (fieldname);
562     type = va_arg (varargs, GType);
564     if (G_UNLIKELY (type == G_TYPE_DATE)) {
565       g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
566       type = GST_TYPE_DATE;
567     }
568 #if GLIB_CHECK_VERSION(2,23,3)
569     G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
570 #else
571     g_value_init (&field.value, type);
572     G_VALUE_COLLECT (&field.value, varargs, 0, &err);
573 #endif
574     if (G_UNLIKELY (err)) {
575       g_critical ("%s", err);
576       return;
577     }
578     gst_structure_set_field (structure, &field);
580     fieldname = va_arg (varargs, gchar *);
581   }
584 /**
585  * gst_structure_set:
586  * @structure: a #GstStructure
587  * @fieldname: the name of the field to set
588  * @...: variable arguments
589  *
590  * Parses the variable arguments and sets fields accordingly.
591  * Variable arguments should be in the form field name, field type
592  * (as a GType), value(s).  The last variable argument should be NULL.
593  */
594 void
595 gst_structure_set (GstStructure * structure, const gchar * field, ...)
597   va_list varargs;
599   g_return_if_fail (structure != NULL);
600   g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
602   va_start (varargs, field);
603   gst_structure_set_valist_internal (structure, field, varargs);
604   va_end (varargs);
607 /**
608  * gst_structure_set_valist:
609  * @structure: a #GstStructure
610  * @fieldname: the name of the field to set
611  * @varargs: variable arguments
612  *
613  * va_list form of gst_structure_set().
614  */
615 void
616 gst_structure_set_valist (GstStructure * structure,
617     const gchar * fieldname, va_list varargs)
619   g_return_if_fail (structure != NULL);
620   g_return_if_fail (IS_MUTABLE (structure));
622   gst_structure_set_valist_internal (structure, fieldname, varargs);
625 static void
626 gst_structure_id_set_valist_internal (GstStructure * structure,
627     GQuark fieldname, va_list varargs)
629   gchar *err = NULL;
630   GType type;
632   while (fieldname) {
633     GstStructureField field = { 0 };
635     field.name = fieldname;
637     type = va_arg (varargs, GType);
639     if (G_UNLIKELY (type == G_TYPE_DATE)) {
640       g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
641       type = GST_TYPE_DATE;
642     }
643 #ifndef G_VALUE_COLLECT_INIT
644     g_value_init (&field.value, type);
645     G_VALUE_COLLECT (&field.value, varargs, 0, &err);
646 #else
647     G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
648 #endif
649     if (G_UNLIKELY (err)) {
650       g_critical ("%s", err);
651       return;
652     }
653     gst_structure_set_field (structure, &field);
655     fieldname = va_arg (varargs, GQuark);
656   }
659 /**
660  * gst_structure_id_set:
661  * @structure: a #GstStructure
662  * @fieldname: the GQuark for the name of the field to set
663  * @...: variable arguments
664  *
665  * Identical to gst_structure_set, except that field names are
666  * passed using the GQuark for the field name. This allows more efficient
667  * setting of the structure if the caller already knows the associated
668  * quark values.
669  * The last variable argument must be NULL.
670  *
671  * Since: 0.10.10
672  */
673 void
674 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
676   va_list varargs;
678   g_return_if_fail (structure != NULL);
680   va_start (varargs, field);
681   gst_structure_id_set_valist_internal (structure, field, varargs);
682   va_end (varargs);
685 /**
686  * gst_structure_id_set_valist:
687  * @structure: a #GstStructure
688  * @fieldname: the name of the field to set
689  * @varargs: variable arguments
690  *
691  * va_list form of gst_structure_id_set().
692  *
693  * Since: 0.10.10
694  */
695 void
696 gst_structure_id_set_valist (GstStructure * structure,
697     GQuark fieldname, va_list varargs)
699   g_return_if_fail (structure != NULL);
700   g_return_if_fail (IS_MUTABLE (structure));
702   gst_structure_id_set_valist_internal (structure, fieldname, varargs);
705 /**
706  * gst_structure_id_new:
707  * @name_quark: name of new structure
708  * @field_quark: the GQuark for the name of the field to set
709  * @...: variable arguments
710  *
711  * Creates a new #GstStructure with the given name as a GQuark, followed by
712  * fieldname quark, GType, argument(s) "triplets" in the same format as
713  * gst_structure_id_set(). Basically a convenience wrapper around
714  * gst_structure_id_empty_new() and gst_structure_id_set().
715  *
716  * The last variable argument must be NULL (or 0).
717  *
718  * Free-function: gst_structure_free
719  *
720  * Returns: (transfer full): a new #GstStructure
721  *
722  * Since: 0.10.24
723  */
724 GstStructure *
725 gst_structure_id_new (GQuark name_quark, GQuark field_quark, ...)
727   GstStructure *s;
728   va_list varargs;
730   g_return_val_if_fail (name_quark != 0, NULL);
731   g_return_val_if_fail (field_quark != 0, NULL);
733   s = gst_structure_id_empty_new (name_quark);
735   va_start (varargs, field_quark);
736   gst_structure_id_set_valist_internal (s, field_quark, varargs);
737   va_end (varargs);
739   return s;
742 #if GST_VERSION_NANO == 1
743 #define GIT_G_WARNING g_warning
744 #else
745 #define GIT_G_WARNING GST_WARNING
746 #endif
748 /* If the structure currently contains a field with the same name, it is
749  * replaced with the provided field. Otherwise, the field is added to the
750  * structure. The field's value is not deeply copied.
751  */
752 static void
753 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
755   GstStructureField *f;
756   guint i, len = structure->fields->len;
758   if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
759     const gchar *s;
761     s = g_value_get_string (&field->value);
762     /* only check for NULL strings in taglists, as they are allowed in message
763      * structs, e.g. error message debug strings */
764     if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
765       if (s == NULL) {
766         GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
767             "Please file a bug.", g_quark_to_string (field->name));
768         g_value_unset (&field->value);
769         return;
770       } else {
771         /* empty strings never make sense */
772         GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
773             "Please file a bug.", g_quark_to_string (field->name));
774         g_value_unset (&field->value);
775         return;
776       }
777     } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
778       g_warning ("Trying to set string on %s field '%s', but string is not "
779           "valid UTF-8. Please file a bug.",
780           IS_TAGLIST (structure) ? "taglist" : "structure",
781           g_quark_to_string (field->name));
782       g_value_unset (&field->value);
783       return;
784     }
785   } else if (G_UNLIKELY (GST_VALUE_HOLDS_DATE (&field->value))) {
786     const GDate *d;
788     d = gst_value_get_date (&field->value);
789     /* only check for NULL GDates in taglists, as they might make sense
790      * in other, generic structs */
791     if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
792       GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
793           "Please file a bug.", g_quark_to_string (field->name));
794       g_value_unset (&field->value);
795       return;
796     } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
797       g_warning
798           ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
799           IS_TAGLIST (structure) ? "taglist" : "structure",
800           g_quark_to_string (field->name));
801       g_value_unset (&field->value);
802       return;
803     }
804   }
806   for (i = 0; i < len; i++) {
807     f = GST_STRUCTURE_FIELD (structure, i);
809     if (G_UNLIKELY (f->name == field->name)) {
810       g_value_unset (&f->value);
811       memcpy (f, field, sizeof (GstStructureField));
812       return;
813     }
814   }
816   g_array_append_val (structure->fields, *field);
819 /* If there is no field with the given ID, NULL is returned.
820  */
821 static GstStructureField *
822 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
824   GstStructureField *field;
825   guint i, len;
827   len = structure->fields->len;
829   for (i = 0; i < len; i++) {
830     field = GST_STRUCTURE_FIELD (structure, i);
832     if (G_UNLIKELY (field->name == field_id))
833       return field;
834   }
836   return NULL;
839 /* If there is no field with the given ID, NULL is returned.
840  */
841 static GstStructureField *
842 gst_structure_get_field (const GstStructure * structure,
843     const gchar * fieldname)
845   g_return_val_if_fail (structure != NULL, NULL);
846   g_return_val_if_fail (fieldname != NULL, NULL);
848   return gst_structure_id_get_field (structure,
849       g_quark_from_string (fieldname));
852 /**
853  * gst_structure_get_value:
854  * @structure: a #GstStructure
855  * @fieldname: the name of the field to get
856  *
857  * Get the value of the field with name @fieldname.
858  *
859  * Returns: the #GValue corresponding to the field with the given name.
860  */
861 const GValue *
862 gst_structure_get_value (const GstStructure * structure,
863     const gchar * fieldname)
865   GstStructureField *field;
867   g_return_val_if_fail (structure != NULL, NULL);
868   g_return_val_if_fail (fieldname != NULL, NULL);
870   field = gst_structure_get_field (structure, fieldname);
871   if (field == NULL)
872     return NULL;
874   return &field->value;
877 /**
878  * gst_structure_id_get_value:
879  * @structure: a #GstStructure
880  * @field: the #GQuark of the field to get
881  *
882  * Get the value of the field with GQuark @field.
883  *
884  * Returns: the #GValue corresponding to the field with the given name
885  *          identifier.
886  */
887 const GValue *
888 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
890   GstStructureField *gsfield;
892   g_return_val_if_fail (structure != NULL, NULL);
894   gsfield = gst_structure_id_get_field (structure, field);
895   if (gsfield == NULL)
896     return NULL;
898   return &gsfield->value;
901 /**
902  * gst_structure_remove_field:
903  * @structure: a #GstStructure
904  * @fieldname: the name of the field to remove
905  *
906  * Removes the field with the given name.  If the field with the given
907  * name does not exist, the structure is unchanged.
908  */
909 void
910 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
912   GstStructureField *field;
913   GQuark id;
914   guint i, len;
916   g_return_if_fail (structure != NULL);
917   g_return_if_fail (fieldname != NULL);
918   g_return_if_fail (IS_MUTABLE (structure));
920   id = g_quark_from_string (fieldname);
921   len = structure->fields->len;
923   for (i = 0; i < len; i++) {
924     field = GST_STRUCTURE_FIELD (structure, i);
926     if (field->name == id) {
927       if (G_IS_VALUE (&field->value)) {
928         g_value_unset (&field->value);
929       }
930       structure->fields = g_array_remove_index (structure->fields, i);
931       return;
932     }
933   }
936 /**
937  * gst_structure_remove_fields:
938  * @structure: a #GstStructure
939  * @fieldname: the name of the field to remove
940  * @...: NULL-terminated list of more fieldnames to remove
941  *
942  * Removes the fields with the given names. If a field does not exist, the
943  * argument is ignored.
944  */
945 void
946 gst_structure_remove_fields (GstStructure * structure,
947     const gchar * fieldname, ...)
949   va_list varargs;
951   g_return_if_fail (structure != NULL);
952   g_return_if_fail (fieldname != NULL);
953   /* mutability checked in remove_field */
955   va_start (varargs, fieldname);
956   gst_structure_remove_fields_valist (structure, fieldname, varargs);
957   va_end (varargs);
960 /**
961  * gst_structure_remove_fields_valist:
962  * @structure: a #GstStructure
963  * @fieldname: the name of the field to remove
964  * @varargs: NULL-terminated list of more fieldnames to remove
965  *
966  * va_list form of gst_structure_remove_fields().
967  */
968 void
969 gst_structure_remove_fields_valist (GstStructure * structure,
970     const gchar * fieldname, va_list varargs)
972   gchar *field = (gchar *) fieldname;
974   g_return_if_fail (structure != NULL);
975   g_return_if_fail (fieldname != NULL);
976   /* mutability checked in remove_field */
978   while (field) {
979     gst_structure_remove_field (structure, field);
980     field = va_arg (varargs, char *);
981   }
984 /**
985  * gst_structure_remove_all_fields:
986  * @structure: a #GstStructure
987  *
988  * Removes all fields in a GstStructure.
989  */
990 void
991 gst_structure_remove_all_fields (GstStructure * structure)
993   GstStructureField *field;
994   int i;
996   g_return_if_fail (structure != NULL);
997   g_return_if_fail (IS_MUTABLE (structure));
999   for (i = structure->fields->len - 1; i >= 0; i--) {
1000     field = GST_STRUCTURE_FIELD (structure, i);
1002     if (G_IS_VALUE (&field->value)) {
1003       g_value_unset (&field->value);
1004     }
1005     structure->fields = g_array_remove_index (structure->fields, i);
1006   }
1009 /**
1010  * gst_structure_get_field_type:
1011  * @structure: a #GstStructure
1012  * @fieldname: the name of the field
1013  *
1014  * Finds the field with the given name, and returns the type of the
1015  * value it contains.  If the field is not found, G_TYPE_INVALID is
1016  * returned.
1017  *
1018  * Returns: the #GValue of the field
1019  */
1020 GType
1021 gst_structure_get_field_type (const GstStructure * structure,
1022     const gchar * fieldname)
1024   GstStructureField *field;
1026   g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1027   g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1029   field = gst_structure_get_field (structure, fieldname);
1030   if (field == NULL)
1031     return G_TYPE_INVALID;
1033   return G_VALUE_TYPE (&field->value);
1036 /**
1037  * gst_structure_n_fields:
1038  * @structure: a #GstStructure
1039  *
1040  * Get the number of fields in the structure.
1041  *
1042  * Returns: the number of fields in the structure
1043  */
1044 gint
1045 gst_structure_n_fields (const GstStructure * structure)
1047   g_return_val_if_fail (structure != NULL, 0);
1049   return structure->fields->len;
1052 /**
1053  * gst_structure_nth_field_name:
1054  * @structure: a #GstStructure
1055  * @index: the index to get the name of
1056  *
1057  * Get the name of the given field number, counting from 0 onwards.
1058  *
1059  * Returns: the name of the given field number
1060  */
1061 const gchar *
1062 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1064   GstStructureField *field;
1066   g_return_val_if_fail (structure != NULL, NULL);
1067   g_return_val_if_fail (index < structure->fields->len, NULL);
1069   field = GST_STRUCTURE_FIELD (structure, index);
1071   return g_quark_to_string (field->name);
1074 /**
1075  * gst_structure_foreach:
1076  * @structure: a #GstStructure
1077  * @func: a function to call for each field
1078  * @user_data: (closure): private data
1079  *
1080  * Calls the provided function once for each field in the #GstStructure. The
1081  * function must not modify the fields. Also see gst_structure_map_in_place().
1082  *
1083  * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1084  * FALSE otherwise.
1085  */
1086 gboolean
1087 gst_structure_foreach (const GstStructure * structure,
1088     GstStructureForeachFunc func, gpointer user_data)
1090   guint i, len;
1091   GstStructureField *field;
1092   gboolean ret;
1094   g_return_val_if_fail (structure != NULL, FALSE);
1095   g_return_val_if_fail (func != NULL, FALSE);
1097   len = structure->fields->len;
1099   for (i = 0; i < len; i++) {
1100     field = GST_STRUCTURE_FIELD (structure, i);
1102     ret = func (field->name, &field->value, user_data);
1103     if (G_UNLIKELY (!ret))
1104       return FALSE;
1105   }
1107   return TRUE;
1110 /**
1111  * gst_structure_map_in_place:
1112  * @structure: a #GstStructure
1113  * @func: a function to call for each field
1114  * @user_data: (closure): private data
1115  *
1116  * Calls the provided function once for each field in the #GstStructure. In
1117  * contrast to gst_structure_foreach(), the function may modify but not delete the
1118  * fields. The structure must be mutable.
1119  *
1120  * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1121  * FALSE otherwise.
1122  */
1123 gboolean
1124 gst_structure_map_in_place (GstStructure * structure,
1125     GstStructureMapFunc func, gpointer user_data)
1127   guint i, len;
1128   GstStructureField *field;
1129   gboolean ret;
1131   g_return_val_if_fail (structure != NULL, FALSE);
1132   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1133   g_return_val_if_fail (func != NULL, FALSE);
1134   len = structure->fields->len;
1136   for (i = 0; i < len; i++) {
1137     field = GST_STRUCTURE_FIELD (structure, i);
1139     ret = func (field->name, &field->value, user_data);
1140     if (!ret)
1141       return FALSE;
1142   }
1144   return TRUE;
1147 /**
1148  * gst_structure_id_has_field:
1149  * @structure: a #GstStructure
1150  * @field: #GQuark of the field name
1151  *
1152  * Check if @structure contains a field named @field.
1153  *
1154  * Returns: TRUE if the structure contains a field with the given name
1155  *
1156  * Since: 0.10.26
1157  */
1158 gboolean
1159 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1161   GstStructureField *f;
1163   g_return_val_if_fail (structure != NULL, FALSE);
1164   g_return_val_if_fail (field != 0, FALSE);
1166   f = gst_structure_id_get_field (structure, field);
1168   return (f != NULL);
1171 /**
1172  * gst_structure_has_field:
1173  * @structure: a #GstStructure
1174  * @fieldname: the name of a field
1175  *
1176  * Check if @structure contains a field named @fieldname.
1177  *
1178  * Returns: TRUE if the structure contains a field with the given name
1179  */
1180 gboolean
1181 gst_structure_has_field (const GstStructure * structure,
1182     const gchar * fieldname)
1184   g_return_val_if_fail (structure != NULL, FALSE);
1185   g_return_val_if_fail (fieldname != NULL, FALSE);
1187   return gst_structure_id_has_field (structure,
1188       g_quark_from_string (fieldname));
1191 /**
1192  * gst_structure_id_has_field_typed:
1193  * @structure: a #GstStructure
1194  * @field: #GQuark of the field name
1195  * @type: the type of a value
1196  *
1197  * Check if @structure contains a field named @field and with GType @type.
1198  *
1199  * Returns: TRUE if the structure contains a field with the given name and type
1200  *
1201  * Since: 0.10.26
1202  */
1203 gboolean
1204 gst_structure_id_has_field_typed (const GstStructure * structure,
1205     GQuark field, GType type)
1207   GstStructureField *f;
1209   g_return_val_if_fail (structure != NULL, FALSE);
1210   g_return_val_if_fail (field != 0, FALSE);
1212   f = gst_structure_id_get_field (structure, field);
1213   if (f == NULL)
1214     return FALSE;
1216   return (G_VALUE_TYPE (&f->value) == type);
1219 /**
1220  * gst_structure_has_field_typed:
1221  * @structure: a #GstStructure
1222  * @fieldname: the name of a field
1223  * @type: the type of a value
1224  *
1225  * Check if @structure contains a field named @fieldname and with GType @type.
1226  *
1227  * Returns: TRUE if the structure contains a field with the given name and type
1228  */
1229 gboolean
1230 gst_structure_has_field_typed (const GstStructure * structure,
1231     const gchar * fieldname, GType type)
1233   g_return_val_if_fail (structure != NULL, FALSE);
1234   g_return_val_if_fail (fieldname != NULL, FALSE);
1236   return gst_structure_id_has_field_typed (structure,
1237       g_quark_from_string (fieldname), type);
1240 /* utility functions */
1242 /**
1243  * gst_structure_get_boolean:
1244  * @structure: a #GstStructure
1245  * @fieldname: the name of a field
1246  * @value: (out): a pointer to a #gboolean to set
1247  *
1248  * Sets the boolean pointed to by @value corresponding to the value of the
1249  * given field.  Caller is responsible for making sure the field exists
1250  * and has the correct type.
1251  *
1252  * Returns: TRUE if the value could be set correctly. If there was no field
1253  * with @fieldname or the existing field did not contain a boolean, this
1254  * function returns FALSE.
1255  */
1256 gboolean
1257 gst_structure_get_boolean (const GstStructure * structure,
1258     const gchar * fieldname, gboolean * value)
1260   GstStructureField *field;
1262   g_return_val_if_fail (structure != NULL, FALSE);
1263   g_return_val_if_fail (fieldname != NULL, FALSE);
1265   field = gst_structure_get_field (structure, fieldname);
1267   if (field == NULL)
1268     return FALSE;
1269   if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1270     return FALSE;
1272   *value = gst_g_value_get_boolean_unchecked (&field->value);
1274   return TRUE;
1277 /**
1278  * gst_structure_get_int:
1279  * @structure: a #GstStructure
1280  * @fieldname: the name of a field
1281  * @value: (out): a pointer to an int to set
1282  *
1283  * Sets the int pointed to by @value corresponding to the value of the
1284  * given field.  Caller is responsible for making sure the field exists
1285  * and has the correct type.
1286  *
1287  * Returns: %TRUE if the value could be set correctly. If there was no field
1288  * with @fieldname or the existing field did not contain an int, this function
1289  * returns %FALSE.
1290  */
1291 gboolean
1292 gst_structure_get_int (const GstStructure * structure,
1293     const gchar * fieldname, gint * value)
1295   GstStructureField *field;
1297   g_return_val_if_fail (structure != NULL, FALSE);
1298   g_return_val_if_fail (fieldname != NULL, FALSE);
1299   g_return_val_if_fail (value != NULL, FALSE);
1301   field = gst_structure_get_field (structure, fieldname);
1303   if (field == NULL)
1304     return FALSE;
1305   if (!G_VALUE_HOLDS_INT (&field->value))
1306     return FALSE;
1308   *value = gst_g_value_get_int_unchecked (&field->value);
1310   return TRUE;
1313 /**
1314  * gst_structure_get_uint:
1315  * @structure: a #GstStructure
1316  * @fieldname: the name of a field
1317  * @value: (out): a pointer to a uint to set
1318  *
1319  * Sets the uint pointed to by @value corresponding to the value of the
1320  * given field.  Caller is responsible for making sure the field exists
1321  * and has the correct type.
1322  *
1323  * Returns: %TRUE if the value could be set correctly. If there was no field
1324  * with @fieldname or the existing field did not contain a uint, this function
1325  * returns %FALSE.
1326  *
1327  * Since: 0.10.15
1328  */
1329 gboolean
1330 gst_structure_get_uint (const GstStructure * structure,
1331     const gchar * fieldname, guint * value)
1333   GstStructureField *field;
1335   g_return_val_if_fail (structure != NULL, FALSE);
1336   g_return_val_if_fail (fieldname != NULL, FALSE);
1337   g_return_val_if_fail (value != NULL, FALSE);
1339   field = gst_structure_get_field (structure, fieldname);
1341   if (field == NULL)
1342     return FALSE;
1343   if (!G_VALUE_HOLDS_UINT (&field->value))
1344     return FALSE;
1346   *value = gst_g_value_get_uint_unchecked (&field->value);
1348   return TRUE;
1351 /**
1352  * gst_structure_get_fourcc:
1353  * @structure: a #GstStructure
1354  * @fieldname: the name of a field
1355  * @value: (out): a pointer to a 32bit unsigned int to set
1356  *
1357  * Sets the Fourcc pointed to by @value corresponding to the value of the
1358  * given field.  Caller is responsible for making sure the field exists
1359  * and has the correct type.
1360  *
1361  * Returns: TRUE if the value could be set correctly. If there was no field
1362  * with @fieldname or the existing field did not contain a fourcc, this function
1363  * returns FALSE.
1364  */
1365 gboolean
1366 gst_structure_get_fourcc (const GstStructure * structure,
1367     const gchar * fieldname, guint32 * value)
1369   GstStructureField *field;
1371   g_return_val_if_fail (structure != NULL, FALSE);
1372   g_return_val_if_fail (fieldname != NULL, FALSE);
1373   g_return_val_if_fail (value != NULL, FALSE);
1375   field = gst_structure_get_field (structure, fieldname);
1377   if (field == NULL)
1378     return FALSE;
1379   if (!GST_VALUE_HOLDS_FOURCC (&field->value))
1380     return FALSE;
1382   *value = gst_value_get_fourcc (&field->value);
1384   return TRUE;
1387 /**
1388  * gst_structure_get_date:
1389  * @structure: a #GstStructure
1390  * @fieldname: the name of a field
1391  * @value: (out callee-allocates): a pointer to a #GDate to set
1392  *
1393  * Sets the date pointed to by @value corresponding to the date of the
1394  * given field.  Caller is responsible for making sure the field exists
1395  * and has the correct type.
1396  *
1397  * On success @value will point to a newly-allocated copy of the date which
1398  * should be freed with g_date_free() when no longer needed (note: this is
1399  * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1400  * copy of the string).
1401  *
1402  * Returns: TRUE if the value could be set correctly. If there was no field
1403  * with @fieldname or the existing field did not contain a data, this function
1404  * returns FALSE.
1405  */
1406 gboolean
1407 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1408     GDate ** value)
1410   GstStructureField *field;
1412   g_return_val_if_fail (structure != NULL, FALSE);
1413   g_return_val_if_fail (fieldname != NULL, FALSE);
1414   g_return_val_if_fail (value != NULL, FALSE);
1416   field = gst_structure_get_field (structure, fieldname);
1418   if (field == NULL)
1419     return FALSE;
1420   if (!GST_VALUE_HOLDS_DATE (&field->value))
1421     return FALSE;
1423   /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1424   *value = g_value_dup_boxed (&field->value);
1426   return TRUE;
1429 /**
1430  * gst_structure_get_date_time:
1431  * @structure: a #GstStructure
1432  * @fieldname: the name of a field
1433  * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1434  *
1435  * Sets the datetime pointed to by @value corresponding to the datetime of the
1436  * given field. Caller is responsible for making sure the field exists
1437  * and has the correct type.
1438  *
1439  * On success @value will point to a reference of the datetime which
1440  * should be unreffed with gst_date_time_unref() when no longer needed
1441  * (note: this is inconsistent with e.g. gst_structure_get_string()
1442  * which doesn't return a copy of the string).
1443  *
1444  * Returns: TRUE if the value could be set correctly. If there was no field
1445  * with @fieldname or the existing field did not contain a data, this function
1446  * returns FALSE.
1447  *
1448  * Since: 0.10.31
1449  */
1450 gboolean
1451 gst_structure_get_date_time (const GstStructure * structure,
1452     const gchar * fieldname, GstDateTime ** value)
1454   GstStructureField *field;
1456   g_return_val_if_fail (structure != NULL, FALSE);
1457   g_return_val_if_fail (fieldname != NULL, FALSE);
1458   g_return_val_if_fail (value != NULL, FALSE);
1460   field = gst_structure_get_field (structure, fieldname);
1462   if (field == NULL)
1463     return FALSE;
1464   if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1465     return FALSE;
1467   /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1468   *value = g_value_dup_boxed (&field->value);
1470   return TRUE;
1473 /**
1474  * gst_structure_get_clock_time:
1475  * @structure: a #GstStructure
1476  * @fieldname: the name of a field
1477  * @value: (out): a pointer to a #GstClockTime to set
1478  *
1479  * Sets the clock time pointed to by @value corresponding to the clock time
1480  * of the given field.  Caller is responsible for making sure the field exists
1481  * and has the correct type.
1482  *
1483  * Returns: TRUE if the value could be set correctly. If there was no field
1484  * with @fieldname or the existing field did not contain a #GstClockTime, this
1485  * function returns FALSE.
1486  */
1487 gboolean
1488 gst_structure_get_clock_time (const GstStructure * structure,
1489     const gchar * fieldname, GstClockTime * value)
1491   GstStructureField *field;
1493   g_return_val_if_fail (structure != NULL, FALSE);
1494   g_return_val_if_fail (fieldname != NULL, FALSE);
1495   g_return_val_if_fail (value != NULL, FALSE);
1497   field = gst_structure_get_field (structure, fieldname);
1499   if (field == NULL)
1500     return FALSE;
1501   if (!G_VALUE_HOLDS_UINT64 (&field->value))
1502     return FALSE;
1504   *value = gst_g_value_get_uint64_unchecked (&field->value);
1506   return TRUE;
1509 /**
1510  * gst_structure_get_double:
1511  * @structure: a #GstStructure
1512  * @fieldname: the name of a field
1513  * @value: (out): a pointer to a gdouble to set
1514  *
1515  * Sets the double pointed to by @value corresponding to the value of the
1516  * given field.  Caller is responsible for making sure the field exists
1517  * and has the correct type.
1518  *
1519  * Returns: TRUE if the value could be set correctly. If there was no field
1520  * with @fieldname or the existing field did not contain a double, this
1521  * function returns FALSE.
1522  */
1523 gboolean
1524 gst_structure_get_double (const GstStructure * structure,
1525     const gchar * fieldname, gdouble * value)
1527   GstStructureField *field;
1529   g_return_val_if_fail (structure != NULL, FALSE);
1530   g_return_val_if_fail (fieldname != NULL, FALSE);
1531   g_return_val_if_fail (value != NULL, FALSE);
1533   field = gst_structure_get_field (structure, fieldname);
1535   if (field == NULL)
1536     return FALSE;
1537   if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1538     return FALSE;
1540   *value = gst_g_value_get_double_unchecked (&field->value);
1542   return TRUE;
1545 /**
1546  * gst_structure_get_string:
1547  * @structure: a #GstStructure
1548  * @fieldname: the name of a field
1549  *
1550  * Finds the field corresponding to @fieldname, and returns the string
1551  * contained in the field's value.  Caller is responsible for making
1552  * sure the field exists and has the correct type.
1553  *
1554  * The string should not be modified, and remains valid until the next
1555  * call to a gst_structure_*() function with the given structure.
1556  *
1557  * Returns: a pointer to the string or NULL when the field did not exist
1558  * or did not contain a string.
1559  */
1560 const gchar *
1561 gst_structure_get_string (const GstStructure * structure,
1562     const gchar * fieldname)
1564   GstStructureField *field;
1566   g_return_val_if_fail (structure != NULL, NULL);
1567   g_return_val_if_fail (fieldname != NULL, NULL);
1569   field = gst_structure_get_field (structure, fieldname);
1571   if (field == NULL)
1572     return NULL;
1573   if (!G_VALUE_HOLDS_STRING (&field->value))
1574     return NULL;
1576   return gst_g_value_get_string_unchecked (&field->value);
1579 /**
1580  * gst_structure_get_enum:
1581  * @structure: a #GstStructure
1582  * @fieldname: the name of a field
1583  * @enumtype: the enum type of a field
1584  * @value: (out): a pointer to an int to set
1585  *
1586  * Sets the int pointed to by @value corresponding to the value of the
1587  * given field.  Caller is responsible for making sure the field exists,
1588  * has the correct type and that the enumtype is correct.
1589  *
1590  * Returns: TRUE if the value could be set correctly. If there was no field
1591  * with @fieldname or the existing field did not contain an enum of the given
1592  * type, this function returns FALSE.
1593  */
1594 gboolean
1595 gst_structure_get_enum (const GstStructure * structure,
1596     const gchar * fieldname, GType enumtype, gint * value)
1598   GstStructureField *field;
1600   g_return_val_if_fail (structure != NULL, FALSE);
1601   g_return_val_if_fail (fieldname != NULL, FALSE);
1602   g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1603   g_return_val_if_fail (value != NULL, FALSE);
1605   field = gst_structure_get_field (structure, fieldname);
1607   if (field == NULL)
1608     return FALSE;
1609   if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1610     return FALSE;
1612   *value = g_value_get_enum (&field->value);
1614   return TRUE;
1617 /**
1618  * gst_structure_get_fraction:
1619  * @structure: a #GstStructure
1620  * @fieldname: the name of a field
1621  * @value_numerator: (out): a pointer to an int to set
1622  * @value_denominator: (out): a pointer to an int to set
1623  *
1624  * Sets the integers pointed to by @value_numerator and @value_denominator
1625  * corresponding to the value of the given field.  Caller is responsible
1626  * for making sure the field exists and has the correct type.
1627  *
1628  * Returns: TRUE if the values could be set correctly. If there was no field
1629  * with @fieldname or the existing field did not contain a GstFraction, this
1630  * function returns FALSE.
1631  */
1632 gboolean
1633 gst_structure_get_fraction (const GstStructure * structure,
1634     const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1636   GstStructureField *field;
1638   g_return_val_if_fail (structure != NULL, FALSE);
1639   g_return_val_if_fail (fieldname != NULL, FALSE);
1640   g_return_val_if_fail (value_numerator != NULL, FALSE);
1641   g_return_val_if_fail (value_denominator != NULL, FALSE);
1643   field = gst_structure_get_field (structure, fieldname);
1645   if (field == NULL)
1646     return FALSE;
1647   if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1648     return FALSE;
1650   *value_numerator = gst_value_get_fraction_numerator (&field->value);
1651   *value_denominator = gst_value_get_fraction_denominator (&field->value);
1653   return TRUE;
1656 typedef struct _GstStructureAbbreviation
1658   const gchar *type_name;
1659   GType type;
1661 GstStructureAbbreviation;
1663 /* return a copy of an array of GstStructureAbbreviation containing all the
1664  * known type_string, GType maps, including abbreviations for common types */
1665 static GstStructureAbbreviation *
1666 gst_structure_get_abbrs (gint * n_abbrs)
1668   static GstStructureAbbreviation *abbrs = NULL;
1669   static volatile gsize num = 0;
1671   if (g_once_init_enter (&num)) {
1672     /* dynamically generate the array */
1673     gsize _num;
1674     GstStructureAbbreviation dyn_abbrs[] = {
1675       {"int", G_TYPE_INT}
1676       ,
1677       {"i", G_TYPE_INT}
1678       ,
1679       {"uint", G_TYPE_UINT}
1680       ,
1681       {"u", G_TYPE_UINT}
1682       ,
1683       {"float", G_TYPE_FLOAT}
1684       ,
1685       {"f", G_TYPE_FLOAT}
1686       ,
1687       {"double", G_TYPE_DOUBLE}
1688       ,
1689       {"d", G_TYPE_DOUBLE}
1690       ,
1691       {"buffer", GST_TYPE_BUFFER}
1692       ,
1693       {"fourcc", GST_TYPE_FOURCC}
1694       ,
1695       {"4", GST_TYPE_FOURCC}
1696       ,
1697       {"fraction", GST_TYPE_FRACTION}
1698       ,
1699       {"boolean", G_TYPE_BOOLEAN}
1700       ,
1701       {"bool", G_TYPE_BOOLEAN}
1702       ,
1703       {"b", G_TYPE_BOOLEAN}
1704       ,
1705       {"string", G_TYPE_STRING}
1706       ,
1707       {"str", G_TYPE_STRING}
1708       ,
1709       {"s", G_TYPE_STRING}
1710       ,
1711       {"structure", GST_TYPE_STRUCTURE}
1712       ,
1713       {"datetime", GST_TYPE_DATE_TIME}
1714     };
1715     _num = G_N_ELEMENTS (dyn_abbrs);
1716     /* permanently allocate and copy the array now */
1717     abbrs = g_new0 (GstStructureAbbreviation, _num);
1718     memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1719     g_once_init_leave (&num, _num);
1720   }
1721   *n_abbrs = num;
1723   return abbrs;
1726 /* given a type_name that could be a type abbreviation or a registered GType,
1727  * return a matching GType */
1728 static GType
1729 gst_structure_gtype_from_abbr (const char *type_name)
1731   int i;
1732   GstStructureAbbreviation *abbrs;
1733   gint n_abbrs;
1735   g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1737   abbrs = gst_structure_get_abbrs (&n_abbrs);
1739   for (i = 0; i < n_abbrs; i++) {
1740     if (strcmp (type_name, abbrs[i].type_name) == 0) {
1741       return abbrs[i].type;
1742     }
1743   }
1745   /* this is the fallback */
1746   return g_type_from_name (type_name);
1749 static const char *
1750 gst_structure_to_abbr (GType type)
1752   int i;
1753   GstStructureAbbreviation *abbrs;
1754   gint n_abbrs;
1756   g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1758   abbrs = gst_structure_get_abbrs (&n_abbrs);
1760   for (i = 0; i < n_abbrs; i++) {
1761     if (type == abbrs[i].type) {
1762       return abbrs[i].type_name;
1763     }
1764   }
1766   return g_type_name (type);
1769 static GType
1770 gst_structure_value_get_generic_type (GValue * val)
1772   if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1773       || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1774     GArray *array = g_value_peek_pointer (val);
1776     if (array->len > 0) {
1777       GValue *value = &g_array_index (array, GValue, 0);
1779       return gst_structure_value_get_generic_type (value);
1780     } else {
1781       return G_TYPE_INT;
1782     }
1783   } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1784     return G_TYPE_INT;
1785   } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1786     return G_TYPE_INT64;
1787   } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1788     return G_TYPE_DOUBLE;
1789   } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1790     return GST_TYPE_FRACTION;
1791   }
1792   return G_VALUE_TYPE (val);
1795 gboolean
1796 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1797     GString * s)
1799   GstStructureField *field;
1800   guint i, len;
1802   g_return_val_if_fail (s != NULL, FALSE);
1804   g_string_append (s, g_quark_to_string (structure->name));
1805   len = structure->fields->len;
1806   for (i = 0; i < len; i++) {
1807     char *t;
1808     GType type;
1810     field = GST_STRUCTURE_FIELD (structure, i);
1812     t = gst_value_serialize (&field->value);
1813     type = gst_structure_value_get_generic_type (&field->value);
1815     g_string_append_len (s, ", ", 2);
1816     /* FIXME: do we need to escape fieldnames? */
1817     g_string_append (s, g_quark_to_string (field->name));
1818     g_string_append_len (s, "=(", 2);
1819     g_string_append (s, gst_structure_to_abbr (type));
1820     g_string_append_c (s, ')');
1821     g_string_append (s, t == NULL ? "NULL" : t);
1822     g_free (t);
1823   }
1825   g_string_append_c (s, ';');
1826   return TRUE;
1829 /**
1830  * gst_structure_to_string:
1831  * @structure: a #GstStructure
1832  *
1833  * Converts @structure to a human-readable string representation.
1834  *
1835  * For debugging purposes its easier to do something like this:
1836  * |[
1837  * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1838  * ]|
1839  * This prints the structure in human readble form.
1840  *
1841  * Free-function: g_free
1842  *
1843  * Returns: (transfer full)L a pointer to string allocated by g_malloc().
1844  *     g_free() after usage.
1845  */
1846 gchar *
1847 gst_structure_to_string (const GstStructure * structure)
1849   GString *s;
1851   /* NOTE:  This function is potentially called by the debug system,
1852    * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1853    * should be careful to avoid recursion.  This includes any functions
1854    * called by gst_structure_to_string.  In particular, calls should
1855    * not use the GST_PTR_FORMAT extension.  */
1857   g_return_val_if_fail (structure != NULL, NULL);
1859   /* we estimate a minimum size based on the number of fields in order to
1860    * avoid unnecessary reallocs within GString */
1861   s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1862   priv_gst_structure_append_to_gstring (structure, s);
1863   return g_string_free (s, FALSE);
1866 /*
1867  * r will still point to the string. if end == next, the string will not be
1868  * null-terminated. In all other cases it will be.
1869  * end = pointer to char behind end of string, next = pointer to start of
1870  * unread data.
1871  * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1872  */
1873 static gboolean
1874 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1875     gboolean unescape)
1877   gchar *w;
1879   if (*s == 0)
1880     return FALSE;
1882   if (*s != '"') {
1883     int ret;
1885     ret = gst_structure_parse_simple_string (s, end);
1886     *next = *end;
1888     return ret;
1889   }
1891   if (unescape) {
1892     w = s;
1893     s++;
1894     while (*s != '"') {
1895       if (G_UNLIKELY (*s == 0))
1896         return FALSE;
1897       if (G_UNLIKELY (*s == '\\'))
1898         s++;
1899       *w = *s;
1900       w++;
1901       s++;
1902     }
1903     s++;
1904   } else {
1905     /* Find the closing quotes */
1906     s++;
1907     while (*s != '"') {
1908       if (G_UNLIKELY (*s == 0))
1909         return FALSE;
1910       if (G_UNLIKELY (*s == '\\'))
1911         s++;
1912       s++;
1913     }
1914     s++;
1915     w = s;
1916   }
1918   *end = w;
1919   *next = s;
1921   return TRUE;
1924 static gboolean
1925 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1926     GType type)
1928   GValue value1 = { 0 };
1929   GValue value2 = { 0 };
1930   GType range_type;
1931   gboolean ret;
1933   if (*s != '[')
1934     return FALSE;
1935   s++;
1937   ret = gst_structure_parse_value (s, &s, &value1, type);
1938   if (ret == FALSE)
1939     return FALSE;
1941   while (g_ascii_isspace (*s))
1942     s++;
1944   if (*s != ',')
1945     return FALSE;
1946   s++;
1948   while (g_ascii_isspace (*s))
1949     s++;
1951   ret = gst_structure_parse_value (s, &s, &value2, type);
1952   if (ret == FALSE)
1953     return FALSE;
1955   while (g_ascii_isspace (*s))
1956     s++;
1958   if (*s != ']')
1959     return FALSE;
1960   s++;
1962   if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1963     return FALSE;
1965   if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1966     range_type = GST_TYPE_DOUBLE_RANGE;
1967     g_value_init (value, range_type);
1968     gst_value_set_double_range (value,
1969         gst_g_value_get_double_unchecked (&value1),
1970         gst_g_value_get_double_unchecked (&value2));
1971   } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1972     range_type = GST_TYPE_INT_RANGE;
1973     g_value_init (value, range_type);
1974     gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
1975         gst_g_value_get_int_unchecked (&value2));
1976   } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1977     range_type = GST_TYPE_INT64_RANGE;
1978     g_value_init (value, range_type);
1979     gst_value_set_int64_range (value, gst_g_value_get_int64_unchecked (&value1),
1980         gst_g_value_get_int64_unchecked (&value2));
1981   } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1982     range_type = GST_TYPE_FRACTION_RANGE;
1983     g_value_init (value, range_type);
1984     gst_value_set_fraction_range (value, &value1, &value2);
1985   } else {
1986     return FALSE;
1987   }
1989   *after = s;
1990   return TRUE;
1993 static gboolean
1994 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1995     GType type, GType list_type, char begin, char end)
1997   GValue list_value = { 0 };
1998   gboolean ret;
1999   GArray *array;
2001   g_value_init (value, list_type);
2002   array = g_value_peek_pointer (value);
2004   if (*s != begin)
2005     return FALSE;
2006   s++;
2008   while (g_ascii_isspace (*s))
2009     s++;
2010   if (*s == end) {
2011     s++;
2012     *after = s;
2013     return TRUE;
2014   }
2016   ret = gst_structure_parse_value (s, &s, &list_value, type);
2017   if (ret == FALSE)
2018     return FALSE;
2020   g_array_append_val (array, list_value);
2022   while (g_ascii_isspace (*s))
2023     s++;
2025   while (*s != end) {
2026     if (*s != ',')
2027       return FALSE;
2028     s++;
2030     while (g_ascii_isspace (*s))
2031       s++;
2033     memset (&list_value, 0, sizeof (list_value));
2034     ret = gst_structure_parse_value (s, &s, &list_value, type);
2035     if (ret == FALSE)
2036       return FALSE;
2038     g_array_append_val (array, list_value);
2039     while (g_ascii_isspace (*s))
2040       s++;
2041   }
2043   s++;
2045   *after = s;
2046   return TRUE;
2049 static gboolean
2050 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2052   return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2053       '{', '}');
2056 static gboolean
2057 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2058     GType type)
2060   return gst_structure_parse_any_list (s, after, value, type,
2061       GST_TYPE_ARRAY, '<', '>');
2064 static gboolean
2065 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2067   char *s = str;
2069   while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2070     s++;
2071   }
2073   *end = s;
2075   return (s != str);
2078 static gboolean
2079 gst_structure_parse_field (gchar * str,
2080     gchar ** after, GstStructureField * field)
2082   gchar *name;
2083   gchar *name_end;
2084   gchar *s;
2085   gchar c;
2087   s = str;
2089   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2090     s++;
2091   name = s;
2092   if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2093     GST_WARNING ("failed to parse simple string, str=%s", str);
2094     return FALSE;
2095   }
2097   s = name_end;
2098   while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2099     s++;
2101   if (G_UNLIKELY (*s != '=')) {
2102     GST_WARNING ("missing assignment operator in the field, str=%s", str);
2103     return FALSE;
2104   }
2105   s++;
2107   c = *name_end;
2108   *name_end = '\0';
2109   field->name = g_quark_from_string (name);
2110   GST_DEBUG ("trying field name '%s'", name);
2111   *name_end = c;
2113   if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2114               G_TYPE_INVALID))) {
2115     GST_WARNING ("failed to parse value %s", str);
2116     return FALSE;
2117   }
2119   *after = s;
2120   return TRUE;
2123 static gboolean
2124 gst_structure_parse_value (gchar * str,
2125     gchar ** after, GValue * value, GType default_type)
2127   gchar *type_name;
2128   gchar *type_end;
2129   gchar *value_s;
2130   gchar *value_end;
2131   gchar *s;
2132   gchar c;
2133   int ret = 0;
2134   GType type = default_type;
2136   s = str;
2137   while (g_ascii_isspace (*s))
2138     s++;
2140   /* check if there's a (type_name) 'cast' */
2141   type_name = NULL;
2142   if (*s == '(') {
2143     s++;
2144     while (g_ascii_isspace (*s))
2145       s++;
2146     type_name = s;
2147     if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2148       return FALSE;
2149     s = type_end;
2150     while (g_ascii_isspace (*s))
2151       s++;
2152     if (G_UNLIKELY (*s != ')'))
2153       return FALSE;
2154     s++;
2155     while (g_ascii_isspace (*s))
2156       s++;
2158     c = *type_end;
2159     *type_end = 0;
2160     type = gst_structure_gtype_from_abbr (type_name);
2161     GST_DEBUG ("trying type name '%s'", type_name);
2162     *type_end = c;
2164     if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2165       GST_WARNING ("invalid type");
2166       return FALSE;
2167     }
2168   }
2170   while (g_ascii_isspace (*s))
2171     s++;
2172   if (*s == '[') {
2173     ret = gst_structure_parse_range (s, &s, value, type);
2174   } else if (*s == '{') {
2175     ret = gst_structure_parse_list (s, &s, value, type);
2176   } else if (*s == '<') {
2177     ret = gst_structure_parse_array (s, &s, value, type);
2178   } else {
2179     value_s = s;
2181     if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2182       GType try_types[] =
2183           { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2184         G_TYPE_STRING
2185       };
2186       int i;
2188       if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2189         return FALSE;
2190       /* Set NULL terminator for deserialization */
2191       c = *value_end;
2192       *value_end = '\0';
2194       for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2195         g_value_init (value, try_types[i]);
2196         ret = gst_value_deserialize (value, value_s);
2197         if (ret)
2198           break;
2199         g_value_unset (value);
2200       }
2201     } else {
2202       g_value_init (value, type);
2204       if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2205                   (type != G_TYPE_STRING))))
2206         return FALSE;
2207       /* Set NULL terminator for deserialization */
2208       c = *value_end;
2209       *value_end = '\0';
2211       ret = gst_value_deserialize (value, value_s);
2212       if (G_UNLIKELY (!ret))
2213         g_value_unset (value);
2214     }
2215     *value_end = c;
2216   }
2218   *after = s;
2220   return ret;
2223 /**
2224  * gst_structure_from_string:
2225  * @string: a string representation of a #GstStructure.
2226  * @end: (out) (allow-none): pointer to store the end of the string in.
2227  *
2228  * Creates a #GstStructure from a string representation.
2229  * If end is not NULL, a pointer to the place inside the given string
2230  * where parsing ended will be returned.
2231  *
2232  * Free-function: gst_structure_free
2233  *
2234  * Returns: (transfer full): a new #GstStructure or NULL when the string could
2235  *     not be parsed. Free with gst_structure_free() after use.
2236  */
2237 GstStructure *
2238 gst_structure_from_string (const gchar * string, gchar ** end)
2240   char *name;
2241   char *copy;
2242   char *w;
2243   char *r;
2244   char save;
2245   GstStructure *structure = NULL;
2246   GstStructureField field;
2248   g_return_val_if_fail (string != NULL, NULL);
2250   copy = g_strdup (string);
2251   r = copy;
2253   /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2254   while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2255               && g_ascii_isspace (r[1]))))
2256     r++;
2258   name = r;
2259   if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2260     GST_WARNING ("Failed to parse structure string '%s'", string);
2261     goto error;
2262   }
2264   save = *w;
2265   *w = '\0';
2266   structure = gst_structure_empty_new (name);
2267   *w = save;
2269   if (G_UNLIKELY (structure == NULL))
2270     goto error;
2272   do {
2273     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2274                 && g_ascii_isspace (r[1]))))
2275       r++;
2276     if (*r == ';') {
2277       /* end of structure, get the next char and finish */
2278       r++;
2279       break;
2280     }
2281     if (*r == '\0') {
2282       /* accept \0 as end delimiter */
2283       break;
2284     }
2285     if (G_UNLIKELY (*r != ',')) {
2286       GST_WARNING ("Failed to find delimiter, r=%s", r);
2287       goto error;
2288     }
2289     r++;
2290     while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2291                 && g_ascii_isspace (r[1]))))
2292       r++;
2294     memset (&field, 0, sizeof (field));
2295     if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2296       GST_WARNING ("Failed to parse field, r=%s", r);
2297       goto error;
2298     }
2299     gst_structure_set_field (structure, &field);
2300   } while (TRUE);
2302   if (end)
2303     *end = (char *) string + (r - copy);
2304   else if (*r)
2305     g_warning ("gst_structure_from_string did not consume whole string,"
2306         " but caller did not provide end pointer (\"%s\")", string);
2308   g_free (copy);
2309   return structure;
2311 error:
2312   if (structure)
2313     gst_structure_free (structure);
2314   g_free (copy);
2315   return NULL;
2318 static void
2319 gst_structure_transform_to_string (const GValue * src_value,
2320     GValue * dest_value)
2322   g_return_if_fail (src_value != NULL);
2323   g_return_if_fail (dest_value != NULL);
2325   dest_value->data[0].v_pointer =
2326       gst_structure_to_string (src_value->data[0].v_pointer);
2329 static GstStructure *
2330 gst_structure_copy_conditional (const GstStructure * structure)
2332   if (structure)
2333     return gst_structure_copy (structure);
2334   return NULL;
2337 /* fixate utility functions */
2339 /**
2340  * gst_structure_fixate_field_nearest_int:
2341  * @structure: a #GstStructure
2342  * @field_name: a field in @structure
2343  * @target: the target value of the fixation
2344  *
2345  * Fixates a #GstStructure by changing the given field to the nearest
2346  * integer to @target that is a subset of the existing field.
2347  *
2348  * Returns: TRUE if the structure could be fixated
2349  */
2350 gboolean
2351 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2352     const char *field_name, int target)
2354   const GValue *value;
2356   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2357   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2359   value = gst_structure_get_value (structure, field_name);
2361   if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2362     /* already fixed */
2363     return FALSE;
2364   } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2365     int x;
2367     x = gst_value_get_int_range_min (value);
2368     if (target < x)
2369       target = x;
2370     x = gst_value_get_int_range_max (value);
2371     if (target > x)
2372       target = x;
2373     gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2374     return TRUE;
2375   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2376     const GValue *list_value;
2377     int i, n;
2378     int best = 0;
2379     int best_index = -1;
2381     n = gst_value_list_get_size (value);
2382     for (i = 0; i < n; i++) {
2383       list_value = gst_value_list_get_value (value, i);
2384       if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2385         int x = gst_g_value_get_int_unchecked (list_value);
2387         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2388           best_index = i;
2389           best = x;
2390         }
2391       }
2392     }
2393     if (best_index != -1) {
2394       gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2395       return TRUE;
2396     }
2397     return FALSE;
2398   }
2400   return FALSE;
2403 /**
2404  * gst_structure_fixate_field_nearest_double:
2405  * @structure: a #GstStructure
2406  * @field_name: a field in @structure
2407  * @target: the target value of the fixation
2408  *
2409  * Fixates a #GstStructure by changing the given field to the nearest
2410  * double to @target that is a subset of the existing field.
2411  *
2412  * Returns: TRUE if the structure could be fixated
2413  */
2414 gboolean
2415 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2416     const char *field_name, double target)
2418   const GValue *value;
2420   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2421   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2423   value = gst_structure_get_value (structure, field_name);
2425   if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2426     /* already fixed */
2427     return FALSE;
2428   } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2429     double x;
2431     x = gst_value_get_double_range_min (value);
2432     if (target < x)
2433       target = x;
2434     x = gst_value_get_double_range_max (value);
2435     if (target > x)
2436       target = x;
2437     gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2438     return TRUE;
2439   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2440     const GValue *list_value;
2441     int i, n;
2442     double best = 0;
2443     int best_index = -1;
2445     n = gst_value_list_get_size (value);
2446     for (i = 0; i < n; i++) {
2447       list_value = gst_value_list_get_value (value, i);
2448       if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2449         double x = gst_g_value_get_double_unchecked (list_value);
2451         if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2452           best_index = i;
2453           best = x;
2454         }
2455       }
2456     }
2457     if (best_index != -1) {
2458       gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2459       return TRUE;
2460     }
2461     return FALSE;
2462   }
2464   return FALSE;
2468 /**
2469  * gst_structure_fixate_field_boolean:
2470  * @structure: a #GstStructure
2471  * @field_name: a field in @structure
2472  * @target: the target value of the fixation
2473  *
2474  * Fixates a #GstStructure by changing the given @field_name field to the given
2475  * @target boolean if that field is not fixed yet.
2476  *
2477  * Returns: TRUE if the structure could be fixated
2478  */
2479 gboolean
2480 gst_structure_fixate_field_boolean (GstStructure * structure,
2481     const char *field_name, gboolean target)
2483   const GValue *value;
2485   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2486   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2488   value = gst_structure_get_value (structure, field_name);
2490   if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2491     /* already fixed */
2492     return FALSE;
2493   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2494     const GValue *list_value;
2495     int i, n;
2496     int best = 0;
2497     int best_index = -1;
2499     n = gst_value_list_get_size (value);
2500     for (i = 0; i < n; i++) {
2501       list_value = gst_value_list_get_value (value, i);
2502       if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2503         gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2505         if (best_index == -1 || x == target) {
2506           best_index = i;
2507           best = x;
2508         }
2509       }
2510     }
2511     if (best_index != -1) {
2512       gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2513       return TRUE;
2514     }
2515     return FALSE;
2516   }
2518   return FALSE;
2521 /**
2522  * gst_structure_fixate_field_string:
2523  * @structure: a #GstStructure
2524  * @field_name: a field in @structure
2525  * @target: the target value of the fixation
2526  *
2527  * Fixates a #GstStructure by changing the given @field_name field to the given
2528  * @target string if that field is not fixed yet.
2529  *
2530  * Returns: TRUE if the structure could be fixated
2531  *
2532  * Since: 0.10.30
2533  */
2534 gboolean
2535 gst_structure_fixate_field_string (GstStructure * structure,
2536     const gchar * field_name, const gchar * target)
2538   const GValue *value;
2540   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2541   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2543   value = gst_structure_get_value (structure, field_name);
2545   if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2546     /* already fixed */
2547     return FALSE;
2548   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2549     const GValue *list_value;
2550     int i, n;
2551     const gchar *best = NULL;
2552     int best_index = -1;
2554     n = gst_value_list_get_size (value);
2555     for (i = 0; i < n; i++) {
2556       list_value = gst_value_list_get_value (value, i);
2557       if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2558         const gchar *x = g_value_get_string (list_value);
2560         if (best_index == -1 || g_str_equal (x, target)) {
2561           best_index = i;
2562           best = x;
2563         }
2564       }
2565     }
2566     if (best_index != -1) {
2567       gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2568       return TRUE;
2569     }
2570     return FALSE;
2571   }
2573   return FALSE;
2576 /**
2577  * gst_structure_fixate_field_nearest_fraction:
2578  * @structure: a #GstStructure
2579  * @field_name: a field in @structure
2580  * @target_numerator: The numerator of the target value of the fixation
2581  * @target_denominator: The denominator of the target value of the fixation
2582  *
2583  * Fixates a #GstStructure by changing the given field to the nearest
2584  * fraction to @target_numerator/@target_denominator that is a subset
2585  * of the existing field.
2586  *
2587  * Returns: TRUE if the structure could be fixated
2588  */
2589 gboolean
2590 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2591     const char *field_name, const gint target_numerator,
2592     const gint target_denominator)
2594   const GValue *value;
2596   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2597   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2599   value = gst_structure_get_value (structure, field_name);
2601   if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2602     /* already fixed */
2603     return FALSE;
2604   } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2605     const GValue *x, *new_value;
2606     GValue target = { 0 };
2607     g_value_init (&target, GST_TYPE_FRACTION);
2608     gst_value_set_fraction (&target, target_numerator, target_denominator);
2610     new_value = &target;
2611     x = gst_value_get_fraction_range_min (value);
2612     if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2613       new_value = x;
2614     x = gst_value_get_fraction_range_max (value);
2615     if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2616       new_value = x;
2618     gst_structure_set_value (structure, field_name, new_value);
2619     g_value_unset (&target);
2620     return TRUE;
2621   } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2622     const GValue *list_value;
2623     int i, n;
2624     const GValue *best = NULL;
2625     gdouble target;
2626     gdouble cur_diff;
2627     gdouble best_diff = G_MAXDOUBLE;
2629     target = (gdouble) target_numerator / (gdouble) target_denominator;
2631     GST_DEBUG ("target %g, best %g", target, best_diff);
2633     best = NULL;
2635     n = gst_value_list_get_size (value);
2636     for (i = 0; i < n; i++) {
2637       list_value = gst_value_list_get_value (value, i);
2638       if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2639         gint num, denom;
2640         gdouble list_double;
2642         num = gst_value_get_fraction_numerator (list_value);
2643         denom = gst_value_get_fraction_denominator (list_value);
2645         list_double = ((gdouble) num / (gdouble) denom);
2646         cur_diff = target - list_double;
2648         GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2650         if (cur_diff < 0)
2651           cur_diff = -cur_diff;
2653         if (!best || cur_diff < best_diff) {
2654           GST_DEBUG ("new best %g", list_double);
2655           best = list_value;
2656           best_diff = cur_diff;
2657         }
2658       }
2659     }
2660     if (best != NULL) {
2661       gst_structure_set_value (structure, field_name, best);
2662       return TRUE;
2663     }
2664   }
2666   return FALSE;
2669 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2670  * (useful for message parsing functions where the return location is user
2671  * supplied and the user may pass NULL if the value isn't of interest) */
2672 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname)           \
2673 G_STMT_START {                                                                \
2674   const GValue *_value = (value);                                             \
2675   guint _flags = (flags);                                                     \
2676   GType _value_type = G_VALUE_TYPE (_value);                                  \
2677   GTypeValueTable *_vtable = g_type_value_table_peek (_value_type);           \
2678   gchar *_lcopy_format = _vtable->lcopy_format;                               \
2679   GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, };      \
2680   guint _n_values = 0;                                                        \
2681                                                                               \
2682   while (*_lcopy_format != '\0') {                                            \
2683     g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER);                     \
2684     _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer);          \
2685     _lcopy_format++;                                                          \
2686   }                                                                           \
2687   if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2688     *(__error) = g_strdup_printf ("either all or none of the return "         \
2689         "locations for field '%s' need to be NULL", fieldname);               \
2690   } else if (_cvalues[0].v_pointer != NULL) {                                 \
2691     *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags);  \
2692   }                                                                           \
2693 } G_STMT_END
2695 /**
2696  * gst_structure_get_valist:
2697  * @structure: a #GstStructure
2698  * @first_fieldname: the name of the first field to read
2699  * @args: variable arguments
2700  *
2701  * Parses the variable arguments and reads fields from @structure accordingly.
2702  * valist-variant of gst_structure_get(). Look at the documentation of
2703  * gst_structure_get() for more details.
2704  *
2705  * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2706  *
2707  * Since: 0.10.24
2708  */
2709 gboolean
2710 gst_structure_get_valist (const GstStructure * structure,
2711     const char *first_fieldname, va_list args)
2713   const char *field_name;
2714   GType expected_type = G_TYPE_INVALID;
2716   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2717   g_return_val_if_fail (first_fieldname != NULL, FALSE);
2719   field_name = first_fieldname;
2720   while (field_name) {
2721     const GValue *val = NULL;
2722     gchar *err = NULL;
2724     expected_type = va_arg (args, GType);
2726     val = gst_structure_get_value (structure, field_name);
2728     if (val == NULL)
2729       goto no_such_field;
2731     if (G_VALUE_TYPE (val) != expected_type)
2732       goto wrong_type;
2734     GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2735     if (err) {
2736       g_warning ("%s: %s", G_STRFUNC, err);
2737       g_free (err);
2738       return FALSE;
2739     }
2741     field_name = va_arg (args, const gchar *);
2742   }
2744   return TRUE;
2746 /* ERRORS */
2747 no_such_field:
2748   {
2749     GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2750         field_name, structure);
2751     return FALSE;
2752   }
2753 wrong_type:
2754   {
2755     GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2756         "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2757         GST_STR_NULL (g_type_name (expected_type)),
2758         G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2759         structure);
2760     return FALSE;
2761   }
2764 /**
2765  * gst_structure_id_get_valist:
2766  * @structure: a #GstStructure
2767  * @first_field_id: the quark of the first field to read
2768  * @args: variable arguments
2769  *
2770  * Parses the variable arguments and reads fields from @structure accordingly.
2771  * valist-variant of gst_structure_id_get(). Look at the documentation of
2772  * gst_structure_id_get() for more details.
2773  *
2774  * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2775  *
2776  * Since: 0.10.24
2777  */
2778 gboolean
2779 gst_structure_id_get_valist (const GstStructure * structure,
2780     GQuark first_field_id, va_list args)
2782   GQuark field_id;
2783   GType expected_type = G_TYPE_INVALID;
2785   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2786   g_return_val_if_fail (first_field_id != 0, FALSE);
2788   field_id = first_field_id;
2789   while (field_id) {
2790     const GValue *val = NULL;
2791     gchar *err = NULL;
2793     expected_type = va_arg (args, GType);
2795     val = gst_structure_id_get_value (structure, field_id);
2797     if (val == NULL)
2798       goto no_such_field;
2800     if (G_VALUE_TYPE (val) != expected_type)
2801       goto wrong_type;
2803     GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2804     if (err) {
2805       g_warning ("%s: %s", G_STRFUNC, err);
2806       g_free (err);
2807       return FALSE;
2808     }
2810     field_id = va_arg (args, GQuark);
2811   }
2813   return TRUE;
2815 /* ERRORS */
2816 no_such_field:
2817   {
2818     GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2819         GST_STR_NULL (g_quark_to_string (field_id)), structure);
2820     return FALSE;
2821   }
2822 wrong_type:
2823   {
2824     GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2825         "field was of type '%s': %" GST_PTR_FORMAT,
2826         g_quark_to_string (field_id),
2827         GST_STR_NULL (g_type_name (expected_type)),
2828         G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2829         structure);
2830     return FALSE;
2831   }
2834 /**
2835  * gst_structure_get:
2836  * @structure: a #GstStructure
2837  * @first_fieldname: the name of the first field to read
2838  * @...: variable arguments
2839  *
2840  * Parses the variable arguments and reads fields from @structure accordingly.
2841  * Variable arguments should be in the form field name, field type
2842  * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2843  * The last variable argument should be NULL.
2844  *
2845  * For refcounted (mini)objects you will acquire your own reference which
2846  * you must release with a suitable _unref() when no longer needed. For
2847  * strings and boxed types you will acquire a copy which you will need to
2848  * release with either g_free() or the suiteable function for the boxed type.
2849  *
2850  * Returns: FALSE if there was a problem reading any of the fields (e.g.
2851  *     because the field requested did not exist, or was of a type other
2852  *     than the type specified), otherwise TRUE.
2853  *
2854  * Since: 0.10.24
2855  */
2856 gboolean
2857 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
2858     ...)
2860   gboolean ret;
2861   va_list args;
2863   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2864   g_return_val_if_fail (first_fieldname != NULL, FALSE);
2866   va_start (args, first_fieldname);
2867   ret = gst_structure_get_valist (structure, first_fieldname, args);
2868   va_end (args);
2870   return ret;
2873 /**
2874  * gst_structure_id_get:
2875  * @structure: a #GstStructure
2876  * @first_field_id: the quark of the first field to read
2877  * @...: variable arguments
2878  *
2879  * Parses the variable arguments and reads fields from @structure accordingly.
2880  * Variable arguments should be in the form field id quark, field type
2881  * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2882  * The last variable argument should be NULL (technically it should be a
2883  * 0 quark, but we require NULL so compilers that support it can check for
2884  * the NULL terminator and warn if it's not there).
2885  *
2886  * This function is just like gst_structure_get() only that it is slightly
2887  * more efficient since it saves the string-to-quark lookup in the global
2888  * quark hashtable.
2889  *
2890  * For refcounted (mini)objects you will acquire your own reference which
2891  * you must release with a suitable _unref() when no longer needed. For
2892  * strings and boxed types you will acquire a copy which you will need to
2893  * release with either g_free() or the suiteable function for the boxed type.
2894  *
2895  * Returns: FALSE if there was a problem reading any of the fields (e.g.
2896  *     because the field requested did not exist, or was of a type other
2897  *     than the type specified), otherwise TRUE.
2898  *
2899  * Since: 0.10.24
2900  */
2901 gboolean
2902 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
2903     ...)
2905   gboolean ret;
2906   va_list args;
2908   g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2909   g_return_val_if_fail (first_field_id != 0, FALSE);
2911   va_start (args, first_field_id);
2912   ret = gst_structure_id_get_valist (structure, first_field_id, args);
2913   va_end (args);
2915   return ret;