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.
623 * Variable arguments should be in the form field name, field type
624 * (as a GType), value(s). The last variable argument should be %NULL.
627 gst_structure_set (GstStructure * structure, const gchar * field, ...)
631 g_return_if_fail (structure != NULL);
632 g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
634 va_start (varargs, field);
635 gst_structure_set_valist_internal (structure, field, varargs);
640 * gst_structure_set_valist:
641 * @structure: a #GstStructure
642 * @fieldname: the name of the field to set
643 * @varargs: variable arguments
645 * va_list form of gst_structure_set().
648 gst_structure_set_valist (GstStructure * structure,
649 const gchar * fieldname, va_list varargs)
651 g_return_if_fail (structure != NULL);
652 g_return_if_fail (IS_MUTABLE (structure));
654 gst_structure_set_valist_internal (structure, fieldname, varargs);
658 gst_structure_id_set_valist_internal (GstStructure * structure,
659 GQuark fieldname, va_list varargs)
665 GstStructureField field = { 0 };
667 field.name = fieldname;
669 type = va_arg (varargs, GType);
671 #ifndef G_VALUE_COLLECT_INIT
672 g_value_init (&field.value, type);
673 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
675 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
677 if (G_UNLIKELY (err)) {
678 g_critical ("%s", err);
681 gst_structure_set_field (structure, &field);
683 fieldname = va_arg (varargs, GQuark);
688 * gst_structure_id_set:
689 * @structure: a #GstStructure
690 * @fieldname: the GQuark for the name of the field to set
691 * @...: variable arguments
693 * Identical to gst_structure_set, except that field names are
694 * passed using the GQuark for the field name. This allows more efficient
695 * setting of the structure if the caller already knows the associated
697 * The last variable argument must be %NULL.
700 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
704 g_return_if_fail (structure != NULL);
706 va_start (varargs, field);
707 gst_structure_id_set_valist_internal (structure, field, varargs);
712 * gst_structure_id_set_valist:
713 * @structure: a #GstStructure
714 * @fieldname: the name of the field to set
715 * @varargs: variable arguments
717 * va_list form of gst_structure_id_set().
720 gst_structure_id_set_valist (GstStructure * structure,
721 GQuark fieldname, va_list varargs)
723 g_return_if_fail (structure != NULL);
724 g_return_if_fail (IS_MUTABLE (structure));
726 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
730 * gst_structure_new_id:
731 * @name_quark: name of new structure
732 * @field_quark: the GQuark for the name of the field to set
733 * @...: variable arguments
735 * Creates a new #GstStructure with the given name as a GQuark, followed by
736 * fieldname quark, GType, argument(s) "triplets" in the same format as
737 * gst_structure_id_set(). Basically a convenience wrapper around
738 * gst_structure_new_id_empty() and gst_structure_id_set().
740 * The last variable argument must be %NULL (or 0).
742 * Free-function: gst_structure_free
744 * Returns: (transfer full): a new #GstStructure
747 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
752 g_return_val_if_fail (name_quark != 0, NULL);
753 g_return_val_if_fail (field_quark != 0, NULL);
755 s = gst_structure_new_id_empty (name_quark);
757 va_start (varargs, field_quark);
758 gst_structure_id_set_valist_internal (s, field_quark, varargs);
764 #if GST_VERSION_NANO == 1
765 #define GIT_G_WARNING g_warning
767 #define GIT_G_WARNING GST_WARNING
770 /* If the structure currently contains a field with the same name, it is
771 * replaced with the provided field. Otherwise, the field is added to the
772 * structure. The field's value is not deeply copied.
775 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
777 GstStructureField *f;
778 GType field_value_type;
781 len = GST_STRUCTURE_FIELDS (structure)->len;
783 field_value_type = G_VALUE_TYPE (&field->value);
784 if (field_value_type == G_TYPE_STRING) {
787 s = g_value_get_string (&field->value);
788 /* only check for NULL strings in taglists, as they are allowed in message
789 * structs, e.g. error message debug strings */
790 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
792 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
793 "Please file a bug.", g_quark_to_string (field->name));
794 g_value_unset (&field->value);
797 /* empty strings never make sense */
798 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
799 "Please file a bug.", g_quark_to_string (field->name));
800 g_value_unset (&field->value);
803 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
804 g_warning ("Trying to set string on %s field '%s', but string is not "
805 "valid UTF-8. Please file a bug.",
806 IS_TAGLIST (structure) ? "taglist" : "structure",
807 g_quark_to_string (field->name));
808 g_value_unset (&field->value);
811 } else if (G_UNLIKELY (field_value_type == G_TYPE_DATE)) {
814 d = g_value_get_boxed (&field->value);
815 /* only check for NULL GDates in taglists, as they might make sense
816 * in other, generic structs */
817 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
818 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
819 "Please file a bug.", g_quark_to_string (field->name));
820 g_value_unset (&field->value);
822 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
824 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
825 IS_TAGLIST (structure) ? "taglist" : "structure",
826 g_quark_to_string (field->name));
827 g_value_unset (&field->value);
832 for (i = 0; i < len; i++) {
833 f = GST_STRUCTURE_FIELD (structure, i);
835 if (G_UNLIKELY (f->name == field->name)) {
836 g_value_unset (&f->value);
837 memcpy (f, field, sizeof (GstStructureField));
842 g_array_append_val (GST_STRUCTURE_FIELDS (structure), *field);
845 /* If there is no field with the given ID, NULL is returned.
847 static GstStructureField *
848 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
850 GstStructureField *field;
853 len = GST_STRUCTURE_FIELDS (structure)->len;
855 for (i = 0; i < len; i++) {
856 field = GST_STRUCTURE_FIELD (structure, i);
858 if (G_UNLIKELY (field->name == field_id))
865 /* If there is no field with the given ID, NULL is returned.
867 static GstStructureField *
868 gst_structure_get_field (const GstStructure * structure,
869 const gchar * fieldname)
871 g_return_val_if_fail (structure != NULL, NULL);
872 g_return_val_if_fail (fieldname != NULL, NULL);
874 return gst_structure_id_get_field (structure,
875 g_quark_from_string (fieldname));
879 * gst_structure_get_value:
880 * @structure: a #GstStructure
881 * @fieldname: the name of the field to get
883 * Get the value of the field with name @fieldname.
885 * Returns: the #GValue corresponding to the field with the given name.
888 gst_structure_get_value (const GstStructure * structure,
889 const gchar * fieldname)
891 GstStructureField *field;
893 g_return_val_if_fail (structure != NULL, NULL);
894 g_return_val_if_fail (fieldname != NULL, NULL);
896 field = gst_structure_get_field (structure, fieldname);
900 return &field->value;
904 * gst_structure_id_get_value:
905 * @structure: a #GstStructure
906 * @field: the #GQuark of the field to get
908 * Get the value of the field with GQuark @field.
910 * Returns: the #GValue corresponding to the field with the given name
914 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
916 GstStructureField *gsfield;
918 g_return_val_if_fail (structure != NULL, NULL);
920 gsfield = gst_structure_id_get_field (structure, field);
924 return &gsfield->value;
928 * gst_structure_remove_field:
929 * @structure: a #GstStructure
930 * @fieldname: the name of the field to remove
932 * Removes the field with the given name. If the field with the given
933 * name does not exist, the structure is unchanged.
936 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
938 GstStructureField *field;
942 g_return_if_fail (structure != NULL);
943 g_return_if_fail (fieldname != NULL);
944 g_return_if_fail (IS_MUTABLE (structure));
946 id = g_quark_from_string (fieldname);
947 len = GST_STRUCTURE_FIELDS (structure)->len;
949 for (i = 0; i < len; i++) {
950 field = GST_STRUCTURE_FIELD (structure, i);
952 if (field->name == id) {
953 if (G_IS_VALUE (&field->value)) {
954 g_value_unset (&field->value);
956 GST_STRUCTURE_FIELDS (structure) =
957 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
964 * gst_structure_remove_fields:
965 * @structure: a #GstStructure
966 * @fieldname: the name of the field to remove
967 * @...: %NULL-terminated list of more fieldnames to remove
969 * Removes the fields with the given names. If a field does not exist, the
970 * argument is ignored.
973 gst_structure_remove_fields (GstStructure * structure,
974 const gchar * fieldname, ...)
978 g_return_if_fail (structure != NULL);
979 g_return_if_fail (fieldname != NULL);
980 /* mutability checked in remove_field */
982 va_start (varargs, fieldname);
983 gst_structure_remove_fields_valist (structure, fieldname, varargs);
988 * gst_structure_remove_fields_valist:
989 * @structure: a #GstStructure
990 * @fieldname: the name of the field to remove
991 * @varargs: %NULL-terminated list of more fieldnames to remove
993 * va_list form of gst_structure_remove_fields().
996 gst_structure_remove_fields_valist (GstStructure * structure,
997 const gchar * fieldname, va_list varargs)
999 gchar *field = (gchar *) fieldname;
1001 g_return_if_fail (structure != NULL);
1002 g_return_if_fail (fieldname != NULL);
1003 /* mutability checked in remove_field */
1006 gst_structure_remove_field (structure, field);
1007 field = va_arg (varargs, char *);
1012 * gst_structure_remove_all_fields:
1013 * @structure: a #GstStructure
1015 * Removes all fields in a GstStructure.
1018 gst_structure_remove_all_fields (GstStructure * structure)
1020 GstStructureField *field;
1023 g_return_if_fail (structure != NULL);
1024 g_return_if_fail (IS_MUTABLE (structure));
1026 for (i = GST_STRUCTURE_FIELDS (structure)->len - 1; i >= 0; i--) {
1027 field = GST_STRUCTURE_FIELD (structure, i);
1029 if (G_IS_VALUE (&field->value)) {
1030 g_value_unset (&field->value);
1032 GST_STRUCTURE_FIELDS (structure) =
1033 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1038 * gst_structure_get_field_type:
1039 * @structure: a #GstStructure
1040 * @fieldname: the name of the field
1042 * Finds the field with the given name, and returns the type of the
1043 * value it contains. If the field is not found, G_TYPE_INVALID is
1046 * Returns: the #GValue of the field
1049 gst_structure_get_field_type (const GstStructure * structure,
1050 const gchar * fieldname)
1052 GstStructureField *field;
1054 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1055 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1057 field = gst_structure_get_field (structure, fieldname);
1059 return G_TYPE_INVALID;
1061 return G_VALUE_TYPE (&field->value);
1065 * gst_structure_n_fields:
1066 * @structure: a #GstStructure
1068 * Get the number of fields in the structure.
1070 * Returns: the number of fields in the structure
1073 gst_structure_n_fields (const GstStructure * structure)
1075 g_return_val_if_fail (structure != NULL, 0);
1077 return GST_STRUCTURE_FIELDS (structure)->len;
1081 * gst_structure_nth_field_name:
1082 * @structure: a #GstStructure
1083 * @index: the index to get the name of
1085 * Get the name of the given field number, counting from 0 onwards.
1087 * Returns: the name of the given field number
1090 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1092 GstStructureField *field;
1094 g_return_val_if_fail (structure != NULL, NULL);
1095 g_return_val_if_fail (index < GST_STRUCTURE_FIELDS (structure)->len, NULL);
1097 field = GST_STRUCTURE_FIELD (structure, index);
1099 return g_quark_to_string (field->name);
1103 * gst_structure_foreach:
1104 * @structure: a #GstStructure
1105 * @func: (scope call): a function to call for each field
1106 * @user_data: (closure): private data
1108 * Calls the provided function once for each field in the #GstStructure. The
1109 * function must not modify the fields. Also see gst_structure_map_in_place().
1111 * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
1115 gst_structure_foreach (const GstStructure * structure,
1116 GstStructureForeachFunc func, gpointer user_data)
1119 GstStructureField *field;
1122 g_return_val_if_fail (structure != NULL, FALSE);
1123 g_return_val_if_fail (func != NULL, FALSE);
1125 len = GST_STRUCTURE_FIELDS (structure)->len;
1127 for (i = 0; i < len; i++) {
1128 field = GST_STRUCTURE_FIELD (structure, i);
1130 ret = func (field->name, &field->value, user_data);
1131 if (G_UNLIKELY (!ret))
1139 * gst_structure_map_in_place:
1140 * @structure: a #GstStructure
1141 * @func: (scope call): a function to call for each field
1142 * @user_data: (closure): private data
1144 * Calls the provided function once for each field in the #GstStructure. In
1145 * contrast to gst_structure_foreach(), the function may modify but not delete the
1146 * fields. The structure must be mutable.
1148 * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
1152 gst_structure_map_in_place (GstStructure * structure,
1153 GstStructureMapFunc func, gpointer user_data)
1156 GstStructureField *field;
1159 g_return_val_if_fail (structure != NULL, FALSE);
1160 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1161 g_return_val_if_fail (func != NULL, FALSE);
1162 len = GST_STRUCTURE_FIELDS (structure)->len;
1164 for (i = 0; i < len; i++) {
1165 field = GST_STRUCTURE_FIELD (structure, i);
1167 ret = func (field->name, &field->value, user_data);
1176 * gst_structure_id_has_field:
1177 * @structure: a #GstStructure
1178 * @field: #GQuark of the field name
1180 * Check if @structure contains a field named @field.
1182 * Returns: %TRUE if the structure contains a field with the given name
1185 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1187 GstStructureField *f;
1189 g_return_val_if_fail (structure != NULL, FALSE);
1190 g_return_val_if_fail (field != 0, FALSE);
1192 f = gst_structure_id_get_field (structure, field);
1198 * gst_structure_has_field:
1199 * @structure: a #GstStructure
1200 * @fieldname: the name of a field
1202 * Check if @structure contains a field named @fieldname.
1204 * Returns: %TRUE if the structure contains a field with the given name
1207 gst_structure_has_field (const GstStructure * structure,
1208 const gchar * fieldname)
1210 g_return_val_if_fail (structure != NULL, FALSE);
1211 g_return_val_if_fail (fieldname != NULL, FALSE);
1213 return gst_structure_id_has_field (structure,
1214 g_quark_from_string (fieldname));
1218 * gst_structure_id_has_field_typed:
1219 * @structure: a #GstStructure
1220 * @field: #GQuark of the field name
1221 * @type: the type of a value
1223 * Check if @structure contains a field named @field and with GType @type.
1225 * Returns: %TRUE if the structure contains a field with the given name and type
1228 gst_structure_id_has_field_typed (const GstStructure * structure,
1229 GQuark field, GType type)
1231 GstStructureField *f;
1233 g_return_val_if_fail (structure != NULL, FALSE);
1234 g_return_val_if_fail (field != 0, FALSE);
1236 f = gst_structure_id_get_field (structure, field);
1240 return (G_VALUE_TYPE (&f->value) == type);
1244 * gst_structure_has_field_typed:
1245 * @structure: a #GstStructure
1246 * @fieldname: the name of a field
1247 * @type: the type of a value
1249 * Check if @structure contains a field named @fieldname and with GType @type.
1251 * Returns: %TRUE if the structure contains a field with the given name and type
1254 gst_structure_has_field_typed (const GstStructure * structure,
1255 const gchar * fieldname, GType type)
1257 g_return_val_if_fail (structure != NULL, FALSE);
1258 g_return_val_if_fail (fieldname != NULL, FALSE);
1260 return gst_structure_id_has_field_typed (structure,
1261 g_quark_from_string (fieldname), type);
1264 /* utility functions */
1267 * gst_structure_get_boolean:
1268 * @structure: a #GstStructure
1269 * @fieldname: the name of a field
1270 * @value: (out): a pointer to a #gboolean to set
1272 * Sets the boolean pointed to by @value corresponding to the value of the
1273 * given field. Caller is responsible for making sure the field exists
1274 * and has the correct type.
1276 * Returns: %TRUE if the value could be set correctly. If there was no field
1277 * with @fieldname or the existing field did not contain a boolean, this
1278 * function returns %FALSE.
1281 gst_structure_get_boolean (const GstStructure * structure,
1282 const gchar * fieldname, gboolean * value)
1284 GstStructureField *field;
1286 g_return_val_if_fail (structure != NULL, FALSE);
1287 g_return_val_if_fail (fieldname != NULL, FALSE);
1289 field = gst_structure_get_field (structure, fieldname);
1291 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_BOOLEAN)
1294 *value = gst_g_value_get_boolean_unchecked (&field->value);
1300 * gst_structure_get_int:
1301 * @structure: a #GstStructure
1302 * @fieldname: the name of a field
1303 * @value: (out): a pointer to an int to set
1305 * Sets the int pointed to by @value corresponding to the value of the
1306 * given field. Caller is responsible for making sure the field exists
1307 * and has the correct type.
1309 * Returns: %TRUE if the value could be set correctly. If there was no field
1310 * with @fieldname or the existing field did not contain an int, this function
1314 gst_structure_get_int (const GstStructure * structure,
1315 const gchar * fieldname, gint * value)
1317 GstStructureField *field;
1319 g_return_val_if_fail (structure != NULL, FALSE);
1320 g_return_val_if_fail (fieldname != NULL, FALSE);
1321 g_return_val_if_fail (value != NULL, FALSE);
1323 field = gst_structure_get_field (structure, fieldname);
1325 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT)
1328 *value = gst_g_value_get_int_unchecked (&field->value);
1334 * gst_structure_get_uint:
1335 * @structure: a #GstStructure
1336 * @fieldname: the name of a field
1337 * @value: (out): a pointer to a uint to set
1339 * Sets the uint pointed to by @value corresponding to the value of the
1340 * given field. Caller is responsible for making sure the field exists
1341 * and has the correct type.
1343 * Returns: %TRUE if the value could be set correctly. If there was no field
1344 * with @fieldname or the existing field did not contain a uint, this function
1348 gst_structure_get_uint (const GstStructure * structure,
1349 const gchar * fieldname, guint * value)
1351 GstStructureField *field;
1353 g_return_val_if_fail (structure != NULL, FALSE);
1354 g_return_val_if_fail (fieldname != NULL, FALSE);
1355 g_return_val_if_fail (value != NULL, FALSE);
1357 field = gst_structure_get_field (structure, fieldname);
1359 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT)
1362 *value = gst_g_value_get_uint_unchecked (&field->value);
1368 * gst_structure_get_int64:
1369 * @structure: a #GstStructure
1370 * @fieldname: the name of a field
1371 * @value: (out): a pointer to an int64 to set
1373 * Sets the int64 pointed to by @value corresponding to the value of the
1374 * given field. Caller is responsible for making sure the field exists
1375 * and has the correct type.
1377 * Returns: %TRUE if the value could be set correctly. If there was no field
1378 * with @fieldname or the existing field did not contain an int64, this function
1384 gst_structure_get_int64 (const GstStructure * structure,
1385 const gchar * fieldname, gint64 * value)
1387 GstStructureField *field;
1389 g_return_val_if_fail (structure != NULL, FALSE);
1390 g_return_val_if_fail (fieldname != NULL, FALSE);
1391 g_return_val_if_fail (value != NULL, FALSE);
1393 field = gst_structure_get_field (structure, fieldname);
1395 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT64)
1398 *value = gst_g_value_get_int64_unchecked (&field->value);
1404 * gst_structure_get_uint64:
1405 * @structure: a #GstStructure
1406 * @fieldname: the name of a field
1407 * @value: (out): a pointer to a uint64 to set
1409 * Sets the uint64 pointed to by @value corresponding to the value of the
1410 * given field. Caller is responsible for making sure the field exists
1411 * and has the correct type.
1413 * Returns: %TRUE if the value could be set correctly. If there was no field
1414 * with @fieldname or the existing field did not contain a uint64, this function
1420 gst_structure_get_uint64 (const GstStructure * structure,
1421 const gchar * fieldname, guint64 * value)
1423 GstStructureField *field;
1425 g_return_val_if_fail (structure != NULL, FALSE);
1426 g_return_val_if_fail (fieldname != NULL, FALSE);
1427 g_return_val_if_fail (value != NULL, FALSE);
1429 field = gst_structure_get_field (structure, fieldname);
1431 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT64)
1434 *value = gst_g_value_get_uint64_unchecked (&field->value);
1440 * gst_structure_get_date:
1441 * @structure: a #GstStructure
1442 * @fieldname: the name of a field
1443 * @value: (out callee-allocates): a pointer to a #GDate to set
1445 * Sets the date pointed to by @value corresponding to the date of the
1446 * given field. Caller is responsible for making sure the field exists
1447 * and has the correct type.
1449 * On success @value will point to a newly-allocated copy of the date which
1450 * should be freed with g_date_free() when no longer needed (note: this is
1451 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1452 * copy of the string).
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 data, this function
1459 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1462 GstStructureField *field;
1464 g_return_val_if_fail (structure != NULL, FALSE);
1465 g_return_val_if_fail (fieldname != NULL, FALSE);
1466 g_return_val_if_fail (value != NULL, FALSE);
1468 field = gst_structure_get_field (structure, fieldname);
1470 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DATE)
1473 /* FIXME: 2.0 g_value_dup_boxed() -> g_value_get_boxed() */
1474 *value = g_value_dup_boxed (&field->value);
1480 * gst_structure_get_date_time:
1481 * @structure: a #GstStructure
1482 * @fieldname: the name of a field
1483 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1485 * Sets the datetime pointed to by @value corresponding to the datetime of the
1486 * given field. Caller is responsible for making sure the field exists
1487 * and has the correct type.
1489 * On success @value will point to a reference of the datetime which
1490 * should be unreffed with gst_date_time_unref() when no longer needed
1491 * (note: this is inconsistent with e.g. gst_structure_get_string()
1492 * which doesn't return a copy of the string).
1494 * Returns: %TRUE if the value could be set correctly. If there was no field
1495 * with @fieldname or the existing field did not contain a data, this function
1499 gst_structure_get_date_time (const GstStructure * structure,
1500 const gchar * fieldname, GstDateTime ** value)
1502 GstStructureField *field;
1504 g_return_val_if_fail (structure != NULL, FALSE);
1505 g_return_val_if_fail (fieldname != NULL, FALSE);
1506 g_return_val_if_fail (value != NULL, FALSE);
1508 field = gst_structure_get_field (structure, fieldname);
1512 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1515 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1516 *value = g_value_dup_boxed (&field->value);
1522 * gst_structure_get_clock_time:
1523 * @structure: a #GstStructure
1524 * @fieldname: the name of a field
1525 * @value: (out): a pointer to a #GstClockTime to set
1527 * Sets the clock time pointed to by @value corresponding to the clock time
1528 * of the given field. Caller is responsible for making sure the field exists
1529 * and has the correct type.
1531 * Returns: %TRUE if the value could be set correctly. If there was no field
1532 * with @fieldname or the existing field did not contain a #GstClockTime, this
1533 * function returns %FALSE.
1536 gst_structure_get_clock_time (const GstStructure * structure,
1537 const gchar * fieldname, GstClockTime * value)
1539 return gst_structure_get_uint64 (structure, fieldname, value);
1543 * gst_structure_get_double:
1544 * @structure: a #GstStructure
1545 * @fieldname: the name of a field
1546 * @value: (out): a pointer to a gdouble to set
1548 * Sets the double pointed to by @value corresponding to the value of the
1549 * given field. Caller is responsible for making sure the field exists
1550 * and has the correct type.
1552 * Returns: %TRUE if the value could be set correctly. If there was no field
1553 * with @fieldname or the existing field did not contain a double, this
1554 * function returns %FALSE.
1557 gst_structure_get_double (const GstStructure * structure,
1558 const gchar * fieldname, gdouble * value)
1560 GstStructureField *field;
1562 g_return_val_if_fail (structure != NULL, FALSE);
1563 g_return_val_if_fail (fieldname != NULL, FALSE);
1564 g_return_val_if_fail (value != NULL, FALSE);
1566 field = gst_structure_get_field (structure, fieldname);
1568 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DOUBLE)
1571 *value = gst_g_value_get_double_unchecked (&field->value);
1577 * gst_structure_get_string:
1578 * @structure: a #GstStructure
1579 * @fieldname: the name of a field
1581 * Finds the field corresponding to @fieldname, and returns the string
1582 * contained in the field's value. Caller is responsible for making
1583 * sure the field exists and has the correct type.
1585 * The string should not be modified, and remains valid until the next
1586 * call to a gst_structure_*() function with the given structure.
1588 * Returns: (nullable): a pointer to the string or %NULL when the
1589 * field did not exist or did not contain a string.
1592 gst_structure_get_string (const GstStructure * structure,
1593 const gchar * fieldname)
1595 GstStructureField *field;
1597 g_return_val_if_fail (structure != NULL, NULL);
1598 g_return_val_if_fail (fieldname != NULL, NULL);
1600 field = gst_structure_get_field (structure, fieldname);
1602 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_STRING)
1605 return gst_g_value_get_string_unchecked (&field->value);
1609 * gst_structure_get_enum:
1610 * @structure: a #GstStructure
1611 * @fieldname: the name of a field
1612 * @enumtype: the enum type of a field
1613 * @value: (out): a pointer to an int to set
1615 * Sets the int pointed to by @value corresponding to the value of the
1616 * given field. Caller is responsible for making sure the field exists,
1617 * has the correct type and that the enumtype is correct.
1619 * Returns: %TRUE if the value could be set correctly. If there was no field
1620 * with @fieldname or the existing field did not contain an enum of the given
1621 * type, this function returns %FALSE.
1624 gst_structure_get_enum (const GstStructure * structure,
1625 const gchar * fieldname, GType enumtype, gint * value)
1627 GstStructureField *field;
1629 g_return_val_if_fail (structure != NULL, FALSE);
1630 g_return_val_if_fail (fieldname != NULL, FALSE);
1631 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1632 g_return_val_if_fail (value != NULL, FALSE);
1634 field = gst_structure_get_field (structure, fieldname);
1638 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1641 *value = g_value_get_enum (&field->value);
1647 * gst_structure_get_fraction:
1648 * @structure: a #GstStructure
1649 * @fieldname: the name of a field
1650 * @value_numerator: (out): a pointer to an int to set
1651 * @value_denominator: (out): a pointer to an int to set
1653 * Sets the integers pointed to by @value_numerator and @value_denominator
1654 * corresponding to the value of the given field. Caller is responsible
1655 * for making sure the field exists and has the correct type.
1657 * Returns: %TRUE if the values could be set correctly. If there was no field
1658 * with @fieldname or the existing field did not contain a GstFraction, this
1659 * function returns %FALSE.
1662 gst_structure_get_fraction (const GstStructure * structure,
1663 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
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_numerator != NULL, FALSE);
1670 g_return_val_if_fail (value_denominator != NULL, FALSE);
1672 field = gst_structure_get_field (structure, fieldname);
1674 if (field == NULL || G_VALUE_TYPE (&field->value) != GST_TYPE_FRACTION)
1677 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1678 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1683 typedef struct _GstStructureAbbreviation
1685 const gchar *type_name;
1688 GstStructureAbbreviation;
1690 /* return a copy of an array of GstStructureAbbreviation containing all the
1691 * known type_string, GType maps, including abbreviations for common types */
1692 static GstStructureAbbreviation *
1693 gst_structure_get_abbrs (gint * n_abbrs)
1695 static GstStructureAbbreviation *abbrs = NULL;
1696 static volatile gsize num = 0;
1698 if (g_once_init_enter (&num)) {
1699 /* dynamically generate the array */
1701 GstStructureAbbreviation dyn_abbrs[] = {
1706 {"uint", G_TYPE_UINT}
1710 {"float", G_TYPE_FLOAT}
1714 {"double", G_TYPE_DOUBLE}
1716 {"d", G_TYPE_DOUBLE}
1718 {"buffer", GST_TYPE_BUFFER}
1720 {"fraction", GST_TYPE_FRACTION}
1722 {"boolean", G_TYPE_BOOLEAN}
1724 {"bool", G_TYPE_BOOLEAN}
1726 {"b", G_TYPE_BOOLEAN}
1728 {"string", G_TYPE_STRING}
1730 {"str", G_TYPE_STRING}
1732 {"s", G_TYPE_STRING}
1734 {"structure", GST_TYPE_STRUCTURE}
1736 {"date", G_TYPE_DATE}
1738 {"datetime", GST_TYPE_DATE_TIME}
1740 {"bitmask", GST_TYPE_BITMASK}
1742 {"sample", GST_TYPE_SAMPLE}
1744 {"taglist", GST_TYPE_TAG_LIST}
1746 _num = G_N_ELEMENTS (dyn_abbrs);
1747 /* permanently allocate and copy the array now */
1748 abbrs = g_new0 (GstStructureAbbreviation, _num);
1749 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1750 g_once_init_leave (&num, _num);
1757 /* given a type_name that could be a type abbreviation or a registered GType,
1758 * return a matching GType */
1760 gst_structure_gtype_from_abbr (const char *type_name)
1763 GstStructureAbbreviation *abbrs;
1766 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1768 abbrs = gst_structure_get_abbrs (&n_abbrs);
1770 for (i = 0; i < n_abbrs; i++) {
1771 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1772 return abbrs[i].type;
1776 /* this is the fallback */
1777 return g_type_from_name (type_name);
1781 gst_structure_to_abbr (GType type)
1784 GstStructureAbbreviation *abbrs;
1787 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1789 abbrs = gst_structure_get_abbrs (&n_abbrs);
1791 for (i = 0; i < n_abbrs; i++) {
1792 if (type == abbrs[i].type) {
1793 return abbrs[i].type_name;
1797 return g_type_name (type);
1801 gst_structure_value_get_generic_type (GValue * val)
1803 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1804 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1805 GArray *array = g_value_peek_pointer (val);
1807 if (array->len > 0) {
1808 GValue *value = &g_array_index (array, GValue, 0);
1810 return gst_structure_value_get_generic_type (value);
1814 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1816 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1817 return G_TYPE_INT64;
1818 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1819 return G_TYPE_DOUBLE;
1820 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1821 return GST_TYPE_FRACTION;
1823 return G_VALUE_TYPE (val);
1827 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1830 GstStructureField *field;
1833 g_return_val_if_fail (s != NULL, FALSE);
1835 len = GST_STRUCTURE_FIELDS (structure)->len;
1836 for (i = 0; i < len; i++) {
1840 field = GST_STRUCTURE_FIELD (structure, i);
1842 t = gst_value_serialize (&field->value);
1843 type = gst_structure_value_get_generic_type (&field->value);
1845 g_string_append_len (s, ", ", 2);
1846 /* FIXME: do we need to escape fieldnames? */
1847 g_string_append (s, g_quark_to_string (field->name));
1848 g_string_append_len (s, "=(", 2);
1849 g_string_append (s, gst_structure_to_abbr (type));
1850 g_string_append_c (s, ')');
1851 g_string_append (s, t == NULL ? "NULL" : t);
1855 g_string_append_c (s, ';');
1860 * gst_structure_to_string:
1861 * @structure: a #GstStructure
1863 * Converts @structure to a human-readable string representation.
1865 * For debugging purposes its easier to do something like this:
1867 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1869 * This prints the structure in human readable form.
1871 * The current implementation of serialization will lead to unexpected results
1872 * when there are nested #GstCaps / #GstStructure deeper than one level.
1874 * Free-function: g_free
1876 * Returns: (transfer full): a pointer to string allocated by g_malloc().
1877 * g_free() after usage.
1880 gst_structure_to_string (const GstStructure * structure)
1884 /* NOTE: This function is potentially called by the debug system,
1885 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1886 * should be careful to avoid recursion. This includes any functions
1887 * called by gst_structure_to_string. In particular, calls should
1888 * not use the GST_PTR_FORMAT extension. */
1890 g_return_val_if_fail (structure != NULL, NULL);
1892 /* we estimate a minimum size based on the number of fields in order to
1893 * avoid unnecessary reallocs within GString */
1894 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1895 g_string_append (s, g_quark_to_string (structure->name));
1896 priv_gst_structure_append_to_gstring (structure, s);
1897 return g_string_free (s, FALSE);
1901 * r will still point to the string. if end == next, the string will not be
1902 * null-terminated. In all other cases it will be.
1903 * end = pointer to char behind end of string, next = pointer to start of
1905 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1908 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1919 ret = gst_structure_parse_simple_string (s, end);
1929 if (G_UNLIKELY (*s == 0))
1931 if (G_UNLIKELY (*s == '\\'))
1939 /* Find the closing quotes */
1942 if (G_UNLIKELY (*s == 0))
1944 if (G_UNLIKELY (*s == '\\'))
1959 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1962 GValue value1 = { 0 };
1963 GValue value2 = { 0 };
1964 GValue value3 = { 0 };
1966 gboolean ret, have_step = FALSE;
1972 ret = gst_structure_parse_value (s, &s, &value1, type);
1976 while (g_ascii_isspace (*s))
1983 while (g_ascii_isspace (*s))
1986 ret = gst_structure_parse_value (s, &s, &value2, type);
1990 while (g_ascii_isspace (*s))
1993 /* optional step for int and int64 */
1994 if (G_VALUE_TYPE (&value1) == G_TYPE_INT
1995 || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1999 while (g_ascii_isspace (*s))
2002 ret = gst_structure_parse_value (s, &s, &value3, type);
2006 while (g_ascii_isspace (*s))
2017 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
2019 if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
2022 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
2023 range_type = GST_TYPE_DOUBLE_RANGE;
2024 g_value_init (value, range_type);
2025 gst_value_set_double_range (value,
2026 gst_g_value_get_double_unchecked (&value1),
2027 gst_g_value_get_double_unchecked (&value2));
2028 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
2029 range_type = GST_TYPE_INT_RANGE;
2030 g_value_init (value, range_type);
2032 gst_value_set_int_range_step (value,
2033 gst_g_value_get_int_unchecked (&value1),
2034 gst_g_value_get_int_unchecked (&value2),
2035 gst_g_value_get_int_unchecked (&value3));
2037 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
2038 gst_g_value_get_int_unchecked (&value2));
2039 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2040 range_type = GST_TYPE_INT64_RANGE;
2041 g_value_init (value, range_type);
2043 gst_value_set_int64_range_step (value,
2044 gst_g_value_get_int64_unchecked (&value1),
2045 gst_g_value_get_int64_unchecked (&value2),
2046 gst_g_value_get_int64_unchecked (&value3));
2048 gst_value_set_int64_range (value,
2049 gst_g_value_get_int64_unchecked (&value1),
2050 gst_g_value_get_int64_unchecked (&value2));
2051 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
2052 range_type = GST_TYPE_FRACTION_RANGE;
2053 g_value_init (value, range_type);
2054 gst_value_set_fraction_range (value, &value1, &value2);
2064 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
2065 GType type, GType list_type, char begin, char end)
2067 GValue list_value = { 0 };
2071 g_value_init (value, list_type);
2072 array = g_value_peek_pointer (value);
2078 while (g_ascii_isspace (*s))
2086 ret = gst_structure_parse_value (s, &s, &list_value, type);
2090 g_array_append_val (array, list_value);
2092 while (g_ascii_isspace (*s))
2100 while (g_ascii_isspace (*s))
2103 memset (&list_value, 0, sizeof (list_value));
2104 ret = gst_structure_parse_value (s, &s, &list_value, type);
2108 g_array_append_val (array, list_value);
2109 while (g_ascii_isspace (*s))
2120 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2122 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2127 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2130 return gst_structure_parse_any_list (s, after, value, type,
2131 GST_TYPE_ARRAY, '<', '>');
2135 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2139 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2149 gst_structure_parse_field (gchar * str,
2150 gchar ** after, GstStructureField * field)
2159 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2162 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2163 GST_WARNING ("failed to parse simple string, str=%s", str);
2168 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2171 if (G_UNLIKELY (*s != '=')) {
2172 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2179 field->name = g_quark_from_string (name);
2180 GST_DEBUG ("trying field name '%s'", name);
2183 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2185 GST_WARNING ("failed to parse value %s", str);
2194 gst_structure_parse_value (gchar * str,
2195 gchar ** after, GValue * value, GType default_type)
2204 GType type = default_type;
2207 while (g_ascii_isspace (*s))
2210 /* check if there's a (type_name) 'cast' */
2214 while (g_ascii_isspace (*s))
2217 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2220 while (g_ascii_isspace (*s))
2222 if (G_UNLIKELY (*s != ')'))
2225 while (g_ascii_isspace (*s))
2230 type = gst_structure_gtype_from_abbr (type_name);
2231 GST_DEBUG ("trying type name '%s'", type_name);
2234 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2235 GST_WARNING ("invalid type");
2240 while (g_ascii_isspace (*s))
2243 ret = gst_structure_parse_range (s, &s, value, type);
2244 } else if (*s == '{') {
2245 ret = gst_structure_parse_list (s, &s, value, type);
2246 } else if (*s == '<') {
2247 ret = gst_structure_parse_array (s, &s, value, type);
2251 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2253 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2258 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2260 /* Set NULL terminator for deserialization */
2264 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2265 g_value_init (value, try_types[i]);
2266 ret = gst_value_deserialize (value, value_s);
2269 g_value_unset (value);
2272 g_value_init (value, type);
2274 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2275 (type != G_TYPE_STRING))))
2277 /* Set NULL terminator for deserialization */
2281 ret = gst_value_deserialize (value, value_s);
2282 if (G_UNLIKELY (!ret))
2283 g_value_unset (value);
2294 priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end,
2302 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2303 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2304 && g_ascii_isspace (r[1]))))
2309 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2310 GST_WARNING ("Failed to parse structure string '%s'", str);
2321 priv_gst_structure_parse_fields (gchar * str, gchar ** end,
2322 GstStructure * structure)
2325 GstStructureField field;
2330 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2331 && g_ascii_isspace (r[1]))))
2334 /* end of structure, get the next char and finish */
2339 /* accept \0 as end delimiter */
2342 if (G_UNLIKELY (*r != ',')) {
2343 GST_WARNING ("Failed to find delimiter, r=%s", r);
2347 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2348 && g_ascii_isspace (r[1]))))
2351 memset (&field, 0, sizeof (field));
2352 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2353 GST_WARNING ("Failed to parse field, r=%s", r);
2356 gst_structure_set_field (structure, &field);
2365 * gst_structure_new_from_string:
2366 * @string: a string representation of a #GstStructure
2368 * Creates a #GstStructure from a string representation.
2369 * If end is not %NULL, a pointer to the place inside the given string
2370 * where parsing ended will be returned.
2372 * The current implementation of serialization will lead to unexpected results
2373 * when there are nested #GstCaps / #GstStructure deeper than one level.
2375 * Free-function: gst_structure_free
2377 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2378 * when the string could not be parsed. Free with
2379 * gst_structure_free() after use.
2384 gst_structure_new_from_string (const gchar * string)
2386 return gst_structure_from_string (string, NULL);
2390 * gst_structure_from_string:
2391 * @string: a string representation of a #GstStructure.
2392 * @end: (out) (allow-none) (transfer none) (skip): pointer to store the end of the string in.
2394 * Creates a #GstStructure from a string representation.
2395 * If end is not %NULL, a pointer to the place inside the given string
2396 * where parsing ended will be returned.
2398 * Free-function: gst_structure_free
2400 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2401 * when the string could not be parsed. Free with
2402 * gst_structure_free() after use.
2405 gst_structure_from_string (const gchar * string, gchar ** end)
2412 GstStructure *structure = NULL;
2414 g_return_val_if_fail (string != NULL, NULL);
2416 copy = g_strdup (string);
2419 if (!priv_gst_structure_parse_name (r, &name, &w, &r))
2424 structure = gst_structure_new_empty (name);
2427 if (G_UNLIKELY (structure == NULL))
2430 if (!priv_gst_structure_parse_fields (r, &r, structure))
2434 *end = (char *) string + (r - copy);
2436 g_warning ("gst_structure_from_string did not consume whole string,"
2437 " but caller did not provide end pointer (\"%s\")", string);
2444 gst_structure_free (structure);
2450 gst_structure_transform_to_string (const GValue * src_value,
2451 GValue * dest_value)
2453 g_return_if_fail (src_value != NULL);
2454 g_return_if_fail (dest_value != NULL);
2456 dest_value->data[0].v_pointer =
2457 gst_structure_to_string (src_value->data[0].v_pointer);
2460 static GstStructure *
2461 gst_structure_copy_conditional (const GstStructure * structure)
2464 return gst_structure_copy (structure);
2468 /* fixate utility functions */
2471 * gst_structure_fixate_field_nearest_int:
2472 * @structure: a #GstStructure
2473 * @field_name: a field in @structure
2474 * @target: the target value of the fixation
2476 * Fixates a #GstStructure by changing the given field to the nearest
2477 * integer to @target that is a subset of the existing field.
2479 * Returns: %TRUE if the structure could be fixated
2482 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2483 const char *field_name, int target)
2485 const GValue *value;
2487 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2488 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2490 value = gst_structure_get_value (structure, field_name);
2492 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2495 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2498 x = gst_value_get_int_range_min (value);
2501 x = gst_value_get_int_range_max (value);
2504 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2506 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2507 const GValue *list_value;
2510 int best_index = -1;
2512 n = gst_value_list_get_size (value);
2513 for (i = 0; i < n; i++) {
2514 list_value = gst_value_list_get_value (value, i);
2515 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2516 int x = gst_g_value_get_int_unchecked (list_value);
2518 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2524 if (best_index != -1) {
2525 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2535 * gst_structure_fixate_field_nearest_double:
2536 * @structure: a #GstStructure
2537 * @field_name: a field in @structure
2538 * @target: the target value of the fixation
2540 * Fixates a #GstStructure by changing the given field to the nearest
2541 * double to @target that is a subset of the existing field.
2543 * Returns: %TRUE if the structure could be fixated
2546 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2547 const char *field_name, double target)
2549 const GValue *value;
2551 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2552 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2554 value = gst_structure_get_value (structure, field_name);
2556 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2559 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2562 x = gst_value_get_double_range_min (value);
2565 x = gst_value_get_double_range_max (value);
2568 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2570 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2571 const GValue *list_value;
2574 int best_index = -1;
2576 n = gst_value_list_get_size (value);
2577 for (i = 0; i < n; i++) {
2578 list_value = gst_value_list_get_value (value, i);
2579 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2580 double x = gst_g_value_get_double_unchecked (list_value);
2582 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2588 if (best_index != -1) {
2589 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2600 * gst_structure_fixate_field_boolean:
2601 * @structure: a #GstStructure
2602 * @field_name: a field in @structure
2603 * @target: the target value of the fixation
2605 * Fixates a #GstStructure by changing the given @field_name field to the given
2606 * @target boolean if that field is not fixed yet.
2608 * Returns: %TRUE if the structure could be fixated
2611 gst_structure_fixate_field_boolean (GstStructure * structure,
2612 const char *field_name, gboolean target)
2614 const GValue *value;
2616 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2617 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2619 value = gst_structure_get_value (structure, field_name);
2621 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2624 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2625 const GValue *list_value;
2628 int best_index = -1;
2630 n = gst_value_list_get_size (value);
2631 for (i = 0; i < n; i++) {
2632 list_value = gst_value_list_get_value (value, i);
2633 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2634 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2636 if (best_index == -1 || x == target) {
2642 if (best_index != -1) {
2643 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2653 * gst_structure_fixate_field_string:
2654 * @structure: a #GstStructure
2655 * @field_name: a field in @structure
2656 * @target: the target value of the fixation
2658 * Fixates a #GstStructure by changing the given @field_name field to the given
2659 * @target string if that field is not fixed yet.
2661 * Returns: %TRUE if the structure could be fixated
2664 gst_structure_fixate_field_string (GstStructure * structure,
2665 const gchar * field_name, const gchar * target)
2667 const GValue *value;
2669 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2670 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2672 value = gst_structure_get_value (structure, field_name);
2674 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2677 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2678 const GValue *list_value;
2680 const gchar *best = NULL;
2681 int best_index = -1;
2683 n = gst_value_list_get_size (value);
2684 for (i = 0; i < n; i++) {
2685 list_value = gst_value_list_get_value (value, i);
2686 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2687 const gchar *x = g_value_get_string (list_value);
2689 if (best_index == -1 || g_str_equal (x, target)) {
2695 if (best_index != -1) {
2696 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2706 * gst_structure_fixate_field_nearest_fraction:
2707 * @structure: a #GstStructure
2708 * @field_name: a field in @structure
2709 * @target_numerator: The numerator of the target value of the fixation
2710 * @target_denominator: The denominator of the target value of the fixation
2712 * Fixates a #GstStructure by changing the given field to the nearest
2713 * fraction to @target_numerator/@target_denominator that is a subset
2714 * of the existing field.
2716 * Returns: %TRUE if the structure could be fixated
2719 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2720 const char *field_name, const gint target_numerator,
2721 const gint target_denominator)
2723 const GValue *value;
2725 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2726 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2727 g_return_val_if_fail (target_denominator != 0, FALSE);
2729 value = gst_structure_get_value (structure, field_name);
2731 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2734 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2735 const GValue *x, *new_value;
2736 GValue target = { 0 };
2737 g_value_init (&target, GST_TYPE_FRACTION);
2738 gst_value_set_fraction (&target, target_numerator, target_denominator);
2740 new_value = ⌖
2741 x = gst_value_get_fraction_range_min (value);
2742 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2744 x = gst_value_get_fraction_range_max (value);
2745 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2748 gst_structure_set_value (structure, field_name, new_value);
2749 g_value_unset (&target);
2751 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2752 const GValue *list_value;
2754 const GValue *best = NULL;
2757 gdouble best_diff = G_MAXDOUBLE;
2759 target = (gdouble) target_numerator / (gdouble) target_denominator;
2761 GST_DEBUG ("target %g, best %g", target, best_diff);
2765 n = gst_value_list_get_size (value);
2766 for (i = 0; i < n; i++) {
2767 list_value = gst_value_list_get_value (value, i);
2768 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2770 gdouble list_double;
2772 num = gst_value_get_fraction_numerator (list_value);
2773 denom = gst_value_get_fraction_denominator (list_value);
2775 list_double = ((gdouble) num / (gdouble) denom);
2776 cur_diff = target - list_double;
2778 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2781 cur_diff = -cur_diff;
2783 if (!best || cur_diff < best_diff) {
2784 GST_DEBUG ("new best %g", list_double);
2786 best_diff = cur_diff;
2791 gst_structure_set_value (structure, field_name, best);
2800 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2802 GstStructure *s = data;
2805 if (gst_value_fixate (&v, value)) {
2806 gst_structure_id_take_value (s, field_id, &v);
2812 * gst_structure_fixate_field:
2813 * @structure: a #GstStructure
2814 * @field_name: a field in @structure
2816 * Fixates a #GstStructure by changing the given field with its fixated value.
2818 * Returns: %TRUE if the structure field could be fixated
2821 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2823 GstStructureField *field;
2825 g_return_val_if_fail (structure != NULL, FALSE);
2826 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2828 if (!(field = gst_structure_get_field (structure, field_name)))
2831 return default_fixate (field->name, &field->value, structure);
2834 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2835 * (useful for message parsing functions where the return location is user
2836 * supplied and the user may pass %NULL if the value isn't of interest) */
2837 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2839 const GValue *_value = (value); \
2840 guint _flags = (flags); \
2841 GType _value_type = G_VALUE_TYPE (_value); \
2842 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2843 const gchar *_lcopy_format = _vtable->lcopy_format; \
2844 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2845 guint _n_values = 0; \
2847 while (*_lcopy_format != '\0') { \
2848 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2849 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2852 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2853 *(__error) = g_strdup_printf ("either all or none of the return " \
2854 "locations for field '%s' need to be NULL", fieldname); \
2855 } else if (_cvalues[0].v_pointer != NULL) { \
2856 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2861 * gst_structure_get_valist:
2862 * @structure: a #GstStructure
2863 * @first_fieldname: the name of the first field to read
2864 * @args: variable arguments
2866 * Parses the variable arguments and reads fields from @structure accordingly.
2867 * valist-variant of gst_structure_get(). Look at the documentation of
2868 * gst_structure_get() for more details.
2870 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2873 gst_structure_get_valist (const GstStructure * structure,
2874 const char *first_fieldname, va_list args)
2876 const char *field_name;
2877 GType expected_type = G_TYPE_INVALID;
2879 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2880 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2882 field_name = first_fieldname;
2883 while (field_name) {
2884 const GValue *val = NULL;
2887 expected_type = va_arg (args, GType);
2889 val = gst_structure_get_value (structure, field_name);
2894 if (G_VALUE_TYPE (val) != expected_type)
2897 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2899 g_warning ("%s: %s", G_STRFUNC, err);
2904 field_name = va_arg (args, const gchar *);
2912 GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2913 field_name, structure);
2918 GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
2919 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2920 GST_STR_NULL (g_type_name (expected_type)),
2921 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2928 * gst_structure_id_get_valist:
2929 * @structure: a #GstStructure
2930 * @first_field_id: the quark of the first field to read
2931 * @args: variable arguments
2933 * Parses the variable arguments and reads fields from @structure accordingly.
2934 * valist-variant of gst_structure_id_get(). Look at the documentation of
2935 * gst_structure_id_get() for more details.
2937 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2940 gst_structure_id_get_valist (const GstStructure * structure,
2941 GQuark first_field_id, va_list args)
2944 GType expected_type = G_TYPE_INVALID;
2946 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2947 g_return_val_if_fail (first_field_id != 0, FALSE);
2949 field_id = first_field_id;
2951 const GValue *val = NULL;
2954 expected_type = va_arg (args, GType);
2956 val = gst_structure_id_get_value (structure, field_id);
2961 if (G_VALUE_TYPE (val) != expected_type)
2964 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2966 g_warning ("%s: %s", G_STRFUNC, err);
2971 field_id = va_arg (args, GQuark);
2979 GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2980 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2985 GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
2986 "field was of type '%s': %" GST_PTR_FORMAT,
2987 g_quark_to_string (field_id),
2988 GST_STR_NULL (g_type_name (expected_type)),
2989 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2996 * gst_structure_get:
2997 * @structure: a #GstStructure
2998 * @first_fieldname: the name of the first field to read
2999 * @...: variable arguments
3001 * Parses the variable arguments and reads fields from @structure accordingly.
3002 * Variable arguments should be in the form field name, field type
3003 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3004 * The last variable argument should be %NULL.
3006 * For refcounted (mini)objects you will receive a new reference which
3007 * you must release with a suitable _unref() when no longer needed. For
3008 * strings and boxed types you will receive a copy which you will need to
3009 * release with either g_free() or the suitable function for the boxed type.
3011 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
3012 * because the field requested did not exist, or was of a type other
3013 * than the type specified), otherwise %TRUE.
3016 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
3022 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3023 g_return_val_if_fail (first_fieldname != NULL, FALSE);
3025 va_start (args, first_fieldname);
3026 ret = gst_structure_get_valist (structure, first_fieldname, args);
3033 * gst_structure_id_get:
3034 * @structure: a #GstStructure
3035 * @first_field_id: the quark of the first field to read
3036 * @...: variable arguments
3038 * Parses the variable arguments and reads fields from @structure accordingly.
3039 * Variable arguments should be in the form field id quark, field type
3040 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3041 * The last variable argument should be %NULL (technically it should be a
3042 * 0 quark, but we require %NULL so compilers that support it can check for
3043 * the %NULL terminator and warn if it's not there).
3045 * This function is just like gst_structure_get() only that it is slightly
3046 * more efficient since it saves the string-to-quark lookup in the global
3049 * For refcounted (mini)objects you will receive a new reference which
3050 * you must release with a suitable _unref() when no longer needed. For
3051 * strings and boxed types you will receive a copy which you will need to
3052 * release with either g_free() or the suitable function for the boxed type.
3054 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
3055 * because the field requested did not exist, or was of a type other
3056 * than the type specified), otherwise %TRUE.
3059 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
3065 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3066 g_return_val_if_fail (first_field_id != 0, FALSE);
3068 va_start (args, first_field_id);
3069 ret = gst_structure_id_get_valist (structure, first_field_id, args);
3076 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
3079 const GstStructure *struct1 = (const GstStructure *) data;
3080 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
3082 if (G_UNLIKELY (val1 == NULL))
3084 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
3092 * gst_structure_is_equal:
3093 * @structure1: a #GstStructure.
3094 * @structure2: a #GstStructure.
3096 * Tests if the two #GstStructure are equal.
3098 * Returns: %TRUE if the two structures have the same name and field.
3101 gst_structure_is_equal (const GstStructure * structure1,
3102 const GstStructure * structure2)
3104 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
3105 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
3107 if (G_UNLIKELY (structure1 == structure2))
3110 if (structure1->name != structure2->name) {
3113 if (GST_STRUCTURE_FIELDS (structure1)->len !=
3114 GST_STRUCTURE_FIELDS (structure2)->len) {
3118 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
3119 (gpointer) structure2);
3126 const GstStructure *intersect;
3131 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
3133 IntersectData *idata = (IntersectData *) data;
3134 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3136 if (G_UNLIKELY (val2 == NULL)) {
3137 gst_structure_id_set_value (idata->dest, id, val1);
3139 GValue dest_value = { 0 };
3140 if (gst_value_intersect (&dest_value, val1, val2)) {
3141 gst_structure_id_take_value (idata->dest, id, &dest_value);
3150 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
3152 IntersectData *idata = (IntersectData *) data;
3153 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3155 if (G_UNLIKELY (val2 == NULL)) {
3156 gst_structure_id_set_value (idata->dest, id, val1);
3162 * gst_structure_intersect:
3163 * @struct1: a #GstStructure
3164 * @struct2: a #GstStructure
3166 * Intersects @struct1 and @struct2 and returns the intersection.
3168 * Returns: Intersection of @struct1 and @struct2
3171 gst_structure_intersect (const GstStructure * struct1,
3172 const GstStructure * struct2)
3176 g_assert (struct1 != NULL);
3177 g_assert (struct2 != NULL);
3179 if (G_UNLIKELY (struct1->name != struct2->name))
3182 /* copy fields from struct1 which we have not in struct2 to target
3183 * intersect if we have the field in both */
3184 data.dest = gst_structure_new_id_empty (struct1->name);
3185 data.intersect = struct2;
3186 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3187 gst_structure_intersect_field1, &data)))
3190 /* copy fields from struct2 which we have not in struct1 to target */
3191 data.intersect = struct1;
3192 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3193 gst_structure_intersect_field2, &data)))
3199 gst_structure_free (data.dest);
3204 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3207 GstStructure *other = (GstStructure *) data;
3208 const GValue *val2 = gst_structure_id_get_value (other, id);
3210 if (G_LIKELY (val2)) {
3211 if (!gst_value_can_intersect (val1, val2)) {
3214 gint eq = gst_value_compare (val1, val2);
3216 if (eq == GST_VALUE_UNORDERED) {
3217 /* we need to try interseting */
3218 if (!gst_value_intersect (NULL, val1, val2)) {
3221 } else if (eq != GST_VALUE_EQUAL) {
3230 * gst_structure_can_intersect:
3231 * @struct1: a #GstStructure
3232 * @struct2: a #GstStructure
3234 * Tries intersecting @struct1 and @struct2 and reports whether the result
3235 * would not be empty.
3237 * Returns: %TRUE if intersection would not be empty
3240 gst_structure_can_intersect (const GstStructure * struct1,
3241 const GstStructure * struct2)
3243 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3244 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3246 if (G_UNLIKELY (struct1->name != struct2->name))
3249 /* tries to intersect if we have the field in both */
3250 return gst_structure_foreach ((GstStructure *) struct1,
3251 gst_caps_structure_can_intersect_field, (gpointer) struct2);
3255 gst_caps_structure_is_superset_field (GQuark field_id, const GValue * value,
3258 GstStructure *subset = user_data;
3259 const GValue *other;
3262 if (!(other = gst_structure_id_get_value (subset, field_id)))
3263 /* field is missing in the subset => no subset */
3266 comparison = gst_value_compare (value, other);
3268 /* equal values are subset */
3269 if (comparison == GST_VALUE_EQUAL)
3272 /* ordered, but unequal, values are not */
3273 if (comparison != GST_VALUE_UNORDERED)
3276 return gst_value_is_subset (other, value);
3280 * gst_structure_is_subset:
3281 * @subset: a #GstStructure
3282 * @superset: a potentially greater #GstStructure
3284 * Checks if @subset is a subset of @superset, i.e. has the same
3285 * structure name and for all fields that are existing in @superset,
3286 * @subset has a value that is a subset of the value in @superset.
3288 * Returns: %TRUE if @subset is a subset of @superset
3291 gst_structure_is_subset (const GstStructure * subset,
3292 const GstStructure * superset)
3294 if ((superset->name != subset->name) ||
3295 (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3298 return gst_structure_foreach ((GstStructure *) superset,
3299 gst_caps_structure_is_superset_field, (gpointer) subset);
3304 * gst_structure_fixate:
3305 * @structure: a #GstStructure
3307 * Fixate all values in @structure using gst_value_fixate().
3308 * @structure will be modified in-place and should be writable.
3311 gst_structure_fixate (GstStructure * structure)
3313 g_return_if_fail (GST_IS_STRUCTURE (structure));
3315 gst_structure_foreach (structure, default_fixate, structure);