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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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 folled 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. It
36 * provides a means to enforce mutability using the refcount of the parent
37 * with the gst_structure_set_parent_refcount() method.
39 * A #GstStructure can be created with gst_structure_new_empty() or
40 * gst_structure_new(), which both take a name and an optional set of
41 * key/value pairs along with the types of the values.
43 * Field values can be changed with gst_structure_set_value() or
44 * gst_structure_set().
46 * Field values can be retrieved with gst_structure_get_value() or the more
47 * convenient gst_structure_get_*() functions.
49 * Fields can be removed with gst_structure_remove_field() or
50 * gst_structure_remove_fields().
52 * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are
53 * not allowed. Strings must not be empty either, but may be NULL.
55 * Last reviewed on 2009-06-08 (0.10.23)
64 #include "gst_private.h"
67 #include <gobject/gvaluecollector.h>
69 typedef struct _GstStructureField GstStructureField;
71 struct _GstStructureField
81 /* owned by parent structure, NULL if no parent */
82 gint *parent_refcount;
87 #define GST_STRUCTURE_REFCOUNT(s) (((GstStructureImpl*)(s))->parent_refcount)
88 #define GST_STRUCTURE_FIELDS(s) (((GstStructureImpl*)(s))->fields)
90 #define GST_STRUCTURE_FIELD(structure, index) \
91 &g_array_index(GST_STRUCTURE_FIELDS(structure), GstStructureField, (index))
93 #define IS_MUTABLE(structure) \
94 (!GST_STRUCTURE_REFCOUNT(structure) || \
95 g_atomic_int_get (GST_STRUCTURE_REFCOUNT(structure)) == 1)
97 #define IS_TAGLIST(structure) \
98 (structure->name == GST_QUARK (TAGLIST))
100 static void gst_structure_set_field (GstStructure * structure,
101 GstStructureField * field);
102 static GstStructureField *gst_structure_get_field (const GstStructure *
103 structure, const gchar * fieldname);
104 static GstStructureField *gst_structure_id_get_field (const GstStructure *
105 structure, GQuark field);
106 static void gst_structure_transform_to_string (const GValue * src_value,
107 GValue * dest_value);
108 static GstStructure *gst_structure_copy_conditional (const GstStructure *
110 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
111 GValue * value, GType default_type);
112 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
114 GType _gst_structure_type = 0;
117 G_DEFINE_BOXED_TYPE (GstStructure, gst_structure,
118 gst_structure_copy_conditional, gst_structure_free);
121 _priv_gst_structure_initialize (void)
123 _gst_structure_type = gst_structure_get_type ();
125 g_value_register_transform_func (_gst_structure_type, G_TYPE_STRING,
126 gst_structure_transform_to_string);
129 static GstStructure *
130 gst_structure_new_id_empty_with_size (GQuark quark, guint prealloc)
132 GstStructureImpl *structure;
134 structure = g_slice_new (GstStructureImpl);
135 ((GstStructure *) structure)->type = _gst_structure_type;
136 ((GstStructure *) structure)->name = quark;
137 GST_STRUCTURE_REFCOUNT (structure) = NULL;
138 GST_STRUCTURE_FIELDS (structure) =
139 g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
141 return GST_STRUCTURE_CAST (structure);
145 * gst_structure_new_id_empty:
146 * @quark: name of new structure
148 * Creates a new, empty #GstStructure with the given name as a GQuark.
150 * Free-function: gst_structure_free
152 * Returns: (transfer full): a new, empty #GstStructure
155 gst_structure_new_id_empty (GQuark quark)
157 g_return_val_if_fail (quark != 0, NULL);
159 return gst_structure_new_id_empty_with_size (quark, 0);
162 #ifndef G_DISABLE_CHECKS
164 gst_structure_validate_name (const gchar * name)
168 g_return_val_if_fail (name != NULL, FALSE);
170 if (G_UNLIKELY (!g_ascii_isalpha (*name))) {
171 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
176 /* FIXME: test name string more */
178 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
180 if (G_UNLIKELY (*s != '\0')) {
181 GST_WARNING ("Invalid character '%c' at offset %" G_GUINTPTR_FORMAT " in"
182 " structure name: %s", *s, ((guintptr) s - (guintptr) name), name);
191 * gst_structure_new_empty:
192 * @name: name of new structure
194 * Creates a new, empty #GstStructure with the given @name.
196 * See gst_structure_set_name() for constraints on the @name parameter.
198 * Free-function: gst_structure_free
200 * Returns: (transfer full): a new, empty #GstStructure
203 gst_structure_new_empty (const gchar * name)
205 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
207 return gst_structure_new_id_empty_with_size (g_quark_from_string (name), 0);
212 * @name: name of new structure
213 * @firstfield: name of first field to set
214 * @...: additional arguments
216 * Creates a new #GstStructure with the given name. Parses the
217 * list of variable arguments and sets fields to the values listed.
218 * Variable arguments should be passed as field name, field type,
219 * and value. Last variable argument should be NULL.
221 * Free-function: gst_structure_free
223 * Returns: (transfer full): a new #GstStructure
226 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
228 GstStructure *structure;
231 va_start (varargs, firstfield);
232 structure = gst_structure_new_valist (name, firstfield, varargs);
239 * gst_structure_new_valist:
240 * @name: name of new structure
241 * @firstfield: name of first field to set
242 * @varargs: variable argument list
244 * Creates a new #GstStructure with the given @name. Structure fields
245 * are set according to the varargs in a manner similar to
246 * gst_structure_new().
248 * See gst_structure_set_name() for constraints on the @name parameter.
250 * Free-function: gst_structure_free
252 * Returns: (transfer full): a new #GstStructure
255 gst_structure_new_valist (const gchar * name,
256 const gchar * firstfield, va_list varargs)
258 GstStructure *structure;
260 structure = gst_structure_new_empty (name);
263 gst_structure_set_valist (structure, firstfield, varargs);
269 * gst_structure_set_parent_refcount:
270 * @structure: a #GstStructure
271 * @refcount: (in): a pointer to the parent's refcount
273 * Sets the parent_refcount field of #GstStructure. This field is used to
274 * determine whether a structure is mutable or not. This function should only be
275 * called by code implementing parent objects of #GstStructure, as described in
276 * the MT Refcounting section of the design documents.
278 * Returns: %TRUE if the parent refcount could be set.
281 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
283 g_return_val_if_fail (structure != NULL, FALSE);
285 /* if we have a parent_refcount already, we can only clear
286 * if with a NULL refcount */
287 if (GST_STRUCTURE_REFCOUNT (structure)) {
288 if (refcount != NULL) {
289 g_return_val_if_fail (refcount == NULL, FALSE);
293 if (refcount == NULL) {
294 g_return_val_if_fail (refcount != NULL, FALSE);
299 GST_STRUCTURE_REFCOUNT (structure) = refcount;
305 * gst_structure_copy:
306 * @structure: a #GstStructure to duplicate
308 * Duplicates a #GstStructure and all its fields and values.
310 * Free-function: gst_structure_free
312 * Returns: (transfer none): a new #GstStructure.
315 gst_structure_copy (const GstStructure * structure)
317 GstStructure *new_structure;
318 GstStructureField *field;
321 g_return_val_if_fail (structure != NULL, NULL);
323 len = GST_STRUCTURE_FIELDS (structure)->len;
324 new_structure = gst_structure_new_id_empty_with_size (structure->name, len);
326 for (i = 0; i < len; i++) {
327 GstStructureField new_field = { 0 };
329 field = GST_STRUCTURE_FIELD (structure, i);
331 new_field.name = field->name;
332 gst_value_init_and_copy (&new_field.value, &field->value);
333 g_array_append_val (GST_STRUCTURE_FIELDS (new_structure), new_field);
336 return new_structure;
340 * gst_structure_free:
341 * @structure: (in) (transfer full): the #GstStructure to free
343 * Frees a #GstStructure and all its fields and values. The structure must not
344 * have a parent when this function is called.
347 gst_structure_free (GstStructure * structure)
349 GstStructureField *field;
352 g_return_if_fail (structure != NULL);
353 g_return_if_fail (GST_STRUCTURE_REFCOUNT (structure) == NULL);
355 len = GST_STRUCTURE_FIELDS (structure)->len;
356 for (i = 0; i < len; i++) {
357 field = GST_STRUCTURE_FIELD (structure, i);
359 if (G_IS_VALUE (&field->value)) {
360 g_value_unset (&field->value);
363 g_array_free (GST_STRUCTURE_FIELDS (structure), TRUE);
365 memset (structure, 0xff, sizeof (GstStructure));
367 g_slice_free1 (sizeof (GstStructureImpl), structure);
371 * gst_structure_get_name:
372 * @structure: a #GstStructure
374 * Get the name of @structure as a string.
376 * Returns: the name of the structure.
379 gst_structure_get_name (const GstStructure * structure)
381 g_return_val_if_fail (structure != NULL, NULL);
383 return g_quark_to_string (structure->name);
387 * gst_structure_has_name:
388 * @structure: a #GstStructure
389 * @name: structure name to check for
391 * Checks if the structure has the given name
393 * Returns: TRUE if @name matches the name of the structure.
396 gst_structure_has_name (const GstStructure * structure, const gchar * name)
398 const gchar *structure_name;
400 g_return_val_if_fail (structure != NULL, FALSE);
401 g_return_val_if_fail (name != NULL, FALSE);
403 /* getting the string is cheap and comparing short strings is too
404 * should be faster than getting the quark for name and comparing the quarks
406 structure_name = g_quark_to_string (structure->name);
408 return (structure_name && strcmp (structure_name, name) == 0);
412 * gst_structure_get_name_id:
413 * @structure: a #GstStructure
415 * Get the name of @structure as a GQuark.
417 * Returns: the quark representing the name of the structure.
420 gst_structure_get_name_id (const GstStructure * structure)
422 g_return_val_if_fail (structure != NULL, 0);
424 return structure->name;
428 * gst_structure_set_name:
429 * @structure: a #GstStructure
430 * @name: the new name of the structure
432 * Sets the name of the structure to the given @name. The string
433 * provided is copied before being used. It must not be empty, start with a
434 * letter and can be followed by letters, numbers and any of "/-_.:".
437 gst_structure_set_name (GstStructure * structure, const gchar * name)
439 g_return_if_fail (structure != NULL);
440 g_return_if_fail (IS_MUTABLE (structure));
441 g_return_if_fail (gst_structure_validate_name (name));
443 structure->name = g_quark_from_string (name);
447 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
448 const GValue * value)
450 GstStructureField gsfield = { 0, {0,} };
452 gsfield.name = field;
453 gst_value_init_and_copy (&gsfield.value, value);
455 gst_structure_set_field (structure, &gsfield);
459 * gst_structure_id_set_value:
460 * @structure: a #GstStructure
461 * @field: a #GQuark representing a field
462 * @value: the new value of the field
464 * Sets the field with the given GQuark @field to @value. If the field
465 * does not exist, it is created. If the field exists, the previous
466 * value is replaced and freed.
469 gst_structure_id_set_value (GstStructure * structure,
470 GQuark field, const GValue * value)
473 g_return_if_fail (structure != NULL);
474 g_return_if_fail (G_IS_VALUE (value));
475 g_return_if_fail (IS_MUTABLE (structure));
477 gst_structure_id_set_value_internal (structure, field, value);
481 * gst_structure_set_value:
482 * @structure: a #GstStructure
483 * @fieldname: the name of the field to set
484 * @value: the new value of the field
486 * Sets the field with the given name @field to @value. If the field
487 * does not exist, it is created. If the field exists, the previous
488 * value is replaced and freed.
491 gst_structure_set_value (GstStructure * structure,
492 const gchar * fieldname, const GValue * value)
494 g_return_if_fail (structure != NULL);
495 g_return_if_fail (fieldname != NULL);
496 g_return_if_fail (G_IS_VALUE (value));
497 g_return_if_fail (IS_MUTABLE (structure));
499 gst_structure_id_set_value_internal (structure,
500 g_quark_from_string (fieldname), value);
504 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
507 GstStructureField gsfield = { 0, {0,} };
509 gsfield.name = field;
510 gsfield.value = *value;
512 gst_structure_set_field (structure, &gsfield);
514 /* we took ownership */
516 memset (value, 0, sizeof (GValue));
518 value->g_type = G_TYPE_INVALID;
523 * gst_structure_id_take_value:
524 * @structure: a #GstStructure
525 * @field: a #GQuark representing a field
526 * @value: (transfer full): the new value of the field
528 * Sets the field with the given GQuark @field to @value. If the field
529 * does not exist, it is created. If the field exists, the previous
530 * value is replaced and freed.
535 gst_structure_id_take_value (GstStructure * structure, GQuark field,
538 g_return_if_fail (structure != NULL);
539 g_return_if_fail (G_IS_VALUE (value));
540 g_return_if_fail (IS_MUTABLE (structure));
542 gst_structure_id_take_value_internal (structure, field, value);
546 * gst_structure_take_value:
547 * @structure: a #GstStructure
548 * @fieldname: the name of the field to set
549 * @value: (transfer full): the new value of the field
551 * Sets the field with the given name @field to @value. If the field
552 * does not exist, it is created. If the field exists, the previous
553 * value is replaced and freed. The function will take ownership of @value.
558 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
561 g_return_if_fail (structure != NULL);
562 g_return_if_fail (fieldname != NULL);
563 g_return_if_fail (G_IS_VALUE (value));
564 g_return_if_fail (IS_MUTABLE (structure));
566 gst_structure_id_take_value_internal (structure,
567 g_quark_from_string (fieldname), value);
571 gst_structure_set_valist_internal (GstStructure * structure,
572 const gchar * fieldname, va_list varargs)
578 GstStructureField field = { 0 };
580 field.name = g_quark_from_string (fieldname);
582 type = va_arg (varargs, GType);
584 if (G_UNLIKELY (type == G_TYPE_DATE)) {
585 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
586 type = GST_TYPE_DATE;
588 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
589 if (G_UNLIKELY (err)) {
590 g_critical ("%s", err);
593 gst_structure_set_field (structure, &field);
595 fieldname = va_arg (varargs, gchar *);
601 * @structure: a #GstStructure
602 * @fieldname: the name of the field to set
603 * @...: variable arguments
605 * Parses the variable arguments and sets fields accordingly.
606 * Variable arguments should be in the form field name, field type
607 * (as a GType), value(s). The last variable argument should be NULL.
610 gst_structure_set (GstStructure * structure, const gchar * field, ...)
614 g_return_if_fail (structure != NULL);
615 g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
617 va_start (varargs, field);
618 gst_structure_set_valist_internal (structure, field, varargs);
623 * gst_structure_set_valist:
624 * @structure: a #GstStructure
625 * @fieldname: the name of the field to set
626 * @varargs: variable arguments
628 * va_list form of gst_structure_set().
631 gst_structure_set_valist (GstStructure * structure,
632 const gchar * fieldname, va_list varargs)
634 g_return_if_fail (structure != NULL);
635 g_return_if_fail (IS_MUTABLE (structure));
637 gst_structure_set_valist_internal (structure, fieldname, varargs);
641 gst_structure_id_set_valist_internal (GstStructure * structure,
642 GQuark fieldname, va_list varargs)
648 GstStructureField field = { 0 };
650 field.name = fieldname;
652 type = va_arg (varargs, GType);
654 if (G_UNLIKELY (type == G_TYPE_DATE)) {
655 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
656 type = GST_TYPE_DATE;
658 #ifndef G_VALUE_COLLECT_INIT
659 g_value_init (&field.value, type);
660 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
662 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
664 if (G_UNLIKELY (err)) {
665 g_critical ("%s", err);
668 gst_structure_set_field (structure, &field);
670 fieldname = va_arg (varargs, GQuark);
675 * gst_structure_id_set:
676 * @structure: a #GstStructure
677 * @fieldname: the GQuark for the name of the field to set
678 * @...: variable arguments
680 * Identical to gst_structure_set, except that field names are
681 * passed using the GQuark for the field name. This allows more efficient
682 * setting of the structure if the caller already knows the associated
684 * The last variable argument must be NULL.
689 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
693 g_return_if_fail (structure != NULL);
695 va_start (varargs, field);
696 gst_structure_id_set_valist_internal (structure, field, varargs);
701 * gst_structure_id_set_valist:
702 * @structure: a #GstStructure
703 * @fieldname: the name of the field to set
704 * @varargs: variable arguments
706 * va_list form of gst_structure_id_set().
711 gst_structure_id_set_valist (GstStructure * structure,
712 GQuark fieldname, va_list varargs)
714 g_return_if_fail (structure != NULL);
715 g_return_if_fail (IS_MUTABLE (structure));
717 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
721 * gst_structure_new_id:
722 * @name_quark: name of new structure
723 * @field_quark: the GQuark for the name of the field to set
724 * @...: variable arguments
726 * Creates a new #GstStructure with the given name as a GQuark, followed by
727 * fieldname quark, GType, argument(s) "triplets" in the same format as
728 * gst_structure_id_set(). Basically a convenience wrapper around
729 * gst_structure_new_id_empty() and gst_structure_id_set().
731 * The last variable argument must be NULL (or 0).
733 * Free-function: gst_structure_free
735 * Returns: (transfer full): a new #GstStructure
740 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
745 g_return_val_if_fail (name_quark != 0, NULL);
746 g_return_val_if_fail (field_quark != 0, NULL);
748 s = gst_structure_new_id_empty (name_quark);
750 va_start (varargs, field_quark);
751 gst_structure_id_set_valist_internal (s, field_quark, varargs);
757 #if GST_VERSION_NANO == 1
758 #define GIT_G_WARNING g_warning
760 #define GIT_G_WARNING GST_WARNING
763 /* If the structure currently contains a field with the same name, it is
764 * replaced with the provided field. Otherwise, the field is added to the
765 * structure. The field's value is not deeply copied.
768 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
770 GstStructureField *f;
771 guint i, len = GST_STRUCTURE_FIELDS (structure)->len;
773 if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
776 s = g_value_get_string (&field->value);
777 /* only check for NULL strings in taglists, as they are allowed in message
778 * structs, e.g. error message debug strings */
779 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
781 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
782 "Please file a bug.", g_quark_to_string (field->name));
783 g_value_unset (&field->value);
786 /* empty strings never make sense */
787 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
788 "Please file a bug.", g_quark_to_string (field->name));
789 g_value_unset (&field->value);
792 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
793 g_warning ("Trying to set string on %s field '%s', but string is not "
794 "valid UTF-8. Please file a bug.",
795 IS_TAGLIST (structure) ? "taglist" : "structure",
796 g_quark_to_string (field->name));
797 g_value_unset (&field->value);
800 } else if (G_UNLIKELY (GST_VALUE_HOLDS_DATE (&field->value))) {
803 d = gst_value_get_date (&field->value);
804 /* only check for NULL GDates in taglists, as they might make sense
805 * in other, generic structs */
806 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
807 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
808 "Please file a bug.", g_quark_to_string (field->name));
809 g_value_unset (&field->value);
811 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
813 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
814 IS_TAGLIST (structure) ? "taglist" : "structure",
815 g_quark_to_string (field->name));
816 g_value_unset (&field->value);
821 for (i = 0; i < len; i++) {
822 f = GST_STRUCTURE_FIELD (structure, i);
824 if (G_UNLIKELY (f->name == field->name)) {
825 g_value_unset (&f->value);
826 memcpy (f, field, sizeof (GstStructureField));
831 g_array_append_val (GST_STRUCTURE_FIELDS (structure), *field);
834 /* If there is no field with the given ID, NULL is returned.
836 static GstStructureField *
837 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
839 GstStructureField *field;
842 len = GST_STRUCTURE_FIELDS (structure)->len;
844 for (i = 0; i < len; i++) {
845 field = GST_STRUCTURE_FIELD (structure, i);
847 if (G_UNLIKELY (field->name == field_id))
854 /* If there is no field with the given ID, NULL is returned.
856 static GstStructureField *
857 gst_structure_get_field (const GstStructure * structure,
858 const gchar * fieldname)
860 g_return_val_if_fail (structure != NULL, NULL);
861 g_return_val_if_fail (fieldname != NULL, NULL);
863 return gst_structure_id_get_field (structure,
864 g_quark_from_string (fieldname));
868 * gst_structure_get_value:
869 * @structure: a #GstStructure
870 * @fieldname: the name of the field to get
872 * Get the value of the field with name @fieldname.
874 * Returns: the #GValue corresponding to the field with the given name.
877 gst_structure_get_value (const GstStructure * structure,
878 const gchar * fieldname)
880 GstStructureField *field;
882 g_return_val_if_fail (structure != NULL, NULL);
883 g_return_val_if_fail (fieldname != NULL, NULL);
885 field = gst_structure_get_field (structure, fieldname);
889 return &field->value;
893 * gst_structure_id_get_value:
894 * @structure: a #GstStructure
895 * @field: the #GQuark of the field to get
897 * Get the value of the field with GQuark @field.
899 * Returns: the #GValue corresponding to the field with the given name
903 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
905 GstStructureField *gsfield;
907 g_return_val_if_fail (structure != NULL, NULL);
909 gsfield = gst_structure_id_get_field (structure, field);
913 return &gsfield->value;
917 * gst_structure_remove_field:
918 * @structure: a #GstStructure
919 * @fieldname: the name of the field to remove
921 * Removes the field with the given name. If the field with the given
922 * name does not exist, the structure is unchanged.
925 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
927 GstStructureField *field;
931 g_return_if_fail (structure != NULL);
932 g_return_if_fail (fieldname != NULL);
933 g_return_if_fail (IS_MUTABLE (structure));
935 id = g_quark_from_string (fieldname);
936 len = GST_STRUCTURE_FIELDS (structure)->len;
938 for (i = 0; i < len; i++) {
939 field = GST_STRUCTURE_FIELD (structure, i);
941 if (field->name == id) {
942 if (G_IS_VALUE (&field->value)) {
943 g_value_unset (&field->value);
945 GST_STRUCTURE_FIELDS (structure) =
946 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
953 * gst_structure_remove_fields:
954 * @structure: a #GstStructure
955 * @fieldname: the name of the field to remove
956 * @...: NULL-terminated list of more fieldnames to remove
958 * Removes the fields with the given names. If a field does not exist, the
959 * argument is ignored.
962 gst_structure_remove_fields (GstStructure * structure,
963 const gchar * fieldname, ...)
967 g_return_if_fail (structure != NULL);
968 g_return_if_fail (fieldname != NULL);
969 /* mutability checked in remove_field */
971 va_start (varargs, fieldname);
972 gst_structure_remove_fields_valist (structure, fieldname, varargs);
977 * gst_structure_remove_fields_valist:
978 * @structure: a #GstStructure
979 * @fieldname: the name of the field to remove
980 * @varargs: NULL-terminated list of more fieldnames to remove
982 * va_list form of gst_structure_remove_fields().
985 gst_structure_remove_fields_valist (GstStructure * structure,
986 const gchar * fieldname, va_list varargs)
988 gchar *field = (gchar *) fieldname;
990 g_return_if_fail (structure != NULL);
991 g_return_if_fail (fieldname != NULL);
992 /* mutability checked in remove_field */
995 gst_structure_remove_field (structure, field);
996 field = va_arg (varargs, char *);
1001 * gst_structure_remove_all_fields:
1002 * @structure: a #GstStructure
1004 * Removes all fields in a GstStructure.
1007 gst_structure_remove_all_fields (GstStructure * structure)
1009 GstStructureField *field;
1012 g_return_if_fail (structure != NULL);
1013 g_return_if_fail (IS_MUTABLE (structure));
1015 for (i = GST_STRUCTURE_FIELDS (structure)->len - 1; i >= 0; i--) {
1016 field = GST_STRUCTURE_FIELD (structure, i);
1018 if (G_IS_VALUE (&field->value)) {
1019 g_value_unset (&field->value);
1021 GST_STRUCTURE_FIELDS (structure) =
1022 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1027 * gst_structure_get_field_type:
1028 * @structure: a #GstStructure
1029 * @fieldname: the name of the field
1031 * Finds the field with the given name, and returns the type of the
1032 * value it contains. If the field is not found, G_TYPE_INVALID is
1035 * Returns: the #GValue of the field
1038 gst_structure_get_field_type (const GstStructure * structure,
1039 const gchar * fieldname)
1041 GstStructureField *field;
1043 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1044 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1046 field = gst_structure_get_field (structure, fieldname);
1048 return G_TYPE_INVALID;
1050 return G_VALUE_TYPE (&field->value);
1054 * gst_structure_n_fields:
1055 * @structure: a #GstStructure
1057 * Get the number of fields in the structure.
1059 * Returns: the number of fields in the structure
1062 gst_structure_n_fields (const GstStructure * structure)
1064 g_return_val_if_fail (structure != NULL, 0);
1066 return GST_STRUCTURE_FIELDS (structure)->len;
1070 * gst_structure_nth_field_name:
1071 * @structure: a #GstStructure
1072 * @index: the index to get the name of
1074 * Get the name of the given field number, counting from 0 onwards.
1076 * Returns: the name of the given field number
1079 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1081 GstStructureField *field;
1083 g_return_val_if_fail (structure != NULL, NULL);
1084 g_return_val_if_fail (index < GST_STRUCTURE_FIELDS (structure)->len, NULL);
1086 field = GST_STRUCTURE_FIELD (structure, index);
1088 return g_quark_to_string (field->name);
1092 * gst_structure_foreach:
1093 * @structure: a #GstStructure
1094 * @func: (scope call): a function to call for each field
1095 * @user_data: (closure): private data
1097 * Calls the provided function once for each field in the #GstStructure. The
1098 * function must not modify the fields. Also see gst_structure_map_in_place().
1100 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1104 gst_structure_foreach (const GstStructure * structure,
1105 GstStructureForeachFunc func, gpointer user_data)
1108 GstStructureField *field;
1111 g_return_val_if_fail (structure != NULL, FALSE);
1112 g_return_val_if_fail (func != NULL, FALSE);
1114 len = GST_STRUCTURE_FIELDS (structure)->len;
1116 for (i = 0; i < len; i++) {
1117 field = GST_STRUCTURE_FIELD (structure, i);
1119 ret = func (field->name, &field->value, user_data);
1120 if (G_UNLIKELY (!ret))
1128 * gst_structure_map_in_place:
1129 * @structure: a #GstStructure
1130 * @func: (scope call): a function to call for each field
1131 * @user_data: (closure): private data
1133 * Calls the provided function once for each field in the #GstStructure. In
1134 * contrast to gst_structure_foreach(), the function may modify but not delete the
1135 * fields. The structure must be mutable.
1137 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1141 gst_structure_map_in_place (GstStructure * structure,
1142 GstStructureMapFunc func, gpointer user_data)
1145 GstStructureField *field;
1148 g_return_val_if_fail (structure != NULL, FALSE);
1149 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1150 g_return_val_if_fail (func != NULL, FALSE);
1151 len = GST_STRUCTURE_FIELDS (structure)->len;
1153 for (i = 0; i < len; i++) {
1154 field = GST_STRUCTURE_FIELD (structure, i);
1156 ret = func (field->name, &field->value, user_data);
1165 * gst_structure_id_has_field:
1166 * @structure: a #GstStructure
1167 * @field: #GQuark of the field name
1169 * Check if @structure contains a field named @field.
1171 * Returns: TRUE if the structure contains a field with the given name
1176 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1178 GstStructureField *f;
1180 g_return_val_if_fail (structure != NULL, FALSE);
1181 g_return_val_if_fail (field != 0, FALSE);
1183 f = gst_structure_id_get_field (structure, field);
1189 * gst_structure_has_field:
1190 * @structure: a #GstStructure
1191 * @fieldname: the name of a field
1193 * Check if @structure contains a field named @fieldname.
1195 * Returns: TRUE if the structure contains a field with the given name
1198 gst_structure_has_field (const GstStructure * structure,
1199 const gchar * fieldname)
1201 g_return_val_if_fail (structure != NULL, FALSE);
1202 g_return_val_if_fail (fieldname != NULL, FALSE);
1204 return gst_structure_id_has_field (structure,
1205 g_quark_from_string (fieldname));
1209 * gst_structure_id_has_field_typed:
1210 * @structure: a #GstStructure
1211 * @field: #GQuark of the field name
1212 * @type: the type of a value
1214 * Check if @structure contains a field named @field and with GType @type.
1216 * Returns: TRUE if the structure contains a field with the given name and type
1221 gst_structure_id_has_field_typed (const GstStructure * structure,
1222 GQuark field, GType type)
1224 GstStructureField *f;
1226 g_return_val_if_fail (structure != NULL, FALSE);
1227 g_return_val_if_fail (field != 0, FALSE);
1229 f = gst_structure_id_get_field (structure, field);
1233 return (G_VALUE_TYPE (&f->value) == type);
1237 * gst_structure_has_field_typed:
1238 * @structure: a #GstStructure
1239 * @fieldname: the name of a field
1240 * @type: the type of a value
1242 * Check if @structure contains a field named @fieldname and with GType @type.
1244 * Returns: TRUE if the structure contains a field with the given name and type
1247 gst_structure_has_field_typed (const GstStructure * structure,
1248 const gchar * fieldname, GType type)
1250 g_return_val_if_fail (structure != NULL, FALSE);
1251 g_return_val_if_fail (fieldname != NULL, FALSE);
1253 return gst_structure_id_has_field_typed (structure,
1254 g_quark_from_string (fieldname), type);
1257 /* utility functions */
1260 * gst_structure_get_boolean:
1261 * @structure: a #GstStructure
1262 * @fieldname: the name of a field
1263 * @value: (out): a pointer to a #gboolean to set
1265 * Sets the boolean pointed to by @value corresponding to the value of the
1266 * given field. Caller is responsible for making sure the field exists
1267 * and has the correct type.
1269 * Returns: TRUE if the value could be set correctly. If there was no field
1270 * with @fieldname or the existing field did not contain a boolean, this
1271 * function returns FALSE.
1274 gst_structure_get_boolean (const GstStructure * structure,
1275 const gchar * fieldname, gboolean * value)
1277 GstStructureField *field;
1279 g_return_val_if_fail (structure != NULL, FALSE);
1280 g_return_val_if_fail (fieldname != NULL, FALSE);
1282 field = gst_structure_get_field (structure, fieldname);
1286 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1289 *value = gst_g_value_get_boolean_unchecked (&field->value);
1295 * gst_structure_get_int:
1296 * @structure: a #GstStructure
1297 * @fieldname: the name of a field
1298 * @value: (out): a pointer to an int to set
1300 * Sets the int pointed to by @value corresponding to the value of the
1301 * given field. Caller is responsible for making sure the field exists
1302 * and has the correct type.
1304 * Returns: %TRUE if the value could be set correctly. If there was no field
1305 * with @fieldname or the existing field did not contain an int, this function
1309 gst_structure_get_int (const GstStructure * structure,
1310 const gchar * fieldname, gint * value)
1312 GstStructureField *field;
1314 g_return_val_if_fail (structure != NULL, FALSE);
1315 g_return_val_if_fail (fieldname != NULL, FALSE);
1316 g_return_val_if_fail (value != NULL, FALSE);
1318 field = gst_structure_get_field (structure, fieldname);
1322 if (!G_VALUE_HOLDS_INT (&field->value))
1325 *value = gst_g_value_get_int_unchecked (&field->value);
1331 * gst_structure_get_uint:
1332 * @structure: a #GstStructure
1333 * @fieldname: the name of a field
1334 * @value: (out): a pointer to a uint to set
1336 * Sets the uint pointed to by @value corresponding to the value of the
1337 * given field. Caller is responsible for making sure the field exists
1338 * and has the correct type.
1340 * Returns: %TRUE if the value could be set correctly. If there was no field
1341 * with @fieldname or the existing field did not contain a uint, this function
1347 gst_structure_get_uint (const GstStructure * structure,
1348 const gchar * fieldname, guint * value)
1350 GstStructureField *field;
1352 g_return_val_if_fail (structure != NULL, FALSE);
1353 g_return_val_if_fail (fieldname != NULL, FALSE);
1354 g_return_val_if_fail (value != NULL, FALSE);
1356 field = gst_structure_get_field (structure, fieldname);
1360 if (!G_VALUE_HOLDS_UINT (&field->value))
1363 *value = gst_g_value_get_uint_unchecked (&field->value);
1369 * gst_structure_get_date:
1370 * @structure: a #GstStructure
1371 * @fieldname: the name of a field
1372 * @value: (out callee-allocates): a pointer to a #GDate to set
1374 * Sets the date pointed to by @value corresponding to the date of the
1375 * given field. Caller is responsible for making sure the field exists
1376 * and has the correct type.
1378 * On success @value will point to a newly-allocated copy of the date which
1379 * should be freed with g_date_free() when no longer needed (note: this is
1380 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1381 * copy of the string).
1383 * Returns: TRUE if the value could be set correctly. If there was no field
1384 * with @fieldname or the existing field did not contain a data, this function
1388 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1391 GstStructureField *field;
1393 g_return_val_if_fail (structure != NULL, FALSE);
1394 g_return_val_if_fail (fieldname != NULL, FALSE);
1395 g_return_val_if_fail (value != NULL, FALSE);
1397 field = gst_structure_get_field (structure, fieldname);
1401 if (!GST_VALUE_HOLDS_DATE (&field->value))
1404 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1405 *value = g_value_dup_boxed (&field->value);
1411 * gst_structure_get_date_time:
1412 * @structure: a #GstStructure
1413 * @fieldname: the name of a field
1414 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1416 * Sets the datetime pointed to by @value corresponding to the datetime of the
1417 * given field. Caller is responsible for making sure the field exists
1418 * and has the correct type.
1420 * On success @value will point to a reference of the datetime which
1421 * should be unreffed with gst_date_time_unref() when no longer needed
1422 * (note: this is inconsistent with e.g. gst_structure_get_string()
1423 * which doesn't return a copy of the string).
1425 * Returns: TRUE if the value could be set correctly. If there was no field
1426 * with @fieldname or the existing field did not contain a data, this function
1432 gst_structure_get_date_time (const GstStructure * structure,
1433 const gchar * fieldname, GstDateTime ** value)
1435 GstStructureField *field;
1437 g_return_val_if_fail (structure != NULL, FALSE);
1438 g_return_val_if_fail (fieldname != NULL, FALSE);
1439 g_return_val_if_fail (value != NULL, FALSE);
1441 field = gst_structure_get_field (structure, fieldname);
1445 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1448 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1449 *value = g_value_dup_boxed (&field->value);
1455 * gst_structure_get_clock_time:
1456 * @structure: a #GstStructure
1457 * @fieldname: the name of a field
1458 * @value: (out): a pointer to a #GstClockTime to set
1460 * Sets the clock time pointed to by @value corresponding to the clock time
1461 * of the given field. Caller is responsible for making sure the field exists
1462 * and has the correct type.
1464 * Returns: TRUE if the value could be set correctly. If there was no field
1465 * with @fieldname or the existing field did not contain a #GstClockTime, this
1466 * function returns FALSE.
1469 gst_structure_get_clock_time (const GstStructure * structure,
1470 const gchar * fieldname, GstClockTime * value)
1472 GstStructureField *field;
1474 g_return_val_if_fail (structure != NULL, FALSE);
1475 g_return_val_if_fail (fieldname != NULL, FALSE);
1476 g_return_val_if_fail (value != NULL, FALSE);
1478 field = gst_structure_get_field (structure, fieldname);
1482 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1485 *value = gst_g_value_get_uint64_unchecked (&field->value);
1491 * gst_structure_get_double:
1492 * @structure: a #GstStructure
1493 * @fieldname: the name of a field
1494 * @value: (out): a pointer to a gdouble to set
1496 * Sets the double pointed to by @value corresponding to the value of the
1497 * given field. Caller is responsible for making sure the field exists
1498 * and has the correct type.
1500 * Returns: TRUE if the value could be set correctly. If there was no field
1501 * with @fieldname or the existing field did not contain a double, this
1502 * function returns FALSE.
1505 gst_structure_get_double (const GstStructure * structure,
1506 const gchar * fieldname, gdouble * value)
1508 GstStructureField *field;
1510 g_return_val_if_fail (structure != NULL, FALSE);
1511 g_return_val_if_fail (fieldname != NULL, FALSE);
1512 g_return_val_if_fail (value != NULL, FALSE);
1514 field = gst_structure_get_field (structure, fieldname);
1518 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1521 *value = gst_g_value_get_double_unchecked (&field->value);
1527 * gst_structure_get_string:
1528 * @structure: a #GstStructure
1529 * @fieldname: the name of a field
1531 * Finds the field corresponding to @fieldname, and returns the string
1532 * contained in the field's value. Caller is responsible for making
1533 * sure the field exists and has the correct type.
1535 * The string should not be modified, and remains valid until the next
1536 * call to a gst_structure_*() function with the given structure.
1538 * Returns: a pointer to the string or NULL when the field did not exist
1539 * or did not contain a string.
1542 gst_structure_get_string (const GstStructure * structure,
1543 const gchar * fieldname)
1545 GstStructureField *field;
1547 g_return_val_if_fail (structure != NULL, NULL);
1548 g_return_val_if_fail (fieldname != NULL, NULL);
1550 field = gst_structure_get_field (structure, fieldname);
1554 if (!G_VALUE_HOLDS_STRING (&field->value))
1557 return gst_g_value_get_string_unchecked (&field->value);
1561 * gst_structure_get_enum:
1562 * @structure: a #GstStructure
1563 * @fieldname: the name of a field
1564 * @enumtype: the enum type of a field
1565 * @value: (out): a pointer to an int to set
1567 * Sets the int pointed to by @value corresponding to the value of the
1568 * given field. Caller is responsible for making sure the field exists,
1569 * has the correct type and that the enumtype is correct.
1571 * Returns: TRUE if the value could be set correctly. If there was no field
1572 * with @fieldname or the existing field did not contain an enum of the given
1573 * type, this function returns FALSE.
1576 gst_structure_get_enum (const GstStructure * structure,
1577 const gchar * fieldname, GType enumtype, gint * value)
1579 GstStructureField *field;
1581 g_return_val_if_fail (structure != NULL, FALSE);
1582 g_return_val_if_fail (fieldname != NULL, FALSE);
1583 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1584 g_return_val_if_fail (value != NULL, FALSE);
1586 field = gst_structure_get_field (structure, fieldname);
1590 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1593 *value = g_value_get_enum (&field->value);
1599 * gst_structure_get_fraction:
1600 * @structure: a #GstStructure
1601 * @fieldname: the name of a field
1602 * @value_numerator: (out): a pointer to an int to set
1603 * @value_denominator: (out): a pointer to an int to set
1605 * Sets the integers pointed to by @value_numerator and @value_denominator
1606 * corresponding to the value of the given field. Caller is responsible
1607 * for making sure the field exists and has the correct type.
1609 * Returns: TRUE if the values could be set correctly. If there was no field
1610 * with @fieldname or the existing field did not contain a GstFraction, this
1611 * function returns FALSE.
1614 gst_structure_get_fraction (const GstStructure * structure,
1615 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1617 GstStructureField *field;
1619 g_return_val_if_fail (structure != NULL, FALSE);
1620 g_return_val_if_fail (fieldname != NULL, FALSE);
1621 g_return_val_if_fail (value_numerator != NULL, FALSE);
1622 g_return_val_if_fail (value_denominator != NULL, FALSE);
1624 field = gst_structure_get_field (structure, fieldname);
1628 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1631 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1632 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1637 typedef struct _GstStructureAbbreviation
1639 const gchar *type_name;
1642 GstStructureAbbreviation;
1644 /* return a copy of an array of GstStructureAbbreviation containing all the
1645 * known type_string, GType maps, including abbreviations for common types */
1646 static GstStructureAbbreviation *
1647 gst_structure_get_abbrs (gint * n_abbrs)
1649 static GstStructureAbbreviation *abbrs = NULL;
1650 static volatile gsize num = 0;
1652 if (g_once_init_enter (&num)) {
1653 /* dynamically generate the array */
1655 GstStructureAbbreviation dyn_abbrs[] = {
1660 {"uint", G_TYPE_UINT}
1664 {"float", G_TYPE_FLOAT}
1668 {"double", G_TYPE_DOUBLE}
1670 {"d", G_TYPE_DOUBLE}
1672 {"buffer", GST_TYPE_BUFFER}
1674 {"fraction", GST_TYPE_FRACTION}
1676 {"boolean", G_TYPE_BOOLEAN}
1678 {"bool", G_TYPE_BOOLEAN}
1680 {"b", G_TYPE_BOOLEAN}
1682 {"string", G_TYPE_STRING}
1684 {"str", G_TYPE_STRING}
1686 {"s", G_TYPE_STRING}
1688 {"structure", GST_TYPE_STRUCTURE}
1690 {"date", GST_TYPE_DATE}
1692 {"datetime", GST_TYPE_DATE_TIME}
1694 _num = G_N_ELEMENTS (dyn_abbrs);
1695 /* permanently allocate and copy the array now */
1696 abbrs = g_new0 (GstStructureAbbreviation, _num);
1697 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1698 g_once_init_leave (&num, _num);
1705 /* given a type_name that could be a type abbreviation or a registered GType,
1706 * return a matching GType */
1708 gst_structure_gtype_from_abbr (const char *type_name)
1711 GstStructureAbbreviation *abbrs;
1714 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1716 abbrs = gst_structure_get_abbrs (&n_abbrs);
1718 for (i = 0; i < n_abbrs; i++) {
1719 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1720 return abbrs[i].type;
1724 /* this is the fallback */
1725 return g_type_from_name (type_name);
1729 gst_structure_to_abbr (GType type)
1732 GstStructureAbbreviation *abbrs;
1735 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1737 abbrs = gst_structure_get_abbrs (&n_abbrs);
1739 for (i = 0; i < n_abbrs; i++) {
1740 if (type == abbrs[i].type) {
1741 return abbrs[i].type_name;
1745 return g_type_name (type);
1749 gst_structure_value_get_generic_type (GValue * val)
1751 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1752 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1753 GArray *array = g_value_peek_pointer (val);
1755 if (array->len > 0) {
1756 GValue *value = &g_array_index (array, GValue, 0);
1758 return gst_structure_value_get_generic_type (value);
1762 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1764 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1765 return G_TYPE_INT64;
1766 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1767 return G_TYPE_DOUBLE;
1768 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1769 return GST_TYPE_FRACTION;
1771 return G_VALUE_TYPE (val);
1775 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1778 GstStructureField *field;
1781 g_return_val_if_fail (s != NULL, FALSE);
1783 g_string_append (s, g_quark_to_string (structure->name));
1784 len = GST_STRUCTURE_FIELDS (structure)->len;
1785 for (i = 0; i < len; i++) {
1789 field = GST_STRUCTURE_FIELD (structure, i);
1791 t = gst_value_serialize (&field->value);
1792 type = gst_structure_value_get_generic_type (&field->value);
1794 g_string_append_len (s, ", ", 2);
1795 /* FIXME: do we need to escape fieldnames? */
1796 g_string_append (s, g_quark_to_string (field->name));
1797 g_string_append_len (s, "=(", 2);
1798 g_string_append (s, gst_structure_to_abbr (type));
1799 g_string_append_c (s, ')');
1800 g_string_append (s, t == NULL ? "NULL" : t);
1804 g_string_append_c (s, ';');
1809 * gst_structure_to_string:
1810 * @structure: a #GstStructure
1812 * Converts @structure to a human-readable string representation.
1814 * For debugging purposes its easier to do something like this:
1816 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1818 * This prints the structure in human readble form.
1820 * Free-function: g_free
1822 * Returns: (transfer full)L a pointer to string allocated by g_malloc().
1823 * g_free() after usage.
1826 gst_structure_to_string (const GstStructure * structure)
1830 /* NOTE: This function is potentially called by the debug system,
1831 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1832 * should be careful to avoid recursion. This includes any functions
1833 * called by gst_structure_to_string. In particular, calls should
1834 * not use the GST_PTR_FORMAT extension. */
1836 g_return_val_if_fail (structure != NULL, NULL);
1838 /* we estimate a minimum size based on the number of fields in order to
1839 * avoid unnecessary reallocs within GString */
1840 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1841 priv_gst_structure_append_to_gstring (structure, s);
1842 return g_string_free (s, FALSE);
1846 * r will still point to the string. if end == next, the string will not be
1847 * null-terminated. In all other cases it will be.
1848 * end = pointer to char behind end of string, next = pointer to start of
1850 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1853 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1864 ret = gst_structure_parse_simple_string (s, end);
1874 if (G_UNLIKELY (*s == 0))
1876 if (G_UNLIKELY (*s == '\\'))
1884 /* Find the closing quotes */
1887 if (G_UNLIKELY (*s == 0))
1889 if (G_UNLIKELY (*s == '\\'))
1904 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1907 GValue value1 = { 0 };
1908 GValue value2 = { 0 };
1916 ret = gst_structure_parse_value (s, &s, &value1, type);
1920 while (g_ascii_isspace (*s))
1927 while (g_ascii_isspace (*s))
1930 ret = gst_structure_parse_value (s, &s, &value2, type);
1934 while (g_ascii_isspace (*s))
1941 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1944 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1945 range_type = GST_TYPE_DOUBLE_RANGE;
1946 g_value_init (value, range_type);
1947 gst_value_set_double_range (value,
1948 gst_g_value_get_double_unchecked (&value1),
1949 gst_g_value_get_double_unchecked (&value2));
1950 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1951 range_type = GST_TYPE_INT_RANGE;
1952 g_value_init (value, range_type);
1953 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
1954 gst_g_value_get_int_unchecked (&value2));
1955 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1956 range_type = GST_TYPE_INT64_RANGE;
1957 g_value_init (value, range_type);
1958 gst_value_set_int64_range (value, gst_g_value_get_int64_unchecked (&value1),
1959 gst_g_value_get_int64_unchecked (&value2));
1960 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1961 range_type = GST_TYPE_FRACTION_RANGE;
1962 g_value_init (value, range_type);
1963 gst_value_set_fraction_range (value, &value1, &value2);
1973 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1974 GType type, GType list_type, char begin, char end)
1976 GValue list_value = { 0 };
1980 g_value_init (value, list_type);
1981 array = g_value_peek_pointer (value);
1987 while (g_ascii_isspace (*s))
1995 ret = gst_structure_parse_value (s, &s, &list_value, type);
1999 g_array_append_val (array, list_value);
2001 while (g_ascii_isspace (*s))
2009 while (g_ascii_isspace (*s))
2012 memset (&list_value, 0, sizeof (list_value));
2013 ret = gst_structure_parse_value (s, &s, &list_value, type);
2017 g_array_append_val (array, list_value);
2018 while (g_ascii_isspace (*s))
2029 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2031 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2036 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2039 return gst_structure_parse_any_list (s, after, value, type,
2040 GST_TYPE_ARRAY, '<', '>');
2044 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2048 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2058 gst_structure_parse_field (gchar * str,
2059 gchar ** after, GstStructureField * field)
2068 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2071 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2072 GST_WARNING ("failed to parse simple string, str=%s", str);
2077 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2080 if (G_UNLIKELY (*s != '=')) {
2081 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2088 field->name = g_quark_from_string (name);
2089 GST_DEBUG ("trying field name '%s'", name);
2092 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2094 GST_WARNING ("failed to parse value %s", str);
2103 gst_structure_parse_value (gchar * str,
2104 gchar ** after, GValue * value, GType default_type)
2113 GType type = default_type;
2116 while (g_ascii_isspace (*s))
2119 /* check if there's a (type_name) 'cast' */
2123 while (g_ascii_isspace (*s))
2126 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2129 while (g_ascii_isspace (*s))
2131 if (G_UNLIKELY (*s != ')'))
2134 while (g_ascii_isspace (*s))
2139 type = gst_structure_gtype_from_abbr (type_name);
2140 GST_DEBUG ("trying type name '%s'", type_name);
2143 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2144 GST_WARNING ("invalid type");
2149 while (g_ascii_isspace (*s))
2152 ret = gst_structure_parse_range (s, &s, value, type);
2153 } else if (*s == '{') {
2154 ret = gst_structure_parse_list (s, &s, value, type);
2155 } else if (*s == '<') {
2156 ret = gst_structure_parse_array (s, &s, value, type);
2160 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2162 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2167 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2169 /* Set NULL terminator for deserialization */
2173 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2174 g_value_init (value, try_types[i]);
2175 ret = gst_value_deserialize (value, value_s);
2178 g_value_unset (value);
2181 g_value_init (value, type);
2183 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2184 (type != G_TYPE_STRING))))
2186 /* Set NULL terminator for deserialization */
2190 ret = gst_value_deserialize (value, value_s);
2191 if (G_UNLIKELY (!ret))
2192 g_value_unset (value);
2203 * gst_structure_from_string:
2204 * @string: a string representation of a #GstStructure.
2205 * @end: (out) (allow-none): pointer to store the end of the string in.
2207 * Creates a #GstStructure from a string representation.
2208 * If end is not NULL, a pointer to the place inside the given string
2209 * where parsing ended will be returned.
2211 * Free-function: gst_structure_free
2213 * Returns: (transfer full): a new #GstStructure or NULL when the string could
2214 * not be parsed. Free with gst_structure_free() after use.
2217 gst_structure_from_string (const gchar * string, gchar ** end)
2224 GstStructure *structure = NULL;
2225 GstStructureField field;
2227 g_return_val_if_fail (string != NULL, NULL);
2229 copy = g_strdup (string);
2232 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2233 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2234 && g_ascii_isspace (r[1]))))
2238 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2239 GST_WARNING ("Failed to parse structure string '%s'", string);
2245 structure = gst_structure_new_empty (name);
2248 if (G_UNLIKELY (structure == NULL))
2252 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2253 && g_ascii_isspace (r[1]))))
2256 /* end of structure, get the next char and finish */
2261 /* accept \0 as end delimiter */
2264 if (G_UNLIKELY (*r != ',')) {
2265 GST_WARNING ("Failed to find delimiter, r=%s", r);
2269 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2270 && g_ascii_isspace (r[1]))))
2273 memset (&field, 0, sizeof (field));
2274 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2275 GST_WARNING ("Failed to parse field, r=%s", r);
2278 gst_structure_set_field (structure, &field);
2282 *end = (char *) string + (r - copy);
2284 g_warning ("gst_structure_from_string did not consume whole string,"
2285 " but caller did not provide end pointer (\"%s\")", string);
2292 gst_structure_free (structure);
2298 gst_structure_transform_to_string (const GValue * src_value,
2299 GValue * dest_value)
2301 g_return_if_fail (src_value != NULL);
2302 g_return_if_fail (dest_value != NULL);
2304 dest_value->data[0].v_pointer =
2305 gst_structure_to_string (src_value->data[0].v_pointer);
2308 static GstStructure *
2309 gst_structure_copy_conditional (const GstStructure * structure)
2312 return gst_structure_copy (structure);
2316 /* fixate utility functions */
2319 * gst_structure_fixate_field_nearest_int:
2320 * @structure: a #GstStructure
2321 * @field_name: a field in @structure
2322 * @target: the target value of the fixation
2324 * Fixates a #GstStructure by changing the given field to the nearest
2325 * integer to @target that is a subset of the existing field.
2327 * Returns: TRUE if the structure could be fixated
2330 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2331 const char *field_name, int target)
2333 const GValue *value;
2335 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2336 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2338 value = gst_structure_get_value (structure, field_name);
2340 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2343 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2346 x = gst_value_get_int_range_min (value);
2349 x = gst_value_get_int_range_max (value);
2352 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2354 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2355 const GValue *list_value;
2358 int best_index = -1;
2360 n = gst_value_list_get_size (value);
2361 for (i = 0; i < n; i++) {
2362 list_value = gst_value_list_get_value (value, i);
2363 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2364 int x = gst_g_value_get_int_unchecked (list_value);
2366 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2372 if (best_index != -1) {
2373 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2383 * gst_structure_fixate_field_nearest_double:
2384 * @structure: a #GstStructure
2385 * @field_name: a field in @structure
2386 * @target: the target value of the fixation
2388 * Fixates a #GstStructure by changing the given field to the nearest
2389 * double to @target that is a subset of the existing field.
2391 * Returns: TRUE if the structure could be fixated
2394 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2395 const char *field_name, double target)
2397 const GValue *value;
2399 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2400 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2402 value = gst_structure_get_value (structure, field_name);
2404 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2407 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2410 x = gst_value_get_double_range_min (value);
2413 x = gst_value_get_double_range_max (value);
2416 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2418 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2419 const GValue *list_value;
2422 int best_index = -1;
2424 n = gst_value_list_get_size (value);
2425 for (i = 0; i < n; i++) {
2426 list_value = gst_value_list_get_value (value, i);
2427 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2428 double x = gst_g_value_get_double_unchecked (list_value);
2430 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2436 if (best_index != -1) {
2437 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2448 * gst_structure_fixate_field_boolean:
2449 * @structure: a #GstStructure
2450 * @field_name: a field in @structure
2451 * @target: the target value of the fixation
2453 * Fixates a #GstStructure by changing the given @field_name field to the given
2454 * @target boolean if that field is not fixed yet.
2456 * Returns: TRUE if the structure could be fixated
2459 gst_structure_fixate_field_boolean (GstStructure * structure,
2460 const char *field_name, gboolean target)
2462 const GValue *value;
2464 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2465 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2467 value = gst_structure_get_value (structure, field_name);
2469 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2472 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2473 const GValue *list_value;
2476 int best_index = -1;
2478 n = gst_value_list_get_size (value);
2479 for (i = 0; i < n; i++) {
2480 list_value = gst_value_list_get_value (value, i);
2481 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2482 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2484 if (best_index == -1 || x == target) {
2490 if (best_index != -1) {
2491 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2501 * gst_structure_fixate_field_string:
2502 * @structure: a #GstStructure
2503 * @field_name: a field in @structure
2504 * @target: the target value of the fixation
2506 * Fixates a #GstStructure by changing the given @field_name field to the given
2507 * @target string if that field is not fixed yet.
2509 * Returns: TRUE if the structure could be fixated
2514 gst_structure_fixate_field_string (GstStructure * structure,
2515 const gchar * field_name, const gchar * target)
2517 const GValue *value;
2519 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2520 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2522 value = gst_structure_get_value (structure, field_name);
2524 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2527 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2528 const GValue *list_value;
2530 const gchar *best = NULL;
2531 int best_index = -1;
2533 n = gst_value_list_get_size (value);
2534 for (i = 0; i < n; i++) {
2535 list_value = gst_value_list_get_value (value, i);
2536 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2537 const gchar *x = g_value_get_string (list_value);
2539 if (best_index == -1 || g_str_equal (x, target)) {
2545 if (best_index != -1) {
2546 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2556 * gst_structure_fixate_field_nearest_fraction:
2557 * @structure: a #GstStructure
2558 * @field_name: a field in @structure
2559 * @target_numerator: The numerator of the target value of the fixation
2560 * @target_denominator: The denominator of the target value of the fixation
2562 * Fixates a #GstStructure by changing the given field to the nearest
2563 * fraction to @target_numerator/@target_denominator that is a subset
2564 * of the existing field.
2566 * Returns: TRUE if the structure could be fixated
2569 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2570 const char *field_name, const gint target_numerator,
2571 const gint target_denominator)
2573 const GValue *value;
2575 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2576 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2578 value = gst_structure_get_value (structure, field_name);
2580 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2583 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2584 const GValue *x, *new_value;
2585 GValue target = { 0 };
2586 g_value_init (&target, GST_TYPE_FRACTION);
2587 gst_value_set_fraction (&target, target_numerator, target_denominator);
2589 new_value = ⌖
2590 x = gst_value_get_fraction_range_min (value);
2591 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2593 x = gst_value_get_fraction_range_max (value);
2594 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2597 gst_structure_set_value (structure, field_name, new_value);
2598 g_value_unset (&target);
2600 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2601 const GValue *list_value;
2603 const GValue *best = NULL;
2606 gdouble best_diff = G_MAXDOUBLE;
2608 target = (gdouble) target_numerator / (gdouble) target_denominator;
2610 GST_DEBUG ("target %g, best %g", target, best_diff);
2614 n = gst_value_list_get_size (value);
2615 for (i = 0; i < n; i++) {
2616 list_value = gst_value_list_get_value (value, i);
2617 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2619 gdouble list_double;
2621 num = gst_value_get_fraction_numerator (list_value);
2622 denom = gst_value_get_fraction_denominator (list_value);
2624 list_double = ((gdouble) num / (gdouble) denom);
2625 cur_diff = target - list_double;
2627 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2630 cur_diff = -cur_diff;
2632 if (!best || cur_diff < best_diff) {
2633 GST_DEBUG ("new best %g", list_double);
2635 best_diff = cur_diff;
2640 gst_structure_set_value (structure, field_name, best);
2649 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2651 GstStructure *s = data;
2654 if (gst_value_fixate (&v, value)) {
2655 gst_structure_id_set_value (s, field_id, &v);
2662 * gst_structure_fixate_field:
2663 * @structure: a #GstStructure
2664 * @field_name: a field in @structure
2666 * Fixates a #GstStructure by changing the given field with its fixated value.
2668 * Returns: TRUE if the structure field could be fixated
2671 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2673 GstStructureField *field;
2675 g_return_val_if_fail (structure != NULL, FALSE);
2676 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2678 if (!(field = gst_structure_get_field (structure, field_name)))
2681 return default_fixate (field->name, &field->value, structure);
2684 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2685 * (useful for message parsing functions where the return location is user
2686 * supplied and the user may pass NULL if the value isn't of interest) */
2687 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2689 const GValue *_value = (value); \
2690 guint _flags = (flags); \
2691 GType _value_type = G_VALUE_TYPE (_value); \
2692 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2693 gchar *_lcopy_format = _vtable->lcopy_format; \
2694 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2695 guint _n_values = 0; \
2697 while (*_lcopy_format != '\0') { \
2698 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2699 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2702 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2703 *(__error) = g_strdup_printf ("either all or none of the return " \
2704 "locations for field '%s' need to be NULL", fieldname); \
2705 } else if (_cvalues[0].v_pointer != NULL) { \
2706 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2711 * gst_structure_get_valist:
2712 * @structure: a #GstStructure
2713 * @first_fieldname: the name of the first field to read
2714 * @args: variable arguments
2716 * Parses the variable arguments and reads fields from @structure accordingly.
2717 * valist-variant of gst_structure_get(). Look at the documentation of
2718 * gst_structure_get() for more details.
2720 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2725 gst_structure_get_valist (const GstStructure * structure,
2726 const char *first_fieldname, va_list args)
2728 const char *field_name;
2729 GType expected_type = G_TYPE_INVALID;
2731 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2732 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2734 field_name = first_fieldname;
2735 while (field_name) {
2736 const GValue *val = NULL;
2739 expected_type = va_arg (args, GType);
2741 val = gst_structure_get_value (structure, field_name);
2746 if (G_VALUE_TYPE (val) != expected_type)
2749 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2751 g_warning ("%s: %s", G_STRFUNC, err);
2756 field_name = va_arg (args, const gchar *);
2764 GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2765 field_name, structure);
2770 GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2771 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2772 GST_STR_NULL (g_type_name (expected_type)),
2773 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2780 * gst_structure_id_get_valist:
2781 * @structure: a #GstStructure
2782 * @first_field_id: the quark of the first field to read
2783 * @args: variable arguments
2785 * Parses the variable arguments and reads fields from @structure accordingly.
2786 * valist-variant of gst_structure_id_get(). Look at the documentation of
2787 * gst_structure_id_get() for more details.
2789 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2794 gst_structure_id_get_valist (const GstStructure * structure,
2795 GQuark first_field_id, va_list args)
2798 GType expected_type = G_TYPE_INVALID;
2800 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2801 g_return_val_if_fail (first_field_id != 0, FALSE);
2803 field_id = first_field_id;
2805 const GValue *val = NULL;
2808 expected_type = va_arg (args, GType);
2810 val = gst_structure_id_get_value (structure, field_id);
2815 if (G_VALUE_TYPE (val) != expected_type)
2818 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2820 g_warning ("%s: %s", G_STRFUNC, err);
2825 field_id = va_arg (args, GQuark);
2833 GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2834 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2839 GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2840 "field was of type '%s': %" GST_PTR_FORMAT,
2841 g_quark_to_string (field_id),
2842 GST_STR_NULL (g_type_name (expected_type)),
2843 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2850 * gst_structure_get:
2851 * @structure: a #GstStructure
2852 * @first_fieldname: the name of the first field to read
2853 * @...: variable arguments
2855 * Parses the variable arguments and reads fields from @structure accordingly.
2856 * Variable arguments should be in the form field name, field type
2857 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2858 * The last variable argument should be NULL.
2860 * For refcounted (mini)objects you will acquire your own reference which
2861 * you must release with a suitable _unref() when no longer needed. For
2862 * strings and boxed types you will acquire a copy which you will need to
2863 * release with either g_free() or the suitable function for the boxed type.
2865 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2866 * because the field requested did not exist, or was of a type other
2867 * than the type specified), otherwise TRUE.
2872 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
2878 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2879 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2881 va_start (args, first_fieldname);
2882 ret = gst_structure_get_valist (structure, first_fieldname, args);
2889 * gst_structure_id_get:
2890 * @structure: a #GstStructure
2891 * @first_field_id: the quark of the first field to read
2892 * @...: variable arguments
2894 * Parses the variable arguments and reads fields from @structure accordingly.
2895 * Variable arguments should be in the form field id quark, field type
2896 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2897 * The last variable argument should be NULL (technically it should be a
2898 * 0 quark, but we require NULL so compilers that support it can check for
2899 * the NULL terminator and warn if it's not there).
2901 * This function is just like gst_structure_get() only that it is slightly
2902 * more efficient since it saves the string-to-quark lookup in the global
2905 * For refcounted (mini)objects you will acquire your own reference which
2906 * you must release with a suitable _unref() when no longer needed. For
2907 * strings and boxed types you will acquire a copy which you will need to
2908 * release with either g_free() or the suitable function for the boxed type.
2910 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2911 * because the field requested did not exist, or was of a type other
2912 * than the type specified), otherwise TRUE.
2917 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
2923 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2924 g_return_val_if_fail (first_field_id != 0, FALSE);
2926 va_start (args, first_field_id);
2927 ret = gst_structure_id_get_valist (structure, first_field_id, args);
2934 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
2937 const GstStructure *struct1 = (const GstStructure *) data;
2938 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
2940 if (G_UNLIKELY (val1 == NULL))
2942 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
2950 * gst_structure_is_equal:
2951 * @structure1: a #GstStructure.
2952 * @structure2: a #GstStructure.
2954 * Tests if the two #GstStructure are equal.
2956 * Returns: TRUE if the two structures have the same name and field.
2961 gst_structure_is_equal (const GstStructure * structure1,
2962 const GstStructure * structure2)
2964 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
2965 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
2967 if (G_UNLIKELY (structure1 == structure2))
2970 if (structure1->name != structure2->name) {
2973 if (GST_STRUCTURE_FIELDS (structure1)->len !=
2974 GST_STRUCTURE_FIELDS (structure2)->len) {
2978 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
2979 (gpointer) structure2);
2986 const GstStructure *intersect;
2991 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
2993 IntersectData *idata = (IntersectData *) data;
2994 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
2996 if (G_UNLIKELY (val2 == NULL)) {
2997 gst_structure_id_set_value (idata->dest, id, val1);
2999 GValue dest_value = { 0 };
3000 if (gst_value_intersect (&dest_value, val1, val2)) {
3001 gst_structure_id_set_value (idata->dest, id, &dest_value);
3002 g_value_unset (&dest_value);
3011 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
3013 IntersectData *idata = (IntersectData *) data;
3014 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3016 if (G_UNLIKELY (val2 == NULL)) {
3017 gst_structure_id_set_value (idata->dest, id, val1);
3023 * gst_structure_intersect:
3024 * @struct1: a #GstStructure
3025 * @struct2: a #GstStructure
3027 * Interesects @struct1 and @struct2 and returns the intersection.
3029 * Returns: Intersection of @struct1 and @struct2
3034 gst_structure_intersect (const GstStructure * struct1,
3035 const GstStructure * struct2)
3039 g_assert (struct1 != NULL);
3040 g_assert (struct2 != NULL);
3042 if (G_UNLIKELY (struct1->name != struct2->name))
3045 /* copy fields from struct1 which we have not in struct2 to target
3046 * intersect if we have the field in both */
3047 data.dest = gst_structure_new_id_empty (struct1->name);
3048 data.intersect = struct2;
3049 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3050 gst_structure_intersect_field1, &data)))
3053 /* copy fields from struct2 which we have not in struct1 to target */
3054 data.intersect = struct1;
3055 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3056 gst_structure_intersect_field2, &data)))
3062 gst_structure_free (data.dest);
3067 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3070 GstStructure *other = (GstStructure *) data;
3071 const GValue *val2 = gst_structure_id_get_value (other, id);
3073 if (G_LIKELY (val2)) {
3074 if (!gst_value_can_intersect (val1, val2)) {
3077 gint eq = gst_value_compare (val1, val2);
3079 if (eq == GST_VALUE_UNORDERED) {
3080 /* we need to try interseting */
3081 if (!gst_value_intersect (NULL, val1, val2)) {
3084 } else if (eq != GST_VALUE_EQUAL) {
3093 * gst_structure_can_intersect:
3094 * @struct1: a #GstStructure
3095 * @struct2: a #GstStructure
3097 * Tries intersecting @struct1 and @struct2 and reports whether the result
3098 * would not be empty.
3100 * Returns: %TRUE if intersection would not be empty
3105 gst_structure_can_intersect (const GstStructure * struct1,
3106 const GstStructure * struct2)
3108 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3109 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3111 if (G_UNLIKELY (struct1->name != struct2->name))
3114 /* tries to intersect if we have the field in both */
3115 return gst_structure_foreach ((GstStructure *) struct1,
3116 gst_caps_structure_can_intersect_field, (gpointer) struct2);
3120 gst_caps_structure_is_subset_field (GQuark field_id, const GValue * value,
3123 GstStructure *superset = user_data;
3124 const GValue *other;
3127 if (!(other = gst_structure_id_get_value (superset, field_id)))
3128 /* field is missing in the superset => is subset */
3131 comparison = gst_value_compare (other, value);
3133 /* equal values are subset */
3134 if (comparison == GST_VALUE_EQUAL)
3137 /* ordered, but unequal, values are not */
3138 if (comparison != GST_VALUE_UNORDERED)
3146 * -> 1 - [1,2] = empty
3150 * -> [1,2] - [1,3] = empty
3154 * -> {1,3} - {1,2} = 3
3157 * First caps subtraction needs to return a non-empty set, second
3158 * subtractions needs to give en empty set.
3159 * Both substractions are switched below, as it's faster that way.
3161 if (!gst_value_subtract (NULL, value, other)) {
3162 if (gst_value_subtract (NULL, other, value)) {
3170 * gst_structure_is_subset:
3171 * @subset: a #GstStructure
3172 * @superset: a potentially greater #GstStructure
3174 * Checks if @subset is a subset of @superset, i.e. has the same
3175 * structure name and for all fields that are existing in @superset,
3176 * @subset has a value that is a subset of the value in @superset.
3178 * Returns: %TRUE if @subset is a subset of @superset
3183 gst_structure_is_subset (const GstStructure * subset,
3184 const GstStructure * superset)
3186 if ((superset->name != subset->name) ||
3187 (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3190 return gst_structure_foreach ((GstStructure *) subset,
3191 gst_caps_structure_is_subset_field, (gpointer) superset);
3196 * gst_structure_fixate:
3197 * @structure: a #GstStructure
3199 * Fixate all values in @structure using gst_value_fixate().
3200 * @structure will be modified in-place and should be writable.
3203 gst_structure_fixate (GstStructure * structure)
3205 g_return_if_fail (GST_IS_STRUCTURE (structure));
3207 gst_structure_foreach (structure, default_fixate, structure);