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 as
29 * 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
35 * #GstStructure is used by various GStreamer subsystems to store information in
36 * a flexible and extensible way. A #GstStructure does not have a refcount
37 * because it usually is part of a higher level object such as #GstCaps,
38 * #GstMessage, #GstEvent, #GstQuery. It provides a means to enforce mutability
39 * using the refcount of the parent with the gst_structure_set_parent_refcount()
42 * A #GstStructure can be created with gst_structure_new_empty() or
43 * gst_structure_new(), which both take a name and an optional set of key/value
44 * pairs along with the types of the values.
46 * Field values can be changed with gst_structure_set_value() or
47 * gst_structure_set().
49 * Field values can be retrieved with gst_structure_get_value() or the more
50 * convenient gst_structure_get_*() functions.
52 * Fields can be removed with gst_structure_remove_field() or
53 * gst_structure_remove_fields().
55 * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are not
56 * allowed. Strings may be %NULL however.
58 * ## The serialization format
60 * GstStructure serialization format serialize the GstStructure name,
61 * keys/GType/values in a comma separated list with the structure name as first
62 * field without value followed by separated key/value pairs in the form
63 * `key=value`, for example:
66 * a-structure, key=value
69 * The values type will be inferred if not explicitly specified with the
70 * `(GTypeName)value` syntax, for example the following struct will have one
71 * field called 'is-string' which has the string 'true' as a value:
74 * a-struct, field-is-string=(string)true, field-is-boolean=true
77 * *Note*: without specifying `(string), `field-is-string` type would have been
78 * inferred as boolean.
80 * *Note*: we specified `(string)` as a type even if `gchararray` is the actual
81 * GType name as for convenience some well known types have been aliased or
84 * To avoid specifying the type, you can give some hints to the "type system".
85 * For example to specify a value as a double, you should add a decimal (ie. `1`
86 * is an `int` while `1.0` is a `double`).
88 * *Note*: when a structure is serialized with #gst_structure_to_string, all
89 * values are explicitly typed.
91 * Some types have special delimiters:
93 * - [GstValueArray](GST_TYPE_ARRAY) are inside curly brackets (`{` and `}`).
94 * For example `a-structure, array={1, 2, 3}`
95 * - Ranges are inside brackets (`[` and `]`). For example `a-structure,
96 * range=[1, 6, 2]` 1 being the min value, 6 the maximum and 2 the step. To
97 * specify a #GST_TYPE_INT64_RANGE you need to explicitly specify it like:
98 * `a-structure, a-int64-range=(gint64) [1, 5]`
99 * - [GstValueList](GST_TYPE_LIST) are inside "less and greater than" (`<` and
100 * `>`). For example `a-structure, list=<1, 2, 3>
102 * Structures are delimited either by a null character `\0` or a semicolon `;`
103 * the latter allowing to store multiple structures in the same string (see
106 * Quotes are used as "default" delimiters and can be used around any types that
107 * don't use other delimiters (for example `a-struct, i=(int)"1"`). They are use
108 * to allow adding spaces or special characters (such as delimiters,
109 * semicolumns, etc..) inside strings and you can use backslashes `\` to escape
110 * characters inside them, for example:
113 * a-struct, special="\"{[(;)]}\" can be used inside quotes"
116 * They also allow for nested structure, such as:
119 * a-struct, nested=(GstStructure)"nested-struct, nested=true"
122 * Since 1.20, nested structures and caps can be specified using brackets (`[`
123 * and `]`), for example:
126 * a-struct, nested=[nested-struct, nested=true]
129 * > *note*: gst_structure_to_string() won't use that syntax for backward
130 * > compatibility reason, gst_structure_serialize() has been added for
138 /* FIXME 2.0: suppress warnings for deprecated API such as GValueArray
139 * with newer GLib versions (>= 2.31.0) */
140 #define GLIB_DISABLE_DEPRECATION_WARNINGS
144 #include "gst_private.h"
145 #include "gstquark.h"
147 #include <gobject/gvaluecollector.h>
149 GST_DEBUG_CATEGORY_STATIC (gst_structure_debug);
150 #define GST_CAT_DEFAULT gst_structure_debug
152 typedef struct _GstStructureField GstStructureField;
154 struct _GstStructureField
164 /* owned by parent structure, NULL if no parent */
165 gint *parent_refcount;
167 guint fields_len; /* Number of valid items in fields */
168 guint fields_alloc; /* Allocated items in fields */
170 /* Fields are allocated if GST_STRUCTURE_IS_USING_DYNAMIC_ARRAY(),
171 * else it's a pointer to the arr field. */
172 GstStructureField *fields;
174 GstStructureField arr[1];
177 #define GST_STRUCTURE_REFCOUNT(s) (((GstStructureImpl*)(s))->parent_refcount)
178 #define GST_STRUCTURE_LEN(s) (((GstStructureImpl*)(s))->fields_len)
180 #define GST_STRUCTURE_IS_USING_DYNAMIC_ARRAY(s) \
181 (((GstStructureImpl*)(s))->fields != &((GstStructureImpl*)(s))->arr[0])
183 #define GST_STRUCTURE_FIELD(structure, index) \
184 (&((GstStructureImpl*)(structure))->fields[(index)])
186 #define IS_MUTABLE(structure) \
187 (!GST_STRUCTURE_REFCOUNT(structure) || \
188 g_atomic_int_get (GST_STRUCTURE_REFCOUNT(structure)) == 1)
190 #define IS_TAGLIST(structure) \
191 (structure->name == GST_QUARK (TAGLIST))
193 /* Replacement for g_array_append_val */
195 _structure_append_val (GstStructure * s, GstStructureField * val)
197 GstStructureImpl *impl = (GstStructureImpl *) s;
199 /* resize if needed */
200 if (G_UNLIKELY (impl->fields_len == impl->fields_alloc)) {
203 if (G_UNLIKELY (impl->fields_alloc > (G_MAXUINT / 2)))
204 g_error ("Growing structure would result in overflow");
207 MAX (GST_ROUND_UP_8 (impl->fields_len + 1), impl->fields_alloc * 2);
208 if (GST_STRUCTURE_IS_USING_DYNAMIC_ARRAY (s)) {
209 impl->fields = g_renew (GstStructureField, impl->fields, want_alloc);
211 impl->fields = g_new0 (GstStructureField, want_alloc);
212 memcpy (impl->fields, &impl->arr[0],
213 impl->fields_len * sizeof (GstStructureField));
214 GST_CAT_LOG (GST_CAT_PERFORMANCE, "Exceeding pre-allocated array");
216 impl->fields_alloc = want_alloc;
219 /* Finally set value */
220 impl->fields[impl->fields_len++] = *val;
223 /* Replacement for g_array_remove_index */
225 _structure_remove_index (GstStructure * s, guint idx)
227 GstStructureImpl *impl = (GstStructureImpl *) s;
229 /* We never "reduce" the memory footprint. */
230 if (idx >= impl->fields_len)
233 /* Shift everything if it's not the last item */
234 if (idx != impl->fields_len)
235 memmove (&impl->fields[idx],
236 &impl->fields[idx + 1],
237 (impl->fields_len - idx - 1) * sizeof (GstStructureField));
241 static void gst_structure_set_field (GstStructure * structure,
242 GstStructureField * field);
243 static GstStructureField *gst_structure_get_field (const GstStructure *
244 structure, const gchar * fieldname);
245 static GstStructureField *gst_structure_id_get_field (const GstStructure *
246 structure, GQuark field);
247 static void gst_structure_transform_to_string (const GValue * src_value,
248 GValue * dest_value);
249 static GstStructure *gst_structure_copy_conditional (const GstStructure *
252 GType _gst_structure_type = 0;
255 G_DEFINE_BOXED_TYPE (GstStructure, gst_structure,
256 gst_structure_copy_conditional, gst_structure_free);
259 _priv_gst_structure_initialize (void)
261 _gst_structure_type = gst_structure_get_type ();
263 g_value_register_transform_func (_gst_structure_type, G_TYPE_STRING,
264 gst_structure_transform_to_string);
266 GST_DEBUG_CATEGORY_INIT (gst_structure_debug, "structure", 0,
267 "GstStructure debug");
270 static GstStructure *
271 gst_structure_new_id_empty_with_size (GQuark quark, guint prealloc)
274 GstStructureImpl *structure;
279 n_alloc = GST_ROUND_UP_8 (prealloc);
281 g_malloc0 (sizeof (GstStructureImpl) + (n_alloc -
282 1) * sizeof (GstStructureField));
284 ((GstStructure *) structure)->type = _gst_structure_type;
285 ((GstStructure *) structure)->name = quark;
286 GST_STRUCTURE_REFCOUNT (structure) = NULL;
288 structure->fields_len = 0;
289 structure->fields_alloc = n_alloc;
290 structure->fields = &structure->arr[0];
292 GST_TRACE ("created structure %p", structure);
294 return GST_STRUCTURE_CAST (structure);
298 * gst_structure_new_id_empty:
299 * @quark: name of new structure
301 * Creates a new, empty #GstStructure with the given name as a GQuark.
303 * Free-function: gst_structure_free
305 * Returns: (transfer full): a new, empty #GstStructure
308 gst_structure_new_id_empty (GQuark quark)
310 g_return_val_if_fail (quark != 0, NULL);
312 return gst_structure_new_id_empty_with_size (quark, 0);
316 gst_structure_validate_name (const gchar * name)
320 g_return_val_if_fail (name != NULL, FALSE);
322 if (G_UNLIKELY (!g_ascii_isalpha (*name))) {
323 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
328 /* FIXME: test name string more */
330 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
332 if (G_UNLIKELY (*s != '\0')) {
333 GST_WARNING ("Invalid character '%c' at offset %" G_GUINTPTR_FORMAT " in"
334 " structure name: %s", *s, ((guintptr) s - (guintptr) name), name);
338 if (strncmp (name, "video/x-raw-", 12) == 0) {
339 g_warning ("0.10-style raw video caps are being created. Should be "
340 "video/x-raw,format=(string).. now.");
341 } else if (strncmp (name, "audio/x-raw-", 12) == 0) {
342 g_warning ("0.10-style raw audio caps are being created. Should be "
343 "audio/x-raw,format=(string).. now.");
350 * gst_structure_new_empty:
351 * @name: name of new structure
353 * Creates a new, empty #GstStructure with the given @name.
355 * See gst_structure_set_name() for constraints on the @name parameter.
357 * Free-function: gst_structure_free
359 * Returns: (transfer full): a new, empty #GstStructure
362 gst_structure_new_empty (const gchar * name)
364 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
366 return gst_structure_new_id_empty_with_size (g_quark_from_string (name), 0);
371 * @name: name of new structure
372 * @firstfield: name of first field to set
373 * @...: additional arguments
375 * Creates a new #GstStructure with the given name. Parses the
376 * list of variable arguments and sets fields to the values listed.
377 * Variable arguments should be passed as field name, field type,
378 * and value. Last variable argument should be %NULL.
380 * Free-function: gst_structure_free
382 * Returns: (transfer full): a new #GstStructure
385 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
387 GstStructure *structure;
390 va_start (varargs, firstfield);
391 structure = gst_structure_new_valist (name, firstfield, varargs);
398 * gst_structure_new_valist:
399 * @name: name of new structure
400 * @firstfield: name of first field to set
401 * @varargs: variable argument list
403 * Creates a new #GstStructure with the given @name. Structure fields
404 * are set according to the varargs in a manner similar to
405 * gst_structure_new().
407 * See gst_structure_set_name() for constraints on the @name parameter.
409 * Free-function: gst_structure_free
411 * Returns: (transfer full): a new #GstStructure
414 gst_structure_new_valist (const gchar * name,
415 const gchar * firstfield, va_list varargs)
417 GstStructure *structure;
420 const gchar *field_copy = firstfield;
423 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
425 /* Calculate size of varargs */
426 va_copy (copy, varargs);
428 type_copy = va_arg (copy, GType);
429 G_VALUE_COLLECT_SKIP (type_copy, copy);
430 field_copy = va_arg (copy, gchar *);
436 gst_structure_new_id_empty_with_size (g_quark_from_string (name), len);
439 gst_structure_set_valist (structure, firstfield, varargs);
445 * gst_structure_set_parent_refcount:
446 * @structure: a #GstStructure
447 * @refcount: (in): a pointer to the parent's refcount
449 * Sets the parent_refcount field of #GstStructure. This field is used to
450 * determine whether a structure is mutable or not. This function should only be
451 * called by code implementing parent objects of #GstStructure, as described in
452 * the MT Refcounting section of the design documents.
454 * Returns: %TRUE if the parent refcount could be set.
457 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
459 g_return_val_if_fail (structure != NULL, FALSE);
461 /* if we have a parent_refcount already, we can only clear
462 * if with a NULL refcount */
463 if (GST_STRUCTURE_REFCOUNT (structure)) {
464 if (refcount != NULL) {
465 g_return_val_if_fail (refcount == NULL, FALSE);
469 if (refcount == NULL) {
470 g_return_val_if_fail (refcount != NULL, FALSE);
475 GST_STRUCTURE_REFCOUNT (structure) = refcount;
481 * gst_structure_copy:
482 * @structure: a #GstStructure to duplicate
484 * Duplicates a #GstStructure and all its fields and values.
486 * Free-function: gst_structure_free
488 * Returns: (transfer full): a new #GstStructure.
491 gst_structure_copy (const GstStructure * structure)
493 GstStructure *new_structure;
494 GstStructureField *field;
497 g_return_val_if_fail (structure != NULL, NULL);
499 len = GST_STRUCTURE_LEN (structure);
500 new_structure = gst_structure_new_id_empty_with_size (structure->name, len);
502 for (i = 0; i < len; i++) {
503 GstStructureField new_field = { 0 };
505 field = GST_STRUCTURE_FIELD (structure, i);
507 new_field.name = field->name;
508 gst_value_init_and_copy (&new_field.value, &field->value);
509 _structure_append_val (new_structure, &new_field);
511 GST_CAT_TRACE (GST_CAT_PERFORMANCE, "doing copy %p -> %p",
512 structure, new_structure);
514 return new_structure;
518 * gst_structure_free:
519 * @structure: (in) (transfer full): the #GstStructure to free
521 * Frees a #GstStructure and all its fields and values. The structure must not
522 * have a parent when this function is called.
525 gst_structure_free (GstStructure * structure)
527 GstStructureField *field;
530 g_return_if_fail (structure != NULL);
531 g_return_if_fail (GST_STRUCTURE_REFCOUNT (structure) == NULL);
533 len = GST_STRUCTURE_LEN (structure);
534 for (i = 0; i < len; i++) {
535 field = GST_STRUCTURE_FIELD (structure, i);
537 if (G_IS_VALUE (&field->value)) {
538 g_value_unset (&field->value);
541 if (GST_STRUCTURE_IS_USING_DYNAMIC_ARRAY (structure))
542 g_free (((GstStructureImpl *) structure)->fields);
545 memset (structure, 0xff, sizeof (GstStructure));
547 GST_TRACE ("free structure %p", structure);
553 * gst_clear_structure: (skip)
554 * @structure_ptr: a pointer to a #GstStructure reference
556 * Clears a reference to a #GstStructure.
558 * @structure_ptr must not be %NULL.
560 * If the reference is %NULL then this function does nothing.
561 * Otherwise, the structure is free'd using gst_structure_free() and the
562 * pointer is set to %NULL.
564 * A macro is also included that allows this function to be used without
569 #undef gst_clear_structure
571 gst_clear_structure (GstStructure ** structure_ptr)
573 g_clear_pointer (structure_ptr, gst_structure_free);
577 * gst_structure_take:
578 * @oldstr_ptr: (inout) (transfer full) (nullable): pointer to a place of
579 * a #GstStructure to take
580 * @newstr: (transfer full) (nullable): a new #GstStructure
582 * Atomically modifies a pointer to point to a new structure.
583 * The #GstStructure @oldstr_ptr is pointing to is freed and
584 * @newstr is taken ownership over.
586 * Either @newstr and the value pointed to by @oldstr_ptr may be %NULL.
588 * It is a programming error if both @newstr and the value pointed to by
589 * @oldstr_ptr refer to the same, non-%NULL structure.
591 * Returns: %TRUE if @newstr was different from @oldstr_ptr
596 gst_structure_take (GstStructure ** oldstr_ptr, GstStructure * newstr)
598 GstStructure *oldstr;
600 g_return_val_if_fail (oldstr_ptr != NULL, FALSE);
603 oldstr = g_atomic_pointer_get ((gpointer *) oldstr_ptr);
604 if (G_UNLIKELY (oldstr == newstr)) {
605 g_return_val_if_fail (newstr == NULL, FALSE);
608 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
609 oldstr_ptr, (gpointer) oldstr, newstr)));
612 gst_structure_free (oldstr);
618 * gst_structure_get_name:
619 * @structure: a #GstStructure
621 * Get the name of @structure as a string.
623 * Returns: the name of the structure.
626 gst_structure_get_name (const GstStructure * structure)
628 g_return_val_if_fail (structure != NULL, NULL);
630 return g_quark_to_string (structure->name);
634 * gst_structure_has_name:
635 * @structure: a #GstStructure
636 * @name: structure name to check for
638 * Checks if the structure has the given name
640 * Returns: %TRUE if @name matches the name of the structure.
643 gst_structure_has_name (const GstStructure * structure, const gchar * name)
645 const gchar *structure_name;
647 g_return_val_if_fail (structure != NULL, FALSE);
648 g_return_val_if_fail (name != NULL, FALSE);
650 /* getting the string is cheap and comparing short strings is too
651 * should be faster than getting the quark for name and comparing the quarks
653 structure_name = g_quark_to_string (structure->name);
655 return (structure_name && strcmp (structure_name, name) == 0);
659 * gst_structure_get_name_id:
660 * @structure: a #GstStructure
662 * Get the name of @structure as a GQuark.
664 * Returns: the quark representing the name of the structure.
667 gst_structure_get_name_id (const GstStructure * structure)
669 g_return_val_if_fail (structure != NULL, 0);
671 return structure->name;
675 * gst_structure_set_name:
676 * @structure: a #GstStructure
677 * @name: the new name of the structure
679 * Sets the name of the structure to the given @name. The string
680 * provided is copied before being used. It must not be empty, start with a
681 * letter and can be followed by letters, numbers and any of "/-_.:".
684 gst_structure_set_name (GstStructure * structure, const gchar * name)
686 g_return_if_fail (structure != NULL);
687 g_return_if_fail (IS_MUTABLE (structure));
688 g_return_if_fail (gst_structure_validate_name (name));
690 structure->name = g_quark_from_string (name);
694 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
695 const GValue * value)
697 GstStructureField gsfield = { 0, {0,} };
699 gsfield.name = field;
700 gst_value_init_and_copy (&gsfield.value, value);
702 gst_structure_set_field (structure, &gsfield);
706 * gst_structure_id_set_value:
707 * @structure: a #GstStructure
708 * @field: a #GQuark representing a field
709 * @value: the new value of the field
711 * Sets the field with the given GQuark @field to @value. If the field
712 * does not exist, it is created. If the field exists, the previous
713 * value is replaced and freed.
716 gst_structure_id_set_value (GstStructure * structure,
717 GQuark field, const GValue * value)
720 g_return_if_fail (structure != NULL);
721 g_return_if_fail (G_IS_VALUE (value));
722 g_return_if_fail (IS_MUTABLE (structure));
724 gst_structure_id_set_value_internal (structure, field, value);
728 * gst_structure_set_value:
729 * @structure: a #GstStructure
730 * @fieldname: the name of the field to set
731 * @value: the new value of the field
733 * Sets the field with the given name @field to @value. If the field
734 * does not exist, it is created. If the field exists, the previous
735 * value is replaced and freed.
738 gst_structure_set_value (GstStructure * structure,
739 const gchar * fieldname, const GValue * value)
741 g_return_if_fail (structure != NULL);
742 g_return_if_fail (fieldname != NULL);
743 g_return_if_fail (G_IS_VALUE (value));
744 g_return_if_fail (IS_MUTABLE (structure));
746 gst_structure_id_set_value_internal (structure,
747 g_quark_from_string (fieldname), value);
751 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
754 GstStructureField gsfield = { 0, {0,} };
756 gsfield.name = field;
757 gsfield.value = *value;
759 gst_structure_set_field (structure, &gsfield);
761 /* we took ownership */
763 memset (value, 0, sizeof (GValue));
765 value->g_type = G_TYPE_INVALID;
770 * gst_structure_id_take_value:
771 * @structure: a #GstStructure
772 * @field: a #GQuark representing a field
773 * @value: (transfer full): the new value of the field
775 * Sets the field with the given GQuark @field to @value. If the field
776 * does not exist, it is created. If the field exists, the previous
777 * value is replaced and freed.
780 gst_structure_id_take_value (GstStructure * structure, GQuark field,
783 g_return_if_fail (structure != NULL);
784 g_return_if_fail (G_IS_VALUE (value));
785 g_return_if_fail (IS_MUTABLE (structure));
787 gst_structure_id_take_value_internal (structure, field, value);
791 * gst_structure_take_value:
792 * @structure: a #GstStructure
793 * @fieldname: the name of the field to set
794 * @value: (transfer full): the new value of the field
796 * Sets the field with the given name @field to @value. If the field
797 * does not exist, it is created. If the field exists, the previous
798 * value is replaced and freed. The function will take ownership of @value.
801 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
804 g_return_if_fail (structure != NULL);
805 g_return_if_fail (fieldname != NULL);
806 g_return_if_fail (G_IS_VALUE (value));
807 g_return_if_fail (IS_MUTABLE (structure));
809 gst_structure_id_take_value_internal (structure,
810 g_quark_from_string (fieldname), value);
814 gst_structure_set_valist_internal (GstStructure * structure,
815 const gchar * fieldname, va_list varargs)
821 GstStructureField field = { 0 };
823 field.name = g_quark_from_string (fieldname);
825 type = va_arg (varargs, GType);
827 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
828 if (G_UNLIKELY (err)) {
829 g_critical ("%s", err);
833 gst_structure_set_field (structure, &field);
835 fieldname = va_arg (varargs, gchar *);
841 * @structure: a #GstStructure
842 * @fieldname: the name of the field to set
843 * @...: variable arguments
845 * Parses the variable arguments and sets fields accordingly. Fields that
846 * weren't already part of the structure are added as needed.
847 * Variable arguments should be in the form field name, field type
848 * (as a GType), value(s). The last variable argument should be %NULL.
851 gst_structure_set (GstStructure * structure, const gchar * field, ...)
855 g_return_if_fail (structure != NULL);
856 g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
858 va_start (varargs, field);
859 gst_structure_set_valist_internal (structure, field, varargs);
864 * gst_structure_set_valist:
865 * @structure: a #GstStructure
866 * @fieldname: the name of the field to set
867 * @varargs: variable arguments
869 * va_list form of gst_structure_set().
872 gst_structure_set_valist (GstStructure * structure,
873 const gchar * fieldname, va_list varargs)
875 g_return_if_fail (structure != NULL);
876 g_return_if_fail (IS_MUTABLE (structure));
878 gst_structure_set_valist_internal (structure, fieldname, varargs);
882 gst_structure_id_set_valist_internal (GstStructure * structure,
883 GQuark fieldname, va_list varargs)
889 GstStructureField field = { 0 };
891 field.name = fieldname;
892 type = va_arg (varargs, GType);
894 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
895 if (G_UNLIKELY (err)) {
896 g_critical ("%s", err);
900 gst_structure_set_field (structure, &field);
902 fieldname = va_arg (varargs, GQuark);
907 * gst_structure_id_set:
908 * @structure: a #GstStructure
909 * @fieldname: the GQuark for the name of the field to set
910 * @...: variable arguments
912 * Identical to gst_structure_set, except that field names are
913 * passed using the GQuark for the field name. This allows more efficient
914 * setting of the structure if the caller already knows the associated
916 * The last variable argument must be %NULL.
919 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
923 g_return_if_fail (structure != NULL);
925 va_start (varargs, field);
926 gst_structure_id_set_valist_internal (structure, field, varargs);
931 * gst_structure_id_set_valist:
932 * @structure: a #GstStructure
933 * @fieldname: the name of the field to set
934 * @varargs: variable arguments
936 * va_list form of gst_structure_id_set().
939 gst_structure_id_set_valist (GstStructure * structure,
940 GQuark fieldname, va_list varargs)
942 g_return_if_fail (structure != NULL);
943 g_return_if_fail (IS_MUTABLE (structure));
945 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
949 * gst_structure_new_id:
950 * @name_quark: name of new structure
951 * @field_quark: the GQuark for the name of the field to set
952 * @...: variable arguments
954 * Creates a new #GstStructure with the given name as a GQuark, followed by
955 * fieldname quark, GType, argument(s) "triplets" in the same format as
956 * gst_structure_id_set(). Basically a convenience wrapper around
957 * gst_structure_new_id_empty() and gst_structure_id_set().
959 * The last variable argument must be %NULL (or 0).
961 * Free-function: gst_structure_free
963 * Returns: (transfer full): a new #GstStructure
966 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
972 GQuark quark_copy = field_quark;
975 g_return_val_if_fail (name_quark != 0, NULL);
976 g_return_val_if_fail (field_quark != 0, NULL);
978 va_start (varargs, field_quark);
980 /* Calculate size of varargs */
981 va_copy (copy, varargs);
983 type_copy = va_arg (copy, GType);
984 G_VALUE_COLLECT_SKIP (type_copy, copy);
985 quark_copy = va_arg (copy, GQuark);
990 s = gst_structure_new_id_empty_with_size (name_quark, len);
992 gst_structure_id_set_valist_internal (s, field_quark, varargs);
998 #if GST_VERSION_NANO == 1
999 #define GIT_G_WARNING g_warning
1001 #define GIT_G_WARNING GST_WARNING
1004 /* If the structure currently contains a field with the same name, it is
1005 * replaced with the provided field. Otherwise, the field is added to the
1006 * structure. The field's value is not deeply copied.
1009 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
1011 GstStructureField *f;
1012 GType field_value_type;
1015 len = GST_STRUCTURE_LEN (structure);
1017 field_value_type = G_VALUE_TYPE (&field->value);
1018 if (field_value_type == G_TYPE_STRING) {
1021 s = g_value_get_string (&field->value);
1022 /* only check for NULL strings in taglists, as they are allowed in message
1023 * structs, e.g. error message debug strings */
1024 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
1026 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
1027 "Please file a bug.", g_quark_to_string (field->name));
1028 g_value_unset (&field->value);
1031 /* empty strings never make sense */
1032 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
1033 "Please file a bug.", g_quark_to_string (field->name));
1034 g_value_unset (&field->value);
1037 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
1038 g_warning ("Trying to set string on %s field '%s', but string is not "
1039 "valid UTF-8. Please file a bug.",
1040 IS_TAGLIST (structure) ? "taglist" : "structure",
1041 g_quark_to_string (field->name));
1042 g_value_unset (&field->value);
1045 } else if (G_UNLIKELY (field_value_type == G_TYPE_DATE)) {
1048 d = g_value_get_boxed (&field->value);
1049 /* only check for NULL GDates in taglists, as they might make sense
1050 * in other, generic structs */
1051 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
1052 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
1053 "Please file a bug.", g_quark_to_string (field->name));
1054 g_value_unset (&field->value);
1056 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
1058 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
1059 IS_TAGLIST (structure) ? "taglist" : "structure",
1060 g_quark_to_string (field->name));
1061 g_value_unset (&field->value);
1066 for (i = 0; i < len; i++) {
1067 f = GST_STRUCTURE_FIELD (structure, i);
1069 if (G_UNLIKELY (f->name == field->name)) {
1070 g_value_unset (&f->value);
1071 memcpy (f, field, sizeof (GstStructureField));
1076 _structure_append_val (structure, field);
1079 /* If there is no field with the given ID, NULL is returned.
1081 static GstStructureField *
1082 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
1084 GstStructureField *field;
1087 len = GST_STRUCTURE_LEN (structure);
1089 for (i = 0; i < len; i++) {
1090 field = GST_STRUCTURE_FIELD (structure, i);
1092 if (G_UNLIKELY (field->name == field_id))
1099 /* If there is no field with the given ID, NULL is returned.
1101 static GstStructureField *
1102 gst_structure_get_field (const GstStructure * structure,
1103 const gchar * fieldname)
1105 g_return_val_if_fail (structure != NULL, NULL);
1106 g_return_val_if_fail (fieldname != NULL, NULL);
1108 return gst_structure_id_get_field (structure,
1109 g_quark_from_string (fieldname));
1113 * gst_structure_get_value:
1114 * @structure: a #GstStructure
1115 * @fieldname: the name of the field to get
1117 * Get the value of the field with name @fieldname.
1119 * Returns: (nullable): the #GValue corresponding to the field with the given
1123 gst_structure_get_value (const GstStructure * structure,
1124 const gchar * fieldname)
1126 GstStructureField *field;
1128 g_return_val_if_fail (structure != NULL, NULL);
1129 g_return_val_if_fail (fieldname != NULL, NULL);
1131 field = gst_structure_get_field (structure, fieldname);
1135 return &field->value;
1139 * gst_structure_id_get_value:
1140 * @structure: a #GstStructure
1141 * @field: the #GQuark of the field to get
1143 * Get the value of the field with GQuark @field.
1145 * Returns: (nullable): the #GValue corresponding to the field with the given
1149 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
1151 GstStructureField *gsfield;
1153 g_return_val_if_fail (structure != NULL, NULL);
1155 gsfield = gst_structure_id_get_field (structure, field);
1156 if (gsfield == NULL)
1159 return &gsfield->value;
1163 * gst_structure_remove_field:
1164 * @structure: a #GstStructure
1165 * @fieldname: the name of the field to remove
1167 * Removes the field with the given name. If the field with the given
1168 * name does not exist, the structure is unchanged.
1171 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
1173 GstStructureField *field;
1177 g_return_if_fail (structure != NULL);
1178 g_return_if_fail (fieldname != NULL);
1179 g_return_if_fail (IS_MUTABLE (structure));
1181 id = g_quark_from_string (fieldname);
1182 len = GST_STRUCTURE_LEN (structure);
1184 for (i = 0; i < len; i++) {
1185 field = GST_STRUCTURE_FIELD (structure, i);
1187 if (field->name == id) {
1188 if (G_IS_VALUE (&field->value)) {
1189 g_value_unset (&field->value);
1191 _structure_remove_index (structure, i);
1198 * gst_structure_remove_fields:
1199 * @structure: a #GstStructure
1200 * @fieldname: the name of the field to remove
1201 * @...: %NULL-terminated list of more fieldnames to remove
1203 * Removes the fields with the given names. If a field does not exist, the
1204 * argument is ignored.
1207 gst_structure_remove_fields (GstStructure * structure,
1208 const gchar * fieldname, ...)
1212 g_return_if_fail (structure != NULL);
1213 g_return_if_fail (fieldname != NULL);
1214 /* mutability checked in remove_field */
1216 va_start (varargs, fieldname);
1217 gst_structure_remove_fields_valist (structure, fieldname, varargs);
1222 * gst_structure_remove_fields_valist:
1223 * @structure: a #GstStructure
1224 * @fieldname: the name of the field to remove
1225 * @varargs: %NULL-terminated list of more fieldnames to remove
1227 * va_list form of gst_structure_remove_fields().
1230 gst_structure_remove_fields_valist (GstStructure * structure,
1231 const gchar * fieldname, va_list varargs)
1233 gchar *field = (gchar *) fieldname;
1235 g_return_if_fail (structure != NULL);
1236 g_return_if_fail (fieldname != NULL);
1237 /* mutability checked in remove_field */
1240 gst_structure_remove_field (structure, field);
1241 field = va_arg (varargs, char *);
1246 * gst_structure_remove_all_fields:
1247 * @structure: a #GstStructure
1249 * Removes all fields in a GstStructure.
1252 gst_structure_remove_all_fields (GstStructure * structure)
1254 GstStructureField *field;
1257 g_return_if_fail (structure != NULL);
1258 g_return_if_fail (IS_MUTABLE (structure));
1260 for (i = GST_STRUCTURE_LEN (structure) - 1; i >= 0; i--) {
1261 field = GST_STRUCTURE_FIELD (structure, i);
1263 if (G_IS_VALUE (&field->value)) {
1264 g_value_unset (&field->value);
1266 _structure_remove_index (structure, i);
1271 * gst_structure_get_field_type:
1272 * @structure: a #GstStructure
1273 * @fieldname: the name of the field
1275 * Finds the field with the given name, and returns the type of the
1276 * value it contains. If the field is not found, G_TYPE_INVALID is
1279 * Returns: the #GValue of the field
1282 gst_structure_get_field_type (const GstStructure * structure,
1283 const gchar * fieldname)
1285 GstStructureField *field;
1287 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1288 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1290 field = gst_structure_get_field (structure, fieldname);
1292 return G_TYPE_INVALID;
1294 return G_VALUE_TYPE (&field->value);
1298 * gst_structure_n_fields:
1299 * @structure: a #GstStructure
1301 * Get the number of fields in the structure.
1303 * Returns: the number of fields in the structure
1306 gst_structure_n_fields (const GstStructure * structure)
1308 g_return_val_if_fail (structure != NULL, 0);
1310 return GST_STRUCTURE_LEN (structure);
1314 * gst_structure_nth_field_name:
1315 * @structure: a #GstStructure
1316 * @index: the index to get the name of
1318 * Get the name of the given field number, counting from 0 onwards.
1320 * Returns: the name of the given field number
1323 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1325 GstStructureField *field;
1327 g_return_val_if_fail (structure != NULL, NULL);
1328 g_return_val_if_fail (index < GST_STRUCTURE_LEN (structure), NULL);
1330 field = GST_STRUCTURE_FIELD (structure, index);
1332 return g_quark_to_string (field->name);
1336 * gst_structure_foreach:
1337 * @structure: a #GstStructure
1338 * @func: (scope call): a function to call for each field
1339 * @user_data: (closure): private data
1341 * Calls the provided function once for each field in the #GstStructure. The
1342 * function must not modify the fields. Also see gst_structure_map_in_place()
1343 * and gst_structure_filter_and_map_in_place().
1345 * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
1349 gst_structure_foreach (const GstStructure * structure,
1350 GstStructureForeachFunc func, gpointer user_data)
1353 GstStructureField *field;
1356 g_return_val_if_fail (structure != NULL, FALSE);
1357 g_return_val_if_fail (func != NULL, FALSE);
1359 len = GST_STRUCTURE_LEN (structure);
1361 for (i = 0; i < len; i++) {
1362 field = GST_STRUCTURE_FIELD (structure, i);
1364 ret = func (field->name, &field->value, user_data);
1365 if (G_UNLIKELY (!ret))
1373 * gst_structure_map_in_place:
1374 * @structure: a #GstStructure
1375 * @func: (scope call): a function to call for each field
1376 * @user_data: (closure): private data
1378 * Calls the provided function once for each field in the #GstStructure. In
1379 * contrast to gst_structure_foreach(), the function may modify but not delete the
1380 * fields. The structure must be mutable.
1382 * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
1386 gst_structure_map_in_place (GstStructure * structure,
1387 GstStructureMapFunc func, gpointer user_data)
1390 GstStructureField *field;
1393 g_return_val_if_fail (structure != NULL, FALSE);
1394 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1395 g_return_val_if_fail (func != NULL, FALSE);
1396 len = GST_STRUCTURE_LEN (structure);
1398 for (i = 0; i < len; i++) {
1399 field = GST_STRUCTURE_FIELD (structure, i);
1401 ret = func (field->name, &field->value, user_data);
1410 * gst_structure_filter_and_map_in_place:
1411 * @structure: a #GstStructure
1412 * @func: (scope call): a function to call for each field
1413 * @user_data: (closure): private data
1415 * Calls the provided function once for each field in the #GstStructure. In
1416 * contrast to gst_structure_foreach(), the function may modify the fields.
1417 * In contrast to gst_structure_map_in_place(), the field is removed from
1418 * the structure if %FALSE is returned from the function.
1419 * The structure must be mutable.
1424 gst_structure_filter_and_map_in_place (GstStructure * structure,
1425 GstStructureFilterMapFunc func, gpointer user_data)
1428 GstStructureField *field;
1431 g_return_if_fail (structure != NULL);
1432 g_return_if_fail (IS_MUTABLE (structure));
1433 g_return_if_fail (func != NULL);
1434 len = GST_STRUCTURE_LEN (structure);
1436 for (i = 0; i < len;) {
1437 field = GST_STRUCTURE_FIELD (structure, i);
1439 ret = func (field->name, &field->value, user_data);
1442 if (G_IS_VALUE (&field->value)) {
1443 g_value_unset (&field->value);
1445 _structure_remove_index (structure, i);
1446 len = GST_STRUCTURE_LEN (structure);
1454 * gst_structure_id_has_field:
1455 * @structure: a #GstStructure
1456 * @field: #GQuark of the field name
1458 * Check if @structure contains a field named @field.
1460 * Returns: %TRUE if the structure contains a field with the given name
1463 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1465 GstStructureField *f;
1467 g_return_val_if_fail (structure != NULL, FALSE);
1468 g_return_val_if_fail (field != 0, FALSE);
1470 f = gst_structure_id_get_field (structure, field);
1476 * gst_structure_has_field:
1477 * @structure: a #GstStructure
1478 * @fieldname: the name of a field
1480 * Check if @structure contains a field named @fieldname.
1482 * Returns: %TRUE if the structure contains a field with the given name
1485 gst_structure_has_field (const GstStructure * structure,
1486 const gchar * fieldname)
1488 g_return_val_if_fail (structure != NULL, FALSE);
1489 g_return_val_if_fail (fieldname != NULL, FALSE);
1491 return gst_structure_id_has_field (structure,
1492 g_quark_from_string (fieldname));
1496 * gst_structure_id_has_field_typed:
1497 * @structure: a #GstStructure
1498 * @field: #GQuark of the field name
1499 * @type: the type of a value
1501 * Check if @structure contains a field named @field and with GType @type.
1503 * Returns: %TRUE if the structure contains a field with the given name and type
1506 gst_structure_id_has_field_typed (const GstStructure * structure,
1507 GQuark field, GType type)
1509 GstStructureField *f;
1511 g_return_val_if_fail (structure != NULL, FALSE);
1512 g_return_val_if_fail (field != 0, FALSE);
1514 f = gst_structure_id_get_field (structure, field);
1518 return (G_VALUE_TYPE (&f->value) == type);
1522 * gst_structure_has_field_typed:
1523 * @structure: a #GstStructure
1524 * @fieldname: the name of a field
1525 * @type: the type of a value
1527 * Check if @structure contains a field named @fieldname and with GType @type.
1529 * Returns: %TRUE if the structure contains a field with the given name and type
1532 gst_structure_has_field_typed (const GstStructure * structure,
1533 const gchar * fieldname, GType type)
1535 g_return_val_if_fail (structure != NULL, FALSE);
1536 g_return_val_if_fail (fieldname != NULL, FALSE);
1538 return gst_structure_id_has_field_typed (structure,
1539 g_quark_from_string (fieldname), type);
1542 /* utility functions */
1545 * gst_structure_get_boolean:
1546 * @structure: a #GstStructure
1547 * @fieldname: the name of a field
1548 * @value: (out): a pointer to a #gboolean to set
1550 * Sets the boolean pointed to by @value corresponding to the value of the
1551 * given field. Caller is responsible for making sure the field exists
1552 * and has the correct type.
1554 * Returns: %TRUE if the value could be set correctly. If there was no field
1555 * with @fieldname or the existing field did not contain a boolean, this
1556 * function returns %FALSE.
1559 gst_structure_get_boolean (const GstStructure * structure,
1560 const gchar * fieldname, gboolean * value)
1562 GstStructureField *field;
1564 g_return_val_if_fail (structure != NULL, FALSE);
1565 g_return_val_if_fail (fieldname != NULL, FALSE);
1567 field = gst_structure_get_field (structure, fieldname);
1569 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_BOOLEAN)
1572 *value = gst_g_value_get_boolean_unchecked (&field->value);
1578 * gst_structure_get_int:
1579 * @structure: a #GstStructure
1580 * @fieldname: the name of a field
1581 * @value: (out): a pointer to an int to set
1583 * Sets the int pointed to by @value corresponding to the value of the
1584 * given field. Caller is responsible for making sure the field exists
1585 * and has the correct type.
1587 * Returns: %TRUE if the value could be set correctly. If there was no field
1588 * with @fieldname or the existing field did not contain an int, this function
1592 gst_structure_get_int (const GstStructure * structure,
1593 const gchar * fieldname, gint * value)
1595 GstStructureField *field;
1597 g_return_val_if_fail (structure != NULL, FALSE);
1598 g_return_val_if_fail (fieldname != NULL, FALSE);
1599 g_return_val_if_fail (value != NULL, FALSE);
1601 field = gst_structure_get_field (structure, fieldname);
1603 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT)
1606 *value = gst_g_value_get_int_unchecked (&field->value);
1612 * gst_structure_get_uint:
1613 * @structure: a #GstStructure
1614 * @fieldname: the name of a field
1615 * @value: (out): a pointer to a uint to set
1617 * Sets the uint pointed to by @value corresponding to the value of the
1618 * given field. Caller is responsible for making sure the field exists
1619 * and has the correct type.
1621 * Returns: %TRUE if the value could be set correctly. If there was no field
1622 * with @fieldname or the existing field did not contain a uint, this function
1626 gst_structure_get_uint (const GstStructure * structure,
1627 const gchar * fieldname, guint * value)
1629 GstStructureField *field;
1631 g_return_val_if_fail (structure != NULL, FALSE);
1632 g_return_val_if_fail (fieldname != NULL, FALSE);
1633 g_return_val_if_fail (value != NULL, FALSE);
1635 field = gst_structure_get_field (structure, fieldname);
1637 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT)
1640 *value = gst_g_value_get_uint_unchecked (&field->value);
1646 * gst_structure_get_int64:
1647 * @structure: a #GstStructure
1648 * @fieldname: the name of a field
1649 * @value: (out): a pointer to a #gint64 to set
1651 * Sets the #gint64 pointed to by @value corresponding to the value of the
1652 * given field. Caller is responsible for making sure the field exists
1653 * and has the correct type.
1655 * Returns: %TRUE if the value could be set correctly. If there was no field
1656 * with @fieldname or the existing field did not contain a #gint64, this function
1662 gst_structure_get_int64 (const GstStructure * structure,
1663 const gchar * fieldname, gint64 * value)
1665 GstStructureField *field;
1667 g_return_val_if_fail (structure != NULL, FALSE);
1668 g_return_val_if_fail (fieldname != NULL, FALSE);
1669 g_return_val_if_fail (value != NULL, FALSE);
1671 field = gst_structure_get_field (structure, fieldname);
1673 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT64)
1676 *value = gst_g_value_get_int64_unchecked (&field->value);
1682 * gst_structure_get_uint64:
1683 * @structure: a #GstStructure
1684 * @fieldname: the name of a field
1685 * @value: (out): a pointer to a #guint64 to set
1687 * Sets the #guint64 pointed to by @value corresponding to the value of the
1688 * given field. Caller is responsible for making sure the field exists
1689 * and has the correct type.
1691 * Returns: %TRUE if the value could be set correctly. If there was no field
1692 * with @fieldname or the existing field did not contain a #guint64, this function
1698 gst_structure_get_uint64 (const GstStructure * structure,
1699 const gchar * fieldname, guint64 * value)
1701 GstStructureField *field;
1703 g_return_val_if_fail (structure != NULL, FALSE);
1704 g_return_val_if_fail (fieldname != NULL, FALSE);
1705 g_return_val_if_fail (value != NULL, FALSE);
1707 field = gst_structure_get_field (structure, fieldname);
1709 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT64)
1712 *value = gst_g_value_get_uint64_unchecked (&field->value);
1718 * gst_structure_get_date:
1719 * @structure: a #GstStructure
1720 * @fieldname: the name of a field
1721 * @value: (out callee-allocates): a pointer to a #GDate to set
1723 * Sets the date pointed to by @value corresponding to the date of the
1724 * given field. Caller is responsible for making sure the field exists
1725 * and has the correct type.
1727 * On success @value will point to a newly-allocated copy of the date which
1728 * should be freed with g_date_free() when no longer needed (note: this is
1729 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1730 * copy of the string).
1732 * Returns: %TRUE if the value could be set correctly. If there was no field
1733 * with @fieldname or the existing field did not contain a data, this function
1737 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1740 GstStructureField *field;
1742 g_return_val_if_fail (structure != NULL, FALSE);
1743 g_return_val_if_fail (fieldname != NULL, FALSE);
1744 g_return_val_if_fail (value != NULL, FALSE);
1746 field = gst_structure_get_field (structure, fieldname);
1748 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DATE)
1751 /* FIXME: 2.0 g_value_dup_boxed() -> g_value_get_boxed() */
1752 *value = g_value_dup_boxed (&field->value);
1758 * gst_structure_get_date_time:
1759 * @structure: a #GstStructure
1760 * @fieldname: the name of a field
1761 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1763 * Sets the datetime pointed to by @value corresponding to the datetime of the
1764 * given field. Caller is responsible for making sure the field exists
1765 * and has the correct type.
1767 * On success @value will point to a reference of the datetime which
1768 * should be unreffed with gst_date_time_unref() when no longer needed
1769 * (note: this is inconsistent with e.g. gst_structure_get_string()
1770 * which doesn't return a copy of the string).
1772 * Returns: %TRUE if the value could be set correctly. If there was no field
1773 * with @fieldname or the existing field did not contain a data, this function
1777 gst_structure_get_date_time (const GstStructure * structure,
1778 const gchar * fieldname, GstDateTime ** value)
1780 GstStructureField *field;
1782 g_return_val_if_fail (structure != NULL, FALSE);
1783 g_return_val_if_fail (fieldname != NULL, FALSE);
1784 g_return_val_if_fail (value != NULL, FALSE);
1786 field = gst_structure_get_field (structure, fieldname);
1790 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1793 /* FIXME 2.0: g_value_dup_boxed() -> g_value_get_boxed() */
1794 *value = g_value_dup_boxed (&field->value);
1800 * gst_structure_get_clock_time:
1801 * @structure: a #GstStructure
1802 * @fieldname: the name of a field
1803 * @value: (out): a pointer to a #GstClockTime to set
1805 * Sets the clock time pointed to by @value corresponding to the clock time
1806 * of the given field. Caller is responsible for making sure the field exists
1807 * and has the correct type.
1809 * Returns: %TRUE if the value could be set correctly. If there was no field
1810 * with @fieldname or the existing field did not contain a #GstClockTime, this
1811 * function returns %FALSE.
1814 gst_structure_get_clock_time (const GstStructure * structure,
1815 const gchar * fieldname, GstClockTime * value)
1817 return gst_structure_get_uint64 (structure, fieldname, value);
1821 * gst_structure_get_double:
1822 * @structure: a #GstStructure
1823 * @fieldname: the name of a field
1824 * @value: (out): a pointer to a gdouble to set
1826 * Sets the double pointed to by @value corresponding to the value of the
1827 * given field. Caller is responsible for making sure the field exists
1828 * and has the correct type.
1830 * Returns: %TRUE if the value could be set correctly. If there was no field
1831 * with @fieldname or the existing field did not contain a double, this
1832 * function returns %FALSE.
1835 gst_structure_get_double (const GstStructure * structure,
1836 const gchar * fieldname, gdouble * value)
1838 GstStructureField *field;
1840 g_return_val_if_fail (structure != NULL, FALSE);
1841 g_return_val_if_fail (fieldname != NULL, FALSE);
1842 g_return_val_if_fail (value != NULL, FALSE);
1844 field = gst_structure_get_field (structure, fieldname);
1846 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DOUBLE)
1849 *value = gst_g_value_get_double_unchecked (&field->value);
1855 * gst_structure_get_string:
1856 * @structure: a #GstStructure
1857 * @fieldname: the name of a field
1859 * Finds the field corresponding to @fieldname, and returns the string
1860 * contained in the field's value. Caller is responsible for making
1861 * sure the field exists and has the correct type.
1863 * The string should not be modified, and remains valid until the next
1864 * call to a gst_structure_*() function with the given structure.
1866 * Returns: (nullable): a pointer to the string or %NULL when the
1867 * field did not exist or did not contain a string.
1870 gst_structure_get_string (const GstStructure * structure,
1871 const gchar * fieldname)
1873 GstStructureField *field;
1875 g_return_val_if_fail (structure != NULL, NULL);
1876 g_return_val_if_fail (fieldname != NULL, NULL);
1878 field = gst_structure_get_field (structure, fieldname);
1880 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_STRING)
1883 return gst_g_value_get_string_unchecked (&field->value);
1887 * gst_structure_get_enum:
1888 * @structure: a #GstStructure
1889 * @fieldname: the name of a field
1890 * @enumtype: the enum type of a field
1891 * @value: (out): a pointer to an int to set
1893 * Sets the int pointed to by @value corresponding to the value of the
1894 * given field. Caller is responsible for making sure the field exists,
1895 * has the correct type and that the enumtype is correct.
1897 * Returns: %TRUE if the value could be set correctly. If there was no field
1898 * with @fieldname or the existing field did not contain an enum of the given
1899 * type, this function returns %FALSE.
1902 gst_structure_get_enum (const GstStructure * structure,
1903 const gchar * fieldname, GType enumtype, gint * value)
1905 GstStructureField *field;
1907 g_return_val_if_fail (structure != NULL, FALSE);
1908 g_return_val_if_fail (fieldname != NULL, FALSE);
1909 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1910 g_return_val_if_fail (value != NULL, FALSE);
1912 field = gst_structure_get_field (structure, fieldname);
1916 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1919 *value = g_value_get_enum (&field->value);
1925 * gst_structure_get_fraction:
1926 * @structure: a #GstStructure
1927 * @fieldname: the name of a field
1928 * @value_numerator: (out): a pointer to an int to set
1929 * @value_denominator: (out): a pointer to an int to set
1931 * Sets the integers pointed to by @value_numerator and @value_denominator
1932 * corresponding to the value of the given field. Caller is responsible
1933 * for making sure the field exists and has the correct type.
1935 * Returns: %TRUE if the values could be set correctly. If there was no field
1936 * with @fieldname or the existing field did not contain a GstFraction, this
1937 * function returns %FALSE.
1940 gst_structure_get_fraction (const GstStructure * structure,
1941 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1943 GstStructureField *field;
1945 g_return_val_if_fail (structure != NULL, FALSE);
1946 g_return_val_if_fail (fieldname != NULL, FALSE);
1947 g_return_val_if_fail (value_numerator != NULL, FALSE);
1948 g_return_val_if_fail (value_denominator != NULL, FALSE);
1950 field = gst_structure_get_field (structure, fieldname);
1952 if (field == NULL || G_VALUE_TYPE (&field->value) != GST_TYPE_FRACTION)
1955 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1956 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1962 * gst_structure_get_flagset:
1963 * @structure: a #GstStructure
1964 * @fieldname: the name of a field
1965 * @value_flags: (out) (optional): a pointer to a guint for the flags field
1966 * @value_mask: (out) (optional): a pointer to a guint for the mask field
1968 * Read the GstFlagSet flags and mask out of the structure into the
1969 * provided pointers.
1971 * Returns: %TRUE if the values could be set correctly. If there was no field
1972 * with @fieldname or the existing field did not contain a GstFlagSet, this
1973 * function returns %FALSE.
1978 gst_structure_get_flagset (const GstStructure * structure,
1979 const gchar * fieldname, guint * value_flags, guint * value_mask)
1981 GstStructureField *field;
1983 g_return_val_if_fail (structure != NULL, FALSE);
1984 g_return_val_if_fail (fieldname != NULL, FALSE);
1986 field = gst_structure_get_field (structure, fieldname);
1988 if (field == NULL || !GST_VALUE_HOLDS_FLAG_SET (&field->value))
1992 *value_flags = gst_value_get_flagset_flags (&field->value);
1994 *value_mask = gst_value_get_flagset_mask (&field->value);
2000 gst_structure_value_get_generic_type (const GValue * val)
2002 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
2003 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
2004 GArray *array = g_value_peek_pointer (val);
2006 /* Note, we can do this because the internal
2007 * GstValueList/GstValueArray implementation is API/ABI compatible
2009 if (array->len > 0) {
2010 GValue *value = &g_array_index (array, GValue, 0);
2012 return gst_structure_value_get_generic_type (value);
2016 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
2018 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
2019 return G_TYPE_INT64;
2020 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
2021 return G_TYPE_DOUBLE;
2022 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
2023 return GST_TYPE_FRACTION;
2025 return G_VALUE_TYPE (val);
2029 priv_gst_structure_append_to_gstring (const GstStructure * structure,
2030 GString * s, GstSerializeFlags flags)
2032 GstStructureField *field;
2034 gboolean nested_structs_brackets =
2035 !(flags & GST_SERIALIZE_FLAG_BACKWARD_COMPAT);
2037 g_return_val_if_fail (s != NULL, FALSE);
2039 len = GST_STRUCTURE_LEN (structure);
2040 for (i = 0; i < len; i++) {
2044 field = GST_STRUCTURE_FIELD (structure, i);
2046 if (G_VALUE_TYPE (&field->value) == GST_TYPE_ARRAY) {
2047 t = _priv_gst_value_serialize_any_list (&field->value, "< ", " >", FALSE,
2049 } else if (G_VALUE_TYPE (&field->value) == GST_TYPE_LIST) {
2050 t = _priv_gst_value_serialize_any_list (&field->value, "{ ", " }", FALSE,
2052 } else if (!nested_structs_brackets
2053 || (G_VALUE_TYPE (&field->value) != GST_TYPE_STRUCTURE
2054 && G_VALUE_TYPE (&field->value) != GST_TYPE_CAPS)) {
2055 t = gst_value_serialize (&field->value);
2058 type = gst_structure_value_get_generic_type (&field->value);
2060 g_string_append_len (s, ", ", 2);
2061 /* FIXME: do we need to escape fieldnames? */
2062 g_string_append (s, g_quark_to_string (field->name));
2063 g_string_append_len (s, "=(", 2);
2064 g_string_append (s, _priv_gst_value_gtype_to_abbr (type));
2065 g_string_append_c (s, ')');
2066 if (nested_structs_brackets
2067 && G_VALUE_TYPE (&field->value) == GST_TYPE_STRUCTURE) {
2068 const GstStructure *substruct = gst_value_get_structure (&field->value);
2070 g_string_append_c (s, '[');
2071 g_string_append (s, g_quark_to_string (substruct->name));
2072 priv_gst_structure_append_to_gstring (substruct, s, flags);
2073 g_string_append_c (s, ']');
2074 } else if (nested_structs_brackets
2075 && G_VALUE_TYPE (&field->value) == GST_TYPE_CAPS) {
2076 const GstCaps *subcaps = gst_value_get_caps (&field->value);
2077 gchar *capsstr = gst_caps_serialize (subcaps, flags);
2079 g_string_append_printf (s, "[%s]", capsstr);
2082 g_string_append (s, t);
2084 } else if (G_TYPE_CHECK_VALUE_TYPE (&field->value, G_TYPE_POINTER)) {
2085 gpointer ptr = g_value_get_pointer (&field->value);
2088 g_string_append (s, "NULL");
2090 g_string_append_printf (s, "%p", ptr);
2092 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, G_TYPE_STRING))
2093 GST_WARNING ("No value transform to serialize field '%s' of type '%s'",
2094 g_quark_to_string (field->name),
2095 _priv_gst_value_gtype_to_abbr (type));
2096 /* TODO(ensonic): don't print NULL if field->value is not empty */
2097 g_string_append (s, "NULL");
2101 g_string_append_c (s, ';');
2106 priv__gst_structure_append_template_to_gstring (GQuark field_id,
2107 const GValue * value, gpointer user_data)
2109 GType type = gst_structure_value_get_generic_type (value);
2110 GString *s = (GString *) user_data;
2112 g_string_append_len (s, ", ", 2);
2113 /* FIXME: do we need to escape fieldnames? */
2114 g_string_append (s, g_quark_to_string (field_id));
2115 g_string_append_len (s, "=(", 2);
2116 g_string_append (s, _priv_gst_value_gtype_to_abbr (type));
2117 g_string_append_c (s, ')');
2119 //TODO(ensonic): table like GstStructureAbbreviation (or extend it)
2120 if (type == G_TYPE_INT) {
2121 g_string_append_len (s, "%i", 2);
2122 } else if (type == G_TYPE_UINT) {
2123 g_string_append_len (s, "%u", 2);
2124 } else if (type == G_TYPE_FLOAT) {
2125 g_string_append_len (s, "%f", 2);
2126 } else if (type == G_TYPE_DOUBLE) {
2127 g_string_append_len (s, "%lf", 3);
2128 } else if (type == G_TYPE_STRING) {
2129 g_string_append_len (s, "%s", 2);
2130 } else if (type == G_TYPE_BOOLEAN) {
2131 /* we normally store this as a string, but can parse it also from an int */
2132 g_string_append_len (s, "%i", 2);
2133 } else if (type == G_TYPE_INT64) {
2134 g_string_append (s, "%" G_GINT64_FORMAT);
2135 } else if (type == G_TYPE_UINT64) {
2136 g_string_append (s, "%" G_GUINT64_FORMAT);
2137 } else if (type == GST_TYPE_STRUCTURE) {
2138 g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
2139 } else if (g_type_is_a (type, G_TYPE_ENUM)
2140 || g_type_is_a (type, G_TYPE_FLAGS)) {
2141 g_string_append_len (s, "%i", 2);
2142 } else if (type == G_TYPE_GTYPE) {
2143 g_string_append_len (s, "%s", 2);
2144 } else if (type == G_TYPE_POINTER) {
2145 g_string_append_len (s, "%p", 2);
2147 GST_WARNING ("unhandled type: %s", g_type_name (type));
2148 g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
2155 structure_serialize (const GstStructure * structure, GstSerializeFlags flags)
2159 /* NOTE: This function is potentially called by the debug system,
2160 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
2161 * should be careful to avoid recursion. This includes any functions
2162 * called by gst_structure_to_string. In particular, calls should
2163 * not use the GST_PTR_FORMAT extension. */
2165 g_return_val_if_fail (structure != NULL, NULL);
2167 /* we estimate a minimum size based on the number of fields in order to
2168 * avoid unnecessary reallocs within GString */
2169 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
2170 g_string_append (s, g_quark_to_string (structure->name));
2171 priv_gst_structure_append_to_gstring (structure, s, flags);
2172 return g_string_free (s, FALSE);
2177 * gst_structure_to_string:
2178 * @structure: a #GstStructure
2180 * Converts @structure to a human-readable string representation.
2182 * For debugging purposes its easier to do something like this: |[<!--
2183 * language="C" --> GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
2185 * This prints the structure in human readable form.
2187 * This function will lead to unexpected results when there are nested #GstCaps
2188 * / #GstStructure deeper than one level, you should user
2189 * gst_structure_serialize() instead for those cases.
2191 * Free-function: g_free
2193 * Returns: (transfer full): a pointer to string allocated by g_malloc().
2194 * g_free() after usage.
2197 gst_structure_to_string (const GstStructure * structure)
2199 return structure_serialize (structure, GST_SERIALIZE_FLAG_BACKWARD_COMPAT);
2203 * gst_structure_serialize:
2204 * @structure: a #GstStructure
2205 * @flags: The flags to use to serialize structure
2207 * Converts @structure to a human-readable string representation.
2209 * This version of the caps serialization function introduces support for nested
2210 * structures and caps but the resulting strings won't be parsable with
2211 * GStreamer prior to 1.20 unless #GST_SERIALIZE_FLAG_BACKWARD_COMPAT is passed
2214 * Free-function: g_free
2216 * Returns: (transfer full): a pointer to string allocated by g_malloc().
2217 * g_free() after usage.
2222 gst_structure_serialize (const GstStructure * structure,
2223 GstSerializeFlags flags)
2225 return structure_serialize (structure, flags);
2229 gst_structure_parse_field (gchar * str,
2230 gchar ** after, GstStructureField * field)
2239 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2242 if (G_UNLIKELY (!_priv_gst_value_parse_simple_string (s, &name_end))) {
2243 GST_WARNING ("failed to parse simple string, str=%s", str);
2248 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2251 if (G_UNLIKELY (*s != '=')) {
2252 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2259 field->name = g_quark_from_string (name);
2260 GST_DEBUG ("trying field name '%s'", name);
2263 if (G_UNLIKELY (!_priv_gst_value_parse_value (s, &s, &field->value,
2264 G_TYPE_INVALID, NULL))) {
2265 GST_WARNING ("failed to parse value %s", str);
2274 priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end,
2275 gchar ** next, gboolean check_valid)
2282 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2283 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2284 && g_ascii_isspace (r[1]))))
2289 if (G_UNLIKELY (!_priv_gst_value_parse_string (r, &w, &r, TRUE))) {
2290 GST_WARNING ("Failed to parse structure string '%s'", str);
2298 if (!gst_structure_validate_name (*start)) {
2312 priv_gst_structure_parse_fields (gchar * str, gchar ** end,
2313 GstStructure * structure)
2316 GstStructureField field;
2321 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2322 && g_ascii_isspace (r[1]))))
2325 /* end of structure, get the next char and finish */
2330 /* accept \0 as end delimiter */
2333 if (G_UNLIKELY (*r != ',')) {
2334 GST_WARNING ("Failed to find delimiter, r=%s", r);
2338 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2339 && g_ascii_isspace (r[1]))))
2342 /* Trailing comma */
2345 } else if (*r == ';') {
2350 memset (&field, 0, sizeof (field));
2351 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2352 GST_WARNING ("Failed to parse field, r=%s", r);
2355 gst_structure_set_field (structure, &field);
2364 * gst_structure_new_from_string:
2365 * @string: a string representation of a #GstStructure
2367 * Creates a #GstStructure from a string representation.
2368 * If end is not %NULL, a pointer to the place inside the given string
2369 * where parsing ended will be returned.
2371 * The current implementation of serialization will lead to unexpected results
2372 * when there are nested #GstCaps / #GstStructure deeper than one level unless
2373 * the gst_structure_serialize() function is used (without
2374 * #GST_SERIALIZE_FLAG_BACKWARD_COMPAT)
2376 * Free-function: gst_structure_free
2378 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2379 * when the string could not be parsed. Free with
2380 * gst_structure_free() after use.
2385 gst_structure_new_from_string (const gchar * string)
2387 return gst_structure_from_string (string, NULL);
2391 * gst_structure_from_string: (constructor):
2392 * @string: a string representation of a #GstStructure.
2393 * @end: (out) (optional) (transfer none) (skip): pointer to store the end of the string in.
2395 * Creates a #GstStructure from a string representation.
2396 * If end is not %NULL, a pointer to the place inside the given string
2397 * where parsing ended will be returned.
2399 * Free-function: gst_structure_free
2401 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2402 * when the string could not be parsed. Free with
2403 * gst_structure_free() after use.
2406 gst_structure_from_string (const gchar * string, gchar ** end)
2413 GstStructure *structure = NULL;
2415 g_return_val_if_fail (string != NULL, NULL);
2417 copy = g_strdup (string);
2420 if (!priv_gst_structure_parse_name (r, &name, &w, &r, FALSE))
2425 structure = gst_structure_new_empty (name);
2428 if (G_UNLIKELY (structure == NULL))
2431 if (!priv_gst_structure_parse_fields (r, &r, structure))
2435 *end = (char *) string + (r - copy);
2437 g_warning ("gst_structure_from_string did not consume whole string,"
2438 " but caller did not provide end pointer (\"%s\")", string);
2445 gst_structure_free (structure);
2451 gst_structure_transform_to_string (const GValue * src_value,
2452 GValue * dest_value)
2454 g_return_if_fail (src_value != NULL);
2455 g_return_if_fail (dest_value != NULL);
2457 dest_value->data[0].v_pointer =
2458 gst_structure_to_string (src_value->data[0].v_pointer);
2461 static GstStructure *
2462 gst_structure_copy_conditional (const GstStructure * structure)
2465 return gst_structure_copy (structure);
2469 /* fixate utility functions */
2472 * gst_structure_fixate_field_nearest_int:
2473 * @structure: a #GstStructure
2474 * @field_name: a field in @structure
2475 * @target: the target value of the fixation
2477 * Fixates a #GstStructure by changing the given field to the nearest
2478 * integer to @target that is a subset of the existing field.
2480 * Returns: %TRUE if the structure could be fixated
2483 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2484 const char *field_name, int target)
2486 const GValue *value;
2488 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2489 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2491 value = gst_structure_get_value (structure, field_name);
2493 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2496 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2499 min = gst_value_get_int_range_min (value);
2500 max = gst_value_get_int_range_max (value);
2501 step = gst_value_get_int_range_step (value);
2503 target = CLAMP (target, min, max);
2504 if (G_UNLIKELY (step != 1)) {
2505 gint rem = target % step;
2511 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2513 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2514 const GValue *list_value;
2517 int best_index = -1;
2519 n = gst_value_list_get_size (value);
2520 for (i = 0; i < n; i++) {
2521 list_value = gst_value_list_get_value (value, i);
2522 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2523 int x = gst_g_value_get_int_unchecked (list_value);
2525 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2531 if (best_index != -1) {
2532 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2542 * gst_structure_fixate_field_nearest_double:
2543 * @structure: a #GstStructure
2544 * @field_name: a field in @structure
2545 * @target: the target value of the fixation
2547 * Fixates a #GstStructure by changing the given field to the nearest
2548 * double to @target that is a subset of the existing field.
2550 * Returns: %TRUE if the structure could be fixated
2553 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2554 const char *field_name, double target)
2556 const GValue *value;
2558 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2559 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2561 value = gst_structure_get_value (structure, field_name);
2563 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2566 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2569 x = gst_value_get_double_range_min (value);
2572 x = gst_value_get_double_range_max (value);
2575 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2577 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2578 const GValue *list_value;
2581 int best_index = -1;
2583 n = gst_value_list_get_size (value);
2584 for (i = 0; i < n; i++) {
2585 list_value = gst_value_list_get_value (value, i);
2586 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2587 double x = gst_g_value_get_double_unchecked (list_value);
2589 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2595 if (best_index != -1) {
2596 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2607 * gst_structure_fixate_field_boolean:
2608 * @structure: a #GstStructure
2609 * @field_name: a field in @structure
2610 * @target: the target value of the fixation
2612 * Fixates a #GstStructure by changing the given @field_name field to the given
2613 * @target boolean if that field is not fixed yet.
2615 * Returns: %TRUE if the structure could be fixated
2618 gst_structure_fixate_field_boolean (GstStructure * structure,
2619 const char *field_name, gboolean target)
2621 const GValue *value;
2623 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2624 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2626 value = gst_structure_get_value (structure, field_name);
2628 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2631 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2632 const GValue *list_value;
2635 int best_index = -1;
2637 n = gst_value_list_get_size (value);
2638 for (i = 0; i < n; i++) {
2639 list_value = gst_value_list_get_value (value, i);
2640 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2641 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2643 if (best_index == -1 || x == target) {
2649 if (best_index != -1) {
2650 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2660 * gst_structure_fixate_field_string:
2661 * @structure: a #GstStructure
2662 * @field_name: a field in @structure
2663 * @target: the target value of the fixation
2665 * Fixates a #GstStructure by changing the given @field_name field to the given
2666 * @target string if that field is not fixed yet.
2668 * Returns: %TRUE if the structure could be fixated
2671 gst_structure_fixate_field_string (GstStructure * structure,
2672 const gchar * field_name, const gchar * target)
2674 const GValue *value;
2676 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2677 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2679 value = gst_structure_get_value (structure, field_name);
2681 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2684 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2685 const GValue *list_value;
2687 const gchar *best = NULL;
2688 int best_index = -1;
2690 n = gst_value_list_get_size (value);
2691 for (i = 0; i < n; i++) {
2692 list_value = gst_value_list_get_value (value, i);
2693 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2694 const gchar *x = g_value_get_string (list_value);
2696 if (best_index == -1 || g_str_equal (x, target)) {
2702 if (best_index != -1) {
2703 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2713 * gst_structure_fixate_field_nearest_fraction:
2714 * @structure: a #GstStructure
2715 * @field_name: a field in @structure
2716 * @target_numerator: The numerator of the target value of the fixation
2717 * @target_denominator: The denominator of the target value of the fixation
2719 * Fixates a #GstStructure by changing the given field to the nearest
2720 * fraction to @target_numerator/@target_denominator that is a subset
2721 * of the existing field.
2723 * Returns: %TRUE if the structure could be fixated
2726 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2727 const char *field_name, const gint target_numerator,
2728 const gint target_denominator)
2730 const GValue *value;
2732 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2733 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2734 g_return_val_if_fail (target_denominator != 0, FALSE);
2736 value = gst_structure_get_value (structure, field_name);
2738 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2741 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2742 const GValue *x, *new_value;
2743 GValue target = { 0 };
2744 g_value_init (&target, GST_TYPE_FRACTION);
2745 gst_value_set_fraction (&target, target_numerator, target_denominator);
2747 new_value = ⌖
2748 x = gst_value_get_fraction_range_min (value);
2749 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2751 x = gst_value_get_fraction_range_max (value);
2752 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2755 gst_structure_set_value (structure, field_name, new_value);
2756 g_value_unset (&target);
2758 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2759 const GValue *list_value;
2761 const GValue *best = NULL;
2764 gdouble best_diff = G_MAXDOUBLE;
2766 target = (gdouble) target_numerator / (gdouble) target_denominator;
2768 GST_DEBUG ("target %g, best %g", target, best_diff);
2772 n = gst_value_list_get_size (value);
2773 for (i = 0; i < n; i++) {
2774 list_value = gst_value_list_get_value (value, i);
2775 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2777 gdouble list_double;
2779 num = gst_value_get_fraction_numerator (list_value);
2780 denom = gst_value_get_fraction_denominator (list_value);
2782 list_double = ((gdouble) num / (gdouble) denom);
2783 cur_diff = target - list_double;
2785 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2788 cur_diff = -cur_diff;
2790 if (!best || cur_diff < best_diff) {
2791 GST_DEBUG ("new best %g", list_double);
2793 best_diff = cur_diff;
2798 gst_structure_set_value (structure, field_name, best);
2807 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2809 GstStructure *s = data;
2812 if (gst_value_fixate (&v, value)) {
2813 gst_structure_id_take_value (s, field_id, &v);
2819 * gst_structure_fixate_field:
2820 * @structure: a #GstStructure
2821 * @field_name: a field in @structure
2823 * Fixates a #GstStructure by changing the given field with its fixated value.
2825 * Returns: %TRUE if the structure field could be fixated
2828 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2830 GstStructureField *field;
2832 g_return_val_if_fail (structure != NULL, FALSE);
2833 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2835 if (!(field = gst_structure_get_field (structure, field_name)))
2838 return default_fixate (field->name, &field->value, structure);
2841 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2842 * (useful for message parsing functions where the return location is user
2843 * supplied and the user may pass %NULL if the value isn't of interest) */
2844 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2846 const GValue *_value = (value); \
2847 guint _flags = (flags); \
2848 GType _value_type = G_VALUE_TYPE (_value); \
2849 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2850 const gchar *_lcopy_format = _vtable->lcopy_format; \
2851 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2852 guint _n_values = 0; \
2854 while (*_lcopy_format != '\0') { \
2855 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2856 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2859 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2860 *(__error) = g_strdup_printf ("either all or none of the return " \
2861 "locations for field '%s' need to be NULL", fieldname); \
2862 } else if (_cvalues[0].v_pointer != NULL) { \
2863 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2868 * gst_structure_get_valist:
2869 * @structure: a #GstStructure
2870 * @first_fieldname: the name of the first field to read
2871 * @args: variable arguments
2873 * Parses the variable arguments and reads fields from @structure accordingly.
2874 * valist-variant of gst_structure_get(). Look at the documentation of
2875 * gst_structure_get() for more details.
2877 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2880 gst_structure_get_valist (const GstStructure * structure,
2881 const char *first_fieldname, va_list args)
2883 const char *field_name;
2884 GType expected_type = G_TYPE_INVALID;
2886 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2887 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2889 field_name = first_fieldname;
2890 while (field_name) {
2891 const GValue *val = NULL;
2894 expected_type = va_arg (args, GType);
2896 val = gst_structure_get_value (structure, field_name);
2901 if (G_VALUE_TYPE (val) != expected_type)
2904 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2906 g_warning ("%s: %s", G_STRFUNC, err);
2911 field_name = va_arg (args, const gchar *);
2919 GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2920 field_name, structure);
2925 GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
2926 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2927 GST_STR_NULL (g_type_name (expected_type)),
2928 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2935 * gst_structure_id_get_valist:
2936 * @structure: a #GstStructure
2937 * @first_field_id: the quark of the first field to read
2938 * @args: variable arguments
2940 * Parses the variable arguments and reads fields from @structure accordingly.
2941 * valist-variant of gst_structure_id_get(). Look at the documentation of
2942 * gst_structure_id_get() for more details.
2944 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2947 gst_structure_id_get_valist (const GstStructure * structure,
2948 GQuark first_field_id, va_list args)
2951 GType expected_type = G_TYPE_INVALID;
2953 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2954 g_return_val_if_fail (first_field_id != 0, FALSE);
2956 field_id = first_field_id;
2958 const GValue *val = NULL;
2961 expected_type = va_arg (args, GType);
2963 val = gst_structure_id_get_value (structure, field_id);
2968 if (G_VALUE_TYPE (val) != expected_type)
2971 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2973 g_warning ("%s: %s", G_STRFUNC, err);
2978 field_id = va_arg (args, GQuark);
2986 GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2987 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2992 GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
2993 "field was of type '%s': %" GST_PTR_FORMAT,
2994 g_quark_to_string (field_id),
2995 GST_STR_NULL (g_type_name (expected_type)),
2996 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
3003 * gst_structure_get:
3004 * @structure: a #GstStructure
3005 * @first_fieldname: the name of the first field to read
3006 * @...: variable arguments
3008 * Parses the variable arguments and reads fields from @structure accordingly.
3009 * Variable arguments should be in the form field name, field type
3010 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3011 * The last variable argument should be %NULL.
3013 * For refcounted (mini)objects you will receive a new reference which
3014 * you must release with a suitable _unref\() when no longer needed. For
3015 * strings and boxed types you will receive a copy which you will need to
3016 * release with either g_free() or the suitable function for the boxed type.
3018 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
3019 * because the field requested did not exist, or was of a type other
3020 * than the type specified), otherwise %TRUE.
3023 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
3029 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3030 g_return_val_if_fail (first_fieldname != NULL, FALSE);
3032 va_start (args, first_fieldname);
3033 ret = gst_structure_get_valist (structure, first_fieldname, args);
3040 * gst_structure_id_get:
3041 * @structure: a #GstStructure
3042 * @first_field_id: the quark of the first field to read
3043 * @...: variable arguments
3045 * Parses the variable arguments and reads fields from @structure accordingly.
3046 * Variable arguments should be in the form field id quark, field type
3047 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3048 * The last variable argument should be %NULL (technically it should be a
3049 * 0 quark, but we require %NULL so compilers that support it can check for
3050 * the %NULL terminator and warn if it's not there).
3052 * This function is just like gst_structure_get() only that it is slightly
3053 * more efficient since it saves the string-to-quark lookup in the global
3056 * For refcounted (mini)objects you will receive a new reference which
3057 * you must release with a suitable _unref\() when no longer needed. For
3058 * strings and boxed types you will receive a copy which you will need to
3059 * release with either g_free() or the suitable function for the boxed type.
3061 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
3062 * because the field requested did not exist, or was of a type other
3063 * than the type specified), otherwise %TRUE.
3066 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
3072 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3073 g_return_val_if_fail (first_field_id != 0, FALSE);
3075 va_start (args, first_field_id);
3076 ret = gst_structure_id_get_valist (structure, first_field_id, args);
3083 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
3086 const GstStructure *struct1 = (const GstStructure *) data;
3087 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
3089 if (G_UNLIKELY (val1 == NULL))
3091 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
3099 * gst_structure_is_equal:
3100 * @structure1: a #GstStructure.
3101 * @structure2: a #GstStructure.
3103 * Tests if the two #GstStructure are equal.
3105 * Returns: %TRUE if the two structures have the same name and field.
3108 gst_structure_is_equal (const GstStructure * structure1,
3109 const GstStructure * structure2)
3111 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
3112 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
3114 if (G_UNLIKELY (structure1 == structure2))
3117 if (structure1->name != structure2->name) {
3120 if (GST_STRUCTURE_LEN (structure1) != GST_STRUCTURE_LEN (structure2)) {
3124 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
3125 (gpointer) structure2);
3129 * gst_structure_intersect:
3130 * @struct1: a #GstStructure
3131 * @struct2: a #GstStructure
3133 * Intersects @struct1 and @struct2 and returns the intersection.
3135 * Returns: (transfer full) (nullable): Intersection of @struct1 and @struct2
3138 gst_structure_intersect (const GstStructure * struct1,
3139 const GstStructure * struct2)
3141 guint it1, len1, it2, len2;
3144 g_assert (struct1 != NULL);
3145 g_assert (struct2 != NULL);
3147 if (G_UNLIKELY (struct1->name != struct2->name))
3150 len1 = GST_STRUCTURE_LEN (struct1);
3151 len2 = GST_STRUCTURE_LEN (struct2);
3153 /* Resulting structure will be at most the size of the smallest structure */
3154 dest = gst_structure_new_id_empty_with_size (struct1->name, MIN (len1, len2));
3156 /* copy fields from struct1 which we have not in struct2 to target
3157 * intersect if we have the field in both */
3158 for (it1 = 0; it1 < len1; it1++) {
3159 GstStructureField *field1 = GST_STRUCTURE_FIELD (struct1, it1);
3160 gboolean seenother = FALSE;
3161 for (it2 = 0; it2 < len2; it2++) {
3162 GstStructureField *field2 = GST_STRUCTURE_FIELD (struct2, it2);
3163 if (field1->name == field2->name) {
3164 GValue dest_value = { 0 };
3166 /* Get the intersection if any */
3167 if (gst_value_intersect (&dest_value, &field1->value, &field2->value)) {
3168 gst_structure_id_take_value (dest, field1->name, &dest_value);
3171 /* No intersection, return nothing */
3176 /* Field1 was only present in struct1, copy it over */
3178 gst_structure_id_set_value (dest, field1->name, &field1->value);
3181 /* Now iterate over the 2nd struct and copy over everything which
3182 * isn't present in the 1st struct (we've already taken care of
3183 * values being present in both just above) */
3184 for (it2 = 0; it2 < len2; it2++) {
3185 GstStructureField *field2 = GST_STRUCTURE_FIELD (struct2, it2);
3186 gboolean seenother = FALSE;
3187 for (it1 = 0; it1 < len1; it1++) {
3188 GstStructureField *field1 = GST_STRUCTURE_FIELD (struct1, it1);
3189 if (field1->name == field2->name) {
3195 gst_structure_id_set_value (dest, field2->name, &field2->value);
3202 gst_structure_free (dest);
3207 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3210 GstStructure *other = (GstStructure *) data;
3211 const GValue *val2 = gst_structure_id_get_value (other, id);
3213 if (G_LIKELY (val2)) {
3214 if (!gst_value_can_intersect (val1, val2)) {
3217 gint eq = gst_value_compare (val1, val2);
3219 if (eq == GST_VALUE_UNORDERED) {
3220 /* we need to try interseting */
3221 if (!gst_value_intersect (NULL, val1, val2)) {
3224 } else if (eq != GST_VALUE_EQUAL) {
3233 * gst_structure_can_intersect:
3234 * @struct1: a #GstStructure
3235 * @struct2: a #GstStructure
3237 * Tries intersecting @struct1 and @struct2 and reports whether the result
3238 * would not be empty.
3240 * Returns: %TRUE if intersection would not be empty
3243 gst_structure_can_intersect (const GstStructure * struct1,
3244 const GstStructure * struct2)
3246 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3247 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3249 if (G_UNLIKELY (struct1->name != struct2->name))
3252 /* tries to intersect if we have the field in both */
3253 return gst_structure_foreach ((GstStructure *) struct1,
3254 gst_caps_structure_can_intersect_field, (gpointer) struct2);
3259 * gst_structure_is_subset:
3260 * @subset: a #GstStructure
3261 * @superset: a potentially greater #GstStructure
3263 * Checks if @subset is a subset of @superset, i.e. has the same
3264 * structure name and for all fields that are existing in @superset,
3265 * @subset has a value that is a subset of the value in @superset.
3267 * Returns: %TRUE if @subset is a subset of @superset
3270 gst_structure_is_subset (const GstStructure * subset,
3271 const GstStructure * superset)
3273 guint it1, len1, it2, len2;
3275 g_assert (superset);
3277 if (G_UNLIKELY (superset->name != subset->name))
3280 len1 = GST_STRUCTURE_LEN (subset);
3281 len2 = GST_STRUCTURE_LEN (superset);
3285 for (it2 = 0; it2 < len2; it2++) {
3286 GstStructureField *superfield = GST_STRUCTURE_FIELD (superset, it2);
3287 gboolean seenother = FALSE;
3288 for (it1 = 0; it1 < len1; it1++) {
3289 GstStructureField *subfield = GST_STRUCTURE_FIELD (subset, it1);
3290 if (subfield->name == superfield->name) {
3292 gst_value_compare (&subfield->value, &superfield->value);
3295 /* If present and equal, stop iterating */
3296 if (comparison == GST_VALUE_EQUAL)
3299 /* Stop everything if ordered but unequal */
3300 if (comparison != GST_VALUE_UNORDERED)
3303 /* Stop everything if not a subset */
3304 if (!gst_value_is_subset (&subfield->value, &superfield->value))
3311 /* We did not see superfield in subfield */
3316 /* We saw everything from subset in the superset */
3322 * gst_structure_fixate:
3323 * @structure: a #GstStructure
3325 * Fixate all values in @structure using gst_value_fixate().
3326 * @structure will be modified in-place and should be writable.
3329 gst_structure_fixate (GstStructure * structure)
3331 g_return_if_fail (GST_IS_STRUCTURE (structure));
3333 gst_structure_foreach (structure, default_fixate, structure);
3337 _gst_structure_get_any_list (GstStructure * structure, GType type,
3338 const gchar * fieldname, GValueArray ** array)
3340 GstStructureField *field;
3341 GValue val = G_VALUE_INIT;
3343 g_return_val_if_fail (structure != NULL, FALSE);
3344 g_return_val_if_fail (fieldname != NULL, FALSE);
3345 g_return_val_if_fail (array != NULL, FALSE);
3347 field = gst_structure_get_field (structure, fieldname);
3349 if (field == NULL || G_VALUE_TYPE (&field->value) != type)
3352 g_value_init (&val, G_TYPE_VALUE_ARRAY);
3354 if (g_value_transform (&field->value, &val)) {
3355 *array = g_value_get_boxed (&val);
3359 g_value_unset (&val);
3364 * gst_structure_get_array:
3365 * @structure: a #GstStructure
3366 * @fieldname: the name of a field
3367 * @array: (out): a pointer to a #GValueArray
3369 * This is useful in language bindings where unknown #GValue types are not
3370 * supported. This function will convert the %GST_TYPE_ARRAY into a newly
3371 * allocated #GValueArray and return it through @array. Be aware that this is
3372 * slower then getting the #GValue directly.
3374 * Returns: %TRUE if the value could be set correctly. If there was no field
3375 * with @fieldname or the existing field did not contain a %GST_TYPE_ARRAY,
3376 * this function returns %FALSE.
3381 gst_structure_get_array (GstStructure * structure, const gchar * fieldname,
3382 GValueArray ** array)
3384 return _gst_structure_get_any_list (structure, GST_TYPE_ARRAY, fieldname,
3389 * gst_structure_get_list:
3390 * @structure: a #GstStructure
3391 * @fieldname: the name of a field
3392 * @array: (out): a pointer to a #GValueArray
3394 * This is useful in language bindings where unknown #GValue types are not
3395 * supported. This function will convert the %GST_TYPE_LIST into a newly
3396 * allocated GValueArray and return it through @array. Be aware that this is
3397 * slower then getting the #GValue directly.
3399 * Returns: %TRUE if the value could be set correctly. If there was no field
3400 * with @fieldname or the existing field did not contain a %GST_TYPE_LIST, this
3401 * function returns %FALSE.
3406 gst_structure_get_list (GstStructure * structure, const gchar * fieldname,
3407 GValueArray ** array)
3409 return _gst_structure_get_any_list (structure, GST_TYPE_LIST, fieldname,
3414 _gst_structure_set_any_list (GstStructure * structure, GType type,
3415 const gchar * fieldname, const GValueArray * array)
3417 GValue arval = G_VALUE_INIT;
3418 GValue value = G_VALUE_INIT;
3420 g_return_if_fail (structure != NULL);
3421 g_return_if_fail (fieldname != NULL);
3422 g_return_if_fail (array != NULL);
3423 g_return_if_fail (IS_MUTABLE (structure));
3425 g_value_init (&value, type);
3426 g_value_init (&arval, G_TYPE_VALUE_ARRAY);
3427 g_value_set_static_boxed (&arval, array);
3429 if (g_value_transform (&arval, &value)) {
3430 gst_structure_id_set_value_internal (structure,
3431 g_quark_from_string (fieldname), &value);
3433 g_warning ("Failed to convert a GValueArray");
3436 g_value_unset (&arval);
3437 g_value_unset (&value);
3441 * gst_structure_set_array:
3442 * @structure: a #GstStructure
3443 * @fieldname: the name of a field
3444 * @array: a pointer to a #GValueArray
3446 * This is useful in language bindings where unknown GValue types are not
3447 * supported. This function will convert a @array to %GST_TYPE_ARRAY and set
3448 * the field specified by @fieldname. Be aware that this is slower then using
3449 * %GST_TYPE_ARRAY in a #GValue directly.
3454 gst_structure_set_array (GstStructure * structure, const gchar * fieldname,
3455 const GValueArray * array)
3457 _gst_structure_set_any_list (structure, GST_TYPE_ARRAY, fieldname, array);
3461 * gst_structure_set_list:
3462 * @structure: a #GstStructure
3463 * @fieldname: the name of a field
3464 * @array: a pointer to a #GValueArray
3466 * This is useful in language bindings where unknown GValue types are not
3467 * supported. This function will convert a @array to %GST_TYPE_LIST and set
3468 * the field specified by @fieldname. Be aware that this is slower then using
3469 * %GST_TYPE_LIST in a #GValue directly.
3474 gst_structure_set_list (GstStructure * structure, const gchar * fieldname,
3475 const GValueArray * array)
3477 _gst_structure_set_any_list (structure, GST_TYPE_LIST, fieldname, array);
3481 * gst_structure_get_flags:
3482 * @structure: a #GstStructure
3483 * @fieldname: the name of a field
3484 * @flags_type: the flags type of a field
3485 * @value: (out): a pointer to an unsigned int to set
3487 * Sets the unsigned int pointed to by @value corresponding to the value of the
3488 * given field. Caller is responsible for making sure the field exists,
3489 * has the correct type and that the flagstype is correct.
3491 * Returns: %TRUE if the value could be set correctly. If there was no field
3492 * with @fieldname or the existing field did not contain flags or
3493 * did not contain flags of the given type, this function returns %FALSE.
3498 gst_structure_get_flags (const GstStructure * structure,
3499 const gchar * fieldname, GType flags_type, guint * value)
3501 GstStructureField *field;
3503 g_return_val_if_fail (structure != NULL, FALSE);
3504 g_return_val_if_fail (fieldname != NULL, FALSE);
3505 g_return_val_if_fail (flags_type != G_TYPE_INVALID, FALSE);
3506 g_return_val_if_fail (value != NULL, FALSE);
3508 field = gst_structure_get_field (structure, fieldname);
3512 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, flags_type))
3515 *value = g_value_get_flags (&field->value);