2 * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
4 * gststructure.c: lists of { GQuark, GValue } tuples
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:gststructure
24 * @short_description: Generic structure containing fields of names and values
25 * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
27 * A #GstStructure is a collection of key/value pairs. The keys are expressed
28 * as GQuarks and the values can be of any GType.
30 * In addition to the key/value pairs, a #GstStructure also has a name. The name
31 * starts with a letter and can be filled by letters, numbers and any of "/-_.:".
33 * #GstStructure is used by various GStreamer subsystems to store information
34 * in a flexible and extensible way. A #GstStructure does not have a refcount
35 * because it usually is part of a higher level object such as #GstCaps,
36 * #GstMessage, #GstEvent, #GstQuery. It provides a means to enforce mutability
37 * using the refcount of the parent with the gst_structure_set_parent_refcount()
40 * A #GstStructure can be created with gst_structure_new_empty() or
41 * gst_structure_new(), which both take a name and an optional set of
42 * key/value pairs along with the types of the values.
44 * Field values can be changed with gst_structure_set_value() or
45 * gst_structure_set().
47 * Field values can be retrieved with gst_structure_get_value() or the more
48 * convenient gst_structure_get_*() functions.
50 * Fields can be removed with gst_structure_remove_field() or
51 * gst_structure_remove_fields().
53 * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are
54 * not allowed. Strings may be %NULL however.
56 * Be aware that the current #GstCaps / #GstStructure serialization into string
57 * has limited support for nested #GstCaps / #GstStructure fields. It can only
58 * support one level of nesting. Using more levels will lead to unexpected
59 * behavior when using serialization features, such as gst_caps_to_string() or
60 * gst_value_serialize() and their counterparts.
69 #include "gst_private.h"
72 #include <gobject/gvaluecollector.h>
74 GST_DEBUG_CATEGORY_STATIC (gst_structure_debug);
75 #define GST_CAT_DEFAULT gst_structure_debug
77 typedef struct _GstStructureField GstStructureField;
79 struct _GstStructureField
89 /* owned by parent structure, NULL if no parent */
90 gint *parent_refcount;
95 #define GST_STRUCTURE_REFCOUNT(s) (((GstStructureImpl*)(s))->parent_refcount)
96 #define GST_STRUCTURE_FIELDS(s) (((GstStructureImpl*)(s))->fields)
98 #define GST_STRUCTURE_FIELD(structure, index) \
99 &g_array_index(GST_STRUCTURE_FIELDS(structure), GstStructureField, (index))
101 #define IS_MUTABLE(structure) \
102 (!GST_STRUCTURE_REFCOUNT(structure) || \
103 g_atomic_int_get (GST_STRUCTURE_REFCOUNT(structure)) == 1)
105 #define IS_TAGLIST(structure) \
106 (structure->name == GST_QUARK (TAGLIST))
108 static void gst_structure_set_field (GstStructure * structure,
109 GstStructureField * field);
110 static GstStructureField *gst_structure_get_field (const GstStructure *
111 structure, const gchar * fieldname);
112 static GstStructureField *gst_structure_id_get_field (const GstStructure *
113 structure, GQuark field);
114 static void gst_structure_transform_to_string (const GValue * src_value,
115 GValue * dest_value);
116 static GstStructure *gst_structure_copy_conditional (const GstStructure *
118 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
119 GValue * value, GType default_type);
120 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
122 GType _gst_structure_type = 0;
125 G_DEFINE_BOXED_TYPE (GstStructure, gst_structure,
126 gst_structure_copy_conditional, gst_structure_free);
129 _priv_gst_structure_initialize (void)
131 _gst_structure_type = gst_structure_get_type ();
133 g_value_register_transform_func (_gst_structure_type, G_TYPE_STRING,
134 gst_structure_transform_to_string);
136 GST_DEBUG_CATEGORY_INIT (gst_structure_debug, "structure", 0,
137 "GstStructure debug");
140 static GstStructure *
141 gst_structure_new_id_empty_with_size (GQuark quark, guint prealloc)
143 GstStructureImpl *structure;
145 structure = g_slice_new (GstStructureImpl);
146 ((GstStructure *) structure)->type = _gst_structure_type;
147 ((GstStructure *) structure)->name = quark;
148 GST_STRUCTURE_REFCOUNT (structure) = NULL;
149 GST_STRUCTURE_FIELDS (structure) =
150 g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
152 GST_TRACE ("created structure %p", structure);
154 return GST_STRUCTURE_CAST (structure);
158 * gst_structure_new_id_empty:
159 * @quark: name of new structure
161 * Creates a new, empty #GstStructure with the given name as a GQuark.
163 * Free-function: gst_structure_free
165 * Returns: (transfer full): a new, empty #GstStructure
168 gst_structure_new_id_empty (GQuark quark)
170 g_return_val_if_fail (quark != 0, NULL);
172 return gst_structure_new_id_empty_with_size (quark, 0);
175 #ifndef G_DISABLE_CHECKS
177 gst_structure_validate_name (const gchar * name)
181 g_return_val_if_fail (name != NULL, FALSE);
183 if (G_UNLIKELY (!g_ascii_isalpha (*name))) {
184 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
189 /* FIXME: test name string more */
191 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
193 if (G_UNLIKELY (*s != '\0')) {
194 GST_WARNING ("Invalid character '%c' at offset %" G_GUINTPTR_FORMAT " in"
195 " structure name: %s", *s, ((guintptr) s - (guintptr) name), name);
199 if (strncmp (name, "video/x-raw-", 12) == 0) {
200 g_warning ("0.10-style raw video caps are being created. Should be "
201 "video/x-raw,format=(string).. now.");
202 } else if (strncmp (name, "audio/x-raw-", 12) == 0) {
203 g_warning ("0.10-style raw audio caps are being created. Should be "
204 "audio/x-raw,format=(string).. now.");
212 * gst_structure_new_empty:
213 * @name: name of new structure
215 * Creates a new, empty #GstStructure with the given @name.
217 * See gst_structure_set_name() for constraints on the @name parameter.
219 * Free-function: gst_structure_free
221 * Returns: (transfer full): a new, empty #GstStructure
224 gst_structure_new_empty (const gchar * name)
226 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
228 return gst_structure_new_id_empty_with_size (g_quark_from_string (name), 0);
233 * @name: name of new structure
234 * @firstfield: name of first field to set
235 * @...: additional arguments
237 * Creates a new #GstStructure with the given name. Parses the
238 * list of variable arguments and sets fields to the values listed.
239 * Variable arguments should be passed as field name, field type,
240 * and value. Last variable argument should be %NULL.
242 * Free-function: gst_structure_free
244 * Returns: (transfer full): a new #GstStructure
247 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
249 GstStructure *structure;
252 va_start (varargs, firstfield);
253 structure = gst_structure_new_valist (name, firstfield, varargs);
260 * gst_structure_new_valist:
261 * @name: name of new structure
262 * @firstfield: name of first field to set
263 * @varargs: variable argument list
265 * Creates a new #GstStructure with the given @name. Structure fields
266 * are set according to the varargs in a manner similar to
267 * gst_structure_new().
269 * See gst_structure_set_name() for constraints on the @name parameter.
271 * Free-function: gst_structure_free
273 * Returns: (transfer full): a new #GstStructure
276 gst_structure_new_valist (const gchar * name,
277 const gchar * firstfield, va_list varargs)
279 GstStructure *structure;
281 structure = gst_structure_new_empty (name);
284 gst_structure_set_valist (structure, firstfield, varargs);
290 * gst_structure_set_parent_refcount:
291 * @structure: a #GstStructure
292 * @refcount: (in): a pointer to the parent's refcount
294 * Sets the parent_refcount field of #GstStructure. This field is used to
295 * determine whether a structure is mutable or not. This function should only be
296 * called by code implementing parent objects of #GstStructure, as described in
297 * the MT Refcounting section of the design documents.
299 * Returns: %TRUE if the parent refcount could be set.
302 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
304 g_return_val_if_fail (structure != NULL, FALSE);
306 /* if we have a parent_refcount already, we can only clear
307 * if with a NULL refcount */
308 if (GST_STRUCTURE_REFCOUNT (structure)) {
309 if (refcount != NULL) {
310 g_return_val_if_fail (refcount == NULL, FALSE);
314 if (refcount == NULL) {
315 g_return_val_if_fail (refcount != NULL, FALSE);
320 GST_STRUCTURE_REFCOUNT (structure) = refcount;
326 * gst_structure_copy:
327 * @structure: a #GstStructure to duplicate
329 * Duplicates a #GstStructure and all its fields and values.
331 * Free-function: gst_structure_free
333 * Returns: (transfer full): a new #GstStructure.
336 gst_structure_copy (const GstStructure * structure)
338 GstStructure *new_structure;
339 GstStructureField *field;
342 g_return_val_if_fail (structure != NULL, NULL);
344 len = GST_STRUCTURE_FIELDS (structure)->len;
345 new_structure = gst_structure_new_id_empty_with_size (structure->name, len);
347 for (i = 0; i < len; i++) {
348 GstStructureField new_field = { 0 };
350 field = GST_STRUCTURE_FIELD (structure, i);
352 new_field.name = field->name;
353 gst_value_init_and_copy (&new_field.value, &field->value);
354 g_array_append_val (GST_STRUCTURE_FIELDS (new_structure), new_field);
356 GST_CAT_TRACE (GST_CAT_PERFORMANCE, "doing copy %p -> %p",
357 structure, new_structure);
359 return new_structure;
363 * gst_structure_free:
364 * @structure: (in) (transfer full): the #GstStructure to free
366 * Frees a #GstStructure and all its fields and values. The structure must not
367 * have a parent when this function is called.
370 gst_structure_free (GstStructure * structure)
372 GstStructureField *field;
375 g_return_if_fail (structure != NULL);
376 g_return_if_fail (GST_STRUCTURE_REFCOUNT (structure) == NULL);
378 len = GST_STRUCTURE_FIELDS (structure)->len;
379 for (i = 0; i < len; i++) {
380 field = GST_STRUCTURE_FIELD (structure, i);
382 if (G_IS_VALUE (&field->value)) {
383 g_value_unset (&field->value);
386 g_array_free (GST_STRUCTURE_FIELDS (structure), TRUE);
388 memset (structure, 0xff, sizeof (GstStructure));
390 GST_TRACE ("free structure %p", structure);
392 g_slice_free1 (sizeof (GstStructureImpl), structure);
396 * gst_structure_get_name:
397 * @structure: a #GstStructure
399 * Get the name of @structure as a string.
401 * Returns: the name of the structure.
404 gst_structure_get_name (const GstStructure * structure)
406 g_return_val_if_fail (structure != NULL, NULL);
408 return g_quark_to_string (structure->name);
412 * gst_structure_has_name:
413 * @structure: a #GstStructure
414 * @name: structure name to check for
416 * Checks if the structure has the given name
418 * Returns: %TRUE if @name matches the name of the structure.
421 gst_structure_has_name (const GstStructure * structure, const gchar * name)
423 const gchar *structure_name;
425 g_return_val_if_fail (structure != NULL, FALSE);
426 g_return_val_if_fail (name != NULL, FALSE);
428 /* getting the string is cheap and comparing short strings is too
429 * should be faster than getting the quark for name and comparing the quarks
431 structure_name = g_quark_to_string (structure->name);
433 return (structure_name && strcmp (structure_name, name) == 0);
437 * gst_structure_get_name_id:
438 * @structure: a #GstStructure
440 * Get the name of @structure as a GQuark.
442 * Returns: the quark representing the name of the structure.
445 gst_structure_get_name_id (const GstStructure * structure)
447 g_return_val_if_fail (structure != NULL, 0);
449 return structure->name;
453 * gst_structure_set_name:
454 * @structure: a #GstStructure
455 * @name: the new name of the structure
457 * Sets the name of the structure to the given @name. The string
458 * provided is copied before being used. It must not be empty, start with a
459 * letter and can be followed by letters, numbers and any of "/-_.:".
462 gst_structure_set_name (GstStructure * structure, const gchar * name)
464 g_return_if_fail (structure != NULL);
465 g_return_if_fail (IS_MUTABLE (structure));
466 g_return_if_fail (gst_structure_validate_name (name));
468 structure->name = g_quark_from_string (name);
472 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
473 const GValue * value)
475 GstStructureField gsfield = { 0, {0,} };
477 gsfield.name = field;
478 gst_value_init_and_copy (&gsfield.value, value);
480 gst_structure_set_field (structure, &gsfield);
484 * gst_structure_id_set_value:
485 * @structure: a #GstStructure
486 * @field: a #GQuark representing a field
487 * @value: the new value of the field
489 * Sets the field with the given GQuark @field to @value. If the field
490 * does not exist, it is created. If the field exists, the previous
491 * value is replaced and freed.
494 gst_structure_id_set_value (GstStructure * structure,
495 GQuark field, const GValue * value)
498 g_return_if_fail (structure != NULL);
499 g_return_if_fail (G_IS_VALUE (value));
500 g_return_if_fail (IS_MUTABLE (structure));
502 gst_structure_id_set_value_internal (structure, field, value);
506 * gst_structure_set_value:
507 * @structure: a #GstStructure
508 * @fieldname: the name of the field to set
509 * @value: the new value of the field
511 * Sets the field with the given name @field to @value. If the field
512 * does not exist, it is created. If the field exists, the previous
513 * value is replaced and freed.
516 gst_structure_set_value (GstStructure * structure,
517 const gchar * fieldname, const GValue * value)
519 g_return_if_fail (structure != NULL);
520 g_return_if_fail (fieldname != NULL);
521 g_return_if_fail (G_IS_VALUE (value));
522 g_return_if_fail (IS_MUTABLE (structure));
524 gst_structure_id_set_value_internal (structure,
525 g_quark_from_string (fieldname), value);
529 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
532 GstStructureField gsfield = { 0, {0,} };
534 gsfield.name = field;
535 gsfield.value = *value;
537 gst_structure_set_field (structure, &gsfield);
539 /* we took ownership */
541 memset (value, 0, sizeof (GValue));
543 value->g_type = G_TYPE_INVALID;
548 * gst_structure_id_take_value:
549 * @structure: a #GstStructure
550 * @field: a #GQuark representing a field
551 * @value: (transfer full): the new value of the field
553 * Sets the field with the given GQuark @field to @value. If the field
554 * does not exist, it is created. If the field exists, the previous
555 * value is replaced and freed.
558 gst_structure_id_take_value (GstStructure * structure, GQuark field,
561 g_return_if_fail (structure != NULL);
562 g_return_if_fail (G_IS_VALUE (value));
563 g_return_if_fail (IS_MUTABLE (structure));
565 gst_structure_id_take_value_internal (structure, field, value);
569 * gst_structure_take_value:
570 * @structure: a #GstStructure
571 * @fieldname: the name of the field to set
572 * @value: (transfer full): the new value of the field
574 * Sets the field with the given name @field to @value. If the field
575 * does not exist, it is created. If the field exists, the previous
576 * value is replaced and freed. The function will take ownership of @value.
579 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
582 g_return_if_fail (structure != NULL);
583 g_return_if_fail (fieldname != NULL);
584 g_return_if_fail (G_IS_VALUE (value));
585 g_return_if_fail (IS_MUTABLE (structure));
587 gst_structure_id_take_value_internal (structure,
588 g_quark_from_string (fieldname), value);
592 gst_structure_set_valist_internal (GstStructure * structure,
593 const gchar * fieldname, va_list varargs)
599 GstStructureField field = { 0 };
601 field.name = g_quark_from_string (fieldname);
603 type = va_arg (varargs, GType);
605 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
606 if (G_UNLIKELY (err)) {
607 g_critical ("%s", err);
610 gst_structure_set_field (structure, &field);
612 fieldname = va_arg (varargs, gchar *);
618 * @structure: a #GstStructure
619 * @fieldname: the name of the field to set
620 * @...: variable arguments
622 * Parses the variable arguments and sets fields accordingly. Fields that
623 * weren't already part of the structure are added as needed.
624 * Variable arguments should be in the form field name, field type
625 * (as a GType), value(s). The last variable argument should be %NULL.
628 gst_structure_set (GstStructure * structure, const gchar * field, ...)
632 g_return_if_fail (structure != NULL);
633 g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
635 va_start (varargs, field);
636 gst_structure_set_valist_internal (structure, field, varargs);
641 * gst_structure_set_valist:
642 * @structure: a #GstStructure
643 * @fieldname: the name of the field to set
644 * @varargs: variable arguments
646 * va_list form of gst_structure_set().
649 gst_structure_set_valist (GstStructure * structure,
650 const gchar * fieldname, va_list varargs)
652 g_return_if_fail (structure != NULL);
653 g_return_if_fail (IS_MUTABLE (structure));
655 gst_structure_set_valist_internal (structure, fieldname, varargs);
659 gst_structure_id_set_valist_internal (GstStructure * structure,
660 GQuark fieldname, va_list varargs)
666 GstStructureField field = { 0 };
668 field.name = fieldname;
669 type = va_arg (varargs, GType);
671 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
672 if (G_UNLIKELY (err)) {
673 g_critical ("%s", err);
676 gst_structure_set_field (structure, &field);
678 fieldname = va_arg (varargs, GQuark);
683 * gst_structure_id_set:
684 * @structure: a #GstStructure
685 * @fieldname: the GQuark for the name of the field to set
686 * @...: variable arguments
688 * Identical to gst_structure_set, except that field names are
689 * passed using the GQuark for the field name. This allows more efficient
690 * setting of the structure if the caller already knows the associated
692 * The last variable argument must be %NULL.
695 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
699 g_return_if_fail (structure != NULL);
701 va_start (varargs, field);
702 gst_structure_id_set_valist_internal (structure, field, varargs);
707 * gst_structure_id_set_valist:
708 * @structure: a #GstStructure
709 * @fieldname: the name of the field to set
710 * @varargs: variable arguments
712 * va_list form of gst_structure_id_set().
715 gst_structure_id_set_valist (GstStructure * structure,
716 GQuark fieldname, va_list varargs)
718 g_return_if_fail (structure != NULL);
719 g_return_if_fail (IS_MUTABLE (structure));
721 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
725 * gst_structure_new_id:
726 * @name_quark: name of new structure
727 * @field_quark: the GQuark for the name of the field to set
728 * @...: variable arguments
730 * Creates a new #GstStructure with the given name as a GQuark, followed by
731 * fieldname quark, GType, argument(s) "triplets" in the same format as
732 * gst_structure_id_set(). Basically a convenience wrapper around
733 * gst_structure_new_id_empty() and gst_structure_id_set().
735 * The last variable argument must be %NULL (or 0).
737 * Free-function: gst_structure_free
739 * Returns: (transfer full): a new #GstStructure
742 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
747 g_return_val_if_fail (name_quark != 0, NULL);
748 g_return_val_if_fail (field_quark != 0, NULL);
750 s = gst_structure_new_id_empty (name_quark);
752 va_start (varargs, field_quark);
753 gst_structure_id_set_valist_internal (s, field_quark, varargs);
759 #if GST_VERSION_NANO == 1
760 #define GIT_G_WARNING g_warning
762 #define GIT_G_WARNING GST_WARNING
765 /* If the structure currently contains a field with the same name, it is
766 * replaced with the provided field. Otherwise, the field is added to the
767 * structure. The field's value is not deeply copied.
770 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
772 GstStructureField *f;
773 GType field_value_type;
776 len = GST_STRUCTURE_FIELDS (structure)->len;
778 field_value_type = G_VALUE_TYPE (&field->value);
779 if (field_value_type == G_TYPE_STRING) {
782 s = g_value_get_string (&field->value);
783 /* only check for NULL strings in taglists, as they are allowed in message
784 * structs, e.g. error message debug strings */
785 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
787 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
788 "Please file a bug.", g_quark_to_string (field->name));
789 g_value_unset (&field->value);
792 /* empty strings never make sense */
793 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
794 "Please file a bug.", g_quark_to_string (field->name));
795 g_value_unset (&field->value);
798 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
799 g_warning ("Trying to set string on %s field '%s', but string is not "
800 "valid UTF-8. Please file a bug.",
801 IS_TAGLIST (structure) ? "taglist" : "structure",
802 g_quark_to_string (field->name));
803 g_value_unset (&field->value);
806 } else if (G_UNLIKELY (field_value_type == G_TYPE_DATE)) {
809 d = g_value_get_boxed (&field->value);
810 /* only check for NULL GDates in taglists, as they might make sense
811 * in other, generic structs */
812 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
813 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
814 "Please file a bug.", g_quark_to_string (field->name));
815 g_value_unset (&field->value);
817 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
819 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
820 IS_TAGLIST (structure) ? "taglist" : "structure",
821 g_quark_to_string (field->name));
822 g_value_unset (&field->value);
827 for (i = 0; i < len; i++) {
828 f = GST_STRUCTURE_FIELD (structure, i);
830 if (G_UNLIKELY (f->name == field->name)) {
831 g_value_unset (&f->value);
832 memcpy (f, field, sizeof (GstStructureField));
837 g_array_append_val (GST_STRUCTURE_FIELDS (structure), *field);
840 /* If there is no field with the given ID, NULL is returned.
842 static GstStructureField *
843 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
845 GstStructureField *field;
848 len = GST_STRUCTURE_FIELDS (structure)->len;
850 for (i = 0; i < len; i++) {
851 field = GST_STRUCTURE_FIELD (structure, i);
853 if (G_UNLIKELY (field->name == field_id))
860 /* If there is no field with the given ID, NULL is returned.
862 static GstStructureField *
863 gst_structure_get_field (const GstStructure * structure,
864 const gchar * fieldname)
866 g_return_val_if_fail (structure != NULL, NULL);
867 g_return_val_if_fail (fieldname != NULL, NULL);
869 return gst_structure_id_get_field (structure,
870 g_quark_from_string (fieldname));
874 * gst_structure_get_value:
875 * @structure: a #GstStructure
876 * @fieldname: the name of the field to get
878 * Get the value of the field with name @fieldname.
880 * Returns: the #GValue corresponding to the field with the given name.
883 gst_structure_get_value (const GstStructure * structure,
884 const gchar * fieldname)
886 GstStructureField *field;
888 g_return_val_if_fail (structure != NULL, NULL);
889 g_return_val_if_fail (fieldname != NULL, NULL);
891 field = gst_structure_get_field (structure, fieldname);
895 return &field->value;
899 * gst_structure_id_get_value:
900 * @structure: a #GstStructure
901 * @field: the #GQuark of the field to get
903 * Get the value of the field with GQuark @field.
905 * Returns: the #GValue corresponding to the field with the given name
909 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
911 GstStructureField *gsfield;
913 g_return_val_if_fail (structure != NULL, NULL);
915 gsfield = gst_structure_id_get_field (structure, field);
919 return &gsfield->value;
923 * gst_structure_remove_field:
924 * @structure: a #GstStructure
925 * @fieldname: the name of the field to remove
927 * Removes the field with the given name. If the field with the given
928 * name does not exist, the structure is unchanged.
931 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
933 GstStructureField *field;
937 g_return_if_fail (structure != NULL);
938 g_return_if_fail (fieldname != NULL);
939 g_return_if_fail (IS_MUTABLE (structure));
941 id = g_quark_from_string (fieldname);
942 len = GST_STRUCTURE_FIELDS (structure)->len;
944 for (i = 0; i < len; i++) {
945 field = GST_STRUCTURE_FIELD (structure, i);
947 if (field->name == id) {
948 if (G_IS_VALUE (&field->value)) {
949 g_value_unset (&field->value);
951 GST_STRUCTURE_FIELDS (structure) =
952 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
959 * gst_structure_remove_fields:
960 * @structure: a #GstStructure
961 * @fieldname: the name of the field to remove
962 * @...: %NULL-terminated list of more fieldnames to remove
964 * Removes the fields with the given names. If a field does not exist, the
965 * argument is ignored.
968 gst_structure_remove_fields (GstStructure * structure,
969 const gchar * fieldname, ...)
973 g_return_if_fail (structure != NULL);
974 g_return_if_fail (fieldname != NULL);
975 /* mutability checked in remove_field */
977 va_start (varargs, fieldname);
978 gst_structure_remove_fields_valist (structure, fieldname, varargs);
983 * gst_structure_remove_fields_valist:
984 * @structure: a #GstStructure
985 * @fieldname: the name of the field to remove
986 * @varargs: %NULL-terminated list of more fieldnames to remove
988 * va_list form of gst_structure_remove_fields().
991 gst_structure_remove_fields_valist (GstStructure * structure,
992 const gchar * fieldname, va_list varargs)
994 gchar *field = (gchar *) fieldname;
996 g_return_if_fail (structure != NULL);
997 g_return_if_fail (fieldname != NULL);
998 /* mutability checked in remove_field */
1001 gst_structure_remove_field (structure, field);
1002 field = va_arg (varargs, char *);
1007 * gst_structure_remove_all_fields:
1008 * @structure: a #GstStructure
1010 * Removes all fields in a GstStructure.
1013 gst_structure_remove_all_fields (GstStructure * structure)
1015 GstStructureField *field;
1018 g_return_if_fail (structure != NULL);
1019 g_return_if_fail (IS_MUTABLE (structure));
1021 for (i = GST_STRUCTURE_FIELDS (structure)->len - 1; i >= 0; i--) {
1022 field = GST_STRUCTURE_FIELD (structure, i);
1024 if (G_IS_VALUE (&field->value)) {
1025 g_value_unset (&field->value);
1027 GST_STRUCTURE_FIELDS (structure) =
1028 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1033 * gst_structure_get_field_type:
1034 * @structure: a #GstStructure
1035 * @fieldname: the name of the field
1037 * Finds the field with the given name, and returns the type of the
1038 * value it contains. If the field is not found, G_TYPE_INVALID is
1041 * Returns: the #GValue of the field
1044 gst_structure_get_field_type (const GstStructure * structure,
1045 const gchar * fieldname)
1047 GstStructureField *field;
1049 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1050 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1052 field = gst_structure_get_field (structure, fieldname);
1054 return G_TYPE_INVALID;
1056 return G_VALUE_TYPE (&field->value);
1060 * gst_structure_n_fields:
1061 * @structure: a #GstStructure
1063 * Get the number of fields in the structure.
1065 * Returns: the number of fields in the structure
1068 gst_structure_n_fields (const GstStructure * structure)
1070 g_return_val_if_fail (structure != NULL, 0);
1072 return GST_STRUCTURE_FIELDS (structure)->len;
1076 * gst_structure_nth_field_name:
1077 * @structure: a #GstStructure
1078 * @index: the index to get the name of
1080 * Get the name of the given field number, counting from 0 onwards.
1082 * Returns: the name of the given field number
1085 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1087 GstStructureField *field;
1089 g_return_val_if_fail (structure != NULL, NULL);
1090 g_return_val_if_fail (index < GST_STRUCTURE_FIELDS (structure)->len, NULL);
1092 field = GST_STRUCTURE_FIELD (structure, index);
1094 return g_quark_to_string (field->name);
1098 * gst_structure_foreach:
1099 * @structure: a #GstStructure
1100 * @func: (scope call): a function to call for each field
1101 * @user_data: (closure): private data
1103 * Calls the provided function once for each field in the #GstStructure. The
1104 * function must not modify the fields. Also see gst_structure_map_in_place()
1105 * and gst_structure_filter_and_map_in_place().
1107 * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
1111 gst_structure_foreach (const GstStructure * structure,
1112 GstStructureForeachFunc func, gpointer user_data)
1115 GstStructureField *field;
1118 g_return_val_if_fail (structure != NULL, FALSE);
1119 g_return_val_if_fail (func != NULL, FALSE);
1121 len = GST_STRUCTURE_FIELDS (structure)->len;
1123 for (i = 0; i < len; i++) {
1124 field = GST_STRUCTURE_FIELD (structure, i);
1126 ret = func (field->name, &field->value, user_data);
1127 if (G_UNLIKELY (!ret))
1135 * gst_structure_map_in_place:
1136 * @structure: a #GstStructure
1137 * @func: (scope call): a function to call for each field
1138 * @user_data: (closure): private data
1140 * Calls the provided function once for each field in the #GstStructure. In
1141 * contrast to gst_structure_foreach(), the function may modify but not delete the
1142 * fields. The structure must be mutable.
1144 * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
1148 gst_structure_map_in_place (GstStructure * structure,
1149 GstStructureMapFunc func, gpointer user_data)
1152 GstStructureField *field;
1155 g_return_val_if_fail (structure != NULL, FALSE);
1156 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1157 g_return_val_if_fail (func != NULL, FALSE);
1158 len = GST_STRUCTURE_FIELDS (structure)->len;
1160 for (i = 0; i < len; i++) {
1161 field = GST_STRUCTURE_FIELD (structure, i);
1163 ret = func (field->name, &field->value, user_data);
1172 * gst_structure_filter_and_map_in_place:
1173 * @structure: a #GstStructure
1174 * @func: (scope call): a function to call for each field
1175 * @user_data: (closure): private data
1177 * Calls the provided function once for each field in the #GstStructure. In
1178 * contrast to gst_structure_foreach(), the function may modify the fields.
1179 * In contrast to gst_structure_map_in_place(), the field is removed from
1180 * the structure if %FALSE is returned from the function.
1181 * The structure must be mutable.
1186 gst_structure_filter_and_map_in_place (GstStructure * structure,
1187 GstStructureFilterMapFunc func, gpointer user_data)
1190 GstStructureField *field;
1193 g_return_if_fail (structure != NULL);
1194 g_return_if_fail (IS_MUTABLE (structure));
1195 g_return_if_fail (func != NULL);
1196 len = GST_STRUCTURE_FIELDS (structure)->len;
1198 for (i = 0; i < len;) {
1199 field = GST_STRUCTURE_FIELD (structure, i);
1201 ret = func (field->name, &field->value, user_data);
1204 if (G_IS_VALUE (&field->value)) {
1205 g_value_unset (&field->value);
1207 GST_STRUCTURE_FIELDS (structure) =
1208 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1209 len = GST_STRUCTURE_FIELDS (structure)->len;
1217 * gst_structure_id_has_field:
1218 * @structure: a #GstStructure
1219 * @field: #GQuark of the field name
1221 * Check if @structure contains a field named @field.
1223 * Returns: %TRUE if the structure contains a field with the given name
1226 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1228 GstStructureField *f;
1230 g_return_val_if_fail (structure != NULL, FALSE);
1231 g_return_val_if_fail (field != 0, FALSE);
1233 f = gst_structure_id_get_field (structure, field);
1239 * gst_structure_has_field:
1240 * @structure: a #GstStructure
1241 * @fieldname: the name of a field
1243 * Check if @structure contains a field named @fieldname.
1245 * Returns: %TRUE if the structure contains a field with the given name
1248 gst_structure_has_field (const GstStructure * structure,
1249 const gchar * fieldname)
1251 g_return_val_if_fail (structure != NULL, FALSE);
1252 g_return_val_if_fail (fieldname != NULL, FALSE);
1254 return gst_structure_id_has_field (structure,
1255 g_quark_from_string (fieldname));
1259 * gst_structure_id_has_field_typed:
1260 * @structure: a #GstStructure
1261 * @field: #GQuark of the field name
1262 * @type: the type of a value
1264 * Check if @structure contains a field named @field and with GType @type.
1266 * Returns: %TRUE if the structure contains a field with the given name and type
1269 gst_structure_id_has_field_typed (const GstStructure * structure,
1270 GQuark field, GType type)
1272 GstStructureField *f;
1274 g_return_val_if_fail (structure != NULL, FALSE);
1275 g_return_val_if_fail (field != 0, FALSE);
1277 f = gst_structure_id_get_field (structure, field);
1281 return (G_VALUE_TYPE (&f->value) == type);
1285 * gst_structure_has_field_typed:
1286 * @structure: a #GstStructure
1287 * @fieldname: the name of a field
1288 * @type: the type of a value
1290 * Check if @structure contains a field named @fieldname and with GType @type.
1292 * Returns: %TRUE if the structure contains a field with the given name and type
1295 gst_structure_has_field_typed (const GstStructure * structure,
1296 const gchar * fieldname, GType type)
1298 g_return_val_if_fail (structure != NULL, FALSE);
1299 g_return_val_if_fail (fieldname != NULL, FALSE);
1301 return gst_structure_id_has_field_typed (structure,
1302 g_quark_from_string (fieldname), type);
1305 /* utility functions */
1308 * gst_structure_get_boolean:
1309 * @structure: a #GstStructure
1310 * @fieldname: the name of a field
1311 * @value: (out): a pointer to a #gboolean to set
1313 * Sets the boolean pointed to by @value corresponding to the value of the
1314 * given field. Caller is responsible for making sure the field exists
1315 * and has the correct type.
1317 * Returns: %TRUE if the value could be set correctly. If there was no field
1318 * with @fieldname or the existing field did not contain a boolean, this
1319 * function returns %FALSE.
1322 gst_structure_get_boolean (const GstStructure * structure,
1323 const gchar * fieldname, gboolean * value)
1325 GstStructureField *field;
1327 g_return_val_if_fail (structure != NULL, FALSE);
1328 g_return_val_if_fail (fieldname != NULL, FALSE);
1330 field = gst_structure_get_field (structure, fieldname);
1332 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_BOOLEAN)
1335 *value = gst_g_value_get_boolean_unchecked (&field->value);
1341 * gst_structure_get_int:
1342 * @structure: a #GstStructure
1343 * @fieldname: the name of a field
1344 * @value: (out): a pointer to an int to set
1346 * Sets the int pointed to by @value corresponding to the value of the
1347 * given field. Caller is responsible for making sure the field exists
1348 * and has the correct type.
1350 * Returns: %TRUE if the value could be set correctly. If there was no field
1351 * with @fieldname or the existing field did not contain an int, this function
1355 gst_structure_get_int (const GstStructure * structure,
1356 const gchar * fieldname, gint * value)
1358 GstStructureField *field;
1360 g_return_val_if_fail (structure != NULL, FALSE);
1361 g_return_val_if_fail (fieldname != NULL, FALSE);
1362 g_return_val_if_fail (value != NULL, FALSE);
1364 field = gst_structure_get_field (structure, fieldname);
1366 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT)
1369 *value = gst_g_value_get_int_unchecked (&field->value);
1375 * gst_structure_get_uint:
1376 * @structure: a #GstStructure
1377 * @fieldname: the name of a field
1378 * @value: (out): a pointer to a uint to set
1380 * Sets the uint pointed to by @value corresponding to the value of the
1381 * given field. Caller is responsible for making sure the field exists
1382 * and has the correct type.
1384 * Returns: %TRUE if the value could be set correctly. If there was no field
1385 * with @fieldname or the existing field did not contain a uint, this function
1389 gst_structure_get_uint (const GstStructure * structure,
1390 const gchar * fieldname, guint * value)
1392 GstStructureField *field;
1394 g_return_val_if_fail (structure != NULL, FALSE);
1395 g_return_val_if_fail (fieldname != NULL, FALSE);
1396 g_return_val_if_fail (value != NULL, FALSE);
1398 field = gst_structure_get_field (structure, fieldname);
1400 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT)
1403 *value = gst_g_value_get_uint_unchecked (&field->value);
1409 * gst_structure_get_int64:
1410 * @structure: a #GstStructure
1411 * @fieldname: the name of a field
1412 * @value: (out): a pointer to a #gint64 to set
1414 * Sets the #gint64 pointed to by @value corresponding to the value of the
1415 * given field. Caller is responsible for making sure the field exists
1416 * and has the correct type.
1418 * Returns: %TRUE if the value could be set correctly. If there was no field
1419 * with @fieldname or the existing field did not contain a #gint64, this function
1425 gst_structure_get_int64 (const GstStructure * structure,
1426 const gchar * fieldname, gint64 * value)
1428 GstStructureField *field;
1430 g_return_val_if_fail (structure != NULL, FALSE);
1431 g_return_val_if_fail (fieldname != NULL, FALSE);
1432 g_return_val_if_fail (value != NULL, FALSE);
1434 field = gst_structure_get_field (structure, fieldname);
1436 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT64)
1439 *value = gst_g_value_get_int64_unchecked (&field->value);
1445 * gst_structure_get_uint64:
1446 * @structure: a #GstStructure
1447 * @fieldname: the name of a field
1448 * @value: (out): a pointer to a #guint64 to set
1450 * Sets the #guint64 pointed to by @value corresponding to the value of the
1451 * given field. Caller is responsible for making sure the field exists
1452 * and has the correct type.
1454 * Returns: %TRUE if the value could be set correctly. If there was no field
1455 * with @fieldname or the existing field did not contain a #guint64, this function
1461 gst_structure_get_uint64 (const GstStructure * structure,
1462 const gchar * fieldname, guint64 * value)
1464 GstStructureField *field;
1466 g_return_val_if_fail (structure != NULL, FALSE);
1467 g_return_val_if_fail (fieldname != NULL, FALSE);
1468 g_return_val_if_fail (value != NULL, FALSE);
1470 field = gst_structure_get_field (structure, fieldname);
1472 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT64)
1475 *value = gst_g_value_get_uint64_unchecked (&field->value);
1481 * gst_structure_get_date:
1482 * @structure: a #GstStructure
1483 * @fieldname: the name of a field
1484 * @value: (out callee-allocates): a pointer to a #GDate to set
1486 * Sets the date pointed to by @value corresponding to the date of the
1487 * given field. Caller is responsible for making sure the field exists
1488 * and has the correct type.
1490 * On success @value will point to a newly-allocated copy of the date which
1491 * should be freed with g_date_free() when no longer needed (note: this is
1492 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1493 * copy of the string).
1495 * Returns: %TRUE if the value could be set correctly. If there was no field
1496 * with @fieldname or the existing field did not contain a data, this function
1500 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1503 GstStructureField *field;
1505 g_return_val_if_fail (structure != NULL, FALSE);
1506 g_return_val_if_fail (fieldname != NULL, FALSE);
1507 g_return_val_if_fail (value != NULL, FALSE);
1509 field = gst_structure_get_field (structure, fieldname);
1511 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DATE)
1514 /* FIXME: 2.0 g_value_dup_boxed() -> g_value_get_boxed() */
1515 *value = g_value_dup_boxed (&field->value);
1521 * gst_structure_get_date_time:
1522 * @structure: a #GstStructure
1523 * @fieldname: the name of a field
1524 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1526 * Sets the datetime pointed to by @value corresponding to the datetime of the
1527 * given field. Caller is responsible for making sure the field exists
1528 * and has the correct type.
1530 * On success @value will point to a reference of the datetime which
1531 * should be unreffed with gst_date_time_unref() when no longer needed
1532 * (note: this is inconsistent with e.g. gst_structure_get_string()
1533 * which doesn't return a copy of the string).
1535 * Returns: %TRUE if the value could be set correctly. If there was no field
1536 * with @fieldname or the existing field did not contain a data, this function
1540 gst_structure_get_date_time (const GstStructure * structure,
1541 const gchar * fieldname, GstDateTime ** value)
1543 GstStructureField *field;
1545 g_return_val_if_fail (structure != NULL, FALSE);
1546 g_return_val_if_fail (fieldname != NULL, FALSE);
1547 g_return_val_if_fail (value != NULL, FALSE);
1549 field = gst_structure_get_field (structure, fieldname);
1553 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1556 /* FIXME 2.0: g_value_dup_boxed() -> g_value_get_boxed() */
1557 *value = g_value_dup_boxed (&field->value);
1563 * gst_structure_get_clock_time:
1564 * @structure: a #GstStructure
1565 * @fieldname: the name of a field
1566 * @value: (out): a pointer to a #GstClockTime to set
1568 * Sets the clock time pointed to by @value corresponding to the clock time
1569 * of the given field. Caller is responsible for making sure the field exists
1570 * and has the correct type.
1572 * Returns: %TRUE if the value could be set correctly. If there was no field
1573 * with @fieldname or the existing field did not contain a #GstClockTime, this
1574 * function returns %FALSE.
1577 gst_structure_get_clock_time (const GstStructure * structure,
1578 const gchar * fieldname, GstClockTime * value)
1580 return gst_structure_get_uint64 (structure, fieldname, value);
1584 * gst_structure_get_double:
1585 * @structure: a #GstStructure
1586 * @fieldname: the name of a field
1587 * @value: (out): a pointer to a gdouble to set
1589 * Sets the double pointed to by @value corresponding to the value of the
1590 * given field. Caller is responsible for making sure the field exists
1591 * and has the correct type.
1593 * Returns: %TRUE if the value could be set correctly. If there was no field
1594 * with @fieldname or the existing field did not contain a double, this
1595 * function returns %FALSE.
1598 gst_structure_get_double (const GstStructure * structure,
1599 const gchar * fieldname, gdouble * value)
1601 GstStructureField *field;
1603 g_return_val_if_fail (structure != NULL, FALSE);
1604 g_return_val_if_fail (fieldname != NULL, FALSE);
1605 g_return_val_if_fail (value != NULL, FALSE);
1607 field = gst_structure_get_field (structure, fieldname);
1609 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DOUBLE)
1612 *value = gst_g_value_get_double_unchecked (&field->value);
1618 * gst_structure_get_string:
1619 * @structure: a #GstStructure
1620 * @fieldname: the name of a field
1622 * Finds the field corresponding to @fieldname, and returns the string
1623 * contained in the field's value. Caller is responsible for making
1624 * sure the field exists and has the correct type.
1626 * The string should not be modified, and remains valid until the next
1627 * call to a gst_structure_*() function with the given structure.
1629 * Returns: (nullable): a pointer to the string or %NULL when the
1630 * field did not exist or did not contain a string.
1633 gst_structure_get_string (const GstStructure * structure,
1634 const gchar * fieldname)
1636 GstStructureField *field;
1638 g_return_val_if_fail (structure != NULL, NULL);
1639 g_return_val_if_fail (fieldname != NULL, NULL);
1641 field = gst_structure_get_field (structure, fieldname);
1643 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_STRING)
1646 return gst_g_value_get_string_unchecked (&field->value);
1650 * gst_structure_get_enum:
1651 * @structure: a #GstStructure
1652 * @fieldname: the name of a field
1653 * @enumtype: the enum type of a field
1654 * @value: (out): a pointer to an int to set
1656 * Sets the int pointed to by @value corresponding to the value of the
1657 * given field. Caller is responsible for making sure the field exists,
1658 * has the correct type and that the enumtype is correct.
1660 * Returns: %TRUE if the value could be set correctly. If there was no field
1661 * with @fieldname or the existing field did not contain an enum of the given
1662 * type, this function returns %FALSE.
1665 gst_structure_get_enum (const GstStructure * structure,
1666 const gchar * fieldname, GType enumtype, gint * value)
1668 GstStructureField *field;
1670 g_return_val_if_fail (structure != NULL, FALSE);
1671 g_return_val_if_fail (fieldname != NULL, FALSE);
1672 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1673 g_return_val_if_fail (value != NULL, FALSE);
1675 field = gst_structure_get_field (structure, fieldname);
1679 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1682 *value = g_value_get_enum (&field->value);
1688 * gst_structure_get_fraction:
1689 * @structure: a #GstStructure
1690 * @fieldname: the name of a field
1691 * @value_numerator: (out): a pointer to an int to set
1692 * @value_denominator: (out): a pointer to an int to set
1694 * Sets the integers pointed to by @value_numerator and @value_denominator
1695 * corresponding to the value of the given field. Caller is responsible
1696 * for making sure the field exists and has the correct type.
1698 * Returns: %TRUE if the values could be set correctly. If there was no field
1699 * with @fieldname or the existing field did not contain a GstFraction, this
1700 * function returns %FALSE.
1703 gst_structure_get_fraction (const GstStructure * structure,
1704 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1706 GstStructureField *field;
1708 g_return_val_if_fail (structure != NULL, FALSE);
1709 g_return_val_if_fail (fieldname != NULL, FALSE);
1710 g_return_val_if_fail (value_numerator != NULL, FALSE);
1711 g_return_val_if_fail (value_denominator != NULL, FALSE);
1713 field = gst_structure_get_field (structure, fieldname);
1715 if (field == NULL || G_VALUE_TYPE (&field->value) != GST_TYPE_FRACTION)
1718 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1719 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1725 * gst_structure_get_flagset:
1726 * @structure: a #GstStructure
1727 * @fieldname: the name of a field
1728 * @value_flags: (out) (allow-none): a pointer to a guint for the flags field
1729 * @value_mask: (out) (allow-none): a pointer to a guint for the mask field
1731 * Read the GstFlagSet flags and mask out of the structure into the
1732 * provided pointers.
1734 * Returns: %TRUE if the values could be set correctly. If there was no field
1735 * with @fieldname or the existing field did not contain a GstFlagSet, this
1736 * function returns %FALSE.
1741 gst_structure_get_flagset (const GstStructure * structure,
1742 const gchar * fieldname, guint * value_flags, guint * value_mask)
1744 GstStructureField *field;
1746 g_return_val_if_fail (structure != NULL, FALSE);
1747 g_return_val_if_fail (fieldname != NULL, FALSE);
1749 field = gst_structure_get_field (structure, fieldname);
1751 if (field == NULL || !GST_VALUE_HOLDS_FLAG_SET (&field->value))
1755 *value_flags = gst_value_get_flagset_flags (&field->value);
1757 *value_mask = gst_value_get_flagset_mask (&field->value);
1762 typedef struct _GstStructureAbbreviation
1764 const gchar *type_name;
1767 GstStructureAbbreviation;
1769 /* return a copy of an array of GstStructureAbbreviation containing all the
1770 * known type_string, GType maps, including abbreviations for common types */
1771 static GstStructureAbbreviation *
1772 gst_structure_get_abbrs (gint * n_abbrs)
1774 static GstStructureAbbreviation *abbrs = NULL;
1775 static volatile gsize num = 0;
1777 if (g_once_init_enter (&num)) {
1778 /* dynamically generate the array */
1780 GstStructureAbbreviation dyn_abbrs[] = {
1785 {"uint", G_TYPE_UINT}
1789 {"float", G_TYPE_FLOAT}
1793 {"double", G_TYPE_DOUBLE}
1795 {"d", G_TYPE_DOUBLE}
1797 {"buffer", GST_TYPE_BUFFER}
1799 {"fraction", GST_TYPE_FRACTION}
1801 {"boolean", G_TYPE_BOOLEAN}
1803 {"bool", G_TYPE_BOOLEAN}
1805 {"b", G_TYPE_BOOLEAN}
1807 {"string", G_TYPE_STRING}
1809 {"str", G_TYPE_STRING}
1811 {"s", G_TYPE_STRING}
1813 {"structure", GST_TYPE_STRUCTURE}
1815 {"date", G_TYPE_DATE}
1817 {"datetime", GST_TYPE_DATE_TIME}
1819 {"bitmask", GST_TYPE_BITMASK}
1821 {"sample", GST_TYPE_SAMPLE}
1823 {"taglist", GST_TYPE_TAG_LIST}
1825 _num = G_N_ELEMENTS (dyn_abbrs);
1826 /* permanently allocate and copy the array now */
1827 abbrs = g_new0 (GstStructureAbbreviation, _num);
1828 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1829 g_once_init_leave (&num, _num);
1836 /* given a type_name that could be a type abbreviation or a registered GType,
1837 * return a matching GType */
1839 gst_structure_gtype_from_abbr (const char *type_name)
1842 GstStructureAbbreviation *abbrs;
1845 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1847 abbrs = gst_structure_get_abbrs (&n_abbrs);
1849 for (i = 0; i < n_abbrs; i++) {
1850 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1851 return abbrs[i].type;
1855 /* this is the fallback */
1856 return g_type_from_name (type_name);
1860 gst_structure_to_abbr (GType type)
1863 GstStructureAbbreviation *abbrs;
1866 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1868 abbrs = gst_structure_get_abbrs (&n_abbrs);
1870 for (i = 0; i < n_abbrs; i++) {
1871 if (type == abbrs[i].type) {
1872 return abbrs[i].type_name;
1876 return g_type_name (type);
1880 gst_structure_value_get_generic_type (GValue * val)
1882 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1883 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1884 GArray *array = g_value_peek_pointer (val);
1886 if (array->len > 0) {
1887 GValue *value = &g_array_index (array, GValue, 0);
1889 return gst_structure_value_get_generic_type (value);
1893 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1895 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1896 return G_TYPE_INT64;
1897 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1898 return G_TYPE_DOUBLE;
1899 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1900 return GST_TYPE_FRACTION;
1902 return G_VALUE_TYPE (val);
1906 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1909 GstStructureField *field;
1912 g_return_val_if_fail (s != NULL, FALSE);
1914 len = GST_STRUCTURE_FIELDS (structure)->len;
1915 for (i = 0; i < len; i++) {
1919 field = GST_STRUCTURE_FIELD (structure, i);
1921 t = gst_value_serialize (&field->value);
1922 type = gst_structure_value_get_generic_type (&field->value);
1924 g_string_append_len (s, ", ", 2);
1925 /* FIXME: do we need to escape fieldnames? */
1926 g_string_append (s, g_quark_to_string (field->name));
1927 g_string_append_len (s, "=(", 2);
1928 g_string_append (s, gst_structure_to_abbr (type));
1929 g_string_append_c (s, ')');
1930 g_string_append (s, t == NULL ? "NULL" : t);
1934 g_string_append_c (s, ';');
1939 * gst_structure_to_string:
1940 * @structure: a #GstStructure
1942 * Converts @structure to a human-readable string representation.
1944 * For debugging purposes its easier to do something like this:
1946 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1948 * This prints the structure in human readable form.
1950 * The current implementation of serialization will lead to unexpected results
1951 * when there are nested #GstCaps / #GstStructure deeper than one level.
1953 * Free-function: g_free
1955 * Returns: (transfer full): a pointer to string allocated by g_malloc().
1956 * g_free() after usage.
1959 gst_structure_to_string (const GstStructure * structure)
1963 /* NOTE: This function is potentially called by the debug system,
1964 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1965 * should be careful to avoid recursion. This includes any functions
1966 * called by gst_structure_to_string. In particular, calls should
1967 * not use the GST_PTR_FORMAT extension. */
1969 g_return_val_if_fail (structure != NULL, NULL);
1971 /* we estimate a minimum size based on the number of fields in order to
1972 * avoid unnecessary reallocs within GString */
1973 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1974 g_string_append (s, g_quark_to_string (structure->name));
1975 priv_gst_structure_append_to_gstring (structure, s);
1976 return g_string_free (s, FALSE);
1980 * r will still point to the string. if end == next, the string will not be
1981 * null-terminated. In all other cases it will be.
1982 * end = pointer to char behind end of string, next = pointer to start of
1984 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1987 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1998 ret = gst_structure_parse_simple_string (s, end);
2008 if (G_UNLIKELY (*s == 0))
2010 if (G_UNLIKELY (*s == '\\')) {
2012 if (G_UNLIKELY (*s == 0))
2021 /* Find the closing quotes */
2024 if (G_UNLIKELY (*s == 0))
2026 if (G_UNLIKELY (*s == '\\')) {
2028 if (G_UNLIKELY (*s == 0))
2044 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
2047 GValue value1 = { 0 };
2048 GValue value2 = { 0 };
2049 GValue value3 = { 0 };
2051 gboolean ret, have_step = FALSE;
2057 ret = gst_structure_parse_value (s, &s, &value1, type);
2061 while (g_ascii_isspace (*s))
2068 while (g_ascii_isspace (*s))
2071 ret = gst_structure_parse_value (s, &s, &value2, type);
2075 while (g_ascii_isspace (*s))
2078 /* optional step for int and int64 */
2079 if (G_VALUE_TYPE (&value1) == G_TYPE_INT
2080 || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2084 while (g_ascii_isspace (*s))
2087 ret = gst_structure_parse_value (s, &s, &value3, type);
2091 while (g_ascii_isspace (*s))
2102 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
2104 if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
2107 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
2108 range_type = GST_TYPE_DOUBLE_RANGE;
2109 g_value_init (value, range_type);
2110 gst_value_set_double_range (value,
2111 gst_g_value_get_double_unchecked (&value1),
2112 gst_g_value_get_double_unchecked (&value2));
2113 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
2114 range_type = GST_TYPE_INT_RANGE;
2115 g_value_init (value, range_type);
2117 gst_value_set_int_range_step (value,
2118 gst_g_value_get_int_unchecked (&value1),
2119 gst_g_value_get_int_unchecked (&value2),
2120 gst_g_value_get_int_unchecked (&value3));
2122 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
2123 gst_g_value_get_int_unchecked (&value2));
2124 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2125 range_type = GST_TYPE_INT64_RANGE;
2126 g_value_init (value, range_type);
2128 gst_value_set_int64_range_step (value,
2129 gst_g_value_get_int64_unchecked (&value1),
2130 gst_g_value_get_int64_unchecked (&value2),
2131 gst_g_value_get_int64_unchecked (&value3));
2133 gst_value_set_int64_range (value,
2134 gst_g_value_get_int64_unchecked (&value1),
2135 gst_g_value_get_int64_unchecked (&value2));
2136 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
2137 range_type = GST_TYPE_FRACTION_RANGE;
2138 g_value_init (value, range_type);
2139 gst_value_set_fraction_range (value, &value1, &value2);
2149 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
2150 GType type, GType list_type, char begin, char end)
2152 GValue list_value = { 0 };
2156 g_value_init (value, list_type);
2157 array = g_value_peek_pointer (value);
2163 while (g_ascii_isspace (*s))
2171 ret = gst_structure_parse_value (s, &s, &list_value, type);
2175 g_array_append_val (array, list_value);
2177 while (g_ascii_isspace (*s))
2185 while (g_ascii_isspace (*s))
2188 memset (&list_value, 0, sizeof (list_value));
2189 ret = gst_structure_parse_value (s, &s, &list_value, type);
2193 g_array_append_val (array, list_value);
2194 while (g_ascii_isspace (*s))
2205 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2207 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2212 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2215 return gst_structure_parse_any_list (s, after, value, type,
2216 GST_TYPE_ARRAY, '<', '>');
2220 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2224 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2234 gst_structure_parse_field (gchar * str,
2235 gchar ** after, GstStructureField * field)
2244 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2247 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2248 GST_WARNING ("failed to parse simple string, str=%s", str);
2253 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2256 if (G_UNLIKELY (*s != '=')) {
2257 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2264 field->name = g_quark_from_string (name);
2265 GST_DEBUG ("trying field name '%s'", name);
2268 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2270 GST_WARNING ("failed to parse value %s", str);
2279 gst_structure_parse_value (gchar * str,
2280 gchar ** after, GValue * value, GType default_type)
2289 GType type = default_type;
2292 while (g_ascii_isspace (*s))
2295 /* check if there's a (type_name) 'cast' */
2299 while (g_ascii_isspace (*s))
2302 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2305 while (g_ascii_isspace (*s))
2307 if (G_UNLIKELY (*s != ')'))
2310 while (g_ascii_isspace (*s))
2315 type = gst_structure_gtype_from_abbr (type_name);
2316 GST_DEBUG ("trying type name '%s'", type_name);
2319 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2320 GST_WARNING ("invalid type");
2325 while (g_ascii_isspace (*s))
2328 ret = gst_structure_parse_range (s, &s, value, type);
2329 } else if (*s == '{') {
2330 ret = gst_structure_parse_list (s, &s, value, type);
2331 } else if (*s == '<') {
2332 ret = gst_structure_parse_array (s, &s, value, type);
2336 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2338 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, GST_TYPE_FLAG_SET,
2339 G_TYPE_BOOLEAN, G_TYPE_STRING
2343 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2345 /* Set NULL terminator for deserialization */
2349 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2350 g_value_init (value, try_types[i]);
2351 ret = gst_value_deserialize (value, value_s);
2354 g_value_unset (value);
2357 g_value_init (value, type);
2359 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2360 (type != G_TYPE_STRING))))
2362 /* Set NULL terminator for deserialization */
2366 ret = gst_value_deserialize (value, value_s);
2367 if (G_UNLIKELY (!ret))
2368 g_value_unset (value);
2379 priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end,
2387 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2388 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2389 && g_ascii_isspace (r[1]))))
2394 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2395 GST_WARNING ("Failed to parse structure string '%s'", str);
2406 priv_gst_structure_parse_fields (gchar * str, gchar ** end,
2407 GstStructure * structure)
2410 GstStructureField field;
2415 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2416 && g_ascii_isspace (r[1]))))
2419 /* end of structure, get the next char and finish */
2424 /* accept \0 as end delimiter */
2427 if (G_UNLIKELY (*r != ',')) {
2428 GST_WARNING ("Failed to find delimiter, r=%s", r);
2432 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2433 && g_ascii_isspace (r[1]))))
2436 memset (&field, 0, sizeof (field));
2437 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2438 GST_WARNING ("Failed to parse field, r=%s", r);
2441 gst_structure_set_field (structure, &field);
2450 * gst_structure_new_from_string:
2451 * @string: a string representation of a #GstStructure
2453 * Creates a #GstStructure from a string representation.
2454 * If end is not %NULL, a pointer to the place inside the given string
2455 * where parsing ended will be returned.
2457 * The current implementation of serialization will lead to unexpected results
2458 * when there are nested #GstCaps / #GstStructure deeper than one level.
2460 * Free-function: gst_structure_free
2462 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2463 * when the string could not be parsed. Free with
2464 * gst_structure_free() after use.
2469 gst_structure_new_from_string (const gchar * string)
2471 return gst_structure_from_string (string, NULL);
2475 * gst_structure_from_string:
2476 * @string: a string representation of a #GstStructure.
2477 * @end: (out) (allow-none) (transfer none) (skip): pointer to store the end of the string in.
2479 * Creates a #GstStructure from a string representation.
2480 * If end is not %NULL, a pointer to the place inside the given string
2481 * where parsing ended will be returned.
2483 * Free-function: gst_structure_free
2485 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2486 * when the string could not be parsed. Free with
2487 * gst_structure_free() after use.
2490 gst_structure_from_string (const gchar * string, gchar ** end)
2497 GstStructure *structure = NULL;
2499 g_return_val_if_fail (string != NULL, NULL);
2501 copy = g_strdup (string);
2504 if (!priv_gst_structure_parse_name (r, &name, &w, &r))
2509 structure = gst_structure_new_empty (name);
2512 if (G_UNLIKELY (structure == NULL))
2515 if (!priv_gst_structure_parse_fields (r, &r, structure))
2519 *end = (char *) string + (r - copy);
2521 g_warning ("gst_structure_from_string did not consume whole string,"
2522 " but caller did not provide end pointer (\"%s\")", string);
2529 gst_structure_free (structure);
2535 gst_structure_transform_to_string (const GValue * src_value,
2536 GValue * dest_value)
2538 g_return_if_fail (src_value != NULL);
2539 g_return_if_fail (dest_value != NULL);
2541 dest_value->data[0].v_pointer =
2542 gst_structure_to_string (src_value->data[0].v_pointer);
2545 static GstStructure *
2546 gst_structure_copy_conditional (const GstStructure * structure)
2549 return gst_structure_copy (structure);
2553 /* fixate utility functions */
2556 * gst_structure_fixate_field_nearest_int:
2557 * @structure: a #GstStructure
2558 * @field_name: a field in @structure
2559 * @target: the target value of the fixation
2561 * Fixates a #GstStructure by changing the given field to the nearest
2562 * integer to @target that is a subset of the existing field.
2564 * Returns: %TRUE if the structure could be fixated
2567 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2568 const char *field_name, int target)
2570 const GValue *value;
2572 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2573 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2575 value = gst_structure_get_value (structure, field_name);
2577 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2580 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2583 x = gst_value_get_int_range_min (value);
2586 x = gst_value_get_int_range_max (value);
2589 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2591 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2592 const GValue *list_value;
2595 int best_index = -1;
2597 n = gst_value_list_get_size (value);
2598 for (i = 0; i < n; i++) {
2599 list_value = gst_value_list_get_value (value, i);
2600 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2601 int x = gst_g_value_get_int_unchecked (list_value);
2603 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2609 if (best_index != -1) {
2610 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2620 * gst_structure_fixate_field_nearest_double:
2621 * @structure: a #GstStructure
2622 * @field_name: a field in @structure
2623 * @target: the target value of the fixation
2625 * Fixates a #GstStructure by changing the given field to the nearest
2626 * double to @target that is a subset of the existing field.
2628 * Returns: %TRUE if the structure could be fixated
2631 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2632 const char *field_name, double target)
2634 const GValue *value;
2636 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2637 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2639 value = gst_structure_get_value (structure, field_name);
2641 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2644 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2647 x = gst_value_get_double_range_min (value);
2650 x = gst_value_get_double_range_max (value);
2653 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2655 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2656 const GValue *list_value;
2659 int best_index = -1;
2661 n = gst_value_list_get_size (value);
2662 for (i = 0; i < n; i++) {
2663 list_value = gst_value_list_get_value (value, i);
2664 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2665 double x = gst_g_value_get_double_unchecked (list_value);
2667 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2673 if (best_index != -1) {
2674 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2685 * gst_structure_fixate_field_boolean:
2686 * @structure: a #GstStructure
2687 * @field_name: a field in @structure
2688 * @target: the target value of the fixation
2690 * Fixates a #GstStructure by changing the given @field_name field to the given
2691 * @target boolean if that field is not fixed yet.
2693 * Returns: %TRUE if the structure could be fixated
2696 gst_structure_fixate_field_boolean (GstStructure * structure,
2697 const char *field_name, gboolean target)
2699 const GValue *value;
2701 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2702 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2704 value = gst_structure_get_value (structure, field_name);
2706 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2709 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2710 const GValue *list_value;
2713 int best_index = -1;
2715 n = gst_value_list_get_size (value);
2716 for (i = 0; i < n; i++) {
2717 list_value = gst_value_list_get_value (value, i);
2718 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2719 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2721 if (best_index == -1 || x == target) {
2727 if (best_index != -1) {
2728 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2738 * gst_structure_fixate_field_string:
2739 * @structure: a #GstStructure
2740 * @field_name: a field in @structure
2741 * @target: the target value of the fixation
2743 * Fixates a #GstStructure by changing the given @field_name field to the given
2744 * @target string if that field is not fixed yet.
2746 * Returns: %TRUE if the structure could be fixated
2749 gst_structure_fixate_field_string (GstStructure * structure,
2750 const gchar * field_name, const gchar * target)
2752 const GValue *value;
2754 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2755 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2757 value = gst_structure_get_value (structure, field_name);
2759 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2762 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2763 const GValue *list_value;
2765 const gchar *best = NULL;
2766 int best_index = -1;
2768 n = gst_value_list_get_size (value);
2769 for (i = 0; i < n; i++) {
2770 list_value = gst_value_list_get_value (value, i);
2771 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2772 const gchar *x = g_value_get_string (list_value);
2774 if (best_index == -1 || g_str_equal (x, target)) {
2780 if (best_index != -1) {
2781 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2791 * gst_structure_fixate_field_nearest_fraction:
2792 * @structure: a #GstStructure
2793 * @field_name: a field in @structure
2794 * @target_numerator: The numerator of the target value of the fixation
2795 * @target_denominator: The denominator of the target value of the fixation
2797 * Fixates a #GstStructure by changing the given field to the nearest
2798 * fraction to @target_numerator/@target_denominator that is a subset
2799 * of the existing field.
2801 * Returns: %TRUE if the structure could be fixated
2804 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2805 const char *field_name, const gint target_numerator,
2806 const gint target_denominator)
2808 const GValue *value;
2810 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2811 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2812 g_return_val_if_fail (target_denominator != 0, FALSE);
2814 value = gst_structure_get_value (structure, field_name);
2816 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2819 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2820 const GValue *x, *new_value;
2821 GValue target = { 0 };
2822 g_value_init (&target, GST_TYPE_FRACTION);
2823 gst_value_set_fraction (&target, target_numerator, target_denominator);
2825 new_value = ⌖
2826 x = gst_value_get_fraction_range_min (value);
2827 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2829 x = gst_value_get_fraction_range_max (value);
2830 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2833 gst_structure_set_value (structure, field_name, new_value);
2834 g_value_unset (&target);
2836 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2837 const GValue *list_value;
2839 const GValue *best = NULL;
2842 gdouble best_diff = G_MAXDOUBLE;
2844 target = (gdouble) target_numerator / (gdouble) target_denominator;
2846 GST_DEBUG ("target %g, best %g", target, best_diff);
2850 n = gst_value_list_get_size (value);
2851 for (i = 0; i < n; i++) {
2852 list_value = gst_value_list_get_value (value, i);
2853 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2855 gdouble list_double;
2857 num = gst_value_get_fraction_numerator (list_value);
2858 denom = gst_value_get_fraction_denominator (list_value);
2860 list_double = ((gdouble) num / (gdouble) denom);
2861 cur_diff = target - list_double;
2863 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2866 cur_diff = -cur_diff;
2868 if (!best || cur_diff < best_diff) {
2869 GST_DEBUG ("new best %g", list_double);
2871 best_diff = cur_diff;
2876 gst_structure_set_value (structure, field_name, best);
2885 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2887 GstStructure *s = data;
2890 if (gst_value_fixate (&v, value)) {
2891 gst_structure_id_take_value (s, field_id, &v);
2897 * gst_structure_fixate_field:
2898 * @structure: a #GstStructure
2899 * @field_name: a field in @structure
2901 * Fixates a #GstStructure by changing the given field with its fixated value.
2903 * Returns: %TRUE if the structure field could be fixated
2906 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2908 GstStructureField *field;
2910 g_return_val_if_fail (structure != NULL, FALSE);
2911 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2913 if (!(field = gst_structure_get_field (structure, field_name)))
2916 return default_fixate (field->name, &field->value, structure);
2919 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2920 * (useful for message parsing functions where the return location is user
2921 * supplied and the user may pass %NULL if the value isn't of interest) */
2922 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2924 const GValue *_value = (value); \
2925 guint _flags = (flags); \
2926 GType _value_type = G_VALUE_TYPE (_value); \
2927 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2928 const gchar *_lcopy_format = _vtable->lcopy_format; \
2929 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2930 guint _n_values = 0; \
2932 while (*_lcopy_format != '\0') { \
2933 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2934 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2937 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2938 *(__error) = g_strdup_printf ("either all or none of the return " \
2939 "locations for field '%s' need to be NULL", fieldname); \
2940 } else if (_cvalues[0].v_pointer != NULL) { \
2941 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2946 * gst_structure_get_valist:
2947 * @structure: a #GstStructure
2948 * @first_fieldname: the name of the first field to read
2949 * @args: variable arguments
2951 * Parses the variable arguments and reads fields from @structure accordingly.
2952 * valist-variant of gst_structure_get(). Look at the documentation of
2953 * gst_structure_get() for more details.
2955 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2958 gst_structure_get_valist (const GstStructure * structure,
2959 const char *first_fieldname, va_list args)
2961 const char *field_name;
2962 GType expected_type = G_TYPE_INVALID;
2964 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2965 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2967 field_name = first_fieldname;
2968 while (field_name) {
2969 const GValue *val = NULL;
2972 expected_type = va_arg (args, GType);
2974 val = gst_structure_get_value (structure, field_name);
2979 if (G_VALUE_TYPE (val) != expected_type)
2982 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2984 g_warning ("%s: %s", G_STRFUNC, err);
2989 field_name = va_arg (args, const gchar *);
2997 GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2998 field_name, structure);
3003 GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
3004 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
3005 GST_STR_NULL (g_type_name (expected_type)),
3006 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
3013 * gst_structure_id_get_valist:
3014 * @structure: a #GstStructure
3015 * @first_field_id: the quark of the first field to read
3016 * @args: variable arguments
3018 * Parses the variable arguments and reads fields from @structure accordingly.
3019 * valist-variant of gst_structure_id_get(). Look at the documentation of
3020 * gst_structure_id_get() for more details.
3022 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
3025 gst_structure_id_get_valist (const GstStructure * structure,
3026 GQuark first_field_id, va_list args)
3029 GType expected_type = G_TYPE_INVALID;
3031 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3032 g_return_val_if_fail (first_field_id != 0, FALSE);
3034 field_id = first_field_id;
3036 const GValue *val = NULL;
3039 expected_type = va_arg (args, GType);
3041 val = gst_structure_id_get_value (structure, field_id);
3046 if (G_VALUE_TYPE (val) != expected_type)
3049 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
3051 g_warning ("%s: %s", G_STRFUNC, err);
3056 field_id = va_arg (args, GQuark);
3064 GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
3065 GST_STR_NULL (g_quark_to_string (field_id)), structure);
3070 GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
3071 "field was of type '%s': %" GST_PTR_FORMAT,
3072 g_quark_to_string (field_id),
3073 GST_STR_NULL (g_type_name (expected_type)),
3074 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
3081 * gst_structure_get:
3082 * @structure: a #GstStructure
3083 * @first_fieldname: the name of the first field to read
3084 * @...: variable arguments
3086 * Parses the variable arguments and reads fields from @structure accordingly.
3087 * Variable arguments should be in the form field name, field type
3088 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3089 * The last variable argument should be %NULL.
3091 * For refcounted (mini)objects you will receive a new reference which
3092 * you must release with a suitable _unref() when no longer needed. For
3093 * strings and boxed types you will receive a copy which you will need to
3094 * release with either g_free() or the suitable function for the boxed type.
3096 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
3097 * because the field requested did not exist, or was of a type other
3098 * than the type specified), otherwise %TRUE.
3101 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
3107 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3108 g_return_val_if_fail (first_fieldname != NULL, FALSE);
3110 va_start (args, first_fieldname);
3111 ret = gst_structure_get_valist (structure, first_fieldname, args);
3118 * gst_structure_id_get:
3119 * @structure: a #GstStructure
3120 * @first_field_id: the quark of the first field to read
3121 * @...: variable arguments
3123 * Parses the variable arguments and reads fields from @structure accordingly.
3124 * Variable arguments should be in the form field id quark, field type
3125 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3126 * The last variable argument should be %NULL (technically it should be a
3127 * 0 quark, but we require %NULL so compilers that support it can check for
3128 * the %NULL terminator and warn if it's not there).
3130 * This function is just like gst_structure_get() only that it is slightly
3131 * more efficient since it saves the string-to-quark lookup in the global
3134 * For refcounted (mini)objects you will receive a new reference which
3135 * you must release with a suitable _unref() when no longer needed. For
3136 * strings and boxed types you will receive a copy which you will need to
3137 * release with either g_free() or the suitable function for the boxed type.
3139 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
3140 * because the field requested did not exist, or was of a type other
3141 * than the type specified), otherwise %TRUE.
3144 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
3150 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3151 g_return_val_if_fail (first_field_id != 0, FALSE);
3153 va_start (args, first_field_id);
3154 ret = gst_structure_id_get_valist (structure, first_field_id, args);
3161 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
3164 const GstStructure *struct1 = (const GstStructure *) data;
3165 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
3167 if (G_UNLIKELY (val1 == NULL))
3169 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
3177 * gst_structure_is_equal:
3178 * @structure1: a #GstStructure.
3179 * @structure2: a #GstStructure.
3181 * Tests if the two #GstStructure are equal.
3183 * Returns: %TRUE if the two structures have the same name and field.
3186 gst_structure_is_equal (const GstStructure * structure1,
3187 const GstStructure * structure2)
3189 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
3190 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
3192 if (G_UNLIKELY (structure1 == structure2))
3195 if (structure1->name != structure2->name) {
3198 if (GST_STRUCTURE_FIELDS (structure1)->len !=
3199 GST_STRUCTURE_FIELDS (structure2)->len) {
3203 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
3204 (gpointer) structure2);
3211 const GstStructure *intersect;
3216 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
3218 IntersectData *idata = (IntersectData *) data;
3219 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3221 if (G_UNLIKELY (val2 == NULL)) {
3222 gst_structure_id_set_value (idata->dest, id, val1);
3224 GValue dest_value = { 0 };
3225 if (gst_value_intersect (&dest_value, val1, val2)) {
3226 gst_structure_id_take_value (idata->dest, id, &dest_value);
3235 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
3237 IntersectData *idata = (IntersectData *) data;
3238 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3240 if (G_UNLIKELY (val2 == NULL)) {
3241 gst_structure_id_set_value (idata->dest, id, val1);
3247 * gst_structure_intersect:
3248 * @struct1: a #GstStructure
3249 * @struct2: a #GstStructure
3251 * Intersects @struct1 and @struct2 and returns the intersection.
3253 * Returns: Intersection of @struct1 and @struct2
3256 gst_structure_intersect (const GstStructure * struct1,
3257 const GstStructure * struct2)
3261 g_assert (struct1 != NULL);
3262 g_assert (struct2 != NULL);
3264 if (G_UNLIKELY (struct1->name != struct2->name))
3267 /* copy fields from struct1 which we have not in struct2 to target
3268 * intersect if we have the field in both */
3269 data.dest = gst_structure_new_id_empty (struct1->name);
3270 data.intersect = struct2;
3271 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3272 gst_structure_intersect_field1, &data)))
3275 /* copy fields from struct2 which we have not in struct1 to target */
3276 data.intersect = struct1;
3277 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3278 gst_structure_intersect_field2, &data)))
3284 gst_structure_free (data.dest);
3289 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3292 GstStructure *other = (GstStructure *) data;
3293 const GValue *val2 = gst_structure_id_get_value (other, id);
3295 if (G_LIKELY (val2)) {
3296 if (!gst_value_can_intersect (val1, val2)) {
3299 gint eq = gst_value_compare (val1, val2);
3301 if (eq == GST_VALUE_UNORDERED) {
3302 /* we need to try interseting */
3303 if (!gst_value_intersect (NULL, val1, val2)) {
3306 } else if (eq != GST_VALUE_EQUAL) {
3315 * gst_structure_can_intersect:
3316 * @struct1: a #GstStructure
3317 * @struct2: a #GstStructure
3319 * Tries intersecting @struct1 and @struct2 and reports whether the result
3320 * would not be empty.
3322 * Returns: %TRUE if intersection would not be empty
3325 gst_structure_can_intersect (const GstStructure * struct1,
3326 const GstStructure * struct2)
3328 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3329 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3331 if (G_UNLIKELY (struct1->name != struct2->name))
3334 /* tries to intersect if we have the field in both */
3335 return gst_structure_foreach ((GstStructure *) struct1,
3336 gst_caps_structure_can_intersect_field, (gpointer) struct2);
3340 gst_caps_structure_is_superset_field (GQuark field_id, const GValue * value,
3343 GstStructure *subset = user_data;
3344 const GValue *other;
3347 if (!(other = gst_structure_id_get_value (subset, field_id)))
3348 /* field is missing in the subset => no subset */
3351 comparison = gst_value_compare (value, other);
3353 /* equal values are subset */
3354 if (comparison == GST_VALUE_EQUAL)
3357 /* ordered, but unequal, values are not */
3358 if (comparison != GST_VALUE_UNORDERED)
3361 return gst_value_is_subset (other, value);
3365 * gst_structure_is_subset:
3366 * @subset: a #GstStructure
3367 * @superset: a potentially greater #GstStructure
3369 * Checks if @subset is a subset of @superset, i.e. has the same
3370 * structure name and for all fields that are existing in @superset,
3371 * @subset has a value that is a subset of the value in @superset.
3373 * Returns: %TRUE if @subset is a subset of @superset
3376 gst_structure_is_subset (const GstStructure * subset,
3377 const GstStructure * superset)
3379 if ((superset->name != subset->name) ||
3380 (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3383 return gst_structure_foreach ((GstStructure *) superset,
3384 gst_caps_structure_is_superset_field, (gpointer) subset);
3389 * gst_structure_fixate:
3390 * @structure: a #GstStructure
3392 * Fixate all values in @structure using gst_value_fixate().
3393 * @structure will be modified in-place and should be writable.
3396 gst_structure_fixate (GstStructure * structure)
3398 g_return_if_fail (GST_IS_STRUCTURE (structure));
3400 gst_structure_foreach (structure, default_fixate, structure);