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 * Free-function: gst_structure_free
141 * Returns: (transfer full): a new, empty #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
153 gst_structure_validate_name (const gchar * name)
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",
166 /* FIXME 0.11: don't allow spaces */
167 /* FIXME: test name string more */
169 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+ ", *s) != NULL))
171 if (G_UNLIKELY (*s != '\0')) {
172 GST_WARNING ("Invalid character '%c' at offset %" G_GUINTPTR_FORMAT " in"
173 " structure name: %s", *s, ((guintptr) s - (guintptr) name), name);
182 * gst_structure_empty_new:
183 * @name: name of new structure
185 * Creates a new, empty #GstStructure with the given @name.
187 * See gst_structure_set_name() for constraints on the @name parameter.
189 * Free-function: gst_structure_free
191 * Returns: (transfer full): a new, empty #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);
203 * @name: name of new structure
204 * @firstfield: name of first field to set
205 * @...: additional arguments
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.
212 * Free-function: gst_structure_free
214 * Returns: (transfer full): a new #GstStructure
217 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
219 GstStructure *structure;
222 va_start (varargs, firstfield);
223 structure = gst_structure_new_valist (name, firstfield, varargs);
230 * gst_structure_new_valist:
231 * @name: name of new structure
232 * @firstfield: name of first field to set
233 * @varargs: variable argument list
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().
239 * See gst_structure_set_name() for constraints on the @name parameter.
241 * Free-function: gst_structure_free
243 * Returns: (transfer full): a new #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);
254 gst_structure_set_valist (structure, firstfield, varargs);
260 * gst_structure_set_parent_refcount:
261 * @structure: a #GstStructure
262 * @refcount: (in): a pointer to the parent's refcount
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.
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);
279 g_return_if_fail (refcount != NULL);
281 structure->parent_refcount = refcount;
285 * gst_structure_copy:
286 * @structure: a #GstStructure to duplicate
288 * Duplicates a #GstStructure and all its fields and values.
290 * Free-function: gst_structure_free
292 * Returns: (transfer none): a new #GstStructure.
295 gst_structure_copy (const GstStructure * structure)
297 GstStructure *new_structure;
298 GstStructureField *field;
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);
316 return new_structure;
320 * gst_structure_free:
321 * @structure: (in) (transfer full): the #GstStructure to free
323 * Frees a #GstStructure and all its fields and values. The structure must not
324 * have a parent when this function is called.
327 gst_structure_free (GstStructure * structure)
329 GstStructureField *field;
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);
343 g_array_free (structure->fields, TRUE);
345 memset (structure, 0xff, sizeof (GstStructure));
347 g_slice_free (GstStructure, structure);
351 * gst_structure_get_name:
352 * @structure: a #GstStructure
354 * Get the name of @structure as a string.
356 * Returns: the name of the structure.
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);
367 * gst_structure_has_name:
368 * @structure: a #GstStructure
369 * @name: structure name to check for
371 * Checks if the structure has the given name
373 * Returns: TRUE if @name matches the name of the structure.
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
386 structure_name = g_quark_to_string (structure->name);
388 return (structure_name && strcmp (structure_name, name) == 0);
392 * gst_structure_get_name_id:
393 * @structure: a #GstStructure
395 * Get the name of @structure as a GQuark.
397 * Returns: the quark representing the name of the structure.
400 gst_structure_get_name_id (const GstStructure * structure)
402 g_return_val_if_fail (structure != NULL, 0);
404 return structure->name;
408 * gst_structure_set_name:
409 * @structure: a #GstStructure
410 * @name: the new name of the structure
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 "/-_.:".
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);
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);
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
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.
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);
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
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.
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);
484 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
487 GstStructureField gsfield = { 0, {0,} };
489 gsfield.name = field;
490 gsfield.value = *value;
492 gst_structure_set_field (structure, &gsfield);
494 /* we took ownership */
496 memset (value, 0, sizeof (GValue));
498 value->g_type = G_TYPE_INVALID;
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
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.
515 gst_structure_id_take_value (GstStructure * structure, GQuark field,
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);
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
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.
538 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
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);
551 gst_structure_set_valist_internal (GstStructure * structure,
552 const gchar * fieldname, va_list varargs)
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;
568 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
569 if (G_UNLIKELY (err)) {
570 g_critical ("%s", err);
573 gst_structure_set_field (structure, &field);
575 fieldname = va_arg (varargs, gchar *);
581 * @structure: a #GstStructure
582 * @fieldname: the name of the field to set
583 * @...: variable arguments
585 * Parses the variable arguments and sets fields accordingly.
586 * Variable arguments should be in the form field name, field type
587 * (as a GType), value(s). The last variable argument should be NULL.
590 gst_structure_set (GstStructure * structure, const gchar * field, ...)
594 g_return_if_fail (structure != NULL);
595 g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
597 va_start (varargs, field);
598 gst_structure_set_valist_internal (structure, field, varargs);
603 * gst_structure_set_valist:
604 * @structure: a #GstStructure
605 * @fieldname: the name of the field to set
606 * @varargs: variable arguments
608 * va_list form of gst_structure_set().
611 gst_structure_set_valist (GstStructure * structure,
612 const gchar * fieldname, va_list varargs)
614 g_return_if_fail (structure != NULL);
615 g_return_if_fail (IS_MUTABLE (structure));
617 gst_structure_set_valist_internal (structure, fieldname, varargs);
621 gst_structure_id_set_valist_internal (GstStructure * structure,
622 GQuark fieldname, va_list varargs)
628 GstStructureField field = { 0 };
630 field.name = fieldname;
632 type = va_arg (varargs, GType);
634 if (G_UNLIKELY (type == G_TYPE_DATE)) {
635 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
636 type = GST_TYPE_DATE;
638 #ifndef G_VALUE_COLLECT_INIT
639 g_value_init (&field.value, type);
640 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
642 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
644 if (G_UNLIKELY (err)) {
645 g_critical ("%s", err);
648 gst_structure_set_field (structure, &field);
650 fieldname = va_arg (varargs, GQuark);
655 * gst_structure_id_set:
656 * @structure: a #GstStructure
657 * @fieldname: the GQuark for the name of the field to set
658 * @...: variable arguments
660 * Identical to gst_structure_set, except that field names are
661 * passed using the GQuark for the field name. This allows more efficient
662 * setting of the structure if the caller already knows the associated
664 * The last variable argument must be NULL.
669 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
673 g_return_if_fail (structure != NULL);
675 va_start (varargs, field);
676 gst_structure_id_set_valist_internal (structure, field, varargs);
681 * gst_structure_id_set_valist:
682 * @structure: a #GstStructure
683 * @fieldname: the name of the field to set
684 * @varargs: variable arguments
686 * va_list form of gst_structure_id_set().
691 gst_structure_id_set_valist (GstStructure * structure,
692 GQuark fieldname, va_list varargs)
694 g_return_if_fail (structure != NULL);
695 g_return_if_fail (IS_MUTABLE (structure));
697 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
701 * gst_structure_id_new:
702 * @name_quark: name of new structure
703 * @field_quark: the GQuark for the name of the field to set
704 * @...: variable arguments
706 * Creates a new #GstStructure with the given name as a GQuark, followed by
707 * fieldname quark, GType, argument(s) "triplets" in the same format as
708 * gst_structure_id_set(). Basically a convenience wrapper around
709 * gst_structure_id_empty_new() and gst_structure_id_set().
711 * The last variable argument must be NULL (or 0).
713 * Free-function: gst_structure_free
715 * Returns: (transfer full): a new #GstStructure
720 gst_structure_id_new (GQuark name_quark, GQuark field_quark, ...)
725 g_return_val_if_fail (name_quark != 0, NULL);
726 g_return_val_if_fail (field_quark != 0, NULL);
728 s = gst_structure_id_empty_new (name_quark);
730 va_start (varargs, field_quark);
731 gst_structure_id_set_valist_internal (s, field_quark, varargs);
737 #if GST_VERSION_NANO == 1
738 #define GIT_G_WARNING g_warning
740 #define GIT_G_WARNING GST_WARNING
743 /* If the structure currently contains a field with the same name, it is
744 * replaced with the provided field. Otherwise, the field is added to the
745 * structure. The field's value is not deeply copied.
748 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
750 GstStructureField *f;
751 guint i, len = structure->fields->len;
753 if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
756 s = g_value_get_string (&field->value);
757 /* only check for NULL strings in taglists, as they are allowed in message
758 * structs, e.g. error message debug strings */
759 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
761 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
762 "Please file a bug.", g_quark_to_string (field->name));
763 g_value_unset (&field->value);
766 /* empty strings never make sense */
767 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
768 "Please file a bug.", g_quark_to_string (field->name));
769 g_value_unset (&field->value);
772 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
773 g_warning ("Trying to set string on %s field '%s', but string is not "
774 "valid UTF-8. Please file a bug.",
775 IS_TAGLIST (structure) ? "taglist" : "structure",
776 g_quark_to_string (field->name));
777 g_value_unset (&field->value);
780 } else if (G_UNLIKELY (GST_VALUE_HOLDS_DATE (&field->value))) {
783 d = gst_value_get_date (&field->value);
784 /* only check for NULL GDates in taglists, as they might make sense
785 * in other, generic structs */
786 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
787 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
788 "Please file a bug.", g_quark_to_string (field->name));
789 g_value_unset (&field->value);
791 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
793 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
794 IS_TAGLIST (structure) ? "taglist" : "structure",
795 g_quark_to_string (field->name));
796 g_value_unset (&field->value);
801 for (i = 0; i < len; i++) {
802 f = GST_STRUCTURE_FIELD (structure, i);
804 if (G_UNLIKELY (f->name == field->name)) {
805 g_value_unset (&f->value);
806 memcpy (f, field, sizeof (GstStructureField));
811 g_array_append_val (structure->fields, *field);
814 /* If there is no field with the given ID, NULL is returned.
816 static GstStructureField *
817 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
819 GstStructureField *field;
822 len = structure->fields->len;
824 for (i = 0; i < len; i++) {
825 field = GST_STRUCTURE_FIELD (structure, i);
827 if (G_UNLIKELY (field->name == field_id))
834 /* If there is no field with the given ID, NULL is returned.
836 static GstStructureField *
837 gst_structure_get_field (const GstStructure * structure,
838 const gchar * fieldname)
840 g_return_val_if_fail (structure != NULL, NULL);
841 g_return_val_if_fail (fieldname != NULL, NULL);
843 return gst_structure_id_get_field (structure,
844 g_quark_from_string (fieldname));
848 * gst_structure_get_value:
849 * @structure: a #GstStructure
850 * @fieldname: the name of the field to get
852 * Get the value of the field with name @fieldname.
854 * Returns: the #GValue corresponding to the field with the given name.
857 gst_structure_get_value (const GstStructure * structure,
858 const gchar * fieldname)
860 GstStructureField *field;
862 g_return_val_if_fail (structure != NULL, NULL);
863 g_return_val_if_fail (fieldname != NULL, NULL);
865 field = gst_structure_get_field (structure, fieldname);
869 return &field->value;
873 * gst_structure_id_get_value:
874 * @structure: a #GstStructure
875 * @field: the #GQuark of the field to get
877 * Get the value of the field with GQuark @field.
879 * Returns: the #GValue corresponding to the field with the given name
883 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
885 GstStructureField *gsfield;
887 g_return_val_if_fail (structure != NULL, NULL);
889 gsfield = gst_structure_id_get_field (structure, field);
893 return &gsfield->value;
897 * gst_structure_remove_field:
898 * @structure: a #GstStructure
899 * @fieldname: the name of the field to remove
901 * Removes the field with the given name. If the field with the given
902 * name does not exist, the structure is unchanged.
905 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
907 GstStructureField *field;
911 g_return_if_fail (structure != NULL);
912 g_return_if_fail (fieldname != NULL);
913 g_return_if_fail (IS_MUTABLE (structure));
915 id = g_quark_from_string (fieldname);
916 len = structure->fields->len;
918 for (i = 0; i < len; i++) {
919 field = GST_STRUCTURE_FIELD (structure, i);
921 if (field->name == id) {
922 if (G_IS_VALUE (&field->value)) {
923 g_value_unset (&field->value);
925 structure->fields = g_array_remove_index (structure->fields, i);
932 * gst_structure_remove_fields:
933 * @structure: a #GstStructure
934 * @fieldname: the name of the field to remove
935 * @...: NULL-terminated list of more fieldnames to remove
937 * Removes the fields with the given names. If a field does not exist, the
938 * argument is ignored.
941 gst_structure_remove_fields (GstStructure * structure,
942 const gchar * fieldname, ...)
946 g_return_if_fail (structure != NULL);
947 g_return_if_fail (fieldname != NULL);
948 /* mutability checked in remove_field */
950 va_start (varargs, fieldname);
951 gst_structure_remove_fields_valist (structure, fieldname, varargs);
956 * gst_structure_remove_fields_valist:
957 * @structure: a #GstStructure
958 * @fieldname: the name of the field to remove
959 * @varargs: NULL-terminated list of more fieldnames to remove
961 * va_list form of gst_structure_remove_fields().
964 gst_structure_remove_fields_valist (GstStructure * structure,
965 const gchar * fieldname, va_list varargs)
967 gchar *field = (gchar *) fieldname;
969 g_return_if_fail (structure != NULL);
970 g_return_if_fail (fieldname != NULL);
971 /* mutability checked in remove_field */
974 gst_structure_remove_field (structure, field);
975 field = va_arg (varargs, char *);
980 * gst_structure_remove_all_fields:
981 * @structure: a #GstStructure
983 * Removes all fields in a GstStructure.
986 gst_structure_remove_all_fields (GstStructure * structure)
988 GstStructureField *field;
991 g_return_if_fail (structure != NULL);
992 g_return_if_fail (IS_MUTABLE (structure));
994 for (i = structure->fields->len - 1; i >= 0; i--) {
995 field = GST_STRUCTURE_FIELD (structure, i);
997 if (G_IS_VALUE (&field->value)) {
998 g_value_unset (&field->value);
1000 structure->fields = g_array_remove_index (structure->fields, i);
1005 * gst_structure_get_field_type:
1006 * @structure: a #GstStructure
1007 * @fieldname: the name of the field
1009 * Finds the field with the given name, and returns the type of the
1010 * value it contains. If the field is not found, G_TYPE_INVALID is
1013 * Returns: the #GValue of the field
1016 gst_structure_get_field_type (const GstStructure * structure,
1017 const gchar * fieldname)
1019 GstStructureField *field;
1021 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1022 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1024 field = gst_structure_get_field (structure, fieldname);
1026 return G_TYPE_INVALID;
1028 return G_VALUE_TYPE (&field->value);
1032 * gst_structure_n_fields:
1033 * @structure: a #GstStructure
1035 * Get the number of fields in the structure.
1037 * Returns: the number of fields in the structure
1040 gst_structure_n_fields (const GstStructure * structure)
1042 g_return_val_if_fail (structure != NULL, 0);
1044 return structure->fields->len;
1048 * gst_structure_nth_field_name:
1049 * @structure: a #GstStructure
1050 * @index: the index to get the name of
1052 * Get the name of the given field number, counting from 0 onwards.
1054 * Returns: the name of the given field number
1057 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1059 GstStructureField *field;
1061 g_return_val_if_fail (structure != NULL, NULL);
1062 g_return_val_if_fail (index < structure->fields->len, NULL);
1064 field = GST_STRUCTURE_FIELD (structure, index);
1066 return g_quark_to_string (field->name);
1070 * gst_structure_foreach:
1071 * @structure: a #GstStructure
1072 * @func: (scope call): a function to call for each field
1073 * @user_data: (closure): private data
1075 * Calls the provided function once for each field in the #GstStructure. The
1076 * function must not modify the fields. Also see gst_structure_map_in_place().
1078 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1082 gst_structure_foreach (const GstStructure * structure,
1083 GstStructureForeachFunc func, gpointer user_data)
1086 GstStructureField *field;
1089 g_return_val_if_fail (structure != NULL, FALSE);
1090 g_return_val_if_fail (func != NULL, FALSE);
1092 len = structure->fields->len;
1094 for (i = 0; i < len; i++) {
1095 field = GST_STRUCTURE_FIELD (structure, i);
1097 ret = func (field->name, &field->value, user_data);
1098 if (G_UNLIKELY (!ret))
1106 * gst_structure_map_in_place:
1107 * @structure: a #GstStructure
1108 * @func: (scope call): a function to call for each field
1109 * @user_data: (closure): private data
1111 * Calls the provided function once for each field in the #GstStructure. In
1112 * contrast to gst_structure_foreach(), the function may modify but not delete the
1113 * fields. The structure must be mutable.
1115 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1119 gst_structure_map_in_place (GstStructure * structure,
1120 GstStructureMapFunc func, gpointer user_data)
1123 GstStructureField *field;
1126 g_return_val_if_fail (structure != NULL, FALSE);
1127 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1128 g_return_val_if_fail (func != NULL, FALSE);
1129 len = structure->fields->len;
1131 for (i = 0; i < len; i++) {
1132 field = GST_STRUCTURE_FIELD (structure, i);
1134 ret = func (field->name, &field->value, user_data);
1143 * gst_structure_id_has_field:
1144 * @structure: a #GstStructure
1145 * @field: #GQuark of the field name
1147 * Check if @structure contains a field named @field.
1149 * Returns: TRUE if the structure contains a field with the given name
1154 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1156 GstStructureField *f;
1158 g_return_val_if_fail (structure != NULL, FALSE);
1159 g_return_val_if_fail (field != 0, FALSE);
1161 f = gst_structure_id_get_field (structure, field);
1167 * gst_structure_has_field:
1168 * @structure: a #GstStructure
1169 * @fieldname: the name of a field
1171 * Check if @structure contains a field named @fieldname.
1173 * Returns: TRUE if the structure contains a field with the given name
1176 gst_structure_has_field (const GstStructure * structure,
1177 const gchar * fieldname)
1179 g_return_val_if_fail (structure != NULL, FALSE);
1180 g_return_val_if_fail (fieldname != NULL, FALSE);
1182 return gst_structure_id_has_field (structure,
1183 g_quark_from_string (fieldname));
1187 * gst_structure_id_has_field_typed:
1188 * @structure: a #GstStructure
1189 * @field: #GQuark of the field name
1190 * @type: the type of a value
1192 * Check if @structure contains a field named @field and with GType @type.
1194 * Returns: TRUE if the structure contains a field with the given name and type
1199 gst_structure_id_has_field_typed (const GstStructure * structure,
1200 GQuark field, GType type)
1202 GstStructureField *f;
1204 g_return_val_if_fail (structure != NULL, FALSE);
1205 g_return_val_if_fail (field != 0, FALSE);
1207 f = gst_structure_id_get_field (structure, field);
1211 return (G_VALUE_TYPE (&f->value) == type);
1215 * gst_structure_has_field_typed:
1216 * @structure: a #GstStructure
1217 * @fieldname: the name of a field
1218 * @type: the type of a value
1220 * Check if @structure contains a field named @fieldname and with GType @type.
1222 * Returns: TRUE if the structure contains a field with the given name and type
1225 gst_structure_has_field_typed (const GstStructure * structure,
1226 const gchar * fieldname, GType type)
1228 g_return_val_if_fail (structure != NULL, FALSE);
1229 g_return_val_if_fail (fieldname != NULL, FALSE);
1231 return gst_structure_id_has_field_typed (structure,
1232 g_quark_from_string (fieldname), type);
1235 /* utility functions */
1238 * gst_structure_get_boolean:
1239 * @structure: a #GstStructure
1240 * @fieldname: the name of a field
1241 * @value: (out): a pointer to a #gboolean to set
1243 * Sets the boolean pointed to by @value corresponding to the value of the
1244 * given field. Caller is responsible for making sure the field exists
1245 * and has the correct type.
1247 * Returns: TRUE if the value could be set correctly. If there was no field
1248 * with @fieldname or the existing field did not contain a boolean, this
1249 * function returns FALSE.
1252 gst_structure_get_boolean (const GstStructure * structure,
1253 const gchar * fieldname, gboolean * value)
1255 GstStructureField *field;
1257 g_return_val_if_fail (structure != NULL, FALSE);
1258 g_return_val_if_fail (fieldname != NULL, FALSE);
1260 field = gst_structure_get_field (structure, fieldname);
1264 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1267 *value = gst_g_value_get_boolean_unchecked (&field->value);
1273 * gst_structure_get_int:
1274 * @structure: a #GstStructure
1275 * @fieldname: the name of a field
1276 * @value: (out): a pointer to an int to set
1278 * Sets the int pointed to by @value corresponding to the value of the
1279 * given field. Caller is responsible for making sure the field exists
1280 * and has the correct type.
1282 * Returns: %TRUE if the value could be set correctly. If there was no field
1283 * with @fieldname or the existing field did not contain an int, this function
1287 gst_structure_get_int (const GstStructure * structure,
1288 const gchar * fieldname, gint * value)
1290 GstStructureField *field;
1292 g_return_val_if_fail (structure != NULL, FALSE);
1293 g_return_val_if_fail (fieldname != NULL, FALSE);
1294 g_return_val_if_fail (value != NULL, FALSE);
1296 field = gst_structure_get_field (structure, fieldname);
1300 if (!G_VALUE_HOLDS_INT (&field->value))
1303 *value = gst_g_value_get_int_unchecked (&field->value);
1309 * gst_structure_get_uint:
1310 * @structure: a #GstStructure
1311 * @fieldname: the name of a field
1312 * @value: (out): a pointer to a uint to set
1314 * Sets the uint pointed to by @value corresponding to the value of the
1315 * given field. Caller is responsible for making sure the field exists
1316 * and has the correct type.
1318 * Returns: %TRUE if the value could be set correctly. If there was no field
1319 * with @fieldname or the existing field did not contain a uint, this function
1325 gst_structure_get_uint (const GstStructure * structure,
1326 const gchar * fieldname, guint * value)
1328 GstStructureField *field;
1330 g_return_val_if_fail (structure != NULL, FALSE);
1331 g_return_val_if_fail (fieldname != NULL, FALSE);
1332 g_return_val_if_fail (value != NULL, FALSE);
1334 field = gst_structure_get_field (structure, fieldname);
1338 if (!G_VALUE_HOLDS_UINT (&field->value))
1341 *value = gst_g_value_get_uint_unchecked (&field->value);
1347 * gst_structure_get_fourcc:
1348 * @structure: a #GstStructure
1349 * @fieldname: the name of a field
1350 * @value: (out): a pointer to a 32bit unsigned int to set
1352 * Sets the Fourcc pointed to by @value corresponding to the value of the
1353 * given field. Caller is responsible for making sure the field exists
1354 * and has the correct type.
1356 * Returns: TRUE if the value could be set correctly. If there was no field
1357 * with @fieldname or the existing field did not contain a fourcc, this function
1361 gst_structure_get_fourcc (const GstStructure * structure,
1362 const gchar * fieldname, guint32 * value)
1364 GstStructureField *field;
1366 g_return_val_if_fail (structure != NULL, FALSE);
1367 g_return_val_if_fail (fieldname != NULL, FALSE);
1368 g_return_val_if_fail (value != NULL, FALSE);
1370 field = gst_structure_get_field (structure, fieldname);
1374 if (!GST_VALUE_HOLDS_FOURCC (&field->value))
1377 *value = gst_value_get_fourcc (&field->value);
1383 * gst_structure_get_date:
1384 * @structure: a #GstStructure
1385 * @fieldname: the name of a field
1386 * @value: (out callee-allocates): a pointer to a #GDate to set
1388 * Sets the date pointed to by @value corresponding to the date of the
1389 * given field. Caller is responsible for making sure the field exists
1390 * and has the correct type.
1392 * On success @value will point to a newly-allocated copy of the date which
1393 * should be freed with g_date_free() when no longer needed (note: this is
1394 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1395 * copy of the string).
1397 * Returns: TRUE if the value could be set correctly. If there was no field
1398 * with @fieldname or the existing field did not contain a data, this function
1402 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1405 GstStructureField *field;
1407 g_return_val_if_fail (structure != NULL, FALSE);
1408 g_return_val_if_fail (fieldname != NULL, FALSE);
1409 g_return_val_if_fail (value != NULL, FALSE);
1411 field = gst_structure_get_field (structure, fieldname);
1415 if (!GST_VALUE_HOLDS_DATE (&field->value))
1418 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1419 *value = g_value_dup_boxed (&field->value);
1425 * gst_structure_get_date_time:
1426 * @structure: a #GstStructure
1427 * @fieldname: the name of a field
1428 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1430 * Sets the datetime pointed to by @value corresponding to the datetime of the
1431 * given field. Caller is responsible for making sure the field exists
1432 * and has the correct type.
1434 * On success @value will point to a reference of the datetime which
1435 * should be unreffed with gst_date_time_unref() when no longer needed
1436 * (note: this is inconsistent with e.g. gst_structure_get_string()
1437 * which doesn't return a copy of the string).
1439 * Returns: TRUE if the value could be set correctly. If there was no field
1440 * with @fieldname or the existing field did not contain a data, this function
1446 gst_structure_get_date_time (const GstStructure * structure,
1447 const gchar * fieldname, GstDateTime ** value)
1449 GstStructureField *field;
1451 g_return_val_if_fail (structure != NULL, FALSE);
1452 g_return_val_if_fail (fieldname != NULL, FALSE);
1453 g_return_val_if_fail (value != NULL, FALSE);
1455 field = gst_structure_get_field (structure, fieldname);
1459 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1462 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1463 *value = g_value_dup_boxed (&field->value);
1469 * gst_structure_get_clock_time:
1470 * @structure: a #GstStructure
1471 * @fieldname: the name of a field
1472 * @value: (out): a pointer to a #GstClockTime to set
1474 * Sets the clock time pointed to by @value corresponding to the clock time
1475 * of the given field. Caller is responsible for making sure the field exists
1476 * and has the correct type.
1478 * Returns: TRUE if the value could be set correctly. If there was no field
1479 * with @fieldname or the existing field did not contain a #GstClockTime, this
1480 * function returns FALSE.
1483 gst_structure_get_clock_time (const GstStructure * structure,
1484 const gchar * fieldname, GstClockTime * value)
1486 GstStructureField *field;
1488 g_return_val_if_fail (structure != NULL, FALSE);
1489 g_return_val_if_fail (fieldname != NULL, FALSE);
1490 g_return_val_if_fail (value != NULL, FALSE);
1492 field = gst_structure_get_field (structure, fieldname);
1496 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1499 *value = gst_g_value_get_uint64_unchecked (&field->value);
1505 * gst_structure_get_double:
1506 * @structure: a #GstStructure
1507 * @fieldname: the name of a field
1508 * @value: (out): a pointer to a gdouble to set
1510 * Sets the double pointed to by @value corresponding to the value of the
1511 * given field. Caller is responsible for making sure the field exists
1512 * and has the correct type.
1514 * Returns: TRUE if the value could be set correctly. If there was no field
1515 * with @fieldname or the existing field did not contain a double, this
1516 * function returns FALSE.
1519 gst_structure_get_double (const GstStructure * structure,
1520 const gchar * fieldname, gdouble * value)
1522 GstStructureField *field;
1524 g_return_val_if_fail (structure != NULL, FALSE);
1525 g_return_val_if_fail (fieldname != NULL, FALSE);
1526 g_return_val_if_fail (value != NULL, FALSE);
1528 field = gst_structure_get_field (structure, fieldname);
1532 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1535 *value = gst_g_value_get_double_unchecked (&field->value);
1541 * gst_structure_get_string:
1542 * @structure: a #GstStructure
1543 * @fieldname: the name of a field
1545 * Finds the field corresponding to @fieldname, and returns the string
1546 * contained in the field's value. Caller is responsible for making
1547 * sure the field exists and has the correct type.
1549 * The string should not be modified, and remains valid until the next
1550 * call to a gst_structure_*() function with the given structure.
1552 * Returns: a pointer to the string or NULL when the field did not exist
1553 * or did not contain a string.
1556 gst_structure_get_string (const GstStructure * structure,
1557 const gchar * fieldname)
1559 GstStructureField *field;
1561 g_return_val_if_fail (structure != NULL, NULL);
1562 g_return_val_if_fail (fieldname != NULL, NULL);
1564 field = gst_structure_get_field (structure, fieldname);
1568 if (!G_VALUE_HOLDS_STRING (&field->value))
1571 return gst_g_value_get_string_unchecked (&field->value);
1575 * gst_structure_get_enum:
1576 * @structure: a #GstStructure
1577 * @fieldname: the name of a field
1578 * @enumtype: the enum type of a field
1579 * @value: (out): a pointer to an int to set
1581 * Sets the int pointed to by @value corresponding to the value of the
1582 * given field. Caller is responsible for making sure the field exists,
1583 * has the correct type and that the enumtype is correct.
1585 * Returns: TRUE if the value could be set correctly. If there was no field
1586 * with @fieldname or the existing field did not contain an enum of the given
1587 * type, this function returns FALSE.
1590 gst_structure_get_enum (const GstStructure * structure,
1591 const gchar * fieldname, GType enumtype, gint * value)
1593 GstStructureField *field;
1595 g_return_val_if_fail (structure != NULL, FALSE);
1596 g_return_val_if_fail (fieldname != NULL, FALSE);
1597 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1598 g_return_val_if_fail (value != NULL, FALSE);
1600 field = gst_structure_get_field (structure, fieldname);
1604 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1607 *value = g_value_get_enum (&field->value);
1613 * gst_structure_get_fraction:
1614 * @structure: a #GstStructure
1615 * @fieldname: the name of a field
1616 * @value_numerator: (out): a pointer to an int to set
1617 * @value_denominator: (out): a pointer to an int to set
1619 * Sets the integers pointed to by @value_numerator and @value_denominator
1620 * corresponding to the value of the given field. Caller is responsible
1621 * for making sure the field exists and has the correct type.
1623 * Returns: TRUE if the values could be set correctly. If there was no field
1624 * with @fieldname or the existing field did not contain a GstFraction, this
1625 * function returns FALSE.
1628 gst_structure_get_fraction (const GstStructure * structure,
1629 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1631 GstStructureField *field;
1633 g_return_val_if_fail (structure != NULL, FALSE);
1634 g_return_val_if_fail (fieldname != NULL, FALSE);
1635 g_return_val_if_fail (value_numerator != NULL, FALSE);
1636 g_return_val_if_fail (value_denominator != NULL, FALSE);
1638 field = gst_structure_get_field (structure, fieldname);
1642 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1645 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1646 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1651 typedef struct _GstStructureAbbreviation
1653 const gchar *type_name;
1656 GstStructureAbbreviation;
1658 /* return a copy of an array of GstStructureAbbreviation containing all the
1659 * known type_string, GType maps, including abbreviations for common types */
1660 static GstStructureAbbreviation *
1661 gst_structure_get_abbrs (gint * n_abbrs)
1663 static GstStructureAbbreviation *abbrs = NULL;
1664 static volatile gsize num = 0;
1666 if (g_once_init_enter (&num)) {
1667 /* dynamically generate the array */
1669 GstStructureAbbreviation dyn_abbrs[] = {
1674 {"uint", G_TYPE_UINT}
1678 {"float", G_TYPE_FLOAT}
1682 {"double", G_TYPE_DOUBLE}
1684 {"d", G_TYPE_DOUBLE}
1686 {"buffer", GST_TYPE_BUFFER}
1688 {"fourcc", GST_TYPE_FOURCC}
1690 {"4", GST_TYPE_FOURCC}
1692 {"fraction", GST_TYPE_FRACTION}
1694 {"boolean", G_TYPE_BOOLEAN}
1696 {"bool", G_TYPE_BOOLEAN}
1698 {"b", G_TYPE_BOOLEAN}
1700 {"string", G_TYPE_STRING}
1702 {"str", G_TYPE_STRING}
1704 {"s", G_TYPE_STRING}
1706 {"structure", GST_TYPE_STRUCTURE}
1708 {"date", GST_TYPE_DATE}
1710 {"datetime", GST_TYPE_DATE_TIME}
1712 _num = G_N_ELEMENTS (dyn_abbrs);
1713 /* permanently allocate and copy the array now */
1714 abbrs = g_new0 (GstStructureAbbreviation, _num);
1715 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1716 g_once_init_leave (&num, _num);
1723 /* given a type_name that could be a type abbreviation or a registered GType,
1724 * return a matching GType */
1726 gst_structure_gtype_from_abbr (const char *type_name)
1729 GstStructureAbbreviation *abbrs;
1732 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1734 abbrs = gst_structure_get_abbrs (&n_abbrs);
1736 for (i = 0; i < n_abbrs; i++) {
1737 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1738 return abbrs[i].type;
1742 /* this is the fallback */
1743 return g_type_from_name (type_name);
1747 gst_structure_to_abbr (GType type)
1750 GstStructureAbbreviation *abbrs;
1753 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1755 abbrs = gst_structure_get_abbrs (&n_abbrs);
1757 for (i = 0; i < n_abbrs; i++) {
1758 if (type == abbrs[i].type) {
1759 return abbrs[i].type_name;
1763 return g_type_name (type);
1767 gst_structure_value_get_generic_type (GValue * val)
1769 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1770 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1771 GArray *array = g_value_peek_pointer (val);
1773 if (array->len > 0) {
1774 GValue *value = &g_array_index (array, GValue, 0);
1776 return gst_structure_value_get_generic_type (value);
1780 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1782 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1783 return G_TYPE_INT64;
1784 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1785 return G_TYPE_DOUBLE;
1786 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1787 return GST_TYPE_FRACTION;
1789 return G_VALUE_TYPE (val);
1793 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1796 GstStructureField *field;
1799 g_return_val_if_fail (s != NULL, FALSE);
1801 g_string_append (s, g_quark_to_string (structure->name));
1802 len = structure->fields->len;
1803 for (i = 0; i < len; i++) {
1807 field = GST_STRUCTURE_FIELD (structure, i);
1809 t = gst_value_serialize (&field->value);
1810 type = gst_structure_value_get_generic_type (&field->value);
1812 g_string_append_len (s, ", ", 2);
1813 /* FIXME: do we need to escape fieldnames? */
1814 g_string_append (s, g_quark_to_string (field->name));
1815 g_string_append_len (s, "=(", 2);
1816 g_string_append (s, gst_structure_to_abbr (type));
1817 g_string_append_c (s, ')');
1818 g_string_append (s, t == NULL ? "NULL" : t);
1822 g_string_append_c (s, ';');
1827 * gst_structure_to_string:
1828 * @structure: a #GstStructure
1830 * Converts @structure to a human-readable string representation.
1832 * For debugging purposes its easier to do something like this:
1834 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1836 * This prints the structure in human readble form.
1838 * Free-function: g_free
1840 * Returns: (transfer full)L a pointer to string allocated by g_malloc().
1841 * g_free() after usage.
1844 gst_structure_to_string (const GstStructure * structure)
1848 /* NOTE: This function is potentially called by the debug system,
1849 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1850 * should be careful to avoid recursion. This includes any functions
1851 * called by gst_structure_to_string. In particular, calls should
1852 * not use the GST_PTR_FORMAT extension. */
1854 g_return_val_if_fail (structure != NULL, NULL);
1856 /* we estimate a minimum size based on the number of fields in order to
1857 * avoid unnecessary reallocs within GString */
1858 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1859 priv_gst_structure_append_to_gstring (structure, s);
1860 return g_string_free (s, FALSE);
1864 * r will still point to the string. if end == next, the string will not be
1865 * null-terminated. In all other cases it will be.
1866 * end = pointer to char behind end of string, next = pointer to start of
1868 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1871 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1882 ret = gst_structure_parse_simple_string (s, end);
1892 if (G_UNLIKELY (*s == 0))
1894 if (G_UNLIKELY (*s == '\\'))
1902 /* Find the closing quotes */
1905 if (G_UNLIKELY (*s == 0))
1907 if (G_UNLIKELY (*s == '\\'))
1922 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1925 GValue value1 = { 0 };
1926 GValue value2 = { 0 };
1934 ret = gst_structure_parse_value (s, &s, &value1, type);
1938 while (g_ascii_isspace (*s))
1945 while (g_ascii_isspace (*s))
1948 ret = gst_structure_parse_value (s, &s, &value2, type);
1952 while (g_ascii_isspace (*s))
1959 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1962 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1963 range_type = GST_TYPE_DOUBLE_RANGE;
1964 g_value_init (value, range_type);
1965 gst_value_set_double_range (value,
1966 gst_g_value_get_double_unchecked (&value1),
1967 gst_g_value_get_double_unchecked (&value2));
1968 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1969 range_type = GST_TYPE_INT_RANGE;
1970 g_value_init (value, range_type);
1971 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
1972 gst_g_value_get_int_unchecked (&value2));
1973 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1974 range_type = GST_TYPE_INT64_RANGE;
1975 g_value_init (value, range_type);
1976 gst_value_set_int64_range (value, gst_g_value_get_int64_unchecked (&value1),
1977 gst_g_value_get_int64_unchecked (&value2));
1978 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1979 range_type = GST_TYPE_FRACTION_RANGE;
1980 g_value_init (value, range_type);
1981 gst_value_set_fraction_range (value, &value1, &value2);
1991 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1992 GType type, GType list_type, char begin, char end)
1994 GValue list_value = { 0 };
1998 g_value_init (value, list_type);
1999 array = g_value_peek_pointer (value);
2005 while (g_ascii_isspace (*s))
2013 ret = gst_structure_parse_value (s, &s, &list_value, type);
2017 g_array_append_val (array, list_value);
2019 while (g_ascii_isspace (*s))
2027 while (g_ascii_isspace (*s))
2030 memset (&list_value, 0, sizeof (list_value));
2031 ret = gst_structure_parse_value (s, &s, &list_value, type);
2035 g_array_append_val (array, list_value);
2036 while (g_ascii_isspace (*s))
2047 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2049 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2054 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2057 return gst_structure_parse_any_list (s, after, value, type,
2058 GST_TYPE_ARRAY, '<', '>');
2062 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2066 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2076 gst_structure_parse_field (gchar * str,
2077 gchar ** after, GstStructureField * field)
2086 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2089 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2090 GST_WARNING ("failed to parse simple string, str=%s", str);
2095 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2098 if (G_UNLIKELY (*s != '=')) {
2099 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2106 field->name = g_quark_from_string (name);
2107 GST_DEBUG ("trying field name '%s'", name);
2110 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2112 GST_WARNING ("failed to parse value %s", str);
2121 gst_structure_parse_value (gchar * str,
2122 gchar ** after, GValue * value, GType default_type)
2131 GType type = default_type;
2134 while (g_ascii_isspace (*s))
2137 /* check if there's a (type_name) 'cast' */
2141 while (g_ascii_isspace (*s))
2144 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2147 while (g_ascii_isspace (*s))
2149 if (G_UNLIKELY (*s != ')'))
2152 while (g_ascii_isspace (*s))
2157 type = gst_structure_gtype_from_abbr (type_name);
2158 GST_DEBUG ("trying type name '%s'", type_name);
2161 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2162 GST_WARNING ("invalid type");
2167 while (g_ascii_isspace (*s))
2170 ret = gst_structure_parse_range (s, &s, value, type);
2171 } else if (*s == '{') {
2172 ret = gst_structure_parse_list (s, &s, value, type);
2173 } else if (*s == '<') {
2174 ret = gst_structure_parse_array (s, &s, value, type);
2178 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2180 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2185 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2187 /* Set NULL terminator for deserialization */
2191 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2192 g_value_init (value, try_types[i]);
2193 ret = gst_value_deserialize (value, value_s);
2196 g_value_unset (value);
2199 g_value_init (value, type);
2201 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2202 (type != G_TYPE_STRING))))
2204 /* Set NULL terminator for deserialization */
2208 ret = gst_value_deserialize (value, value_s);
2209 if (G_UNLIKELY (!ret))
2210 g_value_unset (value);
2221 * gst_structure_from_string:
2222 * @string: a string representation of a #GstStructure.
2223 * @end: (out) (allow-none): pointer to store the end of the string in.
2225 * Creates a #GstStructure from a string representation.
2226 * If end is not NULL, a pointer to the place inside the given string
2227 * where parsing ended will be returned.
2229 * Free-function: gst_structure_free
2231 * Returns: (transfer full): a new #GstStructure or NULL when the string could
2232 * not be parsed. Free with gst_structure_free() after use.
2235 gst_structure_from_string (const gchar * string, gchar ** end)
2242 GstStructure *structure = NULL;
2243 GstStructureField field;
2245 g_return_val_if_fail (string != NULL, NULL);
2247 copy = g_strdup (string);
2250 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2251 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2252 && g_ascii_isspace (r[1]))))
2256 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2257 GST_WARNING ("Failed to parse structure string '%s'", string);
2263 structure = gst_structure_empty_new (name);
2266 if (G_UNLIKELY (structure == NULL))
2270 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2271 && g_ascii_isspace (r[1]))))
2274 /* end of structure, get the next char and finish */
2279 /* accept \0 as end delimiter */
2282 if (G_UNLIKELY (*r != ',')) {
2283 GST_WARNING ("Failed to find delimiter, r=%s", r);
2287 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2288 && g_ascii_isspace (r[1]))))
2291 memset (&field, 0, sizeof (field));
2292 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2293 GST_WARNING ("Failed to parse field, r=%s", r);
2296 gst_structure_set_field (structure, &field);
2300 *end = (char *) string + (r - copy);
2302 g_warning ("gst_structure_from_string did not consume whole string,"
2303 " but caller did not provide end pointer (\"%s\")", string);
2310 gst_structure_free (structure);
2316 gst_structure_transform_to_string (const GValue * src_value,
2317 GValue * dest_value)
2319 g_return_if_fail (src_value != NULL);
2320 g_return_if_fail (dest_value != NULL);
2322 dest_value->data[0].v_pointer =
2323 gst_structure_to_string (src_value->data[0].v_pointer);
2326 static GstStructure *
2327 gst_structure_copy_conditional (const GstStructure * structure)
2330 return gst_structure_copy (structure);
2334 /* fixate utility functions */
2337 * gst_structure_fixate_field_nearest_int:
2338 * @structure: a #GstStructure
2339 * @field_name: a field in @structure
2340 * @target: the target value of the fixation
2342 * Fixates a #GstStructure by changing the given field to the nearest
2343 * integer to @target that is a subset of the existing field.
2345 * Returns: TRUE if the structure could be fixated
2348 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2349 const char *field_name, int target)
2351 const GValue *value;
2353 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2354 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2356 value = gst_structure_get_value (structure, field_name);
2358 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2361 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2364 x = gst_value_get_int_range_min (value);
2367 x = gst_value_get_int_range_max (value);
2370 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2372 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2373 const GValue *list_value;
2376 int best_index = -1;
2378 n = gst_value_list_get_size (value);
2379 for (i = 0; i < n; i++) {
2380 list_value = gst_value_list_get_value (value, i);
2381 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2382 int x = gst_g_value_get_int_unchecked (list_value);
2384 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2390 if (best_index != -1) {
2391 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2401 * gst_structure_fixate_field_nearest_double:
2402 * @structure: a #GstStructure
2403 * @field_name: a field in @structure
2404 * @target: the target value of the fixation
2406 * Fixates a #GstStructure by changing the given field to the nearest
2407 * double to @target that is a subset of the existing field.
2409 * Returns: TRUE if the structure could be fixated
2412 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2413 const char *field_name, double target)
2415 const GValue *value;
2417 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2418 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2420 value = gst_structure_get_value (structure, field_name);
2422 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2425 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2428 x = gst_value_get_double_range_min (value);
2431 x = gst_value_get_double_range_max (value);
2434 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2436 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2437 const GValue *list_value;
2440 int best_index = -1;
2442 n = gst_value_list_get_size (value);
2443 for (i = 0; i < n; i++) {
2444 list_value = gst_value_list_get_value (value, i);
2445 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2446 double x = gst_g_value_get_double_unchecked (list_value);
2448 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2454 if (best_index != -1) {
2455 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2466 * gst_structure_fixate_field_boolean:
2467 * @structure: a #GstStructure
2468 * @field_name: a field in @structure
2469 * @target: the target value of the fixation
2471 * Fixates a #GstStructure by changing the given @field_name field to the given
2472 * @target boolean if that field is not fixed yet.
2474 * Returns: TRUE if the structure could be fixated
2477 gst_structure_fixate_field_boolean (GstStructure * structure,
2478 const char *field_name, gboolean target)
2480 const GValue *value;
2482 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2483 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2485 value = gst_structure_get_value (structure, field_name);
2487 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2490 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2491 const GValue *list_value;
2494 int best_index = -1;
2496 n = gst_value_list_get_size (value);
2497 for (i = 0; i < n; i++) {
2498 list_value = gst_value_list_get_value (value, i);
2499 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2500 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2502 if (best_index == -1 || x == target) {
2508 if (best_index != -1) {
2509 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2519 * gst_structure_fixate_field_string:
2520 * @structure: a #GstStructure
2521 * @field_name: a field in @structure
2522 * @target: the target value of the fixation
2524 * Fixates a #GstStructure by changing the given @field_name field to the given
2525 * @target string if that field is not fixed yet.
2527 * Returns: TRUE if the structure could be fixated
2532 gst_structure_fixate_field_string (GstStructure * structure,
2533 const gchar * field_name, const gchar * target)
2535 const GValue *value;
2537 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2538 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2540 value = gst_structure_get_value (structure, field_name);
2542 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2545 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2546 const GValue *list_value;
2548 const gchar *best = NULL;
2549 int best_index = -1;
2551 n = gst_value_list_get_size (value);
2552 for (i = 0; i < n; i++) {
2553 list_value = gst_value_list_get_value (value, i);
2554 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2555 const gchar *x = g_value_get_string (list_value);
2557 if (best_index == -1 || g_str_equal (x, target)) {
2563 if (best_index != -1) {
2564 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2574 * gst_structure_fixate_field_nearest_fraction:
2575 * @structure: a #GstStructure
2576 * @field_name: a field in @structure
2577 * @target_numerator: The numerator of the target value of the fixation
2578 * @target_denominator: The denominator of the target value of the fixation
2580 * Fixates a #GstStructure by changing the given field to the nearest
2581 * fraction to @target_numerator/@target_denominator that is a subset
2582 * of the existing field.
2584 * Returns: TRUE if the structure could be fixated
2587 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2588 const char *field_name, const gint target_numerator,
2589 const gint target_denominator)
2591 const GValue *value;
2593 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2594 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2596 value = gst_structure_get_value (structure, field_name);
2598 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2601 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2602 const GValue *x, *new_value;
2603 GValue target = { 0 };
2604 g_value_init (&target, GST_TYPE_FRACTION);
2605 gst_value_set_fraction (&target, target_numerator, target_denominator);
2607 new_value = ⌖
2608 x = gst_value_get_fraction_range_min (value);
2609 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2611 x = gst_value_get_fraction_range_max (value);
2612 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2615 gst_structure_set_value (structure, field_name, new_value);
2616 g_value_unset (&target);
2618 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2619 const GValue *list_value;
2621 const GValue *best = NULL;
2624 gdouble best_diff = G_MAXDOUBLE;
2626 target = (gdouble) target_numerator / (gdouble) target_denominator;
2628 GST_DEBUG ("target %g, best %g", target, best_diff);
2632 n = gst_value_list_get_size (value);
2633 for (i = 0; i < n; i++) {
2634 list_value = gst_value_list_get_value (value, i);
2635 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2637 gdouble list_double;
2639 num = gst_value_get_fraction_numerator (list_value);
2640 denom = gst_value_get_fraction_denominator (list_value);
2642 list_double = ((gdouble) num / (gdouble) denom);
2643 cur_diff = target - list_double;
2645 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2648 cur_diff = -cur_diff;
2650 if (!best || cur_diff < best_diff) {
2651 GST_DEBUG ("new best %g", list_double);
2653 best_diff = cur_diff;
2658 gst_structure_set_value (structure, field_name, best);
2666 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2667 * (useful for message parsing functions where the return location is user
2668 * supplied and the user may pass NULL if the value isn't of interest) */
2669 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2671 const GValue *_value = (value); \
2672 guint _flags = (flags); \
2673 GType _value_type = G_VALUE_TYPE (_value); \
2674 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2675 gchar *_lcopy_format = _vtable->lcopy_format; \
2676 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2677 guint _n_values = 0; \
2679 while (*_lcopy_format != '\0') { \
2680 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2681 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2684 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2685 *(__error) = g_strdup_printf ("either all or none of the return " \
2686 "locations for field '%s' need to be NULL", fieldname); \
2687 } else if (_cvalues[0].v_pointer != NULL) { \
2688 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2693 * gst_structure_get_valist:
2694 * @structure: a #GstStructure
2695 * @first_fieldname: the name of the first field to read
2696 * @args: variable arguments
2698 * Parses the variable arguments and reads fields from @structure accordingly.
2699 * valist-variant of gst_structure_get(). Look at the documentation of
2700 * gst_structure_get() for more details.
2702 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2707 gst_structure_get_valist (const GstStructure * structure,
2708 const char *first_fieldname, va_list args)
2710 const char *field_name;
2711 GType expected_type = G_TYPE_INVALID;
2713 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2714 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2716 field_name = first_fieldname;
2717 while (field_name) {
2718 const GValue *val = NULL;
2721 expected_type = va_arg (args, GType);
2723 val = gst_structure_get_value (structure, field_name);
2728 if (G_VALUE_TYPE (val) != expected_type)
2731 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2733 g_warning ("%s: %s", G_STRFUNC, err);
2738 field_name = va_arg (args, const gchar *);
2746 GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2747 field_name, structure);
2752 GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2753 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2754 GST_STR_NULL (g_type_name (expected_type)),
2755 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2762 * gst_structure_id_get_valist:
2763 * @structure: a #GstStructure
2764 * @first_field_id: the quark of the first field to read
2765 * @args: variable arguments
2767 * Parses the variable arguments and reads fields from @structure accordingly.
2768 * valist-variant of gst_structure_id_get(). Look at the documentation of
2769 * gst_structure_id_get() for more details.
2771 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2776 gst_structure_id_get_valist (const GstStructure * structure,
2777 GQuark first_field_id, va_list args)
2780 GType expected_type = G_TYPE_INVALID;
2782 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2783 g_return_val_if_fail (first_field_id != 0, FALSE);
2785 field_id = first_field_id;
2787 const GValue *val = NULL;
2790 expected_type = va_arg (args, GType);
2792 val = gst_structure_id_get_value (structure, field_id);
2797 if (G_VALUE_TYPE (val) != expected_type)
2800 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2802 g_warning ("%s: %s", G_STRFUNC, err);
2807 field_id = va_arg (args, GQuark);
2815 GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2816 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2821 GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2822 "field was of type '%s': %" GST_PTR_FORMAT,
2823 g_quark_to_string (field_id),
2824 GST_STR_NULL (g_type_name (expected_type)),
2825 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2832 * gst_structure_get:
2833 * @structure: a #GstStructure
2834 * @first_fieldname: the name of the first field to read
2835 * @...: variable arguments
2837 * Parses the variable arguments and reads fields from @structure accordingly.
2838 * Variable arguments should be in the form field name, field type
2839 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2840 * The last variable argument should be NULL.
2842 * For refcounted (mini)objects you will receive a new reference which
2843 * you must release with a suitable _unref() when no longer needed. For
2844 * strings and boxed types you will receive a copy which you will need to
2845 * release with either g_free() or the suitable function for the boxed type.
2847 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2848 * because the field requested did not exist, or was of a type other
2849 * than the type specified), otherwise TRUE.
2854 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
2860 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2861 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2863 va_start (args, first_fieldname);
2864 ret = gst_structure_get_valist (structure, first_fieldname, args);
2871 * gst_structure_id_get:
2872 * @structure: a #GstStructure
2873 * @first_field_id: the quark of the first field to read
2874 * @...: variable arguments
2876 * Parses the variable arguments and reads fields from @structure accordingly.
2877 * Variable arguments should be in the form field id quark, field type
2878 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2879 * The last variable argument should be NULL (technically it should be a
2880 * 0 quark, but we require NULL so compilers that support it can check for
2881 * the NULL terminator and warn if it's not there).
2883 * This function is just like gst_structure_get() only that it is slightly
2884 * more efficient since it saves the string-to-quark lookup in the global
2887 * For refcounted (mini)objects you will receive a new reference which
2888 * you must release with a suitable _unref() when no longer needed. For
2889 * strings and boxed types you will receive a copy which you will need to
2890 * release with either g_free() or the suitable function for the boxed type.
2892 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2893 * because the field requested did not exist, or was of a type other
2894 * than the type specified), otherwise TRUE.
2899 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
2905 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2906 g_return_val_if_fail (first_field_id != 0, FALSE);
2908 va_start (args, first_field_id);
2909 ret = gst_structure_id_get_valist (structure, first_field_id, args);
2916 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
2919 const GstStructure *struct1 = (const GstStructure *) data;
2920 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
2922 if (G_UNLIKELY (val1 == NULL))
2924 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
2932 * gst_structure_is_equal:
2933 * @structure1: a #GstStructure.
2934 * @structure2: a #GstStructure.
2936 * Tests if the two #GstStructure are equal.
2938 * Returns: TRUE if the two structures have the same name and field.
2943 gst_structure_is_equal (const GstStructure * structure1,
2944 const GstStructure * structure2)
2946 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
2947 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
2949 if (G_UNLIKELY (structure1 == structure2))
2952 if (structure1->name != structure2->name) {
2955 if (structure1->fields->len != structure2->fields->len) {
2959 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
2960 (gpointer) structure2);
2967 const GstStructure *intersect;
2972 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
2974 IntersectData *idata = (IntersectData *) data;
2975 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
2977 if (G_UNLIKELY (val2 == NULL)) {
2978 gst_structure_id_set_value (idata->dest, id, val1);
2980 GValue dest_value = { 0 };
2981 if (gst_value_intersect (&dest_value, val1, val2)) {
2982 gst_structure_id_set_value (idata->dest, id, &dest_value);
2983 g_value_unset (&dest_value);
2992 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
2994 IntersectData *idata = (IntersectData *) data;
2995 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
2997 if (G_UNLIKELY (val2 == NULL)) {
2998 gst_structure_id_set_value (idata->dest, id, val1);
3004 * gst_structure_intersect:
3005 * @struct1: a #GstStructure
3006 * @struct2: a #GstStructure
3008 * Interesects @struct1 and @struct2 and returns the intersection.
3010 * Returns: Intersection of @struct1 and @struct2
3015 gst_structure_intersect (const GstStructure * struct1,
3016 const GstStructure * struct2)
3020 g_assert (struct1 != NULL);
3021 g_assert (struct2 != NULL);
3023 if (G_UNLIKELY (struct1->name != struct2->name))
3026 /* copy fields from struct1 which we have not in struct2 to target
3027 * intersect if we have the field in both */
3028 data.dest = gst_structure_id_empty_new (struct1->name);
3029 data.intersect = struct2;
3030 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3031 gst_structure_intersect_field1, &data)))
3034 /* copy fields from struct2 which we have not in struct1 to target */
3035 data.intersect = struct1;
3036 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3037 gst_structure_intersect_field2, &data)))
3043 gst_structure_free (data.dest);
3048 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3051 GstStructure *other = (GstStructure *) data;
3052 const GValue *val2 = gst_structure_id_get_value (other, id);
3054 if (G_LIKELY (val2)) {
3055 if (!gst_value_can_intersect (val1, val2)) {
3058 gint eq = gst_value_compare (val1, val2);
3060 if (eq == GST_VALUE_UNORDERED) {
3061 /* we need to try interseting */
3062 if (!gst_value_intersect (NULL, val1, val2)) {
3065 } else if (eq != GST_VALUE_EQUAL) {
3074 * gst_structure_can_intersect:
3075 * @struct1: a #GstStructure
3076 * @struct2: a #GstStructure
3078 * Tries intersecting @struct1 and @struct2 and reports whether the result
3079 * would not be empty.
3081 * Returns: %TRUE if intersection would not be empty
3086 gst_structure_can_intersect (const GstStructure * struct1,
3087 const GstStructure * struct2)
3089 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3090 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3092 if (G_UNLIKELY (struct1->name != struct2->name))
3095 /* tries to intersect if we have the field in both */
3096 return gst_structure_foreach ((GstStructure *) struct1,
3097 gst_caps_structure_can_intersect_field, (gpointer) struct2);
3101 gst_caps_structure_is_subset_field (GQuark field_id, const GValue * value,
3104 GstStructure *superset = user_data;
3105 const GValue *other;
3108 if (!(other = gst_structure_id_get_value (superset, field_id)))
3109 /* field is missing in the superset => is subset */
3112 comparison = gst_value_compare (other, value);
3114 /* equal values are subset */
3115 if (comparison == GST_VALUE_EQUAL)
3118 /* ordered, but unequal, values are not */
3119 if (comparison != GST_VALUE_UNORDERED)
3127 * -> 1 - [1,2] = empty
3131 * -> [1,2] - [1,3] = empty
3135 * -> {1,3} - {1,2} = 3
3138 * First caps subtraction needs to return a non-empty set, second
3139 * subtractions needs to give en empty set.
3140 * Both substractions are switched below, as it's faster that way.
3142 if (!gst_value_subtract (NULL, value, other)) {
3143 if (gst_value_subtract (NULL, other, value)) {
3151 * gst_structure_is_subset:
3152 * @subset: a #GstStructure
3153 * @superset: a potentially greater #GstStructure
3155 * Checks if @subset is a subset of @superset, i.e. has the same
3156 * structure name and for all fields that are existing in @superset,
3157 * @subset has a value that is a subset of the value in @superset.
3159 * Returns: %TRUE if @subset is a subset of @superset
3164 gst_structure_is_subset (const GstStructure * subset,
3165 const GstStructure * superset)
3167 if ((superset->name != subset->name) ||
3168 (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3171 return gst_structure_foreach ((GstStructure *) subset,
3172 gst_caps_structure_is_subset_field, (gpointer) superset);