2 * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
4 * gststructure.c: lists of { GQuark, GValue } tuples
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.
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.
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.
23 * SECTION:gststructure
24 * @short_description: Generic structure containing fields of names and values
25 * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
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.
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 "/-_.:".
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.
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.
43 * Field values can be changed with gst_structure_set_value() or
44 * gst_structure_set().
46 * Field values can be retrieved with gst_structure_get_value() or the more
47 * convenient gst_structure_get_*() functions.
49 * Fields can be removed with gst_structure_remove_field() or
50 * gst_structure_remove_fields().
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.
55 * Last reviewed on 2009-06-08 (0.10.23)
64 #include "gst_private.h"
67 #include <gobject/gvaluecollector.h>
69 typedef struct _GstStructureField GstStructureField;
71 struct _GstStructureField
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,
95 static GstStructure *gst_structure_copy_conditional (const GstStructure *
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);
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);
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;
128 g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
134 * gst_structure_id_empty_new:
135 * @quark: name of new structure
137 * Creates a new, empty #GstStructure with the given name as a GQuark.
139 * Returns: a new, empty #GstStructure
142 gst_structure_id_empty_new (GQuark quark)
144 g_return_val_if_fail (quark != 0, NULL);
146 return gst_structure_id_empty_new_with_size (quark, 0);
149 #ifndef G_DISABLE_CHECKS
151 gst_structure_validate_name (const gchar * name)
155 g_return_val_if_fail (name != NULL, FALSE);
157 /* FIXME 0.11: use g_ascii_isalpha() */
158 if (G_UNLIKELY (!g_ascii_isalnum (*name))) {
159 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
164 /* FIXME 0.11: don't allow spaces */
165 /* FIXME: test name string more */
167 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+ ", *s) != NULL))
169 if (G_UNLIKELY (*s != '\0')) {
170 GST_WARNING ("Invalid character '%c' at offset %lu in structure name: %s",
171 *s, ((gulong) s - (gulong) name), name);
180 * gst_structure_empty_new:
181 * @name: name of new structure
183 * Creates a new, empty #GstStructure with the given @name.
185 * See gst_structure_set_name() for constraints on the @name parameter.
187 * Returns: a new, empty #GstStructure
190 gst_structure_empty_new (const gchar * name)
192 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
194 return gst_structure_id_empty_new_with_size (g_quark_from_string (name), 0);
199 * @name: name of new structure
200 * @firstfield: name of first field to set
201 * @...: additional arguments
203 * Creates a new #GstStructure with the given name. Parses the
204 * list of variable arguments and sets fields to the values listed.
205 * Variable arguments should be passed as field name, field type,
206 * and value. Last variable argument should be NULL.
208 * Returns: a new #GstStructure
211 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
213 GstStructure *structure;
216 g_return_val_if_fail (name != NULL, NULL);
218 va_start (varargs, firstfield);
219 structure = gst_structure_new_valist (name, firstfield, varargs);
226 * gst_structure_new_valist:
227 * @name: name of new structure
228 * @firstfield: name of first field to set
229 * @varargs: variable argument list
231 * Creates a new #GstStructure with the given @name. Structure fields
232 * are set according to the varargs in a manner similar to
233 * gst_structure_new().
235 * See gst_structure_set_name() for constraints on the @name parameter.
237 * Returns: a new #GstStructure
240 gst_structure_new_valist (const gchar * name,
241 const gchar * firstfield, va_list varargs)
243 GstStructure *structure;
245 g_return_val_if_fail (name != NULL, NULL);
247 structure = gst_structure_empty_new (name);
250 gst_structure_set_valist (structure, firstfield, varargs);
256 * gst_structure_set_parent_refcount:
257 * @structure: a #GstStructure
258 * @refcount: a pointer to the parent's refcount
260 * Sets the parent_refcount field of #GstStructure. This field is used to
261 * determine whether a structure is mutable or not. This function should only be
262 * called by code implementing parent objects of #GstStructure, as described in
263 * the MT Refcounting section of the design documents.
266 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
268 g_return_if_fail (structure != NULL);
270 /* if we have a parent_refcount already, we can only clear
271 * if with a NULL refcount */
272 if (structure->parent_refcount)
273 g_return_if_fail (refcount == NULL);
275 g_return_if_fail (refcount != NULL);
277 structure->parent_refcount = refcount;
281 * gst_structure_copy:
282 * @structure: a #GstStructure to duplicate
284 * Duplicates a #GstStructure and all its fields and values.
286 * Returns: a new #GstStructure.
289 gst_structure_copy (const GstStructure * structure)
291 GstStructure *new_structure;
292 GstStructureField *field;
295 g_return_val_if_fail (structure != NULL, NULL);
297 len = structure->fields->len;
298 new_structure = gst_structure_id_empty_new_with_size (structure->name, len);
300 for (i = 0; i < len; i++) {
301 GstStructureField new_field = { 0 };
303 field = GST_STRUCTURE_FIELD (structure, i);
305 new_field.name = field->name;
306 gst_value_init_and_copy (&new_field.value, &field->value);
307 g_array_append_val (new_structure->fields, new_field);
310 return new_structure;
314 * gst_structure_free:
315 * @structure: the #GstStructure to free
317 * Frees a #GstStructure and all its fields and values. The structure must not
318 * have a parent when this function is called.
321 gst_structure_free (GstStructure * structure)
323 GstStructureField *field;
326 g_return_if_fail (structure != NULL);
327 g_return_if_fail (structure->parent_refcount == NULL);
329 len = structure->fields->len;
330 for (i = 0; i < len; i++) {
331 field = GST_STRUCTURE_FIELD (structure, i);
333 if (G_IS_VALUE (&field->value)) {
334 g_value_unset (&field->value);
337 g_array_free (structure->fields, TRUE);
339 memset (structure, 0xff, sizeof (GstStructure));
341 g_slice_free (GstStructure, structure);
345 * gst_structure_get_name:
346 * @structure: a #GstStructure
348 * Get the name of @structure as a string.
350 * Returns: the name of the structure.
353 gst_structure_get_name (const GstStructure * structure)
355 g_return_val_if_fail (structure != NULL, NULL);
357 return g_quark_to_string (structure->name);
361 * gst_structure_has_name:
362 * @structure: a #GstStructure
363 * @name: structure name to check for
365 * Checks if the structure has the given name
367 * Returns: TRUE if @name matches the name of the structure.
370 gst_structure_has_name (const GstStructure * structure, const gchar * name)
372 const gchar *structure_name;
374 g_return_val_if_fail (structure != NULL, FALSE);
375 g_return_val_if_fail (name != NULL, FALSE);
377 /* getting the string is cheap and comparing short strings is too
378 * should be faster than getting the quark for name and comparing the quarks
380 structure_name = g_quark_to_string (structure->name);
382 return (structure_name && strcmp (structure_name, name) == 0);
386 * gst_structure_get_name_id:
387 * @structure: a #GstStructure
389 * Get the name of @structure as a GQuark.
391 * Returns: the quark representing the name of the structure.
394 gst_structure_get_name_id (const GstStructure * structure)
396 g_return_val_if_fail (structure != NULL, 0);
398 return structure->name;
402 * gst_structure_set_name:
403 * @structure: a #GstStructure
404 * @name: the new name of the structure
406 * Sets the name of the structure to the given @name. The string
407 * provided is copied before being used. It must not be empty, start with a
408 * letter and can be followed by letters, numbers and any of "/-_.:".
411 gst_structure_set_name (GstStructure * structure, const gchar * name)
413 g_return_if_fail (structure != NULL);
414 g_return_if_fail (IS_MUTABLE (structure));
415 g_return_if_fail (gst_structure_validate_name (name));
417 structure->name = g_quark_from_string (name);
421 * gst_structure_id_set_value:
422 * @structure: a #GstStructure
423 * @field: a #GQuark representing a field
424 * @value: the new value of the field
426 * Sets the field with the given GQuark @field to @value. If the field
427 * does not exist, it is created. If the field exists, the previous
428 * value is replaced and freed.
431 gst_structure_id_set_value (GstStructure * structure,
432 GQuark field, const GValue * value)
434 GstStructureField gsfield = { 0, {0,} };
436 g_return_if_fail (structure != NULL);
437 g_return_if_fail (G_IS_VALUE (value));
438 g_return_if_fail (IS_MUTABLE (structure));
440 gsfield.name = field;
441 gst_value_init_and_copy (&gsfield.value, value);
443 gst_structure_set_field (structure, &gsfield);
447 * gst_structure_set_value:
448 * @structure: a #GstStructure
449 * @fieldname: the name of the field to set
450 * @value: the new value of the field
452 * Sets the field with the given name @field to @value. If the field
453 * does not exist, it is created. If the field exists, the previous
454 * value is replaced and freed.
457 gst_structure_set_value (GstStructure * structure,
458 const gchar * fieldname, const GValue * value)
460 g_return_if_fail (structure != NULL);
461 g_return_if_fail (fieldname != NULL);
462 g_return_if_fail (G_IS_VALUE (value));
463 g_return_if_fail (IS_MUTABLE (structure));
465 gst_structure_id_set_value (structure, g_quark_from_string (fieldname),
471 * @structure: a #GstStructure
472 * @fieldname: the name of the field to set
473 * @...: variable arguments
475 * Parses the variable arguments and sets fields accordingly.
476 * Variable arguments should be in the form field name, field type
477 * (as a GType), value(s). The last variable argument should be NULL.
480 gst_structure_set (GstStructure * structure, const gchar * field, ...)
484 g_return_if_fail (structure != NULL);
486 va_start (varargs, field);
487 gst_structure_set_valist (structure, field, varargs);
492 * gst_structure_set_valist:
493 * @structure: a #GstStructure
494 * @fieldname: the name of the field to set
495 * @varargs: variable arguments
497 * va_list form of gst_structure_set().
500 gst_structure_set_valist (GstStructure * structure,
501 const gchar * fieldname, va_list varargs)
506 g_return_if_fail (structure != NULL);
507 g_return_if_fail (IS_MUTABLE (structure));
510 GstStructureField field = { 0 };
512 field.name = g_quark_from_string (fieldname);
514 type = va_arg (varargs, GType);
516 if (G_UNLIKELY (type == G_TYPE_DATE)) {
517 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
518 type = GST_TYPE_DATE;
520 #if GLIB_CHECK_VERSION(2,23,3)
521 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
523 g_value_init (&field.value, type);
524 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
526 if (G_UNLIKELY (err)) {
527 g_critical ("%s", err);
530 gst_structure_set_field (structure, &field);
532 fieldname = va_arg (varargs, gchar *);
537 * gst_structure_id_set:
538 * @structure: a #GstStructure
539 * @fieldname: the GQuark for the name of the field to set
540 * @...: variable arguments
542 * Identical to gst_structure_set, except that field names are
543 * passed using the GQuark for the field name. This allows more efficient
544 * setting of the structure if the caller already knows the associated
546 * The last variable argument must be NULL.
551 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
555 g_return_if_fail (structure != NULL);
557 va_start (varargs, field);
558 gst_structure_id_set_valist (structure, field, varargs);
563 * gst_structure_id_set_valist:
564 * @structure: a #GstStructure
565 * @fieldname: the name of the field to set
566 * @varargs: variable arguments
568 * va_list form of gst_structure_id_set().
573 gst_structure_id_set_valist (GstStructure * structure,
574 GQuark fieldname, va_list varargs)
579 g_return_if_fail (structure != NULL);
580 g_return_if_fail (IS_MUTABLE (structure));
583 GstStructureField field = { 0 };
585 field.name = fieldname;
587 type = va_arg (varargs, GType);
589 if (G_UNLIKELY (type == G_TYPE_DATE)) {
590 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
591 type = GST_TYPE_DATE;
593 #ifndef G_VALUE_COLLECT_INIT
594 g_value_init (&field.value, type);
595 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
597 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
599 if (G_UNLIKELY (err)) {
600 g_critical ("%s", err);
603 gst_structure_set_field (structure, &field);
605 fieldname = va_arg (varargs, GQuark);
610 * gst_structure_id_new:
611 * @name_quark: name of new structure
612 * @field_quark: the GQuark for the name of the field to set
613 * @...: variable arguments
615 * Creates a new #GstStructure with the given name as a GQuark, followed by
616 * fieldname quark, GType, argument(s) "triplets" in the same format as
617 * gst_structure_id_set(). Basically a convenience wrapper around
618 * gst_structure_id_empty_new() and gst_structure_id_set().
620 * The last variable argument must be NULL (or 0).
622 * Returns: a new #GstStructure
627 gst_structure_id_new (GQuark name_quark, GQuark field_quark, ...)
632 g_return_val_if_fail (name_quark != 0, NULL);
633 g_return_val_if_fail (field_quark != 0, NULL);
635 s = gst_structure_id_empty_new (name_quark);
637 va_start (varargs, field_quark);
638 gst_structure_id_set_valist (s, field_quark, varargs);
644 #if GST_VERSION_NANO == 1
645 #define GIT_G_WARNING g_warning
647 #define GIT_G_WARNING GST_WARNING
650 /* If the structure currently contains a field with the same name, it is
651 * replaced with the provided field. Otherwise, the field is added to the
652 * structure. The field's value is not deeply copied.
655 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
657 GstStructureField *f;
658 guint i, len = structure->fields->len;
660 if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
663 s = g_value_get_string (&field->value);
664 /* only check for NULL strings in taglists, as they are allowed in message
665 * structs, e.g. error message debug strings */
666 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
668 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
669 "Please file a bug.", g_quark_to_string (field->name));
670 g_value_unset (&field->value);
673 /* empty strings never make sense */
674 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
675 "Please file a bug.", g_quark_to_string (field->name));
676 g_value_unset (&field->value);
679 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
680 g_warning ("Trying to set string on %s field '%s', but string is not "
681 "valid UTF-8. Please file a bug.",
682 IS_TAGLIST (structure) ? "taglist" : "structure",
683 g_quark_to_string (field->name));
684 g_value_unset (&field->value);
689 for (i = 0; i < len; i++) {
690 f = GST_STRUCTURE_FIELD (structure, i);
692 if (G_UNLIKELY (f->name == field->name)) {
693 g_value_unset (&f->value);
694 memcpy (f, field, sizeof (GstStructureField));
699 g_array_append_val (structure->fields, *field);
702 /* If there is no field with the given ID, NULL is returned.
704 static GstStructureField *
705 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
707 GstStructureField *field;
710 len = structure->fields->len;
712 for (i = 0; i < len; i++) {
713 field = GST_STRUCTURE_FIELD (structure, i);
715 if (G_UNLIKELY (field->name == field_id))
722 /* If there is no field with the given ID, NULL is returned.
724 static GstStructureField *
725 gst_structure_get_field (const GstStructure * structure,
726 const gchar * fieldname)
728 g_return_val_if_fail (structure != NULL, NULL);
729 g_return_val_if_fail (fieldname != NULL, NULL);
731 return gst_structure_id_get_field (structure,
732 g_quark_from_string (fieldname));
736 * gst_structure_get_value:
737 * @structure: a #GstStructure
738 * @fieldname: the name of the field to get
740 * Get the value of the field with name @fieldname.
742 * Returns: the #GValue corresponding to the field with the given name.
745 gst_structure_get_value (const GstStructure * structure,
746 const gchar * fieldname)
748 GstStructureField *field;
750 g_return_val_if_fail (structure != NULL, NULL);
751 g_return_val_if_fail (fieldname != NULL, NULL);
753 field = gst_structure_get_field (structure, fieldname);
757 return &field->value;
761 * gst_structure_id_get_value:
762 * @structure: a #GstStructure
763 * @field: the #GQuark of the field to get
765 * Get the value of the field with GQuark @field.
767 * Returns: the #GValue corresponding to the field with the given name
771 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
773 GstStructureField *gsfield;
775 g_return_val_if_fail (structure != NULL, NULL);
777 gsfield = gst_structure_id_get_field (structure, field);
781 return &gsfield->value;
785 * gst_structure_remove_field:
786 * @structure: a #GstStructure
787 * @fieldname: the name of the field to remove
789 * Removes the field with the given name. If the field with the given
790 * name does not exist, the structure is unchanged.
793 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
795 GstStructureField *field;
799 g_return_if_fail (structure != NULL);
800 g_return_if_fail (fieldname != NULL);
801 g_return_if_fail (IS_MUTABLE (structure));
803 id = g_quark_from_string (fieldname);
804 len = structure->fields->len;
806 for (i = 0; i < len; i++) {
807 field = GST_STRUCTURE_FIELD (structure, i);
809 if (field->name == id) {
810 if (G_IS_VALUE (&field->value)) {
811 g_value_unset (&field->value);
813 structure->fields = g_array_remove_index (structure->fields, i);
820 * gst_structure_remove_fields:
821 * @structure: a #GstStructure
822 * @fieldname: the name of the field to remove
823 * @...: NULL-terminated list of more fieldnames to remove
825 * Removes the fields with the given names. If a field does not exist, the
826 * argument is ignored.
829 gst_structure_remove_fields (GstStructure * structure,
830 const gchar * fieldname, ...)
834 g_return_if_fail (structure != NULL);
835 g_return_if_fail (fieldname != NULL);
836 /* mutability checked in remove_field */
838 va_start (varargs, fieldname);
839 gst_structure_remove_fields_valist (structure, fieldname, varargs);
844 * gst_structure_remove_fields_valist:
845 * @structure: a #GstStructure
846 * @fieldname: the name of the field to remove
847 * @varargs: NULL-terminated list of more fieldnames to remove
849 * va_list form of gst_structure_remove_fields().
852 gst_structure_remove_fields_valist (GstStructure * structure,
853 const gchar * fieldname, va_list varargs)
855 gchar *field = (gchar *) fieldname;
857 g_return_if_fail (structure != NULL);
858 g_return_if_fail (fieldname != NULL);
859 /* mutability checked in remove_field */
862 gst_structure_remove_field (structure, field);
863 field = va_arg (varargs, char *);
868 * gst_structure_remove_all_fields:
869 * @structure: a #GstStructure
871 * Removes all fields in a GstStructure.
874 gst_structure_remove_all_fields (GstStructure * structure)
876 GstStructureField *field;
879 g_return_if_fail (structure != NULL);
880 g_return_if_fail (IS_MUTABLE (structure));
882 for (i = structure->fields->len - 1; i >= 0; i--) {
883 field = GST_STRUCTURE_FIELD (structure, i);
885 if (G_IS_VALUE (&field->value)) {
886 g_value_unset (&field->value);
888 structure->fields = g_array_remove_index (structure->fields, i);
893 * gst_structure_get_field_type:
894 * @structure: a #GstStructure
895 * @fieldname: the name of the field
897 * Finds the field with the given name, and returns the type of the
898 * value it contains. If the field is not found, G_TYPE_INVALID is
901 * Returns: the #GValue of the field
904 gst_structure_get_field_type (const GstStructure * structure,
905 const gchar * fieldname)
907 GstStructureField *field;
909 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
910 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
912 field = gst_structure_get_field (structure, fieldname);
914 return G_TYPE_INVALID;
916 return G_VALUE_TYPE (&field->value);
920 * gst_structure_n_fields:
921 * @structure: a #GstStructure
923 * Get the number of fields in the structure.
925 * Returns: the number of fields in the structure
928 gst_structure_n_fields (const GstStructure * structure)
930 g_return_val_if_fail (structure != NULL, 0);
932 return structure->fields->len;
936 * gst_structure_nth_field_name:
937 * @structure: a #GstStructure
938 * @index: the index to get the name of
940 * Get the name of the given field number, counting from 0 onwards.
942 * Returns: the name of the given field number
945 gst_structure_nth_field_name (const GstStructure * structure, guint index)
947 GstStructureField *field;
949 g_return_val_if_fail (structure != NULL, NULL);
950 g_return_val_if_fail (index < structure->fields->len, NULL);
952 field = GST_STRUCTURE_FIELD (structure, index);
954 return g_quark_to_string (field->name);
958 * gst_structure_foreach:
959 * @structure: a #GstStructure
960 * @func: a function to call for each field
961 * @user_data: private data
963 * Calls the provided function once for each field in the #GstStructure. The
964 * function must not modify the fields. Also see gst_structure_map_in_place().
966 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
970 gst_structure_foreach (const GstStructure * structure,
971 GstStructureForeachFunc func, gpointer user_data)
974 GstStructureField *field;
977 g_return_val_if_fail (structure != NULL, FALSE);
978 g_return_val_if_fail (func != NULL, FALSE);
980 len = structure->fields->len;
982 for (i = 0; i < len; i++) {
983 field = GST_STRUCTURE_FIELD (structure, i);
985 ret = func (field->name, &field->value, user_data);
986 if (G_UNLIKELY (!ret))
994 * gst_structure_map_in_place:
995 * @structure: a #GstStructure
996 * @func: a function to call for each field
997 * @user_data: private data
999 * Calls the provided function once for each field in the #GstStructure. In
1000 * contrast to gst_structure_foreach(), the function may modify but not delete the
1001 * fields. The structure must be mutable.
1003 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1007 gst_structure_map_in_place (GstStructure * structure,
1008 GstStructureMapFunc func, gpointer user_data)
1011 GstStructureField *field;
1014 g_return_val_if_fail (structure != NULL, FALSE);
1015 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1016 g_return_val_if_fail (func != NULL, FALSE);
1017 len = structure->fields->len;
1019 for (i = 0; i < len; i++) {
1020 field = GST_STRUCTURE_FIELD (structure, i);
1022 ret = func (field->name, &field->value, user_data);
1031 * gst_structure_id_has_field:
1032 * @structure: a #GstStructure
1033 * @field: #GQuark of the field name
1035 * Check if @structure contains a field named @field.
1037 * Returns: TRUE if the structure contains a field with the given name
1042 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1044 GstStructureField *f;
1046 g_return_val_if_fail (structure != NULL, FALSE);
1047 g_return_val_if_fail (field != 0, FALSE);
1049 f = gst_structure_id_get_field (structure, field);
1055 * gst_structure_has_field:
1056 * @structure: a #GstStructure
1057 * @fieldname: the name of a field
1059 * Check if @structure contains a field named @fieldname.
1061 * Returns: TRUE if the structure contains a field with the given name
1064 gst_structure_has_field (const GstStructure * structure,
1065 const gchar * fieldname)
1067 g_return_val_if_fail (structure != NULL, FALSE);
1068 g_return_val_if_fail (fieldname != NULL, FALSE);
1070 return gst_structure_id_has_field (structure,
1071 g_quark_from_string (fieldname));
1075 * gst_structure_id_has_field_typed:
1076 * @structure: a #GstStructure
1077 * @field: #GQuark of the field name
1078 * @type: the type of a value
1080 * Check if @structure contains a field named @field and with GType @type.
1082 * Returns: TRUE if the structure contains a field with the given name and type
1087 gst_structure_id_has_field_typed (const GstStructure * structure,
1088 GQuark field, GType type)
1090 GstStructureField *f;
1092 g_return_val_if_fail (structure != NULL, FALSE);
1093 g_return_val_if_fail (field != 0, FALSE);
1095 f = gst_structure_id_get_field (structure, field);
1099 return (G_VALUE_TYPE (&f->value) == type);
1103 * gst_structure_has_field_typed:
1104 * @structure: a #GstStructure
1105 * @fieldname: the name of a field
1106 * @type: the type of a value
1108 * Check if @structure contains a field named @fieldname and with GType @type.
1110 * Returns: TRUE if the structure contains a field with the given name and type
1113 gst_structure_has_field_typed (const GstStructure * structure,
1114 const gchar * fieldname, GType type)
1116 g_return_val_if_fail (structure != NULL, FALSE);
1117 g_return_val_if_fail (fieldname != NULL, FALSE);
1119 return gst_structure_id_has_field_typed (structure,
1120 g_quark_from_string (fieldname), type);
1123 /* utility functions */
1126 * gst_structure_get_boolean:
1127 * @structure: a #GstStructure
1128 * @fieldname: the name of a field
1129 * @value: a pointer to a #gboolean to set
1131 * Sets the boolean pointed to by @value corresponding to the value of the
1132 * given field. Caller is responsible for making sure the field exists
1133 * and has the correct type.
1135 * Returns: TRUE if the value could be set correctly. If there was no field
1136 * with @fieldname or the existing field did not contain a boolean, this
1137 * function returns FALSE.
1140 gst_structure_get_boolean (const GstStructure * structure,
1141 const gchar * fieldname, gboolean * value)
1143 GstStructureField *field;
1145 g_return_val_if_fail (structure != NULL, FALSE);
1146 g_return_val_if_fail (fieldname != NULL, FALSE);
1148 field = gst_structure_get_field (structure, fieldname);
1152 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1155 *value = gst_g_value_get_boolean_unchecked (&field->value);
1161 * gst_structure_get_int:
1162 * @structure: a #GstStructure
1163 * @fieldname: the name of a field
1164 * @value: a pointer to an int to set
1166 * Sets the int pointed to by @value corresponding to the value of the
1167 * given field. Caller is responsible for making sure the field exists
1168 * and has the correct type.
1170 * Returns: %TRUE if the value could be set correctly. If there was no field
1171 * with @fieldname or the existing field did not contain an int, this function
1175 gst_structure_get_int (const GstStructure * structure,
1176 const gchar * fieldname, gint * value)
1178 GstStructureField *field;
1180 g_return_val_if_fail (structure != NULL, FALSE);
1181 g_return_val_if_fail (fieldname != NULL, FALSE);
1182 g_return_val_if_fail (value != NULL, FALSE);
1184 field = gst_structure_get_field (structure, fieldname);
1188 if (!G_VALUE_HOLDS_INT (&field->value))
1191 *value = gst_g_value_get_int_unchecked (&field->value);
1197 * gst_structure_get_uint:
1198 * @structure: a #GstStructure
1199 * @fieldname: the name of a field
1200 * @value: a pointer to a uint to set
1202 * Sets the uint pointed to by @value corresponding to the value of the
1203 * given field. Caller is responsible for making sure the field exists
1204 * and has the correct type.
1206 * Returns: %TRUE if the value could be set correctly. If there was no field
1207 * with @fieldname or the existing field did not contain a uint, this function
1213 gst_structure_get_uint (const GstStructure * structure,
1214 const gchar * fieldname, guint * value)
1216 GstStructureField *field;
1218 g_return_val_if_fail (structure != NULL, FALSE);
1219 g_return_val_if_fail (fieldname != NULL, FALSE);
1220 g_return_val_if_fail (value != NULL, FALSE);
1222 field = gst_structure_get_field (structure, fieldname);
1226 if (!G_VALUE_HOLDS_UINT (&field->value))
1229 *value = gst_g_value_get_uint_unchecked (&field->value);
1235 * gst_structure_get_fourcc:
1236 * @structure: a #GstStructure
1237 * @fieldname: the name of a field
1238 * @value: a pointer to a 32bit unsigned int to set
1240 * Sets the Fourcc pointed to by @value corresponding to the value of the
1241 * given field. Caller is responsible for making sure the field exists
1242 * and has the correct type.
1244 * Returns: TRUE if the value could be set correctly. If there was no field
1245 * with @fieldname or the existing field did not contain a fourcc, this function
1249 gst_structure_get_fourcc (const GstStructure * structure,
1250 const gchar * fieldname, guint32 * value)
1252 GstStructureField *field;
1254 g_return_val_if_fail (structure != NULL, FALSE);
1255 g_return_val_if_fail (fieldname != NULL, FALSE);
1256 g_return_val_if_fail (value != NULL, FALSE);
1258 field = gst_structure_get_field (structure, fieldname);
1262 if (!GST_VALUE_HOLDS_FOURCC (&field->value))
1265 *value = gst_value_get_fourcc (&field->value);
1271 * gst_structure_get_date:
1272 * @structure: a #GstStructure
1273 * @fieldname: the name of a field
1274 * @value: a pointer to a #GDate to set
1276 * Sets the date pointed to by @value corresponding to the date of the
1277 * given field. Caller is responsible for making sure the field exists
1278 * and has the correct type.
1280 * On success @value will point to a newly-allocated copy of the date which
1281 * should be freed with g_date_free() when no longer needed (note: this is
1282 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1283 * copy of the string).
1285 * Returns: TRUE if the value could be set correctly. If there was no field
1286 * with @fieldname or the existing field did not contain a data, this function
1290 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1293 GstStructureField *field;
1295 g_return_val_if_fail (structure != NULL, FALSE);
1296 g_return_val_if_fail (fieldname != NULL, FALSE);
1297 g_return_val_if_fail (value != NULL, FALSE);
1299 field = gst_structure_get_field (structure, fieldname);
1303 if (!GST_VALUE_HOLDS_DATE (&field->value))
1306 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1307 *value = g_value_dup_boxed (&field->value);
1313 * gst_structure_get_clock_time:
1314 * @structure: a #GstStructure
1315 * @fieldname: the name of a field
1316 * @value: a pointer to a #GstClockTime to set
1318 * Sets the clock time pointed to by @value corresponding to the clock time
1319 * of the given field. Caller is responsible for making sure the field exists
1320 * and has the correct type.
1322 * Returns: TRUE if the value could be set correctly. If there was no field
1323 * with @fieldname or the existing field did not contain a #GstClockTime, this
1324 * function returns FALSE.
1327 gst_structure_get_clock_time (const GstStructure * structure,
1328 const gchar * fieldname, GstClockTime * value)
1330 GstStructureField *field;
1332 g_return_val_if_fail (structure != NULL, FALSE);
1333 g_return_val_if_fail (fieldname != NULL, FALSE);
1334 g_return_val_if_fail (value != NULL, FALSE);
1336 field = gst_structure_get_field (structure, fieldname);
1340 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1343 *value = gst_g_value_get_uint64_unchecked (&field->value);
1349 * gst_structure_get_double:
1350 * @structure: a #GstStructure
1351 * @fieldname: the name of a field
1352 * @value: a pointer to a gdouble to set
1354 * Sets the double pointed to by @value corresponding to the value of the
1355 * given field. Caller is responsible for making sure the field exists
1356 * and has the correct type.
1358 * Returns: TRUE if the value could be set correctly. If there was no field
1359 * with @fieldname or the existing field did not contain a double, this
1360 * function returns FALSE.
1363 gst_structure_get_double (const GstStructure * structure,
1364 const gchar * fieldname, gdouble * value)
1366 GstStructureField *field;
1368 g_return_val_if_fail (structure != NULL, FALSE);
1369 g_return_val_if_fail (fieldname != NULL, FALSE);
1370 g_return_val_if_fail (value != NULL, FALSE);
1372 field = gst_structure_get_field (structure, fieldname);
1376 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1379 *value = gst_g_value_get_double_unchecked (&field->value);
1385 * gst_structure_get_string:
1386 * @structure: a #GstStructure
1387 * @fieldname: the name of a field
1389 * Finds the field corresponding to @fieldname, and returns the string
1390 * contained in the field's value. Caller is responsible for making
1391 * sure the field exists and has the correct type.
1393 * The string should not be modified, and remains valid until the next
1394 * call to a gst_structure_*() function with the given structure.
1396 * Returns: a pointer to the string or NULL when the field did not exist
1397 * or did not contain a string.
1400 gst_structure_get_string (const GstStructure * structure,
1401 const gchar * fieldname)
1403 GstStructureField *field;
1405 g_return_val_if_fail (structure != NULL, NULL);
1406 g_return_val_if_fail (fieldname != NULL, NULL);
1408 field = gst_structure_get_field (structure, fieldname);
1412 if (!G_VALUE_HOLDS_STRING (&field->value))
1415 return gst_g_value_get_string_unchecked (&field->value);
1419 * gst_structure_get_enum:
1420 * @structure: a #GstStructure
1421 * @fieldname: the name of a field
1422 * @enumtype: the enum type of a field
1423 * @value: a pointer to an int to set
1425 * Sets the int pointed to by @value corresponding to the value of the
1426 * given field. Caller is responsible for making sure the field exists,
1427 * has the correct type and that the enumtype is correct.
1429 * Returns: TRUE if the value could be set correctly. If there was no field
1430 * with @fieldname or the existing field did not contain an enum of the given
1431 * type, this function returns FALSE.
1434 gst_structure_get_enum (const GstStructure * structure,
1435 const gchar * fieldname, GType enumtype, gint * value)
1437 GstStructureField *field;
1439 g_return_val_if_fail (structure != NULL, FALSE);
1440 g_return_val_if_fail (fieldname != NULL, FALSE);
1441 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1442 g_return_val_if_fail (value != NULL, FALSE);
1444 field = gst_structure_get_field (structure, fieldname);
1448 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1451 *value = g_value_get_enum (&field->value);
1457 * gst_structure_get_fraction:
1458 * @structure: a #GstStructure
1459 * @fieldname: the name of a field
1460 * @value_numerator: a pointer to an int to set
1461 * @value_denominator: a pointer to an int to set
1463 * Sets the integers pointed to by @value_numerator and @value_denominator
1464 * corresponding to the value of the given field. Caller is responsible
1465 * for making sure the field exists and has the correct type.
1467 * Returns: TRUE if the values could be set correctly. If there was no field
1468 * with @fieldname or the existing field did not contain a GstFraction, this
1469 * function returns FALSE.
1472 gst_structure_get_fraction (const GstStructure * structure,
1473 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1475 GstStructureField *field;
1477 g_return_val_if_fail (structure != NULL, FALSE);
1478 g_return_val_if_fail (fieldname != NULL, FALSE);
1479 g_return_val_if_fail (value_numerator != NULL, FALSE);
1480 g_return_val_if_fail (value_denominator != NULL, FALSE);
1482 field = gst_structure_get_field (structure, fieldname);
1486 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1489 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1490 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1495 typedef struct _GstStructureAbbreviation
1497 const gchar *type_name;
1500 GstStructureAbbreviation;
1502 /* return a copy of an array of GstStructureAbbreviation containing all the
1503 * known type_string, GType maps, including abbreviations for common types */
1504 static GstStructureAbbreviation *
1505 gst_structure_get_abbrs (gint * n_abbrs)
1507 static GstStructureAbbreviation *abbrs = NULL;
1508 static volatile gsize num = 0;
1510 if (g_once_init_enter (&num)) {
1511 /* dynamically generate the array */
1513 GstStructureAbbreviation dyn_abbrs[] = {
1518 {"uint", G_TYPE_UINT}
1522 {"float", G_TYPE_FLOAT}
1526 {"double", G_TYPE_DOUBLE}
1528 {"d", G_TYPE_DOUBLE}
1530 {"buffer", GST_TYPE_BUFFER}
1532 {"fourcc", GST_TYPE_FOURCC}
1534 {"4", GST_TYPE_FOURCC}
1536 {"fraction", GST_TYPE_FRACTION}
1538 {"boolean", G_TYPE_BOOLEAN}
1540 {"bool", G_TYPE_BOOLEAN}
1542 {"b", G_TYPE_BOOLEAN}
1544 {"string", G_TYPE_STRING}
1546 {"str", G_TYPE_STRING}
1548 {"s", G_TYPE_STRING}
1550 {"structure", GST_TYPE_STRUCTURE}
1552 {"datetime", GST_TYPE_DATE_TIME}
1554 _num = G_N_ELEMENTS (dyn_abbrs);
1555 /* permanently allocate and copy the array now */
1556 abbrs = g_new0 (GstStructureAbbreviation, _num);
1557 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1558 g_once_init_leave (&num, _num);
1565 /* given a type_name that could be a type abbreviation or a registered GType,
1566 * return a matching GType */
1568 gst_structure_gtype_from_abbr (const char *type_name)
1571 GstStructureAbbreviation *abbrs;
1574 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1576 abbrs = gst_structure_get_abbrs (&n_abbrs);
1578 for (i = 0; i < n_abbrs; i++) {
1579 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1580 return abbrs[i].type;
1584 /* this is the fallback */
1585 return g_type_from_name (type_name);
1589 gst_structure_to_abbr (GType type)
1592 GstStructureAbbreviation *abbrs;
1595 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1597 abbrs = gst_structure_get_abbrs (&n_abbrs);
1599 for (i = 0; i < n_abbrs; i++) {
1600 if (type == abbrs[i].type) {
1601 return abbrs[i].type_name;
1605 return g_type_name (type);
1609 gst_structure_value_get_generic_type (GValue * val)
1611 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1612 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1613 GArray *array = g_value_peek_pointer (val);
1615 if (array->len > 0) {
1616 GValue *value = &g_array_index (array, GValue, 0);
1618 return gst_structure_value_get_generic_type (value);
1622 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1624 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1625 return G_TYPE_DOUBLE;
1626 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1627 return GST_TYPE_FRACTION;
1629 return G_VALUE_TYPE (val);
1633 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1636 GstStructureField *field;
1639 g_return_val_if_fail (s != NULL, FALSE);
1641 g_string_append (s, g_quark_to_string (structure->name));
1642 len = structure->fields->len;
1643 for (i = 0; i < len; i++) {
1647 field = GST_STRUCTURE_FIELD (structure, i);
1649 t = gst_value_serialize (&field->value);
1650 type = gst_structure_value_get_generic_type (&field->value);
1652 g_string_append_len (s, ", ", 2);
1653 /* FIXME: do we need to escape fieldnames? */
1654 g_string_append (s, g_quark_to_string (field->name));
1655 g_string_append_len (s, "=(", 2);
1656 g_string_append (s, gst_structure_to_abbr (type));
1657 g_string_append_c (s, ')');
1658 g_string_append (s, t == NULL ? "NULL" : t);
1662 g_string_append_c (s, ';');
1667 * gst_structure_to_string:
1668 * @structure: a #GstStructure
1670 * Converts @structure to a human-readable string representation.
1672 * For debugging purposes its easier to do something like this:
1674 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1676 * This prints the structure in human readble form.
1678 * Returns: a pointer to string allocated by g_malloc(). g_free() after
1682 gst_structure_to_string (const GstStructure * structure)
1686 /* NOTE: This function is potentially called by the debug system,
1687 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1688 * should be careful to avoid recursion. This includes any functions
1689 * called by gst_structure_to_string. In particular, calls should
1690 * not use the GST_PTR_FORMAT extension. */
1692 g_return_val_if_fail (structure != NULL, NULL);
1694 /* we estimate a minimum size based on the number of fields in order to
1695 * avoid unnecessary reallocs within GString */
1696 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1697 priv_gst_structure_append_to_gstring (structure, s);
1698 return g_string_free (s, FALSE);
1702 * r will still point to the string. if end == next, the string will not be
1703 * null-terminated. In all other cases it will be.
1704 * end = pointer to char behind end of string, next = pointer to start of
1706 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1709 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1720 ret = gst_structure_parse_simple_string (s, end);
1730 if (G_UNLIKELY (*s == 0))
1732 if (G_UNLIKELY (*s == '\\'))
1740 /* Find the closing quotes */
1743 if (G_UNLIKELY (*s == 0))
1745 if (G_UNLIKELY (*s == '\\'))
1760 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1763 GValue value1 = { 0 };
1764 GValue value2 = { 0 };
1772 ret = gst_structure_parse_value (s, &s, &value1, type);
1776 while (g_ascii_isspace (*s))
1783 while (g_ascii_isspace (*s))
1786 ret = gst_structure_parse_value (s, &s, &value2, type);
1790 while (g_ascii_isspace (*s))
1797 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1800 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1801 range_type = GST_TYPE_DOUBLE_RANGE;
1802 g_value_init (value, range_type);
1803 gst_value_set_double_range (value,
1804 gst_g_value_get_double_unchecked (&value1),
1805 gst_g_value_get_double_unchecked (&value2));
1806 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1807 range_type = GST_TYPE_INT_RANGE;
1808 g_value_init (value, range_type);
1809 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
1810 gst_g_value_get_int_unchecked (&value2));
1811 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1812 range_type = GST_TYPE_FRACTION_RANGE;
1813 g_value_init (value, range_type);
1814 gst_value_set_fraction_range (value, &value1, &value2);
1824 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1825 GType type, GType list_type, char begin, char end)
1827 GValue list_value = { 0 };
1831 g_value_init (value, list_type);
1832 array = g_value_peek_pointer (value);
1838 while (g_ascii_isspace (*s))
1846 ret = gst_structure_parse_value (s, &s, &list_value, type);
1850 g_array_append_val (array, list_value);
1852 while (g_ascii_isspace (*s))
1860 while (g_ascii_isspace (*s))
1863 memset (&list_value, 0, sizeof (list_value));
1864 ret = gst_structure_parse_value (s, &s, &list_value, type);
1868 g_array_append_val (array, list_value);
1869 while (g_ascii_isspace (*s))
1880 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
1882 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
1887 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
1890 return gst_structure_parse_any_list (s, after, value, type,
1891 GST_TYPE_ARRAY, '<', '>');
1895 gst_structure_parse_simple_string (gchar * str, gchar ** end)
1899 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
1909 gst_structure_parse_field (gchar * str,
1910 gchar ** after, GstStructureField * field)
1919 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1922 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
1923 GST_WARNING ("failed to parse simple string, str=%s", str);
1928 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1931 if (G_UNLIKELY (*s != '=')) {
1932 GST_WARNING ("missing assignment operator in the field, str=%s", str);
1939 field->name = g_quark_from_string (name);
1940 GST_DEBUG ("trying field name '%s'", name);
1943 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
1945 GST_WARNING ("failed to parse value %s", str);
1954 gst_structure_parse_value (gchar * str,
1955 gchar ** after, GValue * value, GType default_type)
1964 GType type = default_type;
1967 while (g_ascii_isspace (*s))
1970 /* check if there's a (type_name) 'cast' */
1974 while (g_ascii_isspace (*s))
1977 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
1980 while (g_ascii_isspace (*s))
1982 if (G_UNLIKELY (*s != ')'))
1985 while (g_ascii_isspace (*s))
1990 type = gst_structure_gtype_from_abbr (type_name);
1991 GST_DEBUG ("trying type name '%s'", type_name);
1994 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
1995 GST_WARNING ("invalid type");
2000 while (g_ascii_isspace (*s))
2003 ret = gst_structure_parse_range (s, &s, value, type);
2004 } else if (*s == '{') {
2005 ret = gst_structure_parse_list (s, &s, value, type);
2006 } else if (*s == '<') {
2007 ret = gst_structure_parse_array (s, &s, value, type);
2011 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2013 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2018 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2020 /* Set NULL terminator for deserialization */
2024 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2025 g_value_init (value, try_types[i]);
2026 ret = gst_value_deserialize (value, value_s);
2029 g_value_unset (value);
2032 g_value_init (value, type);
2034 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2035 (type != G_TYPE_STRING))))
2037 /* Set NULL terminator for deserialization */
2041 ret = gst_value_deserialize (value, value_s);
2042 if (G_UNLIKELY (!ret))
2043 g_value_unset (value);
2054 * gst_structure_from_string:
2055 * @string: a string representation of a #GstStructure.
2056 * @end: pointer to store the end of the string in.
2058 * Creates a #GstStructure from a string representation.
2059 * If end is not NULL, a pointer to the place inside the given string
2060 * where parsing ended will be returned.
2062 * Returns: a new #GstStructure or NULL when the string could not
2063 * be parsed. Free with gst_structure_free() after use.
2066 gst_structure_from_string (const gchar * string, gchar ** end)
2073 GstStructure *structure = NULL;
2074 GstStructureField field;
2076 g_return_val_if_fail (string != NULL, NULL);
2078 copy = g_strdup (string);
2081 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2082 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2083 && g_ascii_isspace (r[1]))))
2087 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2088 GST_WARNING ("Failed to parse structure string '%s'", string);
2094 structure = gst_structure_empty_new (name);
2097 if (G_UNLIKELY (structure == NULL))
2101 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2102 && g_ascii_isspace (r[1]))))
2105 /* end of structure, get the next char and finish */
2110 /* accept \0 as end delimiter */
2113 if (G_UNLIKELY (*r != ',')) {
2114 GST_WARNING ("Failed to find delimiter, r=%s", r);
2118 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2119 && g_ascii_isspace (r[1]))))
2122 memset (&field, 0, sizeof (field));
2123 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2124 GST_WARNING ("Failed to parse field, r=%s", r);
2127 gst_structure_set_field (structure, &field);
2131 *end = (char *) string + (r - copy);
2133 g_warning ("gst_structure_from_string did not consume whole string,"
2134 " but caller did not provide end pointer (\"%s\")", string);
2141 gst_structure_free (structure);
2147 gst_structure_transform_to_string (const GValue * src_value,
2148 GValue * dest_value)
2150 g_return_if_fail (src_value != NULL);
2151 g_return_if_fail (dest_value != NULL);
2153 dest_value->data[0].v_pointer =
2154 gst_structure_to_string (src_value->data[0].v_pointer);
2157 static GstStructure *
2158 gst_structure_copy_conditional (const GstStructure * structure)
2161 return gst_structure_copy (structure);
2165 /* fixate utility functions */
2168 * gst_structure_fixate_field_nearest_int:
2169 * @structure: a #GstStructure
2170 * @field_name: a field in @structure
2171 * @target: the target value of the fixation
2173 * Fixates a #GstStructure by changing the given field to the nearest
2174 * integer to @target that is a subset of the existing field.
2176 * Returns: TRUE if the structure could be fixated
2179 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2180 const char *field_name, int target)
2182 const GValue *value;
2184 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2185 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2187 value = gst_structure_get_value (structure, field_name);
2189 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2192 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2195 x = gst_value_get_int_range_min (value);
2198 x = gst_value_get_int_range_max (value);
2201 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2203 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2204 const GValue *list_value;
2207 int best_index = -1;
2209 n = gst_value_list_get_size (value);
2210 for (i = 0; i < n; i++) {
2211 list_value = gst_value_list_get_value (value, i);
2212 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2213 int x = gst_g_value_get_int_unchecked (list_value);
2215 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2221 if (best_index != -1) {
2222 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2232 * gst_structure_fixate_field_nearest_double:
2233 * @structure: a #GstStructure
2234 * @field_name: a field in @structure
2235 * @target: the target value of the fixation
2237 * Fixates a #GstStructure by changing the given field to the nearest
2238 * double to @target that is a subset of the existing field.
2240 * Returns: TRUE if the structure could be fixated
2243 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2244 const char *field_name, double target)
2246 const GValue *value;
2248 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2249 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2251 value = gst_structure_get_value (structure, field_name);
2253 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2256 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2259 x = gst_value_get_double_range_min (value);
2262 x = gst_value_get_double_range_max (value);
2265 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2267 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2268 const GValue *list_value;
2271 int best_index = -1;
2273 n = gst_value_list_get_size (value);
2274 for (i = 0; i < n; i++) {
2275 list_value = gst_value_list_get_value (value, i);
2276 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2277 double x = gst_g_value_get_double_unchecked (list_value);
2279 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2285 if (best_index != -1) {
2286 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2297 * gst_structure_fixate_field_boolean:
2298 * @structure: a #GstStructure
2299 * @field_name: a field in @structure
2300 * @target: the target value of the fixation
2302 * Fixates a #GstStructure by changing the given @field_name field to the given
2303 * @target boolean if that field is not fixed yet.
2305 * Returns: TRUE if the structure could be fixated
2308 gst_structure_fixate_field_boolean (GstStructure * structure,
2309 const char *field_name, gboolean target)
2311 const GValue *value;
2313 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2314 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2316 value = gst_structure_get_value (structure, field_name);
2318 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2321 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2322 const GValue *list_value;
2325 int best_index = -1;
2327 n = gst_value_list_get_size (value);
2328 for (i = 0; i < n; i++) {
2329 list_value = gst_value_list_get_value (value, i);
2330 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2331 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2333 if (best_index == -1 || x == target) {
2339 if (best_index != -1) {
2340 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2350 * gst_structure_fixate_field_string:
2351 * @structure: a #GstStructure
2352 * @field_name: a field in @structure
2353 * @target: the target value of the fixation
2355 * Fixates a #GstStructure by changing the given @field_name field to the given
2356 * @target string if that field is not fixed yet.
2358 * Returns: TRUE if the structure could be fixated
2363 gst_structure_fixate_field_string (GstStructure * structure,
2364 const gchar * field_name, const gchar * target)
2366 const GValue *value;
2368 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2369 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2371 value = gst_structure_get_value (structure, field_name);
2373 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2376 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2377 const GValue *list_value;
2379 const gchar *best = NULL;
2380 int best_index = -1;
2382 n = gst_value_list_get_size (value);
2383 for (i = 0; i < n; i++) {
2384 list_value = gst_value_list_get_value (value, i);
2385 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2386 const gchar *x = g_value_get_string (list_value);
2388 if (best_index == -1 || g_str_equal (x, target)) {
2394 if (best_index != -1) {
2395 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2405 * gst_structure_fixate_field_nearest_fraction:
2406 * @structure: a #GstStructure
2407 * @field_name: a field in @structure
2408 * @target_numerator: The numerator of the target value of the fixation
2409 * @target_denominator: The denominator of the target value of the fixation
2411 * Fixates a #GstStructure by changing the given field to the nearest
2412 * fraction to @target_numerator/@target_denominator that is a subset
2413 * of the existing field.
2415 * Returns: TRUE if the structure could be fixated
2418 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2419 const char *field_name, const gint target_numerator,
2420 const gint target_denominator)
2422 const GValue *value;
2424 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2425 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2427 value = gst_structure_get_value (structure, field_name);
2429 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2432 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2433 const GValue *x, *new_value;
2434 GValue target = { 0 };
2435 g_value_init (&target, GST_TYPE_FRACTION);
2436 gst_value_set_fraction (&target, target_numerator, target_denominator);
2438 new_value = ⌖
2439 x = gst_value_get_fraction_range_min (value);
2440 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2442 x = gst_value_get_fraction_range_max (value);
2443 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2446 gst_structure_set_value (structure, field_name, new_value);
2447 g_value_unset (&target);
2449 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2450 const GValue *list_value;
2452 const GValue *best = NULL;
2455 gdouble best_diff = G_MAXDOUBLE;
2457 target = (gdouble) target_numerator / (gdouble) target_denominator;
2459 GST_DEBUG ("target %g, best %g", target, best_diff);
2463 n = gst_value_list_get_size (value);
2464 for (i = 0; i < n; i++) {
2465 list_value = gst_value_list_get_value (value, i);
2466 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2468 gdouble list_double;
2470 num = gst_value_get_fraction_numerator (list_value);
2471 denom = gst_value_get_fraction_denominator (list_value);
2473 list_double = ((gdouble) num / (gdouble) denom);
2474 cur_diff = target - list_double;
2476 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2479 cur_diff = -cur_diff;
2481 if (!best || cur_diff < best_diff) {
2482 GST_DEBUG ("new best %g", list_double);
2484 best_diff = cur_diff;
2489 gst_structure_set_value (structure, field_name, best);
2497 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2498 * (useful for message parsing functions where the return location is user
2499 * supplied and the user may pass NULL if the value isn't of interest) */
2500 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2502 const GValue *_value = (value); \
2503 guint _flags = (flags); \
2504 GType _value_type = G_VALUE_TYPE (_value); \
2505 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2506 gchar *_lcopy_format = _vtable->lcopy_format; \
2507 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2508 guint _n_values = 0; \
2510 while (*_lcopy_format != '\0') { \
2511 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2512 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2515 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2516 *(__error) = g_strdup_printf ("either all or none of the return " \
2517 "locations for field '%s' need to be NULL", fieldname); \
2518 } else if (_cvalues[0].v_pointer != NULL) { \
2519 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2524 * gst_structure_get_valist:
2525 * @structure: a #GstStructure
2526 * @first_fieldname: the name of the first field to read
2527 * @args: variable arguments
2529 * Parses the variable arguments and reads fields from @structure accordingly.
2530 * valist-variant of gst_structure_get(). Look at the documentation of
2531 * gst_structure_get() for more details.
2533 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2538 gst_structure_get_valist (const GstStructure * structure,
2539 const char *first_fieldname, va_list args)
2541 const char *field_name;
2542 GType expected_type = G_TYPE_INVALID;
2544 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2545 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2547 field_name = first_fieldname;
2548 while (field_name) {
2549 const GValue *val = NULL;
2552 expected_type = va_arg (args, GType);
2554 val = gst_structure_get_value (structure, field_name);
2559 if (G_VALUE_TYPE (val) != expected_type)
2562 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2564 g_warning ("%s: %s", G_STRFUNC, err);
2569 field_name = va_arg (args, const gchar *);
2577 GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2578 field_name, structure);
2583 GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2584 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2585 GST_STR_NULL (g_type_name (expected_type)),
2586 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2593 * gst_structure_id_get_valist:
2594 * @structure: a #GstStructure
2595 * @first_field_id: the quark of the first field to read
2596 * @args: variable arguments
2598 * Parses the variable arguments and reads fields from @structure accordingly.
2599 * valist-variant of gst_structure_id_get(). Look at the documentation of
2600 * gst_structure_id_get() for more details.
2602 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2607 gst_structure_id_get_valist (const GstStructure * structure,
2608 GQuark first_field_id, va_list args)
2611 GType expected_type = G_TYPE_INVALID;
2613 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2614 g_return_val_if_fail (first_field_id != 0, FALSE);
2616 field_id = first_field_id;
2618 const GValue *val = NULL;
2621 expected_type = va_arg (args, GType);
2623 val = gst_structure_id_get_value (structure, field_id);
2628 if (G_VALUE_TYPE (val) != expected_type)
2631 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2633 g_warning ("%s: %s", G_STRFUNC, err);
2638 field_id = va_arg (args, GQuark);
2646 GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2647 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2652 GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2653 "field was of type '%s': %" GST_PTR_FORMAT,
2654 g_quark_to_string (field_id),
2655 GST_STR_NULL (g_type_name (expected_type)),
2656 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2663 * gst_structure_get:
2664 * @structure: a #GstStructure
2665 * @first_fieldname: the name of the first field to read
2666 * @...: variable arguments
2668 * Parses the variable arguments and reads fields from @structure accordingly.
2669 * Variable arguments should be in the form field name, field type
2670 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2671 * The last variable argument should be NULL.
2673 * For refcounted (mini)objects you will acquire your own reference which
2674 * you must release with a suitable _unref() when no longer needed. For
2675 * strings and boxed types you will acquire a copy which you will need to
2676 * release with either g_free() or the suiteable function for the boxed type.
2678 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2679 * because the field requested did not exist, or was of a type other
2680 * than the type specified), otherwise TRUE.
2685 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
2691 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2692 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2694 va_start (args, first_fieldname);
2695 ret = gst_structure_get_valist (structure, first_fieldname, args);
2702 * gst_structure_id_get:
2703 * @structure: a #GstStructure
2704 * @first_field_id: the quark of the first field to read
2705 * @...: variable arguments
2707 * Parses the variable arguments and reads fields from @structure accordingly.
2708 * Variable arguments should be in the form field id quark, field type
2709 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2710 * The last variable argument should be NULL (technically it should be a
2711 * 0 quark, but we require NULL so compilers that support it can check for
2712 * the NULL terminator and warn if it's not there).
2714 * This function is just like gst_structure_get() only that it is slightly
2715 * more efficient since it saves the string-to-quark lookup in the global
2718 * For refcounted (mini)objects you will acquire your own reference which
2719 * you must release with a suitable _unref() when no longer needed. For
2720 * strings and boxed types you will acquire a copy which you will need to
2721 * release with either g_free() or the suiteable function for the boxed type.
2723 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2724 * because the field requested did not exist, or was of a type other
2725 * than the type specified), otherwise TRUE.
2730 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
2736 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2737 g_return_val_if_fail (first_field_id != 0, FALSE);
2739 va_start (args, first_field_id);
2740 ret = gst_structure_id_get_valist (structure, first_field_id, args);