2 * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
4 * gststructure.c: lists of { GQuark, GValue } tuples
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:gststructure
24 * @short_description: Generic structure containing fields of names and values
25 * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
27 * A #GstStructure is a collection of key/value pairs. The keys are expressed as
28 * GQuarks and the values can be of any GType.
30 * In addition to the key/value pairs, a #GstStructure also has a name.
32 * #GstStructure is used by various GStreamer subsystems to store information in
33 * a flexible an extensible way. A #GstStructure does not have a refcount because it
34 * usually is part of a higher level object such as #GstCaps. It provides a means to
35 * enforce mutability using the refcount of the parent with the
36 * gst_structure_set_parent_refcount() method.
38 * A #GstStructure can be created with gst_structure_empty_new() or gst_structure_new(),
39 * which both take a name and an optional set of key/value pairs along with the types
42 * Field values can be changed with gst_structure_set_value() or gst_structure_set().
44 * Field values can be retrieved with gst_structure_get_value() or the more convenient
45 * gst_structure_get_*() functions.
47 * Fields can be removed with gst_structure_remove_field() or gst_structure_remove_fields().
49 * Last reviewed on 2005-11-09 (0.9.4)
58 #include "gst_private.h"
60 #include <gobject/gvaluecollector.h>
62 typedef struct _GstStructureField GstStructureField;
64 struct _GstStructureField
70 #define GST_STRUCTURE_FIELD(structure, index) \
71 &g_array_index((structure)->fields, GstStructureField, (index))
73 #define IS_MUTABLE(structure) \
74 (!(structure)->parent_refcount || \
75 g_atomic_int_get ((structure)->parent_refcount) == 1)
77 static void gst_structure_set_field (GstStructure * structure,
78 GstStructureField * field);
79 static GstStructureField *gst_structure_get_field (const GstStructure *
80 structure, const gchar * fieldname);
81 static GstStructureField *gst_structure_id_get_field (const GstStructure *
82 structure, GQuark field);
83 static void gst_structure_transform_to_string (const GValue * src_value,
85 static GstStructure *gst_structure_copy_conditional (const GstStructure *
87 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
88 GValue * value, GType default_type);
89 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
92 gst_structure_get_type (void)
94 static GType gst_structure_type = 0;
96 if (!gst_structure_type) {
97 gst_structure_type = g_boxed_type_register_static ("GstStructure",
98 (GBoxedCopyFunc) gst_structure_copy_conditional,
99 (GBoxedFreeFunc) gst_structure_free);
101 g_value_register_transform_func (gst_structure_type, G_TYPE_STRING,
102 gst_structure_transform_to_string);
105 return gst_structure_type;
108 static GstStructure *
109 gst_structure_id_empty_new_with_size (GQuark quark, guint prealloc)
111 GstStructure *structure;
113 structure = g_new0 (GstStructure, 1);
114 structure->type = gst_structure_get_type ();
115 structure->name = quark;
117 g_array_sized_new (FALSE, TRUE, sizeof (GstStructureField), prealloc);
123 * gst_structure_id_empty_new:
124 * @quark: name of new structure
126 * Creates a new, empty #GstStructure with the given name as a GQuark.
128 * Returns: a new, empty #GstStructure
131 gst_structure_id_empty_new (GQuark quark)
133 g_return_val_if_fail (quark != 0, NULL);
135 return gst_structure_id_empty_new_with_size (quark, 0);
139 * gst_structure_empty_new:
140 * @name: name of new structure
142 * Creates a new, empty #GstStructure with the given name.
144 * Returns: a new, empty #GstStructure
147 gst_structure_empty_new (const gchar * name)
149 g_return_val_if_fail (name != NULL, NULL);
151 return gst_structure_id_empty_new_with_size (g_quark_from_string (name), 0);
156 * @name: name of new structure
157 * @firstfield: name of first field to set
158 * @...: additional arguments
160 * Creates a new #GstStructure with the given name. Parses the
161 * list of variable arguments and sets fields to the values listed.
162 * Variable arguments should be passed as field name, field type,
163 * and value. Last variable argument should be NULL.
165 * Returns: a new #GstStructure
168 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
170 GstStructure *structure;
173 g_return_val_if_fail (name != NULL, NULL);
175 va_start (varargs, firstfield);
177 structure = gst_structure_new_valist (name, firstfield, varargs);
185 * gst_structure_new_valist:
186 * @name: name of new structure
187 * @firstfield: name of first field to set
188 * @varargs: variable argument list
190 * Creates a new #GstStructure with the given name. Structure fields
191 * are set according to the varargs in a manner similar to
192 * @gst_structure_new.
194 * Returns: a new #GstStructure
197 gst_structure_new_valist (const gchar * name,
198 const gchar * firstfield, va_list varargs)
200 GstStructure *structure;
202 g_return_val_if_fail (name != NULL, NULL);
204 structure = gst_structure_empty_new (name);
205 gst_structure_set_valist (structure, firstfield, varargs);
211 * gst_structure_set_parent_refcount:
212 * @structure: a #GstStructure
213 * @refcount: a pointer to the parent's refcount
215 * Sets the parent_refcount field of #GstStructure. This field is used to
216 * determine whether a structure is mutable or not. This function should only be
217 * called by code implementing parent objects of #GstStructure, as described in
218 * the MT Refcounting section of the design documents.
221 gst_structure_set_parent_refcount (GstStructure * structure, int *refcount)
223 g_return_if_fail (structure != NULL);
225 /* if we have a parent_refcount already, we can only clear
226 * if with a NULL refcount */
227 if (structure->parent_refcount)
228 g_return_if_fail (refcount == NULL);
230 g_return_if_fail (refcount != NULL);
232 structure->parent_refcount = refcount;
236 * gst_structure_copy:
237 * @structure: a #GstStructure to duplicate
239 * Duplicates a #GstStructure and all its fields and values.
241 * Returns: a new #GstStructure.
244 gst_structure_copy (const GstStructure * structure)
246 GstStructure *new_structure;
247 GstStructureField *field;
250 g_return_val_if_fail (structure != NULL, NULL);
253 gst_structure_id_empty_new_with_size (structure->name,
254 structure->fields->len);
256 for (i = 0; i < structure->fields->len; i++) {
257 GstStructureField new_field = { 0 };
259 field = GST_STRUCTURE_FIELD (structure, i);
261 new_field.name = field->name;
262 gst_value_init_and_copy (&new_field.value, &field->value);
263 g_array_append_val (new_structure->fields, new_field);
266 return new_structure;
270 * gst_structure_free:
271 * @structure: the #GstStructure to free
273 * Frees a #GstStructure and all its fields and values. The structure must not
274 * have a parent when this function is called.
277 gst_structure_free (GstStructure * structure)
279 GstStructureField *field;
282 g_return_if_fail (structure != NULL);
283 g_return_if_fail (structure->parent_refcount == NULL);
285 for (i = 0; i < structure->fields->len; i++) {
286 field = GST_STRUCTURE_FIELD (structure, i);
288 if (G_IS_VALUE (&field->value)) {
289 g_value_unset (&field->value);
292 g_array_free (structure->fields, TRUE);
294 memset (structure, 0xff, sizeof (GstStructure));
300 * gst_structure_get_name:
301 * @structure: a #GstStructure
303 * Get the name of @structure as a string.
305 * Returns: the name of the structure.
308 gst_structure_get_name (const GstStructure * structure)
310 g_return_val_if_fail (structure != NULL, NULL);
312 return g_quark_to_string (structure->name);
316 * gst_structure_has_name:
317 * @structure: a #GstStructure
318 * @name: structure name to check for
320 * Checks if the structure has the given name
322 * Returns: TRUE if @name matches the name of the structure.
325 gst_structure_has_name (const GstStructure * structure, const gchar * name)
327 const gchar *structure_name;
329 g_return_val_if_fail (structure != NULL, FALSE);
330 g_return_val_if_fail (name != NULL, FALSE);
332 structure_name = g_quark_to_string (structure->name);
334 return (structure_name && strcmp (structure_name, name) == 0);
338 * gst_structure_get_name_id:
339 * @structure: a #GstStructure
341 * Get the name of @structure as a GQuark.
343 * Returns: the quark representing the name of the structure.
346 gst_structure_get_name_id (const GstStructure * structure)
348 g_return_val_if_fail (structure != NULL, 0);
350 return structure->name;
354 * gst_structure_set_name:
355 * @structure: a #GstStructure
356 * @name: the new name of the structure
358 * Sets the name of the structure to the given name. The string
359 * provided is copied before being used.
362 gst_structure_set_name (GstStructure * structure, const gchar * name)
364 g_return_if_fail (structure != NULL);
365 g_return_if_fail (name != NULL);
366 g_return_if_fail (IS_MUTABLE (structure));
368 structure->name = g_quark_from_string (name);
372 * gst_structure_id_set_value:
373 * @structure: a #GstStructure
374 * @field: a #GQuark representing a field
375 * @value: the new value of the field
377 * Sets the field with the given GQuark @field to @value. If the field
378 * does not exist, it is created. If the field exists, the previous
379 * value is replaced and freed.
382 gst_structure_id_set_value (GstStructure * structure,
383 GQuark field, const GValue * value)
385 GstStructureField gsfield = { 0, {0,} };
387 g_return_if_fail (structure != NULL);
388 g_return_if_fail (G_IS_VALUE (value));
389 g_return_if_fail (IS_MUTABLE (structure));
391 gsfield.name = field;
392 gst_value_init_and_copy (&gsfield.value, value);
394 gst_structure_set_field (structure, &gsfield);
398 * gst_structure_set_value:
399 * @structure: a #GstStructure
400 * @fieldname: the name of the field to set
401 * @value: the new value of the field
403 * Sets the field with the given name @field to @value. If the field
404 * does not exist, it is created. If the field exists, the previous
405 * value is replaced and freed.
408 gst_structure_set_value (GstStructure * structure,
409 const gchar * fieldname, const GValue * value)
411 g_return_if_fail (structure != NULL);
412 g_return_if_fail (fieldname != NULL);
413 g_return_if_fail (G_IS_VALUE (value));
414 g_return_if_fail (IS_MUTABLE (structure));
416 gst_structure_id_set_value (structure, g_quark_from_string (fieldname),
422 * @structure: a #GstStructure
423 * @fieldname: the name of the field to set
424 * @...: variable arguments
426 * Parses the variable arguments and sets fields accordingly.
427 * Variable arguments should be in the form field name, field type
428 * (as a GType), value(s). The last variable argument should be NULL.
431 gst_structure_set (GstStructure * structure, const gchar * field, ...)
435 g_return_if_fail (structure != NULL);
437 va_start (varargs, field);
439 gst_structure_set_valist (structure, field, varargs);
445 * gst_structure_set_valist:
446 * @structure: a #GstStructure
447 * @fieldname: the name of the field to set
448 * @varargs: variable arguments
450 * va_list form of #gst_structure_set.
453 gst_structure_set_valist (GstStructure * structure,
454 const gchar * fieldname, va_list varargs)
459 g_return_if_fail (structure != NULL);
460 g_return_if_fail (IS_MUTABLE (structure));
463 GstStructureField field = { 0 };
465 field.name = g_quark_from_string (fieldname);
467 type = va_arg (varargs, GType);
469 #if GLIB_CHECK_VERSION(2,8,0)
470 if (type == G_TYPE_DATE) {
471 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
472 type = GST_TYPE_DATE;
476 g_value_init (&field.value, type);
477 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
479 g_critical ("%s", err);
482 gst_structure_set_field (structure, &field);
484 fieldname = va_arg (varargs, gchar *);
488 /* If the structure currently contains a field with the same name, it is
489 * replaced with the provided field. Otherwise, the field is added to the
490 * structure. The field's value is not deeply copied.
493 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
495 GstStructureField *f;
498 for (i = 0; i < structure->fields->len; i++) {
499 f = GST_STRUCTURE_FIELD (structure, i);
501 if (f->name == field->name) {
502 g_value_unset (&f->value);
503 memcpy (f, field, sizeof (GstStructureField));
508 g_array_append_val (structure->fields, *field);
511 /* If there is no field with the given ID, NULL is returned.
513 static GstStructureField *
514 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
516 GstStructureField *field;
519 g_return_val_if_fail (structure != NULL, NULL);
521 for (i = 0; i < structure->fields->len; i++) {
522 field = GST_STRUCTURE_FIELD (structure, i);
524 if (field->name == field_id)
531 /* If there is no field with the given ID, NULL is returned.
533 static GstStructureField *
534 gst_structure_get_field (const GstStructure * structure,
535 const gchar * fieldname)
537 g_return_val_if_fail (structure != NULL, NULL);
538 g_return_val_if_fail (fieldname != NULL, NULL);
540 return gst_structure_id_get_field (structure,
541 g_quark_from_string (fieldname));
545 * gst_structure_get_value:
546 * @structure: a #GstStructure
547 * @fieldname: the name of the field to get
549 * Get the value of the field with name @fieldname.
551 * Returns: the #GValue corresponding to the field with the given name.
554 gst_structure_get_value (const GstStructure * structure,
555 const gchar * fieldname)
557 GstStructureField *field;
559 g_return_val_if_fail (structure != NULL, NULL);
560 g_return_val_if_fail (fieldname != NULL, NULL);
562 field = gst_structure_get_field (structure, fieldname);
566 return &field->value;
570 * gst_structure_id_get_value:
571 * @structure: a #GstStructure
572 * @field: the #GQuark of the field to get
574 * Get the value of the field with GQuark @field.
576 * Returns: the #GValue corresponding to the field with the given name
580 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
582 GstStructureField *gsfield;
584 g_return_val_if_fail (structure != NULL, NULL);
586 gsfield = gst_structure_id_get_field (structure, field);
590 return &gsfield->value;
594 * gst_structure_remove_field:
595 * @structure: a #GstStructure
596 * @fieldname: the name of the field to remove
598 * Removes the field with the given name. If the field with the given
599 * name does not exist, the structure is unchanged.
602 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
604 GstStructureField *field;
608 g_return_if_fail (structure != NULL);
609 g_return_if_fail (fieldname != NULL);
610 g_return_if_fail (IS_MUTABLE (structure));
612 id = g_quark_from_string (fieldname);
614 for (i = 0; i < structure->fields->len; i++) {
615 field = GST_STRUCTURE_FIELD (structure, i);
617 if (field->name == id) {
618 if (G_IS_VALUE (&field->value)) {
619 g_value_unset (&field->value);
621 structure->fields = g_array_remove_index (structure->fields, i);
628 * gst_structure_remove_fields:
629 * @structure: a #GstStructure
630 * @fieldname: the name of the field to remove
631 * @...: NULL-terminated list of more fieldnames to remove
633 * Removes the fields with the given names. If a field does not exist, the
634 * argument is ignored.
637 gst_structure_remove_fields (GstStructure * structure,
638 const gchar * fieldname, ...)
642 g_return_if_fail (structure != NULL);
643 g_return_if_fail (fieldname != NULL);
644 /* mutability checked in remove_field */
646 va_start (varargs, fieldname);
648 gst_structure_remove_fields_valist (structure, fieldname, varargs);
654 * gst_structure_remove_fields_valist:
655 * @structure: a #GstStructure
656 * @fieldname: the name of the field to remove
657 * @varargs: NULL-terminated list of more fieldnames to remove
659 * va_list form of #gst_structure_remove_fields.
662 gst_structure_remove_fields_valist (GstStructure * structure,
663 const gchar * fieldname, va_list varargs)
665 gchar *field = (gchar *) fieldname;
667 g_return_if_fail (structure != NULL);
668 g_return_if_fail (fieldname != NULL);
669 /* mutability checked in remove_field */
672 gst_structure_remove_field (structure, field);
673 field = va_arg (varargs, char *);
678 * gst_structure_remove_all_fields:
679 * @structure: a #GstStructure
681 * Removes all fields in a GstStructure.
684 gst_structure_remove_all_fields (GstStructure * structure)
686 GstStructureField *field;
689 g_return_if_fail (structure != NULL);
690 g_return_if_fail (IS_MUTABLE (structure));
692 for (i = structure->fields->len - 1; i >= 0; i--) {
693 field = GST_STRUCTURE_FIELD (structure, i);
695 if (G_IS_VALUE (&field->value)) {
696 g_value_unset (&field->value);
698 structure->fields = g_array_remove_index (structure->fields, i);
703 * gst_structure_get_field_type:
704 * @structure: a #GstStructure
705 * @fieldname: the name of the field
707 * Finds the field with the given name, and returns the type of the
708 * value it contains. If the field is not found, G_TYPE_INVALID is
711 * Returns: the #GValue of the field
714 gst_structure_get_field_type (const GstStructure * structure,
715 const gchar * fieldname)
717 GstStructureField *field;
719 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
720 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
722 field = gst_structure_get_field (structure, fieldname);
724 return G_TYPE_INVALID;
726 return G_VALUE_TYPE (&field->value);
730 * gst_structure_n_fields:
731 * @structure: a #GstStructure
733 * Get the number of fields in the structure.
735 * Returns: the number of fields in the structure
738 gst_structure_n_fields (const GstStructure * structure)
740 g_return_val_if_fail (structure != NULL, 0);
742 return structure->fields->len;
746 * gst_structure_nth_field_name:
747 * @structure: a #GstStructure
748 * @index: the index to get the name of
750 * Get the name of the given field number, counting from 0 onwards.
752 * Returns: the name of the given field number
755 gst_structure_nth_field_name (const GstStructure * structure, guint index)
757 GstStructureField *field;
759 field = GST_STRUCTURE_FIELD (structure, index);
760 return g_quark_to_string (field->name);
764 * gst_structure_foreach:
765 * @structure: a #GstStructure
766 * @func: a function to call for each field
767 * @user_data: private data
769 * Calls the provided function once for each field in the #GstStructure. The
770 * function must not modify the fields. Also see gst_structure_map_in_place().
772 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
776 gst_structure_foreach (const GstStructure * structure,
777 GstStructureForeachFunc func, gpointer user_data)
780 GstStructureField *field;
783 g_return_val_if_fail (structure != NULL, FALSE);
785 for (i = 0; i < structure->fields->len; i++) {
786 field = GST_STRUCTURE_FIELD (structure, i);
788 ret = func (field->name, &field->value, user_data);
797 * gst_structure_map_in_place:
798 * @structure: a #GstStructure
799 * @func: a function to call for each field
800 * @user_data: private data
802 * Calls the provided function once for each field in the #GstStructure. In
803 * contrast to gst_structure_foreach(), the function may modify the fields. The
804 * structure must be mutable.
806 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
810 gst_structure_map_in_place (GstStructure * structure,
811 GstStructureMapFunc func, gpointer user_data)
814 GstStructureField *field;
817 g_return_val_if_fail (structure != NULL, FALSE);
818 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
820 for (i = 0; i < structure->fields->len; i++) {
821 field = GST_STRUCTURE_FIELD (structure, i);
823 ret = func (field->name, &field->value, user_data);
832 * gst_structure_has_field:
833 * @structure: a #GstStructure
834 * @fieldname: the name of a field
836 * Check if @structure contains a field named @fieldname.
838 * Returns: TRUE if the structure contains a field with the given name
841 gst_structure_has_field (const GstStructure * structure,
842 const gchar * fieldname)
844 GstStructureField *field;
846 g_return_val_if_fail (structure != NULL, 0);
847 g_return_val_if_fail (fieldname != NULL, 0);
849 field = gst_structure_get_field (structure, fieldname);
851 return (field != NULL);
855 * gst_structure_has_field_typed:
856 * @structure: a #GstStructure
857 * @fieldname: the name of a field
858 * @type: the type of a value
860 * Check if @structure contains a field named @fieldname and with GType @type.
862 * Returns: TRUE if the structure contains a field with the given name and type
865 gst_structure_has_field_typed (const GstStructure * structure,
866 const gchar * fieldname, GType type)
868 GstStructureField *field;
870 g_return_val_if_fail (structure != NULL, 0);
871 g_return_val_if_fail (fieldname != NULL, 0);
873 field = gst_structure_get_field (structure, fieldname);
877 return (G_VALUE_TYPE (&field->value) == type);
881 /* utility functions */
884 * gst_structure_get_boolean:
885 * @structure: a #GstStructure
886 * @fieldname: the name of a field
887 * @value: a pointer to a #gboolean to set
889 * Sets the boolean pointed to by @value corresponding to the value of the
890 * given field. Caller is responsible for making sure the field exists
891 * and has the correct type.
893 * Returns: TRUE if the value could be set correctly. If there was no field
894 * with @fieldname or the existing field did not contain a boolean, this function
898 gst_structure_get_boolean (const GstStructure * structure,
899 const gchar * fieldname, gboolean * value)
901 GstStructureField *field;
903 g_return_val_if_fail (structure != NULL, FALSE);
904 g_return_val_if_fail (fieldname != NULL, FALSE);
906 field = gst_structure_get_field (structure, fieldname);
910 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
913 *value = g_value_get_boolean (&field->value);
919 * gst_structure_get_int:
920 * @structure: a #GstStructure
921 * @fieldname: the name of a field
922 * @value: a pointer to an int to set
924 * Sets the int pointed to by @value corresponding to the value of the
925 * given field. Caller is responsible for making sure the field exists
926 * and has the correct type.
928 * Returns: TRUE if the value could be set correctly. If there was no field
929 * with @fieldname or the existing field did not contain an int, this function
933 gst_structure_get_int (const GstStructure * structure,
934 const gchar * fieldname, gint * value)
936 GstStructureField *field;
938 g_return_val_if_fail (structure != NULL, FALSE);
939 g_return_val_if_fail (fieldname != NULL, FALSE);
940 g_return_val_if_fail (value != NULL, FALSE);
942 field = gst_structure_get_field (structure, fieldname);
946 if (!G_VALUE_HOLDS_INT (&field->value))
949 *value = g_value_get_int (&field->value);
955 * gst_structure_get_fourcc:
956 * @structure: a #GstStructure
957 * @fieldname: the name of a field
958 * @value: a pointer to a #GstFourcc to set
960 * Sets the #GstFourcc pointed to by @value corresponding to the value of the
961 * given field. Caller is responsible for making sure the field exists
962 * and has the correct type.
964 * Returns: TRUE if the value could be set correctly. If there was no field
965 * with @fieldname or the existing field did not contain a fourcc, this function
969 gst_structure_get_fourcc (const GstStructure * structure,
970 const gchar * fieldname, guint32 * value)
972 GstStructureField *field;
974 g_return_val_if_fail (structure != NULL, FALSE);
975 g_return_val_if_fail (fieldname != NULL, FALSE);
976 g_return_val_if_fail (value != NULL, FALSE);
978 field = gst_structure_get_field (structure, fieldname);
982 if (!GST_VALUE_HOLDS_FOURCC (&field->value))
985 *value = gst_value_get_fourcc (&field->value);
991 * gst_structure_get_date:
992 * @structure: a #GstStructure
993 * @fieldname: the name of a field
994 * @value: a pointer to a #GDate to set
996 * Sets the date pointed to by @value corresponding to the date of the
997 * given field. Caller is responsible for making sure the field exists
998 * and has the correct type.
1000 * Returns: TRUE if the value could be set correctly. If there was no field
1001 * with @fieldname or the existing field did not contain a data, this function
1005 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1008 GstStructureField *field;
1010 g_return_val_if_fail (structure != NULL, FALSE);
1011 g_return_val_if_fail (fieldname != NULL, FALSE);
1012 g_return_val_if_fail (value != NULL, FALSE);
1014 field = gst_structure_get_field (structure, fieldname);
1018 if (!GST_VALUE_HOLDS_DATE (&field->value))
1021 *value = g_value_dup_boxed (&field->value);
1027 * gst_structure_get_clock_time:
1028 * @structure: a #GstStructure
1029 * @fieldname: the name of a field
1030 * @value: a pointer to a #GstClockTime to set
1032 * Sets the clock time pointed to by @value corresponding to the clock time
1033 * of the given field. Caller is responsible for making sure the field exists
1034 * and has the correct type.
1036 * Returns: TRUE if the value could be set correctly. If there was no field
1037 * with @fieldname or the existing field did not contain a #GstClockTime, this
1038 * function returns FALSE.
1041 gst_structure_get_clock_time (const GstStructure * structure,
1042 const gchar * fieldname, GstClockTime * value)
1044 GstStructureField *field;
1046 g_return_val_if_fail (structure != NULL, FALSE);
1047 g_return_val_if_fail (fieldname != NULL, FALSE);
1048 g_return_val_if_fail (value != NULL, FALSE);
1050 field = gst_structure_get_field (structure, fieldname);
1054 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1057 *value = g_value_get_uint64 (&field->value);
1063 * gst_structure_get_double:
1064 * @structure: a #GstStructure
1065 * @fieldname: the name of a field
1066 * @value: a pointer to a #GstFourcc to set
1068 * Sets the double pointed to by @value corresponding to the value of the
1069 * given field. Caller is responsible for making sure the field exists
1070 * and has the correct type.
1072 * Returns: TRUE if the value could be set correctly. If there was no field
1073 * with @fieldname or the existing field did not contain a double, this
1074 * function returns FALSE.
1077 gst_structure_get_double (const GstStructure * structure,
1078 const gchar * fieldname, gdouble * value)
1080 GstStructureField *field;
1082 g_return_val_if_fail (structure != NULL, FALSE);
1083 g_return_val_if_fail (fieldname != NULL, FALSE);
1084 g_return_val_if_fail (value != NULL, FALSE);
1086 field = gst_structure_get_field (structure, fieldname);
1090 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1093 *value = g_value_get_double (&field->value);
1099 * gst_structure_get_string:
1100 * @structure: a #GstStructure
1101 * @fieldname: the name of a field
1103 * Finds the field corresponding to @fieldname, and returns the string
1104 * contained in the field's value. Caller is responsible for making
1105 * sure the field exists and has the correct type.
1107 * The string should not be modified, and remains valid until the next
1108 * call to a gst_structure_*() function with the given structure.
1110 * Returns: a pointer to the string or NULL when the field did not exist
1111 * or did not contain a string.
1114 gst_structure_get_string (const GstStructure * structure,
1115 const gchar * fieldname)
1117 GstStructureField *field;
1119 g_return_val_if_fail (structure != NULL, NULL);
1120 g_return_val_if_fail (fieldname != NULL, NULL);
1122 field = gst_structure_get_field (structure, fieldname);
1126 if (!G_VALUE_HOLDS_STRING (&field->value))
1129 return g_value_get_string (&field->value);
1133 * gst_structure_get_enum:
1134 * @structure: a #GstStructure
1135 * @fieldname: the name of a field
1136 * @enumtype: the enum type of a field
1137 * @value: a pointer to an int to set
1139 * Sets the int pointed to by @value corresponding to the value of the
1140 * given field. Caller is responsible for making sure the field exists,
1141 * has the correct type and that the enumtype is correct.
1143 * Returns: TRUE if the value could be set correctly. If there was no field
1144 * with @fieldname or the existing field did not contain an enum of the given
1145 * type, this function returns FALSE.
1148 gst_structure_get_enum (const GstStructure * structure,
1149 const gchar * fieldname, GType enumtype, gint * value)
1151 GstStructureField *field;
1153 g_return_val_if_fail (structure != NULL, FALSE);
1154 g_return_val_if_fail (fieldname != NULL, FALSE);
1155 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1156 g_return_val_if_fail (value != NULL, FALSE);
1158 field = gst_structure_get_field (structure, fieldname);
1162 if (!G_VALUE_HOLDS_ENUM (&field->value))
1164 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1167 *value = g_value_get_enum (&field->value);
1172 typedef struct _GstStructureAbbreviation
1177 GstStructureAbbreviation;
1179 static GstStructureAbbreviation *
1180 gst_structure_get_abbrs (gint * n_abbrs)
1182 static GstStructureAbbreviation *abbrs = NULL;
1183 static gint num = 0;
1185 if (abbrs == NULL) {
1186 /* dynamically generate the array */
1187 GstStructureAbbreviation dyn_abbrs[] = {
1192 {"float", G_TYPE_FLOAT}
1196 {"double", G_TYPE_DOUBLE}
1198 {"d", G_TYPE_DOUBLE}
1200 {"buffer", GST_TYPE_BUFFER}
1202 {"fourcc", GST_TYPE_FOURCC}
1204 {"4", GST_TYPE_FOURCC}
1206 {"fraction", GST_TYPE_FRACTION}
1208 {"boolean", G_TYPE_BOOLEAN}
1210 {"bool", G_TYPE_BOOLEAN}
1212 {"b", G_TYPE_BOOLEAN}
1214 {"string", G_TYPE_STRING}
1216 {"str", G_TYPE_STRING}
1218 {"s", G_TYPE_STRING}
1220 num = G_N_ELEMENTS (dyn_abbrs);
1221 /* permanently allocate and copy the array now */
1222 abbrs = g_new0 (GstStructureAbbreviation, num);
1223 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * num);
1231 gst_structure_from_abbr (const char *type_name)
1234 GstStructureAbbreviation *abbrs;
1237 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1239 abbrs = gst_structure_get_abbrs (&n_abbrs);
1241 for (i = 0; i < n_abbrs; i++) {
1242 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1243 return abbrs[i].type;
1247 /* this is the fallback */
1248 return g_type_from_name (type_name);
1252 gst_structure_to_abbr (GType type)
1255 GstStructureAbbreviation *abbrs;
1258 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1260 abbrs = gst_structure_get_abbrs (&n_abbrs);
1262 for (i = 0; i < n_abbrs; i++) {
1263 if (type == abbrs[i].type) {
1264 return abbrs[i].type_name;
1268 return g_type_name (type);
1272 gst_structure_value_get_generic_type (GValue * val)
1274 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1275 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1276 GArray *array = g_value_peek_pointer (val);
1278 if (array->len > 0) {
1279 GValue *value = &g_array_index (array, GValue, 0);
1281 return gst_structure_value_get_generic_type (value);
1285 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1287 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1288 return G_TYPE_DOUBLE;
1290 return G_VALUE_TYPE (val);
1293 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1294 ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1298 * gst_structure_to_string:
1299 * @structure: a #GstStructure
1301 * Converts @structure to a human-readable string representation.
1303 * Returns: a pointer to string allocated by g_malloc(). g_free after
1307 gst_structure_to_string (const GstStructure * structure)
1309 GstStructureField *field;
1313 /* NOTE: This function is potentially called by the debug system,
1314 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1315 * should be careful to avoid recursion. This includes any functions
1316 * called by gst_structure_to_string. In particular, calls should
1317 * not use the GST_PTR_FORMAT extension. */
1319 g_return_val_if_fail (structure != NULL, NULL);
1321 s = g_string_new ("");
1322 /* FIXME this string may need to be escaped */
1323 g_string_append_printf (s, "%s", g_quark_to_string (structure->name));
1324 for (i = 0; i < structure->fields->len; i++) {
1328 field = GST_STRUCTURE_FIELD (structure, i);
1330 t = gst_value_serialize (&field->value);
1331 type = gst_structure_value_get_generic_type (&field->value);
1333 g_string_append_printf (s, ", %s=(%s)%s", g_quark_to_string (field->name),
1334 gst_structure_to_abbr (type), GST_STR_NULL (t));
1337 return g_string_free (s, FALSE);
1341 * r will still point to the string. if end == next, the string will not be
1342 * null-terminated. In all other cases it will be.
1343 * end = pointer to char behind end of string, next = pointer to start of
1345 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1348 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next)
1358 ret = gst_structure_parse_simple_string (s, end);
1387 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1390 GValue value1 = { 0 };
1391 GValue value2 = { 0 };
1400 ret = gst_structure_parse_value (s, &s, &value1, type);
1404 while (g_ascii_isspace (*s))
1411 while (g_ascii_isspace (*s))
1414 ret = gst_structure_parse_value (s, &s, &value2, type);
1418 while (g_ascii_isspace (*s))
1425 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1428 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1429 range_type = GST_TYPE_DOUBLE_RANGE;
1430 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1431 range_type = GST_TYPE_INT_RANGE;
1436 g_value_init (value, range_type);
1437 if (range_type == GST_TYPE_DOUBLE_RANGE) {
1438 gst_value_set_double_range (value, g_value_get_double (&value1),
1439 g_value_get_double (&value2));
1441 gst_value_set_int_range (value, g_value_get_int (&value1),
1442 g_value_get_int (&value2));
1450 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1451 GType type, GType list_type, char begin, char end)
1453 GValue list_value = { 0 };
1457 g_value_init (value, list_type);
1458 array = g_value_peek_pointer (value);
1464 while (g_ascii_isspace (*s))
1472 ret = gst_structure_parse_value (s, &s, &list_value, type);
1476 g_array_append_val (array, list_value);
1478 while (g_ascii_isspace (*s))
1486 while (g_ascii_isspace (*s))
1489 memset (&list_value, 0, sizeof (list_value));
1490 ret = gst_structure_parse_value (s, &s, &list_value, type);
1494 g_array_append_val (array, list_value);
1495 while (g_ascii_isspace (*s))
1506 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
1508 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
1513 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
1516 return gst_structure_parse_any_list (s, after, value, type,
1517 GST_TYPE_ARRAY, '<', '>');
1521 gst_structure_parse_simple_string (gchar * str, gchar ** end)
1525 while (GST_ASCII_IS_STRING (*s)) {
1535 gst_structure_parse_field (gchar * str,
1536 gchar ** after, GstStructureField * field)
1545 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1548 if (!gst_structure_parse_simple_string (s, &name_end))
1552 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1561 field->name = g_quark_from_string (name);
1564 if (!gst_structure_parse_value (s, &s, &field->value, G_TYPE_INVALID))
1572 gst_structure_parse_value (gchar * str,
1573 gchar ** after, GValue * value, GType default_type)
1582 GType type = default_type;
1586 while (g_ascii_isspace (*s))
1591 type = G_TYPE_INVALID;
1594 while (g_ascii_isspace (*s))
1597 if (!gst_structure_parse_simple_string (s, &type_end))
1600 while (g_ascii_isspace (*s))
1605 while (g_ascii_isspace (*s))
1610 type = gst_structure_from_abbr (type_name);
1613 if (type == G_TYPE_INVALID)
1617 while (g_ascii_isspace (*s))
1620 ret = gst_structure_parse_range (s, &s, value, type);
1621 } else if (*s == '{') {
1622 ret = gst_structure_parse_list (s, &s, value, type);
1623 } else if (*s == '<') {
1624 ret = gst_structure_parse_array (s, &s, value, type);
1627 if (!gst_structure_parse_string (s, &value_end, &s))
1632 if (type == G_TYPE_INVALID) {
1633 GType try_types[] = { G_TYPE_INT, G_TYPE_DOUBLE, G_TYPE_STRING };
1636 for (i = 0; i < 3; i++) {
1637 g_value_init (value, try_types[i]);
1638 ret = gst_value_deserialize (value, value_s);
1641 g_value_unset (value);
1644 g_value_init (value, type);
1646 ret = gst_value_deserialize (value, value_s);
1657 * gst_structure_from_string:
1658 * @string: a string representation of a #GstStructure.
1659 * @end: pointer to store the end of the string in.
1661 * Creates a #GstStructure from a string representation.
1662 * If end is not NULL, a pointer to the place inside the given string
1663 * where parsing ended will be returned.
1665 * Returns: a new #GstStructure or NULL when the string could not
1666 * be parsed. Free after usage.
1669 gst_structure_from_string (const gchar * string, gchar ** end)
1676 GstStructure *structure = NULL;
1677 GstStructureField field = { 0 };
1679 g_return_val_if_fail (string != NULL, NULL);
1681 copy = g_strdup (string);
1685 if (!gst_structure_parse_string (r, &w, &r))
1688 while (g_ascii_isspace (*r) || (r[0] == '\\' && g_ascii_isspace (r[1])))
1690 if (*r != 0 && *r != ';' && *r != ',')
1695 structure = gst_structure_empty_new (name);
1698 while (*r && (*r != ';')) {
1702 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1703 && g_ascii_isspace (r[1]))))
1706 memset (&field, 0, sizeof (field));
1707 if (!gst_structure_parse_field (r, &r, &field))
1709 gst_structure_set_field (structure, &field);
1710 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1711 && g_ascii_isspace (r[1]))))
1716 *end = (char *) string + (r - copy);
1723 gst_structure_free (structure);
1729 gst_structure_transform_to_string (const GValue * src_value,
1730 GValue * dest_value)
1732 g_return_if_fail (src_value != NULL);
1733 g_return_if_fail (dest_value != NULL);
1735 dest_value->data[0].v_pointer =
1736 gst_structure_to_string (src_value->data[0].v_pointer);
1739 static GstStructure *
1740 gst_structure_copy_conditional (const GstStructure * structure)
1743 return gst_structure_copy (structure);
1747 /* fixate utility functions */
1750 * gst_structure_fixate_field_nearest_int:
1751 * @structure: a #GstStructure
1752 * @field_name: a field in @structure
1753 * @target: the target value of the fixation
1755 * Fixates a #GstStructure by changing the given field to the nearest
1756 * integer to @target that is a subset of the existing field.
1758 * Returns: TRUE if the structure could be fixated
1761 gst_structure_fixate_field_nearest_int (GstStructure * structure,
1762 const char *field_name, int target)
1764 const GValue *value;
1766 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1767 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1769 value = gst_structure_get_value (structure, field_name);
1771 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
1774 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
1777 x = gst_value_get_int_range_min (value);
1780 x = gst_value_get_int_range_max (value);
1783 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
1785 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1786 const GValue *list_value;
1789 int best_index = -1;
1791 n = gst_value_list_get_size (value);
1792 for (i = 0; i < n; i++) {
1793 list_value = gst_value_list_get_value (value, i);
1794 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
1795 int x = g_value_get_int (list_value);
1797 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
1803 if (best_index != -1) {
1804 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
1814 * gst_structure_fixate_field_nearest_double:
1815 * @structure: a #GstStructure
1816 * @field_name: a field in @structure
1817 * @target: the target value of the fixation
1819 * Fixates a #GstStructure by changing the given field to the nearest
1820 * double to @target that is a subset of the existing field.
1822 * Returns: TRUE if the structure could be fixated
1825 gst_structure_fixate_field_nearest_double (GstStructure * structure,
1826 const char *field_name, double target)
1828 const GValue *value;
1830 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1831 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1833 value = gst_structure_get_value (structure, field_name);
1835 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
1838 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
1841 x = gst_value_get_double_range_min (value);
1844 x = gst_value_get_double_range_max (value);
1847 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
1849 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1850 const GValue *list_value;
1853 int best_index = -1;
1855 n = gst_value_list_get_size (value);
1856 for (i = 0; i < n; i++) {
1857 list_value = gst_value_list_get_value (value, i);
1858 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
1859 double x = g_value_get_double (list_value);
1861 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
1867 if (best_index != -1) {
1868 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
1879 * gst_structure_fixate_field_boolean:
1880 * @structure: a #GstStructure
1881 * @field_name: a field in @structure
1882 * @target: the target value of the fixation
1884 * Fixates a #GstStructure by changing the given @field_name field to the given
1885 * @target boolean if that field is not fixed yet.
1887 * Returns: TRUE if the structure could be fixated
1890 gst_structure_fixate_field_boolean (GstStructure * structure,
1891 const char *field_name, gboolean target)
1893 const GValue *value;
1895 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1896 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1898 value = gst_structure_get_value (structure, field_name);
1900 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
1903 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1904 const GValue *list_value;
1907 int best_index = -1;
1909 n = gst_value_list_get_size (value);
1910 for (i = 0; i < n; i++) {
1911 list_value = gst_value_list_get_value (value, i);
1912 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
1913 gboolean x = g_value_get_boolean (list_value);
1915 if (best_index == -1 || x == target) {
1921 if (best_index != -1) {
1922 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);