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
34 #include "gst_private.h"
36 #include <gobject/gvaluecollector.h>
38 typedef struct _GstStructureField GstStructureField;
40 struct _GstStructureField
46 #define GST_STRUCTURE_FIELD(structure, index) \
47 &g_array_index((structure)->fields, GstStructureField, (index))
49 #define IS_MUTABLE(structure) \
50 (!(structure)->parent_refcount || \
51 g_atomic_int_get ((structure)->parent_refcount) == 1)
53 static void gst_structure_set_field (GstStructure * structure,
54 GstStructureField * field);
55 static GstStructureField *gst_structure_get_field (const GstStructure *
56 structure, const gchar * fieldname);
57 static GstStructureField *gst_structure_id_get_field (const GstStructure *
58 structure, GQuark field);
59 static void gst_structure_transform_to_string (const GValue * src_value,
61 static GstStructure *gst_structure_copy_conditional (const GstStructure *
63 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
64 GValue * value, GType default_type);
65 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
68 gst_structure_get_type (void)
70 static GType gst_structure_type = 0;
72 if (!gst_structure_type) {
73 gst_structure_type = g_boxed_type_register_static ("GstStructure",
74 (GBoxedCopyFunc) gst_structure_copy_conditional,
75 (GBoxedFreeFunc) gst_structure_free);
77 g_value_register_transform_func (gst_structure_type, G_TYPE_STRING,
78 gst_structure_transform_to_string);
81 return gst_structure_type;
85 gst_structure_id_empty_new_with_size (GQuark quark, guint prealloc)
87 GstStructure *structure;
89 structure = g_new0 (GstStructure, 1);
90 structure->type = gst_structure_get_type ();
91 structure->name = quark;
93 g_array_sized_new (FALSE, TRUE, sizeof (GstStructureField), prealloc);
99 * gst_structure_id_empty_new:
100 * @quark: name of new structure
102 * Creates a new, empty #GstStructure with the given name.
104 * Returns: a new, empty #GstStructure
107 gst_structure_id_empty_new (GQuark quark)
109 g_return_val_if_fail (quark != 0, NULL);
111 return gst_structure_id_empty_new_with_size (quark, 0);
115 * gst_structure_empty_new:
116 * @name: name of new structure
118 * Creates a new, empty #GstStructure with the given name.
120 * Returns: a new, empty #GstStructure
123 gst_structure_empty_new (const gchar * name)
125 g_return_val_if_fail (name != NULL, NULL);
127 return gst_structure_id_empty_new_with_size (g_quark_from_string (name), 0);
132 * @name: name of new structure
133 * @firstfield: name of first field to set
134 * @...: additional arguments
136 * Creates a new #GstStructure with the given name. Parses the
137 * list of variable arguments and sets fields to the values listed.
138 * Variable arguments should be passed as field name, field type,
139 * and value. Last variable argument should be NULL.
141 * Returns: a new #GstStructure
144 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
146 GstStructure *structure;
149 g_return_val_if_fail (name != NULL, NULL);
151 va_start (varargs, firstfield);
153 structure = gst_structure_new_valist (name, firstfield, varargs);
161 * gst_structure_new_valist:
162 * @name: name of new structure
163 * @firstfield: name of first field to set
164 * @varargs: variable argument list
166 * Creates a new #GstStructure with the given name. Structure fields
167 * are set according to the varargs in a manner similar to
168 * @gst_structure_new.
170 * Returns: a new #GstStructure
173 gst_structure_new_valist (const gchar * name,
174 const gchar * firstfield, va_list varargs)
176 GstStructure *structure;
178 g_return_val_if_fail (name != NULL, NULL);
180 structure = gst_structure_empty_new (name);
181 gst_structure_set_valist (structure, firstfield, varargs);
187 * gst_structure_set_parent_refcount:
188 * @structure: a #GstStructure
189 * @refcount: a pointer to the parent's refcount
191 * Sets the parent_refcount field of #GstStructure. This field is used to
192 * determine whether a structure is mutable or not. This function should only be
193 * called by code implementing parent objects of GstStructure, as described in
194 * the MT Refcounting section of the design documents.
197 gst_structure_set_parent_refcount (GstStructure * structure, int *refcount)
199 g_return_if_fail (structure != NULL);
201 /* if we have a parent_refcount already, we can only clear
202 * if with a NULL refcount */
203 if (structure->parent_refcount)
204 g_return_if_fail (refcount == NULL);
206 g_return_if_fail (refcount != NULL);
208 structure->parent_refcount = refcount;
212 * gst_structure_copy:
213 * @structure: a #GstStructure to duplicate
215 * Duplicates a #GstStructure and all its fields and values.
217 * Returns: a new #GstStructure.
220 gst_structure_copy (const GstStructure * structure)
222 GstStructure *new_structure;
223 GstStructureField *field;
226 g_return_val_if_fail (structure != NULL, NULL);
229 gst_structure_id_empty_new_with_size (structure->name,
230 structure->fields->len);
232 for (i = 0; i < structure->fields->len; i++) {
233 GstStructureField new_field = { 0 };
235 field = GST_STRUCTURE_FIELD (structure, i);
237 new_field.name = field->name;
238 gst_value_init_and_copy (&new_field.value, &field->value);
239 g_array_append_val (new_structure->fields, new_field);
242 return new_structure;
246 * gst_structure_free:
247 * @structure: the #GstStructure to free
249 * Frees a #GstStructure and all its fields and values. The structure must not
250 * have a parent when this function is called.
253 gst_structure_free (GstStructure * structure)
255 GstStructureField *field;
258 g_return_if_fail (structure != NULL);
259 g_return_if_fail (structure->parent_refcount == NULL);
261 for (i = 0; i < structure->fields->len; i++) {
262 field = GST_STRUCTURE_FIELD (structure, i);
264 if (G_IS_VALUE (&field->value)) {
265 g_value_unset (&field->value);
268 g_array_free (structure->fields, TRUE);
270 memset (structure, 0xff, sizeof (GstStructure));
276 * gst_structure_get_name:
277 * @structure: a #GstStructure
281 * Returns: the name of the structure.
284 gst_structure_get_name (const GstStructure * structure)
286 g_return_val_if_fail (structure != NULL, NULL);
288 return g_quark_to_string (structure->name);
292 * gst_structure_has_name:
293 * @structure: a #GstStructure
294 * @name: structure name to check for
296 * Returns: TRUE if @name matches the name of the structure.
299 gst_structure_has_name (const GstStructure * structure, const gchar * name)
301 const gchar *structure_name;
303 g_return_val_if_fail (structure != NULL, FALSE);
304 g_return_val_if_fail (name != NULL, FALSE);
306 structure_name = g_quark_to_string (structure->name);
308 return (structure_name && strcmp (structure_name, name) == 0);
312 * gst_structure_get_name_id:
313 * @structure: a #GstStructure
317 * Returns: the quark representing the name of the structure.
320 gst_structure_get_name_id (const GstStructure * structure)
322 g_return_val_if_fail (structure != NULL, 0);
324 return structure->name;
328 * gst_structure_set_name:
329 * @structure: a #GstStructure
330 * @name: the new name of the structure
332 * Sets the name of the structure to the given name. The string
333 * provided is copied before being used.
336 gst_structure_set_name (GstStructure * structure, const gchar * name)
338 g_return_if_fail (structure != NULL);
339 g_return_if_fail (name != NULL);
340 g_return_if_fail (IS_MUTABLE (structure));
342 structure->name = g_quark_from_string (name);
346 * gst_structure_id_set_value:
347 * @structure: a #GstStructure
348 * @field: a #GQuark representing a field
349 * @value: the new value of the field
351 * Sets the field with the given ID to the provided value. If the field
352 * does not exist, it is created. If the field exists, the previous
356 gst_structure_id_set_value (GstStructure * structure,
357 GQuark field, const GValue * value)
359 GstStructureField gsfield = { 0, {0,} };
361 g_return_if_fail (structure != NULL);
362 g_return_if_fail (G_IS_VALUE (value));
363 g_return_if_fail (IS_MUTABLE (structure));
365 gsfield.name = field;
366 gst_value_init_and_copy (&gsfield.value, value);
368 gst_structure_set_field (structure, &gsfield);
372 * gst_structure_set_value:
373 * @structure: a #GstStructure
374 * @fieldname: the name of the field to set
375 * @value: the new value of the field
377 * Sets the field with the given name to the provided value. If the field
378 * does not exist, it is created. If the field exists, the previous
382 gst_structure_set_value (GstStructure * structure,
383 const gchar * fieldname, const GValue * value)
385 g_return_if_fail (structure != NULL);
386 g_return_if_fail (fieldname != NULL);
387 g_return_if_fail (G_IS_VALUE (value));
388 g_return_if_fail (IS_MUTABLE (structure));
390 gst_structure_id_set_value (structure, g_quark_from_string (fieldname),
396 * @structure: a #GstStructure
397 * @fieldname: the name of the field to set
398 * @...: variable arguments
400 * Parses the variable arguments and sets fields accordingly.
401 * Variable arguments should be in the form field name, field type
402 * (as a GType), value. The last variable argument should be NULL.
405 gst_structure_set (GstStructure * structure, const gchar * field, ...)
409 g_return_if_fail (structure != NULL);
411 va_start (varargs, field);
413 gst_structure_set_valist (structure, field, varargs);
419 * gst_structure_set_valist:
420 * @structure: a #GstStructure
421 * @fieldname: the name of the field to set
422 * @varargs: variable arguments
424 * va_list form of #gst_structure_set.
427 gst_structure_set_valist (GstStructure * structure,
428 const gchar * fieldname, va_list varargs)
433 g_return_if_fail (structure != NULL);
434 g_return_if_fail (IS_MUTABLE (structure));
437 GstStructureField field = { 0 };
439 field.name = g_quark_from_string (fieldname);
441 type = va_arg (varargs, GType);
443 #if GLIB_CHECK_VERSION(2,8,0)
444 if (type == G_TYPE_DATE) {
445 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
446 type = GST_TYPE_DATE;
450 g_value_init (&field.value, type);
451 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
453 g_critical ("%s", err);
456 gst_structure_set_field (structure, &field);
458 fieldname = va_arg (varargs, gchar *);
462 /* If the structure currently contains a field with the same name, it is
463 * replaced with the provided field. Otherwise, the field is added to the
464 * structure. The field's value is not deeply copied.
467 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
469 GstStructureField *f;
472 for (i = 0; i < structure->fields->len; i++) {
473 f = GST_STRUCTURE_FIELD (structure, i);
475 if (f->name == field->name) {
476 g_value_unset (&f->value);
477 memcpy (f, field, sizeof (GstStructureField));
482 g_array_append_val (structure->fields, *field);
485 /* If there is no field with the given ID, NULL is returned.
487 static GstStructureField *
488 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
490 GstStructureField *field;
493 g_return_val_if_fail (structure != NULL, NULL);
495 for (i = 0; i < structure->fields->len; i++) {
496 field = GST_STRUCTURE_FIELD (structure, i);
498 if (field->name == field_id)
505 /* If there is no field with the given ID, NULL is returned.
507 static GstStructureField *
508 gst_structure_get_field (const GstStructure * structure,
509 const gchar * fieldname)
511 g_return_val_if_fail (structure != NULL, NULL);
512 g_return_val_if_fail (fieldname != NULL, NULL);
514 return gst_structure_id_get_field (structure,
515 g_quark_from_string (fieldname));
519 * gst_structure_get_value:
520 * @structure: a #GstStructure
521 * @fieldname: the name of the field to get
525 * Returns: the #GValue corresponding to the field with the given name.
528 gst_structure_get_value (const GstStructure * structure,
529 const gchar * fieldname)
531 GstStructureField *field;
533 g_return_val_if_fail (structure != NULL, NULL);
534 g_return_val_if_fail (fieldname != NULL, NULL);
536 field = gst_structure_get_field (structure, fieldname);
540 return &field->value;
544 * gst_structure_id_get_value:
545 * @structure: a #GstStructure
546 * @field: the #GQuark of the field to get
550 * Returns: the #GValue corresponding to the field with the given name
554 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
556 GstStructureField *gsfield;
558 g_return_val_if_fail (structure != NULL, NULL);
560 gsfield = gst_structure_id_get_field (structure, field);
564 return &gsfield->value;
568 * gst_structure_remove_field:
569 * @structure: a #GstStructure
570 * @fieldname: the name of the field to remove
572 * Removes the field with the given name. If the field with the given
573 * name does not exist, the structure is unchanged.
576 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
578 GstStructureField *field;
582 g_return_if_fail (structure != NULL);
583 g_return_if_fail (fieldname != NULL);
584 g_return_if_fail (IS_MUTABLE (structure));
586 id = g_quark_from_string (fieldname);
588 for (i = 0; i < structure->fields->len; i++) {
589 field = GST_STRUCTURE_FIELD (structure, i);
591 if (field->name == id) {
592 if (G_IS_VALUE (&field->value)) {
593 g_value_unset (&field->value);
595 structure->fields = g_array_remove_index (structure->fields, i);
602 * gst_structure_remove_fields:
603 * @structure: a #GstStructure
604 * @fieldname: the name of the field to remove
605 * @...: NULL-terminated list of more fieldnames to remove
607 * Removes the field with the given names. If a field does not exist, the
608 * argument is ignored.
611 gst_structure_remove_fields (GstStructure * structure,
612 const gchar * fieldname, ...)
616 g_return_if_fail (structure != NULL);
617 g_return_if_fail (fieldname != NULL);
618 /* mutability checked in remove_field */
620 va_start (varargs, fieldname);
622 gst_structure_remove_fields_valist (structure, fieldname, varargs);
628 * gst_structure_remove_fields_valist:
629 * @structure: a #GstStructure
630 * @fieldname: the name of the field to remove
631 * @varargs: NULL-terminated list of more fieldnames to remove
633 * Removes the field with the given names. If a field does not exist, the
634 * argument is ignored.
637 gst_structure_remove_fields_valist (GstStructure * structure,
638 const gchar * fieldname, va_list varargs)
640 gchar *field = (gchar *) fieldname;
642 g_return_if_fail (structure != NULL);
643 g_return_if_fail (fieldname != NULL);
644 /* mutability checked in remove_field */
647 gst_structure_remove_field (structure, field);
648 field = va_arg (varargs, char *);
653 * gst_structure_remove_all_fields:
654 * @structure: a #GstStructure
656 * Removes all fields in a GstStructure.
659 gst_structure_remove_all_fields (GstStructure * structure)
661 GstStructureField *field;
664 g_return_if_fail (structure != NULL);
665 g_return_if_fail (IS_MUTABLE (structure));
667 for (i = structure->fields->len - 1; i >= 0; i--) {
668 field = GST_STRUCTURE_FIELD (structure, i);
670 if (G_IS_VALUE (&field->value)) {
671 g_value_unset (&field->value);
673 structure->fields = g_array_remove_index (structure->fields, i);
678 * gst_structure_get_field_type:
679 * @structure: a #GstStructure
680 * @fieldname: the name of the field
682 * Finds the field with the given name, and returns the type of the
683 * value it contains. If the field is not found, G_TYPE_INVALID is
686 * Returns: the #GValue of the field
689 gst_structure_get_field_type (const GstStructure * structure,
690 const gchar * fieldname)
692 GstStructureField *field;
694 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
695 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
697 field = gst_structure_get_field (structure, fieldname);
699 return G_TYPE_INVALID;
701 return G_VALUE_TYPE (&field->value);
705 * gst_structure_n_fields:
706 * @structure: a #GstStructure
710 * Returns: the number of fields in the structure
713 gst_structure_n_fields (const GstStructure * structure)
715 g_return_val_if_fail (structure != NULL, 0);
717 return structure->fields->len;
721 * gst_structure_nth_field_name:
722 * @structure: a #GstStructure
723 * @index: the index to get the name of
725 * Returns: the name of the given field number, counting from 0 onwards.
728 gst_structure_nth_field_name (const GstStructure * structure, guint index)
730 GstStructureField *field;
732 field = GST_STRUCTURE_FIELD (structure, index);
733 return g_quark_to_string (field->name);
737 * gst_structure_foreach:
738 * @structure: a #GstStructure
739 * @func: a function to call for each field
740 * @user_data: private data
742 * Calls the provided function once for each field in the #GstStructure. The
743 * function must not modify the fields. Also see gst_structure_map_in_place().
745 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
749 gst_structure_foreach (const GstStructure * structure,
750 GstStructureForeachFunc func, gpointer user_data)
753 GstStructureField *field;
756 g_return_val_if_fail (structure != NULL, FALSE);
758 for (i = 0; i < structure->fields->len; i++) {
759 field = GST_STRUCTURE_FIELD (structure, i);
761 ret = func (field->name, &field->value, user_data);
770 * gst_structure_map_in_place:
771 * @structure: a #GstStructure
772 * @func: a function to call for each field
773 * @user_data: private data
775 * Calls the provided function once for each field in the #GstStructure. In
776 * contrast to gst_structure_foreach(), the function may modify the fields. The
777 * structure must be mutable.
779 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
783 gst_structure_map_in_place (GstStructure * structure,
784 GstStructureMapFunc func, gpointer user_data)
787 GstStructureField *field;
790 g_return_val_if_fail (structure != NULL, FALSE);
791 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
793 for (i = 0; i < structure->fields->len; i++) {
794 field = GST_STRUCTURE_FIELD (structure, i);
796 ret = func (field->name, &field->value, user_data);
805 * gst_structure_has_field:
806 * @structure: a #GstStructure
807 * @fieldname: the name of a field
811 * Returns: TRUE if the structure contains a field with the given name
814 gst_structure_has_field (const GstStructure * structure,
815 const gchar * fieldname)
817 GstStructureField *field;
819 g_return_val_if_fail (structure != NULL, 0);
820 g_return_val_if_fail (fieldname != NULL, 0);
822 field = gst_structure_get_field (structure, fieldname);
824 return (field != NULL);
828 * gst_structure_has_field_typed:
829 * @structure: a #GstStructure
830 * @fieldname: the name of a field
831 * @type: the type of a value
835 * Returns: TRUE if the structure contains a field with the given name and type
838 gst_structure_has_field_typed (const GstStructure * structure,
839 const gchar * fieldname, GType type)
841 GstStructureField *field;
843 g_return_val_if_fail (structure != NULL, 0);
844 g_return_val_if_fail (fieldname != NULL, 0);
846 field = gst_structure_get_field (structure, fieldname);
850 return (G_VALUE_TYPE (&field->value) == type);
854 /* utility functions */
857 * gst_structure_get_boolean:
858 * @structure: a #GstStructure
859 * @fieldname: the name of a field
860 * @value: a pointer to a #gboolean to set
862 * Sets the boolean pointed to by @value corresponding to the value of the
863 * given field. Caller is responsible for making sure the field exists
864 * and has the correct type.
866 * Returns: TRUE if the value could be set correctly
869 gst_structure_get_boolean (const GstStructure * structure,
870 const gchar * fieldname, gboolean * value)
872 GstStructureField *field;
874 g_return_val_if_fail (structure != NULL, FALSE);
875 g_return_val_if_fail (fieldname != NULL, FALSE);
877 field = gst_structure_get_field (structure, fieldname);
881 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
884 *value = g_value_get_boolean (&field->value);
890 * gst_structure_get_int:
891 * @structure: a #GstStructure
892 * @fieldname: the name of a field
893 * @value: a pointer to an int to set
895 * Sets the int pointed to by @value corresponding to the value of the
896 * given field. Caller is responsible for making sure the field exists
897 * and has the correct type.
899 * Returns: TRUE if the value could be set correctly
902 gst_structure_get_int (const GstStructure * structure,
903 const gchar * fieldname, gint * value)
905 GstStructureField *field;
907 g_return_val_if_fail (structure != NULL, FALSE);
908 g_return_val_if_fail (fieldname != NULL, FALSE);
909 g_return_val_if_fail (value != NULL, FALSE);
911 field = gst_structure_get_field (structure, fieldname);
915 if (!G_VALUE_HOLDS_INT (&field->value))
918 *value = g_value_get_int (&field->value);
924 * gst_structure_get_fourcc:
925 * @structure: a #GstStructure
926 * @fieldname: the name of a field
927 * @value: a pointer to a #GstFourcc to set
929 * Sets the #GstFourcc pointed to by @value corresponding to the value of the
930 * given field. Caller is responsible for making sure the field exists
931 * and has the correct type.
933 * Returns: TRUE if the value could be set correctly
936 gst_structure_get_fourcc (const GstStructure * structure,
937 const gchar * fieldname, guint32 * value)
939 GstStructureField *field;
941 g_return_val_if_fail (structure != NULL, FALSE);
942 g_return_val_if_fail (fieldname != NULL, FALSE);
943 g_return_val_if_fail (value != NULL, FALSE);
945 field = gst_structure_get_field (structure, fieldname);
949 if (!GST_VALUE_HOLDS_FOURCC (&field->value))
952 *value = gst_value_get_fourcc (&field->value);
958 * gst_structure_get_date:
959 * @structure: a #GstStructure
960 * @fieldname: the name of a field
961 * @value: a pointer to a #GDate to set
963 * Sets the date pointed to by @value corresponding to the date of the
964 * given field. Caller is responsible for making sure the field exists
965 * and has the correct type.
967 * Returns: TRUE if the value could be set correctly
970 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
973 GstStructureField *field;
975 g_return_val_if_fail (structure != NULL, FALSE);
976 g_return_val_if_fail (fieldname != NULL, FALSE);
977 g_return_val_if_fail (value != NULL, FALSE);
979 field = gst_structure_get_field (structure, fieldname);
983 if (!GST_VALUE_HOLDS_DATE (&field->value))
986 *value = g_value_dup_boxed (&field->value);
992 * gst_structure_get_clock_time:
993 * @structure: a #GstStructure
994 * @fieldname: the name of a field
995 * @value: a pointer to a #GstClockTime to set
997 * Sets the clock time pointed to by @value corresponding to the clock time
998 * of the given field. Caller is responsible for making sure the field exists
999 * and has the correct type.
1001 * Returns: TRUE if the value could be set correctly
1004 gst_structure_get_clock_time (const GstStructure * structure,
1005 const gchar * fieldname, GstClockTime * value)
1007 GstStructureField *field;
1009 g_return_val_if_fail (structure != NULL, FALSE);
1010 g_return_val_if_fail (fieldname != NULL, FALSE);
1011 g_return_val_if_fail (value != NULL, FALSE);
1013 field = gst_structure_get_field (structure, fieldname);
1017 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1020 *value = g_value_get_uint64 (&field->value);
1026 * gst_structure_get_double:
1027 * @structure: a #GstStructure
1028 * @fieldname: the name of a field
1029 * @value: a pointer to a #GstFourcc to set
1031 * Sets the double pointed to by @value corresponding to the value of the
1032 * given field. Caller is responsible for making sure the field exists
1033 * and has the correct type.
1035 * Returns: TRUE if the value could be set correctly
1038 gst_structure_get_double (const GstStructure * structure,
1039 const gchar * fieldname, gdouble * value)
1041 GstStructureField *field;
1043 g_return_val_if_fail (structure != NULL, FALSE);
1044 g_return_val_if_fail (fieldname != NULL, FALSE);
1045 g_return_val_if_fail (value != NULL, FALSE);
1047 field = gst_structure_get_field (structure, fieldname);
1051 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1054 *value = g_value_get_double (&field->value);
1060 * gst_structure_get_string:
1061 * @structure: a #GstStructure
1062 * @fieldname: the name of a field
1064 * Finds the field corresponding to @fieldname, and returns the string
1065 * contained in the field's value. Caller is responsible for making
1066 * sure the field exists and has the correct type.
1068 * The string should not be modified, and remains valid until the next
1069 * call to a gst_structure_*() function with the given structure.
1071 * Returns: a pointer to the string
1074 gst_structure_get_string (const GstStructure * structure,
1075 const gchar * fieldname)
1077 GstStructureField *field;
1079 g_return_val_if_fail (structure != NULL, NULL);
1080 g_return_val_if_fail (fieldname != NULL, NULL);
1082 field = gst_structure_get_field (structure, fieldname);
1086 if (!G_VALUE_HOLDS_STRING (&field->value))
1089 return g_value_get_string (&field->value);
1093 * gst_structure_get_enum:
1094 * @structure: a #GstStructure
1095 * @fieldname: the name of a field
1096 * @enumtype: the enum type of a field
1097 * @value: a pointer to an int to set
1099 * Sets the int pointed to by @value corresponding to the value of the
1100 * given field. Caller is responsible for making sure the field exists,
1101 * has the correct type and that the enumtype is correct.
1103 * Returns: TRUE if the value could be set correctly
1106 gst_structure_get_enum (const GstStructure * structure,
1107 const gchar * fieldname, GType enumtype, gint * value)
1109 GstStructureField *field;
1111 g_return_val_if_fail (structure != NULL, FALSE);
1112 g_return_val_if_fail (fieldname != NULL, FALSE);
1113 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1114 g_return_val_if_fail (value != NULL, FALSE);
1116 field = gst_structure_get_field (structure, fieldname);
1120 if (!G_VALUE_HOLDS_ENUM (&field->value))
1122 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1125 *value = g_value_get_enum (&field->value);
1130 typedef struct _GstStructureAbbreviation
1135 GstStructureAbbreviation;
1137 static GstStructureAbbreviation *
1138 gst_structure_get_abbrs (gint * n_abbrs)
1140 static GstStructureAbbreviation *abbrs = NULL;
1141 static gint num = 0;
1143 if (abbrs == NULL) {
1144 /* dynamically generate the array */
1145 GstStructureAbbreviation dyn_abbrs[] = {
1150 {"float", G_TYPE_FLOAT}
1154 {"double", G_TYPE_DOUBLE}
1156 {"d", G_TYPE_DOUBLE}
1158 {"buffer", GST_TYPE_BUFFER}
1160 {"fourcc", GST_TYPE_FOURCC}
1162 {"4", GST_TYPE_FOURCC}
1164 {"fraction", GST_TYPE_FRACTION}
1166 {"boolean", G_TYPE_BOOLEAN}
1168 {"bool", G_TYPE_BOOLEAN}
1170 {"b", G_TYPE_BOOLEAN}
1172 {"string", G_TYPE_STRING}
1174 {"str", G_TYPE_STRING}
1176 {"s", G_TYPE_STRING}
1178 num = G_N_ELEMENTS (dyn_abbrs);
1179 /* permanently allocate and copy the array now */
1180 abbrs = g_new0 (GstStructureAbbreviation, num);
1181 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * num);
1189 gst_structure_from_abbr (const char *type_name)
1192 GstStructureAbbreviation *abbrs;
1195 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1197 abbrs = gst_structure_get_abbrs (&n_abbrs);
1199 for (i = 0; i < n_abbrs; i++) {
1200 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1201 return abbrs[i].type;
1205 /* this is the fallback */
1206 return g_type_from_name (type_name);
1210 gst_structure_to_abbr (GType type)
1213 GstStructureAbbreviation *abbrs;
1216 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1218 abbrs = gst_structure_get_abbrs (&n_abbrs);
1220 for (i = 0; i < n_abbrs; i++) {
1221 if (type == abbrs[i].type) {
1222 return abbrs[i].type_name;
1226 return g_type_name (type);
1230 gst_structure_value_get_generic_type (GValue * val)
1232 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1233 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1234 GArray *array = g_value_peek_pointer (val);
1236 if (array->len > 0) {
1237 GValue *value = &g_array_index (array, GValue, 0);
1239 return gst_structure_value_get_generic_type (value);
1243 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1245 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1246 return G_TYPE_DOUBLE;
1248 return G_VALUE_TYPE (val);
1251 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1252 ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1256 * gst_structure_to_string:
1257 * @structure: a #GstStructure
1259 * Converts @structure to a human-readable representation.
1261 * Returns: a pointer to string allocated by g_malloc()
1264 gst_structure_to_string (const GstStructure * structure)
1266 GstStructureField *field;
1270 /* NOTE: This function is potentially called by the debug system,
1271 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1272 * should be careful to avoid recursion. This includes any functions
1273 * called by gst_structure_to_string. In particular, calls should
1274 * not use the GST_PTR_FORMAT extension. */
1276 g_return_val_if_fail (structure != NULL, NULL);
1278 s = g_string_new ("");
1279 /* FIXME this string may need to be escaped */
1280 g_string_append_printf (s, "%s", g_quark_to_string (structure->name));
1281 for (i = 0; i < structure->fields->len; i++) {
1285 field = GST_STRUCTURE_FIELD (structure, i);
1287 t = gst_value_serialize (&field->value);
1288 type = gst_structure_value_get_generic_type (&field->value);
1290 g_string_append_printf (s, ", %s=(%s)%s", g_quark_to_string (field->name),
1291 gst_structure_to_abbr (type), GST_STR_NULL (t));
1294 return g_string_free (s, FALSE);
1298 * r will still point to the string. if end == next, the string will not be
1299 * null-terminated. In all other cases it will be.
1300 * end = pointer to char behind end of string, next = pointer to start of
1302 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1305 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next)
1315 ret = gst_structure_parse_simple_string (s, end);
1344 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1347 GValue value1 = { 0 };
1348 GValue value2 = { 0 };
1357 ret = gst_structure_parse_value (s, &s, &value1, type);
1361 while (g_ascii_isspace (*s))
1368 while (g_ascii_isspace (*s))
1371 ret = gst_structure_parse_value (s, &s, &value2, type);
1375 while (g_ascii_isspace (*s))
1382 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1385 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1386 range_type = GST_TYPE_DOUBLE_RANGE;
1387 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1388 range_type = GST_TYPE_INT_RANGE;
1393 g_value_init (value, range_type);
1394 if (range_type == GST_TYPE_DOUBLE_RANGE) {
1395 gst_value_set_double_range (value, g_value_get_double (&value1),
1396 g_value_get_double (&value2));
1398 gst_value_set_int_range (value, g_value_get_int (&value1),
1399 g_value_get_int (&value2));
1407 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1408 GType type, GType list_type, char begin, char end)
1410 GValue list_value = { 0 };
1414 g_value_init (value, list_type);
1415 array = g_value_peek_pointer (value);
1421 while (g_ascii_isspace (*s))
1429 ret = gst_structure_parse_value (s, &s, &list_value, type);
1433 g_array_append_val (array, list_value);
1435 while (g_ascii_isspace (*s))
1443 while (g_ascii_isspace (*s))
1446 memset (&list_value, 0, sizeof (list_value));
1447 ret = gst_structure_parse_value (s, &s, &list_value, type);
1451 g_array_append_val (array, list_value);
1452 while (g_ascii_isspace (*s))
1463 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
1465 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
1470 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
1473 return gst_structure_parse_any_list (s, after, value, type,
1474 GST_TYPE_ARRAY, '<', '>');
1478 gst_structure_parse_simple_string (gchar * str, gchar ** end)
1482 while (GST_ASCII_IS_STRING (*s)) {
1492 gst_structure_parse_field (gchar * str,
1493 gchar ** after, GstStructureField * field)
1502 while (g_ascii_isspace (*s))
1505 if (!gst_structure_parse_simple_string (s, &name_end))
1509 while (g_ascii_isspace (*s))
1518 field->name = g_quark_from_string (name);
1521 if (!gst_structure_parse_value (s, &s, &field->value, G_TYPE_INVALID))
1529 gst_structure_parse_value (gchar * str,
1530 gchar ** after, GValue * value, GType default_type)
1539 GType type = default_type;
1543 while (g_ascii_isspace (*s))
1548 type = G_TYPE_INVALID;
1551 while (g_ascii_isspace (*s))
1554 if (!gst_structure_parse_simple_string (s, &type_end))
1557 while (g_ascii_isspace (*s))
1562 while (g_ascii_isspace (*s))
1567 type = gst_structure_from_abbr (type_name);
1570 if (type == G_TYPE_INVALID)
1574 while (g_ascii_isspace (*s))
1577 ret = gst_structure_parse_range (s, &s, value, type);
1578 } else if (*s == '{') {
1579 ret = gst_structure_parse_list (s, &s, value, type);
1580 } else if (*s == '<') {
1581 ret = gst_structure_parse_array (s, &s, value, type);
1584 if (!gst_structure_parse_string (s, &value_end, &s))
1589 if (type == G_TYPE_INVALID) {
1590 GType try_types[] = { G_TYPE_INT, G_TYPE_DOUBLE, G_TYPE_STRING };
1593 for (i = 0; i < 3; i++) {
1594 g_value_init (value, try_types[i]);
1595 ret = gst_value_deserialize (value, value_s);
1598 g_value_unset (value);
1601 g_value_init (value, type);
1603 ret = gst_value_deserialize (value, value_s);
1614 * gst_structure_from_string:
1615 * @string: a string representation of a #GstStructure.
1616 * @end: pointer to store the end of the string in.
1618 * Creates a #GstStructure from a string representation.
1619 * If end is not NULL, a pointer to the place inside the given string
1620 * where parsing ended will be returned.
1622 * Returns: a new #GstStructure
1625 gst_structure_from_string (const gchar * string, gchar ** end)
1632 GstStructure *structure = NULL;
1633 GstStructureField field = { 0 };
1635 g_return_val_if_fail (string != NULL, NULL);
1637 copy = g_strdup (string);
1641 if (!gst_structure_parse_string (r, &w, &r))
1644 while (g_ascii_isspace (*r))
1646 if (*r != 0 && *r != ';' && *r != ',')
1651 structure = gst_structure_empty_new (name);
1654 while (*r && (*r != ';')) {
1658 while (*r && g_ascii_isspace (*r))
1661 memset (&field, 0, sizeof (field));
1662 if (!gst_structure_parse_field (r, &r, &field))
1664 gst_structure_set_field (structure, &field);
1665 while (*r && g_ascii_isspace (*r))
1670 *end = (char *) string + (r - copy);
1677 gst_structure_free (structure);
1683 gst_structure_transform_to_string (const GValue * src_value,
1684 GValue * dest_value)
1686 g_return_if_fail (src_value != NULL);
1687 g_return_if_fail (dest_value != NULL);
1689 dest_value->data[0].v_pointer =
1690 gst_structure_to_string (src_value->data[0].v_pointer);
1693 static GstStructure *
1694 gst_structure_copy_conditional (const GstStructure * structure)
1697 return gst_structure_copy (structure);
1701 /* fixate utility functions */
1704 * gst_caps_structure_fixate_field_nearest_int:
1705 * @structure: a #GstStructure
1706 * @field_name: a field in @structure
1707 * @target: the target value of the fixation
1709 * Fixates a #GstStructure by changing the given field to the nearest
1710 * integer to @target that is a subset of the existing field.
1712 * Returns: TRUE if the structure could be fixated
1714 /* FIXME: rename to gst_structure_... */
1716 gst_caps_structure_fixate_field_nearest_int (GstStructure * structure,
1717 const char *field_name, int target)
1719 const GValue *value;
1721 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1722 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1724 value = gst_structure_get_value (structure, field_name);
1726 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
1729 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
1732 x = gst_value_get_int_range_min (value);
1735 x = gst_value_get_int_range_max (value);
1738 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
1740 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1741 const GValue *list_value;
1744 int best_index = -1;
1746 n = gst_value_list_get_size (value);
1747 for (i = 0; i < n; i++) {
1748 list_value = gst_value_list_get_value (value, i);
1749 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
1750 int x = g_value_get_int (list_value);
1752 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
1758 if (best_index != -1) {
1759 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
1769 * gst_caps_structure_fixate_field_nearest_double:
1770 * @structure: a #GstStructure
1771 * @field_name: a field in @structure
1772 * @target: the target value of the fixation
1774 * Fixates a #GstStructure by changing the given field to the nearest
1775 * double to @target that is a subset of the existing field.
1777 * Returns: TRUE if the structure could be fixated
1780 gst_caps_structure_fixate_field_nearest_double (GstStructure * structure,
1781 const char *field_name, double target)
1783 const GValue *value;
1785 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1786 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1788 value = gst_structure_get_value (structure, field_name);
1790 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
1793 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
1796 x = gst_value_get_double_range_min (value);
1799 x = gst_value_get_double_range_max (value);
1802 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
1804 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1805 const GValue *list_value;
1808 int best_index = -1;
1810 n = gst_value_list_get_size (value);
1811 for (i = 0; i < n; i++) {
1812 list_value = gst_value_list_get_value (value, i);
1813 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
1814 double x = g_value_get_double (list_value);
1816 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
1822 if (best_index != -1) {
1823 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
1834 * gst_caps_structure_fixate_field_boolean:
1835 * @structure: a #GstStructure
1836 * @field_name: a field in @structure
1837 * @target: the target value of the fixation
1839 * Fixates a #GstStructure by changing the given @field_name field to the given
1840 * @target boolean if that field is not fixed yet.
1842 * Returns: TRUE if the structure could be fixated
1844 /* FIXME: rename to gst_structure_... */
1846 gst_caps_structure_fixate_field_boolean (GstStructure * structure,
1847 const char *field_name, gboolean target)
1849 const GValue *value;
1851 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1852 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1854 value = gst_structure_get_value (structure, field_name);
1856 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
1859 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1860 const GValue *list_value;
1863 int best_index = -1;
1865 n = gst_value_list_get_size (value);
1866 for (i = 0; i < n; i++) {
1867 list_value = gst_value_list_get_value (value, i);
1868 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
1869 gboolean x = g_value_get_boolean (list_value);
1871 if (best_index == -1 || x == target) {
1877 if (best_index != -1) {
1878 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);