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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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 filled 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,
36 * #GstMessage, #GstEvent, #GstQuery. It provides a means to enforce mutability
37 * using the refcount of the parent with the gst_structure_set_parent_refcount()
40 * A #GstStructure can be created with gst_structure_new_empty() or
41 * gst_structure_new(), which both take a name and an optional set of
42 * key/value pairs along with the types of the values.
44 * Field values can be changed with gst_structure_set_value() or
45 * gst_structure_set().
47 * Field values can be retrieved with gst_structure_get_value() or the more
48 * convenient gst_structure_get_*() functions.
50 * Fields can be removed with gst_structure_remove_field() or
51 * gst_structure_remove_fields().
53 * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are
54 * not allowed. Strings must not be empty either, but may be NULL.
56 * Last reviewed on 2012-03-29 (0.11.3)
65 #include "gst_private.h"
68 #include <gobject/gvaluecollector.h>
70 GST_DEBUG_CATEGORY_STATIC (gst_structure_debug);
71 #define GST_CAT_DEFAULT gst_structure_debug
73 typedef struct _GstStructureField GstStructureField;
75 struct _GstStructureField
85 /* owned by parent structure, NULL if no parent */
86 gint *parent_refcount;
91 #define GST_STRUCTURE_REFCOUNT(s) (((GstStructureImpl*)(s))->parent_refcount)
92 #define GST_STRUCTURE_FIELDS(s) (((GstStructureImpl*)(s))->fields)
94 #define GST_STRUCTURE_FIELD(structure, index) \
95 &g_array_index(GST_STRUCTURE_FIELDS(structure), GstStructureField, (index))
97 #define IS_MUTABLE(structure) \
98 (!GST_STRUCTURE_REFCOUNT(structure) || \
99 g_atomic_int_get (GST_STRUCTURE_REFCOUNT(structure)) == 1)
101 #define IS_TAGLIST(structure) \
102 (structure->name == GST_QUARK (TAGLIST))
104 static void gst_structure_set_field (GstStructure * structure,
105 GstStructureField * field);
106 static GstStructureField *gst_structure_get_field (const GstStructure *
107 structure, const gchar * fieldname);
108 static GstStructureField *gst_structure_id_get_field (const GstStructure *
109 structure, GQuark field);
110 static void gst_structure_transform_to_string (const GValue * src_value,
111 GValue * dest_value);
112 static GstStructure *gst_structure_copy_conditional (const GstStructure *
114 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
115 GValue * value, GType default_type);
116 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
118 GType _gst_structure_type = 0;
121 G_DEFINE_BOXED_TYPE (GstStructure, gst_structure,
122 gst_structure_copy_conditional, gst_structure_free);
125 _priv_gst_structure_initialize (void)
127 _gst_structure_type = gst_structure_get_type ();
129 g_value_register_transform_func (_gst_structure_type, G_TYPE_STRING,
130 gst_structure_transform_to_string);
132 GST_DEBUG_CATEGORY_INIT (gst_structure_debug, "structure", 0,
133 "GstStructure debug");
136 static GstStructure *
137 gst_structure_new_id_empty_with_size (GQuark quark, guint prealloc)
139 GstStructureImpl *structure;
141 structure = g_slice_new (GstStructureImpl);
142 ((GstStructure *) structure)->type = _gst_structure_type;
143 ((GstStructure *) structure)->name = quark;
144 GST_STRUCTURE_REFCOUNT (structure) = NULL;
145 GST_STRUCTURE_FIELDS (structure) =
146 g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
148 GST_TRACE ("created structure %p", structure);
150 return GST_STRUCTURE_CAST (structure);
154 * gst_structure_new_id_empty:
155 * @quark: name of new structure
157 * Creates a new, empty #GstStructure with the given name as a GQuark.
159 * Free-function: gst_structure_free
161 * Returns: (transfer full): a new, empty #GstStructure
164 gst_structure_new_id_empty (GQuark quark)
166 g_return_val_if_fail (quark != 0, NULL);
168 return gst_structure_new_id_empty_with_size (quark, 0);
171 #ifndef G_DISABLE_CHECKS
173 gst_structure_validate_name (const gchar * name)
177 g_return_val_if_fail (name != NULL, FALSE);
179 if (G_UNLIKELY (!g_ascii_isalpha (*name))) {
180 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
185 /* FIXME: test name string more */
187 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
189 if (G_UNLIKELY (*s != '\0')) {
190 GST_WARNING ("Invalid character '%c' at offset %" G_GUINTPTR_FORMAT " in"
191 " structure name: %s", *s, ((guintptr) s - (guintptr) name), name);
195 if (strncmp (name, "video/x-raw-", 12) == 0) {
196 g_warning ("0.10-style raw video caps are being created. Should be "
197 "video/x-raw,format=(string).. now.");
198 } else if (strncmp (name, "audio/x-raw-", 12) == 0) {
199 g_warning ("0.10-style raw audio caps are being created. Should be "
200 "audio/x-raw,format=(string).. now.");
208 * gst_structure_new_empty:
209 * @name: name of new structure
211 * Creates a new, empty #GstStructure with the given @name.
213 * See gst_structure_set_name() for constraints on the @name parameter.
215 * Free-function: gst_structure_free
217 * Returns: (transfer full): a new, empty #GstStructure
220 gst_structure_new_empty (const gchar * name)
222 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
224 return gst_structure_new_id_empty_with_size (g_quark_from_string (name), 0);
229 * @name: name of new structure
230 * @firstfield: name of first field to set
231 * @...: additional arguments
233 * Creates a new #GstStructure with the given name. Parses the
234 * list of variable arguments and sets fields to the values listed.
235 * Variable arguments should be passed as field name, field type,
236 * and value. Last variable argument should be NULL.
238 * Free-function: gst_structure_free
240 * Returns: (transfer full): a new #GstStructure
243 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
245 GstStructure *structure;
248 va_start (varargs, firstfield);
249 structure = gst_structure_new_valist (name, firstfield, varargs);
256 * gst_structure_new_valist:
257 * @name: name of new structure
258 * @firstfield: name of first field to set
259 * @varargs: variable argument list
261 * Creates a new #GstStructure with the given @name. Structure fields
262 * are set according to the varargs in a manner similar to
263 * gst_structure_new().
265 * See gst_structure_set_name() for constraints on the @name parameter.
267 * Free-function: gst_structure_free
269 * Returns: (transfer full): a new #GstStructure
272 gst_structure_new_valist (const gchar * name,
273 const gchar * firstfield, va_list varargs)
275 GstStructure *structure;
277 structure = gst_structure_new_empty (name);
280 gst_structure_set_valist (structure, firstfield, varargs);
286 * gst_structure_set_parent_refcount:
287 * @structure: a #GstStructure
288 * @refcount: (in): a pointer to the parent's refcount
290 * Sets the parent_refcount field of #GstStructure. This field is used to
291 * determine whether a structure is mutable or not. This function should only be
292 * called by code implementing parent objects of #GstStructure, as described in
293 * the MT Refcounting section of the design documents.
295 * Returns: %TRUE if the parent refcount could be set.
298 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
300 g_return_val_if_fail (structure != NULL, FALSE);
302 /* if we have a parent_refcount already, we can only clear
303 * if with a NULL refcount */
304 if (GST_STRUCTURE_REFCOUNT (structure)) {
305 if (refcount != NULL) {
306 g_return_val_if_fail (refcount == NULL, FALSE);
310 if (refcount == NULL) {
311 g_return_val_if_fail (refcount != NULL, FALSE);
316 GST_STRUCTURE_REFCOUNT (structure) = refcount;
322 * gst_structure_copy:
323 * @structure: a #GstStructure to duplicate
325 * Duplicates a #GstStructure and all its fields and values.
327 * Free-function: gst_structure_free
329 * Returns: (transfer full): a new #GstStructure.
332 gst_structure_copy (const GstStructure * structure)
334 GstStructure *new_structure;
335 GstStructureField *field;
338 g_return_val_if_fail (structure != NULL, NULL);
340 len = GST_STRUCTURE_FIELDS (structure)->len;
341 new_structure = gst_structure_new_id_empty_with_size (structure->name, len);
343 for (i = 0; i < len; i++) {
344 GstStructureField new_field = { 0 };
346 field = GST_STRUCTURE_FIELD (structure, i);
348 new_field.name = field->name;
349 gst_value_init_and_copy (&new_field.value, &field->value);
350 g_array_append_val (GST_STRUCTURE_FIELDS (new_structure), new_field);
352 GST_CAT_TRACE (GST_CAT_PERFORMANCE, "doing copy %p -> %p",
353 structure, new_structure);
355 return new_structure;
359 * gst_structure_free:
360 * @structure: (in) (transfer full): the #GstStructure to free
362 * Frees a #GstStructure and all its fields and values. The structure must not
363 * have a parent when this function is called.
366 gst_structure_free (GstStructure * structure)
368 GstStructureField *field;
371 g_return_if_fail (structure != NULL);
372 g_return_if_fail (GST_STRUCTURE_REFCOUNT (structure) == NULL);
374 len = GST_STRUCTURE_FIELDS (structure)->len;
375 for (i = 0; i < len; i++) {
376 field = GST_STRUCTURE_FIELD (structure, i);
378 if (G_IS_VALUE (&field->value)) {
379 g_value_unset (&field->value);
382 g_array_free (GST_STRUCTURE_FIELDS (structure), TRUE);
384 memset (structure, 0xff, sizeof (GstStructure));
386 GST_TRACE ("free structure %p", structure);
388 g_slice_free1 (sizeof (GstStructureImpl), structure);
392 * gst_structure_get_name:
393 * @structure: a #GstStructure
395 * Get the name of @structure as a string.
397 * Returns: the name of the structure.
400 gst_structure_get_name (const GstStructure * structure)
402 g_return_val_if_fail (structure != NULL, NULL);
404 return g_quark_to_string (structure->name);
408 * gst_structure_has_name:
409 * @structure: a #GstStructure
410 * @name: structure name to check for
412 * Checks if the structure has the given name
414 * Returns: TRUE if @name matches the name of the structure.
417 gst_structure_has_name (const GstStructure * structure, const gchar * name)
419 const gchar *structure_name;
421 g_return_val_if_fail (structure != NULL, FALSE);
422 g_return_val_if_fail (name != NULL, FALSE);
424 /* getting the string is cheap and comparing short strings is too
425 * should be faster than getting the quark for name and comparing the quarks
427 structure_name = g_quark_to_string (structure->name);
429 return (structure_name && strcmp (structure_name, name) == 0);
433 * gst_structure_get_name_id:
434 * @structure: a #GstStructure
436 * Get the name of @structure as a GQuark.
438 * Returns: the quark representing the name of the structure.
441 gst_structure_get_name_id (const GstStructure * structure)
443 g_return_val_if_fail (structure != NULL, 0);
445 return structure->name;
449 * gst_structure_set_name:
450 * @structure: a #GstStructure
451 * @name: the new name of the structure
453 * Sets the name of the structure to the given @name. The string
454 * provided is copied before being used. It must not be empty, start with a
455 * letter and can be followed by letters, numbers and any of "/-_.:".
458 gst_structure_set_name (GstStructure * structure, const gchar * name)
460 g_return_if_fail (structure != NULL);
461 g_return_if_fail (IS_MUTABLE (structure));
462 g_return_if_fail (gst_structure_validate_name (name));
464 structure->name = g_quark_from_string (name);
468 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
469 const GValue * value)
471 GstStructureField gsfield = { 0, {0,} };
473 gsfield.name = field;
474 gst_value_init_and_copy (&gsfield.value, value);
476 gst_structure_set_field (structure, &gsfield);
480 * gst_structure_id_set_value:
481 * @structure: a #GstStructure
482 * @field: a #GQuark representing a field
483 * @value: the new value of the field
485 * Sets the field with the given GQuark @field to @value. If the field
486 * does not exist, it is created. If the field exists, the previous
487 * value is replaced and freed.
490 gst_structure_id_set_value (GstStructure * structure,
491 GQuark field, const GValue * value)
494 g_return_if_fail (structure != NULL);
495 g_return_if_fail (G_IS_VALUE (value));
496 g_return_if_fail (IS_MUTABLE (structure));
498 gst_structure_id_set_value_internal (structure, field, value);
502 * gst_structure_set_value:
503 * @structure: a #GstStructure
504 * @fieldname: the name of the field to set
505 * @value: the new value of the field
507 * Sets the field with the given name @field to @value. If the field
508 * does not exist, it is created. If the field exists, the previous
509 * value is replaced and freed.
512 gst_structure_set_value (GstStructure * structure,
513 const gchar * fieldname, const GValue * value)
515 g_return_if_fail (structure != NULL);
516 g_return_if_fail (fieldname != NULL);
517 g_return_if_fail (G_IS_VALUE (value));
518 g_return_if_fail (IS_MUTABLE (structure));
520 gst_structure_id_set_value_internal (structure,
521 g_quark_from_string (fieldname), value);
525 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
528 GstStructureField gsfield = { 0, {0,} };
530 gsfield.name = field;
531 gsfield.value = *value;
533 gst_structure_set_field (structure, &gsfield);
535 /* we took ownership */
537 memset (value, 0, sizeof (GValue));
539 value->g_type = G_TYPE_INVALID;
544 * gst_structure_id_take_value:
545 * @structure: a #GstStructure
546 * @field: a #GQuark representing a field
547 * @value: (transfer full): the new value of the field
549 * Sets the field with the given GQuark @field to @value. If the field
550 * does not exist, it is created. If the field exists, the previous
551 * value is replaced and freed.
554 gst_structure_id_take_value (GstStructure * structure, GQuark field,
557 g_return_if_fail (structure != NULL);
558 g_return_if_fail (G_IS_VALUE (value));
559 g_return_if_fail (IS_MUTABLE (structure));
561 gst_structure_id_take_value_internal (structure, field, value);
565 * gst_structure_take_value:
566 * @structure: a #GstStructure
567 * @fieldname: the name of the field to set
568 * @value: (transfer full): the new value of the field
570 * Sets the field with the given name @field to @value. If the field
571 * does not exist, it is created. If the field exists, the previous
572 * value is replaced and freed. The function will take ownership of @value.
575 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
578 g_return_if_fail (structure != NULL);
579 g_return_if_fail (fieldname != NULL);
580 g_return_if_fail (G_IS_VALUE (value));
581 g_return_if_fail (IS_MUTABLE (structure));
583 gst_structure_id_take_value_internal (structure,
584 g_quark_from_string (fieldname), value);
588 gst_structure_set_valist_internal (GstStructure * structure,
589 const gchar * fieldname, va_list varargs)
595 GstStructureField field = { 0 };
597 field.name = g_quark_from_string (fieldname);
599 type = va_arg (varargs, GType);
601 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
602 if (G_UNLIKELY (err)) {
603 g_critical ("%s", err);
606 gst_structure_set_field (structure, &field);
608 fieldname = va_arg (varargs, gchar *);
614 * @structure: a #GstStructure
615 * @fieldname: the name of the field to set
616 * @...: variable arguments
618 * Parses the variable arguments and sets fields accordingly.
619 * Variable arguments should be in the form field name, field type
620 * (as a GType), value(s). The last variable argument should be NULL.
623 gst_structure_set (GstStructure * structure, const gchar * field, ...)
627 g_return_if_fail (structure != NULL);
628 g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
630 va_start (varargs, field);
631 gst_structure_set_valist_internal (structure, field, varargs);
636 * gst_structure_set_valist:
637 * @structure: a #GstStructure
638 * @fieldname: the name of the field to set
639 * @varargs: variable arguments
641 * va_list form of gst_structure_set().
644 gst_structure_set_valist (GstStructure * structure,
645 const gchar * fieldname, va_list varargs)
647 g_return_if_fail (structure != NULL);
648 g_return_if_fail (IS_MUTABLE (structure));
650 gst_structure_set_valist_internal (structure, fieldname, varargs);
654 gst_structure_id_set_valist_internal (GstStructure * structure,
655 GQuark fieldname, va_list varargs)
661 GstStructureField field = { 0 };
663 field.name = fieldname;
665 type = va_arg (varargs, GType);
667 #ifndef G_VALUE_COLLECT_INIT
668 g_value_init (&field.value, type);
669 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
671 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
673 if (G_UNLIKELY (err)) {
674 g_critical ("%s", err);
677 gst_structure_set_field (structure, &field);
679 fieldname = va_arg (varargs, GQuark);
684 * gst_structure_id_set:
685 * @structure: a #GstStructure
686 * @fieldname: the GQuark for the name of the field to set
687 * @...: variable arguments
689 * Identical to gst_structure_set, except that field names are
690 * passed using the GQuark for the field name. This allows more efficient
691 * setting of the structure if the caller already knows the associated
693 * The last variable argument must be NULL.
696 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
700 g_return_if_fail (structure != NULL);
702 va_start (varargs, field);
703 gst_structure_id_set_valist_internal (structure, field, varargs);
708 * gst_structure_id_set_valist:
709 * @structure: a #GstStructure
710 * @fieldname: the name of the field to set
711 * @varargs: variable arguments
713 * va_list form of gst_structure_id_set().
716 gst_structure_id_set_valist (GstStructure * structure,
717 GQuark fieldname, va_list varargs)
719 g_return_if_fail (structure != NULL);
720 g_return_if_fail (IS_MUTABLE (structure));
722 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
726 * gst_structure_new_id:
727 * @name_quark: name of new structure
728 * @field_quark: the GQuark for the name of the field to set
729 * @...: variable arguments
731 * Creates a new #GstStructure with the given name as a GQuark, followed by
732 * fieldname quark, GType, argument(s) "triplets" in the same format as
733 * gst_structure_id_set(). Basically a convenience wrapper around
734 * gst_structure_new_id_empty() and gst_structure_id_set().
736 * The last variable argument must be NULL (or 0).
738 * Free-function: gst_structure_free
740 * Returns: (transfer full): a new #GstStructure
743 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
748 g_return_val_if_fail (name_quark != 0, NULL);
749 g_return_val_if_fail (field_quark != 0, NULL);
751 s = gst_structure_new_id_empty (name_quark);
753 va_start (varargs, field_quark);
754 gst_structure_id_set_valist_internal (s, field_quark, varargs);
760 #if GST_VERSION_NANO == 1
761 #define GIT_G_WARNING g_warning
763 #define GIT_G_WARNING GST_WARNING
766 /* If the structure currently contains a field with the same name, it is
767 * replaced with the provided field. Otherwise, the field is added to the
768 * structure. The field's value is not deeply copied.
771 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
773 GstStructureField *f;
774 guint i, len = GST_STRUCTURE_FIELDS (structure)->len;
776 if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
779 s = g_value_get_string (&field->value);
780 /* only check for NULL strings in taglists, as they are allowed in message
781 * structs, e.g. error message debug strings */
782 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
784 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
785 "Please file a bug.", g_quark_to_string (field->name));
786 g_value_unset (&field->value);
789 /* empty strings never make sense */
790 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
791 "Please file a bug.", g_quark_to_string (field->name));
792 g_value_unset (&field->value);
795 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
796 g_warning ("Trying to set string on %s field '%s', but string is not "
797 "valid UTF-8. Please file a bug.",
798 IS_TAGLIST (structure) ? "taglist" : "structure",
799 g_quark_to_string (field->name));
800 g_value_unset (&field->value);
803 } else if (G_UNLIKELY (G_VALUE_HOLDS (&field->value, G_TYPE_DATE))) {
806 d = g_value_get_boxed (&field->value);
807 /* only check for NULL GDates in taglists, as they might make sense
808 * in other, generic structs */
809 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
810 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
811 "Please file a bug.", g_quark_to_string (field->name));
812 g_value_unset (&field->value);
814 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
816 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
817 IS_TAGLIST (structure) ? "taglist" : "structure",
818 g_quark_to_string (field->name));
819 g_value_unset (&field->value);
824 for (i = 0; i < len; i++) {
825 f = GST_STRUCTURE_FIELD (structure, i);
827 if (G_UNLIKELY (f->name == field->name)) {
828 g_value_unset (&f->value);
829 memcpy (f, field, sizeof (GstStructureField));
834 g_array_append_val (GST_STRUCTURE_FIELDS (structure), *field);
837 /* If there is no field with the given ID, NULL is returned.
839 static GstStructureField *
840 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
842 GstStructureField *field;
845 len = GST_STRUCTURE_FIELDS (structure)->len;
847 for (i = 0; i < len; i++) {
848 field = GST_STRUCTURE_FIELD (structure, i);
850 if (G_UNLIKELY (field->name == field_id))
857 /* If there is no field with the given ID, NULL is returned.
859 static GstStructureField *
860 gst_structure_get_field (const GstStructure * structure,
861 const gchar * fieldname)
863 g_return_val_if_fail (structure != NULL, NULL);
864 g_return_val_if_fail (fieldname != NULL, NULL);
866 return gst_structure_id_get_field (structure,
867 g_quark_from_string (fieldname));
871 * gst_structure_get_value:
872 * @structure: a #GstStructure
873 * @fieldname: the name of the field to get
875 * Get the value of the field with name @fieldname.
877 * Returns: the #GValue corresponding to the field with the given name.
880 gst_structure_get_value (const GstStructure * structure,
881 const gchar * fieldname)
883 GstStructureField *field;
885 g_return_val_if_fail (structure != NULL, NULL);
886 g_return_val_if_fail (fieldname != NULL, NULL);
888 field = gst_structure_get_field (structure, fieldname);
892 return &field->value;
896 * gst_structure_id_get_value:
897 * @structure: a #GstStructure
898 * @field: the #GQuark of the field to get
900 * Get the value of the field with GQuark @field.
902 * Returns: the #GValue corresponding to the field with the given name
906 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
908 GstStructureField *gsfield;
910 g_return_val_if_fail (structure != NULL, NULL);
912 gsfield = gst_structure_id_get_field (structure, field);
916 return &gsfield->value;
920 * gst_structure_remove_field:
921 * @structure: a #GstStructure
922 * @fieldname: the name of the field to remove
924 * Removes the field with the given name. If the field with the given
925 * name does not exist, the structure is unchanged.
928 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
930 GstStructureField *field;
934 g_return_if_fail (structure != NULL);
935 g_return_if_fail (fieldname != NULL);
936 g_return_if_fail (IS_MUTABLE (structure));
938 id = g_quark_from_string (fieldname);
939 len = GST_STRUCTURE_FIELDS (structure)->len;
941 for (i = 0; i < len; i++) {
942 field = GST_STRUCTURE_FIELD (structure, i);
944 if (field->name == id) {
945 if (G_IS_VALUE (&field->value)) {
946 g_value_unset (&field->value);
948 GST_STRUCTURE_FIELDS (structure) =
949 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
956 * gst_structure_remove_fields:
957 * @structure: a #GstStructure
958 * @fieldname: the name of the field to remove
959 * @...: NULL-terminated list of more fieldnames to remove
961 * Removes the fields with the given names. If a field does not exist, the
962 * argument is ignored.
965 gst_structure_remove_fields (GstStructure * structure,
966 const gchar * fieldname, ...)
970 g_return_if_fail (structure != NULL);
971 g_return_if_fail (fieldname != NULL);
972 /* mutability checked in remove_field */
974 va_start (varargs, fieldname);
975 gst_structure_remove_fields_valist (structure, fieldname, varargs);
980 * gst_structure_remove_fields_valist:
981 * @structure: a #GstStructure
982 * @fieldname: the name of the field to remove
983 * @varargs: NULL-terminated list of more fieldnames to remove
985 * va_list form of gst_structure_remove_fields().
988 gst_structure_remove_fields_valist (GstStructure * structure,
989 const gchar * fieldname, va_list varargs)
991 gchar *field = (gchar *) fieldname;
993 g_return_if_fail (structure != NULL);
994 g_return_if_fail (fieldname != NULL);
995 /* mutability checked in remove_field */
998 gst_structure_remove_field (structure, field);
999 field = va_arg (varargs, char *);
1004 * gst_structure_remove_all_fields:
1005 * @structure: a #GstStructure
1007 * Removes all fields in a GstStructure.
1010 gst_structure_remove_all_fields (GstStructure * structure)
1012 GstStructureField *field;
1015 g_return_if_fail (structure != NULL);
1016 g_return_if_fail (IS_MUTABLE (structure));
1018 for (i = GST_STRUCTURE_FIELDS (structure)->len - 1; i >= 0; i--) {
1019 field = GST_STRUCTURE_FIELD (structure, i);
1021 if (G_IS_VALUE (&field->value)) {
1022 g_value_unset (&field->value);
1024 GST_STRUCTURE_FIELDS (structure) =
1025 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1030 * gst_structure_get_field_type:
1031 * @structure: a #GstStructure
1032 * @fieldname: the name of the field
1034 * Finds the field with the given name, and returns the type of the
1035 * value it contains. If the field is not found, G_TYPE_INVALID is
1038 * Returns: the #GValue of the field
1041 gst_structure_get_field_type (const GstStructure * structure,
1042 const gchar * fieldname)
1044 GstStructureField *field;
1046 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1047 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1049 field = gst_structure_get_field (structure, fieldname);
1051 return G_TYPE_INVALID;
1053 return G_VALUE_TYPE (&field->value);
1057 * gst_structure_n_fields:
1058 * @structure: a #GstStructure
1060 * Get the number of fields in the structure.
1062 * Returns: the number of fields in the structure
1065 gst_structure_n_fields (const GstStructure * structure)
1067 g_return_val_if_fail (structure != NULL, 0);
1069 return GST_STRUCTURE_FIELDS (structure)->len;
1073 * gst_structure_nth_field_name:
1074 * @structure: a #GstStructure
1075 * @index: the index to get the name of
1077 * Get the name of the given field number, counting from 0 onwards.
1079 * Returns: the name of the given field number
1082 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1084 GstStructureField *field;
1086 g_return_val_if_fail (structure != NULL, NULL);
1087 g_return_val_if_fail (index < GST_STRUCTURE_FIELDS (structure)->len, NULL);
1089 field = GST_STRUCTURE_FIELD (structure, index);
1091 return g_quark_to_string (field->name);
1095 * gst_structure_foreach:
1096 * @structure: a #GstStructure
1097 * @func: (scope call): a function to call for each field
1098 * @user_data: (closure): private data
1100 * Calls the provided function once for each field in the #GstStructure. The
1101 * function must not modify the fields. Also see gst_structure_map_in_place().
1103 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1107 gst_structure_foreach (const GstStructure * structure,
1108 GstStructureForeachFunc func, gpointer user_data)
1111 GstStructureField *field;
1114 g_return_val_if_fail (structure != NULL, FALSE);
1115 g_return_val_if_fail (func != NULL, FALSE);
1117 len = GST_STRUCTURE_FIELDS (structure)->len;
1119 for (i = 0; i < len; i++) {
1120 field = GST_STRUCTURE_FIELD (structure, i);
1122 ret = func (field->name, &field->value, user_data);
1123 if (G_UNLIKELY (!ret))
1131 * gst_structure_map_in_place:
1132 * @structure: a #GstStructure
1133 * @func: (scope call): a function to call for each field
1134 * @user_data: (closure): private data
1136 * Calls the provided function once for each field in the #GstStructure. In
1137 * contrast to gst_structure_foreach(), the function may modify but not delete the
1138 * fields. The structure must be mutable.
1140 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1144 gst_structure_map_in_place (GstStructure * structure,
1145 GstStructureMapFunc func, gpointer user_data)
1148 GstStructureField *field;
1151 g_return_val_if_fail (structure != NULL, FALSE);
1152 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1153 g_return_val_if_fail (func != NULL, FALSE);
1154 len = GST_STRUCTURE_FIELDS (structure)->len;
1156 for (i = 0; i < len; i++) {
1157 field = GST_STRUCTURE_FIELD (structure, i);
1159 ret = func (field->name, &field->value, user_data);
1168 * gst_structure_id_has_field:
1169 * @structure: a #GstStructure
1170 * @field: #GQuark of the field name
1172 * Check if @structure contains a field named @field.
1174 * Returns: TRUE if the structure contains a field with the given name
1177 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1179 GstStructureField *f;
1181 g_return_val_if_fail (structure != NULL, FALSE);
1182 g_return_val_if_fail (field != 0, FALSE);
1184 f = gst_structure_id_get_field (structure, field);
1190 * gst_structure_has_field:
1191 * @structure: a #GstStructure
1192 * @fieldname: the name of a field
1194 * Check if @structure contains a field named @fieldname.
1196 * Returns: TRUE if the structure contains a field with the given name
1199 gst_structure_has_field (const GstStructure * structure,
1200 const gchar * fieldname)
1202 g_return_val_if_fail (structure != NULL, FALSE);
1203 g_return_val_if_fail (fieldname != NULL, FALSE);
1205 return gst_structure_id_has_field (structure,
1206 g_quark_from_string (fieldname));
1210 * gst_structure_id_has_field_typed:
1211 * @structure: a #GstStructure
1212 * @field: #GQuark of the field name
1213 * @type: the type of a value
1215 * Check if @structure contains a field named @field and with GType @type.
1217 * Returns: TRUE if the structure contains a field with the given name and type
1220 gst_structure_id_has_field_typed (const GstStructure * structure,
1221 GQuark field, GType type)
1223 GstStructureField *f;
1225 g_return_val_if_fail (structure != NULL, FALSE);
1226 g_return_val_if_fail (field != 0, FALSE);
1228 f = gst_structure_id_get_field (structure, field);
1232 return (G_VALUE_TYPE (&f->value) == type);
1236 * gst_structure_has_field_typed:
1237 * @structure: a #GstStructure
1238 * @fieldname: the name of a field
1239 * @type: the type of a value
1241 * Check if @structure contains a field named @fieldname and with GType @type.
1243 * Returns: TRUE if the structure contains a field with the given name and type
1246 gst_structure_has_field_typed (const GstStructure * structure,
1247 const gchar * fieldname, GType type)
1249 g_return_val_if_fail (structure != NULL, FALSE);
1250 g_return_val_if_fail (fieldname != NULL, FALSE);
1252 return gst_structure_id_has_field_typed (structure,
1253 g_quark_from_string (fieldname), type);
1256 /* utility functions */
1259 * gst_structure_get_boolean:
1260 * @structure: a #GstStructure
1261 * @fieldname: the name of a field
1262 * @value: (out): a pointer to a #gboolean to set
1264 * Sets the boolean pointed to by @value corresponding to the value of the
1265 * given field. Caller is responsible for making sure the field exists
1266 * and has the correct type.
1268 * Returns: TRUE if the value could be set correctly. If there was no field
1269 * with @fieldname or the existing field did not contain a boolean, this
1270 * function returns FALSE.
1273 gst_structure_get_boolean (const GstStructure * structure,
1274 const gchar * fieldname, gboolean * value)
1276 GstStructureField *field;
1278 g_return_val_if_fail (structure != NULL, FALSE);
1279 g_return_val_if_fail (fieldname != NULL, FALSE);
1281 field = gst_structure_get_field (structure, fieldname);
1285 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1288 *value = gst_g_value_get_boolean_unchecked (&field->value);
1294 * gst_structure_get_int:
1295 * @structure: a #GstStructure
1296 * @fieldname: the name of a field
1297 * @value: (out): a pointer to an int to set
1299 * Sets the int pointed to by @value corresponding to the value of the
1300 * given field. Caller is responsible for making sure the field exists
1301 * and has the correct type.
1303 * Returns: %TRUE if the value could be set correctly. If there was no field
1304 * with @fieldname or the existing field did not contain an int, this function
1308 gst_structure_get_int (const GstStructure * structure,
1309 const gchar * fieldname, gint * value)
1311 GstStructureField *field;
1313 g_return_val_if_fail (structure != NULL, FALSE);
1314 g_return_val_if_fail (fieldname != NULL, FALSE);
1315 g_return_val_if_fail (value != NULL, FALSE);
1317 field = gst_structure_get_field (structure, fieldname);
1321 if (!G_VALUE_HOLDS_INT (&field->value))
1324 *value = gst_g_value_get_int_unchecked (&field->value);
1330 * gst_structure_get_uint:
1331 * @structure: a #GstStructure
1332 * @fieldname: the name of a field
1333 * @value: (out): a pointer to a uint to set
1335 * Sets the uint pointed to by @value corresponding to the value of the
1336 * given field. Caller is responsible for making sure the field exists
1337 * and has the correct type.
1339 * Returns: %TRUE if the value could be set correctly. If there was no field
1340 * with @fieldname or the existing field did not contain a uint, this function
1344 gst_structure_get_uint (const GstStructure * structure,
1345 const gchar * fieldname, guint * value)
1347 GstStructureField *field;
1349 g_return_val_if_fail (structure != NULL, FALSE);
1350 g_return_val_if_fail (fieldname != NULL, FALSE);
1351 g_return_val_if_fail (value != NULL, FALSE);
1353 field = gst_structure_get_field (structure, fieldname);
1357 if (!G_VALUE_HOLDS_UINT (&field->value))
1360 *value = gst_g_value_get_uint_unchecked (&field->value);
1366 * gst_structure_get_date:
1367 * @structure: a #GstStructure
1368 * @fieldname: the name of a field
1369 * @value: (out callee-allocates): a pointer to a #GDate to set
1371 * Sets the date pointed to by @value corresponding to the date of the
1372 * given field. Caller is responsible for making sure the field exists
1373 * and has the correct type.
1375 * On success @value will point to a newly-allocated copy of the date which
1376 * should be freed with g_date_free() when no longer needed (note: this is
1377 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1378 * copy of the string).
1380 * Returns: TRUE if the value could be set correctly. If there was no field
1381 * with @fieldname or the existing field did not contain a data, this function
1385 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1388 GstStructureField *field;
1390 g_return_val_if_fail (structure != NULL, FALSE);
1391 g_return_val_if_fail (fieldname != NULL, FALSE);
1392 g_return_val_if_fail (value != NULL, FALSE);
1394 field = gst_structure_get_field (structure, fieldname);
1398 if (!G_VALUE_HOLDS (&field->value, G_TYPE_DATE))
1401 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1402 *value = g_value_dup_boxed (&field->value);
1408 * gst_structure_get_date_time:
1409 * @structure: a #GstStructure
1410 * @fieldname: the name of a field
1411 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1413 * Sets the datetime pointed to by @value corresponding to the datetime of the
1414 * given field. Caller is responsible for making sure the field exists
1415 * and has the correct type.
1417 * On success @value will point to a reference of the datetime which
1418 * should be unreffed with gst_date_time_unref() when no longer needed
1419 * (note: this is inconsistent with e.g. gst_structure_get_string()
1420 * which doesn't return a copy of the string).
1422 * Returns: TRUE if the value could be set correctly. If there was no field
1423 * with @fieldname or the existing field did not contain a data, this function
1427 gst_structure_get_date_time (const GstStructure * structure,
1428 const gchar * fieldname, GstDateTime ** value)
1430 GstStructureField *field;
1432 g_return_val_if_fail (structure != NULL, FALSE);
1433 g_return_val_if_fail (fieldname != NULL, FALSE);
1434 g_return_val_if_fail (value != NULL, FALSE);
1436 field = gst_structure_get_field (structure, fieldname);
1440 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1443 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1444 *value = g_value_dup_boxed (&field->value);
1450 * gst_structure_get_clock_time:
1451 * @structure: a #GstStructure
1452 * @fieldname: the name of a field
1453 * @value: (out): a pointer to a #GstClockTime to set
1455 * Sets the clock time pointed to by @value corresponding to the clock time
1456 * of the given field. Caller is responsible for making sure the field exists
1457 * and has the correct type.
1459 * Returns: TRUE if the value could be set correctly. If there was no field
1460 * with @fieldname or the existing field did not contain a #GstClockTime, this
1461 * function returns FALSE.
1464 gst_structure_get_clock_time (const GstStructure * structure,
1465 const gchar * fieldname, GstClockTime * value)
1467 GstStructureField *field;
1469 g_return_val_if_fail (structure != NULL, FALSE);
1470 g_return_val_if_fail (fieldname != NULL, FALSE);
1471 g_return_val_if_fail (value != NULL, FALSE);
1473 field = gst_structure_get_field (structure, fieldname);
1477 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1480 *value = gst_g_value_get_uint64_unchecked (&field->value);
1486 * gst_structure_get_double:
1487 * @structure: a #GstStructure
1488 * @fieldname: the name of a field
1489 * @value: (out): a pointer to a gdouble to set
1491 * Sets the double pointed to by @value corresponding to the value of the
1492 * given field. Caller is responsible for making sure the field exists
1493 * and has the correct type.
1495 * Returns: TRUE if the value could be set correctly. If there was no field
1496 * with @fieldname or the existing field did not contain a double, this
1497 * function returns FALSE.
1500 gst_structure_get_double (const GstStructure * structure,
1501 const gchar * fieldname, gdouble * value)
1503 GstStructureField *field;
1505 g_return_val_if_fail (structure != NULL, FALSE);
1506 g_return_val_if_fail (fieldname != NULL, FALSE);
1507 g_return_val_if_fail (value != NULL, FALSE);
1509 field = gst_structure_get_field (structure, fieldname);
1513 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1516 *value = gst_g_value_get_double_unchecked (&field->value);
1522 * gst_structure_get_string:
1523 * @structure: a #GstStructure
1524 * @fieldname: the name of a field
1526 * Finds the field corresponding to @fieldname, and returns the string
1527 * contained in the field's value. Caller is responsible for making
1528 * sure the field exists and has the correct type.
1530 * The string should not be modified, and remains valid until the next
1531 * call to a gst_structure_*() function with the given structure.
1533 * Returns: a pointer to the string or NULL when the field did not exist
1534 * or did not contain a string.
1537 gst_structure_get_string (const GstStructure * structure,
1538 const gchar * fieldname)
1540 GstStructureField *field;
1542 g_return_val_if_fail (structure != NULL, NULL);
1543 g_return_val_if_fail (fieldname != NULL, NULL);
1545 field = gst_structure_get_field (structure, fieldname);
1549 if (!G_VALUE_HOLDS_STRING (&field->value))
1552 return gst_g_value_get_string_unchecked (&field->value);
1556 * gst_structure_get_enum:
1557 * @structure: a #GstStructure
1558 * @fieldname: the name of a field
1559 * @enumtype: the enum type of a field
1560 * @value: (out): a pointer to an int to set
1562 * Sets the int pointed to by @value corresponding to the value of the
1563 * given field. Caller is responsible for making sure the field exists,
1564 * has the correct type and that the enumtype is correct.
1566 * Returns: TRUE if the value could be set correctly. If there was no field
1567 * with @fieldname or the existing field did not contain an enum of the given
1568 * type, this function returns FALSE.
1571 gst_structure_get_enum (const GstStructure * structure,
1572 const gchar * fieldname, GType enumtype, gint * value)
1574 GstStructureField *field;
1576 g_return_val_if_fail (structure != NULL, FALSE);
1577 g_return_val_if_fail (fieldname != NULL, FALSE);
1578 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1579 g_return_val_if_fail (value != NULL, FALSE);
1581 field = gst_structure_get_field (structure, fieldname);
1585 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1588 *value = g_value_get_enum (&field->value);
1594 * gst_structure_get_fraction:
1595 * @structure: a #GstStructure
1596 * @fieldname: the name of a field
1597 * @value_numerator: (out): a pointer to an int to set
1598 * @value_denominator: (out): a pointer to an int to set
1600 * Sets the integers pointed to by @value_numerator and @value_denominator
1601 * corresponding to the value of the given field. Caller is responsible
1602 * for making sure the field exists and has the correct type.
1604 * Returns: TRUE if the values could be set correctly. If there was no field
1605 * with @fieldname or the existing field did not contain a GstFraction, this
1606 * function returns FALSE.
1609 gst_structure_get_fraction (const GstStructure * structure,
1610 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1612 GstStructureField *field;
1614 g_return_val_if_fail (structure != NULL, FALSE);
1615 g_return_val_if_fail (fieldname != NULL, FALSE);
1616 g_return_val_if_fail (value_numerator != NULL, FALSE);
1617 g_return_val_if_fail (value_denominator != NULL, FALSE);
1619 field = gst_structure_get_field (structure, fieldname);
1623 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1626 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1627 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1632 typedef struct _GstStructureAbbreviation
1634 const gchar *type_name;
1637 GstStructureAbbreviation;
1639 /* return a copy of an array of GstStructureAbbreviation containing all the
1640 * known type_string, GType maps, including abbreviations for common types */
1641 static GstStructureAbbreviation *
1642 gst_structure_get_abbrs (gint * n_abbrs)
1644 static GstStructureAbbreviation *abbrs = NULL;
1645 static volatile gsize num = 0;
1647 if (g_once_init_enter (&num)) {
1648 /* dynamically generate the array */
1650 GstStructureAbbreviation dyn_abbrs[] = {
1655 {"uint", G_TYPE_UINT}
1659 {"float", G_TYPE_FLOAT}
1663 {"double", G_TYPE_DOUBLE}
1665 {"d", G_TYPE_DOUBLE}
1667 {"buffer", GST_TYPE_BUFFER}
1669 {"fraction", GST_TYPE_FRACTION}
1671 {"boolean", G_TYPE_BOOLEAN}
1673 {"bool", G_TYPE_BOOLEAN}
1675 {"b", G_TYPE_BOOLEAN}
1677 {"string", G_TYPE_STRING}
1679 {"str", G_TYPE_STRING}
1681 {"s", G_TYPE_STRING}
1683 {"structure", GST_TYPE_STRUCTURE}
1685 {"date", G_TYPE_DATE}
1687 {"datetime", GST_TYPE_DATE_TIME}
1689 {"bitmask", GST_TYPE_BITMASK}
1691 {"sample", GST_TYPE_SAMPLE}
1693 {"taglist", GST_TYPE_TAG_LIST}
1695 _num = G_N_ELEMENTS (dyn_abbrs);
1696 /* permanently allocate and copy the array now */
1697 abbrs = g_new0 (GstStructureAbbreviation, _num);
1698 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1699 g_once_init_leave (&num, _num);
1706 /* given a type_name that could be a type abbreviation or a registered GType,
1707 * return a matching GType */
1709 gst_structure_gtype_from_abbr (const char *type_name)
1712 GstStructureAbbreviation *abbrs;
1715 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1717 abbrs = gst_structure_get_abbrs (&n_abbrs);
1719 for (i = 0; i < n_abbrs; i++) {
1720 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1721 return abbrs[i].type;
1725 /* this is the fallback */
1726 return g_type_from_name (type_name);
1730 gst_structure_to_abbr (GType type)
1733 GstStructureAbbreviation *abbrs;
1736 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1738 abbrs = gst_structure_get_abbrs (&n_abbrs);
1740 for (i = 0; i < n_abbrs; i++) {
1741 if (type == abbrs[i].type) {
1742 return abbrs[i].type_name;
1746 return g_type_name (type);
1750 gst_structure_value_get_generic_type (GValue * val)
1752 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1753 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1754 GArray *array = g_value_peek_pointer (val);
1756 if (array->len > 0) {
1757 GValue *value = &g_array_index (array, GValue, 0);
1759 return gst_structure_value_get_generic_type (value);
1763 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1765 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1766 return G_TYPE_INT64;
1767 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1768 return G_TYPE_DOUBLE;
1769 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1770 return GST_TYPE_FRACTION;
1772 return G_VALUE_TYPE (val);
1776 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1779 GstStructureField *field;
1782 g_return_val_if_fail (s != NULL, FALSE);
1784 len = GST_STRUCTURE_FIELDS (structure)->len;
1785 for (i = 0; i < len; i++) {
1789 field = GST_STRUCTURE_FIELD (structure, i);
1791 t = gst_value_serialize (&field->value);
1792 type = gst_structure_value_get_generic_type (&field->value);
1794 g_string_append_len (s, ", ", 2);
1795 /* FIXME: do we need to escape fieldnames? */
1796 g_string_append (s, g_quark_to_string (field->name));
1797 g_string_append_len (s, "=(", 2);
1798 g_string_append (s, gst_structure_to_abbr (type));
1799 g_string_append_c (s, ')');
1800 g_string_append (s, t == NULL ? "NULL" : t);
1804 g_string_append_c (s, ';');
1809 * gst_structure_to_string:
1810 * @structure: a #GstStructure
1812 * Converts @structure to a human-readable string representation.
1814 * For debugging purposes its easier to do something like this:
1816 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1818 * This prints the structure in human readble form.
1820 * Free-function: g_free
1822 * Returns: (transfer full): a pointer to string allocated by g_malloc().
1823 * g_free() after usage.
1826 gst_structure_to_string (const GstStructure * structure)
1830 /* NOTE: This function is potentially called by the debug system,
1831 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1832 * should be careful to avoid recursion. This includes any functions
1833 * called by gst_structure_to_string. In particular, calls should
1834 * not use the GST_PTR_FORMAT extension. */
1836 g_return_val_if_fail (structure != NULL, NULL);
1838 /* we estimate a minimum size based on the number of fields in order to
1839 * avoid unnecessary reallocs within GString */
1840 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1841 g_string_append (s, g_quark_to_string (structure->name));
1842 priv_gst_structure_append_to_gstring (structure, s);
1843 return g_string_free (s, FALSE);
1847 * r will still point to the string. if end == next, the string will not be
1848 * null-terminated. In all other cases it will be.
1849 * end = pointer to char behind end of string, next = pointer to start of
1851 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1854 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1865 ret = gst_structure_parse_simple_string (s, end);
1875 if (G_UNLIKELY (*s == 0))
1877 if (G_UNLIKELY (*s == '\\'))
1885 /* Find the closing quotes */
1888 if (G_UNLIKELY (*s == 0))
1890 if (G_UNLIKELY (*s == '\\'))
1905 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1908 GValue value1 = { 0 };
1909 GValue value2 = { 0 };
1910 GValue value3 = { 0 };
1912 gboolean ret, have_step = FALSE;
1918 ret = gst_structure_parse_value (s, &s, &value1, type);
1922 while (g_ascii_isspace (*s))
1929 while (g_ascii_isspace (*s))
1932 ret = gst_structure_parse_value (s, &s, &value2, type);
1936 while (g_ascii_isspace (*s))
1939 /* optional step for int and int64 */
1940 if (G_VALUE_TYPE (&value1) == G_TYPE_INT
1941 || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1945 while (g_ascii_isspace (*s))
1948 ret = gst_structure_parse_value (s, &s, &value3, type);
1952 while (g_ascii_isspace (*s))
1963 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1965 if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
1968 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1969 range_type = GST_TYPE_DOUBLE_RANGE;
1970 g_value_init (value, range_type);
1971 gst_value_set_double_range (value,
1972 gst_g_value_get_double_unchecked (&value1),
1973 gst_g_value_get_double_unchecked (&value2));
1974 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1975 range_type = GST_TYPE_INT_RANGE;
1976 g_value_init (value, range_type);
1978 gst_value_set_int_range_step (value,
1979 gst_g_value_get_int_unchecked (&value1),
1980 gst_g_value_get_int_unchecked (&value2),
1981 gst_g_value_get_int_unchecked (&value3));
1983 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
1984 gst_g_value_get_int_unchecked (&value2));
1985 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1986 range_type = GST_TYPE_INT64_RANGE;
1987 g_value_init (value, range_type);
1989 gst_value_set_int64_range_step (value,
1990 gst_g_value_get_int64_unchecked (&value1),
1991 gst_g_value_get_int64_unchecked (&value2),
1992 gst_g_value_get_int64_unchecked (&value3));
1994 gst_value_set_int64_range (value,
1995 gst_g_value_get_int64_unchecked (&value1),
1996 gst_g_value_get_int64_unchecked (&value2));
1997 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1998 range_type = GST_TYPE_FRACTION_RANGE;
1999 g_value_init (value, range_type);
2000 gst_value_set_fraction_range (value, &value1, &value2);
2010 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
2011 GType type, GType list_type, char begin, char end)
2013 GValue list_value = { 0 };
2017 g_value_init (value, list_type);
2018 array = g_value_peek_pointer (value);
2024 while (g_ascii_isspace (*s))
2032 ret = gst_structure_parse_value (s, &s, &list_value, type);
2036 g_array_append_val (array, list_value);
2038 while (g_ascii_isspace (*s))
2046 while (g_ascii_isspace (*s))
2049 memset (&list_value, 0, sizeof (list_value));
2050 ret = gst_structure_parse_value (s, &s, &list_value, type);
2054 g_array_append_val (array, list_value);
2055 while (g_ascii_isspace (*s))
2066 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2068 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2073 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2076 return gst_structure_parse_any_list (s, after, value, type,
2077 GST_TYPE_ARRAY, '<', '>');
2081 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2085 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2095 gst_structure_parse_field (gchar * str,
2096 gchar ** after, GstStructureField * field)
2105 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2108 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2109 GST_WARNING ("failed to parse simple string, str=%s", str);
2114 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2117 if (G_UNLIKELY (*s != '=')) {
2118 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2125 field->name = g_quark_from_string (name);
2126 GST_DEBUG ("trying field name '%s'", name);
2129 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2131 GST_WARNING ("failed to parse value %s", str);
2140 gst_structure_parse_value (gchar * str,
2141 gchar ** after, GValue * value, GType default_type)
2150 GType type = default_type;
2153 while (g_ascii_isspace (*s))
2156 /* check if there's a (type_name) 'cast' */
2160 while (g_ascii_isspace (*s))
2163 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2166 while (g_ascii_isspace (*s))
2168 if (G_UNLIKELY (*s != ')'))
2171 while (g_ascii_isspace (*s))
2176 type = gst_structure_gtype_from_abbr (type_name);
2177 GST_DEBUG ("trying type name '%s'", type_name);
2180 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2181 GST_WARNING ("invalid type");
2186 while (g_ascii_isspace (*s))
2189 ret = gst_structure_parse_range (s, &s, value, type);
2190 } else if (*s == '{') {
2191 ret = gst_structure_parse_list (s, &s, value, type);
2192 } else if (*s == '<') {
2193 ret = gst_structure_parse_array (s, &s, value, type);
2197 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2199 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2204 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2206 /* Set NULL terminator for deserialization */
2210 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2211 g_value_init (value, try_types[i]);
2212 ret = gst_value_deserialize (value, value_s);
2215 g_value_unset (value);
2218 g_value_init (value, type);
2220 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2221 (type != G_TYPE_STRING))))
2223 /* Set NULL terminator for deserialization */
2227 ret = gst_value_deserialize (value, value_s);
2228 if (G_UNLIKELY (!ret))
2229 g_value_unset (value);
2240 priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end,
2248 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2249 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2250 && g_ascii_isspace (r[1]))))
2255 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2256 GST_WARNING ("Failed to parse structure string '%s'", str);
2267 priv_gst_structure_parse_fields (gchar * str, gchar ** end,
2268 GstStructure * structure)
2271 GstStructureField field;
2276 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2277 && g_ascii_isspace (r[1]))))
2280 /* end of structure, get the next char and finish */
2285 /* accept \0 as end delimiter */
2288 if (G_UNLIKELY (*r != ',')) {
2289 GST_WARNING ("Failed to find delimiter, r=%s", r);
2293 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2294 && g_ascii_isspace (r[1]))))
2297 memset (&field, 0, sizeof (field));
2298 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2299 GST_WARNING ("Failed to parse field, r=%s", r);
2302 gst_structure_set_field (structure, &field);
2311 * gst_structure_new_from_string:
2312 * @string: a string representation of a #GstStructure
2314 * Creates a #GstStructure from a string representation.
2315 * If end is not NULL, a pointer to the place inside the given string
2316 * where parsing ended will be returned.
2318 * Free-function: gst_structure_free
2320 * Returns: (transfer full): a new #GstStructure or NULL when the string could
2321 * not be parsed. Free with gst_structure_free() after use.
2326 gst_structure_new_from_string (const gchar * string)
2328 return gst_structure_from_string (string, NULL);
2332 * gst_structure_from_string:
2333 * @string: a string representation of a #GstStructure.
2334 * @end: (out) (allow-none) (transfer none) (skip): pointer to store the end of the string in.
2336 * Creates a #GstStructure from a string representation.
2337 * If end is not NULL, a pointer to the place inside the given string
2338 * where parsing ended will be returned.
2340 * Free-function: gst_structure_free
2342 * Returns: (transfer full): a new #GstStructure or NULL when the string could
2343 * not be parsed. Free with gst_structure_free() after use.
2346 gst_structure_from_string (const gchar * string, gchar ** end)
2353 GstStructure *structure = NULL;
2355 g_return_val_if_fail (string != NULL, NULL);
2357 copy = g_strdup (string);
2360 if (!priv_gst_structure_parse_name (r, &name, &w, &r))
2365 structure = gst_structure_new_empty (name);
2368 if (G_UNLIKELY (structure == NULL))
2371 if (!priv_gst_structure_parse_fields (r, &r, structure))
2375 *end = (char *) string + (r - copy);
2377 g_warning ("gst_structure_from_string did not consume whole string,"
2378 " but caller did not provide end pointer (\"%s\")", string);
2385 gst_structure_free (structure);
2391 gst_structure_transform_to_string (const GValue * src_value,
2392 GValue * dest_value)
2394 g_return_if_fail (src_value != NULL);
2395 g_return_if_fail (dest_value != NULL);
2397 dest_value->data[0].v_pointer =
2398 gst_structure_to_string (src_value->data[0].v_pointer);
2401 static GstStructure *
2402 gst_structure_copy_conditional (const GstStructure * structure)
2405 return gst_structure_copy (structure);
2409 /* fixate utility functions */
2412 * gst_structure_fixate_field_nearest_int:
2413 * @structure: a #GstStructure
2414 * @field_name: a field in @structure
2415 * @target: the target value of the fixation
2417 * Fixates a #GstStructure by changing the given field to the nearest
2418 * integer to @target that is a subset of the existing field.
2420 * Returns: TRUE if the structure could be fixated
2423 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2424 const char *field_name, int target)
2426 const GValue *value;
2428 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2429 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2431 value = gst_structure_get_value (structure, field_name);
2433 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2436 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2439 x = gst_value_get_int_range_min (value);
2442 x = gst_value_get_int_range_max (value);
2445 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2447 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2448 const GValue *list_value;
2451 int best_index = -1;
2453 n = gst_value_list_get_size (value);
2454 for (i = 0; i < n; i++) {
2455 list_value = gst_value_list_get_value (value, i);
2456 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2457 int x = gst_g_value_get_int_unchecked (list_value);
2459 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2465 if (best_index != -1) {
2466 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2476 * gst_structure_fixate_field_nearest_double:
2477 * @structure: a #GstStructure
2478 * @field_name: a field in @structure
2479 * @target: the target value of the fixation
2481 * Fixates a #GstStructure by changing the given field to the nearest
2482 * double to @target that is a subset of the existing field.
2484 * Returns: TRUE if the structure could be fixated
2487 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2488 const char *field_name, double target)
2490 const GValue *value;
2492 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2493 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2495 value = gst_structure_get_value (structure, field_name);
2497 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2500 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2503 x = gst_value_get_double_range_min (value);
2506 x = gst_value_get_double_range_max (value);
2509 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2511 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2512 const GValue *list_value;
2515 int best_index = -1;
2517 n = gst_value_list_get_size (value);
2518 for (i = 0; i < n; i++) {
2519 list_value = gst_value_list_get_value (value, i);
2520 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2521 double x = gst_g_value_get_double_unchecked (list_value);
2523 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2529 if (best_index != -1) {
2530 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2541 * gst_structure_fixate_field_boolean:
2542 * @structure: a #GstStructure
2543 * @field_name: a field in @structure
2544 * @target: the target value of the fixation
2546 * Fixates a #GstStructure by changing the given @field_name field to the given
2547 * @target boolean if that field is not fixed yet.
2549 * Returns: TRUE if the structure could be fixated
2552 gst_structure_fixate_field_boolean (GstStructure * structure,
2553 const char *field_name, gboolean target)
2555 const GValue *value;
2557 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2558 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2560 value = gst_structure_get_value (structure, field_name);
2562 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2565 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2566 const GValue *list_value;
2569 int best_index = -1;
2571 n = gst_value_list_get_size (value);
2572 for (i = 0; i < n; i++) {
2573 list_value = gst_value_list_get_value (value, i);
2574 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2575 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2577 if (best_index == -1 || x == target) {
2583 if (best_index != -1) {
2584 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2594 * gst_structure_fixate_field_string:
2595 * @structure: a #GstStructure
2596 * @field_name: a field in @structure
2597 * @target: the target value of the fixation
2599 * Fixates a #GstStructure by changing the given @field_name field to the given
2600 * @target string if that field is not fixed yet.
2602 * Returns: TRUE if the structure could be fixated
2605 gst_structure_fixate_field_string (GstStructure * structure,
2606 const gchar * field_name, const gchar * target)
2608 const GValue *value;
2610 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2611 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2613 value = gst_structure_get_value (structure, field_name);
2615 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2618 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2619 const GValue *list_value;
2621 const gchar *best = NULL;
2622 int best_index = -1;
2624 n = gst_value_list_get_size (value);
2625 for (i = 0; i < n; i++) {
2626 list_value = gst_value_list_get_value (value, i);
2627 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2628 const gchar *x = g_value_get_string (list_value);
2630 if (best_index == -1 || g_str_equal (x, target)) {
2636 if (best_index != -1) {
2637 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2647 * gst_structure_fixate_field_nearest_fraction:
2648 * @structure: a #GstStructure
2649 * @field_name: a field in @structure
2650 * @target_numerator: The numerator of the target value of the fixation
2651 * @target_denominator: The denominator of the target value of the fixation
2653 * Fixates a #GstStructure by changing the given field to the nearest
2654 * fraction to @target_numerator/@target_denominator that is a subset
2655 * of the existing field.
2657 * Returns: TRUE if the structure could be fixated
2660 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2661 const char *field_name, const gint target_numerator,
2662 const gint target_denominator)
2664 const GValue *value;
2666 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2667 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2669 value = gst_structure_get_value (structure, field_name);
2671 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2674 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2675 const GValue *x, *new_value;
2676 GValue target = { 0 };
2677 g_value_init (&target, GST_TYPE_FRACTION);
2678 gst_value_set_fraction (&target, target_numerator, target_denominator);
2680 new_value = ⌖
2681 x = gst_value_get_fraction_range_min (value);
2682 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2684 x = gst_value_get_fraction_range_max (value);
2685 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2688 gst_structure_set_value (structure, field_name, new_value);
2689 g_value_unset (&target);
2691 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2692 const GValue *list_value;
2694 const GValue *best = NULL;
2697 gdouble best_diff = G_MAXDOUBLE;
2699 target = (gdouble) target_numerator / (gdouble) target_denominator;
2701 GST_DEBUG ("target %g, best %g", target, best_diff);
2705 n = gst_value_list_get_size (value);
2706 for (i = 0; i < n; i++) {
2707 list_value = gst_value_list_get_value (value, i);
2708 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2710 gdouble list_double;
2712 num = gst_value_get_fraction_numerator (list_value);
2713 denom = gst_value_get_fraction_denominator (list_value);
2715 list_double = ((gdouble) num / (gdouble) denom);
2716 cur_diff = target - list_double;
2718 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2721 cur_diff = -cur_diff;
2723 if (!best || cur_diff < best_diff) {
2724 GST_DEBUG ("new best %g", list_double);
2726 best_diff = cur_diff;
2731 gst_structure_set_value (structure, field_name, best);
2740 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2742 GstStructure *s = data;
2745 if (gst_value_fixate (&v, value)) {
2746 gst_structure_id_take_value (s, field_id, &v);
2752 * gst_structure_fixate_field:
2753 * @structure: a #GstStructure
2754 * @field_name: a field in @structure
2756 * Fixates a #GstStructure by changing the given field with its fixated value.
2758 * Returns: TRUE if the structure field could be fixated
2761 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2763 GstStructureField *field;
2765 g_return_val_if_fail (structure != NULL, FALSE);
2766 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2768 if (!(field = gst_structure_get_field (structure, field_name)))
2771 return default_fixate (field->name, &field->value, structure);
2774 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2775 * (useful for message parsing functions where the return location is user
2776 * supplied and the user may pass NULL if the value isn't of interest) */
2777 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2779 const GValue *_value = (value); \
2780 guint _flags = (flags); \
2781 GType _value_type = G_VALUE_TYPE (_value); \
2782 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2783 const gchar *_lcopy_format = _vtable->lcopy_format; \
2784 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2785 guint _n_values = 0; \
2787 while (*_lcopy_format != '\0') { \
2788 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2789 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2792 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2793 *(__error) = g_strdup_printf ("either all or none of the return " \
2794 "locations for field '%s' need to be NULL", fieldname); \
2795 } else if (_cvalues[0].v_pointer != NULL) { \
2796 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2801 * gst_structure_get_valist:
2802 * @structure: a #GstStructure
2803 * @first_fieldname: the name of the first field to read
2804 * @args: variable arguments
2806 * Parses the variable arguments and reads fields from @structure accordingly.
2807 * valist-variant of gst_structure_get(). Look at the documentation of
2808 * gst_structure_get() for more details.
2810 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2813 gst_structure_get_valist (const GstStructure * structure,
2814 const char *first_fieldname, va_list args)
2816 const char *field_name;
2817 GType expected_type = G_TYPE_INVALID;
2819 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2820 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2822 field_name = first_fieldname;
2823 while (field_name) {
2824 const GValue *val = NULL;
2827 expected_type = va_arg (args, GType);
2829 val = gst_structure_get_value (structure, field_name);
2834 if (G_VALUE_TYPE (val) != expected_type)
2837 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2839 g_warning ("%s: %s", G_STRFUNC, err);
2844 field_name = va_arg (args, const gchar *);
2852 GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2853 field_name, structure);
2858 GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
2859 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2860 GST_STR_NULL (g_type_name (expected_type)),
2861 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2868 * gst_structure_id_get_valist:
2869 * @structure: a #GstStructure
2870 * @first_field_id: the quark of the first field to read
2871 * @args: variable arguments
2873 * Parses the variable arguments and reads fields from @structure accordingly.
2874 * valist-variant of gst_structure_id_get(). Look at the documentation of
2875 * gst_structure_id_get() for more details.
2877 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2880 gst_structure_id_get_valist (const GstStructure * structure,
2881 GQuark first_field_id, va_list args)
2884 GType expected_type = G_TYPE_INVALID;
2886 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2887 g_return_val_if_fail (first_field_id != 0, FALSE);
2889 field_id = first_field_id;
2891 const GValue *val = NULL;
2894 expected_type = va_arg (args, GType);
2896 val = gst_structure_id_get_value (structure, field_id);
2901 if (G_VALUE_TYPE (val) != expected_type)
2904 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2906 g_warning ("%s: %s", G_STRFUNC, err);
2911 field_id = va_arg (args, GQuark);
2919 GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2920 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2925 GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
2926 "field was of type '%s': %" GST_PTR_FORMAT,
2927 g_quark_to_string (field_id),
2928 GST_STR_NULL (g_type_name (expected_type)),
2929 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2936 * gst_structure_get:
2937 * @structure: a #GstStructure
2938 * @first_fieldname: the name of the first field to read
2939 * @...: variable arguments
2941 * Parses the variable arguments and reads fields from @structure accordingly.
2942 * Variable arguments should be in the form field name, field type
2943 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2944 * The last variable argument should be NULL.
2946 * For refcounted (mini)objects you will receive a new reference which
2947 * you must release with a suitable _unref() when no longer needed. For
2948 * strings and boxed types you will receive a copy which you will need to
2949 * release with either g_free() or the suitable function for the boxed type.
2951 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2952 * because the field requested did not exist, or was of a type other
2953 * than the type specified), otherwise TRUE.
2956 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
2962 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2963 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2965 va_start (args, first_fieldname);
2966 ret = gst_structure_get_valist (structure, first_fieldname, args);
2973 * gst_structure_id_get:
2974 * @structure: a #GstStructure
2975 * @first_field_id: the quark of the first field to read
2976 * @...: variable arguments
2978 * Parses the variable arguments and reads fields from @structure accordingly.
2979 * Variable arguments should be in the form field id quark, field type
2980 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2981 * The last variable argument should be NULL (technically it should be a
2982 * 0 quark, but we require NULL so compilers that support it can check for
2983 * the NULL terminator and warn if it's not there).
2985 * This function is just like gst_structure_get() only that it is slightly
2986 * more efficient since it saves the string-to-quark lookup in the global
2989 * For refcounted (mini)objects you will receive a new reference which
2990 * you must release with a suitable _unref() when no longer needed. For
2991 * strings and boxed types you will receive a copy which you will need to
2992 * release with either g_free() or the suitable function for the boxed type.
2994 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2995 * because the field requested did not exist, or was of a type other
2996 * than the type specified), otherwise TRUE.
2999 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
3005 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3006 g_return_val_if_fail (first_field_id != 0, FALSE);
3008 va_start (args, first_field_id);
3009 ret = gst_structure_id_get_valist (structure, first_field_id, args);
3016 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
3019 const GstStructure *struct1 = (const GstStructure *) data;
3020 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
3022 if (G_UNLIKELY (val1 == NULL))
3024 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
3032 * gst_structure_is_equal:
3033 * @structure1: a #GstStructure.
3034 * @structure2: a #GstStructure.
3036 * Tests if the two #GstStructure are equal.
3038 * Returns: TRUE if the two structures have the same name and field.
3041 gst_structure_is_equal (const GstStructure * structure1,
3042 const GstStructure * structure2)
3044 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
3045 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
3047 if (G_UNLIKELY (structure1 == structure2))
3050 if (structure1->name != structure2->name) {
3053 if (GST_STRUCTURE_FIELDS (structure1)->len !=
3054 GST_STRUCTURE_FIELDS (structure2)->len) {
3058 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
3059 (gpointer) structure2);
3066 const GstStructure *intersect;
3071 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
3073 IntersectData *idata = (IntersectData *) data;
3074 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3076 if (G_UNLIKELY (val2 == NULL)) {
3077 gst_structure_id_set_value (idata->dest, id, val1);
3079 GValue dest_value = { 0 };
3080 if (gst_value_intersect (&dest_value, val1, val2)) {
3081 gst_structure_id_take_value (idata->dest, id, &dest_value);
3090 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
3092 IntersectData *idata = (IntersectData *) data;
3093 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3095 if (G_UNLIKELY (val2 == NULL)) {
3096 gst_structure_id_set_value (idata->dest, id, val1);
3102 * gst_structure_intersect:
3103 * @struct1: a #GstStructure
3104 * @struct2: a #GstStructure
3106 * Interesects @struct1 and @struct2 and returns the intersection.
3108 * Returns: Intersection of @struct1 and @struct2
3111 gst_structure_intersect (const GstStructure * struct1,
3112 const GstStructure * struct2)
3116 g_assert (struct1 != NULL);
3117 g_assert (struct2 != NULL);
3119 if (G_UNLIKELY (struct1->name != struct2->name))
3122 /* copy fields from struct1 which we have not in struct2 to target
3123 * intersect if we have the field in both */
3124 data.dest = gst_structure_new_id_empty (struct1->name);
3125 data.intersect = struct2;
3126 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3127 gst_structure_intersect_field1, &data)))
3130 /* copy fields from struct2 which we have not in struct1 to target */
3131 data.intersect = struct1;
3132 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3133 gst_structure_intersect_field2, &data)))
3139 gst_structure_free (data.dest);
3144 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3147 GstStructure *other = (GstStructure *) data;
3148 const GValue *val2 = gst_structure_id_get_value (other, id);
3150 if (G_LIKELY (val2)) {
3151 if (!gst_value_can_intersect (val1, val2)) {
3154 gint eq = gst_value_compare (val1, val2);
3156 if (eq == GST_VALUE_UNORDERED) {
3157 /* we need to try interseting */
3158 if (!gst_value_intersect (NULL, val1, val2)) {
3161 } else if (eq != GST_VALUE_EQUAL) {
3170 * gst_structure_can_intersect:
3171 * @struct1: a #GstStructure
3172 * @struct2: a #GstStructure
3174 * Tries intersecting @struct1 and @struct2 and reports whether the result
3175 * would not be empty.
3177 * Returns: %TRUE if intersection would not be empty
3180 gst_structure_can_intersect (const GstStructure * struct1,
3181 const GstStructure * struct2)
3183 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3184 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3186 if (G_UNLIKELY (struct1->name != struct2->name))
3189 /* tries to intersect if we have the field in both */
3190 return gst_structure_foreach ((GstStructure *) struct1,
3191 gst_caps_structure_can_intersect_field, (gpointer) struct2);
3195 gst_caps_structure_is_superset_field (GQuark field_id, const GValue * value,
3198 GstStructure *subset = user_data;
3199 const GValue *other;
3202 if (!(other = gst_structure_id_get_value (subset, field_id)))
3203 /* field is missing in the subset => no subset */
3206 comparison = gst_value_compare (value, other);
3208 /* equal values are subset */
3209 if (comparison == GST_VALUE_EQUAL)
3212 /* ordered, but unequal, values are not */
3213 if (comparison != GST_VALUE_UNORDERED)
3216 return gst_value_is_subset (other, value);
3220 * gst_structure_is_subset:
3221 * @subset: a #GstStructure
3222 * @superset: a potentially greater #GstStructure
3224 * Checks if @subset is a subset of @superset, i.e. has the same
3225 * structure name and for all fields that are existing in @superset,
3226 * @subset has a value that is a subset of the value in @superset.
3228 * Returns: %TRUE if @subset is a subset of @superset
3231 gst_structure_is_subset (const GstStructure * subset,
3232 const GstStructure * superset)
3234 if ((superset->name != subset->name) ||
3235 (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3238 return gst_structure_foreach ((GstStructure *) superset,
3239 gst_caps_structure_is_superset_field, (gpointer) subset);
3244 * gst_structure_fixate:
3245 * @structure: a #GstStructure
3247 * Fixate all values in @structure using gst_value_fixate().
3248 * @structure will be modified in-place and should be writable.
3251 gst_structure_fixate (GstStructure * structure)
3253 g_return_if_fail (GST_IS_STRUCTURE (structure));
3255 gst_structure_foreach (structure, default_fixate, structure);