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"
47 #include "gst-i18n-lib.h"
48 #include "glib-compat-private.h"
53 * @mem: a pointer to the memory to dump
54 * @size: the size of the memory block to dump
56 * Dumps the memory block into a hex representation. Useful for debugging.
59 gst_util_dump_mem (const guchar * mem, guint size)
62 GString *string = g_string_sized_new (50);
63 GString *chars = g_string_sized_new (18);
67 if (g_ascii_isprint (mem[i]))
68 g_string_append_c (chars, mem[i]);
70 g_string_append_c (chars, '.');
72 g_string_append_printf (string, "%02x ", mem[i]);
77 if (j == 16 || i == size) {
78 g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j, mem + i - j,
79 string->str, chars->str);
80 g_string_set_size (string, 0);
81 g_string_set_size (chars, 0);
85 g_string_free (string, TRUE);
86 g_string_free (chars, TRUE);
91 * gst_util_set_value_from_string:
92 * @value: (out caller-allocates): the value to set
93 * @value_str: the string to get the value from
95 * Converts the string to the type of the value and
96 * sets the value with it.
98 * Note that this function is dangerous as it does not return any indication
99 * if the conversion worked or not.
102 gst_util_set_value_from_string (GValue * value, const gchar * value_str)
106 g_return_if_fail (value != NULL);
107 g_return_if_fail (value_str != NULL);
109 GST_CAT_DEBUG (GST_CAT_PARAMS, "parsing '%s' to type %s", value_str,
110 g_type_name (G_VALUE_TYPE (value)));
112 res = gst_value_deserialize (value, value_str);
113 if (!res && G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
114 /* backwards compat, all booleans that fail to parse are false */
115 g_value_set_boolean (value, FALSE);
118 g_return_if_fail (res);
122 * gst_util_set_object_arg:
123 * @object: the object to set the argument of
124 * @name: the name of the argument to set
125 * @value: the string value to set
127 * Converts the string value to the type of the objects argument and
128 * sets the argument with it.
130 * Note that this function silently returns if @object has no property named
131 * @name or when @value cannot be converted to the type of the property.
134 gst_util_set_object_arg (GObject * object, const gchar * name,
141 g_return_if_fail (G_IS_OBJECT (object));
142 g_return_if_fail (name != NULL);
143 g_return_if_fail (value != NULL);
145 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
149 value_type = pspec->value_type;
151 GST_DEBUG ("pspec->flags is %d, pspec->value_type is %s",
152 pspec->flags, g_type_name (value_type));
154 if (!(pspec->flags & G_PARAM_WRITABLE))
157 g_value_init (&v, value_type);
159 /* special case for element <-> xml (de)serialisation */
160 if (value_type == GST_TYPE_STRUCTURE && strcmp (value, "NULL") == 0) {
161 g_value_set_boxed (&v, NULL);
165 if (!gst_value_deserialize (&v, value))
170 g_object_set_property (object, pspec->name, &v);
175 * gst_util_set_object_array:
176 * @object: the object to set the array to
177 * @name: the name of the property to set
178 * @array: a #GValueArray containing the values
180 * Transfer a #GValueArray to %GST_TYPE_ARRAY and set this value on the
181 * specified property name. This allow language bindings to set GST_TYPE_ARRAY
182 * properties which are otherwise not an accessible type.
187 gst_util_set_object_array (GObject * object, const gchar * name,
188 const GValueArray * array)
190 GValue v1 = G_VALUE_INIT, v2 = G_VALUE_INIT;
191 gboolean ret = FALSE;
193 g_value_init (&v1, G_TYPE_VALUE_ARRAY);
194 g_value_init (&v2, GST_TYPE_ARRAY);
196 g_value_set_static_boxed (&v1, array);
198 if (g_value_transform (&v1, &v2)) {
199 g_object_set_property (object, name, &v2);
210 * gst_util_get_object_array:
211 * @object: the object to set the array to
212 * @name: the name of the property to set
213 * @array: (out): a return #GValueArray
215 * Get a property of type %GST_TYPE_ARRAY and transform it into a
216 * #GValueArray. This allow language bindings to get GST_TYPE_ARRAY
217 * properties which are otherwise not an accessible type.
222 gst_util_get_object_array (GObject * object, const gchar * name,
223 GValueArray ** array)
225 GValue v1 = G_VALUE_INIT, v2 = G_VALUE_INIT;
226 gboolean ret = FALSE;
228 g_value_init (&v1, G_TYPE_VALUE_ARRAY);
229 g_value_init (&v2, GST_TYPE_ARRAY);
231 g_object_get_property (object, name, &v2);
233 if (g_value_transform (&v2, &v1)) {
234 *array = g_value_get_boxed (&v1);
243 /* work around error C2520: conversion from unsigned __int64 to double
244 * not implemented, use signed __int64
246 * These are implemented as functions because on some platforms a 64bit int to
247 * double conversion is not defined/implemented.
251 gst_util_guint64_to_gdouble (guint64 value)
253 if (value & G_GINT64_CONSTANT (0x8000000000000000))
254 return (gdouble) ((gint64) value) + (gdouble) 18446744073709551616.;
256 return (gdouble) ((gint64) value);
260 gst_util_gdouble_to_guint64 (gdouble value)
262 if (value < (gdouble) 9223372036854775808.) /* 1 << 63 */
263 return ((guint64) ((gint64) value));
265 value -= (gdouble) 18446744073709551616.;
266 return ((guint64) ((gint64) value));
269 #ifndef HAVE_UINT128_T
270 /* convenience struct for getting high and low uint32 parts of
277 #if G_BYTE_ORDER == G_BIG_ENDIAN
285 #if defined (__x86_64__) && defined (__GNUC__)
287 gst_util_uint64_mul_uint64 (GstUInt64 * c1, GstUInt64 * c0, guint64 arg1,
290 __asm__ __volatile__ ("mulq %3":"=a" (c0->ll), "=d" (c1->ll)
291 :"a" (arg1), "g" (arg2)
294 #else /* defined (__x86_64__) */
295 /* multiply two 64-bit unsigned ints into a 128-bit unsigned int. the high
296 * and low 64 bits of the product are placed in c1 and c0 respectively.
297 * this operation cannot overflow. */
299 gst_util_uint64_mul_uint64 (GstUInt64 * c1, GstUInt64 * c0, guint64 arg1,
309 /* do 128 bits multiply
317 * -------------------
320 * "a0" is optimized away, result is stored directly in c0. "b1" is
321 * optimized away, result is stored directly in c1.
323 c0->ll = (guint64) v.l.low * n.l.low;
324 a1.ll = (guint64) v.l.low * n.l.high;
325 b0.ll = (guint64) v.l.high * n.l.low;
327 /* add the high word of a0 to the low words of a1 and b0 using c1 as
328 * scrach space to capture the carry. the low word of the result becomes
329 * the final high word of c0 */
330 c1->ll = (guint64) c0->l.high + a1.l.low + b0.l.low;
331 c0->l.high = c1->l.low;
333 /* add the carry from the result above (found in the high word of c1) and
334 * the high words of a1 and b0 to b1, the result is c1. */
335 c1->ll = (guint64) v.l.high * n.l.high + c1->l.high + a1.l.high + b0.l.high;
337 #endif /* defined (__x86_64__) */
339 #if defined (__x86_64__) && defined (__GNUC__)
340 static inline guint64
341 gst_util_div128_64 (GstUInt64 c1, GstUInt64 c0, guint64 denom)
345 __asm__ __volatile__ ("divq %3":"=a" (res)
346 :"d" (c1.ll), "a" (c0.ll), "g" (denom)
352 /* count leading zeros */
354 gst_util_clz (guint32 val)
358 s = val | (val >> 1);
362 s = ~(s | (s >> 16));
363 s = s - ((s >> 1) & 0x55555555);
364 s = (s & 0x33333333) + ((s >> 2) & 0x33333333);
365 s = (s + (s >> 4)) & 0x0f0f0f0f;
367 s = (s + (s >> 16)) & 0x3f;
372 /* based on Hacker's Delight p152 */
373 static inline guint64
374 gst_util_div128_64 (GstUInt64 c1, GstUInt64 c0, guint64 denom)
376 GstUInt64 q1, q0, rhat;
377 GstUInt64 v, cmp1, cmp2;
382 /* count number of leading zeroes, we know they must be in the high
383 * part of denom since denom > G_MAXUINT32. */
384 s = gst_util_clz (v.l.high);
387 /* normalize divisor and dividend */
389 c1.ll = (c1.ll << s) | (c0.l.high >> (32 - s));
393 q1.ll = c1.ll / v.l.high;
394 rhat.ll = c1.ll - q1.ll * v.l.high;
396 cmp1.l.high = rhat.l.low;
397 cmp1.l.low = c0.l.high;
398 cmp2.ll = q1.ll * v.l.low;
400 while (q1.l.high || cmp2.ll > cmp1.ll) {
405 cmp1.l.high = rhat.l.low;
408 c1.l.high = c1.l.low;
409 c1.l.low = c0.l.high;
410 c1.ll -= q1.ll * v.ll;
411 q0.ll = c1.ll / v.l.high;
412 rhat.ll = c1.ll - q0.ll * v.l.high;
414 cmp1.l.high = rhat.l.low;
415 cmp1.l.low = c0.l.low;
416 cmp2.ll = q0.ll * v.l.low;
418 while (q0.l.high || cmp2.ll > cmp1.ll) {
423 cmp1.l.high = rhat.l.low;
426 q0.l.high += q1.l.low;
430 #endif /* defined (__GNUC__) */
432 /* This always gives the correct result because:
433 * a) val <= G_MAXUINT64-1
434 * b) (c0,c1) <= G_MAXUINT64 * (G_MAXUINT64-1)
436 * (c0,c1) == G_MAXUINT64 * G_MAXUINT64 and denom < G_MAXUINT64
437 * (note: num==denom case is handled by short path)
438 * This means that (c0,c1) either has enough space for val
439 * or that the overall result will overflow anyway.
442 /* add correction with carry */
443 #define CORRECT(c0,c1,val) \
445 if (G_MAXUINT64 - c0.ll < val) { \
446 if (G_UNLIKELY (c1.ll == G_MAXUINT64)) \
448 return G_MAXUINT64; \
455 gst_util_uint64_scale_uint64_unchecked (guint64 val, guint64 num,
456 guint64 denom, guint64 correct)
460 /* compute 128-bit numerator product */
461 gst_util_uint64_mul_uint64 (&c1, &c0, val, num);
463 /* perform rounding correction */
464 CORRECT (c0, c1, correct);
466 /* high word as big as or bigger than denom --> overflow */
467 if (G_UNLIKELY (c1.ll >= denom))
470 /* compute quotient, fits in 64 bits */
471 return gst_util_div128_64 (c1, c0, denom);
475 #define GST_MAXUINT128 ((__uint128_t) -1)
477 gst_util_uint64_scale_uint64_unchecked (guint64 val, guint64 num,
478 guint64 denom, guint64 correct)
482 /* Calculate val * num */
483 tmp = ((__uint128_t) val) * ((__uint128_t) num);
485 /* overflow checks */
486 if (G_UNLIKELY (GST_MAXUINT128 - correct < tmp))
489 /* perform rounding correction */
492 /* Divide by denom */
495 /* if larger than G_MAXUINT64 --> overflow */
496 if (G_UNLIKELY (tmp > G_MAXUINT64))
499 /* compute quotient, fits in 64 bits */
500 return (guint64) tmp;
505 #if !defined (__x86_64__) && !defined (HAVE_UINT128_T)
507 gst_util_uint64_mul_uint32 (GstUInt64 * c1, GstUInt64 * c0, guint64 arg1,
514 c0->ll = (guint64) a.l.low * arg2;
515 c1->ll = (guint64) a.l.high * arg2 + c0->l.high;
519 /* divide a 96-bit unsigned int by a 32-bit unsigned int when we know the
520 * quotient fits into 64 bits. the high 64 bits and low 32 bits of the
521 * numerator are expected in c1 and c0 respectively. */
522 static inline guint64
523 gst_util_div96_32 (guint64 c1, guint64 c0, guint32 denom)
525 c0 += (c1 % denom) << 32;
526 return ((c1 / denom) << 32) + (c0 / denom);
529 static inline guint64
530 gst_util_uint64_scale_uint32_unchecked (guint64 val, guint32 num,
531 guint32 denom, guint32 correct)
535 /* compute 96-bit numerator product */
536 gst_util_uint64_mul_uint32 (&c1, &c0, val, num);
538 /* condition numerator based on rounding mode */
539 CORRECT (c0, c1, correct);
541 /* high 32 bits as big as or bigger than denom --> overflow */
542 if (G_UNLIKELY (c1.l.high >= denom))
545 /* compute quotient, fits in 64 bits */
546 return gst_util_div96_32 (c1.ll, c0.ll, denom);
550 /* the guts of the gst_util_uint64_scale() variants */
552 _gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom,
555 g_return_val_if_fail (denom != 0, G_MAXUINT64);
557 if (G_UNLIKELY (num == 0))
560 if (G_UNLIKELY (num == denom))
563 /* on 64bits we always use a full 128bits multiply/division */
564 #if !defined (__x86_64__) && !defined (HAVE_UINT128_T)
565 /* denom is low --> try to use 96 bit muldiv */
566 if (G_LIKELY (denom <= G_MAXUINT32)) {
567 /* num is low --> use 96 bit muldiv */
568 if (G_LIKELY (num <= G_MAXUINT32))
569 return gst_util_uint64_scale_uint32_unchecked (val, (guint32) num,
570 (guint32) denom, correct);
572 /* num is high but val is low --> swap and use 96-bit muldiv */
573 if (G_LIKELY (val <= G_MAXUINT32))
574 return gst_util_uint64_scale_uint32_unchecked (num, (guint32) val,
575 (guint32) denom, correct);
577 #endif /* !defined (__x86_64__) && !defined (HAVE_UINT128_T) */
579 /* val is high and num is high --> use 128-bit muldiv */
580 return gst_util_uint64_scale_uint64_unchecked (val, num, denom, correct);
584 * gst_util_uint64_scale:
585 * @val: the number to scale
586 * @num: the numerator of the scale ratio
587 * @denom: the denominator of the scale ratio
589 * Scale @val by the rational number @num / @denom, avoiding overflows and
590 * underflows and without loss of precision.
592 * This function can potentially be very slow if val and num are both
593 * greater than G_MAXUINT32.
595 * Returns: @val * @num / @denom. In the case of an overflow, this
596 * function returns G_MAXUINT64. If the result is not exactly
597 * representable as an integer it is truncated. See also
598 * gst_util_uint64_scale_round(), gst_util_uint64_scale_ceil(),
599 * gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
600 * gst_util_uint64_scale_int_ceil().
603 gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom)
605 return _gst_util_uint64_scale (val, num, denom, 0);
609 * gst_util_uint64_scale_round:
610 * @val: the number to scale
611 * @num: the numerator of the scale ratio
612 * @denom: the denominator of the scale ratio
614 * Scale @val by the rational number @num / @denom, avoiding overflows and
615 * underflows and without loss of precision.
617 * This function can potentially be very slow if val and num are both
618 * greater than G_MAXUINT32.
620 * Returns: @val * @num / @denom. In the case of an overflow, this
621 * function returns G_MAXUINT64. If the result is not exactly
622 * representable as an integer, it is rounded to the nearest integer
623 * (half-way cases are rounded up). See also gst_util_uint64_scale(),
624 * gst_util_uint64_scale_ceil(), gst_util_uint64_scale_int(),
625 * gst_util_uint64_scale_int_round(), gst_util_uint64_scale_int_ceil().
628 gst_util_uint64_scale_round (guint64 val, guint64 num, guint64 denom)
630 return _gst_util_uint64_scale (val, num, denom, denom >> 1);
634 * gst_util_uint64_scale_ceil:
635 * @val: the number to scale
636 * @num: the numerator of the scale ratio
637 * @denom: the denominator of the scale ratio
639 * Scale @val by the rational number @num / @denom, avoiding overflows and
640 * underflows and without loss of precision.
642 * This function can potentially be very slow if val and num are both
643 * greater than G_MAXUINT32.
645 * Returns: @val * @num / @denom. In the case of an overflow, this
646 * function returns G_MAXUINT64. If the result is not exactly
647 * representable as an integer, it is rounded up. See also
648 * gst_util_uint64_scale(), gst_util_uint64_scale_round(),
649 * gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
650 * gst_util_uint64_scale_int_ceil().
653 gst_util_uint64_scale_ceil (guint64 val, guint64 num, guint64 denom)
655 return _gst_util_uint64_scale (val, num, denom, denom - 1);
658 /* the guts of the gst_util_uint64_scale_int() variants */
660 _gst_util_uint64_scale_int (guint64 val, gint num, gint denom, gint correct)
662 g_return_val_if_fail (denom > 0, G_MAXUINT64);
663 g_return_val_if_fail (num >= 0, G_MAXUINT64);
665 if (G_UNLIKELY (num == 0))
668 if (G_UNLIKELY (num == denom))
671 if (val <= G_MAXUINT32) {
672 /* simple case. num and denom are not negative so casts are OK. when
673 * not truncating, the additions to the numerator cannot overflow
674 * because val*num <= G_MAXUINT32 * G_MAXINT32 < G_MAXUINT64 -
675 * G_MAXINT32, so there's room to add another gint32. */
676 val *= (guint64) num;
677 /* add rounding correction */
680 return val / (guint64) denom;
682 #if !defined (__x86_64__) && !defined (HAVE_UINT128_T)
683 /* num and denom are not negative so casts are OK */
684 return gst_util_uint64_scale_uint32_unchecked (val, (guint32) num,
685 (guint32) denom, (guint32) correct);
687 /* always use full 128bits scale */
688 return gst_util_uint64_scale_uint64_unchecked (val, num, denom, correct);
693 * gst_util_uint64_scale_int:
694 * @val: guint64 (such as a #GstClockTime) to scale.
695 * @num: numerator of the scale factor.
696 * @denom: denominator of the scale factor.
698 * Scale @val by the rational number @num / @denom, avoiding overflows and
699 * underflows and without loss of precision. @num must be non-negative and
700 * @denom must be positive.
702 * Returns: @val * @num / @denom. In the case of an overflow, this
703 * function returns G_MAXUINT64. If the result is not exactly
704 * representable as an integer, it is truncated. See also
705 * gst_util_uint64_scale_int_round(), gst_util_uint64_scale_int_ceil(),
706 * gst_util_uint64_scale(), gst_util_uint64_scale_round(),
707 * gst_util_uint64_scale_ceil().
710 gst_util_uint64_scale_int (guint64 val, gint num, gint denom)
712 return _gst_util_uint64_scale_int (val, num, denom, 0);
716 * gst_util_uint64_scale_int_round:
717 * @val: guint64 (such as a #GstClockTime) to scale.
718 * @num: numerator of the scale factor.
719 * @denom: denominator of the scale factor.
721 * Scale @val by the rational number @num / @denom, avoiding overflows and
722 * underflows and without loss of precision. @num must be non-negative and
723 * @denom must be positive.
725 * Returns: @val * @num / @denom. In the case of an overflow, this
726 * function returns G_MAXUINT64. If the result is not exactly
727 * representable as an integer, it is rounded to the nearest integer
728 * (half-way cases are rounded up). See also gst_util_uint64_scale_int(),
729 * gst_util_uint64_scale_int_ceil(), gst_util_uint64_scale(),
730 * gst_util_uint64_scale_round(), gst_util_uint64_scale_ceil().
733 gst_util_uint64_scale_int_round (guint64 val, gint num, gint denom)
735 /* we can use a shift to divide by 2 because denom is required to be
737 return _gst_util_uint64_scale_int (val, num, denom, denom >> 1);
741 * gst_util_uint64_scale_int_ceil:
742 * @val: guint64 (such as a #GstClockTime) to scale.
743 * @num: numerator of the scale factor.
744 * @denom: denominator of the scale factor.
746 * Scale @val by the rational number @num / @denom, avoiding overflows and
747 * underflows and without loss of precision. @num must be non-negative and
748 * @denom must be positive.
750 * Returns: @val * @num / @denom. In the case of an overflow, this
751 * function returns G_MAXUINT64. If the result is not exactly
752 * representable as an integer, it is rounded up. See also
753 * gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
754 * gst_util_uint64_scale(), gst_util_uint64_scale_round(),
755 * gst_util_uint64_scale_ceil().
758 gst_util_uint64_scale_int_ceil (guint64 val, gint num, gint denom)
760 return _gst_util_uint64_scale_int (val, num, denom, denom - 1);
764 * gst_util_seqnum_next:
766 * Return a constantly incrementing sequence number.
768 * This function is used internally to GStreamer to be able to determine which
769 * events and messages are "the same". For example, elements may set the seqnum
770 * on a segment-done message to be the same as that of the last seek event, to
771 * indicate that event and the message correspond to the same segment.
773 * Returns: A constantly incrementing 32-bit unsigned integer, which might
774 * overflow back to 0 at some point. Use gst_util_seqnum_compare() to make sure
775 * you handle wraparound correctly.
778 gst_util_seqnum_next (void)
780 static gint counter = 0;
781 return g_atomic_int_add (&counter, 1);
785 * gst_util_seqnum_compare:
786 * @s1: A sequence number.
787 * @s2: Another sequence number.
789 * Compare two sequence numbers, handling wraparound.
791 * The current implementation just returns (gint32)(@s1 - @s2).
793 * Returns: A negative number if @s1 is before @s2, 0 if they are equal, or a
794 * positive number if @s1 is after @s2.
797 gst_util_seqnum_compare (guint32 s1, guint32 s2)
799 return (gint32) (s1 - s2);
802 /* -----------------------------------------------------
804 * The following code will be moved out of the main
805 * gstreamer library someday.
811 * gst_element_create_all_pads:
812 * @element: (transfer none): a #GstElement to create pads for
814 * Creates a pad for each pad template that is always available.
815 * This function is only useful during object initialization of
816 * subclasses of #GstElement.
819 gst_element_create_all_pads (GstElement * element)
823 /* FIXME: lock element */
826 gst_element_class_get_pad_template_list (GST_ELEMENT_CLASS
827 (G_OBJECT_GET_CLASS (element)));
830 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
832 if (padtempl->presence == GST_PAD_ALWAYS) {
835 pad = gst_pad_new_from_template (padtempl, padtempl->name_template);
837 gst_element_add_pad (element, pad);
839 padlist = padlist->next;
844 * gst_element_get_compatible_pad_template:
845 * @element: (transfer none): a #GstElement to get a compatible pad template for
846 * @compattempl: (transfer none): the #GstPadTemplate to find a compatible
849 * Retrieves a pad template from @element that is compatible with @compattempl.
850 * Pads from compatible templates can be linked together.
852 * Returns: (transfer none) (nullable): a compatible #GstPadTemplate,
853 * or %NULL if none was found. No unreferencing is necessary.
856 gst_element_get_compatible_pad_template (GstElement * element,
857 GstPadTemplate * compattempl)
859 GstPadTemplate *newtempl = NULL;
861 GstElementClass *class;
864 g_return_val_if_fail (element != NULL, NULL);
865 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
866 g_return_val_if_fail (compattempl != NULL, NULL);
868 class = GST_ELEMENT_GET_CLASS (element);
870 padlist = gst_element_class_get_pad_template_list (class);
872 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
873 "Looking for a suitable pad template in %s out of %d templates...",
874 GST_ELEMENT_NAME (element), g_list_length (padlist));
877 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
881 * Check direction (must be opposite)
884 GST_CAT_LOG (GST_CAT_CAPS,
885 "checking pad template %s", padtempl->name_template);
886 if (padtempl->direction != compattempl->direction) {
887 GST_CAT_DEBUG (GST_CAT_CAPS,
888 "compatible direction: found %s pad template \"%s\"",
889 padtempl->direction == GST_PAD_SRC ? "src" : "sink",
890 padtempl->name_template);
892 GST_CAT_DEBUG (GST_CAT_CAPS,
893 "intersecting %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (compattempl));
894 GST_CAT_DEBUG (GST_CAT_CAPS,
895 "..and %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (padtempl));
897 compatible = gst_caps_can_intersect (GST_PAD_TEMPLATE_CAPS (compattempl),
898 GST_PAD_TEMPLATE_CAPS (padtempl));
900 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
901 (compatible ? "" : "not "));
909 padlist = g_list_next (padlist);
912 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
913 "Returning new pad template %p", newtempl);
915 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "No compatible pad template found");
921 * gst_element_get_pad_from_template:
922 * @element: (transfer none): a #GstElement.
923 * @templ: (transfer none): a #GstPadTemplate belonging to @element.
925 * Gets a pad from @element described by @templ. If the presence of @templ is
926 * #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
929 * Returns: (transfer full) (nullable): the #GstPad, or %NULL if one
930 * could not be found or created.
933 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
936 GstPadPresence presence;
938 /* If this function is ever exported, we need check the validity of `element'
939 * and `templ', and to make sure the template actually belongs to the
942 presence = GST_PAD_TEMPLATE_PRESENCE (templ);
946 case GST_PAD_SOMETIMES:
947 ret = gst_element_get_static_pad (element, templ->name_template);
948 if (!ret && presence == GST_PAD_ALWAYS)
950 ("Element %s has an ALWAYS template %s, but no pad of the same name",
951 GST_OBJECT_NAME (element), templ->name_template);
954 case GST_PAD_REQUEST:
955 ret = gst_element_request_pad (element, templ, NULL, NULL);
963 * gst_element_request_compatible_pad:
964 * @element: a #GstElement.
965 * @templ: the #GstPadTemplate to which the new pad should be able to link.
967 * Requests a pad from @element. The returned pad should be unlinked and
968 * compatible with @templ. Might return an existing pad, or request a new one.
970 * Returns: (nullable): a #GstPad, or %NULL if one could not be found
974 gst_element_request_compatible_pad (GstElement * element,
975 GstPadTemplate * templ)
977 GstPadTemplate *templ_new;
980 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
981 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
983 /* FIXME: should really loop through the templates, testing each for
984 * compatibility and pad availability. */
985 templ_new = gst_element_get_compatible_pad_template (element, templ);
987 pad = gst_element_get_pad_from_template (element, templ_new);
988 /* This can happen for non-request pads. */
989 if (pad && GST_PAD_PEER (pad)) {
990 gst_object_unref (pad);
998 * Checks if the source pad and the sink pad can be linked.
999 * Both @srcpad and @sinkpad must be unlinked and have a parent.
1002 gst_pad_check_link (GstPad * srcpad, GstPad * sinkpad)
1004 /* generic checks */
1005 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1006 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1008 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1009 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1011 if (GST_PAD_PEER (srcpad) != NULL) {
1012 GST_CAT_INFO (GST_CAT_PADS, "Source pad %s:%s has a peer, failed",
1013 GST_DEBUG_PAD_NAME (srcpad));
1016 if (GST_PAD_PEER (sinkpad) != NULL) {
1017 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has a peer, failed",
1018 GST_DEBUG_PAD_NAME (sinkpad));
1021 if (!GST_PAD_IS_SRC (srcpad)) {
1022 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s is not source pad, failed",
1023 GST_DEBUG_PAD_NAME (srcpad));
1026 if (!GST_PAD_IS_SINK (sinkpad)) {
1027 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s is not sink pad, failed",
1028 GST_DEBUG_PAD_NAME (sinkpad));
1031 if (GST_PAD_PARENT (srcpad) == NULL) {
1032 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s has no parent, failed",
1033 GST_DEBUG_PAD_NAME (srcpad));
1036 if (GST_PAD_PARENT (sinkpad) == NULL) {
1037 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has no parent, failed",
1038 GST_DEBUG_PAD_NAME (srcpad));
1046 * gst_element_get_compatible_pad:
1047 * @element: (transfer none): a #GstElement in which the pad should be found.
1048 * @pad: (transfer none): the #GstPad to find a compatible one for.
1049 * @caps: (allow-none): the #GstCaps to use as a filter.
1051 * Looks for an unlinked pad to which the given pad can link. It is not
1052 * guaranteed that linking the pads will work, though it should work in most
1055 * This function will first attempt to find a compatible unlinked ALWAYS pad,
1056 * and if none can be found, it will request a compatible REQUEST pad by looking
1057 * at the templates of @element.
1059 * Returns: (transfer full) (nullable): the #GstPad to which a link
1060 * can be made, or %NULL if one cannot be found. gst_object_unref()
1064 gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
1068 GstPadTemplate *templ;
1070 GstPad *foundpad = NULL;
1072 GValue padptr = { 0, };
1074 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1075 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1077 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1078 "finding pad in %s compatible with %s:%s",
1079 GST_ELEMENT_NAME (element), GST_DEBUG_PAD_NAME (pad));
1081 g_return_val_if_fail (GST_PAD_PEER (pad) == NULL, NULL);
1085 /* try to get an existing unlinked pad */
1086 if (GST_PAD_IS_SRC (pad)) {
1087 pads = gst_element_iterate_sink_pads (element);
1088 } else if (GST_PAD_IS_SINK (pad)) {
1089 pads = gst_element_iterate_src_pads (element);
1091 pads = gst_element_iterate_pads (element);
1095 switch (gst_iterator_next (pads, &padptr)) {
1096 case GST_ITERATOR_OK:
1103 current = g_value_get_object (&padptr);
1105 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
1106 GST_DEBUG_PAD_NAME (current));
1108 if (GST_PAD_IS_SRC (current)) {
1115 peer = gst_pad_get_peer (current);
1117 if (peer == NULL && gst_pad_check_link (srcpad, sinkpad)) {
1118 GstCaps *temp, *intersection;
1119 gboolean compatible;
1121 /* Now check if the two pads' caps are compatible */
1122 temp = gst_pad_query_caps (pad, NULL);
1124 intersection = gst_caps_intersect (temp, caps);
1125 gst_caps_unref (temp);
1127 intersection = temp;
1130 temp = gst_pad_query_caps (current, NULL);
1131 compatible = gst_caps_can_intersect (temp, intersection);
1132 gst_caps_unref (temp);
1133 gst_caps_unref (intersection);
1136 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1137 "found existing unlinked compatible pad %s:%s",
1138 GST_DEBUG_PAD_NAME (current));
1139 gst_iterator_free (pads);
1141 current = gst_object_ref (current);
1142 g_value_unset (&padptr);
1146 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "incompatible pads");
1149 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1150 "already linked or cannot be linked (peer = %p)", peer);
1152 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unreffing pads");
1154 g_value_reset (&padptr);
1156 gst_object_unref (peer);
1159 case GST_ITERATOR_DONE:
1162 case GST_ITERATOR_RESYNC:
1163 gst_iterator_resync (pads);
1165 case GST_ITERATOR_ERROR:
1166 g_assert_not_reached ();
1170 g_value_unset (&padptr);
1171 gst_iterator_free (pads);
1173 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
1174 "Could not find a compatible unlinked always pad to link to %s:%s, now checking request pads",
1175 GST_DEBUG_PAD_NAME (pad));
1177 /* try to create a new one */
1178 /* requesting is a little crazy, we need a template. Let's create one */
1179 templcaps = gst_pad_query_caps (pad, NULL);
1181 GstCaps *inter = gst_caps_intersect (templcaps, caps);
1183 gst_caps_unref (templcaps);
1186 templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
1187 GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
1188 gst_caps_unref (templcaps);
1190 foundpad = gst_element_request_compatible_pad (element, templ);
1191 gst_object_unref (templ);
1194 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1195 "found existing request pad %s:%s", GST_DEBUG_PAD_NAME (foundpad));
1199 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element,
1200 "Could not find a compatible pad to link to %s:%s",
1201 GST_DEBUG_PAD_NAME (pad));
1206 * gst_element_state_get_name:
1207 * @state: a #GstState to get the name of.
1209 * Gets a string representing the given state.
1211 * Returns: (transfer none): a string with the name of the state.
1214 gst_element_state_get_name (GstState state)
1217 case GST_STATE_VOID_PENDING:
1218 return "VOID_PENDING";
1219 case GST_STATE_NULL:
1221 case GST_STATE_READY:
1223 case GST_STATE_PLAYING:
1225 case GST_STATE_PAUSED:
1228 /* This is a memory leak */
1229 return g_strdup_printf ("UNKNOWN!(%d)", state);
1234 * gst_element_state_change_return_get_name:
1235 * @state_ret: a #GstStateChangeReturn to get the name of.
1237 * Gets a string representing the given state change result.
1239 * Returns: (transfer none): a string with the name of the state
1243 gst_element_state_change_return_get_name (GstStateChangeReturn state_ret)
1245 switch (state_ret) {
1246 case GST_STATE_CHANGE_FAILURE:
1248 case GST_STATE_CHANGE_SUCCESS:
1250 case GST_STATE_CHANGE_ASYNC:
1252 case GST_STATE_CHANGE_NO_PREROLL:
1253 return "NO PREROLL";
1255 /* This is a memory leak */
1256 return g_strdup_printf ("UNKNOWN!(%d)", state_ret);
1262 gst_element_factory_can_accept_all_caps_in_direction (GstElementFactory *
1263 factory, const GstCaps * caps, GstPadDirection direction)
1267 g_return_val_if_fail (factory != NULL, FALSE);
1268 g_return_val_if_fail (caps != NULL, FALSE);
1270 templates = factory->staticpadtemplates;
1273 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1275 if (template->direction == direction) {
1276 GstCaps *templcaps = gst_static_caps_get (&template->static_caps);
1278 if (gst_caps_is_always_compatible (caps, templcaps)) {
1279 gst_caps_unref (templcaps);
1282 gst_caps_unref (templcaps);
1284 templates = g_list_next (templates);
1291 gst_element_factory_can_accept_any_caps_in_direction (GstElementFactory *
1292 factory, const GstCaps * caps, GstPadDirection direction)
1296 g_return_val_if_fail (factory != NULL, FALSE);
1297 g_return_val_if_fail (caps != NULL, FALSE);
1299 templates = factory->staticpadtemplates;
1302 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1304 if (template->direction == direction) {
1305 GstCaps *templcaps = gst_static_caps_get (&template->static_caps);
1307 if (gst_caps_can_intersect (caps, templcaps)) {
1308 gst_caps_unref (templcaps);
1311 gst_caps_unref (templcaps);
1313 templates = g_list_next (templates);
1320 * gst_element_factory_can_sink_all_caps:
1321 * @factory: factory to query
1322 * @caps: the caps to check
1324 * Checks if the factory can sink all possible capabilities.
1326 * Returns: %TRUE if the caps are fully compatible.
1329 gst_element_factory_can_sink_all_caps (GstElementFactory * factory,
1330 const GstCaps * caps)
1332 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1337 * gst_element_factory_can_src_all_caps:
1338 * @factory: factory to query
1339 * @caps: the caps to check
1341 * Checks if the factory can src all possible capabilities.
1343 * Returns: %TRUE if the caps are fully compatible.
1346 gst_element_factory_can_src_all_caps (GstElementFactory * factory,
1347 const GstCaps * caps)
1349 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1354 * gst_element_factory_can_sink_any_caps:
1355 * @factory: factory to query
1356 * @caps: the caps to check
1358 * Checks if the factory can sink any possible capability.
1360 * Returns: %TRUE if the caps have a common subset.
1363 gst_element_factory_can_sink_any_caps (GstElementFactory * factory,
1364 const GstCaps * caps)
1366 return gst_element_factory_can_accept_any_caps_in_direction (factory, caps,
1371 * gst_element_factory_can_src_any_caps:
1372 * @factory: factory to query
1373 * @caps: the caps to check
1375 * Checks if the factory can src any possible capability.
1377 * Returns: %TRUE if the caps have a common subset.
1380 gst_element_factory_can_src_any_caps (GstElementFactory * factory,
1381 const GstCaps * caps)
1383 return gst_element_factory_can_accept_any_caps_in_direction (factory, caps,
1387 /* if return val is true, *direct_child is a caller-owned ref on the direct
1388 * child of ancestor that is part of object's ancestry */
1390 object_has_ancestor (GstObject * object, GstObject * ancestor,
1391 GstObject ** direct_child)
1393 GstObject *child, *parent;
1396 *direct_child = NULL;
1398 child = gst_object_ref (object);
1399 parent = gst_object_get_parent (object);
1402 if (ancestor == parent) {
1404 *direct_child = child;
1406 gst_object_unref (child);
1407 gst_object_unref (parent);
1411 gst_object_unref (child);
1413 parent = gst_object_get_parent (parent);
1416 gst_object_unref (child);
1421 /* caller owns return */
1423 find_common_root (GstObject * o1, GstObject * o2)
1425 GstObject *top = o1;
1426 GstObject *kid1, *kid2;
1427 GstObject *root = NULL;
1429 while (GST_OBJECT_PARENT (top))
1430 top = GST_OBJECT_PARENT (top);
1432 /* the itsy-bitsy spider... */
1434 if (!object_has_ancestor (o2, top, &kid2))
1437 root = gst_object_ref (top);
1439 if (!object_has_ancestor (o1, kid2, &kid1)) {
1440 gst_object_unref (kid2);
1443 gst_object_unref (root);
1445 if (!object_has_ancestor (o2, kid1, &kid2)) {
1446 gst_object_unref (kid1);
1449 gst_object_unref (root);
1454 /* caller does not own return */
1456 ghost_up (GstElement * e, GstPad * pad)
1458 static gint ghost_pad_index = 0;
1463 GstObject *parent = GST_OBJECT_PARENT (e);
1465 name = g_strdup_printf ("ghost%d", ghost_pad_index++);
1466 gpad = gst_ghost_pad_new (name, pad);
1469 GST_STATE_LOCK (parent);
1470 gst_element_get_state (GST_ELEMENT (parent), ¤t, &next, 0);
1472 if (current > GST_STATE_READY || next >= GST_STATE_PAUSED)
1473 gst_pad_set_active (gpad, TRUE);
1475 if (!gst_element_add_pad ((GstElement *) parent, gpad)) {
1476 g_warning ("Pad named %s already exists in element %s\n",
1477 GST_OBJECT_NAME (gpad), GST_OBJECT_NAME (parent));
1478 GST_STATE_UNLOCK (parent);
1481 GST_STATE_UNLOCK (parent);
1487 remove_pad (gpointer ppad, gpointer unused)
1491 if (!gst_element_remove_pad ((GstElement *) GST_OBJECT_PARENT (pad), pad))
1492 g_warning ("Couldn't remove pad %s from element %s",
1493 GST_OBJECT_NAME (pad), GST_OBJECT_NAME (GST_OBJECT_PARENT (pad)));
1497 prepare_link_maybe_ghosting (GstPad ** src, GstPad ** sink,
1498 GSList ** pads_created)
1502 GSList *pads_created_local = NULL;
1504 g_assert (pads_created);
1506 e1 = GST_OBJECT_PARENT (*src);
1507 e2 = GST_OBJECT_PARENT (*sink);
1509 if (G_UNLIKELY (e1 == NULL)) {
1510 GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1511 GST_PTR_FORMAT, *src);
1514 if (G_UNLIKELY (e2 == NULL)) {
1515 GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1516 GST_PTR_FORMAT, *sink);
1520 if (GST_OBJECT_PARENT (e1) == GST_OBJECT_PARENT (e2)) {
1521 GST_CAT_INFO (GST_CAT_PADS, "%s and %s in same bin, no need for ghost pads",
1522 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1526 GST_CAT_INFO (GST_CAT_PADS, "%s and %s not in same bin, making ghost pads",
1527 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1529 /* we need to setup some ghost pads */
1530 root = find_common_root (e1, e2);
1532 if (GST_OBJECT_PARENT (e1) == NULL)
1533 g_warning ("Trying to link elements %s and %s that don't share a common "
1534 "ancestor: %s hasn't been added to a bin or pipeline, but %s is in %s",
1535 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
1536 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
1537 GST_ELEMENT_NAME (GST_OBJECT_PARENT (e2)));
1538 else if (GST_OBJECT_PARENT (e2) == NULL)
1539 g_warning ("Trying to link elements %s and %s that don't share a common "
1540 "ancestor: %s hasn't been added to a bin or pipeline, and %s is in %s",
1541 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
1542 GST_ELEMENT_NAME (e2), GST_ELEMENT_NAME (e1),
1543 GST_ELEMENT_NAME (GST_OBJECT_PARENT (e1)));
1545 g_warning ("Trying to link elements %s and %s that don't share a common "
1546 "ancestor: %s is in %s, and %s is in %s",
1547 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
1548 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (GST_OBJECT_PARENT (e1)),
1549 GST_ELEMENT_NAME (e2), GST_ELEMENT_NAME (GST_OBJECT_PARENT (e2)));
1553 while (GST_OBJECT_PARENT (e1) != root) {
1554 *src = ghost_up ((GstElement *) e1, *src);
1557 e1 = GST_OBJECT_PARENT (*src);
1558 pads_created_local = g_slist_prepend (pads_created_local, *src);
1560 while (GST_OBJECT_PARENT (e2) != root) {
1561 *sink = ghost_up ((GstElement *) e2, *sink);
1564 e2 = GST_OBJECT_PARENT (*sink);
1565 pads_created_local = g_slist_prepend (pads_created_local, *sink);
1568 gst_object_unref (root);
1569 *pads_created = g_slist_concat (*pads_created, pads_created_local);
1573 gst_object_unref (root);
1574 g_slist_foreach (pads_created_local, remove_pad, NULL);
1575 g_slist_free (pads_created_local);
1580 pad_link_maybe_ghosting (GstPad * src, GstPad * sink, GstPadLinkCheck flags)
1582 GSList *pads_created = NULL;
1585 if (!prepare_link_maybe_ghosting (&src, &sink, &pads_created)) {
1588 ret = (gst_pad_link_full (src, sink, flags) == GST_PAD_LINK_OK);
1592 g_slist_foreach (pads_created, remove_pad, NULL);
1594 g_slist_free (pads_created);
1600 * gst_pad_link_maybe_ghosting_full:
1603 * @flags: some #GstPadLinkCheck flags
1605 * Links @src to @sink, creating any #GstGhostPad's in between as necessary.
1607 * This is a convenience function to save having to create and add intermediate
1608 * #GstGhostPad's as required for linking across #GstBin boundaries.
1610 * If @src or @sink pads don't have parent elements or do not share a common
1611 * ancestor, the link will fail.
1613 * Calling gst_pad_link_maybe_ghosting_full() with
1614 * @flags == %GST_PAD_LINK_CHECK_DEFAULT is the recommended way of linking
1615 * pads with safety checks applied.
1617 * Returns: whether the link succeeded.
1622 gst_pad_link_maybe_ghosting_full (GstPad * src, GstPad * sink,
1623 GstPadLinkCheck flags)
1625 g_return_val_if_fail (GST_IS_PAD (src), FALSE);
1626 g_return_val_if_fail (GST_IS_PAD (sink), FALSE);
1628 return pad_link_maybe_ghosting (src, sink, flags);
1632 * gst_pad_link_maybe_ghosting:
1636 * Links @src to @sink, creating any #GstGhostPad's in between as necessary.
1638 * This is a convenience function to save having to create and add intermediate
1639 * #GstGhostPad's as required for linking across #GstBin boundaries.
1641 * If @src or @sink pads don't have parent elements or do not share a common
1642 * ancestor, the link will fail.
1644 * Returns: whether the link succeeded.
1649 gst_pad_link_maybe_ghosting (GstPad * src, GstPad * sink)
1651 g_return_val_if_fail (GST_IS_PAD (src), FALSE);
1652 g_return_val_if_fail (GST_IS_PAD (sink), FALSE);
1654 return gst_pad_link_maybe_ghosting_full (src, sink,
1655 GST_PAD_LINK_CHECK_DEFAULT);
1659 release_and_unref_pad (GstElement * element, GstPad * pad, gboolean requestpad)
1663 gst_element_release_request_pad (element, pad);
1664 gst_object_unref (pad);
1669 * gst_element_link_pads_full:
1670 * @src: a #GstElement containing the source pad.
1671 * @srcpadname: (allow-none): the name of the #GstPad in source element
1672 * or %NULL for any pad.
1673 * @dest: (transfer none): the #GstElement containing the destination pad.
1674 * @destpadname: (allow-none): the name of the #GstPad in destination element,
1675 * or %NULL for any pad.
1676 * @flags: the #GstPadLinkCheck to be performed when linking pads.
1678 * Links the two named pads of the source and destination elements.
1679 * Side effect is that if one of the pads has no parent, it becomes a
1680 * child of the parent of the other element. If they have different
1681 * parents, the link fails.
1683 * Calling gst_element_link_pads_full() with @flags == %GST_PAD_LINK_CHECK_DEFAULT
1684 * is the same as calling gst_element_link_pads() and the recommended way of
1685 * linking pads with safety checks applied.
1687 * This is a convenience function for gst_pad_link_full().
1689 * Returns: %TRUE if the pads could be linked, %FALSE otherwise.
1692 gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
1693 GstElement * dest, const gchar * destpadname, GstPadLinkCheck flags)
1695 const GList *srcpads, *destpads, *srctempls, *desttempls, *l;
1696 GstPad *srcpad, *destpad;
1697 GstPadTemplate *srctempl, *desttempl;
1698 GstElementClass *srcclass, *destclass;
1699 gboolean srcrequest, destrequest;
1702 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1703 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1705 GST_CAT_INFO (GST_CAT_ELEMENT_PADS,
1706 "trying to link element %s:%s to element %s:%s", GST_ELEMENT_NAME (src),
1707 srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
1708 destpadname ? destpadname : "(any)");
1711 destrequest = FALSE;
1715 /* name specified, look it up */
1716 if (!(srcpad = gst_element_get_static_pad (src, srcpadname))) {
1717 if ((srcpad = gst_element_get_request_pad (src, srcpadname)))
1721 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1722 GST_ELEMENT_NAME (src), srcpadname);
1725 if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
1726 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
1727 GST_DEBUG_PAD_NAME (srcpad));
1728 release_and_unref_pad (src, srcpad, srcrequest);
1731 if (GST_PAD_PEER (srcpad) != NULL) {
1732 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1733 "pad %s:%s is already linked to %s:%s", GST_DEBUG_PAD_NAME (srcpad),
1734 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1735 /* already linked request pads look like static pads, so the request pad
1736 * was never requested a second time above, so no need to release it */
1737 gst_object_unref (srcpad);
1743 /* no name given, get the first available pad */
1744 GST_OBJECT_LOCK (src);
1745 srcpads = GST_ELEMENT_PADS (src);
1746 srcpad = srcpads ? GST_PAD_CAST (srcpads->data) : NULL;
1748 gst_object_ref (srcpad);
1749 GST_OBJECT_UNLOCK (src);
1752 /* get a destination pad */
1754 /* name specified, look it up */
1755 if (!(destpad = gst_element_get_static_pad (dest, destpadname))) {
1756 if ((destpad = gst_element_get_request_pad (dest, destpadname)))
1760 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1761 GST_ELEMENT_NAME (dest), destpadname);
1762 release_and_unref_pad (src, srcpad, srcrequest);
1765 if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
1766 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
1767 GST_DEBUG_PAD_NAME (destpad));
1768 release_and_unref_pad (src, srcpad, srcrequest);
1769 release_and_unref_pad (dest, destpad, destrequest);
1772 if (GST_PAD_PEER (destpad) != NULL) {
1773 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1774 "pad %s:%s is already linked to %s:%s",
1775 GST_DEBUG_PAD_NAME (destpad),
1776 GST_DEBUG_PAD_NAME (GST_PAD_PEER (destpad)));
1777 release_and_unref_pad (src, srcpad, srcrequest);
1778 /* already linked request pads look like static pads, so the request pad
1779 * was never requested a second time above, so no need to release it */
1780 gst_object_unref (destpad);
1786 /* no name given, get the first available pad */
1787 GST_OBJECT_LOCK (dest);
1788 destpads = GST_ELEMENT_PADS (dest);
1789 destpad = destpads ? GST_PAD_CAST (destpads->data) : NULL;
1791 gst_object_ref (destpad);
1792 GST_OBJECT_UNLOCK (dest);
1795 if (srcpadname && destpadname) {
1798 /* two explicitly specified pads */
1799 result = pad_link_maybe_ghosting (srcpad, destpad, flags);
1802 gst_object_unref (srcpad);
1803 gst_object_unref (destpad);
1805 release_and_unref_pad (src, srcpad, srcrequest);
1806 release_and_unref_pad (dest, destpad, destrequest);
1812 /* loop through the allowed pads in the source, trying to find a
1813 * compatible destination pad */
1814 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1815 "looping through allowed src and dest pads");
1817 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying src pad %s:%s",
1818 GST_DEBUG_PAD_NAME (srcpad));
1819 if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
1820 (GST_PAD_PEER (srcpad) == NULL)) {
1821 gboolean temprequest = FALSE;
1826 gst_object_ref (temp);
1828 temp = gst_element_get_compatible_pad (dest, srcpad, NULL);
1829 if (temp && GST_PAD_PAD_TEMPLATE (temp)
1830 && GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (temp)) ==
1836 if (temp && pad_link_maybe_ghosting (srcpad, temp, flags)) {
1837 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1838 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (temp));
1840 gst_object_unref (destpad);
1841 gst_object_unref (srcpad);
1842 gst_object_unref (temp);
1848 gst_element_release_request_pad (dest, temp);
1849 gst_object_unref (temp);
1852 /* find a better way for this mess */
1854 srcpads = g_list_next (srcpads);
1856 gst_object_unref (srcpad);
1857 srcpad = GST_PAD_CAST (srcpads->data);
1858 gst_object_ref (srcpad);
1864 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
1865 GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
1866 /* no need to release any request pad as both src- and destpadname must be
1867 * set to end up here, but this case has already been taken care of above */
1869 gst_object_unref (destpad);
1873 release_and_unref_pad (src, srcpad, srcrequest);
1878 /* loop through the existing pads in the destination */
1880 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying dest pad %s:%s",
1881 GST_DEBUG_PAD_NAME (destpad));
1882 if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
1883 (GST_PAD_PEER (destpad) == NULL)) {
1884 GstPad *temp = gst_element_get_compatible_pad (src, destpad, NULL);
1885 gboolean temprequest = FALSE;
1887 if (temp && GST_PAD_PAD_TEMPLATE (temp)
1888 && GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (temp)) ==
1893 if (temp && pad_link_maybe_ghosting (temp, destpad, flags)) {
1894 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1895 GST_DEBUG_PAD_NAME (temp), GST_DEBUG_PAD_NAME (destpad));
1896 gst_object_unref (temp);
1897 gst_object_unref (destpad);
1901 release_and_unref_pad (src, temp, temprequest);
1904 destpads = g_list_next (destpads);
1906 gst_object_unref (destpad);
1907 destpad = GST_PAD_CAST (destpads->data);
1908 gst_object_ref (destpad);
1915 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
1916 GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
1917 release_and_unref_pad (dest, destpad, destrequest);
1920 /* no need to release any request pad as the case of unset destpatname and
1921 * destpad being a requst pad has already been taken care of when looking
1922 * though the destination pads above */
1924 gst_object_unref (destpad);
1929 srcclass = GST_ELEMENT_GET_CLASS (src);
1930 destclass = GST_ELEMENT_GET_CLASS (dest);
1932 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1933 "we might have request pads on both sides, checking...");
1934 srctempls = gst_element_class_get_pad_template_list (srcclass);
1935 desttempls = gst_element_class_get_pad_template_list (destclass);
1937 if (srctempls && desttempls) {
1939 srctempl = (GstPadTemplate *) srctempls->data;
1940 if (srctempl->presence == GST_PAD_REQUEST) {
1941 for (l = desttempls; l; l = l->next) {
1942 desttempl = (GstPadTemplate *) l->data;
1943 if (desttempl->presence == GST_PAD_REQUEST &&
1944 desttempl->direction != srctempl->direction) {
1945 GstCaps *srccaps, *destcaps;
1947 srccaps = gst_pad_template_get_caps (srctempl);
1948 destcaps = gst_pad_template_get_caps (desttempl);
1949 if (gst_caps_is_always_compatible (srccaps, destcaps)) {
1951 gst_element_request_pad (src, srctempl,
1952 srctempl->name_template, NULL);
1954 gst_element_request_pad (dest, desttempl,
1955 desttempl->name_template, NULL);
1956 if (srcpad && destpad
1957 && pad_link_maybe_ghosting (srcpad, destpad, flags)) {
1958 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1959 "linked pad %s:%s to pad %s:%s",
1960 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
1961 gst_object_unref (srcpad);
1962 gst_object_unref (destpad);
1963 gst_caps_unref (srccaps);
1964 gst_caps_unref (destcaps);
1967 /* it failed, so we release the request pads */
1969 gst_element_release_request_pad (src, srcpad);
1970 gst_object_unref (srcpad);
1973 gst_element_release_request_pad (dest, destpad);
1974 gst_object_unref (destpad);
1977 gst_caps_unref (srccaps);
1978 gst_caps_unref (destcaps);
1982 srctempls = srctempls->next;
1986 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s",
1987 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1992 * gst_element_link_pads:
1993 * @src: a #GstElement containing the source pad.
1994 * @srcpadname: (allow-none): the name of the #GstPad in source element
1995 * or %NULL for any pad.
1996 * @dest: (transfer none): the #GstElement containing the destination pad.
1997 * @destpadname: (allow-none): the name of the #GstPad in destination element,
1998 * or %NULL for any pad.
2000 * Links the two named pads of the source and destination elements.
2001 * Side effect is that if one of the pads has no parent, it becomes a
2002 * child of the parent of the other element. If they have different
2003 * parents, the link fails.
2005 * Returns: %TRUE if the pads could be linked, %FALSE otherwise.
2008 gst_element_link_pads (GstElement * src, const gchar * srcpadname,
2009 GstElement * dest, const gchar * destpadname)
2011 return gst_element_link_pads_full (src, srcpadname, dest, destpadname,
2012 GST_PAD_LINK_CHECK_DEFAULT);
2016 * gst_element_link_pads_filtered:
2017 * @src: a #GstElement containing the source pad.
2018 * @srcpadname: (allow-none): the name of the #GstPad in source element
2019 * or %NULL for any pad.
2020 * @dest: (transfer none): the #GstElement containing the destination pad.
2021 * @destpadname: (allow-none): the name of the #GstPad in destination element
2022 * or %NULL for any pad.
2023 * @filter: (transfer none) (allow-none): the #GstCaps to filter the link,
2024 * or %NULL for no filter.
2026 * Links the two named pads of the source and destination elements. Side effect
2027 * is that if one of the pads has no parent, it becomes a child of the parent of
2028 * the other element. If they have different parents, the link fails. If @caps
2029 * is not %NULL, makes sure that the caps of the link is a subset of @caps.
2031 * Returns: %TRUE if the pads could be linked, %FALSE otherwise.
2034 gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
2035 GstElement * dest, const gchar * destpadname, GstCaps * filter)
2038 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
2039 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
2040 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), FALSE);
2043 GstElement *capsfilter;
2045 GstState state, pending;
2048 capsfilter = gst_element_factory_make ("capsfilter", NULL);
2050 GST_ERROR ("Could not make a capsfilter");
2054 parent = gst_object_get_parent (GST_OBJECT (src));
2055 g_return_val_if_fail (GST_IS_BIN (parent), FALSE);
2057 gst_element_get_state (GST_ELEMENT_CAST (parent), &state, &pending, 0);
2059 if (!gst_bin_add (GST_BIN (parent), capsfilter)) {
2060 GST_ERROR ("Could not add capsfilter");
2061 gst_object_unref (parent);
2065 if (pending != GST_STATE_VOID_PENDING)
2068 gst_element_set_state (capsfilter, state);
2070 gst_object_unref (parent);
2072 g_object_set (capsfilter, "caps", filter, NULL);
2074 lr1 = gst_element_link_pads (src, srcpadname, capsfilter, "sink");
2075 lr2 = gst_element_link_pads (capsfilter, "src", dest, destpadname);
2080 GST_INFO ("Could not link pads: %s:%s - capsfilter:sink",
2081 GST_ELEMENT_NAME (src), srcpadname);
2083 GST_INFO ("Could not link pads: capsfilter:src - %s:%s",
2084 GST_ELEMENT_NAME (dest), destpadname);
2086 gst_element_set_state (capsfilter, GST_STATE_NULL);
2087 /* this will unlink and unref as appropriate */
2088 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (capsfilter)), capsfilter);
2092 if (gst_element_link_pads (src, srcpadname, dest, destpadname)) {
2095 GST_INFO ("Could not link pads: %s:%s - %s:%s", GST_ELEMENT_NAME (src),
2096 srcpadname, GST_ELEMENT_NAME (dest), destpadname);
2104 * @src: (transfer none): a #GstElement containing the source pad.
2105 * @dest: (transfer none): the #GstElement containing the destination pad.
2107 * Links @src to @dest. The link must be from source to
2108 * destination; the other direction will not be tried. The function looks for
2109 * existing pads that aren't linked yet. It will request new pads if necessary.
2110 * Such pads need to be released manually when unlinking.
2111 * If multiple links are possible, only one is established.
2113 * Make sure you have added your elements to a bin or pipeline with
2114 * gst_bin_add() before trying to link them.
2116 * Returns: %TRUE if the elements could be linked, %FALSE otherwise.
2119 gst_element_link (GstElement * src, GstElement * dest)
2121 return gst_element_link_pads (src, NULL, dest, NULL);
2125 * gst_element_link_many:
2126 * @element_1: (transfer none): the first #GstElement in the link chain.
2127 * @element_2: (transfer none): the second #GstElement in the link chain.
2128 * @...: the %NULL-terminated list of elements to link in order.
2130 * Chain together a series of elements. Uses gst_element_link().
2131 * Make sure you have added your elements to a bin or pipeline with
2132 * gst_bin_add() before trying to link them.
2134 * Returns: %TRUE on success, %FALSE otherwise.
2137 gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
2139 gboolean res = TRUE;
2142 g_return_val_if_fail (GST_IS_ELEMENT (element_1), FALSE);
2143 g_return_val_if_fail (GST_IS_ELEMENT (element_2), FALSE);
2145 va_start (args, element_2);
2148 if (!gst_element_link (element_1, element_2)) {
2153 element_1 = element_2;
2154 element_2 = va_arg (args, GstElement *);
2163 * gst_element_link_filtered:
2164 * @src: a #GstElement containing the source pad.
2165 * @dest: (transfer none): the #GstElement containing the destination pad.
2166 * @filter: (transfer none) (allow-none): the #GstCaps to filter the link,
2167 * or %NULL for no filter.
2169 * Links @src to @dest using the given caps as filtercaps.
2170 * The link must be from source to
2171 * destination; the other direction will not be tried. The function looks for
2172 * existing pads that aren't linked yet. It will request new pads if necessary.
2173 * If multiple links are possible, only one is established.
2175 * Make sure you have added your elements to a bin or pipeline with
2176 * gst_bin_add() before trying to link them.
2178 * Returns: %TRUE if the pads could be linked, %FALSE otherwise.
2181 gst_element_link_filtered (GstElement * src, GstElement * dest,
2184 return gst_element_link_pads_filtered (src, NULL, dest, NULL, filter);
2188 * gst_element_unlink_pads:
2189 * @src: a (transfer none): #GstElement containing the source pad.
2190 * @srcpadname: the name of the #GstPad in source element.
2191 * @dest: (transfer none): a #GstElement containing the destination pad.
2192 * @destpadname: the name of the #GstPad in destination element.
2194 * Unlinks the two named pads of the source and destination elements.
2196 * This is a convenience function for gst_pad_unlink().
2199 gst_element_unlink_pads (GstElement * src, const gchar * srcpadname,
2200 GstElement * dest, const gchar * destpadname)
2202 GstPad *srcpad, *destpad;
2203 gboolean srcrequest, destrequest;
2205 srcrequest = destrequest = FALSE;
2207 g_return_if_fail (src != NULL);
2208 g_return_if_fail (GST_IS_ELEMENT (src));
2209 g_return_if_fail (srcpadname != NULL);
2210 g_return_if_fail (dest != NULL);
2211 g_return_if_fail (GST_IS_ELEMENT (dest));
2212 g_return_if_fail (destpadname != NULL);
2214 /* obtain the pads requested */
2215 if (!(srcpad = gst_element_get_static_pad (src, srcpadname)))
2216 if ((srcpad = gst_element_get_request_pad (src, srcpadname)))
2218 if (srcpad == NULL) {
2219 GST_WARNING_OBJECT (src, "source element has no pad \"%s\"", srcpadname);
2222 if (!(destpad = gst_element_get_static_pad (dest, destpadname)))
2223 if ((destpad = gst_element_get_request_pad (dest, destpadname)))
2225 if (destpad == NULL) {
2226 GST_WARNING_OBJECT (dest, "destination element has no pad \"%s\"",
2231 /* we're satisfied they can be unlinked, let's do it */
2232 gst_pad_unlink (srcpad, destpad);
2235 gst_element_release_request_pad (dest, destpad);
2236 gst_object_unref (destpad);
2240 gst_element_release_request_pad (src, srcpad);
2241 gst_object_unref (srcpad);
2245 * gst_element_unlink_many:
2246 * @element_1: (transfer none): the first #GstElement in the link chain.
2247 * @element_2: (transfer none): the second #GstElement in the link chain.
2248 * @...: the %NULL-terminated list of elements to unlink in order.
2250 * Unlinks a series of elements. Uses gst_element_unlink().
2253 gst_element_unlink_many (GstElement * element_1, GstElement * element_2, ...)
2257 g_return_if_fail (element_1 != NULL && element_2 != NULL);
2258 g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
2260 va_start (args, element_2);
2263 gst_element_unlink (element_1, element_2);
2265 element_1 = element_2;
2266 element_2 = va_arg (args, GstElement *);
2273 * gst_element_unlink:
2274 * @src: (transfer none): the source #GstElement to unlink.
2275 * @dest: (transfer none): the sink #GstElement to unlink.
2277 * Unlinks all source pads of the source element with all sink pads
2278 * of the sink element to which they are linked.
2280 * If the link has been made using gst_element_link(), it could have created an
2281 * requestpad, which has to be released using gst_element_release_request_pad().
2284 gst_element_unlink (GstElement * src, GstElement * dest)
2287 gboolean done = FALSE;
2288 GValue data = { 0, };
2290 g_return_if_fail (GST_IS_ELEMENT (src));
2291 g_return_if_fail (GST_IS_ELEMENT (dest));
2293 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unlinking \"%s\" and \"%s\"",
2294 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
2296 pads = gst_element_iterate_pads (src);
2298 switch (gst_iterator_next (pads, &data)) {
2299 case GST_ITERATOR_OK:
2301 GstPad *pad = g_value_get_object (&data);
2303 if (GST_PAD_IS_SRC (pad)) {
2304 GstPad *peerpad = gst_pad_get_peer (pad);
2306 /* see if the pad is linked and is really a pad of dest */
2308 GstElement *peerelem;
2310 peerelem = gst_pad_get_parent_element (peerpad);
2312 if (peerelem == dest) {
2313 gst_pad_unlink (pad, peerpad);
2316 gst_object_unref (peerelem);
2318 gst_object_unref (peerpad);
2321 g_value_reset (&data);
2324 case GST_ITERATOR_RESYNC:
2325 gst_iterator_resync (pads);
2327 case GST_ITERATOR_DONE:
2331 g_assert_not_reached ();
2335 g_value_unset (&data);
2336 gst_iterator_free (pads);
2340 * gst_element_query_position:
2341 * @element: a #GstElement to invoke the position query on.
2342 * @format: the #GstFormat requested
2343 * @cur: (out) (allow-none): a location in which to store the current
2344 * position, or %NULL.
2346 * Queries an element (usually top-level pipeline or playbin element) for the
2347 * stream position in nanoseconds. This will be a value between 0 and the
2348 * stream duration (if the stream duration is known). This query will usually
2349 * only work once the pipeline is prerolled (i.e. reached PAUSED or PLAYING
2350 * state). The application will receive an ASYNC_DONE message on the pipeline
2351 * bus when that is the case.
2353 * If one repeatedly calls this function one can also create a query and reuse
2354 * it in gst_element_query().
2356 * Returns: %TRUE if the query could be performed.
2359 gst_element_query_position (GstElement * element, GstFormat format,
2365 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2366 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2368 query = gst_query_new_position (format);
2369 ret = gst_element_query (element, query);
2372 gst_query_parse_position (query, NULL, cur);
2374 gst_query_unref (query);
2380 * gst_element_query_duration:
2381 * @element: a #GstElement to invoke the duration query on.
2382 * @format: the #GstFormat requested
2383 * @duration: (out) (allow-none): A location in which to store the total duration, or %NULL.
2385 * Queries an element (usually top-level pipeline or playbin element) for the
2386 * total stream duration in nanoseconds. This query will only work once the
2387 * pipeline is prerolled (i.e. reached PAUSED or PLAYING state). The application
2388 * will receive an ASYNC_DONE message on the pipeline bus when that is the case.
2390 * If the duration changes for some reason, you will get a DURATION_CHANGED
2391 * message on the pipeline bus, in which case you should re-query the duration
2392 * using this function.
2394 * Returns: %TRUE if the query could be performed.
2397 gst_element_query_duration (GstElement * element, GstFormat format,
2403 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2404 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2406 query = gst_query_new_duration (format);
2407 ret = gst_element_query (element, query);
2410 gst_query_parse_duration (query, NULL, duration);
2412 gst_query_unref (query);
2418 * gst_element_query_convert:
2419 * @element: a #GstElement to invoke the convert query on.
2420 * @src_format: a #GstFormat to convert from.
2421 * @src_val: a value to convert.
2422 * @dest_format: the #GstFormat to convert to.
2423 * @dest_val: (out): a pointer to the result.
2425 * Queries an element to convert @src_val in @src_format to @dest_format.
2427 * Returns: %TRUE if the query could be performed.
2430 gst_element_query_convert (GstElement * element, GstFormat src_format,
2431 gint64 src_val, GstFormat dest_format, gint64 * dest_val)
2436 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2437 g_return_val_if_fail (dest_format != GST_FORMAT_UNDEFINED, FALSE);
2438 g_return_val_if_fail (dest_val != NULL, FALSE);
2440 if (dest_format == src_format || src_val == -1) {
2441 *dest_val = src_val;
2445 query = gst_query_new_convert (src_format, src_val, dest_format);
2446 ret = gst_element_query (element, query);
2449 gst_query_parse_convert (query, NULL, NULL, NULL, dest_val);
2451 gst_query_unref (query);
2457 * gst_element_seek_simple:
2458 * @element: a #GstElement to seek on
2459 * @format: a #GstFormat to execute the seek in, such as #GST_FORMAT_TIME
2460 * @seek_flags: seek options; playback applications will usually want to use
2461 * GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT here
2462 * @seek_pos: position to seek to (relative to the start); if you are doing
2463 * a seek in #GST_FORMAT_TIME this value is in nanoseconds -
2464 * multiply with #GST_SECOND to convert seconds to nanoseconds or
2465 * with #GST_MSECOND to convert milliseconds to nanoseconds.
2467 * Simple API to perform a seek on the given element, meaning it just seeks
2468 * to the given position relative to the start of the stream. For more complex
2469 * operations like segment seeks (e.g. for looping) or changing the playback
2470 * rate or seeking relative to the last configured playback segment you should
2471 * use gst_element_seek().
2473 * In a completely prerolled PAUSED or PLAYING pipeline, seeking is always
2474 * guaranteed to return %TRUE on a seekable media type or %FALSE when the media
2475 * type is certainly not seekable (such as a live stream).
2477 * Some elements allow for seeking in the READY state, in this
2478 * case they will store the seek event and execute it when they are put to
2479 * PAUSED. If the element supports seek in READY, it will always return %TRUE when
2480 * it receives the event in the READY state.
2482 * Returns: %TRUE if the seek operation succeeded. Flushing seeks will trigger a
2483 * preroll, which will emit %GST_MESSAGE_ASYNC_DONE.
2486 gst_element_seek_simple (GstElement * element, GstFormat format,
2487 GstSeekFlags seek_flags, gint64 seek_pos)
2489 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2490 g_return_val_if_fail (seek_pos >= 0, FALSE);
2492 return gst_element_seek (element, 1.0, format, seek_flags,
2493 GST_SEEK_TYPE_SET, seek_pos, GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
2497 * gst_pad_use_fixed_caps:
2498 * @pad: the pad to use
2500 * A helper function you can use that sets the FIXED_CAPS flag
2501 * This way the default CAPS query will always return the negotiated caps
2502 * or in case the pad is not negotiated, the padtemplate caps.
2504 * The negotiated caps are the caps of the last CAPS event that passed on the
2505 * pad. Use this function on a pad that, once it negotiated to a CAPS, cannot
2506 * be renegotiated to something else.
2509 gst_pad_use_fixed_caps (GstPad * pad)
2511 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_FIXED_CAPS);
2515 * gst_pad_get_parent_element:
2518 * Gets the parent of @pad, cast to a #GstElement. If a @pad has no parent or
2519 * its parent is not an element, return %NULL.
2521 * Returns: (transfer full) (nullable): the parent of the pad. The
2522 * caller has a reference on the parent, so unref when you're finished
2528 gst_pad_get_parent_element (GstPad * pad)
2532 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2534 p = gst_object_get_parent (GST_OBJECT_CAST (pad));
2536 if (p && !GST_IS_ELEMENT (p)) {
2537 gst_object_unref (p);
2540 return GST_ELEMENT_CAST (p);
2544 * gst_object_default_error:
2545 * @source: the #GstObject that initiated the error.
2546 * @error: (in): the GError.
2547 * @debug: (in) (allow-none): an additional debug information string, or %NULL
2549 * A default error function that uses g_printerr() to display the error message
2550 * and the optional debug sting..
2552 * The default handler will simply print the error string using g_print.
2555 gst_object_default_error (GstObject * source, const GError * error,
2556 const gchar * debug)
2558 gchar *name = gst_object_get_path_string (source);
2560 g_printerr (_("ERROR: from element %s: %s\n"), name, error->message);
2562 g_printerr (_("Additional debug info:\n%s\n"), debug);
2570 * @element_1: (transfer floating): the #GstElement element to add to the bin
2571 * @...: (transfer floating): additional elements to add to the bin
2573 * Adds a %NULL-terminated list of elements to a bin. This function is
2574 * equivalent to calling gst_bin_add() for each member of the list. The return
2575 * value of each gst_bin_add() is ignored.
2578 gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
2582 g_return_if_fail (GST_IS_BIN (bin));
2583 g_return_if_fail (GST_IS_ELEMENT (element_1));
2585 va_start (args, element_1);
2588 gst_bin_add (bin, element_1);
2590 element_1 = va_arg (args, GstElement *);
2597 * gst_bin_remove_many:
2599 * @element_1: (transfer none): the first #GstElement to remove from the bin
2600 * @...: (transfer none): %NULL-terminated list of elements to remove from the bin
2602 * Remove a list of elements from a bin. This function is equivalent
2603 * to calling gst_bin_remove() with each member of the list.
2606 gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
2610 g_return_if_fail (GST_IS_BIN (bin));
2611 g_return_if_fail (GST_IS_ELEMENT (element_1));
2613 va_start (args, element_1);
2616 gst_bin_remove (bin, element_1);
2618 element_1 = va_arg (args, GstElement *);
2628 } QueryAcceptCapsData;
2631 query_accept_caps_func (GstPad * pad, QueryAcceptCapsData * data)
2633 if (G_LIKELY (gst_pad_peer_query (pad, data->query))) {
2636 gst_query_parse_accept_caps_result (data->query, &result);
2637 data->ret &= result;
2643 * gst_pad_proxy_query_accept_caps:
2644 * @pad: a #GstPad to proxy.
2645 * @query: an ACCEPT_CAPS #GstQuery.
2647 * Checks if all internally linked pads of @pad accepts the caps in @query and
2648 * returns the intersection of the results.
2650 * This function is useful as a default accept caps query function for an element
2651 * that can handle any stream format, but requires caps that are acceptable for
2652 * all opposite pads.
2654 * Returns: %TRUE if @query could be executed
2657 gst_pad_proxy_query_accept_caps (GstPad * pad, GstQuery * query)
2659 QueryAcceptCapsData data;
2661 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2662 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2663 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS, FALSE);
2665 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2666 "proxying accept caps query for %s:%s", GST_DEBUG_PAD_NAME (pad));
2669 /* value to hold the return, by default it holds TRUE */
2670 /* FIXME: TRUE is wrong when there are no pads */
2673 gst_pad_forward (pad, (GstPadForwardFunction) query_accept_caps_func, &data);
2674 gst_query_set_accept_caps_result (query, data.ret);
2676 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "proxying accept caps query: %d",
2689 query_caps_func (GstPad * pad, QueryCapsData * data)
2691 gboolean empty = FALSE;
2693 if (G_LIKELY (gst_pad_peer_query (pad, data->query))) {
2694 GstCaps *peercaps, *intersection;
2696 gst_query_parse_caps_result (data->query, &peercaps);
2697 GST_DEBUG_OBJECT (pad, "intersect with result %" GST_PTR_FORMAT, peercaps);
2698 intersection = gst_caps_intersect (data->ret, peercaps);
2699 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, intersection);
2701 gst_caps_unref (data->ret);
2702 data->ret = intersection;
2704 /* stop when empty */
2705 empty = gst_caps_is_empty (intersection);
2711 * gst_pad_proxy_query_caps:
2712 * @pad: a #GstPad to proxy.
2713 * @query: a CAPS #GstQuery.
2715 * Calls gst_pad_query_caps() for all internally linked pads of @pad and returns
2716 * the intersection of the results.
2718 * This function is useful as a default caps query function for an element
2719 * that can handle any stream format, but requires all its pads to have
2720 * the same caps. Two such elements are tee and adder.
2722 * Returns: %TRUE if @query could be executed
2725 gst_pad_proxy_query_caps (GstPad * pad, GstQuery * query)
2727 GstCaps *filter, *templ, *result;
2730 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2731 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2732 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS, FALSE);
2734 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "proxying caps query for %s:%s",
2735 GST_DEBUG_PAD_NAME (pad));
2739 /* value to hold the return, by default it holds the filter or ANY */
2740 gst_query_parse_caps (query, &filter);
2741 data.ret = filter ? gst_caps_ref (filter) : gst_caps_new_any ();
2743 gst_pad_forward (pad, (GstPadForwardFunction) query_caps_func, &data);
2745 templ = gst_pad_get_pad_template_caps (pad);
2746 result = gst_caps_intersect (data.ret, templ);
2747 gst_caps_unref (data.ret);
2748 gst_caps_unref (templ);
2750 gst_query_set_caps_result (query, result);
2751 gst_caps_unref (result);
2753 /* FIXME: return something depending on the processing */
2758 * gst_pad_query_position:
2759 * @pad: a #GstPad to invoke the position query on.
2760 * @format: the #GstFormat requested
2761 * @cur: (out) (allow-none): A location in which to store the current position, or %NULL.
2763 * Queries a pad for the stream position.
2765 * Returns: %TRUE if the query could be performed.
2768 gst_pad_query_position (GstPad * pad, GstFormat format, gint64 * cur)
2773 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2774 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2776 query = gst_query_new_position (format);
2777 if ((ret = gst_pad_query (pad, query)))
2778 gst_query_parse_position (query, NULL, cur);
2779 gst_query_unref (query);
2785 * gst_pad_peer_query_position:
2786 * @pad: a #GstPad on whose peer to invoke the position query on.
2787 * Must be a sink pad.
2788 * @format: the #GstFormat requested
2789 * @cur: (out) (allow-none): a location in which to store the current
2790 * position, or %NULL.
2792 * Queries the peer of a given sink pad for the stream position.
2794 * Returns: %TRUE if the query could be performed.
2797 gst_pad_peer_query_position (GstPad * pad, GstFormat format, gint64 * cur)
2800 gboolean ret = FALSE;
2802 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2803 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2805 query = gst_query_new_position (format);
2806 if ((ret = gst_pad_peer_query (pad, query)))
2807 gst_query_parse_position (query, NULL, cur);
2808 gst_query_unref (query);
2814 * gst_pad_query_duration:
2815 * @pad: a #GstPad to invoke the duration query on.
2816 * @format: the #GstFormat requested
2817 * @duration: (out) (allow-none): a location in which to store the total
2818 * duration, or %NULL.
2820 * Queries a pad for the total stream duration.
2822 * Returns: %TRUE if the query could be performed.
2825 gst_pad_query_duration (GstPad * pad, GstFormat format, gint64 * duration)
2830 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2831 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2833 query = gst_query_new_duration (format);
2834 if ((ret = gst_pad_query (pad, query)))
2835 gst_query_parse_duration (query, NULL, duration);
2836 gst_query_unref (query);
2842 * gst_pad_peer_query_duration:
2843 * @pad: a #GstPad on whose peer pad to invoke the duration query on.
2844 * Must be a sink pad.
2845 * @format: the #GstFormat requested
2846 * @duration: (out) (allow-none): a location in which to store the total
2847 * duration, or %NULL.
2849 * Queries the peer pad of a given sink pad for the total stream duration.
2851 * Returns: %TRUE if the query could be performed.
2854 gst_pad_peer_query_duration (GstPad * pad, GstFormat format, gint64 * duration)
2857 gboolean ret = FALSE;
2859 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2860 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2861 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2863 query = gst_query_new_duration (format);
2864 if ((ret = gst_pad_peer_query (pad, query)))
2865 gst_query_parse_duration (query, NULL, duration);
2866 gst_query_unref (query);
2872 * gst_pad_query_convert:
2873 * @pad: a #GstPad to invoke the convert query on.
2874 * @src_format: a #GstFormat to convert from.
2875 * @src_val: a value to convert.
2876 * @dest_format: the #GstFormat to convert to.
2877 * @dest_val: (out): a pointer to the result.
2879 * Queries a pad to convert @src_val in @src_format to @dest_format.
2881 * Returns: %TRUE if the query could be performed.
2884 gst_pad_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2885 GstFormat dest_format, gint64 * dest_val)
2890 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2891 g_return_val_if_fail (dest_format != GST_FORMAT_UNDEFINED, FALSE);
2892 g_return_val_if_fail (dest_val != NULL, FALSE);
2894 if (dest_format == src_format || src_val == -1) {
2895 *dest_val = src_val;
2899 query = gst_query_new_convert (src_format, src_val, dest_format);
2900 if ((ret = gst_pad_query (pad, query)))
2901 gst_query_parse_convert (query, NULL, NULL, NULL, dest_val);
2902 gst_query_unref (query);
2908 * gst_pad_peer_query_convert:
2909 * @pad: a #GstPad, on whose peer pad to invoke the convert query on.
2910 * Must be a sink pad.
2911 * @src_format: a #GstFormat to convert from.
2912 * @src_val: a value to convert.
2913 * @dest_format: the #GstFormat to convert to.
2914 * @dest_val: (out): a pointer to the result.
2916 * Queries the peer pad of a given sink pad to convert @src_val in @src_format
2919 * Returns: %TRUE if the query could be performed.
2922 gst_pad_peer_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2923 GstFormat dest_format, gint64 * dest_val)
2926 gboolean ret = FALSE;
2928 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2929 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2930 g_return_val_if_fail (dest_format != GST_FORMAT_UNDEFINED, FALSE);
2931 g_return_val_if_fail (dest_val != NULL, FALSE);
2933 query = gst_query_new_convert (src_format, src_val, dest_format);
2934 if ((ret = gst_pad_peer_query (pad, query)))
2935 gst_query_parse_convert (query, NULL, NULL, NULL, dest_val);
2936 gst_query_unref (query);
2942 * gst_pad_query_caps:
2943 * @pad: a #GstPad to get the capabilities of.
2944 * @filter: (allow-none): suggested #GstCaps, or %NULL
2946 * Gets the capabilities this pad can produce or consume.
2947 * Note that this method doesn't necessarily return the caps set by sending a
2948 * gst_event_new_caps() - use gst_pad_get_current_caps() for that instead.
2949 * gst_pad_query_caps returns all possible caps a pad can operate with, using
2950 * the pad's CAPS query function, If the query fails, this function will return
2951 * @filter, if not %NULL, otherwise ANY.
2953 * When called on sinkpads @filter contains the caps that
2954 * upstream could produce in the order preferred by upstream. When
2955 * called on srcpads @filter contains the caps accepted by
2956 * downstream in the preferred order. @filter might be %NULL but
2957 * if it is not %NULL the returned caps will be a subset of @filter.
2959 * Note that this function does not return writable #GstCaps, use
2960 * gst_caps_make_writable() before modifying the caps.
2962 * Returns: (transfer full): the caps of the pad with incremented ref-count.
2965 gst_pad_query_caps (GstPad * pad, GstCaps * filter)
2967 GstCaps *result = NULL;
2970 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2971 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2973 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2974 "get pad caps with filter %" GST_PTR_FORMAT, filter);
2976 query = gst_query_new_caps (filter);
2977 if (gst_pad_query (pad, query)) {
2978 gst_query_parse_caps_result (query, &result);
2979 gst_caps_ref (result);
2980 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2981 "query returned %" GST_PTR_FORMAT, result);
2982 } else if (filter) {
2983 result = gst_caps_ref (filter);
2985 result = gst_caps_new_any ();
2987 gst_query_unref (query);
2993 * gst_pad_peer_query_caps:
2994 * @pad: a #GstPad to get the capabilities of.
2995 * @filter: (allow-none): a #GstCaps filter, or %NULL.
2997 * Gets the capabilities of the peer connected to this pad. Similar to
2998 * gst_pad_query_caps().
3000 * When called on srcpads @filter contains the caps that
3001 * upstream could produce in the order preferred by upstream. When
3002 * called on sinkpads @filter contains the caps accepted by
3003 * downstream in the preferred order. @filter might be %NULL but
3004 * if it is not %NULL the returned caps will be a subset of @filter.
3006 * Returns: (transfer full): the caps of the peer pad with incremented
3007 * ref-count. When there is no peer pad, this function returns @filter or,
3008 * when @filter is %NULL, ANY caps.
3011 gst_pad_peer_query_caps (GstPad * pad, GstCaps * filter)
3013 GstCaps *result = NULL;
3016 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3017 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
3019 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3020 "get pad peer caps with filter %" GST_PTR_FORMAT, filter);
3022 query = gst_query_new_caps (filter);
3023 if (gst_pad_peer_query (pad, query)) {
3024 gst_query_parse_caps_result (query, &result);
3025 gst_caps_ref (result);
3026 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
3027 "peer query returned %" GST_PTR_FORMAT, result);
3028 } else if (filter) {
3029 result = gst_caps_ref (filter);
3031 result = gst_caps_new_any ();
3033 gst_query_unref (query);
3039 * gst_pad_query_accept_caps:
3040 * @pad: a #GstPad to check
3041 * @caps: a #GstCaps to check on the pad
3043 * Check if the given pad accepts the caps.
3045 * Returns: %TRUE if the pad can accept the caps.
3048 gst_pad_query_accept_caps (GstPad * pad, GstCaps * caps)
3050 gboolean res = TRUE;
3053 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3054 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
3056 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %"
3057 GST_PTR_FORMAT, caps);
3059 query = gst_query_new_accept_caps (caps);
3060 if (gst_pad_query (pad, query)) {
3061 gst_query_parse_accept_caps_result (query, &res);
3062 GST_DEBUG_OBJECT (pad, "query returned %d", res);
3064 gst_query_unref (query);
3070 * gst_pad_peer_query_accept_caps:
3071 * @pad: a #GstPad to check the peer of
3072 * @caps: a #GstCaps to check on the pad
3074 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
3077 * Returns: %TRUE if the peer of @pad can accept the caps or @pad has no peer.
3080 gst_pad_peer_query_accept_caps (GstPad * pad, GstCaps * caps)
3082 gboolean res = TRUE;
3085 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3086 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
3088 query = gst_query_new_accept_caps (caps);
3089 if (gst_pad_peer_query (pad, query)) {
3090 gst_query_parse_accept_caps_result (query, &res);
3091 GST_DEBUG_OBJECT (pad, "query returned %d", res);
3093 gst_query_unref (query);
3099 element_find_unlinked_pad (GstElement * element, GstPadDirection direction)
3102 GstPad *unlinked_pad = NULL;
3104 GValue data = { 0, };
3106 switch (direction) {
3108 iter = gst_element_iterate_src_pads (element);
3111 iter = gst_element_iterate_sink_pads (element);
3114 g_return_val_if_reached (NULL);
3119 switch (gst_iterator_next (iter, &data)) {
3120 case GST_ITERATOR_OK:{
3122 GstPad *pad = g_value_get_object (&data);
3124 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
3125 GST_DEBUG_PAD_NAME (pad));
3127 peer = gst_pad_get_peer (pad);
3129 unlinked_pad = gst_object_ref (pad);
3131 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
3132 "found existing unlinked pad %s:%s",
3133 GST_DEBUG_PAD_NAME (unlinked_pad));
3135 gst_object_unref (peer);
3137 g_value_reset (&data);
3140 case GST_ITERATOR_DONE:
3143 case GST_ITERATOR_RESYNC:
3144 gst_iterator_resync (iter);
3146 case GST_ITERATOR_ERROR:
3147 g_return_val_if_reached (NULL);
3151 g_value_unset (&data);
3152 gst_iterator_free (iter);
3154 return unlinked_pad;
3158 * gst_bin_find_unlinked_pad:
3159 * @bin: bin in which to look for elements with unlinked pads
3160 * @direction: whether to look for an unlinked source or sink pad
3162 * Recursively looks for elements with an unlinked pad of the given
3163 * direction within the specified bin and returns an unlinked pad
3164 * if one is found, or %NULL otherwise. If a pad is found, the caller
3165 * owns a reference to it and should use gst_object_unref() on the
3166 * pad when it is not needed any longer.
3168 * Returns: (transfer full) (nullable): unlinked pad of the given
3172 gst_bin_find_unlinked_pad (GstBin * bin, GstPadDirection direction)
3177 GValue data = { 0, };
3179 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3180 g_return_val_if_fail (direction != GST_PAD_UNKNOWN, NULL);
3183 iter = gst_bin_iterate_recurse (bin);
3185 switch (gst_iterator_next (iter, &data)) {
3186 case GST_ITERATOR_OK:{
3187 GstElement *element = g_value_get_object (&data);
3189 pad = element_find_unlinked_pad (element, direction);
3192 g_value_reset (&data);
3195 case GST_ITERATOR_DONE:
3198 case GST_ITERATOR_RESYNC:
3199 gst_iterator_resync (iter);
3201 case GST_ITERATOR_ERROR:
3202 g_return_val_if_reached (NULL);
3206 g_value_unset (&data);
3207 gst_iterator_free (iter);
3213 gst_bin_sync_children_states_foreach (const GValue * value, gpointer user_data)
3215 gboolean *success = user_data;
3216 GstElement *element = g_value_get_object (value);
3218 if (gst_element_is_locked_state (element)) {
3221 *success = *success && gst_element_sync_state_with_parent (element);
3223 if (GST_IS_BIN (element))
3225 && gst_bin_sync_children_states (GST_BIN_CAST (element));
3230 * gst_bin_sync_children_states:
3233 * Synchronizes the state of every child of @bin with the state
3234 * of @bin. See also gst_element_sync_state_with_parent().
3236 * Returns: %TRUE if syncing the state was successful for all children,
3242 gst_bin_sync_children_states (GstBin * bin)
3245 GstIteratorResult res = GST_ITERATOR_OK;
3246 gboolean success = TRUE;
3248 it = gst_bin_iterate_sorted (bin);
3251 if (res == GST_ITERATOR_RESYNC) {
3253 gst_iterator_resync (it);
3256 gst_iterator_foreach (it, gst_bin_sync_children_states_foreach,
3258 } while (res == GST_ITERATOR_RESYNC);
3259 gst_iterator_free (it);
3265 * gst_parse_bin_from_description:
3266 * @bin_description: command line describing the bin
3267 * @ghost_unlinked_pads: whether to automatically create ghost pads
3268 * for unlinked source or sink pads within the bin
3269 * @err: where to store the error message in case of an error, or %NULL
3271 * This is a convenience wrapper around gst_parse_launch() to create a
3272 * #GstBin from a gst-launch-style pipeline description. See
3273 * gst_parse_launch() and the gst-launch man page for details about the
3274 * syntax. Ghost pads on the bin for unlinked source or sink pads
3275 * within the bin can automatically be created (but only a maximum of
3276 * one ghost pad for each direction will be created; if you expect
3277 * multiple unlinked source pads or multiple unlinked sink pads
3278 * and want them all ghosted, you will have to create the ghost pads
3281 * Returns: (transfer floating) (type Gst.Bin) (nullable): a
3282 * newly-created bin, or %NULL if an error occurred.
3285 gst_parse_bin_from_description (const gchar * bin_description,
3286 gboolean ghost_unlinked_pads, GError ** err)
3288 return gst_parse_bin_from_description_full (bin_description,
3289 ghost_unlinked_pads, NULL, GST_PARSE_FLAG_NONE, err);
3293 * gst_parse_bin_from_description_full:
3294 * @bin_description: command line describing the bin
3295 * @ghost_unlinked_pads: whether to automatically create ghost pads
3296 * for unlinked source or sink pads within the bin
3297 * @context: (transfer none) (allow-none): a parse context allocated with
3298 * gst_parse_context_new(), or %NULL
3299 * @flags: parsing options, or #GST_PARSE_FLAG_NONE
3300 * @err: where to store the error message in case of an error, or %NULL
3302 * This is a convenience wrapper around gst_parse_launch() to create a
3303 * #GstBin from a gst-launch-style pipeline description. See
3304 * gst_parse_launch() and the gst-launch man page for details about the
3305 * syntax. Ghost pads on the bin for unlinked source or sink pads
3306 * within the bin can automatically be created (but only a maximum of
3307 * one ghost pad for each direction will be created; if you expect
3308 * multiple unlinked source pads or multiple unlinked sink pads
3309 * and want them all ghosted, you will have to create the ghost pads
3312 * Returns: (transfer floating) (type Gst.Element): a newly-created
3313 * element, which is guaranteed to be a bin unless
3314 * GST_FLAG_NO_SINGLE_ELEMENT_BINS was passed, or %NULL if an error
3318 gst_parse_bin_from_description_full (const gchar * bin_description,
3319 gboolean ghost_unlinked_pads, GstParseContext * context,
3320 GstParseFlags flags, GError ** err)
3322 #ifndef GST_DISABLE_PARSE
3324 GstElement *element;
3328 g_return_val_if_fail (bin_description != NULL, NULL);
3329 g_return_val_if_fail (err == NULL || *err == NULL, NULL);
3331 GST_DEBUG ("Making bin from description '%s'", bin_description);
3333 /* parse the pipeline to a bin */
3334 if (flags & GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS) {
3335 element = gst_parse_launch_full (bin_description, context, flags, err);
3337 desc = g_strdup_printf ("bin.( %s )", bin_description);
3338 element = gst_parse_launch_full (desc, context, flags, err);
3342 if (element == NULL || (err && *err != NULL)) {
3344 gst_object_unref (element);
3348 if (GST_IS_BIN (element)) {
3349 bin = GST_BIN (element);
3354 /* find pads and ghost them if necessary */
3355 if (ghost_unlinked_pads) {
3356 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC))) {
3357 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("src", pad));
3358 gst_object_unref (pad);
3360 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SINK))) {
3361 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("sink", pad));
3362 gst_object_unref (pad);
3366 return GST_ELEMENT (bin);
3370 GST_WARNING ("Disabled API called");
3372 msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
3373 g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
3381 * gst_util_get_timestamp:
3383 * Get a timestamp as GstClockTime to be used for interval measurements.
3384 * The timestamp should not be interpreted in any other way.
3386 * Returns: the timestamp
3389 gst_util_get_timestamp (void)
3391 #if defined (HAVE_POSIX_TIMERS) && defined(HAVE_MONOTONIC_CLOCK) &&\
3392 defined (HAVE_CLOCK_GETTIME)
3393 struct timespec now;
3395 clock_gettime (CLOCK_MONOTONIC, &now);
3396 return GST_TIMESPEC_TO_TIME (now);
3400 g_get_current_time (&now);
3401 return GST_TIMEVAL_TO_TIME (now);
3406 * gst_util_array_binary_search:
3407 * @array: the sorted input array
3408 * @num_elements: number of elements in the array
3409 * @element_size: size of every element in bytes
3410 * @search_func: (scope call): function to compare two elements, @search_data will always be passed as second argument
3411 * @mode: search mode that should be used
3412 * @search_data: element that should be found
3413 * @user_data: (closure): data to pass to @search_func
3415 * Searches inside @array for @search_data by using the comparison function
3416 * @search_func. @array must be sorted ascending.
3418 * As @search_data is always passed as second argument to @search_func it's
3419 * not required that @search_data has the same type as the array elements.
3421 * The complexity of this search function is O(log (num_elements)).
3423 * Returns: (transfer none) (nullable): The address of the found
3424 * element or %NULL if nothing was found
3427 gst_util_array_binary_search (gpointer array, guint num_elements,
3428 gsize element_size, GCompareDataFunc search_func, GstSearchMode mode,
3429 gconstpointer search_data, gpointer user_data)
3431 glong left = 0, right = num_elements - 1, m;
3433 guint8 *data = (guint8 *) array;
3435 g_return_val_if_fail (array != NULL, NULL);
3436 g_return_val_if_fail (element_size > 0, NULL);
3437 g_return_val_if_fail (search_func != NULL, NULL);
3439 /* 0. No elements => return NULL */
3440 if (num_elements == 0)
3443 /* 1. If search_data is before the 0th element return the 0th element */
3444 ret = search_func (data, search_data, user_data);
3445 if ((ret >= 0 && mode == GST_SEARCH_MODE_AFTER) || ret == 0)
3450 /* 2. If search_data is after the last element return the last element */
3452 search_func (data + (num_elements - 1) * element_size, search_data,
3454 if ((ret <= 0 && mode == GST_SEARCH_MODE_BEFORE) || ret == 0)
3455 return data + (num_elements - 1) * element_size;
3459 /* 3. else binary search */
3461 m = left + (right - left) / 2;
3463 ret = search_func (data + m * element_size, search_data, user_data);
3466 return data + m * element_size;
3467 } else if (ret < 0) {
3473 /* No exact match found */
3475 if (mode == GST_SEARCH_MODE_EXACT) {
3477 } else if (mode == GST_SEARCH_MODE_AFTER) {
3479 return (m < num_elements) ? data + (m + 1) * element_size : NULL;
3481 return data + m * element_size;
3484 return data + m * element_size;
3486 return (m > 0) ? data + (m - 1) * element_size : NULL;
3492 /* Finds the greatest common divisor.
3493 * Returns 1 if none other found.
3494 * This is Euclid's algorithm. */
3497 * gst_util_greatest_common_divisor:
3498 * @a: First value as #gint
3499 * @b: Second value as #gint
3501 * Calculates the greatest common divisor of @a
3504 * Returns: Greatest common divisor of @a and @b
3507 gst_util_greatest_common_divisor (gint a, gint b)
3520 * gst_util_greatest_common_divisor_int64:
3521 * @a: First value as #gint64
3522 * @b: Second value as #gint64
3524 * Calculates the greatest common divisor of @a
3527 * Returns: Greatest common divisor of @a and @b
3530 gst_util_greatest_common_divisor_int64 (gint64 a, gint64 b)
3544 * gst_util_fraction_to_double:
3545 * @src_n: Fraction numerator as #gint
3546 * @src_d: Fraction denominator #gint
3547 * @dest: (out): pointer to a #gdouble for the result
3549 * Transforms a fraction to a #gdouble.
3552 gst_util_fraction_to_double (gint src_n, gint src_d, gdouble * dest)
3554 g_return_if_fail (dest != NULL);
3555 g_return_if_fail (src_d != 0);
3557 *dest = ((gdouble) src_n) / ((gdouble) src_d);
3560 #define MAX_TERMS 30
3561 #define MIN_DIVISOR 1.0e-10
3562 #define MAX_ERROR 1.0e-20
3564 /* use continued fractions to transform a double into a fraction,
3565 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
3566 * This algorithm takes care of overflows.
3570 * gst_util_double_to_fraction:
3571 * @src: #gdouble to transform
3572 * @dest_n: (out): pointer to a #gint to hold the result numerator
3573 * @dest_d: (out): pointer to a #gint to hold the result denominator
3575 * Transforms a #gdouble to a fraction and simplifies
3579 gst_util_double_to_fraction (gdouble src, gint * dest_n, gint * dest_d)
3582 gdouble V, F; /* double being converted */
3583 gint N, D; /* will contain the result */
3584 gint A; /* current term in continued fraction */
3585 gint64 N1, D1; /* numerator, denominator of last approx */
3586 gint64 N2, D2; /* numerator, denominator of previous approx */
3589 gboolean negative = FALSE;
3591 g_return_if_fail (dest_n != NULL);
3592 g_return_if_fail (dest_d != NULL);
3594 /* initialize fraction being converted */
3602 /* initialize fractions with 1/0, 0/1 */
3610 for (i = 0; i < MAX_TERMS; i++) {
3612 A = (gint) F; /* no floor() needed, F is always >= 0 */
3613 /* get new divisor */
3616 /* calculate new fraction in temp */
3620 /* guard against overflow */
3621 if (N2 > G_MAXINT || D2 > G_MAXINT) {
3628 /* save last two fractions */
3634 /* quit if dividing by zero or close enough to target */
3635 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
3639 /* Take reciprocal */
3642 /* fix for overflow */
3647 /* fix for negative */
3652 gcd = gst_util_greatest_common_divisor (N, D);
3664 * gst_util_fraction_multiply:
3665 * @a_n: Numerator of first value
3666 * @a_d: Denominator of first value
3667 * @b_n: Numerator of second value
3668 * @b_d: Denominator of second value
3669 * @res_n: (out): Pointer to #gint to hold the result numerator
3670 * @res_d: (out): Pointer to #gint to hold the result denominator
3672 * Multiplies the fractions @a_n/@a_d and @b_n/@b_d and stores
3673 * the result in @res_n and @res_d.
3675 * Returns: %FALSE on overflow, %TRUE otherwise.
3678 gst_util_fraction_multiply (gint a_n, gint a_d, gint b_n, gint b_d,
3679 gint * res_n, gint * res_d)
3683 g_return_val_if_fail (res_n != NULL, FALSE);
3684 g_return_val_if_fail (res_d != NULL, FALSE);
3685 g_return_val_if_fail (a_d != 0, FALSE);
3686 g_return_val_if_fail (b_d != 0, FALSE);
3688 /* early out if either is 0, as its gcd would be 0 */
3689 if (a_n == 0 || b_n == 0) {
3695 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3699 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3703 gcd = gst_util_greatest_common_divisor (a_n, b_d);
3707 gcd = gst_util_greatest_common_divisor (a_d, b_n);
3711 /* This would result in overflow */
3712 if (a_n != 0 && G_MAXINT / ABS (a_n) < ABS (b_n))
3714 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3720 gcd = gst_util_greatest_common_divisor (*res_n, *res_d);
3728 * gst_util_fraction_add:
3729 * @a_n: Numerator of first value
3730 * @a_d: Denominator of first value
3731 * @b_n: Numerator of second value
3732 * @b_d: Denominator of second value
3733 * @res_n: (out): Pointer to #gint to hold the result numerator
3734 * @res_d: (out): Pointer to #gint to hold the result denominator
3736 * Adds the fractions @a_n/@a_d and @b_n/@b_d and stores
3737 * the result in @res_n and @res_d.
3739 * Returns: %FALSE on overflow, %TRUE otherwise.
3742 gst_util_fraction_add (gint a_n, gint a_d, gint b_n, gint b_d, gint * res_n,
3747 g_return_val_if_fail (res_n != NULL, FALSE);
3748 g_return_val_if_fail (res_d != NULL, FALSE);
3749 g_return_val_if_fail (a_d != 0, FALSE);
3750 g_return_val_if_fail (b_d != 0, FALSE);
3752 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3756 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3771 /* This would result in overflow */
3772 if (G_MAXINT / ABS (a_n) < ABS (b_n))
3774 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3777 *res_n = (a_n * b_d) + (a_d * b_n);
3780 gcd = gst_util_greatest_common_divisor (*res_n, *res_d);
3793 * gst_util_fraction_compare:
3794 * @a_n: Numerator of first value
3795 * @a_d: Denominator of first value
3796 * @b_n: Numerator of second value
3797 * @b_d: Denominator of second value
3799 * Compares the fractions @a_n/@a_d and @b_n/@b_d and returns
3800 * -1 if a < b, 0 if a = b and 1 if a > b.
3802 * Returns: -1 if a < b; 0 if a = b; 1 if a > b.
3805 gst_util_fraction_compare (gint a_n, gint a_d, gint b_n, gint b_d)
3811 g_return_val_if_fail (a_d != 0 && b_d != 0, 0);
3814 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3818 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3822 /* fractions are reduced when set, so we can quickly see if they're equal */
3823 if (a_n == b_n && a_d == b_d)
3826 /* extend to 64 bits */
3827 new_num_1 = ((gint64) a_n) * b_d;
3828 new_num_2 = ((gint64) b_n) * a_d;
3829 if (new_num_1 < new_num_2)
3831 if (new_num_1 > new_num_2)
3834 /* Should not happen because a_d and b_d are not 0 */
3835 g_return_val_if_reached (0);
3839 gst_pad_create_stream_id_internal (GstPad * pad, GstElement * parent,
3840 const gchar * stream_id)
3842 GstEvent *upstream_event;
3843 gchar *upstream_stream_id = NULL, *new_stream_id;
3846 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3847 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
3848 g_return_val_if_fail (GST_IS_ELEMENT (parent), NULL);
3850 g_return_val_if_fail (parent->numsinkpads <= 1, NULL);
3852 /* If the element has multiple source pads it must
3853 * provide a stream-id for every source pad, otherwise
3854 * all source pads will have the same and are not
3855 * distinguishable */
3856 g_return_val_if_fail (parent->numsrcpads <= 1 || stream_id, NULL);
3858 /* First try to get the upstream stream-start stream-id from the sinkpad.
3859 * This will only work for non-source elements */
3860 sinkpad = gst_element_get_static_pad (parent, "sink");
3863 gst_pad_get_sticky_event (sinkpad, GST_EVENT_STREAM_START, 0);
3864 if (upstream_event) {
3867 gst_event_parse_stream_start (upstream_event, &tmp);
3869 upstream_stream_id = g_strdup (tmp);
3870 gst_event_unref (upstream_event);
3872 gst_object_unref (sinkpad);
3875 /* The only case where we don't have an upstream start-start event
3876 * here is for source elements */
3877 if (!upstream_stream_id) {
3881 /* Try to generate one from the URI query and
3882 * if it fails take a random number instead */
3883 query = gst_query_new_uri ();
3884 if (gst_element_query (parent, query)) {
3885 gst_query_parse_uri (query, &uri);
3891 /* And then generate an SHA256 sum of the URI */
3892 cs = g_checksum_new (G_CHECKSUM_SHA256);
3893 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
3895 upstream_stream_id = g_strdup (g_checksum_get_string (cs));
3896 g_checksum_free (cs);
3898 /* Just get some random number if the URI query fails */
3899 GST_FIXME_OBJECT (pad, "Creating random stream-id, consider "
3900 "implementing a deterministic way of creating a stream-id");
3901 upstream_stream_id =
3902 g_strdup_printf ("%08x%08x%08x%08x", g_random_int (), g_random_int (),
3903 g_random_int (), g_random_int ());
3906 gst_query_unref (query);
3910 new_stream_id = g_strconcat (upstream_stream_id, "/", stream_id, NULL);
3912 new_stream_id = g_strdup (upstream_stream_id);
3915 g_free (upstream_stream_id);
3917 return new_stream_id;
3921 * gst_pad_create_stream_id_printf_valist:
3922 * @pad: A source #GstPad
3923 * @parent: Parent #GstElement of @pad
3924 * @stream_id: (allow-none): The stream-id
3925 * @var_args: parameters for the @stream_id format string
3927 * Creates a stream-id for the source #GstPad @pad by combining the
3928 * upstream information with the optional @stream_id of the stream
3929 * of @pad. @pad must have a parent #GstElement and which must have zero
3930 * or one sinkpad. @stream_id can only be %NULL if the parent element
3931 * of @pad has only a single source pad.
3933 * This function generates an unique stream-id by getting the upstream
3934 * stream-start event stream ID and appending @stream_id to it. If the
3935 * element has no sinkpad it will generate an upstream stream-id by
3936 * doing an URI query on the element and in the worst case just uses
3937 * a random number. Source elements that don't implement the URI
3938 * handler interface should ideally generate a unique, deterministic
3939 * stream-id manually instead.
3941 * Returns: A stream-id for @pad. g_free() after usage.
3944 gst_pad_create_stream_id_printf_valist (GstPad * pad, GstElement * parent,
3945 const gchar * stream_id, va_list var_args)
3947 gchar *expanded = NULL, *new_stream_id;
3950 expanded = g_strdup_vprintf (stream_id, var_args);
3952 new_stream_id = gst_pad_create_stream_id_internal (pad, parent, expanded);
3956 return new_stream_id;
3960 * gst_pad_create_stream_id_printf:
3961 * @pad: A source #GstPad
3962 * @parent: Parent #GstElement of @pad
3963 * @stream_id: (allow-none): The stream-id
3964 * @...: parameters for the @stream_id format string
3966 * Creates a stream-id for the source #GstPad @pad by combining the
3967 * upstream information with the optional @stream_id of the stream
3968 * of @pad. @pad must have a parent #GstElement and which must have zero
3969 * or one sinkpad. @stream_id can only be %NULL if the parent element
3970 * of @pad has only a single source pad.
3972 * This function generates an unique stream-id by getting the upstream
3973 * stream-start event stream ID and appending @stream_id to it. If the
3974 * element has no sinkpad it will generate an upstream stream-id by
3975 * doing an URI query on the element and in the worst case just uses
3976 * a random number. Source elements that don't implement the URI
3977 * handler interface should ideally generate a unique, deterministic
3978 * stream-id manually instead.
3980 * Returns: A stream-id for @pad. g_free() after usage.
3983 gst_pad_create_stream_id_printf (GstPad * pad, GstElement * parent,
3984 const gchar * stream_id, ...)
3987 gchar *new_stream_id;
3989 va_start (var_args, stream_id);
3991 gst_pad_create_stream_id_printf_valist (pad, parent, stream_id, var_args);
3994 return new_stream_id;
3998 * gst_pad_create_stream_id:
3999 * @pad: A source #GstPad
4000 * @parent: Parent #GstElement of @pad
4001 * @stream_id: (allow-none): The stream-id
4003 * Creates a stream-id for the source #GstPad @pad by combining the
4004 * upstream information with the optional @stream_id of the stream
4005 * of @pad. @pad must have a parent #GstElement and which must have zero
4006 * or one sinkpad. @stream_id can only be %NULL if the parent element
4007 * of @pad has only a single source pad.
4009 * This function generates an unique stream-id by getting the upstream
4010 * stream-start event stream ID and appending @stream_id to it. If the
4011 * element has no sinkpad it will generate an upstream stream-id by
4012 * doing an URI query on the element and in the worst case just uses
4013 * a random number. Source elements that don't implement the URI
4014 * handler interface should ideally generate a unique, deterministic
4015 * stream-id manually instead.
4017 * Since stream IDs are sorted alphabetically, any numbers in the
4018 * stream ID should be printed with a fixed number of characters,
4019 * preceded by 0's, such as by using the format \%03u instead of \%u.
4021 * Returns: A stream-id for @pad. g_free() after usage.
4024 gst_pad_create_stream_id (GstPad * pad, GstElement * parent,
4025 const gchar * stream_id)
4027 return gst_pad_create_stream_id_internal (pad, parent, stream_id);
4031 * gst_pad_get_stream_id:
4032 * @pad: A source #GstPad
4034 * Returns the current stream-id for the @pad, or %NULL if none has been
4035 * set yet, i.e. the pad has not received a stream-start event yet.
4037 * This is a convenience wrapper around gst_pad_get_sticky_event() and
4038 * gst_event_parse_stream_start().
4040 * The returned stream-id string should be treated as an opaque string, its
4041 * contents should not be interpreted.
4043 * Returns: (nullable): a newly-allocated copy of the stream-id for
4044 * @pad, or %NULL. g_free() the returned string when no longer
4050 gst_pad_get_stream_id (GstPad * pad)
4052 const gchar *stream_id = NULL;
4056 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4058 event = gst_pad_get_sticky_event (pad, GST_EVENT_STREAM_START, 0);
4059 if (event != NULL) {
4060 gst_event_parse_stream_start (event, &stream_id);
4061 ret = g_strdup (stream_id);
4062 gst_event_unref (event);
4063 GST_LOG_OBJECT (pad, "pad has stream-id '%s'", ret);
4065 GST_DEBUG_OBJECT (pad, "pad has not received a stream-start event yet");
4072 * gst_pad_get_stream:
4073 * @pad: A source #GstPad
4075 * Returns the current #GstStream for the @pad, or %NULL if none has been
4076 * set yet, i.e. the pad has not received a stream-start event yet.
4078 * This is a convenience wrapper around gst_pad_get_sticky_event() and
4079 * gst_event_parse_stream().
4081 * Returns: (nullable) (transfer full): the current #GstStream for @pad, or %NULL.
4082 * unref the returned stream when no longer needed.
4087 gst_pad_get_stream (GstPad * pad)
4089 GstStream *stream = NULL;
4092 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
4094 event = gst_pad_get_sticky_event (pad, GST_EVENT_STREAM_START, 0);
4095 if (event != NULL) {
4096 gst_event_parse_stream (event, &stream);
4097 gst_event_unref (event);
4098 GST_LOG_OBJECT (pad, "pad has stream object %p", stream);
4100 GST_DEBUG_OBJECT (pad, "pad has not received a stream-start event yet");
4107 * gst_util_group_id_next:
4109 * Return a constantly incrementing group id.
4111 * This function is used to generate a new group-id for the
4112 * stream-start event.
4114 * Returns: A constantly incrementing unsigned integer, which might
4115 * overflow back to 0 at some point.
4118 gst_util_group_id_next (void)
4120 static gint counter = 0;
4121 return g_atomic_int_add (&counter, 1);
4124 /* Compute log2 of the passed 64-bit number by finding the highest set bit */
4126 gst_log2 (GstClockTime in)
4129 { 0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000, 0xFFFFFFFF00000000LL };
4130 const guint64 S[] = { 1, 2, 4, 8, 16, 32 };
4134 for (i = 5; i >= 0; i--) {
4145 * gst_calculate_linear_regression:
4146 * @xy: Pairs of (x,y) values
4147 * @temp: Temporary scratch space used by the function
4148 * @n: number of (x,y) pairs
4149 * @m_num: (out): numerator of calculated slope
4150 * @m_denom: (out): denominator of calculated slope
4151 * @b: (out): Offset at Y-axis
4152 * @xbase: (out): Offset at X-axis
4153 * @r_squared: (out): R-squared
4155 * Calculates the linear regression of the values @xy and places the
4156 * result in @m_num, @m_denom, @b and @xbase, representing the function
4157 * y(x) = m_num/m_denom * (x - xbase) + b
4158 * that has the least-square distance from all points @x and @y.
4160 * @r_squared will contain the remaining error.
4162 * If @temp is not %NULL, it will be used as temporary space for the function,
4163 * in which case the function works without any allocation at all. If @temp is
4164 * %NULL, an allocation will take place. @temp should have at least the same
4165 * amount of memory allocated as @xy, i.e. 2*n*sizeof(GstClockTime).
4167 * > This function assumes (x,y) values with reasonable large differences
4168 * > between them. It will not calculate the exact results if the differences
4169 * > between neighbouring values are too small due to not being able to
4170 * > represent sub-integer values during the calculations.
4172 * Returns: %TRUE if the linear regression was successfully calculated
4176 /* http://mathworld.wolfram.com/LeastSquaresFitting.html
4180 gst_calculate_linear_regression (const GstClockTime * xy,
4181 GstClockTime * temp, guint n,
4182 GstClockTime * m_num, GstClockTime * m_denom,
4183 GstClockTime * b, GstClockTime * xbase, gdouble * r_squared)
4185 const GstClockTime *x, *y;
4186 GstClockTime *newx, *newy;
4187 GstClockTime xmin, ymin, xbar, ybar, xbar4, ybar4;
4188 GstClockTime xmax, ymax;
4189 GstClockTimeDiff sxx, sxy, syy;
4194 g_return_val_if_fail (xy != NULL, FALSE);
4195 g_return_val_if_fail (m_num != NULL, FALSE);
4196 g_return_val_if_fail (m_denom != NULL, FALSE);
4197 g_return_val_if_fail (b != NULL, FALSE);
4198 g_return_val_if_fail (xbase != NULL, FALSE);
4199 g_return_val_if_fail (r_squared != NULL, FALSE);
4204 xbar = ybar = sxx = syy = sxy = 0;
4206 xmin = ymin = G_MAXUINT64;
4208 for (i = j = 0; i < n; i++, j += 2) {
4209 xmin = MIN (xmin, x[j]);
4210 ymin = MIN (ymin, y[j]);
4212 xmax = MAX (xmax, x[j]);
4213 ymax = MAX (ymax, y[j]);
4217 /* Allocate up to 1kb on the stack, otherwise heap */
4218 newx = n > 64 ? g_new (GstClockTime, 2 * n) : g_newa (GstClockTime, 2 * n);
4225 /* strip off unnecessary bits of precision */
4226 for (i = j = 0; i < n; i++, j += 2) {
4227 newx[j] = x[j] - xmin;
4228 newy[j] = y[j] - ymin;
4231 #ifdef DEBUGGING_ENABLED
4232 GST_CAT_DEBUG (GST_CAT_CLOCK, "reduced numbers:");
4233 for (i = j = 0; i < n; i++, j += 2)
4234 GST_CAT_DEBUG (GST_CAT_CLOCK,
4235 " %" G_GUINT64_FORMAT " %" G_GUINT64_FORMAT, newx[j], newy[j]);
4238 /* have to do this precisely otherwise the results are pretty much useless.
4239 * should guarantee that none of these accumulators can overflow */
4241 /* quantities on the order of 1e10 to 1e13 -> 30-35 bits;
4242 * window size a max of 2^10, so
4243 this addition could end up around 2^45 or so -- ample headroom */
4244 for (i = j = 0; i < n; i++, j += 2) {
4245 /* Just in case assumptions about headroom prove false, let's check */
4246 if ((newx[j] > 0 && G_MAXUINT64 - xbar <= newx[j]) ||
4247 (newy[j] > 0 && G_MAXUINT64 - ybar <= newy[j])) {
4248 GST_CAT_WARNING (GST_CAT_CLOCK,
4249 "Regression overflowed in clock slaving! xbar %"
4250 G_GUINT64_FORMAT " newx[j] %" G_GUINT64_FORMAT " ybar %"
4251 G_GUINT64_FORMAT " newy[j] %" G_GUINT64_FORMAT, xbar, newx[j], ybar,
4253 if (temp == NULL && n > 64)
4264 /* multiplying directly would give quantities on the order of 1e20-1e26 ->
4265 * 60 bits to 70 bits times the window size that's 80 which is too much.
4266 * Instead we (1) subtract off the xbar*ybar in the loop instead of after,
4267 * to avoid accumulation; (2) shift off some estimated number of bits from
4268 * each multiplicand to limit the expected ceiling. For strange
4269 * distributions of input values, things can still overflow, in which
4270 * case we drop precision and retry - at most a few times, in practice rarely
4273 /* Guess how many bits we might need for the usual distribution of input,
4274 * with a fallback loop that drops precision if things go pear-shaped */
4275 max_bits = gst_log2 (MAX (xmax - xmin, ymax - ymin)) * 7 / 8 + gst_log2 (n);
4277 pshift = max_bits - 64;
4281 #ifdef DEBUGGING_ENABLED
4282 GST_CAT_DEBUG (GST_CAT_CLOCK,
4283 "Restarting regression with precision shift %u", pshift);
4286 xbar4 = xbar >> pshift;
4287 ybar4 = ybar >> pshift;
4288 sxx = syy = sxy = 0;
4289 for (i = j = 0; i < n; i++, j += 2) {
4290 GstClockTime newx4, newy4;
4291 GstClockTimeDiff tmp;
4293 newx4 = newx[j] >> pshift;
4294 newy4 = newy[j] >> pshift;
4296 tmp = (newx4 + xbar4) * (newx4 - xbar4);
4297 if (G_UNLIKELY (tmp > 0 && sxx > 0 && (G_MAXINT64 - sxx <= tmp))) {
4299 /* Drop some precision and restart */
4303 } while (G_MAXINT64 - sxx <= tmp);
4305 } else if (G_UNLIKELY (tmp < 0 && sxx < 0 && (G_MAXINT64 - sxx >= tmp))) {
4307 /* Drop some precision and restart */
4311 } while (G_MININT64 - sxx >= tmp);
4316 tmp = newy4 * newy4 - ybar4 * ybar4;
4317 if (G_UNLIKELY (tmp > 0 && syy > 0 && (G_MAXINT64 - syy <= tmp))) {
4322 } while (G_MAXINT64 - syy <= tmp);
4324 } else if (G_UNLIKELY (tmp < 0 && syy < 0 && (G_MAXINT64 - syy >= tmp))) {
4329 } while (G_MININT64 - syy >= tmp);
4334 tmp = newx4 * newy4 - xbar4 * ybar4;
4335 if (G_UNLIKELY (tmp > 0 && sxy > 0 && (G_MAXINT64 - sxy <= tmp))) {
4340 } while (G_MAXINT64 - sxy <= tmp);
4342 } else if (G_UNLIKELY (tmp < 0 && sxy < 0 && (G_MININT64 - sxy >= tmp))) {
4347 } while (G_MININT64 - sxy >= tmp);
4354 if (G_UNLIKELY (sxx == 0))
4359 *b = (ymin + ybar) - gst_util_uint64_scale_round (xbar, *m_num, *m_denom);
4360 /* Report base starting from the most recent observation */
4362 *b += gst_util_uint64_scale_round (xmax - xmin, *m_num, *m_denom);
4364 *r_squared = ((double) sxy * (double) sxy) / ((double) sxx * (double) syy);
4366 #ifdef DEBUGGING_ENABLED
4367 GST_CAT_DEBUG (GST_CAT_CLOCK, " m = %g", ((double) *m_num) / *m_denom);
4368 GST_CAT_DEBUG (GST_CAT_CLOCK, " b = %" G_GUINT64_FORMAT, *b);
4369 GST_CAT_DEBUG (GST_CAT_CLOCK, " xbase = %" G_GUINT64_FORMAT, *xbase);
4370 GST_CAT_DEBUG (GST_CAT_CLOCK, " r2 = %g", *r_squared);
4373 if (temp == NULL && n > 64)
4380 GST_CAT_DEBUG (GST_CAT_CLOCK, "sxx == 0, regression failed");
4381 if (temp == NULL && n > 64)