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
28 * as GQuarks and the values can be of any GType.
29 *
30 * In addition to the key/value pairs, a #GstStructure also has a name. The name
31 * starts with a letter and can be folled by letters, numbers and any of "/-_.:".
32 *
33 * #GstStructure is used by various GStreamer subsystems to store information
34 * in a flexible and extensible way. A #GstStructure does not have a refcount
35 * because it usually is part of a higher level object such as #GstCaps. It
36 * provides a means to enforce mutability using the refcount of the parent
37 * with the gst_structure_set_parent_refcount() method.
38 *
39 * A #GstStructure can be created with gst_structure_empty_new() or
40 * gst_structure_new(), which both take a name and an optional set of
41 * key/value pairs along with the types of the values.
42 *
43 * Field values can be changed with gst_structure_set_value() or
44 * gst_structure_set().
45 *
46 * Field values can be retrieved with gst_structure_get_value() or the more
47 * convenient gst_structure_get_*() functions.
48 *
49 * Fields can be removed with gst_structure_remove_field() or
50 * gst_structure_remove_fields().
51 *
52 * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are
53 * not allowed. Strings must not be empty either, but may be NULL.
54 *
55 * Last reviewed on 2009-06-08 (0.10.23)
56 */
58 #ifdef HAVE_CONFIG_H
59 #include "config.h"
60 #endif
62 #include <string.h>
64 #include "gst_private.h"
65 #include "gstquark.h"
66 #include <gst/gst.h>
67 #include <gobject/gvaluecollector.h>
69 typedef struct _GstStructureField GstStructureField;
71 struct _GstStructureField
72 {
73 GQuark name;
74 GValue value;
75 };
77 #define GST_STRUCTURE_FIELD(structure, index) \
78 &g_array_index((structure)->fields, GstStructureField, (index))
80 #define IS_MUTABLE(structure) \
81 (!(structure)->parent_refcount || \
82 g_atomic_int_get ((structure)->parent_refcount) == 1)
84 #define IS_TAGLIST(structure) \
85 (structure->name == GST_QUARK (TAGLIST))
87 static void gst_structure_set_field (GstStructure * structure,
88 GstStructureField * field);
89 static GstStructureField *gst_structure_get_field (const GstStructure *
90 structure, const gchar * fieldname);
91 static GstStructureField *gst_structure_id_get_field (const GstStructure *
92 structure, GQuark field);
93 static void gst_structure_transform_to_string (const GValue * src_value,
94 GValue * dest_value);
95 static GstStructure *gst_structure_copy_conditional (const GstStructure *
96 structure);
97 static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
98 GValue * value, GType default_type);
99 static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
101 GType
102 gst_structure_get_type (void)
103 {
104 static GType gst_structure_type = 0;
106 if (G_UNLIKELY (gst_structure_type == 0)) {
107 gst_structure_type = g_boxed_type_register_static ("GstStructure",
108 (GBoxedCopyFunc) gst_structure_copy_conditional,
109 (GBoxedFreeFunc) gst_structure_free);
111 g_value_register_transform_func (gst_structure_type, G_TYPE_STRING,
112 gst_structure_transform_to_string);
113 }
115 return gst_structure_type;
116 }
118 static GstStructure *
119 gst_structure_id_empty_new_with_size (GQuark quark, guint prealloc)
120 {
121 GstStructure *structure;
123 structure = g_slice_new (GstStructure);
124 structure->type = gst_structure_get_type ();
125 structure->name = quark;
126 structure->parent_refcount = NULL;
127 structure->fields =
128 g_array_sized_new (FALSE, FALSE, sizeof (GstStructureField), prealloc);
130 return structure;
131 }
133 /**
134 * gst_structure_id_empty_new:
135 * @quark: name of new structure
136 *
137 * Creates a new, empty #GstStructure with the given name as a GQuark.
138 *
139 * Free-function: gst_structure_free
140 *
141 * Returns: (transfer full): a new, empty #GstStructure
142 */
143 GstStructure *
144 gst_structure_id_empty_new (GQuark quark)
145 {
146 g_return_val_if_fail (quark != 0, NULL);
148 return gst_structure_id_empty_new_with_size (quark, 0);
149 }
151 #ifndef G_DISABLE_CHECKS
152 static gboolean
153 gst_structure_validate_name (const gchar * name)
154 {
155 const gchar *s;
157 g_return_val_if_fail (name != NULL, FALSE);
159 /* FIXME 0.11: use g_ascii_isalpha() */
160 if (G_UNLIKELY (!g_ascii_isalnum (*name))) {
161 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
162 *name, name);
163 return FALSE;
164 }
166 /* FIXME 0.11: don't allow spaces */
167 /* FIXME: test name string more */
168 s = &name[1];
169 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+ ", *s) != NULL))
170 s++;
171 if (G_UNLIKELY (*s != '\0')) {
172 GST_WARNING ("Invalid character '%c' at offset %lu in structure name: %s",
173 *s, ((gulong) s - (gulong) name), name);
174 return FALSE;
175 }
177 return TRUE;
178 }
179 #endif
181 /**
182 * gst_structure_empty_new:
183 * @name: name of new structure
184 *
185 * Creates a new, empty #GstStructure with the given @name.
186 *
187 * See gst_structure_set_name() for constraints on the @name parameter.
188 *
189 * Free-function: gst_structure_free
190 *
191 * Returns: (transfer full): a new, empty #GstStructure
192 */
193 GstStructure *
194 gst_structure_empty_new (const gchar * name)
195 {
196 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
198 return gst_structure_id_empty_new_with_size (g_quark_from_string (name), 0);
199 }
201 /**
202 * gst_structure_new:
203 * @name: name of new structure
204 * @firstfield: name of first field to set
205 * @...: additional arguments
206 *
207 * Creates a new #GstStructure with the given name. Parses the
208 * list of variable arguments and sets fields to the values listed.
209 * Variable arguments should be passed as field name, field type,
210 * and value. Last variable argument should be NULL.
211 *
212 * Free-function: gst_structure_free
213 *
214 * Returns: (transfer full): a new #GstStructure
215 */
216 GstStructure *
217 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
218 {
219 GstStructure *structure;
220 va_list varargs;
222 va_start (varargs, firstfield);
223 structure = gst_structure_new_valist (name, firstfield, varargs);
224 va_end (varargs);
226 return structure;
227 }
229 /**
230 * gst_structure_new_valist:
231 * @name: name of new structure
232 * @firstfield: name of first field to set
233 * @varargs: variable argument list
234 *
235 * Creates a new #GstStructure with the given @name. Structure fields
236 * are set according to the varargs in a manner similar to
237 * gst_structure_new().
238 *
239 * See gst_structure_set_name() for constraints on the @name parameter.
240 *
241 * Free-function: gst_structure_free
242 *
243 * Returns: (transfer full): a new #GstStructure
244 */
245 GstStructure *
246 gst_structure_new_valist (const gchar * name,
247 const gchar * firstfield, va_list varargs)
248 {
249 GstStructure *structure;
251 structure = gst_structure_empty_new (name);
253 if (structure)
254 gst_structure_set_valist (structure, firstfield, varargs);
256 return structure;
257 }
259 /**
260 * gst_structure_set_parent_refcount:
261 * @structure: a #GstStructure
262 * @refcount: (in): a pointer to the parent's refcount
263 *
264 * Sets the parent_refcount field of #GstStructure. This field is used to
265 * determine whether a structure is mutable or not. This function should only be
266 * called by code implementing parent objects of #GstStructure, as described in
267 * the MT Refcounting section of the design documents.
268 */
269 void
270 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
271 {
272 g_return_if_fail (structure != NULL);
274 /* if we have a parent_refcount already, we can only clear
275 * if with a NULL refcount */
276 if (structure->parent_refcount)
277 g_return_if_fail (refcount == NULL);
278 else
279 g_return_if_fail (refcount != NULL);
281 structure->parent_refcount = refcount;
282 }
284 /**
285 * gst_structure_copy:
286 * @structure: a #GstStructure to duplicate
287 *
288 * Duplicates a #GstStructure and all its fields and values.
289 *
290 * Free-function: gst_structure_free
291 *
292 * Returns: (transfer none): a new #GstStructure.
293 */
294 GstStructure *
295 gst_structure_copy (const GstStructure * structure)
296 {
297 GstStructure *new_structure;
298 GstStructureField *field;
299 guint i, len;
301 g_return_val_if_fail (structure != NULL, NULL);
303 len = structure->fields->len;
304 new_structure = gst_structure_id_empty_new_with_size (structure->name, len);
306 for (i = 0; i < len; i++) {
307 GstStructureField new_field = { 0 };
309 field = GST_STRUCTURE_FIELD (structure, i);
311 new_field.name = field->name;
312 gst_value_init_and_copy (&new_field.value, &field->value);
313 g_array_append_val (new_structure->fields, new_field);
314 }
316 return new_structure;
317 }
319 /**
320 * gst_structure_free:
321 * @structure: (in) (transfer full): the #GstStructure to free
322 *
323 * Frees a #GstStructure and all its fields and values. The structure must not
324 * have a parent when this function is called.
325 */
326 void
327 gst_structure_free (GstStructure * structure)
328 {
329 GstStructureField *field;
330 guint i, len;
332 g_return_if_fail (structure != NULL);
333 g_return_if_fail (structure->parent_refcount == NULL);
335 len = structure->fields->len;
336 for (i = 0; i < len; i++) {
337 field = GST_STRUCTURE_FIELD (structure, i);
339 if (G_IS_VALUE (&field->value)) {
340 g_value_unset (&field->value);
341 }
342 }
343 g_array_free (structure->fields, TRUE);
344 #ifdef USE_POISONING
345 memset (structure, 0xff, sizeof (GstStructure));
346 #endif
347 g_slice_free (GstStructure, structure);
348 }
350 /**
351 * gst_structure_get_name:
352 * @structure: a #GstStructure
353 *
354 * Get the name of @structure as a string.
355 *
356 * Returns: the name of the structure.
357 */
358 const gchar *
359 gst_structure_get_name (const GstStructure * structure)
360 {
361 g_return_val_if_fail (structure != NULL, NULL);
363 return g_quark_to_string (structure->name);
364 }
366 /**
367 * gst_structure_has_name:
368 * @structure: a #GstStructure
369 * @name: structure name to check for
370 *
371 * Checks if the structure has the given name
372 *
373 * Returns: TRUE if @name matches the name of the structure.
374 */
375 gboolean
376 gst_structure_has_name (const GstStructure * structure, const gchar * name)
377 {
378 const gchar *structure_name;
380 g_return_val_if_fail (structure != NULL, FALSE);
381 g_return_val_if_fail (name != NULL, FALSE);
383 /* getting the string is cheap and comparing short strings is too
384 * should be faster than getting the quark for name and comparing the quarks
385 */
386 structure_name = g_quark_to_string (structure->name);
388 return (structure_name && strcmp (structure_name, name) == 0);
389 }
391 /**
392 * gst_structure_get_name_id:
393 * @structure: a #GstStructure
394 *
395 * Get the name of @structure as a GQuark.
396 *
397 * Returns: the quark representing the name of the structure.
398 */
399 GQuark
400 gst_structure_get_name_id (const GstStructure * structure)
401 {
402 g_return_val_if_fail (structure != NULL, 0);
404 return structure->name;
405 }
407 /**
408 * gst_structure_set_name:
409 * @structure: a #GstStructure
410 * @name: the new name of the structure
411 *
412 * Sets the name of the structure to the given @name. The string
413 * provided is copied before being used. It must not be empty, start with a
414 * letter and can be followed by letters, numbers and any of "/-_.:".
415 */
416 void
417 gst_structure_set_name (GstStructure * structure, const gchar * name)
418 {
419 g_return_if_fail (structure != NULL);
420 g_return_if_fail (IS_MUTABLE (structure));
421 g_return_if_fail (gst_structure_validate_name (name));
423 structure->name = g_quark_from_string (name);
424 }
426 static inline void
427 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
428 const GValue * value)
429 {
430 GstStructureField gsfield = { 0, {0,} };
432 gsfield.name = field;
433 gst_value_init_and_copy (&gsfield.value, value);
435 gst_structure_set_field (structure, &gsfield);
436 }
438 /**
439 * gst_structure_id_set_value:
440 * @structure: a #GstStructure
441 * @field: a #GQuark representing a field
442 * @value: the new value of the field
443 *
444 * Sets the field with the given GQuark @field to @value. If the field
445 * does not exist, it is created. If the field exists, the previous
446 * value is replaced and freed.
447 */
448 void
449 gst_structure_id_set_value (GstStructure * structure,
450 GQuark field, const GValue * value)
451 {
453 g_return_if_fail (structure != NULL);
454 g_return_if_fail (G_IS_VALUE (value));
455 g_return_if_fail (IS_MUTABLE (structure));
457 gst_structure_id_set_value_internal (structure, field, value);
458 }
460 /**
461 * gst_structure_set_value:
462 * @structure: a #GstStructure
463 * @fieldname: the name of the field to set
464 * @value: the new value of the field
465 *
466 * Sets the field with the given name @field to @value. If the field
467 * does not exist, it is created. If the field exists, the previous
468 * value is replaced and freed.
469 */
470 void
471 gst_structure_set_value (GstStructure * structure,
472 const gchar * fieldname, const GValue * value)
473 {
474 g_return_if_fail (structure != NULL);
475 g_return_if_fail (fieldname != NULL);
476 g_return_if_fail (G_IS_VALUE (value));
477 g_return_if_fail (IS_MUTABLE (structure));
479 gst_structure_id_set_value_internal (structure,
480 g_quark_from_string (fieldname), value);
481 }
483 static inline void
484 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
485 GValue * value)
486 {
487 GstStructureField gsfield = { 0, {0,} };
489 gsfield.name = field;
490 gsfield.value = *value;
492 gst_structure_set_field (structure, &gsfield);
494 /* we took ownership */
495 #ifdef USE_POISONING
496 memset (value, 0, sizeof (GValue));
497 #else
498 value->g_type = G_TYPE_INVALID;
499 #endif
500 }
502 /**
503 * gst_structure_id_take_value:
504 * @structure: a #GstStructure
505 * @field: a #GQuark representing a field
506 * @value: (transfer full): the new value of the field
507 *
508 * Sets the field with the given GQuark @field to @value. If the field
509 * does not exist, it is created. If the field exists, the previous
510 * value is replaced and freed.
511 *
512 * Since: 0.10.31
513 */
514 void
515 gst_structure_id_take_value (GstStructure * structure, GQuark field,
516 GValue * value)
517 {
518 g_return_if_fail (structure != NULL);
519 g_return_if_fail (G_IS_VALUE (value));
520 g_return_if_fail (IS_MUTABLE (structure));
522 gst_structure_id_take_value_internal (structure, field, value);
523 }
525 /**
526 * gst_structure_take_value:
527 * @structure: a #GstStructure
528 * @fieldname: the name of the field to set
529 * @value: (transfer full): the new value of the field
530 *
531 * Sets the field with the given name @field to @value. If the field
532 * does not exist, it is created. If the field exists, the previous
533 * value is replaced and freed. The function will take ownership of @value.
534 *
535 * Since: 0.10.31
536 */
537 void
538 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
539 GValue * value)
540 {
541 g_return_if_fail (structure != NULL);
542 g_return_if_fail (fieldname != NULL);
543 g_return_if_fail (G_IS_VALUE (value));
544 g_return_if_fail (IS_MUTABLE (structure));
546 gst_structure_id_take_value_internal (structure,
547 g_quark_from_string (fieldname), value);
548 }
550 static void
551 gst_structure_set_valist_internal (GstStructure * structure,
552 const gchar * fieldname, va_list varargs)
553 {
554 gchar *err = NULL;
555 GType type;
557 while (fieldname) {
558 GstStructureField field = { 0 };
560 field.name = g_quark_from_string (fieldname);
562 type = va_arg (varargs, GType);
564 if (G_UNLIKELY (type == G_TYPE_DATE)) {
565 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
566 type = GST_TYPE_DATE;
567 }
568 #if GLIB_CHECK_VERSION(2,23,3)
569 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
570 #else
571 g_value_init (&field.value, type);
572 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
573 #endif
574 if (G_UNLIKELY (err)) {
575 g_critical ("%s", err);
576 return;
577 }
578 gst_structure_set_field (structure, &field);
580 fieldname = va_arg (varargs, gchar *);
581 }
582 }
584 /**
585 * gst_structure_set:
586 * @structure: a #GstStructure
587 * @fieldname: the name of the field to set
588 * @...: variable arguments
589 *
590 * Parses the variable arguments and sets fields accordingly.
591 * Variable arguments should be in the form field name, field type
592 * (as a GType), value(s). The last variable argument should be NULL.
593 */
594 void
595 gst_structure_set (GstStructure * structure, const gchar * field, ...)
596 {
597 va_list varargs;
599 g_return_if_fail (structure != NULL);
600 g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
602 va_start (varargs, field);
603 gst_structure_set_valist_internal (structure, field, varargs);
604 va_end (varargs);
605 }
607 /**
608 * gst_structure_set_valist:
609 * @structure: a #GstStructure
610 * @fieldname: the name of the field to set
611 * @varargs: variable arguments
612 *
613 * va_list form of gst_structure_set().
614 */
615 void
616 gst_structure_set_valist (GstStructure * structure,
617 const gchar * fieldname, va_list varargs)
618 {
619 g_return_if_fail (structure != NULL);
620 g_return_if_fail (IS_MUTABLE (structure));
622 gst_structure_set_valist_internal (structure, fieldname, varargs);
623 }
625 static void
626 gst_structure_id_set_valist_internal (GstStructure * structure,
627 GQuark fieldname, va_list varargs)
628 {
629 gchar *err = NULL;
630 GType type;
632 while (fieldname) {
633 GstStructureField field = { 0 };
635 field.name = fieldname;
637 type = va_arg (varargs, GType);
639 if (G_UNLIKELY (type == G_TYPE_DATE)) {
640 g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
641 type = GST_TYPE_DATE;
642 }
643 #ifndef G_VALUE_COLLECT_INIT
644 g_value_init (&field.value, type);
645 G_VALUE_COLLECT (&field.value, varargs, 0, &err);
646 #else
647 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
648 #endif
649 if (G_UNLIKELY (err)) {
650 g_critical ("%s", err);
651 return;
652 }
653 gst_structure_set_field (structure, &field);
655 fieldname = va_arg (varargs, GQuark);
656 }
657 }
659 /**
660 * gst_structure_id_set:
661 * @structure: a #GstStructure
662 * @fieldname: the GQuark for the name of the field to set
663 * @...: variable arguments
664 *
665 * Identical to gst_structure_set, except that field names are
666 * passed using the GQuark for the field name. This allows more efficient
667 * setting of the structure if the caller already knows the associated
668 * quark values.
669 * The last variable argument must be NULL.
670 *
671 * Since: 0.10.10
672 */
673 void
674 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
675 {
676 va_list varargs;
678 g_return_if_fail (structure != NULL);
680 va_start (varargs, field);
681 gst_structure_id_set_valist_internal (structure, field, varargs);
682 va_end (varargs);
683 }
685 /**
686 * gst_structure_id_set_valist:
687 * @structure: a #GstStructure
688 * @fieldname: the name of the field to set
689 * @varargs: variable arguments
690 *
691 * va_list form of gst_structure_id_set().
692 *
693 * Since: 0.10.10
694 */
695 void
696 gst_structure_id_set_valist (GstStructure * structure,
697 GQuark fieldname, va_list varargs)
698 {
699 g_return_if_fail (structure != NULL);
700 g_return_if_fail (IS_MUTABLE (structure));
702 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
703 }
705 /**
706 * gst_structure_id_new:
707 * @name_quark: name of new structure
708 * @field_quark: the GQuark for the name of the field to set
709 * @...: variable arguments
710 *
711 * Creates a new #GstStructure with the given name as a GQuark, followed by
712 * fieldname quark, GType, argument(s) "triplets" in the same format as
713 * gst_structure_id_set(). Basically a convenience wrapper around
714 * gst_structure_id_empty_new() and gst_structure_id_set().
715 *
716 * The last variable argument must be NULL (or 0).
717 *
718 * Free-function: gst_structure_free
719 *
720 * Returns: (transfer full): a new #GstStructure
721 *
722 * Since: 0.10.24
723 */
724 GstStructure *
725 gst_structure_id_new (GQuark name_quark, GQuark field_quark, ...)
726 {
727 GstStructure *s;
728 va_list varargs;
730 g_return_val_if_fail (name_quark != 0, NULL);
731 g_return_val_if_fail (field_quark != 0, NULL);
733 s = gst_structure_id_empty_new (name_quark);
735 va_start (varargs, field_quark);
736 gst_structure_id_set_valist_internal (s, field_quark, varargs);
737 va_end (varargs);
739 return s;
740 }
742 #if GST_VERSION_NANO == 1
743 #define GIT_G_WARNING g_warning
744 #else
745 #define GIT_G_WARNING GST_WARNING
746 #endif
748 /* If the structure currently contains a field with the same name, it is
749 * replaced with the provided field. Otherwise, the field is added to the
750 * structure. The field's value is not deeply copied.
751 */
752 static void
753 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
754 {
755 GstStructureField *f;
756 guint i, len = structure->fields->len;
758 if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
759 const gchar *s;
761 s = g_value_get_string (&field->value);
762 /* only check for NULL strings in taglists, as they are allowed in message
763 * structs, e.g. error message debug strings */
764 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
765 if (s == NULL) {
766 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
767 "Please file a bug.", g_quark_to_string (field->name));
768 g_value_unset (&field->value);
769 return;
770 } else {
771 /* empty strings never make sense */
772 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
773 "Please file a bug.", g_quark_to_string (field->name));
774 g_value_unset (&field->value);
775 return;
776 }
777 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
778 g_warning ("Trying to set string on %s field '%s', but string is not "
779 "valid UTF-8. Please file a bug.",
780 IS_TAGLIST (structure) ? "taglist" : "structure",
781 g_quark_to_string (field->name));
782 g_value_unset (&field->value);
783 return;
784 }
785 } else if (G_UNLIKELY (GST_VALUE_HOLDS_DATE (&field->value))) {
786 const GDate *d;
788 d = gst_value_get_date (&field->value);
789 /* only check for NULL GDates in taglists, as they might make sense
790 * in other, generic structs */
791 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
792 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
793 "Please file a bug.", g_quark_to_string (field->name));
794 g_value_unset (&field->value);
795 return;
796 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
797 g_warning
798 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
799 IS_TAGLIST (structure) ? "taglist" : "structure",
800 g_quark_to_string (field->name));
801 g_value_unset (&field->value);
802 return;
803 }
804 }
806 for (i = 0; i < len; i++) {
807 f = GST_STRUCTURE_FIELD (structure, i);
809 if (G_UNLIKELY (f->name == field->name)) {
810 g_value_unset (&f->value);
811 memcpy (f, field, sizeof (GstStructureField));
812 return;
813 }
814 }
816 g_array_append_val (structure->fields, *field);
817 }
819 /* If there is no field with the given ID, NULL is returned.
820 */
821 static GstStructureField *
822 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
823 {
824 GstStructureField *field;
825 guint i, len;
827 len = structure->fields->len;
829 for (i = 0; i < len; i++) {
830 field = GST_STRUCTURE_FIELD (structure, i);
832 if (G_UNLIKELY (field->name == field_id))
833 return field;
834 }
836 return NULL;
837 }
839 /* If there is no field with the given ID, NULL is returned.
840 */
841 static GstStructureField *
842 gst_structure_get_field (const GstStructure * structure,
843 const gchar * fieldname)
844 {
845 g_return_val_if_fail (structure != NULL, NULL);
846 g_return_val_if_fail (fieldname != NULL, NULL);
848 return gst_structure_id_get_field (structure,
849 g_quark_from_string (fieldname));
850 }
852 /**
853 * gst_structure_get_value:
854 * @structure: a #GstStructure
855 * @fieldname: the name of the field to get
856 *
857 * Get the value of the field with name @fieldname.
858 *
859 * Returns: the #GValue corresponding to the field with the given name.
860 */
861 const GValue *
862 gst_structure_get_value (const GstStructure * structure,
863 const gchar * fieldname)
864 {
865 GstStructureField *field;
867 g_return_val_if_fail (structure != NULL, NULL);
868 g_return_val_if_fail (fieldname != NULL, NULL);
870 field = gst_structure_get_field (structure, fieldname);
871 if (field == NULL)
872 return NULL;
874 return &field->value;
875 }
877 /**
878 * gst_structure_id_get_value:
879 * @structure: a #GstStructure
880 * @field: the #GQuark of the field to get
881 *
882 * Get the value of the field with GQuark @field.
883 *
884 * Returns: the #GValue corresponding to the field with the given name
885 * identifier.
886 */
887 const GValue *
888 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
889 {
890 GstStructureField *gsfield;
892 g_return_val_if_fail (structure != NULL, NULL);
894 gsfield = gst_structure_id_get_field (structure, field);
895 if (gsfield == NULL)
896 return NULL;
898 return &gsfield->value;
899 }
901 /**
902 * gst_structure_remove_field:
903 * @structure: a #GstStructure
904 * @fieldname: the name of the field to remove
905 *
906 * Removes the field with the given name. If the field with the given
907 * name does not exist, the structure is unchanged.
908 */
909 void
910 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
911 {
912 GstStructureField *field;
913 GQuark id;
914 guint i, len;
916 g_return_if_fail (structure != NULL);
917 g_return_if_fail (fieldname != NULL);
918 g_return_if_fail (IS_MUTABLE (structure));
920 id = g_quark_from_string (fieldname);
921 len = structure->fields->len;
923 for (i = 0; i < len; i++) {
924 field = GST_STRUCTURE_FIELD (structure, i);
926 if (field->name == id) {
927 if (G_IS_VALUE (&field->value)) {
928 g_value_unset (&field->value);
929 }
930 structure->fields = g_array_remove_index (structure->fields, i);
931 return;
932 }
933 }
934 }
936 /**
937 * gst_structure_remove_fields:
938 * @structure: a #GstStructure
939 * @fieldname: the name of the field to remove
940 * @...: NULL-terminated list of more fieldnames to remove
941 *
942 * Removes the fields with the given names. If a field does not exist, the
943 * argument is ignored.
944 */
945 void
946 gst_structure_remove_fields (GstStructure * structure,
947 const gchar * fieldname, ...)
948 {
949 va_list varargs;
951 g_return_if_fail (structure != NULL);
952 g_return_if_fail (fieldname != NULL);
953 /* mutability checked in remove_field */
955 va_start (varargs, fieldname);
956 gst_structure_remove_fields_valist (structure, fieldname, varargs);
957 va_end (varargs);
958 }
960 /**
961 * gst_structure_remove_fields_valist:
962 * @structure: a #GstStructure
963 * @fieldname: the name of the field to remove
964 * @varargs: NULL-terminated list of more fieldnames to remove
965 *
966 * va_list form of gst_structure_remove_fields().
967 */
968 void
969 gst_structure_remove_fields_valist (GstStructure * structure,
970 const gchar * fieldname, va_list varargs)
971 {
972 gchar *field = (gchar *) fieldname;
974 g_return_if_fail (structure != NULL);
975 g_return_if_fail (fieldname != NULL);
976 /* mutability checked in remove_field */
978 while (field) {
979 gst_structure_remove_field (structure, field);
980 field = va_arg (varargs, char *);
981 }
982 }
984 /**
985 * gst_structure_remove_all_fields:
986 * @structure: a #GstStructure
987 *
988 * Removes all fields in a GstStructure.
989 */
990 void
991 gst_structure_remove_all_fields (GstStructure * structure)
992 {
993 GstStructureField *field;
994 int i;
996 g_return_if_fail (structure != NULL);
997 g_return_if_fail (IS_MUTABLE (structure));
999 for (i = structure->fields->len - 1; i >= 0; i--) {
1000 field = GST_STRUCTURE_FIELD (structure, i);
1002 if (G_IS_VALUE (&field->value)) {
1003 g_value_unset (&field->value);
1004 }
1005 structure->fields = g_array_remove_index (structure->fields, i);
1006 }
1007 }
1009 /**
1010 * gst_structure_get_field_type:
1011 * @structure: a #GstStructure
1012 * @fieldname: the name of the field
1013 *
1014 * Finds the field with the given name, and returns the type of the
1015 * value it contains. If the field is not found, G_TYPE_INVALID is
1016 * returned.
1017 *
1018 * Returns: the #GValue of the field
1019 */
1020 GType
1021 gst_structure_get_field_type (const GstStructure * structure,
1022 const gchar * fieldname)
1023 {
1024 GstStructureField *field;
1026 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1027 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1029 field = gst_structure_get_field (structure, fieldname);
1030 if (field == NULL)
1031 return G_TYPE_INVALID;
1033 return G_VALUE_TYPE (&field->value);
1034 }
1036 /**
1037 * gst_structure_n_fields:
1038 * @structure: a #GstStructure
1039 *
1040 * Get the number of fields in the structure.
1041 *
1042 * Returns: the number of fields in the structure
1043 */
1044 gint
1045 gst_structure_n_fields (const GstStructure * structure)
1046 {
1047 g_return_val_if_fail (structure != NULL, 0);
1049 return structure->fields->len;
1050 }
1052 /**
1053 * gst_structure_nth_field_name:
1054 * @structure: a #GstStructure
1055 * @index: the index to get the name of
1056 *
1057 * Get the name of the given field number, counting from 0 onwards.
1058 *
1059 * Returns: the name of the given field number
1060 */
1061 const gchar *
1062 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1063 {
1064 GstStructureField *field;
1066 g_return_val_if_fail (structure != NULL, NULL);
1067 g_return_val_if_fail (index < structure->fields->len, NULL);
1069 field = GST_STRUCTURE_FIELD (structure, index);
1071 return g_quark_to_string (field->name);
1072 }
1074 /**
1075 * gst_structure_foreach:
1076 * @structure: a #GstStructure
1077 * @func: (scope call): a function to call for each field
1078 * @user_data: (closure): private data
1079 *
1080 * Calls the provided function once for each field in the #GstStructure. The
1081 * function must not modify the fields. Also see gst_structure_map_in_place().
1082 *
1083 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1084 * FALSE otherwise.
1085 */
1086 gboolean
1087 gst_structure_foreach (const GstStructure * structure,
1088 GstStructureForeachFunc func, gpointer user_data)
1089 {
1090 guint i, len;
1091 GstStructureField *field;
1092 gboolean ret;
1094 g_return_val_if_fail (structure != NULL, FALSE);
1095 g_return_val_if_fail (func != NULL, FALSE);
1097 len = structure->fields->len;
1099 for (i = 0; i < len; i++) {
1100 field = GST_STRUCTURE_FIELD (structure, i);
1102 ret = func (field->name, &field->value, user_data);
1103 if (G_UNLIKELY (!ret))
1104 return FALSE;
1105 }
1107 return TRUE;
1108 }
1110 /**
1111 * gst_structure_map_in_place:
1112 * @structure: a #GstStructure
1113 * @func: (scope call): a function to call for each field
1114 * @user_data: (closure): private data
1115 *
1116 * Calls the provided function once for each field in the #GstStructure. In
1117 * contrast to gst_structure_foreach(), the function may modify but not delete the
1118 * fields. The structure must be mutable.
1119 *
1120 * Returns: TRUE if the supplied function returns TRUE For each of the fields,
1121 * FALSE otherwise.
1122 */
1123 gboolean
1124 gst_structure_map_in_place (GstStructure * structure,
1125 GstStructureMapFunc func, gpointer user_data)
1126 {
1127 guint i, len;
1128 GstStructureField *field;
1129 gboolean ret;
1131 g_return_val_if_fail (structure != NULL, FALSE);
1132 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1133 g_return_val_if_fail (func != NULL, FALSE);
1134 len = structure->fields->len;
1136 for (i = 0; i < len; i++) {
1137 field = GST_STRUCTURE_FIELD (structure, i);
1139 ret = func (field->name, &field->value, user_data);
1140 if (!ret)
1141 return FALSE;
1142 }
1144 return TRUE;
1145 }
1147 /**
1148 * gst_structure_id_has_field:
1149 * @structure: a #GstStructure
1150 * @field: #GQuark of the field name
1151 *
1152 * Check if @structure contains a field named @field.
1153 *
1154 * Returns: TRUE if the structure contains a field with the given name
1155 *
1156 * Since: 0.10.26
1157 */
1158 gboolean
1159 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1160 {
1161 GstStructureField *f;
1163 g_return_val_if_fail (structure != NULL, FALSE);
1164 g_return_val_if_fail (field != 0, FALSE);
1166 f = gst_structure_id_get_field (structure, field);
1168 return (f != NULL);
1169 }
1171 /**
1172 * gst_structure_has_field:
1173 * @structure: a #GstStructure
1174 * @fieldname: the name of a field
1175 *
1176 * Check if @structure contains a field named @fieldname.
1177 *
1178 * Returns: TRUE if the structure contains a field with the given name
1179 */
1180 gboolean
1181 gst_structure_has_field (const GstStructure * structure,
1182 const gchar * fieldname)
1183 {
1184 g_return_val_if_fail (structure != NULL, FALSE);
1185 g_return_val_if_fail (fieldname != NULL, FALSE);
1187 return gst_structure_id_has_field (structure,
1188 g_quark_from_string (fieldname));
1189 }
1191 /**
1192 * gst_structure_id_has_field_typed:
1193 * @structure: a #GstStructure
1194 * @field: #GQuark of the field name
1195 * @type: the type of a value
1196 *
1197 * Check if @structure contains a field named @field and with GType @type.
1198 *
1199 * Returns: TRUE if the structure contains a field with the given name and type
1200 *
1201 * Since: 0.10.26
1202 */
1203 gboolean
1204 gst_structure_id_has_field_typed (const GstStructure * structure,
1205 GQuark field, GType type)
1206 {
1207 GstStructureField *f;
1209 g_return_val_if_fail (structure != NULL, FALSE);
1210 g_return_val_if_fail (field != 0, FALSE);
1212 f = gst_structure_id_get_field (structure, field);
1213 if (f == NULL)
1214 return FALSE;
1216 return (G_VALUE_TYPE (&f->value) == type);
1217 }
1219 /**
1220 * gst_structure_has_field_typed:
1221 * @structure: a #GstStructure
1222 * @fieldname: the name of a field
1223 * @type: the type of a value
1224 *
1225 * Check if @structure contains a field named @fieldname and with GType @type.
1226 *
1227 * Returns: TRUE if the structure contains a field with the given name and type
1228 */
1229 gboolean
1230 gst_structure_has_field_typed (const GstStructure * structure,
1231 const gchar * fieldname, GType type)
1232 {
1233 g_return_val_if_fail (structure != NULL, FALSE);
1234 g_return_val_if_fail (fieldname != NULL, FALSE);
1236 return gst_structure_id_has_field_typed (structure,
1237 g_quark_from_string (fieldname), type);
1238 }
1240 /* utility functions */
1242 /**
1243 * gst_structure_get_boolean:
1244 * @structure: a #GstStructure
1245 * @fieldname: the name of a field
1246 * @value: (out): a pointer to a #gboolean to set
1247 *
1248 * Sets the boolean pointed to by @value corresponding to the value of the
1249 * given field. Caller is responsible for making sure the field exists
1250 * and has the correct type.
1251 *
1252 * Returns: TRUE if the value could be set correctly. If there was no field
1253 * with @fieldname or the existing field did not contain a boolean, this
1254 * function returns FALSE.
1255 */
1256 gboolean
1257 gst_structure_get_boolean (const GstStructure * structure,
1258 const gchar * fieldname, gboolean * value)
1259 {
1260 GstStructureField *field;
1262 g_return_val_if_fail (structure != NULL, FALSE);
1263 g_return_val_if_fail (fieldname != NULL, FALSE);
1265 field = gst_structure_get_field (structure, fieldname);
1267 if (field == NULL)
1268 return FALSE;
1269 if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
1270 return FALSE;
1272 *value = gst_g_value_get_boolean_unchecked (&field->value);
1274 return TRUE;
1275 }
1277 /**
1278 * gst_structure_get_int:
1279 * @structure: a #GstStructure
1280 * @fieldname: the name of a field
1281 * @value: (out): a pointer to an int to set
1282 *
1283 * Sets the int pointed to by @value corresponding to the value of the
1284 * given field. Caller is responsible for making sure the field exists
1285 * and has the correct type.
1286 *
1287 * Returns: %TRUE if the value could be set correctly. If there was no field
1288 * with @fieldname or the existing field did not contain an int, this function
1289 * returns %FALSE.
1290 */
1291 gboolean
1292 gst_structure_get_int (const GstStructure * structure,
1293 const gchar * fieldname, gint * value)
1294 {
1295 GstStructureField *field;
1297 g_return_val_if_fail (structure != NULL, FALSE);
1298 g_return_val_if_fail (fieldname != NULL, FALSE);
1299 g_return_val_if_fail (value != NULL, FALSE);
1301 field = gst_structure_get_field (structure, fieldname);
1303 if (field == NULL)
1304 return FALSE;
1305 if (!G_VALUE_HOLDS_INT (&field->value))
1306 return FALSE;
1308 *value = gst_g_value_get_int_unchecked (&field->value);
1310 return TRUE;
1311 }
1313 /**
1314 * gst_structure_get_uint:
1315 * @structure: a #GstStructure
1316 * @fieldname: the name of a field
1317 * @value: (out): a pointer to a uint to set
1318 *
1319 * Sets the uint pointed to by @value corresponding to the value of the
1320 * given field. Caller is responsible for making sure the field exists
1321 * and has the correct type.
1322 *
1323 * Returns: %TRUE if the value could be set correctly. If there was no field
1324 * with @fieldname or the existing field did not contain a uint, this function
1325 * returns %FALSE.
1326 *
1327 * Since: 0.10.15
1328 */
1329 gboolean
1330 gst_structure_get_uint (const GstStructure * structure,
1331 const gchar * fieldname, guint * value)
1332 {
1333 GstStructureField *field;
1335 g_return_val_if_fail (structure != NULL, FALSE);
1336 g_return_val_if_fail (fieldname != NULL, FALSE);
1337 g_return_val_if_fail (value != NULL, FALSE);
1339 field = gst_structure_get_field (structure, fieldname);
1341 if (field == NULL)
1342 return FALSE;
1343 if (!G_VALUE_HOLDS_UINT (&field->value))
1344 return FALSE;
1346 *value = gst_g_value_get_uint_unchecked (&field->value);
1348 return TRUE;
1349 }
1351 /**
1352 * gst_structure_get_fourcc:
1353 * @structure: a #GstStructure
1354 * @fieldname: the name of a field
1355 * @value: (out): a pointer to a 32bit unsigned int to set
1356 *
1357 * Sets the Fourcc pointed to by @value corresponding to the value of the
1358 * given field. Caller is responsible for making sure the field exists
1359 * and has the correct type.
1360 *
1361 * Returns: TRUE if the value could be set correctly. If there was no field
1362 * with @fieldname or the existing field did not contain a fourcc, this function
1363 * returns FALSE.
1364 */
1365 gboolean
1366 gst_structure_get_fourcc (const GstStructure * structure,
1367 const gchar * fieldname, guint32 * value)
1368 {
1369 GstStructureField *field;
1371 g_return_val_if_fail (structure != NULL, FALSE);
1372 g_return_val_if_fail (fieldname != NULL, FALSE);
1373 g_return_val_if_fail (value != NULL, FALSE);
1375 field = gst_structure_get_field (structure, fieldname);
1377 if (field == NULL)
1378 return FALSE;
1379 if (!GST_VALUE_HOLDS_FOURCC (&field->value))
1380 return FALSE;
1382 *value = gst_value_get_fourcc (&field->value);
1384 return TRUE;
1385 }
1387 /**
1388 * gst_structure_get_date:
1389 * @structure: a #GstStructure
1390 * @fieldname: the name of a field
1391 * @value: (out callee-allocates): a pointer to a #GDate to set
1392 *
1393 * Sets the date pointed to by @value corresponding to the date of the
1394 * given field. Caller is responsible for making sure the field exists
1395 * and has the correct type.
1396 *
1397 * On success @value will point to a newly-allocated copy of the date which
1398 * should be freed with g_date_free() when no longer needed (note: this is
1399 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1400 * copy of the string).
1401 *
1402 * Returns: TRUE if the value could be set correctly. If there was no field
1403 * with @fieldname or the existing field did not contain a data, this function
1404 * returns FALSE.
1405 */
1406 gboolean
1407 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1408 GDate ** value)
1409 {
1410 GstStructureField *field;
1412 g_return_val_if_fail (structure != NULL, FALSE);
1413 g_return_val_if_fail (fieldname != NULL, FALSE);
1414 g_return_val_if_fail (value != NULL, FALSE);
1416 field = gst_structure_get_field (structure, fieldname);
1418 if (field == NULL)
1419 return FALSE;
1420 if (!GST_VALUE_HOLDS_DATE (&field->value))
1421 return FALSE;
1423 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1424 *value = g_value_dup_boxed (&field->value);
1426 return TRUE;
1427 }
1429 /**
1430 * gst_structure_get_date_time:
1431 * @structure: a #GstStructure
1432 * @fieldname: the name of a field
1433 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1434 *
1435 * Sets the datetime pointed to by @value corresponding to the datetime of the
1436 * given field. Caller is responsible for making sure the field exists
1437 * and has the correct type.
1438 *
1439 * On success @value will point to a reference of the datetime which
1440 * should be unreffed with gst_date_time_unref() when no longer needed
1441 * (note: this is inconsistent with e.g. gst_structure_get_string()
1442 * which doesn't return a copy of the string).
1443 *
1444 * Returns: TRUE if the value could be set correctly. If there was no field
1445 * with @fieldname or the existing field did not contain a data, this function
1446 * returns FALSE.
1447 *
1448 * Since: 0.10.31
1449 */
1450 gboolean
1451 gst_structure_get_date_time (const GstStructure * structure,
1452 const gchar * fieldname, GstDateTime ** value)
1453 {
1454 GstStructureField *field;
1456 g_return_val_if_fail (structure != NULL, FALSE);
1457 g_return_val_if_fail (fieldname != NULL, FALSE);
1458 g_return_val_if_fail (value != NULL, FALSE);
1460 field = gst_structure_get_field (structure, fieldname);
1462 if (field == NULL)
1463 return FALSE;
1464 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1465 return FALSE;
1467 /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
1468 *value = g_value_dup_boxed (&field->value);
1470 return TRUE;
1471 }
1473 /**
1474 * gst_structure_get_clock_time:
1475 * @structure: a #GstStructure
1476 * @fieldname: the name of a field
1477 * @value: (out): a pointer to a #GstClockTime to set
1478 *
1479 * Sets the clock time pointed to by @value corresponding to the clock time
1480 * of the given field. Caller is responsible for making sure the field exists
1481 * and has the correct type.
1482 *
1483 * Returns: TRUE if the value could be set correctly. If there was no field
1484 * with @fieldname or the existing field did not contain a #GstClockTime, this
1485 * function returns FALSE.
1486 */
1487 gboolean
1488 gst_structure_get_clock_time (const GstStructure * structure,
1489 const gchar * fieldname, GstClockTime * value)
1490 {
1491 GstStructureField *field;
1493 g_return_val_if_fail (structure != NULL, FALSE);
1494 g_return_val_if_fail (fieldname != NULL, FALSE);
1495 g_return_val_if_fail (value != NULL, FALSE);
1497 field = gst_structure_get_field (structure, fieldname);
1499 if (field == NULL)
1500 return FALSE;
1501 if (!G_VALUE_HOLDS_UINT64 (&field->value))
1502 return FALSE;
1504 *value = gst_g_value_get_uint64_unchecked (&field->value);
1506 return TRUE;
1507 }
1509 /**
1510 * gst_structure_get_double:
1511 * @structure: a #GstStructure
1512 * @fieldname: the name of a field
1513 * @value: (out): a pointer to a gdouble to set
1514 *
1515 * Sets the double pointed to by @value corresponding to the value of the
1516 * given field. Caller is responsible for making sure the field exists
1517 * and has the correct type.
1518 *
1519 * Returns: TRUE if the value could be set correctly. If there was no field
1520 * with @fieldname or the existing field did not contain a double, this
1521 * function returns FALSE.
1522 */
1523 gboolean
1524 gst_structure_get_double (const GstStructure * structure,
1525 const gchar * fieldname, gdouble * value)
1526 {
1527 GstStructureField *field;
1529 g_return_val_if_fail (structure != NULL, FALSE);
1530 g_return_val_if_fail (fieldname != NULL, FALSE);
1531 g_return_val_if_fail (value != NULL, FALSE);
1533 field = gst_structure_get_field (structure, fieldname);
1535 if (field == NULL)
1536 return FALSE;
1537 if (!G_VALUE_HOLDS_DOUBLE (&field->value))
1538 return FALSE;
1540 *value = gst_g_value_get_double_unchecked (&field->value);
1542 return TRUE;
1543 }
1545 /**
1546 * gst_structure_get_string:
1547 * @structure: a #GstStructure
1548 * @fieldname: the name of a field
1549 *
1550 * Finds the field corresponding to @fieldname, and returns the string
1551 * contained in the field's value. Caller is responsible for making
1552 * sure the field exists and has the correct type.
1553 *
1554 * The string should not be modified, and remains valid until the next
1555 * call to a gst_structure_*() function with the given structure.
1556 *
1557 * Returns: a pointer to the string or NULL when the field did not exist
1558 * or did not contain a string.
1559 */
1560 const gchar *
1561 gst_structure_get_string (const GstStructure * structure,
1562 const gchar * fieldname)
1563 {
1564 GstStructureField *field;
1566 g_return_val_if_fail (structure != NULL, NULL);
1567 g_return_val_if_fail (fieldname != NULL, NULL);
1569 field = gst_structure_get_field (structure, fieldname);
1571 if (field == NULL)
1572 return NULL;
1573 if (!G_VALUE_HOLDS_STRING (&field->value))
1574 return NULL;
1576 return gst_g_value_get_string_unchecked (&field->value);
1577 }
1579 /**
1580 * gst_structure_get_enum:
1581 * @structure: a #GstStructure
1582 * @fieldname: the name of a field
1583 * @enumtype: the enum type of a field
1584 * @value: (out): a pointer to an int to set
1585 *
1586 * Sets the int pointed to by @value corresponding to the value of the
1587 * given field. Caller is responsible for making sure the field exists,
1588 * has the correct type and that the enumtype is correct.
1589 *
1590 * Returns: TRUE if the value could be set correctly. If there was no field
1591 * with @fieldname or the existing field did not contain an enum of the given
1592 * type, this function returns FALSE.
1593 */
1594 gboolean
1595 gst_structure_get_enum (const GstStructure * structure,
1596 const gchar * fieldname, GType enumtype, gint * value)
1597 {
1598 GstStructureField *field;
1600 g_return_val_if_fail (structure != NULL, FALSE);
1601 g_return_val_if_fail (fieldname != NULL, FALSE);
1602 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1603 g_return_val_if_fail (value != NULL, FALSE);
1605 field = gst_structure_get_field (structure, fieldname);
1607 if (field == NULL)
1608 return FALSE;
1609 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1610 return FALSE;
1612 *value = g_value_get_enum (&field->value);
1614 return TRUE;
1615 }
1617 /**
1618 * gst_structure_get_fraction:
1619 * @structure: a #GstStructure
1620 * @fieldname: the name of a field
1621 * @value_numerator: (out): a pointer to an int to set
1622 * @value_denominator: (out): a pointer to an int to set
1623 *
1624 * Sets the integers pointed to by @value_numerator and @value_denominator
1625 * corresponding to the value of the given field. Caller is responsible
1626 * for making sure the field exists and has the correct type.
1627 *
1628 * Returns: TRUE if the values could be set correctly. If there was no field
1629 * with @fieldname or the existing field did not contain a GstFraction, this
1630 * function returns FALSE.
1631 */
1632 gboolean
1633 gst_structure_get_fraction (const GstStructure * structure,
1634 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1635 {
1636 GstStructureField *field;
1638 g_return_val_if_fail (structure != NULL, FALSE);
1639 g_return_val_if_fail (fieldname != NULL, FALSE);
1640 g_return_val_if_fail (value_numerator != NULL, FALSE);
1641 g_return_val_if_fail (value_denominator != NULL, FALSE);
1643 field = gst_structure_get_field (structure, fieldname);
1645 if (field == NULL)
1646 return FALSE;
1647 if (!GST_VALUE_HOLDS_FRACTION (&field->value))
1648 return FALSE;
1650 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1651 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1653 return TRUE;
1654 }
1656 typedef struct _GstStructureAbbreviation
1657 {
1658 const gchar *type_name;
1659 GType type;
1660 }
1661 GstStructureAbbreviation;
1663 /* return a copy of an array of GstStructureAbbreviation containing all the
1664 * known type_string, GType maps, including abbreviations for common types */
1665 static GstStructureAbbreviation *
1666 gst_structure_get_abbrs (gint * n_abbrs)
1667 {
1668 static GstStructureAbbreviation *abbrs = NULL;
1669 static volatile gsize num = 0;
1671 if (g_once_init_enter (&num)) {
1672 /* dynamically generate the array */
1673 gsize _num;
1674 GstStructureAbbreviation dyn_abbrs[] = {
1675 {"int", G_TYPE_INT}
1676 ,
1677 {"i", G_TYPE_INT}
1678 ,
1679 {"uint", G_TYPE_UINT}
1680 ,
1681 {"u", G_TYPE_UINT}
1682 ,
1683 {"float", G_TYPE_FLOAT}
1684 ,
1685 {"f", G_TYPE_FLOAT}
1686 ,
1687 {"double", G_TYPE_DOUBLE}
1688 ,
1689 {"d", G_TYPE_DOUBLE}
1690 ,
1691 {"buffer", GST_TYPE_BUFFER}
1692 ,
1693 {"fourcc", GST_TYPE_FOURCC}
1694 ,
1695 {"4", GST_TYPE_FOURCC}
1696 ,
1697 {"fraction", GST_TYPE_FRACTION}
1698 ,
1699 {"boolean", G_TYPE_BOOLEAN}
1700 ,
1701 {"bool", G_TYPE_BOOLEAN}
1702 ,
1703 {"b", G_TYPE_BOOLEAN}
1704 ,
1705 {"string", G_TYPE_STRING}
1706 ,
1707 {"str", G_TYPE_STRING}
1708 ,
1709 {"s", G_TYPE_STRING}
1710 ,
1711 {"structure", GST_TYPE_STRUCTURE}
1712 ,
1713 {"date", GST_TYPE_DATE}
1714 ,
1715 {"datetime", GST_TYPE_DATE_TIME}
1716 };
1717 _num = G_N_ELEMENTS (dyn_abbrs);
1718 /* permanently allocate and copy the array now */
1719 abbrs = g_new0 (GstStructureAbbreviation, _num);
1720 memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
1721 g_once_init_leave (&num, _num);
1722 }
1723 *n_abbrs = num;
1725 return abbrs;
1726 }
1728 /* given a type_name that could be a type abbreviation or a registered GType,
1729 * return a matching GType */
1730 static GType
1731 gst_structure_gtype_from_abbr (const char *type_name)
1732 {
1733 int i;
1734 GstStructureAbbreviation *abbrs;
1735 gint n_abbrs;
1737 g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
1739 abbrs = gst_structure_get_abbrs (&n_abbrs);
1741 for (i = 0; i < n_abbrs; i++) {
1742 if (strcmp (type_name, abbrs[i].type_name) == 0) {
1743 return abbrs[i].type;
1744 }
1745 }
1747 /* this is the fallback */
1748 return g_type_from_name (type_name);
1749 }
1751 static const char *
1752 gst_structure_to_abbr (GType type)
1753 {
1754 int i;
1755 GstStructureAbbreviation *abbrs;
1756 gint n_abbrs;
1758 g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
1760 abbrs = gst_structure_get_abbrs (&n_abbrs);
1762 for (i = 0; i < n_abbrs; i++) {
1763 if (type == abbrs[i].type) {
1764 return abbrs[i].type_name;
1765 }
1766 }
1768 return g_type_name (type);
1769 }
1771 static GType
1772 gst_structure_value_get_generic_type (GValue * val)
1773 {
1774 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
1775 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
1776 GArray *array = g_value_peek_pointer (val);
1778 if (array->len > 0) {
1779 GValue *value = &g_array_index (array, GValue, 0);
1781 return gst_structure_value_get_generic_type (value);
1782 } else {
1783 return G_TYPE_INT;
1784 }
1785 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
1786 return G_TYPE_INT;
1787 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
1788 return G_TYPE_INT64;
1789 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
1790 return G_TYPE_DOUBLE;
1791 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
1792 return GST_TYPE_FRACTION;
1793 }
1794 return G_VALUE_TYPE (val);
1795 }
1797 gboolean
1798 priv_gst_structure_append_to_gstring (const GstStructure * structure,
1799 GString * s)
1800 {
1801 GstStructureField *field;
1802 guint i, len;
1804 g_return_val_if_fail (s != NULL, FALSE);
1806 g_string_append (s, g_quark_to_string (structure->name));
1807 len = structure->fields->len;
1808 for (i = 0; i < len; i++) {
1809 char *t;
1810 GType type;
1812 field = GST_STRUCTURE_FIELD (structure, i);
1814 t = gst_value_serialize (&field->value);
1815 type = gst_structure_value_get_generic_type (&field->value);
1817 g_string_append_len (s, ", ", 2);
1818 /* FIXME: do we need to escape fieldnames? */
1819 g_string_append (s, g_quark_to_string (field->name));
1820 g_string_append_len (s, "=(", 2);
1821 g_string_append (s, gst_structure_to_abbr (type));
1822 g_string_append_c (s, ')');
1823 g_string_append (s, t == NULL ? "NULL" : t);
1824 g_free (t);
1825 }
1827 g_string_append_c (s, ';');
1828 return TRUE;
1829 }
1831 /**
1832 * gst_structure_to_string:
1833 * @structure: a #GstStructure
1834 *
1835 * Converts @structure to a human-readable string representation.
1836 *
1837 * For debugging purposes its easier to do something like this:
1838 * |[
1839 * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
1840 * ]|
1841 * This prints the structure in human readble form.
1842 *
1843 * Free-function: g_free
1844 *
1845 * Returns: (transfer full)L a pointer to string allocated by g_malloc().
1846 * g_free() after usage.
1847 */
1848 gchar *
1849 gst_structure_to_string (const GstStructure * structure)
1850 {
1851 GString *s;
1853 /* NOTE: This function is potentially called by the debug system,
1854 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
1855 * should be careful to avoid recursion. This includes any functions
1856 * called by gst_structure_to_string. In particular, calls should
1857 * not use the GST_PTR_FORMAT extension. */
1859 g_return_val_if_fail (structure != NULL, NULL);
1861 /* we estimate a minimum size based on the number of fields in order to
1862 * avoid unnecessary reallocs within GString */
1863 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
1864 priv_gst_structure_append_to_gstring (structure, s);
1865 return g_string_free (s, FALSE);
1866 }
1868 /*
1869 * r will still point to the string. if end == next, the string will not be
1870 * null-terminated. In all other cases it will be.
1871 * end = pointer to char behind end of string, next = pointer to start of
1872 * unread data.
1873 * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
1874 */
1875 static gboolean
1876 gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
1877 gboolean unescape)
1878 {
1879 gchar *w;
1881 if (*s == 0)
1882 return FALSE;
1884 if (*s != '"') {
1885 int ret;
1887 ret = gst_structure_parse_simple_string (s, end);
1888 *next = *end;
1890 return ret;
1891 }
1893 if (unescape) {
1894 w = s;
1895 s++;
1896 while (*s != '"') {
1897 if (G_UNLIKELY (*s == 0))
1898 return FALSE;
1899 if (G_UNLIKELY (*s == '\\'))
1900 s++;
1901 *w = *s;
1902 w++;
1903 s++;
1904 }
1905 s++;
1906 } else {
1907 /* Find the closing quotes */
1908 s++;
1909 while (*s != '"') {
1910 if (G_UNLIKELY (*s == 0))
1911 return FALSE;
1912 if (G_UNLIKELY (*s == '\\'))
1913 s++;
1914 s++;
1915 }
1916 s++;
1917 w = s;
1918 }
1920 *end = w;
1921 *next = s;
1923 return TRUE;
1924 }
1926 static gboolean
1927 gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
1928 GType type)
1929 {
1930 GValue value1 = { 0 };
1931 GValue value2 = { 0 };
1932 GType range_type;
1933 gboolean ret;
1935 if (*s != '[')
1936 return FALSE;
1937 s++;
1939 ret = gst_structure_parse_value (s, &s, &value1, type);
1940 if (ret == FALSE)
1941 return FALSE;
1943 while (g_ascii_isspace (*s))
1944 s++;
1946 if (*s != ',')
1947 return FALSE;
1948 s++;
1950 while (g_ascii_isspace (*s))
1951 s++;
1953 ret = gst_structure_parse_value (s, &s, &value2, type);
1954 if (ret == FALSE)
1955 return FALSE;
1957 while (g_ascii_isspace (*s))
1958 s++;
1960 if (*s != ']')
1961 return FALSE;
1962 s++;
1964 if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
1965 return FALSE;
1967 if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
1968 range_type = GST_TYPE_DOUBLE_RANGE;
1969 g_value_init (value, range_type);
1970 gst_value_set_double_range (value,
1971 gst_g_value_get_double_unchecked (&value1),
1972 gst_g_value_get_double_unchecked (&value2));
1973 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
1974 range_type = GST_TYPE_INT_RANGE;
1975 g_value_init (value, range_type);
1976 gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
1977 gst_g_value_get_int_unchecked (&value2));
1978 } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
1979 range_type = GST_TYPE_INT64_RANGE;
1980 g_value_init (value, range_type);
1981 gst_value_set_int64_range (value, gst_g_value_get_int64_unchecked (&value1),
1982 gst_g_value_get_int64_unchecked (&value2));
1983 } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
1984 range_type = GST_TYPE_FRACTION_RANGE;
1985 g_value_init (value, range_type);
1986 gst_value_set_fraction_range (value, &value1, &value2);
1987 } else {
1988 return FALSE;
1989 }
1991 *after = s;
1992 return TRUE;
1993 }
1995 static gboolean
1996 gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
1997 GType type, GType list_type, char begin, char end)
1998 {
1999 GValue list_value = { 0 };
2000 gboolean ret;
2001 GArray *array;
2003 g_value_init (value, list_type);
2004 array = g_value_peek_pointer (value);
2006 if (*s != begin)
2007 return FALSE;
2008 s++;
2010 while (g_ascii_isspace (*s))
2011 s++;
2012 if (*s == end) {
2013 s++;
2014 *after = s;
2015 return TRUE;
2016 }
2018 ret = gst_structure_parse_value (s, &s, &list_value, type);
2019 if (ret == FALSE)
2020 return FALSE;
2022 g_array_append_val (array, list_value);
2024 while (g_ascii_isspace (*s))
2025 s++;
2027 while (*s != end) {
2028 if (*s != ',')
2029 return FALSE;
2030 s++;
2032 while (g_ascii_isspace (*s))
2033 s++;
2035 memset (&list_value, 0, sizeof (list_value));
2036 ret = gst_structure_parse_value (s, &s, &list_value, type);
2037 if (ret == FALSE)
2038 return FALSE;
2040 g_array_append_val (array, list_value);
2041 while (g_ascii_isspace (*s))
2042 s++;
2043 }
2045 s++;
2047 *after = s;
2048 return TRUE;
2049 }
2051 static gboolean
2052 gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
2053 {
2054 return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
2055 '{', '}');
2056 }
2058 static gboolean
2059 gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
2060 GType type)
2061 {
2062 return gst_structure_parse_any_list (s, after, value, type,
2063 GST_TYPE_ARRAY, '<', '>');
2064 }
2066 static gboolean
2067 gst_structure_parse_simple_string (gchar * str, gchar ** end)
2068 {
2069 char *s = str;
2071 while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
2072 s++;
2073 }
2075 *end = s;
2077 return (s != str);
2078 }
2080 static gboolean
2081 gst_structure_parse_field (gchar * str,
2082 gchar ** after, GstStructureField * field)
2083 {
2084 gchar *name;
2085 gchar *name_end;
2086 gchar *s;
2087 gchar c;
2089 s = str;
2091 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2092 s++;
2093 name = s;
2094 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
2095 GST_WARNING ("failed to parse simple string, str=%s", str);
2096 return FALSE;
2097 }
2099 s = name_end;
2100 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2101 s++;
2103 if (G_UNLIKELY (*s != '=')) {
2104 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2105 return FALSE;
2106 }
2107 s++;
2109 c = *name_end;
2110 *name_end = '\0';
2111 field->name = g_quark_from_string (name);
2112 GST_DEBUG ("trying field name '%s'", name);
2113 *name_end = c;
2115 if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
2116 G_TYPE_INVALID))) {
2117 GST_WARNING ("failed to parse value %s", str);
2118 return FALSE;
2119 }
2121 *after = s;
2122 return TRUE;
2123 }
2125 static gboolean
2126 gst_structure_parse_value (gchar * str,
2127 gchar ** after, GValue * value, GType default_type)
2128 {
2129 gchar *type_name;
2130 gchar *type_end;
2131 gchar *value_s;
2132 gchar *value_end;
2133 gchar *s;
2134 gchar c;
2135 int ret = 0;
2136 GType type = default_type;
2138 s = str;
2139 while (g_ascii_isspace (*s))
2140 s++;
2142 /* check if there's a (type_name) 'cast' */
2143 type_name = NULL;
2144 if (*s == '(') {
2145 s++;
2146 while (g_ascii_isspace (*s))
2147 s++;
2148 type_name = s;
2149 if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
2150 return FALSE;
2151 s = type_end;
2152 while (g_ascii_isspace (*s))
2153 s++;
2154 if (G_UNLIKELY (*s != ')'))
2155 return FALSE;
2156 s++;
2157 while (g_ascii_isspace (*s))
2158 s++;
2160 c = *type_end;
2161 *type_end = 0;
2162 type = gst_structure_gtype_from_abbr (type_name);
2163 GST_DEBUG ("trying type name '%s'", type_name);
2164 *type_end = c;
2166 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2167 GST_WARNING ("invalid type");
2168 return FALSE;
2169 }
2170 }
2172 while (g_ascii_isspace (*s))
2173 s++;
2174 if (*s == '[') {
2175 ret = gst_structure_parse_range (s, &s, value, type);
2176 } else if (*s == '{') {
2177 ret = gst_structure_parse_list (s, &s, value, type);
2178 } else if (*s == '<') {
2179 ret = gst_structure_parse_array (s, &s, value, type);
2180 } else {
2181 value_s = s;
2183 if (G_UNLIKELY (type == G_TYPE_INVALID)) {
2184 GType try_types[] =
2185 { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
2186 G_TYPE_STRING
2187 };
2188 int i;
2190 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
2191 return FALSE;
2192 /* Set NULL terminator for deserialization */
2193 c = *value_end;
2194 *value_end = '\0';
2196 for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
2197 g_value_init (value, try_types[i]);
2198 ret = gst_value_deserialize (value, value_s);
2199 if (ret)
2200 break;
2201 g_value_unset (value);
2202 }
2203 } else {
2204 g_value_init (value, type);
2206 if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
2207 (type != G_TYPE_STRING))))
2208 return FALSE;
2209 /* Set NULL terminator for deserialization */
2210 c = *value_end;
2211 *value_end = '\0';
2213 ret = gst_value_deserialize (value, value_s);
2214 if (G_UNLIKELY (!ret))
2215 g_value_unset (value);
2216 }
2217 *value_end = c;
2218 }
2220 *after = s;
2222 return ret;
2223 }
2225 /**
2226 * gst_structure_from_string:
2227 * @string: a string representation of a #GstStructure.
2228 * @end: (out) (allow-none): pointer to store the end of the string in.
2229 *
2230 * Creates a #GstStructure from a string representation.
2231 * If end is not NULL, a pointer to the place inside the given string
2232 * where parsing ended will be returned.
2233 *
2234 * Free-function: gst_structure_free
2235 *
2236 * Returns: (transfer full): a new #GstStructure or NULL when the string could
2237 * not be parsed. Free with gst_structure_free() after use.
2238 */
2239 GstStructure *
2240 gst_structure_from_string (const gchar * string, gchar ** end)
2241 {
2242 char *name;
2243 char *copy;
2244 char *w;
2245 char *r;
2246 char save;
2247 GstStructure *structure = NULL;
2248 GstStructureField field;
2250 g_return_val_if_fail (string != NULL, NULL);
2252 copy = g_strdup (string);
2253 r = copy;
2255 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2256 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2257 && g_ascii_isspace (r[1]))))
2258 r++;
2260 name = r;
2261 if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
2262 GST_WARNING ("Failed to parse structure string '%s'", string);
2263 goto error;
2264 }
2266 save = *w;
2267 *w = '\0';
2268 structure = gst_structure_empty_new (name);
2269 *w = save;
2271 if (G_UNLIKELY (structure == NULL))
2272 goto error;
2274 do {
2275 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2276 && g_ascii_isspace (r[1]))))
2277 r++;
2278 if (*r == ';') {
2279 /* end of structure, get the next char and finish */
2280 r++;
2281 break;
2282 }
2283 if (*r == '\0') {
2284 /* accept \0 as end delimiter */
2285 break;
2286 }
2287 if (G_UNLIKELY (*r != ',')) {
2288 GST_WARNING ("Failed to find delimiter, r=%s", r);
2289 goto error;
2290 }
2291 r++;
2292 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2293 && g_ascii_isspace (r[1]))))
2294 r++;
2296 memset (&field, 0, sizeof (field));
2297 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2298 GST_WARNING ("Failed to parse field, r=%s", r);
2299 goto error;
2300 }
2301 gst_structure_set_field (structure, &field);
2302 } while (TRUE);
2304 if (end)
2305 *end = (char *) string + (r - copy);
2306 else if (*r)
2307 g_warning ("gst_structure_from_string did not consume whole string,"
2308 " but caller did not provide end pointer (\"%s\")", string);
2310 g_free (copy);
2311 return structure;
2313 error:
2314 if (structure)
2315 gst_structure_free (structure);
2316 g_free (copy);
2317 return NULL;
2318 }
2320 static void
2321 gst_structure_transform_to_string (const GValue * src_value,
2322 GValue * dest_value)
2323 {
2324 g_return_if_fail (src_value != NULL);
2325 g_return_if_fail (dest_value != NULL);
2327 dest_value->data[0].v_pointer =
2328 gst_structure_to_string (src_value->data[0].v_pointer);
2329 }
2331 static GstStructure *
2332 gst_structure_copy_conditional (const GstStructure * structure)
2333 {
2334 if (structure)
2335 return gst_structure_copy (structure);
2336 return NULL;
2337 }
2339 /* fixate utility functions */
2341 /**
2342 * gst_structure_fixate_field_nearest_int:
2343 * @structure: a #GstStructure
2344 * @field_name: a field in @structure
2345 * @target: the target value of the fixation
2346 *
2347 * Fixates a #GstStructure by changing the given field to the nearest
2348 * integer to @target that is a subset of the existing field.
2349 *
2350 * Returns: TRUE if the structure could be fixated
2351 */
2352 gboolean
2353 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2354 const char *field_name, int target)
2355 {
2356 const GValue *value;
2358 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2359 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2361 value = gst_structure_get_value (structure, field_name);
2363 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2364 /* already fixed */
2365 return FALSE;
2366 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2367 int x;
2369 x = gst_value_get_int_range_min (value);
2370 if (target < x)
2371 target = x;
2372 x = gst_value_get_int_range_max (value);
2373 if (target > x)
2374 target = x;
2375 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2376 return TRUE;
2377 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2378 const GValue *list_value;
2379 int i, n;
2380 int best = 0;
2381 int best_index = -1;
2383 n = gst_value_list_get_size (value);
2384 for (i = 0; i < n; i++) {
2385 list_value = gst_value_list_get_value (value, i);
2386 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2387 int x = gst_g_value_get_int_unchecked (list_value);
2389 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2390 best_index = i;
2391 best = x;
2392 }
2393 }
2394 }
2395 if (best_index != -1) {
2396 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2397 return TRUE;
2398 }
2399 return FALSE;
2400 }
2402 return FALSE;
2403 }
2405 /**
2406 * gst_structure_fixate_field_nearest_double:
2407 * @structure: a #GstStructure
2408 * @field_name: a field in @structure
2409 * @target: the target value of the fixation
2410 *
2411 * Fixates a #GstStructure by changing the given field to the nearest
2412 * double to @target that is a subset of the existing field.
2413 *
2414 * Returns: TRUE if the structure could be fixated
2415 */
2416 gboolean
2417 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2418 const char *field_name, double target)
2419 {
2420 const GValue *value;
2422 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2423 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2425 value = gst_structure_get_value (structure, field_name);
2427 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2428 /* already fixed */
2429 return FALSE;
2430 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2431 double x;
2433 x = gst_value_get_double_range_min (value);
2434 if (target < x)
2435 target = x;
2436 x = gst_value_get_double_range_max (value);
2437 if (target > x)
2438 target = x;
2439 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2440 return TRUE;
2441 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2442 const GValue *list_value;
2443 int i, n;
2444 double best = 0;
2445 int best_index = -1;
2447 n = gst_value_list_get_size (value);
2448 for (i = 0; i < n; i++) {
2449 list_value = gst_value_list_get_value (value, i);
2450 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2451 double x = gst_g_value_get_double_unchecked (list_value);
2453 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2454 best_index = i;
2455 best = x;
2456 }
2457 }
2458 }
2459 if (best_index != -1) {
2460 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2461 return TRUE;
2462 }
2463 return FALSE;
2464 }
2466 return FALSE;
2468 }
2470 /**
2471 * gst_structure_fixate_field_boolean:
2472 * @structure: a #GstStructure
2473 * @field_name: a field in @structure
2474 * @target: the target value of the fixation
2475 *
2476 * Fixates a #GstStructure by changing the given @field_name field to the given
2477 * @target boolean if that field is not fixed yet.
2478 *
2479 * Returns: TRUE if the structure could be fixated
2480 */
2481 gboolean
2482 gst_structure_fixate_field_boolean (GstStructure * structure,
2483 const char *field_name, gboolean target)
2484 {
2485 const GValue *value;
2487 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2488 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2490 value = gst_structure_get_value (structure, field_name);
2492 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2493 /* already fixed */
2494 return FALSE;
2495 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2496 const GValue *list_value;
2497 int i, n;
2498 int best = 0;
2499 int best_index = -1;
2501 n = gst_value_list_get_size (value);
2502 for (i = 0; i < n; i++) {
2503 list_value = gst_value_list_get_value (value, i);
2504 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2505 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2507 if (best_index == -1 || x == target) {
2508 best_index = i;
2509 best = x;
2510 }
2511 }
2512 }
2513 if (best_index != -1) {
2514 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2515 return TRUE;
2516 }
2517 return FALSE;
2518 }
2520 return FALSE;
2521 }
2523 /**
2524 * gst_structure_fixate_field_string:
2525 * @structure: a #GstStructure
2526 * @field_name: a field in @structure
2527 * @target: the target value of the fixation
2528 *
2529 * Fixates a #GstStructure by changing the given @field_name field to the given
2530 * @target string if that field is not fixed yet.
2531 *
2532 * Returns: TRUE if the structure could be fixated
2533 *
2534 * Since: 0.10.30
2535 */
2536 gboolean
2537 gst_structure_fixate_field_string (GstStructure * structure,
2538 const gchar * field_name, const gchar * target)
2539 {
2540 const GValue *value;
2542 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2543 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2545 value = gst_structure_get_value (structure, field_name);
2547 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2548 /* already fixed */
2549 return FALSE;
2550 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2551 const GValue *list_value;
2552 int i, n;
2553 const gchar *best = NULL;
2554 int best_index = -1;
2556 n = gst_value_list_get_size (value);
2557 for (i = 0; i < n; i++) {
2558 list_value = gst_value_list_get_value (value, i);
2559 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2560 const gchar *x = g_value_get_string (list_value);
2562 if (best_index == -1 || g_str_equal (x, target)) {
2563 best_index = i;
2564 best = x;
2565 }
2566 }
2567 }
2568 if (best_index != -1) {
2569 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2570 return TRUE;
2571 }
2572 return FALSE;
2573 }
2575 return FALSE;
2576 }
2578 /**
2579 * gst_structure_fixate_field_nearest_fraction:
2580 * @structure: a #GstStructure
2581 * @field_name: a field in @structure
2582 * @target_numerator: The numerator of the target value of the fixation
2583 * @target_denominator: The denominator of the target value of the fixation
2584 *
2585 * Fixates a #GstStructure by changing the given field to the nearest
2586 * fraction to @target_numerator/@target_denominator that is a subset
2587 * of the existing field.
2588 *
2589 * Returns: TRUE if the structure could be fixated
2590 */
2591 gboolean
2592 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2593 const char *field_name, const gint target_numerator,
2594 const gint target_denominator)
2595 {
2596 const GValue *value;
2598 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2599 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2601 value = gst_structure_get_value (structure, field_name);
2603 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2604 /* already fixed */
2605 return FALSE;
2606 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2607 const GValue *x, *new_value;
2608 GValue target = { 0 };
2609 g_value_init (&target, GST_TYPE_FRACTION);
2610 gst_value_set_fraction (&target, target_numerator, target_denominator);
2612 new_value = ⌖
2613 x = gst_value_get_fraction_range_min (value);
2614 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2615 new_value = x;
2616 x = gst_value_get_fraction_range_max (value);
2617 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2618 new_value = x;
2620 gst_structure_set_value (structure, field_name, new_value);
2621 g_value_unset (&target);
2622 return TRUE;
2623 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2624 const GValue *list_value;
2625 int i, n;
2626 const GValue *best = NULL;
2627 gdouble target;
2628 gdouble cur_diff;
2629 gdouble best_diff = G_MAXDOUBLE;
2631 target = (gdouble) target_numerator / (gdouble) target_denominator;
2633 GST_DEBUG ("target %g, best %g", target, best_diff);
2635 best = NULL;
2637 n = gst_value_list_get_size (value);
2638 for (i = 0; i < n; i++) {
2639 list_value = gst_value_list_get_value (value, i);
2640 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2641 gint num, denom;
2642 gdouble list_double;
2644 num = gst_value_get_fraction_numerator (list_value);
2645 denom = gst_value_get_fraction_denominator (list_value);
2647 list_double = ((gdouble) num / (gdouble) denom);
2648 cur_diff = target - list_double;
2650 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2652 if (cur_diff < 0)
2653 cur_diff = -cur_diff;
2655 if (!best || cur_diff < best_diff) {
2656 GST_DEBUG ("new best %g", list_double);
2657 best = list_value;
2658 best_diff = cur_diff;
2659 }
2660 }
2661 }
2662 if (best != NULL) {
2663 gst_structure_set_value (structure, field_name, best);
2664 return TRUE;
2665 }
2666 }
2668 return FALSE;
2669 }
2671 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2672 * (useful for message parsing functions where the return location is user
2673 * supplied and the user may pass NULL if the value isn't of interest) */
2674 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2675 G_STMT_START { \
2676 const GValue *_value = (value); \
2677 guint _flags = (flags); \
2678 GType _value_type = G_VALUE_TYPE (_value); \
2679 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2680 gchar *_lcopy_format = _vtable->lcopy_format; \
2681 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2682 guint _n_values = 0; \
2683 \
2684 while (*_lcopy_format != '\0') { \
2685 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2686 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2687 _lcopy_format++; \
2688 } \
2689 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2690 *(__error) = g_strdup_printf ("either all or none of the return " \
2691 "locations for field '%s' need to be NULL", fieldname); \
2692 } else if (_cvalues[0].v_pointer != NULL) { \
2693 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2694 } \
2695 } G_STMT_END
2697 /**
2698 * gst_structure_get_valist:
2699 * @structure: a #GstStructure
2700 * @first_fieldname: the name of the first field to read
2701 * @args: variable arguments
2702 *
2703 * Parses the variable arguments and reads fields from @structure accordingly.
2704 * valist-variant of gst_structure_get(). Look at the documentation of
2705 * gst_structure_get() for more details.
2706 *
2707 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2708 *
2709 * Since: 0.10.24
2710 */
2711 gboolean
2712 gst_structure_get_valist (const GstStructure * structure,
2713 const char *first_fieldname, va_list args)
2714 {
2715 const char *field_name;
2716 GType expected_type = G_TYPE_INVALID;
2718 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2719 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2721 field_name = first_fieldname;
2722 while (field_name) {
2723 const GValue *val = NULL;
2724 gchar *err = NULL;
2726 expected_type = va_arg (args, GType);
2728 val = gst_structure_get_value (structure, field_name);
2730 if (val == NULL)
2731 goto no_such_field;
2733 if (G_VALUE_TYPE (val) != expected_type)
2734 goto wrong_type;
2736 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2737 if (err) {
2738 g_warning ("%s: %s", G_STRFUNC, err);
2739 g_free (err);
2740 return FALSE;
2741 }
2743 field_name = va_arg (args, const gchar *);
2744 }
2746 return TRUE;
2748 /* ERRORS */
2749 no_such_field:
2750 {
2751 GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2752 field_name, structure);
2753 return FALSE;
2754 }
2755 wrong_type:
2756 {
2757 GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2758 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2759 GST_STR_NULL (g_type_name (expected_type)),
2760 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2761 structure);
2762 return FALSE;
2763 }
2764 }
2766 /**
2767 * gst_structure_id_get_valist:
2768 * @structure: a #GstStructure
2769 * @first_field_id: the quark of the first field to read
2770 * @args: variable arguments
2771 *
2772 * Parses the variable arguments and reads fields from @structure accordingly.
2773 * valist-variant of gst_structure_id_get(). Look at the documentation of
2774 * gst_structure_id_get() for more details.
2775 *
2776 * Returns: TRUE, or FALSE if there was a problem reading any of the fields
2777 *
2778 * Since: 0.10.24
2779 */
2780 gboolean
2781 gst_structure_id_get_valist (const GstStructure * structure,
2782 GQuark first_field_id, va_list args)
2783 {
2784 GQuark field_id;
2785 GType expected_type = G_TYPE_INVALID;
2787 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2788 g_return_val_if_fail (first_field_id != 0, FALSE);
2790 field_id = first_field_id;
2791 while (field_id) {
2792 const GValue *val = NULL;
2793 gchar *err = NULL;
2795 expected_type = va_arg (args, GType);
2797 val = gst_structure_id_get_value (structure, field_id);
2799 if (val == NULL)
2800 goto no_such_field;
2802 if (G_VALUE_TYPE (val) != expected_type)
2803 goto wrong_type;
2805 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2806 if (err) {
2807 g_warning ("%s: %s", G_STRFUNC, err);
2808 g_free (err);
2809 return FALSE;
2810 }
2812 field_id = va_arg (args, GQuark);
2813 }
2815 return TRUE;
2817 /* ERRORS */
2818 no_such_field:
2819 {
2820 GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2821 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2822 return FALSE;
2823 }
2824 wrong_type:
2825 {
2826 GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
2827 "field was of type '%s': %" GST_PTR_FORMAT,
2828 g_quark_to_string (field_id),
2829 GST_STR_NULL (g_type_name (expected_type)),
2830 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2831 structure);
2832 return FALSE;
2833 }
2834 }
2836 /**
2837 * gst_structure_get:
2838 * @structure: a #GstStructure
2839 * @first_fieldname: the name of the first field to read
2840 * @...: variable arguments
2841 *
2842 * Parses the variable arguments and reads fields from @structure accordingly.
2843 * Variable arguments should be in the form field name, field type
2844 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2845 * The last variable argument should be NULL.
2846 *
2847 * For refcounted (mini)objects you will acquire your own reference which
2848 * you must release with a suitable _unref() when no longer needed. For
2849 * strings and boxed types you will acquire a copy which you will need to
2850 * release with either g_free() or the suiteable function for the boxed type.
2851 *
2852 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2853 * because the field requested did not exist, or was of a type other
2854 * than the type specified), otherwise TRUE.
2855 *
2856 * Since: 0.10.24
2857 */
2858 gboolean
2859 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
2860 ...)
2861 {
2862 gboolean ret;
2863 va_list args;
2865 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2866 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2868 va_start (args, first_fieldname);
2869 ret = gst_structure_get_valist (structure, first_fieldname, args);
2870 va_end (args);
2872 return ret;
2873 }
2875 /**
2876 * gst_structure_id_get:
2877 * @structure: a #GstStructure
2878 * @first_field_id: the quark of the first field to read
2879 * @...: variable arguments
2880 *
2881 * Parses the variable arguments and reads fields from @structure accordingly.
2882 * Variable arguments should be in the form field id quark, field type
2883 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
2884 * The last variable argument should be NULL (technically it should be a
2885 * 0 quark, but we require NULL so compilers that support it can check for
2886 * the NULL terminator and warn if it's not there).
2887 *
2888 * This function is just like gst_structure_get() only that it is slightly
2889 * more efficient since it saves the string-to-quark lookup in the global
2890 * quark hashtable.
2891 *
2892 * For refcounted (mini)objects you will acquire your own reference which
2893 * you must release with a suitable _unref() when no longer needed. For
2894 * strings and boxed types you will acquire a copy which you will need to
2895 * release with either g_free() or the suiteable function for the boxed type.
2896 *
2897 * Returns: FALSE if there was a problem reading any of the fields (e.g.
2898 * because the field requested did not exist, or was of a type other
2899 * than the type specified), otherwise TRUE.
2900 *
2901 * Since: 0.10.24
2902 */
2903 gboolean
2904 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
2905 ...)
2906 {
2907 gboolean ret;
2908 va_list args;
2910 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2911 g_return_val_if_fail (first_field_id != 0, FALSE);
2913 va_start (args, first_field_id);
2914 ret = gst_structure_id_get_valist (structure, first_field_id, args);
2915 va_end (args);
2917 return ret;
2918 }
2920 static gboolean
2921 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
2922 gpointer data)
2923 {
2924 const GstStructure *struct1 = (const GstStructure *) data;
2925 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
2927 if (G_UNLIKELY (val1 == NULL))
2928 return FALSE;
2929 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
2930 return TRUE;
2931 }
2933 return FALSE;
2934 }
2936 /**
2937 * gst_structure_is_equal:
2938 * @structure1: a #GstStructure.
2939 * @structure2: a #GstStructure.
2940 *
2941 * Tests if the two #GstStructure are equal.
2942 *
2943 * Returns: TRUE if the two structures have the same name and field.
2944 *
2945 * Since: 0.10.35
2946 **/
2947 gboolean
2948 gst_structure_is_equal (const GstStructure * structure1,
2949 const GstStructure * structure2)
2950 {
2951 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
2952 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
2954 if (structure1->name != structure2->name) {
2955 return FALSE;
2956 }
2957 if (structure1->fields->len != structure2->fields->len) {
2958 return FALSE;
2959 }
2961 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
2962 (gpointer) structure2);
2963 }
2966 typedef struct
2967 {
2968 GstStructure *dest;
2969 const GstStructure *intersect;
2970 }
2971 IntersectData;
2973 static gboolean
2974 gst_structure_intersect_field1 (GQuark id, const GValue * val1, gpointer data)
2975 {
2976 IntersectData *idata = (IntersectData *) data;
2977 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
2979 if (G_UNLIKELY (val2 == NULL)) {
2980 gst_structure_id_set_value (idata->dest, id, val1);
2981 } else {
2982 GValue dest_value = { 0 };
2983 if (gst_value_intersect (&dest_value, val1, val2)) {
2984 gst_structure_id_set_value (idata->dest, id, &dest_value);
2985 g_value_unset (&dest_value);
2986 } else {
2987 return FALSE;
2988 }
2989 }
2990 return TRUE;
2991 }
2993 static gboolean
2994 gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data)
2995 {
2996 IntersectData *idata = (IntersectData *) data;
2997 const GValue *val2 = gst_structure_id_get_value (idata->intersect, id);
2999 if (G_UNLIKELY (val2 == NULL)) {
3000 gst_structure_id_set_value (idata->dest, id, val1);
3001 }
3002 return TRUE;
3003 }
3005 /**
3006 * gst_structure_intersect:
3007 * @struct1: a #GstStructure
3008 * @struct2: a #GstStructure
3009 *
3010 * Interesects @struct1 and @struct2 and returns the intersection.
3011 *
3012 * Returns: Intersection of @struct1 and @struct2
3013 *
3014 * Since: 0.10.35
3015 */
3016 GstStructure *
3017 gst_structure_intersect (const GstStructure * struct1,
3018 const GstStructure * struct2)
3019 {
3020 IntersectData data;
3022 g_assert (struct1 != NULL);
3023 g_assert (struct2 != NULL);
3025 if (G_UNLIKELY (struct1->name != struct2->name))
3026 return NULL;
3028 /* copy fields from struct1 which we have not in struct2 to target
3029 * intersect if we have the field in both */
3030 data.dest = gst_structure_id_empty_new (struct1->name);
3031 data.intersect = struct2;
3032 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3033 gst_structure_intersect_field1, &data)))
3034 goto error;
3036 /* copy fields from struct2 which we have not in struct1 to target */
3037 data.intersect = struct1;
3038 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct2,
3039 gst_structure_intersect_field2, &data)))
3040 goto error;
3042 return data.dest;
3044 error:
3045 gst_structure_free (data.dest);
3046 return NULL;
3047 }
3049 static gboolean
3050 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3051 gpointer data)
3052 {
3053 GstStructure *other = (GstStructure *) data;
3054 const GValue *val2 = gst_structure_id_get_value (other, id);
3056 if (G_LIKELY (val2)) {
3057 if (!gst_value_can_intersect (val1, val2)) {
3058 return FALSE;
3059 } else {
3060 gint eq = gst_value_compare (val1, val2);
3062 if (eq == GST_VALUE_UNORDERED) {
3063 /* we need to try interseting */
3064 GValue dest_value = { 0 };
3065 if (gst_value_intersect (&dest_value, val1, val2)) {
3066 g_value_unset (&dest_value);
3067 } else {
3068 return FALSE;
3069 }
3070 } else if (eq != GST_VALUE_EQUAL) {
3071 return FALSE;
3072 }
3073 }
3074 }
3075 return TRUE;
3076 }
3078 /**
3079 * gst_structure_can_intersect:
3080 * @struct1: a #GstStructure
3081 * @struct2: a #GstStructure
3082 *
3083 * Tries interesecting @struct1 and @struct2 and reports whether the result
3084 * would not be empty.
3085 *
3086 * Returns: %TRUE if intersection would not be empty
3087 *
3088 * Since: 0.10.35
3089 */
3090 gboolean
3091 gst_structure_can_intersect (const GstStructure * struct1,
3092 const GstStructure * struct2)
3093 {
3094 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3095 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3097 if (G_UNLIKELY (struct1->name != struct2->name))
3098 return FALSE;
3100 /* tries to intersect if we have the field in both */
3101 if (G_UNLIKELY (!gst_structure_foreach ((GstStructure *) struct1,
3102 gst_caps_structure_can_intersect_field, (gpointer) struct2)))
3103 return FALSE;
3105 return TRUE;
3106 }