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_new_empty() 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
81 /* owned by parent structure, NULL if no parent */
82 gint *parent_refcount;
87 #define GST_STRUCTURE_REFCOUNT(s) (((GstStructureImpl*)(s))->parent_refcount)
88 #define GST_STRUCTURE_FIELDS(s) (((GstStructureImpl*)(s))->fields)
90 #define GST_STRUCTURE_FIELD(structure, index) \
91 &g_array_index(GST_STRUCTURE_FIELDS(structure), GstStructureField, (index))
93 #define IS_MUTABLE(structure) \
94 (!GST_STRUCTURE_REFCOUNT(structure) || \
95 g_atomic_int_get (GST_STRUCTURE_REFCOUNT(structure)) == 1)
97 #define IS_TAGLIST(structure) \
98 (structure->name == GST_QUARK (TAGLIST))
100 static void gst_structure_set_field (GstStructure * structure,
101 GstStructureField * field);
102 static GstStructureField *gst_structure_get_field (const GstStructure *
103 structure, const gchar * fieldname);
104 static GstStructureField *gst_structure_id_get_field (const GstStructure *
105 structure, GQuark field);
106 static void gst_structure_transform_to_string (const GValue * src_value,
107 GValue * dest_value);
108 static GstStructure *gst_structure_copy_conditional (const GstStructure *
110 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
111 GValue * value, GType default_type);
112 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
114 GType _gst_structure_type = 0;
117 G_DEFINE_BOXED_TYPE (GstStructure, gst_structure,
118 gst_structure_copy_conditional, gst_structure_free);
121 _priv_gst_structure_initialize (void)
123 _gst_structure_type = gst_structure_get_type ();
125 g_value_register_transform_func (_gst_structure_type, G_TYPE_STRING,
126 gst_structure_transform_to_string);
129 static GstStructure *
130 gst_structure_new_id_empty_with_size (GQuark quark, guint prealloc)
132 GstStructureImpl *structure;
134 structure = g_slice_new (GstStructureImpl);
135 ((GstStructure *) structure)->type = _gst_structure_type;
136 ((GstStructure *) structure)->name = quark;
137 GST_STRUCTURE_REFCOUNT (structure) = NULL;
138 GST_STRUCTURE_FIELDS (structure) =
139 g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
141 GST_TRACE ("created structure %p", structure);
143 return GST_STRUCTURE_CAST (structure);
147 * gst_structure_new_id_empty:
148 * @quark: name of new structure
150 * Creates a new, empty #GstStructure with the given name as a GQuark.
152 * Free-function: gst_structure_free
154 * Returns: (transfer full): a new, empty #GstStructure
157 gst_structure_new_id_empty (GQuark quark)
159 g_return_val_if_fail (quark != 0, NULL);
161 return gst_structure_new_id_empty_with_size (quark, 0);
164 #ifndef G_DISABLE_CHECKS
166 gst_structure_validate_name (const gchar * name)
170 g_return_val_if_fail (name != NULL, FALSE);
172 if (G_UNLIKELY (!g_ascii_isalpha (*name))) {
173 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
178 /* FIXME: test name string more */
180 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
182 if (G_UNLIKELY (*s != '\0')) {
183 GST_WARNING ("Invalid character '%c' at offset %" G_GUINTPTR_FORMAT " in"
184 " structure name: %s", *s, ((guintptr) s - (guintptr) name), name);
188 if (strncmp (name, "video/x-raw-", 12) == 0) {
189 g_warning ("0.10-style raw video caps are being created. Should be "
190 "video/x-raw,format=(string).. now.");
191 } else if (strncmp (name, "audio/x-raw-", 12) == 0) {
192 g_warning ("0.10-style raw audio caps are being created. Should be "
193 "audio/x-raw,format=(string).. now.");
201 * gst_structure_new_empty:
202 * @name: name of new structure
204 * Creates a new, empty #GstStructure with the given @name.
206 * See gst_structure_set_name() for constraints on the @name parameter.
208 * Free-function: gst_structure_free
210 * Returns: (transfer full): a new, empty #GstStructure
213 gst_structure_new_empty (const gchar * name)
215 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
217 return gst_structure_new_id_empty_with_size (g_quark_from_string (name), 0);
222 * @name: name of new structure
223 * @firstfield: name of first field to set
224 * @...: additional arguments
226 * Creates a new #GstStructure with the given name. Parses the
227 * list of variable arguments and sets fields to the values listed.
228 * Variable arguments should be passed as field name, field type,
229 * and value. Last variable argument should be NULL.
231 * Free-function: gst_structure_free
233 * Returns: (transfer full): a new #GstStructure
236 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
238 GstStructure *structure;
241 va_start (varargs, firstfield);
242 structure = gst_structure_new_valist (name, firstfield, varargs);
249 * gst_structure_new_valist:
250 * @name: name of new structure
251 * @firstfield: name of first field to set
252 * @varargs: variable argument list
254 * Creates a new #GstStructure with the given @name. Structure fields
255 * are set according to the varargs in a manner similar to
256 * gst_structure_new().
258 * See gst_structure_set_name() for constraints on the @name parameter.
260 * Free-function: gst_structure_free
262 * Returns: (transfer full): a new #GstStructure
265 gst_structure_new_valist (const gchar * name,
266 const gchar * firstfield, va_list varargs)
268 GstStructure *structure;
270 structure = gst_structure_new_empty (name);
273 gst_structure_set_valist (structure, firstfield, varargs);
279 * gst_structure_set_parent_refcount:
280 * @structure: a #GstStructure
281 * @refcount: (in): a pointer to the parent's refcount
283 * Sets the parent_refcount field of #GstStructure. This field is used to
284 * determine whether a structure is mutable or not. This function should only be
285 * called by code implementing parent objects of #GstStructure, as described in
286 * the MT Refcounting section of the design documents.
288 * Returns: %TRUE if the parent refcount could be set.
291 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
293 g_return_val_if_fail (structure != NULL, FALSE);
295 /* if we have a parent_refcount already, we can only clear
296 * if with a NULL refcount */
297 if (GST_STRUCTURE_REFCOUNT (structure)) {
298 if (refcount != NULL) {
299 g_return_val_if_fail (refcount == NULL, FALSE);
303 if (refcount == NULL) {
304 g_return_val_if_fail (refcount != NULL, FALSE);
309 GST_STRUCTURE_REFCOUNT (structure) = refcount;
315 * gst_structure_copy:
316 * @structure: a #GstStructure to duplicate
318 * Duplicates a #GstStructure and all its fields and values.
320 * Free-function: gst_structure_free
322 * Returns: (transfer none): a new #GstStructure.
325 gst_structure_copy (const GstStructure * structure)
327 GstStructure *new_structure;
328 GstStructureField *field;
331 g_return_val_if_fail (structure != NULL, NULL);
333 len = GST_STRUCTURE_FIELDS (structure)->len;
334 new_structure = gst_structure_new_id_empty_with_size (structure->name, len);
336 for (i = 0; i < len; i++) {
337 GstStructureField new_field = { 0 };
339 field = GST_STRUCTURE_FIELD (structure, i);
341 new_field.name = field->name;
342 gst_value_init_and_copy (&new_field.value, &field->value);
343 g_array_append_val (GST_STRUCTURE_FIELDS (new_structure), new_field);
345 GST_CAT_TRACE (GST_CAT_PERFORMANCE, "doing copy %p -> %p",
346 structure, new_structure);
348 return new_structure;
352 * gst_structure_free:
353 * @structure: (in) (transfer full): the #GstStructure to free
355 * Frees a #GstStructure and all its fields and values. The structure must not
356 * have a parent when this function is called.
359 gst_structure_free (GstStructure * structure)
361 GstStructureField *field;
364 g_return_if_fail (structure != NULL);
365 g_return_if_fail (GST_STRUCTURE_REFCOUNT (structure) == NULL);
367 len = GST_STRUCTURE_FIELDS (structure)->len;
368 for (i = 0; i < len; i++) {
369 field = GST_STRUCTURE_FIELD (structure, i);
371 if (G_IS_VALUE (&field->value)) {
372 g_value_unset (&field->value);
375 g_array_free (GST_STRUCTURE_FIELDS (structure), TRUE);
377 memset (structure, 0xff, sizeof (GstStructure));
379 GST_TRACE ("free structure %p", structure);
381 g_slice_free1 (sizeof (GstStructureImpl), structure);
385 * gst_structure_get_name:
386 * @structure: a #GstStructure
388 * Get the name of @structure as a string.
390 * Returns: the name of the structure.
393 gst_structure_get_name (const GstStructure * structure)
395 g_return_val_if_fail (structure != NULL, NULL);
397 return g_quark_to_string (structure->name);
401 * gst_structure_has_name:
402 * @structure: a #GstStructure
403 * @name: structure name to check for
405 * Checks if the structure has the given name
407 * Returns: TRUE if @name matches the name of the structure.
410 gst_structure_has_name (const GstStructure * structure, const gchar * name)
412 const gchar *structure_name;
414 g_return_val_if_fail (structure != NULL, FALSE);
415 g_return_val_if_fail (name != NULL, FALSE);
417 /* getting the string is cheap and comparing short strings is too
418 * should be faster than getting the quark for name and comparing the quarks
420 structure_name = g_quark_to_string (structure->name);
422 return (structure_name && strcmp (structure_name, name) == 0);
426 * gst_structure_get_name_id:
427 * @structure: a #GstStructure
429 * Get the name of @structure as a GQuark.
431 * Returns: the quark representing the name of the structure.
434 gst_structure_get_name_id (const GstStructure * structure)
436 g_return_val_if_fail (structure != NULL, 0);
438 return structure->name;
442 * gst_structure_set_name:
443 * @structure: a #GstStructure
444 * @name: the new name of the structure
446 * Sets the name of the structure to the given @name. The string
447 * provided is copied before being used. It must not be empty, start with a
448 * letter and can be followed by letters, numbers and any of "/-_.:".
451 gst_structure_set_name (GstStructure * structure, const gchar * name)
453 g_return_if_fail (structure != NULL);
454 g_return_if_fail (IS_MUTABLE (structure));
455 g_return_if_fail (gst_structure_validate_name (name));
457 structure->name = g_quark_from_string (name);
461 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
462 const GValue * value)
464 GstStructureField gsfield = { 0, {0,} };
466 gsfield.name = field;
467 gst_value_init_and_copy (&gsfield.value, value);
469 gst_structure_set_field (structure, &gsfield);
473 * gst_structure_id_set_value:
474 * @structure: a #GstStructure
475 * @field: a #GQuark representing a field
476 * @value: the new value of the field
478 * Sets the field with the given GQuark @field to @value. If the field
479 * does not exist, it is created. If the field exists, the previous
480 * value is replaced and freed.
483 gst_structure_id_set_value (GstStructure * structure,
484 GQuark field, const GValue * value)
487 g_return_if_fail (structure != NULL);
488 g_return_if_fail (G_IS_VALUE (value));
489 g_return_if_fail (IS_MUTABLE (structure));
491 gst_structure_id_set_value_internal (structure, field, value);
495 * gst_structure_set_value:
496 * @structure: a #GstStructure
497 * @fieldname: the name of the field to set
498 * @value: the new value of the field
500 * Sets the field with the given name @field to @value. If the field
501 * does not exist, it is created. If the field exists, the previous
502 * value is replaced and freed.
505 gst_structure_set_value (GstStructure * structure,
506 const gchar * fieldname, const GValue * value)
508 g_return_if_fail (structure != NULL);
509 g_return_if_fail (fieldname != NULL);
510 g_return_if_fail (G_IS_VALUE (value));
511 g_return_if_fail (IS_MUTABLE (structure));
513 gst_structure_id_set_value_internal (structure,
514 g_quark_from_string (fieldname), value);
518 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
521 GstStructureField gsfield = { 0, {0,} };
523 gsfield.name = field;
524 gsfield.value = *value;
526 gst_structure_set_field (structure, &gsfield);
528 /* we took ownership */
530 memset (value, 0, sizeof (GValue));
532 value->g_type = G_TYPE_INVALID;
537 * gst_structure_id_take_value:
538 * @structure: a #GstStructure
539 * @field: a #GQuark representing a field
540 * @value: (transfer full): the new value of the field
542 * Sets the field with the given GQuark @field to @value. If the field
543 * does not exist, it is created. If the field exists, the previous
544 * value is replaced and freed.
549 gst_structure_id_take_value (GstStructure * structure, GQuark field,
552 g_return_if_fail (structure != NULL);
553 g_return_if_fail (G_IS_VALUE (value));
554 g_return_if_fail (IS_MUTABLE (structure));
556 gst_structure_id_take_value_internal (structure, field, value);
560 * gst_structure_take_value:
561 * @structure: a #GstStructure
562 * @fieldname: the name of the field to set
563 * @value: (transfer full): the new value of the field
565 * Sets the field with the given name @field to @value. If the field
566 * does not exist, it is created. If the field exists, the previous
567 * value is replaced and freed. The function will take ownership of @value.
572 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
575 g_return_if_fail (structure != NULL);
576 g_return_if_fail (fieldname != NULL);
577 g_return_if_fail (G_IS_VALUE (value));
578 g_return_if_fail (IS_MUTABLE (structure));
580 gst_structure_id_take_value_internal (structure,
581 g_quark_from_string (fieldname), value);
585 gst_structure_set_valist_internal (GstStructure * structure,
586 const gchar * fieldname, va_list varargs)
592 GstStructureField field = { 0 };
594 field.name = g_quark_from_string (fieldname);
596 type = va_arg (varargs, GType);
598 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, gchar *);
611 * @structure: a #GstStructure
612 * @fieldname: the name of the field to set
613 * @...: variable arguments
615 * Parses the variable arguments and sets fields accordingly.
616 * Variable arguments should be in the form field name, field type
617 * (as a GType), value(s). The last variable argument should be NULL.
620 gst_structure_set (GstStructure * structure, const gchar * field, ...)
624 g_return_if_fail (structure != NULL);
625 g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
627 va_start (varargs, field);
628 gst_structure_set_valist_internal (structure, field, varargs);
633 * gst_structure_set_valist:
634 * @structure: a #GstStructure
635 * @fieldname: the name of the field to set
636 * @varargs: variable arguments
638 * va_list form of gst_structure_set().
641 gst_structure_set_valist (GstStructure * structure,
642 const gchar * fieldname, va_list varargs)
644 g_return_if_fail (structure != NULL);
645 g_return_if_fail (IS_MUTABLE (structure));
647 gst_structure_set_valist_internal (structure, fieldname, varargs);
651 gst_structure_id_set_valist_internal (GstStructure * structure,
652 GQuark fieldname, va_list varargs)
658 GstStructureField field = { 0 };
660 field.name = fieldname;
662 type = va_arg (varargs, GType);
664 #ifndef G_VALUE_COLLECT_INIT
665 g_value_init (&field.value, type);
666 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
668 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
670 if (G_UNLIKELY (err)) {
671 g_critical ("%s", err);
674 gst_structure_set_field (structure, &field);
676 fieldname = va_arg (varargs, GQuark);
681 * gst_structure_id_set:
682 * @structure: a #GstStructure
683 * @fieldname: the GQuark for the name of the field to set
684 * @...: variable arguments
686 * Identical to gst_structure_set, except that field names are
687 * passed using the GQuark for the field name. This allows more efficient
688 * setting of the structure if the caller already knows the associated
690 * The last variable argument must be NULL.
695 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
699 g_return_if_fail (structure != NULL);
701 va_start (varargs, field);
702 gst_structure_id_set_valist_internal (structure, field, varargs);
707 * gst_structure_id_set_valist:
708 * @structure: a #GstStructure
709 * @fieldname: the name of the field to set
710 * @varargs: variable arguments
712 * va_list form of gst_structure_id_set().
717 gst_structure_id_set_valist (GstStructure * structure,
718 GQuark fieldname, va_list varargs)
720 g_return_if_fail (structure != NULL);
721 g_return_if_fail (IS_MUTABLE (structure));
723 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
727 * gst_structure_new_id:
728 * @name_quark: name of new structure
729 * @field_quark: the GQuark for the name of the field to set
730 * @...: variable arguments
732 * Creates a new #GstStructure with the given name as a GQuark, followed by
733 * fieldname quark, GType, argument(s) "triplets" in the same format as
734 * gst_structure_id_set(). Basically a convenience wrapper around
735 * gst_structure_new_id_empty() and gst_structure_id_set().
737 * The last variable argument must be NULL (or 0).
739 * Free-function: gst_structure_free
741 * Returns: (transfer full): a new #GstStructure
746 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
751 g_return_val_if_fail (name_quark != 0, NULL);
752 g_return_val_if_fail (field_quark != 0, NULL);
754 s = gst_structure_new_id_empty (name_quark);
756 va_start (varargs, field_quark);
757 gst_structure_id_set_valist_internal (s, field_quark, varargs);
763 #if GST_VERSION_NANO == 1
764 #define GIT_G_WARNING g_warning
766 #define GIT_G_WARNING GST_WARNING
769 /* If the structure currently contains a field with the same name, it is
770 * replaced with the provided field. Otherwise, the field is added to the
771 * structure. The field's value is not deeply copied.
774 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
776 GstStructureField *f;
777 guint i, len = GST_STRUCTURE_FIELDS (structure)->len;
779 if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
782 s = g_value_get_string (&field->value);
783 /* only check for NULL strings in taglists, as they are allowed in message
784 * structs, e.g. error message debug strings */
785 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
787 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
788 "Please file a bug.", g_quark_to_string (field->name));
789 g_value_unset (&field->value);
792 /* empty strings never make sense */
793 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
794 "Please file a bug.", g_quark_to_string (field->name));
795 g_value_unset (&field->value);
798 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
799 g_warning ("Trying to set string on %s field '%s', but string is not "
800 "valid UTF-8. Please file a bug.",
801 IS_TAGLIST (structure) ? "taglist" : "structure",
802 g_quark_to_string (field->name));
803 g_value_unset (&field->value);
806 } else if (G_UNLIKELY (G_VALUE_HOLDS (&field->value, G_TYPE_DATE))) {
809 d = g_value_get_boxed (&field->value);
810 /* only check for NULL GDates in taglists, as they might make sense
811 * in other, generic structs */
812 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
813 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
814 "Please file a bug.", g_quark_to_string (field->name));
815 g_value_unset (&field->value);
817 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
819 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
820 IS_TAGLIST (structure) ? "taglist" : "structure",
821 g_quark_to_string (field->name));
822 g_value_unset (&field->value);
827 for (i = 0; i < len; i++) {
828 f = GST_STRUCTURE_FIELD (structure, i);
830 if (G_UNLIKELY (f->name == field->name)) {
831 g_value_unset (&f->value);
832 memcpy (f, field, sizeof (GstStructureField));
837 g_array_append_val (GST_STRUCTURE_FIELDS (structure), *field);
840 /* If there is no field with the given ID, NULL is returned.
842 static GstStructureField *
843 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
845 GstStructureField *field;
848 len = GST_STRUCTURE_FIELDS (structure)->len;
850 for (i = 0; i < len; i++) {
851 field = GST_STRUCTURE_FIELD (structure, i);
853 if (G_UNLIKELY (field->name == field_id))
860 /* If there is no field with the given ID, NULL is returned.
862 static GstStructureField *
863 gst_structure_get_field (const GstStructure * structure,
864 const gchar * fieldname)
866 g_return_val_if_fail (structure != NULL, NULL);
867 g_return_val_if_fail (fieldname != NULL, NULL);
869 return gst_structure_id_get_field (structure,
870 g_quark_from_string (fieldname));
874 * gst_structure_get_value:
875 * @structure: a #GstStructure
876 * @fieldname: the name of the field to get
878 * Get the value of the field with name @fieldname.
880 * Returns: the #GValue corresponding to the field with the given name.
883 gst_structure_get_value (const GstStructure * structure,
884 const gchar * fieldname)
886 GstStructureField *field;
888 g_return_val_if_fail (structure != NULL, NULL);
889 g_return_val_if_fail (fieldname != NULL, NULL);
891 field = gst_structure_get_field (structure, fieldname);
895 return &field->value;
899 * gst_structure_id_get_value:
900 * @structure: a #GstStructure
901 * @field: the #GQuark of the field to get
903 * Get the value of the field with GQuark @field.
905 * Returns: the #GValue corresponding to the field with the given name
909 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
911 GstStructureField *gsfield;
913 g_return_val_if_fail (structure != NULL, NULL);
915 gsfield = gst_structure_id_get_field (structure, field);
919 return &gsfield->value;
923 * gst_structure_remove_field:
924 * @structure: a #GstStructure
925 * @fieldname: the name of the field to remove
927 * Removes the field with the given name. If the field with the given
928 * name does not exist, the structure is unchanged.
931 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
933 GstStructureField *field;
937 g_return_if_fail (structure != NULL);
938 g_return_if_fail (fieldname != NULL);
939 g_return_if_fail (IS_MUTABLE (structure));
941 id = g_quark_from_string (fieldname);
942 len = GST_STRUCTURE_FIELDS (structure)->len;
944 for (i = 0; i < len; i++) {
945 field = GST_STRUCTURE_FIELD (structure, i);
947 if (field->name == id) {
948 if (G_IS_VALUE (&field->value)) {
949 g_value_unset (&field->value);
951 GST_STRUCTURE_FIELDS (structure) =
952 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
959 * gst_structure_remove_fields:
960 * @structure: a #GstStructure
961 * @fieldname: the name of the field to remove
962 * @...: NULL-terminated list of more fieldnames to remove
964 * Removes the fields with the given names. If a field does not exist, the
965 * argument is ignored.
968 gst_structure_remove_fields (GstStructure * structure,
969 const gchar * fieldname, ...)
973 g_return_if_fail (structure != NULL);
974 g_return_if_fail (fieldname != NULL);
975 /* mutability checked in remove_field */
977 va_start (varargs, fieldname);
978 gst_structure_remove_fields_valist (structure, fieldname, varargs);
983 * gst_structure_remove_fields_valist:
984 * @structure: a #GstStructure
985 * @fieldname: the name of the field to remove
986 * @varargs: NULL-terminated list of more fieldnames to remove
988 * va_list form of gst_structure_remove_fields().
991 gst_structure_remove_fields_valist (GstStructure * structure,
992 const gchar * fieldname, va_list varargs)
994 gchar *field = (gchar *) fieldname;
996 g_return_if_fail (structure != NULL);
997 g_return_if_fail (fieldname != NULL);
998 /* mutability checked in remove_field */
1001 gst_structure_remove_field (structure, field);
1002 field = va_arg (varargs, char *);
1007 * gst_structure_remove_all_fields:
1008 * @structure: a #GstStructure
1010 * Removes all fields in a GstStructure.
1013 gst_structure_remove_all_fields (GstStructure * structure)
1015 GstStructureField *field;
1018 g_return_if_fail (structure != NULL);
1019 g_return_if_fail (IS_MUTABLE (structure));
1021 for (i = GST_STRUCTURE_FIELDS (structure)->len - 1; i >= 0; i--) {
1022 field = GST_STRUCTURE_FIELD (structure, i);
1024 if (G_IS_VALUE (&field->value)) {
1025 g_value_unset (&field->value);
1027 GST_STRUCTURE_FIELDS (structure) =
1028 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1033 * gst_structure_get_field_type:
1034 * @structure: a #GstStructure
1035 * @fieldname: the name of the field
1037 * Finds the field with the given name, and returns the type of the
1038 * value it contains. If the field is not found, G_TYPE_INVALID is
1041 * Returns: the #GValue of the field
1044 gst_structure_get_field_type (const GstStructure * structure,
1045 const gchar * fieldname)
1047 GstStructureField *field;
1049 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1050 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1052 field = gst_structure_get_field (structure, fieldname);
1054 return G_TYPE_INVALID;
1056 return G_VALUE_TYPE (&field->value);
1060 * gst_structure_n_fields:
1061 * @structure: a #GstStructure
1063 * Get the number of fields in the structure.
1065 * Returns: the number of fields in the structure
1068 gst_structure_n_fields (const GstStructure * structure)
1070 g_return_val_if_fail (structure != NULL, 0);
1072 return GST_STRUCTURE_FIELDS (structure)->len;
1076 * gst_structure_nth_field_name:
1077 * @structure: a #GstStructure
1078 * @index: the index to get the name of
1080 * Get the name of the given field number, counting from 0 onwards.
1082 * Returns: the name of the given field number
1085 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1087 GstStructureField *field;
1089 g_return_val_if_fail (structure != NULL, NULL);
1090 g_return_val_if_fail (index < GST_STRUCTURE_FIELDS (structure)->len, NULL);
1092 field = GST_STRUCTURE_FIELD (structure, index);
1094 return g_quark_to_string (field->name);
1098 * gst_structure_foreach:
1099 * @structure: a #GstStructure
1100 * @func: (scope call): a function to call for each field
1101 * @user_data: (closure): private data
1103 * Calls the provided function once for each field in the #GstStructure. The
1104 * function must not modify the fields. Also see gst_structure_map_in_place().
1106 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1110 gst_structure_foreach (const GstStructure * structure,
1111 GstStructureForeachFunc func, gpointer user_data)
1114 GstStructureField *field;
1117 g_return_val_if_fail (structure != NULL, FALSE);
1118 g_return_val_if_fail (func != NULL, FALSE);
1120 len = GST_STRUCTURE_FIELDS (structure)->len;
1122 for (i = 0; i < len; i++) {
1123 field = GST_STRUCTURE_FIELD (structure, i);
1125 ret = func (field->name, &field->value, user_data);
1126 if (G_UNLIKELY (!ret))
1134 * gst_structure_map_in_place:
1135 * @structure: a #GstStructure
1136 * @func: (scope call): a function to call for each field
1137 * @user_data: (closure): private data
1139 * Calls the provided function once for each field in the #GstStructure. In
1140 * contrast to gst_structure_foreach(), the function may modify but not delete the
1141 * fields. The structure must be mutable.
1143 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1147 gst_structure_map_in_place (GstStructure * structure,
1148 GstStructureMapFunc func, gpointer user_data)
1151 GstStructureField *field;
1154 g_return_val_if_fail (structure != NULL, FALSE);
1155 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1156 g_return_val_if_fail (func != NULL, FALSE);
1157 len = GST_STRUCTURE_FIELDS (structure)->len;
1159 for (i = 0; i < len; i++) {
1160 field = GST_STRUCTURE_FIELD (structure, i);
1162 ret = func (field->name, &field->value, user_data);
1171 * gst_structure_id_has_field:
1172 * @structure: a #GstStructure
1173 * @field: #GQuark of the field name
1175 * Check if @structure contains a field named @field.
1177 * Returns: TRUE if the structure contains a field with the given name
1182 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1184 GstStructureField *f;
1186 g_return_val_if_fail (structure != NULL, FALSE);
1187 g_return_val_if_fail (field != 0, FALSE);
1189 f = gst_structure_id_get_field (structure, field);
1195 * gst_structure_has_field:
1196 * @structure: a #GstStructure
1197 * @fieldname: the name of a field
1199 * Check if @structure contains a field named @fieldname.
1201 * Returns: TRUE if the structure contains a field with the given name
1204 gst_structure_has_field (const GstStructure * structure,
1205 const gchar * fieldname)
1207 g_return_val_if_fail (structure != NULL, FALSE);
1208 g_return_val_if_fail (fieldname != NULL, FALSE);
1210 return gst_structure_id_has_field (structure,
1211 g_quark_from_string (fieldname));
1215 * gst_structure_id_has_field_typed:
1216 * @structure: a #GstStructure
1217 * @field: #GQuark of the field name
1218 * @type: the type of a value
1220 * Check if @structure contains a field named @field and with GType @type.
1222 * Returns: TRUE if the structure contains a field with the given name and type
1227 gst_structure_id_has_field_typed (const GstStructure * structure,
1228 GQuark field, GType type)
1230 GstStructureField *f;
1232 g_return_val_if_fail (structure != NULL, FALSE);
1233 g_return_val_if_fail (field != 0, FALSE);
1235 f = gst_structure_id_get_field (structure, field);
1239 return (G_VALUE_TYPE (&f->value) == type);
1243 * gst_structure_has_field_typed:
1244 * @structure: a #GstStructure
1245 * @fieldname: the name of a field
1246 * @type: the type of a value
1248 * Check if @structure contains a field named @fieldname and with GType @type.
1250 * Returns: TRUE if the structure contains a field with the given name and type
1253 gst_structure_has_field_typed (const GstStructure * structure,
1254 const gchar * fieldname, GType type)
1256 g_return_val_if_fail (structure != NULL, FALSE);
1257 g_return_val_if_fail (fieldname != NULL, FALSE);
1259 return gst_structure_id_has_field_typed (structure,
1260 g_quark_from_string (fieldname), type);
1263 /* utility functions */
1266 * gst_structure_get_boolean:
1267 * @structure: a #GstStructure
1268 * @fieldname: the name of a field
1269 * @value: (out): a pointer to a #gboolean to set
1271 * Sets the boolean pointed to by @value corresponding to the value of the
1272 * given field. Caller is responsible for making sure the field exists
1273 * and has the correct type.
1275 * Returns: TRUE if the value could be set correctly. If there was no field
1276 * with @fieldname or the existing field did not contain a boolean, this
1277 * function returns FALSE.
1280 gst_structure_get_boolean (const GstStructure * structure,
1281 const gchar * fieldname, gboolean * value)
1283 GstStructureField *field;
1285 g_return_val_if_fail (structure != NULL, FALSE);
1286 g_return_val_if_fail (fieldname != NULL, FALSE);
1288 field = gst_structure_get_field (structure, fieldname);
1292 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1295 *value = gst_g_value_get_boolean_unchecked (&field->value);
1301 * gst_structure_get_int:
1302 * @structure: a #GstStructure
1303 * @fieldname: the name of a field
1304 * @value: (out): a pointer to an int to set
1306 * Sets the int pointed to by @value corresponding to the value of the
1307 * given field. Caller is responsible for making sure the field exists
1308 * and has the correct type.
1310 * Returns: %TRUE if the value could be set correctly. If there was no field
1311 * with @fieldname or the existing field did not contain an int, this function
1315 gst_structure_get_int (const GstStructure * structure,
1316 const gchar * fieldname, gint * value)
1318 GstStructureField *field;
1320 g_return_val_if_fail (structure != NULL, FALSE);
1321 g_return_val_if_fail (fieldname != NULL, FALSE);
1322 g_return_val_if_fail (value != NULL, FALSE);
1324 field = gst_structure_get_field (structure, fieldname);
1328 if (!G_VALUE_HOLDS_INT (&field->value))
1331 *value = gst_g_value_get_int_unchecked (&field->value);
1337 * gst_structure_get_uint:
1338 * @structure: a #GstStructure
1339 * @fieldname: the name of a field
1340 * @value: (out): a pointer to a uint to set
1342 * Sets the uint pointed to by @value corresponding to the value of the
1343 * given field. Caller is responsible for making sure the field exists
1344 * and has the correct type.
1346 * Returns: %TRUE if the value could be set correctly. If there was no field
1347 * with @fieldname or the existing field did not contain a uint, this function
1353 gst_structure_get_uint (const GstStructure * structure,
1354 const gchar * fieldname, guint * value)
1356 GstStructureField *field;
1358 g_return_val_if_fail (structure != NULL, FALSE);
1359 g_return_val_if_fail (fieldname != NULL, FALSE);
1360 g_return_val_if_fail (value != NULL, FALSE);
1362 field = gst_structure_get_field (structure, fieldname);
1366 if (!G_VALUE_HOLDS_UINT (&field->value))
1369 *value = gst_g_value_get_uint_unchecked (&field->value);
1375 * gst_structure_get_date:
1376 * @structure: a #GstStructure
1377 * @fieldname: the name of a field
1378 * @value: (out callee-allocates): a pointer to a #GDate to set
1380 * Sets the date pointed to by @value corresponding to the date of the
1381 * given field. Caller is responsible for making sure the field exists
1382 * and has the correct type.
1384 * On success @value will point to a newly-allocated copy of the date which
1385 * should be freed with g_date_free() when no longer needed (note: this is
1386 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1387 * copy of the string).
1389 * Returns: TRUE if the value could be set correctly. If there was no field
1390 * with @fieldname or the existing field did not contain a data, this function
1394 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1397 GstStructureField *field;
1399 g_return_val_if_fail (structure != NULL, FALSE);
1400 g_return_val_if_fail (fieldname != NULL, FALSE);
1401 g_return_val_if_fail (value != NULL, FALSE);
1403 field = gst_structure_get_field (structure, fieldname);
1407 if (!G_VALUE_HOLDS (&field->value, G_TYPE_DATE))
1410 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1411 *value = g_value_dup_boxed (&field->value);
1417 * gst_structure_get_date_time:
1418 * @structure: a #GstStructure
1419 * @fieldname: the name of a field
1420 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1422 * Sets the datetime pointed to by @value corresponding to the datetime of the
1423 * given field. Caller is responsible for making sure the field exists
1424 * and has the correct type.
1426 * On success @value will point to a reference of the datetime which
1427 * should be unreffed with gst_date_time_unref() when no longer needed
1428 * (note: this is inconsistent with e.g. gst_structure_get_string()
1429 * which doesn't return a copy of the string).
1431 * Returns: TRUE if the value could be set correctly. If there was no field
1432 * with @fieldname or the existing field did not contain a data, this function
1438 gst_structure_get_date_time (const GstStructure * structure,
1439 const gchar * fieldname, GstDateTime ** value)
1441 GstStructureField *field;
1443 g_return_val_if_fail (structure != NULL, FALSE);
1444 g_return_val_if_fail (fieldname != NULL, FALSE);
1445 g_return_val_if_fail (value != NULL, FALSE);
1447 field = gst_structure_get_field (structure, fieldname);
1451 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1454 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1455 *value = g_value_dup_boxed (&field->value);
1461 * gst_structure_get_clock_time:
1462 * @structure: a #GstStructure
1463 * @fieldname: the name of a field
1464 * @value: (out): a pointer to a #GstClockTime to set
1466 * Sets the clock time pointed to by @value corresponding to the clock time
1467 * of the given field. Caller is responsible for making sure the field exists
1468 * and has the correct type.
1470 * Returns: TRUE if the value could be set correctly. If there was no field
1471 * with @fieldname or the existing field did not contain a #GstClockTime, this
1472 * function returns FALSE.
1475 gst_structure_get_clock_time (const GstStructure * structure,
1476 const gchar * fieldname, GstClockTime * value)
1478 GstStructureField *field;
1480 g_return_val_if_fail (structure != NULL, FALSE);
1481 g_return_val_if_fail (fieldname != NULL, FALSE);
1482 g_return_val_if_fail (value != NULL, FALSE);
1484 field = gst_structure_get_field (structure, fieldname);
1488 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1491 *value = gst_g_value_get_uint64_unchecked (&field->value);
1497 * gst_structure_get_double:
1498 * @structure: a #GstStructure
1499 * @fieldname: the name of a field
1500 * @value: (out): a pointer to a gdouble to set
1502 * Sets the double pointed to by @value corresponding to the value of the
1503 * given field. Caller is responsible for making sure the field exists
1504 * and has the correct type.
1506 * Returns: TRUE if the value could be set correctly. If there was no field
1507 * with @fieldname or the existing field did not contain a double, this
1508 * function returns FALSE.
1511 gst_structure_get_double (const GstStructure * structure,
1512 const gchar * fieldname, gdouble * value)
1514 GstStructureField *field;
1516 g_return_val_if_fail (structure != NULL, FALSE);
1517 g_return_val_if_fail (fieldname != NULL, FALSE);
1518 g_return_val_if_fail (value != NULL, FALSE);
1520 field = gst_structure_get_field (structure, fieldname);
1524 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1527 *value = gst_g_value_get_double_unchecked (&field->value);
1533 * gst_structure_get_string:
1534 * @structure: a #GstStructure
1535 * @fieldname: the name of a field
1537 * Finds the field corresponding to @fieldname, and returns the string
1538 * contained in the field's value. Caller is responsible for making
1539 * sure the field exists and has the correct type.
1541 * The string should not be modified, and remains valid until the next
1542 * call to a gst_structure_*() function with the given structure.
1544 * Returns: a pointer to the string or NULL when the field did not exist
1545 * or did not contain a string.
1548 gst_structure_get_string (const GstStructure * structure,
1549 const gchar * fieldname)
1551 GstStructureField *field;
1553 g_return_val_if_fail (structure != NULL, NULL);
1554 g_return_val_if_fail (fieldname != NULL, NULL);
1556 field = gst_structure_get_field (structure, fieldname);
1560 if (!G_VALUE_HOLDS_STRING (&field->value))
1563 return gst_g_value_get_string_unchecked (&field->value);
1567 * gst_structure_get_enum:
1568 * @structure: a #GstStructure
1569 * @fieldname: the name of a field
1570 * @enumtype: the enum type of a field
1571 * @value: (out): a pointer to an int to set
1573 * Sets the int pointed to by @value corresponding to the value of the
1574 * given field. Caller is responsible for making sure the field exists,
1575 * has the correct type and that the enumtype is correct.
1577 * Returns: TRUE if the value could be set correctly. If there was no field
1578 * with @fieldname or the existing field did not contain an enum of the given
1579 * type, this function returns FALSE.
1582 gst_structure_get_enum (const GstStructure * structure,
1583 const gchar * fieldname, GType enumtype, gint * value)
1585 GstStructureField *field;
1587 g_return_val_if_fail (structure != NULL, FALSE);
1588 g_return_val_if_fail (fieldname != NULL, FALSE);
1589 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1590 g_return_val_if_fail (value != NULL, FALSE);
1592 field = gst_structure_get_field (structure, fieldname);
1596 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1599 *value = g_value_get_enum (&field->value);
1605 * gst_structure_get_fraction:
1606 * @structure: a #GstStructure
1607 * @fieldname: the name of a field
1608 * @value_numerator: (out): a pointer to an int to set
1609 * @value_denominator: (out): a pointer to an int to set
1611 * Sets the integers pointed to by @value_numerator and @value_denominator
1612 * corresponding to the value of the given field. Caller is responsible
1613 * for making sure the field exists and has the correct type.
1615 * Returns: TRUE if the values could be set correctly. If there was no field
1616 * with @fieldname or the existing field did not contain a GstFraction, this
1617 * function returns FALSE.
1620 gst_structure_get_fraction (const GstStructure * structure,
1621 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1623 GstStructureField *field;
1625 g_return_val_if_fail (structure != NULL, FALSE);
1626 g_return_val_if_fail (fieldname != NULL, FALSE);
1627 g_return_val_if_fail (value_numerator != NULL, FALSE);
1628 g_return_val_if_fail (value_denominator != NULL, FALSE);
1630 field = gst_structure_get_field (structure, fieldname);
1634 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1637 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1638 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1643 typedef struct _GstStructureAbbreviation
1645 const gchar *type_name;
1648 GstStructureAbbreviation;
1650 /* return a copy of an array of GstStructureAbbreviation containing all the
1651 * known type_string, GType maps, including abbreviations for common types */
1652 static GstStructureAbbreviation *
1653 gst_structure_get_abbrs (gint * n_abbrs)
1655 static GstStructureAbbreviation *abbrs = NULL;
1656 static volatile gsize num = 0;
1658 if (g_once_init_enter (&num)) {
1659 /* dynamically generate the array */
1661 GstStructureAbbreviation dyn_abbrs[] = {
1666 {"uint", G_TYPE_UINT}
1670 {"float", G_TYPE_FLOAT}
1674 {"double", G_TYPE_DOUBLE}
1676 {"d", G_TYPE_DOUBLE}
1678 {"buffer", GST_TYPE_BUFFER}
1680 {"fraction", GST_TYPE_FRACTION}
1682 {"boolean", G_TYPE_BOOLEAN}
1684 {"bool", G_TYPE_BOOLEAN}
1686 {"b", G_TYPE_BOOLEAN}
1688 {"string", G_TYPE_STRING}
1690 {"str", G_TYPE_STRING}
1692 {"s", G_TYPE_STRING}
1694 {"structure", GST_TYPE_STRUCTURE}
1696 {"date", G_TYPE_DATE}
1698 {"datetime", GST_TYPE_DATE_TIME}
1700 {"bitmask", GST_TYPE_BITMASK}
1702 _num = G_N_ELEMENTS (dyn_abbrs);
1703 /* permanently allocate and copy the array now */
1704 abbrs = g_new0 (GstStructureAbbreviation, _num);
1705 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1706 g_once_init_leave (&num, _num);
1713 /* given a type_name that could be a type abbreviation or a registered GType,
1714 * return a matching GType */
1716 gst_structure_gtype_from_abbr (const char *type_name)
1719 GstStructureAbbreviation *abbrs;
1722 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1724 abbrs = gst_structure_get_abbrs (&n_abbrs);
1726 for (i = 0; i < n_abbrs; i++) {
1727 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1728 return abbrs[i].type;
1732 /* this is the fallback */
1733 return g_type_from_name (type_name);
1737 gst_structure_to_abbr (GType type)
1740 GstStructureAbbreviation *abbrs;
1743 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1745 abbrs = gst_structure_get_abbrs (&n_abbrs);
1747 for (i = 0; i < n_abbrs; i++) {
1748 if (type == abbrs[i].type) {
1749 return abbrs[i].type_name;
1753 return g_type_name (type);
1757 gst_structure_value_get_generic_type (GValue * val)
1759 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1760 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1761 GArray *array = g_value_peek_pointer (val);
1763 if (array->len > 0) {
1764 GValue *value = &g_array_index (array, GValue, 0);
1766 return gst_structure_value_get_generic_type (value);
1770 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1772 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1773 return G_TYPE_INT64;
1774 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1775 return G_TYPE_DOUBLE;
1776 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1777 return GST_TYPE_FRACTION;
1779 return G_VALUE_TYPE (val);
1783 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1786 GstStructureField *field;
1789 g_return_val_if_fail (s != NULL, FALSE);
1791 g_string_append (s, g_quark_to_string (structure->name));
1792 len = GST_STRUCTURE_FIELDS (structure)->len;
1793 for (i = 0; i < len; i++) {
1797 field = GST_STRUCTURE_FIELD (structure, i);
1799 t = gst_value_serialize (&field->value);
1800 type = gst_structure_value_get_generic_type (&field->value);
1802 g_string_append_len (s, ", ", 2);
1803 /* FIXME: do we need to escape fieldnames? */
1804 g_string_append (s, g_quark_to_string (field->name));
1805 g_string_append_len (s, "=(", 2);
1806 g_string_append (s, gst_structure_to_abbr (type));
1807 g_string_append_c (s, ')');
1808 g_string_append (s, t == NULL ? "NULL" : t);
1812 g_string_append_c (s, ';');
1817 * gst_structure_to_string:
1818 * @structure: a #GstStructure
1820 * Converts @structure to a human-readable string representation.
1822 * For debugging purposes its easier to do something like this:
1824 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1826 * This prints the structure in human readble form.
1828 * Free-function: g_free
1830 * Returns: (transfer full)L a pointer to string allocated by g_malloc().
1831 * g_free() after usage.
1834 gst_structure_to_string (const GstStructure * structure)
1838 /* NOTE: This function is potentially called by the debug system,
1839 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1840 * should be careful to avoid recursion. This includes any functions
1841 * called by gst_structure_to_string. In particular, calls should
1842 * not use the GST_PTR_FORMAT extension. */
1844 g_return_val_if_fail (structure != NULL, NULL);
1846 /* we estimate a minimum size based on the number of fields in order to
1847 * avoid unnecessary reallocs within GString */
1848 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1849 priv_gst_structure_append_to_gstring (structure, s);
1850 return g_string_free (s, FALSE);
1854 * r will still point to the string. if end == next, the string will not be
1855 * null-terminated. In all other cases it will be.
1856 * end = pointer to char behind end of string, next = pointer to start of
1858 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1861 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1872 ret = gst_structure_parse_simple_string (s, end);
1882 if (G_UNLIKELY (*s == 0))
1884 if (G_UNLIKELY (*s == '\\'))
1892 /* Find the closing quotes */
1895 if (G_UNLIKELY (*s == 0))
1897 if (G_UNLIKELY (*s == '\\'))
1912 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1915 GValue value1 = { 0 };
1916 GValue value2 = { 0 };
1917 GValue value3 = { 0 };
1919 gboolean ret, have_step = FALSE;
1925 ret = gst_structure_parse_value (s, &s, &value1, type);
1929 while (g_ascii_isspace (*s))
1936 while (g_ascii_isspace (*s))
1939 ret = gst_structure_parse_value (s, &s, &value2, type);
1943 while (g_ascii_isspace (*s))
1946 /* optional step for int and int64 */
1947 if (G_VALUE_TYPE (&value1) == G_TYPE_INT
1948 || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1952 while (g_ascii_isspace (*s))
1955 ret = gst_structure_parse_value (s, &s, &value3, type);
1959 while (g_ascii_isspace (*s))
1970 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1972 if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
1975 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1976 range_type = GST_TYPE_DOUBLE_RANGE;
1977 g_value_init (value, range_type);
1978 gst_value_set_double_range (value,
1979 gst_g_value_get_double_unchecked (&value1),
1980 gst_g_value_get_double_unchecked (&value2));
1981 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1982 range_type = GST_TYPE_INT_RANGE;
1983 g_value_init (value, range_type);
1985 gst_value_set_int_range_step (value,
1986 gst_g_value_get_int_unchecked (&value1),
1987 gst_g_value_get_int_unchecked (&value2),
1988 gst_g_value_get_int_unchecked (&value3));
1990 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
1991 gst_g_value_get_int_unchecked (&value2));
1992 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1993 range_type = GST_TYPE_INT64_RANGE;
1994 g_value_init (value, range_type);
1996 gst_value_set_int64_range_step (value,
1997 gst_g_value_get_int64_unchecked (&value1),
1998 gst_g_value_get_int64_unchecked (&value2),
1999 gst_g_value_get_int64_unchecked (&value3));
2001 gst_value_set_int64_range (value,
2002 gst_g_value_get_int64_unchecked (&value1),
2003 gst_g_value_get_int64_unchecked (&value2));
2004 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
2005 range_type = GST_TYPE_FRACTION_RANGE;
2006 g_value_init (value, range_type);
2007 gst_value_set_fraction_range (value, &value1, &value2);
2017 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
2018 GType type, GType list_type, char begin, char end)
2020 GValue list_value = { 0 };
2024 g_value_init (value, list_type);
2025 array = g_value_peek_pointer (value);
2031 while (g_ascii_isspace (*s))
2039 ret = gst_structure_parse_value (s, &s, &list_value, type);
2043 g_array_append_val (array, list_value);
2045 while (g_ascii_isspace (*s))
2053 while (g_ascii_isspace (*s))
2056 memset (&list_value, 0, sizeof (list_value));
2057 ret = gst_structure_parse_value (s, &s, &list_value, type);
2061 g_array_append_val (array, list_value);
2062 while (g_ascii_isspace (*s))
2073 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2075 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2080 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2083 return gst_structure_parse_any_list (s, after, value, type,
2084 GST_TYPE_ARRAY, '<', '>');
2088 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2092 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2102 gst_structure_parse_field (gchar * str,
2103 gchar ** after, GstStructureField * field)
2112 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2115 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2116 GST_WARNING ("failed to parse simple string, str=%s", str);
2121 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2124 if (G_UNLIKELY (*s != '=')) {
2125 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2132 field->name = g_quark_from_string (name);
2133 GST_DEBUG ("trying field name '%s'", name);
2136 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2138 GST_WARNING ("failed to parse value %s", str);
2147 gst_structure_parse_value (gchar * str,
2148 gchar ** after, GValue * value, GType default_type)
2157 GType type = default_type;
2160 while (g_ascii_isspace (*s))
2163 /* check if there's a (type_name) 'cast' */
2167 while (g_ascii_isspace (*s))
2170 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2173 while (g_ascii_isspace (*s))
2175 if (G_UNLIKELY (*s != ')'))
2178 while (g_ascii_isspace (*s))
2183 type = gst_structure_gtype_from_abbr (type_name);
2184 GST_DEBUG ("trying type name '%s'", type_name);
2187 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2188 GST_WARNING ("invalid type");
2193 while (g_ascii_isspace (*s))
2196 ret = gst_structure_parse_range (s, &s, value, type);
2197 } else if (*s == '{') {
2198 ret = gst_structure_parse_list (s, &s, value, type);
2199 } else if (*s == '<') {
2200 ret = gst_structure_parse_array (s, &s, value, type);
2204 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2206 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2211 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2213 /* Set NULL terminator for deserialization */
2217 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2218 g_value_init (value, try_types[i]);
2219 ret = gst_value_deserialize (value, value_s);
2222 g_value_unset (value);
2225 g_value_init (value, type);
2227 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2228 (type != G_TYPE_STRING))))
2230 /* Set NULL terminator for deserialization */
2234 ret = gst_value_deserialize (value, value_s);
2235 if (G_UNLIKELY (!ret))
2236 g_value_unset (value);
2247 * gst_structure_from_string:
2248 * @string: a string representation of a #GstStructure.
2249 * @end: (out) (allow-none) (transfer none): pointer to store the end of the string in.
2251 * Creates a #GstStructure from a string representation.
2252 * If end is not NULL, a pointer to the place inside the given string
2253 * where parsing ended will be returned.
2255 * Free-function: gst_structure_free
2257 * Returns: (transfer full): a new #GstStructure or NULL when the string could
2258 * not be parsed. Free with gst_structure_free() after use.
2261 gst_structure_from_string (const gchar * string, gchar ** end)
2268 GstStructure *structure = NULL;
2269 GstStructureField field;
2271 g_return_val_if_fail (string != NULL, NULL);
2273 copy = g_strdup (string);
2276 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2277 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2278 && g_ascii_isspace (r[1]))))
2282 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2283 GST_WARNING ("Failed to parse structure string '%s'", string);
2289 structure = gst_structure_new_empty (name);
2292 if (G_UNLIKELY (structure == NULL))
2296 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2297 && g_ascii_isspace (r[1]))))
2300 /* end of structure, get the next char and finish */
2305 /* accept \0 as end delimiter */
2308 if (G_UNLIKELY (*r != ',')) {
2309 GST_WARNING ("Failed to find delimiter, r=%s", r);
2313 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2314 && g_ascii_isspace (r[1]))))
2317 memset (&field, 0, sizeof (field));
2318 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2319 GST_WARNING ("Failed to parse field, r=%s", r);
2322 gst_structure_set_field (structure, &field);
2326 *end = (char *) string + (r - copy);
2328 g_warning ("gst_structure_from_string did not consume whole string,"
2329 " but caller did not provide end pointer (\"%s\")", string);
2336 gst_structure_free (structure);
2342 gst_structure_transform_to_string (const GValue * src_value,
2343 GValue * dest_value)
2345 g_return_if_fail (src_value != NULL);
2346 g_return_if_fail (dest_value != NULL);
2348 dest_value->data[0].v_pointer =
2349 gst_structure_to_string (src_value->data[0].v_pointer);
2352 static GstStructure *
2353 gst_structure_copy_conditional (const GstStructure * structure)
2356 return gst_structure_copy (structure);
2360 /* fixate utility functions */
2363 * gst_structure_fixate_field_nearest_int:
2364 * @structure: a #GstStructure
2365 * @field_name: a field in @structure
2366 * @target: the target value of the fixation
2368 * Fixates a #GstStructure by changing the given field to the nearest
2369 * integer to @target that is a subset of the existing field.
2371 * Returns: TRUE if the structure could be fixated
2374 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2375 const char *field_name, int target)
2377 const GValue *value;
2379 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2380 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2382 value = gst_structure_get_value (structure, field_name);
2384 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2387 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2390 x = gst_value_get_int_range_min (value);
2393 x = gst_value_get_int_range_max (value);
2396 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2398 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2399 const GValue *list_value;
2402 int best_index = -1;
2404 n = gst_value_list_get_size (value);
2405 for (i = 0; i < n; i++) {
2406 list_value = gst_value_list_get_value (value, i);
2407 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2408 int x = gst_g_value_get_int_unchecked (list_value);
2410 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2416 if (best_index != -1) {
2417 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2427 * gst_structure_fixate_field_nearest_double:
2428 * @structure: a #GstStructure
2429 * @field_name: a field in @structure
2430 * @target: the target value of the fixation
2432 * Fixates a #GstStructure by changing the given field to the nearest
2433 * double to @target that is a subset of the existing field.
2435 * Returns: TRUE if the structure could be fixated
2438 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2439 const char *field_name, double target)
2441 const GValue *value;
2443 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2444 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2446 value = gst_structure_get_value (structure, field_name);
2448 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2451 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2454 x = gst_value_get_double_range_min (value);
2457 x = gst_value_get_double_range_max (value);
2460 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2462 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2463 const GValue *list_value;
2466 int best_index = -1;
2468 n = gst_value_list_get_size (value);
2469 for (i = 0; i < n; i++) {
2470 list_value = gst_value_list_get_value (value, i);
2471 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2472 double x = gst_g_value_get_double_unchecked (list_value);
2474 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2480 if (best_index != -1) {
2481 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2492 * gst_structure_fixate_field_boolean:
2493 * @structure: a #GstStructure
2494 * @field_name: a field in @structure
2495 * @target: the target value of the fixation
2497 * Fixates a #GstStructure by changing the given @field_name field to the given
2498 * @target boolean if that field is not fixed yet.
2500 * Returns: TRUE if the structure could be fixated
2503 gst_structure_fixate_field_boolean (GstStructure * structure,
2504 const char *field_name, gboolean target)
2506 const GValue *value;
2508 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2509 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2511 value = gst_structure_get_value (structure, field_name);
2513 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2516 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2517 const GValue *list_value;
2520 int best_index = -1;
2522 n = gst_value_list_get_size (value);
2523 for (i = 0; i < n; i++) {
2524 list_value = gst_value_list_get_value (value, i);
2525 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2526 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2528 if (best_index == -1 || x == target) {
2534 if (best_index != -1) {
2535 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2545 * gst_structure_fixate_field_string:
2546 * @structure: a #GstStructure
2547 * @field_name: a field in @structure
2548 * @target: the target value of the fixation
2550 * Fixates a #GstStructure by changing the given @field_name field to the given
2551 * @target string if that field is not fixed yet.
2553 * Returns: TRUE if the structure could be fixated
2558 gst_structure_fixate_field_string (GstStructure * structure,
2559 const gchar * field_name, const gchar * target)
2561 const GValue *value;
2563 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2564 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2566 value = gst_structure_get_value (structure, field_name);
2568 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2571 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2572 const GValue *list_value;
2574 const gchar *best = NULL;
2575 int best_index = -1;
2577 n = gst_value_list_get_size (value);
2578 for (i = 0; i < n; i++) {
2579 list_value = gst_value_list_get_value (value, i);
2580 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2581 const gchar *x = g_value_get_string (list_value);
2583 if (best_index == -1 || g_str_equal (x, target)) {
2589 if (best_index != -1) {
2590 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2600 * gst_structure_fixate_field_nearest_fraction:
2601 * @structure: a #GstStructure
2602 * @field_name: a field in @structure
2603 * @target_numerator: The numerator of the target value of the fixation
2604 * @target_denominator: The denominator of the target value of the fixation
2606 * Fixates a #GstStructure by changing the given field to the nearest
2607 * fraction to @target_numerator/@target_denominator that is a subset
2608 * of the existing field.
2610 * Returns: TRUE if the structure could be fixated
2613 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2614 const char *field_name, const gint target_numerator,
2615 const gint target_denominator)
2617 const GValue *value;
2619 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2620 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2622 value = gst_structure_get_value (structure, field_name);
2624 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2627 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2628 const GValue *x, *new_value;
2629 GValue target = { 0 };
2630 g_value_init (&target, GST_TYPE_FRACTION);
2631 gst_value_set_fraction (&target, target_numerator, target_denominator);
2633 new_value = ⌖
2634 x = gst_value_get_fraction_range_min (value);
2635 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2637 x = gst_value_get_fraction_range_max (value);
2638 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2641 gst_structure_set_value (structure, field_name, new_value);
2642 g_value_unset (&target);
2644 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2645 const GValue *list_value;
2647 const GValue *best = NULL;
2650 gdouble best_diff = G_MAXDOUBLE;
2652 target = (gdouble) target_numerator / (gdouble) target_denominator;
2654 GST_DEBUG ("target %g, best %g", target, best_diff);
2658 n = gst_value_list_get_size (value);
2659 for (i = 0; i < n; i++) {
2660 list_value = gst_value_list_get_value (value, i);
2661 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2663 gdouble list_double;
2665 num = gst_value_get_fraction_numerator (list_value);
2666 denom = gst_value_get_fraction_denominator (list_value);
2668 list_double = ((gdouble) num / (gdouble) denom);
2669 cur_diff = target - list_double;
2671 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2674 cur_diff = -cur_diff;
2676 if (!best || cur_diff < best_diff) {
2677 GST_DEBUG ("new best %g", list_double);
2679 best_diff = cur_diff;
2684 gst_structure_set_value (structure, field_name, best);
2693 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2695 GstStructure *s = data;
2698 if (gst_value_fixate (&v, value)) {
2699 gst_structure_id_set_value (s, field_id, &v);
2706 * gst_structure_fixate_field:
2707 * @structure: a #GstStructure
2708 * @field_name: a field in @structure
2710 * Fixates a #GstStructure by changing the given field with its fixated value.
2712 * Returns: TRUE if the structure field could be fixated
2715 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2717 GstStructureField *field;
2719 g_return_val_if_fail (structure != NULL, FALSE);
2720 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2722 if (!(field = gst_structure_get_field (structure, field_name)))
2725 return default_fixate (field->name, &field->value, structure);
2728 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2729 * (useful for message parsing functions where the return location is user
2730 * supplied and the user may pass NULL if the value isn't of interest) */
2731 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2733 const GValue *_value = (value); \
2734 guint _flags = (flags); \
2735 GType _value_type = G_VALUE_TYPE (_value); \
2736 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2737 gchar *_lcopy_format = _vtable->lcopy_format; \
2738 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2739 guint _n_values = 0; \
2741 while (*_lcopy_format != '\0') { \
2742 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2743 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2746 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2747 *(__error) = g_strdup_printf ("either all or none of the return " \
2748 "locations for field '%s' need to be NULL", fieldname); \
2749 } else if (_cvalues[0].v_pointer != NULL) { \
2750 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2755 * gst_structure_get_valist:
2756 * @structure: a #GstStructure
2757 * @first_fieldname: the name of the first field to read
2758 * @args: variable arguments
2760 * Parses the variable arguments and reads fields from @structure accordingly.
2761 * valist-variant of gst_structure_get(). Look at the documentation of
2762 * gst_structure_get() for more details.
2764 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2769 gst_structure_get_valist (const GstStructure * structure,
2770 const char *first_fieldname, va_list args)
2772 const char *field_name;
2773 GType expected_type = G_TYPE_INVALID;
2775 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2776 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2778 field_name = first_fieldname;
2779 while (field_name) {
2780 const GValue *val = NULL;
2783 expected_type = va_arg (args, GType);
2785 val = gst_structure_get_value (structure, field_name);
2790 if (G_VALUE_TYPE (val) != expected_type)
2793 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2795 g_warning ("%s: %s", G_STRFUNC, err);
2800 field_name = va_arg (args, const gchar *);
2808 GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2809 field_name, structure);
2814 GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2815 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2816 GST_STR_NULL (g_type_name (expected_type)),
2817 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2824 * gst_structure_id_get_valist:
2825 * @structure: a #GstStructure
2826 * @first_field_id: the quark of the first field to read
2827 * @args: variable arguments
2829 * Parses the variable arguments and reads fields from @structure accordingly.
2830 * valist-variant of gst_structure_id_get(). Look at the documentation of
2831 * gst_structure_id_get() for more details.
2833 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2838 gst_structure_id_get_valist (const GstStructure * structure,
2839 GQuark first_field_id, va_list args)
2842 GType expected_type = G_TYPE_INVALID;
2844 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2845 g_return_val_if_fail (first_field_id != 0, FALSE);
2847 field_id = first_field_id;
2849 const GValue *val = NULL;
2852 expected_type = va_arg (args, GType);
2854 val = gst_structure_id_get_value (structure, field_id);
2859 if (G_VALUE_TYPE (val) != expected_type)
2862 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2864 g_warning ("%s: %s", G_STRFUNC, err);
2869 field_id = va_arg (args, GQuark);
2877 GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2878 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2883 GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2884 "field was of type '%s': %" GST_PTR_FORMAT,
2885 g_quark_to_string (field_id),
2886 GST_STR_NULL (g_type_name (expected_type)),
2887 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2894 * gst_structure_get:
2895 * @structure: a #GstStructure
2896 * @first_fieldname: the name of the first field to read
2897 * @...: variable arguments
2899 * Parses the variable arguments and reads fields from @structure accordingly.
2900 * Variable arguments should be in the form field name, field type
2901 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2902 * The last variable argument should be NULL.
2904 * For refcounted (mini)objects you will receive a new reference which
2905 * you must release with a suitable _unref() when no longer needed. For
2906 * strings and boxed types you will receive a copy which you will need to
2907 * release with either g_free() or the suitable function for the boxed type.
2909 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2910 * because the field requested did not exist, or was of a type other
2911 * than the type specified), otherwise TRUE.
2916 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
2922 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2923 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2925 va_start (args, first_fieldname);
2926 ret = gst_structure_get_valist (structure, first_fieldname, args);
2933 * gst_structure_id_get:
2934 * @structure: a #GstStructure
2935 * @first_field_id: the quark of the first field to read
2936 * @...: variable arguments
2938 * Parses the variable arguments and reads fields from @structure accordingly.
2939 * Variable arguments should be in the form field id quark, field type
2940 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2941 * The last variable argument should be NULL (technically it should be a
2942 * 0 quark, but we require NULL so compilers that support it can check for
2943 * the NULL terminator and warn if it's not there).
2945 * This function is just like gst_structure_get() only that it is slightly
2946 * more efficient since it saves the string-to-quark lookup in the global
2949 * For refcounted (mini)objects you will receive a new reference which
2950 * you must release with a suitable _unref() when no longer needed. For
2951 * strings and boxed types you will receive a copy which you will need to
2952 * release with either g_free() or the suitable function for the boxed type.
2954 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2955 * because the field requested did not exist, or was of a type other
2956 * than the type specified), otherwise TRUE.
2961 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
2967 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2968 g_return_val_if_fail (first_field_id != 0, FALSE);
2970 va_start (args, first_field_id);
2971 ret = gst_structure_id_get_valist (structure, first_field_id, args);
2978 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
2981 const GstStructure *struct1 = (const GstStructure *) data;
2982 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
2984 if (G_UNLIKELY (val1 == NULL))
2986 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
2994 * gst_structure_is_equal:
2995 * @structure1: a #GstStructure.
2996 * @structure2: a #GstStructure.
2998 * Tests if the two #GstStructure are equal.
3000 * Returns: TRUE if the two structures have the same name and field.
3005 gst_structure_is_equal (const GstStructure * structure1,
3006 const GstStructure * structure2)
3008 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
3009 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
3011 if (G_UNLIKELY (structure1 == structure2))
3014 if (structure1->name != structure2->name) {
3017 if (GST_STRUCTURE_FIELDS (structure1)->len !=
3018 GST_STRUCTURE_FIELDS (structure2)->len) {
3022 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
3023 (gpointer) structure2);
3030 const GstStructure *intersect;
3035 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
3037 IntersectData *idata = (IntersectData *) data;
3038 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3040 if (G_UNLIKELY (val2 == NULL)) {
3041 gst_structure_id_set_value (idata->dest, id, val1);
3043 GValue dest_value = { 0 };
3044 if (gst_value_intersect (&dest_value, val1, val2)) {
3045 gst_structure_id_set_value (idata->dest, id, &dest_value);
3046 g_value_unset (&dest_value);
3055 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
3057 IntersectData *idata = (IntersectData *) data;
3058 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3060 if (G_UNLIKELY (val2 == NULL)) {
3061 gst_structure_id_set_value (idata->dest, id, val1);
3067 * gst_structure_intersect:
3068 * @struct1: a #GstStructure
3069 * @struct2: a #GstStructure
3071 * Interesects @struct1 and @struct2 and returns the intersection.
3073 * Returns: Intersection of @struct1 and @struct2
3078 gst_structure_intersect (const GstStructure * struct1,
3079 const GstStructure * struct2)
3083 g_assert (struct1 != NULL);
3084 g_assert (struct2 != NULL);
3086 if (G_UNLIKELY (struct1->name != struct2->name))
3089 /* copy fields from struct1 which we have not in struct2 to target
3090 * intersect if we have the field in both */
3091 data.dest = gst_structure_new_id_empty (struct1->name);
3092 data.intersect = struct2;
3093 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3094 gst_structure_intersect_field1, &data)))
3097 /* copy fields from struct2 which we have not in struct1 to target */
3098 data.intersect = struct1;
3099 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3100 gst_structure_intersect_field2, &data)))
3106 gst_structure_free (data.dest);
3111 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3114 GstStructure *other = (GstStructure *) data;
3115 const GValue *val2 = gst_structure_id_get_value (other, id);
3117 if (G_LIKELY (val2)) {
3118 if (!gst_value_can_intersect (val1, val2)) {
3121 gint eq = gst_value_compare (val1, val2);
3123 if (eq == GST_VALUE_UNORDERED) {
3124 /* we need to try interseting */
3125 if (!gst_value_intersect (NULL, val1, val2)) {
3128 } else if (eq != GST_VALUE_EQUAL) {
3137 * gst_structure_can_intersect:
3138 * @struct1: a #GstStructure
3139 * @struct2: a #GstStructure
3141 * Tries intersecting @struct1 and @struct2 and reports whether the result
3142 * would not be empty.
3144 * Returns: %TRUE if intersection would not be empty
3149 gst_structure_can_intersect (const GstStructure * struct1,
3150 const GstStructure * struct2)
3152 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3153 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3155 if (G_UNLIKELY (struct1->name != struct2->name))
3158 /* tries to intersect if we have the field in both */
3159 return gst_structure_foreach ((GstStructure *) struct1,
3160 gst_caps_structure_can_intersect_field, (gpointer) struct2);
3164 gst_caps_structure_is_subset_field (GQuark field_id, const GValue * value,
3167 GstStructure *superset = user_data;
3168 const GValue *other;
3171 if (!(other = gst_structure_id_get_value (superset, field_id)))
3172 /* field is missing in the superset => is subset */
3175 comparison = gst_value_compare (other, value);
3177 /* equal values are subset */
3178 if (comparison == GST_VALUE_EQUAL)
3181 /* ordered, but unequal, values are not */
3182 if (comparison != GST_VALUE_UNORDERED)
3185 return gst_value_is_subset (value, other);
3189 * gst_structure_is_subset:
3190 * @subset: a #GstStructure
3191 * @superset: a potentially greater #GstStructure
3193 * Checks if @subset is a subset of @superset, i.e. has the same
3194 * structure name and for all fields that are existing in @superset,
3195 * @subset has a value that is a subset of the value in @superset.
3197 * Returns: %TRUE if @subset is a subset of @superset
3202 gst_structure_is_subset (const GstStructure * subset,
3203 const GstStructure * superset)
3205 if ((superset->name != subset->name) ||
3206 (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3209 return gst_structure_foreach ((GstStructure *) subset,
3210 gst_caps_structure_is_subset_field, (gpointer) superset);
3215 * gst_structure_fixate:
3216 * @structure: a #GstStructure
3218 * Fixate all values in @structure using gst_value_fixate().
3219 * @structure will be modified in-place and should be writable.
3222 gst_structure_fixate (GstStructure * structure)
3224 g_return_if_fail (GST_IS_STRUCTURE (structure));
3226 gst_structure_foreach (structure, default_fixate, structure);