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 must not be empty either, but may be NULL.
56 * Last reviewed on 2012-03-29 (0.11.3)
65 #include "gst_private.h"
68 #include <gobject/gvaluecollector.h>
70 GST_DEBUG_CATEGORY_STATIC (gst_structure_debug);
71 #define GST_CAT_DEFAULT gst_structure_debug
73 typedef struct _GstStructureField GstStructureField;
75 struct _GstStructureField
85 /* owned by parent structure, NULL if no parent */
86 gint *parent_refcount;
91 #define GST_STRUCTURE_REFCOUNT(s) (((GstStructureImpl*)(s))->parent_refcount)
92 #define GST_STRUCTURE_FIELDS(s) (((GstStructureImpl*)(s))->fields)
94 #define GST_STRUCTURE_FIELD(structure, index) \
95 &g_array_index(GST_STRUCTURE_FIELDS(structure), GstStructureField, (index))
97 #define IS_MUTABLE(structure) \
98 (!GST_STRUCTURE_REFCOUNT(structure) || \
99 g_atomic_int_get (GST_STRUCTURE_REFCOUNT(structure)) == 1)
101 #define IS_TAGLIST(structure) \
102 (structure->name == GST_QUARK (TAGLIST))
104 static void gst_structure_set_field (GstStructure * structure,
105 GstStructureField * field);
106 static GstStructureField *gst_structure_get_field (const GstStructure *
107 structure, const gchar * fieldname);
108 static GstStructureField *gst_structure_id_get_field (const GstStructure *
109 structure, GQuark field);
110 static void gst_structure_transform_to_string (const GValue * src_value,
111 GValue * dest_value);
112 static GstStructure *gst_structure_copy_conditional (const GstStructure *
114 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
115 GValue * value, GType default_type);
116 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
118 GType _gst_structure_type = 0;
121 G_DEFINE_BOXED_TYPE (GstStructure, gst_structure,
122 gst_structure_copy_conditional, gst_structure_free);
125 _priv_gst_structure_initialize (void)
127 _gst_structure_type = gst_structure_get_type ();
129 g_value_register_transform_func (_gst_structure_type, G_TYPE_STRING,
130 gst_structure_transform_to_string);
132 GST_DEBUG_CATEGORY_INIT (gst_structure_debug, "structure", 0,
133 "GstStructure debug");
136 static GstStructure *
137 gst_structure_new_id_empty_with_size (GQuark quark, guint prealloc)
139 GstStructureImpl *structure;
141 structure = g_slice_new (GstStructureImpl);
142 ((GstStructure *) structure)->type = _gst_structure_type;
143 ((GstStructure *) structure)->name = quark;
144 GST_STRUCTURE_REFCOUNT (structure) = NULL;
145 GST_STRUCTURE_FIELDS (structure) =
146 g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
148 GST_TRACE ("created structure %p", structure);
150 return GST_STRUCTURE_CAST (structure);
154 * gst_structure_new_id_empty:
155 * @quark: name of new structure
157 * Creates a new, empty #GstStructure with the given name as a GQuark.
159 * Free-function: gst_structure_free
161 * Returns: (transfer full): a new, empty #GstStructure
164 gst_structure_new_id_empty (GQuark quark)
166 g_return_val_if_fail (quark != 0, NULL);
168 return gst_structure_new_id_empty_with_size (quark, 0);
171 #ifndef G_DISABLE_CHECKS
173 gst_structure_validate_name (const gchar * name)
177 g_return_val_if_fail (name != NULL, FALSE);
179 if (G_UNLIKELY (!g_ascii_isalpha (*name))) {
180 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
185 /* FIXME: test name string more */
187 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
189 if (G_UNLIKELY (*s != '\0')) {
190 GST_WARNING ("Invalid character '%c' at offset %" G_GUINTPTR_FORMAT " in"
191 " structure name: %s", *s, ((guintptr) s - (guintptr) name), name);
195 if (strncmp (name, "video/x-raw-", 12) == 0) {
196 g_warning ("0.10-style raw video caps are being created. Should be "
197 "video/x-raw,format=(string).. now.");
198 } else if (strncmp (name, "audio/x-raw-", 12) == 0) {
199 g_warning ("0.10-style raw audio caps are being created. Should be "
200 "audio/x-raw,format=(string).. now.");
208 * gst_structure_new_empty:
209 * @name: name of new structure
211 * Creates a new, empty #GstStructure with the given @name.
213 * See gst_structure_set_name() for constraints on the @name parameter.
215 * Free-function: gst_structure_free
217 * Returns: (transfer full): a new, empty #GstStructure
220 gst_structure_new_empty (const gchar * name)
222 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
224 return gst_structure_new_id_empty_with_size (g_quark_from_string (name), 0);
229 * @name: name of new structure
230 * @firstfield: name of first field to set
231 * @...: additional arguments
233 * Creates a new #GstStructure with the given name. Parses the
234 * list of variable arguments and sets fields to the values listed.
235 * Variable arguments should be passed as field name, field type,
236 * and value. Last variable argument should be NULL.
238 * Free-function: gst_structure_free
240 * Returns: (transfer full): a new #GstStructure
243 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
245 GstStructure *structure;
248 va_start (varargs, firstfield);
249 structure = gst_structure_new_valist (name, firstfield, varargs);
256 * gst_structure_new_valist:
257 * @name: name of new structure
258 * @firstfield: name of first field to set
259 * @varargs: variable argument list
261 * Creates a new #GstStructure with the given @name. Structure fields
262 * are set according to the varargs in a manner similar to
263 * gst_structure_new().
265 * See gst_structure_set_name() for constraints on the @name parameter.
267 * Free-function: gst_structure_free
269 * Returns: (transfer full): a new #GstStructure
272 gst_structure_new_valist (const gchar * name,
273 const gchar * firstfield, va_list varargs)
275 GstStructure *structure;
277 structure = gst_structure_new_empty (name);
280 gst_structure_set_valist (structure, firstfield, varargs);
286 * gst_structure_set_parent_refcount:
287 * @structure: a #GstStructure
288 * @refcount: (in): a pointer to the parent's refcount
290 * Sets the parent_refcount field of #GstStructure. This field is used to
291 * determine whether a structure is mutable or not. This function should only be
292 * called by code implementing parent objects of #GstStructure, as described in
293 * the MT Refcounting section of the design documents.
295 * Returns: %TRUE if the parent refcount could be set.
298 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
300 g_return_val_if_fail (structure != NULL, FALSE);
302 /* if we have a parent_refcount already, we can only clear
303 * if with a NULL refcount */
304 if (GST_STRUCTURE_REFCOUNT (structure)) {
305 if (refcount != NULL) {
306 g_return_val_if_fail (refcount == NULL, FALSE);
310 if (refcount == NULL) {
311 g_return_val_if_fail (refcount != NULL, FALSE);
316 GST_STRUCTURE_REFCOUNT (structure) = refcount;
322 * gst_structure_copy:
323 * @structure: a #GstStructure to duplicate
325 * Duplicates a #GstStructure and all its fields and values.
327 * Free-function: gst_structure_free
329 * Returns: (transfer none): a new #GstStructure.
332 gst_structure_copy (const GstStructure * structure)
334 GstStructure *new_structure;
335 GstStructureField *field;
338 g_return_val_if_fail (structure != NULL, NULL);
340 len = GST_STRUCTURE_FIELDS (structure)->len;
341 new_structure = gst_structure_new_id_empty_with_size (structure->name, len);
343 for (i = 0; i < len; i++) {
344 GstStructureField new_field = { 0 };
346 field = GST_STRUCTURE_FIELD (structure, i);
348 new_field.name = field->name;
349 gst_value_init_and_copy (&new_field.value, &field->value);
350 g_array_append_val (GST_STRUCTURE_FIELDS (new_structure), new_field);
352 GST_CAT_TRACE (GST_CAT_PERFORMANCE, "doing copy %p -> %p",
353 structure, new_structure);
355 return new_structure;
359 * gst_structure_free:
360 * @structure: (in) (transfer full): the #GstStructure to free
362 * Frees a #GstStructure and all its fields and values. The structure must not
363 * have a parent when this function is called.
366 gst_structure_free (GstStructure * structure)
368 GstStructureField *field;
371 g_return_if_fail (structure != NULL);
372 g_return_if_fail (GST_STRUCTURE_REFCOUNT (structure) == NULL);
374 len = GST_STRUCTURE_FIELDS (structure)->len;
375 for (i = 0; i < len; i++) {
376 field = GST_STRUCTURE_FIELD (structure, i);
378 if (G_IS_VALUE (&field->value)) {
379 g_value_unset (&field->value);
382 g_array_free (GST_STRUCTURE_FIELDS (structure), TRUE);
384 memset (structure, 0xff, sizeof (GstStructure));
386 GST_TRACE ("free structure %p", structure);
388 g_slice_free1 (sizeof (GstStructureImpl), structure);
392 * gst_structure_get_name:
393 * @structure: a #GstStructure
395 * Get the name of @structure as a string.
397 * Returns: the name of the structure.
400 gst_structure_get_name (const GstStructure * structure)
402 g_return_val_if_fail (structure != NULL, NULL);
404 return g_quark_to_string (structure->name);
408 * gst_structure_has_name:
409 * @structure: a #GstStructure
410 * @name: structure name to check for
412 * Checks if the structure has the given name
414 * Returns: TRUE if @name matches the name of the structure.
417 gst_structure_has_name (const GstStructure * structure, const gchar * name)
419 const gchar *structure_name;
421 g_return_val_if_fail (structure != NULL, FALSE);
422 g_return_val_if_fail (name != NULL, FALSE);
424 /* getting the string is cheap and comparing short strings is too
425 * should be faster than getting the quark for name and comparing the quarks
427 structure_name = g_quark_to_string (structure->name);
429 return (structure_name && strcmp (structure_name, name) == 0);
433 * gst_structure_get_name_id:
434 * @structure: a #GstStructure
436 * Get the name of @structure as a GQuark.
438 * Returns: the quark representing the name of the structure.
441 gst_structure_get_name_id (const GstStructure * structure)
443 g_return_val_if_fail (structure != NULL, 0);
445 return structure->name;
449 * gst_structure_set_name:
450 * @structure: a #GstStructure
451 * @name: the new name of the structure
453 * Sets the name of the structure to the given @name. The string
454 * provided is copied before being used. It must not be empty, start with a
455 * letter and can be followed by letters, numbers and any of "/-_.:".
458 gst_structure_set_name (GstStructure * structure, const gchar * name)
460 g_return_if_fail (structure != NULL);
461 g_return_if_fail (IS_MUTABLE (structure));
462 g_return_if_fail (gst_structure_validate_name (name));
464 structure->name = g_quark_from_string (name);
468 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
469 const GValue * value)
471 GstStructureField gsfield = { 0, {0,} };
473 gsfield.name = field;
474 gst_value_init_and_copy (&gsfield.value, value);
476 gst_structure_set_field (structure, &gsfield);
480 * gst_structure_id_set_value:
481 * @structure: a #GstStructure
482 * @field: a #GQuark representing a field
483 * @value: the new value of the field
485 * Sets the field with the given GQuark @field to @value. If the field
486 * does not exist, it is created. If the field exists, the previous
487 * value is replaced and freed.
490 gst_structure_id_set_value (GstStructure * structure,
491 GQuark field, const GValue * value)
494 g_return_if_fail (structure != NULL);
495 g_return_if_fail (G_IS_VALUE (value));
496 g_return_if_fail (IS_MUTABLE (structure));
498 gst_structure_id_set_value_internal (structure, field, value);
502 * gst_structure_set_value:
503 * @structure: a #GstStructure
504 * @fieldname: the name of the field to set
505 * @value: the new value of the field
507 * Sets the field with the given name @field to @value. If the field
508 * does not exist, it is created. If the field exists, the previous
509 * value is replaced and freed.
512 gst_structure_set_value (GstStructure * structure,
513 const gchar * fieldname, const GValue * value)
515 g_return_if_fail (structure != NULL);
516 g_return_if_fail (fieldname != NULL);
517 g_return_if_fail (G_IS_VALUE (value));
518 g_return_if_fail (IS_MUTABLE (structure));
520 gst_structure_id_set_value_internal (structure,
521 g_quark_from_string (fieldname), value);
525 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
528 GstStructureField gsfield = { 0, {0,} };
530 gsfield.name = field;
531 gsfield.value = *value;
533 gst_structure_set_field (structure, &gsfield);
535 /* we took ownership */
537 memset (value, 0, sizeof (GValue));
539 value->g_type = G_TYPE_INVALID;
544 * gst_structure_id_take_value:
545 * @structure: a #GstStructure
546 * @field: a #GQuark representing a field
547 * @value: (transfer full): the new value of the field
549 * Sets the field with the given GQuark @field to @value. If the field
550 * does not exist, it is created. If the field exists, the previous
551 * value is replaced and freed.
554 gst_structure_id_take_value (GstStructure * structure, GQuark field,
557 g_return_if_fail (structure != NULL);
558 g_return_if_fail (G_IS_VALUE (value));
559 g_return_if_fail (IS_MUTABLE (structure));
561 gst_structure_id_take_value_internal (structure, field, value);
565 * gst_structure_take_value:
566 * @structure: a #GstStructure
567 * @fieldname: the name of the field to set
568 * @value: (transfer full): the new value of the field
570 * Sets the field with the given name @field to @value. If the field
571 * does not exist, it is created. If the field exists, the previous
572 * value is replaced and freed. The function will take ownership of @value.
575 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
578 g_return_if_fail (structure != NULL);
579 g_return_if_fail (fieldname != NULL);
580 g_return_if_fail (G_IS_VALUE (value));
581 g_return_if_fail (IS_MUTABLE (structure));
583 gst_structure_id_take_value_internal (structure,
584 g_quark_from_string (fieldname), value);
588 gst_structure_set_valist_internal (GstStructure * structure,
589 const gchar * fieldname, va_list varargs)
595 GstStructureField field = { 0 };
597 field.name = g_quark_from_string (fieldname);
599 type = va_arg (varargs, GType);
601 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
602 if (G_UNLIKELY (err)) {
603 g_critical ("%s", err);
606 gst_structure_set_field (structure, &field);
608 fieldname = va_arg (varargs, gchar *);
614 * @structure: a #GstStructure
615 * @fieldname: the name of the field to set
616 * @...: variable arguments
618 * Parses the variable arguments and sets fields accordingly.
619 * Variable arguments should be in the form field name, field type
620 * (as a GType), value(s). The last variable argument should be NULL.
623 gst_structure_set (GstStructure * structure, const gchar * field, ...)
627 g_return_if_fail (structure != NULL);
628 g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
630 va_start (varargs, field);
631 gst_structure_set_valist_internal (structure, field, varargs);
636 * gst_structure_set_valist:
637 * @structure: a #GstStructure
638 * @fieldname: the name of the field to set
639 * @varargs: variable arguments
641 * va_list form of gst_structure_set().
644 gst_structure_set_valist (GstStructure * structure,
645 const gchar * fieldname, va_list varargs)
647 g_return_if_fail (structure != NULL);
648 g_return_if_fail (IS_MUTABLE (structure));
650 gst_structure_set_valist_internal (structure, fieldname, varargs);
654 gst_structure_id_set_valist_internal (GstStructure * structure,
655 GQuark fieldname, va_list varargs)
661 GstStructureField field = { 0 };
663 field.name = fieldname;
665 type = va_arg (varargs, GType);
667 #ifndef G_VALUE_COLLECT_INIT
668 g_value_init (&field.value, type);
669 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
671 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
673 if (G_UNLIKELY (err)) {
674 g_critical ("%s", err);
677 gst_structure_set_field (structure, &field);
679 fieldname = va_arg (varargs, GQuark);
684 * gst_structure_id_set:
685 * @structure: a #GstStructure
686 * @fieldname: the GQuark for the name of the field to set
687 * @...: variable arguments
689 * Identical to gst_structure_set, except that field names are
690 * passed using the GQuark for the field name. This allows more efficient
691 * setting of the structure if the caller already knows the associated
693 * The last variable argument must be NULL.
696 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
700 g_return_if_fail (structure != NULL);
702 va_start (varargs, field);
703 gst_structure_id_set_valist_internal (structure, field, varargs);
708 * gst_structure_id_set_valist:
709 * @structure: a #GstStructure
710 * @fieldname: the name of the field to set
711 * @varargs: variable arguments
713 * va_list form of gst_structure_id_set().
716 gst_structure_id_set_valist (GstStructure * structure,
717 GQuark fieldname, va_list varargs)
719 g_return_if_fail (structure != NULL);
720 g_return_if_fail (IS_MUTABLE (structure));
722 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
726 * gst_structure_new_id:
727 * @name_quark: name of new structure
728 * @field_quark: the GQuark for the name of the field to set
729 * @...: variable arguments
731 * Creates a new #GstStructure with the given name as a GQuark, followed by
732 * fieldname quark, GType, argument(s) "triplets" in the same format as
733 * gst_structure_id_set(). Basically a convenience wrapper around
734 * gst_structure_new_id_empty() and gst_structure_id_set().
736 * The last variable argument must be NULL (or 0).
738 * Free-function: gst_structure_free
740 * Returns: (transfer full): a new #GstStructure
743 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
748 g_return_val_if_fail (name_quark != 0, NULL);
749 g_return_val_if_fail (field_quark != 0, NULL);
751 s = gst_structure_new_id_empty (name_quark);
753 va_start (varargs, field_quark);
754 gst_structure_id_set_valist_internal (s, field_quark, varargs);
760 #if GST_VERSION_NANO == 1
761 #define GIT_G_WARNING g_warning
763 #define GIT_G_WARNING GST_WARNING
766 /* If the structure currently contains a field with the same name, it is
767 * replaced with the provided field. Otherwise, the field is added to the
768 * structure. The field's value is not deeply copied.
771 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
773 GstStructureField *f;
774 guint i, len = GST_STRUCTURE_FIELDS (structure)->len;
776 if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
779 s = g_value_get_string (&field->value);
780 /* only check for NULL strings in taglists, as they are allowed in message
781 * structs, e.g. error message debug strings */
782 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
784 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
785 "Please file a bug.", g_quark_to_string (field->name));
786 g_value_unset (&field->value);
789 /* empty strings never make sense */
790 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
791 "Please file a bug.", g_quark_to_string (field->name));
792 g_value_unset (&field->value);
795 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
796 g_warning ("Trying to set string on %s field '%s', but string is not "
797 "valid UTF-8. Please file a bug.",
798 IS_TAGLIST (structure) ? "taglist" : "structure",
799 g_quark_to_string (field->name));
800 g_value_unset (&field->value);
803 } else if (G_UNLIKELY (G_VALUE_HOLDS (&field->value, G_TYPE_DATE))) {
806 d = g_value_get_boxed (&field->value);
807 /* only check for NULL GDates in taglists, as they might make sense
808 * in other, generic structs */
809 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
810 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
811 "Please file a bug.", g_quark_to_string (field->name));
812 g_value_unset (&field->value);
814 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
816 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
817 IS_TAGLIST (structure) ? "taglist" : "structure",
818 g_quark_to_string (field->name));
819 g_value_unset (&field->value);
824 for (i = 0; i < len; i++) {
825 f = GST_STRUCTURE_FIELD (structure, i);
827 if (G_UNLIKELY (f->name == field->name)) {
828 g_value_unset (&f->value);
829 memcpy (f, field, sizeof (GstStructureField));
834 g_array_append_val (GST_STRUCTURE_FIELDS (structure), *field);
837 /* If there is no field with the given ID, NULL is returned.
839 static GstStructureField *
840 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
842 GstStructureField *field;
845 len = GST_STRUCTURE_FIELDS (structure)->len;
847 for (i = 0; i < len; i++) {
848 field = GST_STRUCTURE_FIELD (structure, i);
850 if (G_UNLIKELY (field->name == field_id))
857 /* If there is no field with the given ID, NULL is returned.
859 static GstStructureField *
860 gst_structure_get_field (const GstStructure * structure,
861 const gchar * fieldname)
863 g_return_val_if_fail (structure != NULL, NULL);
864 g_return_val_if_fail (fieldname != NULL, NULL);
866 return gst_structure_id_get_field (structure,
867 g_quark_from_string (fieldname));
871 * gst_structure_get_value:
872 * @structure: a #GstStructure
873 * @fieldname: the name of the field to get
875 * Get the value of the field with name @fieldname.
877 * Returns: the #GValue corresponding to the field with the given name.
880 gst_structure_get_value (const GstStructure * structure,
881 const gchar * fieldname)
883 GstStructureField *field;
885 g_return_val_if_fail (structure != NULL, NULL);
886 g_return_val_if_fail (fieldname != NULL, NULL);
888 field = gst_structure_get_field (structure, fieldname);
892 return &field->value;
896 * gst_structure_id_get_value:
897 * @structure: a #GstStructure
898 * @field: the #GQuark of the field to get
900 * Get the value of the field with GQuark @field.
902 * Returns: the #GValue corresponding to the field with the given name
906 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
908 GstStructureField *gsfield;
910 g_return_val_if_fail (structure != NULL, NULL);
912 gsfield = gst_structure_id_get_field (structure, field);
916 return &gsfield->value;
920 * gst_structure_remove_field:
921 * @structure: a #GstStructure
922 * @fieldname: the name of the field to remove
924 * Removes the field with the given name. If the field with the given
925 * name does not exist, the structure is unchanged.
928 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
930 GstStructureField *field;
934 g_return_if_fail (structure != NULL);
935 g_return_if_fail (fieldname != NULL);
936 g_return_if_fail (IS_MUTABLE (structure));
938 id = g_quark_from_string (fieldname);
939 len = GST_STRUCTURE_FIELDS (structure)->len;
941 for (i = 0; i < len; i++) {
942 field = GST_STRUCTURE_FIELD (structure, i);
944 if (field->name == id) {
945 if (G_IS_VALUE (&field->value)) {
946 g_value_unset (&field->value);
948 GST_STRUCTURE_FIELDS (structure) =
949 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
956 * gst_structure_remove_fields:
957 * @structure: a #GstStructure
958 * @fieldname: the name of the field to remove
959 * @...: NULL-terminated list of more fieldnames to remove
961 * Removes the fields with the given names. If a field does not exist, the
962 * argument is ignored.
965 gst_structure_remove_fields (GstStructure * structure,
966 const gchar * fieldname, ...)
970 g_return_if_fail (structure != NULL);
971 g_return_if_fail (fieldname != NULL);
972 /* mutability checked in remove_field */
974 va_start (varargs, fieldname);
975 gst_structure_remove_fields_valist (structure, fieldname, varargs);
980 * gst_structure_remove_fields_valist:
981 * @structure: a #GstStructure
982 * @fieldname: the name of the field to remove
983 * @varargs: NULL-terminated list of more fieldnames to remove
985 * va_list form of gst_structure_remove_fields().
988 gst_structure_remove_fields_valist (GstStructure * structure,
989 const gchar * fieldname, va_list varargs)
991 gchar *field = (gchar *) fieldname;
993 g_return_if_fail (structure != NULL);
994 g_return_if_fail (fieldname != NULL);
995 /* mutability checked in remove_field */
998 gst_structure_remove_field (structure, field);
999 field = va_arg (varargs, char *);
1004 * gst_structure_remove_all_fields:
1005 * @structure: a #GstStructure
1007 * Removes all fields in a GstStructure.
1010 gst_structure_remove_all_fields (GstStructure * structure)
1012 GstStructureField *field;
1015 g_return_if_fail (structure != NULL);
1016 g_return_if_fail (IS_MUTABLE (structure));
1018 for (i = GST_STRUCTURE_FIELDS (structure)->len - 1; i >= 0; i--) {
1019 field = GST_STRUCTURE_FIELD (structure, i);
1021 if (G_IS_VALUE (&field->value)) {
1022 g_value_unset (&field->value);
1024 GST_STRUCTURE_FIELDS (structure) =
1025 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1030 * gst_structure_get_field_type:
1031 * @structure: a #GstStructure
1032 * @fieldname: the name of the field
1034 * Finds the field with the given name, and returns the type of the
1035 * value it contains. If the field is not found, G_TYPE_INVALID is
1038 * Returns: the #GValue of the field
1041 gst_structure_get_field_type (const GstStructure * structure,
1042 const gchar * fieldname)
1044 GstStructureField *field;
1046 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1047 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1049 field = gst_structure_get_field (structure, fieldname);
1051 return G_TYPE_INVALID;
1053 return G_VALUE_TYPE (&field->value);
1057 * gst_structure_n_fields:
1058 * @structure: a #GstStructure
1060 * Get the number of fields in the structure.
1062 * Returns: the number of fields in the structure
1065 gst_structure_n_fields (const GstStructure * structure)
1067 g_return_val_if_fail (structure != NULL, 0);
1069 return GST_STRUCTURE_FIELDS (structure)->len;
1073 * gst_structure_nth_field_name:
1074 * @structure: a #GstStructure
1075 * @index: the index to get the name of
1077 * Get the name of the given field number, counting from 0 onwards.
1079 * Returns: the name of the given field number
1082 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1084 GstStructureField *field;
1086 g_return_val_if_fail (structure != NULL, NULL);
1087 g_return_val_if_fail (index < GST_STRUCTURE_FIELDS (structure)->len, NULL);
1089 field = GST_STRUCTURE_FIELD (structure, index);
1091 return g_quark_to_string (field->name);
1095 * gst_structure_foreach:
1096 * @structure: a #GstStructure
1097 * @func: (scope call): a function to call for each field
1098 * @user_data: (closure): private data
1100 * Calls the provided function once for each field in the #GstStructure. The
1101 * function must not modify the fields. Also see gst_structure_map_in_place().
1103 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1107 gst_structure_foreach (const GstStructure * structure,
1108 GstStructureForeachFunc func, gpointer user_data)
1111 GstStructureField *field;
1114 g_return_val_if_fail (structure != NULL, FALSE);
1115 g_return_val_if_fail (func != NULL, FALSE);
1117 len = GST_STRUCTURE_FIELDS (structure)->len;
1119 for (i = 0; i < len; i++) {
1120 field = GST_STRUCTURE_FIELD (structure, i);
1122 ret = func (field->name, &field->value, user_data);
1123 if (G_UNLIKELY (!ret))
1131 * gst_structure_map_in_place:
1132 * @structure: a #GstStructure
1133 * @func: (scope call): a function to call for each field
1134 * @user_data: (closure): private data
1136 * Calls the provided function once for each field in the #GstStructure. In
1137 * contrast to gst_structure_foreach(), the function may modify but not delete the
1138 * fields. The structure must be mutable.
1140 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1144 gst_structure_map_in_place (GstStructure * structure,
1145 GstStructureMapFunc func, gpointer user_data)
1148 GstStructureField *field;
1151 g_return_val_if_fail (structure != NULL, FALSE);
1152 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1153 g_return_val_if_fail (func != NULL, FALSE);
1154 len = GST_STRUCTURE_FIELDS (structure)->len;
1156 for (i = 0; i < len; i++) {
1157 field = GST_STRUCTURE_FIELD (structure, i);
1159 ret = func (field->name, &field->value, user_data);
1168 * gst_structure_id_has_field:
1169 * @structure: a #GstStructure
1170 * @field: #GQuark of the field name
1172 * Check if @structure contains a field named @field.
1174 * Returns: TRUE if the structure contains a field with the given name
1177 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1179 GstStructureField *f;
1181 g_return_val_if_fail (structure != NULL, FALSE);
1182 g_return_val_if_fail (field != 0, FALSE);
1184 f = gst_structure_id_get_field (structure, field);
1190 * gst_structure_has_field:
1191 * @structure: a #GstStructure
1192 * @fieldname: the name of a field
1194 * Check if @structure contains a field named @fieldname.
1196 * Returns: TRUE if the structure contains a field with the given name
1199 gst_structure_has_field (const GstStructure * structure,
1200 const gchar * fieldname)
1202 g_return_val_if_fail (structure != NULL, FALSE);
1203 g_return_val_if_fail (fieldname != NULL, FALSE);
1205 return gst_structure_id_has_field (structure,
1206 g_quark_from_string (fieldname));
1210 * gst_structure_id_has_field_typed:
1211 * @structure: a #GstStructure
1212 * @field: #GQuark of the field name
1213 * @type: the type of a value
1215 * Check if @structure contains a field named @field and with GType @type.
1217 * Returns: TRUE if the structure contains a field with the given name and type
1220 gst_structure_id_has_field_typed (const GstStructure * structure,
1221 GQuark field, GType type)
1223 GstStructureField *f;
1225 g_return_val_if_fail (structure != NULL, FALSE);
1226 g_return_val_if_fail (field != 0, FALSE);
1228 f = gst_structure_id_get_field (structure, field);
1232 return (G_VALUE_TYPE (&f->value) == type);
1236 * gst_structure_has_field_typed:
1237 * @structure: a #GstStructure
1238 * @fieldname: the name of a field
1239 * @type: the type of a value
1241 * Check if @structure contains a field named @fieldname and with GType @type.
1243 * Returns: TRUE if the structure contains a field with the given name and type
1246 gst_structure_has_field_typed (const GstStructure * structure,
1247 const gchar * fieldname, GType type)
1249 g_return_val_if_fail (structure != NULL, FALSE);
1250 g_return_val_if_fail (fieldname != NULL, FALSE);
1252 return gst_structure_id_has_field_typed (structure,
1253 g_quark_from_string (fieldname), type);
1256 /* utility functions */
1259 * gst_structure_get_boolean:
1260 * @structure: a #GstStructure
1261 * @fieldname: the name of a field
1262 * @value: (out): a pointer to a #gboolean to set
1264 * Sets the boolean pointed to by @value corresponding to the value of the
1265 * given field. Caller is responsible for making sure the field exists
1266 * and has the correct type.
1268 * Returns: TRUE if the value could be set correctly. If there was no field
1269 * with @fieldname or the existing field did not contain a boolean, this
1270 * function returns FALSE.
1273 gst_structure_get_boolean (const GstStructure * structure,
1274 const gchar * fieldname, gboolean * value)
1276 GstStructureField *field;
1278 g_return_val_if_fail (structure != NULL, FALSE);
1279 g_return_val_if_fail (fieldname != NULL, FALSE);
1281 field = gst_structure_get_field (structure, fieldname);
1285 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1288 *value = gst_g_value_get_boolean_unchecked (&field->value);
1294 * gst_structure_get_int:
1295 * @structure: a #GstStructure
1296 * @fieldname: the name of a field
1297 * @value: (out): a pointer to an int to set
1299 * Sets the int pointed to by @value corresponding to the value of the
1300 * given field. Caller is responsible for making sure the field exists
1301 * and has the correct type.
1303 * Returns: %TRUE if the value could be set correctly. If there was no field
1304 * with @fieldname or the existing field did not contain an int, this function
1308 gst_structure_get_int (const GstStructure * structure,
1309 const gchar * fieldname, gint * value)
1311 GstStructureField *field;
1313 g_return_val_if_fail (structure != NULL, FALSE);
1314 g_return_val_if_fail (fieldname != NULL, FALSE);
1315 g_return_val_if_fail (value != NULL, FALSE);
1317 field = gst_structure_get_field (structure, fieldname);
1321 if (!G_VALUE_HOLDS_INT (&field->value))
1324 *value = gst_g_value_get_int_unchecked (&field->value);
1330 * gst_structure_get_uint:
1331 * @structure: a #GstStructure
1332 * @fieldname: the name of a field
1333 * @value: (out): a pointer to a uint to set
1335 * Sets the uint pointed to by @value corresponding to the value of the
1336 * given field. Caller is responsible for making sure the field exists
1337 * and has the correct type.
1339 * Returns: %TRUE if the value could be set correctly. If there was no field
1340 * with @fieldname or the existing field did not contain a uint, this function
1344 gst_structure_get_uint (const GstStructure * structure,
1345 const gchar * fieldname, guint * value)
1347 GstStructureField *field;
1349 g_return_val_if_fail (structure != NULL, FALSE);
1350 g_return_val_if_fail (fieldname != NULL, FALSE);
1351 g_return_val_if_fail (value != NULL, FALSE);
1353 field = gst_structure_get_field (structure, fieldname);
1357 if (!G_VALUE_HOLDS_UINT (&field->value))
1360 *value = gst_g_value_get_uint_unchecked (&field->value);
1366 * gst_structure_get_date:
1367 * @structure: a #GstStructure
1368 * @fieldname: the name of a field
1369 * @value: (out callee-allocates): a pointer to a #GDate to set
1371 * Sets the date pointed to by @value corresponding to the date of the
1372 * given field. Caller is responsible for making sure the field exists
1373 * and has the correct type.
1375 * On success @value will point to a newly-allocated copy of the date which
1376 * should be freed with g_date_free() when no longer needed (note: this is
1377 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1378 * copy of the string).
1380 * Returns: TRUE if the value could be set correctly. If there was no field
1381 * with @fieldname or the existing field did not contain a data, this function
1385 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1388 GstStructureField *field;
1390 g_return_val_if_fail (structure != NULL, FALSE);
1391 g_return_val_if_fail (fieldname != NULL, FALSE);
1392 g_return_val_if_fail (value != NULL, FALSE);
1394 field = gst_structure_get_field (structure, fieldname);
1398 if (!G_VALUE_HOLDS (&field->value, G_TYPE_DATE))
1401 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1402 *value = g_value_dup_boxed (&field->value);
1408 * gst_structure_get_date_time:
1409 * @structure: a #GstStructure
1410 * @fieldname: the name of a field
1411 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1413 * Sets the datetime pointed to by @value corresponding to the datetime of the
1414 * given field. Caller is responsible for making sure the field exists
1415 * and has the correct type.
1417 * On success @value will point to a reference of the datetime which
1418 * should be unreffed with gst_date_time_unref() when no longer needed
1419 * (note: this is inconsistent with e.g. gst_structure_get_string()
1420 * which doesn't return a copy of the string).
1422 * Returns: TRUE if the value could be set correctly. If there was no field
1423 * with @fieldname or the existing field did not contain a data, this function
1427 gst_structure_get_date_time (const GstStructure * structure,
1428 const gchar * fieldname, GstDateTime ** value)
1430 GstStructureField *field;
1432 g_return_val_if_fail (structure != NULL, FALSE);
1433 g_return_val_if_fail (fieldname != NULL, FALSE);
1434 g_return_val_if_fail (value != NULL, FALSE);
1436 field = gst_structure_get_field (structure, fieldname);
1440 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1443 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1444 *value = g_value_dup_boxed (&field->value);
1450 * gst_structure_get_clock_time:
1451 * @structure: a #GstStructure
1452 * @fieldname: the name of a field
1453 * @value: (out): a pointer to a #GstClockTime to set
1455 * Sets the clock time pointed to by @value corresponding to the clock time
1456 * of the given field. Caller is responsible for making sure the field exists
1457 * and has the correct type.
1459 * Returns: TRUE if the value could be set correctly. If there was no field
1460 * with @fieldname or the existing field did not contain a #GstClockTime, this
1461 * function returns FALSE.
1464 gst_structure_get_clock_time (const GstStructure * structure,
1465 const gchar * fieldname, GstClockTime * value)
1467 GstStructureField *field;
1469 g_return_val_if_fail (structure != NULL, FALSE);
1470 g_return_val_if_fail (fieldname != NULL, FALSE);
1471 g_return_val_if_fail (value != NULL, FALSE);
1473 field = gst_structure_get_field (structure, fieldname);
1477 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1480 *value = gst_g_value_get_uint64_unchecked (&field->value);
1486 * gst_structure_get_double:
1487 * @structure: a #GstStructure
1488 * @fieldname: the name of a field
1489 * @value: (out): a pointer to a gdouble to set
1491 * Sets the double pointed to by @value corresponding to the value of the
1492 * given field. Caller is responsible for making sure the field exists
1493 * and has the correct type.
1495 * Returns: TRUE if the value could be set correctly. If there was no field
1496 * with @fieldname or the existing field did not contain a double, this
1497 * function returns FALSE.
1500 gst_structure_get_double (const GstStructure * structure,
1501 const gchar * fieldname, gdouble * value)
1503 GstStructureField *field;
1505 g_return_val_if_fail (structure != NULL, FALSE);
1506 g_return_val_if_fail (fieldname != NULL, FALSE);
1507 g_return_val_if_fail (value != NULL, FALSE);
1509 field = gst_structure_get_field (structure, fieldname);
1513 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1516 *value = gst_g_value_get_double_unchecked (&field->value);
1522 * gst_structure_get_string:
1523 * @structure: a #GstStructure
1524 * @fieldname: the name of a field
1526 * Finds the field corresponding to @fieldname, and returns the string
1527 * contained in the field's value. Caller is responsible for making
1528 * sure the field exists and has the correct type.
1530 * The string should not be modified, and remains valid until the next
1531 * call to a gst_structure_*() function with the given structure.
1533 * Returns: a pointer to the string or NULL when the field did not exist
1534 * or did not contain a string.
1537 gst_structure_get_string (const GstStructure * structure,
1538 const gchar * fieldname)
1540 GstStructureField *field;
1542 g_return_val_if_fail (structure != NULL, NULL);
1543 g_return_val_if_fail (fieldname != NULL, NULL);
1545 field = gst_structure_get_field (structure, fieldname);
1549 if (!G_VALUE_HOLDS_STRING (&field->value))
1552 return gst_g_value_get_string_unchecked (&field->value);
1556 * gst_structure_get_enum:
1557 * @structure: a #GstStructure
1558 * @fieldname: the name of a field
1559 * @enumtype: the enum type of a field
1560 * @value: (out): a pointer to an int to set
1562 * Sets the int pointed to by @value corresponding to the value of the
1563 * given field. Caller is responsible for making sure the field exists,
1564 * has the correct type and that the enumtype is correct.
1566 * Returns: TRUE if the value could be set correctly. If there was no field
1567 * with @fieldname or the existing field did not contain an enum of the given
1568 * type, this function returns FALSE.
1571 gst_structure_get_enum (const GstStructure * structure,
1572 const gchar * fieldname, GType enumtype, gint * value)
1574 GstStructureField *field;
1576 g_return_val_if_fail (structure != NULL, FALSE);
1577 g_return_val_if_fail (fieldname != NULL, FALSE);
1578 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1579 g_return_val_if_fail (value != NULL, FALSE);
1581 field = gst_structure_get_field (structure, fieldname);
1585 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1588 *value = g_value_get_enum (&field->value);
1594 * gst_structure_get_fraction:
1595 * @structure: a #GstStructure
1596 * @fieldname: the name of a field
1597 * @value_numerator: (out): a pointer to an int to set
1598 * @value_denominator: (out): a pointer to an int to set
1600 * Sets the integers pointed to by @value_numerator and @value_denominator
1601 * corresponding to the value of the given field. Caller is responsible
1602 * for making sure the field exists and has the correct type.
1604 * Returns: TRUE if the values could be set correctly. If there was no field
1605 * with @fieldname or the existing field did not contain a GstFraction, this
1606 * function returns FALSE.
1609 gst_structure_get_fraction (const GstStructure * structure,
1610 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1612 GstStructureField *field;
1614 g_return_val_if_fail (structure != NULL, FALSE);
1615 g_return_val_if_fail (fieldname != NULL, FALSE);
1616 g_return_val_if_fail (value_numerator != NULL, FALSE);
1617 g_return_val_if_fail (value_denominator != NULL, FALSE);
1619 field = gst_structure_get_field (structure, fieldname);
1623 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1626 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1627 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1632 typedef struct _GstStructureAbbreviation
1634 const gchar *type_name;
1637 GstStructureAbbreviation;
1639 /* return a copy of an array of GstStructureAbbreviation containing all the
1640 * known type_string, GType maps, including abbreviations for common types */
1641 static GstStructureAbbreviation *
1642 gst_structure_get_abbrs (gint * n_abbrs)
1644 static GstStructureAbbreviation *abbrs = NULL;
1645 static volatile gsize num = 0;
1647 if (g_once_init_enter (&num)) {
1648 /* dynamically generate the array */
1650 GstStructureAbbreviation dyn_abbrs[] = {
1655 {"uint", G_TYPE_UINT}
1659 {"float", G_TYPE_FLOAT}
1663 {"double", G_TYPE_DOUBLE}
1665 {"d", G_TYPE_DOUBLE}
1667 {"buffer", GST_TYPE_BUFFER}
1669 {"fraction", GST_TYPE_FRACTION}
1671 {"boolean", G_TYPE_BOOLEAN}
1673 {"bool", G_TYPE_BOOLEAN}
1675 {"b", G_TYPE_BOOLEAN}
1677 {"string", G_TYPE_STRING}
1679 {"str", G_TYPE_STRING}
1681 {"s", G_TYPE_STRING}
1683 {"structure", GST_TYPE_STRUCTURE}
1685 {"date", G_TYPE_DATE}
1687 {"datetime", GST_TYPE_DATE_TIME}
1689 {"bitmask", GST_TYPE_BITMASK}
1691 {"sample", GST_TYPE_SAMPLE}
1693 {"taglist", GST_TYPE_TAG_LIST}
1695 _num = G_N_ELEMENTS (dyn_abbrs);
1696 /* permanently allocate and copy the array now */
1697 abbrs = g_new0 (GstStructureAbbreviation, _num);
1698 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1699 g_once_init_leave (&num, _num);
1706 /* given a type_name that could be a type abbreviation or a registered GType,
1707 * return a matching GType */
1709 gst_structure_gtype_from_abbr (const char *type_name)
1712 GstStructureAbbreviation *abbrs;
1715 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1717 abbrs = gst_structure_get_abbrs (&n_abbrs);
1719 for (i = 0; i < n_abbrs; i++) {
1720 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1721 return abbrs[i].type;
1725 /* this is the fallback */
1726 return g_type_from_name (type_name);
1730 gst_structure_to_abbr (GType type)
1733 GstStructureAbbreviation *abbrs;
1736 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1738 abbrs = gst_structure_get_abbrs (&n_abbrs);
1740 for (i = 0; i < n_abbrs; i++) {
1741 if (type == abbrs[i].type) {
1742 return abbrs[i].type_name;
1746 return g_type_name (type);
1750 gst_structure_value_get_generic_type (GValue * val)
1752 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1753 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1754 GArray *array = g_value_peek_pointer (val);
1756 if (array->len > 0) {
1757 GValue *value = &g_array_index (array, GValue, 0);
1759 return gst_structure_value_get_generic_type (value);
1763 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1765 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1766 return G_TYPE_INT64;
1767 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1768 return G_TYPE_DOUBLE;
1769 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1770 return GST_TYPE_FRACTION;
1772 return G_VALUE_TYPE (val);
1776 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1779 GstStructureField *field;
1782 g_return_val_if_fail (s != NULL, FALSE);
1784 g_string_append (s, g_quark_to_string (structure->name));
1785 len = GST_STRUCTURE_FIELDS (structure)->len;
1786 for (i = 0; i < len; i++) {
1790 field = GST_STRUCTURE_FIELD (structure, i);
1792 t = gst_value_serialize (&field->value);
1793 type = gst_structure_value_get_generic_type (&field->value);
1795 g_string_append_len (s, ", ", 2);
1796 /* FIXME: do we need to escape fieldnames? */
1797 g_string_append (s, g_quark_to_string (field->name));
1798 g_string_append_len (s, "=(", 2);
1799 g_string_append (s, gst_structure_to_abbr (type));
1800 g_string_append_c (s, ')');
1801 g_string_append (s, t == NULL ? "NULL" : t);
1805 g_string_append_c (s, ';');
1810 * gst_structure_to_string:
1811 * @structure: a #GstStructure
1813 * Converts @structure to a human-readable string representation.
1815 * For debugging purposes its easier to do something like this:
1817 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1819 * This prints the structure in human readble form.
1821 * Free-function: g_free
1823 * Returns: (transfer full): a pointer to string allocated by g_malloc().
1824 * g_free() after usage.
1827 gst_structure_to_string (const GstStructure * structure)
1831 /* NOTE: This function is potentially called by the debug system,
1832 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1833 * should be careful to avoid recursion. This includes any functions
1834 * called by gst_structure_to_string. In particular, calls should
1835 * not use the GST_PTR_FORMAT extension. */
1837 g_return_val_if_fail (structure != NULL, NULL);
1839 /* we estimate a minimum size based on the number of fields in order to
1840 * avoid unnecessary reallocs within GString */
1841 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1842 priv_gst_structure_append_to_gstring (structure, s);
1843 return g_string_free (s, FALSE);
1847 * r will still point to the string. if end == next, the string will not be
1848 * null-terminated. In all other cases it will be.
1849 * end = pointer to char behind end of string, next = pointer to start of
1851 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1854 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1865 ret = gst_structure_parse_simple_string (s, end);
1875 if (G_UNLIKELY (*s == 0))
1877 if (G_UNLIKELY (*s == '\\'))
1885 /* Find the closing quotes */
1888 if (G_UNLIKELY (*s == 0))
1890 if (G_UNLIKELY (*s == '\\'))
1905 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1908 GValue value1 = { 0 };
1909 GValue value2 = { 0 };
1910 GValue value3 = { 0 };
1912 gboolean ret, have_step = FALSE;
1918 ret = gst_structure_parse_value (s, &s, &value1, type);
1922 while (g_ascii_isspace (*s))
1929 while (g_ascii_isspace (*s))
1932 ret = gst_structure_parse_value (s, &s, &value2, type);
1936 while (g_ascii_isspace (*s))
1939 /* optional step for int and int64 */
1940 if (G_VALUE_TYPE (&value1) == G_TYPE_INT
1941 || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1945 while (g_ascii_isspace (*s))
1948 ret = gst_structure_parse_value (s, &s, &value3, type);
1952 while (g_ascii_isspace (*s))
1963 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1965 if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
1968 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1969 range_type = GST_TYPE_DOUBLE_RANGE;
1970 g_value_init (value, range_type);
1971 gst_value_set_double_range (value,
1972 gst_g_value_get_double_unchecked (&value1),
1973 gst_g_value_get_double_unchecked (&value2));
1974 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1975 range_type = GST_TYPE_INT_RANGE;
1976 g_value_init (value, range_type);
1978 gst_value_set_int_range_step (value,
1979 gst_g_value_get_int_unchecked (&value1),
1980 gst_g_value_get_int_unchecked (&value2),
1981 gst_g_value_get_int_unchecked (&value3));
1983 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
1984 gst_g_value_get_int_unchecked (&value2));
1985 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1986 range_type = GST_TYPE_INT64_RANGE;
1987 g_value_init (value, range_type);
1989 gst_value_set_int64_range_step (value,
1990 gst_g_value_get_int64_unchecked (&value1),
1991 gst_g_value_get_int64_unchecked (&value2),
1992 gst_g_value_get_int64_unchecked (&value3));
1994 gst_value_set_int64_range (value,
1995 gst_g_value_get_int64_unchecked (&value1),
1996 gst_g_value_get_int64_unchecked (&value2));
1997 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1998 range_type = GST_TYPE_FRACTION_RANGE;
1999 g_value_init (value, range_type);
2000 gst_value_set_fraction_range (value, &value1, &value2);
2010 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
2011 GType type, GType list_type, char begin, char end)
2013 GValue list_value = { 0 };
2017 g_value_init (value, list_type);
2018 array = g_value_peek_pointer (value);
2024 while (g_ascii_isspace (*s))
2032 ret = gst_structure_parse_value (s, &s, &list_value, type);
2036 g_array_append_val (array, list_value);
2038 while (g_ascii_isspace (*s))
2046 while (g_ascii_isspace (*s))
2049 memset (&list_value, 0, sizeof (list_value));
2050 ret = gst_structure_parse_value (s, &s, &list_value, type);
2054 g_array_append_val (array, list_value);
2055 while (g_ascii_isspace (*s))
2066 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2068 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2073 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2076 return gst_structure_parse_any_list (s, after, value, type,
2077 GST_TYPE_ARRAY, '<', '>');
2081 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2085 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2095 gst_structure_parse_field (gchar * str,
2096 gchar ** after, GstStructureField * field)
2105 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2108 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2109 GST_WARNING ("failed to parse simple string, str=%s", str);
2114 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2117 if (G_UNLIKELY (*s != '=')) {
2118 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2125 field->name = g_quark_from_string (name);
2126 GST_DEBUG ("trying field name '%s'", name);
2129 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2131 GST_WARNING ("failed to parse value %s", str);
2140 gst_structure_parse_value (gchar * str,
2141 gchar ** after, GValue * value, GType default_type)
2150 GType type = default_type;
2153 while (g_ascii_isspace (*s))
2156 /* check if there's a (type_name) 'cast' */
2160 while (g_ascii_isspace (*s))
2163 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2166 while (g_ascii_isspace (*s))
2168 if (G_UNLIKELY (*s != ')'))
2171 while (g_ascii_isspace (*s))
2176 type = gst_structure_gtype_from_abbr (type_name);
2177 GST_DEBUG ("trying type name '%s'", type_name);
2180 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2181 GST_WARNING ("invalid type");
2186 while (g_ascii_isspace (*s))
2189 ret = gst_structure_parse_range (s, &s, value, type);
2190 } else if (*s == '{') {
2191 ret = gst_structure_parse_list (s, &s, value, type);
2192 } else if (*s == '<') {
2193 ret = gst_structure_parse_array (s, &s, value, type);
2197 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2199 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2204 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2206 /* Set NULL terminator for deserialization */
2210 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2211 g_value_init (value, try_types[i]);
2212 ret = gst_value_deserialize (value, value_s);
2215 g_value_unset (value);
2218 g_value_init (value, type);
2220 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2221 (type != G_TYPE_STRING))))
2223 /* Set NULL terminator for deserialization */
2227 ret = gst_value_deserialize (value, value_s);
2228 if (G_UNLIKELY (!ret))
2229 g_value_unset (value);
2240 * gst_structure_from_string:
2241 * @string: a string representation of a #GstStructure.
2242 * @end: (out) (allow-none) (transfer none): pointer to store the end of the string in.
2244 * Creates a #GstStructure from a string representation.
2245 * If end is not NULL, a pointer to the place inside the given string
2246 * where parsing ended will be returned.
2248 * Free-function: gst_structure_free
2250 * Returns: (transfer full): a new #GstStructure or NULL when the string could
2251 * not be parsed. Free with gst_structure_free() after use.
2254 gst_structure_from_string (const gchar * string, gchar ** end)
2261 GstStructure *structure = NULL;
2262 GstStructureField field;
2264 g_return_val_if_fail (string != NULL, NULL);
2266 copy = g_strdup (string);
2269 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2270 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2271 && g_ascii_isspace (r[1]))))
2275 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2276 GST_WARNING ("Failed to parse structure string '%s'", string);
2282 structure = gst_structure_new_empty (name);
2285 if (G_UNLIKELY (structure == NULL))
2289 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2290 && g_ascii_isspace (r[1]))))
2293 /* end of structure, get the next char and finish */
2298 /* accept \0 as end delimiter */
2301 if (G_UNLIKELY (*r != ',')) {
2302 GST_WARNING ("Failed to find delimiter, r=%s", r);
2306 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2307 && g_ascii_isspace (r[1]))))
2310 memset (&field, 0, sizeof (field));
2311 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2312 GST_WARNING ("Failed to parse field, r=%s", r);
2315 gst_structure_set_field (structure, &field);
2319 *end = (char *) string + (r - copy);
2321 g_warning ("gst_structure_from_string did not consume whole string,"
2322 " but caller did not provide end pointer (\"%s\")", string);
2329 gst_structure_free (structure);
2335 gst_structure_transform_to_string (const GValue * src_value,
2336 GValue * dest_value)
2338 g_return_if_fail (src_value != NULL);
2339 g_return_if_fail (dest_value != NULL);
2341 dest_value->data[0].v_pointer =
2342 gst_structure_to_string (src_value->data[0].v_pointer);
2345 static GstStructure *
2346 gst_structure_copy_conditional (const GstStructure * structure)
2349 return gst_structure_copy (structure);
2353 /* fixate utility functions */
2356 * gst_structure_fixate_field_nearest_int:
2357 * @structure: a #GstStructure
2358 * @field_name: a field in @structure
2359 * @target: the target value of the fixation
2361 * Fixates a #GstStructure by changing the given field to the nearest
2362 * integer to @target that is a subset of the existing field.
2364 * Returns: TRUE if the structure could be fixated
2367 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2368 const char *field_name, int target)
2370 const GValue *value;
2372 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2373 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2375 value = gst_structure_get_value (structure, field_name);
2377 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2380 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2383 x = gst_value_get_int_range_min (value);
2386 x = gst_value_get_int_range_max (value);
2389 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2391 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2392 const GValue *list_value;
2395 int best_index = -1;
2397 n = gst_value_list_get_size (value);
2398 for (i = 0; i < n; i++) {
2399 list_value = gst_value_list_get_value (value, i);
2400 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2401 int x = gst_g_value_get_int_unchecked (list_value);
2403 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2409 if (best_index != -1) {
2410 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2420 * gst_structure_fixate_field_nearest_double:
2421 * @structure: a #GstStructure
2422 * @field_name: a field in @structure
2423 * @target: the target value of the fixation
2425 * Fixates a #GstStructure by changing the given field to the nearest
2426 * double to @target that is a subset of the existing field.
2428 * Returns: TRUE if the structure could be fixated
2431 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2432 const char *field_name, double target)
2434 const GValue *value;
2436 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2437 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2439 value = gst_structure_get_value (structure, field_name);
2441 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2444 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2447 x = gst_value_get_double_range_min (value);
2450 x = gst_value_get_double_range_max (value);
2453 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2455 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2456 const GValue *list_value;
2459 int best_index = -1;
2461 n = gst_value_list_get_size (value);
2462 for (i = 0; i < n; i++) {
2463 list_value = gst_value_list_get_value (value, i);
2464 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2465 double x = gst_g_value_get_double_unchecked (list_value);
2467 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2473 if (best_index != -1) {
2474 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2485 * gst_structure_fixate_field_boolean:
2486 * @structure: a #GstStructure
2487 * @field_name: a field in @structure
2488 * @target: the target value of the fixation
2490 * Fixates a #GstStructure by changing the given @field_name field to the given
2491 * @target boolean if that field is not fixed yet.
2493 * Returns: TRUE if the structure could be fixated
2496 gst_structure_fixate_field_boolean (GstStructure * structure,
2497 const char *field_name, gboolean target)
2499 const GValue *value;
2501 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2502 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2504 value = gst_structure_get_value (structure, field_name);
2506 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2509 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2510 const GValue *list_value;
2513 int best_index = -1;
2515 n = gst_value_list_get_size (value);
2516 for (i = 0; i < n; i++) {
2517 list_value = gst_value_list_get_value (value, i);
2518 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2519 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2521 if (best_index == -1 || x == target) {
2527 if (best_index != -1) {
2528 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2538 * gst_structure_fixate_field_string:
2539 * @structure: a #GstStructure
2540 * @field_name: a field in @structure
2541 * @target: the target value of the fixation
2543 * Fixates a #GstStructure by changing the given @field_name field to the given
2544 * @target string if that field is not fixed yet.
2546 * Returns: TRUE if the structure could be fixated
2549 gst_structure_fixate_field_string (GstStructure * structure,
2550 const gchar * field_name, const gchar * target)
2552 const GValue *value;
2554 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2555 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2557 value = gst_structure_get_value (structure, field_name);
2559 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2562 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2563 const GValue *list_value;
2565 const gchar *best = NULL;
2566 int best_index = -1;
2568 n = gst_value_list_get_size (value);
2569 for (i = 0; i < n; i++) {
2570 list_value = gst_value_list_get_value (value, i);
2571 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2572 const gchar *x = g_value_get_string (list_value);
2574 if (best_index == -1 || g_str_equal (x, target)) {
2580 if (best_index != -1) {
2581 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2591 * gst_structure_fixate_field_nearest_fraction:
2592 * @structure: a #GstStructure
2593 * @field_name: a field in @structure
2594 * @target_numerator: The numerator of the target value of the fixation
2595 * @target_denominator: The denominator of the target value of the fixation
2597 * Fixates a #GstStructure by changing the given field to the nearest
2598 * fraction to @target_numerator/@target_denominator that is a subset
2599 * of the existing field.
2601 * Returns: TRUE if the structure could be fixated
2604 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2605 const char *field_name, const gint target_numerator,
2606 const gint target_denominator)
2608 const GValue *value;
2610 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2611 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2613 value = gst_structure_get_value (structure, field_name);
2615 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2618 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2619 const GValue *x, *new_value;
2620 GValue target = { 0 };
2621 g_value_init (&target, GST_TYPE_FRACTION);
2622 gst_value_set_fraction (&target, target_numerator, target_denominator);
2624 new_value = ⌖
2625 x = gst_value_get_fraction_range_min (value);
2626 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2628 x = gst_value_get_fraction_range_max (value);
2629 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2632 gst_structure_set_value (structure, field_name, new_value);
2633 g_value_unset (&target);
2635 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2636 const GValue *list_value;
2638 const GValue *best = NULL;
2641 gdouble best_diff = G_MAXDOUBLE;
2643 target = (gdouble) target_numerator / (gdouble) target_denominator;
2645 GST_DEBUG ("target %g, best %g", target, best_diff);
2649 n = gst_value_list_get_size (value);
2650 for (i = 0; i < n; i++) {
2651 list_value = gst_value_list_get_value (value, i);
2652 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2654 gdouble list_double;
2656 num = gst_value_get_fraction_numerator (list_value);
2657 denom = gst_value_get_fraction_denominator (list_value);
2659 list_double = ((gdouble) num / (gdouble) denom);
2660 cur_diff = target - list_double;
2662 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2665 cur_diff = -cur_diff;
2667 if (!best || cur_diff < best_diff) {
2668 GST_DEBUG ("new best %g", list_double);
2670 best_diff = cur_diff;
2675 gst_structure_set_value (structure, field_name, best);
2684 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2686 GstStructure *s = data;
2689 if (gst_value_fixate (&v, value)) {
2690 gst_structure_id_take_value (s, field_id, &v);
2696 * gst_structure_fixate_field:
2697 * @structure: a #GstStructure
2698 * @field_name: a field in @structure
2700 * Fixates a #GstStructure by changing the given field with its fixated value.
2702 * Returns: TRUE if the structure field could be fixated
2705 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2707 GstStructureField *field;
2709 g_return_val_if_fail (structure != NULL, FALSE);
2710 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2712 if (!(field = gst_structure_get_field (structure, field_name)))
2715 return default_fixate (field->name, &field->value, structure);
2718 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2719 * (useful for message parsing functions where the return location is user
2720 * supplied and the user may pass NULL if the value isn't of interest) */
2721 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2723 const GValue *_value = (value); \
2724 guint _flags = (flags); \
2725 GType _value_type = G_VALUE_TYPE (_value); \
2726 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2727 const gchar *_lcopy_format = _vtable->lcopy_format; \
2728 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2729 guint _n_values = 0; \
2731 while (*_lcopy_format != '\0') { \
2732 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2733 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2736 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2737 *(__error) = g_strdup_printf ("either all or none of the return " \
2738 "locations for field '%s' need to be NULL", fieldname); \
2739 } else if (_cvalues[0].v_pointer != NULL) { \
2740 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2745 * gst_structure_get_valist:
2746 * @structure: a #GstStructure
2747 * @first_fieldname: the name of the first field to read
2748 * @args: variable arguments
2750 * Parses the variable arguments and reads fields from @structure accordingly.
2751 * valist-variant of gst_structure_get(). Look at the documentation of
2752 * gst_structure_get() for more details.
2754 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2757 gst_structure_get_valist (const GstStructure * structure,
2758 const char *first_fieldname, va_list args)
2760 const char *field_name;
2761 GType expected_type = G_TYPE_INVALID;
2763 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2764 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2766 field_name = first_fieldname;
2767 while (field_name) {
2768 const GValue *val = NULL;
2771 expected_type = va_arg (args, GType);
2773 val = gst_structure_get_value (structure, field_name);
2778 if (G_VALUE_TYPE (val) != expected_type)
2781 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2783 g_warning ("%s: %s", G_STRFUNC, err);
2788 field_name = va_arg (args, const gchar *);
2796 GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2797 field_name, structure);
2802 GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
2803 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2804 GST_STR_NULL (g_type_name (expected_type)),
2805 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2812 * gst_structure_id_get_valist:
2813 * @structure: a #GstStructure
2814 * @first_field_id: the quark of the first field to read
2815 * @args: variable arguments
2817 * Parses the variable arguments and reads fields from @structure accordingly.
2818 * valist-variant of gst_structure_id_get(). Look at the documentation of
2819 * gst_structure_id_get() for more details.
2821 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2824 gst_structure_id_get_valist (const GstStructure * structure,
2825 GQuark first_field_id, va_list args)
2828 GType expected_type = G_TYPE_INVALID;
2830 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2831 g_return_val_if_fail (first_field_id != 0, FALSE);
2833 field_id = first_field_id;
2835 const GValue *val = NULL;
2838 expected_type = va_arg (args, GType);
2840 val = gst_structure_id_get_value (structure, field_id);
2845 if (G_VALUE_TYPE (val) != expected_type)
2848 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2850 g_warning ("%s: %s", G_STRFUNC, err);
2855 field_id = va_arg (args, GQuark);
2863 GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2864 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2869 GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
2870 "field was of type '%s': %" GST_PTR_FORMAT,
2871 g_quark_to_string (field_id),
2872 GST_STR_NULL (g_type_name (expected_type)),
2873 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2880 * gst_structure_get:
2881 * @structure: a #GstStructure
2882 * @first_fieldname: the name of the first field to read
2883 * @...: variable arguments
2885 * Parses the variable arguments and reads fields from @structure accordingly.
2886 * Variable arguments should be in the form field name, field type
2887 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2888 * The last variable argument should be NULL.
2890 * For refcounted (mini)objects you will receive a new reference which
2891 * you must release with a suitable _unref() when no longer needed. For
2892 * strings and boxed types you will receive a copy which you will need to
2893 * release with either g_free() or the suitable function for the boxed type.
2895 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2896 * because the field requested did not exist, or was of a type other
2897 * than the type specified), otherwise TRUE.
2900 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
2906 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2907 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2909 va_start (args, first_fieldname);
2910 ret = gst_structure_get_valist (structure, first_fieldname, args);
2917 * gst_structure_id_get:
2918 * @structure: a #GstStructure
2919 * @first_field_id: the quark of the first field to read
2920 * @...: variable arguments
2922 * Parses the variable arguments and reads fields from @structure accordingly.
2923 * Variable arguments should be in the form field id quark, field type
2924 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2925 * The last variable argument should be NULL (technically it should be a
2926 * 0 quark, but we require NULL so compilers that support it can check for
2927 * the NULL terminator and warn if it's not there).
2929 * This function is just like gst_structure_get() only that it is slightly
2930 * more efficient since it saves the string-to-quark lookup in the global
2933 * For refcounted (mini)objects you will receive a new reference which
2934 * you must release with a suitable _unref() when no longer needed. For
2935 * strings and boxed types you will receive a copy which you will need to
2936 * release with either g_free() or the suitable function for the boxed type.
2938 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2939 * because the field requested did not exist, or was of a type other
2940 * than the type specified), otherwise TRUE.
2943 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
2949 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2950 g_return_val_if_fail (first_field_id != 0, FALSE);
2952 va_start (args, first_field_id);
2953 ret = gst_structure_id_get_valist (structure, first_field_id, args);
2960 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
2963 const GstStructure *struct1 = (const GstStructure *) data;
2964 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
2966 if (G_UNLIKELY (val1 == NULL))
2968 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
2976 * gst_structure_is_equal:
2977 * @structure1: a #GstStructure.
2978 * @structure2: a #GstStructure.
2980 * Tests if the two #GstStructure are equal.
2982 * Returns: TRUE if the two structures have the same name and field.
2985 gst_structure_is_equal (const GstStructure * structure1,
2986 const GstStructure * structure2)
2988 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
2989 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
2991 if (G_UNLIKELY (structure1 == structure2))
2994 if (structure1->name != structure2->name) {
2997 if (GST_STRUCTURE_FIELDS (structure1)->len !=
2998 GST_STRUCTURE_FIELDS (structure2)->len) {
3002 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
3003 (gpointer) structure2);
3010 const GstStructure *intersect;
3015 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
3017 IntersectData *idata = (IntersectData *) data;
3018 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3020 if (G_UNLIKELY (val2 == NULL)) {
3021 gst_structure_id_set_value (idata->dest, id, val1);
3023 GValue dest_value = { 0 };
3024 if (gst_value_intersect (&dest_value, val1, val2)) {
3025 gst_structure_id_take_value (idata->dest, id, &dest_value);
3034 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
3036 IntersectData *idata = (IntersectData *) data;
3037 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3039 if (G_UNLIKELY (val2 == NULL)) {
3040 gst_structure_id_set_value (idata->dest, id, val1);
3046 * gst_structure_intersect:
3047 * @struct1: a #GstStructure
3048 * @struct2: a #GstStructure
3050 * Interesects @struct1 and @struct2 and returns the intersection.
3052 * Returns: Intersection of @struct1 and @struct2
3055 gst_structure_intersect (const GstStructure * struct1,
3056 const GstStructure * struct2)
3060 g_assert (struct1 != NULL);
3061 g_assert (struct2 != NULL);
3063 if (G_UNLIKELY (struct1->name != struct2->name))
3066 /* copy fields from struct1 which we have not in struct2 to target
3067 * intersect if we have the field in both */
3068 data.dest = gst_structure_new_id_empty (struct1->name);
3069 data.intersect = struct2;
3070 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3071 gst_structure_intersect_field1, &data)))
3074 /* copy fields from struct2 which we have not in struct1 to target */
3075 data.intersect = struct1;
3076 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3077 gst_structure_intersect_field2, &data)))
3083 gst_structure_free (data.dest);
3088 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3091 GstStructure *other = (GstStructure *) data;
3092 const GValue *val2 = gst_structure_id_get_value (other, id);
3094 if (G_LIKELY (val2)) {
3095 if (!gst_value_can_intersect (val1, val2)) {
3098 gint eq = gst_value_compare (val1, val2);
3100 if (eq == GST_VALUE_UNORDERED) {
3101 /* we need to try interseting */
3102 if (!gst_value_intersect (NULL, val1, val2)) {
3105 } else if (eq != GST_VALUE_EQUAL) {
3114 * gst_structure_can_intersect:
3115 * @struct1: a #GstStructure
3116 * @struct2: a #GstStructure
3118 * Tries intersecting @struct1 and @struct2 and reports whether the result
3119 * would not be empty.
3121 * Returns: %TRUE if intersection would not be empty
3124 gst_structure_can_intersect (const GstStructure * struct1,
3125 const GstStructure * struct2)
3127 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3128 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3130 if (G_UNLIKELY (struct1->name != struct2->name))
3133 /* tries to intersect if we have the field in both */
3134 return gst_structure_foreach ((GstStructure *) struct1,
3135 gst_caps_structure_can_intersect_field, (gpointer) struct2);
3139 gst_caps_structure_has_field (GQuark field_id, const GValue * value,
3142 GstStructure *subset = user_data;
3144 return gst_structure_id_get_value (subset, field_id) != NULL;
3148 gst_caps_structure_is_subset_field (GQuark field_id, const GValue * value,
3151 GstStructure *superset = user_data;
3152 const GValue *other;
3155 if (!(other = gst_structure_id_get_value (superset, field_id)))
3156 /* field is missing in the superset => is subset */
3159 comparison = gst_value_compare (other, value);
3161 /* equal values are subset */
3162 if (comparison == GST_VALUE_EQUAL)
3165 /* ordered, but unequal, values are not */
3166 if (comparison != GST_VALUE_UNORDERED)
3169 return gst_value_is_subset (value, other);
3173 * gst_structure_is_subset:
3174 * @subset: a #GstStructure
3175 * @superset: a potentially greater #GstStructure
3177 * Checks if @subset is a subset of @superset, i.e. has the same
3178 * structure name and for all fields that are existing in @superset,
3179 * @subset has a value that is a subset of the value in @superset.
3181 * Returns: %TRUE if @subset is a subset of @superset
3184 gst_structure_is_subset (const GstStructure * subset,
3185 const GstStructure * superset)
3187 if ((superset->name != subset->name) ||
3188 (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3191 /* The subset must have all fields that are in superset */
3192 if (!gst_structure_foreach ((GstStructure *) superset,
3193 gst_caps_structure_has_field, (gpointer) subset))
3196 return gst_structure_foreach ((GstStructure *) subset,
3197 gst_caps_structure_is_subset_field, (gpointer) superset);
3202 * gst_structure_fixate:
3203 * @structure: a #GstStructure
3205 * Fixate all values in @structure using gst_value_fixate().
3206 * @structure will be modified in-place and should be writable.
3209 gst_structure_fixate (GstStructure * structure)
3211 g_return_if_fail (GST_IS_STRUCTURE (structure));
3213 gst_structure_foreach (structure, default_fixate, structure);