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 guint i, len = GST_STRUCTURE_FIELDS (structure)->len;
780 if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
783 s = g_value_get_string (&field->value);
784 /* only check for NULL strings in taglists, as they are allowed in message
785 * structs, e.g. error message debug strings */
786 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
788 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
789 "Please file a bug.", g_quark_to_string (field->name));
790 g_value_unset (&field->value);
793 /* empty strings never make sense */
794 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
795 "Please file a bug.", g_quark_to_string (field->name));
796 g_value_unset (&field->value);
799 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
800 g_warning ("Trying to set string on %s field '%s', but string is not "
801 "valid UTF-8. Please file a bug.",
802 IS_TAGLIST (structure) ? "taglist" : "structure",
803 g_quark_to_string (field->name));
804 g_value_unset (&field->value);
807 } else if (G_UNLIKELY (G_VALUE_HOLDS (&field->value, G_TYPE_DATE))) {
810 d = g_value_get_boxed (&field->value);
811 /* only check for NULL GDates in taglists, as they might make sense
812 * in other, generic structs */
813 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
814 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
815 "Please file a bug.", g_quark_to_string (field->name));
816 g_value_unset (&field->value);
818 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
820 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
821 IS_TAGLIST (structure) ? "taglist" : "structure",
822 g_quark_to_string (field->name));
823 g_value_unset (&field->value);
828 for (i = 0; i < len; i++) {
829 f = GST_STRUCTURE_FIELD (structure, i);
831 if (G_UNLIKELY (f->name == field->name)) {
832 g_value_unset (&f->value);
833 memcpy (f, field, sizeof (GstStructureField));
838 g_array_append_val (GST_STRUCTURE_FIELDS (structure), *field);
841 /* If there is no field with the given ID, NULL is returned.
843 static GstStructureField *
844 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
846 GstStructureField *field;
849 len = GST_STRUCTURE_FIELDS (structure)->len;
851 for (i = 0; i < len; i++) {
852 field = GST_STRUCTURE_FIELD (structure, i);
854 if (G_UNLIKELY (field->name == field_id))
861 /* If there is no field with the given ID, NULL is returned.
863 static GstStructureField *
864 gst_structure_get_field (const GstStructure * structure,
865 const gchar * fieldname)
867 g_return_val_if_fail (structure != NULL, NULL);
868 g_return_val_if_fail (fieldname != NULL, NULL);
870 return gst_structure_id_get_field (structure,
871 g_quark_from_string (fieldname));
875 * gst_structure_get_value:
876 * @structure: a #GstStructure
877 * @fieldname: the name of the field to get
879 * Get the value of the field with name @fieldname.
881 * Returns: the #GValue corresponding to the field with the given name.
884 gst_structure_get_value (const GstStructure * structure,
885 const gchar * fieldname)
887 GstStructureField *field;
889 g_return_val_if_fail (structure != NULL, NULL);
890 g_return_val_if_fail (fieldname != NULL, NULL);
892 field = gst_structure_get_field (structure, fieldname);
896 return &field->value;
900 * gst_structure_id_get_value:
901 * @structure: a #GstStructure
902 * @field: the #GQuark of the field to get
904 * Get the value of the field with GQuark @field.
906 * Returns: the #GValue corresponding to the field with the given name
910 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
912 GstStructureField *gsfield;
914 g_return_val_if_fail (structure != NULL, NULL);
916 gsfield = gst_structure_id_get_field (structure, field);
920 return &gsfield->value;
924 * gst_structure_remove_field:
925 * @structure: a #GstStructure
926 * @fieldname: the name of the field to remove
928 * Removes the field with the given name. If the field with the given
929 * name does not exist, the structure is unchanged.
932 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
934 GstStructureField *field;
938 g_return_if_fail (structure != NULL);
939 g_return_if_fail (fieldname != NULL);
940 g_return_if_fail (IS_MUTABLE (structure));
942 id = g_quark_from_string (fieldname);
943 len = GST_STRUCTURE_FIELDS (structure)->len;
945 for (i = 0; i < len; i++) {
946 field = GST_STRUCTURE_FIELD (structure, i);
948 if (field->name == id) {
949 if (G_IS_VALUE (&field->value)) {
950 g_value_unset (&field->value);
952 GST_STRUCTURE_FIELDS (structure) =
953 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
960 * gst_structure_remove_fields:
961 * @structure: a #GstStructure
962 * @fieldname: the name of the field to remove
963 * @...: %NULL-terminated list of more fieldnames to remove
965 * Removes the fields with the given names. If a field does not exist, the
966 * argument is ignored.
969 gst_structure_remove_fields (GstStructure * structure,
970 const gchar * fieldname, ...)
974 g_return_if_fail (structure != NULL);
975 g_return_if_fail (fieldname != NULL);
976 /* mutability checked in remove_field */
978 va_start (varargs, fieldname);
979 gst_structure_remove_fields_valist (structure, fieldname, varargs);
984 * gst_structure_remove_fields_valist:
985 * @structure: a #GstStructure
986 * @fieldname: the name of the field to remove
987 * @varargs: %NULL-terminated list of more fieldnames to remove
989 * va_list form of gst_structure_remove_fields().
992 gst_structure_remove_fields_valist (GstStructure * structure,
993 const gchar * fieldname, va_list varargs)
995 gchar *field = (gchar *) fieldname;
997 g_return_if_fail (structure != NULL);
998 g_return_if_fail (fieldname != NULL);
999 /* mutability checked in remove_field */
1002 gst_structure_remove_field (structure, field);
1003 field = va_arg (varargs, char *);
1008 * gst_structure_remove_all_fields:
1009 * @structure: a #GstStructure
1011 * Removes all fields in a GstStructure.
1014 gst_structure_remove_all_fields (GstStructure * structure)
1016 GstStructureField *field;
1019 g_return_if_fail (structure != NULL);
1020 g_return_if_fail (IS_MUTABLE (structure));
1022 for (i = GST_STRUCTURE_FIELDS (structure)->len - 1; i >= 0; i--) {
1023 field = GST_STRUCTURE_FIELD (structure, i);
1025 if (G_IS_VALUE (&field->value)) {
1026 g_value_unset (&field->value);
1028 GST_STRUCTURE_FIELDS (structure) =
1029 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1034 * gst_structure_get_field_type:
1035 * @structure: a #GstStructure
1036 * @fieldname: the name of the field
1038 * Finds the field with the given name, and returns the type of the
1039 * value it contains. If the field is not found, G_TYPE_INVALID is
1042 * Returns: the #GValue of the field
1045 gst_structure_get_field_type (const GstStructure * structure,
1046 const gchar * fieldname)
1048 GstStructureField *field;
1050 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1051 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1053 field = gst_structure_get_field (structure, fieldname);
1055 return G_TYPE_INVALID;
1057 return G_VALUE_TYPE (&field->value);
1061 * gst_structure_n_fields:
1062 * @structure: a #GstStructure
1064 * Get the number of fields in the structure.
1066 * Returns: the number of fields in the structure
1069 gst_structure_n_fields (const GstStructure * structure)
1071 g_return_val_if_fail (structure != NULL, 0);
1073 return GST_STRUCTURE_FIELDS (structure)->len;
1077 * gst_structure_nth_field_name:
1078 * @structure: a #GstStructure
1079 * @index: the index to get the name of
1081 * Get the name of the given field number, counting from 0 onwards.
1083 * Returns: the name of the given field number
1086 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1088 GstStructureField *field;
1090 g_return_val_if_fail (structure != NULL, NULL);
1091 g_return_val_if_fail (index < GST_STRUCTURE_FIELDS (structure)->len, NULL);
1093 field = GST_STRUCTURE_FIELD (structure, index);
1095 return g_quark_to_string (field->name);
1099 * gst_structure_foreach:
1100 * @structure: a #GstStructure
1101 * @func: (scope call): a function to call for each field
1102 * @user_data: (closure): private data
1104 * Calls the provided function once for each field in the #GstStructure. The
1105 * function must not modify the fields. Also see gst_structure_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_id_has_field:
1173 * @structure: a #GstStructure
1174 * @field: #GQuark of the field name
1176 * Check if @structure contains a field named @field.
1178 * Returns: %TRUE if the structure contains a field with the given name
1181 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1183 GstStructureField *f;
1185 g_return_val_if_fail (structure != NULL, FALSE);
1186 g_return_val_if_fail (field != 0, FALSE);
1188 f = gst_structure_id_get_field (structure, field);
1194 * gst_structure_has_field:
1195 * @structure: a #GstStructure
1196 * @fieldname: the name of a field
1198 * Check if @structure contains a field named @fieldname.
1200 * Returns: %TRUE if the structure contains a field with the given name
1203 gst_structure_has_field (const GstStructure * structure,
1204 const gchar * fieldname)
1206 g_return_val_if_fail (structure != NULL, FALSE);
1207 g_return_val_if_fail (fieldname != NULL, FALSE);
1209 return gst_structure_id_has_field (structure,
1210 g_quark_from_string (fieldname));
1214 * gst_structure_id_has_field_typed:
1215 * @structure: a #GstStructure
1216 * @field: #GQuark of the field name
1217 * @type: the type of a value
1219 * Check if @structure contains a field named @field and with GType @type.
1221 * Returns: %TRUE if the structure contains a field with the given name and type
1224 gst_structure_id_has_field_typed (const GstStructure * structure,
1225 GQuark field, GType type)
1227 GstStructureField *f;
1229 g_return_val_if_fail (structure != NULL, FALSE);
1230 g_return_val_if_fail (field != 0, FALSE);
1232 f = gst_structure_id_get_field (structure, field);
1236 return (G_VALUE_TYPE (&f->value) == type);
1240 * gst_structure_has_field_typed:
1241 * @structure: a #GstStructure
1242 * @fieldname: the name of a field
1243 * @type: the type of a value
1245 * Check if @structure contains a field named @fieldname and with GType @type.
1247 * Returns: %TRUE if the structure contains a field with the given name and type
1250 gst_structure_has_field_typed (const GstStructure * structure,
1251 const gchar * fieldname, GType type)
1253 g_return_val_if_fail (structure != NULL, FALSE);
1254 g_return_val_if_fail (fieldname != NULL, FALSE);
1256 return gst_structure_id_has_field_typed (structure,
1257 g_quark_from_string (fieldname), type);
1260 /* utility functions */
1263 * gst_structure_get_boolean:
1264 * @structure: a #GstStructure
1265 * @fieldname: the name of a field
1266 * @value: (out): a pointer to a #gboolean to set
1268 * Sets the boolean pointed to by @value corresponding to the value of the
1269 * given field. Caller is responsible for making sure the field exists
1270 * and has the correct type.
1272 * Returns: %TRUE if the value could be set correctly. If there was no field
1273 * with @fieldname or the existing field did not contain a boolean, this
1274 * function returns %FALSE.
1277 gst_structure_get_boolean (const GstStructure * structure,
1278 const gchar * fieldname, gboolean * value)
1280 GstStructureField *field;
1282 g_return_val_if_fail (structure != NULL, FALSE);
1283 g_return_val_if_fail (fieldname != NULL, FALSE);
1285 field = gst_structure_get_field (structure, fieldname);
1289 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1292 *value = gst_g_value_get_boolean_unchecked (&field->value);
1298 * gst_structure_get_int:
1299 * @structure: a #GstStructure
1300 * @fieldname: the name of a field
1301 * @value: (out): a pointer to an int to set
1303 * Sets the int pointed to by @value corresponding to the value of the
1304 * given field. Caller is responsible for making sure the field exists
1305 * and has the correct type.
1307 * Returns: %TRUE if the value could be set correctly. If there was no field
1308 * with @fieldname or the existing field did not contain an int, this function
1312 gst_structure_get_int (const GstStructure * structure,
1313 const gchar * fieldname, gint * value)
1315 GstStructureField *field;
1317 g_return_val_if_fail (structure != NULL, FALSE);
1318 g_return_val_if_fail (fieldname != NULL, FALSE);
1319 g_return_val_if_fail (value != NULL, FALSE);
1321 field = gst_structure_get_field (structure, fieldname);
1325 if (!G_VALUE_HOLDS_INT (&field->value))
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);
1361 if (!G_VALUE_HOLDS_UINT (&field->value))
1364 *value = gst_g_value_get_uint_unchecked (&field->value);
1370 * gst_structure_get_int64:
1371 * @structure: a #GstStructure
1372 * @fieldname: the name of a field
1373 * @value: (out): a pointer to an int64 to set
1375 * Sets the int64 pointed to by @value corresponding to the value of the
1376 * given field. Caller is responsible for making sure the field exists
1377 * and has the correct type.
1379 * Returns: %TRUE if the value could be set correctly. If there was no field
1380 * with @fieldname or the existing field did not contain an int64, this function
1386 gst_structure_get_int64 (const GstStructure * structure,
1387 const gchar * fieldname, gint64 * value)
1389 GstStructureField *field;
1391 g_return_val_if_fail (structure != NULL, FALSE);
1392 g_return_val_if_fail (fieldname != NULL, FALSE);
1393 g_return_val_if_fail (value != NULL, FALSE);
1395 field = gst_structure_get_field (structure, fieldname);
1399 if (!G_VALUE_HOLDS_INT64 (&field->value))
1402 *value = gst_g_value_get_int64_unchecked (&field->value);
1408 * gst_structure_get_uint64:
1409 * @structure: a #GstStructure
1410 * @fieldname: the name of a field
1411 * @value: (out): a pointer to a uint64 to set
1413 * Sets the uint64 pointed to by @value corresponding to the value of the
1414 * given field. Caller is responsible for making sure the field exists
1415 * and has the correct type.
1417 * Returns: %TRUE if the value could be set correctly. If there was no field
1418 * with @fieldname or the existing field did not contain a uint64, this function
1424 gst_structure_get_uint64 (const GstStructure * structure,
1425 const gchar * fieldname, guint64 * value)
1427 GstStructureField *field;
1429 g_return_val_if_fail (structure != NULL, FALSE);
1430 g_return_val_if_fail (fieldname != NULL, FALSE);
1431 g_return_val_if_fail (value != NULL, FALSE);
1433 field = gst_structure_get_field (structure, fieldname);
1437 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1440 *value = gst_g_value_get_uint64_unchecked (&field->value);
1446 * gst_structure_get_date:
1447 * @structure: a #GstStructure
1448 * @fieldname: the name of a field
1449 * @value: (out callee-allocates): a pointer to a #GDate to set
1451 * Sets the date pointed to by @value corresponding to the date of the
1452 * given field. Caller is responsible for making sure the field exists
1453 * and has the correct type.
1455 * On success @value will point to a newly-allocated copy of the date which
1456 * should be freed with g_date_free() when no longer needed (note: this is
1457 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1458 * copy of the string).
1460 * Returns: %TRUE if the value could be set correctly. If there was no field
1461 * with @fieldname or the existing field did not contain a data, this function
1465 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1468 GstStructureField *field;
1470 g_return_val_if_fail (structure != NULL, FALSE);
1471 g_return_val_if_fail (fieldname != NULL, FALSE);
1472 g_return_val_if_fail (value != NULL, FALSE);
1474 field = gst_structure_get_field (structure, fieldname);
1478 if (!G_VALUE_HOLDS (&field->value, G_TYPE_DATE))
1481 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1482 *value = g_value_dup_boxed (&field->value);
1488 * gst_structure_get_date_time:
1489 * @structure: a #GstStructure
1490 * @fieldname: the name of a field
1491 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1493 * Sets the datetime pointed to by @value corresponding to the datetime of the
1494 * given field. Caller is responsible for making sure the field exists
1495 * and has the correct type.
1497 * On success @value will point to a reference of the datetime which
1498 * should be unreffed with gst_date_time_unref() when no longer needed
1499 * (note: this is inconsistent with e.g. gst_structure_get_string()
1500 * which doesn't return a copy of the string).
1502 * Returns: %TRUE if the value could be set correctly. If there was no field
1503 * with @fieldname or the existing field did not contain a data, this function
1507 gst_structure_get_date_time (const GstStructure * structure,
1508 const gchar * fieldname, GstDateTime ** value)
1510 GstStructureField *field;
1512 g_return_val_if_fail (structure != NULL, FALSE);
1513 g_return_val_if_fail (fieldname != NULL, FALSE);
1514 g_return_val_if_fail (value != NULL, FALSE);
1516 field = gst_structure_get_field (structure, fieldname);
1520 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1523 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1524 *value = g_value_dup_boxed (&field->value);
1530 * gst_structure_get_clock_time:
1531 * @structure: a #GstStructure
1532 * @fieldname: the name of a field
1533 * @value: (out): a pointer to a #GstClockTime to set
1535 * Sets the clock time pointed to by @value corresponding to the clock time
1536 * of the given field. Caller is responsible for making sure the field exists
1537 * and has the correct type.
1539 * Returns: %TRUE if the value could be set correctly. If there was no field
1540 * with @fieldname or the existing field did not contain a #GstClockTime, this
1541 * function returns %FALSE.
1544 gst_structure_get_clock_time (const GstStructure * structure,
1545 const gchar * fieldname, GstClockTime * value)
1547 return gst_structure_get_uint64 (structure, fieldname, value);
1551 * gst_structure_get_double:
1552 * @structure: a #GstStructure
1553 * @fieldname: the name of a field
1554 * @value: (out): a pointer to a gdouble to set
1556 * Sets the double pointed to by @value corresponding to the value of the
1557 * given field. Caller is responsible for making sure the field exists
1558 * and has the correct type.
1560 * Returns: %TRUE if the value could be set correctly. If there was no field
1561 * with @fieldname or the existing field did not contain a double, this
1562 * function returns %FALSE.
1565 gst_structure_get_double (const GstStructure * structure,
1566 const gchar * fieldname, gdouble * value)
1568 GstStructureField *field;
1570 g_return_val_if_fail (structure != NULL, FALSE);
1571 g_return_val_if_fail (fieldname != NULL, FALSE);
1572 g_return_val_if_fail (value != NULL, FALSE);
1574 field = gst_structure_get_field (structure, fieldname);
1578 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1581 *value = gst_g_value_get_double_unchecked (&field->value);
1587 * gst_structure_get_string:
1588 * @structure: a #GstStructure
1589 * @fieldname: the name of a field
1591 * Finds the field corresponding to @fieldname, and returns the string
1592 * contained in the field's value. Caller is responsible for making
1593 * sure the field exists and has the correct type.
1595 * The string should not be modified, and remains valid until the next
1596 * call to a gst_structure_*() function with the given structure.
1598 * Returns: a pointer to the string or %NULL when the field did not exist
1599 * or did not contain a string.
1602 gst_structure_get_string (const GstStructure * structure,
1603 const gchar * fieldname)
1605 GstStructureField *field;
1607 g_return_val_if_fail (structure != NULL, NULL);
1608 g_return_val_if_fail (fieldname != NULL, NULL);
1610 field = gst_structure_get_field (structure, fieldname);
1614 if (!G_VALUE_HOLDS_STRING (&field->value))
1617 return gst_g_value_get_string_unchecked (&field->value);
1621 * gst_structure_get_enum:
1622 * @structure: a #GstStructure
1623 * @fieldname: the name of a field
1624 * @enumtype: the enum type of a field
1625 * @value: (out): a pointer to an int to set
1627 * Sets the int pointed to by @value corresponding to the value of the
1628 * given field. Caller is responsible for making sure the field exists,
1629 * has the correct type and that the enumtype is correct.
1631 * Returns: %TRUE if the value could be set correctly. If there was no field
1632 * with @fieldname or the existing field did not contain an enum of the given
1633 * type, this function returns %FALSE.
1636 gst_structure_get_enum (const GstStructure * structure,
1637 const gchar * fieldname, GType enumtype, gint * value)
1639 GstStructureField *field;
1641 g_return_val_if_fail (structure != NULL, FALSE);
1642 g_return_val_if_fail (fieldname != NULL, FALSE);
1643 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1644 g_return_val_if_fail (value != NULL, FALSE);
1646 field = gst_structure_get_field (structure, fieldname);
1650 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1653 *value = g_value_get_enum (&field->value);
1659 * gst_structure_get_fraction:
1660 * @structure: a #GstStructure
1661 * @fieldname: the name of a field
1662 * @value_numerator: (out): a pointer to an int to set
1663 * @value_denominator: (out): a pointer to an int to set
1665 * Sets the integers pointed to by @value_numerator and @value_denominator
1666 * corresponding to the value of the given field. Caller is responsible
1667 * for making sure the field exists and has the correct type.
1669 * Returns: %TRUE if the values could be set correctly. If there was no field
1670 * with @fieldname or the existing field did not contain a GstFraction, this
1671 * function returns %FALSE.
1674 gst_structure_get_fraction (const GstStructure * structure,
1675 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1677 GstStructureField *field;
1679 g_return_val_if_fail (structure != NULL, FALSE);
1680 g_return_val_if_fail (fieldname != NULL, FALSE);
1681 g_return_val_if_fail (value_numerator != NULL, FALSE);
1682 g_return_val_if_fail (value_denominator != NULL, FALSE);
1684 field = gst_structure_get_field (structure, fieldname);
1688 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1691 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1692 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1697 typedef struct _GstStructureAbbreviation
1699 const gchar *type_name;
1702 GstStructureAbbreviation;
1704 /* return a copy of an array of GstStructureAbbreviation containing all the
1705 * known type_string, GType maps, including abbreviations for common types */
1706 static GstStructureAbbreviation *
1707 gst_structure_get_abbrs (gint * n_abbrs)
1709 static GstStructureAbbreviation *abbrs = NULL;
1710 static volatile gsize num = 0;
1712 if (g_once_init_enter (&num)) {
1713 /* dynamically generate the array */
1715 GstStructureAbbreviation dyn_abbrs[] = {
1720 {"uint", G_TYPE_UINT}
1724 {"float", G_TYPE_FLOAT}
1728 {"double", G_TYPE_DOUBLE}
1730 {"d", G_TYPE_DOUBLE}
1732 {"buffer", GST_TYPE_BUFFER}
1734 {"fraction", GST_TYPE_FRACTION}
1736 {"boolean", G_TYPE_BOOLEAN}
1738 {"bool", G_TYPE_BOOLEAN}
1740 {"b", G_TYPE_BOOLEAN}
1742 {"string", G_TYPE_STRING}
1744 {"str", G_TYPE_STRING}
1746 {"s", G_TYPE_STRING}
1748 {"structure", GST_TYPE_STRUCTURE}
1750 {"date", G_TYPE_DATE}
1752 {"datetime", GST_TYPE_DATE_TIME}
1754 {"bitmask", GST_TYPE_BITMASK}
1756 {"sample", GST_TYPE_SAMPLE}
1758 {"taglist", GST_TYPE_TAG_LIST}
1760 _num = G_N_ELEMENTS (dyn_abbrs);
1761 /* permanently allocate and copy the array now */
1762 abbrs = g_new0 (GstStructureAbbreviation, _num);
1763 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1764 g_once_init_leave (&num, _num);
1771 /* given a type_name that could be a type abbreviation or a registered GType,
1772 * return a matching GType */
1774 gst_structure_gtype_from_abbr (const char *type_name)
1777 GstStructureAbbreviation *abbrs;
1780 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1782 abbrs = gst_structure_get_abbrs (&n_abbrs);
1784 for (i = 0; i < n_abbrs; i++) {
1785 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1786 return abbrs[i].type;
1790 /* this is the fallback */
1791 return g_type_from_name (type_name);
1795 gst_structure_to_abbr (GType type)
1798 GstStructureAbbreviation *abbrs;
1801 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1803 abbrs = gst_structure_get_abbrs (&n_abbrs);
1805 for (i = 0; i < n_abbrs; i++) {
1806 if (type == abbrs[i].type) {
1807 return abbrs[i].type_name;
1811 return g_type_name (type);
1815 gst_structure_value_get_generic_type (GValue * val)
1817 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1818 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1819 GArray *array = g_value_peek_pointer (val);
1821 if (array->len > 0) {
1822 GValue *value = &g_array_index (array, GValue, 0);
1824 return gst_structure_value_get_generic_type (value);
1828 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1830 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1831 return G_TYPE_INT64;
1832 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1833 return G_TYPE_DOUBLE;
1834 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1835 return GST_TYPE_FRACTION;
1837 return G_VALUE_TYPE (val);
1841 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1844 GstStructureField *field;
1847 g_return_val_if_fail (s != NULL, FALSE);
1849 len = GST_STRUCTURE_FIELDS (structure)->len;
1850 for (i = 0; i < len; i++) {
1854 field = GST_STRUCTURE_FIELD (structure, i);
1856 t = gst_value_serialize (&field->value);
1857 type = gst_structure_value_get_generic_type (&field->value);
1859 g_string_append_len (s, ", ", 2);
1860 /* FIXME: do we need to escape fieldnames? */
1861 g_string_append (s, g_quark_to_string (field->name));
1862 g_string_append_len (s, "=(", 2);
1863 g_string_append (s, gst_structure_to_abbr (type));
1864 g_string_append_c (s, ')');
1865 g_string_append (s, t == NULL ? "NULL" : t);
1869 g_string_append_c (s, ';');
1874 * gst_structure_to_string:
1875 * @structure: a #GstStructure
1877 * Converts @structure to a human-readable string representation.
1879 * For debugging purposes its easier to do something like this:
1881 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1883 * This prints the structure in human readable form.
1885 * The current implementation of serialization will lead to unexpected results
1886 * when there are nested #GstCaps / #GstStructure deeper than one level.
1888 * Free-function: g_free
1890 * Returns: (transfer full): a pointer to string allocated by g_malloc().
1891 * g_free() after usage.
1894 gst_structure_to_string (const GstStructure * structure)
1898 /* NOTE: This function is potentially called by the debug system,
1899 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1900 * should be careful to avoid recursion. This includes any functions
1901 * called by gst_structure_to_string. In particular, calls should
1902 * not use the GST_PTR_FORMAT extension. */
1904 g_return_val_if_fail (structure != NULL, NULL);
1906 /* we estimate a minimum size based on the number of fields in order to
1907 * avoid unnecessary reallocs within GString */
1908 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1909 g_string_append (s, g_quark_to_string (structure->name));
1910 priv_gst_structure_append_to_gstring (structure, s);
1911 return g_string_free (s, FALSE);
1915 * r will still point to the string. if end == next, the string will not be
1916 * null-terminated. In all other cases it will be.
1917 * end = pointer to char behind end of string, next = pointer to start of
1919 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1922 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1933 ret = gst_structure_parse_simple_string (s, end);
1943 if (G_UNLIKELY (*s == 0))
1945 if (G_UNLIKELY (*s == '\\'))
1953 /* Find the closing quotes */
1956 if (G_UNLIKELY (*s == 0))
1958 if (G_UNLIKELY (*s == '\\'))
1973 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1976 GValue value1 = { 0 };
1977 GValue value2 = { 0 };
1978 GValue value3 = { 0 };
1980 gboolean ret, have_step = FALSE;
1986 ret = gst_structure_parse_value (s, &s, &value1, type);
1990 while (g_ascii_isspace (*s))
1997 while (g_ascii_isspace (*s))
2000 ret = gst_structure_parse_value (s, &s, &value2, type);
2004 while (g_ascii_isspace (*s))
2007 /* optional step for int and int64 */
2008 if (G_VALUE_TYPE (&value1) == G_TYPE_INT
2009 || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2013 while (g_ascii_isspace (*s))
2016 ret = gst_structure_parse_value (s, &s, &value3, type);
2020 while (g_ascii_isspace (*s))
2031 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
2033 if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
2036 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
2037 range_type = GST_TYPE_DOUBLE_RANGE;
2038 g_value_init (value, range_type);
2039 gst_value_set_double_range (value,
2040 gst_g_value_get_double_unchecked (&value1),
2041 gst_g_value_get_double_unchecked (&value2));
2042 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
2043 range_type = GST_TYPE_INT_RANGE;
2044 g_value_init (value, range_type);
2046 gst_value_set_int_range_step (value,
2047 gst_g_value_get_int_unchecked (&value1),
2048 gst_g_value_get_int_unchecked (&value2),
2049 gst_g_value_get_int_unchecked (&value3));
2051 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
2052 gst_g_value_get_int_unchecked (&value2));
2053 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2054 range_type = GST_TYPE_INT64_RANGE;
2055 g_value_init (value, range_type);
2057 gst_value_set_int64_range_step (value,
2058 gst_g_value_get_int64_unchecked (&value1),
2059 gst_g_value_get_int64_unchecked (&value2),
2060 gst_g_value_get_int64_unchecked (&value3));
2062 gst_value_set_int64_range (value,
2063 gst_g_value_get_int64_unchecked (&value1),
2064 gst_g_value_get_int64_unchecked (&value2));
2065 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
2066 range_type = GST_TYPE_FRACTION_RANGE;
2067 g_value_init (value, range_type);
2068 gst_value_set_fraction_range (value, &value1, &value2);
2078 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
2079 GType type, GType list_type, char begin, char end)
2081 GValue list_value = { 0 };
2085 g_value_init (value, list_type);
2086 array = g_value_peek_pointer (value);
2092 while (g_ascii_isspace (*s))
2100 ret = gst_structure_parse_value (s, &s, &list_value, type);
2104 g_array_append_val (array, list_value);
2106 while (g_ascii_isspace (*s))
2114 while (g_ascii_isspace (*s))
2117 memset (&list_value, 0, sizeof (list_value));
2118 ret = gst_structure_parse_value (s, &s, &list_value, type);
2122 g_array_append_val (array, list_value);
2123 while (g_ascii_isspace (*s))
2134 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2136 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2141 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2144 return gst_structure_parse_any_list (s, after, value, type,
2145 GST_TYPE_ARRAY, '<', '>');
2149 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2153 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2163 gst_structure_parse_field (gchar * str,
2164 gchar ** after, GstStructureField * field)
2173 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2176 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2177 GST_WARNING ("failed to parse simple string, str=%s", str);
2182 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2185 if (G_UNLIKELY (*s != '=')) {
2186 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2193 field->name = g_quark_from_string (name);
2194 GST_DEBUG ("trying field name '%s'", name);
2197 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2199 GST_WARNING ("failed to parse value %s", str);
2208 gst_structure_parse_value (gchar * str,
2209 gchar ** after, GValue * value, GType default_type)
2218 GType type = default_type;
2221 while (g_ascii_isspace (*s))
2224 /* check if there's a (type_name) 'cast' */
2228 while (g_ascii_isspace (*s))
2231 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2234 while (g_ascii_isspace (*s))
2236 if (G_UNLIKELY (*s != ')'))
2239 while (g_ascii_isspace (*s))
2244 type = gst_structure_gtype_from_abbr (type_name);
2245 GST_DEBUG ("trying type name '%s'", type_name);
2248 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2249 GST_WARNING ("invalid type");
2254 while (g_ascii_isspace (*s))
2257 ret = gst_structure_parse_range (s, &s, value, type);
2258 } else if (*s == '{') {
2259 ret = gst_structure_parse_list (s, &s, value, type);
2260 } else if (*s == '<') {
2261 ret = gst_structure_parse_array (s, &s, value, type);
2265 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2267 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2272 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2274 /* Set NULL terminator for deserialization */
2278 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2279 g_value_init (value, try_types[i]);
2280 ret = gst_value_deserialize (value, value_s);
2283 g_value_unset (value);
2286 g_value_init (value, type);
2288 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2289 (type != G_TYPE_STRING))))
2291 /* Set NULL terminator for deserialization */
2295 ret = gst_value_deserialize (value, value_s);
2296 if (G_UNLIKELY (!ret))
2297 g_value_unset (value);
2308 priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end,
2316 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2317 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2318 && g_ascii_isspace (r[1]))))
2323 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2324 GST_WARNING ("Failed to parse structure string '%s'", str);
2335 priv_gst_structure_parse_fields (gchar * str, gchar ** end,
2336 GstStructure * structure)
2339 GstStructureField field;
2344 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2345 && g_ascii_isspace (r[1]))))
2348 /* end of structure, get the next char and finish */
2353 /* accept \0 as end delimiter */
2356 if (G_UNLIKELY (*r != ',')) {
2357 GST_WARNING ("Failed to find delimiter, r=%s", r);
2361 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2362 && g_ascii_isspace (r[1]))))
2365 memset (&field, 0, sizeof (field));
2366 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2367 GST_WARNING ("Failed to parse field, r=%s", r);
2370 gst_structure_set_field (structure, &field);
2379 * gst_structure_new_from_string:
2380 * @string: a string representation of a #GstStructure
2382 * Creates a #GstStructure from a string representation.
2383 * If end is not %NULL, a pointer to the place inside the given string
2384 * where parsing ended will be returned.
2386 * The current implementation of serialization will lead to unexpected results
2387 * when there are nested #GstCaps / #GstStructure deeper than one level.
2389 * Free-function: gst_structure_free
2391 * Returns: (transfer full): a new #GstStructure or %NULL when the string could
2392 * not be parsed. Free with gst_structure_free() after use.
2397 gst_structure_new_from_string (const gchar * string)
2399 return gst_structure_from_string (string, NULL);
2403 * gst_structure_from_string:
2404 * @string: a string representation of a #GstStructure.
2405 * @end: (out) (allow-none) (transfer none) (skip): pointer to store the end of the string in.
2407 * Creates a #GstStructure from a string representation.
2408 * If end is not %NULL, a pointer to the place inside the given string
2409 * where parsing ended will be returned.
2411 * Free-function: gst_structure_free
2413 * Returns: (transfer full): a new #GstStructure or %NULL when the string could
2414 * not be parsed. Free with gst_structure_free() after use.
2417 gst_structure_from_string (const gchar * string, gchar ** end)
2424 GstStructure *structure = NULL;
2426 g_return_val_if_fail (string != NULL, NULL);
2428 copy = g_strdup (string);
2431 if (!priv_gst_structure_parse_name (r, &name, &w, &r))
2436 structure = gst_structure_new_empty (name);
2439 if (G_UNLIKELY (structure == NULL))
2442 if (!priv_gst_structure_parse_fields (r, &r, structure))
2446 *end = (char *) string + (r - copy);
2448 g_warning ("gst_structure_from_string did not consume whole string,"
2449 " but caller did not provide end pointer (\"%s\")", string);
2456 gst_structure_free (structure);
2462 gst_structure_transform_to_string (const GValue * src_value,
2463 GValue * dest_value)
2465 g_return_if_fail (src_value != NULL);
2466 g_return_if_fail (dest_value != NULL);
2468 dest_value->data[0].v_pointer =
2469 gst_structure_to_string (src_value->data[0].v_pointer);
2472 static GstStructure *
2473 gst_structure_copy_conditional (const GstStructure * structure)
2476 return gst_structure_copy (structure);
2480 /* fixate utility functions */
2483 * gst_structure_fixate_field_nearest_int:
2484 * @structure: a #GstStructure
2485 * @field_name: a field in @structure
2486 * @target: the target value of the fixation
2488 * Fixates a #GstStructure by changing the given field to the nearest
2489 * integer to @target that is a subset of the existing field.
2491 * Returns: %TRUE if the structure could be fixated
2494 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2495 const char *field_name, int target)
2497 const GValue *value;
2499 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2500 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2502 value = gst_structure_get_value (structure, field_name);
2504 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2507 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2510 x = gst_value_get_int_range_min (value);
2513 x = gst_value_get_int_range_max (value);
2516 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2518 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2519 const GValue *list_value;
2522 int best_index = -1;
2524 n = gst_value_list_get_size (value);
2525 for (i = 0; i < n; i++) {
2526 list_value = gst_value_list_get_value (value, i);
2527 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2528 int x = gst_g_value_get_int_unchecked (list_value);
2530 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2536 if (best_index != -1) {
2537 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2547 * gst_structure_fixate_field_nearest_double:
2548 * @structure: a #GstStructure
2549 * @field_name: a field in @structure
2550 * @target: the target value of the fixation
2552 * Fixates a #GstStructure by changing the given field to the nearest
2553 * double to @target that is a subset of the existing field.
2555 * Returns: %TRUE if the structure could be fixated
2558 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2559 const char *field_name, double target)
2561 const GValue *value;
2563 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2564 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2566 value = gst_structure_get_value (structure, field_name);
2568 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2571 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2574 x = gst_value_get_double_range_min (value);
2577 x = gst_value_get_double_range_max (value);
2580 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2582 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2583 const GValue *list_value;
2586 int best_index = -1;
2588 n = gst_value_list_get_size (value);
2589 for (i = 0; i < n; i++) {
2590 list_value = gst_value_list_get_value (value, i);
2591 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2592 double x = gst_g_value_get_double_unchecked (list_value);
2594 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2600 if (best_index != -1) {
2601 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2612 * gst_structure_fixate_field_boolean:
2613 * @structure: a #GstStructure
2614 * @field_name: a field in @structure
2615 * @target: the target value of the fixation
2617 * Fixates a #GstStructure by changing the given @field_name field to the given
2618 * @target boolean if that field is not fixed yet.
2620 * Returns: %TRUE if the structure could be fixated
2623 gst_structure_fixate_field_boolean (GstStructure * structure,
2624 const char *field_name, gboolean target)
2626 const GValue *value;
2628 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2629 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2631 value = gst_structure_get_value (structure, field_name);
2633 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2636 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2637 const GValue *list_value;
2640 int best_index = -1;
2642 n = gst_value_list_get_size (value);
2643 for (i = 0; i < n; i++) {
2644 list_value = gst_value_list_get_value (value, i);
2645 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2646 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2648 if (best_index == -1 || x == target) {
2654 if (best_index != -1) {
2655 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2665 * gst_structure_fixate_field_string:
2666 * @structure: a #GstStructure
2667 * @field_name: a field in @structure
2668 * @target: the target value of the fixation
2670 * Fixates a #GstStructure by changing the given @field_name field to the given
2671 * @target string if that field is not fixed yet.
2673 * Returns: %TRUE if the structure could be fixated
2676 gst_structure_fixate_field_string (GstStructure * structure,
2677 const gchar * field_name, const gchar * target)
2679 const GValue *value;
2681 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2682 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2684 value = gst_structure_get_value (structure, field_name);
2686 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2689 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2690 const GValue *list_value;
2692 const gchar *best = NULL;
2693 int best_index = -1;
2695 n = gst_value_list_get_size (value);
2696 for (i = 0; i < n; i++) {
2697 list_value = gst_value_list_get_value (value, i);
2698 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2699 const gchar *x = g_value_get_string (list_value);
2701 if (best_index == -1 || g_str_equal (x, target)) {
2707 if (best_index != -1) {
2708 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2718 * gst_structure_fixate_field_nearest_fraction:
2719 * @structure: a #GstStructure
2720 * @field_name: a field in @structure
2721 * @target_numerator: The numerator of the target value of the fixation
2722 * @target_denominator: The denominator of the target value of the fixation
2724 * Fixates a #GstStructure by changing the given field to the nearest
2725 * fraction to @target_numerator/@target_denominator that is a subset
2726 * of the existing field.
2728 * Returns: %TRUE if the structure could be fixated
2731 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2732 const char *field_name, const gint target_numerator,
2733 const gint target_denominator)
2735 const GValue *value;
2737 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2738 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2739 g_return_val_if_fail (target_denominator != 0, FALSE);
2741 value = gst_structure_get_value (structure, field_name);
2743 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2746 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2747 const GValue *x, *new_value;
2748 GValue target = { 0 };
2749 g_value_init (&target, GST_TYPE_FRACTION);
2750 gst_value_set_fraction (&target, target_numerator, target_denominator);
2752 new_value = ⌖
2753 x = gst_value_get_fraction_range_min (value);
2754 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2756 x = gst_value_get_fraction_range_max (value);
2757 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2760 gst_structure_set_value (structure, field_name, new_value);
2761 g_value_unset (&target);
2763 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2764 const GValue *list_value;
2766 const GValue *best = NULL;
2769 gdouble best_diff = G_MAXDOUBLE;
2771 target = (gdouble) target_numerator / (gdouble) target_denominator;
2773 GST_DEBUG ("target %g, best %g", target, best_diff);
2777 n = gst_value_list_get_size (value);
2778 for (i = 0; i < n; i++) {
2779 list_value = gst_value_list_get_value (value, i);
2780 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2782 gdouble list_double;
2784 num = gst_value_get_fraction_numerator (list_value);
2785 denom = gst_value_get_fraction_denominator (list_value);
2787 list_double = ((gdouble) num / (gdouble) denom);
2788 cur_diff = target - list_double;
2790 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2793 cur_diff = -cur_diff;
2795 if (!best || cur_diff < best_diff) {
2796 GST_DEBUG ("new best %g", list_double);
2798 best_diff = cur_diff;
2803 gst_structure_set_value (structure, field_name, best);
2812 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2814 GstStructure *s = data;
2817 if (gst_value_fixate (&v, value)) {
2818 gst_structure_id_take_value (s, field_id, &v);
2824 * gst_structure_fixate_field:
2825 * @structure: a #GstStructure
2826 * @field_name: a field in @structure
2828 * Fixates a #GstStructure by changing the given field with its fixated value.
2830 * Returns: %TRUE if the structure field could be fixated
2833 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2835 GstStructureField *field;
2837 g_return_val_if_fail (structure != NULL, FALSE);
2838 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2840 if (!(field = gst_structure_get_field (structure, field_name)))
2843 return default_fixate (field->name, &field->value, structure);
2846 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2847 * (useful for message parsing functions where the return location is user
2848 * supplied and the user may pass %NULL if the value isn't of interest) */
2849 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2851 const GValue *_value = (value); \
2852 guint _flags = (flags); \
2853 GType _value_type = G_VALUE_TYPE (_value); \
2854 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2855 const gchar *_lcopy_format = _vtable->lcopy_format; \
2856 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2857 guint _n_values = 0; \
2859 while (*_lcopy_format != '\0') { \
2860 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2861 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2864 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2865 *(__error) = g_strdup_printf ("either all or none of the return " \
2866 "locations for field '%s' need to be NULL", fieldname); \
2867 } else if (_cvalues[0].v_pointer != NULL) { \
2868 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2873 * gst_structure_get_valist:
2874 * @structure: a #GstStructure
2875 * @first_fieldname: the name of the first field to read
2876 * @args: variable arguments
2878 * Parses the variable arguments and reads fields from @structure accordingly.
2879 * valist-variant of gst_structure_get(). Look at the documentation of
2880 * gst_structure_get() for more details.
2882 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2885 gst_structure_get_valist (const GstStructure * structure,
2886 const char *first_fieldname, va_list args)
2888 const char *field_name;
2889 GType expected_type = G_TYPE_INVALID;
2891 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2892 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2894 field_name = first_fieldname;
2895 while (field_name) {
2896 const GValue *val = NULL;
2899 expected_type = va_arg (args, GType);
2901 val = gst_structure_get_value (structure, field_name);
2906 if (G_VALUE_TYPE (val) != expected_type)
2909 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2911 g_warning ("%s: %s", G_STRFUNC, err);
2916 field_name = va_arg (args, const gchar *);
2924 GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2925 field_name, structure);
2930 GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
2931 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2932 GST_STR_NULL (g_type_name (expected_type)),
2933 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2940 * gst_structure_id_get_valist:
2941 * @structure: a #GstStructure
2942 * @first_field_id: the quark of the first field to read
2943 * @args: variable arguments
2945 * Parses the variable arguments and reads fields from @structure accordingly.
2946 * valist-variant of gst_structure_id_get(). Look at the documentation of
2947 * gst_structure_id_get() for more details.
2949 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2952 gst_structure_id_get_valist (const GstStructure * structure,
2953 GQuark first_field_id, va_list args)
2956 GType expected_type = G_TYPE_INVALID;
2958 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2959 g_return_val_if_fail (first_field_id != 0, FALSE);
2961 field_id = first_field_id;
2963 const GValue *val = NULL;
2966 expected_type = va_arg (args, GType);
2968 val = gst_structure_id_get_value (structure, field_id);
2973 if (G_VALUE_TYPE (val) != expected_type)
2976 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2978 g_warning ("%s: %s", G_STRFUNC, err);
2983 field_id = va_arg (args, GQuark);
2991 GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2992 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2997 GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
2998 "field was of type '%s': %" GST_PTR_FORMAT,
2999 g_quark_to_string (field_id),
3000 GST_STR_NULL (g_type_name (expected_type)),
3001 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
3008 * gst_structure_get:
3009 * @structure: a #GstStructure
3010 * @first_fieldname: the name of the first field to read
3011 * @...: variable arguments
3013 * Parses the variable arguments and reads fields from @structure accordingly.
3014 * Variable arguments should be in the form field name, field type
3015 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3016 * The last variable argument should be %NULL.
3018 * For refcounted (mini)objects you will receive a new reference which
3019 * you must release with a suitable _unref() when no longer needed. For
3020 * strings and boxed types you will receive a copy which you will need to
3021 * release with either g_free() or the suitable function for the boxed type.
3023 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
3024 * because the field requested did not exist, or was of a type other
3025 * than the type specified), otherwise %TRUE.
3028 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
3034 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3035 g_return_val_if_fail (first_fieldname != NULL, FALSE);
3037 va_start (args, first_fieldname);
3038 ret = gst_structure_get_valist (structure, first_fieldname, args);
3045 * gst_structure_id_get:
3046 * @structure: a #GstStructure
3047 * @first_field_id: the quark of the first field to read
3048 * @...: variable arguments
3050 * Parses the variable arguments and reads fields from @structure accordingly.
3051 * Variable arguments should be in the form field id quark, field type
3052 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3053 * The last variable argument should be %NULL (technically it should be a
3054 * 0 quark, but we require %NULL so compilers that support it can check for
3055 * the %NULL terminator and warn if it's not there).
3057 * This function is just like gst_structure_get() only that it is slightly
3058 * more efficient since it saves the string-to-quark lookup in the global
3061 * For refcounted (mini)objects you will receive a new reference which
3062 * you must release with a suitable _unref() when no longer needed. For
3063 * strings and boxed types you will receive a copy which you will need to
3064 * release with either g_free() or the suitable function for the boxed type.
3066 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
3067 * because the field requested did not exist, or was of a type other
3068 * than the type specified), otherwise %TRUE.
3071 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
3077 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3078 g_return_val_if_fail (first_field_id != 0, FALSE);
3080 va_start (args, first_field_id);
3081 ret = gst_structure_id_get_valist (structure, first_field_id, args);
3088 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
3091 const GstStructure *struct1 = (const GstStructure *) data;
3092 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
3094 if (G_UNLIKELY (val1 == NULL))
3096 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
3104 * gst_structure_is_equal:
3105 * @structure1: a #GstStructure.
3106 * @structure2: a #GstStructure.
3108 * Tests if the two #GstStructure are equal.
3110 * Returns: %TRUE if the two structures have the same name and field.
3113 gst_structure_is_equal (const GstStructure * structure1,
3114 const GstStructure * structure2)
3116 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
3117 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
3119 if (G_UNLIKELY (structure1 == structure2))
3122 if (structure1->name != structure2->name) {
3125 if (GST_STRUCTURE_FIELDS (structure1)->len !=
3126 GST_STRUCTURE_FIELDS (structure2)->len) {
3130 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
3131 (gpointer) structure2);
3138 const GstStructure *intersect;
3143 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
3145 IntersectData *idata = (IntersectData *) data;
3146 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3148 if (G_UNLIKELY (val2 == NULL)) {
3149 gst_structure_id_set_value (idata->dest, id, val1);
3151 GValue dest_value = { 0 };
3152 if (gst_value_intersect (&dest_value, val1, val2)) {
3153 gst_structure_id_take_value (idata->dest, id, &dest_value);
3162 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
3164 IntersectData *idata = (IntersectData *) data;
3165 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3167 if (G_UNLIKELY (val2 == NULL)) {
3168 gst_structure_id_set_value (idata->dest, id, val1);
3174 * gst_structure_intersect:
3175 * @struct1: a #GstStructure
3176 * @struct2: a #GstStructure
3178 * Intersects @struct1 and @struct2 and returns the intersection.
3180 * Returns: Intersection of @struct1 and @struct2
3183 gst_structure_intersect (const GstStructure * struct1,
3184 const GstStructure * struct2)
3188 g_assert (struct1 != NULL);
3189 g_assert (struct2 != NULL);
3191 if (G_UNLIKELY (struct1->name != struct2->name))
3194 /* copy fields from struct1 which we have not in struct2 to target
3195 * intersect if we have the field in both */
3196 data.dest = gst_structure_new_id_empty (struct1->name);
3197 data.intersect = struct2;
3198 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3199 gst_structure_intersect_field1, &data)))
3202 /* copy fields from struct2 which we have not in struct1 to target */
3203 data.intersect = struct1;
3204 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3205 gst_structure_intersect_field2, &data)))
3211 gst_structure_free (data.dest);
3216 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3219 GstStructure *other = (GstStructure *) data;
3220 const GValue *val2 = gst_structure_id_get_value (other, id);
3222 if (G_LIKELY (val2)) {
3223 if (!gst_value_can_intersect (val1, val2)) {
3226 gint eq = gst_value_compare (val1, val2);
3228 if (eq == GST_VALUE_UNORDERED) {
3229 /* we need to try interseting */
3230 if (!gst_value_intersect (NULL, val1, val2)) {
3233 } else if (eq != GST_VALUE_EQUAL) {
3242 * gst_structure_can_intersect:
3243 * @struct1: a #GstStructure
3244 * @struct2: a #GstStructure
3246 * Tries intersecting @struct1 and @struct2 and reports whether the result
3247 * would not be empty.
3249 * Returns: %TRUE if intersection would not be empty
3252 gst_structure_can_intersect (const GstStructure * struct1,
3253 const GstStructure * struct2)
3255 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3256 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3258 if (G_UNLIKELY (struct1->name != struct2->name))
3261 /* tries to intersect if we have the field in both */
3262 return gst_structure_foreach ((GstStructure *) struct1,
3263 gst_caps_structure_can_intersect_field, (gpointer) struct2);
3267 gst_caps_structure_is_superset_field (GQuark field_id, const GValue * value,
3270 GstStructure *subset = user_data;
3271 const GValue *other;
3274 if (!(other = gst_structure_id_get_value (subset, field_id)))
3275 /* field is missing in the subset => no subset */
3278 comparison = gst_value_compare (value, other);
3280 /* equal values are subset */
3281 if (comparison == GST_VALUE_EQUAL)
3284 /* ordered, but unequal, values are not */
3285 if (comparison != GST_VALUE_UNORDERED)
3288 return gst_value_is_subset (other, value);
3292 * gst_structure_is_subset:
3293 * @subset: a #GstStructure
3294 * @superset: a potentially greater #GstStructure
3296 * Checks if @subset is a subset of @superset, i.e. has the same
3297 * structure name and for all fields that are existing in @superset,
3298 * @subset has a value that is a subset of the value in @superset.
3300 * Returns: %TRUE if @subset is a subset of @superset
3303 gst_structure_is_subset (const GstStructure * subset,
3304 const GstStructure * superset)
3306 if ((superset->name != subset->name) ||
3307 (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3310 return gst_structure_foreach ((GstStructure *) superset,
3311 gst_caps_structure_is_superset_field, (gpointer) subset);
3316 * gst_structure_fixate:
3317 * @structure: a #GstStructure
3319 * Fixate all values in @structure using gst_value_fixate().
3320 * @structure will be modified in-place and should be writable.
3323 gst_structure_fixate (GstStructure * structure)
3325 g_return_if_fail (GST_IS_STRUCTURE (structure));
3327 gst_structure_foreach (structure, default_fixate, structure);