2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2002 Thomas Vander Stichele <thomas@apestaart.org>
5 * 2004 Wim Taymans <wim@fluendo.com>
6 * 2015 Jan Schmidt <jan@centricular.com>
8 * gstutils.c: Utility functions
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
29 * @short_description: Various utility functions
33 /* FIXME 2.0: suppress warnings for deprecated API such as GValueArray
34 * with newer GLib versions (>= 2.31.0) */
35 #define GLIB_DISABLE_DEPRECATION_WARNINGS
37 #include "gst_private.h"
41 #include "gstghostpad.h"
48 #include <glib/gi18n-lib.h>
49 #include "glib-compat-private.h"
51 #ifdef TIZEN_PROFILE_TV
52 #include "gstchildproxy.h"
57 gst_util_dump_mem_offset (const guchar * mem, guint size, guint offset)
60 GString *string = g_string_sized_new (50);
61 GString *chars = g_string_sized_new (18);
65 if (g_ascii_isprint (mem[i]))
66 g_string_append_c (chars, mem[i]);
68 g_string_append_c (chars, '.');
70 g_string_append_printf (string, "%02x ", mem[i]);
75 if (j == 16 || i == size) {
76 g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j + offset, mem + i - j,
77 string->str, chars->str);
78 g_string_set_size (string, 0);
79 g_string_set_size (chars, 0);
83 g_string_free (string, TRUE);
84 g_string_free (chars, TRUE);
89 * @mem: (array length=size): a pointer to the memory to dump
90 * @size: the size of the memory block to dump
92 * Dumps the memory block into a hex representation. Useful for debugging.
95 gst_util_dump_mem (const guchar * mem, guint size)
97 gst_util_dump_mem_offset (mem, size, 0);
101 * gst_util_dump_buffer:
102 * @buf: a #GstBuffer whose memory to dump
104 * Dumps the buffer memory into a hex representation. Useful for debugging.
109 gst_util_dump_buffer (GstBuffer * buf)
117 n_memory = gst_buffer_n_memory (buf);
120 if (gst_buffer_map (buf, &map, GST_MAP_READ)) {
121 gst_util_dump_mem (map.data, map.size);
122 gst_buffer_unmap (buf, &map);
124 } else if (n_memory > 1) {
125 /* gst_buffer_map() will merge multiple memory segments into one contiguous
126 * area so we need to use gst_memory_map() in order not to affect the
129 for (i = 0; i < n_memory; ++i) {
130 g_print ("[Memory #%u]\n", i);
131 mem = gst_buffer_get_memory (buf, i);
132 if (gst_memory_map (mem, &map, GST_MAP_READ)) {
133 gst_util_dump_mem_offset (map.data, map.size, offset);
135 gst_memory_unmap (mem, &map);
137 gst_memory_unref (mem);
140 g_print ("[Empty]\n");
145 * gst_util_set_value_from_string:
146 * @value: (out caller-allocates): the value to set
147 * @value_str: the string to get the value from
149 * Converts the string to the type of the value and
150 * sets the value with it.
152 * Note that this function is dangerous as it does not return any indication
153 * if the conversion worked or not.
156 gst_util_set_value_from_string (GValue * value, const gchar * value_str)
160 g_return_if_fail (value != NULL);
161 g_return_if_fail (value_str != NULL);
163 GST_CAT_DEBUG (GST_CAT_PARAMS, "parsing '%s' to type %s", value_str,
164 g_type_name (G_VALUE_TYPE (value)));
166 res = gst_value_deserialize (value, value_str);
167 if (!res && G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
168 /* backwards compat, all booleans that fail to parse are false */
169 g_value_set_boolean (value, FALSE);
172 g_return_if_fail (res);
176 * gst_util_set_object_arg:
177 * @object: the object to set the argument of
178 * @name: the name of the argument to set
179 * @value: the string value to set
181 * Converts the string value to the type of the objects argument and
182 * sets the argument with it.
184 * Note that this function silently returns if @object has no property named
185 * @name or when @value cannot be converted to the type of the property.
188 gst_util_set_object_arg (GObject * object, const gchar * name,
195 g_return_if_fail (G_IS_OBJECT (object));
196 g_return_if_fail (name != NULL);
197 g_return_if_fail (value != NULL);
199 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
203 value_type = pspec->value_type;
205 GST_CAT_DEBUG_OBJECT (GST_CAT_PARAMS, object,
206 "pspec->flags is %d, pspec->value_type is %s",
207 pspec->flags, g_type_name (value_type));
209 if (!(pspec->flags & G_PARAM_WRITABLE))
212 g_value_init (&v, value_type);
214 /* special case for element <-> xml (de)serialisation */
215 if (value_type == GST_TYPE_STRUCTURE && strcmp (value, "NULL") == 0) {
216 g_value_set_boxed (&v, NULL);
220 if (!gst_value_deserialize_with_pspec (&v, value, pspec))
225 g_object_set_property (object, pspec->name, &v);
230 * gst_util_set_object_array:
231 * @object: the object to set the array to
232 * @name: the name of the property to set
233 * @array: a #GValueArray containing the values
235 * Transfer a #GValueArray to %GST_TYPE_ARRAY and set this value on the
236 * specified property name. This allow language bindings to set GST_TYPE_ARRAY
237 * properties which are otherwise not an accessible type.
242 gst_util_set_object_array (GObject * object, const gchar * name,
243 const GValueArray * array)
245 GValue v1 = G_VALUE_INIT, v2 = G_VALUE_INIT;
246 gboolean ret = FALSE;
248 g_value_init (&v1, G_TYPE_VALUE_ARRAY);
249 g_value_init (&v2, GST_TYPE_ARRAY);
251 g_value_set_static_boxed (&v1, array);
253 if (g_value_transform (&v1, &v2)) {
254 g_object_set_property (object, name, &v2);
265 * gst_util_get_object_array:
266 * @object: the object to set the array to
267 * @name: the name of the property to set
268 * @array: (out): a return #GValueArray
270 * Get a property of type %GST_TYPE_ARRAY and transform it into a
271 * #GValueArray. This allow language bindings to get GST_TYPE_ARRAY
272 * properties which are otherwise not an accessible type.
277 gst_util_get_object_array (GObject * object, const gchar * name,
278 GValueArray ** array)
280 GValue v1 = G_VALUE_INIT, v2 = G_VALUE_INIT;
281 gboolean ret = FALSE;
283 g_value_init (&v1, G_TYPE_VALUE_ARRAY);
284 g_value_init (&v2, GST_TYPE_ARRAY);
286 g_object_get_property (object, name, &v2);
288 if (g_value_transform (&v2, &v1)) {
289 *array = g_value_get_boxed (&v1);
298 /* work around error C2520: conversion from unsigned __int64 to double
299 * not implemented, use signed __int64
301 * These are implemented as functions because on some platforms a 64bit int to
302 * double conversion is not defined/implemented.
306 * gst_util_guint64_to_gdouble:
307 * @value: The #guint64 value to convert to double
309 * Returns: @value casted to #gdouble
312 gst_util_guint64_to_gdouble (guint64 value)
314 if (value & G_GINT64_CONSTANT (0x8000000000000000))
315 return (gdouble) ((gint64) value) + (gdouble) 18446744073709551616.;
317 return (gdouble) ((gint64) value);
321 * gst_util_gdouble_to_guint64:
322 * @value: The #gdouble value to convert guint64 double
324 * Returns: @value casted to #guint64
327 gst_util_gdouble_to_guint64 (gdouble value)
329 if (value < (gdouble) 9223372036854775808.) /* 1 << 63 */
330 return ((guint64) ((gint64) value));
332 value -= (gdouble) 18446744073709551616.;
333 return ((guint64) ((gint64) value));
336 #ifndef HAVE_UINT128_T
337 /* convenience struct for getting high and low uint32 parts of
344 #if G_BYTE_ORDER == G_BIG_ENDIAN
352 #if defined (__x86_64__) && defined (__GNUC__)
354 gst_util_uint64_mul_uint64 (GstUInt64 * c1, GstUInt64 * c0, guint64 arg1,
357 __asm__ __volatile__ ("mulq %3":"=a" (c0->ll), "=d" (c1->ll)
358 :"a" (arg1), "g" (arg2)
361 #else /* defined (__x86_64__) */
362 /* multiply two 64-bit unsigned ints into a 128-bit unsigned int. the high
363 * and low 64 bits of the product are placed in c1 and c0 respectively.
364 * this operation cannot overflow. */
366 gst_util_uint64_mul_uint64 (GstUInt64 * c1, GstUInt64 * c0, guint64 arg1,
376 /* do 128 bits multiply
384 * -------------------
387 * "a0" is optimized away, result is stored directly in c0. "b1" is
388 * optimized away, result is stored directly in c1.
390 c0->ll = (guint64) v.l.low * n.l.low;
391 a1.ll = (guint64) v.l.low * n.l.high;
392 b0.ll = (guint64) v.l.high * n.l.low;
394 /* add the high word of a0 to the low words of a1 and b0 using c1 as
395 * scratch space to capture the carry. the low word of the result becomes
396 * the final high word of c0 */
397 c1->ll = (guint64) c0->l.high + a1.l.low + b0.l.low;
398 c0->l.high = c1->l.low;
400 /* add the carry from the result above (found in the high word of c1) and
401 * the high words of a1 and b0 to b1, the result is c1. */
402 c1->ll = (guint64) v.l.high * n.l.high + c1->l.high + a1.l.high + b0.l.high;
404 #endif /* defined (__x86_64__) */
406 #if defined (__x86_64__) && defined (__GNUC__)
407 static inline guint64
408 gst_util_div128_64 (GstUInt64 c1, GstUInt64 c0, guint64 denom)
412 __asm__ __volatile__ ("divq %3":"=a" (res)
413 :"d" (c1.ll), "a" (c0.ll), "g" (denom)
419 /* count leading zeros */
421 gst_util_clz (guint32 val)
425 s = val | (val >> 1);
429 s = ~(s | (s >> 16));
430 s = s - ((s >> 1) & 0x55555555);
431 s = (s & 0x33333333) + ((s >> 2) & 0x33333333);
432 s = (s + (s >> 4)) & 0x0f0f0f0f;
434 s = (s + (s >> 16)) & 0x3f;
439 /* based on Hacker's Delight p152 */
440 static inline guint64
441 gst_util_div128_64 (GstUInt64 c1, GstUInt64 c0, guint64 denom)
443 GstUInt64 q1, q0, rhat;
444 GstUInt64 v, cmp1, cmp2;
449 /* count number of leading zeroes, we know they must be in the high
450 * part of denom since denom > G_MAXUINT32. */
451 s = gst_util_clz (v.l.high);
454 /* normalize divisor and dividend */
456 c1.ll = (c1.ll << s) | (c0.l.high >> (32 - s));
460 q1.ll = c1.ll / v.l.high;
461 rhat.ll = c1.ll - q1.ll * v.l.high;
463 cmp1.l.high = rhat.l.low;
464 cmp1.l.low = c0.l.high;
465 cmp2.ll = q1.ll * v.l.low;
467 while (q1.l.high || cmp2.ll > cmp1.ll) {
472 cmp1.l.high = rhat.l.low;
475 c1.l.high = c1.l.low;
476 c1.l.low = c0.l.high;
477 c1.ll -= q1.ll * v.ll;
478 q0.ll = c1.ll / v.l.high;
479 rhat.ll = c1.ll - q0.ll * v.l.high;
481 cmp1.l.high = rhat.l.low;
482 cmp1.l.low = c0.l.low;
483 cmp2.ll = q0.ll * v.l.low;
485 while (q0.l.high || cmp2.ll > cmp1.ll) {
490 cmp1.l.high = rhat.l.low;
493 q0.l.high += q1.l.low;
497 #endif /* defined (__GNUC__) */
499 /* This always gives the correct result because:
500 * a) val <= G_MAXUINT64-1
501 * b) (c0,c1) <= G_MAXUINT64 * (G_MAXUINT64-1)
503 * (c0,c1) == G_MAXUINT64 * G_MAXUINT64 and denom < G_MAXUINT64
504 * (note: num==denom case is handled by short path)
505 * This means that (c0,c1) either has enough space for val
506 * or that the overall result will overflow anyway.
509 /* add correction with carry */
510 #define CORRECT(c0,c1,val) \
512 if (G_MAXUINT64 - c0.ll < val) { \
513 if (G_UNLIKELY (c1.ll == G_MAXUINT64)) \
515 return G_MAXUINT64; \
522 gst_util_uint64_scale_uint64_unchecked (guint64 val, guint64 num,
523 guint64 denom, guint64 correct)
527 /* compute 128-bit numerator product */
528 gst_util_uint64_mul_uint64 (&c1, &c0, val, num);
530 /* perform rounding correction */
531 CORRECT (c0, c1, correct);
533 /* high word as big as or bigger than denom --> overflow */
534 if (G_UNLIKELY (c1.ll >= denom))
537 /* compute quotient, fits in 64 bits */
538 return gst_util_div128_64 (c1, c0, denom);
542 #define GST_MAXUINT128 ((__uint128_t) -1)
544 gst_util_uint64_scale_uint64_unchecked (guint64 val, guint64 num,
545 guint64 denom, guint64 correct)
549 /* Calculate val * num */
550 tmp = ((__uint128_t) val) * ((__uint128_t) num);
552 /* overflow checks */
553 if (G_UNLIKELY (GST_MAXUINT128 - correct < tmp))
556 /* perform rounding correction */
559 /* Divide by denom */
562 /* if larger than G_MAXUINT64 --> overflow */
563 if (G_UNLIKELY (tmp > G_MAXUINT64))
566 /* compute quotient, fits in 64 bits */
567 return (guint64) tmp;
572 #if !defined (__x86_64__) && !defined (HAVE_UINT128_T)
574 gst_util_uint64_mul_uint32 (GstUInt64 * c1, GstUInt64 * c0, guint64 arg1,
581 c0->ll = (guint64) a.l.low * arg2;
582 c1->ll = (guint64) a.l.high * arg2 + c0->l.high;
586 /* divide a 96-bit unsigned int by a 32-bit unsigned int when we know the
587 * quotient fits into 64 bits. the high 64 bits and low 32 bits of the
588 * numerator are expected in c1 and c0 respectively. */
589 static inline guint64
590 gst_util_div96_32 (guint64 c1, guint64 c0, guint32 denom)
592 c0 += (c1 % denom) << 32;
593 return ((c1 / denom) << 32) + (c0 / denom);
596 static inline guint64
597 gst_util_uint64_scale_uint32_unchecked (guint64 val, guint32 num,
598 guint32 denom, guint32 correct)
602 /* compute 96-bit numerator product */
603 gst_util_uint64_mul_uint32 (&c1, &c0, val, num);
605 /* condition numerator based on rounding mode */
606 CORRECT (c0, c1, correct);
608 /* high 32 bits as big as or bigger than denom --> overflow */
609 if (G_UNLIKELY (c1.l.high >= denom))
612 /* compute quotient, fits in 64 bits */
613 return gst_util_div96_32 (c1.ll, c0.ll, denom);
617 /* the guts of the gst_util_uint64_scale() variants */
619 _gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom,
622 g_return_val_if_fail (denom != 0, G_MAXUINT64);
624 if (G_UNLIKELY (num == 0))
627 if (G_UNLIKELY (num == denom))
630 /* on 64bits we always use a full 128bits multiply/division */
631 #if !defined (__x86_64__) && !defined (HAVE_UINT128_T)
632 /* denom is low --> try to use 96 bit muldiv */
633 if (G_LIKELY (denom <= G_MAXUINT32)) {
634 /* num is low --> use 96 bit muldiv */
635 if (G_LIKELY (num <= G_MAXUINT32))
636 return gst_util_uint64_scale_uint32_unchecked (val, (guint32) num,
637 (guint32) denom, correct);
639 /* num is high but val is low --> swap and use 96-bit muldiv */
640 if (G_LIKELY (val <= G_MAXUINT32))
641 return gst_util_uint64_scale_uint32_unchecked (num, (guint32) val,
642 (guint32) denom, correct);
644 #endif /* !defined (__x86_64__) && !defined (HAVE_UINT128_T) */
646 /* val is high and num is high --> use 128-bit muldiv */
647 return gst_util_uint64_scale_uint64_unchecked (val, num, denom, correct);
651 * gst_util_uint64_scale:
652 * @val: the number to scale
653 * @num: the numerator of the scale ratio
654 * @denom: the denominator of the scale ratio
656 * Scale @val by the rational number @num / @denom, avoiding overflows and
657 * underflows and without loss of precision.
659 * This function can potentially be very slow if val and num are both
660 * greater than G_MAXUINT32.
662 * Returns: @val * @num / @denom. In the case of an overflow, this
663 * function returns G_MAXUINT64. If the result is not exactly
664 * representable as an integer it is truncated. See also
665 * gst_util_uint64_scale_round(), gst_util_uint64_scale_ceil(),
666 * gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
667 * gst_util_uint64_scale_int_ceil().
670 gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom)
672 return _gst_util_uint64_scale (val, num, denom, 0);
676 * gst_util_uint64_scale_round:
677 * @val: the number to scale
678 * @num: the numerator of the scale ratio
679 * @denom: the denominator of the scale ratio
681 * Scale @val by the rational number @num / @denom, avoiding overflows and
682 * underflows and without loss of precision.
684 * This function can potentially be very slow if val and num are both
685 * greater than G_MAXUINT32.
687 * Returns: @val * @num / @denom. In the case of an overflow, this
688 * function returns G_MAXUINT64. If the result is not exactly
689 * representable as an integer, it is rounded to the nearest integer
690 * (half-way cases are rounded up). See also gst_util_uint64_scale(),
691 * gst_util_uint64_scale_ceil(), gst_util_uint64_scale_int(),
692 * gst_util_uint64_scale_int_round(), gst_util_uint64_scale_int_ceil().
695 gst_util_uint64_scale_round (guint64 val, guint64 num, guint64 denom)
697 return _gst_util_uint64_scale (val, num, denom, denom >> 1);
701 * gst_util_uint64_scale_ceil:
702 * @val: the number to scale
703 * @num: the numerator of the scale ratio
704 * @denom: the denominator of the scale ratio
706 * Scale @val by the rational number @num / @denom, avoiding overflows and
707 * underflows and without loss of precision.
709 * This function can potentially be very slow if val and num are both
710 * greater than G_MAXUINT32.
712 * Returns: @val * @num / @denom. In the case of an overflow, this
713 * function returns G_MAXUINT64. If the result is not exactly
714 * representable as an integer, it is rounded up. See also
715 * gst_util_uint64_scale(), gst_util_uint64_scale_round(),
716 * gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
717 * gst_util_uint64_scale_int_ceil().
720 gst_util_uint64_scale_ceil (guint64 val, guint64 num, guint64 denom)
722 return _gst_util_uint64_scale (val, num, denom, denom - 1);
725 /* the guts of the gst_util_uint64_scale_int() variants */
727 _gst_util_uint64_scale_int (guint64 val, gint num, gint denom, gint correct)
729 g_return_val_if_fail (denom > 0, G_MAXUINT64);
730 g_return_val_if_fail (num >= 0, G_MAXUINT64);
732 if (G_UNLIKELY (num == 0))
735 if (G_UNLIKELY (num == denom))
738 if (val <= G_MAXUINT32) {
739 /* simple case. num and denom are not negative so casts are OK. when
740 * not truncating, the additions to the numerator cannot overflow
741 * because val*num <= G_MAXUINT32 * G_MAXINT32 < G_MAXUINT64 -
742 * G_MAXINT32, so there's room to add another gint32. */
743 val *= (guint64) num;
744 /* add rounding correction */
747 return val / (guint64) denom;
749 #if !defined (__x86_64__) && !defined (HAVE_UINT128_T)
750 /* num and denom are not negative so casts are OK */
751 return gst_util_uint64_scale_uint32_unchecked (val, (guint32) num,
752 (guint32) denom, (guint32) correct);
754 /* always use full 128bits scale */
755 return gst_util_uint64_scale_uint64_unchecked (val, num, denom, correct);
760 * gst_util_uint64_scale_int:
761 * @val: guint64 (such as a #GstClockTime) to scale.
762 * @num: numerator of the scale factor.
763 * @denom: denominator of the scale factor.
765 * Scale @val by the rational number @num / @denom, avoiding overflows and
766 * underflows and without loss of precision. @num must be non-negative and
767 * @denom must be positive.
769 * Returns: @val * @num / @denom. In the case of an overflow, this
770 * function returns G_MAXUINT64. If the result is not exactly
771 * representable as an integer, it is truncated. See also
772 * gst_util_uint64_scale_int_round(), gst_util_uint64_scale_int_ceil(),
773 * gst_util_uint64_scale(), gst_util_uint64_scale_round(),
774 * gst_util_uint64_scale_ceil().
777 gst_util_uint64_scale_int (guint64 val, gint num, gint denom)
779 return _gst_util_uint64_scale_int (val, num, denom, 0);
783 * gst_util_uint64_scale_int_round:
784 * @val: guint64 (such as a #GstClockTime) to scale.
785 * @num: numerator of the scale factor.
786 * @denom: denominator of the scale factor.
788 * Scale @val by the rational number @num / @denom, avoiding overflows and
789 * underflows and without loss of precision. @num must be non-negative and
790 * @denom must be positive.
792 * Returns: @val * @num / @denom. In the case of an overflow, this
793 * function returns G_MAXUINT64. If the result is not exactly
794 * representable as an integer, it is rounded to the nearest integer
795 * (half-way cases are rounded up). See also gst_util_uint64_scale_int(),
796 * gst_util_uint64_scale_int_ceil(), gst_util_uint64_scale(),
797 * gst_util_uint64_scale_round(), gst_util_uint64_scale_ceil().
800 gst_util_uint64_scale_int_round (guint64 val, gint num, gint denom)
802 /* we can use a shift to divide by 2 because denom is required to be
804 return _gst_util_uint64_scale_int (val, num, denom, denom >> 1);
808 * gst_util_uint64_scale_int_ceil:
809 * @val: guint64 (such as a #GstClockTime) to scale.
810 * @num: numerator of the scale factor.
811 * @denom: denominator of the scale factor.
813 * Scale @val by the rational number @num / @denom, avoiding overflows and
814 * underflows and without loss of precision. @num must be non-negative and
815 * @denom must be positive.
817 * Returns: @val * @num / @denom. In the case of an overflow, this
818 * function returns G_MAXUINT64. If the result is not exactly
819 * representable as an integer, it is rounded up. See also
820 * gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
821 * gst_util_uint64_scale(), gst_util_uint64_scale_round(),
822 * gst_util_uint64_scale_ceil().
825 gst_util_uint64_scale_int_ceil (guint64 val, gint num, gint denom)
827 return _gst_util_uint64_scale_int (val, num, denom, denom - 1);
831 * gst_util_seqnum_next:
833 * Return a constantly incrementing sequence number.
835 * This function is used internally to GStreamer to be able to determine which
836 * events and messages are "the same". For example, elements may set the seqnum
837 * on a segment-done message to be the same as that of the last seek event, to
838 * indicate that event and the message correspond to the same segment.
840 * This function never returns %GST_SEQNUM_INVALID (which is 0).
842 * Returns: A constantly incrementing 32-bit unsigned integer, which might
843 * overflow at some point. Use gst_util_seqnum_compare() to make sure
844 * you handle wraparound correctly.
847 gst_util_seqnum_next (void)
849 static gint counter = 1;
850 gint ret = g_atomic_int_add (&counter, 1);
852 /* Make sure we don't return 0 */
853 if (G_UNLIKELY (ret == GST_SEQNUM_INVALID))
854 ret = g_atomic_int_add (&counter, 1);
860 * gst_util_seqnum_compare:
861 * @s1: A sequence number.
862 * @s2: Another sequence number.
864 * Compare two sequence numbers, handling wraparound.
866 * The current implementation just returns (gint32)(@s1 - @s2).
868 * Returns: A negative number if @s1 is before @s2, 0 if they are equal, or a
869 * positive number if @s1 is after @s2.
872 gst_util_seqnum_compare (guint32 s1, guint32 s2)
874 return (gint32) (s1 - s2);
877 /* -----------------------------------------------------
879 * The following code will be moved out of the main
880 * gstreamer library someday.
886 * gst_element_create_all_pads:
887 * @element: (transfer none): a #GstElement to create pads for
889 * Creates a pad for each pad template that is always available.
890 * This function is only useful during object initialization of
891 * subclasses of #GstElement.
894 gst_element_create_all_pads (GstElement * element)
898 /* FIXME: lock element */
901 gst_element_class_get_pad_template_list (GST_ELEMENT_CLASS
902 (G_OBJECT_GET_CLASS (element)));
905 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
907 if (padtempl->presence == GST_PAD_ALWAYS) {
910 pad = gst_pad_new_from_template (padtempl, padtempl->name_template);
912 gst_element_add_pad (element, pad);
914 padlist = padlist->next;
919 * gst_element_get_compatible_pad_template:
920 * @element: (transfer none): a #GstElement to get a compatible pad template for
921 * @compattempl: (transfer none): the #GstPadTemplate to find a compatible
924 * Retrieves a pad template from @element that is compatible with @compattempl.
925 * Pads from compatible templates can be linked together.
927 * Returns: (transfer none) (nullable): a compatible #GstPadTemplate,
928 * or %NULL if none was found. No unreferencing is necessary.
931 gst_element_get_compatible_pad_template (GstElement * element,
932 GstPadTemplate * compattempl)
934 GstPadTemplate *newtempl = NULL;
936 GstElementClass *class;
939 g_return_val_if_fail (element != NULL, NULL);
940 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
941 g_return_val_if_fail (compattempl != NULL, NULL);
943 class = GST_ELEMENT_GET_CLASS (element);
945 padlist = gst_element_class_get_pad_template_list (class);
947 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
948 "Looking for a suitable pad template in %s out of %d templates...",
949 GST_ELEMENT_NAME (element), g_list_length (padlist));
952 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
956 * Check direction (must be opposite)
959 GST_CAT_LOG (GST_CAT_CAPS,
960 "checking pad template %s", padtempl->name_template);
961 if (padtempl->direction != compattempl->direction) {
962 GST_CAT_DEBUG (GST_CAT_CAPS,
963 "compatible direction: found %s pad template \"%s\"",
964 padtempl->direction == GST_PAD_SRC ? "src" : "sink",
965 padtempl->name_template);
967 GST_CAT_DEBUG (GST_CAT_CAPS,
968 "intersecting %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (compattempl));
969 GST_CAT_DEBUG (GST_CAT_CAPS,
970 "..and %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (padtempl));
972 compatible = gst_caps_can_intersect (GST_PAD_TEMPLATE_CAPS (compattempl),
973 GST_PAD_TEMPLATE_CAPS (padtempl));
975 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
976 (compatible ? "" : "not "));
984 padlist = g_list_next (padlist);
987 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
988 "Returning new pad template %p", newtempl);
990 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "No compatible pad template found");
996 * gst_element_get_pad_from_template:
997 * @element: (transfer none): a #GstElement.
998 * @templ: (transfer none): a #GstPadTemplate belonging to @element.
1000 * Gets a pad from @element described by @templ. If the presence of @templ is
1001 * #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
1004 * Returns: (transfer full) (nullable): the #GstPad, or %NULL if one
1005 * could not be found or created.
1008 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
1011 GstPadPresence presence;
1013 /* If this function is ever exported, we need check the validity of `element'
1014 * and `templ', and to make sure the template actually belongs to the
1017 presence = GST_PAD_TEMPLATE_PRESENCE (templ);
1020 case GST_PAD_ALWAYS:
1021 case GST_PAD_SOMETIMES:
1022 ret = gst_element_get_static_pad (element, templ->name_template);
1023 if (!ret && presence == GST_PAD_ALWAYS)
1025 ("Element %s has an ALWAYS template %s, but no pad of the same name",
1026 GST_OBJECT_NAME (element), templ->name_template);
1029 case GST_PAD_REQUEST:
1030 ret = gst_element_request_pad (element, templ, NULL, NULL);
1038 * gst_element_request_compatible_pad:
1039 * @element: a #GstElement.
1040 * @templ: the #GstPadTemplate to which the new pad should be able to link.
1042 * Requests a pad from @element. The returned pad should be unlinked and
1043 * compatible with @templ. Might return an existing pad, or request a new one.
1045 * Returns: (nullable): a #GstPad, or %NULL if one could not be found
1049 gst_element_request_compatible_pad (GstElement * element,
1050 GstPadTemplate * templ)
1052 GstPadTemplate *templ_new;
1055 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1056 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
1058 /* FIXME: should really loop through the templates, testing each for
1059 * compatibility and pad availability. */
1060 templ_new = gst_element_get_compatible_pad_template (element, templ);
1062 pad = gst_element_get_pad_from_template (element, templ_new);
1063 /* This can happen for non-request pads. */
1064 if (pad && GST_PAD_PEER (pad)) {
1065 gst_object_unref (pad);
1073 * Checks if the source pad and the sink pad can be linked.
1074 * Both @srcpad and @sinkpad must be unlinked and have a parent.
1077 gst_pad_check_link (GstPad * srcpad, GstPad * sinkpad)
1079 /* generic checks */
1080 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1081 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1083 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1084 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1086 if (GST_PAD_PEER (srcpad) != NULL) {
1087 GST_CAT_INFO (GST_CAT_PADS, "Source pad %s:%s has a peer, failed",
1088 GST_DEBUG_PAD_NAME (srcpad));
1091 if (GST_PAD_PEER (sinkpad) != NULL) {
1092 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has a peer, failed",
1093 GST_DEBUG_PAD_NAME (sinkpad));
1096 if (!GST_PAD_IS_SRC (srcpad)) {
1097 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s is not source pad, failed",
1098 GST_DEBUG_PAD_NAME (srcpad));
1101 if (!GST_PAD_IS_SINK (sinkpad)) {
1102 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s is not sink pad, failed",
1103 GST_DEBUG_PAD_NAME (sinkpad));
1106 if (GST_PAD_PARENT (srcpad) == NULL) {
1107 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s has no parent, failed",
1108 GST_DEBUG_PAD_NAME (srcpad));
1111 if (GST_PAD_PARENT (sinkpad) == NULL) {
1112 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has no parent, failed",
1113 GST_DEBUG_PAD_NAME (srcpad));
1121 * gst_element_get_compatible_pad:
1122 * @element: (transfer none): a #GstElement in which the pad should be found.
1123 * @pad: (transfer none): the #GstPad to find a compatible one for.
1124 * @caps: (nullable): the #GstCaps to use as a filter.
1126 * Looks for an unlinked pad to which the given pad can link. It is not
1127 * guaranteed that linking the pads will work, though it should work in most
1130 * This function will first attempt to find a compatible unlinked ALWAYS pad,
1131 * and if none can be found, it will request a compatible REQUEST pad by looking
1132 * at the templates of @element.
1134 * Returns: (transfer full) (nullable): the #GstPad to which a link
1135 * can be made, or %NULL if one cannot be found. gst_object_unref()
1139 gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
1143 GstPadTemplate *templ;
1145 GstPad *foundpad = NULL;
1147 GValue padptr = { 0, };
1149 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1150 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1152 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
1153 "finding pad compatible with %s:%s", GST_DEBUG_PAD_NAME (pad));
1155 g_return_val_if_fail (GST_PAD_PEER (pad) == NULL, NULL);
1159 /* try to get an existing unlinked pad */
1160 if (GST_PAD_IS_SRC (pad)) {
1161 pads = gst_element_iterate_sink_pads (element);
1162 } else if (GST_PAD_IS_SINK (pad)) {
1163 pads = gst_element_iterate_src_pads (element);
1165 pads = gst_element_iterate_pads (element);
1169 switch (gst_iterator_next (pads, &padptr)) {
1170 case GST_ITERATOR_OK:
1177 current = g_value_get_object (&padptr);
1179 GST_CAT_LOG_OBJECT (GST_CAT_ELEMENT_PADS, element,
1180 "examining pad %s:%s", GST_DEBUG_PAD_NAME (current));
1182 if (GST_PAD_IS_SRC (current)) {
1189 peer = gst_pad_get_peer (current);
1191 if (peer == NULL && gst_pad_check_link (srcpad, sinkpad)) {
1192 GstCaps *temp, *intersection;
1193 gboolean compatible;
1195 /* Now check if the two pads' caps are compatible */
1196 temp = gst_pad_query_caps (pad, NULL);
1198 intersection = gst_caps_intersect (temp, caps);
1199 gst_caps_unref (temp);
1201 intersection = temp;
1204 temp = gst_pad_query_caps (current, NULL);
1205 compatible = gst_caps_can_intersect (temp, intersection);
1206 gst_caps_unref (temp);
1207 gst_caps_unref (intersection);
1210 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
1211 "found existing unlinked compatible pad %s:%s",
1212 GST_DEBUG_PAD_NAME (current));
1213 gst_iterator_free (pads);
1215 current = gst_object_ref (current);
1216 g_value_unset (&padptr);
1220 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "incompatible pads");
1223 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1224 "already linked or cannot be linked (peer = %p)", peer);
1226 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unreffing pads");
1228 g_value_reset (&padptr);
1230 gst_object_unref (peer);
1233 case GST_ITERATOR_DONE:
1236 case GST_ITERATOR_RESYNC:
1237 gst_iterator_resync (pads);
1239 case GST_ITERATOR_ERROR:
1240 g_assert_not_reached ();
1244 g_value_unset (&padptr);
1245 gst_iterator_free (pads);
1247 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
1248 "Could not find a compatible unlinked always pad to link to %s:%s, now checking request pads",
1249 GST_DEBUG_PAD_NAME (pad));
1251 /* try to create a new one */
1252 /* requesting is a little crazy, we need a template. Let's create one */
1253 templcaps = gst_pad_query_caps (pad, NULL);
1255 GstCaps *inter = gst_caps_intersect (templcaps, caps);
1257 gst_caps_unref (templcaps);
1260 templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
1261 GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
1262 gst_caps_unref (templcaps);
1264 foundpad = gst_element_request_compatible_pad (element, templ);
1265 gst_object_unref (templ);
1268 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
1269 "found existing request pad %s:%s", GST_DEBUG_PAD_NAME (foundpad));
1273 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element,
1274 "Could not find a compatible pad to link to %s:%s",
1275 GST_DEBUG_PAD_NAME (pad));
1280 * gst_element_state_get_name:
1281 * @state: a #GstState to get the name of.
1283 * Gets a string representing the given state.
1285 * Returns: (transfer none): a string with the name of the state.
1288 gst_element_state_get_name (GstState state)
1291 case GST_STATE_VOID_PENDING:
1292 return "VOID_PENDING";
1293 case GST_STATE_NULL:
1295 case GST_STATE_READY:
1297 case GST_STATE_PLAYING:
1299 case GST_STATE_PAUSED:
1302 /* This is a memory leak */
1303 return g_strdup_printf ("UNKNOWN!(%d)", state);
1308 * gst_element_state_change_return_get_name:
1309 * @state_ret: a #GstStateChangeReturn to get the name of.
1311 * Gets a string representing the given state change result.
1313 * Returns: (transfer none): a string with the name of the state
1317 gst_element_state_change_return_get_name (GstStateChangeReturn state_ret)
1319 switch (state_ret) {
1320 case GST_STATE_CHANGE_FAILURE:
1322 case GST_STATE_CHANGE_SUCCESS:
1324 case GST_STATE_CHANGE_ASYNC:
1326 case GST_STATE_CHANGE_NO_PREROLL:
1327 return "NO PREROLL";
1329 /* This is a memory leak */
1330 return g_strdup_printf ("UNKNOWN!(%d)", state_ret);
1335 * gst_state_change_get_name:
1336 * @transition: a #GstStateChange to get the name of.
1338 * Gets a string representing the given state transition.
1340 * Returns: (transfer none): a string with the name of the state
1346 gst_state_change_get_name (GstStateChange transition)
1348 switch (transition) {
1349 case GST_STATE_CHANGE_NULL_TO_READY:
1350 return "NULL->READY";
1351 case GST_STATE_CHANGE_READY_TO_PAUSED:
1352 return "READY->PAUSED";
1353 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1354 return "PAUSED->PLAYING";
1355 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1356 return "PLAYING->PAUSED";
1357 case GST_STATE_CHANGE_PAUSED_TO_READY:
1358 return "PAUSED->READY";
1359 case GST_STATE_CHANGE_READY_TO_NULL:
1360 return "READY->NULL";
1361 case GST_STATE_CHANGE_NULL_TO_NULL:
1362 return "NULL->NULL";
1363 case GST_STATE_CHANGE_READY_TO_READY:
1364 return "READY->READY";
1365 case GST_STATE_CHANGE_PAUSED_TO_PAUSED:
1366 return "PAUSED->PAUSED";
1367 case GST_STATE_CHANGE_PLAYING_TO_PLAYING:
1368 return "PLAYING->PLAYING";
1371 return "Unknown state return";
1376 gst_element_factory_can_accept_all_caps_in_direction (GstElementFactory *
1377 factory, const GstCaps * caps, GstPadDirection direction)
1381 g_return_val_if_fail (factory != NULL, FALSE);
1382 g_return_val_if_fail (caps != NULL, FALSE);
1384 templates = factory->staticpadtemplates;
1387 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1389 if (template->direction == direction) {
1390 GstCaps *templcaps = gst_static_caps_get (&template->static_caps);
1392 if (gst_caps_is_always_compatible (caps, templcaps)) {
1393 gst_caps_unref (templcaps);
1396 gst_caps_unref (templcaps);
1398 templates = g_list_next (templates);
1405 gst_element_factory_can_accept_any_caps_in_direction (GstElementFactory *
1406 factory, const GstCaps * caps, GstPadDirection direction)
1410 g_return_val_if_fail (factory != NULL, FALSE);
1411 g_return_val_if_fail (caps != NULL, FALSE);
1413 templates = factory->staticpadtemplates;
1416 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1418 if (template->direction == direction) {
1419 GstCaps *templcaps = gst_static_caps_get (&template->static_caps);
1421 if (gst_caps_can_intersect (caps, templcaps)) {
1422 gst_caps_unref (templcaps);
1425 gst_caps_unref (templcaps);
1427 templates = g_list_next (templates);
1434 * gst_element_factory_can_sink_all_caps:
1435 * @factory: factory to query
1436 * @caps: the caps to check
1438 * Checks if the factory can sink all possible capabilities.
1440 * Returns: %TRUE if the caps are fully compatible.
1443 gst_element_factory_can_sink_all_caps (GstElementFactory * factory,
1444 const GstCaps * caps)
1446 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1451 * gst_element_factory_can_src_all_caps:
1452 * @factory: factory to query
1453 * @caps: the caps to check
1455 * Checks if the factory can src all possible capabilities.
1457 * Returns: %TRUE if the caps are fully compatible.
1460 gst_element_factory_can_src_all_caps (GstElementFactory * factory,
1461 const GstCaps * caps)
1463 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1468 * gst_element_factory_can_sink_any_caps:
1469 * @factory: factory to query
1470 * @caps: the caps to check
1472 * Checks if the factory can sink any possible capability.
1474 * Returns: %TRUE if the caps have a common subset.
1477 gst_element_factory_can_sink_any_caps (GstElementFactory * factory,
1478 const GstCaps * caps)
1480 return gst_element_factory_can_accept_any_caps_in_direction (factory, caps,
1485 * gst_element_factory_can_src_any_caps:
1486 * @factory: factory to query
1487 * @caps: the caps to check
1489 * Checks if the factory can src any possible capability.
1491 * Returns: %TRUE if the caps have a common subset.
1494 gst_element_factory_can_src_any_caps (GstElementFactory * factory,
1495 const GstCaps * caps)
1497 return gst_element_factory_can_accept_any_caps_in_direction (factory, caps,
1501 /* if return val is true, *direct_child is a caller-owned ref on the direct
1502 * child of ancestor that is part of object's ancestry */
1504 object_has_ancestor (GstObject * object, GstObject * ancestor,
1505 GstObject ** direct_child)
1507 GstObject *child, *parent;
1510 *direct_child = NULL;
1512 child = gst_object_ref (object);
1513 parent = gst_object_get_parent (object);
1516 if (ancestor == parent) {
1518 *direct_child = child;
1520 gst_object_unref (child);
1521 gst_object_unref (parent);
1525 gst_object_unref (child);
1527 parent = gst_object_get_parent (parent);
1530 gst_object_unref (child);
1535 /* caller owns return */
1537 find_common_root (GstObject * o1, GstObject * o2)
1539 GstObject *top = o1;
1540 GstObject *kid1, *kid2;
1541 GstObject *root = NULL;
1543 while (GST_OBJECT_PARENT (top))
1544 top = GST_OBJECT_PARENT (top);
1546 /* the itsy-bitsy spider... */
1548 if (!object_has_ancestor (o2, top, &kid2))
1551 root = gst_object_ref (top);
1553 if (!object_has_ancestor (o1, kid2, &kid1)) {
1554 gst_object_unref (kid2);
1557 gst_object_unref (root);
1559 if (!object_has_ancestor (o2, kid1, &kid2)) {
1560 gst_object_unref (kid1);
1563 gst_object_unref (root);
1568 /* caller does not own return */
1570 ghost_up (GstElement * e, GstPad * pad)
1572 static gint ghost_pad_index = 0;
1577 GstObject *parent = GST_OBJECT_PARENT (e);
1579 name = g_strdup_printf ("ghost%d", ghost_pad_index++);
1580 gpad = gst_ghost_pad_new (name, pad);
1583 GST_STATE_LOCK (parent);
1584 gst_element_get_state (GST_ELEMENT (parent), ¤t, &next, 0);
1586 if (current > GST_STATE_READY || next >= GST_STATE_PAUSED)
1587 gst_pad_set_active (gpad, TRUE);
1589 if (!gst_element_add_pad ((GstElement *) parent, gpad)) {
1590 g_warning ("Pad named %s already exists in element %s\n",
1591 GST_OBJECT_NAME (gpad), GST_OBJECT_NAME (parent));
1592 GST_STATE_UNLOCK (parent);
1595 GST_STATE_UNLOCK (parent);
1601 remove_pad (gpointer ppad, gpointer unused)
1605 if (!gst_element_remove_pad ((GstElement *) GST_OBJECT_PARENT (pad), pad))
1606 g_warning ("Couldn't remove pad %s from element %s",
1607 GST_OBJECT_NAME (pad), GST_OBJECT_NAME (GST_OBJECT_PARENT (pad)));
1611 prepare_link_maybe_ghosting (GstPad ** src, GstPad ** sink,
1612 GSList ** pads_created)
1616 GSList *pads_created_local = NULL;
1618 g_assert (pads_created);
1620 e1 = GST_OBJECT_PARENT (*src);
1621 e2 = GST_OBJECT_PARENT (*sink);
1623 if (G_UNLIKELY (e1 == NULL)) {
1624 GST_CAT_WARNING (GST_CAT_ELEMENT_PADS,
1625 "Trying to ghost a pad that doesn't have a parent: %" GST_PTR_FORMAT,
1629 if (G_UNLIKELY (e2 == NULL)) {
1630 GST_CAT_WARNING (GST_CAT_ELEMENT_PADS,
1631 "Trying to ghost a pad that doesn't have a parent: %" GST_PTR_FORMAT,
1636 if (GST_OBJECT_PARENT (e1) == GST_OBJECT_PARENT (e2)) {
1637 GST_CAT_INFO (GST_CAT_PADS, "%s and %s in same bin, no need for ghost pads",
1638 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1642 GST_CAT_INFO (GST_CAT_PADS, "%s and %s not in same bin, making ghost pads",
1643 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1645 /* we need to setup some ghost pads */
1646 root = find_common_root (e1, e2);
1648 if (GST_OBJECT_PARENT (e1) == NULL)
1649 g_warning ("Trying to link elements %s and %s that don't share a common "
1650 "ancestor: %s hasn't been added to a bin or pipeline, but %s is in %s",
1651 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
1652 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
1653 GST_ELEMENT_NAME (GST_OBJECT_PARENT (e2)));
1654 else if (GST_OBJECT_PARENT (e2) == NULL)
1655 g_warning ("Trying to link elements %s and %s that don't share a common "
1656 "ancestor: %s hasn't been added to a bin or pipeline, and %s is in %s",
1657 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
1658 GST_ELEMENT_NAME (e2), GST_ELEMENT_NAME (e1),
1659 GST_ELEMENT_NAME (GST_OBJECT_PARENT (e1)));
1661 g_warning ("Trying to link elements %s and %s that don't share a common "
1662 "ancestor: %s is in %s, and %s is in %s",
1663 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
1664 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (GST_OBJECT_PARENT (e1)),
1665 GST_ELEMENT_NAME (e2), GST_ELEMENT_NAME (GST_OBJECT_PARENT (e2)));
1669 while (GST_OBJECT_PARENT (e1) != root) {
1670 *src = ghost_up ((GstElement *) e1, *src);
1673 e1 = GST_OBJECT_PARENT (*src);
1674 pads_created_local = g_slist_prepend (pads_created_local, *src);
1676 while (GST_OBJECT_PARENT (e2) != root) {
1677 *sink = ghost_up ((GstElement *) e2, *sink);
1680 e2 = GST_OBJECT_PARENT (*sink);
1681 pads_created_local = g_slist_prepend (pads_created_local, *sink);
1684 gst_object_unref (root);
1685 *pads_created = g_slist_concat (*pads_created, pads_created_local);
1689 gst_object_unref (root);
1690 g_slist_foreach (pads_created_local, remove_pad, NULL);
1691 g_slist_free (pads_created_local);
1696 pad_link_maybe_ghosting (GstPad * src, GstPad * sink, GstPadLinkCheck flags)
1698 GSList *pads_created = NULL;
1701 if (!prepare_link_maybe_ghosting (&src, &sink, &pads_created)) {
1704 ret = (gst_pad_link_full (src, sink, flags) == GST_PAD_LINK_OK);
1708 g_slist_foreach (pads_created, remove_pad, NULL);
1710 g_slist_free (pads_created);
1716 * gst_pad_link_maybe_ghosting_full:
1719 * @flags: some #GstPadLinkCheck flags
1721 * Links @src to @sink, creating any #GstGhostPad's in between as necessary.
1723 * This is a convenience function to save having to create and add intermediate
1724 * #GstGhostPad's as required for linking across #GstBin boundaries.
1726 * If @src or @sink pads don't have parent elements or do not share a common
1727 * ancestor, the link will fail.
1729 * Calling gst_pad_link_maybe_ghosting_full() with
1730 * @flags == %GST_PAD_LINK_CHECK_DEFAULT is the recommended way of linking
1731 * pads with safety checks applied.
1733 * Returns: whether the link succeeded.
1738 gst_pad_link_maybe_ghosting_full (GstPad * src, GstPad * sink,
1739 GstPadLinkCheck flags)
1741 g_return_val_if_fail (GST_IS_PAD (src), FALSE);
1742 g_return_val_if_fail (GST_IS_PAD (sink), FALSE);
1744 return pad_link_maybe_ghosting (src, sink, flags);
1748 * gst_pad_link_maybe_ghosting:
1752 * Links @src to @sink, creating any #GstGhostPad's in between as necessary.
1754 * This is a convenience function to save having to create and add intermediate
1755 * #GstGhostPad's as required for linking across #GstBin boundaries.
1757 * If @src or @sink pads don't have parent elements or do not share a common
1758 * ancestor, the link will fail.
1760 * Returns: whether the link succeeded.
1765 gst_pad_link_maybe_ghosting (GstPad * src, GstPad * sink)
1767 g_return_val_if_fail (GST_IS_PAD (src), FALSE);
1768 g_return_val_if_fail (GST_IS_PAD (sink), FALSE);
1770 return gst_pad_link_maybe_ghosting_full (src, sink,
1771 GST_PAD_LINK_CHECK_DEFAULT);
1775 release_and_unref_pad (GstElement * element, GstPad * pad, gboolean requestpad)
1779 gst_element_release_request_pad (element, pad);
1780 gst_object_unref (pad);
1785 * gst_element_link_pads_full:
1786 * @src: a #GstElement containing the source pad.
1787 * @srcpadname: (nullable): the name of the #GstPad in source element
1788 * or %NULL for any pad.
1789 * @dest: (transfer none): the #GstElement containing the destination pad.
1790 * @destpadname: (nullable): the name of the #GstPad in destination element,
1791 * or %NULL for any pad.
1792 * @flags: the #GstPadLinkCheck to be performed when linking pads.
1794 * Links the two named pads of the source and destination elements.
1795 * Side effect is that if one of the pads has no parent, it becomes a
1796 * child of the parent of the other element. If they have different
1797 * parents, the link fails.
1799 * Calling gst_element_link_pads_full() with @flags == %GST_PAD_LINK_CHECK_DEFAULT
1800 * is the same as calling gst_element_link_pads() and the recommended way of
1801 * linking pads with safety checks applied.
1803 * This is a convenience function for gst_pad_link_full().
1805 * Returns: %TRUE if the pads could be linked, %FALSE otherwise.
1808 gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
1809 GstElement * dest, const gchar * destpadname, GstPadLinkCheck flags)
1811 const GList *srcpads, *destpads, *srctempls, *desttempls, *l;
1812 GstPad *srcpad, *destpad;
1813 GstPadTemplate *srctempl, *desttempl;
1814 GstElementClass *srcclass, *destclass;
1815 gboolean srcrequest, destrequest;
1818 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1819 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1821 GST_CAT_INFO (GST_CAT_ELEMENT_PADS,
1822 "trying to link element %s:%s to element %s:%s", GST_ELEMENT_NAME (src),
1823 srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
1824 destpadname ? destpadname : "(any)");
1827 destrequest = FALSE;
1831 /* name specified, look it up */
1832 if (!(srcpad = gst_element_get_static_pad (src, srcpadname))) {
1833 if ((srcpad = gst_element_request_pad_simple (src, srcpadname)))
1837 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1838 GST_ELEMENT_NAME (src), srcpadname);
1841 if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
1842 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
1843 GST_DEBUG_PAD_NAME (srcpad));
1844 release_and_unref_pad (src, srcpad, srcrequest);
1847 if (GST_PAD_PEER (srcpad) != NULL) {
1848 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1849 "pad %s:%s is already linked to %s:%s", GST_DEBUG_PAD_NAME (srcpad),
1850 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1851 /* already linked request pads look like static pads, so the request pad
1852 * was never requested a second time above, so no need to release it */
1853 gst_object_unref (srcpad);
1859 /* no name given, get the first available pad */
1860 GST_OBJECT_LOCK (src);
1861 srcpads = GST_ELEMENT_PADS (src);
1862 srcpad = srcpads ? GST_PAD_CAST (srcpads->data) : NULL;
1864 gst_object_ref (srcpad);
1865 GST_OBJECT_UNLOCK (src);
1868 /* get a destination pad */
1870 /* name specified, look it up */
1871 if (!(destpad = gst_element_get_static_pad (dest, destpadname))) {
1872 if ((destpad = gst_element_request_pad_simple (dest, destpadname)))
1876 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1877 GST_ELEMENT_NAME (dest), destpadname);
1878 release_and_unref_pad (src, srcpad, srcrequest);
1881 if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
1882 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
1883 GST_DEBUG_PAD_NAME (destpad));
1884 release_and_unref_pad (src, srcpad, srcrequest);
1885 release_and_unref_pad (dest, destpad, destrequest);
1888 if (GST_PAD_PEER (destpad) != NULL) {
1889 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1890 "pad %s:%s is already linked to %s:%s",
1891 GST_DEBUG_PAD_NAME (destpad),
1892 GST_DEBUG_PAD_NAME (GST_PAD_PEER (destpad)));
1893 release_and_unref_pad (src, srcpad, srcrequest);
1894 /* already linked request pads look like static pads, so the request pad
1895 * was never requested a second time above, so no need to release it */
1896 gst_object_unref (destpad);
1902 /* no name given, get the first available pad */
1903 GST_OBJECT_LOCK (dest);
1904 destpads = GST_ELEMENT_PADS (dest);
1905 destpad = destpads ? GST_PAD_CAST (destpads->data) : NULL;
1907 gst_object_ref (destpad);
1908 GST_OBJECT_UNLOCK (dest);
1911 if (srcpadname && destpadname) {
1914 /* two explicitly specified pads */
1915 result = pad_link_maybe_ghosting (srcpad, destpad, flags);
1918 gst_object_unref (srcpad);
1919 gst_object_unref (destpad);
1921 release_and_unref_pad (src, srcpad, srcrequest);
1922 release_and_unref_pad (dest, destpad, destrequest);
1928 /* loop through the allowed pads in the source, trying to find a
1929 * compatible destination pad */
1930 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1931 "looping through allowed src and dest pads");
1933 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying src pad %s:%s",
1934 GST_DEBUG_PAD_NAME (srcpad));
1935 if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
1936 (GST_PAD_PEER (srcpad) == NULL)) {
1937 gboolean temprequest = FALSE;
1942 gst_object_ref (temp);
1944 temp = gst_element_get_compatible_pad (dest, srcpad, NULL);
1945 if (temp && GST_PAD_PAD_TEMPLATE (temp)
1946 && GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (temp)) ==
1952 if (temp && pad_link_maybe_ghosting (srcpad, temp, flags)) {
1953 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1954 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (temp));
1956 gst_object_unref (destpad);
1957 gst_object_unref (srcpad);
1958 gst_object_unref (temp);
1964 gst_element_release_request_pad (dest, temp);
1965 gst_object_unref (temp);
1968 /* find a better way for this mess */
1970 srcpads = g_list_next (srcpads);
1972 gst_object_unref (srcpad);
1973 srcpad = GST_PAD_CAST (srcpads->data);
1974 gst_object_ref (srcpad);
1980 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
1981 GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
1982 /* no need to release any request pad as both src- and destpadname must be
1983 * set to end up here, but this case has already been taken care of above */
1985 gst_object_unref (destpad);
1989 release_and_unref_pad (src, srcpad, srcrequest);
1994 /* loop through the existing pads in the destination */
1996 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying dest pad %s:%s",
1997 GST_DEBUG_PAD_NAME (destpad));
1998 if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
1999 (GST_PAD_PEER (destpad) == NULL)) {
2000 GstPad *temp = gst_element_get_compatible_pad (src, destpad, NULL);
2001 gboolean temprequest = FALSE;
2003 if (temp && GST_PAD_PAD_TEMPLATE (temp)
2004 && GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (temp)) ==
2009 if (temp && pad_link_maybe_ghosting (temp, destpad, flags)) {
2010 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
2011 GST_DEBUG_PAD_NAME (temp), GST_DEBUG_PAD_NAME (destpad));
2012 gst_object_unref (temp);
2013 gst_object_unref (destpad);
2017 release_and_unref_pad (src, temp, temprequest);
2020 destpads = g_list_next (destpads);
2022 gst_object_unref (destpad);
2023 destpad = GST_PAD_CAST (destpads->data);
2024 gst_object_ref (destpad);
2031 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
2032 GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
2033 release_and_unref_pad (dest, destpad, destrequest);
2036 /* no need to release any request pad as the case of unset destpatname and
2037 * destpad being a request pad has already been taken care of when looking
2038 * though the destination pads above */
2040 gst_object_unref (destpad);
2045 srcclass = GST_ELEMENT_GET_CLASS (src);
2046 destclass = GST_ELEMENT_GET_CLASS (dest);
2048 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
2049 "we might have request pads on both sides, checking...");
2050 srctempls = gst_element_class_get_pad_template_list (srcclass);
2051 desttempls = gst_element_class_get_pad_template_list (destclass);
2053 if (srctempls && desttempls) {
2055 srctempl = (GstPadTemplate *) srctempls->data;
2056 if (srctempl->presence == GST_PAD_REQUEST) {
2057 for (l = desttempls; l; l = l->next) {
2058 desttempl = (GstPadTemplate *) l->data;
2059 if (desttempl->presence == GST_PAD_REQUEST &&
2060 desttempl->direction != srctempl->direction) {
2061 GstCaps *srccaps, *destcaps;
2063 srccaps = gst_pad_template_get_caps (srctempl);
2064 destcaps = gst_pad_template_get_caps (desttempl);
2065 if (gst_caps_is_always_compatible (srccaps, destcaps)) {
2067 gst_element_request_pad (src, srctempl,
2068 srctempl->name_template, NULL);
2070 gst_element_request_pad (dest, desttempl,
2071 desttempl->name_template, NULL);
2072 if (srcpad && destpad
2073 && pad_link_maybe_ghosting (srcpad, destpad, flags)) {
2074 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
2075 "linked pad %s:%s to pad %s:%s",
2076 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
2077 gst_object_unref (srcpad);
2078 gst_object_unref (destpad);
2079 gst_caps_unref (srccaps);
2080 gst_caps_unref (destcaps);
2083 /* it failed, so we release the request pads */
2085 gst_element_release_request_pad (src, srcpad);
2086 gst_object_unref (srcpad);
2089 gst_element_release_request_pad (dest, destpad);
2090 gst_object_unref (destpad);
2093 gst_caps_unref (srccaps);
2094 gst_caps_unref (destcaps);
2098 srctempls = srctempls->next;
2102 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s",
2103 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
2108 * gst_element_link_pads:
2109 * @src: a #GstElement containing the source pad.
2110 * @srcpadname: (nullable): the name of the #GstPad in source element
2111 * or %NULL for any pad.
2112 * @dest: (transfer none): the #GstElement containing the destination pad.
2113 * @destpadname: (nullable): the name of the #GstPad in destination element,
2114 * or %NULL for any pad.
2116 * Links the two named pads of the source and destination elements.
2117 * Side effect is that if one of the pads has no parent, it becomes a
2118 * child of the parent of the other element. If they have different
2119 * parents, the link fails.
2121 * Returns: %TRUE if the pads could be linked, %FALSE otherwise.
2124 gst_element_link_pads (GstElement * src, const gchar * srcpadname,
2125 GstElement * dest, const gchar * destpadname)
2127 return gst_element_link_pads_full (src, srcpadname, dest, destpadname,
2128 GST_PAD_LINK_CHECK_DEFAULT);
2132 * gst_element_link_pads_filtered:
2133 * @src: a #GstElement containing the source pad.
2134 * @srcpadname: (nullable): the name of the #GstPad in source element
2135 * or %NULL for any pad.
2136 * @dest: (transfer none): the #GstElement containing the destination pad.
2137 * @destpadname: (nullable): the name of the #GstPad in destination element
2138 * or %NULL for any pad.
2139 * @filter: (transfer none) (nullable): the #GstCaps to filter the link,
2140 * or %NULL for no filter.
2142 * Links the two named pads of the source and destination elements. Side effect
2143 * is that if one of the pads has no parent, it becomes a child of the parent of
2144 * the other element. If they have different parents, the link fails. If @caps
2145 * is not %NULL, makes sure that the caps of the link is a subset of @caps.
2147 * Returns: %TRUE if the pads could be linked, %FALSE otherwise.
2150 gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
2151 GstElement * dest, const gchar * destpadname, GstCaps * filter)
2154 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
2155 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
2156 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), FALSE);
2159 GstElement *capsfilter;
2161 GstState state, pending;
2164 capsfilter = gst_element_factory_make ("capsfilter", NULL);
2166 GST_CAT_ERROR (GST_CAT_ELEMENT_PADS, "Could not make a capsfilter");
2170 parent = gst_object_get_parent (GST_OBJECT (src));
2171 g_return_val_if_fail (GST_IS_BIN (parent), FALSE);
2173 gst_element_get_state (GST_ELEMENT_CAST (parent), &state, &pending, 0);
2175 if (!gst_bin_add (GST_BIN (parent), capsfilter)) {
2176 GST_CAT_ERROR (GST_CAT_ELEMENT_PADS, "Could not add capsfilter");
2177 gst_object_unref (parent);
2181 if (pending != GST_STATE_VOID_PENDING)
2184 gst_element_set_state (capsfilter, state);
2186 gst_object_unref (parent);
2188 g_object_set (capsfilter, "caps", filter, NULL);
2190 lr1 = gst_element_link_pads (src, srcpadname, capsfilter, "sink");
2191 lr2 = gst_element_link_pads (capsfilter, "src", dest, destpadname);
2196 GST_INFO ("Could not link pads: %s:%s - capsfilter:sink",
2197 GST_ELEMENT_NAME (src), srcpadname);
2199 GST_INFO ("Could not link pads: capsfilter:src - %s:%s",
2200 GST_ELEMENT_NAME (dest), destpadname);
2202 gst_element_set_state (capsfilter, GST_STATE_NULL);
2203 /* this will unlink and unref as appropriate */
2204 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (capsfilter)), capsfilter);
2208 if (gst_element_link_pads (src, srcpadname, dest, destpadname)) {
2211 GST_INFO ("Could not link pads: %s:%s - %s:%s", GST_ELEMENT_NAME (src),
2212 srcpadname, GST_ELEMENT_NAME (dest), destpadname);
2220 * @src: (transfer none): a #GstElement containing the source pad.
2221 * @dest: (transfer none): the #GstElement containing the destination pad.
2223 * Links @src to @dest. The link must be from source to
2224 * destination; the other direction will not be tried. The function looks for
2225 * existing pads that aren't linked yet. It will request new pads if necessary.
2226 * Such pads need to be released manually when unlinking.
2227 * If multiple links are possible, only one is established.
2229 * Make sure you have added your elements to a bin or pipeline with
2230 * gst_bin_add() before trying to link them.
2232 * Returns: %TRUE if the elements could be linked, %FALSE otherwise.
2235 gst_element_link (GstElement * src, GstElement * dest)
2237 return gst_element_link_pads (src, NULL, dest, NULL);
2241 * gst_element_link_many:
2242 * @element_1: (transfer none): the first #GstElement in the link chain.
2243 * @element_2: (transfer none): the second #GstElement in the link chain.
2244 * @...: the %NULL-terminated list of elements to link in order.
2246 * Chain together a series of elements. Uses gst_element_link().
2247 * Make sure you have added your elements to a bin or pipeline with
2248 * gst_bin_add() before trying to link them.
2250 * Returns: %TRUE on success, %FALSE otherwise.
2253 gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
2255 gboolean res = TRUE;
2258 g_return_val_if_fail (GST_IS_ELEMENT (element_1), FALSE);
2259 g_return_val_if_fail (GST_IS_ELEMENT (element_2), FALSE);
2261 va_start (args, element_2);
2264 if (!gst_element_link (element_1, element_2)) {
2269 element_1 = element_2;
2270 element_2 = va_arg (args, GstElement *);
2279 * gst_element_link_filtered:
2280 * @src: a #GstElement containing the source pad.
2281 * @dest: (transfer none): the #GstElement containing the destination pad.
2282 * @filter: (transfer none) (nullable): the #GstCaps to filter the link,
2283 * or %NULL for no filter.
2285 * Links @src to @dest using the given caps as filtercaps.
2286 * The link must be from source to
2287 * destination; the other direction will not be tried. The function looks for
2288 * existing pads that aren't linked yet. It will request new pads if necessary.
2289 * If multiple links are possible, only one is established.
2291 * Make sure you have added your elements to a bin or pipeline with
2292 * gst_bin_add() before trying to link them.
2294 * Returns: %TRUE if the pads could be linked, %FALSE otherwise.
2297 gst_element_link_filtered (GstElement * src, GstElement * dest,
2300 return gst_element_link_pads_filtered (src, NULL, dest, NULL, filter);
2304 * gst_element_unlink_pads:
2305 * @src: a (transfer none): #GstElement containing the source pad.
2306 * @srcpadname: the name of the #GstPad in source element.
2307 * @dest: (transfer none): a #GstElement containing the destination pad.
2308 * @destpadname: the name of the #GstPad in destination element.
2310 * Unlinks the two named pads of the source and destination elements.
2312 * This is a convenience function for gst_pad_unlink().
2315 gst_element_unlink_pads (GstElement * src, const gchar * srcpadname,
2316 GstElement * dest, const gchar * destpadname)
2318 GstPad *srcpad, *destpad;
2319 gboolean srcrequest, destrequest;
2321 srcrequest = destrequest = FALSE;
2323 g_return_if_fail (src != NULL);
2324 g_return_if_fail (GST_IS_ELEMENT (src));
2325 g_return_if_fail (srcpadname != NULL);
2326 g_return_if_fail (dest != NULL);
2327 g_return_if_fail (GST_IS_ELEMENT (dest));
2328 g_return_if_fail (destpadname != NULL);
2330 /* obtain the pads requested */
2331 if (!(srcpad = gst_element_get_static_pad (src, srcpadname)))
2332 if ((srcpad = gst_element_request_pad_simple (src, srcpadname)))
2334 if (srcpad == NULL) {
2335 GST_CAT_WARNING_OBJECT (GST_CAT_ELEMENT_PADS, src,
2336 "source element has no pad \"%s\"", srcpadname);
2339 if (!(destpad = gst_element_get_static_pad (dest, destpadname)))
2340 if ((destpad = gst_element_request_pad_simple (dest, destpadname)))
2342 if (destpad == NULL) {
2343 GST_CAT_WARNING_OBJECT (GST_CAT_ELEMENT_PADS, dest,
2344 "destination element has no pad \"%s\"", destpadname);
2348 /* we're satisfied they can be unlinked, let's do it */
2349 gst_pad_unlink (srcpad, destpad);
2352 gst_element_release_request_pad (dest, destpad);
2353 gst_object_unref (destpad);
2357 gst_element_release_request_pad (src, srcpad);
2358 gst_object_unref (srcpad);
2362 * gst_element_unlink_many:
2363 * @element_1: (transfer none): the first #GstElement in the link chain.
2364 * @element_2: (transfer none): the second #GstElement in the link chain.
2365 * @...: the %NULL-terminated list of elements to unlink in order.
2367 * Unlinks a series of elements. Uses gst_element_unlink().
2370 gst_element_unlink_many (GstElement * element_1, GstElement * element_2, ...)
2374 g_return_if_fail (element_1 != NULL && element_2 != NULL);
2375 g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
2377 va_start (args, element_2);
2380 gst_element_unlink (element_1, element_2);
2382 element_1 = element_2;
2383 element_2 = va_arg (args, GstElement *);
2390 * gst_element_unlink:
2391 * @src: (transfer none): the source #GstElement to unlink.
2392 * @dest: (transfer none): the sink #GstElement to unlink.
2394 * Unlinks all source pads of the source element with all sink pads
2395 * of the sink element to which they are linked.
2397 * If the link has been made using gst_element_link(), it could have created an
2398 * requestpad, which has to be released using gst_element_release_request_pad().
2401 gst_element_unlink (GstElement * src, GstElement * dest)
2404 gboolean done = FALSE;
2405 GValue data = { 0, };
2407 g_return_if_fail (GST_IS_ELEMENT (src));
2408 g_return_if_fail (GST_IS_ELEMENT (dest));
2410 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unlinking \"%s\" and \"%s\"",
2411 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
2413 pads = gst_element_iterate_pads (src);
2415 switch (gst_iterator_next (pads, &data)) {
2416 case GST_ITERATOR_OK:
2418 GstPad *pad = g_value_get_object (&data);
2420 if (GST_PAD_IS_SRC (pad)) {
2421 GstPad *peerpad = gst_pad_get_peer (pad);
2423 /* see if the pad is linked and is really a pad of dest */
2425 GstElement *peerelem;
2427 peerelem = gst_pad_get_parent_element (peerpad);
2429 if (peerelem == dest) {
2430 gst_pad_unlink (pad, peerpad);
2433 gst_object_unref (peerelem);
2435 gst_object_unref (peerpad);
2438 g_value_reset (&data);
2441 case GST_ITERATOR_RESYNC:
2442 gst_iterator_resync (pads);
2444 case GST_ITERATOR_DONE:
2448 g_assert_not_reached ();
2452 g_value_unset (&data);
2453 gst_iterator_free (pads);
2457 * gst_element_query_position:
2458 * @element: a #GstElement to invoke the position query on.
2459 * @format: the #GstFormat requested
2460 * @cur: (out) (optional): a location in which to store the current
2461 * position, or %NULL.
2463 * Queries an element (usually top-level pipeline or playbin element) for the
2464 * stream position in nanoseconds. This will be a value between 0 and the
2465 * stream duration (if the stream duration is known). This query will usually
2466 * only work once the pipeline is prerolled (i.e. reached PAUSED or PLAYING
2467 * state). The application will receive an ASYNC_DONE message on the pipeline
2468 * bus when that is the case.
2470 * If one repeatedly calls this function one can also create a query and reuse
2471 * it in gst_element_query().
2473 * Returns: %TRUE if the query could be performed.
2476 gst_element_query_position (GstElement * element, GstFormat format,
2483 *cur = GST_CLOCK_TIME_NONE;
2485 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2486 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2488 query = gst_query_new_position (format);
2489 ret = gst_element_query (element, query);
2492 gst_query_parse_position (query, NULL, cur);
2494 gst_query_unref (query);
2500 * gst_element_query_duration:
2501 * @element: a #GstElement to invoke the duration query on.
2502 * @format: the #GstFormat requested
2503 * @duration: (out) (optional): A location in which to store the total duration, or %NULL.
2505 * Queries an element (usually top-level pipeline or playbin element) for the
2506 * total stream duration in nanoseconds. This query will only work once the
2507 * pipeline is prerolled (i.e. reached PAUSED or PLAYING state). The application
2508 * will receive an ASYNC_DONE message on the pipeline bus when that is the case.
2510 * If the duration changes for some reason, you will get a DURATION_CHANGED
2511 * message on the pipeline bus, in which case you should re-query the duration
2512 * using this function.
2514 * Returns: %TRUE if the query could be performed.
2517 gst_element_query_duration (GstElement * element, GstFormat format,
2523 if (duration != NULL)
2524 *duration = GST_CLOCK_TIME_NONE;
2526 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2527 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2529 query = gst_query_new_duration (format);
2530 ret = gst_element_query (element, query);
2533 gst_query_parse_duration (query, NULL, duration);
2535 gst_query_unref (query);
2541 * gst_element_query_convert:
2542 * @element: a #GstElement to invoke the convert query on.
2543 * @src_format: a #GstFormat to convert from.
2544 * @src_val: a value to convert.
2545 * @dest_format: the #GstFormat to convert to.
2546 * @dest_val: (out): a pointer to the result.
2548 * Queries an element to convert @src_val in @src_format to @dest_format.
2550 * Returns: %TRUE if the query could be performed.
2553 gst_element_query_convert (GstElement * element, GstFormat src_format,
2554 gint64 src_val, GstFormat dest_format, gint64 * dest_val)
2559 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2560 g_return_val_if_fail (dest_format != GST_FORMAT_UNDEFINED, FALSE);
2561 g_return_val_if_fail (dest_val != NULL, FALSE);
2563 if (dest_format == src_format || src_val == -1) {
2564 *dest_val = src_val;
2570 query = gst_query_new_convert (src_format, src_val, dest_format);
2571 ret = gst_element_query (element, query);
2574 gst_query_parse_convert (query, NULL, NULL, NULL, dest_val);
2576 gst_query_unref (query);
2582 * gst_element_seek_simple:
2583 * @element: a #GstElement to seek on
2584 * @format: a #GstFormat to execute the seek in, such as #GST_FORMAT_TIME
2585 * @seek_flags: seek options; playback applications will usually want to use
2586 * GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT here
2587 * @seek_pos: position to seek to (relative to the start); if you are doing
2588 * a seek in #GST_FORMAT_TIME this value is in nanoseconds -
2589 * multiply with #GST_SECOND to convert seconds to nanoseconds or
2590 * with #GST_MSECOND to convert milliseconds to nanoseconds.
2592 * Simple API to perform a seek on the given element, meaning it just seeks
2593 * to the given position relative to the start of the stream. For more complex
2594 * operations like segment seeks (e.g. for looping) or changing the playback
2595 * rate or seeking relative to the last configured playback segment you should
2596 * use gst_element_seek().
2598 * In a completely prerolled PAUSED or PLAYING pipeline, seeking is always
2599 * guaranteed to return %TRUE on a seekable media type or %FALSE when the media
2600 * type is certainly not seekable (such as a live stream).
2602 * Some elements allow for seeking in the READY state, in this
2603 * case they will store the seek event and execute it when they are put to
2604 * PAUSED. If the element supports seek in READY, it will always return %TRUE when
2605 * it receives the event in the READY state.
2607 * Returns: %TRUE if the seek operation succeeded. Flushing seeks will trigger a
2608 * preroll, which will emit %GST_MESSAGE_ASYNC_DONE.
2611 gst_element_seek_simple (GstElement * element, GstFormat format,
2612 GstSeekFlags seek_flags, gint64 seek_pos)
2614 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2615 g_return_val_if_fail (seek_pos >= 0, FALSE);
2617 return gst_element_seek (element, 1.0, format, seek_flags,
2618 GST_SEEK_TYPE_SET, seek_pos, GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
2622 * gst_pad_use_fixed_caps:
2623 * @pad: the pad to use
2625 * A helper function you can use that sets the FIXED_CAPS flag
2626 * This way the default CAPS query will always return the negotiated caps
2627 * or in case the pad is not negotiated, the padtemplate caps.
2629 * The negotiated caps are the caps of the last CAPS event that passed on the
2630 * pad. Use this function on a pad that, once it negotiated to a CAPS, cannot
2631 * be renegotiated to something else.
2634 gst_pad_use_fixed_caps (GstPad * pad)
2636 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_FIXED_CAPS);
2640 * gst_pad_get_parent_element:
2643 * Gets the parent of @pad, cast to a #GstElement. If a @pad has no parent or
2644 * its parent is not an element, return %NULL.
2646 * Returns: (transfer full) (nullable): the parent of the pad. The
2647 * caller has a reference on the parent, so unref when you're finished
2653 gst_pad_get_parent_element (GstPad * pad)
2657 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2659 p = gst_object_get_parent (GST_OBJECT_CAST (pad));
2661 if (p && !GST_IS_ELEMENT (p)) {
2662 gst_object_unref (p);
2665 return GST_ELEMENT_CAST (p);
2669 * gst_object_default_error:
2670 * @source: the #GstObject that initiated the error.
2671 * @error: (in): the GError.
2672 * @debug: (in) (nullable): an additional debug information string, or %NULL
2674 * A default error function that uses g_printerr() to display the error message
2675 * and the optional debug string..
2677 * The default handler will simply print the error string using g_print.
2680 gst_object_default_error (GstObject * source, const GError * error,
2681 const gchar * debug)
2683 gchar *name = gst_object_get_path_string (source);
2685 g_printerr (_("ERROR: from element %s: %s\n"), name, error->message);
2687 g_printerr (_("Additional debug info:\n%s\n"), debug);
2693 * gst_bin_add_many: (skip)
2695 * @element_1: (transfer floating): the #GstElement element to add to the bin
2696 * @...: additional elements to add to the bin
2698 * Adds a %NULL-terminated list of elements to a bin. This function is
2699 * equivalent to calling gst_bin_add() for each member of the list. The return
2700 * value of each gst_bin_add() is ignored.
2703 gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
2707 g_return_if_fail (GST_IS_BIN (bin));
2708 g_return_if_fail (GST_IS_ELEMENT (element_1));
2710 va_start (args, element_1);
2713 gst_bin_add (bin, element_1);
2715 element_1 = va_arg (args, GstElement *);
2722 * gst_bin_remove_many: (skip)
2724 * @element_1: (transfer none): the first #GstElement to remove from the bin
2725 * @...: (transfer none): %NULL-terminated list of elements to remove from the bin
2727 * Removes a list of elements from a bin. This function is equivalent
2728 * to calling gst_bin_remove() with each member of the list.
2731 gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
2735 g_return_if_fail (GST_IS_BIN (bin));
2736 g_return_if_fail (GST_IS_ELEMENT (element_1));
2738 va_start (args, element_1);
2741 gst_bin_remove (bin, element_1);
2743 element_1 = va_arg (args, GstElement *);
2753 } QueryAcceptCapsData;
2756 query_accept_caps_func (GstPad * pad, QueryAcceptCapsData * data)
2758 if (G_LIKELY (gst_pad_peer_query (pad, data->query))) {
2761 gst_query_parse_accept_caps_result (data->query, &result);
2762 data->ret &= result;
2768 * gst_pad_proxy_query_accept_caps:
2769 * @pad: a #GstPad to proxy.
2770 * @query: an ACCEPT_CAPS #GstQuery.
2772 * Checks if all internally linked pads of @pad accepts the caps in @query and
2773 * returns the intersection of the results.
2775 * This function is useful as a default accept caps query function for an element
2776 * that can handle any stream format, but requires caps that are acceptable for
2777 * all opposite pads.
2779 * Returns: %TRUE if @query could be executed
2782 gst_pad_proxy_query_accept_caps (GstPad * pad, GstQuery * query)
2784 QueryAcceptCapsData data;
2786 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2787 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2788 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS, FALSE);
2790 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2791 "proxying accept caps query for %s:%s", GST_DEBUG_PAD_NAME (pad));
2794 /* value to hold the return, by default it holds TRUE */
2795 /* FIXME: TRUE is wrong when there are no pads */
2798 gst_pad_forward (pad, (GstPadForwardFunction) query_accept_caps_func, &data);
2799 gst_query_set_accept_caps_result (query, data.ret);
2801 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "proxying accept caps query: %d",
2814 query_caps_func (GstPad * pad, QueryCapsData * data)
2816 gboolean empty = FALSE;
2818 if (G_LIKELY (gst_pad_peer_query (pad, data->query))) {
2819 GstCaps *peercaps, *intersection;
2821 gst_query_parse_caps_result (data->query, &peercaps);
2822 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2823 "intersect with result %" GST_PTR_FORMAT, peercaps);
2824 intersection = gst_caps_intersect (data->ret, peercaps);
2825 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2826 "intersected %" GST_PTR_FORMAT, intersection);
2828 gst_caps_unref (data->ret);
2829 data->ret = intersection;
2831 /* stop when empty */
2832 empty = gst_caps_is_empty (intersection);
2838 * gst_pad_proxy_query_caps:
2839 * @pad: a #GstPad to proxy.
2840 * @query: a CAPS #GstQuery.
2842 * Calls gst_pad_query_caps() for all internally linked pads of @pad and returns
2843 * the intersection of the results.
2845 * This function is useful as a default caps query function for an element
2846 * that can handle any stream format, but requires all its pads to have
2847 * the same caps. Two such elements are tee and adder.
2849 * Returns: %TRUE if @query could be executed
2852 gst_pad_proxy_query_caps (GstPad * pad, GstQuery * query)
2854 GstCaps *filter, *templ, *result;
2857 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2858 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2859 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS, FALSE);
2861 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "proxying caps query for %s:%s",
2862 GST_DEBUG_PAD_NAME (pad));
2866 /* value to hold the return, by default it holds the filter or ANY */
2867 gst_query_parse_caps (query, &filter);
2868 data.ret = filter ? gst_caps_ref (filter) : gst_caps_new_any ();
2870 gst_pad_forward (pad, (GstPadForwardFunction) query_caps_func, &data);
2872 templ = gst_pad_get_pad_template_caps (pad);
2873 result = gst_caps_intersect (data.ret, templ);
2874 gst_caps_unref (data.ret);
2875 gst_caps_unref (templ);
2877 gst_query_set_caps_result (query, result);
2878 gst_caps_unref (result);
2880 /* FIXME: return something depending on the processing */
2885 * gst_pad_query_position:
2886 * @pad: a #GstPad to invoke the position query on.
2887 * @format: the #GstFormat requested
2888 * @cur: (out) (optional): A location in which to store the current position, or %NULL.
2890 * Queries a pad for the stream position.
2892 * Returns: %TRUE if the query could be performed.
2895 gst_pad_query_position (GstPad * pad, GstFormat format, gint64 * cur)
2901 *cur = GST_CLOCK_TIME_NONE;
2903 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2904 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2906 query = gst_query_new_position (format);
2907 if ((ret = gst_pad_query (pad, query)))
2908 gst_query_parse_position (query, NULL, cur);
2909 gst_query_unref (query);
2915 * gst_pad_peer_query_position:
2916 * @pad: a #GstPad on whose peer to invoke the position query on.
2917 * Must be a sink pad.
2918 * @format: the #GstFormat requested
2919 * @cur: (out) (optional): a location in which to store the current
2920 * position, or %NULL.
2922 * Queries the peer of a given sink pad for the stream position.
2924 * Returns: %TRUE if the query could be performed.
2927 gst_pad_peer_query_position (GstPad * pad, GstFormat format, gint64 * cur)
2930 gboolean ret = FALSE;
2933 *cur = GST_CLOCK_TIME_NONE;
2935 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2936 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2938 query = gst_query_new_position (format);
2939 if ((ret = gst_pad_peer_query (pad, query)))
2940 gst_query_parse_position (query, NULL, cur);
2941 gst_query_unref (query);
2947 * gst_pad_query_duration:
2948 * @pad: a #GstPad to invoke the duration query on.
2949 * @format: the #GstFormat requested
2950 * @duration: (out) (optional): a location in which to store the total
2951 * duration, or %NULL.
2953 * Queries a pad for the total stream duration.
2955 * Returns: %TRUE if the query could be performed.
2958 gst_pad_query_duration (GstPad * pad, GstFormat format, gint64 * duration)
2963 if (duration != NULL)
2964 *duration = GST_CLOCK_TIME_NONE;
2966 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2967 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2969 query = gst_query_new_duration (format);
2970 if ((ret = gst_pad_query (pad, query)))
2971 gst_query_parse_duration (query, NULL, duration);
2972 gst_query_unref (query);
2978 * gst_pad_peer_query_duration:
2979 * @pad: a #GstPad on whose peer pad to invoke the duration query on.
2980 * Must be a sink pad.
2981 * @format: the #GstFormat requested
2982 * @duration: (out) (optional): a location in which to store the total
2983 * duration, or %NULL.
2985 * Queries the peer pad of a given sink pad for the total stream duration.
2987 * Returns: %TRUE if the query could be performed.
2990 gst_pad_peer_query_duration (GstPad * pad, GstFormat format, gint64 * duration)
2993 gboolean ret = FALSE;
2995 if (duration != NULL)
2996 *duration = GST_CLOCK_TIME_NONE;
2998 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2999 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
3000 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
3002 query = gst_query_new_duration (format);
3003 if ((ret = gst_pad_peer_query (pad, query)))
3004 gst_query_parse_duration (query, NULL, duration);
3005 gst_query_unref (query);
3011 * gst_pad_query_convert:
3012 * @pad: a #GstPad to invoke the convert query on.
3013 * @src_format: a #GstFormat to convert from.
3014 * @src_val: a value to convert.
3015 * @dest_format: the #GstFormat to convert to.
3016 * @dest_val: (out): a pointer to the result.
3018 * Queries a pad to convert @src_val in @src_format to @dest_format.
3020 * Returns: %TRUE if the query could be performed.
3023 gst_pad_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
3024 GstFormat dest_format, gint64 * dest_val)
3029 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3030 g_return_val_if_fail (dest_format != GST_FORMAT_UNDEFINED, FALSE);
3031 g_return_val_if_fail (dest_val != NULL, FALSE);
3033 if (dest_format == src_format || src_val == -1) {
3034 *dest_val = src_val;
3040 query = gst_query_new_convert (src_format, src_val, dest_format);
3041 if ((ret = gst_pad_query (pad, query)))
3042 gst_query_parse_convert (query, NULL, NULL, NULL, dest_val);
3043 gst_query_unref (query);
3049 * gst_pad_peer_query_convert:
3050 * @pad: a #GstPad, on whose peer pad to invoke the convert query on.
3051 * Must be a sink pad.
3052 * @src_format: a #GstFormat to convert from.
3053 * @src_val: a value to convert.
3054 * @dest_format: the #GstFormat to convert to.
3055 * @dest_val: (out): a pointer to the result.
3057 * Queries the peer pad of a given sink pad to convert @src_val in @src_format
3060 * Returns: %TRUE if the query could be performed.
3063 gst_pad_peer_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
3064 GstFormat dest_format, gint64 * dest_val)
3067 gboolean ret = FALSE;
3069 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3070 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
3071 g_return_val_if_fail (dest_format != GST_FORMAT_UNDEFINED, FALSE);
3072 g_return_val_if_fail (dest_val != NULL, FALSE);
3076 query = gst_query_new_convert (src_format, src_val, dest_format);
3077 if ((ret = gst_pad_peer_query (pad, query)))
3078 gst_query_parse_convert (query, NULL, NULL, NULL, dest_val);
3079 gst_query_unref (query);
3085 * gst_pad_query_caps:
3086 * @pad: a #GstPad to get the capabilities of.
3087 * @filter: (nullable): suggested #GstCaps, or %NULL
3089 * Gets the capabilities this pad can produce or consume.
3090 * Note that this method doesn't necessarily return the caps set by sending a
3091 * gst_event_new_caps() - use gst_pad_get_current_caps() for that instead.
3092 * gst_pad_query_caps returns all possible caps a pad can operate with, using
3093 * the pad's CAPS query function, If the query fails, this function will return
3094 * @filter, if not %NULL, otherwise ANY.
3096 * When called on sinkpads @filter contains the caps that
3097 * upstream could produce in the order preferred by upstream. When
3098 * called on srcpads @filter contains the caps accepted by
3099 * downstream in the preferred order. @filter might be %NULL but
3100 * if it is not %NULL the returned caps will be a subset of @filter.
3102 * Note that this function does not return writable #GstCaps, use
3103 * gst_caps_make_writable() before modifying the caps.
3105 * Returns: (transfer full): the caps of the pad with incremented ref-count.
3108 gst_pad_query_caps (GstPad * pad, GstCaps * filter)
3110 GstCaps *result = NULL;
3113 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3114 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
3116 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3117 "get pad caps with filter %" GST_PTR_FORMAT, filter);
3119 query = gst_query_new_caps (filter);
3120 if (gst_pad_query (pad, query)) {
3121 gst_query_parse_caps_result (query, &result);
3122 gst_caps_ref (result);
3123 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3124 "query returned %" GST_PTR_FORMAT, result);
3125 } else if (filter) {
3126 result = gst_caps_ref (filter);
3128 result = gst_caps_new_any ();
3130 gst_query_unref (query);
3136 * gst_pad_peer_query_caps:
3137 * @pad: a #GstPad to get the capabilities of.
3138 * @filter: (nullable): a #GstCaps filter, or %NULL.
3140 * Gets the capabilities of the peer connected to this pad. Similar to
3141 * gst_pad_query_caps().
3143 * When called on srcpads @filter contains the caps that
3144 * upstream could produce in the order preferred by upstream. When
3145 * called on sinkpads @filter contains the caps accepted by
3146 * downstream in the preferred order. @filter might be %NULL but
3147 * if it is not %NULL the returned caps will be a subset of @filter.
3149 * Returns: (transfer full): the caps of the peer pad with incremented
3150 * ref-count. When there is no peer pad, this function returns @filter or,
3151 * when @filter is %NULL, ANY caps.
3154 gst_pad_peer_query_caps (GstPad * pad, GstCaps * filter)
3156 GstCaps *result = NULL;
3159 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3160 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
3162 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3163 "get pad peer caps with filter %" GST_PTR_FORMAT, filter);
3165 query = gst_query_new_caps (filter);
3166 if (gst_pad_peer_query (pad, query)) {
3167 gst_query_parse_caps_result (query, &result);
3168 gst_caps_ref (result);
3169 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3170 "peer query returned %" GST_PTR_FORMAT, result);
3171 } else if (filter) {
3172 result = gst_caps_ref (filter);
3174 result = gst_caps_new_any ();
3176 gst_query_unref (query);
3182 * gst_pad_query_accept_caps:
3183 * @pad: a #GstPad to check
3184 * @caps: a #GstCaps to check on the pad
3186 * Check if the given pad accepts the caps.
3188 * Returns: %TRUE if the pad can accept the caps.
3191 gst_pad_query_accept_caps (GstPad * pad, GstCaps * caps)
3193 gboolean res = TRUE;
3196 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3197 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
3199 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %"
3200 GST_PTR_FORMAT, caps);
3202 query = gst_query_new_accept_caps (caps);
3203 if (gst_pad_query (pad, query)) {
3204 gst_query_parse_accept_caps_result (query, &res);
3205 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query returned %d", res);
3207 gst_query_unref (query);
3213 * gst_pad_peer_query_accept_caps:
3214 * @pad: a #GstPad to check the peer of
3215 * @caps: a #GstCaps to check on the pad
3217 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
3220 * Returns: %TRUE if the peer of @pad can accept the caps or @pad has no peer.
3223 gst_pad_peer_query_accept_caps (GstPad * pad, GstCaps * caps)
3225 gboolean res = TRUE;
3228 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3229 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
3231 query = gst_query_new_accept_caps (caps);
3232 if (gst_pad_peer_query (pad, query)) {
3233 gst_query_parse_accept_caps_result (query, &res);
3234 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "query returned %d", res);
3236 gst_query_unref (query);
3242 element_find_unlinked_pad (GstElement * element, GstPadDirection direction)
3245 GstPad *unlinked_pad = NULL;
3247 GValue data = { 0, };
3249 switch (direction) {
3251 iter = gst_element_iterate_src_pads (element);
3254 iter = gst_element_iterate_sink_pads (element);
3257 g_return_val_if_reached (NULL);
3262 switch (gst_iterator_next (iter, &data)) {
3263 case GST_ITERATOR_OK:{
3265 GstPad *pad = g_value_get_object (&data);
3267 GST_CAT_LOG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3268 "examining pad %s:%s", GST_DEBUG_PAD_NAME (pad));
3270 peer = gst_pad_get_peer (pad);
3272 unlinked_pad = gst_object_ref (pad);
3274 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
3275 "found existing unlinked pad %s:%s",
3276 GST_DEBUG_PAD_NAME (unlinked_pad));
3278 gst_object_unref (peer);
3280 g_value_reset (&data);
3283 case GST_ITERATOR_DONE:
3286 case GST_ITERATOR_RESYNC:
3287 gst_iterator_resync (iter);
3289 case GST_ITERATOR_ERROR:
3290 g_return_val_if_reached (NULL);
3294 g_value_unset (&data);
3295 gst_iterator_free (iter);
3297 return unlinked_pad;
3301 * gst_bin_find_unlinked_pad:
3302 * @bin: bin in which to look for elements with unlinked pads
3303 * @direction: whether to look for an unlinked source or sink pad
3305 * Recursively looks for elements with an unlinked pad of the given
3306 * direction within the specified bin and returns an unlinked pad
3307 * if one is found, or %NULL otherwise. If a pad is found, the caller
3308 * owns a reference to it and should use gst_object_unref() on the
3309 * pad when it is not needed any longer.
3311 * Returns: (transfer full) (nullable): unlinked pad of the given
3315 gst_bin_find_unlinked_pad (GstBin * bin, GstPadDirection direction)
3320 GValue data = { 0, };
3322 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3323 g_return_val_if_fail (direction != GST_PAD_UNKNOWN, NULL);
3326 iter = gst_bin_iterate_recurse (bin);
3328 switch (gst_iterator_next (iter, &data)) {
3329 case GST_ITERATOR_OK:{
3330 GstElement *element = g_value_get_object (&data);
3332 pad = element_find_unlinked_pad (element, direction);
3335 g_value_reset (&data);
3338 case GST_ITERATOR_DONE:
3341 case GST_ITERATOR_RESYNC:
3342 gst_iterator_resync (iter);
3344 case GST_ITERATOR_ERROR:
3345 g_return_val_if_reached (NULL);
3349 g_value_unset (&data);
3350 gst_iterator_free (iter);
3356 gst_bin_sync_children_states_foreach (const GValue * value, gpointer user_data)
3358 gboolean *success = user_data;
3359 GstElement *element = g_value_get_object (value);
3361 if (gst_element_is_locked_state (element)) {
3364 *success = *success && gst_element_sync_state_with_parent (element);
3366 if (GST_IS_BIN (element))
3368 && gst_bin_sync_children_states (GST_BIN_CAST (element));
3373 * gst_bin_sync_children_states:
3376 * Synchronizes the state of every child of @bin with the state
3377 * of @bin. See also gst_element_sync_state_with_parent().
3379 * Returns: %TRUE if syncing the state was successful for all children,
3385 gst_bin_sync_children_states (GstBin * bin)
3388 GstIteratorResult res = GST_ITERATOR_OK;
3389 gboolean success = TRUE;
3391 it = gst_bin_iterate_sorted (bin);
3394 if (res == GST_ITERATOR_RESYNC) {
3396 gst_iterator_resync (it);
3399 gst_iterator_foreach (it, gst_bin_sync_children_states_foreach,
3401 } while (res == GST_ITERATOR_RESYNC);
3402 gst_iterator_free (it);
3408 * gst_parse_bin_from_description:
3409 * @bin_description: command line describing the bin
3410 * @ghost_unlinked_pads: whether to automatically create ghost pads
3411 * for unlinked source or sink pads within the bin
3412 * @err: where to store the error message in case of an error, or %NULL
3414 * This is a convenience wrapper around gst_parse_launch() to create a
3415 * #GstBin from a gst-launch-style pipeline description. See
3416 * gst_parse_launch() and the gst-launch man page for details about the
3417 * syntax. Ghost pads on the bin for unlinked source or sink pads
3418 * within the bin can automatically be created (but only a maximum of
3419 * one ghost pad for each direction will be created; if you expect
3420 * multiple unlinked source pads or multiple unlinked sink pads
3421 * and want them all ghosted, you will have to create the ghost pads
3424 * Returns: (transfer floating) (type Gst.Bin): a
3425 * newly-created bin, or %NULL if an error occurred.
3428 gst_parse_bin_from_description (const gchar * bin_description,
3429 gboolean ghost_unlinked_pads, GError ** err)
3431 return gst_parse_bin_from_description_full (bin_description,
3432 ghost_unlinked_pads, NULL, GST_PARSE_FLAG_NONE, err);
3436 * gst_parse_bin_from_description_full:
3437 * @bin_description: command line describing the bin
3438 * @ghost_unlinked_pads: whether to automatically create ghost pads
3439 * for unlinked source or sink pads within the bin
3440 * @context: (transfer none) (nullable): a parse context allocated with
3441 * gst_parse_context_new(), or %NULL
3442 * @flags: parsing options, or #GST_PARSE_FLAG_NONE
3443 * @err: where to store the error message in case of an error, or %NULL
3445 * This is a convenience wrapper around gst_parse_launch() to create a
3446 * #GstBin from a gst-launch-style pipeline description. See
3447 * gst_parse_launch() and the gst-launch man page for details about the
3448 * syntax. Ghost pads on the bin for unlinked source or sink pads
3449 * within the bin can automatically be created (but only a maximum of
3450 * one ghost pad for each direction will be created; if you expect
3451 * multiple unlinked source pads or multiple unlinked sink pads
3452 * and want them all ghosted, you will have to create the ghost pads
3455 * Returns: (transfer floating) (type Gst.Element): a newly-created
3456 * element, which is guaranteed to be a bin unless
3457 * #GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS was passed, or %NULL if an error
3461 gst_parse_bin_from_description_full (const gchar * bin_description,
3462 gboolean ghost_unlinked_pads, GstParseContext * context,
3463 GstParseFlags flags, GError ** err)
3465 #ifndef GST_DISABLE_PARSE
3467 GstElement *element;
3471 g_return_val_if_fail (bin_description != NULL, NULL);
3472 g_return_val_if_fail (err == NULL || *err == NULL, NULL);
3474 GST_DEBUG ("Making bin from description '%s'", bin_description);
3476 /* parse the pipeline to a bin */
3477 if (flags & GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS) {
3478 element = gst_parse_launch_full (bin_description, context, flags, err);
3480 desc = g_strdup_printf ("bin.( %s )", bin_description);
3481 element = gst_parse_launch_full (desc, context, flags, err);
3485 if (element == NULL || (err && *err != NULL)) {
3487 gst_object_unref (element);
3491 if (GST_IS_BIN (element)) {
3492 bin = GST_BIN (element);
3497 /* find pads and ghost them if necessary */
3498 if (ghost_unlinked_pads) {
3499 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC))) {
3500 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("src", pad));
3501 gst_object_unref (pad);
3503 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SINK))) {
3504 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("sink", pad));
3505 gst_object_unref (pad);
3509 return GST_ELEMENT (bin);
3513 GST_WARNING ("Disabled API called");
3515 msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
3516 g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
3524 * gst_util_get_timestamp:
3526 * Get a timestamp as GstClockTime to be used for interval measurements.
3527 * The timestamp should not be interpreted in any other way.
3529 * Returns: the timestamp
3532 gst_util_get_timestamp (void)
3534 #if defined (HAVE_POSIX_TIMERS) && defined(HAVE_MONOTONIC_CLOCK) &&\
3535 defined (HAVE_CLOCK_GETTIME)
3536 struct timespec now;
3538 clock_gettime (CLOCK_MONOTONIC, &now);
3539 return GST_TIMESPEC_TO_TIME (now);
3541 return g_get_monotonic_time () * 1000;
3546 * gst_util_array_binary_search:
3547 * @array: the sorted input array
3548 * @num_elements: number of elements in the array
3549 * @element_size: size of every element in bytes
3550 * @search_func: (scope call): function to compare two elements, @search_data will always be passed as second argument
3551 * @mode: search mode that should be used
3552 * @search_data: element that should be found
3553 * @user_data: (closure): data to pass to @search_func
3555 * Searches inside @array for @search_data by using the comparison function
3556 * @search_func. @array must be sorted ascending.
3558 * As @search_data is always passed as second argument to @search_func it's
3559 * not required that @search_data has the same type as the array elements.
3561 * The complexity of this search function is O(log (num_elements)).
3563 * Returns: (transfer none) (nullable): The address of the found
3564 * element or %NULL if nothing was found
3567 gst_util_array_binary_search (gpointer array, guint num_elements,
3568 gsize element_size, GCompareDataFunc search_func, GstSearchMode mode,
3569 gconstpointer search_data, gpointer user_data)
3571 glong left = 0, right = num_elements - 1, m;
3573 guint8 *data = (guint8 *) array;
3575 g_return_val_if_fail (array != NULL, NULL);
3576 g_return_val_if_fail (element_size > 0, NULL);
3577 g_return_val_if_fail (search_func != NULL, NULL);
3579 /* 0. No elements => return NULL */
3580 if (num_elements == 0)
3583 /* 1. If search_data is before the 0th element return the 0th element */
3584 ret = search_func (data, search_data, user_data);
3585 if ((ret >= 0 && mode == GST_SEARCH_MODE_AFTER) || ret == 0)
3590 /* 2. If search_data is after the last element return the last element */
3592 search_func (data + (num_elements - 1) * element_size, search_data,
3594 if ((ret <= 0 && mode == GST_SEARCH_MODE_BEFORE) || ret == 0)
3595 return data + (num_elements - 1) * element_size;
3599 /* 3. else binary search */
3601 m = left + (right - left) / 2;
3603 ret = search_func (data + m * element_size, search_data, user_data);
3606 return data + m * element_size;
3607 } else if (ret < 0) {
3613 /* No exact match found */
3615 if (mode == GST_SEARCH_MODE_EXACT) {
3617 } else if (mode == GST_SEARCH_MODE_AFTER) {
3619 return (m < num_elements) ? data + (m + 1) * element_size : NULL;
3621 return data + m * element_size;
3624 return data + m * element_size;
3626 return (m > 0) ? data + (m - 1) * element_size : NULL;
3632 /* Finds the greatest common divisor.
3633 * Returns 1 if none other found.
3634 * This is Euclid's algorithm. */
3637 * gst_util_greatest_common_divisor:
3638 * @a: First value as #gint
3639 * @b: Second value as #gint
3641 * Calculates the greatest common divisor of @a
3644 * Returns: Greatest common divisor of @a and @b
3647 gst_util_greatest_common_divisor (gint a, gint b)
3660 * gst_util_greatest_common_divisor_int64:
3661 * @a: First value as #gint64
3662 * @b: Second value as #gint64
3664 * Calculates the greatest common divisor of @a
3667 * Returns: Greatest common divisor of @a and @b
3670 gst_util_greatest_common_divisor_int64 (gint64 a, gint64 b)
3684 * gst_util_fraction_to_double:
3685 * @src_n: Fraction numerator as #gint
3686 * @src_d: Fraction denominator #gint
3687 * @dest: (out): pointer to a #gdouble for the result
3689 * Transforms a fraction to a #gdouble.
3692 gst_util_fraction_to_double (gint src_n, gint src_d, gdouble * dest)
3694 g_return_if_fail (dest != NULL);
3695 g_return_if_fail (src_d != 0);
3697 *dest = ((gdouble) src_n) / ((gdouble) src_d);
3700 #define MAX_TERMS 30
3701 #define MIN_DIVISOR 1.0e-10
3702 #define MAX_ERROR 1.0e-20
3704 /* use continued fractions to transform a double into a fraction,
3705 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
3706 * This algorithm takes care of overflows.
3710 * gst_util_double_to_fraction:
3711 * @src: #gdouble to transform
3712 * @dest_n: (out): pointer to a #gint to hold the result numerator
3713 * @dest_d: (out): pointer to a #gint to hold the result denominator
3715 * Transforms a #gdouble to a fraction and simplifies
3719 gst_util_double_to_fraction (gdouble src, gint * dest_n, gint * dest_d)
3722 gdouble V, F; /* double being converted */
3723 gint N, D; /* will contain the result */
3724 gint A; /* current term in continued fraction */
3725 gint64 N1, D1; /* numerator, denominator of last approx */
3726 gint64 N2, D2; /* numerator, denominator of previous approx */
3729 gboolean negative = FALSE;
3731 g_return_if_fail (dest_n != NULL);
3732 g_return_if_fail (dest_d != NULL);
3734 /* initialize fraction being converted */
3742 /* initialize fractions with 1/0, 0/1 */
3750 for (i = 0; i < MAX_TERMS; i++) {
3752 A = (gint) F; /* no floor() needed, F is always >= 0 */
3753 /* get new divisor */
3756 /* calculate new fraction in temp */
3760 /* guard against overflow */
3761 if (N2 > G_MAXINT || D2 > G_MAXINT) {
3768 /* save last two fractions */
3774 /* quit if dividing by zero or close enough to target */
3775 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
3779 /* Take reciprocal */
3782 /* fix for overflow */
3787 /* fix for negative */
3792 gcd = gst_util_greatest_common_divisor (N, D);
3804 * gst_util_fraction_multiply:
3805 * @a_n: Numerator of first value
3806 * @a_d: Denominator of first value
3807 * @b_n: Numerator of second value
3808 * @b_d: Denominator of second value
3809 * @res_n: (out): Pointer to #gint to hold the result numerator
3810 * @res_d: (out): Pointer to #gint to hold the result denominator
3812 * Multiplies the fractions @a_n/@a_d and @b_n/@b_d and stores
3813 * the result in @res_n and @res_d.
3815 * Returns: %FALSE on overflow, %TRUE otherwise.
3818 gst_util_fraction_multiply (gint a_n, gint a_d, gint b_n, gint b_d,
3819 gint * res_n, gint * res_d)
3823 g_return_val_if_fail (res_n != NULL, FALSE);
3824 g_return_val_if_fail (res_d != NULL, FALSE);
3825 g_return_val_if_fail (a_d != 0, FALSE);
3826 g_return_val_if_fail (b_d != 0, FALSE);
3828 /* early out if either is 0, as its gcd would be 0 */
3829 if (a_n == 0 || b_n == 0) {
3835 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3839 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3843 gcd = gst_util_greatest_common_divisor (a_n, b_d);
3847 gcd = gst_util_greatest_common_divisor (a_d, b_n);
3851 /* This would result in overflow */
3852 if (a_n != 0 && G_MAXINT / ABS (a_n) < ABS (b_n))
3854 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3860 gcd = gst_util_greatest_common_divisor (*res_n, *res_d);
3868 * gst_util_fraction_add:
3869 * @a_n: Numerator of first value
3870 * @a_d: Denominator of first value
3871 * @b_n: Numerator of second value
3872 * @b_d: Denominator of second value
3873 * @res_n: (out): Pointer to #gint to hold the result numerator
3874 * @res_d: (out): Pointer to #gint to hold the result denominator
3876 * Adds the fractions @a_n/@a_d and @b_n/@b_d and stores
3877 * the result in @res_n and @res_d.
3879 * Returns: %FALSE on overflow, %TRUE otherwise.
3882 gst_util_fraction_add (gint a_n, gint a_d, gint b_n, gint b_d, gint * res_n,
3887 g_return_val_if_fail (res_n != NULL, FALSE);
3888 g_return_val_if_fail (res_d != NULL, FALSE);
3889 g_return_val_if_fail (a_d != 0, FALSE);
3890 g_return_val_if_fail (b_d != 0, FALSE);
3892 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3896 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3911 /* This would result in overflow */
3912 if (G_MAXINT / ABS (a_n) < ABS (b_n))
3914 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3917 *res_n = (a_n * b_d) + (a_d * b_n);
3920 gcd = gst_util_greatest_common_divisor (*res_n, *res_d);
3933 * gst_util_fraction_compare:
3934 * @a_n: Numerator of first value
3935 * @a_d: Denominator of first value
3936 * @b_n: Numerator of second value
3937 * @b_d: Denominator of second value
3939 * Compares the fractions @a_n/@a_d and @b_n/@b_d and returns
3940 * -1 if a < b, 0 if a = b and 1 if a > b.
3942 * Returns: -1 if a < b; 0 if a = b; 1 if a > b.
3945 gst_util_fraction_compare (gint a_n, gint a_d, gint b_n, gint b_d)
3951 g_return_val_if_fail (a_d != 0 && b_d != 0, 0);
3954 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3958 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3962 /* fractions are reduced when set, so we can quickly see if they're equal */
3963 if (a_n == b_n && a_d == b_d)
3966 /* extend to 64 bits */
3967 new_num_1 = ((gint64) a_n) * b_d;
3968 new_num_2 = ((gint64) b_n) * a_d;
3969 if (new_num_1 < new_num_2)
3971 if (new_num_1 > new_num_2)
3974 /* Should not happen because a_d and b_d are not 0 */
3975 g_return_val_if_reached (0);
3979 gst_pad_create_stream_id_internal (GstPad * pad, GstElement * parent,
3980 const gchar * stream_id)
3982 GstEvent *upstream_event;
3983 gchar *upstream_stream_id = NULL, *new_stream_id;
3986 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3987 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
3988 g_return_val_if_fail (GST_IS_ELEMENT (parent), NULL);
3990 g_return_val_if_fail (parent->numsinkpads <= 1, NULL);
3992 /* If the element has multiple source pads it must
3993 * provide a stream-id for every source pad, otherwise
3994 * all source pads will have the same and are not
3995 * distinguishable */
3996 g_return_val_if_fail (parent->numsrcpads <= 1 || stream_id, NULL);
3998 /* First try to get the upstream stream-start stream-id from the sinkpad.
3999 * This will only work for non-source elements */
4000 sinkpad = gst_element_get_static_pad (parent, "sink");
4003 gst_pad_get_sticky_event (sinkpad, GST_EVENT_STREAM_START, 0);
4004 if (upstream_event) {
4007 gst_event_parse_stream_start (upstream_event, &tmp);
4009 upstream_stream_id = g_strdup (tmp);
4010 gst_event_unref (upstream_event);
4012 gst_object_unref (sinkpad);
4015 /* The only case where we don't have an upstream stream-start event
4016 * here is for source elements */
4017 if (!upstream_stream_id) {
4021 /* Try to generate one from the URI query and
4022 * if it fails take a random number instead */
4023 query = gst_query_new_uri ();
4024 if (gst_element_query (parent, query)) {
4025 gst_query_parse_uri (query, &uri);
4031 /* And then generate an SHA256 sum of the URI */
4032 cs = g_checksum_new (G_CHECKSUM_SHA256);
4033 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4035 upstream_stream_id = g_strdup (g_checksum_get_string (cs));
4036 g_checksum_free (cs);
4038 /* Just get some random number if the URI query fails */
4039 GST_FIXME_OBJECT (pad, "Creating random stream-id, consider "
4040 "implementing a deterministic way of creating a stream-id");
4041 upstream_stream_id =
4042 g_strdup_printf ("%08x%08x%08x%08x", g_random_int (), g_random_int (),
4043 g_random_int (), g_random_int ());
4046 gst_query_unref (query);
4050 new_stream_id = g_strconcat (upstream_stream_id, "/", stream_id, NULL);
4052 new_stream_id = g_strdup (upstream_stream_id);
4055 g_free (upstream_stream_id);
4057 return new_stream_id;
4061 * gst_pad_create_stream_id_printf_valist:
4062 * @pad: A source #GstPad
4063 * @parent: Parent #GstElement of @pad
4064 * @stream_id: (nullable): The stream-id
4065 * @var_args: parameters for the @stream_id format string
4067 * Creates a stream-id for the source #GstPad @pad by combining the
4068 * upstream information with the optional @stream_id of the stream
4069 * of @pad. @pad must have a parent #GstElement and which must have zero
4070 * or one sinkpad. @stream_id can only be %NULL if the parent element
4071 * of @pad has only a single source pad.
4073 * This function generates an unique stream-id by getting the upstream
4074 * stream-start event stream ID and appending @stream_id to it. If the
4075 * element has no sinkpad it will generate an upstream stream-id by
4076 * doing an URI query on the element and in the worst case just uses
4077 * a random number. Source elements that don't implement the URI
4078 * handler interface should ideally generate a unique, deterministic
4079 * stream-id manually instead.
4081 * Returns: A stream-id for @pad. g_free() after usage.
4084 gst_pad_create_stream_id_printf_valist (GstPad * pad, GstElement * parent,
4085 const gchar * stream_id, va_list var_args)
4087 gchar *expanded = NULL, *new_stream_id;
4090 expanded = g_strdup_vprintf (stream_id, var_args);
4092 new_stream_id = gst_pad_create_stream_id_internal (pad, parent, expanded);
4096 return new_stream_id;
4100 * gst_pad_create_stream_id_printf:
4101 * @pad: A source #GstPad
4102 * @parent: Parent #GstElement of @pad
4103 * @stream_id: (nullable): The stream-id
4104 * @...: parameters for the @stream_id format string
4106 * Creates a stream-id for the source #GstPad @pad by combining the
4107 * upstream information with the optional @stream_id of the stream
4108 * of @pad. @pad must have a parent #GstElement and which must have zero
4109 * or one sinkpad. @stream_id can only be %NULL if the parent element
4110 * of @pad has only a single source pad.
4112 * This function generates an unique stream-id by getting the upstream
4113 * stream-start event stream ID and appending @stream_id to it. If the
4114 * element has no sinkpad it will generate an upstream stream-id by
4115 * doing an URI query on the element and in the worst case just uses
4116 * a random number. Source elements that don't implement the URI
4117 * handler interface should ideally generate a unique, deterministic
4118 * stream-id manually instead.
4120 * Returns: A stream-id for @pad. g_free() after usage.
4123 gst_pad_create_stream_id_printf (GstPad * pad, GstElement * parent,
4124 const gchar * stream_id, ...)
4127 gchar *new_stream_id;
4129 va_start (var_args, stream_id);
4131 gst_pad_create_stream_id_printf_valist (pad, parent, stream_id, var_args);
4134 return new_stream_id;
4138 * gst_pad_create_stream_id:
4139 * @pad: A source #GstPad
4140 * @parent: Parent #GstElement of @pad
4141 * @stream_id: (nullable): The stream-id
4143 * Creates a stream-id for the source #GstPad @pad by combining the
4144 * upstream information with the optional @stream_id of the stream
4145 * of @pad. @pad must have a parent #GstElement and which must have zero
4146 * or one sinkpad. @stream_id can only be %NULL if the parent element
4147 * of @pad has only a single source pad.
4149 * This function generates an unique stream-id by getting the upstream
4150 * stream-start event stream ID and appending @stream_id to it. If the
4151 * element has no sinkpad it will generate an upstream stream-id by
4152 * doing an URI query on the element and in the worst case just uses
4153 * a random number. Source elements that don't implement the URI
4154 * handler interface should ideally generate a unique, deterministic
4155 * stream-id manually instead.
4157 * Since stream IDs are sorted alphabetically, any numbers in the
4158 * stream ID should be printed with a fixed number of characters,
4159 * preceded by 0's, such as by using the format \%03u instead of \%u.
4161 * Returns: A stream-id for @pad. g_free() after usage.
4164 gst_pad_create_stream_id (GstPad * pad, GstElement * parent,
4165 const gchar * stream_id)
4167 return gst_pad_create_stream_id_internal (pad, parent, stream_id);
4171 * gst_pad_get_stream_id:
4172 * @pad: A source #GstPad
4174 * Returns the current stream-id for the @pad, or %NULL if none has been
4175 * set yet, i.e. the pad has not received a stream-start event yet.
4177 * This is a convenience wrapper around gst_pad_get_sticky_event() and
4178 * gst_event_parse_stream_start().
4180 * The returned stream-id string should be treated as an opaque string, its
4181 * contents should not be interpreted.
4183 * Returns: (nullable): a newly-allocated copy of the stream-id for
4184 * @pad, or %NULL. g_free() the returned string when no longer
4190 gst_pad_get_stream_id (GstPad * pad)
4192 const gchar *stream_id = NULL;
4196 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4198 event = gst_pad_get_sticky_event (pad, GST_EVENT_STREAM_START, 0);
4199 if (event != NULL) {
4200 gst_event_parse_stream_start (event, &stream_id);
4201 ret = g_strdup (stream_id);
4202 gst_event_unref (event);
4203 GST_CAT_LOG_OBJECT (GST_CAT_PADS, pad, "pad has stream-id '%s'", ret);
4205 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
4206 "pad has not received a stream-start event yet");
4213 * gst_pad_get_stream:
4214 * @pad: A source #GstPad
4216 * Returns the current #GstStream for the @pad, or %NULL if none has been
4217 * set yet, i.e. the pad has not received a stream-start event yet.
4219 * This is a convenience wrapper around gst_pad_get_sticky_event() and
4220 * gst_event_parse_stream().
4222 * Returns: (nullable) (transfer full): the current #GstStream for @pad, or %NULL.
4223 * unref the returned stream when no longer needed.
4228 gst_pad_get_stream (GstPad * pad)
4230 GstStream *stream = NULL;
4233 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4235 event = gst_pad_get_sticky_event (pad, GST_EVENT_STREAM_START, 0);
4236 if (event != NULL) {
4237 gst_event_parse_stream (event, &stream);
4238 gst_event_unref (event);
4239 GST_CAT_LOG_OBJECT (GST_CAT_PADS, pad, "pad has stream object %p", stream);
4241 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
4242 "pad has not received a stream-start event yet");
4249 * gst_util_group_id_next:
4251 * Return a constantly incrementing group id.
4253 * This function is used to generate a new group-id for the
4254 * stream-start event.
4256 * This function never returns %GST_GROUP_ID_INVALID (which is 0)
4258 * Returns: A constantly incrementing unsigned integer, which might
4259 * overflow back to 0 at some point.
4262 gst_util_group_id_next (void)
4264 static gint counter = 1;
4265 gint ret = g_atomic_int_add (&counter, 1);
4267 /* Make sure we don't return GST_GROUP_ID_INVALID */
4268 if (G_UNLIKELY (ret == GST_GROUP_ID_INVALID))
4269 ret = g_atomic_int_add (&counter, 1);
4274 /* Compute log2 of the passed 64-bit number by finding the highest set bit */
4276 gst_log2 (GstClockTime in)
4279 { 0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000, 0xFFFFFFFF00000000LL };
4280 const guint64 S[] = { 1, 2, 4, 8, 16, 32 };
4284 for (i = 5; i >= 0; i--) {
4295 * gst_calculate_linear_regression: (skip)
4296 * @xy: Pairs of (x,y) values
4297 * @temp: Temporary scratch space used by the function
4298 * @n: number of (x,y) pairs
4299 * @m_num: (out): numerator of calculated slope
4300 * @m_denom: (out): denominator of calculated slope
4301 * @b: (out): Offset at Y-axis
4302 * @xbase: (out): Offset at X-axis
4303 * @r_squared: (out): R-squared
4305 * Calculates the linear regression of the values @xy and places the
4306 * result in @m_num, @m_denom, @b and @xbase, representing the function
4307 * y(x) = m_num/m_denom * (x - xbase) + b
4308 * that has the least-square distance from all points @x and @y.
4310 * @r_squared will contain the remaining error.
4312 * If @temp is not %NULL, it will be used as temporary space for the function,
4313 * in which case the function works without any allocation at all. If @temp is
4314 * %NULL, an allocation will take place. @temp should have at least the same
4315 * amount of memory allocated as @xy, i.e. 2*n*sizeof(GstClockTime).
4317 * > This function assumes (x,y) values with reasonable large differences
4318 * > between them. It will not calculate the exact results if the differences
4319 * > between neighbouring values are too small due to not being able to
4320 * > represent sub-integer values during the calculations.
4322 * Returns: %TRUE if the linear regression was successfully calculated
4326 /* http://mathworld.wolfram.com/LeastSquaresFitting.html
4330 gst_calculate_linear_regression (const GstClockTime * xy,
4331 GstClockTime * temp, guint n,
4332 GstClockTime * m_num, GstClockTime * m_denom,
4333 GstClockTime * b, GstClockTime * xbase, gdouble * r_squared)
4335 const GstClockTime *x, *y;
4336 GstClockTime *newx, *newy;
4337 GstClockTime xmin, ymin, xbar, ybar, xbar4, ybar4;
4338 GstClockTime xmax, ymax;
4339 GstClockTimeDiff sxx, sxy, syy;
4344 g_return_val_if_fail (xy != NULL, FALSE);
4345 g_return_val_if_fail (m_num != NULL, FALSE);
4346 g_return_val_if_fail (m_denom != NULL, FALSE);
4347 g_return_val_if_fail (b != NULL, FALSE);
4348 g_return_val_if_fail (xbase != NULL, FALSE);
4349 g_return_val_if_fail (r_squared != NULL, FALSE);
4354 xbar = ybar = sxx = syy = sxy = 0;
4356 xmin = ymin = G_MAXUINT64;
4358 for (i = j = 0; i < n; i++, j += 2) {
4359 xmin = MIN (xmin, x[j]);
4360 ymin = MIN (ymin, y[j]);
4362 xmax = MAX (xmax, x[j]);
4363 ymax = MAX (ymax, y[j]);
4367 /* Allocate up to 1kb on the stack, otherwise heap */
4368 newx = n > 64 ? g_new (GstClockTime, 2 * n) : g_newa (GstClockTime, 2 * n);
4375 /* strip off unnecessary bits of precision */
4376 for (i = j = 0; i < n; i++, j += 2) {
4377 newx[j] = x[j] - xmin;
4378 newy[j] = y[j] - ymin;
4381 #ifdef DEBUGGING_ENABLED
4382 GST_CAT_DEBUG (GST_CAT_CLOCK, "reduced numbers:");
4383 for (i = j = 0; i < n; i++, j += 2)
4384 GST_CAT_DEBUG (GST_CAT_CLOCK,
4385 " %" G_GUINT64_FORMAT " %" G_GUINT64_FORMAT, newx[j], newy[j]);
4388 /* have to do this precisely otherwise the results are pretty much useless.
4389 * should guarantee that none of these accumulators can overflow */
4391 /* quantities on the order of 1e10 to 1e13 -> 30-35 bits;
4392 * window size a max of 2^10, so
4393 this addition could end up around 2^45 or so -- ample headroom */
4394 for (i = j = 0; i < n; i++, j += 2) {
4395 /* Just in case assumptions about headroom prove false, let's check */
4396 if ((newx[j] > 0 && G_MAXUINT64 - xbar <= newx[j]) ||
4397 (newy[j] > 0 && G_MAXUINT64 - ybar <= newy[j])) {
4398 GST_CAT_WARNING (GST_CAT_CLOCK,
4399 "Regression overflowed in clock slaving! xbar %"
4400 G_GUINT64_FORMAT " newx[j] %" G_GUINT64_FORMAT " ybar %"
4401 G_GUINT64_FORMAT " newy[j] %" G_GUINT64_FORMAT, xbar, newx[j], ybar,
4403 if (temp == NULL && n > 64)
4414 /* multiplying directly would give quantities on the order of 1e20-1e26 ->
4415 * 60 bits to 70 bits times the window size that's 80 which is too much.
4416 * Instead we (1) subtract off the xbar*ybar in the loop instead of after,
4417 * to avoid accumulation; (2) shift off some estimated number of bits from
4418 * each multiplicand to limit the expected ceiling. For strange
4419 * distributions of input values, things can still overflow, in which
4420 * case we drop precision and retry - at most a few times, in practice rarely
4423 /* Guess how many bits we might need for the usual distribution of input,
4424 * with a fallback loop that drops precision if things go pear-shaped */
4425 max_bits = gst_log2 (MAX (xmax - xmin, ymax - ymin)) * 7 / 8 + gst_log2 (n);
4427 pshift = max_bits - 64;
4431 #ifdef DEBUGGING_ENABLED
4432 GST_CAT_DEBUG (GST_CAT_CLOCK,
4433 "Restarting regression with precision shift %u", pshift);
4436 xbar4 = xbar >> pshift;
4437 ybar4 = ybar >> pshift;
4438 sxx = syy = sxy = 0;
4439 for (i = j = 0; i < n; i++, j += 2) {
4440 GstClockTime newx4, newy4;
4441 GstClockTimeDiff tmp;
4443 newx4 = newx[j] >> pshift;
4444 newy4 = newy[j] >> pshift;
4446 tmp = (newx4 + xbar4) * (newx4 - xbar4);
4447 if (G_UNLIKELY (tmp > 0 && sxx > 0 && (G_MAXINT64 - sxx <= tmp))) {
4449 /* Drop some precision and restart */
4453 } while (G_MAXINT64 - sxx <= tmp);
4455 } else if (G_UNLIKELY (tmp < 0 && sxx < 0 && (G_MININT64 - sxx >= tmp))) {
4457 /* Drop some precision and restart */
4461 } while (G_MININT64 - sxx >= tmp);
4466 tmp = newy4 * newy4 - ybar4 * ybar4;
4467 if (G_UNLIKELY (tmp > 0 && syy > 0 && (G_MAXINT64 - syy <= tmp))) {
4472 } while (G_MAXINT64 - syy <= tmp);
4474 } else if (G_UNLIKELY (tmp < 0 && syy < 0 && (G_MININT64 - syy >= tmp))) {
4479 } while (G_MININT64 - syy >= tmp);
4484 tmp = newx4 * newy4 - xbar4 * ybar4;
4485 if (G_UNLIKELY (tmp > 0 && sxy > 0 && (G_MAXINT64 - sxy <= tmp))) {
4490 } while (G_MAXINT64 - sxy <= tmp);
4492 } else if (G_UNLIKELY (tmp < 0 && sxy < 0 && (G_MININT64 - sxy >= tmp))) {
4497 } while (G_MININT64 - sxy >= tmp);
4504 if (G_UNLIKELY (sxx == 0))
4509 *b = (ymin + ybar) - gst_util_uint64_scale_round (xbar, *m_num, *m_denom);
4510 /* Report base starting from the most recent observation */
4512 *b += gst_util_uint64_scale_round (xmax - xmin, *m_num, *m_denom);
4514 *r_squared = ((double) sxy * (double) sxy) / ((double) sxx * (double) syy);
4516 #ifdef DEBUGGING_ENABLED
4517 GST_CAT_DEBUG (GST_CAT_CLOCK, " m = %g", ((double) *m_num) / *m_denom);
4518 GST_CAT_DEBUG (GST_CAT_CLOCK, " b = %" G_GUINT64_FORMAT, *b);
4519 GST_CAT_DEBUG (GST_CAT_CLOCK, " xbase = %" G_GUINT64_FORMAT, *xbase);
4520 GST_CAT_DEBUG (GST_CAT_CLOCK, " r2 = %g", *r_squared);
4523 if (temp == NULL && n > 64)
4530 GST_CAT_DEBUG (GST_CAT_CLOCK, "sxx == 0, regression failed");
4531 if (temp == NULL && n > 64)
4537 #ifdef TIZEN_PROFILE_TV
4539 * gst_element_query_resource
4540 * @element: (in): a #GstElement to invoke the resource query on.
4541 * @resources: (out): a pointer to the list of resources asked for.
4542 * Returns: TRUE if the query could be performed.
4545 gst_element_query_resource (GstElement * element, GList ** resource_list)
4549 char resources[250];
4550 char *element_resource;
4551 guint count = 0, i = 0;
4555 if (resource_list == NULL) {
4559 query = gst_query_new_resource (resources);
4560 if (GST_IS_BIN (element)) {
4561 count = gst_child_proxy_get_children_count ((GstChildProxy *)element);
4562 for (i = 0; i < count; i++) {
4563 if (!(object = gst_child_proxy_get_child_by_index ((GstChildProxy *)element, i)))
4566 if (GST_IS_BIN (object)) {
4567 ret = gst_element_query_resource ((GstElement *)object, resource_list);
4568 gst_object_unref (object);
4572 ret = gst_element_query ((GstElement *) object, query);
4574 element_resource = gst_query_parse_resource (query);
4575 resource_num = (int) atoi (element_resource);
4576 GST_DEBUG_OBJECT (element,
4577 "\n resource ID received after query is :%d\n", resource_num);
4578 if (NULL == (g_list_find (*resource_list, (gconstpointer)resource_num))) {
4580 g_list_append (*resource_list, GINT_TO_POINTER (resource_num));
4583 gst_object_unref (object);
4586 ret = gst_element_query ((GstElement *) element, query);
4588 element_resource = gst_query_parse_resource (query);
4589 resource_num = (int) atoi (element_resource);
4590 GST_DEBUG_OBJECT (element, "\n resource ID received after query is :%d\n",
4592 if (NULL == (g_list_find (*resource_list, (gconstpointer)resource_num))) {
4594 g_list_append (*resource_list, GINT_TO_POINTER (resource_num));
4598 gst_query_unref (query);
4603 set_family_id (const GValue * item, gpointer user_data)
4605 gpointer object = g_value_get_object (item);
4606 if (GST_IS_PAD (object)) {
4607 GstPad *pad = GST_PAD_CAST (object);
4608 g_object_set (G_OBJECT (pad), "family-id", (int) user_data, NULL);
4609 } else if (GST_IS_ELEMENT (object)) {
4610 gst_set_family_id_to_child (GST_ELEMENT_CAST (object), (int) user_data);
4616 gst_set_family_id_to_child (GstElement * child, int id)
4619 if (!GST_IS_ELEMENT (child))
4621 g_object_set (G_OBJECT (child), "family-id", id, NULL);
4622 it = gst_element_iterate_pads (child);
4623 gst_iterator_foreach (it, (GstIteratorForeachFunction) set_family_id,
4625 gst_iterator_free (it);
4628 if (!GST_IS_BIN (child))
4630 it = gst_bin_iterate_recurse (GST_BIN (child));
4631 gst_iterator_foreach (it, (GstIteratorForeachFunction) set_family_id,
4633 gst_iterator_free (it);
4641 * gst_type_mark_as_plugin_api:
4643 * @flags: a set of #GstPluginAPIFlags to further inform cache generation.
4645 * Marks @type as plugin API. This should be called in `class_init` of
4646 * elements that expose new types (i.e. enums, flags or internal GObjects) via
4647 * properties, signals or pad templates.
4649 * Types exposed by plugins are not automatically added to the documentation
4650 * as they might originate from another library and should in that case be
4651 * documented via that library instead.
4653 * By marking a type as plugin API it will be included in the documentation of
4654 * the plugin that defines it.
4659 gst_type_mark_as_plugin_api (GType type, GstPluginAPIFlags flags)
4661 g_type_set_qdata (type, GST_QUARK (PLUGIN_API), GINT_TO_POINTER (TRUE));
4662 g_type_set_qdata (type, GST_QUARK (PLUGIN_API_FLAGS),
4663 GINT_TO_POINTER (flags));
4667 * gst_type_is_plugin_api:
4669 * @flags: (out) (optional): What #GstPluginAPIFlags the plugin was marked with
4671 * Checks if @type is plugin API. See gst_type_mark_as_plugin_api() for
4674 * Returns: %TRUE if @type is plugin API or %FALSE otherwise.
4679 gst_type_is_plugin_api (GType type, GstPluginAPIFlags * flags)
4682 ! !GPOINTER_TO_INT (g_type_get_qdata (type, GST_QUARK (PLUGIN_API)));
4686 GPOINTER_TO_INT (g_type_get_qdata (type, GST_QUARK (PLUGIN_API_FLAGS)));