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>
6 * gstutils.c: Utility functions
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
26 * @short_description: Various utility functions
30 #include "gst_private.h"
34 #include "gstghostpad.h"
40 #include "gst-i18n-lib.h"
41 #include "glib-compat-private.h"
43 #ifdef TIZEN_PROFILE_TV
44 #include "gstchildproxy.h"
49 * @mem: a pointer to the memory to dump
50 * @size: the size of the memory block to dump
52 * Dumps the memory block into a hex representation. Useful for debugging.
55 gst_util_dump_mem (const guchar * mem, guint size)
58 GString *string = g_string_sized_new (50);
59 GString *chars = g_string_sized_new (18);
63 if (g_ascii_isprint (mem[i]))
64 g_string_append_c (chars, mem[i]);
66 g_string_append_c (chars, '.');
68 g_string_append_printf (string, "%02x ", mem[i]);
73 if (j == 16 || i == size) {
74 g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j, mem + i - j,
75 string->str, chars->str);
76 g_string_set_size (string, 0);
77 g_string_set_size (chars, 0);
81 g_string_free (string, TRUE);
82 g_string_free (chars, TRUE);
87 * gst_util_set_value_from_string:
88 * @value: (out caller-allocates): the value to set
89 * @value_str: the string to get the value from
91 * Converts the string to the type of the value and
92 * sets the value with it.
94 * Note that this function is dangerous as it does not return any indication
95 * if the conversion worked or not.
98 gst_util_set_value_from_string (GValue * value, const gchar * value_str)
102 g_return_if_fail (value != NULL);
103 g_return_if_fail (value_str != NULL);
105 GST_CAT_DEBUG (GST_CAT_PARAMS, "parsing '%s' to type %s", value_str,
106 g_type_name (G_VALUE_TYPE (value)));
108 res = gst_value_deserialize (value, value_str);
109 if (!res && G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
110 /* backwards compat, all booleans that fail to parse are false */
111 g_value_set_boolean (value, FALSE);
114 g_return_if_fail (res);
118 * gst_util_set_object_arg:
119 * @object: the object to set the argument of
120 * @name: the name of the argument to set
121 * @value: the string value to set
123 * Converts the string value to the type of the objects argument and
124 * sets the argument with it.
126 * Note that this function silently returns if @object has no property named
127 * @name or when @value cannot be converted to the type of the property.
130 gst_util_set_object_arg (GObject * object, const gchar * name,
137 g_return_if_fail (G_IS_OBJECT (object));
138 g_return_if_fail (name != NULL);
139 g_return_if_fail (value != NULL);
141 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
145 value_type = pspec->value_type;
147 GST_DEBUG ("pspec->flags is %d, pspec->value_type is %s",
148 pspec->flags, g_type_name (value_type));
150 if (!(pspec->flags & G_PARAM_WRITABLE))
153 g_value_init (&v, value_type);
155 /* special case for element <-> xml (de)serialisation */
156 if (value_type == GST_TYPE_STRUCTURE && strcmp (value, "NULL") == 0) {
157 g_value_set_boxed (&v, NULL);
161 if (!gst_value_deserialize (&v, value))
166 g_object_set_property (object, pspec->name, &v);
170 /* work around error C2520: conversion from unsigned __int64 to double
171 * not implemented, use signed __int64
173 * These are implemented as functions because on some platforms a 64bit int to
174 * double conversion is not defined/implemented.
178 gst_util_guint64_to_gdouble (guint64 value)
180 if (value & G_GINT64_CONSTANT (0x8000000000000000))
181 return (gdouble) ((gint64) value) + (gdouble) 18446744073709551616.;
183 return (gdouble) ((gint64) value);
187 gst_util_gdouble_to_guint64 (gdouble value)
189 if (value < (gdouble) 9223372036854775808.) /* 1 << 63 */
190 return ((guint64) ((gint64) value));
192 value -= (gdouble) 18446744073709551616.;
193 return ((guint64) ((gint64) value));
196 #ifndef HAVE_UINT128_T
197 /* convenience struct for getting high and low uint32 parts of
204 #if G_BYTE_ORDER == G_BIG_ENDIAN
212 #if defined (__x86_64__) && defined (__GNUC__)
214 gst_util_uint64_mul_uint64 (GstUInt64 * c1, GstUInt64 * c0, guint64 arg1,
217 __asm__ __volatile__ ("mulq %3":"=a" (c0->ll), "=d" (c1->ll)
218 :"a" (arg1), "g" (arg2)
221 #else /* defined (__x86_64__) */
222 /* multiply two 64-bit unsigned ints into a 128-bit unsigned int. the high
223 * and low 64 bits of the product are placed in c1 and c0 respectively.
224 * this operation cannot overflow. */
226 gst_util_uint64_mul_uint64 (GstUInt64 * c1, GstUInt64 * c0, guint64 arg1,
236 /* do 128 bits multiply
244 * -------------------
247 * "a0" is optimized away, result is stored directly in c0. "b1" is
248 * optimized away, result is stored directly in c1.
250 c0->ll = (guint64) v.l.low * n.l.low;
251 a1.ll = (guint64) v.l.low * n.l.high;
252 b0.ll = (guint64) v.l.high * n.l.low;
254 /* add the high word of a0 to the low words of a1 and b0 using c1 as
255 * scrach space to capture the carry. the low word of the result becomes
256 * the final high word of c0 */
257 c1->ll = (guint64) c0->l.high + a1.l.low + b0.l.low;
258 c0->l.high = c1->l.low;
260 /* add the carry from the result above (found in the high word of c1) and
261 * the high words of a1 and b0 to b1, the result is c1. */
262 c1->ll = (guint64) v.l.high * n.l.high + c1->l.high + a1.l.high + b0.l.high;
264 #endif /* defined (__x86_64__) */
266 #if defined (__x86_64__) && defined (__GNUC__)
267 static inline guint64
268 gst_util_div128_64 (GstUInt64 c1, GstUInt64 c0, guint64 denom)
272 __asm__ __volatile__ ("divq %3":"=a" (res)
273 :"d" (c1.ll), "a" (c0.ll), "g" (denom)
279 /* count leading zeros */
281 gst_util_clz (guint32 val)
285 s = val | (val >> 1);
289 s = ~(s | (s >> 16));
290 s = s - ((s >> 1) & 0x55555555);
291 s = (s & 0x33333333) + ((s >> 2) & 0x33333333);
292 s = (s + (s >> 4)) & 0x0f0f0f0f;
294 s = (s + (s >> 16)) & 0x3f;
299 /* based on Hacker's Delight p152 */
300 static inline guint64
301 gst_util_div128_64 (GstUInt64 c1, GstUInt64 c0, guint64 denom)
303 GstUInt64 q1, q0, rhat;
304 GstUInt64 v, cmp1, cmp2;
309 /* count number of leading zeroes, we know they must be in the high
310 * part of denom since denom > G_MAXUINT32. */
311 s = gst_util_clz (v.l.high);
314 /* normalize divisor and dividend */
316 c1.ll = (c1.ll << s) | (c0.l.high >> (32 - s));
320 q1.ll = c1.ll / v.l.high;
321 rhat.ll = c1.ll - q1.ll * v.l.high;
323 cmp1.l.high = rhat.l.low;
324 cmp1.l.low = c0.l.high;
325 cmp2.ll = q1.ll * v.l.low;
327 while (q1.l.high || cmp2.ll > cmp1.ll) {
332 cmp1.l.high = rhat.l.low;
335 c1.l.high = c1.l.low;
336 c1.l.low = c0.l.high;
337 c1.ll -= q1.ll * v.ll;
338 q0.ll = c1.ll / v.l.high;
339 rhat.ll = c1.ll - q0.ll * v.l.high;
341 cmp1.l.high = rhat.l.low;
342 cmp1.l.low = c0.l.low;
343 cmp2.ll = q0.ll * v.l.low;
345 while (q0.l.high || cmp2.ll > cmp1.ll) {
350 cmp1.l.high = rhat.l.low;
353 q0.l.high += q1.l.low;
357 #endif /* defined (__GNUC__) */
359 /* This always gives the correct result because:
360 * a) val <= G_MAXUINT64-1
361 * b) (c0,c1) <= G_MAXUINT64 * (G_MAXUINT64-1)
363 * (c0,c1) == G_MAXUINT64 * G_MAXUINT64 and denom < G_MAXUINT64
364 * (note: num==denom case is handled by short path)
365 * This means that (c0,c1) either has enough space for val
366 * or that the overall result will overflow anyway.
369 /* add correction with carry */
370 #define CORRECT(c0,c1,val) \
372 if (G_MAXUINT64 - c0.ll < val) { \
373 if (G_UNLIKELY (c1.ll == G_MAXUINT64)) \
375 return G_MAXUINT64; \
382 gst_util_uint64_scale_uint64_unchecked (guint64 val, guint64 num,
383 guint64 denom, guint64 correct)
387 /* compute 128-bit numerator product */
388 gst_util_uint64_mul_uint64 (&c1, &c0, val, num);
390 /* perform rounding correction */
391 CORRECT (c0, c1, correct);
393 /* high word as big as or bigger than denom --> overflow */
394 if (G_UNLIKELY (c1.ll >= denom))
397 /* compute quotient, fits in 64 bits */
398 return gst_util_div128_64 (c1, c0, denom);
402 #define GST_MAXUINT128 ((__uint128_t) -1)
404 gst_util_uint64_scale_uint64_unchecked (guint64 val, guint64 num,
405 guint64 denom, guint64 correct)
409 /* Calculate val * num */
410 tmp = ((__uint128_t) val) * ((__uint128_t) num);
412 /* overflow checks */
413 if (G_UNLIKELY (GST_MAXUINT128 - correct < tmp))
416 /* perform rounding correction */
419 /* Divide by denom */
422 /* if larger than G_MAXUINT64 --> overflow */
423 if (G_UNLIKELY (tmp > G_MAXUINT64))
426 /* compute quotient, fits in 64 bits */
427 return (guint64) tmp;
432 #if !defined (__x86_64__) && !defined (HAVE_UINT128_T)
434 gst_util_uint64_mul_uint32 (GstUInt64 * c1, GstUInt64 * c0, guint64 arg1,
441 c0->ll = (guint64) a.l.low * arg2;
442 c1->ll = (guint64) a.l.high * arg2 + c0->l.high;
446 /* divide a 96-bit unsigned int by a 32-bit unsigned int when we know the
447 * quotient fits into 64 bits. the high 64 bits and low 32 bits of the
448 * numerator are expected in c1 and c0 respectively. */
449 static inline guint64
450 gst_util_div96_32 (guint64 c1, guint64 c0, guint32 denom)
452 c0 += (c1 % denom) << 32;
453 return ((c1 / denom) << 32) + (c0 / denom);
456 static inline guint64
457 gst_util_uint64_scale_uint32_unchecked (guint64 val, guint32 num,
458 guint32 denom, guint32 correct)
462 /* compute 96-bit numerator product */
463 gst_util_uint64_mul_uint32 (&c1, &c0, val, num);
465 /* condition numerator based on rounding mode */
466 CORRECT (c0, c1, correct);
468 /* high 32 bits as big as or bigger than denom --> overflow */
469 if (G_UNLIKELY (c1.l.high >= denom))
472 /* compute quotient, fits in 64 bits */
473 return gst_util_div96_32 (c1.ll, c0.ll, denom);
477 /* the guts of the gst_util_uint64_scale() variants */
479 _gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom,
482 g_return_val_if_fail (denom != 0, G_MAXUINT64);
484 if (G_UNLIKELY (num == 0))
487 if (G_UNLIKELY (num == denom))
490 /* on 64bits we always use a full 128bits multiply/division */
491 #if !defined (__x86_64__) && !defined (HAVE_UINT128_T)
492 /* denom is low --> try to use 96 bit muldiv */
493 if (G_LIKELY (denom <= G_MAXUINT32)) {
494 /* num is low --> use 96 bit muldiv */
495 if (G_LIKELY (num <= G_MAXUINT32))
496 return gst_util_uint64_scale_uint32_unchecked (val, (guint32) num,
497 (guint32) denom, correct);
499 /* num is high but val is low --> swap and use 96-bit muldiv */
500 if (G_LIKELY (val <= G_MAXUINT32))
501 return gst_util_uint64_scale_uint32_unchecked (num, (guint32) val,
502 (guint32) denom, correct);
504 #endif /* !defined (__x86_64__) && !defined (HAVE_UINT128_T) */
506 /* val is high and num is high --> use 128-bit muldiv */
507 return gst_util_uint64_scale_uint64_unchecked (val, num, denom, correct);
511 * gst_util_uint64_scale:
512 * @val: the number to scale
513 * @num: the numerator of the scale ratio
514 * @denom: the denominator of the scale ratio
516 * Scale @val by the rational number @num / @denom, avoiding overflows and
517 * underflows and without loss of precision.
519 * This function can potentially be very slow if val and num are both
520 * greater than G_MAXUINT32.
522 * Returns: @val * @num / @denom. In the case of an overflow, this
523 * function returns G_MAXUINT64. If the result is not exactly
524 * representable as an integer it is truncated. See also
525 * gst_util_uint64_scale_round(), gst_util_uint64_scale_ceil(),
526 * gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
527 * gst_util_uint64_scale_int_ceil().
530 gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom)
532 return _gst_util_uint64_scale (val, num, denom, 0);
536 * gst_util_uint64_scale_round:
537 * @val: the number to scale
538 * @num: the numerator of the scale ratio
539 * @denom: the denominator of the scale ratio
541 * Scale @val by the rational number @num / @denom, avoiding overflows and
542 * underflows and without loss of precision.
544 * This function can potentially be very slow if val and num are both
545 * greater than G_MAXUINT32.
547 * Returns: @val * @num / @denom. In the case of an overflow, this
548 * function returns G_MAXUINT64. If the result is not exactly
549 * representable as an integer, it is rounded to the nearest integer
550 * (half-way cases are rounded up). See also gst_util_uint64_scale(),
551 * gst_util_uint64_scale_ceil(), gst_util_uint64_scale_int(),
552 * gst_util_uint64_scale_int_round(), gst_util_uint64_scale_int_ceil().
555 gst_util_uint64_scale_round (guint64 val, guint64 num, guint64 denom)
557 return _gst_util_uint64_scale (val, num, denom, denom >> 1);
561 * gst_util_uint64_scale_ceil:
562 * @val: the number to scale
563 * @num: the numerator of the scale ratio
564 * @denom: the denominator of the scale ratio
566 * Scale @val by the rational number @num / @denom, avoiding overflows and
567 * underflows and without loss of precision.
569 * This function can potentially be very slow if val and num are both
570 * greater than G_MAXUINT32.
572 * Returns: @val * @num / @denom. In the case of an overflow, this
573 * function returns G_MAXUINT64. If the result is not exactly
574 * representable as an integer, it is rounded up. See also
575 * gst_util_uint64_scale(), gst_util_uint64_scale_round(),
576 * gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
577 * gst_util_uint64_scale_int_ceil().
580 gst_util_uint64_scale_ceil (guint64 val, guint64 num, guint64 denom)
582 return _gst_util_uint64_scale (val, num, denom, denom - 1);
585 /* the guts of the gst_util_uint64_scale_int() variants */
587 _gst_util_uint64_scale_int (guint64 val, gint num, gint denom, gint correct)
589 g_return_val_if_fail (denom > 0, G_MAXUINT64);
590 g_return_val_if_fail (num >= 0, G_MAXUINT64);
592 if (G_UNLIKELY (num == 0))
595 if (G_UNLIKELY (num == denom))
598 if (val <= G_MAXUINT32) {
599 /* simple case. num and denom are not negative so casts are OK. when
600 * not truncating, the additions to the numerator cannot overflow
601 * because val*num <= G_MAXUINT32 * G_MAXINT32 < G_MAXUINT64 -
602 * G_MAXINT32, so there's room to add another gint32. */
603 val *= (guint64) num;
604 /* add rounding correction */
607 return val / (guint64) denom;
609 #if !defined (__x86_64__) && !defined (HAVE_UINT128_T)
610 /* num and denom are not negative so casts are OK */
611 return gst_util_uint64_scale_uint32_unchecked (val, (guint32) num,
612 (guint32) denom, (guint32) correct);
614 /* always use full 128bits scale */
615 return gst_util_uint64_scale_uint64_unchecked (val, num, denom, correct);
620 * gst_util_uint64_scale_int:
621 * @val: guint64 (such as a #GstClockTime) to scale.
622 * @num: numerator of the scale factor.
623 * @denom: denominator of the scale factor.
625 * Scale @val by the rational number @num / @denom, avoiding overflows and
626 * underflows and without loss of precision. @num must be non-negative and
627 * @denom must be positive.
629 * Returns: @val * @num / @denom. In the case of an overflow, this
630 * function returns G_MAXUINT64. If the result is not exactly
631 * representable as an integer, it is truncated. See also
632 * gst_util_uint64_scale_int_round(), gst_util_uint64_scale_int_ceil(),
633 * gst_util_uint64_scale(), gst_util_uint64_scale_round(),
634 * gst_util_uint64_scale_ceil().
637 gst_util_uint64_scale_int (guint64 val, gint num, gint denom)
639 return _gst_util_uint64_scale_int (val, num, denom, 0);
643 * gst_util_uint64_scale_int_round:
644 * @val: guint64 (such as a #GstClockTime) to scale.
645 * @num: numerator of the scale factor.
646 * @denom: denominator of the scale factor.
648 * Scale @val by the rational number @num / @denom, avoiding overflows and
649 * underflows and without loss of precision. @num must be non-negative and
650 * @denom must be positive.
652 * Returns: @val * @num / @denom. In the case of an overflow, this
653 * function returns G_MAXUINT64. If the result is not exactly
654 * representable as an integer, it is rounded to the nearest integer
655 * (half-way cases are rounded up). See also gst_util_uint64_scale_int(),
656 * gst_util_uint64_scale_int_ceil(), gst_util_uint64_scale(),
657 * gst_util_uint64_scale_round(), gst_util_uint64_scale_ceil().
660 gst_util_uint64_scale_int_round (guint64 val, gint num, gint denom)
662 /* we can use a shift to divide by 2 because denom is required to be
664 return _gst_util_uint64_scale_int (val, num, denom, denom >> 1);
668 * gst_util_uint64_scale_int_ceil:
669 * @val: guint64 (such as a #GstClockTime) to scale.
670 * @num: numerator of the scale factor.
671 * @denom: denominator of the scale factor.
673 * Scale @val by the rational number @num / @denom, avoiding overflows and
674 * underflows and without loss of precision. @num must be non-negative and
675 * @denom must be positive.
677 * Returns: @val * @num / @denom. In the case of an overflow, this
678 * function returns G_MAXUINT64. If the result is not exactly
679 * representable as an integer, it is rounded up. See also
680 * gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
681 * gst_util_uint64_scale(), gst_util_uint64_scale_round(),
682 * gst_util_uint64_scale_ceil().
685 gst_util_uint64_scale_int_ceil (guint64 val, gint num, gint denom)
687 return _gst_util_uint64_scale_int (val, num, denom, denom - 1);
691 * gst_util_seqnum_next:
693 * Return a constantly incrementing sequence number.
695 * This function is used internally to GStreamer to be able to determine which
696 * events and messages are "the same". For example, elements may set the seqnum
697 * on a segment-done message to be the same as that of the last seek event, to
698 * indicate that event and the message correspond to the same segment.
700 * Returns: A constantly incrementing 32-bit unsigned integer, which might
701 * overflow back to 0 at some point. Use gst_util_seqnum_compare() to make sure
702 * you handle wraparound correctly.
705 gst_util_seqnum_next (void)
707 static gint counter = 0;
708 return g_atomic_int_add (&counter, 1);
712 * gst_util_seqnum_compare:
713 * @s1: A sequence number.
714 * @s2: Another sequence number.
716 * Compare two sequence numbers, handling wraparound.
718 * The current implementation just returns (gint32)(@s1 - @s2).
720 * Returns: A negative number if @s1 is before @s2, 0 if they are equal, or a
721 * positive number if @s1 is after @s2.
724 gst_util_seqnum_compare (guint32 s1, guint32 s2)
726 return (gint32) (s1 - s2);
729 /* -----------------------------------------------------
731 * The following code will be moved out of the main
732 * gstreamer library someday.
738 * gst_element_create_all_pads:
739 * @element: (transfer none): a #GstElement to create pads for
741 * Creates a pad for each pad template that is always available.
742 * This function is only useful during object initialization of
743 * subclasses of #GstElement.
746 gst_element_create_all_pads (GstElement * element)
750 /* FIXME: lock element */
753 gst_element_class_get_pad_template_list (GST_ELEMENT_CLASS
754 (G_OBJECT_GET_CLASS (element)));
757 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
759 if (padtempl->presence == GST_PAD_ALWAYS) {
762 pad = gst_pad_new_from_template (padtempl, padtempl->name_template);
764 gst_element_add_pad (element, pad);
766 padlist = padlist->next;
771 * gst_element_get_compatible_pad_template:
772 * @element: (transfer none): a #GstElement to get a compatible pad template for
773 * @compattempl: (transfer none): the #GstPadTemplate to find a compatible
776 * Retrieves a pad template from @element that is compatible with @compattempl.
777 * Pads from compatible templates can be linked together.
779 * Returns: (transfer none) (nullable): a compatible #GstPadTemplate,
780 * or %NULL if none was found. No unreferencing is necessary.
783 gst_element_get_compatible_pad_template (GstElement * element,
784 GstPadTemplate * compattempl)
786 GstPadTemplate *newtempl = NULL;
788 GstElementClass *class;
791 g_return_val_if_fail (element != NULL, NULL);
792 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
793 g_return_val_if_fail (compattempl != NULL, NULL);
795 class = GST_ELEMENT_GET_CLASS (element);
797 padlist = gst_element_class_get_pad_template_list (class);
799 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
800 "Looking for a suitable pad template in %s out of %d templates...",
801 GST_ELEMENT_NAME (element), g_list_length (padlist));
804 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
808 * Check direction (must be opposite)
811 GST_CAT_LOG (GST_CAT_CAPS,
812 "checking pad template %s", padtempl->name_template);
813 if (padtempl->direction != compattempl->direction) {
814 GST_CAT_DEBUG (GST_CAT_CAPS,
815 "compatible direction: found %s pad template \"%s\"",
816 padtempl->direction == GST_PAD_SRC ? "src" : "sink",
817 padtempl->name_template);
819 GST_CAT_DEBUG (GST_CAT_CAPS,
820 "intersecting %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (compattempl));
821 GST_CAT_DEBUG (GST_CAT_CAPS,
822 "..and %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (padtempl));
824 compatible = gst_caps_can_intersect (GST_PAD_TEMPLATE_CAPS (compattempl),
825 GST_PAD_TEMPLATE_CAPS (padtempl));
827 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
828 (compatible ? "" : "not "));
836 padlist = g_list_next (padlist);
839 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
840 "Returning new pad template %p", newtempl);
842 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "No compatible pad template found");
848 * gst_element_get_pad_from_template:
849 * @element: (transfer none): a #GstElement.
850 * @templ: (transfer none): a #GstPadTemplate belonging to @element.
852 * Gets a pad from @element described by @templ. If the presence of @templ is
853 * #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
856 * Returns: (transfer full) (nullable): the #GstPad, or %NULL if one
857 * could not be found or created.
860 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
863 GstPadPresence presence;
865 /* If this function is ever exported, we need check the validity of `element'
866 * and `templ', and to make sure the template actually belongs to the
869 presence = GST_PAD_TEMPLATE_PRESENCE (templ);
873 case GST_PAD_SOMETIMES:
874 ret = gst_element_get_static_pad (element, templ->name_template);
875 if (!ret && presence == GST_PAD_ALWAYS)
877 ("Element %s has an ALWAYS template %s, but no pad of the same name",
878 GST_OBJECT_NAME (element), templ->name_template);
881 case GST_PAD_REQUEST:
882 ret = gst_element_request_pad (element, templ, NULL, NULL);
890 * gst_element_request_compatible_pad:
891 * @element: a #GstElement.
892 * @templ: the #GstPadTemplate to which the new pad should be able to link.
894 * Requests a pad from @element. The returned pad should be unlinked and
895 * compatible with @templ. Might return an existing pad, or request a new one.
897 * Returns: (nullable): a #GstPad, or %NULL if one could not be found
901 gst_element_request_compatible_pad (GstElement * element,
902 GstPadTemplate * templ)
904 GstPadTemplate *templ_new;
907 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
908 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
910 /* FIXME: should really loop through the templates, testing each for
911 * compatibility and pad availability. */
912 templ_new = gst_element_get_compatible_pad_template (element, templ);
914 pad = gst_element_get_pad_from_template (element, templ_new);
916 /* This can happen for non-request pads. No need to unref. */
917 if (pad && GST_PAD_PEER (pad))
924 * Checks if the source pad and the sink pad can be linked.
925 * Both @srcpad and @sinkpad must be unlinked and have a parent.
928 gst_pad_check_link (GstPad * srcpad, GstPad * sinkpad)
931 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
932 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
934 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
935 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
937 if (GST_PAD_PEER (srcpad) != NULL) {
938 GST_CAT_INFO (GST_CAT_PADS, "Source pad %s:%s has a peer, failed",
939 GST_DEBUG_PAD_NAME (srcpad));
942 if (GST_PAD_PEER (sinkpad) != NULL) {
943 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has a peer, failed",
944 GST_DEBUG_PAD_NAME (sinkpad));
947 if (!GST_PAD_IS_SRC (srcpad)) {
948 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s is not source pad, failed",
949 GST_DEBUG_PAD_NAME (srcpad));
952 if (!GST_PAD_IS_SINK (sinkpad)) {
953 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s is not sink pad, failed",
954 GST_DEBUG_PAD_NAME (sinkpad));
957 if (GST_PAD_PARENT (srcpad) == NULL) {
958 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s has no parent, failed",
959 GST_DEBUG_PAD_NAME (srcpad));
962 if (GST_PAD_PARENT (sinkpad) == NULL) {
963 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has no parent, failed",
964 GST_DEBUG_PAD_NAME (srcpad));
972 * gst_element_get_compatible_pad:
973 * @element: (transfer none): a #GstElement in which the pad should be found.
974 * @pad: (transfer none): the #GstPad to find a compatible one for.
975 * @caps: (allow-none): the #GstCaps to use as a filter.
977 * Looks for an unlinked pad to which the given pad can link. It is not
978 * guaranteed that linking the pads will work, though it should work in most
981 * This function will first attempt to find a compatible unlinked ALWAYS pad,
982 * and if none can be found, it will request a compatible REQUEST pad by looking
983 * at the templates of @element.
985 * Returns: (transfer full) (nullable): the #GstPad to which a link
986 * can be made, or %NULL if one cannot be found. gst_object_unref()
990 gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
994 GstPadTemplate *templ;
996 GstPad *foundpad = NULL;
998 GValue padptr = { 0, };
1000 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1001 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1003 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1004 "finding pad in %s compatible with %s:%s",
1005 GST_ELEMENT_NAME (element), GST_DEBUG_PAD_NAME (pad));
1007 g_return_val_if_fail (GST_PAD_PEER (pad) == NULL, NULL);
1011 /* try to get an existing unlinked pad */
1012 if (GST_PAD_IS_SRC (pad)) {
1013 pads = gst_element_iterate_sink_pads (element);
1014 } else if (GST_PAD_IS_SINK (pad)) {
1015 pads = gst_element_iterate_src_pads (element);
1017 pads = gst_element_iterate_pads (element);
1021 switch (gst_iterator_next (pads, &padptr)) {
1022 case GST_ITERATOR_OK:
1029 current = g_value_get_object (&padptr);
1031 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
1032 GST_DEBUG_PAD_NAME (current));
1034 if (GST_PAD_IS_SRC (current)) {
1041 peer = gst_pad_get_peer (current);
1043 if (peer == NULL && gst_pad_check_link (srcpad, sinkpad)) {
1044 GstCaps *temp, *intersection;
1045 gboolean compatible;
1047 /* Now check if the two pads' caps are compatible */
1048 temp = gst_pad_query_caps (pad, NULL);
1050 intersection = gst_caps_intersect (temp, caps);
1051 gst_caps_unref (temp);
1053 intersection = temp;
1056 temp = gst_pad_query_caps (current, NULL);
1057 compatible = gst_caps_can_intersect (temp, intersection);
1058 gst_caps_unref (temp);
1059 gst_caps_unref (intersection);
1062 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1063 "found existing unlinked compatible pad %s:%s",
1064 GST_DEBUG_PAD_NAME (current));
1065 gst_iterator_free (pads);
1067 current = gst_object_ref (current);
1068 g_value_unset (&padptr);
1072 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "incompatible pads");
1075 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1076 "already linked or cannot be linked (peer = %p)", peer);
1078 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unreffing pads");
1080 g_value_reset (&padptr);
1082 gst_object_unref (peer);
1085 case GST_ITERATOR_DONE:
1088 case GST_ITERATOR_RESYNC:
1089 gst_iterator_resync (pads);
1091 case GST_ITERATOR_ERROR:
1092 g_assert_not_reached ();
1096 g_value_unset (&padptr);
1097 gst_iterator_free (pads);
1099 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
1100 "Could not find a compatible unlinked always pad to link to %s:%s, now checking request pads",
1101 GST_DEBUG_PAD_NAME (pad));
1103 /* try to create a new one */
1104 /* requesting is a little crazy, we need a template. Let's create one */
1105 templcaps = gst_pad_query_caps (pad, NULL);
1107 GstCaps *inter = gst_caps_intersect (templcaps, caps);
1109 gst_caps_unref (templcaps);
1112 templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
1113 GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
1114 gst_caps_unref (templcaps);
1116 foundpad = gst_element_request_compatible_pad (element, templ);
1117 gst_object_unref (templ);
1120 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1121 "found existing request pad %s:%s", GST_DEBUG_PAD_NAME (foundpad));
1125 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element,
1126 "Could not find a compatible pad to link to %s:%s",
1127 GST_DEBUG_PAD_NAME (pad));
1132 * gst_element_state_get_name:
1133 * @state: a #GstState to get the name of.
1135 * Gets a string representing the given state.
1137 * Returns: (transfer none): a string with the name of the state.
1140 gst_element_state_get_name (GstState state)
1143 case GST_STATE_VOID_PENDING:
1144 return "VOID_PENDING";
1145 case GST_STATE_NULL:
1147 case GST_STATE_READY:
1149 case GST_STATE_PLAYING:
1151 case GST_STATE_PAUSED:
1154 /* This is a memory leak */
1155 return g_strdup_printf ("UNKNOWN!(%d)", state);
1160 * gst_element_state_change_return_get_name:
1161 * @state_ret: a #GstStateChangeReturn to get the name of.
1163 * Gets a string representing the given state change result.
1165 * Returns: (transfer none): a string with the name of the state
1169 gst_element_state_change_return_get_name (GstStateChangeReturn state_ret)
1171 switch (state_ret) {
1172 case GST_STATE_CHANGE_FAILURE:
1174 case GST_STATE_CHANGE_SUCCESS:
1176 case GST_STATE_CHANGE_ASYNC:
1178 case GST_STATE_CHANGE_NO_PREROLL:
1179 return "NO PREROLL";
1181 /* This is a memory leak */
1182 return g_strdup_printf ("UNKNOWN!(%d)", state_ret);
1188 gst_element_factory_can_accept_all_caps_in_direction (GstElementFactory *
1189 factory, const GstCaps * caps, GstPadDirection direction)
1193 g_return_val_if_fail (factory != NULL, FALSE);
1194 g_return_val_if_fail (caps != NULL, FALSE);
1196 templates = factory->staticpadtemplates;
1199 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1201 if (template->direction == direction) {
1202 GstCaps *templcaps = gst_static_caps_get (&template->static_caps);
1204 if (gst_caps_is_always_compatible (caps, templcaps)) {
1205 gst_caps_unref (templcaps);
1208 gst_caps_unref (templcaps);
1210 templates = g_list_next (templates);
1217 gst_element_factory_can_accept_any_caps_in_direction (GstElementFactory *
1218 factory, const GstCaps * caps, GstPadDirection direction)
1222 g_return_val_if_fail (factory != NULL, FALSE);
1223 g_return_val_if_fail (caps != NULL, FALSE);
1225 templates = factory->staticpadtemplates;
1228 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1230 if (template->direction == direction) {
1231 GstCaps *templcaps = gst_static_caps_get (&template->static_caps);
1233 if (gst_caps_can_intersect (caps, templcaps)) {
1234 gst_caps_unref (templcaps);
1237 gst_caps_unref (templcaps);
1239 templates = g_list_next (templates);
1246 * gst_element_factory_can_sink_all_caps:
1247 * @factory: factory to query
1248 * @caps: the caps to check
1250 * Checks if the factory can sink all possible capabilities.
1252 * Returns: %TRUE if the caps are fully compatible.
1255 gst_element_factory_can_sink_all_caps (GstElementFactory * factory,
1256 const GstCaps * caps)
1258 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1263 * gst_element_factory_can_src_all_caps:
1264 * @factory: factory to query
1265 * @caps: the caps to check
1267 * Checks if the factory can src all possible capabilities.
1269 * Returns: %TRUE if the caps are fully compatible.
1272 gst_element_factory_can_src_all_caps (GstElementFactory * factory,
1273 const GstCaps * caps)
1275 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1280 * gst_element_factory_can_sink_any_caps:
1281 * @factory: factory to query
1282 * @caps: the caps to check
1284 * Checks if the factory can sink any possible capability.
1286 * Returns: %TRUE if the caps have a common subset.
1289 gst_element_factory_can_sink_any_caps (GstElementFactory * factory,
1290 const GstCaps * caps)
1292 return gst_element_factory_can_accept_any_caps_in_direction (factory, caps,
1297 * gst_element_factory_can_src_any_caps:
1298 * @factory: factory to query
1299 * @caps: the caps to check
1301 * Checks if the factory can src any possible capability.
1303 * Returns: %TRUE if the caps have a common subset.
1306 gst_element_factory_can_src_any_caps (GstElementFactory * factory,
1307 const GstCaps * caps)
1309 return gst_element_factory_can_accept_any_caps_in_direction (factory, caps,
1313 /* if return val is true, *direct_child is a caller-owned ref on the direct
1314 * child of ancestor that is part of object's ancestry */
1316 object_has_ancestor (GstObject * object, GstObject * ancestor,
1317 GstObject ** direct_child)
1319 GstObject *child, *parent;
1322 *direct_child = NULL;
1324 child = gst_object_ref (object);
1325 parent = gst_object_get_parent (object);
1328 if (ancestor == parent) {
1330 *direct_child = child;
1332 gst_object_unref (child);
1333 gst_object_unref (parent);
1337 gst_object_unref (child);
1339 parent = gst_object_get_parent (parent);
1342 gst_object_unref (child);
1347 /* caller owns return */
1349 find_common_root (GstObject * o1, GstObject * o2)
1351 GstObject *top = o1;
1352 GstObject *kid1, *kid2;
1353 GstObject *root = NULL;
1355 while (GST_OBJECT_PARENT (top))
1356 top = GST_OBJECT_PARENT (top);
1358 /* the itsy-bitsy spider... */
1360 if (!object_has_ancestor (o2, top, &kid2))
1363 root = gst_object_ref (top);
1365 if (!object_has_ancestor (o1, kid2, &kid1)) {
1366 gst_object_unref (kid2);
1370 if (!object_has_ancestor (o2, kid1, &kid2)) {
1371 gst_object_unref (kid1);
1378 /* caller does not own return */
1380 ghost_up (GstElement * e, GstPad * pad)
1382 static gint ghost_pad_index = 0;
1387 GstObject *parent = GST_OBJECT_PARENT (e);
1389 name = g_strdup_printf ("ghost%d", ghost_pad_index++);
1390 gpad = gst_ghost_pad_new (name, pad);
1394 gst_element_get_state (e, ¤t, &next, 0);
1396 if (current > GST_STATE_READY || next == GST_STATE_PAUSED)
1397 gst_pad_set_active (gpad, TRUE);
1399 if (!gst_element_add_pad ((GstElement *) parent, gpad)) {
1400 g_warning ("Pad named %s already exists in element %s\n",
1401 GST_OBJECT_NAME (gpad), GST_OBJECT_NAME (parent));
1402 gst_object_unref ((GstObject *) gpad);
1403 GST_STATE_UNLOCK (e);
1406 GST_STATE_UNLOCK (e);
1412 remove_pad (gpointer ppad, gpointer unused)
1416 if (!gst_element_remove_pad ((GstElement *) GST_OBJECT_PARENT (pad), pad))
1417 g_warning ("Couldn't remove pad %s from element %s",
1418 GST_OBJECT_NAME (pad), GST_OBJECT_NAME (GST_OBJECT_PARENT (pad)));
1422 prepare_link_maybe_ghosting (GstPad ** src, GstPad ** sink,
1423 GSList ** pads_created)
1427 GSList *pads_created_local = NULL;
1429 g_assert (pads_created);
1431 e1 = GST_OBJECT_PARENT (*src);
1432 e2 = GST_OBJECT_PARENT (*sink);
1434 if (G_UNLIKELY (e1 == NULL)) {
1435 GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1436 GST_PTR_FORMAT, *src);
1439 if (G_UNLIKELY (e2 == NULL)) {
1440 GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1441 GST_PTR_FORMAT, *sink);
1445 if (GST_OBJECT_PARENT (e1) == GST_OBJECT_PARENT (e2)) {
1446 GST_CAT_INFO (GST_CAT_PADS, "%s and %s in same bin, no need for ghost pads",
1447 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1451 GST_CAT_INFO (GST_CAT_PADS, "%s and %s not in same bin, making ghost pads",
1452 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1454 /* we need to setup some ghost pads */
1455 root = find_common_root (e1, e2);
1457 if (GST_OBJECT_PARENT (e1) == NULL)
1458 g_warning ("Trying to link elements %s and %s that don't share a common "
1459 "ancestor: %s hasn't been added to a bin or pipeline, but %s is in %s",
1460 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
1461 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
1462 GST_ELEMENT_NAME (GST_OBJECT_PARENT (e2)));
1463 else if (GST_OBJECT_PARENT (e2) == NULL)
1464 g_warning ("Trying to link elements %s and %s that don't share a common "
1465 "ancestor: %s hasn't been added to a bin or pipeline, and %s is in %s",
1466 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
1467 GST_ELEMENT_NAME (e2), GST_ELEMENT_NAME (e1),
1468 GST_ELEMENT_NAME (GST_OBJECT_PARENT (e1)));
1470 g_warning ("Trying to link elements %s and %s that don't share a common "
1471 "ancestor: %s is in %s, and %s is in %s",
1472 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2),
1473 GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (GST_OBJECT_PARENT (e1)),
1474 GST_ELEMENT_NAME (e2), GST_ELEMENT_NAME (GST_OBJECT_PARENT (e2)));
1478 while (GST_OBJECT_PARENT (e1) != root) {
1479 *src = ghost_up ((GstElement *) e1, *src);
1482 e1 = GST_OBJECT_PARENT (*src);
1483 pads_created_local = g_slist_prepend (pads_created_local, *src);
1485 while (GST_OBJECT_PARENT (e2) != root) {
1486 *sink = ghost_up ((GstElement *) e2, *sink);
1489 e2 = GST_OBJECT_PARENT (*sink);
1490 pads_created_local = g_slist_prepend (pads_created_local, *sink);
1493 gst_object_unref (root);
1494 *pads_created = g_slist_concat (*pads_created, pads_created_local);
1498 gst_object_unref (root);
1499 g_slist_foreach (pads_created_local, remove_pad, NULL);
1500 g_slist_free (pads_created_local);
1505 pad_link_maybe_ghosting (GstPad * src, GstPad * sink, GstPadLinkCheck flags)
1507 GSList *pads_created = NULL;
1510 if (!prepare_link_maybe_ghosting (&src, &sink, &pads_created)) {
1513 ret = (gst_pad_link_full (src, sink, flags) == GST_PAD_LINK_OK);
1517 g_slist_foreach (pads_created, remove_pad, NULL);
1519 g_slist_free (pads_created);
1525 release_and_unref_pad (GstElement * element, GstPad * pad, gboolean requestpad)
1529 gst_element_release_request_pad (element, pad);
1530 gst_object_unref (pad);
1535 * gst_element_link_pads_full:
1536 * @src: a #GstElement containing the source pad.
1537 * @srcpadname: (allow-none): the name of the #GstPad in source element
1538 * or %NULL for any pad.
1539 * @dest: (transfer none): the #GstElement containing the destination pad.
1540 * @destpadname: (allow-none): the name of the #GstPad in destination element,
1541 * or %NULL for any pad.
1542 * @flags: the #GstPadLinkCheck to be performed when linking pads.
1544 * Links the two named pads of the source and destination elements.
1545 * Side effect is that if one of the pads has no parent, it becomes a
1546 * child of the parent of the other element. If they have different
1547 * parents, the link fails.
1549 * Calling gst_element_link_pads_full() with @flags == %GST_PAD_LINK_CHECK_DEFAULT
1550 * is the same as calling gst_element_link_pads() and the recommended way of
1551 * linking pads with safety checks applied.
1553 * This is a convenience function for gst_pad_link_full().
1555 * Returns: %TRUE if the pads could be linked, %FALSE otherwise.
1558 gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
1559 GstElement * dest, const gchar * destpadname, GstPadLinkCheck flags)
1561 const GList *srcpads, *destpads, *srctempls, *desttempls, *l;
1562 GstPad *srcpad, *destpad;
1563 GstPadTemplate *srctempl, *desttempl;
1564 GstElementClass *srcclass, *destclass;
1565 gboolean srcrequest, destrequest;
1568 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1569 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1571 GST_CAT_INFO (GST_CAT_ELEMENT_PADS,
1572 "trying to link element %s:%s to element %s:%s", GST_ELEMENT_NAME (src),
1573 srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
1574 destpadname ? destpadname : "(any)");
1577 destrequest = FALSE;
1581 /* name specified, look it up */
1582 if (!(srcpad = gst_element_get_static_pad (src, srcpadname))) {
1583 if ((srcpad = gst_element_get_request_pad (src, srcpadname)))
1587 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1588 GST_ELEMENT_NAME (src), srcpadname);
1591 if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
1592 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
1593 GST_DEBUG_PAD_NAME (srcpad));
1594 release_and_unref_pad (src, srcpad, srcrequest);
1597 if (GST_PAD_PEER (srcpad) != NULL) {
1598 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1599 "pad %s:%s is already linked to %s:%s", GST_DEBUG_PAD_NAME (srcpad),
1600 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1601 /* already linked request pads look like static pads, so the request pad
1602 * was never requested a second time above, so no need to release it */
1603 gst_object_unref (srcpad);
1609 /* no name given, get the first available pad */
1610 GST_OBJECT_LOCK (src);
1611 srcpads = GST_ELEMENT_PADS (src);
1612 srcpad = srcpads ? GST_PAD_CAST (srcpads->data) : NULL;
1614 gst_object_ref (srcpad);
1615 GST_OBJECT_UNLOCK (src);
1618 /* get a destination pad */
1620 /* name specified, look it up */
1621 if (!(destpad = gst_element_get_static_pad (dest, destpadname))) {
1622 if ((destpad = gst_element_get_request_pad (dest, destpadname)))
1626 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1627 GST_ELEMENT_NAME (dest), destpadname);
1628 release_and_unref_pad (src, srcpad, srcrequest);
1631 if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
1632 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
1633 GST_DEBUG_PAD_NAME (destpad));
1634 release_and_unref_pad (src, srcpad, srcrequest);
1635 release_and_unref_pad (dest, destpad, destrequest);
1638 if (GST_PAD_PEER (destpad) != NULL) {
1639 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1640 "pad %s:%s is already linked to %s:%s",
1641 GST_DEBUG_PAD_NAME (destpad),
1642 GST_DEBUG_PAD_NAME (GST_PAD_PEER (destpad)));
1643 release_and_unref_pad (src, srcpad, srcrequest);
1644 /* already linked request pads look like static pads, so the request pad
1645 * was never requested a second time above, so no need to release it */
1646 gst_object_unref (destpad);
1652 /* no name given, get the first available pad */
1653 GST_OBJECT_LOCK (dest);
1654 destpads = GST_ELEMENT_PADS (dest);
1655 destpad = destpads ? GST_PAD_CAST (destpads->data) : NULL;
1657 gst_object_ref (destpad);
1658 GST_OBJECT_UNLOCK (dest);
1661 if (srcpadname && destpadname) {
1664 /* two explicitly specified pads */
1665 result = pad_link_maybe_ghosting (srcpad, destpad, flags);
1668 gst_object_unref (srcpad);
1669 gst_object_unref (destpad);
1671 release_and_unref_pad (src, srcpad, srcrequest);
1672 release_and_unref_pad (dest, destpad, destrequest);
1678 /* loop through the allowed pads in the source, trying to find a
1679 * compatible destination pad */
1680 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1681 "looping through allowed src and dest pads");
1683 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying src pad %s:%s",
1684 GST_DEBUG_PAD_NAME (srcpad));
1685 if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
1686 (GST_PAD_PEER (srcpad) == NULL)) {
1687 gboolean temprequest = FALSE;
1692 gst_object_ref (temp);
1694 temp = gst_element_get_compatible_pad (dest, srcpad, NULL);
1695 if (temp && GST_PAD_PAD_TEMPLATE (temp)
1696 && GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (temp)) ==
1702 if (temp && pad_link_maybe_ghosting (srcpad, temp, flags)) {
1703 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1704 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (temp));
1706 gst_object_unref (destpad);
1707 gst_object_unref (srcpad);
1708 gst_object_unref (temp);
1714 gst_element_release_request_pad (dest, temp);
1715 gst_object_unref (temp);
1718 /* find a better way for this mess */
1720 srcpads = g_list_next (srcpads);
1722 gst_object_unref (srcpad);
1723 srcpad = GST_PAD_CAST (srcpads->data);
1724 gst_object_ref (srcpad);
1730 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
1731 GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
1732 /* no need to release any request pad as both src- and destpadname must be
1733 * set to end up here, but this case has already been taken care of above */
1735 gst_object_unref (destpad);
1739 release_and_unref_pad (src, srcpad, srcrequest);
1744 /* loop through the existing pads in the destination */
1746 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying dest pad %s:%s",
1747 GST_DEBUG_PAD_NAME (destpad));
1748 if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
1749 (GST_PAD_PEER (destpad) == NULL)) {
1750 GstPad *temp = gst_element_get_compatible_pad (src, destpad, NULL);
1751 gboolean temprequest = FALSE;
1753 if (temp && GST_PAD_PAD_TEMPLATE (temp)
1754 && GST_PAD_TEMPLATE_PRESENCE (GST_PAD_PAD_TEMPLATE (temp)) ==
1759 if (temp && pad_link_maybe_ghosting (temp, destpad, flags)) {
1760 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1761 GST_DEBUG_PAD_NAME (temp), GST_DEBUG_PAD_NAME (destpad));
1762 gst_object_unref (temp);
1763 gst_object_unref (destpad);
1767 release_and_unref_pad (src, temp, temprequest);
1770 destpads = g_list_next (destpads);
1772 gst_object_unref (destpad);
1773 destpad = GST_PAD_CAST (destpads->data);
1774 gst_object_ref (destpad);
1781 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
1782 GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
1783 release_and_unref_pad (dest, destpad, destrequest);
1786 /* no need to release any request pad as the case of unset destpatname and
1787 * destpad being a requst pad has already been taken care of when looking
1788 * though the destination pads above */
1790 gst_object_unref (destpad);
1795 srcclass = GST_ELEMENT_GET_CLASS (src);
1796 destclass = GST_ELEMENT_GET_CLASS (dest);
1798 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1799 "we might have request pads on both sides, checking...");
1800 srctempls = gst_element_class_get_pad_template_list (srcclass);
1801 desttempls = gst_element_class_get_pad_template_list (destclass);
1803 if (srctempls && desttempls) {
1805 srctempl = (GstPadTemplate *) srctempls->data;
1806 if (srctempl->presence == GST_PAD_REQUEST) {
1807 for (l = desttempls; l; l = l->next) {
1808 desttempl = (GstPadTemplate *) l->data;
1809 if (desttempl->presence == GST_PAD_REQUEST &&
1810 desttempl->direction != srctempl->direction) {
1811 GstCaps *srccaps, *destcaps;
1813 srccaps = gst_pad_template_get_caps (srctempl);
1814 destcaps = gst_pad_template_get_caps (desttempl);
1815 if (gst_caps_is_always_compatible (srccaps, destcaps)) {
1817 gst_element_request_pad (src, srctempl,
1818 srctempl->name_template, NULL);
1820 gst_element_request_pad (dest, desttempl,
1821 desttempl->name_template, NULL);
1822 if (srcpad && destpad
1823 && pad_link_maybe_ghosting (srcpad, destpad, flags)) {
1824 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1825 "linked pad %s:%s to pad %s:%s",
1826 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
1827 gst_object_unref (srcpad);
1828 gst_object_unref (destpad);
1829 gst_caps_unref (srccaps);
1830 gst_caps_unref (destcaps);
1833 /* it failed, so we release the request pads */
1835 gst_element_release_request_pad (src, srcpad);
1836 gst_object_unref (srcpad);
1839 gst_element_release_request_pad (dest, destpad);
1840 gst_object_unref (destpad);
1843 gst_caps_unref (srccaps);
1844 gst_caps_unref (destcaps);
1848 srctempls = srctempls->next;
1852 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s",
1853 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1858 * gst_element_link_pads:
1859 * @src: a #GstElement containing the source pad.
1860 * @srcpadname: (allow-none): the name of the #GstPad in source element
1861 * or %NULL for any pad.
1862 * @dest: (transfer none): the #GstElement containing the destination pad.
1863 * @destpadname: (allow-none): the name of the #GstPad in destination element,
1864 * or %NULL for any pad.
1866 * Links the two named pads of the source and destination elements.
1867 * Side effect is that if one of the pads has no parent, it becomes a
1868 * child of the parent of the other element. If they have different
1869 * parents, the link fails.
1871 * Returns: %TRUE if the pads could be linked, %FALSE otherwise.
1874 gst_element_link_pads (GstElement * src, const gchar * srcpadname,
1875 GstElement * dest, const gchar * destpadname)
1877 return gst_element_link_pads_full (src, srcpadname, dest, destpadname,
1878 GST_PAD_LINK_CHECK_DEFAULT);
1882 * gst_element_link_pads_filtered:
1883 * @src: a #GstElement containing the source pad.
1884 * @srcpadname: (allow-none): the name of the #GstPad in source element
1885 * or %NULL for any pad.
1886 * @dest: (transfer none): the #GstElement containing the destination pad.
1887 * @destpadname: (allow-none): the name of the #GstPad in destination element
1888 * or %NULL for any pad.
1889 * @filter: (transfer none) (allow-none): the #GstCaps to filter the link,
1890 * or %NULL for no filter.
1892 * Links the two named pads of the source and destination elements. Side effect
1893 * is that if one of the pads has no parent, it becomes a child of the parent of
1894 * the other element. If they have different parents, the link fails. If @caps
1895 * is not %NULL, makes sure that the caps of the link is a subset of @caps.
1897 * Returns: %TRUE if the pads could be linked, %FALSE otherwise.
1900 gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
1901 GstElement * dest, const gchar * destpadname, GstCaps * filter)
1904 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1905 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1906 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), FALSE);
1909 GstElement *capsfilter;
1911 GstState state, pending;
1914 capsfilter = gst_element_factory_make ("capsfilter", NULL);
1916 GST_ERROR ("Could not make a capsfilter");
1920 parent = gst_object_get_parent (GST_OBJECT (src));
1921 g_return_val_if_fail (GST_IS_BIN (parent), FALSE);
1923 gst_element_get_state (GST_ELEMENT_CAST (parent), &state, &pending, 0);
1925 if (!gst_bin_add (GST_BIN (parent), capsfilter)) {
1926 GST_ERROR ("Could not add capsfilter");
1927 gst_object_unref (capsfilter);
1928 gst_object_unref (parent);
1932 if (pending != GST_STATE_VOID_PENDING)
1935 gst_element_set_state (capsfilter, state);
1937 gst_object_unref (parent);
1939 g_object_set (capsfilter, "caps", filter, NULL);
1941 lr1 = gst_element_link_pads (src, srcpadname, capsfilter, "sink");
1942 lr2 = gst_element_link_pads (capsfilter, "src", dest, destpadname);
1947 GST_INFO ("Could not link pads: %s:%s - capsfilter:sink",
1948 GST_ELEMENT_NAME (src), srcpadname);
1950 GST_INFO ("Could not link pads: capsfilter:src - %s:%s",
1951 GST_ELEMENT_NAME (dest), destpadname);
1953 gst_element_set_state (capsfilter, GST_STATE_NULL);
1954 /* this will unlink and unref as appropriate */
1955 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (capsfilter)), capsfilter);
1959 if (gst_element_link_pads (src, srcpadname, dest, destpadname)) {
1962 GST_INFO ("Could not link pads: %s:%s - %s:%s", GST_ELEMENT_NAME (src),
1963 srcpadname, GST_ELEMENT_NAME (dest), destpadname);
1971 * @src: (transfer none): a #GstElement containing the source pad.
1972 * @dest: (transfer none): the #GstElement containing the destination pad.
1974 * Links @src to @dest. The link must be from source to
1975 * destination; the other direction will not be tried. The function looks for
1976 * existing pads that aren't linked yet. It will request new pads if necessary.
1977 * Such pads need to be released manually when unlinking.
1978 * If multiple links are possible, only one is established.
1980 * Make sure you have added your elements to a bin or pipeline with
1981 * gst_bin_add() before trying to link them.
1983 * Returns: %TRUE if the elements could be linked, %FALSE otherwise.
1986 gst_element_link (GstElement * src, GstElement * dest)
1988 return gst_element_link_pads (src, NULL, dest, NULL);
1992 * gst_element_link_many:
1993 * @element_1: (transfer none): the first #GstElement in the link chain.
1994 * @element_2: (transfer none): the second #GstElement in the link chain.
1995 * @...: the %NULL-terminated list of elements to link in order.
1997 * Chain together a series of elements. Uses gst_element_link().
1998 * Make sure you have added your elements to a bin or pipeline with
1999 * gst_bin_add() before trying to link them.
2001 * Returns: %TRUE on success, %FALSE otherwise.
2004 gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
2006 gboolean res = TRUE;
2009 g_return_val_if_fail (GST_IS_ELEMENT (element_1), FALSE);
2010 g_return_val_if_fail (GST_IS_ELEMENT (element_2), FALSE);
2012 va_start (args, element_2);
2015 if (!gst_element_link (element_1, element_2)) {
2020 element_1 = element_2;
2021 element_2 = va_arg (args, GstElement *);
2030 * gst_element_link_filtered:
2031 * @src: a #GstElement containing the source pad.
2032 * @dest: (transfer none): the #GstElement containing the destination pad.
2033 * @filter: (transfer none) (allow-none): the #GstCaps to filter the link,
2034 * or %NULL for no filter.
2036 * Links @src to @dest using the given caps as filtercaps.
2037 * The link must be from source to
2038 * destination; the other direction will not be tried. The function looks for
2039 * existing pads that aren't linked yet. It will request new pads if necessary.
2040 * If multiple links are possible, only one is established.
2042 * Make sure you have added your elements to a bin or pipeline with
2043 * gst_bin_add() before trying to link them.
2045 * Returns: %TRUE if the pads could be linked, %FALSE otherwise.
2048 gst_element_link_filtered (GstElement * src, GstElement * dest,
2051 return gst_element_link_pads_filtered (src, NULL, dest, NULL, filter);
2055 * gst_element_unlink_pads:
2056 * @src: a (transfer none): #GstElement containing the source pad.
2057 * @srcpadname: the name of the #GstPad in source element.
2058 * @dest: (transfer none): a #GstElement containing the destination pad.
2059 * @destpadname: the name of the #GstPad in destination element.
2061 * Unlinks the two named pads of the source and destination elements.
2063 * This is a convenience function for gst_pad_unlink().
2066 gst_element_unlink_pads (GstElement * src, const gchar * srcpadname,
2067 GstElement * dest, const gchar * destpadname)
2069 GstPad *srcpad, *destpad;
2070 gboolean srcrequest, destrequest;
2072 srcrequest = destrequest = FALSE;
2074 g_return_if_fail (src != NULL);
2075 g_return_if_fail (GST_IS_ELEMENT (src));
2076 g_return_if_fail (srcpadname != NULL);
2077 g_return_if_fail (dest != NULL);
2078 g_return_if_fail (GST_IS_ELEMENT (dest));
2079 g_return_if_fail (destpadname != NULL);
2081 /* obtain the pads requested */
2082 if (!(srcpad = gst_element_get_static_pad (src, srcpadname)))
2083 if ((srcpad = gst_element_get_request_pad (src, srcpadname)))
2085 if (srcpad == NULL) {
2086 GST_WARNING_OBJECT (src, "source element has no pad \"%s\"", srcpadname);
2089 if (!(destpad = gst_element_get_static_pad (dest, destpadname)))
2090 if ((destpad = gst_element_get_request_pad (dest, destpadname)))
2092 if (destpad == NULL) {
2093 GST_WARNING_OBJECT (dest, "destination element has no pad \"%s\"",
2098 /* we're satisfied they can be unlinked, let's do it */
2099 gst_pad_unlink (srcpad, destpad);
2102 gst_element_release_request_pad (dest, destpad);
2103 gst_object_unref (destpad);
2107 gst_element_release_request_pad (src, srcpad);
2108 gst_object_unref (srcpad);
2112 * gst_element_unlink_many:
2113 * @element_1: (transfer none): the first #GstElement in the link chain.
2114 * @element_2: (transfer none): the second #GstElement in the link chain.
2115 * @...: the %NULL-terminated list of elements to unlink in order.
2117 * Unlinks a series of elements. Uses gst_element_unlink().
2120 gst_element_unlink_many (GstElement * element_1, GstElement * element_2, ...)
2124 g_return_if_fail (element_1 != NULL && element_2 != NULL);
2125 g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
2127 va_start (args, element_2);
2130 gst_element_unlink (element_1, element_2);
2132 element_1 = element_2;
2133 element_2 = va_arg (args, GstElement *);
2140 * gst_element_unlink:
2141 * @src: (transfer none): the source #GstElement to unlink.
2142 * @dest: (transfer none): the sink #GstElement to unlink.
2144 * Unlinks all source pads of the source element with all sink pads
2145 * of the sink element to which they are linked.
2147 * If the link has been made using gst_element_link(), it could have created an
2148 * requestpad, which has to be released using gst_element_release_request_pad().
2151 gst_element_unlink (GstElement * src, GstElement * dest)
2154 gboolean done = FALSE;
2155 GValue data = { 0, };
2157 g_return_if_fail (GST_IS_ELEMENT (src));
2158 g_return_if_fail (GST_IS_ELEMENT (dest));
2160 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unlinking \"%s\" and \"%s\"",
2161 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
2163 pads = gst_element_iterate_pads (src);
2165 switch (gst_iterator_next (pads, &data)) {
2166 case GST_ITERATOR_OK:
2168 GstPad *pad = g_value_get_object (&data);
2170 if (GST_PAD_IS_SRC (pad)) {
2171 GstPad *peerpad = gst_pad_get_peer (pad);
2173 /* see if the pad is linked and is really a pad of dest */
2175 GstElement *peerelem;
2177 peerelem = gst_pad_get_parent_element (peerpad);
2179 if (peerelem == dest) {
2180 gst_pad_unlink (pad, peerpad);
2183 gst_object_unref (peerelem);
2185 gst_object_unref (peerpad);
2188 g_value_reset (&data);
2191 case GST_ITERATOR_RESYNC:
2192 gst_iterator_resync (pads);
2194 case GST_ITERATOR_DONE:
2198 g_assert_not_reached ();
2202 g_value_unset (&data);
2203 gst_iterator_free (pads);
2207 * gst_element_query_position:
2208 * @element: a #GstElement to invoke the position query on.
2209 * @format: the #GstFormat requested
2210 * @cur: (out) (allow-none): a location in which to store the current
2211 * position, or %NULL.
2213 * Queries an element (usually top-level pipeline or playbin element) for the
2214 * stream position in nanoseconds. This will be a value between 0 and the
2215 * stream duration (if the stream duration is known). This query will usually
2216 * only work once the pipeline is prerolled (i.e. reached PAUSED or PLAYING
2217 * state). The application will receive an ASYNC_DONE message on the pipeline
2218 * bus when that is the case.
2220 * If one repeatedly calls this function one can also create a query and reuse
2221 * it in gst_element_query().
2223 * Returns: %TRUE if the query could be performed.
2226 gst_element_query_position (GstElement * element, GstFormat format,
2232 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2233 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2235 query = gst_query_new_position (format);
2236 ret = gst_element_query (element, query);
2239 gst_query_parse_position (query, NULL, cur);
2241 gst_query_unref (query);
2247 * gst_element_query_duration:
2248 * @element: a #GstElement to invoke the duration query on.
2249 * @format: the #GstFormat requested
2250 * @duration: (out) (allow-none): A location in which to store the total duration, or %NULL.
2252 * Queries an element (usually top-level pipeline or playbin element) for the
2253 * total stream duration in nanoseconds. This query will only work once the
2254 * pipeline is prerolled (i.e. reached PAUSED or PLAYING state). The application
2255 * will receive an ASYNC_DONE message on the pipeline bus when that is the case.
2257 * If the duration changes for some reason, you will get a DURATION_CHANGED
2258 * message on the pipeline bus, in which case you should re-query the duration
2259 * using this function.
2261 * Returns: %TRUE if the query could be performed.
2264 gst_element_query_duration (GstElement * element, GstFormat format,
2270 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2271 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2273 query = gst_query_new_duration (format);
2274 ret = gst_element_query (element, query);
2277 gst_query_parse_duration (query, NULL, duration);
2279 gst_query_unref (query);
2285 * gst_element_query_convert:
2286 * @element: a #GstElement to invoke the convert query on.
2287 * @src_format: (inout): a #GstFormat to convert from.
2288 * @src_val: a value to convert.
2289 * @dest_format: the #GstFormat to convert to.
2290 * @dest_val: (out): a pointer to the result.
2292 * Queries an element to convert @src_val in @src_format to @dest_format.
2294 * Returns: %TRUE if the query could be performed.
2297 gst_element_query_convert (GstElement * element, GstFormat src_format,
2298 gint64 src_val, GstFormat dest_format, gint64 * dest_val)
2303 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2304 g_return_val_if_fail (dest_format != GST_FORMAT_UNDEFINED, FALSE);
2305 g_return_val_if_fail (dest_val != NULL, FALSE);
2307 if (dest_format == src_format || src_val == -1) {
2308 *dest_val = src_val;
2312 query = gst_query_new_convert (src_format, src_val, dest_format);
2313 ret = gst_element_query (element, query);
2316 gst_query_parse_convert (query, NULL, NULL, NULL, dest_val);
2318 gst_query_unref (query);
2324 * gst_element_seek_simple:
2325 * @element: a #GstElement to seek on
2326 * @format: a #GstFormat to execute the seek in, such as #GST_FORMAT_TIME
2327 * @seek_flags: seek options; playback applications will usually want to use
2328 * GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT here
2329 * @seek_pos: position to seek to (relative to the start); if you are doing
2330 * a seek in #GST_FORMAT_TIME this value is in nanoseconds -
2331 * multiply with #GST_SECOND to convert seconds to nanoseconds or
2332 * with #GST_MSECOND to convert milliseconds to nanoseconds.
2334 * Simple API to perform a seek on the given element, meaning it just seeks
2335 * to the given position relative to the start of the stream. For more complex
2336 * operations like segment seeks (e.g. for looping) or changing the playback
2337 * rate or seeking relative to the last configured playback segment you should
2338 * use gst_element_seek().
2340 * In a completely prerolled PAUSED or PLAYING pipeline, seeking is always
2341 * guaranteed to return %TRUE on a seekable media type or %FALSE when the media
2342 * type is certainly not seekable (such as a live stream).
2344 * Some elements allow for seeking in the READY state, in this
2345 * case they will store the seek event and execute it when they are put to
2346 * PAUSED. If the element supports seek in READY, it will always return %TRUE when
2347 * it receives the event in the READY state.
2349 * Returns: %TRUE if the seek operation succeeded. Flushing seeks will trigger a
2350 * preroll, which will emit %GST_MESSAGE_ASYNC_DONE.
2353 gst_element_seek_simple (GstElement * element, GstFormat format,
2354 GstSeekFlags seek_flags, gint64 seek_pos)
2356 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2357 g_return_val_if_fail (seek_pos >= 0, FALSE);
2359 return gst_element_seek (element, 1.0, format, seek_flags,
2360 GST_SEEK_TYPE_SET, seek_pos, GST_SEEK_TYPE_NONE, 0);
2364 * gst_pad_use_fixed_caps:
2365 * @pad: the pad to use
2367 * A helper function you can use that sets the FIXED_CAPS flag
2368 * This way the default CAPS query will always return the negotiated caps
2369 * or in case the pad is not negotiated, the padtemplate caps.
2371 * The negotiated caps are the caps of the last CAPS event that passed on the
2372 * pad. Use this function on a pad that, once it negotiated to a CAPS, cannot
2373 * be renegotiated to something else.
2376 gst_pad_use_fixed_caps (GstPad * pad)
2378 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_FIXED_CAPS);
2382 * gst_pad_get_parent_element:
2385 * Gets the parent of @pad, cast to a #GstElement. If a @pad has no parent or
2386 * its parent is not an element, return %NULL.
2388 * Returns: (transfer full) (nullable): the parent of the pad. The
2389 * caller has a reference on the parent, so unref when you're finished
2395 gst_pad_get_parent_element (GstPad * pad)
2399 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2401 p = gst_object_get_parent (GST_OBJECT_CAST (pad));
2403 if (p && !GST_IS_ELEMENT (p)) {
2404 gst_object_unref (p);
2407 return GST_ELEMENT_CAST (p);
2411 * gst_object_default_error:
2412 * @source: the #GstObject that initiated the error.
2413 * @error: (in): the GError.
2414 * @debug: (in) (allow-none): an additional debug information string, or %NULL
2416 * A default error function that uses g_printerr() to display the error message
2417 * and the optional debug sting..
2419 * The default handler will simply print the error string using g_print.
2422 gst_object_default_error (GstObject * source, const GError * error,
2423 const gchar * debug)
2425 gchar *name = gst_object_get_path_string (source);
2427 g_printerr (_("ERROR: from element %s: %s\n"), name, error->message);
2429 g_printerr (_("Additional debug info:\n%s\n"), debug);
2437 * @element_1: (transfer full): the #GstElement element to add to the bin
2438 * @...: (transfer full): additional elements to add to the bin
2440 * Adds a %NULL-terminated list of elements to a bin. This function is
2441 * equivalent to calling gst_bin_add() for each member of the list. The return
2442 * value of each gst_bin_add() is ignored.
2445 gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
2449 g_return_if_fail (GST_IS_BIN (bin));
2450 g_return_if_fail (GST_IS_ELEMENT (element_1));
2452 va_start (args, element_1);
2455 gst_bin_add (bin, element_1);
2457 element_1 = va_arg (args, GstElement *);
2464 * gst_bin_remove_many:
2466 * @element_1: (transfer none): the first #GstElement to remove from the bin
2467 * @...: (transfer none): %NULL-terminated list of elements to remove from the bin
2469 * Remove a list of elements from a bin. This function is equivalent
2470 * to calling gst_bin_remove() with each member of the list.
2473 gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
2477 g_return_if_fail (GST_IS_BIN (bin));
2478 g_return_if_fail (GST_IS_ELEMENT (element_1));
2480 va_start (args, element_1);
2483 gst_bin_remove (bin, element_1);
2485 element_1 = va_arg (args, GstElement *);
2495 } QueryAcceptCapsData;
2498 query_accept_caps_func (GstPad * pad, QueryAcceptCapsData * data)
2500 if (G_LIKELY (gst_pad_peer_query (pad, data->query))) {
2503 gst_query_parse_accept_caps_result (data->query, &result);
2504 data->ret &= result;
2510 * gst_pad_proxy_query_accept_caps:
2511 * @pad: a #GstPad to proxy.
2512 * @query: an ACCEPT_CAPS #GstQuery.
2514 * Checks if all internally linked pads of @pad accepts the caps in @query and
2515 * returns the intersection of the results.
2517 * This function is useful as a default accept caps query function for an element
2518 * that can handle any stream format, but requires caps that are acceptable for
2519 * all opposite pads.
2521 * Returns: %TRUE if @query could be executed
2524 gst_pad_proxy_query_accept_caps (GstPad * pad, GstQuery * query)
2526 QueryAcceptCapsData data;
2528 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2529 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2530 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS, FALSE);
2532 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
2533 "proxying accept caps query for %s:%s", GST_DEBUG_PAD_NAME (pad));
2536 /* value to hold the return, by default it holds TRUE */
2537 /* FIXME: TRUE is wrong when there are no pads */
2540 gst_pad_forward (pad, (GstPadForwardFunction) query_accept_caps_func, &data);
2541 gst_query_set_accept_caps_result (query, data.ret);
2543 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "proxying accept caps query: %d",
2556 query_caps_func (GstPad * pad, QueryCapsData * data)
2558 gboolean empty = FALSE;
2560 if (G_LIKELY (gst_pad_peer_query (pad, data->query))) {
2561 GstCaps *peercaps, *intersection;
2563 gst_query_parse_caps_result (data->query, &peercaps);
2564 GST_DEBUG_OBJECT (pad, "intersect with result %" GST_PTR_FORMAT, peercaps);
2565 intersection = gst_caps_intersect (data->ret, peercaps);
2566 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, intersection);
2568 gst_caps_unref (data->ret);
2569 data->ret = intersection;
2571 /* stop when empty */
2572 empty = gst_caps_is_empty (intersection);
2578 * gst_pad_proxy_query_caps:
2579 * @pad: a #GstPad to proxy.
2580 * @query: a CAPS #GstQuery.
2582 * Calls gst_pad_query_caps() for all internally linked pads of @pad and returns
2583 * the intersection of the results.
2585 * This function is useful as a default caps query function for an element
2586 * that can handle any stream format, but requires all its pads to have
2587 * the same caps. Two such elements are tee and adder.
2589 * Returns: %TRUE if @query could be executed
2592 gst_pad_proxy_query_caps (GstPad * pad, GstQuery * query)
2594 GstCaps *filter, *templ, *result;
2597 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2598 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2599 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS, FALSE);
2601 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "proxying caps query for %s:%s",
2602 GST_DEBUG_PAD_NAME (pad));
2606 /* value to hold the return, by default it holds the filter or ANY */
2607 gst_query_parse_caps (query, &filter);
2608 data.ret = filter ? gst_caps_ref (filter) : gst_caps_new_any ();
2610 gst_pad_forward (pad, (GstPadForwardFunction) query_caps_func, &data);
2612 templ = gst_pad_get_pad_template_caps (pad);
2613 result = gst_caps_intersect (data.ret, templ);
2614 gst_caps_unref (data.ret);
2615 gst_caps_unref (templ);
2617 gst_query_set_caps_result (query, result);
2618 gst_caps_unref (result);
2620 /* FIXME: return something depending on the processing */
2625 * gst_pad_query_position:
2626 * @pad: a #GstPad to invoke the position query on.
2627 * @format: the #GstFormat requested
2628 * @cur: (out) (allow-none): A location in which to store the current position, or %NULL.
2630 * Queries a pad for the stream position.
2632 * Returns: %TRUE if the query could be performed.
2635 gst_pad_query_position (GstPad * pad, GstFormat format, gint64 * cur)
2640 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2641 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2643 query = gst_query_new_position (format);
2644 if ((ret = gst_pad_query (pad, query)))
2645 gst_query_parse_position (query, NULL, cur);
2646 gst_query_unref (query);
2652 * gst_pad_peer_query_position:
2653 * @pad: a #GstPad on whose peer to invoke the position query on.
2654 * Must be a sink pad.
2655 * @format: the #GstFormat requested
2656 * @cur: (out) (allow-none): a location in which to store the current
2657 * position, or %NULL.
2659 * Queries the peer of a given sink pad for the stream position.
2661 * Returns: %TRUE if the query could be performed.
2664 gst_pad_peer_query_position (GstPad * pad, GstFormat format, gint64 * cur)
2667 gboolean ret = FALSE;
2669 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2670 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2672 query = gst_query_new_position (format);
2673 if ((ret = gst_pad_peer_query (pad, query)))
2674 gst_query_parse_position (query, NULL, cur);
2675 gst_query_unref (query);
2681 * gst_pad_query_duration:
2682 * @pad: a #GstPad to invoke the duration query on.
2683 * @format: the #GstFormat requested
2684 * @duration: (out) (allow-none): a location in which to store the total
2685 * duration, or %NULL.
2687 * Queries a pad for the total stream duration.
2689 * Returns: %TRUE if the query could be performed.
2692 gst_pad_query_duration (GstPad * pad, GstFormat format, gint64 * duration)
2697 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2698 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2700 query = gst_query_new_duration (format);
2701 if ((ret = gst_pad_query (pad, query)))
2702 gst_query_parse_duration (query, NULL, duration);
2703 gst_query_unref (query);
2709 * gst_pad_peer_query_duration:
2710 * @pad: a #GstPad on whose peer pad to invoke the duration query on.
2711 * Must be a sink pad.
2712 * @format: the #GstFormat requested
2713 * @duration: (out) (allow-none): a location in which to store the total
2714 * duration, or %NULL.
2716 * Queries the peer pad of a given sink pad for the total stream duration.
2718 * Returns: %TRUE if the query could be performed.
2721 gst_pad_peer_query_duration (GstPad * pad, GstFormat format, gint64 * duration)
2724 gboolean ret = FALSE;
2726 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2727 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2728 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2730 query = gst_query_new_duration (format);
2731 if ((ret = gst_pad_peer_query (pad, query)))
2732 gst_query_parse_duration (query, NULL, duration);
2733 gst_query_unref (query);
2739 * gst_pad_query_convert:
2740 * @pad: a #GstPad to invoke the convert query on.
2741 * @src_format: a #GstFormat to convert from.
2742 * @src_val: a value to convert.
2743 * @dest_format: the #GstFormat to convert to.
2744 * @dest_val: (out): a pointer to the result.
2746 * Queries a pad to convert @src_val in @src_format to @dest_format.
2748 * Returns: %TRUE if the query could be performed.
2751 gst_pad_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2752 GstFormat dest_format, gint64 * dest_val)
2757 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2758 g_return_val_if_fail (dest_format != GST_FORMAT_UNDEFINED, FALSE);
2759 g_return_val_if_fail (dest_val != NULL, FALSE);
2761 if (dest_format == src_format || src_val == -1) {
2762 *dest_val = src_val;
2766 query = gst_query_new_convert (src_format, src_val, dest_format);
2767 if ((ret = gst_pad_query (pad, query)))
2768 gst_query_parse_convert (query, NULL, NULL, NULL, dest_val);
2769 gst_query_unref (query);
2775 * gst_pad_peer_query_convert:
2776 * @pad: a #GstPad, on whose peer pad to invoke the convert query on.
2777 * Must be a sink pad.
2778 * @src_format: a #GstFormat to convert from.
2779 * @src_val: a value to convert.
2780 * @dest_format: the #GstFormat to convert to.
2781 * @dest_val: (out): a pointer to the result.
2783 * Queries the peer pad of a given sink pad to convert @src_val in @src_format
2786 * Returns: %TRUE if the query could be performed.
2789 gst_pad_peer_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2790 GstFormat dest_format, gint64 * dest_val)
2793 gboolean ret = FALSE;
2795 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2796 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2797 g_return_val_if_fail (dest_format != GST_FORMAT_UNDEFINED, FALSE);
2798 g_return_val_if_fail (dest_val != NULL, FALSE);
2800 query = gst_query_new_convert (src_format, src_val, dest_format);
2801 if ((ret = gst_pad_peer_query (pad, query)))
2802 gst_query_parse_convert (query, NULL, NULL, NULL, dest_val);
2803 gst_query_unref (query);
2809 * gst_pad_query_caps:
2810 * @pad: a #GstPad to get the capabilities of.
2811 * @filter: (allow-none): suggested #GstCaps, or %NULL
2813 * Gets the capabilities this pad can produce or consume.
2814 * Note that this method doesn't necessarily return the caps set by sending a
2815 * gst_event_new_caps() - use gst_pad_get_current_caps() for that instead.
2816 * gst_pad_query_caps returns all possible caps a pad can operate with, using
2817 * the pad's CAPS query function, If the query fails, this function will return
2818 * @filter, if not %NULL, otherwise ANY.
2820 * When called on sinkpads @filter contains the caps that
2821 * upstream could produce in the order preferred by upstream. When
2822 * called on srcpads @filter contains the caps accepted by
2823 * downstream in the preferred order. @filter might be %NULL but
2824 * if it is not %NULL the returned caps will be a subset of @filter.
2826 * Note that this function does not return writable #GstCaps, use
2827 * gst_caps_make_writable() before modifying the caps.
2829 * Returns: (transfer full): the caps of the pad with incremented ref-count.
2832 gst_pad_query_caps (GstPad * pad, GstCaps * filter)
2834 GstCaps *result = NULL;
2837 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2838 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2840 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2841 "get pad caps with filter %" GST_PTR_FORMAT, filter);
2843 query = gst_query_new_caps (filter);
2844 if (gst_pad_query (pad, query)) {
2845 gst_query_parse_caps_result (query, &result);
2846 gst_caps_ref (result);
2847 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2848 "query returned %" GST_PTR_FORMAT, result);
2849 } else if (filter) {
2850 result = gst_caps_ref (filter);
2852 result = gst_caps_new_any ();
2854 gst_query_unref (query);
2860 * gst_pad_peer_query_caps:
2861 * @pad: a #GstPad to get the capabilities of.
2862 * @filter: (allow-none): a #GstCaps filter, or %NULL.
2864 * Gets the capabilities of the peer connected to this pad. Similar to
2865 * gst_pad_query_caps().
2867 * When called on srcpads @filter contains the caps that
2868 * upstream could produce in the order preferred by upstream. When
2869 * called on sinkpads @filter contains the caps accepted by
2870 * downstream in the preferred order. @filter might be %NULL but
2871 * if it is not %NULL the returned caps will be a subset of @filter.
2873 * Returns: the caps of the peer pad with incremented ref-count. When there is
2874 * no peer pad, this function returns @filter or, when @filter is %NULL, ANY
2878 gst_pad_peer_query_caps (GstPad * pad, GstCaps * filter)
2880 GstCaps *result = NULL;
2883 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2884 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
2886 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2887 "get pad peer caps with filter %" GST_PTR_FORMAT, filter);
2889 query = gst_query_new_caps (filter);
2890 if (gst_pad_peer_query (pad, query)) {
2891 gst_query_parse_caps_result (query, &result);
2892 gst_caps_ref (result);
2893 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad,
2894 "peer query returned %" GST_PTR_FORMAT, result);
2895 } else if (filter) {
2896 result = gst_caps_ref (filter);
2898 result = gst_caps_new_any ();
2900 gst_query_unref (query);
2906 * gst_pad_query_accept_caps:
2907 * @pad: a #GstPad to check
2908 * @caps: a #GstCaps to check on the pad
2910 * Check if the given pad accepts the caps.
2912 * Returns: %TRUE if the pad can accept the caps.
2915 gst_pad_query_accept_caps (GstPad * pad, GstCaps * caps)
2917 gboolean res = TRUE;
2920 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2921 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
2923 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %"
2924 GST_PTR_FORMAT, caps);
2926 query = gst_query_new_accept_caps (caps);
2927 if (gst_pad_query (pad, query)) {
2928 gst_query_parse_accept_caps_result (query, &res);
2929 GST_DEBUG_OBJECT (pad, "query returned %d", res);
2931 gst_query_unref (query);
2937 * gst_pad_peer_query_accept_caps:
2938 * @pad: a #GstPad to check the peer of
2939 * @caps: a #GstCaps to check on the pad
2941 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
2944 * Returns: %TRUE if the peer of @pad can accept the caps or @pad has no peer.
2947 gst_pad_peer_query_accept_caps (GstPad * pad, GstCaps * caps)
2949 gboolean res = TRUE;
2952 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2953 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
2955 query = gst_query_new_accept_caps (caps);
2956 if (gst_pad_peer_query (pad, query)) {
2957 gst_query_parse_accept_caps_result (query, &res);
2958 GST_DEBUG_OBJECT (pad, "query returned %d", res);
2960 gst_query_unref (query);
2966 element_find_unlinked_pad (GstElement * element, GstPadDirection direction)
2969 GstPad *unlinked_pad = NULL;
2971 GValue data = { 0, };
2973 switch (direction) {
2975 iter = gst_element_iterate_src_pads (element);
2978 iter = gst_element_iterate_sink_pads (element);
2981 g_return_val_if_reached (NULL);
2986 switch (gst_iterator_next (iter, &data)) {
2987 case GST_ITERATOR_OK:{
2989 GstPad *pad = g_value_get_object (&data);
2991 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
2992 GST_DEBUG_PAD_NAME (pad));
2994 peer = gst_pad_get_peer (pad);
2996 unlinked_pad = gst_object_ref (pad);
2998 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
2999 "found existing unlinked pad %s:%s",
3000 GST_DEBUG_PAD_NAME (unlinked_pad));
3002 gst_object_unref (peer);
3004 g_value_reset (&data);
3007 case GST_ITERATOR_DONE:
3010 case GST_ITERATOR_RESYNC:
3011 gst_iterator_resync (iter);
3013 case GST_ITERATOR_ERROR:
3014 g_return_val_if_reached (NULL);
3018 g_value_unset (&data);
3019 gst_iterator_free (iter);
3021 return unlinked_pad;
3025 * gst_bin_find_unlinked_pad:
3026 * @bin: bin in which to look for elements with unlinked pads
3027 * @direction: whether to look for an unlinked source or sink pad
3029 * Recursively looks for elements with an unlinked pad of the given
3030 * direction within the specified bin and returns an unlinked pad
3031 * if one is found, or %NULL otherwise. If a pad is found, the caller
3032 * owns a reference to it and should use gst_object_unref() on the
3033 * pad when it is not needed any longer.
3035 * Returns: (transfer full) (nullable): unlinked pad of the given
3039 gst_bin_find_unlinked_pad (GstBin * bin, GstPadDirection direction)
3044 GValue data = { 0, };
3046 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3047 g_return_val_if_fail (direction != GST_PAD_UNKNOWN, NULL);
3050 iter = gst_bin_iterate_recurse (bin);
3052 switch (gst_iterator_next (iter, &data)) {
3053 case GST_ITERATOR_OK:{
3054 GstElement *element = g_value_get_object (&data);
3056 pad = element_find_unlinked_pad (element, direction);
3059 g_value_reset (&data);
3062 case GST_ITERATOR_DONE:
3065 case GST_ITERATOR_RESYNC:
3066 gst_iterator_resync (iter);
3068 case GST_ITERATOR_ERROR:
3069 g_return_val_if_reached (NULL);
3073 g_value_unset (&data);
3074 gst_iterator_free (iter);
3080 gst_bin_sync_children_states_foreach (const GValue * value, gpointer user_data)
3082 gboolean *success = user_data;
3083 GstElement *element = g_value_get_object (value);
3085 if (gst_element_is_locked_state (element)) {
3088 *success = *success && gst_element_sync_state_with_parent (element);
3090 if (GST_IS_BIN (element))
3092 && gst_bin_sync_children_states (GST_BIN_CAST (element));
3097 * gst_bin_sync_children_states:
3100 * Synchronizes the state of every child of @bin with the state
3101 * of @bin. See also gst_element_sync_state_with_parent().
3103 * Returns: %TRUE if syncing the state was successful for all children,
3109 gst_bin_sync_children_states (GstBin * bin)
3112 GstIteratorResult res = GST_ITERATOR_OK;
3113 gboolean success = TRUE;
3115 it = gst_bin_iterate_sorted (bin);
3118 if (res == GST_ITERATOR_RESYNC) {
3120 gst_iterator_resync (it);
3123 gst_iterator_foreach (it, gst_bin_sync_children_states_foreach,
3125 } while (res == GST_ITERATOR_RESYNC);
3126 gst_iterator_free (it);
3132 * gst_parse_bin_from_description:
3133 * @bin_description: command line describing the bin
3134 * @ghost_unlinked_pads: whether to automatically create ghost pads
3135 * for unlinked source or sink pads within the bin
3136 * @err: where to store the error message in case of an error, or %NULL
3138 * This is a convenience wrapper around gst_parse_launch() to create a
3139 * #GstBin from a gst-launch-style pipeline description. See
3140 * gst_parse_launch() and the gst-launch man page for details about the
3141 * syntax. Ghost pads on the bin for unlinked source or sink pads
3142 * within the bin can automatically be created (but only a maximum of
3143 * one ghost pad for each direction will be created; if you expect
3144 * multiple unlinked source pads or multiple unlinked sink pads
3145 * and want them all ghosted, you will have to create the ghost pads
3148 * Returns: (transfer floating) (type Gst.Bin) (nullable): a
3149 * newly-created bin, or %NULL if an error occurred.
3152 gst_parse_bin_from_description (const gchar * bin_description,
3153 gboolean ghost_unlinked_pads, GError ** err)
3155 return gst_parse_bin_from_description_full (bin_description,
3156 ghost_unlinked_pads, NULL, GST_PARSE_FLAG_NONE, err);
3160 * gst_parse_bin_from_description_full:
3161 * @bin_description: command line describing the bin
3162 * @ghost_unlinked_pads: whether to automatically create ghost pads
3163 * for unlinked source or sink pads within the bin
3164 * @context: (transfer none) (allow-none): a parse context allocated with
3165 * gst_parse_context_new(), or %NULL
3166 * @flags: parsing options, or #GST_PARSE_FLAG_NONE
3167 * @err: where to store the error message in case of an error, or %NULL
3169 * This is a convenience wrapper around gst_parse_launch() to create a
3170 * #GstBin from a gst-launch-style pipeline description. See
3171 * gst_parse_launch() and the gst-launch man page for details about the
3172 * syntax. Ghost pads on the bin for unlinked source or sink pads
3173 * within the bin can automatically be created (but only a maximum of
3174 * one ghost pad for each direction will be created; if you expect
3175 * multiple unlinked source pads or multiple unlinked sink pads
3176 * and want them all ghosted, you will have to create the ghost pads
3179 * Returns: (transfer floating) (type Gst.Element): a newly-created
3180 * element, which is guaranteed to be a bin unless
3181 * GST_FLAG_NO_SINGLE_ELEMENT_BINS was passed, or %NULL if an error
3185 gst_parse_bin_from_description_full (const gchar * bin_description,
3186 gboolean ghost_unlinked_pads, GstParseContext * context,
3187 GstParseFlags flags, GError ** err)
3189 #ifndef GST_DISABLE_PARSE
3191 GstElement *element;
3195 g_return_val_if_fail (bin_description != NULL, NULL);
3196 g_return_val_if_fail (err == NULL || *err == NULL, NULL);
3198 GST_DEBUG ("Making bin from description '%s'", bin_description);
3200 /* parse the pipeline to a bin */
3201 if (flags & GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS) {
3202 element = gst_parse_launch_full (bin_description, context, flags, err);
3204 desc = g_strdup_printf ("bin.( %s )", bin_description);
3205 element = gst_parse_launch_full (desc, context, flags, err);
3209 if (element == NULL || (err && *err != NULL)) {
3211 gst_object_unref (element);
3215 if (GST_IS_BIN (element)) {
3216 bin = GST_BIN (element);
3221 /* find pads and ghost them if necessary */
3222 if (ghost_unlinked_pads) {
3223 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC))) {
3224 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("src", pad));
3225 gst_object_unref (pad);
3227 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SINK))) {
3228 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("sink", pad));
3229 gst_object_unref (pad);
3233 return GST_ELEMENT (bin);
3237 GST_WARNING ("Disabled API called");
3239 msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
3240 g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
3248 * gst_util_get_timestamp:
3250 * Get a timestamp as GstClockTime to be used for interval measurements.
3251 * The timestamp should not be interpreted in any other way.
3253 * Returns: the timestamp
3256 gst_util_get_timestamp (void)
3258 #if defined (HAVE_POSIX_TIMERS) && defined(HAVE_MONOTONIC_CLOCK) &&\
3259 defined (HAVE_CLOCK_GETTIME)
3260 struct timespec now;
3262 clock_gettime (CLOCK_MONOTONIC, &now);
3263 return GST_TIMESPEC_TO_TIME (now);
3267 g_get_current_time (&now);
3268 return GST_TIMEVAL_TO_TIME (now);
3273 * gst_util_array_binary_search:
3274 * @array: the sorted input array
3275 * @num_elements: number of elements in the array
3276 * @element_size: size of every element in bytes
3277 * @search_func: (scope call): function to compare two elements, @search_data will always be passed as second argument
3278 * @mode: search mode that should be used
3279 * @search_data: element that should be found
3280 * @user_data: (closure): data to pass to @search_func
3282 * Searches inside @array for @search_data by using the comparison function
3283 * @search_func. @array must be sorted ascending.
3285 * As @search_data is always passed as second argument to @search_func it's
3286 * not required that @search_data has the same type as the array elements.
3288 * The complexity of this search function is O(log (num_elements)).
3290 * Returns: (transfer none) (nullable): The address of the found
3291 * element or %NULL if nothing was found
3294 gst_util_array_binary_search (gpointer array, guint num_elements,
3295 gsize element_size, GCompareDataFunc search_func, GstSearchMode mode,
3296 gconstpointer search_data, gpointer user_data)
3298 glong left = 0, right = num_elements - 1, m;
3300 guint8 *data = (guint8 *) array;
3302 g_return_val_if_fail (array != NULL, NULL);
3303 g_return_val_if_fail (element_size > 0, NULL);
3304 g_return_val_if_fail (search_func != NULL, NULL);
3306 /* 0. No elements => return NULL */
3307 if (num_elements == 0)
3310 /* 1. If search_data is before the 0th element return the 0th element */
3311 ret = search_func (data, search_data, user_data);
3312 if ((ret >= 0 && mode == GST_SEARCH_MODE_AFTER) || ret == 0)
3317 /* 2. If search_data is after the last element return the last element */
3319 search_func (data + (num_elements - 1) * element_size, search_data,
3321 if ((ret <= 0 && mode == GST_SEARCH_MODE_BEFORE) || ret == 0)
3322 return data + (num_elements - 1) * element_size;
3326 /* 3. else binary search */
3328 m = left + (right - left) / 2;
3330 ret = search_func (data + m * element_size, search_data, user_data);
3333 return data + m * element_size;
3334 } else if (ret < 0) {
3340 /* No exact match found */
3342 if (mode == GST_SEARCH_MODE_EXACT) {
3344 } else if (mode == GST_SEARCH_MODE_AFTER) {
3346 return (m < num_elements) ? data + (m + 1) * element_size : NULL;
3348 return data + m * element_size;
3351 return data + m * element_size;
3353 return (m > 0) ? data + (m - 1) * element_size : NULL;
3359 /* Finds the greatest common divisor.
3360 * Returns 1 if none other found.
3361 * This is Euclid's algorithm. */
3364 * gst_util_greatest_common_divisor:
3365 * @a: First value as #gint
3366 * @b: Second value as #gint
3368 * Calculates the greatest common divisor of @a
3371 * Returns: Greatest common divisor of @a and @b
3374 gst_util_greatest_common_divisor (gint a, gint b)
3387 * gst_util_greatest_common_divisor_int64:
3388 * @a: First value as #gint64
3389 * @b: Second value as #gint64
3391 * Calculates the greatest common divisor of @a
3394 * Returns: Greatest common divisor of @a and @b
3397 gst_util_greatest_common_divisor_int64 (gint64 a, gint64 b)
3411 * gst_util_fraction_to_double:
3412 * @src_n: Fraction numerator as #gint
3413 * @src_d: Fraction denominator #gint
3414 * @dest: (out): pointer to a #gdouble for the result
3416 * Transforms a fraction to a #gdouble.
3419 gst_util_fraction_to_double (gint src_n, gint src_d, gdouble * dest)
3421 g_return_if_fail (dest != NULL);
3422 g_return_if_fail (src_d != 0);
3424 *dest = ((gdouble) src_n) / ((gdouble) src_d);
3427 #define MAX_TERMS 30
3428 #define MIN_DIVISOR 1.0e-10
3429 #define MAX_ERROR 1.0e-20
3431 /* use continued fractions to transform a double into a fraction,
3432 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
3433 * This algorithm takes care of overflows.
3437 * gst_util_double_to_fraction:
3438 * @src: #gdouble to transform
3439 * @dest_n: (out): pointer to a #gint to hold the result numerator
3440 * @dest_d: (out): pointer to a #gint to hold the result denominator
3442 * Transforms a #gdouble to a fraction and simplifies
3446 gst_util_double_to_fraction (gdouble src, gint * dest_n, gint * dest_d)
3449 gdouble V, F; /* double being converted */
3450 gint N, D; /* will contain the result */
3451 gint A; /* current term in continued fraction */
3452 gint64 N1, D1; /* numerator, denominator of last approx */
3453 gint64 N2, D2; /* numerator, denominator of previous approx */
3456 gboolean negative = FALSE;
3458 g_return_if_fail (dest_n != NULL);
3459 g_return_if_fail (dest_d != NULL);
3461 /* initialize fraction being converted */
3469 /* initialize fractions with 1/0, 0/1 */
3477 for (i = 0; i < MAX_TERMS; i++) {
3479 A = (gint) F; /* no floor() needed, F is always >= 0 */
3480 /* get new divisor */
3483 /* calculate new fraction in temp */
3487 /* guard against overflow */
3488 if (N2 > G_MAXINT || D2 > G_MAXINT) {
3495 /* save last two fractions */
3501 /* quit if dividing by zero or close enough to target */
3502 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
3506 /* Take reciprocal */
3509 /* fix for overflow */
3514 /* fix for negative */
3519 gcd = gst_util_greatest_common_divisor (N, D);
3531 * gst_util_fraction_multiply:
3532 * @a_n: Numerator of first value
3533 * @a_d: Denominator of first value
3534 * @b_n: Numerator of second value
3535 * @b_d: Denominator of second value
3536 * @res_n: (out): Pointer to #gint to hold the result numerator
3537 * @res_d: (out): Pointer to #gint to hold the result denominator
3539 * Multiplies the fractions @a_n/@a_d and @b_n/@b_d and stores
3540 * the result in @res_n and @res_d.
3542 * Returns: %FALSE on overflow, %TRUE otherwise.
3545 gst_util_fraction_multiply (gint a_n, gint a_d, gint b_n, gint b_d,
3546 gint * res_n, gint * res_d)
3550 g_return_val_if_fail (res_n != NULL, FALSE);
3551 g_return_val_if_fail (res_d != NULL, FALSE);
3552 g_return_val_if_fail (a_d != 0, FALSE);
3553 g_return_val_if_fail (b_d != 0, FALSE);
3555 /* early out if either is 0, as its gcd would be 0 */
3556 if (a_n == 0 || b_n == 0) {
3562 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3566 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3570 gcd = gst_util_greatest_common_divisor (a_n, b_d);
3574 gcd = gst_util_greatest_common_divisor (a_d, b_n);
3578 /* This would result in overflow */
3579 if (a_n != 0 && G_MAXINT / ABS (a_n) < ABS (b_n))
3581 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3587 gcd = gst_util_greatest_common_divisor (*res_n, *res_d);
3595 * gst_util_fraction_add:
3596 * @a_n: Numerator of first value
3597 * @a_d: Denominator of first value
3598 * @b_n: Numerator of second value
3599 * @b_d: Denominator of second value
3600 * @res_n: (out): Pointer to #gint to hold the result numerator
3601 * @res_d: (out): Pointer to #gint to hold the result denominator
3603 * Adds the fractions @a_n/@a_d and @b_n/@b_d and stores
3604 * the result in @res_n and @res_d.
3606 * Returns: %FALSE on overflow, %TRUE otherwise.
3609 gst_util_fraction_add (gint a_n, gint a_d, gint b_n, gint b_d, gint * res_n,
3614 g_return_val_if_fail (res_n != NULL, FALSE);
3615 g_return_val_if_fail (res_d != NULL, FALSE);
3616 g_return_val_if_fail (a_d != 0, FALSE);
3617 g_return_val_if_fail (b_d != 0, FALSE);
3619 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3623 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3638 /* This would result in overflow */
3639 if (G_MAXINT / ABS (a_n) < ABS (b_n))
3641 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3643 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3646 *res_n = (a_n * b_d) + (a_d * b_n);
3649 gcd = gst_util_greatest_common_divisor (*res_n, *res_d);
3662 * gst_util_fraction_compare:
3663 * @a_n: Numerator of first value
3664 * @a_d: Denominator of first value
3665 * @b_n: Numerator of second value
3666 * @b_d: Denominator of second value
3668 * Compares the fractions @a_n/@a_d and @b_n/@b_d and returns
3669 * -1 if a < b, 0 if a = b and 1 if a > b.
3671 * Returns: -1 if a < b; 0 if a = b; 1 if a > b.
3674 gst_util_fraction_compare (gint a_n, gint a_d, gint b_n, gint b_d)
3680 g_return_val_if_fail (a_d != 0 && b_d != 0, 0);
3683 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3687 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3691 /* fractions are reduced when set, so we can quickly see if they're equal */
3692 if (a_n == b_n && a_d == b_d)
3695 /* extend to 64 bits */
3696 new_num_1 = ((gint64) a_n) * b_d;
3697 new_num_2 = ((gint64) b_n) * a_d;
3698 if (new_num_1 < new_num_2)
3700 if (new_num_1 > new_num_2)
3703 /* Should not happen because a_d and b_d are not 0 */
3704 g_return_val_if_reached (0);
3708 gst_pad_create_stream_id_internal (GstPad * pad, GstElement * parent,
3709 const gchar * stream_id)
3711 GstEvent *upstream_event;
3712 gchar *upstream_stream_id = NULL, *new_stream_id;
3715 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3716 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
3717 g_return_val_if_fail (GST_IS_ELEMENT (parent), NULL);
3719 g_return_val_if_fail (parent->numsinkpads <= 1, NULL);
3721 /* If the element has multiple source pads it must
3722 * provide a stream-id for every source pad, otherwise
3723 * all source pads will have the same and are not
3724 * distinguishable */
3725 g_return_val_if_fail (parent->numsrcpads <= 1 || stream_id, NULL);
3727 /* First try to get the upstream stream-start stream-id from the sinkpad.
3728 * This will only work for non-source elements */
3729 sinkpad = gst_element_get_static_pad (parent, "sink");
3732 gst_pad_get_sticky_event (sinkpad, GST_EVENT_STREAM_START, 0);
3733 if (upstream_event) {
3736 gst_event_parse_stream_start (upstream_event, &tmp);
3738 upstream_stream_id = g_strdup (tmp);
3739 gst_event_unref (upstream_event);
3741 gst_object_unref (sinkpad);
3744 /* The only case where we don't have an upstream start-start event
3745 * here is for source elements */
3746 if (!upstream_stream_id) {
3750 /* Try to generate one from the URI query and
3751 * if it fails take a random number instead */
3752 query = gst_query_new_uri ();
3753 if (gst_element_query (parent, query)) {
3754 gst_query_parse_uri (query, &uri);
3760 /* And then generate an SHA256 sum of the URI */
3761 cs = g_checksum_new (G_CHECKSUM_SHA256);
3762 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
3764 upstream_stream_id = g_strdup (g_checksum_get_string (cs));
3765 g_checksum_free (cs);
3767 /* Just get some random number if the URI query fails */
3768 GST_FIXME_OBJECT (pad, "Creating random stream-id, consider "
3769 "implementing a deterministic way of creating a stream-id");
3770 upstream_stream_id =
3771 g_strdup_printf ("%08x%08x%08x%08x", g_random_int (), g_random_int (),
3772 g_random_int (), g_random_int ());
3775 gst_query_unref (query);
3779 new_stream_id = g_strconcat (upstream_stream_id, "/", stream_id, NULL);
3781 new_stream_id = g_strdup (upstream_stream_id);
3784 g_free (upstream_stream_id);
3786 return new_stream_id;
3790 * gst_pad_create_stream_id_printf_valist:
3791 * @pad: A source #GstPad
3792 * @parent: Parent #GstElement of @pad
3793 * @stream_id: (allow-none): The stream-id
3794 * @var_args: parameters for the @stream_id format string
3796 * Creates a stream-id for the source #GstPad @pad by combining the
3797 * upstream information with the optional @stream_id of the stream
3798 * of @pad. @pad must have a parent #GstElement and which must have zero
3799 * or one sinkpad. @stream_id can only be %NULL if the parent element
3800 * of @pad has only a single source pad.
3802 * This function generates an unique stream-id by getting the upstream
3803 * stream-start event stream ID and appending @stream_id to it. If the
3804 * element has no sinkpad it will generate an upstream stream-id by
3805 * doing an URI query on the element and in the worst case just uses
3806 * a random number. Source elements that don't implement the URI
3807 * handler interface should ideally generate a unique, deterministic
3808 * stream-id manually instead.
3810 * Returns: A stream-id for @pad. g_free() after usage.
3813 gst_pad_create_stream_id_printf_valist (GstPad * pad, GstElement * parent,
3814 const gchar * stream_id, va_list var_args)
3816 gchar *expanded = NULL, *new_stream_id;
3819 expanded = g_strdup_vprintf (stream_id, var_args);
3821 new_stream_id = gst_pad_create_stream_id_internal (pad, parent, expanded);
3825 return new_stream_id;
3829 * gst_pad_create_stream_id_printf:
3830 * @pad: A source #GstPad
3831 * @parent: Parent #GstElement of @pad
3832 * @stream_id: (allow-none): The stream-id
3833 * @...: parameters for the @stream_id format string
3835 * Creates a stream-id for the source #GstPad @pad by combining the
3836 * upstream information with the optional @stream_id of the stream
3837 * of @pad. @pad must have a parent #GstElement and which must have zero
3838 * or one sinkpad. @stream_id can only be %NULL if the parent element
3839 * of @pad has only a single source pad.
3841 * This function generates an unique stream-id by getting the upstream
3842 * stream-start event stream ID and appending @stream_id to it. If the
3843 * element has no sinkpad it will generate an upstream stream-id by
3844 * doing an URI query on the element and in the worst case just uses
3845 * a random number. Source elements that don't implement the URI
3846 * handler interface should ideally generate a unique, deterministic
3847 * stream-id manually instead.
3849 * Returns: A stream-id for @pad. g_free() after usage.
3852 gst_pad_create_stream_id_printf (GstPad * pad, GstElement * parent,
3853 const gchar * stream_id, ...)
3856 gchar *new_stream_id;
3858 va_start (var_args, stream_id);
3860 gst_pad_create_stream_id_printf_valist (pad, parent, stream_id, var_args);
3863 return new_stream_id;
3867 * gst_pad_create_stream_id:
3868 * @pad: A source #GstPad
3869 * @parent: Parent #GstElement of @pad
3870 * @stream_id: (allow-none): The stream-id
3872 * Creates a stream-id for the source #GstPad @pad by combining the
3873 * upstream information with the optional @stream_id of the stream
3874 * of @pad. @pad must have a parent #GstElement and which must have zero
3875 * or one sinkpad. @stream_id can only be %NULL if the parent element
3876 * of @pad has only a single source pad.
3878 * This function generates an unique stream-id by getting the upstream
3879 * stream-start event stream ID and appending @stream_id to it. If the
3880 * element has no sinkpad it will generate an upstream stream-id by
3881 * doing an URI query on the element and in the worst case just uses
3882 * a random number. Source elements that don't implement the URI
3883 * handler interface should ideally generate a unique, deterministic
3884 * stream-id manually instead.
3886 * Since stream IDs are sorted alphabetically, any numbers in the
3887 * stream ID should be printed with a fixed number of characters,
3888 * preceded by 0's, such as by using the format \%03u instead of \%u.
3890 * Returns: A stream-id for @pad. g_free() after usage.
3893 gst_pad_create_stream_id (GstPad * pad, GstElement * parent,
3894 const gchar * stream_id)
3896 return gst_pad_create_stream_id_internal (pad, parent, stream_id);
3900 * gst_pad_get_stream_id:
3901 * @pad: A source #GstPad
3903 * Returns the current stream-id for the @pad, or %NULL if none has been
3904 * set yet, i.e. the pad has not received a stream-start event yet.
3906 * This is a convenience wrapper around gst_pad_get_sticky_event() and
3907 * gst_event_parse_stream_start().
3909 * The returned stream-id string should be treated as an opaque string, its
3910 * contents should not be interpreted.
3912 * Returns: (nullable): a newly-allocated copy of the stream-id for
3913 * @pad, or %NULL. g_free() the returned string when no longer
3919 gst_pad_get_stream_id (GstPad * pad)
3921 const gchar *stream_id = NULL;
3925 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3927 event = gst_pad_get_sticky_event (pad, GST_EVENT_STREAM_START, 0);
3928 if (event != NULL) {
3929 gst_event_parse_stream_start (event, &stream_id);
3930 ret = g_strdup (stream_id);
3931 gst_event_unref (event);
3932 GST_LOG_OBJECT (pad, "pad has stream-id '%s'", ret);
3934 GST_DEBUG_OBJECT (pad, "pad has not received a stream-start event yet");
3941 * gst_util_group_id_next:
3943 * Return a constantly incrementing group id.
3945 * This function is used to generate a new group-id for the
3946 * stream-start event.
3948 * Returns: A constantly incrementing unsigned integer, which might
3949 * overflow back to 0 at some point.
3952 gst_util_group_id_next (void)
3954 static gint counter = 0;
3955 return g_atomic_int_add (&counter, 1);
3958 #ifdef TIZEN_PROFILE_TV
3960 * gst_element_query_resource
3961 * @element: (in) a #GstElement to invoke the resource query on.
3962 * @resources: (out): a pointer to the list of resources asked for.
3963 * Returns: TRUE if the query could be performed.
3966 gst_element_query_resource (GstElement * element, GList ** resource_list)
3970 char resources[250];
3971 char *element_resource;
3972 guint count = 0, i = 0;
3976 if (resource_list == NULL) {
3980 query = gst_query_new_resource (resources);
3981 if (GST_IS_BIN (element)) {
3982 count = gst_child_proxy_get_children_count ((GstChildProxy *)element);
3983 for (i = 0; i < count; i++) {
3984 if (!(object = gst_child_proxy_get_child_by_index ((GstChildProxy *)element, i)))
3987 if (GST_IS_BIN (object)) {
3988 ret = gst_element_query_resource ((GstElement *)object, resource_list);
3989 gst_object_unref (object);
3993 ret = gst_element_query ((GstElement *) object, query);
3995 element_resource = gst_query_parse_resource (query);
3996 resource_num = (int) atoi (element_resource);
3997 GST_DEBUG_OBJECT (element,
3998 "\n resource ID received after query is :%d\n", resource_num);
3999 if (NULL == (g_list_find (*resource_list, (gconstpointer)resource_num))) {
4001 g_list_append (*resource_list, GINT_TO_POINTER (resource_num));
4004 gst_object_unref (object);
4007 ret = gst_element_query ((GstElement *) element, query);
4009 element_resource = gst_query_parse_resource (query);
4010 resource_num = (int) atoi (element_resource);
4011 GST_DEBUG_OBJECT (element, "\n resource ID received after query is :%d\n",
4013 if (NULL == (g_list_find (*resource_list, (gconstpointer)resource_num))) {
4015 g_list_append (*resource_list, GINT_TO_POINTER (resource_num));
4019 gst_query_unref (query);
4024 set_family_id (const GValue * item, gpointer user_data)
4026 gpointer object = g_value_get_object (item);
4027 if (GST_IS_PAD (object)) {
4028 GstPad *pad = GST_PAD_CAST (object);
4029 g_object_set (G_OBJECT (pad), "family-id", (int) user_data, NULL);
4030 } else if (GST_IS_ELEMENT (object)) {
4031 gst_set_family_id_to_child (GST_ELEMENT_CAST (object), (int) user_data);
4037 gst_set_family_id_to_child (GstElement * child, int id)
4040 if (!GST_IS_ELEMENT (child))
4042 g_object_set (G_OBJECT (child), "family-id", id, NULL);
4043 it = gst_element_iterate_pads (child);
4044 gst_iterator_foreach (it, (GstIteratorForeachFunction) set_family_id,
4046 gst_iterator_free (it);
4049 if (!GST_IS_BIN (child))
4051 it = gst_bin_iterate_recurse (GST_BIN (child));
4052 gst_iterator_foreach (it, (GstIteratorForeachFunction) set_family_id,
4054 gst_iterator_free (it);