2 * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
4 * gststructure.c: lists of { GQuark, GValue } tuples
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:gststructure
24 * @short_description: Generic structure containing fields of names and values
25 * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
27 * A #GstStructure is a collection of key/value pairs. The keys are expressed
28 * as GQuarks and the values can be of any GType.
30 * In addition to the key/value pairs, a #GstStructure also has a name. The name
31 * starts with a letter and can be filled by letters, numbers and any of "/-_.:".
33 * #GstStructure is used by various GStreamer subsystems to store information
34 * in a flexible and extensible way. A #GstStructure does not have a refcount
35 * because it usually is part of a higher level object such as #GstCaps,
36 * #GstMessage, #GstEvent, #GstQuery. It provides a means to enforce mutability
37 * using the refcount of the parent with the gst_structure_set_parent_refcount()
40 * A #GstStructure can be created with gst_structure_new_empty() or
41 * gst_structure_new(), which both take a name and an optional set of
42 * key/value pairs along with the types of the values.
44 * Field values can be changed with gst_structure_set_value() or
45 * gst_structure_set().
47 * Field values can be retrieved with gst_structure_get_value() or the more
48 * convenient gst_structure_get_*() functions.
50 * Fields can be removed with gst_structure_remove_field() or
51 * gst_structure_remove_fields().
53 * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are
54 * not allowed. Strings may be NULL however.
56 * Be aware that the current #GstCaps / #GstStructure serialization into string
57 * has limited support for nested #GstCaps / #GstStructure fields. It can only
58 * support one level of nesting. Using more levels will lead to unexpected
59 * behavior when using serialization features, such as gst_caps_to_string() or
60 * gst_value_serialize() and their counterparts.
62 * Last reviewed on 2012-03-29 (0.11.3)
71 #include "gst_private.h"
74 #include <gobject/gvaluecollector.h>
76 GST_DEBUG_CATEGORY_STATIC (gst_structure_debug);
77 #define GST_CAT_DEFAULT gst_structure_debug
79 typedef struct _GstStructureField GstStructureField;
81 struct _GstStructureField
91 /* owned by parent structure, NULL if no parent */
92 gint *parent_refcount;
97 #define GST_STRUCTURE_REFCOUNT(s) (((GstStructureImpl*)(s))->parent_refcount)
98 #define GST_STRUCTURE_FIELDS(s) (((GstStructureImpl*)(s))->fields)
100 #define GST_STRUCTURE_FIELD(structure, index) \
101 &g_array_index(GST_STRUCTURE_FIELDS(structure), GstStructureField, (index))
103 #define IS_MUTABLE(structure) \
104 (!GST_STRUCTURE_REFCOUNT(structure) || \
105 g_atomic_int_get (GST_STRUCTURE_REFCOUNT(structure)) == 1)
107 #define IS_TAGLIST(structure) \
108 (structure->name == GST_QUARK (TAGLIST))
110 static void gst_structure_set_field (GstStructure * structure,
111 GstStructureField * field);
112 static GstStructureField *gst_structure_get_field (const GstStructure *
113 structure, const gchar * fieldname);
114 static GstStructureField *gst_structure_id_get_field (const GstStructure *
115 structure, GQuark field);
116 static void gst_structure_transform_to_string (const GValue * src_value,
117 GValue * dest_value);
118 static GstStructure *gst_structure_copy_conditional (const GstStructure *
120 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
121 GValue * value, GType default_type);
122 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
124 GType _gst_structure_type = 0;
127 G_DEFINE_BOXED_TYPE (GstStructure, gst_structure,
128 gst_structure_copy_conditional, gst_structure_free);
131 _priv_gst_structure_initialize (void)
133 _gst_structure_type = gst_structure_get_type ();
135 g_value_register_transform_func (_gst_structure_type, G_TYPE_STRING,
136 gst_structure_transform_to_string);
138 GST_DEBUG_CATEGORY_INIT (gst_structure_debug, "structure", 0,
139 "GstStructure debug");
142 static GstStructure *
143 gst_structure_new_id_empty_with_size (GQuark quark, guint prealloc)
145 GstStructureImpl *structure;
147 structure = g_slice_new (GstStructureImpl);
148 ((GstStructure *) structure)->type = _gst_structure_type;
149 ((GstStructure *) structure)->name = quark;
150 GST_STRUCTURE_REFCOUNT (structure) = NULL;
151 GST_STRUCTURE_FIELDS (structure) =
152 g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
154 GST_TRACE ("created structure %p", structure);
156 return GST_STRUCTURE_CAST (structure);
160 * gst_structure_new_id_empty:
161 * @quark: name of new structure
163 * Creates a new, empty #GstStructure with the given name as a GQuark.
165 * Free-function: gst_structure_free
167 * Returns: (transfer full): a new, empty #GstStructure
170 gst_structure_new_id_empty (GQuark quark)
172 g_return_val_if_fail (quark != 0, NULL);
174 return gst_structure_new_id_empty_with_size (quark, 0);
177 #ifndef G_DISABLE_CHECKS
179 gst_structure_validate_name (const gchar * name)
183 g_return_val_if_fail (name != NULL, FALSE);
185 if (G_UNLIKELY (!g_ascii_isalpha (*name))) {
186 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
191 /* FIXME: test name string more */
193 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
195 if (G_UNLIKELY (*s != '\0')) {
196 GST_WARNING ("Invalid character '%c' at offset %" G_GUINTPTR_FORMAT " in"
197 " structure name: %s", *s, ((guintptr) s - (guintptr) name), name);
201 if (strncmp (name, "video/x-raw-", 12) == 0) {
202 g_warning ("0.10-style raw video caps are being created. Should be "
203 "video/x-raw,format=(string).. now.");
204 } else if (strncmp (name, "audio/x-raw-", 12) == 0) {
205 g_warning ("0.10-style raw audio caps are being created. Should be "
206 "audio/x-raw,format=(string).. now.");
214 * gst_structure_new_empty:
215 * @name: name of new structure
217 * Creates a new, empty #GstStructure with the given @name.
219 * See gst_structure_set_name() for constraints on the @name parameter.
221 * Free-function: gst_structure_free
223 * Returns: (transfer full): a new, empty #GstStructure
226 gst_structure_new_empty (const gchar * name)
228 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
230 return gst_structure_new_id_empty_with_size (g_quark_from_string (name), 0);
235 * @name: name of new structure
236 * @firstfield: name of first field to set
237 * @...: additional arguments
239 * Creates a new #GstStructure with the given name. Parses the
240 * list of variable arguments and sets fields to the values listed.
241 * Variable arguments should be passed as field name, field type,
242 * and value. Last variable argument should be NULL.
244 * Free-function: gst_structure_free
246 * Returns: (transfer full): a new #GstStructure
249 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
251 GstStructure *structure;
254 va_start (varargs, firstfield);
255 structure = gst_structure_new_valist (name, firstfield, varargs);
262 * gst_structure_new_valist:
263 * @name: name of new structure
264 * @firstfield: name of first field to set
265 * @varargs: variable argument list
267 * Creates a new #GstStructure with the given @name. Structure fields
268 * are set according to the varargs in a manner similar to
269 * gst_structure_new().
271 * See gst_structure_set_name() for constraints on the @name parameter.
273 * Free-function: gst_structure_free
275 * Returns: (transfer full): a new #GstStructure
278 gst_structure_new_valist (const gchar * name,
279 const gchar * firstfield, va_list varargs)
281 GstStructure *structure;
283 structure = gst_structure_new_empty (name);
286 gst_structure_set_valist (structure, firstfield, varargs);
292 * gst_structure_set_parent_refcount:
293 * @structure: a #GstStructure
294 * @refcount: (in): a pointer to the parent's refcount
296 * Sets the parent_refcount field of #GstStructure. This field is used to
297 * determine whether a structure is mutable or not. This function should only be
298 * called by code implementing parent objects of #GstStructure, as described in
299 * the MT Refcounting section of the design documents.
301 * Returns: %TRUE if the parent refcount could be set.
304 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
306 g_return_val_if_fail (structure != NULL, FALSE);
308 /* if we have a parent_refcount already, we can only clear
309 * if with a NULL refcount */
310 if (GST_STRUCTURE_REFCOUNT (structure)) {
311 if (refcount != NULL) {
312 g_return_val_if_fail (refcount == NULL, FALSE);
316 if (refcount == NULL) {
317 g_return_val_if_fail (refcount != NULL, FALSE);
322 GST_STRUCTURE_REFCOUNT (structure) = refcount;
328 * gst_structure_copy:
329 * @structure: a #GstStructure to duplicate
331 * Duplicates a #GstStructure and all its fields and values.
333 * Free-function: gst_structure_free
335 * Returns: (transfer full): a new #GstStructure.
338 gst_structure_copy (const GstStructure * structure)
340 GstStructure *new_structure;
341 GstStructureField *field;
344 g_return_val_if_fail (structure != NULL, NULL);
346 len = GST_STRUCTURE_FIELDS (structure)->len;
347 new_structure = gst_structure_new_id_empty_with_size (structure->name, len);
349 for (i = 0; i < len; i++) {
350 GstStructureField new_field = { 0 };
352 field = GST_STRUCTURE_FIELD (structure, i);
354 new_field.name = field->name;
355 gst_value_init_and_copy (&new_field.value, &field->value);
356 g_array_append_val (GST_STRUCTURE_FIELDS (new_structure), new_field);
358 GST_CAT_TRACE (GST_CAT_PERFORMANCE, "doing copy %p -> %p",
359 structure, new_structure);
361 return new_structure;
365 * gst_structure_free:
366 * @structure: (in) (transfer full): the #GstStructure to free
368 * Frees a #GstStructure and all its fields and values. The structure must not
369 * have a parent when this function is called.
372 gst_structure_free (GstStructure * structure)
374 GstStructureField *field;
377 g_return_if_fail (structure != NULL);
378 g_return_if_fail (GST_STRUCTURE_REFCOUNT (structure) == NULL);
380 len = GST_STRUCTURE_FIELDS (structure)->len;
381 for (i = 0; i < len; i++) {
382 field = GST_STRUCTURE_FIELD (structure, i);
384 if (G_IS_VALUE (&field->value)) {
385 g_value_unset (&field->value);
388 g_array_free (GST_STRUCTURE_FIELDS (structure), TRUE);
390 memset (structure, 0xff, sizeof (GstStructure));
392 GST_TRACE ("free structure %p", structure);
394 g_slice_free1 (sizeof (GstStructureImpl), structure);
398 * gst_structure_get_name:
399 * @structure: a #GstStructure
401 * Get the name of @structure as a string.
403 * Returns: the name of the structure.
406 gst_structure_get_name (const GstStructure * structure)
408 g_return_val_if_fail (structure != NULL, NULL);
410 return g_quark_to_string (structure->name);
414 * gst_structure_has_name:
415 * @structure: a #GstStructure
416 * @name: structure name to check for
418 * Checks if the structure has the given name
420 * Returns: TRUE if @name matches the name of the structure.
423 gst_structure_has_name (const GstStructure * structure, const gchar * name)
425 const gchar *structure_name;
427 g_return_val_if_fail (structure != NULL, FALSE);
428 g_return_val_if_fail (name != NULL, FALSE);
430 /* getting the string is cheap and comparing short strings is too
431 * should be faster than getting the quark for name and comparing the quarks
433 structure_name = g_quark_to_string (structure->name);
435 return (structure_name && strcmp (structure_name, name) == 0);
439 * gst_structure_get_name_id:
440 * @structure: a #GstStructure
442 * Get the name of @structure as a GQuark.
444 * Returns: the quark representing the name of the structure.
447 gst_structure_get_name_id (const GstStructure * structure)
449 g_return_val_if_fail (structure != NULL, 0);
451 return structure->name;
455 * gst_structure_set_name:
456 * @structure: a #GstStructure
457 * @name: the new name of the structure
459 * Sets the name of the structure to the given @name. The string
460 * provided is copied before being used. It must not be empty, start with a
461 * letter and can be followed by letters, numbers and any of "/-_.:".
464 gst_structure_set_name (GstStructure * structure, const gchar * name)
466 g_return_if_fail (structure != NULL);
467 g_return_if_fail (IS_MUTABLE (structure));
468 g_return_if_fail (gst_structure_validate_name (name));
470 structure->name = g_quark_from_string (name);
474 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
475 const GValue * value)
477 GstStructureField gsfield = { 0, {0,} };
479 gsfield.name = field;
480 gst_value_init_and_copy (&gsfield.value, value);
482 gst_structure_set_field (structure, &gsfield);
486 * gst_structure_id_set_value:
487 * @structure: a #GstStructure
488 * @field: a #GQuark representing a field
489 * @value: the new value of the field
491 * Sets the field with the given GQuark @field to @value. If the field
492 * does not exist, it is created. If the field exists, the previous
493 * value is replaced and freed.
496 gst_structure_id_set_value (GstStructure * structure,
497 GQuark field, const GValue * value)
500 g_return_if_fail (structure != NULL);
501 g_return_if_fail (G_IS_VALUE (value));
502 g_return_if_fail (IS_MUTABLE (structure));
504 gst_structure_id_set_value_internal (structure, field, value);
508 * gst_structure_set_value:
509 * @structure: a #GstStructure
510 * @fieldname: the name of the field to set
511 * @value: the new value of the field
513 * Sets the field with the given name @field to @value. If the field
514 * does not exist, it is created. If the field exists, the previous
515 * value is replaced and freed.
518 gst_structure_set_value (GstStructure * structure,
519 const gchar * fieldname, const GValue * value)
521 g_return_if_fail (structure != NULL);
522 g_return_if_fail (fieldname != NULL);
523 g_return_if_fail (G_IS_VALUE (value));
524 g_return_if_fail (IS_MUTABLE (structure));
526 gst_structure_id_set_value_internal (structure,
527 g_quark_from_string (fieldname), value);
531 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
534 GstStructureField gsfield = { 0, {0,} };
536 gsfield.name = field;
537 gsfield.value = *value;
539 gst_structure_set_field (structure, &gsfield);
541 /* we took ownership */
543 memset (value, 0, sizeof (GValue));
545 value->g_type = G_TYPE_INVALID;
550 * gst_structure_id_take_value:
551 * @structure: a #GstStructure
552 * @field: a #GQuark representing a field
553 * @value: (transfer full): the new value of the field
555 * Sets the field with the given GQuark @field to @value. If the field
556 * does not exist, it is created. If the field exists, the previous
557 * value is replaced and freed.
560 gst_structure_id_take_value (GstStructure * structure, GQuark field,
563 g_return_if_fail (structure != NULL);
564 g_return_if_fail (G_IS_VALUE (value));
565 g_return_if_fail (IS_MUTABLE (structure));
567 gst_structure_id_take_value_internal (structure, field, value);
571 * gst_structure_take_value:
572 * @structure: a #GstStructure
573 * @fieldname: the name of the field to set
574 * @value: (transfer full): the new value of the field
576 * Sets the field with the given name @field to @value. If the field
577 * does not exist, it is created. If the field exists, the previous
578 * value is replaced and freed. The function will take ownership of @value.
581 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
584 g_return_if_fail (structure != NULL);
585 g_return_if_fail (fieldname != NULL);
586 g_return_if_fail (G_IS_VALUE (value));
587 g_return_if_fail (IS_MUTABLE (structure));
589 gst_structure_id_take_value_internal (structure,
590 g_quark_from_string (fieldname), value);
594 gst_structure_set_valist_internal (GstStructure * structure,
595 const gchar * fieldname, va_list varargs)
601 GstStructureField field = { 0 };
603 field.name = g_quark_from_string (fieldname);
605 type = va_arg (varargs, GType);
607 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
608 if (G_UNLIKELY (err)) {
609 g_critical ("%s", err);
612 gst_structure_set_field (structure, &field);
614 fieldname = va_arg (varargs, gchar *);
620 * @structure: a #GstStructure
621 * @fieldname: the name of the field to set
622 * @...: variable arguments
624 * Parses the variable arguments and sets fields accordingly.
625 * Variable arguments should be in the form field name, field type
626 * (as a GType), value(s). The last variable argument should be NULL.
629 gst_structure_set (GstStructure * structure, const gchar * field, ...)
633 g_return_if_fail (structure != NULL);
634 g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
636 va_start (varargs, field);
637 gst_structure_set_valist_internal (structure, field, varargs);
642 * gst_structure_set_valist:
643 * @structure: a #GstStructure
644 * @fieldname: the name of the field to set
645 * @varargs: variable arguments
647 * va_list form of gst_structure_set().
650 gst_structure_set_valist (GstStructure * structure,
651 const gchar * fieldname, va_list varargs)
653 g_return_if_fail (structure != NULL);
654 g_return_if_fail (IS_MUTABLE (structure));
656 gst_structure_set_valist_internal (structure, fieldname, varargs);
660 gst_structure_id_set_valist_internal (GstStructure * structure,
661 GQuark fieldname, va_list varargs)
667 GstStructureField field = { 0 };
669 field.name = fieldname;
671 type = va_arg (varargs, GType);
673 #ifndef G_VALUE_COLLECT_INIT
674 g_value_init (&field.value, type);
675 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
677 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
679 if (G_UNLIKELY (err)) {
680 g_critical ("%s", err);
683 gst_structure_set_field (structure, &field);
685 fieldname = va_arg (varargs, GQuark);
690 * gst_structure_id_set:
691 * @structure: a #GstStructure
692 * @fieldname: the GQuark for the name of the field to set
693 * @...: variable arguments
695 * Identical to gst_structure_set, except that field names are
696 * passed using the GQuark for the field name. This allows more efficient
697 * setting of the structure if the caller already knows the associated
699 * The last variable argument must be NULL.
702 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
706 g_return_if_fail (structure != NULL);
708 va_start (varargs, field);
709 gst_structure_id_set_valist_internal (structure, field, varargs);
714 * gst_structure_id_set_valist:
715 * @structure: a #GstStructure
716 * @fieldname: the name of the field to set
717 * @varargs: variable arguments
719 * va_list form of gst_structure_id_set().
722 gst_structure_id_set_valist (GstStructure * structure,
723 GQuark fieldname, va_list varargs)
725 g_return_if_fail (structure != NULL);
726 g_return_if_fail (IS_MUTABLE (structure));
728 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
732 * gst_structure_new_id:
733 * @name_quark: name of new structure
734 * @field_quark: the GQuark for the name of the field to set
735 * @...: variable arguments
737 * Creates a new #GstStructure with the given name as a GQuark, followed by
738 * fieldname quark, GType, argument(s) "triplets" in the same format as
739 * gst_structure_id_set(). Basically a convenience wrapper around
740 * gst_structure_new_id_empty() and gst_structure_id_set().
742 * The last variable argument must be NULL (or 0).
744 * Free-function: gst_structure_free
746 * Returns: (transfer full): a new #GstStructure
749 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
754 g_return_val_if_fail (name_quark != 0, NULL);
755 g_return_val_if_fail (field_quark != 0, NULL);
757 s = gst_structure_new_id_empty (name_quark);
759 va_start (varargs, field_quark);
760 gst_structure_id_set_valist_internal (s, field_quark, varargs);
766 #if GST_VERSION_NANO == 1
767 #define GIT_G_WARNING g_warning
769 #define GIT_G_WARNING GST_WARNING
772 /* If the structure currently contains a field with the same name, it is
773 * replaced with the provided field. Otherwise, the field is added to the
774 * structure. The field's value is not deeply copied.
777 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
779 GstStructureField *f;
780 guint i, len = GST_STRUCTURE_FIELDS (structure)->len;
782 if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
785 s = g_value_get_string (&field->value);
786 /* only check for NULL strings in taglists, as they are allowed in message
787 * structs, e.g. error message debug strings */
788 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
790 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
791 "Please file a bug.", g_quark_to_string (field->name));
792 g_value_unset (&field->value);
795 /* empty strings never make sense */
796 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
797 "Please file a bug.", g_quark_to_string (field->name));
798 g_value_unset (&field->value);
801 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
802 g_warning ("Trying to set string on %s field '%s', but string is not "
803 "valid UTF-8. Please file a bug.",
804 IS_TAGLIST (structure) ? "taglist" : "structure",
805 g_quark_to_string (field->name));
806 g_value_unset (&field->value);
809 } else if (G_UNLIKELY (G_VALUE_HOLDS (&field->value, G_TYPE_DATE))) {
812 d = g_value_get_boxed (&field->value);
813 /* only check for NULL GDates in taglists, as they might make sense
814 * in other, generic structs */
815 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
816 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
817 "Please file a bug.", g_quark_to_string (field->name));
818 g_value_unset (&field->value);
820 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
822 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
823 IS_TAGLIST (structure) ? "taglist" : "structure",
824 g_quark_to_string (field->name));
825 g_value_unset (&field->value);
830 for (i = 0; i < len; i++) {
831 f = GST_STRUCTURE_FIELD (structure, i);
833 if (G_UNLIKELY (f->name == field->name)) {
834 g_value_unset (&f->value);
835 memcpy (f, field, sizeof (GstStructureField));
840 g_array_append_val (GST_STRUCTURE_FIELDS (structure), *field);
843 /* If there is no field with the given ID, NULL is returned.
845 static GstStructureField *
846 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
848 GstStructureField *field;
851 len = GST_STRUCTURE_FIELDS (structure)->len;
853 for (i = 0; i < len; i++) {
854 field = GST_STRUCTURE_FIELD (structure, i);
856 if (G_UNLIKELY (field->name == field_id))
863 /* If there is no field with the given ID, NULL is returned.
865 static GstStructureField *
866 gst_structure_get_field (const GstStructure * structure,
867 const gchar * fieldname)
869 g_return_val_if_fail (structure != NULL, NULL);
870 g_return_val_if_fail (fieldname != NULL, NULL);
872 return gst_structure_id_get_field (structure,
873 g_quark_from_string (fieldname));
877 * gst_structure_get_value:
878 * @structure: a #GstStructure
879 * @fieldname: the name of the field to get
881 * Get the value of the field with name @fieldname.
883 * Returns: the #GValue corresponding to the field with the given name.
886 gst_structure_get_value (const GstStructure * structure,
887 const gchar * fieldname)
889 GstStructureField *field;
891 g_return_val_if_fail (structure != NULL, NULL);
892 g_return_val_if_fail (fieldname != NULL, NULL);
894 field = gst_structure_get_field (structure, fieldname);
898 return &field->value;
902 * gst_structure_id_get_value:
903 * @structure: a #GstStructure
904 * @field: the #GQuark of the field to get
906 * Get the value of the field with GQuark @field.
908 * Returns: the #GValue corresponding to the field with the given name
912 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
914 GstStructureField *gsfield;
916 g_return_val_if_fail (structure != NULL, NULL);
918 gsfield = gst_structure_id_get_field (structure, field);
922 return &gsfield->value;
926 * gst_structure_remove_field:
927 * @structure: a #GstStructure
928 * @fieldname: the name of the field to remove
930 * Removes the field with the given name. If the field with the given
931 * name does not exist, the structure is unchanged.
934 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
936 GstStructureField *field;
940 g_return_if_fail (structure != NULL);
941 g_return_if_fail (fieldname != NULL);
942 g_return_if_fail (IS_MUTABLE (structure));
944 id = g_quark_from_string (fieldname);
945 len = GST_STRUCTURE_FIELDS (structure)->len;
947 for (i = 0; i < len; i++) {
948 field = GST_STRUCTURE_FIELD (structure, i);
950 if (field->name == id) {
951 if (G_IS_VALUE (&field->value)) {
952 g_value_unset (&field->value);
954 GST_STRUCTURE_FIELDS (structure) =
955 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
962 * gst_structure_remove_fields:
963 * @structure: a #GstStructure
964 * @fieldname: the name of the field to remove
965 * @...: NULL-terminated list of more fieldnames to remove
967 * Removes the fields with the given names. If a field does not exist, the
968 * argument is ignored.
971 gst_structure_remove_fields (GstStructure * structure,
972 const gchar * fieldname, ...)
976 g_return_if_fail (structure != NULL);
977 g_return_if_fail (fieldname != NULL);
978 /* mutability checked in remove_field */
980 va_start (varargs, fieldname);
981 gst_structure_remove_fields_valist (structure, fieldname, varargs);
986 * gst_structure_remove_fields_valist:
987 * @structure: a #GstStructure
988 * @fieldname: the name of the field to remove
989 * @varargs: NULL-terminated list of more fieldnames to remove
991 * va_list form of gst_structure_remove_fields().
994 gst_structure_remove_fields_valist (GstStructure * structure,
995 const gchar * fieldname, va_list varargs)
997 gchar *field = (gchar *) fieldname;
999 g_return_if_fail (structure != NULL);
1000 g_return_if_fail (fieldname != NULL);
1001 /* mutability checked in remove_field */
1004 gst_structure_remove_field (structure, field);
1005 field = va_arg (varargs, char *);
1010 * gst_structure_remove_all_fields:
1011 * @structure: a #GstStructure
1013 * Removes all fields in a GstStructure.
1016 gst_structure_remove_all_fields (GstStructure * structure)
1018 GstStructureField *field;
1021 g_return_if_fail (structure != NULL);
1022 g_return_if_fail (IS_MUTABLE (structure));
1024 for (i = GST_STRUCTURE_FIELDS (structure)->len - 1; i >= 0; i--) {
1025 field = GST_STRUCTURE_FIELD (structure, i);
1027 if (G_IS_VALUE (&field->value)) {
1028 g_value_unset (&field->value);
1030 GST_STRUCTURE_FIELDS (structure) =
1031 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1036 * gst_structure_get_field_type:
1037 * @structure: a #GstStructure
1038 * @fieldname: the name of the field
1040 * Finds the field with the given name, and returns the type of the
1041 * value it contains. If the field is not found, G_TYPE_INVALID is
1044 * Returns: the #GValue of the field
1047 gst_structure_get_field_type (const GstStructure * structure,
1048 const gchar * fieldname)
1050 GstStructureField *field;
1052 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1053 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1055 field = gst_structure_get_field (structure, fieldname);
1057 return G_TYPE_INVALID;
1059 return G_VALUE_TYPE (&field->value);
1063 * gst_structure_n_fields:
1064 * @structure: a #GstStructure
1066 * Get the number of fields in the structure.
1068 * Returns: the number of fields in the structure
1071 gst_structure_n_fields (const GstStructure * structure)
1073 g_return_val_if_fail (structure != NULL, 0);
1075 return GST_STRUCTURE_FIELDS (structure)->len;
1079 * gst_structure_nth_field_name:
1080 * @structure: a #GstStructure
1081 * @index: the index to get the name of
1083 * Get the name of the given field number, counting from 0 onwards.
1085 * Returns: the name of the given field number
1088 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1090 GstStructureField *field;
1092 g_return_val_if_fail (structure != NULL, NULL);
1093 g_return_val_if_fail (index < GST_STRUCTURE_FIELDS (structure)->len, NULL);
1095 field = GST_STRUCTURE_FIELD (structure, index);
1097 return g_quark_to_string (field->name);
1101 * gst_structure_foreach:
1102 * @structure: a #GstStructure
1103 * @func: (scope call): a function to call for each field
1104 * @user_data: (closure): private data
1106 * Calls the provided function once for each field in the #GstStructure. The
1107 * function must not modify the fields. Also see gst_structure_map_in_place().
1109 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1113 gst_structure_foreach (const GstStructure * structure,
1114 GstStructureForeachFunc func, gpointer user_data)
1117 GstStructureField *field;
1120 g_return_val_if_fail (structure != NULL, FALSE);
1121 g_return_val_if_fail (func != NULL, FALSE);
1123 len = GST_STRUCTURE_FIELDS (structure)->len;
1125 for (i = 0; i < len; i++) {
1126 field = GST_STRUCTURE_FIELD (structure, i);
1128 ret = func (field->name, &field->value, user_data);
1129 if (G_UNLIKELY (!ret))
1137 * gst_structure_map_in_place:
1138 * @structure: a #GstStructure
1139 * @func: (scope call): a function to call for each field
1140 * @user_data: (closure): private data
1142 * Calls the provided function once for each field in the #GstStructure. In
1143 * contrast to gst_structure_foreach(), the function may modify but not delete the
1144 * fields. The structure must be mutable.
1146 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1150 gst_structure_map_in_place (GstStructure * structure,
1151 GstStructureMapFunc func, gpointer user_data)
1154 GstStructureField *field;
1157 g_return_val_if_fail (structure != NULL, FALSE);
1158 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1159 g_return_val_if_fail (func != NULL, FALSE);
1160 len = GST_STRUCTURE_FIELDS (structure)->len;
1162 for (i = 0; i < len; i++) {
1163 field = GST_STRUCTURE_FIELD (structure, i);
1165 ret = func (field->name, &field->value, user_data);
1174 * gst_structure_id_has_field:
1175 * @structure: a #GstStructure
1176 * @field: #GQuark of the field name
1178 * Check if @structure contains a field named @field.
1180 * Returns: TRUE if the structure contains a field with the given name
1183 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1185 GstStructureField *f;
1187 g_return_val_if_fail (structure != NULL, FALSE);
1188 g_return_val_if_fail (field != 0, FALSE);
1190 f = gst_structure_id_get_field (structure, field);
1196 * gst_structure_has_field:
1197 * @structure: a #GstStructure
1198 * @fieldname: the name of a field
1200 * Check if @structure contains a field named @fieldname.
1202 * Returns: TRUE if the structure contains a field with the given name
1205 gst_structure_has_field (const GstStructure * structure,
1206 const gchar * fieldname)
1208 g_return_val_if_fail (structure != NULL, FALSE);
1209 g_return_val_if_fail (fieldname != NULL, FALSE);
1211 return gst_structure_id_has_field (structure,
1212 g_quark_from_string (fieldname));
1216 * gst_structure_id_has_field_typed:
1217 * @structure: a #GstStructure
1218 * @field: #GQuark of the field name
1219 * @type: the type of a value
1221 * Check if @structure contains a field named @field and with GType @type.
1223 * Returns: TRUE if the structure contains a field with the given name and type
1226 gst_structure_id_has_field_typed (const GstStructure * structure,
1227 GQuark field, GType type)
1229 GstStructureField *f;
1231 g_return_val_if_fail (structure != NULL, FALSE);
1232 g_return_val_if_fail (field != 0, FALSE);
1234 f = gst_structure_id_get_field (structure, field);
1238 return (G_VALUE_TYPE (&f->value) == type);
1242 * gst_structure_has_field_typed:
1243 * @structure: a #GstStructure
1244 * @fieldname: the name of a field
1245 * @type: the type of a value
1247 * Check if @structure contains a field named @fieldname and with GType @type.
1249 * Returns: TRUE if the structure contains a field with the given name and type
1252 gst_structure_has_field_typed (const GstStructure * structure,
1253 const gchar * fieldname, GType type)
1255 g_return_val_if_fail (structure != NULL, FALSE);
1256 g_return_val_if_fail (fieldname != NULL, FALSE);
1258 return gst_structure_id_has_field_typed (structure,
1259 g_quark_from_string (fieldname), type);
1262 /* utility functions */
1265 * gst_structure_get_boolean:
1266 * @structure: a #GstStructure
1267 * @fieldname: the name of a field
1268 * @value: (out): a pointer to a #gboolean to set
1270 * Sets the boolean pointed to by @value corresponding to the value of the
1271 * given field. Caller is responsible for making sure the field exists
1272 * and has the correct type.
1274 * Returns: TRUE if the value could be set correctly. If there was no field
1275 * with @fieldname or the existing field did not contain a boolean, this
1276 * function returns FALSE.
1279 gst_structure_get_boolean (const GstStructure * structure,
1280 const gchar * fieldname, gboolean * value)
1282 GstStructureField *field;
1284 g_return_val_if_fail (structure != NULL, FALSE);
1285 g_return_val_if_fail (fieldname != NULL, FALSE);
1287 field = gst_structure_get_field (structure, fieldname);
1291 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1294 *value = gst_g_value_get_boolean_unchecked (&field->value);
1300 * gst_structure_get_int:
1301 * @structure: a #GstStructure
1302 * @fieldname: the name of a field
1303 * @value: (out): a pointer to an int to set
1305 * Sets the int pointed to by @value corresponding to the value of the
1306 * given field. Caller is responsible for making sure the field exists
1307 * and has the correct type.
1309 * Returns: %TRUE if the value could be set correctly. If there was no field
1310 * with @fieldname or the existing field did not contain an int, this function
1314 gst_structure_get_int (const GstStructure * structure,
1315 const gchar * fieldname, gint * value)
1317 GstStructureField *field;
1319 g_return_val_if_fail (structure != NULL, FALSE);
1320 g_return_val_if_fail (fieldname != NULL, FALSE);
1321 g_return_val_if_fail (value != NULL, FALSE);
1323 field = gst_structure_get_field (structure, fieldname);
1327 if (!G_VALUE_HOLDS_INT (&field->value))
1330 *value = gst_g_value_get_int_unchecked (&field->value);
1336 * gst_structure_get_uint:
1337 * @structure: a #GstStructure
1338 * @fieldname: the name of a field
1339 * @value: (out): a pointer to a uint to set
1341 * Sets the uint pointed to by @value corresponding to the value of the
1342 * given field. Caller is responsible for making sure the field exists
1343 * and has the correct type.
1345 * Returns: %TRUE if the value could be set correctly. If there was no field
1346 * with @fieldname or the existing field did not contain a uint, this function
1350 gst_structure_get_uint (const GstStructure * structure,
1351 const gchar * fieldname, guint * value)
1353 GstStructureField *field;
1355 g_return_val_if_fail (structure != NULL, FALSE);
1356 g_return_val_if_fail (fieldname != NULL, FALSE);
1357 g_return_val_if_fail (value != NULL, FALSE);
1359 field = gst_structure_get_field (structure, fieldname);
1363 if (!G_VALUE_HOLDS_UINT (&field->value))
1366 *value = gst_g_value_get_uint_unchecked (&field->value);
1372 * gst_structure_get_int64:
1373 * @structure: a #GstStructure
1374 * @fieldname: the name of a field
1375 * @value: (out): a pointer to an int64 to set
1377 * Sets the int64 pointed to by @value corresponding to the value of the
1378 * given field. Caller is responsible for making sure the field exists
1379 * and has the correct type.
1381 * Returns: %TRUE if the value could be set correctly. If there was no field
1382 * with @fieldname or the existing field did not contain an int64, this function
1388 gst_structure_get_int64 (const GstStructure * structure,
1389 const gchar * fieldname, gint64 * value)
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 (!G_VALUE_HOLDS_INT64 (&field->value))
1404 *value = gst_g_value_get_int64_unchecked (&field->value);
1410 * gst_structure_get_uint64:
1411 * @structure: a #GstStructure
1412 * @fieldname: the name of a field
1413 * @value: (out): a pointer to a uint64 to set
1415 * Sets the uint64 pointed to by @value corresponding to the value of the
1416 * given field. Caller is responsible for making sure the field exists
1417 * and has the correct type.
1419 * Returns: %TRUE if the value could be set correctly. If there was no field
1420 * with @fieldname or the existing field did not contain a uint64, this function
1426 gst_structure_get_uint64 (const GstStructure * structure,
1427 const gchar * fieldname, guint64 * value)
1429 GstStructureField *field;
1431 g_return_val_if_fail (structure != NULL, FALSE);
1432 g_return_val_if_fail (fieldname != NULL, FALSE);
1433 g_return_val_if_fail (value != NULL, FALSE);
1435 field = gst_structure_get_field (structure, fieldname);
1439 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1442 *value = gst_g_value_get_uint64_unchecked (&field->value);
1448 * gst_structure_get_date:
1449 * @structure: a #GstStructure
1450 * @fieldname: the name of a field
1451 * @value: (out callee-allocates): a pointer to a #GDate to set
1453 * Sets the date pointed to by @value corresponding to the date of the
1454 * given field. Caller is responsible for making sure the field exists
1455 * and has the correct type.
1457 * On success @value will point to a newly-allocated copy of the date which
1458 * should be freed with g_date_free() when no longer needed (note: this is
1459 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1460 * copy of the string).
1462 * Returns: TRUE if the value could be set correctly. If there was no field
1463 * with @fieldname or the existing field did not contain a data, this function
1467 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1470 GstStructureField *field;
1472 g_return_val_if_fail (structure != NULL, FALSE);
1473 g_return_val_if_fail (fieldname != NULL, FALSE);
1474 g_return_val_if_fail (value != NULL, FALSE);
1476 field = gst_structure_get_field (structure, fieldname);
1480 if (!G_VALUE_HOLDS (&field->value, G_TYPE_DATE))
1483 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1484 *value = g_value_dup_boxed (&field->value);
1490 * gst_structure_get_date_time:
1491 * @structure: a #GstStructure
1492 * @fieldname: the name of a field
1493 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1495 * Sets the datetime pointed to by @value corresponding to the datetime of the
1496 * given field. Caller is responsible for making sure the field exists
1497 * and has the correct type.
1499 * On success @value will point to a reference of the datetime which
1500 * should be unreffed with gst_date_time_unref() when no longer needed
1501 * (note: this is inconsistent with e.g. gst_structure_get_string()
1502 * which doesn't return a copy of the string).
1504 * Returns: TRUE if the value could be set correctly. If there was no field
1505 * with @fieldname or the existing field did not contain a data, this function
1509 gst_structure_get_date_time (const GstStructure * structure,
1510 const gchar * fieldname, GstDateTime ** value)
1512 GstStructureField *field;
1514 g_return_val_if_fail (structure != NULL, FALSE);
1515 g_return_val_if_fail (fieldname != NULL, FALSE);
1516 g_return_val_if_fail (value != NULL, FALSE);
1518 field = gst_structure_get_field (structure, fieldname);
1522 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1525 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1526 *value = g_value_dup_boxed (&field->value);
1532 * gst_structure_get_clock_time:
1533 * @structure: a #GstStructure
1534 * @fieldname: the name of a field
1535 * @value: (out): a pointer to a #GstClockTime to set
1537 * Sets the clock time pointed to by @value corresponding to the clock time
1538 * of the given field. Caller is responsible for making sure the field exists
1539 * and has the correct type.
1541 * Returns: TRUE if the value could be set correctly. If there was no field
1542 * with @fieldname or the existing field did not contain a #GstClockTime, this
1543 * function returns FALSE.
1546 gst_structure_get_clock_time (const GstStructure * structure,
1547 const gchar * fieldname, GstClockTime * value)
1549 return gst_structure_get_uint64 (structure, fieldname, value);
1553 * gst_structure_get_double:
1554 * @structure: a #GstStructure
1555 * @fieldname: the name of a field
1556 * @value: (out): a pointer to a gdouble to set
1558 * Sets the double pointed to by @value corresponding to the value of the
1559 * given field. Caller is responsible for making sure the field exists
1560 * and has the correct type.
1562 * Returns: TRUE if the value could be set correctly. If there was no field
1563 * with @fieldname or the existing field did not contain a double, this
1564 * function returns FALSE.
1567 gst_structure_get_double (const GstStructure * structure,
1568 const gchar * fieldname, gdouble * value)
1570 GstStructureField *field;
1572 g_return_val_if_fail (structure != NULL, FALSE);
1573 g_return_val_if_fail (fieldname != NULL, FALSE);
1574 g_return_val_if_fail (value != NULL, FALSE);
1576 field = gst_structure_get_field (structure, fieldname);
1580 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1583 *value = gst_g_value_get_double_unchecked (&field->value);
1589 * gst_structure_get_string:
1590 * @structure: a #GstStructure
1591 * @fieldname: the name of a field
1593 * Finds the field corresponding to @fieldname, and returns the string
1594 * contained in the field's value. Caller is responsible for making
1595 * sure the field exists and has the correct type.
1597 * The string should not be modified, and remains valid until the next
1598 * call to a gst_structure_*() function with the given structure.
1600 * Returns: a pointer to the string or NULL when the field did not exist
1601 * or did not contain a string.
1604 gst_structure_get_string (const GstStructure * structure,
1605 const gchar * fieldname)
1607 GstStructureField *field;
1609 g_return_val_if_fail (structure != NULL, NULL);
1610 g_return_val_if_fail (fieldname != NULL, NULL);
1612 field = gst_structure_get_field (structure, fieldname);
1616 if (!G_VALUE_HOLDS_STRING (&field->value))
1619 return gst_g_value_get_string_unchecked (&field->value);
1623 * gst_structure_get_enum:
1624 * @structure: a #GstStructure
1625 * @fieldname: the name of a field
1626 * @enumtype: the enum type of a field
1627 * @value: (out): a pointer to an int to set
1629 * Sets the int pointed to by @value corresponding to the value of the
1630 * given field. Caller is responsible for making sure the field exists,
1631 * has the correct type and that the enumtype is correct.
1633 * Returns: TRUE if the value could be set correctly. If there was no field
1634 * with @fieldname or the existing field did not contain an enum of the given
1635 * type, this function returns FALSE.
1638 gst_structure_get_enum (const GstStructure * structure,
1639 const gchar * fieldname, GType enumtype, gint * value)
1641 GstStructureField *field;
1643 g_return_val_if_fail (structure != NULL, FALSE);
1644 g_return_val_if_fail (fieldname != NULL, FALSE);
1645 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1646 g_return_val_if_fail (value != NULL, FALSE);
1648 field = gst_structure_get_field (structure, fieldname);
1652 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1655 *value = g_value_get_enum (&field->value);
1661 * gst_structure_get_fraction:
1662 * @structure: a #GstStructure
1663 * @fieldname: the name of a field
1664 * @value_numerator: (out): a pointer to an int to set
1665 * @value_denominator: (out): a pointer to an int to set
1667 * Sets the integers pointed to by @value_numerator and @value_denominator
1668 * corresponding to the value of the given field. Caller is responsible
1669 * for making sure the field exists and has the correct type.
1671 * Returns: TRUE if the values could be set correctly. If there was no field
1672 * with @fieldname or the existing field did not contain a GstFraction, this
1673 * function returns FALSE.
1676 gst_structure_get_fraction (const GstStructure * structure,
1677 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1679 GstStructureField *field;
1681 g_return_val_if_fail (structure != NULL, FALSE);
1682 g_return_val_if_fail (fieldname != NULL, FALSE);
1683 g_return_val_if_fail (value_numerator != NULL, FALSE);
1684 g_return_val_if_fail (value_denominator != NULL, FALSE);
1686 field = gst_structure_get_field (structure, fieldname);
1690 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1693 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1694 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1699 typedef struct _GstStructureAbbreviation
1701 const gchar *type_name;
1704 GstStructureAbbreviation;
1706 /* return a copy of an array of GstStructureAbbreviation containing all the
1707 * known type_string, GType maps, including abbreviations for common types */
1708 static GstStructureAbbreviation *
1709 gst_structure_get_abbrs (gint * n_abbrs)
1711 static GstStructureAbbreviation *abbrs = NULL;
1712 static volatile gsize num = 0;
1714 if (g_once_init_enter (&num)) {
1715 /* dynamically generate the array */
1717 GstStructureAbbreviation dyn_abbrs[] = {
1722 {"uint", G_TYPE_UINT}
1726 {"float", G_TYPE_FLOAT}
1730 {"double", G_TYPE_DOUBLE}
1732 {"d", G_TYPE_DOUBLE}
1734 {"buffer", GST_TYPE_BUFFER}
1736 {"fraction", GST_TYPE_FRACTION}
1738 {"boolean", G_TYPE_BOOLEAN}
1740 {"bool", G_TYPE_BOOLEAN}
1742 {"b", G_TYPE_BOOLEAN}
1744 {"string", G_TYPE_STRING}
1746 {"str", G_TYPE_STRING}
1748 {"s", G_TYPE_STRING}
1750 {"structure", GST_TYPE_STRUCTURE}
1752 {"date", G_TYPE_DATE}
1754 {"datetime", GST_TYPE_DATE_TIME}
1756 {"bitmask", GST_TYPE_BITMASK}
1758 {"sample", GST_TYPE_SAMPLE}
1760 {"taglist", GST_TYPE_TAG_LIST}
1762 _num = G_N_ELEMENTS (dyn_abbrs);
1763 /* permanently allocate and copy the array now */
1764 abbrs = g_new0 (GstStructureAbbreviation, _num);
1765 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1766 g_once_init_leave (&num, _num);
1773 /* given a type_name that could be a type abbreviation or a registered GType,
1774 * return a matching GType */
1776 gst_structure_gtype_from_abbr (const char *type_name)
1779 GstStructureAbbreviation *abbrs;
1782 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1784 abbrs = gst_structure_get_abbrs (&n_abbrs);
1786 for (i = 0; i < n_abbrs; i++) {
1787 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1788 return abbrs[i].type;
1792 /* this is the fallback */
1793 return g_type_from_name (type_name);
1797 gst_structure_to_abbr (GType type)
1800 GstStructureAbbreviation *abbrs;
1803 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1805 abbrs = gst_structure_get_abbrs (&n_abbrs);
1807 for (i = 0; i < n_abbrs; i++) {
1808 if (type == abbrs[i].type) {
1809 return abbrs[i].type_name;
1813 return g_type_name (type);
1817 gst_structure_value_get_generic_type (GValue * val)
1819 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1820 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1821 GArray *array = g_value_peek_pointer (val);
1823 if (array->len > 0) {
1824 GValue *value = &g_array_index (array, GValue, 0);
1826 return gst_structure_value_get_generic_type (value);
1830 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1832 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1833 return G_TYPE_INT64;
1834 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1835 return G_TYPE_DOUBLE;
1836 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1837 return GST_TYPE_FRACTION;
1839 return G_VALUE_TYPE (val);
1843 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1846 GstStructureField *field;
1849 g_return_val_if_fail (s != NULL, FALSE);
1851 len = GST_STRUCTURE_FIELDS (structure)->len;
1852 for (i = 0; i < len; i++) {
1856 field = GST_STRUCTURE_FIELD (structure, i);
1858 t = gst_value_serialize (&field->value);
1859 type = gst_structure_value_get_generic_type (&field->value);
1861 g_string_append_len (s, ", ", 2);
1862 /* FIXME: do we need to escape fieldnames? */
1863 g_string_append (s, g_quark_to_string (field->name));
1864 g_string_append_len (s, "=(", 2);
1865 g_string_append (s, gst_structure_to_abbr (type));
1866 g_string_append_c (s, ')');
1867 g_string_append (s, t == NULL ? "NULL" : t);
1871 g_string_append_c (s, ';');
1876 * gst_structure_to_string:
1877 * @structure: a #GstStructure
1879 * Converts @structure to a human-readable string representation.
1881 * For debugging purposes its easier to do something like this:
1883 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1885 * This prints the structure in human readable form.
1887 * The current implementation of serialization will lead to unexpected results
1888 * when there are nested #GstCaps / #GstStructure deeper than one level.
1890 * Free-function: g_free
1892 * Returns: (transfer full): a pointer to string allocated by g_malloc().
1893 * g_free() after usage.
1896 gst_structure_to_string (const GstStructure * structure)
1900 /* NOTE: This function is potentially called by the debug system,
1901 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1902 * should be careful to avoid recursion. This includes any functions
1903 * called by gst_structure_to_string. In particular, calls should
1904 * not use the GST_PTR_FORMAT extension. */
1906 g_return_val_if_fail (structure != NULL, NULL);
1908 /* we estimate a minimum size based on the number of fields in order to
1909 * avoid unnecessary reallocs within GString */
1910 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1911 g_string_append (s, g_quark_to_string (structure->name));
1912 priv_gst_structure_append_to_gstring (structure, s);
1913 return g_string_free (s, FALSE);
1917 * r will still point to the string. if end == next, the string will not be
1918 * null-terminated. In all other cases it will be.
1919 * end = pointer to char behind end of string, next = pointer to start of
1921 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1924 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1935 ret = gst_structure_parse_simple_string (s, end);
1945 if (G_UNLIKELY (*s == 0))
1947 if (G_UNLIKELY (*s == '\\'))
1955 /* Find the closing quotes */
1958 if (G_UNLIKELY (*s == 0))
1960 if (G_UNLIKELY (*s == '\\'))
1975 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1978 GValue value1 = { 0 };
1979 GValue value2 = { 0 };
1980 GValue value3 = { 0 };
1982 gboolean ret, have_step = FALSE;
1988 ret = gst_structure_parse_value (s, &s, &value1, type);
1992 while (g_ascii_isspace (*s))
1999 while (g_ascii_isspace (*s))
2002 ret = gst_structure_parse_value (s, &s, &value2, type);
2006 while (g_ascii_isspace (*s))
2009 /* optional step for int and int64 */
2010 if (G_VALUE_TYPE (&value1) == G_TYPE_INT
2011 || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2015 while (g_ascii_isspace (*s))
2018 ret = gst_structure_parse_value (s, &s, &value3, type);
2022 while (g_ascii_isspace (*s))
2033 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
2035 if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
2038 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
2039 range_type = GST_TYPE_DOUBLE_RANGE;
2040 g_value_init (value, range_type);
2041 gst_value_set_double_range (value,
2042 gst_g_value_get_double_unchecked (&value1),
2043 gst_g_value_get_double_unchecked (&value2));
2044 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
2045 range_type = GST_TYPE_INT_RANGE;
2046 g_value_init (value, range_type);
2048 gst_value_set_int_range_step (value,
2049 gst_g_value_get_int_unchecked (&value1),
2050 gst_g_value_get_int_unchecked (&value2),
2051 gst_g_value_get_int_unchecked (&value3));
2053 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
2054 gst_g_value_get_int_unchecked (&value2));
2055 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2056 range_type = GST_TYPE_INT64_RANGE;
2057 g_value_init (value, range_type);
2059 gst_value_set_int64_range_step (value,
2060 gst_g_value_get_int64_unchecked (&value1),
2061 gst_g_value_get_int64_unchecked (&value2),
2062 gst_g_value_get_int64_unchecked (&value3));
2064 gst_value_set_int64_range (value,
2065 gst_g_value_get_int64_unchecked (&value1),
2066 gst_g_value_get_int64_unchecked (&value2));
2067 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
2068 range_type = GST_TYPE_FRACTION_RANGE;
2069 g_value_init (value, range_type);
2070 gst_value_set_fraction_range (value, &value1, &value2);
2080 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
2081 GType type, GType list_type, char begin, char end)
2083 GValue list_value = { 0 };
2087 g_value_init (value, list_type);
2088 array = g_value_peek_pointer (value);
2094 while (g_ascii_isspace (*s))
2102 ret = gst_structure_parse_value (s, &s, &list_value, type);
2106 g_array_append_val (array, list_value);
2108 while (g_ascii_isspace (*s))
2116 while (g_ascii_isspace (*s))
2119 memset (&list_value, 0, sizeof (list_value));
2120 ret = gst_structure_parse_value (s, &s, &list_value, type);
2124 g_array_append_val (array, list_value);
2125 while (g_ascii_isspace (*s))
2136 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2138 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2143 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2146 return gst_structure_parse_any_list (s, after, value, type,
2147 GST_TYPE_ARRAY, '<', '>');
2151 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2155 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2165 gst_structure_parse_field (gchar * str,
2166 gchar ** after, GstStructureField * field)
2175 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2178 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2179 GST_WARNING ("failed to parse simple string, str=%s", str);
2184 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2187 if (G_UNLIKELY (*s != '=')) {
2188 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2195 field->name = g_quark_from_string (name);
2196 GST_DEBUG ("trying field name '%s'", name);
2199 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2201 GST_WARNING ("failed to parse value %s", str);
2210 gst_structure_parse_value (gchar * str,
2211 gchar ** after, GValue * value, GType default_type)
2220 GType type = default_type;
2223 while (g_ascii_isspace (*s))
2226 /* check if there's a (type_name) 'cast' */
2230 while (g_ascii_isspace (*s))
2233 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2236 while (g_ascii_isspace (*s))
2238 if (G_UNLIKELY (*s != ')'))
2241 while (g_ascii_isspace (*s))
2246 type = gst_structure_gtype_from_abbr (type_name);
2247 GST_DEBUG ("trying type name '%s'", type_name);
2250 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2251 GST_WARNING ("invalid type");
2256 while (g_ascii_isspace (*s))
2259 ret = gst_structure_parse_range (s, &s, value, type);
2260 } else if (*s == '{') {
2261 ret = gst_structure_parse_list (s, &s, value, type);
2262 } else if (*s == '<') {
2263 ret = gst_structure_parse_array (s, &s, value, type);
2267 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2269 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2274 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2276 /* Set NULL terminator for deserialization */
2280 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2281 g_value_init (value, try_types[i]);
2282 ret = gst_value_deserialize (value, value_s);
2285 g_value_unset (value);
2288 g_value_init (value, type);
2290 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2291 (type != G_TYPE_STRING))))
2293 /* Set NULL terminator for deserialization */
2297 ret = gst_value_deserialize (value, value_s);
2298 if (G_UNLIKELY (!ret))
2299 g_value_unset (value);
2310 priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end,
2318 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2319 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2320 && g_ascii_isspace (r[1]))))
2325 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2326 GST_WARNING ("Failed to parse structure string '%s'", str);
2337 priv_gst_structure_parse_fields (gchar * str, gchar ** end,
2338 GstStructure * structure)
2341 GstStructureField field;
2346 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2347 && g_ascii_isspace (r[1]))))
2350 /* end of structure, get the next char and finish */
2355 /* accept \0 as end delimiter */
2358 if (G_UNLIKELY (*r != ',')) {
2359 GST_WARNING ("Failed to find delimiter, r=%s", r);
2363 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2364 && g_ascii_isspace (r[1]))))
2367 memset (&field, 0, sizeof (field));
2368 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2369 GST_WARNING ("Failed to parse field, r=%s", r);
2372 gst_structure_set_field (structure, &field);
2381 * gst_structure_new_from_string:
2382 * @string: a string representation of a #GstStructure
2384 * Creates a #GstStructure from a string representation.
2385 * If end is not NULL, a pointer to the place inside the given string
2386 * where parsing ended will be returned.
2388 * The current implementation of serialization will lead to unexpected results
2389 * when there are nested #GstCaps / #GstStructure deeper than one level.
2391 * Free-function: gst_structure_free
2393 * Returns: (transfer full): a new #GstStructure or NULL when the string could
2394 * not be parsed. Free with gst_structure_free() after use.
2399 gst_structure_new_from_string (const gchar * string)
2401 return gst_structure_from_string (string, NULL);
2405 * gst_structure_from_string:
2406 * @string: a string representation of a #GstStructure.
2407 * @end: (out) (allow-none) (transfer none) (skip): pointer to store the end of the string in.
2409 * Creates a #GstStructure from a string representation.
2410 * If end is not NULL, a pointer to the place inside the given string
2411 * where parsing ended will be returned.
2413 * Free-function: gst_structure_free
2415 * Returns: (transfer full): a new #GstStructure or NULL when the string could
2416 * not be parsed. Free with gst_structure_free() after use.
2419 gst_structure_from_string (const gchar * string, gchar ** end)
2426 GstStructure *structure = NULL;
2428 g_return_val_if_fail (string != NULL, NULL);
2430 copy = g_strdup (string);
2433 if (!priv_gst_structure_parse_name (r, &name, &w, &r))
2438 structure = gst_structure_new_empty (name);
2441 if (G_UNLIKELY (structure == NULL))
2444 if (!priv_gst_structure_parse_fields (r, &r, structure))
2448 *end = (char *) string + (r - copy);
2450 g_warning ("gst_structure_from_string did not consume whole string,"
2451 " but caller did not provide end pointer (\"%s\")", string);
2458 gst_structure_free (structure);
2464 gst_structure_transform_to_string (const GValue * src_value,
2465 GValue * dest_value)
2467 g_return_if_fail (src_value != NULL);
2468 g_return_if_fail (dest_value != NULL);
2470 dest_value->data[0].v_pointer =
2471 gst_structure_to_string (src_value->data[0].v_pointer);
2474 static GstStructure *
2475 gst_structure_copy_conditional (const GstStructure * structure)
2478 return gst_structure_copy (structure);
2482 /* fixate utility functions */
2485 * gst_structure_fixate_field_nearest_int:
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 to the nearest
2491 * integer to @target that is a subset of the existing field.
2493 * Returns: TRUE if the structure could be fixated
2496 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2497 const char *field_name, int 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_INT) {
2509 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2512 x = gst_value_get_int_range_min (value);
2515 x = gst_value_get_int_range_max (value);
2518 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2520 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2521 const GValue *list_value;
2524 int best_index = -1;
2526 n = gst_value_list_get_size (value);
2527 for (i = 0; i < n; i++) {
2528 list_value = gst_value_list_get_value (value, i);
2529 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2530 int x = gst_g_value_get_int_unchecked (list_value);
2532 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2538 if (best_index != -1) {
2539 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2549 * gst_structure_fixate_field_nearest_double:
2550 * @structure: a #GstStructure
2551 * @field_name: a field in @structure
2552 * @target: the target value of the fixation
2554 * Fixates a #GstStructure by changing the given field to the nearest
2555 * double to @target that is a subset of the existing field.
2557 * Returns: TRUE if the structure could be fixated
2560 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2561 const char *field_name, double target)
2563 const GValue *value;
2565 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2566 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2568 value = gst_structure_get_value (structure, field_name);
2570 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2573 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2576 x = gst_value_get_double_range_min (value);
2579 x = gst_value_get_double_range_max (value);
2582 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2584 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2585 const GValue *list_value;
2588 int best_index = -1;
2590 n = gst_value_list_get_size (value);
2591 for (i = 0; i < n; i++) {
2592 list_value = gst_value_list_get_value (value, i);
2593 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2594 double x = gst_g_value_get_double_unchecked (list_value);
2596 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2602 if (best_index != -1) {
2603 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2614 * gst_structure_fixate_field_boolean:
2615 * @structure: a #GstStructure
2616 * @field_name: a field in @structure
2617 * @target: the target value of the fixation
2619 * Fixates a #GstStructure by changing the given @field_name field to the given
2620 * @target boolean if that field is not fixed yet.
2622 * Returns: TRUE if the structure could be fixated
2625 gst_structure_fixate_field_boolean (GstStructure * structure,
2626 const char *field_name, gboolean target)
2628 const GValue *value;
2630 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2631 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2633 value = gst_structure_get_value (structure, field_name);
2635 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2638 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2639 const GValue *list_value;
2642 int best_index = -1;
2644 n = gst_value_list_get_size (value);
2645 for (i = 0; i < n; i++) {
2646 list_value = gst_value_list_get_value (value, i);
2647 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2648 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2650 if (best_index == -1 || x == target) {
2656 if (best_index != -1) {
2657 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2667 * gst_structure_fixate_field_string:
2668 * @structure: a #GstStructure
2669 * @field_name: a field in @structure
2670 * @target: the target value of the fixation
2672 * Fixates a #GstStructure by changing the given @field_name field to the given
2673 * @target string if that field is not fixed yet.
2675 * Returns: TRUE if the structure could be fixated
2678 gst_structure_fixate_field_string (GstStructure * structure,
2679 const gchar * field_name, const gchar * target)
2681 const GValue *value;
2683 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2684 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2686 value = gst_structure_get_value (structure, field_name);
2688 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2691 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2692 const GValue *list_value;
2694 const gchar *best = NULL;
2695 int best_index = -1;
2697 n = gst_value_list_get_size (value);
2698 for (i = 0; i < n; i++) {
2699 list_value = gst_value_list_get_value (value, i);
2700 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2701 const gchar *x = g_value_get_string (list_value);
2703 if (best_index == -1 || g_str_equal (x, target)) {
2709 if (best_index != -1) {
2710 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2720 * gst_structure_fixate_field_nearest_fraction:
2721 * @structure: a #GstStructure
2722 * @field_name: a field in @structure
2723 * @target_numerator: The numerator of the target value of the fixation
2724 * @target_denominator: The denominator of the target value of the fixation
2726 * Fixates a #GstStructure by changing the given field to the nearest
2727 * fraction to @target_numerator/@target_denominator that is a subset
2728 * of the existing field.
2730 * Returns: TRUE if the structure could be fixated
2733 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2734 const char *field_name, const gint target_numerator,
2735 const gint target_denominator)
2737 const GValue *value;
2739 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2740 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2742 value = gst_structure_get_value (structure, field_name);
2744 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2747 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2748 const GValue *x, *new_value;
2749 GValue target = { 0 };
2750 g_value_init (&target, GST_TYPE_FRACTION);
2751 gst_value_set_fraction (&target, target_numerator, target_denominator);
2753 new_value = ⌖
2754 x = gst_value_get_fraction_range_min (value);
2755 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2757 x = gst_value_get_fraction_range_max (value);
2758 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2761 gst_structure_set_value (structure, field_name, new_value);
2762 g_value_unset (&target);
2764 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2765 const GValue *list_value;
2767 const GValue *best = NULL;
2770 gdouble best_diff = G_MAXDOUBLE;
2772 target = (gdouble) target_numerator / (gdouble) target_denominator;
2774 GST_DEBUG ("target %g, best %g", target, best_diff);
2778 n = gst_value_list_get_size (value);
2779 for (i = 0; i < n; i++) {
2780 list_value = gst_value_list_get_value (value, i);
2781 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2783 gdouble list_double;
2785 num = gst_value_get_fraction_numerator (list_value);
2786 denom = gst_value_get_fraction_denominator (list_value);
2788 list_double = ((gdouble) num / (gdouble) denom);
2789 cur_diff = target - list_double;
2791 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2794 cur_diff = -cur_diff;
2796 if (!best || cur_diff < best_diff) {
2797 GST_DEBUG ("new best %g", list_double);
2799 best_diff = cur_diff;
2804 gst_structure_set_value (structure, field_name, best);
2813 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2815 GstStructure *s = data;
2818 if (gst_value_fixate (&v, value)) {
2819 gst_structure_id_take_value (s, field_id, &v);
2825 * gst_structure_fixate_field:
2826 * @structure: a #GstStructure
2827 * @field_name: a field in @structure
2829 * Fixates a #GstStructure by changing the given field with its fixated value.
2831 * Returns: TRUE if the structure field could be fixated
2834 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2836 GstStructureField *field;
2838 g_return_val_if_fail (structure != NULL, FALSE);
2839 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2841 if (!(field = gst_structure_get_field (structure, field_name)))
2844 return default_fixate (field->name, &field->value, structure);
2847 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2848 * (useful for message parsing functions where the return location is user
2849 * supplied and the user may pass NULL if the value isn't of interest) */
2850 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2852 const GValue *_value = (value); \
2853 guint _flags = (flags); \
2854 GType _value_type = G_VALUE_TYPE (_value); \
2855 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2856 const gchar *_lcopy_format = _vtable->lcopy_format; \
2857 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2858 guint _n_values = 0; \
2860 while (*_lcopy_format != '\0') { \
2861 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2862 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2865 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2866 *(__error) = g_strdup_printf ("either all or none of the return " \
2867 "locations for field '%s' need to be NULL", fieldname); \
2868 } else if (_cvalues[0].v_pointer != NULL) { \
2869 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2874 * gst_structure_get_valist:
2875 * @structure: a #GstStructure
2876 * @first_fieldname: the name of the first field to read
2877 * @args: variable arguments
2879 * Parses the variable arguments and reads fields from @structure accordingly.
2880 * valist-variant of gst_structure_get(). Look at the documentation of
2881 * gst_structure_get() for more details.
2883 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2886 gst_structure_get_valist (const GstStructure * structure,
2887 const char *first_fieldname, va_list args)
2889 const char *field_name;
2890 GType expected_type = G_TYPE_INVALID;
2892 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2893 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2895 field_name = first_fieldname;
2896 while (field_name) {
2897 const GValue *val = NULL;
2900 expected_type = va_arg (args, GType);
2902 val = gst_structure_get_value (structure, field_name);
2907 if (G_VALUE_TYPE (val) != expected_type)
2910 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2912 g_warning ("%s: %s", G_STRFUNC, err);
2917 field_name = va_arg (args, const gchar *);
2925 GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2926 field_name, structure);
2931 GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
2932 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2933 GST_STR_NULL (g_type_name (expected_type)),
2934 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2941 * gst_structure_id_get_valist:
2942 * @structure: a #GstStructure
2943 * @first_field_id: the quark of the first field to read
2944 * @args: variable arguments
2946 * Parses the variable arguments and reads fields from @structure accordingly.
2947 * valist-variant of gst_structure_id_get(). Look at the documentation of
2948 * gst_structure_id_get() for more details.
2950 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2953 gst_structure_id_get_valist (const GstStructure * structure,
2954 GQuark first_field_id, va_list args)
2957 GType expected_type = G_TYPE_INVALID;
2959 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2960 g_return_val_if_fail (first_field_id != 0, FALSE);
2962 field_id = first_field_id;
2964 const GValue *val = NULL;
2967 expected_type = va_arg (args, GType);
2969 val = gst_structure_id_get_value (structure, field_id);
2974 if (G_VALUE_TYPE (val) != expected_type)
2977 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2979 g_warning ("%s: %s", G_STRFUNC, err);
2984 field_id = va_arg (args, GQuark);
2992 GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2993 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2998 GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
2999 "field was of type '%s': %" GST_PTR_FORMAT,
3000 g_quark_to_string (field_id),
3001 GST_STR_NULL (g_type_name (expected_type)),
3002 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
3009 * gst_structure_get:
3010 * @structure: a #GstStructure
3011 * @first_fieldname: the name of the first field to read
3012 * @...: variable arguments
3014 * Parses the variable arguments and reads fields from @structure accordingly.
3015 * Variable arguments should be in the form field name, field type
3016 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3017 * The last variable argument should be NULL.
3019 * For refcounted (mini)objects you will receive a new reference which
3020 * you must release with a suitable _unref() when no longer needed. For
3021 * strings and boxed types you will receive a copy which you will need to
3022 * release with either g_free() or the suitable function for the boxed type.
3024 * Returns: FALSE if there was a problem reading any of the fields (e.g.
3025 * because the field requested did not exist, or was of a type other
3026 * than the type specified), otherwise TRUE.
3029 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
3035 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3036 g_return_val_if_fail (first_fieldname != NULL, FALSE);
3038 va_start (args, first_fieldname);
3039 ret = gst_structure_get_valist (structure, first_fieldname, args);
3046 * gst_structure_id_get:
3047 * @structure: a #GstStructure
3048 * @first_field_id: the quark of the first field to read
3049 * @...: variable arguments
3051 * Parses the variable arguments and reads fields from @structure accordingly.
3052 * Variable arguments should be in the form field id quark, field type
3053 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3054 * The last variable argument should be NULL (technically it should be a
3055 * 0 quark, but we require NULL so compilers that support it can check for
3056 * the NULL terminator and warn if it's not there).
3058 * This function is just like gst_structure_get() only that it is slightly
3059 * more efficient since it saves the string-to-quark lookup in the global
3062 * For refcounted (mini)objects you will receive a new reference which
3063 * you must release with a suitable _unref() when no longer needed. For
3064 * strings and boxed types you will receive a copy which you will need to
3065 * release with either g_free() or the suitable function for the boxed type.
3067 * Returns: FALSE if there was a problem reading any of the fields (e.g.
3068 * because the field requested did not exist, or was of a type other
3069 * than the type specified), otherwise TRUE.
3072 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
3078 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3079 g_return_val_if_fail (first_field_id != 0, FALSE);
3081 va_start (args, first_field_id);
3082 ret = gst_structure_id_get_valist (structure, first_field_id, args);
3089 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
3092 const GstStructure *struct1 = (const GstStructure *) data;
3093 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
3095 if (G_UNLIKELY (val1 == NULL))
3097 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
3105 * gst_structure_is_equal:
3106 * @structure1: a #GstStructure.
3107 * @structure2: a #GstStructure.
3109 * Tests if the two #GstStructure are equal.
3111 * Returns: TRUE if the two structures have the same name and field.
3114 gst_structure_is_equal (const GstStructure * structure1,
3115 const GstStructure * structure2)
3117 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
3118 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
3120 if (G_UNLIKELY (structure1 == structure2))
3123 if (structure1->name != structure2->name) {
3126 if (GST_STRUCTURE_FIELDS (structure1)->len !=
3127 GST_STRUCTURE_FIELDS (structure2)->len) {
3131 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
3132 (gpointer) structure2);
3139 const GstStructure *intersect;
3144 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
3146 IntersectData *idata = (IntersectData *) data;
3147 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3149 if (G_UNLIKELY (val2 == NULL)) {
3150 gst_structure_id_set_value (idata->dest, id, val1);
3152 GValue dest_value = { 0 };
3153 if (gst_value_intersect (&dest_value, val1, val2)) {
3154 gst_structure_id_take_value (idata->dest, id, &dest_value);
3163 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
3165 IntersectData *idata = (IntersectData *) data;
3166 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3168 if (G_UNLIKELY (val2 == NULL)) {
3169 gst_structure_id_set_value (idata->dest, id, val1);
3175 * gst_structure_intersect:
3176 * @struct1: a #GstStructure
3177 * @struct2: a #GstStructure
3179 * Intersects @struct1 and @struct2 and returns the intersection.
3181 * Returns: Intersection of @struct1 and @struct2
3184 gst_structure_intersect (const GstStructure * struct1,
3185 const GstStructure * struct2)
3189 g_assert (struct1 != NULL);
3190 g_assert (struct2 != NULL);
3192 if (G_UNLIKELY (struct1->name != struct2->name))
3195 /* copy fields from struct1 which we have not in struct2 to target
3196 * intersect if we have the field in both */
3197 data.dest = gst_structure_new_id_empty (struct1->name);
3198 data.intersect = struct2;
3199 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3200 gst_structure_intersect_field1, &data)))
3203 /* copy fields from struct2 which we have not in struct1 to target */
3204 data.intersect = struct1;
3205 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3206 gst_structure_intersect_field2, &data)))
3212 gst_structure_free (data.dest);
3217 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3220 GstStructure *other = (GstStructure *) data;
3221 const GValue *val2 = gst_structure_id_get_value (other, id);
3223 if (G_LIKELY (val2)) {
3224 if (!gst_value_can_intersect (val1, val2)) {
3227 gint eq = gst_value_compare (val1, val2);
3229 if (eq == GST_VALUE_UNORDERED) {
3230 /* we need to try interseting */
3231 if (!gst_value_intersect (NULL, val1, val2)) {
3234 } else if (eq != GST_VALUE_EQUAL) {
3243 * gst_structure_can_intersect:
3244 * @struct1: a #GstStructure
3245 * @struct2: a #GstStructure
3247 * Tries intersecting @struct1 and @struct2 and reports whether the result
3248 * would not be empty.
3250 * Returns: %TRUE if intersection would not be empty
3253 gst_structure_can_intersect (const GstStructure * struct1,
3254 const GstStructure * struct2)
3256 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3257 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3259 if (G_UNLIKELY (struct1->name != struct2->name))
3262 /* tries to intersect if we have the field in both */
3263 return gst_structure_foreach ((GstStructure *) struct1,
3264 gst_caps_structure_can_intersect_field, (gpointer) struct2);
3268 gst_caps_structure_is_superset_field (GQuark field_id, const GValue * value,
3271 GstStructure *subset = user_data;
3272 const GValue *other;
3275 if (!(other = gst_structure_id_get_value (subset, field_id)))
3276 /* field is missing in the subset => no subset */
3279 comparison = gst_value_compare (value, other);
3281 /* equal values are subset */
3282 if (comparison == GST_VALUE_EQUAL)
3285 /* ordered, but unequal, values are not */
3286 if (comparison != GST_VALUE_UNORDERED)
3289 return gst_value_is_subset (other, value);
3293 * gst_structure_is_subset:
3294 * @subset: a #GstStructure
3295 * @superset: a potentially greater #GstStructure
3297 * Checks if @subset is a subset of @superset, i.e. has the same
3298 * structure name and for all fields that are existing in @superset,
3299 * @subset has a value that is a subset of the value in @superset.
3301 * Returns: %TRUE if @subset is a subset of @superset
3304 gst_structure_is_subset (const GstStructure * subset,
3305 const GstStructure * superset)
3307 if ((superset->name != subset->name) ||
3308 (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3311 return gst_structure_foreach ((GstStructure *) superset,
3312 gst_caps_structure_is_superset_field, (gpointer) subset);
3317 * gst_structure_fixate:
3318 * @structure: a #GstStructure
3320 * Fixate all values in @structure using gst_value_fixate().
3321 * @structure will be modified in-place and should be writable.
3324 gst_structure_fixate (GstStructure * structure)
3326 g_return_if_fail (GST_IS_STRUCTURE (structure));
3328 gst_structure_foreach (structure, default_fixate, structure);