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 * @title: GstStructure
25 * @short_description: Generic structure containing fields of names and values
26 * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
28 * A #GstStructure is a collection of key/value pairs. The keys are expressed
29 * as GQuarks and the values can be of any GType.
31 * In addition to the key/value pairs, a #GstStructure also has a name. The name
32 * starts with a letter and can be filled by letters, numbers and any of "/-_.:".
34 * #GstStructure is used by various GStreamer subsystems to store information
35 * in a flexible and extensible way. A #GstStructure does not have a refcount
36 * because it usually is part of a higher level object such as #GstCaps,
37 * #GstMessage, #GstEvent, #GstQuery. It provides a means to enforce mutability
38 * using the refcount of the parent with the gst_structure_set_parent_refcount()
41 * A #GstStructure can be created with gst_structure_new_empty() or
42 * gst_structure_new(), which both take a name and an optional set of
43 * key/value pairs along with the types of the values.
45 * Field values can be changed with gst_structure_set_value() or
46 * gst_structure_set().
48 * Field values can be retrieved with gst_structure_get_value() or the more
49 * convenient gst_structure_get_*() functions.
51 * Fields can be removed with gst_structure_remove_field() or
52 * gst_structure_remove_fields().
54 * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are
55 * not allowed. Strings may be %NULL however.
57 * Be aware that the current #GstCaps / #GstStructure serialization into string
58 * has limited support for nested #GstCaps / #GstStructure fields. It can only
59 * support one level of nesting. Using more levels will lead to unexpected
60 * behavior when using serialization features, such as gst_caps_to_string() or
61 * gst_value_serialize() and their counterparts.
70 #include "gst_private.h"
73 #include <gobject/gvaluecollector.h>
75 GST_DEBUG_CATEGORY_STATIC (gst_structure_debug);
76 #define GST_CAT_DEFAULT gst_structure_debug
78 typedef struct _GstStructureField GstStructureField;
80 struct _GstStructureField
90 /* owned by parent structure, NULL if no parent */
91 gint *parent_refcount;
96 #define GST_STRUCTURE_REFCOUNT(s) (((GstStructureImpl*)(s))->parent_refcount)
97 #define GST_STRUCTURE_FIELDS(s) (((GstStructureImpl*)(s))->fields)
99 #define GST_STRUCTURE_FIELD(structure, index) \
100 &g_array_index(GST_STRUCTURE_FIELDS(structure), GstStructureField, (index))
102 #define IS_MUTABLE(structure) \
103 (!GST_STRUCTURE_REFCOUNT(structure) || \
104 g_atomic_int_get (GST_STRUCTURE_REFCOUNT(structure)) == 1)
106 #define IS_TAGLIST(structure) \
107 (structure->name == GST_QUARK (TAGLIST))
109 static void gst_structure_set_field (GstStructure * structure,
110 GstStructureField * field);
111 static GstStructureField *gst_structure_get_field (const GstStructure *
112 structure, const gchar * fieldname);
113 static GstStructureField *gst_structure_id_get_field (const GstStructure *
114 structure, GQuark field);
115 static void gst_structure_transform_to_string (const GValue * src_value,
116 GValue * dest_value);
117 static GstStructure *gst_structure_copy_conditional (const GstStructure *
120 GType _gst_structure_type = 0;
123 G_DEFINE_BOXED_TYPE (GstStructure, gst_structure,
124 gst_structure_copy_conditional, gst_structure_free);
127 _priv_gst_structure_initialize (void)
129 _gst_structure_type = gst_structure_get_type ();
131 g_value_register_transform_func (_gst_structure_type, G_TYPE_STRING,
132 gst_structure_transform_to_string);
134 GST_DEBUG_CATEGORY_INIT (gst_structure_debug, "structure", 0,
135 "GstStructure debug");
138 static GstStructure *
139 gst_structure_new_id_empty_with_size (GQuark quark, guint prealloc)
141 GstStructureImpl *structure;
143 structure = g_slice_new (GstStructureImpl);
144 ((GstStructure *) structure)->type = _gst_structure_type;
145 ((GstStructure *) structure)->name = quark;
146 GST_STRUCTURE_REFCOUNT (structure) = NULL;
147 GST_STRUCTURE_FIELDS (structure) =
148 g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
150 GST_TRACE ("created structure %p", structure);
152 return GST_STRUCTURE_CAST (structure);
156 * gst_structure_new_id_empty:
157 * @quark: name of new structure
159 * Creates a new, empty #GstStructure with the given name as a GQuark.
161 * Free-function: gst_structure_free
163 * Returns: (transfer full): a new, empty #GstStructure
166 gst_structure_new_id_empty (GQuark quark)
168 g_return_val_if_fail (quark != 0, NULL);
170 return gst_structure_new_id_empty_with_size (quark, 0);
173 #ifndef G_DISABLE_CHECKS
175 gst_structure_validate_name (const gchar * name)
179 g_return_val_if_fail (name != NULL, FALSE);
181 if (G_UNLIKELY (!g_ascii_isalpha (*name))) {
182 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
187 /* FIXME: test name string more */
189 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
191 if (G_UNLIKELY (*s != '\0')) {
192 GST_WARNING ("Invalid character '%c' at offset %" G_GUINTPTR_FORMAT " in"
193 " structure name: %s", *s, ((guintptr) s - (guintptr) name), name);
197 if (strncmp (name, "video/x-raw-", 12) == 0) {
198 g_warning ("0.10-style raw video caps are being created. Should be "
199 "video/x-raw,format=(string).. now.");
200 } else if (strncmp (name, "audio/x-raw-", 12) == 0) {
201 g_warning ("0.10-style raw audio caps are being created. Should be "
202 "audio/x-raw,format=(string).. now.");
210 * gst_structure_new_empty:
211 * @name: name of new structure
213 * Creates a new, empty #GstStructure with the given @name.
215 * See gst_structure_set_name() for constraints on the @name parameter.
217 * Free-function: gst_structure_free
219 * Returns: (transfer full): a new, empty #GstStructure
222 gst_structure_new_empty (const gchar * name)
224 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
226 return gst_structure_new_id_empty_with_size (g_quark_from_string (name), 0);
231 * @name: name of new structure
232 * @firstfield: name of first field to set
233 * @...: additional arguments
235 * Creates a new #GstStructure with the given name. Parses the
236 * list of variable arguments and sets fields to the values listed.
237 * Variable arguments should be passed as field name, field type,
238 * and value. Last variable argument should be %NULL.
240 * Free-function: gst_structure_free
242 * Returns: (transfer full): a new #GstStructure
245 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
247 GstStructure *structure;
250 va_start (varargs, firstfield);
251 structure = gst_structure_new_valist (name, firstfield, varargs);
258 * gst_structure_new_valist:
259 * @name: name of new structure
260 * @firstfield: name of first field to set
261 * @varargs: variable argument list
263 * Creates a new #GstStructure with the given @name. Structure fields
264 * are set according to the varargs in a manner similar to
265 * gst_structure_new().
267 * See gst_structure_set_name() for constraints on the @name parameter.
269 * Free-function: gst_structure_free
271 * Returns: (transfer full): a new #GstStructure
274 gst_structure_new_valist (const gchar * name,
275 const gchar * firstfield, va_list varargs)
277 GstStructure *structure;
279 structure = gst_structure_new_empty (name);
282 gst_structure_set_valist (structure, firstfield, varargs);
288 * gst_structure_set_parent_refcount:
289 * @structure: a #GstStructure
290 * @refcount: (in): a pointer to the parent's refcount
292 * Sets the parent_refcount field of #GstStructure. This field is used to
293 * determine whether a structure is mutable or not. This function should only be
294 * called by code implementing parent objects of #GstStructure, as described in
295 * the MT Refcounting section of the design documents.
297 * Returns: %TRUE if the parent refcount could be set.
300 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
302 g_return_val_if_fail (structure != NULL, FALSE);
304 /* if we have a parent_refcount already, we can only clear
305 * if with a NULL refcount */
306 if (GST_STRUCTURE_REFCOUNT (structure)) {
307 if (refcount != NULL) {
308 g_return_val_if_fail (refcount == NULL, FALSE);
312 if (refcount == NULL) {
313 g_return_val_if_fail (refcount != NULL, FALSE);
318 GST_STRUCTURE_REFCOUNT (structure) = refcount;
324 * gst_structure_copy:
325 * @structure: a #GstStructure to duplicate
327 * Duplicates a #GstStructure and all its fields and values.
329 * Free-function: gst_structure_free
331 * Returns: (transfer full): a new #GstStructure.
334 gst_structure_copy (const GstStructure * structure)
336 GstStructure *new_structure;
337 GstStructureField *field;
340 g_return_val_if_fail (structure != NULL, NULL);
342 len = GST_STRUCTURE_FIELDS (structure)->len;
343 new_structure = gst_structure_new_id_empty_with_size (structure->name, len);
345 for (i = 0; i < len; i++) {
346 GstStructureField new_field = { 0 };
348 field = GST_STRUCTURE_FIELD (structure, i);
350 new_field.name = field->name;
351 gst_value_init_and_copy (&new_field.value, &field->value);
352 g_array_append_val (GST_STRUCTURE_FIELDS (new_structure), new_field);
354 GST_CAT_TRACE (GST_CAT_PERFORMANCE, "doing copy %p -> %p",
355 structure, new_structure);
357 return new_structure;
361 * gst_structure_free:
362 * @structure: (in) (transfer full): the #GstStructure to free
364 * Frees a #GstStructure and all its fields and values. The structure must not
365 * have a parent when this function is called.
368 gst_structure_free (GstStructure * structure)
370 GstStructureField *field;
373 g_return_if_fail (structure != NULL);
374 g_return_if_fail (GST_STRUCTURE_REFCOUNT (structure) == NULL);
376 len = GST_STRUCTURE_FIELDS (structure)->len;
377 for (i = 0; i < len; i++) {
378 field = GST_STRUCTURE_FIELD (structure, i);
380 if (G_IS_VALUE (&field->value)) {
381 g_value_unset (&field->value);
384 g_array_free (GST_STRUCTURE_FIELDS (structure), TRUE);
386 memset (structure, 0xff, sizeof (GstStructure));
388 GST_TRACE ("free structure %p", structure);
390 g_slice_free1 (sizeof (GstStructureImpl), structure);
394 * gst_structure_get_name:
395 * @structure: a #GstStructure
397 * Get the name of @structure as a string.
399 * Returns: the name of the structure.
402 gst_structure_get_name (const GstStructure * structure)
404 g_return_val_if_fail (structure != NULL, NULL);
406 return g_quark_to_string (structure->name);
410 * gst_structure_has_name:
411 * @structure: a #GstStructure
412 * @name: structure name to check for
414 * Checks if the structure has the given name
416 * Returns: %TRUE if @name matches the name of the structure.
419 gst_structure_has_name (const GstStructure * structure, const gchar * name)
421 const gchar *structure_name;
423 g_return_val_if_fail (structure != NULL, FALSE);
424 g_return_val_if_fail (name != NULL, FALSE);
426 /* getting the string is cheap and comparing short strings is too
427 * should be faster than getting the quark for name and comparing the quarks
429 structure_name = g_quark_to_string (structure->name);
431 return (structure_name && strcmp (structure_name, name) == 0);
435 * gst_structure_get_name_id:
436 * @structure: a #GstStructure
438 * Get the name of @structure as a GQuark.
440 * Returns: the quark representing the name of the structure.
443 gst_structure_get_name_id (const GstStructure * structure)
445 g_return_val_if_fail (structure != NULL, 0);
447 return structure->name;
451 * gst_structure_set_name:
452 * @structure: a #GstStructure
453 * @name: the new name of the structure
455 * Sets the name of the structure to the given @name. The string
456 * provided is copied before being used. It must not be empty, start with a
457 * letter and can be followed by letters, numbers and any of "/-_.:".
460 gst_structure_set_name (GstStructure * structure, const gchar * name)
462 g_return_if_fail (structure != NULL);
463 g_return_if_fail (IS_MUTABLE (structure));
464 g_return_if_fail (gst_structure_validate_name (name));
466 structure->name = g_quark_from_string (name);
470 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
471 const GValue * value)
473 GstStructureField gsfield = { 0, {0,} };
475 gsfield.name = field;
476 gst_value_init_and_copy (&gsfield.value, value);
478 gst_structure_set_field (structure, &gsfield);
482 * gst_structure_id_set_value:
483 * @structure: a #GstStructure
484 * @field: a #GQuark representing a field
485 * @value: the new value of the field
487 * Sets the field with the given GQuark @field to @value. If the field
488 * does not exist, it is created. If the field exists, the previous
489 * value is replaced and freed.
492 gst_structure_id_set_value (GstStructure * structure,
493 GQuark field, const GValue * value)
496 g_return_if_fail (structure != NULL);
497 g_return_if_fail (G_IS_VALUE (value));
498 g_return_if_fail (IS_MUTABLE (structure));
500 gst_structure_id_set_value_internal (structure, field, value);
504 * gst_structure_set_value:
505 * @structure: a #GstStructure
506 * @fieldname: the name of the field to set
507 * @value: the new value of the field
509 * Sets the field with the given name @field to @value. If the field
510 * does not exist, it is created. If the field exists, the previous
511 * value is replaced and freed.
514 gst_structure_set_value (GstStructure * structure,
515 const gchar * fieldname, const GValue * value)
517 g_return_if_fail (structure != NULL);
518 g_return_if_fail (fieldname != NULL);
519 g_return_if_fail (G_IS_VALUE (value));
520 g_return_if_fail (IS_MUTABLE (structure));
522 gst_structure_id_set_value_internal (structure,
523 g_quark_from_string (fieldname), value);
527 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
530 GstStructureField gsfield = { 0, {0,} };
532 gsfield.name = field;
533 gsfield.value = *value;
535 gst_structure_set_field (structure, &gsfield);
537 /* we took ownership */
539 memset (value, 0, sizeof (GValue));
541 value->g_type = G_TYPE_INVALID;
546 * gst_structure_id_take_value:
547 * @structure: a #GstStructure
548 * @field: a #GQuark representing a field
549 * @value: (transfer full): the new value of the field
551 * Sets the field with the given GQuark @field to @value. If the field
552 * does not exist, it is created. If the field exists, the previous
553 * value is replaced and freed.
556 gst_structure_id_take_value (GstStructure * structure, GQuark field,
559 g_return_if_fail (structure != NULL);
560 g_return_if_fail (G_IS_VALUE (value));
561 g_return_if_fail (IS_MUTABLE (structure));
563 gst_structure_id_take_value_internal (structure, field, value);
567 * gst_structure_take_value:
568 * @structure: a #GstStructure
569 * @fieldname: the name of the field to set
570 * @value: (transfer full): the new value of the field
572 * Sets the field with the given name @field to @value. If the field
573 * does not exist, it is created. If the field exists, the previous
574 * value is replaced and freed. The function will take ownership of @value.
577 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
580 g_return_if_fail (structure != NULL);
581 g_return_if_fail (fieldname != NULL);
582 g_return_if_fail (G_IS_VALUE (value));
583 g_return_if_fail (IS_MUTABLE (structure));
585 gst_structure_id_take_value_internal (structure,
586 g_quark_from_string (fieldname), value);
590 gst_structure_set_valist_internal (GstStructure * structure,
591 const gchar * fieldname, va_list varargs)
597 GstStructureField field = { 0 };
599 field.name = g_quark_from_string (fieldname);
601 type = va_arg (varargs, GType);
603 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
604 if (G_UNLIKELY (err)) {
605 g_critical ("%s", err);
608 gst_structure_set_field (structure, &field);
610 fieldname = va_arg (varargs, gchar *);
616 * @structure: a #GstStructure
617 * @fieldname: the name of the field to set
618 * @...: variable arguments
620 * Parses the variable arguments and sets fields accordingly. Fields that
621 * weren't already part of the structure are added as needed.
622 * Variable arguments should be in the form field name, field type
623 * (as a GType), value(s). The last variable argument should be %NULL.
626 gst_structure_set (GstStructure * structure, const gchar * field, ...)
630 g_return_if_fail (structure != NULL);
631 g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
633 va_start (varargs, field);
634 gst_structure_set_valist_internal (structure, field, varargs);
639 * gst_structure_set_valist:
640 * @structure: a #GstStructure
641 * @fieldname: the name of the field to set
642 * @varargs: variable arguments
644 * va_list form of gst_structure_set().
647 gst_structure_set_valist (GstStructure * structure,
648 const gchar * fieldname, va_list varargs)
650 g_return_if_fail (structure != NULL);
651 g_return_if_fail (IS_MUTABLE (structure));
653 gst_structure_set_valist_internal (structure, fieldname, varargs);
657 gst_structure_id_set_valist_internal (GstStructure * structure,
658 GQuark fieldname, va_list varargs)
664 GstStructureField field = { 0 };
666 field.name = fieldname;
667 type = va_arg (varargs, GType);
669 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.
693 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
697 g_return_if_fail (structure != NULL);
699 va_start (varargs, field);
700 gst_structure_id_set_valist_internal (structure, field, varargs);
705 * gst_structure_id_set_valist:
706 * @structure: a #GstStructure
707 * @fieldname: the name of the field to set
708 * @varargs: variable arguments
710 * va_list form of gst_structure_id_set().
713 gst_structure_id_set_valist (GstStructure * structure,
714 GQuark fieldname, va_list varargs)
716 g_return_if_fail (structure != NULL);
717 g_return_if_fail (IS_MUTABLE (structure));
719 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
723 * gst_structure_new_id:
724 * @name_quark: name of new structure
725 * @field_quark: the GQuark for the name of the field to set
726 * @...: variable arguments
728 * Creates a new #GstStructure with the given name as a GQuark, followed by
729 * fieldname quark, GType, argument(s) "triplets" in the same format as
730 * gst_structure_id_set(). Basically a convenience wrapper around
731 * gst_structure_new_id_empty() and gst_structure_id_set().
733 * The last variable argument must be %NULL (or 0).
735 * Free-function: gst_structure_free
737 * Returns: (transfer full): a new #GstStructure
740 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
745 g_return_val_if_fail (name_quark != 0, NULL);
746 g_return_val_if_fail (field_quark != 0, NULL);
748 s = gst_structure_new_id_empty (name_quark);
750 va_start (varargs, field_quark);
751 gst_structure_id_set_valist_internal (s, field_quark, varargs);
757 #if GST_VERSION_NANO == 1
758 #define GIT_G_WARNING g_warning
760 #define GIT_G_WARNING GST_WARNING
763 /* If the structure currently contains a field with the same name, it is
764 * replaced with the provided field. Otherwise, the field is added to the
765 * structure. The field's value is not deeply copied.
768 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
770 GstStructureField *f;
771 GType field_value_type;
774 len = GST_STRUCTURE_FIELDS (structure)->len;
776 field_value_type = G_VALUE_TYPE (&field->value);
777 if (field_value_type == G_TYPE_STRING) {
780 s = g_value_get_string (&field->value);
781 /* only check for NULL strings in taglists, as they are allowed in message
782 * structs, e.g. error message debug strings */
783 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
785 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
786 "Please file a bug.", g_quark_to_string (field->name));
787 g_value_unset (&field->value);
790 /* empty strings never make sense */
791 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
792 "Please file a bug.", g_quark_to_string (field->name));
793 g_value_unset (&field->value);
796 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
797 g_warning ("Trying to set string on %s field '%s', but string is not "
798 "valid UTF-8. Please file a bug.",
799 IS_TAGLIST (structure) ? "taglist" : "structure",
800 g_quark_to_string (field->name));
801 g_value_unset (&field->value);
804 } else if (G_UNLIKELY (field_value_type == G_TYPE_DATE)) {
807 d = g_value_get_boxed (&field->value);
808 /* only check for NULL GDates in taglists, as they might make sense
809 * in other, generic structs */
810 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
811 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
812 "Please file a bug.", g_quark_to_string (field->name));
813 g_value_unset (&field->value);
815 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
817 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
818 IS_TAGLIST (structure) ? "taglist" : "structure",
819 g_quark_to_string (field->name));
820 g_value_unset (&field->value);
825 for (i = 0; i < len; i++) {
826 f = GST_STRUCTURE_FIELD (structure, i);
828 if (G_UNLIKELY (f->name == field->name)) {
829 g_value_unset (&f->value);
830 memcpy (f, field, sizeof (GstStructureField));
835 g_array_append_val (GST_STRUCTURE_FIELDS (structure), *field);
838 /* If there is no field with the given ID, NULL is returned.
840 static GstStructureField *
841 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
843 GstStructureField *field;
846 len = GST_STRUCTURE_FIELDS (structure)->len;
848 for (i = 0; i < len; i++) {
849 field = GST_STRUCTURE_FIELD (structure, i);
851 if (G_UNLIKELY (field->name == field_id))
858 /* If there is no field with the given ID, NULL is returned.
860 static GstStructureField *
861 gst_structure_get_field (const GstStructure * structure,
862 const gchar * fieldname)
864 g_return_val_if_fail (structure != NULL, NULL);
865 g_return_val_if_fail (fieldname != NULL, NULL);
867 return gst_structure_id_get_field (structure,
868 g_quark_from_string (fieldname));
872 * gst_structure_get_value:
873 * @structure: a #GstStructure
874 * @fieldname: the name of the field to get
876 * Get the value of the field with name @fieldname.
878 * Returns: the #GValue corresponding to the field with the given name.
881 gst_structure_get_value (const GstStructure * structure,
882 const gchar * fieldname)
884 GstStructureField *field;
886 g_return_val_if_fail (structure != NULL, NULL);
887 g_return_val_if_fail (fieldname != NULL, NULL);
889 field = gst_structure_get_field (structure, fieldname);
893 return &field->value;
897 * gst_structure_id_get_value:
898 * @structure: a #GstStructure
899 * @field: the #GQuark of the field to get
901 * Get the value of the field with GQuark @field.
903 * Returns: the #GValue corresponding to the field with the given name
907 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
909 GstStructureField *gsfield;
911 g_return_val_if_fail (structure != NULL, NULL);
913 gsfield = gst_structure_id_get_field (structure, field);
917 return &gsfield->value;
921 * gst_structure_remove_field:
922 * @structure: a #GstStructure
923 * @fieldname: the name of the field to remove
925 * Removes the field with the given name. If the field with the given
926 * name does not exist, the structure is unchanged.
929 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
931 GstStructureField *field;
935 g_return_if_fail (structure != NULL);
936 g_return_if_fail (fieldname != NULL);
937 g_return_if_fail (IS_MUTABLE (structure));
939 id = g_quark_from_string (fieldname);
940 len = GST_STRUCTURE_FIELDS (structure)->len;
942 for (i = 0; i < len; i++) {
943 field = GST_STRUCTURE_FIELD (structure, i);
945 if (field->name == id) {
946 if (G_IS_VALUE (&field->value)) {
947 g_value_unset (&field->value);
949 GST_STRUCTURE_FIELDS (structure) =
950 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
957 * gst_structure_remove_fields:
958 * @structure: a #GstStructure
959 * @fieldname: the name of the field to remove
960 * @...: %NULL-terminated list of more fieldnames to remove
962 * Removes the fields with the given names. If a field does not exist, the
963 * argument is ignored.
966 gst_structure_remove_fields (GstStructure * structure,
967 const gchar * fieldname, ...)
971 g_return_if_fail (structure != NULL);
972 g_return_if_fail (fieldname != NULL);
973 /* mutability checked in remove_field */
975 va_start (varargs, fieldname);
976 gst_structure_remove_fields_valist (structure, fieldname, varargs);
981 * gst_structure_remove_fields_valist:
982 * @structure: a #GstStructure
983 * @fieldname: the name of the field to remove
984 * @varargs: %NULL-terminated list of more fieldnames to remove
986 * va_list form of gst_structure_remove_fields().
989 gst_structure_remove_fields_valist (GstStructure * structure,
990 const gchar * fieldname, va_list varargs)
992 gchar *field = (gchar *) fieldname;
994 g_return_if_fail (structure != NULL);
995 g_return_if_fail (fieldname != NULL);
996 /* mutability checked in remove_field */
999 gst_structure_remove_field (structure, field);
1000 field = va_arg (varargs, char *);
1005 * gst_structure_remove_all_fields:
1006 * @structure: a #GstStructure
1008 * Removes all fields in a GstStructure.
1011 gst_structure_remove_all_fields (GstStructure * structure)
1013 GstStructureField *field;
1016 g_return_if_fail (structure != NULL);
1017 g_return_if_fail (IS_MUTABLE (structure));
1019 for (i = GST_STRUCTURE_FIELDS (structure)->len - 1; i >= 0; i--) {
1020 field = GST_STRUCTURE_FIELD (structure, i);
1022 if (G_IS_VALUE (&field->value)) {
1023 g_value_unset (&field->value);
1025 GST_STRUCTURE_FIELDS (structure) =
1026 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1031 * gst_structure_get_field_type:
1032 * @structure: a #GstStructure
1033 * @fieldname: the name of the field
1035 * Finds the field with the given name, and returns the type of the
1036 * value it contains. If the field is not found, G_TYPE_INVALID is
1039 * Returns: the #GValue of the field
1042 gst_structure_get_field_type (const GstStructure * structure,
1043 const gchar * fieldname)
1045 GstStructureField *field;
1047 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1048 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1050 field = gst_structure_get_field (structure, fieldname);
1052 return G_TYPE_INVALID;
1054 return G_VALUE_TYPE (&field->value);
1058 * gst_structure_n_fields:
1059 * @structure: a #GstStructure
1061 * Get the number of fields in the structure.
1063 * Returns: the number of fields in the structure
1066 gst_structure_n_fields (const GstStructure * structure)
1068 g_return_val_if_fail (structure != NULL, 0);
1070 return GST_STRUCTURE_FIELDS (structure)->len;
1074 * gst_structure_nth_field_name:
1075 * @structure: a #GstStructure
1076 * @index: the index to get the name of
1078 * Get the name of the given field number, counting from 0 onwards.
1080 * Returns: the name of the given field number
1083 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1085 GstStructureField *field;
1087 g_return_val_if_fail (structure != NULL, NULL);
1088 g_return_val_if_fail (index < GST_STRUCTURE_FIELDS (structure)->len, NULL);
1090 field = GST_STRUCTURE_FIELD (structure, index);
1092 return g_quark_to_string (field->name);
1096 * gst_structure_foreach:
1097 * @structure: a #GstStructure
1098 * @func: (scope call): a function to call for each field
1099 * @user_data: (closure): private data
1101 * Calls the provided function once for each field in the #GstStructure. The
1102 * function must not modify the fields. Also see gst_structure_map_in_place()
1103 * and gst_structure_filter_and_map_in_place().
1105 * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
1109 gst_structure_foreach (const GstStructure * structure,
1110 GstStructureForeachFunc func, gpointer user_data)
1113 GstStructureField *field;
1116 g_return_val_if_fail (structure != NULL, FALSE);
1117 g_return_val_if_fail (func != NULL, FALSE);
1119 len = GST_STRUCTURE_FIELDS (structure)->len;
1121 for (i = 0; i < len; i++) {
1122 field = GST_STRUCTURE_FIELD (structure, i);
1124 ret = func (field->name, &field->value, user_data);
1125 if (G_UNLIKELY (!ret))
1133 * gst_structure_map_in_place:
1134 * @structure: a #GstStructure
1135 * @func: (scope call): a function to call for each field
1136 * @user_data: (closure): private data
1138 * Calls the provided function once for each field in the #GstStructure. In
1139 * contrast to gst_structure_foreach(), the function may modify but not delete the
1140 * fields. The structure must be mutable.
1142 * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
1146 gst_structure_map_in_place (GstStructure * structure,
1147 GstStructureMapFunc func, gpointer user_data)
1150 GstStructureField *field;
1153 g_return_val_if_fail (structure != NULL, FALSE);
1154 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1155 g_return_val_if_fail (func != NULL, FALSE);
1156 len = GST_STRUCTURE_FIELDS (structure)->len;
1158 for (i = 0; i < len; i++) {
1159 field = GST_STRUCTURE_FIELD (structure, i);
1161 ret = func (field->name, &field->value, user_data);
1170 * gst_structure_filter_and_map_in_place:
1171 * @structure: a #GstStructure
1172 * @func: (scope call): a function to call for each field
1173 * @user_data: (closure): private data
1175 * Calls the provided function once for each field in the #GstStructure. In
1176 * contrast to gst_structure_foreach(), the function may modify the fields.
1177 * In contrast to gst_structure_map_in_place(), the field is removed from
1178 * the structure if %FALSE is returned from the function.
1179 * The structure must be mutable.
1184 gst_structure_filter_and_map_in_place (GstStructure * structure,
1185 GstStructureFilterMapFunc func, gpointer user_data)
1188 GstStructureField *field;
1191 g_return_if_fail (structure != NULL);
1192 g_return_if_fail (IS_MUTABLE (structure));
1193 g_return_if_fail (func != NULL);
1194 len = GST_STRUCTURE_FIELDS (structure)->len;
1196 for (i = 0; i < len;) {
1197 field = GST_STRUCTURE_FIELD (structure, i);
1199 ret = func (field->name, &field->value, user_data);
1202 if (G_IS_VALUE (&field->value)) {
1203 g_value_unset (&field->value);
1205 GST_STRUCTURE_FIELDS (structure) =
1206 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1207 len = GST_STRUCTURE_FIELDS (structure)->len;
1215 * gst_structure_id_has_field:
1216 * @structure: a #GstStructure
1217 * @field: #GQuark of the field name
1219 * Check if @structure contains a field named @field.
1221 * Returns: %TRUE if the structure contains a field with the given name
1224 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1226 GstStructureField *f;
1228 g_return_val_if_fail (structure != NULL, FALSE);
1229 g_return_val_if_fail (field != 0, FALSE);
1231 f = gst_structure_id_get_field (structure, field);
1237 * gst_structure_has_field:
1238 * @structure: a #GstStructure
1239 * @fieldname: the name of a field
1241 * Check if @structure contains a field named @fieldname.
1243 * Returns: %TRUE if the structure contains a field with the given name
1246 gst_structure_has_field (const GstStructure * structure,
1247 const gchar * fieldname)
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 (structure,
1253 g_quark_from_string (fieldname));
1257 * gst_structure_id_has_field_typed:
1258 * @structure: a #GstStructure
1259 * @field: #GQuark of the field name
1260 * @type: the type of a value
1262 * Check if @structure contains a field named @field and with GType @type.
1264 * Returns: %TRUE if the structure contains a field with the given name and type
1267 gst_structure_id_has_field_typed (const GstStructure * structure,
1268 GQuark field, GType type)
1270 GstStructureField *f;
1272 g_return_val_if_fail (structure != NULL, FALSE);
1273 g_return_val_if_fail (field != 0, FALSE);
1275 f = gst_structure_id_get_field (structure, field);
1279 return (G_VALUE_TYPE (&f->value) == type);
1283 * gst_structure_has_field_typed:
1284 * @structure: a #GstStructure
1285 * @fieldname: the name of a field
1286 * @type: the type of a value
1288 * Check if @structure contains a field named @fieldname and with GType @type.
1290 * Returns: %TRUE if the structure contains a field with the given name and type
1293 gst_structure_has_field_typed (const GstStructure * structure,
1294 const gchar * fieldname, GType type)
1296 g_return_val_if_fail (structure != NULL, FALSE);
1297 g_return_val_if_fail (fieldname != NULL, FALSE);
1299 return gst_structure_id_has_field_typed (structure,
1300 g_quark_from_string (fieldname), type);
1303 /* utility functions */
1306 * gst_structure_get_boolean:
1307 * @structure: a #GstStructure
1308 * @fieldname: the name of a field
1309 * @value: (out): a pointer to a #gboolean to set
1311 * Sets the boolean pointed to by @value corresponding to the value of the
1312 * given field. Caller is responsible for making sure the field exists
1313 * and has the correct type.
1315 * Returns: %TRUE if the value could be set correctly. If there was no field
1316 * with @fieldname or the existing field did not contain a boolean, this
1317 * function returns %FALSE.
1320 gst_structure_get_boolean (const GstStructure * structure,
1321 const gchar * fieldname, gboolean * value)
1323 GstStructureField *field;
1325 g_return_val_if_fail (structure != NULL, FALSE);
1326 g_return_val_if_fail (fieldname != NULL, FALSE);
1328 field = gst_structure_get_field (structure, fieldname);
1330 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_BOOLEAN)
1333 *value = gst_g_value_get_boolean_unchecked (&field->value);
1339 * gst_structure_get_int:
1340 * @structure: a #GstStructure
1341 * @fieldname: the name of a field
1342 * @value: (out): a pointer to an int to set
1344 * Sets the int pointed to by @value corresponding to the value of the
1345 * given field. Caller is responsible for making sure the field exists
1346 * and has the correct type.
1348 * Returns: %TRUE if the value could be set correctly. If there was no field
1349 * with @fieldname or the existing field did not contain an int, this function
1353 gst_structure_get_int (const GstStructure * structure,
1354 const gchar * fieldname, gint * 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);
1364 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT)
1367 *value = gst_g_value_get_int_unchecked (&field->value);
1373 * gst_structure_get_uint:
1374 * @structure: a #GstStructure
1375 * @fieldname: the name of a field
1376 * @value: (out): a pointer to a uint to set
1378 * Sets the uint pointed to by @value corresponding to the value of the
1379 * given field. Caller is responsible for making sure the field exists
1380 * and has the correct type.
1382 * Returns: %TRUE if the value could be set correctly. If there was no field
1383 * with @fieldname or the existing field did not contain a uint, this function
1387 gst_structure_get_uint (const GstStructure * structure,
1388 const gchar * fieldname, guint * value)
1390 GstStructureField *field;
1392 g_return_val_if_fail (structure != NULL, FALSE);
1393 g_return_val_if_fail (fieldname != NULL, FALSE);
1394 g_return_val_if_fail (value != NULL, FALSE);
1396 field = gst_structure_get_field (structure, fieldname);
1398 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT)
1401 *value = gst_g_value_get_uint_unchecked (&field->value);
1407 * gst_structure_get_int64:
1408 * @structure: a #GstStructure
1409 * @fieldname: the name of a field
1410 * @value: (out): a pointer to a #gint64 to set
1412 * Sets the #gint64 pointed to by @value corresponding to the value of the
1413 * given field. Caller is responsible for making sure the field exists
1414 * and has the correct type.
1416 * Returns: %TRUE if the value could be set correctly. If there was no field
1417 * with @fieldname or the existing field did not contain a #gint64, this function
1423 gst_structure_get_int64 (const GstStructure * structure,
1424 const gchar * fieldname, gint64 * value)
1426 GstStructureField *field;
1428 g_return_val_if_fail (structure != NULL, FALSE);
1429 g_return_val_if_fail (fieldname != NULL, FALSE);
1430 g_return_val_if_fail (value != NULL, FALSE);
1432 field = gst_structure_get_field (structure, fieldname);
1434 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT64)
1437 *value = gst_g_value_get_int64_unchecked (&field->value);
1443 * gst_structure_get_uint64:
1444 * @structure: a #GstStructure
1445 * @fieldname: the name of a field
1446 * @value: (out): a pointer to a #guint64 to set
1448 * Sets the #guint64 pointed to by @value corresponding to the value of the
1449 * given field. Caller is responsible for making sure the field exists
1450 * and has the correct type.
1452 * Returns: %TRUE if the value could be set correctly. If there was no field
1453 * with @fieldname or the existing field did not contain a #guint64, this function
1459 gst_structure_get_uint64 (const GstStructure * structure,
1460 const gchar * fieldname, guint64 * value)
1462 GstStructureField *field;
1464 g_return_val_if_fail (structure != NULL, FALSE);
1465 g_return_val_if_fail (fieldname != NULL, FALSE);
1466 g_return_val_if_fail (value != NULL, FALSE);
1468 field = gst_structure_get_field (structure, fieldname);
1470 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT64)
1473 *value = gst_g_value_get_uint64_unchecked (&field->value);
1479 * gst_structure_get_date:
1480 * @structure: a #GstStructure
1481 * @fieldname: the name of a field
1482 * @value: (out callee-allocates): a pointer to a #GDate to set
1484 * Sets the date pointed to by @value corresponding to the date of the
1485 * given field. Caller is responsible for making sure the field exists
1486 * and has the correct type.
1488 * On success @value will point to a newly-allocated copy of the date which
1489 * should be freed with g_date_free() when no longer needed (note: this is
1490 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1491 * copy of the string).
1493 * Returns: %TRUE if the value could be set correctly. If there was no field
1494 * with @fieldname or the existing field did not contain a data, this function
1498 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1501 GstStructureField *field;
1503 g_return_val_if_fail (structure != NULL, FALSE);
1504 g_return_val_if_fail (fieldname != NULL, FALSE);
1505 g_return_val_if_fail (value != NULL, FALSE);
1507 field = gst_structure_get_field (structure, fieldname);
1509 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DATE)
1512 /* FIXME: 2.0 g_value_dup_boxed() -> g_value_get_boxed() */
1513 *value = g_value_dup_boxed (&field->value);
1519 * gst_structure_get_date_time:
1520 * @structure: a #GstStructure
1521 * @fieldname: the name of a field
1522 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1524 * Sets the datetime pointed to by @value corresponding to the datetime of the
1525 * given field. Caller is responsible for making sure the field exists
1526 * and has the correct type.
1528 * On success @value will point to a reference of the datetime which
1529 * should be unreffed with gst_date_time_unref() when no longer needed
1530 * (note: this is inconsistent with e.g. gst_structure_get_string()
1531 * which doesn't return a copy of the string).
1533 * Returns: %TRUE if the value could be set correctly. If there was no field
1534 * with @fieldname or the existing field did not contain a data, this function
1538 gst_structure_get_date_time (const GstStructure * structure,
1539 const gchar * fieldname, GstDateTime ** value)
1541 GstStructureField *field;
1543 g_return_val_if_fail (structure != NULL, FALSE);
1544 g_return_val_if_fail (fieldname != NULL, FALSE);
1545 g_return_val_if_fail (value != NULL, FALSE);
1547 field = gst_structure_get_field (structure, fieldname);
1551 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1554 /* FIXME 2.0: g_value_dup_boxed() -> g_value_get_boxed() */
1555 *value = g_value_dup_boxed (&field->value);
1561 * gst_structure_get_clock_time:
1562 * @structure: a #GstStructure
1563 * @fieldname: the name of a field
1564 * @value: (out): a pointer to a #GstClockTime to set
1566 * Sets the clock time pointed to by @value corresponding to the clock time
1567 * of the given field. Caller is responsible for making sure the field exists
1568 * and has the correct type.
1570 * Returns: %TRUE if the value could be set correctly. If there was no field
1571 * with @fieldname or the existing field did not contain a #GstClockTime, this
1572 * function returns %FALSE.
1575 gst_structure_get_clock_time (const GstStructure * structure,
1576 const gchar * fieldname, GstClockTime * value)
1578 return gst_structure_get_uint64 (structure, fieldname, value);
1582 * gst_structure_get_double:
1583 * @structure: a #GstStructure
1584 * @fieldname: the name of a field
1585 * @value: (out): a pointer to a gdouble to set
1587 * Sets the double pointed to by @value corresponding to the value of the
1588 * given field. Caller is responsible for making sure the field exists
1589 * and has the correct type.
1591 * Returns: %TRUE if the value could be set correctly. If there was no field
1592 * with @fieldname or the existing field did not contain a double, this
1593 * function returns %FALSE.
1596 gst_structure_get_double (const GstStructure * structure,
1597 const gchar * fieldname, gdouble * value)
1599 GstStructureField *field;
1601 g_return_val_if_fail (structure != NULL, FALSE);
1602 g_return_val_if_fail (fieldname != NULL, FALSE);
1603 g_return_val_if_fail (value != NULL, FALSE);
1605 field = gst_structure_get_field (structure, fieldname);
1607 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DOUBLE)
1610 *value = gst_g_value_get_double_unchecked (&field->value);
1616 * gst_structure_get_string:
1617 * @structure: a #GstStructure
1618 * @fieldname: the name of a field
1620 * Finds the field corresponding to @fieldname, and returns the string
1621 * contained in the field's value. Caller is responsible for making
1622 * sure the field exists and has the correct type.
1624 * The string should not be modified, and remains valid until the next
1625 * call to a gst_structure_*() function with the given structure.
1627 * Returns: (nullable): a pointer to the string or %NULL when the
1628 * field did not exist or did not contain a string.
1631 gst_structure_get_string (const GstStructure * structure,
1632 const gchar * fieldname)
1634 GstStructureField *field;
1636 g_return_val_if_fail (structure != NULL, NULL);
1637 g_return_val_if_fail (fieldname != NULL, NULL);
1639 field = gst_structure_get_field (structure, fieldname);
1641 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_STRING)
1644 return gst_g_value_get_string_unchecked (&field->value);
1648 * gst_structure_get_enum:
1649 * @structure: a #GstStructure
1650 * @fieldname: the name of a field
1651 * @enumtype: the enum type of a field
1652 * @value: (out): a pointer to an int to set
1654 * Sets the int pointed to by @value corresponding to the value of the
1655 * given field. Caller is responsible for making sure the field exists,
1656 * has the correct type and that the enumtype is correct.
1658 * Returns: %TRUE if the value could be set correctly. If there was no field
1659 * with @fieldname or the existing field did not contain an enum of the given
1660 * type, this function returns %FALSE.
1663 gst_structure_get_enum (const GstStructure * structure,
1664 const gchar * fieldname, GType enumtype, gint * value)
1666 GstStructureField *field;
1668 g_return_val_if_fail (structure != NULL, FALSE);
1669 g_return_val_if_fail (fieldname != NULL, FALSE);
1670 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1671 g_return_val_if_fail (value != NULL, FALSE);
1673 field = gst_structure_get_field (structure, fieldname);
1677 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1680 *value = g_value_get_enum (&field->value);
1686 * gst_structure_get_fraction:
1687 * @structure: a #GstStructure
1688 * @fieldname: the name of a field
1689 * @value_numerator: (out): a pointer to an int to set
1690 * @value_denominator: (out): a pointer to an int to set
1692 * Sets the integers pointed to by @value_numerator and @value_denominator
1693 * corresponding to the value of the given field. Caller is responsible
1694 * for making sure the field exists and has the correct type.
1696 * Returns: %TRUE if the values could be set correctly. If there was no field
1697 * with @fieldname or the existing field did not contain a GstFraction, this
1698 * function returns %FALSE.
1701 gst_structure_get_fraction (const GstStructure * structure,
1702 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1704 GstStructureField *field;
1706 g_return_val_if_fail (structure != NULL, FALSE);
1707 g_return_val_if_fail (fieldname != NULL, FALSE);
1708 g_return_val_if_fail (value_numerator != NULL, FALSE);
1709 g_return_val_if_fail (value_denominator != NULL, FALSE);
1711 field = gst_structure_get_field (structure, fieldname);
1713 if (field == NULL || G_VALUE_TYPE (&field->value) != GST_TYPE_FRACTION)
1716 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1717 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1723 * gst_structure_get_flagset:
1724 * @structure: a #GstStructure
1725 * @fieldname: the name of a field
1726 * @value_flags: (out) (allow-none): a pointer to a guint for the flags field
1727 * @value_mask: (out) (allow-none): a pointer to a guint for the mask field
1729 * Read the GstFlagSet flags and mask out of the structure into the
1730 * provided pointers.
1732 * Returns: %TRUE if the values could be set correctly. If there was no field
1733 * with @fieldname or the existing field did not contain a GstFlagSet, this
1734 * function returns %FALSE.
1739 gst_structure_get_flagset (const GstStructure * structure,
1740 const gchar * fieldname, guint * value_flags, guint * value_mask)
1742 GstStructureField *field;
1744 g_return_val_if_fail (structure != NULL, FALSE);
1745 g_return_val_if_fail (fieldname != NULL, FALSE);
1747 field = gst_structure_get_field (structure, fieldname);
1749 if (field == NULL || !GST_VALUE_HOLDS_FLAG_SET (&field->value))
1753 *value_flags = gst_value_get_flagset_flags (&field->value);
1755 *value_mask = gst_value_get_flagset_mask (&field->value);
1761 gst_structure_value_get_generic_type (const GValue * val)
1763 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1764 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1765 GArray *array = g_value_peek_pointer (val);
1767 if (array->len > 0) {
1768 GValue *value = &g_array_index (array, GValue, 0);
1770 return gst_structure_value_get_generic_type (value);
1774 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1776 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1777 return G_TYPE_INT64;
1778 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1779 return G_TYPE_DOUBLE;
1780 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1781 return GST_TYPE_FRACTION;
1783 return G_VALUE_TYPE (val);
1787 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1790 GstStructureField *field;
1793 g_return_val_if_fail (s != NULL, FALSE);
1795 len = GST_STRUCTURE_FIELDS (structure)->len;
1796 for (i = 0; i < len; i++) {
1800 field = GST_STRUCTURE_FIELD (structure, i);
1802 t = gst_value_serialize (&field->value);
1803 type = gst_structure_value_get_generic_type (&field->value);
1805 g_string_append_len (s, ", ", 2);
1806 /* FIXME: do we need to escape fieldnames? */
1807 g_string_append (s, g_quark_to_string (field->name));
1808 g_string_append_len (s, "=(", 2);
1809 g_string_append (s, _priv_gst_value_gtype_to_abbr (type));
1810 g_string_append_c (s, ')');
1812 g_string_append (s, t);
1815 GST_WARNING ("No value transform to serialize field '%s' of type '%s'",
1816 g_quark_to_string (field->name),
1817 _priv_gst_value_gtype_to_abbr (type));
1818 g_string_append (s, "NULL");
1822 g_string_append_c (s, ';');
1827 priv__gst_structure_append_template_to_gstring (GQuark field_id,
1828 const GValue * value, gpointer user_data)
1830 GType type = gst_structure_value_get_generic_type (value);
1831 GString *s = (GString *) user_data;
1833 g_string_append_len (s, ", ", 2);
1834 /* FIXME: do we need to escape fieldnames? */
1835 g_string_append (s, g_quark_to_string (field_id));
1836 g_string_append_len (s, "=(", 2);
1837 g_string_append (s, _priv_gst_value_gtype_to_abbr (type));
1838 g_string_append_c (s, ')');
1840 //TODO(ensonic): table like GstStructureAbbreviation (or extend it)
1841 if (type == G_TYPE_INT) {
1842 g_string_append_len (s, "%i", 2);
1843 } else if (type == G_TYPE_UINT) {
1844 g_string_append_len (s, "%u", 2);
1845 } else if (type == G_TYPE_FLOAT) {
1846 g_string_append_len (s, "%f", 2);
1847 } else if (type == G_TYPE_DOUBLE) {
1848 g_string_append_len (s, "%lf", 3);
1849 } else if (type == G_TYPE_STRING) {
1850 g_string_append_len (s, "%s", 2);
1851 } else if (type == G_TYPE_BOOLEAN) {
1852 /* we normally store this as a string, but can parse it also from an int */
1853 g_string_append_len (s, "%i", 2);
1854 } else if (type == G_TYPE_INT64) {
1855 g_string_append (s, "%" G_GINT64_FORMAT);
1856 } else if (type == G_TYPE_UINT64) {
1857 g_string_append (s, "%" G_GUINT64_FORMAT);
1858 } else if (type == GST_TYPE_STRUCTURE) {
1859 g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
1860 } else if (g_type_is_a (type, G_TYPE_ENUM)
1861 || g_type_is_a (type, G_TYPE_FLAGS)) {
1862 g_string_append_len (s, "%i", 2);
1863 } else if (type == G_TYPE_GTYPE) {
1864 g_string_append_len (s, "%s", 2);
1865 } else if (type == G_TYPE_POINTER) {
1866 g_string_append_len (s, "%p", 2);
1868 GST_WARNING ("unhandled type: %s", g_type_name (type));
1869 g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
1876 * gst_structure_to_string:
1877 * @structure: a #GstStructure
1879 * Converts @structure to a human-readable string representation.
1881 * For debugging purposes its easier to do something like this:
1882 * |[<!-- language="C" -->
1883 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1885 * This prints the structure in human readable form.
1887 * The current implementation of serialization will lead to unexpected results
1888 * when there are nested #GstCaps / #GstStructure deeper than one level.
1890 * Free-function: g_free
1892 * Returns: (transfer full): a pointer to string allocated by g_malloc().
1893 * g_free() after usage.
1896 gst_structure_to_string (const GstStructure * structure)
1900 /* NOTE: This function is potentially called by the debug system,
1901 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1902 * should be careful to avoid recursion. This includes any functions
1903 * called by gst_structure_to_string. In particular, calls should
1904 * not use the GST_PTR_FORMAT extension. */
1906 g_return_val_if_fail (structure != NULL, NULL);
1908 /* we estimate a minimum size based on the number of fields in order to
1909 * avoid unnecessary reallocs within GString */
1910 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1911 g_string_append (s, g_quark_to_string (structure->name));
1912 priv_gst_structure_append_to_gstring (structure, s);
1913 return g_string_free (s, FALSE);
1917 gst_structure_parse_field (gchar * str,
1918 gchar ** after, GstStructureField * field)
1927 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1930 if (G_UNLIKELY (!_priv_gst_value_parse_simple_string (s, &name_end))) {
1931 GST_WARNING ("failed to parse simple string, str=%s", str);
1936 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1939 if (G_UNLIKELY (*s != '=')) {
1940 GST_WARNING ("missing assignment operator in the field, str=%s", str);
1947 field->name = g_quark_from_string (name);
1948 GST_DEBUG ("trying field name '%s'", name);
1951 if (G_UNLIKELY (!_priv_gst_value_parse_value (s, &s, &field->value,
1953 GST_WARNING ("failed to parse value %s", str);
1962 priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end,
1970 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
1971 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1972 && g_ascii_isspace (r[1]))))
1977 if (G_UNLIKELY (!_priv_gst_value_parse_string (r, &w, &r, TRUE))) {
1978 GST_WARNING ("Failed to parse structure string '%s'", str);
1989 priv_gst_structure_parse_fields (gchar * str, gchar ** end,
1990 GstStructure * structure)
1993 GstStructureField field;
1998 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1999 && g_ascii_isspace (r[1]))))
2002 /* end of structure, get the next char and finish */
2007 /* accept \0 as end delimiter */
2010 if (G_UNLIKELY (*r != ',')) {
2011 GST_WARNING ("Failed to find delimiter, r=%s", r);
2015 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2016 && g_ascii_isspace (r[1]))))
2019 memset (&field, 0, sizeof (field));
2020 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2021 GST_WARNING ("Failed to parse field, r=%s", r);
2024 gst_structure_set_field (structure, &field);
2033 * gst_structure_new_from_string:
2034 * @string: a string representation of a #GstStructure
2036 * Creates a #GstStructure from a string representation.
2037 * If end is not %NULL, a pointer to the place inside the given string
2038 * where parsing ended will be returned.
2040 * The current implementation of serialization will lead to unexpected results
2041 * when there are nested #GstCaps / #GstStructure deeper than one level.
2043 * Free-function: gst_structure_free
2045 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2046 * when the string could not be parsed. Free with
2047 * gst_structure_free() after use.
2052 gst_structure_new_from_string (const gchar * string)
2054 return gst_structure_from_string (string, NULL);
2058 * gst_structure_from_string:
2059 * @string: a string representation of a #GstStructure.
2060 * @end: (out) (allow-none) (transfer none) (skip): pointer to store the end of the string in.
2062 * Creates a #GstStructure from a string representation.
2063 * If end is not %NULL, a pointer to the place inside the given string
2064 * where parsing ended will be returned.
2066 * Free-function: gst_structure_free
2068 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2069 * when the string could not be parsed. Free with
2070 * gst_structure_free() after use.
2073 gst_structure_from_string (const gchar * string, gchar ** end)
2080 GstStructure *structure = NULL;
2082 g_return_val_if_fail (string != NULL, NULL);
2084 copy = g_strdup (string);
2087 if (!priv_gst_structure_parse_name (r, &name, &w, &r))
2092 structure = gst_structure_new_empty (name);
2095 if (G_UNLIKELY (structure == NULL))
2098 if (!priv_gst_structure_parse_fields (r, &r, structure))
2102 *end = (char *) string + (r - copy);
2104 g_warning ("gst_structure_from_string did not consume whole string,"
2105 " but caller did not provide end pointer (\"%s\")", string);
2112 gst_structure_free (structure);
2118 gst_structure_transform_to_string (const GValue * src_value,
2119 GValue * dest_value)
2121 g_return_if_fail (src_value != NULL);
2122 g_return_if_fail (dest_value != NULL);
2124 dest_value->data[0].v_pointer =
2125 gst_structure_to_string (src_value->data[0].v_pointer);
2128 static GstStructure *
2129 gst_structure_copy_conditional (const GstStructure * structure)
2132 return gst_structure_copy (structure);
2136 /* fixate utility functions */
2139 * gst_structure_fixate_field_nearest_int:
2140 * @structure: a #GstStructure
2141 * @field_name: a field in @structure
2142 * @target: the target value of the fixation
2144 * Fixates a #GstStructure by changing the given field to the nearest
2145 * integer to @target that is a subset of the existing field.
2147 * Returns: %TRUE if the structure could be fixated
2150 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2151 const char *field_name, int target)
2153 const GValue *value;
2155 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2156 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2158 value = gst_structure_get_value (structure, field_name);
2160 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2163 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2166 x = gst_value_get_int_range_min (value);
2169 x = gst_value_get_int_range_max (value);
2172 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2174 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2175 const GValue *list_value;
2178 int best_index = -1;
2180 n = gst_value_list_get_size (value);
2181 for (i = 0; i < n; i++) {
2182 list_value = gst_value_list_get_value (value, i);
2183 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2184 int x = gst_g_value_get_int_unchecked (list_value);
2186 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2192 if (best_index != -1) {
2193 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2203 * gst_structure_fixate_field_nearest_double:
2204 * @structure: a #GstStructure
2205 * @field_name: a field in @structure
2206 * @target: the target value of the fixation
2208 * Fixates a #GstStructure by changing the given field to the nearest
2209 * double to @target that is a subset of the existing field.
2211 * Returns: %TRUE if the structure could be fixated
2214 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2215 const char *field_name, double target)
2217 const GValue *value;
2219 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2220 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2222 value = gst_structure_get_value (structure, field_name);
2224 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2227 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2230 x = gst_value_get_double_range_min (value);
2233 x = gst_value_get_double_range_max (value);
2236 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2238 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2239 const GValue *list_value;
2242 int best_index = -1;
2244 n = gst_value_list_get_size (value);
2245 for (i = 0; i < n; i++) {
2246 list_value = gst_value_list_get_value (value, i);
2247 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2248 double x = gst_g_value_get_double_unchecked (list_value);
2250 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2256 if (best_index != -1) {
2257 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2268 * gst_structure_fixate_field_boolean:
2269 * @structure: a #GstStructure
2270 * @field_name: a field in @structure
2271 * @target: the target value of the fixation
2273 * Fixates a #GstStructure by changing the given @field_name field to the given
2274 * @target boolean if that field is not fixed yet.
2276 * Returns: %TRUE if the structure could be fixated
2279 gst_structure_fixate_field_boolean (GstStructure * structure,
2280 const char *field_name, gboolean target)
2282 const GValue *value;
2284 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2285 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2287 value = gst_structure_get_value (structure, field_name);
2289 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2292 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2293 const GValue *list_value;
2296 int best_index = -1;
2298 n = gst_value_list_get_size (value);
2299 for (i = 0; i < n; i++) {
2300 list_value = gst_value_list_get_value (value, i);
2301 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2302 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2304 if (best_index == -1 || x == target) {
2310 if (best_index != -1) {
2311 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2321 * gst_structure_fixate_field_string:
2322 * @structure: a #GstStructure
2323 * @field_name: a field in @structure
2324 * @target: the target value of the fixation
2326 * Fixates a #GstStructure by changing the given @field_name field to the given
2327 * @target string if that field is not fixed yet.
2329 * Returns: %TRUE if the structure could be fixated
2332 gst_structure_fixate_field_string (GstStructure * structure,
2333 const gchar * field_name, const gchar * target)
2335 const GValue *value;
2337 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2338 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2340 value = gst_structure_get_value (structure, field_name);
2342 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2345 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2346 const GValue *list_value;
2348 const gchar *best = NULL;
2349 int best_index = -1;
2351 n = gst_value_list_get_size (value);
2352 for (i = 0; i < n; i++) {
2353 list_value = gst_value_list_get_value (value, i);
2354 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2355 const gchar *x = g_value_get_string (list_value);
2357 if (best_index == -1 || g_str_equal (x, target)) {
2363 if (best_index != -1) {
2364 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2374 * gst_structure_fixate_field_nearest_fraction:
2375 * @structure: a #GstStructure
2376 * @field_name: a field in @structure
2377 * @target_numerator: The numerator of the target value of the fixation
2378 * @target_denominator: The denominator of the target value of the fixation
2380 * Fixates a #GstStructure by changing the given field to the nearest
2381 * fraction to @target_numerator/@target_denominator that is a subset
2382 * of the existing field.
2384 * Returns: %TRUE if the structure could be fixated
2387 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2388 const char *field_name, const gint target_numerator,
2389 const gint target_denominator)
2391 const GValue *value;
2393 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2394 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2395 g_return_val_if_fail (target_denominator != 0, FALSE);
2397 value = gst_structure_get_value (structure, field_name);
2399 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2402 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2403 const GValue *x, *new_value;
2404 GValue target = { 0 };
2405 g_value_init (&target, GST_TYPE_FRACTION);
2406 gst_value_set_fraction (&target, target_numerator, target_denominator);
2408 new_value = ⌖
2409 x = gst_value_get_fraction_range_min (value);
2410 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2412 x = gst_value_get_fraction_range_max (value);
2413 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2416 gst_structure_set_value (structure, field_name, new_value);
2417 g_value_unset (&target);
2419 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2420 const GValue *list_value;
2422 const GValue *best = NULL;
2425 gdouble best_diff = G_MAXDOUBLE;
2427 target = (gdouble) target_numerator / (gdouble) target_denominator;
2429 GST_DEBUG ("target %g, best %g", target, best_diff);
2433 n = gst_value_list_get_size (value);
2434 for (i = 0; i < n; i++) {
2435 list_value = gst_value_list_get_value (value, i);
2436 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2438 gdouble list_double;
2440 num = gst_value_get_fraction_numerator (list_value);
2441 denom = gst_value_get_fraction_denominator (list_value);
2443 list_double = ((gdouble) num / (gdouble) denom);
2444 cur_diff = target - list_double;
2446 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2449 cur_diff = -cur_diff;
2451 if (!best || cur_diff < best_diff) {
2452 GST_DEBUG ("new best %g", list_double);
2454 best_diff = cur_diff;
2459 gst_structure_set_value (structure, field_name, best);
2468 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2470 GstStructure *s = data;
2473 if (gst_value_fixate (&v, value)) {
2474 gst_structure_id_take_value (s, field_id, &v);
2480 * gst_structure_fixate_field:
2481 * @structure: a #GstStructure
2482 * @field_name: a field in @structure
2484 * Fixates a #GstStructure by changing the given field with its fixated value.
2486 * Returns: %TRUE if the structure field could be fixated
2489 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2491 GstStructureField *field;
2493 g_return_val_if_fail (structure != NULL, FALSE);
2494 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2496 if (!(field = gst_structure_get_field (structure, field_name)))
2499 return default_fixate (field->name, &field->value, structure);
2502 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2503 * (useful for message parsing functions where the return location is user
2504 * supplied and the user may pass %NULL if the value isn't of interest) */
2505 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2507 const GValue *_value = (value); \
2508 guint _flags = (flags); \
2509 GType _value_type = G_VALUE_TYPE (_value); \
2510 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2511 const gchar *_lcopy_format = _vtable->lcopy_format; \
2512 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2513 guint _n_values = 0; \
2515 while (*_lcopy_format != '\0') { \
2516 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2517 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2520 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2521 *(__error) = g_strdup_printf ("either all or none of the return " \
2522 "locations for field '%s' need to be NULL", fieldname); \
2523 } else if (_cvalues[0].v_pointer != NULL) { \
2524 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2529 * gst_structure_get_valist:
2530 * @structure: a #GstStructure
2531 * @first_fieldname: the name of the first field to read
2532 * @args: variable arguments
2534 * Parses the variable arguments and reads fields from @structure accordingly.
2535 * valist-variant of gst_structure_get(). Look at the documentation of
2536 * gst_structure_get() for more details.
2538 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2541 gst_structure_get_valist (const GstStructure * structure,
2542 const char *first_fieldname, va_list args)
2544 const char *field_name;
2545 GType expected_type = G_TYPE_INVALID;
2547 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2548 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2550 field_name = first_fieldname;
2551 while (field_name) {
2552 const GValue *val = NULL;
2555 expected_type = va_arg (args, GType);
2557 val = gst_structure_get_value (structure, field_name);
2562 if (G_VALUE_TYPE (val) != expected_type)
2565 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2567 g_warning ("%s: %s", G_STRFUNC, err);
2572 field_name = va_arg (args, const gchar *);
2580 GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2581 field_name, structure);
2586 GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
2587 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2588 GST_STR_NULL (g_type_name (expected_type)),
2589 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2596 * gst_structure_id_get_valist:
2597 * @structure: a #GstStructure
2598 * @first_field_id: the quark of the first field to read
2599 * @args: variable arguments
2601 * Parses the variable arguments and reads fields from @structure accordingly.
2602 * valist-variant of gst_structure_id_get(). Look at the documentation of
2603 * gst_structure_id_get() for more details.
2605 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2608 gst_structure_id_get_valist (const GstStructure * structure,
2609 GQuark first_field_id, va_list args)
2612 GType expected_type = G_TYPE_INVALID;
2614 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2615 g_return_val_if_fail (first_field_id != 0, FALSE);
2617 field_id = first_field_id;
2619 const GValue *val = NULL;
2622 expected_type = va_arg (args, GType);
2624 val = gst_structure_id_get_value (structure, field_id);
2629 if (G_VALUE_TYPE (val) != expected_type)
2632 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2634 g_warning ("%s: %s", G_STRFUNC, err);
2639 field_id = va_arg (args, GQuark);
2647 GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2648 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2653 GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
2654 "field was of type '%s': %" GST_PTR_FORMAT,
2655 g_quark_to_string (field_id),
2656 GST_STR_NULL (g_type_name (expected_type)),
2657 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2664 * gst_structure_get:
2665 * @structure: a #GstStructure
2666 * @first_fieldname: the name of the first field to read
2667 * @...: variable arguments
2669 * Parses the variable arguments and reads fields from @structure accordingly.
2670 * Variable arguments should be in the form field name, field type
2671 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2672 * The last variable argument should be %NULL.
2674 * For refcounted (mini)objects you will receive a new reference which
2675 * you must release with a suitable _unref() when no longer needed. For
2676 * strings and boxed types you will receive a copy which you will need to
2677 * release with either g_free() or the suitable function for the boxed type.
2679 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
2680 * because the field requested did not exist, or was of a type other
2681 * than the type specified), otherwise %TRUE.
2684 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
2690 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2691 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2693 va_start (args, first_fieldname);
2694 ret = gst_structure_get_valist (structure, first_fieldname, args);
2701 * gst_structure_id_get:
2702 * @structure: a #GstStructure
2703 * @first_field_id: the quark of the first field to read
2704 * @...: variable arguments
2706 * Parses the variable arguments and reads fields from @structure accordingly.
2707 * Variable arguments should be in the form field id quark, field type
2708 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2709 * The last variable argument should be %NULL (technically it should be a
2710 * 0 quark, but we require %NULL so compilers that support it can check for
2711 * the %NULL terminator and warn if it's not there).
2713 * This function is just like gst_structure_get() only that it is slightly
2714 * more efficient since it saves the string-to-quark lookup in the global
2717 * For refcounted (mini)objects you will receive a new reference which
2718 * you must release with a suitable _unref() when no longer needed. For
2719 * strings and boxed types you will receive a copy which you will need to
2720 * release with either g_free() or the suitable function for the boxed type.
2722 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
2723 * because the field requested did not exist, or was of a type other
2724 * than the type specified), otherwise %TRUE.
2727 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
2733 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2734 g_return_val_if_fail (first_field_id != 0, FALSE);
2736 va_start (args, first_field_id);
2737 ret = gst_structure_id_get_valist (structure, first_field_id, args);
2744 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
2747 const GstStructure *struct1 = (const GstStructure *) data;
2748 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
2750 if (G_UNLIKELY (val1 == NULL))
2752 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
2760 * gst_structure_is_equal:
2761 * @structure1: a #GstStructure.
2762 * @structure2: a #GstStructure.
2764 * Tests if the two #GstStructure are equal.
2766 * Returns: %TRUE if the two structures have the same name and field.
2769 gst_structure_is_equal (const GstStructure * structure1,
2770 const GstStructure * structure2)
2772 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
2773 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
2775 if (G_UNLIKELY (structure1 == structure2))
2778 if (structure1->name != structure2->name) {
2781 if (GST_STRUCTURE_FIELDS (structure1)->len !=
2782 GST_STRUCTURE_FIELDS (structure2)->len) {
2786 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
2787 (gpointer) structure2);
2794 const GstStructure *intersect;
2799 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
2801 IntersectData *idata = (IntersectData *) data;
2802 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
2804 if (G_UNLIKELY (val2 == NULL)) {
2805 gst_structure_id_set_value (idata->dest, id, val1);
2807 GValue dest_value = { 0 };
2808 if (gst_value_intersect (&dest_value, val1, val2)) {
2809 gst_structure_id_take_value (idata->dest, id, &dest_value);
2818 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
2820 IntersectData *idata = (IntersectData *) data;
2821 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
2823 if (G_UNLIKELY (val2 == NULL)) {
2824 gst_structure_id_set_value (idata->dest, id, val1);
2830 * gst_structure_intersect:
2831 * @struct1: a #GstStructure
2832 * @struct2: a #GstStructure
2834 * Intersects @struct1 and @struct2 and returns the intersection.
2836 * Returns: Intersection of @struct1 and @struct2
2839 gst_structure_intersect (const GstStructure * struct1,
2840 const GstStructure * struct2)
2844 g_assert (struct1 != NULL);
2845 g_assert (struct2 != NULL);
2847 if (G_UNLIKELY (struct1->name != struct2->name))
2850 /* copy fields from struct1 which we have not in struct2 to target
2851 * intersect if we have the field in both */
2852 data.dest = gst_structure_new_id_empty (struct1->name);
2853 data.intersect = struct2;
2854 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
2855 gst_structure_intersect_field1, &data)))
2858 /* copy fields from struct2 which we have not in struct1 to target */
2859 data.intersect = struct1;
2860 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
2861 gst_structure_intersect_field2, &data)))
2867 gst_structure_free (data.dest);
2872 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
2875 GstStructure *other = (GstStructure *) data;
2876 const GValue *val2 = gst_structure_id_get_value (other, id);
2878 if (G_LIKELY (val2)) {
2879 if (!gst_value_can_intersect (val1, val2)) {
2882 gint eq = gst_value_compare (val1, val2);
2884 if (eq == GST_VALUE_UNORDERED) {
2885 /* we need to try interseting */
2886 if (!gst_value_intersect (NULL, val1, val2)) {
2889 } else if (eq != GST_VALUE_EQUAL) {
2898 * gst_structure_can_intersect:
2899 * @struct1: a #GstStructure
2900 * @struct2: a #GstStructure
2902 * Tries intersecting @struct1 and @struct2 and reports whether the result
2903 * would not be empty.
2905 * Returns: %TRUE if intersection would not be empty
2908 gst_structure_can_intersect (const GstStructure * struct1,
2909 const GstStructure * struct2)
2911 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
2912 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
2914 if (G_UNLIKELY (struct1->name != struct2->name))
2917 /* tries to intersect if we have the field in both */
2918 return gst_structure_foreach ((GstStructure *) struct1,
2919 gst_caps_structure_can_intersect_field, (gpointer) struct2);
2923 gst_caps_structure_is_superset_field (GQuark field_id, const GValue * value,
2926 GstStructure *subset = user_data;
2927 const GValue *other;
2930 if (!(other = gst_structure_id_get_value (subset, field_id)))
2931 /* field is missing in the subset => no subset */
2934 comparison = gst_value_compare (value, other);
2936 /* equal values are subset */
2937 if (comparison == GST_VALUE_EQUAL)
2940 /* ordered, but unequal, values are not */
2941 if (comparison != GST_VALUE_UNORDERED)
2944 return gst_value_is_subset (other, value);
2948 * gst_structure_is_subset:
2949 * @subset: a #GstStructure
2950 * @superset: a potentially greater #GstStructure
2952 * Checks if @subset is a subset of @superset, i.e. has the same
2953 * structure name and for all fields that are existing in @superset,
2954 * @subset has a value that is a subset of the value in @superset.
2956 * Returns: %TRUE if @subset is a subset of @superset
2959 gst_structure_is_subset (const GstStructure * subset,
2960 const GstStructure * superset)
2962 if ((superset->name != subset->name) ||
2963 (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
2966 return gst_structure_foreach ((GstStructure *) superset,
2967 gst_caps_structure_is_superset_field, (gpointer) subset);
2972 * gst_structure_fixate:
2973 * @structure: a #GstStructure
2975 * Fixate all values in @structure using gst_value_fixate().
2976 * @structure will be modified in-place and should be writable.
2979 gst_structure_fixate (GstStructure * structure)
2981 g_return_if_fail (GST_IS_STRUCTURE (structure));
2983 gst_structure_foreach (structure, default_fixate, structure);