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 * @title: GstStructure
25 * @short_description: Generic structure containing fields of names and values
26 * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
28 * A #GstStructure is a collection of key/value pairs. The keys are expressed
29 * as GQuarks and the values can be of any GType.
31 * In addition to the key/value pairs, a #GstStructure also has a name. The name
32 * starts with a letter and can be filled by letters, numbers and any of "/-_.:".
34 * #GstStructure is used by various GStreamer subsystems to store information
35 * in a flexible and extensible way. A #GstStructure does not have a refcount
36 * because it usually is part of a higher level object such as #GstCaps,
37 * #GstMessage, #GstEvent, #GstQuery. It provides a means to enforce mutability
38 * using the refcount of the parent with the gst_structure_set_parent_refcount()
41 * A #GstStructure can be created with gst_structure_new_empty() or
42 * gst_structure_new(), which both take a name and an optional set of
43 * key/value pairs along with the types of the values.
45 * Field values can be changed with gst_structure_set_value() or
46 * gst_structure_set().
48 * Field values can be retrieved with gst_structure_get_value() or the more
49 * convenient gst_structure_get_*() functions.
51 * Fields can be removed with gst_structure_remove_field() or
52 * gst_structure_remove_fields().
54 * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are
55 * not allowed. Strings may be %NULL however.
57 * Be aware that the current #GstCaps / #GstStructure serialization into string
58 * has limited support for nested #GstCaps / #GstStructure fields. It can only
59 * support one level of nesting. Using more levels will lead to unexpected
60 * behavior when using serialization features, such as gst_caps_to_string() or
61 * gst_value_serialize() and their counterparts.
70 #include "gst_private.h"
73 #include <gobject/gvaluecollector.h>
75 GST_DEBUG_CATEGORY_STATIC (gst_structure_debug);
76 #define GST_CAT_DEFAULT gst_structure_debug
78 typedef struct _GstStructureField GstStructureField;
80 struct _GstStructureField
90 /* owned by parent structure, NULL if no parent */
91 gint *parent_refcount;
96 #define GST_STRUCTURE_REFCOUNT(s) (((GstStructureImpl*)(s))->parent_refcount)
97 #define GST_STRUCTURE_FIELDS(s) (((GstStructureImpl*)(s))->fields)
99 #define GST_STRUCTURE_FIELD(structure, index) \
100 &g_array_index(GST_STRUCTURE_FIELDS(structure), GstStructureField, (index))
102 #define IS_MUTABLE(structure) \
103 (!GST_STRUCTURE_REFCOUNT(structure) || \
104 g_atomic_int_get (GST_STRUCTURE_REFCOUNT(structure)) == 1)
106 #define IS_TAGLIST(structure) \
107 (structure->name == GST_QUARK (TAGLIST))
109 static void gst_structure_set_field (GstStructure * structure,
110 GstStructureField * field);
111 static GstStructureField *gst_structure_get_field (const GstStructure *
112 structure, const gchar * fieldname);
113 static GstStructureField *gst_structure_id_get_field (const GstStructure *
114 structure, GQuark field);
115 static void gst_structure_transform_to_string (const GValue * src_value,
116 GValue * dest_value);
117 static GstStructure *gst_structure_copy_conditional (const GstStructure *
119 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
120 GValue * value, GType default_type);
121 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
123 GType _gst_structure_type = 0;
126 G_DEFINE_BOXED_TYPE (GstStructure, gst_structure,
127 gst_structure_copy_conditional, gst_structure_free);
130 _priv_gst_structure_initialize (void)
132 _gst_structure_type = gst_structure_get_type ();
134 g_value_register_transform_func (_gst_structure_type, G_TYPE_STRING,
135 gst_structure_transform_to_string);
137 GST_DEBUG_CATEGORY_INIT (gst_structure_debug, "structure", 0,
138 "GstStructure debug");
141 static GstStructure *
142 gst_structure_new_id_empty_with_size (GQuark quark, guint prealloc)
144 GstStructureImpl *structure;
146 structure = g_slice_new (GstStructureImpl);
147 ((GstStructure *) structure)->type = _gst_structure_type;
148 ((GstStructure *) structure)->name = quark;
149 GST_STRUCTURE_REFCOUNT (structure) = NULL;
150 GST_STRUCTURE_FIELDS (structure) =
151 g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
153 GST_TRACE ("created structure %p", structure);
155 return GST_STRUCTURE_CAST (structure);
159 * gst_structure_new_id_empty:
160 * @quark: name of new structure
162 * Creates a new, empty #GstStructure with the given name as a GQuark.
164 * Free-function: gst_structure_free
166 * Returns: (transfer full): a new, empty #GstStructure
169 gst_structure_new_id_empty (GQuark quark)
171 g_return_val_if_fail (quark != 0, NULL);
173 return gst_structure_new_id_empty_with_size (quark, 0);
176 #ifndef G_DISABLE_CHECKS
178 gst_structure_validate_name (const gchar * name)
182 g_return_val_if_fail (name != NULL, FALSE);
184 if (G_UNLIKELY (!g_ascii_isalpha (*name))) {
185 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
190 /* FIXME: test name string more */
192 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
194 if (G_UNLIKELY (*s != '\0')) {
195 GST_WARNING ("Invalid character '%c' at offset %" G_GUINTPTR_FORMAT " in"
196 " structure name: %s", *s, ((guintptr) s - (guintptr) name), name);
200 if (strncmp (name, "video/x-raw-", 12) == 0) {
201 g_warning ("0.10-style raw video caps are being created. Should be "
202 "video/x-raw,format=(string).. now.");
203 } else if (strncmp (name, "audio/x-raw-", 12) == 0) {
204 g_warning ("0.10-style raw audio caps are being created. Should be "
205 "audio/x-raw,format=(string).. now.");
213 * gst_structure_new_empty:
214 * @name: name of new structure
216 * Creates a new, empty #GstStructure with the given @name.
218 * See gst_structure_set_name() for constraints on the @name parameter.
220 * Free-function: gst_structure_free
222 * Returns: (transfer full): a new, empty #GstStructure
225 gst_structure_new_empty (const gchar * name)
227 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
229 return gst_structure_new_id_empty_with_size (g_quark_from_string (name), 0);
234 * @name: name of new structure
235 * @firstfield: name of first field to set
236 * @...: additional arguments
238 * Creates a new #GstStructure with the given name. Parses the
239 * list of variable arguments and sets fields to the values listed.
240 * Variable arguments should be passed as field name, field type,
241 * and value. Last variable argument should be %NULL.
243 * Free-function: gst_structure_free
245 * Returns: (transfer full): a new #GstStructure
248 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
250 GstStructure *structure;
253 va_start (varargs, firstfield);
254 structure = gst_structure_new_valist (name, firstfield, varargs);
261 * gst_structure_new_valist:
262 * @name: name of new structure
263 * @firstfield: name of first field to set
264 * @varargs: variable argument list
266 * Creates a new #GstStructure with the given @name. Structure fields
267 * are set according to the varargs in a manner similar to
268 * gst_structure_new().
270 * See gst_structure_set_name() for constraints on the @name parameter.
272 * Free-function: gst_structure_free
274 * Returns: (transfer full): a new #GstStructure
277 gst_structure_new_valist (const gchar * name,
278 const gchar * firstfield, va_list varargs)
280 GstStructure *structure;
282 structure = gst_structure_new_empty (name);
285 gst_structure_set_valist (structure, firstfield, varargs);
291 * gst_structure_set_parent_refcount:
292 * @structure: a #GstStructure
293 * @refcount: (in): a pointer to the parent's refcount
295 * Sets the parent_refcount field of #GstStructure. This field is used to
296 * determine whether a structure is mutable or not. This function should only be
297 * called by code implementing parent objects of #GstStructure, as described in
298 * the MT Refcounting section of the design documents.
300 * Returns: %TRUE if the parent refcount could be set.
303 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
305 g_return_val_if_fail (structure != NULL, FALSE);
307 /* if we have a parent_refcount already, we can only clear
308 * if with a NULL refcount */
309 if (GST_STRUCTURE_REFCOUNT (structure)) {
310 if (refcount != NULL) {
311 g_return_val_if_fail (refcount == NULL, FALSE);
315 if (refcount == NULL) {
316 g_return_val_if_fail (refcount != NULL, FALSE);
321 GST_STRUCTURE_REFCOUNT (structure) = refcount;
327 * gst_structure_copy:
328 * @structure: a #GstStructure to duplicate
330 * Duplicates a #GstStructure and all its fields and values.
332 * Free-function: gst_structure_free
334 * Returns: (transfer full): a new #GstStructure.
337 gst_structure_copy (const GstStructure * structure)
339 GstStructure *new_structure;
340 GstStructureField *field;
343 g_return_val_if_fail (structure != NULL, NULL);
345 len = GST_STRUCTURE_FIELDS (structure)->len;
346 new_structure = gst_structure_new_id_empty_with_size (structure->name, len);
348 for (i = 0; i < len; i++) {
349 GstStructureField new_field = { 0 };
351 field = GST_STRUCTURE_FIELD (structure, i);
353 new_field.name = field->name;
354 gst_value_init_and_copy (&new_field.value, &field->value);
355 g_array_append_val (GST_STRUCTURE_FIELDS (new_structure), new_field);
357 GST_CAT_TRACE (GST_CAT_PERFORMANCE, "doing copy %p -> %p",
358 structure, new_structure);
360 return new_structure;
364 * gst_structure_free:
365 * @structure: (in) (transfer full): the #GstStructure to free
367 * Frees a #GstStructure and all its fields and values. The structure must not
368 * have a parent when this function is called.
371 gst_structure_free (GstStructure * structure)
373 GstStructureField *field;
376 g_return_if_fail (structure != NULL);
377 g_return_if_fail (GST_STRUCTURE_REFCOUNT (structure) == NULL);
379 len = GST_STRUCTURE_FIELDS (structure)->len;
380 for (i = 0; i < len; i++) {
381 field = GST_STRUCTURE_FIELD (structure, i);
383 if (G_IS_VALUE (&field->value)) {
384 g_value_unset (&field->value);
387 g_array_free (GST_STRUCTURE_FIELDS (structure), TRUE);
389 memset (structure, 0xff, sizeof (GstStructure));
391 GST_TRACE ("free structure %p", structure);
393 g_slice_free1 (sizeof (GstStructureImpl), structure);
397 * gst_structure_get_name:
398 * @structure: a #GstStructure
400 * Get the name of @structure as a string.
402 * Returns: the name of the structure.
405 gst_structure_get_name (const GstStructure * structure)
407 g_return_val_if_fail (structure != NULL, NULL);
409 return g_quark_to_string (structure->name);
413 * gst_structure_has_name:
414 * @structure: a #GstStructure
415 * @name: structure name to check for
417 * Checks if the structure has the given name
419 * Returns: %TRUE if @name matches the name of the structure.
422 gst_structure_has_name (const GstStructure * structure, const gchar * name)
424 const gchar *structure_name;
426 g_return_val_if_fail (structure != NULL, FALSE);
427 g_return_val_if_fail (name != NULL, FALSE);
429 /* getting the string is cheap and comparing short strings is too
430 * should be faster than getting the quark for name and comparing the quarks
432 structure_name = g_quark_to_string (structure->name);
434 return (structure_name && strcmp (structure_name, name) == 0);
438 * gst_structure_get_name_id:
439 * @structure: a #GstStructure
441 * Get the name of @structure as a GQuark.
443 * Returns: the quark representing the name of the structure.
446 gst_structure_get_name_id (const GstStructure * structure)
448 g_return_val_if_fail (structure != NULL, 0);
450 return structure->name;
454 * gst_structure_set_name:
455 * @structure: a #GstStructure
456 * @name: the new name of the structure
458 * Sets the name of the structure to the given @name. The string
459 * provided is copied before being used. It must not be empty, start with a
460 * letter and can be followed by letters, numbers and any of "/-_.:".
463 gst_structure_set_name (GstStructure * structure, const gchar * name)
465 g_return_if_fail (structure != NULL);
466 g_return_if_fail (IS_MUTABLE (structure));
467 g_return_if_fail (gst_structure_validate_name (name));
469 structure->name = g_quark_from_string (name);
473 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
474 const GValue * value)
476 GstStructureField gsfield = { 0, {0,} };
478 gsfield.name = field;
479 gst_value_init_and_copy (&gsfield.value, value);
481 gst_structure_set_field (structure, &gsfield);
485 * gst_structure_id_set_value:
486 * @structure: a #GstStructure
487 * @field: a #GQuark representing a field
488 * @value: the new value of the field
490 * Sets the field with the given GQuark @field to @value. If the field
491 * does not exist, it is created. If the field exists, the previous
492 * value is replaced and freed.
495 gst_structure_id_set_value (GstStructure * structure,
496 GQuark field, const GValue * value)
499 g_return_if_fail (structure != NULL);
500 g_return_if_fail (G_IS_VALUE (value));
501 g_return_if_fail (IS_MUTABLE (structure));
503 gst_structure_id_set_value_internal (structure, field, value);
507 * gst_structure_set_value:
508 * @structure: a #GstStructure
509 * @fieldname: the name of the field to set
510 * @value: the new value of the field
512 * Sets the field with the given name @field to @value. If the field
513 * does not exist, it is created. If the field exists, the previous
514 * value is replaced and freed.
517 gst_structure_set_value (GstStructure * structure,
518 const gchar * fieldname, const GValue * value)
520 g_return_if_fail (structure != NULL);
521 g_return_if_fail (fieldname != NULL);
522 g_return_if_fail (G_IS_VALUE (value));
523 g_return_if_fail (IS_MUTABLE (structure));
525 gst_structure_id_set_value_internal (structure,
526 g_quark_from_string (fieldname), value);
530 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
533 GstStructureField gsfield = { 0, {0,} };
535 gsfield.name = field;
536 gsfield.value = *value;
538 gst_structure_set_field (structure, &gsfield);
540 /* we took ownership */
542 memset (value, 0, sizeof (GValue));
544 value->g_type = G_TYPE_INVALID;
549 * gst_structure_id_take_value:
550 * @structure: a #GstStructure
551 * @field: a #GQuark representing a field
552 * @value: (transfer full): the new value of the field
554 * Sets the field with the given GQuark @field to @value. If the field
555 * does not exist, it is created. If the field exists, the previous
556 * value is replaced and freed.
559 gst_structure_id_take_value (GstStructure * structure, GQuark field,
562 g_return_if_fail (structure != 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, field, value);
570 * gst_structure_take_value:
571 * @structure: a #GstStructure
572 * @fieldname: the name of the field to set
573 * @value: (transfer full): the new value of the field
575 * Sets the field with the given name @field to @value. If the field
576 * does not exist, it is created. If the field exists, the previous
577 * value is replaced and freed. The function will take ownership of @value.
580 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
583 g_return_if_fail (structure != NULL);
584 g_return_if_fail (fieldname != NULL);
585 g_return_if_fail (G_IS_VALUE (value));
586 g_return_if_fail (IS_MUTABLE (structure));
588 gst_structure_id_take_value_internal (structure,
589 g_quark_from_string (fieldname), value);
593 gst_structure_set_valist_internal (GstStructure * structure,
594 const gchar * fieldname, va_list varargs)
600 GstStructureField field = { 0 };
602 field.name = g_quark_from_string (fieldname);
604 type = va_arg (varargs, GType);
606 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
607 if (G_UNLIKELY (err)) {
608 g_critical ("%s", err);
611 gst_structure_set_field (structure, &field);
613 fieldname = va_arg (varargs, gchar *);
619 * @structure: a #GstStructure
620 * @fieldname: the name of the field to set
621 * @...: variable arguments
623 * Parses the variable arguments and sets fields accordingly. Fields that
624 * weren't already part of the structure are added as needed.
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;
670 type = va_arg (varargs, GType);
672 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
673 if (G_UNLIKELY (err)) {
674 g_critical ("%s", err);
677 gst_structure_set_field (structure, &field);
679 fieldname = va_arg (varargs, GQuark);
684 * gst_structure_id_set:
685 * @structure: a #GstStructure
686 * @fieldname: the GQuark for the name of the field to set
687 * @...: variable arguments
689 * Identical to gst_structure_set, except that field names are
690 * passed using the GQuark for the field name. This allows more efficient
691 * setting of the structure if the caller already knows the associated
693 * The last variable argument must be %NULL.
696 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
700 g_return_if_fail (structure != NULL);
702 va_start (varargs, field);
703 gst_structure_id_set_valist_internal (structure, field, varargs);
708 * gst_structure_id_set_valist:
709 * @structure: a #GstStructure
710 * @fieldname: the name of the field to set
711 * @varargs: variable arguments
713 * va_list form of gst_structure_id_set().
716 gst_structure_id_set_valist (GstStructure * structure,
717 GQuark fieldname, va_list varargs)
719 g_return_if_fail (structure != NULL);
720 g_return_if_fail (IS_MUTABLE (structure));
722 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
726 * gst_structure_new_id:
727 * @name_quark: name of new structure
728 * @field_quark: the GQuark for the name of the field to set
729 * @...: variable arguments
731 * Creates a new #GstStructure with the given name as a GQuark, followed by
732 * fieldname quark, GType, argument(s) "triplets" in the same format as
733 * gst_structure_id_set(). Basically a convenience wrapper around
734 * gst_structure_new_id_empty() and gst_structure_id_set().
736 * The last variable argument must be %NULL (or 0).
738 * Free-function: gst_structure_free
740 * Returns: (transfer full): a new #GstStructure
743 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
748 g_return_val_if_fail (name_quark != 0, NULL);
749 g_return_val_if_fail (field_quark != 0, NULL);
751 s = gst_structure_new_id_empty (name_quark);
753 va_start (varargs, field_quark);
754 gst_structure_id_set_valist_internal (s, field_quark, varargs);
760 #if GST_VERSION_NANO == 1
761 #define GIT_G_WARNING g_warning
763 #define GIT_G_WARNING GST_WARNING
766 /* If the structure currently contains a field with the same name, it is
767 * replaced with the provided field. Otherwise, the field is added to the
768 * structure. The field's value is not deeply copied.
771 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
773 GstStructureField *f;
774 GType field_value_type;
777 len = GST_STRUCTURE_FIELDS (structure)->len;
779 field_value_type = G_VALUE_TYPE (&field->value);
780 if (field_value_type == G_TYPE_STRING) {
783 s = g_value_get_string (&field->value);
784 /* only check for NULL strings in taglists, as they are allowed in message
785 * structs, e.g. error message debug strings */
786 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
788 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
789 "Please file a bug.", g_quark_to_string (field->name));
790 g_value_unset (&field->value);
793 /* empty strings never make sense */
794 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
795 "Please file a bug.", g_quark_to_string (field->name));
796 g_value_unset (&field->value);
799 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
800 g_warning ("Trying to set string on %s field '%s', but string is not "
801 "valid UTF-8. Please file a bug.",
802 IS_TAGLIST (structure) ? "taglist" : "structure",
803 g_quark_to_string (field->name));
804 g_value_unset (&field->value);
807 } else if (G_UNLIKELY (field_value_type == G_TYPE_DATE)) {
810 d = g_value_get_boxed (&field->value);
811 /* only check for NULL GDates in taglists, as they might make sense
812 * in other, generic structs */
813 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
814 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
815 "Please file a bug.", g_quark_to_string (field->name));
816 g_value_unset (&field->value);
818 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
820 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
821 IS_TAGLIST (structure) ? "taglist" : "structure",
822 g_quark_to_string (field->name));
823 g_value_unset (&field->value);
828 for (i = 0; i < len; i++) {
829 f = GST_STRUCTURE_FIELD (structure, i);
831 if (G_UNLIKELY (f->name == field->name)) {
832 g_value_unset (&f->value);
833 memcpy (f, field, sizeof (GstStructureField));
838 g_array_append_val (GST_STRUCTURE_FIELDS (structure), *field);
841 /* If there is no field with the given ID, NULL is returned.
843 static GstStructureField *
844 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
846 GstStructureField *field;
849 len = GST_STRUCTURE_FIELDS (structure)->len;
851 for (i = 0; i < len; i++) {
852 field = GST_STRUCTURE_FIELD (structure, i);
854 if (G_UNLIKELY (field->name == field_id))
861 /* If there is no field with the given ID, NULL is returned.
863 static GstStructureField *
864 gst_structure_get_field (const GstStructure * structure,
865 const gchar * fieldname)
867 g_return_val_if_fail (structure != NULL, NULL);
868 g_return_val_if_fail (fieldname != NULL, NULL);
870 return gst_structure_id_get_field (structure,
871 g_quark_from_string (fieldname));
875 * gst_structure_get_value:
876 * @structure: a #GstStructure
877 * @fieldname: the name of the field to get
879 * Get the value of the field with name @fieldname.
881 * Returns: the #GValue corresponding to the field with the given name.
884 gst_structure_get_value (const GstStructure * structure,
885 const gchar * fieldname)
887 GstStructureField *field;
889 g_return_val_if_fail (structure != NULL, NULL);
890 g_return_val_if_fail (fieldname != NULL, NULL);
892 field = gst_structure_get_field (structure, fieldname);
896 return &field->value;
900 * gst_structure_id_get_value:
901 * @structure: a #GstStructure
902 * @field: the #GQuark of the field to get
904 * Get the value of the field with GQuark @field.
906 * Returns: the #GValue corresponding to the field with the given name
910 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
912 GstStructureField *gsfield;
914 g_return_val_if_fail (structure != NULL, NULL);
916 gsfield = gst_structure_id_get_field (structure, field);
920 return &gsfield->value;
924 * gst_structure_remove_field:
925 * @structure: a #GstStructure
926 * @fieldname: the name of the field to remove
928 * Removes the field with the given name. If the field with the given
929 * name does not exist, the structure is unchanged.
932 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
934 GstStructureField *field;
938 g_return_if_fail (structure != NULL);
939 g_return_if_fail (fieldname != NULL);
940 g_return_if_fail (IS_MUTABLE (structure));
942 id = g_quark_from_string (fieldname);
943 len = GST_STRUCTURE_FIELDS (structure)->len;
945 for (i = 0; i < len; i++) {
946 field = GST_STRUCTURE_FIELD (structure, i);
948 if (field->name == id) {
949 if (G_IS_VALUE (&field->value)) {
950 g_value_unset (&field->value);
952 GST_STRUCTURE_FIELDS (structure) =
953 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
960 * gst_structure_remove_fields:
961 * @structure: a #GstStructure
962 * @fieldname: the name of the field to remove
963 * @...: %NULL-terminated list of more fieldnames to remove
965 * Removes the fields with the given names. If a field does not exist, the
966 * argument is ignored.
969 gst_structure_remove_fields (GstStructure * structure,
970 const gchar * fieldname, ...)
974 g_return_if_fail (structure != NULL);
975 g_return_if_fail (fieldname != NULL);
976 /* mutability checked in remove_field */
978 va_start (varargs, fieldname);
979 gst_structure_remove_fields_valist (structure, fieldname, varargs);
984 * gst_structure_remove_fields_valist:
985 * @structure: a #GstStructure
986 * @fieldname: the name of the field to remove
987 * @varargs: %NULL-terminated list of more fieldnames to remove
989 * va_list form of gst_structure_remove_fields().
992 gst_structure_remove_fields_valist (GstStructure * structure,
993 const gchar * fieldname, va_list varargs)
995 gchar *field = (gchar *) fieldname;
997 g_return_if_fail (structure != NULL);
998 g_return_if_fail (fieldname != NULL);
999 /* mutability checked in remove_field */
1002 gst_structure_remove_field (structure, field);
1003 field = va_arg (varargs, char *);
1008 * gst_structure_remove_all_fields:
1009 * @structure: a #GstStructure
1011 * Removes all fields in a GstStructure.
1014 gst_structure_remove_all_fields (GstStructure * structure)
1016 GstStructureField *field;
1019 g_return_if_fail (structure != NULL);
1020 g_return_if_fail (IS_MUTABLE (structure));
1022 for (i = GST_STRUCTURE_FIELDS (structure)->len - 1; i >= 0; i--) {
1023 field = GST_STRUCTURE_FIELD (structure, i);
1025 if (G_IS_VALUE (&field->value)) {
1026 g_value_unset (&field->value);
1028 GST_STRUCTURE_FIELDS (structure) =
1029 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1034 * gst_structure_get_field_type:
1035 * @structure: a #GstStructure
1036 * @fieldname: the name of the field
1038 * Finds the field with the given name, and returns the type of the
1039 * value it contains. If the field is not found, G_TYPE_INVALID is
1042 * Returns: the #GValue of the field
1045 gst_structure_get_field_type (const GstStructure * structure,
1046 const gchar * fieldname)
1048 GstStructureField *field;
1050 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1051 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1053 field = gst_structure_get_field (structure, fieldname);
1055 return G_TYPE_INVALID;
1057 return G_VALUE_TYPE (&field->value);
1061 * gst_structure_n_fields:
1062 * @structure: a #GstStructure
1064 * Get the number of fields in the structure.
1066 * Returns: the number of fields in the structure
1069 gst_structure_n_fields (const GstStructure * structure)
1071 g_return_val_if_fail (structure != NULL, 0);
1073 return GST_STRUCTURE_FIELDS (structure)->len;
1077 * gst_structure_nth_field_name:
1078 * @structure: a #GstStructure
1079 * @index: the index to get the name of
1081 * Get the name of the given field number, counting from 0 onwards.
1083 * Returns: the name of the given field number
1086 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1088 GstStructureField *field;
1090 g_return_val_if_fail (structure != NULL, NULL);
1091 g_return_val_if_fail (index < GST_STRUCTURE_FIELDS (structure)->len, NULL);
1093 field = GST_STRUCTURE_FIELD (structure, index);
1095 return g_quark_to_string (field->name);
1099 * gst_structure_foreach:
1100 * @structure: a #GstStructure
1101 * @func: (scope call): a function to call for each field
1102 * @user_data: (closure): private data
1104 * Calls the provided function once for each field in the #GstStructure. The
1105 * function must not modify the fields. Also see gst_structure_map_in_place()
1106 * and gst_structure_filter_and_map_in_place().
1108 * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
1112 gst_structure_foreach (const GstStructure * structure,
1113 GstStructureForeachFunc func, gpointer user_data)
1116 GstStructureField *field;
1119 g_return_val_if_fail (structure != NULL, FALSE);
1120 g_return_val_if_fail (func != NULL, FALSE);
1122 len = GST_STRUCTURE_FIELDS (structure)->len;
1124 for (i = 0; i < len; i++) {
1125 field = GST_STRUCTURE_FIELD (structure, i);
1127 ret = func (field->name, &field->value, user_data);
1128 if (G_UNLIKELY (!ret))
1136 * gst_structure_map_in_place:
1137 * @structure: a #GstStructure
1138 * @func: (scope call): a function to call for each field
1139 * @user_data: (closure): private data
1141 * Calls the provided function once for each field in the #GstStructure. In
1142 * contrast to gst_structure_foreach(), the function may modify but not delete the
1143 * fields. The structure must be mutable.
1145 * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
1149 gst_structure_map_in_place (GstStructure * structure,
1150 GstStructureMapFunc func, gpointer user_data)
1153 GstStructureField *field;
1156 g_return_val_if_fail (structure != NULL, FALSE);
1157 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1158 g_return_val_if_fail (func != NULL, FALSE);
1159 len = GST_STRUCTURE_FIELDS (structure)->len;
1161 for (i = 0; i < len; i++) {
1162 field = GST_STRUCTURE_FIELD (structure, i);
1164 ret = func (field->name, &field->value, user_data);
1173 * gst_structure_filter_and_map_in_place:
1174 * @structure: a #GstStructure
1175 * @func: (scope call): a function to call for each field
1176 * @user_data: (closure): private data
1178 * Calls the provided function once for each field in the #GstStructure. In
1179 * contrast to gst_structure_foreach(), the function may modify the fields.
1180 * In contrast to gst_structure_map_in_place(), the field is removed from
1181 * the structure if %FALSE is returned from the function.
1182 * The structure must be mutable.
1187 gst_structure_filter_and_map_in_place (GstStructure * structure,
1188 GstStructureFilterMapFunc func, gpointer user_data)
1191 GstStructureField *field;
1194 g_return_if_fail (structure != NULL);
1195 g_return_if_fail (IS_MUTABLE (structure));
1196 g_return_if_fail (func != NULL);
1197 len = GST_STRUCTURE_FIELDS (structure)->len;
1199 for (i = 0; i < len;) {
1200 field = GST_STRUCTURE_FIELD (structure, i);
1202 ret = func (field->name, &field->value, user_data);
1205 if (G_IS_VALUE (&field->value)) {
1206 g_value_unset (&field->value);
1208 GST_STRUCTURE_FIELDS (structure) =
1209 g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
1210 len = GST_STRUCTURE_FIELDS (structure)->len;
1218 * gst_structure_id_has_field:
1219 * @structure: a #GstStructure
1220 * @field: #GQuark of the field name
1222 * Check if @structure contains a field named @field.
1224 * Returns: %TRUE if the structure contains a field with the given name
1227 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
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);
1240 * gst_structure_has_field:
1241 * @structure: a #GstStructure
1242 * @fieldname: the name of a field
1244 * Check if @structure contains a field named @fieldname.
1246 * Returns: %TRUE if the structure contains a field with the given name
1249 gst_structure_has_field (const GstStructure * structure,
1250 const gchar * fieldname)
1252 g_return_val_if_fail (structure != NULL, FALSE);
1253 g_return_val_if_fail (fieldname != NULL, FALSE);
1255 return gst_structure_id_has_field (structure,
1256 g_quark_from_string (fieldname));
1260 * gst_structure_id_has_field_typed:
1261 * @structure: a #GstStructure
1262 * @field: #GQuark of the field name
1263 * @type: the type of a value
1265 * Check if @structure contains a field named @field and with GType @type.
1267 * Returns: %TRUE if the structure contains a field with the given name and type
1270 gst_structure_id_has_field_typed (const GstStructure * structure,
1271 GQuark field, GType type)
1273 GstStructureField *f;
1275 g_return_val_if_fail (structure != NULL, FALSE);
1276 g_return_val_if_fail (field != 0, FALSE);
1278 f = gst_structure_id_get_field (structure, field);
1282 return (G_VALUE_TYPE (&f->value) == type);
1286 * gst_structure_has_field_typed:
1287 * @structure: a #GstStructure
1288 * @fieldname: the name of a field
1289 * @type: the type of a value
1291 * Check if @structure contains a field named @fieldname and with GType @type.
1293 * Returns: %TRUE if the structure contains a field with the given name and type
1296 gst_structure_has_field_typed (const GstStructure * structure,
1297 const gchar * fieldname, GType type)
1299 g_return_val_if_fail (structure != NULL, FALSE);
1300 g_return_val_if_fail (fieldname != NULL, FALSE);
1302 return gst_structure_id_has_field_typed (structure,
1303 g_quark_from_string (fieldname), type);
1306 /* utility functions */
1309 * gst_structure_get_boolean:
1310 * @structure: a #GstStructure
1311 * @fieldname: the name of a field
1312 * @value: (out): a pointer to a #gboolean to set
1314 * Sets the boolean pointed to by @value corresponding to the value of the
1315 * given field. Caller is responsible for making sure the field exists
1316 * and has the correct type.
1318 * Returns: %TRUE if the value could be set correctly. If there was no field
1319 * with @fieldname or the existing field did not contain a boolean, this
1320 * function returns %FALSE.
1323 gst_structure_get_boolean (const GstStructure * structure,
1324 const gchar * fieldname, gboolean * value)
1326 GstStructureField *field;
1328 g_return_val_if_fail (structure != NULL, FALSE);
1329 g_return_val_if_fail (fieldname != NULL, FALSE);
1331 field = gst_structure_get_field (structure, fieldname);
1333 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_BOOLEAN)
1336 *value = gst_g_value_get_boolean_unchecked (&field->value);
1342 * gst_structure_get_int:
1343 * @structure: a #GstStructure
1344 * @fieldname: the name of a field
1345 * @value: (out): a pointer to an int to set
1347 * Sets the int pointed to by @value corresponding to the value of the
1348 * given field. Caller is responsible for making sure the field exists
1349 * and has the correct type.
1351 * Returns: %TRUE if the value could be set correctly. If there was no field
1352 * with @fieldname or the existing field did not contain an int, this function
1356 gst_structure_get_int (const GstStructure * structure,
1357 const gchar * fieldname, gint * value)
1359 GstStructureField *field;
1361 g_return_val_if_fail (structure != NULL, FALSE);
1362 g_return_val_if_fail (fieldname != NULL, FALSE);
1363 g_return_val_if_fail (value != NULL, FALSE);
1365 field = gst_structure_get_field (structure, fieldname);
1367 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT)
1370 *value = gst_g_value_get_int_unchecked (&field->value);
1376 * gst_structure_get_uint:
1377 * @structure: a #GstStructure
1378 * @fieldname: the name of a field
1379 * @value: (out): a pointer to a uint to set
1381 * Sets the uint pointed to by @value corresponding to the value of the
1382 * given field. Caller is responsible for making sure the field exists
1383 * and has the correct type.
1385 * Returns: %TRUE if the value could be set correctly. If there was no field
1386 * with @fieldname or the existing field did not contain a uint, this function
1390 gst_structure_get_uint (const GstStructure * structure,
1391 const gchar * fieldname, guint * value)
1393 GstStructureField *field;
1395 g_return_val_if_fail (structure != NULL, FALSE);
1396 g_return_val_if_fail (fieldname != NULL, FALSE);
1397 g_return_val_if_fail (value != NULL, FALSE);
1399 field = gst_structure_get_field (structure, fieldname);
1401 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT)
1404 *value = gst_g_value_get_uint_unchecked (&field->value);
1410 * gst_structure_get_int64:
1411 * @structure: a #GstStructure
1412 * @fieldname: the name of a field
1413 * @value: (out): a pointer to a #gint64 to set
1415 * Sets the #gint64 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 #gint64, this function
1426 gst_structure_get_int64 (const GstStructure * structure,
1427 const gchar * fieldname, gint64 * 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);
1437 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT64)
1440 *value = gst_g_value_get_int64_unchecked (&field->value);
1446 * gst_structure_get_uint64:
1447 * @structure: a #GstStructure
1448 * @fieldname: the name of a field
1449 * @value: (out): a pointer to a #guint64 to set
1451 * Sets the #guint64 pointed to by @value corresponding to the value of the
1452 * given field. Caller is responsible for making sure the field exists
1453 * and has the correct type.
1455 * Returns: %TRUE if the value could be set correctly. If there was no field
1456 * with @fieldname or the existing field did not contain a #guint64, this function
1462 gst_structure_get_uint64 (const GstStructure * structure,
1463 const gchar * fieldname, guint64 * value)
1465 GstStructureField *field;
1467 g_return_val_if_fail (structure != NULL, FALSE);
1468 g_return_val_if_fail (fieldname != NULL, FALSE);
1469 g_return_val_if_fail (value != NULL, FALSE);
1471 field = gst_structure_get_field (structure, fieldname);
1473 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT64)
1476 *value = gst_g_value_get_uint64_unchecked (&field->value);
1482 * gst_structure_get_date:
1483 * @structure: a #GstStructure
1484 * @fieldname: the name of a field
1485 * @value: (out callee-allocates): a pointer to a #GDate to set
1487 * Sets the date pointed to by @value corresponding to the date of the
1488 * given field. Caller is responsible for making sure the field exists
1489 * and has the correct type.
1491 * On success @value will point to a newly-allocated copy of the date which
1492 * should be freed with g_date_free() when no longer needed (note: this is
1493 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1494 * copy of the string).
1496 * Returns: %TRUE if the value could be set correctly. If there was no field
1497 * with @fieldname or the existing field did not contain a data, this function
1501 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1504 GstStructureField *field;
1506 g_return_val_if_fail (structure != NULL, FALSE);
1507 g_return_val_if_fail (fieldname != NULL, FALSE);
1508 g_return_val_if_fail (value != NULL, FALSE);
1510 field = gst_structure_get_field (structure, fieldname);
1512 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DATE)
1515 /* FIXME: 2.0 g_value_dup_boxed() -> g_value_get_boxed() */
1516 *value = g_value_dup_boxed (&field->value);
1522 * gst_structure_get_date_time:
1523 * @structure: a #GstStructure
1524 * @fieldname: the name of a field
1525 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1527 * Sets the datetime pointed to by @value corresponding to the datetime of the
1528 * given field. Caller is responsible for making sure the field exists
1529 * and has the correct type.
1531 * On success @value will point to a reference of the datetime which
1532 * should be unreffed with gst_date_time_unref() when no longer needed
1533 * (note: this is inconsistent with e.g. gst_structure_get_string()
1534 * which doesn't return a copy of the string).
1536 * Returns: %TRUE if the value could be set correctly. If there was no field
1537 * with @fieldname or the existing field did not contain a data, this function
1541 gst_structure_get_date_time (const GstStructure * structure,
1542 const gchar * fieldname, GstDateTime ** value)
1544 GstStructureField *field;
1546 g_return_val_if_fail (structure != NULL, FALSE);
1547 g_return_val_if_fail (fieldname != NULL, FALSE);
1548 g_return_val_if_fail (value != NULL, FALSE);
1550 field = gst_structure_get_field (structure, fieldname);
1554 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1557 /* FIXME 2.0: g_value_dup_boxed() -> g_value_get_boxed() */
1558 *value = g_value_dup_boxed (&field->value);
1564 * gst_structure_get_clock_time:
1565 * @structure: a #GstStructure
1566 * @fieldname: the name of a field
1567 * @value: (out): a pointer to a #GstClockTime to set
1569 * Sets the clock time pointed to by @value corresponding to the clock time
1570 * of the given field. Caller is responsible for making sure the field exists
1571 * and has the correct type.
1573 * Returns: %TRUE if the value could be set correctly. If there was no field
1574 * with @fieldname or the existing field did not contain a #GstClockTime, this
1575 * function returns %FALSE.
1578 gst_structure_get_clock_time (const GstStructure * structure,
1579 const gchar * fieldname, GstClockTime * value)
1581 return gst_structure_get_uint64 (structure, fieldname, value);
1585 * gst_structure_get_double:
1586 * @structure: a #GstStructure
1587 * @fieldname: the name of a field
1588 * @value: (out): a pointer to a gdouble to set
1590 * Sets the double pointed to by @value corresponding to the value of the
1591 * given field. Caller is responsible for making sure the field exists
1592 * and has the correct type.
1594 * Returns: %TRUE if the value could be set correctly. If there was no field
1595 * with @fieldname or the existing field did not contain a double, this
1596 * function returns %FALSE.
1599 gst_structure_get_double (const GstStructure * structure,
1600 const gchar * fieldname, gdouble * value)
1602 GstStructureField *field;
1604 g_return_val_if_fail (structure != NULL, FALSE);
1605 g_return_val_if_fail (fieldname != NULL, FALSE);
1606 g_return_val_if_fail (value != NULL, FALSE);
1608 field = gst_structure_get_field (structure, fieldname);
1610 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DOUBLE)
1613 *value = gst_g_value_get_double_unchecked (&field->value);
1619 * gst_structure_get_string:
1620 * @structure: a #GstStructure
1621 * @fieldname: the name of a field
1623 * Finds the field corresponding to @fieldname, and returns the string
1624 * contained in the field's value. Caller is responsible for making
1625 * sure the field exists and has the correct type.
1627 * The string should not be modified, and remains valid until the next
1628 * call to a gst_structure_*() function with the given structure.
1630 * Returns: (nullable): a pointer to the string or %NULL when the
1631 * field did not exist or did not contain a string.
1634 gst_structure_get_string (const GstStructure * structure,
1635 const gchar * fieldname)
1637 GstStructureField *field;
1639 g_return_val_if_fail (structure != NULL, NULL);
1640 g_return_val_if_fail (fieldname != NULL, NULL);
1642 field = gst_structure_get_field (structure, fieldname);
1644 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_STRING)
1647 return gst_g_value_get_string_unchecked (&field->value);
1651 * gst_structure_get_enum:
1652 * @structure: a #GstStructure
1653 * @fieldname: the name of a field
1654 * @enumtype: the enum type of a field
1655 * @value: (out): a pointer to an int to set
1657 * Sets the int pointed to by @value corresponding to the value of the
1658 * given field. Caller is responsible for making sure the field exists,
1659 * has the correct type and that the enumtype is correct.
1661 * Returns: %TRUE if the value could be set correctly. If there was no field
1662 * with @fieldname or the existing field did not contain an enum of the given
1663 * type, this function returns %FALSE.
1666 gst_structure_get_enum (const GstStructure * structure,
1667 const gchar * fieldname, GType enumtype, gint * value)
1669 GstStructureField *field;
1671 g_return_val_if_fail (structure != NULL, FALSE);
1672 g_return_val_if_fail (fieldname != NULL, FALSE);
1673 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1674 g_return_val_if_fail (value != NULL, FALSE);
1676 field = gst_structure_get_field (structure, fieldname);
1680 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1683 *value = g_value_get_enum (&field->value);
1689 * gst_structure_get_fraction:
1690 * @structure: a #GstStructure
1691 * @fieldname: the name of a field
1692 * @value_numerator: (out): a pointer to an int to set
1693 * @value_denominator: (out): a pointer to an int to set
1695 * Sets the integers pointed to by @value_numerator and @value_denominator
1696 * corresponding to the value of the given field. Caller is responsible
1697 * for making sure the field exists and has the correct type.
1699 * Returns: %TRUE if the values could be set correctly. If there was no field
1700 * with @fieldname or the existing field did not contain a GstFraction, this
1701 * function returns %FALSE.
1704 gst_structure_get_fraction (const GstStructure * structure,
1705 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1707 GstStructureField *field;
1709 g_return_val_if_fail (structure != NULL, FALSE);
1710 g_return_val_if_fail (fieldname != NULL, FALSE);
1711 g_return_val_if_fail (value_numerator != NULL, FALSE);
1712 g_return_val_if_fail (value_denominator != NULL, FALSE);
1714 field = gst_structure_get_field (structure, fieldname);
1716 if (field == NULL || G_VALUE_TYPE (&field->value) != GST_TYPE_FRACTION)
1719 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1720 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1726 * gst_structure_get_flagset:
1727 * @structure: a #GstStructure
1728 * @fieldname: the name of a field
1729 * @value_flags: (out) (allow-none): a pointer to a guint for the flags field
1730 * @value_mask: (out) (allow-none): a pointer to a guint for the mask field
1732 * Read the GstFlagSet flags and mask out of the structure into the
1733 * provided pointers.
1735 * Returns: %TRUE if the values could be set correctly. If there was no field
1736 * with @fieldname or the existing field did not contain a GstFlagSet, this
1737 * function returns %FALSE.
1742 gst_structure_get_flagset (const GstStructure * structure,
1743 const gchar * fieldname, guint * value_flags, guint * value_mask)
1745 GstStructureField *field;
1747 g_return_val_if_fail (structure != NULL, FALSE);
1748 g_return_val_if_fail (fieldname != NULL, FALSE);
1750 field = gst_structure_get_field (structure, fieldname);
1752 if (field == NULL || !GST_VALUE_HOLDS_FLAG_SET (&field->value))
1756 *value_flags = gst_value_get_flagset_flags (&field->value);
1758 *value_mask = gst_value_get_flagset_mask (&field->value);
1763 typedef struct _GstStructureAbbreviation
1765 const gchar *type_name;
1768 GstStructureAbbreviation;
1770 /* return a copy of an array of GstStructureAbbreviation containing all the
1771 * known type_string, GType maps, including abbreviations for common types */
1772 static GstStructureAbbreviation *
1773 gst_structure_get_abbrs (gint * n_abbrs)
1775 static GstStructureAbbreviation *abbrs = NULL;
1776 static volatile gsize num = 0;
1778 if (g_once_init_enter (&num)) {
1779 /* dynamically generate the array */
1781 GstStructureAbbreviation dyn_abbrs[] = {
1786 {"uint", G_TYPE_UINT}
1790 {"float", G_TYPE_FLOAT}
1794 {"double", G_TYPE_DOUBLE}
1796 {"d", G_TYPE_DOUBLE}
1798 {"buffer", GST_TYPE_BUFFER}
1800 {"fraction", GST_TYPE_FRACTION}
1802 {"boolean", G_TYPE_BOOLEAN}
1804 {"bool", G_TYPE_BOOLEAN}
1806 {"b", G_TYPE_BOOLEAN}
1808 {"string", G_TYPE_STRING}
1810 {"str", G_TYPE_STRING}
1812 {"s", G_TYPE_STRING}
1814 {"structure", GST_TYPE_STRUCTURE}
1816 {"date", G_TYPE_DATE}
1818 {"datetime", GST_TYPE_DATE_TIME}
1820 {"bitmask", GST_TYPE_BITMASK}
1822 {"sample", GST_TYPE_SAMPLE}
1824 {"taglist", GST_TYPE_TAG_LIST}
1826 {"type", G_TYPE_GTYPE}
1828 _num = G_N_ELEMENTS (dyn_abbrs);
1829 /* permanently allocate and copy the array now */
1830 abbrs = g_new0 (GstStructureAbbreviation, _num);
1831 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1832 g_once_init_leave (&num, _num);
1839 /* given a type_name that could be a type abbreviation or a registered GType,
1840 * return a matching GType */
1842 gst_structure_gtype_from_abbr (const char *type_name)
1845 GstStructureAbbreviation *abbrs;
1849 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1851 abbrs = gst_structure_get_abbrs (&n_abbrs);
1853 for (i = 0; i < n_abbrs; i++) {
1854 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1855 return abbrs[i].type;
1859 /* this is the fallback */
1860 ret = g_type_from_name (type_name);
1861 /* If not found, try it as a dynamic type */
1862 if (G_UNLIKELY (ret == 0))
1863 ret = gst_dynamic_type_factory_load (type_name);
1868 gst_structure_to_abbr (GType type)
1871 GstStructureAbbreviation *abbrs;
1874 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1876 abbrs = gst_structure_get_abbrs (&n_abbrs);
1878 for (i = 0; i < n_abbrs; i++) {
1879 if (type == abbrs[i].type) {
1880 return abbrs[i].type_name;
1884 return g_type_name (type);
1888 gst_structure_value_get_generic_type (const GValue * val)
1890 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1891 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1892 GArray *array = g_value_peek_pointer (val);
1894 if (array->len > 0) {
1895 GValue *value = &g_array_index (array, GValue, 0);
1897 return gst_structure_value_get_generic_type (value);
1901 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1903 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1904 return G_TYPE_INT64;
1905 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1906 return G_TYPE_DOUBLE;
1907 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1908 return GST_TYPE_FRACTION;
1910 return G_VALUE_TYPE (val);
1914 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1917 GstStructureField *field;
1920 g_return_val_if_fail (s != NULL, FALSE);
1922 len = GST_STRUCTURE_FIELDS (structure)->len;
1923 for (i = 0; i < len; i++) {
1927 field = GST_STRUCTURE_FIELD (structure, i);
1929 t = gst_value_serialize (&field->value);
1930 type = gst_structure_value_get_generic_type (&field->value);
1932 g_string_append_len (s, ", ", 2);
1933 /* FIXME: do we need to escape fieldnames? */
1934 g_string_append (s, g_quark_to_string (field->name));
1935 g_string_append_len (s, "=(", 2);
1936 g_string_append (s, gst_structure_to_abbr (type));
1937 g_string_append_c (s, ')');
1939 g_string_append (s, t);
1942 GST_WARNING ("No value transform to serialize field '%s' of type '%s'",
1943 g_quark_to_string (field->name), gst_structure_to_abbr (type));
1944 g_string_append (s, "NULL");
1948 g_string_append_c (s, ';');
1953 priv__gst_structure_append_template_to_gstring (GQuark field_id,
1954 const GValue * value, gpointer user_data)
1956 GType type = gst_structure_value_get_generic_type (value);
1957 GString *s = (GString *) user_data;
1959 g_string_append_len (s, ", ", 2);
1960 /* FIXME: do we need to escape fieldnames? */
1961 g_string_append (s, g_quark_to_string (field_id));
1962 g_string_append_len (s, "=(", 2);
1963 g_string_append (s, gst_structure_to_abbr (type));
1964 g_string_append_c (s, ')');
1966 //TODO(ensonic): table like GstStructureAbbreviation (or extend it)
1967 if (type == G_TYPE_INT) {
1968 g_string_append_len (s, "%i", 2);
1969 } else if (type == G_TYPE_UINT) {
1970 g_string_append_len (s, "%u", 2);
1971 } else if (type == G_TYPE_FLOAT) {
1972 g_string_append_len (s, "%f", 2);
1973 } else if (type == G_TYPE_DOUBLE) {
1974 g_string_append_len (s, "%lf", 3);
1975 } else if (type == G_TYPE_STRING) {
1976 g_string_append_len (s, "%s", 2);
1977 } else if (type == G_TYPE_BOOLEAN) {
1978 /* we normally store this as a string, but can parse it also from an int */
1979 g_string_append_len (s, "%i", 2);
1980 } else if (type == G_TYPE_INT64) {
1981 g_string_append (s, "%" G_GINT64_FORMAT);
1982 } else if (type == G_TYPE_UINT64) {
1983 g_string_append (s, "%" G_GUINT64_FORMAT);
1984 } else if (type == GST_TYPE_STRUCTURE) {
1985 g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
1986 } else if (g_type_is_a (type, G_TYPE_ENUM)
1987 || g_type_is_a (type, G_TYPE_FLAGS)) {
1988 g_string_append_len (s, "%i", 2);
1989 } else if (type == G_TYPE_GTYPE) {
1990 g_string_append_len (s, "%s", 2);
1991 } else if (type == G_TYPE_POINTER) {
1992 g_string_append_len (s, "%p", 2);
1994 GST_WARNING ("unhandled type: %s", g_type_name (type));
1995 g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
2002 * gst_structure_to_string:
2003 * @structure: a #GstStructure
2005 * Converts @structure to a human-readable string representation.
2007 * For debugging purposes its easier to do something like this:
2008 * |[<!-- language="C" -->
2009 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
2011 * This prints the structure in human readable form.
2013 * The current implementation of serialization will lead to unexpected results
2014 * when there are nested #GstCaps / #GstStructure deeper than one level.
2016 * Free-function: g_free
2018 * Returns: (transfer full): a pointer to string allocated by g_malloc().
2019 * g_free() after usage.
2022 gst_structure_to_string (const GstStructure * structure)
2026 /* NOTE: This function is potentially called by the debug system,
2027 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
2028 * should be careful to avoid recursion. This includes any functions
2029 * called by gst_structure_to_string. In particular, calls should
2030 * not use the GST_PTR_FORMAT extension. */
2032 g_return_val_if_fail (structure != NULL, NULL);
2034 /* we estimate a minimum size based on the number of fields in order to
2035 * avoid unnecessary reallocs within GString */
2036 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
2037 g_string_append (s, g_quark_to_string (structure->name));
2038 priv_gst_structure_append_to_gstring (structure, s);
2039 return g_string_free (s, FALSE);
2043 * gst_structure_parse_string:
2044 * @s: string to parse
2045 * @end: out-pointer to char behind end of string
2046 * @next: out-pointer to start of unread data
2047 * @unescape: @TRUE if the substring is escaped.
2049 * Find the end of a sub-string. If end == next, the string will not be
2050 * null-terminated. In all other cases it will be.
2052 * Note: This function modifies the string in @s (if unescape == @TRUE).
2054 * Returns: @TRUE if a sub-string was found and @FALSE if the string is not
2058 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
2067 int ret = gst_structure_parse_simple_string (s, end);
2073 /* Find the closing quotes */
2078 if (G_UNLIKELY (*s == 0))
2080 if (G_UNLIKELY (*s == '\\')) {
2082 if (G_UNLIKELY (*s == 0))
2093 if (G_UNLIKELY (*s == 0))
2095 if (G_UNLIKELY (*s == '\\')) {
2097 if (G_UNLIKELY (*s == 0))
2113 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
2116 GValue value1 = { 0 };
2117 GValue value2 = { 0 };
2118 GValue value3 = { 0 };
2120 gboolean ret, have_step = FALSE;
2126 ret = gst_structure_parse_value (s, &s, &value1, type);
2130 while (g_ascii_isspace (*s))
2137 while (g_ascii_isspace (*s))
2140 ret = gst_structure_parse_value (s, &s, &value2, type);
2144 while (g_ascii_isspace (*s))
2147 /* optional step for int and int64 */
2148 if (G_VALUE_TYPE (&value1) == G_TYPE_INT
2149 || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2153 while (g_ascii_isspace (*s))
2156 ret = gst_structure_parse_value (s, &s, &value3, type);
2160 while (g_ascii_isspace (*s))
2171 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
2173 if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
2176 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
2177 range_type = GST_TYPE_DOUBLE_RANGE;
2178 g_value_init (value, range_type);
2179 gst_value_set_double_range (value,
2180 gst_g_value_get_double_unchecked (&value1),
2181 gst_g_value_get_double_unchecked (&value2));
2182 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
2183 range_type = GST_TYPE_INT_RANGE;
2184 g_value_init (value, range_type);
2186 gst_value_set_int_range_step (value,
2187 gst_g_value_get_int_unchecked (&value1),
2188 gst_g_value_get_int_unchecked (&value2),
2189 gst_g_value_get_int_unchecked (&value3));
2191 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
2192 gst_g_value_get_int_unchecked (&value2));
2193 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
2194 range_type = GST_TYPE_INT64_RANGE;
2195 g_value_init (value, range_type);
2197 gst_value_set_int64_range_step (value,
2198 gst_g_value_get_int64_unchecked (&value1),
2199 gst_g_value_get_int64_unchecked (&value2),
2200 gst_g_value_get_int64_unchecked (&value3));
2202 gst_value_set_int64_range (value,
2203 gst_g_value_get_int64_unchecked (&value1),
2204 gst_g_value_get_int64_unchecked (&value2));
2205 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
2206 range_type = GST_TYPE_FRACTION_RANGE;
2207 g_value_init (value, range_type);
2208 gst_value_set_fraction_range (value, &value1, &value2);
2218 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
2219 GType type, GType list_type, char begin, char end)
2221 GValue list_value = { 0 };
2225 g_value_init (value, list_type);
2226 array = g_value_peek_pointer (value);
2232 while (g_ascii_isspace (*s))
2240 ret = gst_structure_parse_value (s, &s, &list_value, type);
2244 g_array_append_val (array, list_value);
2246 while (g_ascii_isspace (*s))
2254 while (g_ascii_isspace (*s))
2257 memset (&list_value, 0, sizeof (list_value));
2258 ret = gst_structure_parse_value (s, &s, &list_value, type);
2262 g_array_append_val (array, list_value);
2263 while (g_ascii_isspace (*s))
2274 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2276 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2281 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2284 return gst_structure_parse_any_list (s, after, value, type,
2285 GST_TYPE_ARRAY, '<', '>');
2289 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2293 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2303 gst_structure_parse_field (gchar * str,
2304 gchar ** after, GstStructureField * field)
2313 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2316 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2317 GST_WARNING ("failed to parse simple string, str=%s", str);
2322 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2325 if (G_UNLIKELY (*s != '=')) {
2326 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2333 field->name = g_quark_from_string (name);
2334 GST_DEBUG ("trying field name '%s'", name);
2337 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2339 GST_WARNING ("failed to parse value %s", str);
2348 gst_structure_parse_value (gchar * str,
2349 gchar ** after, GValue * value, GType default_type)
2358 GType type = default_type;
2361 while (g_ascii_isspace (*s))
2364 /* check if there's a (type_name) 'cast' */
2368 while (g_ascii_isspace (*s))
2371 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2374 while (g_ascii_isspace (*s))
2376 if (G_UNLIKELY (*s != ')'))
2379 while (g_ascii_isspace (*s))
2384 type = gst_structure_gtype_from_abbr (type_name);
2385 GST_DEBUG ("trying type name '%s'", type_name);
2388 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2389 GST_WARNING ("invalid type");
2394 while (g_ascii_isspace (*s))
2397 ret = gst_structure_parse_range (s, &s, value, type);
2398 } else if (*s == '{') {
2399 ret = gst_structure_parse_list (s, &s, value, type);
2400 } else if (*s == '<') {
2401 ret = gst_structure_parse_array (s, &s, value, type);
2405 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2407 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, GST_TYPE_FLAG_SET,
2408 G_TYPE_BOOLEAN, G_TYPE_STRING
2412 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2414 /* Set NULL terminator for deserialization */
2418 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2419 g_value_init (value, try_types[i]);
2420 ret = gst_value_deserialize (value, value_s);
2423 g_value_unset (value);
2426 g_value_init (value, type);
2428 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2429 (type != G_TYPE_STRING))))
2431 /* Set NULL terminator for deserialization */
2435 ret = gst_value_deserialize (value, value_s);
2436 if (G_UNLIKELY (!ret))
2437 g_value_unset (value);
2448 priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end,
2456 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2457 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2458 && g_ascii_isspace (r[1]))))
2463 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2464 GST_WARNING ("Failed to parse structure string '%s'", str);
2475 priv_gst_structure_parse_fields (gchar * str, gchar ** end,
2476 GstStructure * structure)
2479 GstStructureField field;
2484 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2485 && g_ascii_isspace (r[1]))))
2488 /* end of structure, get the next char and finish */
2493 /* accept \0 as end delimiter */
2496 if (G_UNLIKELY (*r != ',')) {
2497 GST_WARNING ("Failed to find delimiter, r=%s", r);
2501 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2502 && g_ascii_isspace (r[1]))))
2505 memset (&field, 0, sizeof (field));
2506 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2507 GST_WARNING ("Failed to parse field, r=%s", r);
2510 gst_structure_set_field (structure, &field);
2519 * gst_structure_new_from_string:
2520 * @string: a string representation of a #GstStructure
2522 * Creates a #GstStructure from a string representation.
2523 * If end is not %NULL, a pointer to the place inside the given string
2524 * where parsing ended will be returned.
2526 * The current implementation of serialization will lead to unexpected results
2527 * when there are nested #GstCaps / #GstStructure deeper than one level.
2529 * Free-function: gst_structure_free
2531 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2532 * when the string could not be parsed. Free with
2533 * gst_structure_free() after use.
2538 gst_structure_new_from_string (const gchar * string)
2540 return gst_structure_from_string (string, NULL);
2544 * gst_structure_from_string:
2545 * @string: a string representation of a #GstStructure.
2546 * @end: (out) (allow-none) (transfer none) (skip): pointer to store the end of the string in.
2548 * Creates a #GstStructure from a string representation.
2549 * If end is not %NULL, a pointer to the place inside the given string
2550 * where parsing ended will be returned.
2552 * Free-function: gst_structure_free
2554 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2555 * when the string could not be parsed. Free with
2556 * gst_structure_free() after use.
2559 gst_structure_from_string (const gchar * string, gchar ** end)
2566 GstStructure *structure = NULL;
2568 g_return_val_if_fail (string != NULL, NULL);
2570 copy = g_strdup (string);
2573 if (!priv_gst_structure_parse_name (r, &name, &w, &r))
2578 structure = gst_structure_new_empty (name);
2581 if (G_UNLIKELY (structure == NULL))
2584 if (!priv_gst_structure_parse_fields (r, &r, structure))
2588 *end = (char *) string + (r - copy);
2590 g_warning ("gst_structure_from_string did not consume whole string,"
2591 " but caller did not provide end pointer (\"%s\")", string);
2598 gst_structure_free (structure);
2604 gst_structure_transform_to_string (const GValue * src_value,
2605 GValue * dest_value)
2607 g_return_if_fail (src_value != NULL);
2608 g_return_if_fail (dest_value != NULL);
2610 dest_value->data[0].v_pointer =
2611 gst_structure_to_string (src_value->data[0].v_pointer);
2614 static GstStructure *
2615 gst_structure_copy_conditional (const GstStructure * structure)
2618 return gst_structure_copy (structure);
2622 /* fixate utility functions */
2625 * gst_structure_fixate_field_nearest_int:
2626 * @structure: a #GstStructure
2627 * @field_name: a field in @structure
2628 * @target: the target value of the fixation
2630 * Fixates a #GstStructure by changing the given field to the nearest
2631 * integer to @target that is a subset of the existing field.
2633 * Returns: %TRUE if the structure could be fixated
2636 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2637 const char *field_name, int target)
2639 const GValue *value;
2641 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2642 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2644 value = gst_structure_get_value (structure, field_name);
2646 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2649 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2652 x = gst_value_get_int_range_min (value);
2655 x = gst_value_get_int_range_max (value);
2658 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2660 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2661 const GValue *list_value;
2664 int best_index = -1;
2666 n = gst_value_list_get_size (value);
2667 for (i = 0; i < n; i++) {
2668 list_value = gst_value_list_get_value (value, i);
2669 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2670 int x = gst_g_value_get_int_unchecked (list_value);
2672 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2678 if (best_index != -1) {
2679 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2689 * gst_structure_fixate_field_nearest_double:
2690 * @structure: a #GstStructure
2691 * @field_name: a field in @structure
2692 * @target: the target value of the fixation
2694 * Fixates a #GstStructure by changing the given field to the nearest
2695 * double to @target that is a subset of the existing field.
2697 * Returns: %TRUE if the structure could be fixated
2700 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2701 const char *field_name, double target)
2703 const GValue *value;
2705 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2706 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2708 value = gst_structure_get_value (structure, field_name);
2710 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2713 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2716 x = gst_value_get_double_range_min (value);
2719 x = gst_value_get_double_range_max (value);
2722 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2724 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2725 const GValue *list_value;
2728 int best_index = -1;
2730 n = gst_value_list_get_size (value);
2731 for (i = 0; i < n; i++) {
2732 list_value = gst_value_list_get_value (value, i);
2733 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2734 double x = gst_g_value_get_double_unchecked (list_value);
2736 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2742 if (best_index != -1) {
2743 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2754 * gst_structure_fixate_field_boolean:
2755 * @structure: a #GstStructure
2756 * @field_name: a field in @structure
2757 * @target: the target value of the fixation
2759 * Fixates a #GstStructure by changing the given @field_name field to the given
2760 * @target boolean if that field is not fixed yet.
2762 * Returns: %TRUE if the structure could be fixated
2765 gst_structure_fixate_field_boolean (GstStructure * structure,
2766 const char *field_name, gboolean target)
2768 const GValue *value;
2770 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2771 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2773 value = gst_structure_get_value (structure, field_name);
2775 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2778 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2779 const GValue *list_value;
2782 int best_index = -1;
2784 n = gst_value_list_get_size (value);
2785 for (i = 0; i < n; i++) {
2786 list_value = gst_value_list_get_value (value, i);
2787 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2788 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2790 if (best_index == -1 || x == target) {
2796 if (best_index != -1) {
2797 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2807 * gst_structure_fixate_field_string:
2808 * @structure: a #GstStructure
2809 * @field_name: a field in @structure
2810 * @target: the target value of the fixation
2812 * Fixates a #GstStructure by changing the given @field_name field to the given
2813 * @target string if that field is not fixed yet.
2815 * Returns: %TRUE if the structure could be fixated
2818 gst_structure_fixate_field_string (GstStructure * structure,
2819 const gchar * field_name, const gchar * target)
2821 const GValue *value;
2823 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2824 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2826 value = gst_structure_get_value (structure, field_name);
2828 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2831 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2832 const GValue *list_value;
2834 const gchar *best = NULL;
2835 int best_index = -1;
2837 n = gst_value_list_get_size (value);
2838 for (i = 0; i < n; i++) {
2839 list_value = gst_value_list_get_value (value, i);
2840 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2841 const gchar *x = g_value_get_string (list_value);
2843 if (best_index == -1 || g_str_equal (x, target)) {
2849 if (best_index != -1) {
2850 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2860 * gst_structure_fixate_field_nearest_fraction:
2861 * @structure: a #GstStructure
2862 * @field_name: a field in @structure
2863 * @target_numerator: The numerator of the target value of the fixation
2864 * @target_denominator: The denominator of the target value of the fixation
2866 * Fixates a #GstStructure by changing the given field to the nearest
2867 * fraction to @target_numerator/@target_denominator that is a subset
2868 * of the existing field.
2870 * Returns: %TRUE if the structure could be fixated
2873 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2874 const char *field_name, const gint target_numerator,
2875 const gint target_denominator)
2877 const GValue *value;
2879 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2880 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2881 g_return_val_if_fail (target_denominator != 0, FALSE);
2883 value = gst_structure_get_value (structure, field_name);
2885 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2888 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2889 const GValue *x, *new_value;
2890 GValue target = { 0 };
2891 g_value_init (&target, GST_TYPE_FRACTION);
2892 gst_value_set_fraction (&target, target_numerator, target_denominator);
2894 new_value = ⌖
2895 x = gst_value_get_fraction_range_min (value);
2896 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2898 x = gst_value_get_fraction_range_max (value);
2899 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2902 gst_structure_set_value (structure, field_name, new_value);
2903 g_value_unset (&target);
2905 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2906 const GValue *list_value;
2908 const GValue *best = NULL;
2911 gdouble best_diff = G_MAXDOUBLE;
2913 target = (gdouble) target_numerator / (gdouble) target_denominator;
2915 GST_DEBUG ("target %g, best %g", target, best_diff);
2919 n = gst_value_list_get_size (value);
2920 for (i = 0; i < n; i++) {
2921 list_value = gst_value_list_get_value (value, i);
2922 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2924 gdouble list_double;
2926 num = gst_value_get_fraction_numerator (list_value);
2927 denom = gst_value_get_fraction_denominator (list_value);
2929 list_double = ((gdouble) num / (gdouble) denom);
2930 cur_diff = target - list_double;
2932 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2935 cur_diff = -cur_diff;
2937 if (!best || cur_diff < best_diff) {
2938 GST_DEBUG ("new best %g", list_double);
2940 best_diff = cur_diff;
2945 gst_structure_set_value (structure, field_name, best);
2954 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2956 GstStructure *s = data;
2959 if (gst_value_fixate (&v, value)) {
2960 gst_structure_id_take_value (s, field_id, &v);
2966 * gst_structure_fixate_field:
2967 * @structure: a #GstStructure
2968 * @field_name: a field in @structure
2970 * Fixates a #GstStructure by changing the given field with its fixated value.
2972 * Returns: %TRUE if the structure field could be fixated
2975 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2977 GstStructureField *field;
2979 g_return_val_if_fail (structure != NULL, FALSE);
2980 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2982 if (!(field = gst_structure_get_field (structure, field_name)))
2985 return default_fixate (field->name, &field->value, structure);
2988 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2989 * (useful for message parsing functions where the return location is user
2990 * supplied and the user may pass %NULL if the value isn't of interest) */
2991 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2993 const GValue *_value = (value); \
2994 guint _flags = (flags); \
2995 GType _value_type = G_VALUE_TYPE (_value); \
2996 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2997 const gchar *_lcopy_format = _vtable->lcopy_format; \
2998 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2999 guint _n_values = 0; \
3001 while (*_lcopy_format != '\0') { \
3002 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
3003 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
3006 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
3007 *(__error) = g_strdup_printf ("either all or none of the return " \
3008 "locations for field '%s' need to be NULL", fieldname); \
3009 } else if (_cvalues[0].v_pointer != NULL) { \
3010 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
3015 * gst_structure_get_valist:
3016 * @structure: a #GstStructure
3017 * @first_fieldname: the name of the first field to read
3018 * @args: variable arguments
3020 * Parses the variable arguments and reads fields from @structure accordingly.
3021 * valist-variant of gst_structure_get(). Look at the documentation of
3022 * gst_structure_get() for more details.
3024 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
3027 gst_structure_get_valist (const GstStructure * structure,
3028 const char *first_fieldname, va_list args)
3030 const char *field_name;
3031 GType expected_type = G_TYPE_INVALID;
3033 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3034 g_return_val_if_fail (first_fieldname != NULL, FALSE);
3036 field_name = first_fieldname;
3037 while (field_name) {
3038 const GValue *val = NULL;
3041 expected_type = va_arg (args, GType);
3043 val = gst_structure_get_value (structure, field_name);
3048 if (G_VALUE_TYPE (val) != expected_type)
3051 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
3053 g_warning ("%s: %s", G_STRFUNC, err);
3058 field_name = va_arg (args, const gchar *);
3066 GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
3067 field_name, structure);
3072 GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
3073 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
3074 GST_STR_NULL (g_type_name (expected_type)),
3075 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
3082 * gst_structure_id_get_valist:
3083 * @structure: a #GstStructure
3084 * @first_field_id: the quark of the first field to read
3085 * @args: variable arguments
3087 * Parses the variable arguments and reads fields from @structure accordingly.
3088 * valist-variant of gst_structure_id_get(). Look at the documentation of
3089 * gst_structure_id_get() for more details.
3091 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
3094 gst_structure_id_get_valist (const GstStructure * structure,
3095 GQuark first_field_id, va_list args)
3098 GType expected_type = G_TYPE_INVALID;
3100 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3101 g_return_val_if_fail (first_field_id != 0, FALSE);
3103 field_id = first_field_id;
3105 const GValue *val = NULL;
3108 expected_type = va_arg (args, GType);
3110 val = gst_structure_id_get_value (structure, field_id);
3115 if (G_VALUE_TYPE (val) != expected_type)
3118 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
3120 g_warning ("%s: %s", G_STRFUNC, err);
3125 field_id = va_arg (args, GQuark);
3133 GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
3134 GST_STR_NULL (g_quark_to_string (field_id)), structure);
3139 GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
3140 "field was of type '%s': %" GST_PTR_FORMAT,
3141 g_quark_to_string (field_id),
3142 GST_STR_NULL (g_type_name (expected_type)),
3143 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
3150 * gst_structure_get:
3151 * @structure: a #GstStructure
3152 * @first_fieldname: the name of the first field to read
3153 * @...: variable arguments
3155 * Parses the variable arguments and reads fields from @structure accordingly.
3156 * Variable arguments should be in the form field name, field type
3157 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3158 * The last variable argument should be %NULL.
3160 * For refcounted (mini)objects you will receive a new reference which
3161 * you must release with a suitable _unref() when no longer needed. For
3162 * strings and boxed types you will receive a copy which you will need to
3163 * release with either g_free() or the suitable function for the boxed type.
3165 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
3166 * because the field requested did not exist, or was of a type other
3167 * than the type specified), otherwise %TRUE.
3170 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
3176 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3177 g_return_val_if_fail (first_fieldname != NULL, FALSE);
3179 va_start (args, first_fieldname);
3180 ret = gst_structure_get_valist (structure, first_fieldname, args);
3187 * gst_structure_id_get:
3188 * @structure: a #GstStructure
3189 * @first_field_id: the quark of the first field to read
3190 * @...: variable arguments
3192 * Parses the variable arguments and reads fields from @structure accordingly.
3193 * Variable arguments should be in the form field id quark, field type
3194 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3195 * The last variable argument should be %NULL (technically it should be a
3196 * 0 quark, but we require %NULL so compilers that support it can check for
3197 * the %NULL terminator and warn if it's not there).
3199 * This function is just like gst_structure_get() only that it is slightly
3200 * more efficient since it saves the string-to-quark lookup in the global
3203 * For refcounted (mini)objects you will receive a new reference which
3204 * you must release with a suitable _unref() when no longer needed. For
3205 * strings and boxed types you will receive a copy which you will need to
3206 * release with either g_free() or the suitable function for the boxed type.
3208 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
3209 * because the field requested did not exist, or was of a type other
3210 * than the type specified), otherwise %TRUE.
3213 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
3219 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3220 g_return_val_if_fail (first_field_id != 0, FALSE);
3222 va_start (args, first_field_id);
3223 ret = gst_structure_id_get_valist (structure, first_field_id, args);
3230 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
3233 const GstStructure *struct1 = (const GstStructure *) data;
3234 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
3236 if (G_UNLIKELY (val1 == NULL))
3238 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
3246 * gst_structure_is_equal:
3247 * @structure1: a #GstStructure.
3248 * @structure2: a #GstStructure.
3250 * Tests if the two #GstStructure are equal.
3252 * Returns: %TRUE if the two structures have the same name and field.
3255 gst_structure_is_equal (const GstStructure * structure1,
3256 const GstStructure * structure2)
3258 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
3259 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
3261 if (G_UNLIKELY (structure1 == structure2))
3264 if (structure1->name != structure2->name) {
3267 if (GST_STRUCTURE_FIELDS (structure1)->len !=
3268 GST_STRUCTURE_FIELDS (structure2)->len) {
3272 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
3273 (gpointer) structure2);
3280 const GstStructure *intersect;
3285 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
3287 IntersectData *idata = (IntersectData *) data;
3288 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3290 if (G_UNLIKELY (val2 == NULL)) {
3291 gst_structure_id_set_value (idata->dest, id, val1);
3293 GValue dest_value = { 0 };
3294 if (gst_value_intersect (&dest_value, val1, val2)) {
3295 gst_structure_id_take_value (idata->dest, id, &dest_value);
3304 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
3306 IntersectData *idata = (IntersectData *) data;
3307 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
3309 if (G_UNLIKELY (val2 == NULL)) {
3310 gst_structure_id_set_value (idata->dest, id, val1);
3316 * gst_structure_intersect:
3317 * @struct1: a #GstStructure
3318 * @struct2: a #GstStructure
3320 * Intersects @struct1 and @struct2 and returns the intersection.
3322 * Returns: Intersection of @struct1 and @struct2
3325 gst_structure_intersect (const GstStructure * struct1,
3326 const GstStructure * struct2)
3330 g_assert (struct1 != NULL);
3331 g_assert (struct2 != NULL);
3333 if (G_UNLIKELY (struct1->name != struct2->name))
3336 /* copy fields from struct1 which we have not in struct2 to target
3337 * intersect if we have the field in both */
3338 data.dest = gst_structure_new_id_empty (struct1->name);
3339 data.intersect = struct2;
3340 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3341 gst_structure_intersect_field1, &data)))
3344 /* copy fields from struct2 which we have not in struct1 to target */
3345 data.intersect = struct1;
3346 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3347 gst_structure_intersect_field2, &data)))
3353 gst_structure_free (data.dest);
3358 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3361 GstStructure *other = (GstStructure *) data;
3362 const GValue *val2 = gst_structure_id_get_value (other, id);
3364 if (G_LIKELY (val2)) {
3365 if (!gst_value_can_intersect (val1, val2)) {
3368 gint eq = gst_value_compare (val1, val2);
3370 if (eq == GST_VALUE_UNORDERED) {
3371 /* we need to try interseting */
3372 if (!gst_value_intersect (NULL, val1, val2)) {
3375 } else if (eq != GST_VALUE_EQUAL) {
3384 * gst_structure_can_intersect:
3385 * @struct1: a #GstStructure
3386 * @struct2: a #GstStructure
3388 * Tries intersecting @struct1 and @struct2 and reports whether the result
3389 * would not be empty.
3391 * Returns: %TRUE if intersection would not be empty
3394 gst_structure_can_intersect (const GstStructure * struct1,
3395 const GstStructure * struct2)
3397 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3398 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3400 if (G_UNLIKELY (struct1->name != struct2->name))
3403 /* tries to intersect if we have the field in both */
3404 return gst_structure_foreach ((GstStructure *) struct1,
3405 gst_caps_structure_can_intersect_field, (gpointer) struct2);
3409 gst_caps_structure_is_superset_field (GQuark field_id, const GValue * value,
3412 GstStructure *subset = user_data;
3413 const GValue *other;
3416 if (!(other = gst_structure_id_get_value (subset, field_id)))
3417 /* field is missing in the subset => no subset */
3420 comparison = gst_value_compare (value, other);
3422 /* equal values are subset */
3423 if (comparison == GST_VALUE_EQUAL)
3426 /* ordered, but unequal, values are not */
3427 if (comparison != GST_VALUE_UNORDERED)
3430 return gst_value_is_subset (other, value);
3434 * gst_structure_is_subset:
3435 * @subset: a #GstStructure
3436 * @superset: a potentially greater #GstStructure
3438 * Checks if @subset is a subset of @superset, i.e. has the same
3439 * structure name and for all fields that are existing in @superset,
3440 * @subset has a value that is a subset of the value in @superset.
3442 * Returns: %TRUE if @subset is a subset of @superset
3445 gst_structure_is_subset (const GstStructure * subset,
3446 const GstStructure * superset)
3448 if ((superset->name != subset->name) ||
3449 (gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
3452 return gst_structure_foreach ((GstStructure *) superset,
3453 gst_caps_structure_is_superset_field, (gpointer) subset);
3458 * gst_structure_fixate:
3459 * @structure: a #GstStructure
3461 * Fixate all values in @structure using gst_value_fixate().
3462 * @structure will be modified in-place and should be writable.
3465 gst_structure_fixate (GstStructure * structure)
3467 g_return_if_fail (GST_IS_STRUCTURE (structure));
3469 gst_structure_foreach (structure, default_fixate, structure);