1 /* GStreamer
2 * Copyright (C) 2003 David A. Schleef <ds@schleef.org>
3 *
4 * gststructure.c: lists of { GQuark, GValue } tuples
5 *
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.
10 *
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.
15 *
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.
20 */
22 /**
23 * SECTION:gststructure
24 * @short_description: Generic structure containing fields of names and values
25 * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
26 *
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.
29 *
30 * In addition to the key/value pairs, a #GstStructure also has a name.
31 *
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.
37 *
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
40 * of the values.
41 *
42 * Field values can be changed with gst_structure_set_value() or gst_structure_set().
43 *
44 * Field values can be retrieved with gst_structure_get_value() or the more convenient
45 * gst_structure_get_*() functions.
46 *
47 * Fields can be removed with gst_structure_remove_field() or gst_structure_remove_fields().
48 *
49 * Last reviewed on 2005-11-09 (0.9.4)
50 */
52 #ifdef HAVE_CONFIG_H
53 #include "config.h"
54 #endif
56 #include <string.h>
58 #include "gst_private.h"
59 #include <gst/gst.h>
60 #include <gobject/gvaluecollector.h>
62 typedef struct _GstStructureField GstStructureField;
64 struct _GstStructureField
65 {
66 GQuark name;
67 GValue value;
68 };
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,
84 GValue * dest_value);
85 static GstStructure *gst_structure_copy_conditional (const GstStructure *
86 structure);
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);
91 GType
92 gst_structure_get_type (void)
93 {
94 static GType gst_structure_type = 0;
96 if (G_UNLIKELY (gst_structure_type == 0)) {
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);
103 }
105 return gst_structure_type;
106 }
108 static GstStructure *
109 gst_structure_id_empty_new_with_size (GQuark quark, guint prealloc)
110 {
111 GstStructure *structure;
113 structure = g_new0 (GstStructure, 1);
114 structure->type = gst_structure_get_type ();
115 structure->name = quark;
116 structure->fields =
117 g_array_sized_new (FALSE, TRUE, sizeof (GstStructureField), prealloc);
119 return structure;
120 }
122 /**
123 * gst_structure_id_empty_new:
124 * @quark: name of new structure
125 *
126 * Creates a new, empty #GstStructure with the given name as a GQuark.
127 *
128 * Returns: a new, empty #GstStructure
129 */
130 GstStructure *
131 gst_structure_id_empty_new (GQuark quark)
132 {
133 g_return_val_if_fail (quark != 0, NULL);
135 return gst_structure_id_empty_new_with_size (quark, 0);
136 }
138 /**
139 * gst_structure_empty_new:
140 * @name: name of new structure
141 *
142 * Creates a new, empty #GstStructure with the given name.
143 *
144 * Returns: a new, empty #GstStructure
145 */
146 GstStructure *
147 gst_structure_empty_new (const gchar * name)
148 {
149 g_return_val_if_fail (name != NULL, NULL);
151 return gst_structure_id_empty_new_with_size (g_quark_from_string (name), 0);
152 }
154 /**
155 * gst_structure_new:
156 * @name: name of new structure
157 * @firstfield: name of first field to set
158 * @...: additional arguments
159 *
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.
164 *
165 * Returns: a new #GstStructure
166 */
167 GstStructure *
168 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
169 {
170 GstStructure *structure;
171 va_list varargs;
173 g_return_val_if_fail (name != NULL, NULL);
175 va_start (varargs, firstfield);
177 structure = gst_structure_new_valist (name, firstfield, varargs);
179 va_end (varargs);
181 return structure;
182 }
184 /**
185 * gst_structure_new_valist:
186 * @name: name of new structure
187 * @firstfield: name of first field to set
188 * @varargs: variable argument list
189 *
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.
193 *
194 * Returns: a new #GstStructure
195 */
196 GstStructure *
197 gst_structure_new_valist (const gchar * name,
198 const gchar * firstfield, va_list varargs)
199 {
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);
207 return structure;
208 }
210 /**
211 * gst_structure_set_parent_refcount:
212 * @structure: a #GstStructure
213 * @refcount: a pointer to the parent's refcount
214 *
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.
219 */
220 void
221 gst_structure_set_parent_refcount (GstStructure * structure, int *refcount)
222 {
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);
229 else
230 g_return_if_fail (refcount != NULL);
232 structure->parent_refcount = refcount;
233 }
235 /**
236 * gst_structure_copy:
237 * @structure: a #GstStructure to duplicate
238 *
239 * Duplicates a #GstStructure and all its fields and values.
240 *
241 * Returns: a new #GstStructure.
242 */
243 GstStructure *
244 gst_structure_copy (const GstStructure * structure)
245 {
246 GstStructure *new_structure;
247 GstStructureField *field;
248 guint i;
250 g_return_val_if_fail (structure != NULL, NULL);
252 new_structure =
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);
264 }
266 return new_structure;
267 }
269 /**
270 * gst_structure_free:
271 * @structure: the #GstStructure to free
272 *
273 * Frees a #GstStructure and all its fields and values. The structure must not
274 * have a parent when this function is called.
275 */
276 void
277 gst_structure_free (GstStructure * structure)
278 {
279 GstStructureField *field;
280 guint i;
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);
290 }
291 }
292 g_array_free (structure->fields, TRUE);
293 #ifdef USE_POISONING
294 memset (structure, 0xff, sizeof (GstStructure));
295 #endif
296 g_free (structure);
297 }
299 /**
300 * gst_structure_get_name:
301 * @structure: a #GstStructure
302 *
303 * Get the name of @structure as a string.
304 *
305 * Returns: the name of the structure.
306 */
307 const gchar *
308 gst_structure_get_name (const GstStructure * structure)
309 {
310 g_return_val_if_fail (structure != NULL, NULL);
312 return g_quark_to_string (structure->name);
313 }
315 /**
316 * gst_structure_has_name:
317 * @structure: a #GstStructure
318 * @name: structure name to check for
319 *
320 * Checks if the structure has the given name
321 *
322 * Returns: TRUE if @name matches the name of the structure.
323 */
324 gboolean
325 gst_structure_has_name (const GstStructure * structure, const gchar * name)
326 {
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);
335 }
337 /**
338 * gst_structure_get_name_id:
339 * @structure: a #GstStructure
340 *
341 * Get the name of @structure as a GQuark.
342 *
343 * Returns: the quark representing the name of the structure.
344 */
345 GQuark
346 gst_structure_get_name_id (const GstStructure * structure)
347 {
348 g_return_val_if_fail (structure != NULL, 0);
350 return structure->name;
351 }
353 /**
354 * gst_structure_set_name:
355 * @structure: a #GstStructure
356 * @name: the new name of the structure
357 *
358 * Sets the name of the structure to the given name. The string
359 * provided is copied before being used.
360 */
361 void
362 gst_structure_set_name (GstStructure * structure, const gchar * name)
363 {
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);
369 }
371 /**
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
376 *
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.
380 */
381 void
382 gst_structure_id_set_value (GstStructure * structure,
383 GQuark field, const GValue * value)
384 {
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 /* if someones disables GST_DEBUG output, they probably do it for
392 * performance reasons, so skip the UTF-8 check here as well then */
393 #ifndef GST_DISABLE_GST_DEBUG
394 if (G_VALUE_HOLDS_STRING (value)) {
395 const gchar *s;
397 s = g_value_get_string (value);
398 if (s != NULL && !g_utf8_validate (s, -1, NULL)) {
399 g_warning ("Trying to set string field '%s' on structure, but string is "
400 "not valid UTF-8. Please file a bug.", g_quark_to_string (field));
401 }
402 }
403 #endif
405 gsfield.name = field;
406 gst_value_init_and_copy (&gsfield.value, value);
408 gst_structure_set_field (structure, &gsfield);
409 }
411 /**
412 * gst_structure_set_value:
413 * @structure: a #GstStructure
414 * @fieldname: the name of the field to set
415 * @value: the new value of the field
416 *
417 * Sets the field with the given name @field to @value. If the field
418 * does not exist, it is created. If the field exists, the previous
419 * value is replaced and freed.
420 */
421 void
422 gst_structure_set_value (GstStructure * structure,
423 const gchar * fieldname, const GValue * value)
424 {
425 g_return_if_fail (structure != NULL);
426 g_return_if_fail (fieldname != NULL);
427 g_return_if_fail (G_IS_VALUE (value));
428 g_return_if_fail (IS_MUTABLE (structure));
430 gst_structure_id_set_value (structure, g_quark_from_string (fieldname),
431 value);
432 }
434 /**
435 * gst_structure_set:
436 * @structure: a #GstStructure
437 * @fieldname: the name of the field to set
438 * @...: variable arguments
439 *
440 * Parses the variable arguments and sets fields accordingly.
441 * Variable arguments should be in the form field name, field type
442 * (as a GType), value(s). The last variable argument should be NULL.
443 */
444 void
445 gst_structure_set (GstStructure * structure, const gchar * field, ...)
446 {
447 va_list varargs;
449 g_return_if_fail (structure != NULL);
451 va_start (varargs, field);
453 gst_structure_set_valist (structure, field, varargs);
455 va_end (varargs);
456 }
458 /**
459 * gst_structure_set_valist:
460 * @structure: a #GstStructure
461 * @fieldname: the name of the field to set
462 * @varargs: variable arguments
463 *
464 * va_list form of gst_structure_set().
465 */
466 void
467 gst_structure_set_valist (GstStructure * structure,
468 const gchar * fieldname, va_list varargs)
469 {
470 gchar *err = NULL;
471 GType type;
473 g_return_if_fail (structure != NULL);
474 g_return_if_fail (IS_MUTABLE (structure));
476 while (fieldname) {
477 GstStructureField field = { 0 };
479 field.name = g_quark_from_string (fieldname);
481 type = va_arg (varargs, GType);
483 if (type == G_TYPE_DATE) {
484 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
485 type = GST_TYPE_DATE;
486 }
488 g_value_init (&field.value, type);
489 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
490 if (err) {
491 g_critical ("%s", err);
492 return;
493 }
494 gst_structure_set_field (structure, &field);
496 fieldname = va_arg (varargs, gchar *);
497 }
498 }
500 /* If the structure currently contains a field with the same name, it is
501 * replaced with the provided field. Otherwise, the field is added to the
502 * structure. The field's value is not deeply copied.
503 */
504 static void
505 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
506 {
507 GstStructureField *f;
508 guint i;
510 for (i = 0; i < structure->fields->len; i++) {
511 f = GST_STRUCTURE_FIELD (structure, i);
513 if (f->name == field->name) {
514 g_value_unset (&f->value);
515 memcpy (f, field, sizeof (GstStructureField));
516 return;
517 }
518 }
520 g_array_append_val (structure->fields, *field);
521 }
523 /* If there is no field with the given ID, NULL is returned.
524 */
525 static GstStructureField *
526 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
527 {
528 GstStructureField *field;
529 guint i;
531 g_return_val_if_fail (structure != NULL, NULL);
533 for (i = 0; i < structure->fields->len; i++) {
534 field = GST_STRUCTURE_FIELD (structure, i);
536 if (field->name == field_id)
537 return field;
538 }
540 return NULL;
541 }
543 /* If there is no field with the given ID, NULL is returned.
544 */
545 static GstStructureField *
546 gst_structure_get_field (const GstStructure * structure,
547 const gchar * fieldname)
548 {
549 g_return_val_if_fail (structure != NULL, NULL);
550 g_return_val_if_fail (fieldname != NULL, NULL);
552 return gst_structure_id_get_field (structure,
553 g_quark_from_string (fieldname));
554 }
556 /**
557 * gst_structure_get_value:
558 * @structure: a #GstStructure
559 * @fieldname: the name of the field to get
560 *
561 * Get the value of the field with name @fieldname.
562 *
563 * Returns: the #GValue corresponding to the field with the given name.
564 */
565 const GValue *
566 gst_structure_get_value (const GstStructure * structure,
567 const gchar * fieldname)
568 {
569 GstStructureField *field;
571 g_return_val_if_fail (structure != NULL, NULL);
572 g_return_val_if_fail (fieldname != NULL, NULL);
574 field = gst_structure_get_field (structure, fieldname);
575 if (field == NULL)
576 return NULL;
578 return &field->value;
579 }
581 /**
582 * gst_structure_id_get_value:
583 * @structure: a #GstStructure
584 * @field: the #GQuark of the field to get
585 *
586 * Get the value of the field with GQuark @field.
587 *
588 * Returns: the #GValue corresponding to the field with the given name
589 * identifier.
590 */
591 const GValue *
592 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
593 {
594 GstStructureField *gsfield;
596 g_return_val_if_fail (structure != NULL, NULL);
598 gsfield = gst_structure_id_get_field (structure, field);
599 if (gsfield == NULL)
600 return NULL;
602 return &gsfield->value;
603 }
605 /**
606 * gst_structure_remove_field:
607 * @structure: a #GstStructure
608 * @fieldname: the name of the field to remove
609 *
610 * Removes the field with the given name. If the field with the given
611 * name does not exist, the structure is unchanged.
612 */
613 void
614 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
615 {
616 GstStructureField *field;
617 GQuark id;
618 guint i;
620 g_return_if_fail (structure != NULL);
621 g_return_if_fail (fieldname != NULL);
622 g_return_if_fail (IS_MUTABLE (structure));
624 id = g_quark_from_string (fieldname);
626 for (i = 0; i < structure->fields->len; i++) {
627 field = GST_STRUCTURE_FIELD (structure, i);
629 if (field->name == id) {
630 if (G_IS_VALUE (&field->value)) {
631 g_value_unset (&field->value);
632 }
633 structure->fields = g_array_remove_index (structure->fields, i);
634 return;
635 }
636 }
637 }
639 /**
640 * gst_structure_remove_fields:
641 * @structure: a #GstStructure
642 * @fieldname: the name of the field to remove
643 * @...: NULL-terminated list of more fieldnames to remove
644 *
645 * Removes the fields with the given names. If a field does not exist, the
646 * argument is ignored.
647 */
648 void
649 gst_structure_remove_fields (GstStructure * structure,
650 const gchar * fieldname, ...)
651 {
652 va_list varargs;
654 g_return_if_fail (structure != NULL);
655 g_return_if_fail (fieldname != NULL);
656 /* mutability checked in remove_field */
658 va_start (varargs, fieldname);
660 gst_structure_remove_fields_valist (structure, fieldname, varargs);
662 va_end (varargs);
663 }
665 /**
666 * gst_structure_remove_fields_valist:
667 * @structure: a #GstStructure
668 * @fieldname: the name of the field to remove
669 * @varargs: NULL-terminated list of more fieldnames to remove
670 *
671 * va_list form of gst_structure_remove_fields().
672 */
673 void
674 gst_structure_remove_fields_valist (GstStructure * structure,
675 const gchar * fieldname, va_list varargs)
676 {
677 gchar *field = (gchar *) fieldname;
679 g_return_if_fail (structure != NULL);
680 g_return_if_fail (fieldname != NULL);
681 /* mutability checked in remove_field */
683 while (field) {
684 gst_structure_remove_field (structure, field);
685 field = va_arg (varargs, char *);
686 }
687 }
689 /**
690 * gst_structure_remove_all_fields:
691 * @structure: a #GstStructure
692 *
693 * Removes all fields in a GstStructure.
694 */
695 void
696 gst_structure_remove_all_fields (GstStructure * structure)
697 {
698 GstStructureField *field;
699 int i;
701 g_return_if_fail (structure != NULL);
702 g_return_if_fail (IS_MUTABLE (structure));
704 for (i = structure->fields->len - 1; i >= 0; i--) {
705 field = GST_STRUCTURE_FIELD (structure, i);
707 if (G_IS_VALUE (&field->value)) {
708 g_value_unset (&field->value);
709 }
710 structure->fields = g_array_remove_index (structure->fields, i);
711 }
712 }
714 /**
715 * gst_structure_get_field_type:
716 * @structure: a #GstStructure
717 * @fieldname: the name of the field
718 *
719 * Finds the field with the given name, and returns the type of the
720 * value it contains. If the field is not found, G_TYPE_INVALID is
721 * returned.
722 *
723 * Returns: the #GValue of the field
724 */
725 GType
726 gst_structure_get_field_type (const GstStructure * structure,
727 const gchar * fieldname)
728 {
729 GstStructureField *field;
731 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
732 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
734 field = gst_structure_get_field (structure, fieldname);
735 if (field == NULL)
736 return G_TYPE_INVALID;
738 return G_VALUE_TYPE (&field->value);
739 }
741 /**
742 * gst_structure_n_fields:
743 * @structure: a #GstStructure
744 *
745 * Get the number of fields in the structure.
746 *
747 * Returns: the number of fields in the structure
748 */
749 gint
750 gst_structure_n_fields (const GstStructure * structure)
751 {
752 g_return_val_if_fail (structure != NULL, 0);
754 return structure->fields->len;
755 }
757 /**
758 * gst_structure_nth_field_name:
759 * @structure: a #GstStructure
760 * @index: the index to get the name of
761 *
762 * Get the name of the given field number, counting from 0 onwards.
763 *
764 * Returns: the name of the given field number
765 */
766 const gchar *
767 gst_structure_nth_field_name (const GstStructure * structure, guint index)
768 {
769 GstStructureField *field;
771 field = GST_STRUCTURE_FIELD (structure, index);
772 return g_quark_to_string (field->name);
773 }
775 /**
776 * gst_structure_foreach:
777 * @structure: a #GstStructure
778 * @func: a function to call for each field
779 * @user_data: private data
780 *
781 * Calls the provided function once for each field in the #GstStructure. The
782 * function must not modify the fields. Also see gst_structure_map_in_place().
783 *
784 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
785 * FALSE otherwise.
786 */
787 gboolean
788 gst_structure_foreach (const GstStructure * structure,
789 GstStructureForeachFunc func, gpointer user_data)
790 {
791 guint i;
792 GstStructureField *field;
793 gboolean ret;
795 g_return_val_if_fail (structure != NULL, FALSE);
797 for (i = 0; i < structure->fields->len; i++) {
798 field = GST_STRUCTURE_FIELD (structure, i);
800 ret = func (field->name, &field->value, user_data);
801 if (!ret)
802 return FALSE;
803 }
805 return TRUE;
806 }
808 /**
809 * gst_structure_map_in_place:
810 * @structure: a #GstStructure
811 * @func: a function to call for each field
812 * @user_data: private data
813 *
814 * Calls the provided function once for each field in the #GstStructure. In
815 * contrast to gst_structure_foreach(), the function may modify the fields. The
816 * structure must be mutable.
817 *
818 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
819 * FALSE otherwise.
820 */
821 gboolean
822 gst_structure_map_in_place (GstStructure * structure,
823 GstStructureMapFunc func, gpointer user_data)
824 {
825 guint i;
826 GstStructureField *field;
827 gboolean ret;
829 g_return_val_if_fail (structure != NULL, FALSE);
830 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
832 for (i = 0; i < structure->fields->len; i++) {
833 field = GST_STRUCTURE_FIELD (structure, i);
835 ret = func (field->name, &field->value, user_data);
836 if (!ret)
837 return FALSE;
838 }
840 return TRUE;
841 }
843 /**
844 * gst_structure_has_field:
845 * @structure: a #GstStructure
846 * @fieldname: the name of a field
847 *
848 * Check if @structure contains a field named @fieldname.
849 *
850 * Returns: TRUE if the structure contains a field with the given name
851 */
852 gboolean
853 gst_structure_has_field (const GstStructure * structure,
854 const gchar * fieldname)
855 {
856 GstStructureField *field;
858 g_return_val_if_fail (structure != NULL, 0);
859 g_return_val_if_fail (fieldname != NULL, 0);
861 field = gst_structure_get_field (structure, fieldname);
863 return (field != NULL);
864 }
866 /**
867 * gst_structure_has_field_typed:
868 * @structure: a #GstStructure
869 * @fieldname: the name of a field
870 * @type: the type of a value
871 *
872 * Check if @structure contains a field named @fieldname and with GType @type.
873 *
874 * Returns: TRUE if the structure contains a field with the given name and type
875 */
876 gboolean
877 gst_structure_has_field_typed (const GstStructure * structure,
878 const gchar * fieldname, GType type)
879 {
880 GstStructureField *field;
882 g_return_val_if_fail (structure != NULL, 0);
883 g_return_val_if_fail (fieldname != NULL, 0);
885 field = gst_structure_get_field (structure, fieldname);
886 if (field == NULL)
887 return FALSE;
889 return (G_VALUE_TYPE (&field->value) == type);
890 }
893 /* utility functions */
895 /**
896 * gst_structure_get_boolean:
897 * @structure: a #GstStructure
898 * @fieldname: the name of a field
899 * @value: a pointer to a #gboolean to set
900 *
901 * Sets the boolean pointed to by @value corresponding to the value of the
902 * given field. Caller is responsible for making sure the field exists
903 * and has the correct type.
904 *
905 * Returns: TRUE if the value could be set correctly. If there was no field
906 * with @fieldname or the existing field did not contain a boolean, this function
907 * returns FALSE.
908 */
909 gboolean
910 gst_structure_get_boolean (const GstStructure * structure,
911 const gchar * fieldname, gboolean * value)
912 {
913 GstStructureField *field;
915 g_return_val_if_fail (structure != NULL, FALSE);
916 g_return_val_if_fail (fieldname != NULL, FALSE);
918 field = gst_structure_get_field (structure, fieldname);
920 if (field == NULL)
921 return FALSE;
922 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
923 return FALSE;
925 *value = g_value_get_boolean (&field->value);
927 return TRUE;
928 }
930 /**
931 * gst_structure_get_int:
932 * @structure: a #GstStructure
933 * @fieldname: the name of a field
934 * @value: a pointer to an int to set
935 *
936 * Sets the int pointed to by @value corresponding to the value of the
937 * given field. Caller is responsible for making sure the field exists
938 * and has the correct type.
939 *
940 * Returns: TRUE if the value could be set correctly. If there was no field
941 * with @fieldname or the existing field did not contain an int, this function
942 * returns FALSE.
943 */
944 gboolean
945 gst_structure_get_int (const GstStructure * structure,
946 const gchar * fieldname, gint * value)
947 {
948 GstStructureField *field;
950 g_return_val_if_fail (structure != NULL, FALSE);
951 g_return_val_if_fail (fieldname != NULL, FALSE);
952 g_return_val_if_fail (value != NULL, FALSE);
954 field = gst_structure_get_field (structure, fieldname);
956 if (field == NULL)
957 return FALSE;
958 if (!G_VALUE_HOLDS_INT (&field->value))
959 return FALSE;
961 *value = g_value_get_int (&field->value);
963 return TRUE;
964 }
966 /**
967 * gst_structure_get_fourcc:
968 * @structure: a #GstStructure
969 * @fieldname: the name of a field
970 * @value: a pointer to a #GstFourcc to set
971 *
972 * Sets the #GstFourcc pointed to by @value corresponding to the value of the
973 * given field. Caller is responsible for making sure the field exists
974 * and has the correct type.
975 *
976 * Returns: TRUE if the value could be set correctly. If there was no field
977 * with @fieldname or the existing field did not contain a fourcc, this function
978 * returns FALSE.
979 */
980 gboolean
981 gst_structure_get_fourcc (const GstStructure * structure,
982 const gchar * fieldname, guint32 * value)
983 {
984 GstStructureField *field;
986 g_return_val_if_fail (structure != NULL, FALSE);
987 g_return_val_if_fail (fieldname != NULL, FALSE);
988 g_return_val_if_fail (value != NULL, FALSE);
990 field = gst_structure_get_field (structure, fieldname);
992 if (field == NULL)
993 return FALSE;
994 if (!GST_VALUE_HOLDS_FOURCC (&field->value))
995 return FALSE;
997 *value = gst_value_get_fourcc (&field->value);
999 return TRUE;
1000 }
1002 /**
1003 * gst_structure_get_date:
1004 * @structure: a #GstStructure
1005 * @fieldname: the name of a field
1006 * @value: a pointer to a #GDate to set
1007 *
1008 * Sets the date pointed to by @value corresponding to the date of the
1009 * given field. Caller is responsible for making sure the field exists
1010 * and has the correct type.
1011 *
1012 * Returns: TRUE if the value could be set correctly. If there was no field
1013 * with @fieldname or the existing field did not contain a data, this function
1014 * returns FALSE.
1015 */
1016 gboolean
1017 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1018 GDate ** value)
1019 {
1020 GstStructureField *field;
1022 g_return_val_if_fail (structure != NULL, FALSE);
1023 g_return_val_if_fail (fieldname != NULL, FALSE);
1024 g_return_val_if_fail (value != NULL, FALSE);
1026 field = gst_structure_get_field (structure, fieldname);
1028 if (field == NULL)
1029 return FALSE;
1030 if (!GST_VALUE_HOLDS_DATE (&field->value))
1031 return FALSE;
1033 *value = g_value_dup_boxed (&field->value);
1035 return TRUE;
1036 }
1038 /**
1039 * gst_structure_get_clock_time:
1040 * @structure: a #GstStructure
1041 * @fieldname: the name of a field
1042 * @value: a pointer to a #GstClockTime to set
1043 *
1044 * Sets the clock time pointed to by @value corresponding to the clock time
1045 * of the given field. Caller is responsible for making sure the field exists
1046 * and has the correct type.
1047 *
1048 * Returns: TRUE if the value could be set correctly. If there was no field
1049 * with @fieldname or the existing field did not contain a #GstClockTime, this
1050 * function returns FALSE.
1051 */
1052 gboolean
1053 gst_structure_get_clock_time (const GstStructure * structure,
1054 const gchar * fieldname, GstClockTime * value)
1055 {
1056 GstStructureField *field;
1058 g_return_val_if_fail (structure != NULL, FALSE);
1059 g_return_val_if_fail (fieldname != NULL, FALSE);
1060 g_return_val_if_fail (value != NULL, FALSE);
1062 field = gst_structure_get_field (structure, fieldname);
1064 if (field == NULL)
1065 return FALSE;
1066 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1067 return FALSE;
1069 *value = g_value_get_uint64 (&field->value);
1071 return TRUE;
1072 }
1074 /**
1075 * gst_structure_get_double:
1076 * @structure: a #GstStructure
1077 * @fieldname: the name of a field
1078 * @value: a pointer to a #GstFourcc to set
1079 *
1080 * Sets the double pointed to by @value corresponding to the value of the
1081 * given field. Caller is responsible for making sure the field exists
1082 * and has the correct type.
1083 *
1084 * Returns: TRUE if the value could be set correctly. If there was no field
1085 * with @fieldname or the existing field did not contain a double, this
1086 * function returns FALSE.
1087 */
1088 gboolean
1089 gst_structure_get_double (const GstStructure * structure,
1090 const gchar * fieldname, gdouble * value)
1091 {
1092 GstStructureField *field;
1094 g_return_val_if_fail (structure != NULL, FALSE);
1095 g_return_val_if_fail (fieldname != NULL, FALSE);
1096 g_return_val_if_fail (value != NULL, FALSE);
1098 field = gst_structure_get_field (structure, fieldname);
1100 if (field == NULL)
1101 return FALSE;
1102 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1103 return FALSE;
1105 *value = g_value_get_double (&field->value);
1107 return TRUE;
1108 }
1110 /**
1111 * gst_structure_get_string:
1112 * @structure: a #GstStructure
1113 * @fieldname: the name of a field
1114 *
1115 * Finds the field corresponding to @fieldname, and returns the string
1116 * contained in the field's value. Caller is responsible for making
1117 * sure the field exists and has the correct type.
1118 *
1119 * The string should not be modified, and remains valid until the next
1120 * call to a gst_structure_*() function with the given structure.
1121 *
1122 * Returns: a pointer to the string or NULL when the field did not exist
1123 * or did not contain a string.
1124 */
1125 const gchar *
1126 gst_structure_get_string (const GstStructure * structure,
1127 const gchar * fieldname)
1128 {
1129 GstStructureField *field;
1131 g_return_val_if_fail (structure != NULL, NULL);
1132 g_return_val_if_fail (fieldname != NULL, NULL);
1134 field = gst_structure_get_field (structure, fieldname);
1136 if (field == NULL)
1137 return NULL;
1138 if (!G_VALUE_HOLDS_STRING (&field->value))
1139 return NULL;
1141 return g_value_get_string (&field->value);
1142 }
1144 /**
1145 * gst_structure_get_enum:
1146 * @structure: a #GstStructure
1147 * @fieldname: the name of a field
1148 * @enumtype: the enum type of a field
1149 * @value: a pointer to an int to set
1150 *
1151 * Sets the int pointed to by @value corresponding to the value of the
1152 * given field. Caller is responsible for making sure the field exists,
1153 * has the correct type and that the enumtype is correct.
1154 *
1155 * Returns: TRUE if the value could be set correctly. If there was no field
1156 * with @fieldname or the existing field did not contain an enum of the given
1157 * type, this function returns FALSE.
1158 */
1159 gboolean
1160 gst_structure_get_enum (const GstStructure * structure,
1161 const gchar * fieldname, GType enumtype, gint * value)
1162 {
1163 GstStructureField *field;
1165 g_return_val_if_fail (structure != NULL, FALSE);
1166 g_return_val_if_fail (fieldname != NULL, FALSE);
1167 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1168 g_return_val_if_fail (value != NULL, FALSE);
1170 field = gst_structure_get_field (structure, fieldname);
1172 if (field == NULL)
1173 return FALSE;
1174 if (!G_VALUE_HOLDS_ENUM (&field->value))
1175 return FALSE;
1176 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1177 return FALSE;
1179 *value = g_value_get_enum (&field->value);
1181 return TRUE;
1182 }
1184 /**
1185 * gst_structure_get_fraction:
1186 * @structure: a #GstStructure
1187 * @fieldname: the name of a field
1188 * @value_numerator: a pointer to an int to set
1189 * @value_denominator: a pointer to an int to set
1190 *
1191 * Sets the integers pointed to by @value_numerator and @value_denominator
1192 * corresponding to the value of the given field. Caller is responsible
1193 * for making sure the field exists and has the correct type.
1194 *
1195 * Returns: TRUE if the values could be set correctly. If there was no field
1196 * with @fieldname or the existing field did not contain a GstFraction, this
1197 * function returns FALSE.
1198 */
1199 gboolean
1200 gst_structure_get_fraction (const GstStructure * structure,
1201 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1202 {
1203 GstStructureField *field;
1205 g_return_val_if_fail (structure != NULL, FALSE);
1206 g_return_val_if_fail (fieldname != NULL, FALSE);
1207 g_return_val_if_fail (value_numerator != NULL, FALSE);
1208 g_return_val_if_fail (value_denominator != NULL, FALSE);
1210 field = gst_structure_get_field (structure, fieldname);
1212 if (field == NULL)
1213 return FALSE;
1214 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1215 return FALSE;
1217 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1218 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1220 return TRUE;
1221 }
1223 typedef struct _GstStructureAbbreviation
1224 {
1225 char *type_name;
1226 GType type;
1227 }
1228 GstStructureAbbreviation;
1230 static GstStructureAbbreviation *
1231 gst_structure_get_abbrs (gint * n_abbrs)
1232 {
1233 static GstStructureAbbreviation *abbrs = NULL;
1234 static gint num = 0;
1236 if (abbrs == NULL) {
1237 /* dynamically generate the array */
1238 GstStructureAbbreviation dyn_abbrs[] = {
1239 {"int", G_TYPE_INT}
1240 ,
1241 {"i", G_TYPE_INT}
1242 ,
1243 {"float", G_TYPE_FLOAT}
1244 ,
1245 {"f", G_TYPE_FLOAT}
1246 ,
1247 {"double", G_TYPE_DOUBLE}
1248 ,
1249 {"d", G_TYPE_DOUBLE}
1250 ,
1251 {"buffer", GST_TYPE_BUFFER}
1252 ,
1253 {"fourcc", GST_TYPE_FOURCC}
1254 ,
1255 {"4", GST_TYPE_FOURCC}
1256 ,
1257 {"fraction", GST_TYPE_FRACTION}
1258 ,
1259 {"boolean", G_TYPE_BOOLEAN}
1260 ,
1261 {"bool", G_TYPE_BOOLEAN}
1262 ,
1263 {"b", G_TYPE_BOOLEAN}
1264 ,
1265 {"string", G_TYPE_STRING}
1266 ,
1267 {"str", G_TYPE_STRING}
1268 ,
1269 {"s", G_TYPE_STRING}
1270 };
1271 num = G_N_ELEMENTS (dyn_abbrs);
1272 /* permanently allocate and copy the array now */
1273 abbrs = g_new0 (GstStructureAbbreviation, num);
1274 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * num);
1275 }
1276 *n_abbrs = num;
1278 return abbrs;
1279 }
1281 static GType
1282 gst_structure_from_abbr (const char *type_name)
1283 {
1284 int i;
1285 GstStructureAbbreviation *abbrs;
1286 gint n_abbrs;
1288 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1290 abbrs = gst_structure_get_abbrs (&n_abbrs);
1292 for (i = 0; i < n_abbrs; i++) {
1293 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1294 return abbrs[i].type;
1295 }
1296 }
1298 /* this is the fallback */
1299 return g_type_from_name (type_name);
1300 }
1302 static const char *
1303 gst_structure_to_abbr (GType type)
1304 {
1305 int i;
1306 GstStructureAbbreviation *abbrs;
1307 gint n_abbrs;
1309 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1311 abbrs = gst_structure_get_abbrs (&n_abbrs);
1313 for (i = 0; i < n_abbrs; i++) {
1314 if (type == abbrs[i].type) {
1315 return abbrs[i].type_name;
1316 }
1317 }
1319 return g_type_name (type);
1320 }
1322 static GType
1323 gst_structure_value_get_generic_type (GValue * val)
1324 {
1325 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1326 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1327 GArray *array = g_value_peek_pointer (val);
1329 if (array->len > 0) {
1330 GValue *value = &g_array_index (array, GValue, 0);
1332 return gst_structure_value_get_generic_type (value);
1333 } else {
1334 return G_TYPE_INT;
1335 }
1336 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1337 return G_TYPE_INT;
1338 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1339 return G_TYPE_DOUBLE;
1340 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1341 return GST_TYPE_FRACTION;
1342 }
1343 return G_VALUE_TYPE (val);
1344 }
1346 #define GST_ASCII_IS_STRING(c) (g_ascii_isalnum((c)) || ((c) == '_') || \
1347 ((c) == '-') || ((c) == '+') || ((c) == '/') || ((c) == ':') || \
1348 ((c) == '.'))
1350 /**
1351 * gst_structure_to_string:
1352 * @structure: a #GstStructure
1353 *
1354 * Converts @structure to a human-readable string representation.
1355 *
1356 * Returns: a pointer to string allocated by g_malloc(). g_free() after
1357 * usage.
1358 */
1359 gchar *
1360 gst_structure_to_string (const GstStructure * structure)
1361 {
1362 GstStructureField *field;
1363 GString *s;
1364 guint i;
1366 /* NOTE: This function is potentially called by the debug system,
1367 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1368 * should be careful to avoid recursion. This includes any functions
1369 * called by gst_structure_to_string. In particular, calls should
1370 * not use the GST_PTR_FORMAT extension. */
1372 g_return_val_if_fail (structure != NULL, NULL);
1374 s = g_string_new ("");
1375 /* FIXME this string may need to be escaped */
1376 g_string_append_printf (s, "%s", g_quark_to_string (structure->name));
1377 for (i = 0; i < structure->fields->len; i++) {
1378 char *t;
1379 GType type;
1381 field = GST_STRUCTURE_FIELD (structure, i);
1383 t = gst_value_serialize (&field->value);
1384 type = gst_structure_value_get_generic_type (&field->value);
1386 g_string_append_printf (s, ", %s=(%s)%s", g_quark_to_string (field->name),
1387 gst_structure_to_abbr (type), GST_STR_NULL (t));
1388 g_free (t);
1389 }
1390 return g_string_free (s, FALSE);
1391 }
1393 /*
1394 * r will still point to the string. if end == next, the string will not be
1395 * null-terminated. In all other cases it will be.
1396 * end = pointer to char behind end of string, next = pointer to start of
1397 * unread data.
1398 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1399 */
1400 static gboolean
1401 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next)
1402 {
1403 gchar *w;
1405 if (*s == 0)
1406 return FALSE;
1408 if (*s != '"') {
1409 int ret;
1411 ret = gst_structure_parse_simple_string (s, end);
1412 *next = *end;
1414 return ret;
1415 }
1417 w = s;
1418 s++;
1419 while (*s != '"') {
1420 if (*s == 0)
1421 return FALSE;
1423 if (*s == '\\') {
1424 s++;
1425 }
1427 *w = *s;
1428 w++;
1429 s++;
1430 }
1431 s++;
1433 *end = w;
1434 *next = s;
1436 return TRUE;
1437 }
1439 static gboolean
1440 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1441 GType type)
1442 {
1443 GValue value1 = { 0 };
1444 GValue value2 = { 0 };
1445 GType range_type;
1446 gboolean ret;
1449 if (*s != '[')
1450 return FALSE;
1451 s++;
1453 ret = gst_structure_parse_value (s, &s, &value1, type);
1454 if (ret == FALSE)
1455 return FALSE;
1457 while (g_ascii_isspace (*s))
1458 s++;
1460 if (*s != ',')
1461 return FALSE;
1462 s++;
1464 while (g_ascii_isspace (*s))
1465 s++;
1467 ret = gst_structure_parse_value (s, &s, &value2, type);
1468 if (ret == FALSE)
1469 return FALSE;
1471 while (g_ascii_isspace (*s))
1472 s++;
1474 if (*s != ']')
1475 return FALSE;
1476 s++;
1478 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1479 return FALSE;
1481 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1482 range_type = GST_TYPE_DOUBLE_RANGE;
1483 g_value_init (value, range_type);
1484 gst_value_set_double_range (value, g_value_get_double (&value1),
1485 g_value_get_double (&value2));
1486 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1487 range_type = GST_TYPE_INT_RANGE;
1488 g_value_init (value, range_type);
1489 gst_value_set_int_range (value, g_value_get_int (&value1),
1490 g_value_get_int (&value2));
1491 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1492 range_type = GST_TYPE_FRACTION_RANGE;
1493 g_value_init (value, range_type);
1494 gst_value_set_fraction_range (value, &value1, &value2);
1495 } else {
1496 return FALSE;
1497 }
1499 *after = s;
1500 return TRUE;
1501 }
1503 static gboolean
1504 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1505 GType type, GType list_type, char begin, char end)
1506 {
1507 GValue list_value = { 0 };
1508 gboolean ret;
1509 GArray *array;
1511 g_value_init (value, list_type);
1512 array = g_value_peek_pointer (value);
1514 if (*s != begin)
1515 return FALSE;
1516 s++;
1518 while (g_ascii_isspace (*s))
1519 s++;
1520 if (*s == end) {
1521 s++;
1522 *after = s;
1523 return TRUE;
1524 }
1526 ret = gst_structure_parse_value (s, &s, &list_value, type);
1527 if (ret == FALSE)
1528 return FALSE;
1530 g_array_append_val (array, list_value);
1532 while (g_ascii_isspace (*s))
1533 s++;
1535 while (*s != end) {
1536 if (*s != ',')
1537 return FALSE;
1538 s++;
1540 while (g_ascii_isspace (*s))
1541 s++;
1543 memset (&list_value, 0, sizeof (list_value));
1544 ret = gst_structure_parse_value (s, &s, &list_value, type);
1545 if (ret == FALSE)
1546 return FALSE;
1548 g_array_append_val (array, list_value);
1549 while (g_ascii_isspace (*s))
1550 s++;
1551 }
1553 s++;
1555 *after = s;
1556 return TRUE;
1557 }
1559 static gboolean
1560 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
1561 {
1562 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
1563 '{', '}');
1564 }
1566 static gboolean
1567 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
1568 GType type)
1569 {
1570 return gst_structure_parse_any_list (s, after, value, type,
1571 GST_TYPE_ARRAY, '<', '>');
1572 }
1574 static gboolean
1575 gst_structure_parse_simple_string (gchar * str, gchar ** end)
1576 {
1577 char *s = str;
1579 while (GST_ASCII_IS_STRING (*s)) {
1580 s++;
1581 }
1583 *end = s;
1585 return (s != str);
1586 }
1588 static gboolean
1589 gst_structure_parse_field (gchar * str,
1590 gchar ** after, GstStructureField * field)
1591 {
1592 gchar *name;
1593 gchar *name_end;
1594 gchar *s;
1595 gchar c;
1597 s = str;
1599 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1600 s++;
1601 name = s;
1602 if (!gst_structure_parse_simple_string (s, &name_end))
1603 return FALSE;
1605 s = name_end;
1606 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
1607 s++;
1609 if (*s != '=')
1610 return FALSE;
1611 s++;
1613 c = *name_end;
1614 *name_end = 0;
1615 field->name = g_quark_from_string (name);
1616 *name_end = c;
1618 if (!gst_structure_parse_value (s, &s, &field->value, G_TYPE_INVALID))
1619 return FALSE;
1621 *after = s;
1622 return TRUE;
1623 }
1625 static gboolean
1626 gst_structure_parse_value (gchar * str,
1627 gchar ** after, GValue * value, GType default_type)
1628 {
1629 gchar *type_name;
1630 gchar *type_end;
1631 gchar *value_s;
1632 gchar *value_end;
1633 gchar *s;
1634 gchar c;
1635 int ret = 0;
1636 GType type = default_type;
1639 s = str;
1640 while (g_ascii_isspace (*s))
1641 s++;
1643 type_name = NULL;
1644 if (*s == '(') {
1645 type = G_TYPE_INVALID;
1647 s++;
1648 while (g_ascii_isspace (*s))
1649 s++;
1650 type_name = s;
1651 if (!gst_structure_parse_simple_string (s, &type_end))
1652 return FALSE;
1653 s = type_end;
1654 while (g_ascii_isspace (*s))
1655 s++;
1656 if (*s != ')')
1657 return FALSE;
1658 s++;
1659 while (g_ascii_isspace (*s))
1660 s++;
1662 c = *type_end;
1663 *type_end = 0;
1664 type = gst_structure_from_abbr (type_name);
1665 *type_end = c;
1667 if (type == G_TYPE_INVALID)
1668 return FALSE;
1669 }
1671 while (g_ascii_isspace (*s))
1672 s++;
1673 if (*s == '[') {
1674 ret = gst_structure_parse_range (s, &s, value, type);
1675 } else if (*s == '{') {
1676 ret = gst_structure_parse_list (s, &s, value, type);
1677 } else if (*s == '<') {
1678 ret = gst_structure_parse_array (s, &s, value, type);
1679 } else {
1680 value_s = s;
1681 if (!gst_structure_parse_string (s, &value_end, &s))
1682 return FALSE;
1684 c = *value_end;
1685 *value_end = 0;
1686 if (type == G_TYPE_INVALID) {
1687 GType try_types[] =
1688 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_STRING };
1689 int i;
1691 for (i = 0; i < 4; i++) {
1692 g_value_init (value, try_types[i]);
1693 ret = gst_value_deserialize (value, value_s);
1694 if (ret)
1695 break;
1696 g_value_unset (value);
1697 }
1698 } else {
1699 g_value_init (value, type);
1701 ret = gst_value_deserialize (value, value_s);
1702 }
1703 *value_end = c;
1704 }
1706 *after = s;
1708 return ret;
1709 }
1711 /**
1712 * gst_structure_from_string:
1713 * @string: a string representation of a #GstStructure.
1714 * @end: pointer to store the end of the string in.
1715 *
1716 * Creates a #GstStructure from a string representation.
1717 * If end is not NULL, a pointer to the place inside the given string
1718 * where parsing ended will be returned.
1719 *
1720 * Returns: a new #GstStructure or NULL when the string could not
1721 * be parsed. Free after usage.
1722 */
1723 GstStructure *
1724 gst_structure_from_string (const gchar * string, gchar ** end)
1725 {
1726 char *name;
1727 char *copy;
1728 char *w;
1729 char *r;
1730 char save;
1731 GstStructure *structure = NULL;
1732 GstStructureField field = { 0 };
1734 g_return_val_if_fail (string != NULL, NULL);
1736 copy = g_strdup (string);
1737 r = copy;
1739 name = r;
1740 if (!gst_structure_parse_string (r, &w, &r))
1741 goto error;
1743 while (g_ascii_isspace (*r) || (r[0] == '\\' && g_ascii_isspace (r[1])))
1744 r++;
1745 if (*r != 0 && *r != ';' && *r != ',')
1746 goto error;
1748 save = *w;
1749 *w = 0;
1750 structure = gst_structure_empty_new (name);
1751 *w = save;
1753 while (*r && (*r != ';')) {
1754 if (*r != ',')
1755 goto error;
1756 r++;
1757 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1758 && g_ascii_isspace (r[1]))))
1759 r++;
1761 memset (&field, 0, sizeof (field));
1762 if (!gst_structure_parse_field (r, &r, &field))
1763 goto error;
1764 gst_structure_set_field (structure, &field);
1765 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
1766 && g_ascii_isspace (r[1]))))
1767 r++;
1768 }
1770 if (end)
1771 *end = (char *) string + (r - copy);
1773 g_free (copy);
1774 return structure;
1776 error:
1777 if (structure)
1778 gst_structure_free (structure);
1779 g_free (copy);
1780 return NULL;
1781 }
1783 static void
1784 gst_structure_transform_to_string (const GValue * src_value,
1785 GValue * dest_value)
1786 {
1787 g_return_if_fail (src_value != NULL);
1788 g_return_if_fail (dest_value != NULL);
1790 dest_value->data[0].v_pointer =
1791 gst_structure_to_string (src_value->data[0].v_pointer);
1792 }
1794 static GstStructure *
1795 gst_structure_copy_conditional (const GstStructure * structure)
1796 {
1797 if (structure)
1798 return gst_structure_copy (structure);
1799 return NULL;
1800 }
1802 /* fixate utility functions */
1804 /**
1805 * gst_structure_fixate_field_nearest_int:
1806 * @structure: a #GstStructure
1807 * @field_name: a field in @structure
1808 * @target: the target value of the fixation
1809 *
1810 * Fixates a #GstStructure by changing the given field to the nearest
1811 * integer to @target that is a subset of the existing field.
1812 *
1813 * Returns: TRUE if the structure could be fixated
1814 */
1815 gboolean
1816 gst_structure_fixate_field_nearest_int (GstStructure * structure,
1817 const char *field_name, int target)
1818 {
1819 const GValue *value;
1821 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1822 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1824 value = gst_structure_get_value (structure, field_name);
1826 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
1827 /* already fixed */
1828 return FALSE;
1829 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
1830 int x;
1832 x = gst_value_get_int_range_min (value);
1833 if (target < x)
1834 target = x;
1835 x = gst_value_get_int_range_max (value);
1836 if (target > x)
1837 target = x;
1838 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
1839 return TRUE;
1840 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1841 const GValue *list_value;
1842 int i, n;
1843 int best = 0;
1844 int best_index = -1;
1846 n = gst_value_list_get_size (value);
1847 for (i = 0; i < n; i++) {
1848 list_value = gst_value_list_get_value (value, i);
1849 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
1850 int x = g_value_get_int (list_value);
1852 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
1853 best_index = i;
1854 best = x;
1855 }
1856 }
1857 }
1858 if (best_index != -1) {
1859 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
1860 return TRUE;
1861 }
1862 return FALSE;
1863 }
1865 return FALSE;
1866 }
1868 /**
1869 * gst_structure_fixate_field_nearest_double:
1870 * @structure: a #GstStructure
1871 * @field_name: a field in @structure
1872 * @target: the target value of the fixation
1873 *
1874 * Fixates a #GstStructure by changing the given field to the nearest
1875 * double to @target that is a subset of the existing field.
1876 *
1877 * Returns: TRUE if the structure could be fixated
1878 */
1879 gboolean
1880 gst_structure_fixate_field_nearest_double (GstStructure * structure,
1881 const char *field_name, double target)
1882 {
1883 const GValue *value;
1885 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1886 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1888 value = gst_structure_get_value (structure, field_name);
1890 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
1891 /* already fixed */
1892 return FALSE;
1893 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
1894 double x;
1896 x = gst_value_get_double_range_min (value);
1897 if (target < x)
1898 target = x;
1899 x = gst_value_get_double_range_max (value);
1900 if (target > x)
1901 target = x;
1902 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
1903 return TRUE;
1904 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1905 const GValue *list_value;
1906 int i, n;
1907 double best = 0;
1908 int best_index = -1;
1910 n = gst_value_list_get_size (value);
1911 for (i = 0; i < n; i++) {
1912 list_value = gst_value_list_get_value (value, i);
1913 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
1914 double x = g_value_get_double (list_value);
1916 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
1917 best_index = i;
1918 best = x;
1919 }
1920 }
1921 }
1922 if (best_index != -1) {
1923 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
1924 return TRUE;
1925 }
1926 return FALSE;
1927 }
1929 return FALSE;
1931 }
1933 /**
1934 * gst_structure_fixate_field_boolean:
1935 * @structure: a #GstStructure
1936 * @field_name: a field in @structure
1937 * @target: the target value of the fixation
1938 *
1939 * Fixates a #GstStructure by changing the given @field_name field to the given
1940 * @target boolean if that field is not fixed yet.
1941 *
1942 * Returns: TRUE if the structure could be fixated
1943 */
1944 gboolean
1945 gst_structure_fixate_field_boolean (GstStructure * structure,
1946 const char *field_name, gboolean target)
1947 {
1948 const GValue *value;
1950 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
1951 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1953 value = gst_structure_get_value (structure, field_name);
1955 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
1956 /* already fixed */
1957 return FALSE;
1958 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
1959 const GValue *list_value;
1960 int i, n;
1961 int best = 0;
1962 int best_index = -1;
1964 n = gst_value_list_get_size (value);
1965 for (i = 0; i < n; i++) {
1966 list_value = gst_value_list_get_value (value, i);
1967 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
1968 gboolean x = g_value_get_boolean (list_value);
1970 if (best_index == -1 || x == target) {
1971 best_index = i;
1972 best = x;
1973 }
1974 }
1975 }
1976 if (best_index != -1) {
1977 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
1978 return TRUE;
1979 }
1980 return FALSE;
1981 }
1983 return FALSE;
1984 }
1986 /**
1987 * gst_structure_fixate_field_nearest_fraction:
1988 * @structure: a #GstStructure
1989 * @field_name: a field in @structure
1990 * @target_numerator: The numerator of the target value of the fixation
1991 * @target_denominator: The denominator of the target value of the fixation
1992 *
1993 * Fixates a #GstStructure by changing the given field to the nearest
1994 * fraction to @target_numerator/@target_denominator that is a subset
1995 * of the existing field.
1996 *
1997 * Returns: TRUE if the structure could be fixated
1998 */
1999 gboolean
2000 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2001 const char *field_name, const gint target_numerator,
2002 const gint target_denominator)
2003 {
2004 const GValue *value;
2006 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2007 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2009 value = gst_structure_get_value (structure, field_name);
2011 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2012 /* already fixed */
2013 return FALSE;
2014 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2015 const GValue *x, *new_value;
2016 GValue target = { 0 };
2017 g_value_init (&target, GST_TYPE_FRACTION);
2018 gst_value_set_fraction (&target, target_numerator, target_denominator);
2020 new_value = ⌖
2021 x = gst_value_get_fraction_range_min (value);
2022 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2023 new_value = x;
2024 x = gst_value_get_fraction_range_max (value);
2025 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2026 new_value = x;
2028 gst_structure_set_value (structure, field_name, new_value);
2029 g_value_unset (&target);
2030 return TRUE;
2031 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2032 const GValue *list_value;
2033 int i, n;
2034 const GValue *best = NULL;
2035 GValue best_diff = { 0 };
2036 GValue cur_diff = { 0 };
2037 GValue target = { 0 };
2038 gboolean res = FALSE;
2040 g_value_init (&best_diff, GST_TYPE_FRACTION);
2041 g_value_init (&cur_diff, GST_TYPE_FRACTION);
2042 g_value_init (&target, GST_TYPE_FRACTION);
2044 gst_value_set_fraction (&target, target_numerator, target_denominator);
2046 n = gst_value_list_get_size (value);
2047 for (i = 0; i < n; i++) {
2048 list_value = gst_value_list_get_value (value, i);
2049 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2050 if (best == NULL) {
2051 best = list_value;
2052 gst_value_set_fraction (&best_diff, 0, 1);
2053 } else {
2054 if (gst_value_compare (list_value, &target) == GST_VALUE_LESS_THAN)
2055 gst_value_fraction_subtract (&cur_diff, &target, list_value);
2056 else
2057 gst_value_fraction_subtract (&cur_diff, list_value, &target);
2059 if (gst_value_compare (&cur_diff, &best_diff) == GST_VALUE_LESS_THAN) {
2060 best = list_value;
2061 g_value_copy (&cur_diff, &best_diff);
2062 }
2063 }
2064 }
2065 }
2066 if (best != NULL) {
2067 gst_structure_set_value (structure, field_name, best);
2068 res = TRUE;
2069 }
2070 g_value_unset (&best_diff);
2071 g_value_unset (&cur_diff);
2072 g_value_unset (&target);
2073 return res;
2074 }
2076 return FALSE;
2077 }