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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /**
23 * SECTION:gststructure
24 * @title: GstStructure
25 * @short_description: Generic structure containing fields of names and values
26 * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
27 *
28 * A #GstStructure is a collection of key/value pairs. The keys are expressed as
29 * GQuarks and the values can be of any GType.
30 *
31 * In addition to the key/value pairs, a #GstStructure also has a name. The name
32 * starts with a letter and can be filled by letters, numbers and any of
33 * "/-_.:".
34 *
35 * #GstStructure is used by various GStreamer subsystems to store information in
36 * a flexible and extensible way. A #GstStructure does not have a refcount
37 * because it usually is part of a higher level object such as #GstCaps,
38 * #GstMessage, #GstEvent, #GstQuery. It provides a means to enforce mutability
39 * using the refcount of the parent with the gst_structure_set_parent_refcount()
40 * method.
41 *
42 * A #GstStructure can be created with gst_structure_new_empty() or
43 * gst_structure_new(), which both take a name and an optional set of key/value
44 * pairs along with the types of the values.
45 *
46 * Field values can be changed with gst_structure_set_value() or
47 * gst_structure_set().
48 *
49 * Field values can be retrieved with gst_structure_get_value() or the more
50 * convenient gst_structure_get_*() functions.
51 *
52 * Fields can be removed with gst_structure_remove_field() or
53 * gst_structure_remove_fields().
54 *
55 * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are not
56 * allowed. Strings may be %NULL however.
57 *
58 * ## The serialization format
59 *
60 * GstStructure serialization format serialize the GstStructure name,
61 * keys/GType/values in a comma separated list with the structure name as first
62 * field without value followed by separated key/value pairs in the form
63 * `key=value`, for example:
64 *
65 * ```
66 * a-structure, key=value
67 * ````
68 *
69 * The values type will be inferred if not explicitly specified with the
70 * `(GTypeName)value` syntax, for example the following struct will have one
71 * field called 'is-string' which has the string 'true' as a value:
72 *
73 * ```
74 * a-struct, field-is-string=(string)true, field-is-boolean=true
75 * ```
76 *
77 * *Note*: without specifying `(string), `field-is-string` type would have been
78 * inferred as boolean.
79 *
80 * *Note*: we specified `(string)` as a type even if `gchararray` is the actual
81 * GType name as for convenience some well known types have been aliased or
82 * abbreviated.
83 *
84 * To avoid specifying the type, you can give some hints to the "type system".
85 * For example to specify a value as a double, you should add a decimal (ie. `1`
86 * is an `int` while `1.0` is a `double`).
87 *
88 * *Note*: when a structure is serialized with #gst_structure_to_string, all
89 * values are explicitly typed.
90 *
91 * Some types have special delimiters:
92 *
93 * - [GstValueArray](GST_TYPE_ARRAY) are inside curly brackets (`{` and `}`).
94 * For example `a-structure, array={1, 2, 3}`
95 * - Ranges are inside brackets (`[` and `]`). For example `a-structure,
96 * range=[1, 6, 2]` 1 being the min value, 6 the maximum and 2 the step. To
97 * specify a #GST_TYPE_INT64_RANGE you need to explicitly specify it like:
98 * `a-structure, a-int64-range=(gint64) [1, 5]`
99 * - [GstValueList](GST_TYPE_LIST) are inside "less and greater than" (`<` and
100 * `>`). For example `a-structure, list=<1, 2, 3>
101 *
102 * Structures are delimited either by a null character `\0` or a semicolon `;`
103 * the latter allowing to store multiple structures in the same string (see
104 * #GstCaps).
105 *
106 * Quotes are used as "default" delimiters and can be used around any types that
107 * don't use other delimiters (for example `a-struct, i=(int)"1"`). They are use
108 * to allow adding spaces or special characters (such as delimiters,
109 * semicolumns, etc..) inside strings and you can use backslashes `\` to escape
110 * characters inside them, for example:
111 *
112 * ```
113 * a-struct, special="\"{[(;)]}\" can be used inside quotes"
114 * ```
115 *
116 * They also allow for nested structure, such as:
117 *
118 * ```
119 * a-struct, nested=(GstStructure)"nested-struct, nested=true"
120 * ```
121 *
122 * Since 1.20, nested structures and caps can be specified using brackets (`[`
123 * and `]`), for example:
124 *
125 * ```
126 * a-struct, nested=[nested-struct, nested=true]
127 * ```
128 *
129 * > *note*: gst_structure_to_string() won't use that syntax for backward
130 * > compatibility reason, gst_structure_serialize() has been added for
131 * > that purpose.
132 */
133
134 #ifdef HAVE_CONFIG_H
135 #include "config.h"
136 #endif
137
138 /* FIXME 2.0: suppress warnings for deprecated API such as GValueArray
139 * with newer GLib versions (>= 2.31.0) */
140 #define GLIB_DISABLE_DEPRECATION_WARNINGS
141
142 #include <string.h>
143
144 #include "gst_private.h"
145 #include "gstquark.h"
146 #include <gst/gst.h>
147 #include <gobject/gvaluecollector.h>
148
149 GST_DEBUG_CATEGORY_STATIC (gst_structure_debug);
150 #define GST_CAT_DEFAULT gst_structure_debug
151
152 typedef struct _GstStructureField GstStructureField;
153
154 struct _GstStructureField
155 {
156 GQuark name;
157 GValue value;
158 };
159
160 typedef struct
161 {
162 GstStructure s;
163
164 /* owned by parent structure, NULL if no parent */
165 gint *parent_refcount;
166
167 guint fields_len; /* Number of valid items in fields */
168 guint fields_alloc; /* Allocated items in fields */
169
170 /* Fields are allocated if GST_STRUCTURE_IS_USING_DYNAMIC_ARRAY(),
171 * else it's a pointer to the arr field. */
172 GstStructureField *fields;
173
174 GstStructureField arr[1];
175 } GstStructureImpl;
176
177 #define GST_STRUCTURE_REFCOUNT(s) (((GstStructureImpl*)(s))->parent_refcount)
178 #define GST_STRUCTURE_LEN(s) (((GstStructureImpl*)(s))->fields_len)
179
180 #define GST_STRUCTURE_IS_USING_DYNAMIC_ARRAY(s) \
181 (((GstStructureImpl*)(s))->fields != &((GstStructureImpl*)(s))->arr[0])
182
183 #define GST_STRUCTURE_FIELD(structure, index) \
184 (&((GstStructureImpl*)(structure))->fields[(index)])
185
186 #define IS_MUTABLE(structure) \
187 (!GST_STRUCTURE_REFCOUNT(structure) || \
188 g_atomic_int_get (GST_STRUCTURE_REFCOUNT(structure)) == 1)
189
190 #define IS_TAGLIST(structure) \
191 (structure->name == GST_QUARK (TAGLIST))
192
193 /* Replacement for g_array_append_val */
194 static void
_structure_append_val(GstStructure * s,GstStructureField * val)195 _structure_append_val (GstStructure * s, GstStructureField * val)
196 {
197 GstStructureImpl *impl = (GstStructureImpl *) s;
198
199 /* resize if needed */
200 if (G_UNLIKELY (impl->fields_len == impl->fields_alloc)) {
201 guint want_alloc;
202
203 if (G_UNLIKELY (impl->fields_alloc > (G_MAXUINT / 2)))
204 g_error ("Growing structure would result in overflow");
205
206 want_alloc =
207 MAX (GST_ROUND_UP_8 (impl->fields_len + 1), impl->fields_alloc * 2);
208 if (GST_STRUCTURE_IS_USING_DYNAMIC_ARRAY (s)) {
209 impl->fields = g_renew (GstStructureField, impl->fields, want_alloc);
210 } else {
211 impl->fields = g_new0 (GstStructureField, want_alloc);
212 memcpy (impl->fields, &impl->arr[0],
213 impl->fields_len * sizeof (GstStructureField));
214 GST_CAT_LOG (GST_CAT_PERFORMANCE, "Exceeding pre-allocated array");
215 }
216 impl->fields_alloc = want_alloc;
217 }
218
219 /* Finally set value */
220 impl->fields[impl->fields_len++] = *val;
221 }
222
223 /* Replacement for g_array_remove_index */
224 static inline void
_structure_remove_index(GstStructure * s,guint idx)225 _structure_remove_index (GstStructure * s, guint idx)
226 {
227 GstStructureImpl *impl = (GstStructureImpl *) s;
228
229 /* We never "reduce" the memory footprint. */
230 if (idx >= impl->fields_len)
231 return;
232
233 /* Shift everything if it's not the last item */
234 if (idx != impl->fields_len)
235 memmove (&impl->fields[idx],
236 &impl->fields[idx + 1],
237 (impl->fields_len - idx - 1) * sizeof (GstStructureField));
238 impl->fields_len--;
239 }
240
241 static void gst_structure_set_field (GstStructure * structure,
242 GstStructureField * field);
243 static GstStructureField *gst_structure_get_field (const GstStructure *
244 structure, const gchar * fieldname);
245 static GstStructureField *gst_structure_id_get_field (const GstStructure *
246 structure, GQuark field);
247 static void gst_structure_transform_to_string (const GValue * src_value,
248 GValue * dest_value);
249 static GstStructure *gst_structure_copy_conditional (const GstStructure *
250 structure);
251
252 GType _gst_structure_type = 0;
253
254
255 G_DEFINE_BOXED_TYPE (GstStructure, gst_structure,
256 gst_structure_copy_conditional, gst_structure_free);
257
258 void
_priv_gst_structure_initialize(void)259 _priv_gst_structure_initialize (void)
260 {
261 _gst_structure_type = gst_structure_get_type ();
262
263 g_value_register_transform_func (_gst_structure_type, G_TYPE_STRING,
264 gst_structure_transform_to_string);
265
266 GST_DEBUG_CATEGORY_INIT (gst_structure_debug, "structure", 0,
267 "GstStructure debug");
268 }
269
270 static GstStructure *
gst_structure_new_id_empty_with_size(GQuark quark,guint prealloc)271 gst_structure_new_id_empty_with_size (GQuark quark, guint prealloc)
272 {
273 guint n_alloc;
274 GstStructureImpl *structure;
275
276 if (prealloc == 0)
277 prealloc = 1;
278
279 n_alloc = GST_ROUND_UP_8 (prealloc);
280 structure =
281 g_malloc0 (sizeof (GstStructureImpl) + (n_alloc -
282 1) * sizeof (GstStructureField));
283
284 ((GstStructure *) structure)->type = _gst_structure_type;
285 ((GstStructure *) structure)->name = quark;
286 GST_STRUCTURE_REFCOUNT (structure) = NULL;
287
288 structure->fields_len = 0;
289 structure->fields_alloc = n_alloc;
290 structure->fields = &structure->arr[0];
291
292 GST_TRACE ("created structure %p", structure);
293
294 return GST_STRUCTURE_CAST (structure);
295 }
296
297 /**
298 * gst_structure_new_id_empty:
299 * @quark: name of new structure
300 *
301 * Creates a new, empty #GstStructure with the given name as a GQuark.
302 *
303 * Free-function: gst_structure_free
304 *
305 * Returns: (transfer full): a new, empty #GstStructure
306 */
307 GstStructure *
gst_structure_new_id_empty(GQuark quark)308 gst_structure_new_id_empty (GQuark quark)
309 {
310 g_return_val_if_fail (quark != 0, NULL);
311
312 return gst_structure_new_id_empty_with_size (quark, 0);
313 }
314
315 static gboolean
gst_structure_validate_name(const gchar * name)316 gst_structure_validate_name (const gchar * name)
317 {
318 const gchar *s;
319
320 g_return_val_if_fail (name != NULL, FALSE);
321
322 if (G_UNLIKELY (!g_ascii_isalpha (*name))) {
323 GST_WARNING ("Invalid character '%c' at offset 0 in structure name: %s",
324 *name, name);
325 return FALSE;
326 }
327
328 /* FIXME: test name string more */
329 s = &name[1];
330 while (*s && (g_ascii_isalnum (*s) || strchr ("/-_.:+", *s) != NULL))
331 s++;
332 if (G_UNLIKELY (*s != '\0')) {
333 GST_WARNING ("Invalid character '%c' at offset %" G_GUINTPTR_FORMAT " in"
334 " structure name: %s", *s, ((guintptr) s - (guintptr) name), name);
335 return FALSE;
336 }
337
338 if (strncmp (name, "video/x-raw-", 12) == 0) {
339 g_warning ("0.10-style raw video caps are being created. Should be "
340 "video/x-raw,format=(string).. now.");
341 } else if (strncmp (name, "audio/x-raw-", 12) == 0) {
342 g_warning ("0.10-style raw audio caps are being created. Should be "
343 "audio/x-raw,format=(string).. now.");
344 }
345
346 return TRUE;
347 }
348
349 /**
350 * gst_structure_new_empty:
351 * @name: name of new structure
352 *
353 * Creates a new, empty #GstStructure with the given @name.
354 *
355 * See gst_structure_set_name() for constraints on the @name parameter.
356 *
357 * Free-function: gst_structure_free
358 *
359 * Returns: (transfer full): a new, empty #GstStructure
360 */
361 GstStructure *
gst_structure_new_empty(const gchar * name)362 gst_structure_new_empty (const gchar * name)
363 {
364 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
365
366 return gst_structure_new_id_empty_with_size (g_quark_from_string (name), 0);
367 }
368
369 /**
370 * gst_structure_new:
371 * @name: name of new structure
372 * @firstfield: name of first field to set
373 * @...: additional arguments
374 *
375 * Creates a new #GstStructure with the given name. Parses the
376 * list of variable arguments and sets fields to the values listed.
377 * Variable arguments should be passed as field name, field type,
378 * and value. Last variable argument should be %NULL.
379 *
380 * Free-function: gst_structure_free
381 *
382 * Returns: (transfer full): a new #GstStructure
383 */
384 GstStructure *
gst_structure_new(const gchar * name,const gchar * firstfield,...)385 gst_structure_new (const gchar * name, const gchar * firstfield, ...)
386 {
387 GstStructure *structure;
388 va_list varargs;
389
390 va_start (varargs, firstfield);
391 structure = gst_structure_new_valist (name, firstfield, varargs);
392 va_end (varargs);
393
394 return structure;
395 }
396
397 /**
398 * gst_structure_new_valist:
399 * @name: name of new structure
400 * @firstfield: name of first field to set
401 * @varargs: variable argument list
402 *
403 * Creates a new #GstStructure with the given @name. Structure fields
404 * are set according to the varargs in a manner similar to
405 * gst_structure_new().
406 *
407 * See gst_structure_set_name() for constraints on the @name parameter.
408 *
409 * Free-function: gst_structure_free
410 *
411 * Returns: (transfer full): a new #GstStructure
412 */
413 GstStructure *
gst_structure_new_valist(const gchar * name,const gchar * firstfield,va_list varargs)414 gst_structure_new_valist (const gchar * name,
415 const gchar * firstfield, va_list varargs)
416 {
417 GstStructure *structure;
418 va_list copy;
419 guint len = 0;
420 const gchar *field_copy = firstfield;
421 GType type_copy;
422
423 g_return_val_if_fail (gst_structure_validate_name (name), NULL);
424
425 /* Calculate size of varargs */
426 va_copy (copy, varargs);
427 while (field_copy) {
428 type_copy = va_arg (copy, GType);
429 G_VALUE_COLLECT_SKIP (type_copy, copy);
430 field_copy = va_arg (copy, gchar *);
431 len++;
432 }
433 va_end (copy);
434
435 structure =
436 gst_structure_new_id_empty_with_size (g_quark_from_string (name), len);
437
438 if (structure)
439 gst_structure_set_valist (structure, firstfield, varargs);
440
441 return structure;
442 }
443
444 /**
445 * gst_structure_set_parent_refcount:
446 * @structure: a #GstStructure
447 * @refcount: (in): a pointer to the parent's refcount
448 *
449 * Sets the parent_refcount field of #GstStructure. This field is used to
450 * determine whether a structure is mutable or not. This function should only be
451 * called by code implementing parent objects of #GstStructure, as described in
452 * the MT Refcounting section of the design documents.
453 *
454 * Returns: %TRUE if the parent refcount could be set.
455 */
456 gboolean
gst_structure_set_parent_refcount(GstStructure * structure,gint * refcount)457 gst_structure_set_parent_refcount (GstStructure * structure, gint * refcount)
458 {
459 g_return_val_if_fail (structure != NULL, FALSE);
460
461 /* if we have a parent_refcount already, we can only clear
462 * if with a NULL refcount */
463 if (GST_STRUCTURE_REFCOUNT (structure)) {
464 if (refcount != NULL) {
465 g_return_val_if_fail (refcount == NULL, FALSE);
466 return FALSE;
467 }
468 } else {
469 if (refcount == NULL) {
470 g_return_val_if_fail (refcount != NULL, FALSE);
471 return FALSE;
472 }
473 }
474
475 GST_STRUCTURE_REFCOUNT (structure) = refcount;
476
477 return TRUE;
478 }
479
480 /**
481 * gst_structure_copy:
482 * @structure: a #GstStructure to duplicate
483 *
484 * Duplicates a #GstStructure and all its fields and values.
485 *
486 * Free-function: gst_structure_free
487 *
488 * Returns: (transfer full): a new #GstStructure.
489 */
490 GstStructure *
gst_structure_copy(const GstStructure * structure)491 gst_structure_copy (const GstStructure * structure)
492 {
493 GstStructure *new_structure;
494 GstStructureField *field;
495 guint i, len;
496
497 g_return_val_if_fail (structure != NULL, NULL);
498
499 len = GST_STRUCTURE_LEN (structure);
500 new_structure = gst_structure_new_id_empty_with_size (structure->name, len);
501
502 for (i = 0; i < len; i++) {
503 GstStructureField new_field = { 0 };
504
505 field = GST_STRUCTURE_FIELD (structure, i);
506
507 new_field.name = field->name;
508 gst_value_init_and_copy (&new_field.value, &field->value);
509 _structure_append_val (new_structure, &new_field);
510 }
511 GST_CAT_TRACE (GST_CAT_PERFORMANCE, "doing copy %p -> %p",
512 structure, new_structure);
513
514 return new_structure;
515 }
516
517 /**
518 * gst_structure_free:
519 * @structure: (in) (transfer full): the #GstStructure to free
520 *
521 * Frees a #GstStructure and all its fields and values. The structure must not
522 * have a parent when this function is called.
523 */
524 void
gst_structure_free(GstStructure * structure)525 gst_structure_free (GstStructure * structure)
526 {
527 GstStructureField *field;
528 guint i, len;
529
530 g_return_if_fail (structure != NULL);
531 g_return_if_fail (GST_STRUCTURE_REFCOUNT (structure) == NULL);
532
533 len = GST_STRUCTURE_LEN (structure);
534 for (i = 0; i < len; i++) {
535 field = GST_STRUCTURE_FIELD (structure, i);
536
537 if (G_IS_VALUE (&field->value)) {
538 g_value_unset (&field->value);
539 }
540 }
541 if (GST_STRUCTURE_IS_USING_DYNAMIC_ARRAY (structure))
542 g_free (((GstStructureImpl *) structure)->fields);
543
544 #ifdef USE_POISONING
545 memset (structure, 0xff, sizeof (GstStructure));
546 #endif
547 GST_TRACE ("free structure %p", structure);
548
549 g_free (structure);
550 }
551
552 /**
553 * gst_clear_structure: (skip)
554 * @structure_ptr: a pointer to a #GstStructure reference
555 *
556 * Clears a reference to a #GstStructure.
557 *
558 * @structure_ptr must not be %NULL.
559 *
560 * If the reference is %NULL then this function does nothing.
561 * Otherwise, the structure is free'd using gst_structure_free() and the
562 * pointer is set to %NULL.
563 *
564 * A macro is also included that allows this function to be used without
565 * pointer casts.
566 *
567 * Since: 1.16
568 **/
569 #undef gst_clear_structure
570 void
gst_clear_structure(GstStructure ** structure_ptr)571 gst_clear_structure (GstStructure ** structure_ptr)
572 {
573 g_clear_pointer (structure_ptr, gst_structure_free);
574 }
575
576 /**
577 * gst_structure_take:
578 * @oldstr_ptr: (inout) (transfer full) (nullable): pointer to a place of
579 * a #GstStructure to take
580 * @newstr: (transfer full) (allow-none): a new #GstStructure
581 *
582 * Atomically modifies a pointer to point to a new structure.
583 * The #GstStructure @oldstr_ptr is pointing to is freed and
584 * @newstr is taken ownership over.
585 *
586 * Either @newstr and the value pointed to by @oldstr_ptr may be %NULL.
587 *
588 * It is a programming error if both @newstr and the value pointed to by
589 * @oldstr_ptr refer to the same, non-%NULL structure.
590 *
591 * Returns: %TRUE if @newstr was different from @oldstr_ptr
592 *
593 * Since: 1.18
594 */
595 gboolean
gst_structure_take(GstStructure ** oldstr_ptr,GstStructure * newstr)596 gst_structure_take (GstStructure ** oldstr_ptr, GstStructure * newstr)
597 {
598 GstStructure *oldstr;
599
600 g_return_val_if_fail (oldstr_ptr != NULL, FALSE);
601
602 do {
603 oldstr = g_atomic_pointer_get ((gpointer *) oldstr_ptr);
604 if (G_UNLIKELY (oldstr == newstr)) {
605 g_return_val_if_fail (newstr == NULL, FALSE);
606 return FALSE;
607 }
608 } while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
609 oldstr_ptr, (gpointer) oldstr, newstr)));
610
611 if (oldstr)
612 gst_structure_free (oldstr);
613
614 return TRUE;
615 }
616
617 /**
618 * gst_structure_get_name:
619 * @structure: a #GstStructure
620 *
621 * Get the name of @structure as a string.
622 *
623 * Returns: the name of the structure.
624 */
625 const gchar *
gst_structure_get_name(const GstStructure * structure)626 gst_structure_get_name (const GstStructure * structure)
627 {
628 g_return_val_if_fail (structure != NULL, NULL);
629
630 return g_quark_to_string (structure->name);
631 }
632
633 /**
634 * gst_structure_has_name:
635 * @structure: a #GstStructure
636 * @name: structure name to check for
637 *
638 * Checks if the structure has the given name
639 *
640 * Returns: %TRUE if @name matches the name of the structure.
641 */
642 gboolean
gst_structure_has_name(const GstStructure * structure,const gchar * name)643 gst_structure_has_name (const GstStructure * structure, const gchar * name)
644 {
645 const gchar *structure_name;
646
647 g_return_val_if_fail (structure != NULL, FALSE);
648 g_return_val_if_fail (name != NULL, FALSE);
649
650 /* getting the string is cheap and comparing short strings is too
651 * should be faster than getting the quark for name and comparing the quarks
652 */
653 structure_name = g_quark_to_string (structure->name);
654
655 return (structure_name && strcmp (structure_name, name) == 0);
656 }
657
658 /**
659 * gst_structure_get_name_id:
660 * @structure: a #GstStructure
661 *
662 * Get the name of @structure as a GQuark.
663 *
664 * Returns: the quark representing the name of the structure.
665 */
666 GQuark
gst_structure_get_name_id(const GstStructure * structure)667 gst_structure_get_name_id (const GstStructure * structure)
668 {
669 g_return_val_if_fail (structure != NULL, 0);
670
671 return structure->name;
672 }
673
674 /**
675 * gst_structure_set_name:
676 * @structure: a #GstStructure
677 * @name: the new name of the structure
678 *
679 * Sets the name of the structure to the given @name. The string
680 * provided is copied before being used. It must not be empty, start with a
681 * letter and can be followed by letters, numbers and any of "/-_.:".
682 */
683 void
gst_structure_set_name(GstStructure * structure,const gchar * name)684 gst_structure_set_name (GstStructure * structure, const gchar * name)
685 {
686 g_return_if_fail (structure != NULL);
687 g_return_if_fail (IS_MUTABLE (structure));
688 g_return_if_fail (gst_structure_validate_name (name));
689
690 structure->name = g_quark_from_string (name);
691 }
692
693 static inline void
gst_structure_id_set_value_internal(GstStructure * structure,GQuark field,const GValue * value)694 gst_structure_id_set_value_internal (GstStructure * structure, GQuark field,
695 const GValue * value)
696 {
697 GstStructureField gsfield = { 0, {0,} };
698
699 gsfield.name = field;
700 gst_value_init_and_copy (&gsfield.value, value);
701
702 gst_structure_set_field (structure, &gsfield);
703 }
704
705 /**
706 * gst_structure_id_set_value:
707 * @structure: a #GstStructure
708 * @field: a #GQuark representing a field
709 * @value: the new value of the field
710 *
711 * Sets the field with the given GQuark @field to @value. If the field
712 * does not exist, it is created. If the field exists, the previous
713 * value is replaced and freed.
714 */
715 void
gst_structure_id_set_value(GstStructure * structure,GQuark field,const GValue * value)716 gst_structure_id_set_value (GstStructure * structure,
717 GQuark field, const GValue * value)
718 {
719
720 g_return_if_fail (structure != NULL);
721 g_return_if_fail (G_IS_VALUE (value));
722 g_return_if_fail (IS_MUTABLE (structure));
723
724 gst_structure_id_set_value_internal (structure, field, value);
725 }
726
727 /**
728 * gst_structure_set_value:
729 * @structure: a #GstStructure
730 * @fieldname: the name of the field to set
731 * @value: the new value of the field
732 *
733 * Sets the field with the given name @field to @value. If the field
734 * does not exist, it is created. If the field exists, the previous
735 * value is replaced and freed.
736 */
737 void
gst_structure_set_value(GstStructure * structure,const gchar * fieldname,const GValue * value)738 gst_structure_set_value (GstStructure * structure,
739 const gchar * fieldname, const GValue * value)
740 {
741 g_return_if_fail (structure != NULL);
742 g_return_if_fail (fieldname != NULL);
743 g_return_if_fail (G_IS_VALUE (value));
744 g_return_if_fail (IS_MUTABLE (structure));
745
746 gst_structure_id_set_value_internal (structure,
747 g_quark_from_string (fieldname), value);
748 }
749
750 static inline void
gst_structure_id_take_value_internal(GstStructure * structure,GQuark field,GValue * value)751 gst_structure_id_take_value_internal (GstStructure * structure, GQuark field,
752 GValue * value)
753 {
754 GstStructureField gsfield = { 0, {0,} };
755
756 gsfield.name = field;
757 gsfield.value = *value;
758
759 gst_structure_set_field (structure, &gsfield);
760
761 /* we took ownership */
762 #ifdef USE_POISONING
763 memset (value, 0, sizeof (GValue));
764 #else
765 value->g_type = G_TYPE_INVALID;
766 #endif
767 }
768
769 /**
770 * gst_structure_id_take_value:
771 * @structure: a #GstStructure
772 * @field: a #GQuark representing a field
773 * @value: (transfer full): the new value of the field
774 *
775 * Sets the field with the given GQuark @field to @value. If the field
776 * does not exist, it is created. If the field exists, the previous
777 * value is replaced and freed.
778 */
779 void
gst_structure_id_take_value(GstStructure * structure,GQuark field,GValue * value)780 gst_structure_id_take_value (GstStructure * structure, GQuark field,
781 GValue * value)
782 {
783 g_return_if_fail (structure != NULL);
784 g_return_if_fail (G_IS_VALUE (value));
785 g_return_if_fail (IS_MUTABLE (structure));
786
787 gst_structure_id_take_value_internal (structure, field, value);
788 }
789
790 /**
791 * gst_structure_take_value:
792 * @structure: a #GstStructure
793 * @fieldname: the name of the field to set
794 * @value: (transfer full): the new value of the field
795 *
796 * Sets the field with the given name @field to @value. If the field
797 * does not exist, it is created. If the field exists, the previous
798 * value is replaced and freed. The function will take ownership of @value.
799 */
800 void
gst_structure_take_value(GstStructure * structure,const gchar * fieldname,GValue * value)801 gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
802 GValue * value)
803 {
804 g_return_if_fail (structure != NULL);
805 g_return_if_fail (fieldname != NULL);
806 g_return_if_fail (G_IS_VALUE (value));
807 g_return_if_fail (IS_MUTABLE (structure));
808
809 gst_structure_id_take_value_internal (structure,
810 g_quark_from_string (fieldname), value);
811 }
812
813 static void
gst_structure_set_valist_internal(GstStructure * structure,const gchar * fieldname,va_list varargs)814 gst_structure_set_valist_internal (GstStructure * structure,
815 const gchar * fieldname, va_list varargs)
816 {
817 gchar *err = NULL;
818 GType type;
819
820 while (fieldname) {
821 GstStructureField field = { 0 };
822
823 field.name = g_quark_from_string (fieldname);
824
825 type = va_arg (varargs, GType);
826
827 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
828 if (G_UNLIKELY (err)) {
829 g_critical ("%s", err);
830 g_free (err);
831 return;
832 }
833 gst_structure_set_field (structure, &field);
834
835 fieldname = va_arg (varargs, gchar *);
836 }
837 }
838
839 /**
840 * gst_structure_set:
841 * @structure: a #GstStructure
842 * @fieldname: the name of the field to set
843 * @...: variable arguments
844 *
845 * Parses the variable arguments and sets fields accordingly. Fields that
846 * weren't already part of the structure are added as needed.
847 * Variable arguments should be in the form field name, field type
848 * (as a GType), value(s). The last variable argument should be %NULL.
849 */
850 void
gst_structure_set(GstStructure * structure,const gchar * field,...)851 gst_structure_set (GstStructure * structure, const gchar * field, ...)
852 {
853 va_list varargs;
854
855 g_return_if_fail (structure != NULL);
856 g_return_if_fail (IS_MUTABLE (structure) || field == NULL);
857
858 va_start (varargs, field);
859 gst_structure_set_valist_internal (structure, field, varargs);
860 va_end (varargs);
861 }
862
863 /**
864 * gst_structure_set_valist:
865 * @structure: a #GstStructure
866 * @fieldname: the name of the field to set
867 * @varargs: variable arguments
868 *
869 * va_list form of gst_structure_set().
870 */
871 void
gst_structure_set_valist(GstStructure * structure,const gchar * fieldname,va_list varargs)872 gst_structure_set_valist (GstStructure * structure,
873 const gchar * fieldname, va_list varargs)
874 {
875 g_return_if_fail (structure != NULL);
876 g_return_if_fail (IS_MUTABLE (structure));
877
878 gst_structure_set_valist_internal (structure, fieldname, varargs);
879 }
880
881 static void
gst_structure_id_set_valist_internal(GstStructure * structure,GQuark fieldname,va_list varargs)882 gst_structure_id_set_valist_internal (GstStructure * structure,
883 GQuark fieldname, va_list varargs)
884 {
885 gchar *err = NULL;
886 GType type;
887
888 while (fieldname) {
889 GstStructureField field = { 0 };
890
891 field.name = fieldname;
892 type = va_arg (varargs, GType);
893
894 G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
895 if (G_UNLIKELY (err)) {
896 g_critical ("%s", err);
897 g_free (err);
898 return;
899 }
900 gst_structure_set_field (structure, &field);
901
902 fieldname = va_arg (varargs, GQuark);
903 }
904 }
905
906 /**
907 * gst_structure_id_set:
908 * @structure: a #GstStructure
909 * @fieldname: the GQuark for the name of the field to set
910 * @...: variable arguments
911 *
912 * Identical to gst_structure_set, except that field names are
913 * passed using the GQuark for the field name. This allows more efficient
914 * setting of the structure if the caller already knows the associated
915 * quark values.
916 * The last variable argument must be %NULL.
917 */
918 void
gst_structure_id_set(GstStructure * structure,GQuark field,...)919 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
920 {
921 va_list varargs;
922
923 g_return_if_fail (structure != NULL);
924
925 va_start (varargs, field);
926 gst_structure_id_set_valist_internal (structure, field, varargs);
927 va_end (varargs);
928 }
929
930 /**
931 * gst_structure_id_set_valist:
932 * @structure: a #GstStructure
933 * @fieldname: the name of the field to set
934 * @varargs: variable arguments
935 *
936 * va_list form of gst_structure_id_set().
937 */
938 void
gst_structure_id_set_valist(GstStructure * structure,GQuark fieldname,va_list varargs)939 gst_structure_id_set_valist (GstStructure * structure,
940 GQuark fieldname, va_list varargs)
941 {
942 g_return_if_fail (structure != NULL);
943 g_return_if_fail (IS_MUTABLE (structure));
944
945 gst_structure_id_set_valist_internal (structure, fieldname, varargs);
946 }
947
948 /**
949 * gst_structure_new_id:
950 * @name_quark: name of new structure
951 * @field_quark: the GQuark for the name of the field to set
952 * @...: variable arguments
953 *
954 * Creates a new #GstStructure with the given name as a GQuark, followed by
955 * fieldname quark, GType, argument(s) "triplets" in the same format as
956 * gst_structure_id_set(). Basically a convenience wrapper around
957 * gst_structure_new_id_empty() and gst_structure_id_set().
958 *
959 * The last variable argument must be %NULL (or 0).
960 *
961 * Free-function: gst_structure_free
962 *
963 * Returns: (transfer full): a new #GstStructure
964 */
965 GstStructure *
gst_structure_new_id(GQuark name_quark,GQuark field_quark,...)966 gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
967 {
968 GstStructure *s;
969 va_list varargs;
970 va_list copy;
971 guint len = 0;
972 GQuark quark_copy = field_quark;
973 GType type_copy;
974
975 g_return_val_if_fail (name_quark != 0, NULL);
976 g_return_val_if_fail (field_quark != 0, NULL);
977
978 va_start (varargs, field_quark);
979
980 /* Calculate size of varargs */
981 va_copy (copy, varargs);
982 while (quark_copy) {
983 type_copy = va_arg (copy, GType);
984 G_VALUE_COLLECT_SKIP (type_copy, copy);
985 quark_copy = va_arg (copy, GQuark);
986 len++;
987 }
988 va_end (copy);
989
990 s = gst_structure_new_id_empty_with_size (name_quark, len);
991
992 gst_structure_id_set_valist_internal (s, field_quark, varargs);
993 va_end (varargs);
994
995 return s;
996 }
997
998 #if GST_VERSION_NANO == 1
999 #define GIT_G_WARNING g_warning
1000 #else
1001 #define GIT_G_WARNING GST_WARNING
1002 #endif
1003
1004 /* If the structure currently contains a field with the same name, it is
1005 * replaced with the provided field. Otherwise, the field is added to the
1006 * structure. The field's value is not deeply copied.
1007 */
1008 static void
gst_structure_set_field(GstStructure * structure,GstStructureField * field)1009 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
1010 {
1011 GstStructureField *f;
1012 GType field_value_type;
1013 guint i, len;
1014
1015 len = GST_STRUCTURE_LEN (structure);
1016
1017 field_value_type = G_VALUE_TYPE (&field->value);
1018 if (field_value_type == G_TYPE_STRING) {
1019 const gchar *s;
1020
1021 s = g_value_get_string (&field->value);
1022 /* only check for NULL strings in taglists, as they are allowed in message
1023 * structs, e.g. error message debug strings */
1024 if (G_UNLIKELY (IS_TAGLIST (structure) && (s == NULL || *s == '\0'))) {
1025 if (s == NULL) {
1026 GIT_G_WARNING ("Trying to set NULL string on field '%s' on taglist. "
1027 "Please file a bug.", g_quark_to_string (field->name));
1028 g_value_unset (&field->value);
1029 return;
1030 } else {
1031 /* empty strings never make sense */
1032 GIT_G_WARNING ("Trying to set empty string on taglist field '%s'. "
1033 "Please file a bug.", g_quark_to_string (field->name));
1034 g_value_unset (&field->value);
1035 return;
1036 }
1037 } else if (G_UNLIKELY (s != NULL && !g_utf8_validate (s, -1, NULL))) {
1038 g_warning ("Trying to set string on %s field '%s', but string is not "
1039 "valid UTF-8. Please file a bug.",
1040 IS_TAGLIST (structure) ? "taglist" : "structure",
1041 g_quark_to_string (field->name));
1042 g_value_unset (&field->value);
1043 return;
1044 }
1045 } else if (G_UNLIKELY (field_value_type == G_TYPE_DATE)) {
1046 const GDate *d;
1047
1048 d = g_value_get_boxed (&field->value);
1049 /* only check for NULL GDates in taglists, as they might make sense
1050 * in other, generic structs */
1051 if (G_UNLIKELY ((IS_TAGLIST (structure) && d == NULL))) {
1052 GIT_G_WARNING ("Trying to set NULL GDate on field '%s' on taglist. "
1053 "Please file a bug.", g_quark_to_string (field->name));
1054 g_value_unset (&field->value);
1055 return;
1056 } else if (G_UNLIKELY (d != NULL && !g_date_valid (d))) {
1057 g_warning
1058 ("Trying to set invalid GDate on %s field '%s'. Please file a bug.",
1059 IS_TAGLIST (structure) ? "taglist" : "structure",
1060 g_quark_to_string (field->name));
1061 g_value_unset (&field->value);
1062 return;
1063 }
1064 }
1065
1066 for (i = 0; i < len; i++) {
1067 f = GST_STRUCTURE_FIELD (structure, i);
1068
1069 if (G_UNLIKELY (f->name == field->name)) {
1070 g_value_unset (&f->value);
1071 memcpy (f, field, sizeof (GstStructureField));
1072 return;
1073 }
1074 }
1075
1076 _structure_append_val (structure, field);
1077 }
1078
1079 /* If there is no field with the given ID, NULL is returned.
1080 */
1081 static GstStructureField *
gst_structure_id_get_field(const GstStructure * structure,GQuark field_id)1082 gst_structure_id_get_field (const GstStructure * structure, GQuark field_id)
1083 {
1084 GstStructureField *field;
1085 guint i, len;
1086
1087 len = GST_STRUCTURE_LEN (structure);
1088
1089 for (i = 0; i < len; i++) {
1090 field = GST_STRUCTURE_FIELD (structure, i);
1091
1092 if (G_UNLIKELY (field->name == field_id))
1093 return field;
1094 }
1095
1096 return NULL;
1097 }
1098
1099 /* If there is no field with the given ID, NULL is returned.
1100 */
1101 static GstStructureField *
gst_structure_get_field(const GstStructure * structure,const gchar * fieldname)1102 gst_structure_get_field (const GstStructure * structure,
1103 const gchar * fieldname)
1104 {
1105 g_return_val_if_fail (structure != NULL, NULL);
1106 g_return_val_if_fail (fieldname != NULL, NULL);
1107
1108 return gst_structure_id_get_field (structure,
1109 g_quark_from_string (fieldname));
1110 }
1111
1112 /**
1113 * gst_structure_get_value:
1114 * @structure: a #GstStructure
1115 * @fieldname: the name of the field to get
1116 *
1117 * Get the value of the field with name @fieldname.
1118 *
1119 * Returns: (nullable): the #GValue corresponding to the field with the given
1120 * name.
1121 */
1122 const GValue *
gst_structure_get_value(const GstStructure * structure,const gchar * fieldname)1123 gst_structure_get_value (const GstStructure * structure,
1124 const gchar * fieldname)
1125 {
1126 GstStructureField *field;
1127
1128 g_return_val_if_fail (structure != NULL, NULL);
1129 g_return_val_if_fail (fieldname != NULL, NULL);
1130
1131 field = gst_structure_get_field (structure, fieldname);
1132 if (field == NULL)
1133 return NULL;
1134
1135 return &field->value;
1136 }
1137
1138 /**
1139 * gst_structure_id_get_value:
1140 * @structure: a #GstStructure
1141 * @field: the #GQuark of the field to get
1142 *
1143 * Get the value of the field with GQuark @field.
1144 *
1145 * Returns: (nullable): the #GValue corresponding to the field with the given
1146 * name identifier.
1147 */
1148 const GValue *
gst_structure_id_get_value(const GstStructure * structure,GQuark field)1149 gst_structure_id_get_value (const GstStructure * structure, GQuark field)
1150 {
1151 GstStructureField *gsfield;
1152
1153 g_return_val_if_fail (structure != NULL, NULL);
1154
1155 gsfield = gst_structure_id_get_field (structure, field);
1156 if (gsfield == NULL)
1157 return NULL;
1158
1159 return &gsfield->value;
1160 }
1161
1162 /**
1163 * gst_structure_remove_field:
1164 * @structure: a #GstStructure
1165 * @fieldname: the name of the field to remove
1166 *
1167 * Removes the field with the given name. If the field with the given
1168 * name does not exist, the structure is unchanged.
1169 */
1170 void
gst_structure_remove_field(GstStructure * structure,const gchar * fieldname)1171 gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
1172 {
1173 GstStructureField *field;
1174 GQuark id;
1175 guint i, len;
1176
1177 g_return_if_fail (structure != NULL);
1178 g_return_if_fail (fieldname != NULL);
1179 g_return_if_fail (IS_MUTABLE (structure));
1180
1181 id = g_quark_from_string (fieldname);
1182 len = GST_STRUCTURE_LEN (structure);
1183
1184 for (i = 0; i < len; i++) {
1185 field = GST_STRUCTURE_FIELD (structure, i);
1186
1187 if (field->name == id) {
1188 if (G_IS_VALUE (&field->value)) {
1189 g_value_unset (&field->value);
1190 }
1191 _structure_remove_index (structure, i);
1192 return;
1193 }
1194 }
1195 }
1196
1197 /**
1198 * gst_structure_remove_fields:
1199 * @structure: a #GstStructure
1200 * @fieldname: the name of the field to remove
1201 * @...: %NULL-terminated list of more fieldnames to remove
1202 *
1203 * Removes the fields with the given names. If a field does not exist, the
1204 * argument is ignored.
1205 */
1206 void
gst_structure_remove_fields(GstStructure * structure,const gchar * fieldname,...)1207 gst_structure_remove_fields (GstStructure * structure,
1208 const gchar * fieldname, ...)
1209 {
1210 va_list varargs;
1211
1212 g_return_if_fail (structure != NULL);
1213 g_return_if_fail (fieldname != NULL);
1214 /* mutability checked in remove_field */
1215
1216 va_start (varargs, fieldname);
1217 gst_structure_remove_fields_valist (structure, fieldname, varargs);
1218 va_end (varargs);
1219 }
1220
1221 /**
1222 * gst_structure_remove_fields_valist:
1223 * @structure: a #GstStructure
1224 * @fieldname: the name of the field to remove
1225 * @varargs: %NULL-terminated list of more fieldnames to remove
1226 *
1227 * va_list form of gst_structure_remove_fields().
1228 */
1229 void
gst_structure_remove_fields_valist(GstStructure * structure,const gchar * fieldname,va_list varargs)1230 gst_structure_remove_fields_valist (GstStructure * structure,
1231 const gchar * fieldname, va_list varargs)
1232 {
1233 gchar *field = (gchar *) fieldname;
1234
1235 g_return_if_fail (structure != NULL);
1236 g_return_if_fail (fieldname != NULL);
1237 /* mutability checked in remove_field */
1238
1239 while (field) {
1240 gst_structure_remove_field (structure, field);
1241 field = va_arg (varargs, char *);
1242 }
1243 }
1244
1245 /**
1246 * gst_structure_remove_all_fields:
1247 * @structure: a #GstStructure
1248 *
1249 * Removes all fields in a GstStructure.
1250 */
1251 void
gst_structure_remove_all_fields(GstStructure * structure)1252 gst_structure_remove_all_fields (GstStructure * structure)
1253 {
1254 GstStructureField *field;
1255 int i;
1256
1257 g_return_if_fail (structure != NULL);
1258 g_return_if_fail (IS_MUTABLE (structure));
1259
1260 for (i = GST_STRUCTURE_LEN (structure) - 1; i >= 0; i--) {
1261 field = GST_STRUCTURE_FIELD (structure, i);
1262
1263 if (G_IS_VALUE (&field->value)) {
1264 g_value_unset (&field->value);
1265 }
1266 _structure_remove_index (structure, i);
1267 }
1268 }
1269
1270 /**
1271 * gst_structure_get_field_type:
1272 * @structure: a #GstStructure
1273 * @fieldname: the name of the field
1274 *
1275 * Finds the field with the given name, and returns the type of the
1276 * value it contains. If the field is not found, G_TYPE_INVALID is
1277 * returned.
1278 *
1279 * Returns: the #GValue of the field
1280 */
1281 GType
gst_structure_get_field_type(const GstStructure * structure,const gchar * fieldname)1282 gst_structure_get_field_type (const GstStructure * structure,
1283 const gchar * fieldname)
1284 {
1285 GstStructureField *field;
1286
1287 g_return_val_if_fail (structure != NULL, G_TYPE_INVALID);
1288 g_return_val_if_fail (fieldname != NULL, G_TYPE_INVALID);
1289
1290 field = gst_structure_get_field (structure, fieldname);
1291 if (field == NULL)
1292 return G_TYPE_INVALID;
1293
1294 return G_VALUE_TYPE (&field->value);
1295 }
1296
1297 /**
1298 * gst_structure_n_fields:
1299 * @structure: a #GstStructure
1300 *
1301 * Get the number of fields in the structure.
1302 *
1303 * Returns: the number of fields in the structure
1304 */
1305 gint
gst_structure_n_fields(const GstStructure * structure)1306 gst_structure_n_fields (const GstStructure * structure)
1307 {
1308 g_return_val_if_fail (structure != NULL, 0);
1309
1310 return GST_STRUCTURE_LEN (structure);
1311 }
1312
1313 /**
1314 * gst_structure_nth_field_name:
1315 * @structure: a #GstStructure
1316 * @index: the index to get the name of
1317 *
1318 * Get the name of the given field number, counting from 0 onwards.
1319 *
1320 * Returns: the name of the given field number
1321 */
1322 const gchar *
gst_structure_nth_field_name(const GstStructure * structure,guint index)1323 gst_structure_nth_field_name (const GstStructure * structure, guint index)
1324 {
1325 GstStructureField *field;
1326
1327 g_return_val_if_fail (structure != NULL, NULL);
1328 g_return_val_if_fail (index < GST_STRUCTURE_LEN (structure), NULL);
1329
1330 field = GST_STRUCTURE_FIELD (structure, index);
1331
1332 return g_quark_to_string (field->name);
1333 }
1334
1335 /**
1336 * gst_structure_foreach:
1337 * @structure: a #GstStructure
1338 * @func: (scope call): a function to call for each field
1339 * @user_data: (closure): private data
1340 *
1341 * Calls the provided function once for each field in the #GstStructure. The
1342 * function must not modify the fields. Also see gst_structure_map_in_place()
1343 * and gst_structure_filter_and_map_in_place().
1344 *
1345 * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
1346 * %FALSE otherwise.
1347 */
1348 gboolean
gst_structure_foreach(const GstStructure * structure,GstStructureForeachFunc func,gpointer user_data)1349 gst_structure_foreach (const GstStructure * structure,
1350 GstStructureForeachFunc func, gpointer user_data)
1351 {
1352 guint i, len;
1353 GstStructureField *field;
1354 gboolean ret;
1355
1356 g_return_val_if_fail (structure != NULL, FALSE);
1357 g_return_val_if_fail (func != NULL, FALSE);
1358
1359 len = GST_STRUCTURE_LEN (structure);
1360
1361 for (i = 0; i < len; i++) {
1362 field = GST_STRUCTURE_FIELD (structure, i);
1363
1364 ret = func (field->name, &field->value, user_data);
1365 if (G_UNLIKELY (!ret))
1366 return FALSE;
1367 }
1368
1369 return TRUE;
1370 }
1371
1372 /**
1373 * gst_structure_map_in_place:
1374 * @structure: a #GstStructure
1375 * @func: (scope call): a function to call for each field
1376 * @user_data: (closure): private data
1377 *
1378 * Calls the provided function once for each field in the #GstStructure. In
1379 * contrast to gst_structure_foreach(), the function may modify but not delete the
1380 * fields. The structure must be mutable.
1381 *
1382 * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
1383 * %FALSE otherwise.
1384 */
1385 gboolean
gst_structure_map_in_place(GstStructure * structure,GstStructureMapFunc func,gpointer user_data)1386 gst_structure_map_in_place (GstStructure * structure,
1387 GstStructureMapFunc func, gpointer user_data)
1388 {
1389 guint i, len;
1390 GstStructureField *field;
1391 gboolean ret;
1392
1393 g_return_val_if_fail (structure != NULL, FALSE);
1394 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
1395 g_return_val_if_fail (func != NULL, FALSE);
1396 len = GST_STRUCTURE_LEN (structure);
1397
1398 for (i = 0; i < len; i++) {
1399 field = GST_STRUCTURE_FIELD (structure, i);
1400
1401 ret = func (field->name, &field->value, user_data);
1402 if (!ret)
1403 return FALSE;
1404 }
1405
1406 return TRUE;
1407 }
1408
1409 /**
1410 * gst_structure_filter_and_map_in_place:
1411 * @structure: a #GstStructure
1412 * @func: (scope call): a function to call for each field
1413 * @user_data: (closure): private data
1414 *
1415 * Calls the provided function once for each field in the #GstStructure. In
1416 * contrast to gst_structure_foreach(), the function may modify the fields.
1417 * In contrast to gst_structure_map_in_place(), the field is removed from
1418 * the structure if %FALSE is returned from the function.
1419 * The structure must be mutable.
1420 *
1421 * Since: 1.6
1422 */
1423 void
gst_structure_filter_and_map_in_place(GstStructure * structure,GstStructureFilterMapFunc func,gpointer user_data)1424 gst_structure_filter_and_map_in_place (GstStructure * structure,
1425 GstStructureFilterMapFunc func, gpointer user_data)
1426 {
1427 guint i, len;
1428 GstStructureField *field;
1429 gboolean ret;
1430
1431 g_return_if_fail (structure != NULL);
1432 g_return_if_fail (IS_MUTABLE (structure));
1433 g_return_if_fail (func != NULL);
1434 len = GST_STRUCTURE_LEN (structure);
1435
1436 for (i = 0; i < len;) {
1437 field = GST_STRUCTURE_FIELD (structure, i);
1438
1439 ret = func (field->name, &field->value, user_data);
1440
1441 if (!ret) {
1442 if (G_IS_VALUE (&field->value)) {
1443 g_value_unset (&field->value);
1444 }
1445 _structure_remove_index (structure, i);
1446 len = GST_STRUCTURE_LEN (structure);
1447 } else {
1448 i++;
1449 }
1450 }
1451 }
1452
1453 /**
1454 * gst_structure_id_has_field:
1455 * @structure: a #GstStructure
1456 * @field: #GQuark of the field name
1457 *
1458 * Check if @structure contains a field named @field.
1459 *
1460 * Returns: %TRUE if the structure contains a field with the given name
1461 */
1462 gboolean
gst_structure_id_has_field(const GstStructure * structure,GQuark field)1463 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
1464 {
1465 GstStructureField *f;
1466
1467 g_return_val_if_fail (structure != NULL, FALSE);
1468 g_return_val_if_fail (field != 0, FALSE);
1469
1470 f = gst_structure_id_get_field (structure, field);
1471
1472 return (f != NULL);
1473 }
1474
1475 /**
1476 * gst_structure_has_field:
1477 * @structure: a #GstStructure
1478 * @fieldname: the name of a field
1479 *
1480 * Check if @structure contains a field named @fieldname.
1481 *
1482 * Returns: %TRUE if the structure contains a field with the given name
1483 */
1484 gboolean
gst_structure_has_field(const GstStructure * structure,const gchar * fieldname)1485 gst_structure_has_field (const GstStructure * structure,
1486 const gchar * fieldname)
1487 {
1488 g_return_val_if_fail (structure != NULL, FALSE);
1489 g_return_val_if_fail (fieldname != NULL, FALSE);
1490
1491 return gst_structure_id_has_field (structure,
1492 g_quark_from_string (fieldname));
1493 }
1494
1495 /**
1496 * gst_structure_id_has_field_typed:
1497 * @structure: a #GstStructure
1498 * @field: #GQuark of the field name
1499 * @type: the type of a value
1500 *
1501 * Check if @structure contains a field named @field and with GType @type.
1502 *
1503 * Returns: %TRUE if the structure contains a field with the given name and type
1504 */
1505 gboolean
gst_structure_id_has_field_typed(const GstStructure * structure,GQuark field,GType type)1506 gst_structure_id_has_field_typed (const GstStructure * structure,
1507 GQuark field, GType type)
1508 {
1509 GstStructureField *f;
1510
1511 g_return_val_if_fail (structure != NULL, FALSE);
1512 g_return_val_if_fail (field != 0, FALSE);
1513
1514 f = gst_structure_id_get_field (structure, field);
1515 if (f == NULL)
1516 return FALSE;
1517
1518 return (G_VALUE_TYPE (&f->value) == type);
1519 }
1520
1521 /**
1522 * gst_structure_has_field_typed:
1523 * @structure: a #GstStructure
1524 * @fieldname: the name of a field
1525 * @type: the type of a value
1526 *
1527 * Check if @structure contains a field named @fieldname and with GType @type.
1528 *
1529 * Returns: %TRUE if the structure contains a field with the given name and type
1530 */
1531 gboolean
gst_structure_has_field_typed(const GstStructure * structure,const gchar * fieldname,GType type)1532 gst_structure_has_field_typed (const GstStructure * structure,
1533 const gchar * fieldname, GType type)
1534 {
1535 g_return_val_if_fail (structure != NULL, FALSE);
1536 g_return_val_if_fail (fieldname != NULL, FALSE);
1537
1538 return gst_structure_id_has_field_typed (structure,
1539 g_quark_from_string (fieldname), type);
1540 }
1541
1542 /* utility functions */
1543
1544 /**
1545 * gst_structure_get_boolean:
1546 * @structure: a #GstStructure
1547 * @fieldname: the name of a field
1548 * @value: (out): a pointer to a #gboolean to set
1549 *
1550 * Sets the boolean pointed to by @value corresponding to the value of the
1551 * given field. Caller is responsible for making sure the field exists
1552 * and has the correct type.
1553 *
1554 * Returns: %TRUE if the value could be set correctly. If there was no field
1555 * with @fieldname or the existing field did not contain a boolean, this
1556 * function returns %FALSE.
1557 */
1558 gboolean
gst_structure_get_boolean(const GstStructure * structure,const gchar * fieldname,gboolean * value)1559 gst_structure_get_boolean (const GstStructure * structure,
1560 const gchar * fieldname, gboolean * value)
1561 {
1562 GstStructureField *field;
1563
1564 g_return_val_if_fail (structure != NULL, FALSE);
1565 g_return_val_if_fail (fieldname != NULL, FALSE);
1566
1567 field = gst_structure_get_field (structure, fieldname);
1568
1569 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_BOOLEAN)
1570 return FALSE;
1571
1572 *value = gst_g_value_get_boolean_unchecked (&field->value);
1573
1574 return TRUE;
1575 }
1576
1577 /**
1578 * gst_structure_get_int:
1579 * @structure: a #GstStructure
1580 * @fieldname: the name of a field
1581 * @value: (out): a pointer to an int to set
1582 *
1583 * Sets the int pointed to by @value corresponding to the value of the
1584 * given field. Caller is responsible for making sure the field exists
1585 * and has the correct type.
1586 *
1587 * Returns: %TRUE if the value could be set correctly. If there was no field
1588 * with @fieldname or the existing field did not contain an int, this function
1589 * returns %FALSE.
1590 */
1591 gboolean
gst_structure_get_int(const GstStructure * structure,const gchar * fieldname,gint * value)1592 gst_structure_get_int (const GstStructure * structure,
1593 const gchar * fieldname, gint * value)
1594 {
1595 GstStructureField *field;
1596
1597 g_return_val_if_fail (structure != NULL, FALSE);
1598 g_return_val_if_fail (fieldname != NULL, FALSE);
1599 g_return_val_if_fail (value != NULL, FALSE);
1600
1601 field = gst_structure_get_field (structure, fieldname);
1602
1603 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT)
1604 return FALSE;
1605
1606 *value = gst_g_value_get_int_unchecked (&field->value);
1607
1608 return TRUE;
1609 }
1610
1611 /**
1612 * gst_structure_get_uint:
1613 * @structure: a #GstStructure
1614 * @fieldname: the name of a field
1615 * @value: (out): a pointer to a uint to set
1616 *
1617 * Sets the uint pointed to by @value corresponding to the value of the
1618 * given field. Caller is responsible for making sure the field exists
1619 * and has the correct type.
1620 *
1621 * Returns: %TRUE if the value could be set correctly. If there was no field
1622 * with @fieldname or the existing field did not contain a uint, this function
1623 * returns %FALSE.
1624 */
1625 gboolean
gst_structure_get_uint(const GstStructure * structure,const gchar * fieldname,guint * value)1626 gst_structure_get_uint (const GstStructure * structure,
1627 const gchar * fieldname, guint * value)
1628 {
1629 GstStructureField *field;
1630
1631 g_return_val_if_fail (structure != NULL, FALSE);
1632 g_return_val_if_fail (fieldname != NULL, FALSE);
1633 g_return_val_if_fail (value != NULL, FALSE);
1634
1635 field = gst_structure_get_field (structure, fieldname);
1636
1637 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT)
1638 return FALSE;
1639
1640 *value = gst_g_value_get_uint_unchecked (&field->value);
1641
1642 return TRUE;
1643 }
1644
1645 /**
1646 * gst_structure_get_int64:
1647 * @structure: a #GstStructure
1648 * @fieldname: the name of a field
1649 * @value: (out): a pointer to a #gint64 to set
1650 *
1651 * Sets the #gint64 pointed to by @value corresponding to the value of the
1652 * given field. Caller is responsible for making sure the field exists
1653 * and has the correct type.
1654 *
1655 * Returns: %TRUE if the value could be set correctly. If there was no field
1656 * with @fieldname or the existing field did not contain a #gint64, this function
1657 * returns %FALSE.
1658 *
1659 * Since: 1.4
1660 */
1661 gboolean
gst_structure_get_int64(const GstStructure * structure,const gchar * fieldname,gint64 * value)1662 gst_structure_get_int64 (const GstStructure * structure,
1663 const gchar * fieldname, gint64 * value)
1664 {
1665 GstStructureField *field;
1666
1667 g_return_val_if_fail (structure != NULL, FALSE);
1668 g_return_val_if_fail (fieldname != NULL, FALSE);
1669 g_return_val_if_fail (value != NULL, FALSE);
1670
1671 field = gst_structure_get_field (structure, fieldname);
1672
1673 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT64)
1674 return FALSE;
1675
1676 *value = gst_g_value_get_int64_unchecked (&field->value);
1677
1678 return TRUE;
1679 }
1680
1681 /**
1682 * gst_structure_get_uint64:
1683 * @structure: a #GstStructure
1684 * @fieldname: the name of a field
1685 * @value: (out): a pointer to a #guint64 to set
1686 *
1687 * Sets the #guint64 pointed to by @value corresponding to the value of the
1688 * given field. Caller is responsible for making sure the field exists
1689 * and has the correct type.
1690 *
1691 * Returns: %TRUE if the value could be set correctly. If there was no field
1692 * with @fieldname or the existing field did not contain a #guint64, this function
1693 * returns %FALSE.
1694 *
1695 * Since: 1.4
1696 */
1697 gboolean
gst_structure_get_uint64(const GstStructure * structure,const gchar * fieldname,guint64 * value)1698 gst_structure_get_uint64 (const GstStructure * structure,
1699 const gchar * fieldname, guint64 * value)
1700 {
1701 GstStructureField *field;
1702
1703 g_return_val_if_fail (structure != NULL, FALSE);
1704 g_return_val_if_fail (fieldname != NULL, FALSE);
1705 g_return_val_if_fail (value != NULL, FALSE);
1706
1707 field = gst_structure_get_field (structure, fieldname);
1708
1709 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT64)
1710 return FALSE;
1711
1712 *value = gst_g_value_get_uint64_unchecked (&field->value);
1713
1714 return TRUE;
1715 }
1716
1717 /**
1718 * gst_structure_get_date:
1719 * @structure: a #GstStructure
1720 * @fieldname: the name of a field
1721 * @value: (out callee-allocates): a pointer to a #GDate to set
1722 *
1723 * Sets the date pointed to by @value corresponding to the date of the
1724 * given field. Caller is responsible for making sure the field exists
1725 * and has the correct type.
1726 *
1727 * On success @value will point to a newly-allocated copy of the date which
1728 * should be freed with g_date_free() when no longer needed (note: this is
1729 * inconsistent with e.g. gst_structure_get_string() which doesn't return a
1730 * copy of the string).
1731 *
1732 * Returns: %TRUE if the value could be set correctly. If there was no field
1733 * with @fieldname or the existing field did not contain a data, this function
1734 * returns %FALSE.
1735 */
1736 gboolean
gst_structure_get_date(const GstStructure * structure,const gchar * fieldname,GDate ** value)1737 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
1738 GDate ** value)
1739 {
1740 GstStructureField *field;
1741
1742 g_return_val_if_fail (structure != NULL, FALSE);
1743 g_return_val_if_fail (fieldname != NULL, FALSE);
1744 g_return_val_if_fail (value != NULL, FALSE);
1745
1746 field = gst_structure_get_field (structure, fieldname);
1747
1748 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DATE)
1749 return FALSE;
1750
1751 /* FIXME: 2.0 g_value_dup_boxed() -> g_value_get_boxed() */
1752 *value = g_value_dup_boxed (&field->value);
1753
1754 return TRUE;
1755 }
1756
1757 /**
1758 * gst_structure_get_date_time:
1759 * @structure: a #GstStructure
1760 * @fieldname: the name of a field
1761 * @value: (out callee-allocates): a pointer to a #GstDateTime to set
1762 *
1763 * Sets the datetime pointed to by @value corresponding to the datetime of the
1764 * given field. Caller is responsible for making sure the field exists
1765 * and has the correct type.
1766 *
1767 * On success @value will point to a reference of the datetime which
1768 * should be unreffed with gst_date_time_unref() when no longer needed
1769 * (note: this is inconsistent with e.g. gst_structure_get_string()
1770 * which doesn't return a copy of the string).
1771 *
1772 * Returns: %TRUE if the value could be set correctly. If there was no field
1773 * with @fieldname or the existing field did not contain a data, this function
1774 * returns %FALSE.
1775 */
1776 gboolean
gst_structure_get_date_time(const GstStructure * structure,const gchar * fieldname,GstDateTime ** value)1777 gst_structure_get_date_time (const GstStructure * structure,
1778 const gchar * fieldname, GstDateTime ** value)
1779 {
1780 GstStructureField *field;
1781
1782 g_return_val_if_fail (structure != NULL, FALSE);
1783 g_return_val_if_fail (fieldname != NULL, FALSE);
1784 g_return_val_if_fail (value != NULL, FALSE);
1785
1786 field = gst_structure_get_field (structure, fieldname);
1787
1788 if (field == NULL)
1789 return FALSE;
1790 if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
1791 return FALSE;
1792
1793 /* FIXME 2.0: g_value_dup_boxed() -> g_value_get_boxed() */
1794 *value = g_value_dup_boxed (&field->value);
1795
1796 return TRUE;
1797 }
1798
1799 /**
1800 * gst_structure_get_clock_time:
1801 * @structure: a #GstStructure
1802 * @fieldname: the name of a field
1803 * @value: (out): a pointer to a #GstClockTime to set
1804 *
1805 * Sets the clock time pointed to by @value corresponding to the clock time
1806 * of the given field. Caller is responsible for making sure the field exists
1807 * and has the correct type.
1808 *
1809 * Returns: %TRUE if the value could be set correctly. If there was no field
1810 * with @fieldname or the existing field did not contain a #GstClockTime, this
1811 * function returns %FALSE.
1812 */
1813 gboolean
gst_structure_get_clock_time(const GstStructure * structure,const gchar * fieldname,GstClockTime * value)1814 gst_structure_get_clock_time (const GstStructure * structure,
1815 const gchar * fieldname, GstClockTime * value)
1816 {
1817 return gst_structure_get_uint64 (structure, fieldname, value);
1818 }
1819
1820 /**
1821 * gst_structure_get_double:
1822 * @structure: a #GstStructure
1823 * @fieldname: the name of a field
1824 * @value: (out): a pointer to a gdouble to set
1825 *
1826 * Sets the double pointed to by @value corresponding to the value of the
1827 * given field. Caller is responsible for making sure the field exists
1828 * and has the correct type.
1829 *
1830 * Returns: %TRUE if the value could be set correctly. If there was no field
1831 * with @fieldname or the existing field did not contain a double, this
1832 * function returns %FALSE.
1833 */
1834 gboolean
gst_structure_get_double(const GstStructure * structure,const gchar * fieldname,gdouble * value)1835 gst_structure_get_double (const GstStructure * structure,
1836 const gchar * fieldname, gdouble * value)
1837 {
1838 GstStructureField *field;
1839
1840 g_return_val_if_fail (structure != NULL, FALSE);
1841 g_return_val_if_fail (fieldname != NULL, FALSE);
1842 g_return_val_if_fail (value != NULL, FALSE);
1843
1844 field = gst_structure_get_field (structure, fieldname);
1845
1846 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DOUBLE)
1847 return FALSE;
1848
1849 *value = gst_g_value_get_double_unchecked (&field->value);
1850
1851 return TRUE;
1852 }
1853
1854 /**
1855 * gst_structure_get_string:
1856 * @structure: a #GstStructure
1857 * @fieldname: the name of a field
1858 *
1859 * Finds the field corresponding to @fieldname, and returns the string
1860 * contained in the field's value. Caller is responsible for making
1861 * sure the field exists and has the correct type.
1862 *
1863 * The string should not be modified, and remains valid until the next
1864 * call to a gst_structure_*() function with the given structure.
1865 *
1866 * Returns: (nullable): a pointer to the string or %NULL when the
1867 * field did not exist or did not contain a string.
1868 */
1869 const gchar *
gst_structure_get_string(const GstStructure * structure,const gchar * fieldname)1870 gst_structure_get_string (const GstStructure * structure,
1871 const gchar * fieldname)
1872 {
1873 GstStructureField *field;
1874
1875 g_return_val_if_fail (structure != NULL, NULL);
1876 g_return_val_if_fail (fieldname != NULL, NULL);
1877
1878 field = gst_structure_get_field (structure, fieldname);
1879
1880 if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_STRING)
1881 return NULL;
1882
1883 return gst_g_value_get_string_unchecked (&field->value);
1884 }
1885
1886 /**
1887 * gst_structure_get_enum:
1888 * @structure: a #GstStructure
1889 * @fieldname: the name of a field
1890 * @enumtype: the enum type of a field
1891 * @value: (out): a pointer to an int to set
1892 *
1893 * Sets the int pointed to by @value corresponding to the value of the
1894 * given field. Caller is responsible for making sure the field exists,
1895 * has the correct type and that the enumtype is correct.
1896 *
1897 * Returns: %TRUE if the value could be set correctly. If there was no field
1898 * with @fieldname or the existing field did not contain an enum of the given
1899 * type, this function returns %FALSE.
1900 */
1901 gboolean
gst_structure_get_enum(const GstStructure * structure,const gchar * fieldname,GType enumtype,gint * value)1902 gst_structure_get_enum (const GstStructure * structure,
1903 const gchar * fieldname, GType enumtype, gint * value)
1904 {
1905 GstStructureField *field;
1906
1907 g_return_val_if_fail (structure != NULL, FALSE);
1908 g_return_val_if_fail (fieldname != NULL, FALSE);
1909 g_return_val_if_fail (enumtype != G_TYPE_INVALID, FALSE);
1910 g_return_val_if_fail (value != NULL, FALSE);
1911
1912 field = gst_structure_get_field (structure, fieldname);
1913
1914 if (field == NULL)
1915 return FALSE;
1916 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, enumtype))
1917 return FALSE;
1918
1919 *value = g_value_get_enum (&field->value);
1920
1921 return TRUE;
1922 }
1923
1924 /**
1925 * gst_structure_get_fraction:
1926 * @structure: a #GstStructure
1927 * @fieldname: the name of a field
1928 * @value_numerator: (out): a pointer to an int to set
1929 * @value_denominator: (out): a pointer to an int to set
1930 *
1931 * Sets the integers pointed to by @value_numerator and @value_denominator
1932 * corresponding to the value of the given field. Caller is responsible
1933 * for making sure the field exists and has the correct type.
1934 *
1935 * Returns: %TRUE if the values could be set correctly. If there was no field
1936 * with @fieldname or the existing field did not contain a GstFraction, this
1937 * function returns %FALSE.
1938 */
1939 gboolean
gst_structure_get_fraction(const GstStructure * structure,const gchar * fieldname,gint * value_numerator,gint * value_denominator)1940 gst_structure_get_fraction (const GstStructure * structure,
1941 const gchar * fieldname, gint * value_numerator, gint * value_denominator)
1942 {
1943 GstStructureField *field;
1944
1945 g_return_val_if_fail (structure != NULL, FALSE);
1946 g_return_val_if_fail (fieldname != NULL, FALSE);
1947 g_return_val_if_fail (value_numerator != NULL, FALSE);
1948 g_return_val_if_fail (value_denominator != NULL, FALSE);
1949
1950 field = gst_structure_get_field (structure, fieldname);
1951
1952 if (field == NULL || G_VALUE_TYPE (&field->value) != GST_TYPE_FRACTION)
1953 return FALSE;
1954
1955 *value_numerator = gst_value_get_fraction_numerator (&field->value);
1956 *value_denominator = gst_value_get_fraction_denominator (&field->value);
1957
1958 return TRUE;
1959 }
1960
1961 /**
1962 * gst_structure_get_flagset:
1963 * @structure: a #GstStructure
1964 * @fieldname: the name of a field
1965 * @value_flags: (out) (allow-none): a pointer to a guint for the flags field
1966 * @value_mask: (out) (allow-none): a pointer to a guint for the mask field
1967 *
1968 * Read the GstFlagSet flags and mask out of the structure into the
1969 * provided pointers.
1970 *
1971 * Returns: %TRUE if the values could be set correctly. If there was no field
1972 * with @fieldname or the existing field did not contain a GstFlagSet, this
1973 * function returns %FALSE.
1974 *
1975 * Since: 1.6
1976 */
1977 gboolean
gst_structure_get_flagset(const GstStructure * structure,const gchar * fieldname,guint * value_flags,guint * value_mask)1978 gst_structure_get_flagset (const GstStructure * structure,
1979 const gchar * fieldname, guint * value_flags, guint * value_mask)
1980 {
1981 GstStructureField *field;
1982
1983 g_return_val_if_fail (structure != NULL, FALSE);
1984 g_return_val_if_fail (fieldname != NULL, FALSE);
1985
1986 field = gst_structure_get_field (structure, fieldname);
1987
1988 if (field == NULL || !GST_VALUE_HOLDS_FLAG_SET (&field->value))
1989 return FALSE;
1990
1991 if (value_flags)
1992 *value_flags = gst_value_get_flagset_flags (&field->value);
1993 if (value_mask)
1994 *value_mask = gst_value_get_flagset_mask (&field->value);
1995
1996 return TRUE;
1997 }
1998
1999 static GType
gst_structure_value_get_generic_type(const GValue * val)2000 gst_structure_value_get_generic_type (const GValue * val)
2001 {
2002 if (G_VALUE_TYPE (val) == GST_TYPE_LIST
2003 || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
2004 GArray *array = g_value_peek_pointer (val);
2005
2006 /* Note, we can do this because the internal
2007 * GstValueList/GstValueArray implementation is API/ABI compatible
2008 * with GArray */
2009 if (array->len > 0) {
2010 GValue *value = &g_array_index (array, GValue, 0);
2011
2012 return gst_structure_value_get_generic_type (value);
2013 } else {
2014 return G_TYPE_INT;
2015 }
2016 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT_RANGE) {
2017 return G_TYPE_INT;
2018 } else if (G_VALUE_TYPE (val) == GST_TYPE_INT64_RANGE) {
2019 return G_TYPE_INT64;
2020 } else if (G_VALUE_TYPE (val) == GST_TYPE_DOUBLE_RANGE) {
2021 return G_TYPE_DOUBLE;
2022 } else if (G_VALUE_TYPE (val) == GST_TYPE_FRACTION_RANGE) {
2023 return GST_TYPE_FRACTION;
2024 }
2025 return G_VALUE_TYPE (val);
2026 }
2027
2028 gboolean
priv_gst_structure_append_to_gstring(const GstStructure * structure,GString * s,GstSerializeFlags flags)2029 priv_gst_structure_append_to_gstring (const GstStructure * structure,
2030 GString * s, GstSerializeFlags flags)
2031 {
2032 GstStructureField *field;
2033 guint i, len;
2034 gboolean nested_structs_brackets =
2035 !(flags & GST_SERIALIZE_FLAG_BACKWARD_COMPAT);
2036
2037 g_return_val_if_fail (s != NULL, FALSE);
2038
2039 len = GST_STRUCTURE_LEN (structure);
2040 for (i = 0; i < len; i++) {
2041 gchar *t = NULL;
2042 GType type;
2043
2044 field = GST_STRUCTURE_FIELD (structure, i);
2045
2046 if (G_VALUE_TYPE (&field->value) == GST_TYPE_ARRAY) {
2047 t = _priv_gst_value_serialize_any_list (&field->value, "< ", " >", FALSE);
2048 } else if (G_VALUE_TYPE (&field->value) == GST_TYPE_LIST) {
2049 t = _priv_gst_value_serialize_any_list (&field->value, "{ ", " }", FALSE);
2050 } else if (!nested_structs_brackets
2051 || (G_VALUE_TYPE (&field->value) != GST_TYPE_STRUCTURE
2052 && G_VALUE_TYPE (&field->value) != GST_TYPE_CAPS)) {
2053 t = gst_value_serialize (&field->value);
2054 }
2055
2056 type = gst_structure_value_get_generic_type (&field->value);
2057
2058 g_string_append_len (s, ", ", 2);
2059 /* FIXME: do we need to escape fieldnames? */
2060 g_string_append (s, g_quark_to_string (field->name));
2061 g_string_append_len (s, "=(", 2);
2062 g_string_append (s, _priv_gst_value_gtype_to_abbr (type));
2063 g_string_append_c (s, ')');
2064 if (nested_structs_brackets
2065 && G_VALUE_TYPE (&field->value) == GST_TYPE_STRUCTURE) {
2066 const GstStructure *substruct = gst_value_get_structure (&field->value);
2067
2068 g_string_append_c (s, '[');
2069 g_string_append (s, g_quark_to_string (substruct->name));
2070 priv_gst_structure_append_to_gstring (substruct, s, flags);
2071 g_string_append_c (s, ']');
2072 } else if (nested_structs_brackets
2073 && G_VALUE_TYPE (&field->value) == GST_TYPE_CAPS) {
2074 const GstCaps *subcaps = gst_value_get_caps (&field->value);
2075 gchar *capsstr = gst_caps_serialize (subcaps, flags);
2076
2077 g_string_append_printf (s, "[%s]", capsstr);
2078 g_free (capsstr);
2079 } else if (t) {
2080 g_string_append (s, t);
2081 g_free (t);
2082 } else if (G_TYPE_CHECK_VALUE_TYPE (&field->value, G_TYPE_POINTER)) {
2083 gpointer ptr = g_value_get_pointer (&field->value);
2084
2085 if (!ptr)
2086 g_string_append (s, "NULL");
2087 else
2088 g_string_append_printf (s, "%p", ptr);
2089 } else {
2090 if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, G_TYPE_STRING))
2091 GST_WARNING ("No value transform to serialize field '%s' of type '%s'",
2092 g_quark_to_string (field->name),
2093 _priv_gst_value_gtype_to_abbr (type));
2094 /* TODO(ensonic): don't print NULL if field->value is not empty */
2095 g_string_append (s, "NULL");
2096 }
2097 }
2098
2099 g_string_append_c (s, ';');
2100 return TRUE;
2101 }
2102
2103 gboolean
priv__gst_structure_append_template_to_gstring(GQuark field_id,const GValue * value,gpointer user_data)2104 priv__gst_structure_append_template_to_gstring (GQuark field_id,
2105 const GValue * value, gpointer user_data)
2106 {
2107 GType type = gst_structure_value_get_generic_type (value);
2108 GString *s = (GString *) user_data;
2109
2110 g_string_append_len (s, ", ", 2);
2111 /* FIXME: do we need to escape fieldnames? */
2112 g_string_append (s, g_quark_to_string (field_id));
2113 g_string_append_len (s, "=(", 2);
2114 g_string_append (s, _priv_gst_value_gtype_to_abbr (type));
2115 g_string_append_c (s, ')');
2116
2117 //TODO(ensonic): table like GstStructureAbbreviation (or extend it)
2118 if (type == G_TYPE_INT) {
2119 g_string_append_len (s, "%i", 2);
2120 } else if (type == G_TYPE_UINT) {
2121 g_string_append_len (s, "%u", 2);
2122 } else if (type == G_TYPE_FLOAT) {
2123 g_string_append_len (s, "%f", 2);
2124 } else if (type == G_TYPE_DOUBLE) {
2125 g_string_append_len (s, "%lf", 3);
2126 } else if (type == G_TYPE_STRING) {
2127 g_string_append_len (s, "%s", 2);
2128 } else if (type == G_TYPE_BOOLEAN) {
2129 /* we normally store this as a string, but can parse it also from an int */
2130 g_string_append_len (s, "%i", 2);
2131 } else if (type == G_TYPE_INT64) {
2132 g_string_append (s, "%" G_GINT64_FORMAT);
2133 } else if (type == G_TYPE_UINT64) {
2134 g_string_append (s, "%" G_GUINT64_FORMAT);
2135 } else if (type == GST_TYPE_STRUCTURE) {
2136 g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
2137 } else if (g_type_is_a (type, G_TYPE_ENUM)
2138 || g_type_is_a (type, G_TYPE_FLAGS)) {
2139 g_string_append_len (s, "%i", 2);
2140 } else if (type == G_TYPE_GTYPE) {
2141 g_string_append_len (s, "%s", 2);
2142 } else if (type == G_TYPE_POINTER) {
2143 g_string_append_len (s, "%p", 2);
2144 } else {
2145 GST_WARNING ("unhandled type: %s", g_type_name (type));
2146 g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
2147 }
2148
2149 return TRUE;
2150 }
2151
2152 static gchar *
structure_serialize(const GstStructure * structure,GstSerializeFlags flags)2153 structure_serialize (const GstStructure * structure, GstSerializeFlags flags)
2154 {
2155 GString *s;
2156
2157 /* NOTE: This function is potentially called by the debug system,
2158 * so any calls to gst_log() (and GST_DEBUG(), GST_LOG(), etc.)
2159 * should be careful to avoid recursion. This includes any functions
2160 * called by gst_structure_to_string. In particular, calls should
2161 * not use the GST_PTR_FORMAT extension. */
2162
2163 g_return_val_if_fail (structure != NULL, NULL);
2164
2165 /* we estimate a minimum size based on the number of fields in order to
2166 * avoid unnecessary reallocs within GString */
2167 s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
2168 g_string_append (s, g_quark_to_string (structure->name));
2169 priv_gst_structure_append_to_gstring (structure, s, flags);
2170 return g_string_free (s, FALSE);
2171
2172 }
2173
2174 /**
2175 * gst_structure_to_string:
2176 * @structure: a #GstStructure
2177 *
2178 * Converts @structure to a human-readable string representation.
2179 *
2180 * For debugging purposes its easier to do something like this: |[<!--
2181 * language="C" --> GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
2182 * ]|
2183 * This prints the structure in human readable form.
2184 *
2185 * This function will lead to unexpected results when there are nested #GstCaps
2186 * / #GstStructure deeper than one level, you should user
2187 * gst_structure_serialize() instead for those cases.
2188 *
2189 * Free-function: g_free
2190 *
2191 * Returns: (transfer full): a pointer to string allocated by g_malloc().
2192 * g_free() after usage.
2193 */
2194 gchar *
gst_structure_to_string(const GstStructure * structure)2195 gst_structure_to_string (const GstStructure * structure)
2196 {
2197 return structure_serialize (structure, GST_SERIALIZE_FLAG_BACKWARD_COMPAT);
2198 }
2199
2200 /**
2201 * gst_structure_serialize:
2202 * @structure: a #GstStructure
2203 * @flags: The flags to use to serialize structure
2204 *
2205 * Converts @structure to a human-readable string representation.
2206 *
2207 * This version of the caps serialization function introduces support for nested
2208 * structures and caps but the resulting strings won't be parsable with
2209 * GStreamer prior to 1.20 unless #GST_SERIALIZE_FLAG_BACKWARD_COMPAT is passed
2210 * as @flag.
2211 *
2212 * Free-function: g_free
2213 *
2214 * Returns: (transfer full): a pointer to string allocated by g_malloc().
2215 * g_free() after usage.
2216 *
2217 * Since: 1.20
2218 */
2219 gchar *
gst_structure_serialize(const GstStructure * structure,GstSerializeFlags flags)2220 gst_structure_serialize (const GstStructure * structure,
2221 GstSerializeFlags flags)
2222 {
2223 return structure_serialize (structure, flags);
2224 }
2225
2226 static gboolean
gst_structure_parse_field(gchar * str,gchar ** after,GstStructureField * field)2227 gst_structure_parse_field (gchar * str,
2228 gchar ** after, GstStructureField * field)
2229 {
2230 gchar *name;
2231 gchar *name_end;
2232 gchar *s;
2233 gchar c;
2234
2235 s = str;
2236
2237 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2238 s++;
2239 name = s;
2240 if (G_UNLIKELY (!_priv_gst_value_parse_simple_string (s, &name_end))) {
2241 GST_WARNING ("failed to parse simple string, str=%s", str);
2242 return FALSE;
2243 }
2244
2245 s = name_end;
2246 while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
2247 s++;
2248
2249 if (G_UNLIKELY (*s != '=')) {
2250 GST_WARNING ("missing assignment operator in the field, str=%s", str);
2251 return FALSE;
2252 }
2253 s++;
2254
2255 c = *name_end;
2256 *name_end = '\0';
2257 field->name = g_quark_from_string (name);
2258 GST_DEBUG ("trying field name '%s'", name);
2259 *name_end = c;
2260
2261 if (G_UNLIKELY (!_priv_gst_value_parse_value (s, &s, &field->value,
2262 G_TYPE_INVALID, NULL))) {
2263 GST_WARNING ("failed to parse value %s", str);
2264 return FALSE;
2265 }
2266
2267 *after = s;
2268 return TRUE;
2269 }
2270
2271 gboolean
priv_gst_structure_parse_name(gchar * str,gchar ** start,gchar ** end,gchar ** next,gboolean check_valid)2272 priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end,
2273 gchar ** next, gboolean check_valid)
2274 {
2275 char *w;
2276 char *r;
2277
2278 r = str;
2279
2280 /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
2281 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2282 && g_ascii_isspace (r[1]))))
2283 r++;
2284
2285 *start = r;
2286
2287 if (G_UNLIKELY (!_priv_gst_value_parse_string (r, &w, &r, TRUE))) {
2288 GST_WARNING ("Failed to parse structure string '%s'", str);
2289 return FALSE;
2290 }
2291
2292 if (check_valid) {
2293 gchar save = *w;
2294
2295 *w = '\0';
2296 if (!gst_structure_validate_name (*start)) {
2297 *w = save;
2298 return FALSE;
2299 }
2300 *w = save;
2301 }
2302
2303 *end = w;
2304 *next = r;
2305
2306 return TRUE;
2307 }
2308
2309 gboolean
priv_gst_structure_parse_fields(gchar * str,gchar ** end,GstStructure * structure)2310 priv_gst_structure_parse_fields (gchar * str, gchar ** end,
2311 GstStructure * structure)
2312 {
2313 gchar *r;
2314 GstStructureField field;
2315
2316 r = str;
2317
2318 do {
2319 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2320 && g_ascii_isspace (r[1]))))
2321 r++;
2322 if (*r == ';') {
2323 /* end of structure, get the next char and finish */
2324 r++;
2325 break;
2326 }
2327 if (*r == '\0') {
2328 /* accept \0 as end delimiter */
2329 break;
2330 }
2331 if (G_UNLIKELY (*r != ',')) {
2332 GST_WARNING ("Failed to find delimiter, r=%s", r);
2333 return FALSE;
2334 }
2335 r++;
2336 while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
2337 && g_ascii_isspace (r[1]))))
2338 r++;
2339
2340 /* Trailing comma */
2341 if (*r == '\0') {
2342 break;
2343 } else if (*r == ';') {
2344 r++;
2345 break;
2346 }
2347
2348 memset (&field, 0, sizeof (field));
2349 if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
2350 GST_WARNING ("Failed to parse field, r=%s", r);
2351 return FALSE;
2352 }
2353 gst_structure_set_field (structure, &field);
2354 } while (TRUE);
2355
2356 *end = r;
2357
2358 return TRUE;
2359 }
2360
2361 /**
2362 * gst_structure_new_from_string:
2363 * @string: a string representation of a #GstStructure
2364 *
2365 * Creates a #GstStructure from a string representation.
2366 * If end is not %NULL, a pointer to the place inside the given string
2367 * where parsing ended will be returned.
2368 *
2369 * The current implementation of serialization will lead to unexpected results
2370 * when there are nested #GstCaps / #GstStructure deeper than one level unless
2371 * the gst_structure_serialize() function is used (without
2372 * #GST_SERIALIZE_FLAG_BACKWARD_COMPAT)
2373 *
2374 * Free-function: gst_structure_free
2375 *
2376 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2377 * when the string could not be parsed. Free with
2378 * gst_structure_free() after use.
2379 *
2380 * Since: 1.2
2381 */
2382 GstStructure *
gst_structure_new_from_string(const gchar * string)2383 gst_structure_new_from_string (const gchar * string)
2384 {
2385 return gst_structure_from_string (string, NULL);
2386 }
2387
2388 /**
2389 * gst_structure_from_string: (constructor):
2390 * @string: a string representation of a #GstStructure.
2391 * @end: (out) (allow-none) (transfer none) (skip): pointer to store the end of the string in.
2392 *
2393 * Creates a #GstStructure from a string representation.
2394 * If end is not %NULL, a pointer to the place inside the given string
2395 * where parsing ended will be returned.
2396 *
2397 * Free-function: gst_structure_free
2398 *
2399 * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
2400 * when the string could not be parsed. Free with
2401 * gst_structure_free() after use.
2402 */
2403 GstStructure *
gst_structure_from_string(const gchar * string,gchar ** end)2404 gst_structure_from_string (const gchar * string, gchar ** end)
2405 {
2406 char *name;
2407 char *copy;
2408 char *w;
2409 char *r;
2410 char save;
2411 GstStructure *structure = NULL;
2412
2413 g_return_val_if_fail (string != NULL, NULL);
2414
2415 copy = g_strdup (string);
2416 r = copy;
2417
2418 if (!priv_gst_structure_parse_name (r, &name, &w, &r, FALSE))
2419 goto error;
2420
2421 save = *w;
2422 *w = '\0';
2423 structure = gst_structure_new_empty (name);
2424 *w = save;
2425
2426 if (G_UNLIKELY (structure == NULL))
2427 goto error;
2428
2429 if (!priv_gst_structure_parse_fields (r, &r, structure))
2430 goto error;
2431
2432 if (end)
2433 *end = (char *) string + (r - copy);
2434 else if (*r)
2435 g_warning ("gst_structure_from_string did not consume whole string,"
2436 " but caller did not provide end pointer (\"%s\")", string);
2437
2438 g_free (copy);
2439 return structure;
2440
2441 error:
2442 if (structure)
2443 gst_structure_free (structure);
2444 g_free (copy);
2445 return NULL;
2446 }
2447
2448 static void
gst_structure_transform_to_string(const GValue * src_value,GValue * dest_value)2449 gst_structure_transform_to_string (const GValue * src_value,
2450 GValue * dest_value)
2451 {
2452 g_return_if_fail (src_value != NULL);
2453 g_return_if_fail (dest_value != NULL);
2454
2455 dest_value->data[0].v_pointer =
2456 gst_structure_to_string (src_value->data[0].v_pointer);
2457 }
2458
2459 static GstStructure *
gst_structure_copy_conditional(const GstStructure * structure)2460 gst_structure_copy_conditional (const GstStructure * structure)
2461 {
2462 if (structure)
2463 return gst_structure_copy (structure);
2464 return NULL;
2465 }
2466
2467 /* fixate utility functions */
2468
2469 /**
2470 * gst_structure_fixate_field_nearest_int:
2471 * @structure: a #GstStructure
2472 * @field_name: a field in @structure
2473 * @target: the target value of the fixation
2474 *
2475 * Fixates a #GstStructure by changing the given field to the nearest
2476 * integer to @target that is a subset of the existing field.
2477 *
2478 * Returns: %TRUE if the structure could be fixated
2479 */
2480 gboolean
gst_structure_fixate_field_nearest_int(GstStructure * structure,const char * field_name,int target)2481 gst_structure_fixate_field_nearest_int (GstStructure * structure,
2482 const char *field_name, int target)
2483 {
2484 const GValue *value;
2485
2486 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2487 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2488
2489 value = gst_structure_get_value (structure, field_name);
2490
2491 if (G_VALUE_TYPE (value) == G_TYPE_INT) {
2492 /* already fixed */
2493 return FALSE;
2494 } else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
2495 int min, max, step;
2496
2497 min = gst_value_get_int_range_min (value);
2498 max = gst_value_get_int_range_max (value);
2499 step = gst_value_get_int_range_step (value);
2500
2501 target = CLAMP (target, min, max);
2502 if (G_UNLIKELY (step != 1)) {
2503 gint rem = target % step;
2504 target -= rem;
2505 if (rem > step / 2)
2506 target += step;
2507 }
2508
2509 gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
2510 return TRUE;
2511 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2512 const GValue *list_value;
2513 int i, n;
2514 int best = 0;
2515 int best_index = -1;
2516
2517 n = gst_value_list_get_size (value);
2518 for (i = 0; i < n; i++) {
2519 list_value = gst_value_list_get_value (value, i);
2520 if (G_VALUE_TYPE (list_value) == G_TYPE_INT) {
2521 int x = gst_g_value_get_int_unchecked (list_value);
2522
2523 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2524 best_index = i;
2525 best = x;
2526 }
2527 }
2528 }
2529 if (best_index != -1) {
2530 gst_structure_set (structure, field_name, G_TYPE_INT, best, NULL);
2531 return TRUE;
2532 }
2533 return FALSE;
2534 }
2535
2536 return FALSE;
2537 }
2538
2539 /**
2540 * gst_structure_fixate_field_nearest_double:
2541 * @structure: a #GstStructure
2542 * @field_name: a field in @structure
2543 * @target: the target value of the fixation
2544 *
2545 * Fixates a #GstStructure by changing the given field to the nearest
2546 * double to @target that is a subset of the existing field.
2547 *
2548 * Returns: %TRUE if the structure could be fixated
2549 */
2550 gboolean
gst_structure_fixate_field_nearest_double(GstStructure * structure,const char * field_name,double target)2551 gst_structure_fixate_field_nearest_double (GstStructure * structure,
2552 const char *field_name, double target)
2553 {
2554 const GValue *value;
2555
2556 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2557 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2558
2559 value = gst_structure_get_value (structure, field_name);
2560
2561 if (G_VALUE_TYPE (value) == G_TYPE_DOUBLE) {
2562 /* already fixed */
2563 return FALSE;
2564 } else if (G_VALUE_TYPE (value) == GST_TYPE_DOUBLE_RANGE) {
2565 double x;
2566
2567 x = gst_value_get_double_range_min (value);
2568 if (target < x)
2569 target = x;
2570 x = gst_value_get_double_range_max (value);
2571 if (target > x)
2572 target = x;
2573 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, target, NULL);
2574 return TRUE;
2575 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2576 const GValue *list_value;
2577 int i, n;
2578 double best = 0;
2579 int best_index = -1;
2580
2581 n = gst_value_list_get_size (value);
2582 for (i = 0; i < n; i++) {
2583 list_value = gst_value_list_get_value (value, i);
2584 if (G_VALUE_TYPE (list_value) == G_TYPE_DOUBLE) {
2585 double x = gst_g_value_get_double_unchecked (list_value);
2586
2587 if (best_index == -1 || (ABS (target - x) < ABS (target - best))) {
2588 best_index = i;
2589 best = x;
2590 }
2591 }
2592 }
2593 if (best_index != -1) {
2594 gst_structure_set (structure, field_name, G_TYPE_DOUBLE, best, NULL);
2595 return TRUE;
2596 }
2597 return FALSE;
2598 }
2599
2600 return FALSE;
2601
2602 }
2603
2604 /**
2605 * gst_structure_fixate_field_boolean:
2606 * @structure: a #GstStructure
2607 * @field_name: a field in @structure
2608 * @target: the target value of the fixation
2609 *
2610 * Fixates a #GstStructure by changing the given @field_name field to the given
2611 * @target boolean if that field is not fixed yet.
2612 *
2613 * Returns: %TRUE if the structure could be fixated
2614 */
2615 gboolean
gst_structure_fixate_field_boolean(GstStructure * structure,const char * field_name,gboolean target)2616 gst_structure_fixate_field_boolean (GstStructure * structure,
2617 const char *field_name, gboolean target)
2618 {
2619 const GValue *value;
2620
2621 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2622 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2623
2624 value = gst_structure_get_value (structure, field_name);
2625
2626 if (G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
2627 /* already fixed */
2628 return FALSE;
2629 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2630 const GValue *list_value;
2631 int i, n;
2632 int best = 0;
2633 int best_index = -1;
2634
2635 n = gst_value_list_get_size (value);
2636 for (i = 0; i < n; i++) {
2637 list_value = gst_value_list_get_value (value, i);
2638 if (G_VALUE_TYPE (list_value) == G_TYPE_BOOLEAN) {
2639 gboolean x = gst_g_value_get_boolean_unchecked (list_value);
2640
2641 if (best_index == -1 || x == target) {
2642 best_index = i;
2643 best = x;
2644 }
2645 }
2646 }
2647 if (best_index != -1) {
2648 gst_structure_set (structure, field_name, G_TYPE_BOOLEAN, best, NULL);
2649 return TRUE;
2650 }
2651 return FALSE;
2652 }
2653
2654 return FALSE;
2655 }
2656
2657 /**
2658 * gst_structure_fixate_field_string:
2659 * @structure: a #GstStructure
2660 * @field_name: a field in @structure
2661 * @target: the target value of the fixation
2662 *
2663 * Fixates a #GstStructure by changing the given @field_name field to the given
2664 * @target string if that field is not fixed yet.
2665 *
2666 * Returns: %TRUE if the structure could be fixated
2667 */
2668 gboolean
gst_structure_fixate_field_string(GstStructure * structure,const gchar * field_name,const gchar * target)2669 gst_structure_fixate_field_string (GstStructure * structure,
2670 const gchar * field_name, const gchar * target)
2671 {
2672 const GValue *value;
2673
2674 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2675 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2676
2677 value = gst_structure_get_value (structure, field_name);
2678
2679 if (G_VALUE_TYPE (value) == G_TYPE_STRING) {
2680 /* already fixed */
2681 return FALSE;
2682 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2683 const GValue *list_value;
2684 int i, n;
2685 const gchar *best = NULL;
2686 int best_index = -1;
2687
2688 n = gst_value_list_get_size (value);
2689 for (i = 0; i < n; i++) {
2690 list_value = gst_value_list_get_value (value, i);
2691 if (G_VALUE_TYPE (list_value) == G_TYPE_STRING) {
2692 const gchar *x = g_value_get_string (list_value);
2693
2694 if (best_index == -1 || g_str_equal (x, target)) {
2695 best_index = i;
2696 best = x;
2697 }
2698 }
2699 }
2700 if (best_index != -1) {
2701 gst_structure_set (structure, field_name, G_TYPE_STRING, best, NULL);
2702 return TRUE;
2703 }
2704 return FALSE;
2705 }
2706
2707 return FALSE;
2708 }
2709
2710 /**
2711 * gst_structure_fixate_field_nearest_fraction:
2712 * @structure: a #GstStructure
2713 * @field_name: a field in @structure
2714 * @target_numerator: The numerator of the target value of the fixation
2715 * @target_denominator: The denominator of the target value of the fixation
2716 *
2717 * Fixates a #GstStructure by changing the given field to the nearest
2718 * fraction to @target_numerator/@target_denominator that is a subset
2719 * of the existing field.
2720 *
2721 * Returns: %TRUE if the structure could be fixated
2722 */
2723 gboolean
gst_structure_fixate_field_nearest_fraction(GstStructure * structure,const char * field_name,const gint target_numerator,const gint target_denominator)2724 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
2725 const char *field_name, const gint target_numerator,
2726 const gint target_denominator)
2727 {
2728 const GValue *value;
2729
2730 g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
2731 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2732 g_return_val_if_fail (target_denominator != 0, FALSE);
2733
2734 value = gst_structure_get_value (structure, field_name);
2735
2736 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
2737 /* already fixed */
2738 return FALSE;
2739 } else if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION_RANGE) {
2740 const GValue *x, *new_value;
2741 GValue target = { 0 };
2742 g_value_init (&target, GST_TYPE_FRACTION);
2743 gst_value_set_fraction (&target, target_numerator, target_denominator);
2744
2745 new_value = ⌖
2746 x = gst_value_get_fraction_range_min (value);
2747 if (gst_value_compare (&target, x) == GST_VALUE_LESS_THAN)
2748 new_value = x;
2749 x = gst_value_get_fraction_range_max (value);
2750 if (gst_value_compare (&target, x) == GST_VALUE_GREATER_THAN)
2751 new_value = x;
2752
2753 gst_structure_set_value (structure, field_name, new_value);
2754 g_value_unset (&target);
2755 return TRUE;
2756 } else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
2757 const GValue *list_value;
2758 int i, n;
2759 const GValue *best = NULL;
2760 gdouble target;
2761 gdouble cur_diff;
2762 gdouble best_diff = G_MAXDOUBLE;
2763
2764 target = (gdouble) target_numerator / (gdouble) target_denominator;
2765
2766 GST_DEBUG ("target %g, best %g", target, best_diff);
2767
2768 best = NULL;
2769
2770 n = gst_value_list_get_size (value);
2771 for (i = 0; i < n; i++) {
2772 list_value = gst_value_list_get_value (value, i);
2773 if (G_VALUE_TYPE (list_value) == GST_TYPE_FRACTION) {
2774 gint num, denom;
2775 gdouble list_double;
2776
2777 num = gst_value_get_fraction_numerator (list_value);
2778 denom = gst_value_get_fraction_denominator (list_value);
2779
2780 list_double = ((gdouble) num / (gdouble) denom);
2781 cur_diff = target - list_double;
2782
2783 GST_DEBUG ("curr diff %g, list %g", cur_diff, list_double);
2784
2785 if (cur_diff < 0)
2786 cur_diff = -cur_diff;
2787
2788 if (!best || cur_diff < best_diff) {
2789 GST_DEBUG ("new best %g", list_double);
2790 best = list_value;
2791 best_diff = cur_diff;
2792 }
2793 }
2794 }
2795 if (best != NULL) {
2796 gst_structure_set_value (structure, field_name, best);
2797 return TRUE;
2798 }
2799 }
2800
2801 return FALSE;
2802 }
2803
2804 static gboolean
default_fixate(GQuark field_id,const GValue * value,gpointer data)2805 default_fixate (GQuark field_id, const GValue * value, gpointer data)
2806 {
2807 GstStructure *s = data;
2808 GValue v = { 0 };
2809
2810 if (gst_value_fixate (&v, value)) {
2811 gst_structure_id_take_value (s, field_id, &v);
2812 }
2813 return TRUE;
2814 }
2815
2816 /**
2817 * gst_structure_fixate_field:
2818 * @structure: a #GstStructure
2819 * @field_name: a field in @structure
2820 *
2821 * Fixates a #GstStructure by changing the given field with its fixated value.
2822 *
2823 * Returns: %TRUE if the structure field could be fixated
2824 */
2825 gboolean
gst_structure_fixate_field(GstStructure * structure,const char * field_name)2826 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
2827 {
2828 GstStructureField *field;
2829
2830 g_return_val_if_fail (structure != NULL, FALSE);
2831 g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
2832
2833 if (!(field = gst_structure_get_field (structure, field_name)))
2834 return FALSE;
2835
2836 return default_fixate (field->name, &field->value, structure);
2837 }
2838
2839 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
2840 * (useful for message parsing functions where the return location is user
2841 * supplied and the user may pass %NULL if the value isn't of interest) */
2842 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname) \
2843 G_STMT_START { \
2844 const GValue *_value = (value); \
2845 guint _flags = (flags); \
2846 GType _value_type = G_VALUE_TYPE (_value); \
2847 GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
2848 const gchar *_lcopy_format = _vtable->lcopy_format; \
2849 GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
2850 guint _n_values = 0; \
2851 \
2852 while (*_lcopy_format != '\0') { \
2853 g_assert (*_lcopy_format == G_VALUE_COLLECT_POINTER); \
2854 _cvalues[_n_values++].v_pointer = va_arg ((var_args), gpointer); \
2855 _lcopy_format++; \
2856 } \
2857 if (_n_values == 2 && !!_cvalues[0].v_pointer != !!_cvalues[1].v_pointer) { \
2858 *(__error) = g_strdup_printf ("either all or none of the return " \
2859 "locations for field '%s' need to be NULL", fieldname); \
2860 } else if (_cvalues[0].v_pointer != NULL) { \
2861 *(__error) = _vtable->lcopy_value (_value, _n_values, _cvalues, _flags); \
2862 } \
2863 } G_STMT_END
2864
2865 /**
2866 * gst_structure_get_valist:
2867 * @structure: a #GstStructure
2868 * @first_fieldname: the name of the first field to read
2869 * @args: variable arguments
2870 *
2871 * Parses the variable arguments and reads fields from @structure accordingly.
2872 * valist-variant of gst_structure_get(). Look at the documentation of
2873 * gst_structure_get() for more details.
2874 *
2875 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2876 */
2877 gboolean
gst_structure_get_valist(const GstStructure * structure,const char * first_fieldname,va_list args)2878 gst_structure_get_valist (const GstStructure * structure,
2879 const char *first_fieldname, va_list args)
2880 {
2881 const char *field_name;
2882 GType expected_type = G_TYPE_INVALID;
2883
2884 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2885 g_return_val_if_fail (first_fieldname != NULL, FALSE);
2886
2887 field_name = first_fieldname;
2888 while (field_name) {
2889 const GValue *val = NULL;
2890 gchar *err = NULL;
2891
2892 expected_type = va_arg (args, GType);
2893
2894 val = gst_structure_get_value (structure, field_name);
2895
2896 if (val == NULL)
2897 goto no_such_field;
2898
2899 if (G_VALUE_TYPE (val) != expected_type)
2900 goto wrong_type;
2901
2902 GST_VALUE_LCOPY (val, args, 0, &err, field_name);
2903 if (err) {
2904 g_warning ("%s: %s", G_STRFUNC, err);
2905 g_free (err);
2906 return FALSE;
2907 }
2908
2909 field_name = va_arg (args, const gchar *);
2910 }
2911
2912 return TRUE;
2913
2914 /* ERRORS */
2915 no_such_field:
2916 {
2917 GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2918 field_name, structure);
2919 return FALSE;
2920 }
2921 wrong_type:
2922 {
2923 GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
2924 "field was of type '%s': %" GST_PTR_FORMAT, field_name,
2925 GST_STR_NULL (g_type_name (expected_type)),
2926 G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
2927 structure);
2928 return FALSE;
2929 }
2930 }
2931
2932 /**
2933 * gst_structure_id_get_valist:
2934 * @structure: a #GstStructure
2935 * @first_field_id: the quark of the first field to read
2936 * @args: variable arguments
2937 *
2938 * Parses the variable arguments and reads fields from @structure accordingly.
2939 * valist-variant of gst_structure_id_get(). Look at the documentation of
2940 * gst_structure_id_get() for more details.
2941 *
2942 * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
2943 */
2944 gboolean
gst_structure_id_get_valist(const GstStructure * structure,GQuark first_field_id,va_list args)2945 gst_structure_id_get_valist (const GstStructure * structure,
2946 GQuark first_field_id, va_list args)
2947 {
2948 GQuark field_id;
2949 GType expected_type = G_TYPE_INVALID;
2950
2951 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
2952 g_return_val_if_fail (first_field_id != 0, FALSE);
2953
2954 field_id = first_field_id;
2955 while (field_id) {
2956 const GValue *val = NULL;
2957 gchar *err = NULL;
2958
2959 expected_type = va_arg (args, GType);
2960
2961 val = gst_structure_id_get_value (structure, field_id);
2962
2963 if (val == NULL)
2964 goto no_such_field;
2965
2966 if (G_VALUE_TYPE (val) != expected_type)
2967 goto wrong_type;
2968
2969 GST_VALUE_LCOPY (val, args, 0, &err, g_quark_to_string (field_id));
2970 if (err) {
2971 g_warning ("%s: %s", G_STRFUNC, err);
2972 g_free (err);
2973 return FALSE;
2974 }
2975
2976 field_id = va_arg (args, GQuark);
2977 }
2978
2979 return TRUE;
2980
2981 /* ERRORS */
2982 no_such_field:
2983 {
2984 GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
2985 GST_STR_NULL (g_quark_to_string (field_id)), structure);
2986 return FALSE;
2987 }
2988 wrong_type:
2989 {
2990 GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
2991 "field was of type '%s': %" GST_PTR_FORMAT,
2992 g_quark_to_string (field_id),
2993 GST_STR_NULL (g_type_name (expected_type)),
2994 G_VALUE_TYPE_NAME (gst_structure_id_get_value (structure, field_id)),
2995 structure);
2996 return FALSE;
2997 }
2998 }
2999
3000 /**
3001 * gst_structure_get:
3002 * @structure: a #GstStructure
3003 * @first_fieldname: the name of the first field to read
3004 * @...: variable arguments
3005 *
3006 * Parses the variable arguments and reads fields from @structure accordingly.
3007 * Variable arguments should be in the form field name, field type
3008 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3009 * The last variable argument should be %NULL.
3010 *
3011 * For refcounted (mini)objects you will receive a new reference which
3012 * you must release with a suitable _unref\() when no longer needed. For
3013 * strings and boxed types you will receive a copy which you will need to
3014 * release with either g_free() or the suitable function for the boxed type.
3015 *
3016 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
3017 * because the field requested did not exist, or was of a type other
3018 * than the type specified), otherwise %TRUE.
3019 */
3020 gboolean
gst_structure_get(const GstStructure * structure,const char * first_fieldname,...)3021 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
3022 ...)
3023 {
3024 gboolean ret;
3025 va_list args;
3026
3027 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3028 g_return_val_if_fail (first_fieldname != NULL, FALSE);
3029
3030 va_start (args, first_fieldname);
3031 ret = gst_structure_get_valist (structure, first_fieldname, args);
3032 va_end (args);
3033
3034 return ret;
3035 }
3036
3037 /**
3038 * gst_structure_id_get:
3039 * @structure: a #GstStructure
3040 * @first_field_id: the quark of the first field to read
3041 * @...: variable arguments
3042 *
3043 * Parses the variable arguments and reads fields from @structure accordingly.
3044 * Variable arguments should be in the form field id quark, field type
3045 * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
3046 * The last variable argument should be %NULL (technically it should be a
3047 * 0 quark, but we require %NULL so compilers that support it can check for
3048 * the %NULL terminator and warn if it's not there).
3049 *
3050 * This function is just like gst_structure_get() only that it is slightly
3051 * more efficient since it saves the string-to-quark lookup in the global
3052 * quark hashtable.
3053 *
3054 * For refcounted (mini)objects you will receive a new reference which
3055 * you must release with a suitable _unref\() when no longer needed. For
3056 * strings and boxed types you will receive a copy which you will need to
3057 * release with either g_free() or the suitable function for the boxed type.
3058 *
3059 * Returns: %FALSE if there was a problem reading any of the fields (e.g.
3060 * because the field requested did not exist, or was of a type other
3061 * than the type specified), otherwise %TRUE.
3062 */
3063 gboolean
gst_structure_id_get(const GstStructure * structure,GQuark first_field_id,...)3064 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
3065 ...)
3066 {
3067 gboolean ret;
3068 va_list args;
3069
3070 g_return_val_if_fail (GST_IS_STRUCTURE (structure), FALSE);
3071 g_return_val_if_fail (first_field_id != 0, FALSE);
3072
3073 va_start (args, first_field_id);
3074 ret = gst_structure_id_get_valist (structure, first_field_id, args);
3075 va_end (args);
3076
3077 return ret;
3078 }
3079
3080 static gboolean
gst_structure_is_equal_foreach(GQuark field_id,const GValue * val2,gpointer data)3081 gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
3082 gpointer data)
3083 {
3084 const GstStructure *struct1 = (const GstStructure *) data;
3085 const GValue *val1 = gst_structure_id_get_value (struct1, field_id);
3086
3087 if (G_UNLIKELY (val1 == NULL))
3088 return FALSE;
3089 if (gst_value_compare (val1, val2) == GST_VALUE_EQUAL) {
3090 return TRUE;
3091 }
3092
3093 return FALSE;
3094 }
3095
3096 /**
3097 * gst_structure_is_equal:
3098 * @structure1: a #GstStructure.
3099 * @structure2: a #GstStructure.
3100 *
3101 * Tests if the two #GstStructure are equal.
3102 *
3103 * Returns: %TRUE if the two structures have the same name and field.
3104 **/
3105 gboolean
gst_structure_is_equal(const GstStructure * structure1,const GstStructure * structure2)3106 gst_structure_is_equal (const GstStructure * structure1,
3107 const GstStructure * structure2)
3108 {
3109 g_return_val_if_fail (GST_IS_STRUCTURE (structure1), FALSE);
3110 g_return_val_if_fail (GST_IS_STRUCTURE (structure2), FALSE);
3111
3112 if (G_UNLIKELY (structure1 == structure2))
3113 return TRUE;
3114
3115 if (structure1->name != structure2->name) {
3116 return FALSE;
3117 }
3118 if (GST_STRUCTURE_LEN (structure1) != GST_STRUCTURE_LEN (structure2)) {
3119 return FALSE;
3120 }
3121
3122 return gst_structure_foreach (structure1, gst_structure_is_equal_foreach,
3123 (gpointer) structure2);
3124 }
3125
3126 /**
3127 * gst_structure_intersect:
3128 * @struct1: a #GstStructure
3129 * @struct2: a #GstStructure
3130 *
3131 * Intersects @struct1 and @struct2 and returns the intersection.
3132 *
3133 * Returns: (nullable): Intersection of @struct1 and @struct2
3134 */
3135 GstStructure *
gst_structure_intersect(const GstStructure * struct1,const GstStructure * struct2)3136 gst_structure_intersect (const GstStructure * struct1,
3137 const GstStructure * struct2)
3138 {
3139 guint it1, len1, it2, len2;
3140 GstStructure *dest;
3141
3142 g_assert (struct1 != NULL);
3143 g_assert (struct2 != NULL);
3144
3145 if (G_UNLIKELY (struct1->name != struct2->name))
3146 return NULL;
3147
3148 len1 = GST_STRUCTURE_LEN (struct1);
3149 len2 = GST_STRUCTURE_LEN (struct2);
3150
3151 /* Resulting structure will be at most the size of the smallest structure */
3152 dest = gst_structure_new_id_empty_with_size (struct1->name, MIN (len1, len2));
3153
3154 /* copy fields from struct1 which we have not in struct2 to target
3155 * intersect if we have the field in both */
3156 for (it1 = 0; it1 < len1; it1++) {
3157 GstStructureField *field1 = GST_STRUCTURE_FIELD (struct1, it1);
3158 gboolean seenother = FALSE;
3159 for (it2 = 0; it2 < len2; it2++) {
3160 GstStructureField *field2 = GST_STRUCTURE_FIELD (struct2, it2);
3161 if (field1->name == field2->name) {
3162 GValue dest_value = { 0 };
3163 seenother = TRUE;
3164 /* Get the intersection if any */
3165 if (gst_value_intersect (&dest_value, &field1->value, &field2->value)) {
3166 gst_structure_id_take_value (dest, field1->name, &dest_value);
3167 break;
3168 } else {
3169 /* No intersection, return nothing */
3170 goto error;
3171 }
3172 }
3173 }
3174 /* Field1 was only present in struct1, copy it over */
3175 if (!seenother)
3176 gst_structure_id_set_value (dest, field1->name, &field1->value);
3177 }
3178
3179 /* Now iterate over the 2nd struct and copy over everything which
3180 * isn't present in the 1st struct (we've already taken care of
3181 * values being present in both just above) */
3182 for (it2 = 0; it2 < len2; it2++) {
3183 GstStructureField *field2 = GST_STRUCTURE_FIELD (struct2, it2);
3184 gboolean seenother = FALSE;
3185 for (it1 = 0; it1 < len1; it1++) {
3186 GstStructureField *field1 = GST_STRUCTURE_FIELD (struct1, it1);
3187 if (field1->name == field2->name) {
3188 seenother = TRUE;
3189 break;
3190 }
3191 }
3192 if (!seenother)
3193 gst_structure_id_set_value (dest, field2->name, &field2->value);
3194
3195 }
3196
3197 return dest;
3198
3199 error:
3200 gst_structure_free (dest);
3201 return NULL;
3202 }
3203
3204 static gboolean
gst_caps_structure_can_intersect_field(GQuark id,const GValue * val1,gpointer data)3205 gst_caps_structure_can_intersect_field (GQuark id, const GValue * val1,
3206 gpointer data)
3207 {
3208 GstStructure *other = (GstStructure *) data;
3209 const GValue *val2 = gst_structure_id_get_value (other, id);
3210
3211 if (G_LIKELY (val2)) {
3212 if (!gst_value_can_intersect (val1, val2)) {
3213 return FALSE;
3214 } else {
3215 gint eq = gst_value_compare (val1, val2);
3216
3217 if (eq == GST_VALUE_UNORDERED) {
3218 /* we need to try interseting */
3219 if (!gst_value_intersect (NULL, val1, val2)) {
3220 return FALSE;
3221 }
3222 } else if (eq != GST_VALUE_EQUAL) {
3223 return FALSE;
3224 }
3225 }
3226 }
3227 return TRUE;
3228 }
3229
3230 /**
3231 * gst_structure_can_intersect:
3232 * @struct1: a #GstStructure
3233 * @struct2: a #GstStructure
3234 *
3235 * Tries intersecting @struct1 and @struct2 and reports whether the result
3236 * would not be empty.
3237 *
3238 * Returns: %TRUE if intersection would not be empty
3239 */
3240 gboolean
gst_structure_can_intersect(const GstStructure * struct1,const GstStructure * struct2)3241 gst_structure_can_intersect (const GstStructure * struct1,
3242 const GstStructure * struct2)
3243 {
3244 g_return_val_if_fail (GST_IS_STRUCTURE (struct1), FALSE);
3245 g_return_val_if_fail (GST_IS_STRUCTURE (struct2), FALSE);
3246
3247 if (G_UNLIKELY (struct1->name != struct2->name))
3248 return FALSE;
3249
3250 /* tries to intersect if we have the field in both */
3251 return gst_structure_foreach ((GstStructure *) struct1,
3252 gst_caps_structure_can_intersect_field, (gpointer) struct2);
3253 }
3254
3255
3256 /**
3257 * gst_structure_is_subset:
3258 * @subset: a #GstStructure
3259 * @superset: a potentially greater #GstStructure
3260 *
3261 * Checks if @subset is a subset of @superset, i.e. has the same
3262 * structure name and for all fields that are existing in @superset,
3263 * @subset has a value that is a subset of the value in @superset.
3264 *
3265 * Returns: %TRUE if @subset is a subset of @superset
3266 */
3267 gboolean
gst_structure_is_subset(const GstStructure * subset,const GstStructure * superset)3268 gst_structure_is_subset (const GstStructure * subset,
3269 const GstStructure * superset)
3270 {
3271 guint it1, len1, it2, len2;
3272
3273 g_assert (superset);
3274
3275 if (G_UNLIKELY (superset->name != subset->name))
3276 return FALSE;
3277
3278 len1 = GST_STRUCTURE_LEN (subset);
3279 len2 = GST_STRUCTURE_LEN (superset);
3280 if (len2 > len1)
3281 return FALSE;
3282
3283 for (it2 = 0; it2 < len2; it2++) {
3284 GstStructureField *superfield = GST_STRUCTURE_FIELD (superset, it2);
3285 gboolean seenother = FALSE;
3286 for (it1 = 0; it1 < len1; it1++) {
3287 GstStructureField *subfield = GST_STRUCTURE_FIELD (subset, it1);
3288 if (subfield->name == superfield->name) {
3289 int comparison =
3290 gst_value_compare (&subfield->value, &superfield->value);
3291 seenother = TRUE;
3292
3293 /* If present and equal, stop iterating */
3294 if (comparison == GST_VALUE_EQUAL)
3295 break;
3296
3297 /* Stop everything if ordered but unequal */
3298 if (comparison != GST_VALUE_UNORDERED)
3299 return FALSE;
3300
3301 /* Stop everything if not a subset */
3302 if (!gst_value_is_subset (&subfield->value, &superfield->value))
3303 return FALSE;
3304
3305 break;
3306 }
3307 }
3308
3309 /* We did not see superfield in subfield */
3310 if (!seenother)
3311 return FALSE;
3312 }
3313
3314 /* We saw everything from subset in the superset */
3315 return TRUE;
3316 }
3317
3318
3319 /**
3320 * gst_structure_fixate:
3321 * @structure: a #GstStructure
3322 *
3323 * Fixate all values in @structure using gst_value_fixate().
3324 * @structure will be modified in-place and should be writable.
3325 */
3326 void
gst_structure_fixate(GstStructure * structure)3327 gst_structure_fixate (GstStructure * structure)
3328 {
3329 g_return_if_fail (GST_IS_STRUCTURE (structure));
3330
3331 gst_structure_foreach (structure, default_fixate, structure);
3332 }
3333
3334 static gboolean
_gst_structure_get_any_list(GstStructure * structure,GType type,const gchar * fieldname,GValueArray ** array)3335 _gst_structure_get_any_list (GstStructure * structure, GType type,
3336 const gchar * fieldname, GValueArray ** array)
3337 {
3338 GstStructureField *field;
3339 GValue val = G_VALUE_INIT;
3340
3341 g_return_val_if_fail (structure != NULL, FALSE);
3342 g_return_val_if_fail (fieldname != NULL, FALSE);
3343 g_return_val_if_fail (array != NULL, FALSE);
3344
3345 field = gst_structure_get_field (structure, fieldname);
3346
3347 if (field == NULL || G_VALUE_TYPE (&field->value) != type)
3348 return FALSE;
3349
3350 g_value_init (&val, G_TYPE_VALUE_ARRAY);
3351
3352 if (g_value_transform (&field->value, &val)) {
3353 *array = g_value_get_boxed (&val);
3354 return TRUE;
3355 }
3356
3357 g_value_unset (&val);
3358 return FALSE;
3359 }
3360
3361 /**
3362 * gst_structure_get_array:
3363 * @structure: a #GstStructure
3364 * @fieldname: the name of a field
3365 * @array: (out): a pointer to a #GValueArray
3366 *
3367 * This is useful in language bindings where unknown #GValue types are not
3368 * supported. This function will convert the %GST_TYPE_ARRAY into a newly
3369 * allocated #GValueArray and return it through @array. Be aware that this is
3370 * slower then getting the #GValue directly.
3371 *
3372 * Returns: %TRUE if the value could be set correctly. If there was no field
3373 * with @fieldname or the existing field did not contain a %GST_TYPE_ARRAY,
3374 * this function returns %FALSE.
3375 *
3376 * Since: 1.12
3377 */
3378 gboolean
gst_structure_get_array(GstStructure * structure,const gchar * fieldname,GValueArray ** array)3379 gst_structure_get_array (GstStructure * structure, const gchar * fieldname,
3380 GValueArray ** array)
3381 {
3382 return _gst_structure_get_any_list (structure, GST_TYPE_ARRAY, fieldname,
3383 array);
3384 }
3385
3386 /**
3387 * gst_structure_get_list:
3388 * @structure: a #GstStructure
3389 * @fieldname: the name of a field
3390 * @array: (out): a pointer to a #GValueArray
3391 *
3392 * This is useful in language bindings where unknown #GValue types are not
3393 * supported. This function will convert the %GST_TYPE_LIST into a newly
3394 * allocated GValueArray and return it through @array. Be aware that this is
3395 * slower then getting the #GValue directly.
3396 *
3397 * Returns: %TRUE if the value could be set correctly. If there was no field
3398 * with @fieldname or the existing field did not contain a %GST_TYPE_LIST, this
3399 * function returns %FALSE.
3400 *
3401 * Since: 1.12
3402 */
3403 gboolean
gst_structure_get_list(GstStructure * structure,const gchar * fieldname,GValueArray ** array)3404 gst_structure_get_list (GstStructure * structure, const gchar * fieldname,
3405 GValueArray ** array)
3406 {
3407 return _gst_structure_get_any_list (structure, GST_TYPE_LIST, fieldname,
3408 array);
3409 }
3410
3411 static void
_gst_structure_set_any_list(GstStructure * structure,GType type,const gchar * fieldname,const GValueArray * array)3412 _gst_structure_set_any_list (GstStructure * structure, GType type,
3413 const gchar * fieldname, const GValueArray * array)
3414 {
3415 GValue arval = G_VALUE_INIT;
3416 GValue value = G_VALUE_INIT;
3417
3418 g_return_if_fail (structure != NULL);
3419 g_return_if_fail (fieldname != NULL);
3420 g_return_if_fail (array != NULL);
3421 g_return_if_fail (IS_MUTABLE (structure));
3422
3423 g_value_init (&value, type);
3424 g_value_init (&arval, G_TYPE_VALUE_ARRAY);
3425 g_value_set_static_boxed (&arval, array);
3426
3427 if (g_value_transform (&arval, &value)) {
3428 gst_structure_id_set_value_internal (structure,
3429 g_quark_from_string (fieldname), &value);
3430 } else {
3431 g_warning ("Failed to convert a GValueArray");
3432 }
3433
3434 g_value_unset (&arval);
3435 g_value_unset (&value);
3436 }
3437
3438 /**
3439 * gst_structure_set_array:
3440 * @structure: a #GstStructure
3441 * @fieldname: the name of a field
3442 * @array: a pointer to a #GValueArray
3443 *
3444 * This is useful in language bindings where unknown GValue types are not
3445 * supported. This function will convert a @array to %GST_TYPE_ARRAY and set
3446 * the field specified by @fieldname. Be aware that this is slower then using
3447 * %GST_TYPE_ARRAY in a #GValue directly.
3448 *
3449 * Since: 1.12
3450 */
3451 void
gst_structure_set_array(GstStructure * structure,const gchar * fieldname,const GValueArray * array)3452 gst_structure_set_array (GstStructure * structure, const gchar * fieldname,
3453 const GValueArray * array)
3454 {
3455 _gst_structure_set_any_list (structure, GST_TYPE_ARRAY, fieldname, array);
3456 }
3457
3458 /**
3459 * gst_structure_set_list:
3460 * @structure: a #GstStructure
3461 * @fieldname: the name of a field
3462 * @array: a pointer to a #GValueArray
3463 *
3464 * This is useful in language bindings where unknown GValue types are not
3465 * supported. This function will convert a @array to %GST_TYPE_LIST and set
3466 * the field specified by @fieldname. Be aware that this is slower then using
3467 * %GST_TYPE_LIST in a #GValue directly.
3468 *
3469 * Since: 1.12
3470 */
3471 void
gst_structure_set_list(GstStructure * structure,const gchar * fieldname,const GValueArray * array)3472 gst_structure_set_list (GstStructure * structure, const gchar * fieldname,
3473 const GValueArray * array)
3474 {
3475 _gst_structure_set_any_list (structure, GST_TYPE_LIST, fieldname, array);
3476 }
3477