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 if (G_VALUE_TYPE (&field->value) == GST_TYPE_ARRAY) {
1803 t = _priv_gst_value_serialize_any_list (&field->value, "< ", " >", FALSE);
1804 } else if (G_VALUE_TYPE (&field->value) == GST_TYPE_LIST) {
1805 t = _priv_gst_value_serialize_any_list (&field->value, "{ ", " }", FALSE);
1807 t = gst_value_serialize (&field->value);
1810 type = gst_structure_value_get_generic_type (&field->value);
1812 g_string_append_len (s, ", ", 2);
1813 /* FIXME: do we need to escape fieldnames? */
1814 g_string_append (s, g_quark_to_string (field->name));
1815 g_string_append_len (s, "=(", 2);
1816 g_string_append (s, _priv_gst_value_gtype_to_abbr (type));
1817 g_string_append_c (s, ')');
1819 g_string_append (s, t);
1822 GST_WARNING ("No value transform to serialize field '%s' of type '%s'",
1823 g_quark_to_string (field->name),
1824 _priv_gst_value_gtype_to_abbr (type));
1825 g_string_append (s, "NULL");
1829 g_string_append_c (s, ';');
1834 priv__gst_structure_append_template_to_gstring (GQuark field_id,
1835 const GValue * value, gpointer user_data)
1837 GType type = gst_structure_value_get_generic_type (value);
1838 GString *s = (GString *) user_data;
1840 g_string_append_len (s, ", ", 2);
1841 /* FIXME: do we need to escape fieldnames? */
1842 g_string_append (s, g_quark_to_string (field_id));
1843 g_string_append_len (s, "=(", 2);
1844 g_string_append (s, _priv_gst_value_gtype_to_abbr (type));
1845 g_string_append_c (s, ')');
1847 //TODO(ensonic): table like GstStructureAbbreviation (or extend it)
1848 if (type == G_TYPE_INT) {
1849 g_string_append_len (s, "%i", 2);
1850 } else if (type == G_TYPE_UINT) {
1851 g_string_append_len (s, "%u", 2);
1852 } else if (type == G_TYPE_FLOAT) {
1853 g_string_append_len (s, "%f", 2);
1854 } else if (type == G_TYPE_DOUBLE) {
1855 g_string_append_len (s, "%lf", 3);
1856 } else if (type == G_TYPE_STRING) {
1857 g_string_append_len (s, "%s", 2);
1858 } else if (type == G_TYPE_BOOLEAN) {
1859 /* we normally store this as a string, but can parse it also from an int */
1860 g_string_append_len (s, "%i", 2);
1861 } else if (type == G_TYPE_INT64) {
1862 g_string_append (s, "%" G_GINT64_FORMAT);
1863 } else if (type == G_TYPE_UINT64) {
1864 g_string_append (s, "%" G_GUINT64_FORMAT);
1865 } else if (type == GST_TYPE_STRUCTURE) {
1866 g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
1867 } else if (g_type_is_a (type, G_TYPE_ENUM)
1868 || g_type_is_a (type, G_TYPE_FLAGS)) {
1869 g_string_append_len (s, "%i", 2);
1870 } else if (type == G_TYPE_GTYPE) {
1871 g_string_append_len (s, "%s", 2);
1872 } else if (type == G_TYPE_POINTER) {
1873 g_string_append_len (s, "%p", 2);
1875 GST_WARNING ("unhandled type: %s", g_type_name (type));
1876 g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
1883 * gst_structure_to_string:
1884 * @structure: a #GstStructure
1886 * Converts @structure to a human-readable string representation.
1888 * For debugging purposes its easier to do something like this:
1889 * |[<!-- language="C" -->
1890 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1892 * This prints the structure in human readable form.
1894 * The current implementation of serialization will lead to unexpected results
1895 * when there are nested #GstCaps / #GstStructure deeper than one level.
1897 * Free-function: g_free
1899 * Returns: (transfer full): a pointer to string allocated by g_malloc().
1900 * g_free() after usage.
1903 gst_structure_to_string (const GstStructure * structure)
1907 /* NOTE: This function is potentially called by the debug system,
1908 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1909 * should be careful to avoid recursion. This includes any functions
1910 * called by gst_structure_to_string. In particular, calls should
1911 * not use the GST_PTR_FORMAT extension. */
1913 g_return_val_if_fail (structure != NULL, NULL);
1915 /* we estimate a minimum size based on the number of fields in order to
1916 * avoid unnecessary reallocs within GString */
1917 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1918 g_string_append (s, g_quark_to_string (structure->name));
1919 priv_gst_structure_append_to_gstring (structure, s);
1920 return g_string_free (s, FALSE);
1924 gst_structure_parse_field (gchar * str,
1925 gchar ** after, GstStructureField * field)
1934 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1937 if (G_UNLIKELY (!_priv_gst_value_parse_simple_string (s, &name_end))) {
1938 GST_WARNING ("failed to parse simple string, str=%s", str);
1943 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1946 if (G_UNLIKELY (*s != '=')) {
1947 GST_WARNING ("missing assignment operator in the field, str=%s", str);
1954 field->name = g_quark_from_string (name);
1955 GST_DEBUG ("trying field name '%s'", name);
1958 if (G_UNLIKELY (!_priv_gst_value_parse_value (s, &s, &field->value,
1960 GST_WARNING ("failed to parse value %s", str);
1969 priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end,
1977 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
1978 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1979 && g_ascii_isspace (r[1]))))
1984 if (G_UNLIKELY (!_priv_gst_value_parse_string (r, &w, &r, TRUE))) {
1985 GST_WARNING ("Failed to parse structure string '%s'", str);
1996 priv_gst_structure_parse_fields (gchar * str, gchar ** end,
1997 GstStructure * structure)
2000 GstStructureField field;
2005 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2006 && g_ascii_isspace (r[1]))))
2009 /* end of structure, get the next char and finish */
2014 /* accept \0 as end delimiter */
2017 if (G_UNLIKELY (*r != ',')) {
2018 GST_WARNING ("Failed to find delimiter, r=%s", r);
2022 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2023 && g_ascii_isspace (r[1]))))
2026 memset (&field, 0, sizeof (field));
2027 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2028 GST_WARNING ("Failed to parse field, r=%s", r);
2031 gst_structure_set_field (structure, &field);
2040 * gst_structure_new_from_string:
2041 * @string: a string representation of a #GstStructure
2043 * Creates a #GstStructure from a string representation.
2044 * If end is not %NULL, a pointer to the place inside the given string
2045 * where parsing ended will be returned.
2047 * The current implementation of serialization will lead to unexpected results
2048 * when there are nested #GstCaps / #GstStructure deeper than one level.
2050 * Free-function: gst_structure_free
2052 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2053 * when the string could not be parsed. Free with
2054 * gst_structure_free() after use.
2059 gst_structure_new_from_string (const gchar * string)
2061 return gst_structure_from_string (string, NULL);
2065 * gst_structure_from_string:
2066 * @string: a string representation of a #GstStructure.
2067 * @end: (out) (allow-none) (transfer none) (skip): pointer to store the end of the string in.
2069 * Creates a #GstStructure from a string representation.
2070 * If end is not %NULL, a pointer to the place inside the given string
2071 * where parsing ended will be returned.
2073 * Free-function: gst_structure_free
2075 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2076 * when the string could not be parsed. Free with
2077 * gst_structure_free() after use.
2080 gst_structure_from_string (const gchar * string, gchar ** end)
2087 GstStructure *structure = NULL;
2089 g_return_val_if_fail (string != NULL, NULL);
2091 copy = g_strdup (string);
2094 if (!priv_gst_structure_parse_name (r, &name, &w, &r))
2099 structure = gst_structure_new_empty (name);
2102 if (G_UNLIKELY (structure == NULL))
2105 if (!priv_gst_structure_parse_fields (r, &r, structure))
2109 *end = (char *) string + (r - copy);
2111 g_warning ("gst_structure_from_string did not consume whole string,"
2112 " but caller did not provide end pointer (\"%s\")", string);
2119 gst_structure_free (structure);
2125 gst_structure_transform_to_string (const GValue * src_value,
2126 GValue * dest_value)
2128 g_return_if_fail (src_value != NULL);
2129 g_return_if_fail (dest_value != NULL);
2131 dest_value->data[0].v_pointer =
2132 gst_structure_to_string (src_value->data[0].v_pointer);
2135 static GstStructure *
2136 gst_structure_copy_conditional (const GstStructure * structure)
2139 return gst_structure_copy (structure);
2143 /* fixate utility functions */
2146 * gst_structure_fixate_field_nearest_int:
2147 * @structure: a #GstStructure
2148 * @field_name: a field in @structure
2149 * @target: the target value of the fixation
2151 * Fixates a #GstStructure by changing the given field to the nearest
2152 * integer to @target that is a subset of the existing field.
2154 * Returns: %TRUE if the structure could be fixated
2157 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2158 const char *field_name, int target)
2160 const GValue *value;
2162 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2163 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2165 value = gst_structure_get_value (structure, field_name);
2167 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2170 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2173 x = gst_value_get_int_range_min (value);
2176 x = gst_value_get_int_range_max (value);
2179 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2181 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2182 const GValue *list_value;
2185 int best_index = -1;
2187 n = gst_value_list_get_size (value);
2188 for (i = 0; i < n; i++) {
2189 list_value = gst_value_list_get_value (value, i);
2190 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2191 int x = gst_g_value_get_int_unchecked (list_value);
2193 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2199 if (best_index != -1) {
2200 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2210 * gst_structure_fixate_field_nearest_double:
2211 * @structure: a #GstStructure
2212 * @field_name: a field in @structure
2213 * @target: the target value of the fixation
2215 * Fixates a #GstStructure by changing the given field to the nearest
2216 * double to @target that is a subset of the existing field.
2218 * Returns: %TRUE if the structure could be fixated
2221 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2222 const char *field_name, double target)
2224 const GValue *value;
2226 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2227 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2229 value = gst_structure_get_value (structure, field_name);
2231 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2234 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2237 x = gst_value_get_double_range_min (value);
2240 x = gst_value_get_double_range_max (value);
2243 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2245 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2246 const GValue *list_value;
2249 int best_index = -1;
2251 n = gst_value_list_get_size (value);
2252 for (i = 0; i < n; i++) {
2253 list_value = gst_value_list_get_value (value, i);
2254 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2255 double x = gst_g_value_get_double_unchecked (list_value);
2257 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2263 if (best_index != -1) {
2264 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2275 * gst_structure_fixate_field_boolean:
2276 * @structure: a #GstStructure
2277 * @field_name: a field in @structure
2278 * @target: the target value of the fixation
2280 * Fixates a #GstStructure by changing the given @field_name field to the given
2281 * @target boolean if that field is not fixed yet.
2283 * Returns: %TRUE if the structure could be fixated
2286 gst_structure_fixate_field_boolean (GstStructure * structure,
2287 const char *field_name, gboolean target)
2289 const GValue *value;
2291 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2292 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2294 value = gst_structure_get_value (structure, field_name);
2296 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2299 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2300 const GValue *list_value;
2303 int best_index = -1;
2305 n = gst_value_list_get_size (value);
2306 for (i = 0; i < n; i++) {
2307 list_value = gst_value_list_get_value (value, i);
2308 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2309 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2311 if (best_index == -1 || x == target) {
2317 if (best_index != -1) {
2318 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2328 * gst_structure_fixate_field_string:
2329 * @structure: a #GstStructure
2330 * @field_name: a field in @structure
2331 * @target: the target value of the fixation
2333 * Fixates a #GstStructure by changing the given @field_name field to the given
2334 * @target string if that field is not fixed yet.
2336 * Returns: %TRUE if the structure could be fixated
2339 gst_structure_fixate_field_string (GstStructure * structure,
2340 const gchar * field_name, const gchar * target)
2342 const GValue *value;
2344 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2345 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2347 value = gst_structure_get_value (structure, field_name);
2349 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2352 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2353 const GValue *list_value;
2355 const gchar *best = NULL;
2356 int best_index = -1;
2358 n = gst_value_list_get_size (value);
2359 for (i = 0; i < n; i++) {
2360 list_value = gst_value_list_get_value (value, i);
2361 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2362 const gchar *x = g_value_get_string (list_value);
2364 if (best_index == -1 || g_str_equal (x, target)) {
2370 if (best_index != -1) {
2371 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2381 * gst_structure_fixate_field_nearest_fraction:
2382 * @structure: a #GstStructure
2383 * @field_name: a field in @structure
2384 * @target_numerator: The numerator of the target value of the fixation
2385 * @target_denominator: The denominator of the target value of the fixation
2387 * Fixates a #GstStructure by changing the given field to the nearest
2388 * fraction to @target_numerator/@target_denominator that is a subset
2389 * of the existing field.
2391 * Returns: %TRUE if the structure could be fixated
2394 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2395 const char *field_name, const gint target_numerator,
2396 const gint target_denominator)
2398 const GValue *value;
2400 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2401 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2402 g_return_val_if_fail (target_denominator != 0, FALSE);
2404 value = gst_structure_get_value (structure, field_name);
2406 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2409 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2410 const GValue *x, *new_value;
2411 GValue target = { 0 };
2412 g_value_init (&target, GST_TYPE_FRACTION);
2413 gst_value_set_fraction (&target, target_numerator, target_denominator);
2415 new_value = ⌖
2416 x = gst_value_get_fraction_range_min (value);
2417 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2419 x = gst_value_get_fraction_range_max (value);
2420 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2423 gst_structure_set_value (structure, field_name, new_value);
2424 g_value_unset (&target);
2426 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2427 const GValue *list_value;
2429 const GValue *best = NULL;
2432 gdouble best_diff = G_MAXDOUBLE;
2434 target = (gdouble) target_numerator / (gdouble) target_denominator;
2436 GST_DEBUG ("target %g, best %g", target, best_diff);
2440 n = gst_value_list_get_size (value);
2441 for (i = 0; i < n; i++) {
2442 list_value = gst_value_list_get_value (value, i);
2443 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2445 gdouble list_double;
2447 num = gst_value_get_fraction_numerator (list_value);
2448 denom = gst_value_get_fraction_denominator (list_value);
2450 list_double = ((gdouble) num / (gdouble) denom);
2451 cur_diff = target - list_double;
2453 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2456 cur_diff = -cur_diff;
2458 if (!best || cur_diff < best_diff) {
2459 GST_DEBUG ("new best %g", list_double);
2461 best_diff = cur_diff;
2466 gst_structure_set_value (structure, field_name, best);
2475 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2477 GstStructure *s = data;
2480 if (gst_value_fixate (&v, value)) {
2481 gst_structure_id_take_value (s, field_id, &v);
2487 * gst_structure_fixate_field:
2488 * @structure: a #GstStructure
2489 * @field_name: a field in @structure
2491 * Fixates a #GstStructure by changing the given field with its fixated value.
2493 * Returns: %TRUE if the structure field could be fixated
2496 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2498 GstStructureField *field;
2500 g_return_val_if_fail (structure != NULL, FALSE);
2501 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2503 if (!(field = gst_structure_get_field (structure, field_name)))
2506 return default_fixate (field->name, &field->value, structure);
2509 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2510 * (useful for message parsing functions where the return location is user
2511 * supplied and the user may pass %NULL if the value isn't of interest) */
2512 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2514 const GValue *_value = (value); \
2515 guint _flags = (flags); \
2516 GType _value_type = G_VALUE_TYPE (_value); \
2517 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2518 const gchar *_lcopy_format = _vtable->lcopy_format; \
2519 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2520 guint _n_values = 0; \
2522 while (*_lcopy_format != '\0') { \
2523 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2524 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2527 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2528 *(__error) = g_strdup_printf ("either all or none of the return " \
2529 "locations for field '%s' need to be NULL", fieldname); \
2530 } else if (_cvalues[0].v_pointer != NULL) { \
2531 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2536 * gst_structure_get_valist:
2537 * @structure: a #GstStructure
2538 * @first_fieldname: the name of the first field to read
2539 * @args: variable arguments
2541 * Parses the variable arguments and reads fields from @structure accordingly.
2542 * valist-variant of gst_structure_get(). Look at the documentation of
2543 * gst_structure_get() for more details.
2545 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2548 gst_structure_get_valist (const GstStructure * structure,
2549 const char *first_fieldname, va_list args)
2551 const char *field_name;
2552 GType expected_type = G_TYPE_INVALID;
2554 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2555 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2557 field_name = first_fieldname;
2558 while (field_name) {
2559 const GValue *val = NULL;
2562 expected_type = va_arg (args, GType);
2564 val = gst_structure_get_value (structure, field_name);
2569 if (G_VALUE_TYPE (val) != expected_type)
2572 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2574 g_warning ("%s: %s", G_STRFUNC, err);
2579 field_name = va_arg (args, const gchar *);
2587 GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2588 field_name, structure);
2593 GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
2594 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2595 GST_STR_NULL (g_type_name (expected_type)),
2596 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2603 * gst_structure_id_get_valist:
2604 * @structure: a #GstStructure
2605 * @first_field_id: the quark of the first field to read
2606 * @args: variable arguments
2608 * Parses the variable arguments and reads fields from @structure accordingly.
2609 * valist-variant of gst_structure_id_get(). Look at the documentation of
2610 * gst_structure_id_get() for more details.
2612 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2615 gst_structure_id_get_valist (const GstStructure * structure,
2616 GQuark first_field_id, va_list args)
2619 GType expected_type = G_TYPE_INVALID;
2621 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2622 g_return_val_if_fail (first_field_id != 0, FALSE);
2624 field_id = first_field_id;
2626 const GValue *val = NULL;
2629 expected_type = va_arg (args, GType);
2631 val = gst_structure_id_get_value (structure, field_id);
2636 if (G_VALUE_TYPE (val) != expected_type)
2639 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2641 g_warning ("%s: %s", G_STRFUNC, err);
2646 field_id = va_arg (args, GQuark);
2654 GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2655 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2660 GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
2661 "field was of type '%s': %" GST_PTR_FORMAT,
2662 g_quark_to_string (field_id),
2663 GST_STR_NULL (g_type_name (expected_type)),
2664 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2671 * gst_structure_get:
2672 * @structure: a #GstStructure
2673 * @first_fieldname: the name of the first field to read
2674 * @...: variable arguments
2676 * Parses the variable arguments and reads fields from @structure accordingly.
2677 * Variable arguments should be in the form field name, field type
2678 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2679 * The last variable argument should be %NULL.
2681 * For refcounted (mini)objects you will receive a new reference which
2682 * you must release with a suitable _unref() when no longer needed. For
2683 * strings and boxed types you will receive a copy which you will need to
2684 * release with either g_free() or the suitable function for the boxed type.
2686 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
2687 * because the field requested did not exist, or was of a type other
2688 * than the type specified), otherwise %TRUE.
2691 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
2697 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2698 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2700 va_start (args, first_fieldname);
2701 ret = gst_structure_get_valist (structure, first_fieldname, args);
2708 * gst_structure_id_get:
2709 * @structure: a #GstStructure
2710 * @first_field_id: the quark of the first field to read
2711 * @...: variable arguments
2713 * Parses the variable arguments and reads fields from @structure accordingly.
2714 * Variable arguments should be in the form field id quark, field type
2715 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2716 * The last variable argument should be %NULL (technically it should be a
2717 * 0 quark, but we require %NULL so compilers that support it can check for
2718 * the %NULL terminator and warn if it's not there).
2720 * This function is just like gst_structure_get() only that it is slightly
2721 * more efficient since it saves the string-to-quark lookup in the global
2724 * For refcounted (mini)objects you will receive a new reference which
2725 * you must release with a suitable _unref() when no longer needed. For
2726 * strings and boxed types you will receive a copy which you will need to
2727 * release with either g_free() or the suitable function for the boxed type.
2729 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
2730 * because the field requested did not exist, or was of a type other
2731 * than the type specified), otherwise %TRUE.
2734 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
2740 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2741 g_return_val_if_fail (first_field_id != 0, FALSE);
2743 va_start (args, first_field_id);
2744 ret = gst_structure_id_get_valist (structure, first_field_id, args);
2751 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
2754 const GstStructure *struct1 = (const GstStructure *) data;
2755 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
2757 if (G_UNLIKELY (val1 == NULL))
2759 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
2767 * gst_structure_is_equal:
2768 * @structure1: a #GstStructure.
2769 * @structure2: a #GstStructure.
2771 * Tests if the two #GstStructure are equal.
2773 * Returns: %TRUE if the two structures have the same name and field.
2776 gst_structure_is_equal (const GstStructure * structure1,
2777 const GstStructure * structure2)
2779 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
2780 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
2782 if (G_UNLIKELY (structure1 == structure2))
2785 if (structure1->name != structure2->name) {
2788 if (GST_STRUCTURE_FIELDS (structure1)->len !=
2789 GST_STRUCTURE_FIELDS (structure2)->len) {
2793 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
2794 (gpointer) structure2);
2801 const GstStructure *intersect;
2806 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
2808 IntersectData *idata = (IntersectData *) data;
2809 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
2811 if (G_UNLIKELY (val2 == NULL)) {
2812 gst_structure_id_set_value (idata->dest, id, val1);
2814 GValue dest_value = { 0 };
2815 if (gst_value_intersect (&dest_value, val1, val2)) {
2816 gst_structure_id_take_value (idata->dest, id, &dest_value);
2825 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
2827 IntersectData *idata = (IntersectData *) data;
2828 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
2830 if (G_UNLIKELY (val2 == NULL)) {
2831 gst_structure_id_set_value (idata->dest, id, val1);
2837 * gst_structure_intersect:
2838 * @struct1: a #GstStructure
2839 * @struct2: a #GstStructure
2841 * Intersects @struct1 and @struct2 and returns the intersection.
2843 * Returns: Intersection of @struct1 and @struct2
2846 gst_structure_intersect (const GstStructure * struct1,
2847 const GstStructure * struct2)
2851 g_assert (struct1 != NULL);
2852 g_assert (struct2 != NULL);
2854 if (G_UNLIKELY (struct1->name != struct2->name))
2857 /* copy fields from struct1 which we have not in struct2 to target
2858 * intersect if we have the field in both */
2859 data.dest = gst_structure_new_id_empty (struct1->name);
2860 data.intersect = struct2;
2861 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
2862 gst_structure_intersect_field1, &data)))
2865 /* copy fields from struct2 which we have not in struct1 to target */
2866 data.intersect = struct1;
2867 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
2868 gst_structure_intersect_field2, &data)))
2874 gst_structure_free (data.dest);
2879 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
2882 GstStructure *other = (GstStructure *) data;
2883 const GValue *val2 = gst_structure_id_get_value (other, id);
2885 if (G_LIKELY (val2)) {
2886 if (!gst_value_can_intersect (val1, val2)) {
2889 gint eq = gst_value_compare (val1, val2);
2891 if (eq == GST_VALUE_UNORDERED) {
2892 /* we need to try interseting */
2893 if (!gst_value_intersect (NULL, val1, val2)) {
2896 } else if (eq != GST_VALUE_EQUAL) {
2905 * gst_structure_can_intersect:
2906 * @struct1: a #GstStructure
2907 * @struct2: a #GstStructure
2909 * Tries intersecting @struct1 and @struct2 and reports whether the result
2910 * would not be empty.
2912 * Returns: %TRUE if intersection would not be empty
2915 gst_structure_can_intersect (const GstStructure * struct1,
2916 const GstStructure * struct2)
2918 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
2919 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
2921 if (G_UNLIKELY (struct1->name != struct2->name))
2924 /* tries to intersect if we have the field in both */
2925 return gst_structure_foreach ((GstStructure *) struct1,
2926 gst_caps_structure_can_intersect_field, (gpointer) struct2);
2930 gst_caps_structure_is_superset_field (GQuark field_id, const GValue * value,
2933 GstStructure *subset = user_data;
2934 const GValue *other;
2937 if (!(other = gst_structure_id_get_value (subset, field_id)))
2938 /* field is missing in the subset => no subset */
2941 comparison = gst_value_compare (value, other);
2943 /* equal values are subset */
2944 if (comparison == GST_VALUE_EQUAL)
2947 /* ordered, but unequal, values are not */
2948 if (comparison != GST_VALUE_UNORDERED)
2951 return gst_value_is_subset (other, value);
2955 * gst_structure_is_subset:
2956 * @subset: a #GstStructure
2957 * @superset: a potentially greater #GstStructure
2959 * Checks if @subset is a subset of @superset, i.e. has the same
2960 * structure name and for all fields that are existing in @superset,
2961 * @subset has a value that is a subset of the value in @superset.
2963 * Returns: %TRUE if @subset is a subset of @superset
2966 gst_structure_is_subset (const GstStructure * subset,
2967 const GstStructure * superset)
2969 if ((superset->name != subset->name) ||
2970 (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
2973 return gst_structure_foreach ((GstStructure *) superset,
2974 gst_caps_structure_is_superset_field, (gpointer) subset);
2979 * gst_structure_fixate:
2980 * @structure: a #GstStructure
2982 * Fixate all values in @structure using gst_value_fixate().
2983 * @structure will be modified in-place and should be writable.
2986 gst_structure_fixate (GstStructure * structure)
2988 g_return_if_fail (GST_IS_STRUCTURE (structure));
2990 gst_structure_foreach (structure, default_fixate, structure);