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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 * @short_description: Various utility functions
30 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
31 * with newer GLib versions (>= 2.31.0) */
32 #define GLIB_DISABLE_DEPRECATION_WARNINGS
33 #include "gst_private.h"
37 #include "gstghostpad.h"
43 #include "gst-i18n-lib.h"
44 #include "glib-compat-private.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 * Convertes 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 (GST_VALUE_HOLDS_STRUCTURE (&v) && 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.
707 gst_util_seqnum_next (void)
709 static gint counter = 0;
710 return G_ATOMIC_INT_ADD (&counter, 1);
714 * gst_util_seqnum_compare:
715 * @s1: A sequence number.
716 * @s2: Another sequence number.
718 * Compare two sequence numbers, handling wraparound.
720 * The current implementation just returns (gint32)(@s1 - @s2).
722 * Returns: A negative number if @s1 is before @s2, 0 if they are equal, or a
723 * positive number if @s1 is after @s2.
728 gst_util_seqnum_compare (guint32 s1, guint32 s2)
730 return (gint32) (s1 - s2);
733 /* -----------------------------------------------------
735 * The following code will be moved out of the main
736 * gstreamer library someday.
742 string_append_indent (GString * str, gint count)
746 for (xx = 0; xx < count; xx++)
747 g_string_append_c (str, ' ');
751 * gst_print_pad_caps:
752 * @buf: the buffer to print the caps in
753 * @indent: initial indentation
754 * @pad: (transfer none): the pad to print the caps from
756 * Write the pad capabilities in a human readable format into
760 gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
764 caps = gst_pad_get_current_caps (pad);
767 string_append_indent (buf, indent);
768 g_string_printf (buf, "%s:%s has no capabilities",
769 GST_DEBUG_PAD_NAME (pad));
773 s = gst_caps_to_string (caps);
774 g_string_append (buf, s);
777 gst_caps_unref (caps);
782 * gst_print_element_args:
783 * @buf: the buffer to print the args in
784 * @indent: initial indentation
785 * @element: (transfer none): the element to print the args of
787 * Print the element argument in a human readable format in the given
791 gst_print_element_args (GString * buf, gint indent, GstElement * element)
794 GValue value = { 0, }; /* the important thing is that value.type = 0 */
796 GParamSpec *spec, **specs, **walk;
798 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
801 for (walk = specs; *walk; walk++) {
803 if (width < strlen (spec->name))
804 width = strlen (spec->name);
807 for (walk = specs; *walk; walk++) {
810 if (spec->flags & G_PARAM_READABLE) {
811 g_value_init (&value, spec->value_type);
812 g_object_get_property (G_OBJECT (element), spec->name, &value);
813 str = g_strdup_value_contents (&value);
814 g_value_unset (&value);
816 str = g_strdup ("Parameter not readable.");
819 string_append_indent (buf, indent);
820 g_string_append (buf, spec->name);
821 string_append_indent (buf, 2 + width - strlen (spec->name));
822 g_string_append (buf, str);
823 g_string_append_c (buf, '\n');
832 * gst_element_create_all_pads:
833 * @element: (transfer none): a #GstElement to create pads for
835 * Creates a pad for each pad template that is always available.
836 * This function is only useful during object initialization of
837 * subclasses of #GstElement.
840 gst_element_create_all_pads (GstElement * element)
844 /* FIXME: lock element */
847 gst_element_class_get_pad_template_list (GST_ELEMENT_CLASS
848 (G_OBJECT_GET_CLASS (element)));
851 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
853 if (padtempl->presence == GST_PAD_ALWAYS) {
856 pad = gst_pad_new_from_template (padtempl, padtempl->name_template);
858 gst_element_add_pad (element, pad);
860 padlist = padlist->next;
865 * gst_element_get_compatible_pad_template:
866 * @element: (transfer none): a #GstElement to get a compatible pad template for
867 * @compattempl: (transfer none): the #GstPadTemplate to find a compatible
870 * Retrieves a pad template from @element that is compatible with @compattempl.
871 * Pads from compatible templates can be linked together.
873 * Returns: (transfer none): a compatible #GstPadTemplate, or NULL if none
874 * was found. No unreferencing is necessary.
877 gst_element_get_compatible_pad_template (GstElement * element,
878 GstPadTemplate * compattempl)
880 GstPadTemplate *newtempl = NULL;
882 GstElementClass *class;
885 g_return_val_if_fail (element != NULL, NULL);
886 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
887 g_return_val_if_fail (compattempl != NULL, NULL);
889 class = GST_ELEMENT_GET_CLASS (element);
891 padlist = gst_element_class_get_pad_template_list (class);
893 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
894 "Looking for a suitable pad template in %s out of %d templates...",
895 GST_ELEMENT_NAME (element), g_list_length (padlist));
898 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
902 * Check direction (must be opposite)
905 GST_CAT_LOG (GST_CAT_CAPS,
906 "checking pad template %s", padtempl->name_template);
907 if (padtempl->direction != compattempl->direction) {
908 GST_CAT_DEBUG (GST_CAT_CAPS,
909 "compatible direction: found %s pad template \"%s\"",
910 padtempl->direction == GST_PAD_SRC ? "src" : "sink",
911 padtempl->name_template);
913 GST_CAT_DEBUG (GST_CAT_CAPS,
914 "intersecting %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (compattempl));
915 GST_CAT_DEBUG (GST_CAT_CAPS,
916 "..and %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (padtempl));
918 compatible = gst_caps_can_intersect (GST_PAD_TEMPLATE_CAPS (compattempl),
919 GST_PAD_TEMPLATE_CAPS (padtempl));
921 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
922 (compatible ? "" : "not "));
930 padlist = g_list_next (padlist);
933 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
934 "Returning new pad template %p", newtempl);
936 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "No compatible pad template found");
942 * gst_element_get_pad_from_template:
943 * @element: (transfer none): a #GstElement.
944 * @templ: (transfer none): a #GstPadTemplate belonging to @element.
946 * Gets a pad from @element described by @templ. If the presence of @templ is
947 * #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
950 * Returns: (transfer full): the #GstPad, or NULL if one could not be found
954 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
957 GstPadPresence presence;
959 /* If this function is ever exported, we need check the validity of `element'
960 * and `templ', and to make sure the template actually belongs to the
963 presence = GST_PAD_TEMPLATE_PRESENCE (templ);
967 case GST_PAD_SOMETIMES:
968 ret = gst_element_get_static_pad (element, templ->name_template);
969 if (!ret && presence == GST_PAD_ALWAYS)
971 ("Element %s has an ALWAYS template %s, but no pad of the same name",
972 GST_OBJECT_NAME (element), templ->name_template);
975 case GST_PAD_REQUEST:
976 ret = gst_element_request_pad (element, templ, NULL, NULL);
984 * gst_element_request_compatible_pad:
985 * @element: a #GstElement.
986 * @templ: the #GstPadTemplate to which the new pad should be able to link.
988 * Requests a pad from @element. The returned pad should be unlinked and
989 * compatible with @templ. Might return an existing pad, or request a new one.
991 * Returns: a #GstPad, or %NULL if one could not be found or created.
994 gst_element_request_compatible_pad (GstElement * element,
995 GstPadTemplate * templ)
997 GstPadTemplate *templ_new;
1000 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1001 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
1003 /* FIXME: should really loop through the templates, testing each for
1004 * compatibility and pad availability. */
1005 templ_new = gst_element_get_compatible_pad_template (element, templ);
1007 pad = gst_element_get_pad_from_template (element, templ_new);
1009 /* This can happen for non-request pads. No need to unref. */
1010 if (pad && GST_PAD_PEER (pad))
1017 * Checks if the source pad and the sink pad can be linked.
1018 * Both @srcpad and @sinkpad must be unlinked and have a parent.
1021 gst_pad_check_link (GstPad * srcpad, GstPad * sinkpad)
1023 /* FIXME This function is gross. It's almost a direct copy of
1024 * gst_pad_link_filtered(). Any decent programmer would attempt
1025 * to merge the two functions, which I will do some day. --ds
1028 /* generic checks */
1029 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1030 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1032 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1033 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1035 /* FIXME: shouldn't we convert this to g_return_val_if_fail? */
1036 if (GST_PAD_PEER (srcpad) != NULL) {
1037 GST_CAT_INFO (GST_CAT_PADS, "Source pad %s:%s has a peer, failed",
1038 GST_DEBUG_PAD_NAME (srcpad));
1041 if (GST_PAD_PEER (sinkpad) != NULL) {
1042 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has a peer, failed",
1043 GST_DEBUG_PAD_NAME (sinkpad));
1046 if (!GST_PAD_IS_SRC (srcpad)) {
1047 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s is not source pad, failed",
1048 GST_DEBUG_PAD_NAME (srcpad));
1051 if (!GST_PAD_IS_SINK (sinkpad)) {
1052 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s is not sink pad, failed",
1053 GST_DEBUG_PAD_NAME (sinkpad));
1056 if (GST_PAD_PARENT (srcpad) == NULL) {
1057 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s has no parent, failed",
1058 GST_DEBUG_PAD_NAME (srcpad));
1061 if (GST_PAD_PARENT (sinkpad) == NULL) {
1062 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has no parent, failed",
1063 GST_DEBUG_PAD_NAME (srcpad));
1071 * gst_element_get_compatible_pad:
1072 * @element: (transfer none): a #GstElement in which the pad should be found.
1073 * @pad: (transfer none): the #GstPad to find a compatible one for.
1074 * @caps: the #GstCaps to use as a filter.
1076 * Looks for an unlinked pad to which the given pad can link. It is not
1077 * guaranteed that linking the pads will work, though it should work in most
1080 * This function will first attempt to find a compatible unlinked ALWAYS pad,
1081 * and if none can be found, it will request a compatible REQUEST pad by looking
1082 * at the templates of @element.
1084 * Returns: (transfer full): the #GstPad to which a link can be made, or %NULL
1085 * if one cannot be found. gst_object_unref() after usage.
1088 gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
1089 const GstCaps * caps)
1092 GstPadTemplate *templ;
1094 GstPad *foundpad = NULL;
1096 GValue padptr = { 0, };
1098 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1099 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1101 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1102 "finding pad in %s compatible with %s:%s",
1103 GST_ELEMENT_NAME (element), GST_DEBUG_PAD_NAME (pad));
1105 g_return_val_if_fail (GST_PAD_PEER (pad) == NULL, NULL);
1109 /* try to get an existing unlinked pad */
1110 if (GST_PAD_IS_SRC (pad)) {
1111 pads = gst_element_iterate_sink_pads (element);
1112 } else if (GST_PAD_IS_SINK (pad)) {
1113 pads = gst_element_iterate_src_pads (element);
1115 pads = gst_element_iterate_pads (element);
1119 switch (gst_iterator_next (pads, &padptr)) {
1120 case GST_ITERATOR_OK:
1127 current = g_value_get_object (&padptr);
1129 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
1130 GST_DEBUG_PAD_NAME (current));
1132 if (GST_PAD_IS_SRC (current)) {
1139 peer = gst_pad_get_peer (current);
1141 if (peer == NULL && gst_pad_check_link (srcpad, sinkpad)) {
1142 GstCaps *temp, *intersection;
1143 gboolean compatible;
1145 /* Now check if the two pads' caps are compatible */
1146 temp = gst_pad_query_caps (pad, NULL);
1148 intersection = gst_caps_intersect (temp, caps);
1149 gst_caps_unref (temp);
1151 intersection = temp;
1154 temp = gst_pad_query_caps (current, NULL);
1155 compatible = gst_caps_can_intersect (temp, intersection);
1156 gst_caps_unref (temp);
1157 gst_caps_unref (intersection);
1160 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1161 "found existing unlinked compatible pad %s:%s",
1162 GST_DEBUG_PAD_NAME (current));
1163 gst_iterator_free (pads);
1165 current = gst_object_ref (current);
1166 g_value_unset (&padptr);
1170 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "incompatible pads");
1173 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1174 "already linked or cannot be linked (peer = %p)", peer);
1176 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unreffing pads");
1178 g_value_reset (&padptr);
1180 gst_object_unref (peer);
1183 case GST_ITERATOR_DONE:
1186 case GST_ITERATOR_RESYNC:
1187 gst_iterator_resync (pads);
1189 case GST_ITERATOR_ERROR:
1190 g_assert_not_reached ();
1194 g_value_unset (&padptr);
1195 gst_iterator_free (pads);
1197 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
1198 "Could not find a compatible unlinked always pad to link to %s:%s, now checking request pads",
1199 GST_DEBUG_PAD_NAME (pad));
1201 /* try to create a new one */
1202 /* requesting is a little crazy, we need a template. Let's create one */
1203 /* FIXME: why not gst_pad_get_pad_template (pad); */
1204 templcaps = gst_pad_query_caps (pad, NULL);
1205 templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
1206 GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
1207 gst_caps_unref (templcaps);
1209 foundpad = gst_element_request_compatible_pad (element, templ);
1210 gst_object_unref (templ);
1213 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1214 "found existing request pad %s:%s", GST_DEBUG_PAD_NAME (foundpad));
1218 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element,
1219 "Could not find a compatible pad to link to %s:%s",
1220 GST_DEBUG_PAD_NAME (pad));
1225 * gst_element_state_get_name:
1226 * @state: a #GstState to get the name of.
1228 * Gets a string representing the given state.
1230 * Returns: (transfer none): a string with the name of the state.
1233 gst_element_state_get_name (GstState state)
1236 case GST_STATE_VOID_PENDING:
1237 return "VOID_PENDING";
1238 case GST_STATE_NULL:
1240 case GST_STATE_READY:
1242 case GST_STATE_PLAYING:
1244 case GST_STATE_PAUSED:
1247 /* This is a memory leak */
1248 return g_strdup_printf ("UNKNOWN!(%d)", state);
1253 * gst_element_state_change_return_get_name:
1254 * @state_ret: a #GstStateChangeReturn to get the name of.
1256 * Gets a string representing the given state change result.
1258 * Returns: (transfer none): a string with the name of the state
1264 gst_element_state_change_return_get_name (GstStateChangeReturn state_ret)
1266 switch (state_ret) {
1267 case GST_STATE_CHANGE_FAILURE:
1269 case GST_STATE_CHANGE_SUCCESS:
1271 case GST_STATE_CHANGE_ASYNC:
1273 case GST_STATE_CHANGE_NO_PREROLL:
1274 return "NO PREROLL";
1276 /* This is a memory leak */
1277 return g_strdup_printf ("UNKNOWN!(%d)", state_ret);
1283 gst_element_factory_can_accept_all_caps_in_direction (GstElementFactory *
1284 factory, const GstCaps * caps, GstPadDirection direction)
1288 g_return_val_if_fail (factory != NULL, FALSE);
1289 g_return_val_if_fail (caps != NULL, FALSE);
1291 templates = factory->staticpadtemplates;
1294 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1296 if (template->direction == direction) {
1297 GstCaps *templcaps = gst_static_caps_get (&template->static_caps);
1299 if (gst_caps_is_always_compatible (caps, templcaps)) {
1300 gst_caps_unref (templcaps);
1303 gst_caps_unref (templcaps);
1305 templates = g_list_next (templates);
1312 gst_element_factory_can_accept_any_caps_in_direction (GstElementFactory *
1313 factory, const GstCaps * caps, GstPadDirection direction)
1317 g_return_val_if_fail (factory != NULL, FALSE);
1318 g_return_val_if_fail (caps != NULL, FALSE);
1320 templates = factory->staticpadtemplates;
1323 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1325 if (template->direction == direction) {
1326 GstCaps *templcaps = gst_static_caps_get (&template->static_caps);
1328 if (gst_caps_can_intersect (caps, templcaps)) {
1329 gst_caps_unref (templcaps);
1332 gst_caps_unref (templcaps);
1334 templates = g_list_next (templates);
1341 * gst_element_factory_can_sink_all_caps:
1342 * @factory: factory to query
1343 * @caps: the caps to check
1345 * Checks if the factory can sink all possible capabilities.
1347 * Returns: %TRUE if the caps are fully compatible.
1352 gst_element_factory_can_sink_all_caps (GstElementFactory * factory,
1353 const GstCaps * caps)
1355 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1360 * gst_element_factory_can_src_all_caps:
1361 * @factory: factory to query
1362 * @caps: the caps to check
1364 * Checks if the factory can src all possible capabilities.
1366 * Returns: %TRUE if the caps are fully compatible.
1371 gst_element_factory_can_src_all_caps (GstElementFactory * factory,
1372 const GstCaps * caps)
1374 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1379 * gst_element_factory_can_sink_any_caps:
1380 * @factory: factory to query
1381 * @caps: the caps to check
1383 * Checks if the factory can sink any possible capability.
1385 * Returns: %TRUE if the caps have a common subset.
1390 gst_element_factory_can_sink_any_caps (GstElementFactory * factory,
1391 const GstCaps * caps)
1393 return gst_element_factory_can_accept_any_caps_in_direction (factory, caps,
1398 * gst_element_factory_can_src_any_caps:
1399 * @factory: factory to query
1400 * @caps: the caps to check
1402 * Checks if the factory can src any possible capability.
1404 * Returns: %TRUE if the caps have a common subset.
1409 gst_element_factory_can_src_any_caps (GstElementFactory * factory,
1410 const GstCaps * caps)
1412 return gst_element_factory_can_accept_any_caps_in_direction (factory, caps,
1416 /* if return val is true, *direct_child is a caller-owned ref on the direct
1417 * child of ancestor that is part of object's ancestry */
1419 object_has_ancestor (GstObject * object, GstObject * ancestor,
1420 GstObject ** direct_child)
1422 GstObject *child, *parent;
1425 *direct_child = NULL;
1427 child = gst_object_ref (object);
1428 parent = gst_object_get_parent (object);
1431 if (ancestor == parent) {
1433 *direct_child = child;
1435 gst_object_unref (child);
1436 gst_object_unref (parent);
1440 gst_object_unref (child);
1442 parent = gst_object_get_parent (parent);
1445 gst_object_unref (child);
1450 /* caller owns return */
1452 find_common_root (GstObject * o1, GstObject * o2)
1454 GstObject *top = o1;
1455 GstObject *kid1, *kid2;
1456 GstObject *root = NULL;
1458 while (GST_OBJECT_PARENT (top))
1459 top = GST_OBJECT_PARENT (top);
1461 /* the itsy-bitsy spider... */
1463 if (!object_has_ancestor (o2, top, &kid2))
1466 root = gst_object_ref (top);
1468 if (!object_has_ancestor (o1, kid2, &kid1)) {
1469 gst_object_unref (kid2);
1473 if (!object_has_ancestor (o2, kid1, &kid2)) {
1474 gst_object_unref (kid1);
1481 /* caller does not own return */
1483 ghost_up (GstElement * e, GstPad * pad)
1485 static gint ghost_pad_index = 0;
1490 GstObject *parent = GST_OBJECT_PARENT (e);
1492 name = g_strdup_printf ("ghost%d", ghost_pad_index++);
1493 gpad = gst_ghost_pad_new (name, pad);
1497 gst_element_get_state (e, ¤t, &next, 0);
1499 if (current > GST_STATE_READY || next == GST_STATE_PAUSED)
1500 gst_pad_set_active (gpad, TRUE);
1502 if (!gst_element_add_pad ((GstElement *) parent, gpad)) {
1503 g_warning ("Pad named %s already exists in element %s\n",
1504 GST_OBJECT_NAME (gpad), GST_OBJECT_NAME (parent));
1505 gst_object_unref ((GstObject *) gpad);
1506 GST_STATE_UNLOCK (e);
1509 GST_STATE_UNLOCK (e);
1515 remove_pad (gpointer ppad, gpointer unused)
1519 if (!gst_element_remove_pad ((GstElement *) GST_OBJECT_PARENT (pad), pad))
1520 g_warning ("Couldn't remove pad %s from element %s",
1521 GST_OBJECT_NAME (pad), GST_OBJECT_NAME (GST_OBJECT_PARENT (pad)));
1525 prepare_link_maybe_ghosting (GstPad ** src, GstPad ** sink,
1526 GSList ** pads_created)
1530 GSList *pads_created_local = NULL;
1532 g_assert (pads_created);
1534 e1 = GST_OBJECT_PARENT (*src);
1535 e2 = GST_OBJECT_PARENT (*sink);
1537 if (G_UNLIKELY (e1 == NULL)) {
1538 GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1539 GST_PTR_FORMAT, *src);
1542 if (G_UNLIKELY (e2 == NULL)) {
1543 GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1544 GST_PTR_FORMAT, *sink);
1548 if (GST_OBJECT_PARENT (e1) == GST_OBJECT_PARENT (e2)) {
1549 GST_CAT_INFO (GST_CAT_PADS, "%s and %s in same bin, no need for ghost pads",
1550 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1554 GST_CAT_INFO (GST_CAT_PADS, "%s and %s not in same bin, making ghost pads",
1555 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1557 /* we need to setup some ghost pads */
1558 root = find_common_root (e1, e2);
1560 g_warning ("Trying to connect elements that don't share a common "
1561 "ancestor: %s and %s", GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2));
1565 while (GST_OBJECT_PARENT (e1) != root) {
1566 *src = ghost_up ((GstElement *) e1, *src);
1569 e1 = GST_OBJECT_PARENT (*src);
1570 pads_created_local = g_slist_prepend (pads_created_local, *src);
1572 while (GST_OBJECT_PARENT (e2) != root) {
1573 *sink = ghost_up ((GstElement *) e2, *sink);
1576 e2 = GST_OBJECT_PARENT (*sink);
1577 pads_created_local = g_slist_prepend (pads_created_local, *sink);
1580 gst_object_unref (root);
1581 *pads_created = g_slist_concat (*pads_created, pads_created_local);
1585 gst_object_unref (root);
1586 g_slist_foreach (pads_created_local, remove_pad, NULL);
1587 g_slist_free (pads_created_local);
1592 pad_link_maybe_ghosting (GstPad * src, GstPad * sink, GstPadLinkCheck flags)
1594 GSList *pads_created = NULL;
1597 if (!prepare_link_maybe_ghosting (&src, &sink, &pads_created)) {
1600 ret = (gst_pad_link_full (src, sink, flags) == GST_PAD_LINK_OK);
1604 g_slist_foreach (pads_created, remove_pad, NULL);
1606 g_slist_free (pads_created);
1612 * gst_element_link_pads_full:
1613 * @src: a #GstElement containing the source pad.
1614 * @srcpadname: (allow-none): the name of the #GstPad in source element
1615 * or NULL for any pad.
1616 * @dest: (transfer none): the #GstElement containing the destination pad.
1617 * @destpadname: (allow-none): the name of the #GstPad in destination element,
1618 * or NULL for any pad.
1619 * @flags: the #GstPadLinkCheck to be performed when linking pads.
1621 * Links the two named pads of the source and destination elements.
1622 * Side effect is that if one of the pads has no parent, it becomes a
1623 * child of the parent of the other element. If they have different
1624 * parents, the link fails.
1626 * Calling gst_element_link_pads_full() with @flags == %GST_PAD_LINK_CHECK_DEFAULT
1627 * is the same as calling gst_element_link_pads() and the recommended way of
1628 * linking pads with safety checks applied.
1630 * This is a convenience function for gst_pad_link_full().
1632 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1637 gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
1638 GstElement * dest, const gchar * destpadname, GstPadLinkCheck flags)
1640 const GList *srcpads, *destpads, *srctempls, *desttempls, *l;
1641 GstPad *srcpad, *destpad;
1642 GstPadTemplate *srctempl, *desttempl;
1643 GstElementClass *srcclass, *destclass;
1646 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1647 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1649 GST_CAT_INFO (GST_CAT_ELEMENT_PADS,
1650 "trying to link element %s:%s to element %s:%s", GST_ELEMENT_NAME (src),
1651 srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
1652 destpadname ? destpadname : "(any)");
1656 /* name specified, look it up */
1657 if (!(srcpad = gst_element_get_static_pad (src, srcpadname)))
1658 srcpad = gst_element_get_request_pad (src, srcpadname);
1660 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1661 GST_ELEMENT_NAME (src), srcpadname);
1664 if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
1665 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
1666 GST_DEBUG_PAD_NAME (srcpad));
1667 gst_object_unref (srcpad);
1670 if (GST_PAD_PEER (srcpad) != NULL) {
1671 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1672 "pad %s:%s is already linked to %s:%s", GST_DEBUG_PAD_NAME (srcpad),
1673 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1674 gst_object_unref (srcpad);
1680 /* no name given, get the first available pad */
1681 GST_OBJECT_LOCK (src);
1682 srcpads = GST_ELEMENT_PADS (src);
1683 srcpad = srcpads ? GST_PAD_CAST (srcpads->data) : NULL;
1685 gst_object_ref (srcpad);
1686 GST_OBJECT_UNLOCK (src);
1689 /* get a destination pad */
1691 /* name specified, look it up */
1692 if (!(destpad = gst_element_get_static_pad (dest, destpadname)))
1693 destpad = gst_element_get_request_pad (dest, destpadname);
1695 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1696 GST_ELEMENT_NAME (dest), destpadname);
1699 if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
1700 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
1701 GST_DEBUG_PAD_NAME (destpad));
1702 gst_object_unref (destpad);
1705 if (GST_PAD_PEER (destpad) != NULL) {
1706 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1707 "pad %s:%s is already linked to %s:%s",
1708 GST_DEBUG_PAD_NAME (destpad),
1709 GST_DEBUG_PAD_NAME (GST_PAD_PEER (destpad)));
1710 gst_object_unref (destpad);
1716 /* no name given, get the first available pad */
1717 GST_OBJECT_LOCK (dest);
1718 destpads = GST_ELEMENT_PADS (dest);
1719 destpad = destpads ? GST_PAD_CAST (destpads->data) : NULL;
1721 gst_object_ref (destpad);
1722 GST_OBJECT_UNLOCK (dest);
1725 if (srcpadname && destpadname) {
1728 /* two explicitly specified pads */
1729 result = pad_link_maybe_ghosting (srcpad, destpad, flags);
1731 gst_object_unref (srcpad);
1732 gst_object_unref (destpad);
1738 /* loop through the allowed pads in the source, trying to find a
1739 * compatible destination pad */
1740 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1741 "looping through allowed src and dest pads");
1743 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying src pad %s:%s",
1744 GST_DEBUG_PAD_NAME (srcpad));
1745 if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
1746 (GST_PAD_PEER (srcpad) == NULL)) {
1751 gst_object_ref (temp);
1753 temp = gst_element_get_compatible_pad (dest, srcpad, NULL);
1756 if (temp && pad_link_maybe_ghosting (srcpad, temp, flags)) {
1757 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1758 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (temp));
1760 gst_object_unref (destpad);
1761 gst_object_unref (srcpad);
1762 gst_object_unref (temp);
1767 gst_object_unref (temp);
1770 /* find a better way for this mess */
1772 srcpads = g_list_next (srcpads);
1774 gst_object_unref (srcpad);
1775 srcpad = GST_PAD_CAST (srcpads->data);
1776 gst_object_ref (srcpad);
1782 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
1783 GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
1785 gst_object_unref (destpad);
1789 gst_object_unref (srcpad);
1793 /* loop through the existing pads in the destination */
1795 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying dest pad %s:%s",
1796 GST_DEBUG_PAD_NAME (destpad));
1797 if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
1798 (GST_PAD_PEER (destpad) == NULL)) {
1799 GstPad *temp = gst_element_get_compatible_pad (src, destpad, NULL);
1801 if (temp && pad_link_maybe_ghosting (temp, destpad, flags)) {
1802 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1803 GST_DEBUG_PAD_NAME (temp), GST_DEBUG_PAD_NAME (destpad));
1804 gst_object_unref (temp);
1805 gst_object_unref (destpad);
1809 gst_object_unref (temp);
1813 destpads = g_list_next (destpads);
1815 gst_object_unref (destpad);
1816 destpad = GST_PAD_CAST (destpads->data);
1817 gst_object_ref (destpad);
1824 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
1825 GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
1826 gst_object_unref (destpad);
1830 gst_object_unref (destpad);
1834 srcclass = GST_ELEMENT_GET_CLASS (src);
1835 destclass = GST_ELEMENT_GET_CLASS (dest);
1837 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1838 "we might have request pads on both sides, checking...");
1839 srctempls = gst_element_class_get_pad_template_list (srcclass);
1840 desttempls = gst_element_class_get_pad_template_list (destclass);
1842 if (srctempls && desttempls) {
1844 srctempl = (GstPadTemplate *) srctempls->data;
1845 if (srctempl->presence == GST_PAD_REQUEST) {
1846 for (l = desttempls; l; l = l->next) {
1847 desttempl = (GstPadTemplate *) l->data;
1848 if (desttempl->presence == GST_PAD_REQUEST &&
1849 desttempl->direction != srctempl->direction) {
1850 GstCaps *srccaps, *destcaps;
1852 srccaps = gst_pad_template_get_caps (srctempl);
1853 destcaps = gst_pad_template_get_caps (desttempl);
1854 if (gst_caps_is_always_compatible (srccaps, destcaps)) {
1856 gst_element_request_pad (src, srctempl,
1857 srctempl->name_template, NULL);
1859 gst_element_request_pad (dest, desttempl,
1860 desttempl->name_template, NULL);
1861 if (srcpad && destpad
1862 && pad_link_maybe_ghosting (srcpad, destpad, flags)) {
1863 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1864 "linked pad %s:%s to pad %s:%s",
1865 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
1866 gst_object_unref (srcpad);
1867 gst_object_unref (destpad);
1868 gst_caps_unref (srccaps);
1869 gst_caps_unref (destcaps);
1872 /* it failed, so we release the request pads */
1874 gst_element_release_request_pad (src, srcpad);
1876 gst_element_release_request_pad (dest, destpad);
1878 gst_caps_unref (srccaps);
1879 gst_caps_unref (destcaps);
1883 srctempls = srctempls->next;
1887 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s",
1888 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1893 * gst_element_link_pads:
1894 * @src: a #GstElement containing the source pad.
1895 * @srcpadname: (allow-none): the name of the #GstPad in source element
1896 * or NULL for any pad.
1897 * @dest: (transfer none): the #GstElement containing the destination pad.
1898 * @destpadname: (allow-none): the name of the #GstPad in destination element,
1899 * or NULL for any pad.
1901 * Links the two named pads of the source and destination elements.
1902 * Side effect is that if one of the pads has no parent, it becomes a
1903 * child of the parent of the other element. If they have different
1904 * parents, the link fails.
1906 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1909 gst_element_link_pads (GstElement * src, const gchar * srcpadname,
1910 GstElement * dest, const gchar * destpadname)
1912 return gst_element_link_pads_full (src, srcpadname, dest, destpadname,
1913 GST_PAD_LINK_CHECK_DEFAULT);
1917 * gst_element_link_pads_filtered:
1918 * @src: a #GstElement containing the source pad.
1919 * @srcpadname: (allow-none): the name of the #GstPad in source element
1920 * or NULL for any pad.
1921 * @dest: (transfer none): the #GstElement containing the destination pad.
1922 * @destpadname: (allow-none): the name of the #GstPad in destination element
1923 * or NULL for any pad.
1924 * @filter: (transfer none) (allow-none): the #GstCaps to filter the link,
1925 * or #NULL for no filter.
1927 * Links the two named pads of the source and destination elements. Side effect
1928 * is that if one of the pads has no parent, it becomes a child of the parent of
1929 * the other element. If they have different parents, the link fails. If @caps
1930 * is not #NULL, makes sure that the caps of the link is a subset of @caps.
1932 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1935 gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
1936 GstElement * dest, const gchar * destpadname, GstCaps * filter)
1939 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1940 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1941 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), FALSE);
1944 GstElement *capsfilter;
1946 GstState state, pending;
1949 capsfilter = gst_element_factory_make ("capsfilter", NULL);
1951 GST_ERROR ("Could not make a capsfilter");
1955 parent = gst_object_get_parent (GST_OBJECT (src));
1956 g_return_val_if_fail (GST_IS_BIN (parent), FALSE);
1958 gst_element_get_state (GST_ELEMENT_CAST (parent), &state, &pending, 0);
1960 if (!gst_bin_add (GST_BIN (parent), capsfilter)) {
1961 GST_ERROR ("Could not add capsfilter");
1962 gst_object_unref (capsfilter);
1963 gst_object_unref (parent);
1967 if (pending != GST_STATE_VOID_PENDING)
1970 gst_element_set_state (capsfilter, state);
1972 gst_object_unref (parent);
1974 g_object_set (capsfilter, "caps", filter, NULL);
1976 lr1 = gst_element_link_pads (src, srcpadname, capsfilter, "sink");
1977 lr2 = gst_element_link_pads (capsfilter, "src", dest, destpadname);
1982 GST_INFO ("Could not link pads: %s:%s - capsfilter:sink",
1983 GST_ELEMENT_NAME (src), srcpadname);
1985 GST_INFO ("Could not link pads: capsfilter:src - %s:%s",
1986 GST_ELEMENT_NAME (dest), destpadname);
1988 gst_element_set_state (capsfilter, GST_STATE_NULL);
1989 /* this will unlink and unref as appropriate */
1990 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (capsfilter)), capsfilter);
1994 if (gst_element_link_pads (src, srcpadname, dest, destpadname)) {
1997 GST_INFO ("Could not link pads: %s:%s - %s:%s", GST_ELEMENT_NAME (src),
1998 srcpadname, GST_ELEMENT_NAME (dest), destpadname);
2006 * @src: (transfer none): a #GstElement containing the source pad.
2007 * @dest: (transfer none): the #GstElement containing the destination pad.
2009 * Links @src to @dest. The link must be from source to
2010 * destination; the other direction will not be tried. The function looks for
2011 * existing pads that aren't linked yet. It will request new pads if necessary.
2012 * Such pads need to be released manually when unlinking.
2013 * If multiple links are possible, only one is established.
2015 * Make sure you have added your elements to a bin or pipeline with
2016 * gst_bin_add() before trying to link them.
2018 * Returns: TRUE if the elements could be linked, FALSE otherwise.
2021 gst_element_link (GstElement * src, GstElement * dest)
2023 return gst_element_link_pads (src, NULL, dest, NULL);
2027 * gst_element_link_many:
2028 * @element_1: (transfer none): the first #GstElement in the link chain.
2029 * @element_2: (transfer none): the second #GstElement in the link chain.
2030 * @...: the NULL-terminated list of elements to link in order.
2032 * Chain together a series of elements. Uses gst_element_link().
2033 * Make sure you have added your elements to a bin or pipeline with
2034 * gst_bin_add() before trying to link them.
2036 * Returns: TRUE on success, FALSE otherwise.
2039 gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
2041 gboolean res = TRUE;
2044 g_return_val_if_fail (GST_IS_ELEMENT (element_1), FALSE);
2045 g_return_val_if_fail (GST_IS_ELEMENT (element_2), FALSE);
2047 va_start (args, element_2);
2050 if (!gst_element_link (element_1, element_2)) {
2055 element_1 = element_2;
2056 element_2 = va_arg (args, GstElement *);
2065 * gst_element_link_filtered:
2066 * @src: a #GstElement containing the source pad.
2067 * @dest: (transfer none): the #GstElement containing the destination pad.
2068 * @filter: (transfer none) (allow-none): the #GstCaps to filter the link,
2069 * or #NULL for no filter.
2071 * Links @src to @dest using the given caps as filtercaps.
2072 * The link must be from source to
2073 * destination; the other direction will not be tried. The function looks for
2074 * existing pads that aren't linked yet. It will request new pads if necessary.
2075 * If multiple links are possible, only one is established.
2077 * Make sure you have added your elements to a bin or pipeline with
2078 * gst_bin_add() before trying to link them.
2080 * Returns: TRUE if the pads could be linked, FALSE otherwise.
2083 gst_element_link_filtered (GstElement * src, GstElement * dest,
2086 return gst_element_link_pads_filtered (src, NULL, dest, NULL, filter);
2090 * gst_element_unlink_pads:
2091 * @src: a (transfer none): #GstElement containing the source pad.
2092 * @srcpadname: the name of the #GstPad in source element.
2093 * @dest: (transfer none): a #GstElement containing the destination pad.
2094 * @destpadname: the name of the #GstPad in destination element.
2096 * Unlinks the two named pads of the source and destination elements.
2098 * This is a convenience function for gst_pad_unlink().
2101 gst_element_unlink_pads (GstElement * src, const gchar * srcpadname,
2102 GstElement * dest, const gchar * destpadname)
2104 GstPad *srcpad, *destpad;
2105 gboolean srcrequest, destrequest;
2107 srcrequest = destrequest = FALSE;
2109 g_return_if_fail (src != NULL);
2110 g_return_if_fail (GST_IS_ELEMENT (src));
2111 g_return_if_fail (srcpadname != NULL);
2112 g_return_if_fail (dest != NULL);
2113 g_return_if_fail (GST_IS_ELEMENT (dest));
2114 g_return_if_fail (destpadname != NULL);
2116 /* obtain the pads requested */
2117 if (!(srcpad = gst_element_get_static_pad (src, srcpadname)))
2118 if ((srcpad = gst_element_get_request_pad (src, srcpadname)))
2120 if (srcpad == NULL) {
2121 GST_WARNING_OBJECT (src, "source element has no pad \"%s\"", srcpadname);
2124 if (!(destpad = gst_element_get_static_pad (dest, destpadname)))
2125 if ((destpad = gst_element_get_request_pad (dest, destpadname)))
2127 if (destpad == NULL) {
2128 GST_WARNING_OBJECT (dest, "destination element has no pad \"%s\"",
2133 /* we're satisfied they can be unlinked, let's do it */
2134 gst_pad_unlink (srcpad, destpad);
2137 gst_element_release_request_pad (dest, destpad);
2138 gst_object_unref (destpad);
2142 gst_element_release_request_pad (src, srcpad);
2143 gst_object_unref (srcpad);
2147 * gst_element_unlink_many:
2148 * @element_1: (transfer none): the first #GstElement in the link chain.
2149 * @element_2: (transfer none): the second #GstElement in the link chain.
2150 * @...: the NULL-terminated list of elements to unlink in order.
2152 * Unlinks a series of elements. Uses gst_element_unlink().
2155 gst_element_unlink_many (GstElement * element_1, GstElement * element_2, ...)
2159 g_return_if_fail (element_1 != NULL && element_2 != NULL);
2160 g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
2162 va_start (args, element_2);
2165 gst_element_unlink (element_1, element_2);
2167 element_1 = element_2;
2168 element_2 = va_arg (args, GstElement *);
2175 * gst_element_unlink:
2176 * @src: (transfer none): the source #GstElement to unlink.
2177 * @dest: (transfer none): the sink #GstElement to unlink.
2179 * Unlinks all source pads of the source element with all sink pads
2180 * of the sink element to which they are linked.
2182 * If the link has been made using gst_element_link(), it could have created an
2183 * requestpad, which has to be released using gst_element_release_request_pad().
2186 gst_element_unlink (GstElement * src, GstElement * dest)
2189 gboolean done = FALSE;
2190 GValue data = { 0, };
2192 g_return_if_fail (GST_IS_ELEMENT (src));
2193 g_return_if_fail (GST_IS_ELEMENT (dest));
2195 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unlinking \"%s\" and \"%s\"",
2196 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
2198 pads = gst_element_iterate_pads (src);
2200 switch (gst_iterator_next (pads, &data)) {
2201 case GST_ITERATOR_OK:
2203 GstPad *pad = g_value_get_object (&data);
2205 if (GST_PAD_IS_SRC (pad)) {
2206 GstPad *peerpad = gst_pad_get_peer (pad);
2208 /* see if the pad is linked and is really a pad of dest */
2210 GstElement *peerelem;
2212 peerelem = gst_pad_get_parent_element (peerpad);
2214 if (peerelem == dest) {
2215 gst_pad_unlink (pad, peerpad);
2218 gst_object_unref (peerelem);
2220 gst_object_unref (peerpad);
2223 g_value_reset (&data);
2226 case GST_ITERATOR_RESYNC:
2227 gst_iterator_resync (pads);
2229 case GST_ITERATOR_DONE:
2233 g_assert_not_reached ();
2237 g_value_unset (&data);
2238 gst_iterator_free (pads);
2242 * gst_element_query_position:
2243 * @element: a #GstElement to invoke the position query on.
2244 * @format: the #GstFormat requested
2245 * @cur: (out) (allow-none): a location in which to store the current
2246 * position, or NULL.
2248 * Queries an element for the stream position. If one repeatedly calls this
2249 * function one can also create and reuse it in gst_element_query().
2251 * Returns: TRUE if the query could be performed.
2254 gst_element_query_position (GstElement * element, GstFormat format,
2260 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2261 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2263 query = gst_query_new_position (format);
2264 ret = gst_element_query (element, query);
2267 gst_query_parse_position (query, NULL, cur);
2269 gst_query_unref (query);
2275 * gst_element_query_duration:
2276 * @element: a #GstElement to invoke the duration query on.
2277 * @format: the #GstFormat requested
2278 * @duration: (out): A location in which to store the total duration, or NULL.
2280 * Queries an element for the total stream duration.
2282 * Returns: TRUE if the query could be performed.
2285 gst_element_query_duration (GstElement * element, GstFormat format,
2291 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2292 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2294 query = gst_query_new_duration (format);
2295 ret = gst_element_query (element, query);
2298 gst_query_parse_duration (query, NULL, duration);
2300 gst_query_unref (query);
2306 * gst_element_query_convert:
2307 * @element: a #GstElement to invoke the convert query on.
2308 * @src_format: (inout): a #GstFormat to convert from.
2309 * @src_val: a value to convert.
2310 * @dest_format: the #GstFormat to convert to.
2311 * @dest_val: (out): a pointer to the result.
2313 * Queries an element to convert @src_val in @src_format to @dest_format.
2315 * Returns: TRUE if the query could be performed.
2318 gst_element_query_convert (GstElement * element, GstFormat src_format,
2319 gint64 src_val, GstFormat dest_format, gint64 * dest_val)
2324 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2325 g_return_val_if_fail (dest_format != GST_FORMAT_UNDEFINED, FALSE);
2326 g_return_val_if_fail (dest_val != NULL, FALSE);
2328 if (dest_format == src_format || src_val == -1) {
2329 *dest_val = src_val;
2333 query = gst_query_new_convert (src_format, src_val, dest_format);
2334 ret = gst_element_query (element, query);
2337 gst_query_parse_convert (query, NULL, NULL, NULL, dest_val);
2339 gst_query_unref (query);
2345 * gst_element_seek_simple
2346 * @element: a #GstElement to seek on
2347 * @format: a #GstFormat to execute the seek in, such as #GST_FORMAT_TIME
2348 * @seek_flags: seek options; playback applications will usually want to use
2349 * GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT here
2350 * @seek_pos: position to seek to (relative to the start); if you are doing
2351 * a seek in #GST_FORMAT_TIME this value is in nanoseconds -
2352 * multiply with #GST_SECOND to convert seconds to nanoseconds or
2353 * with #GST_MSECOND to convert milliseconds to nanoseconds.
2355 * Simple API to perform a seek on the given element, meaning it just seeks
2356 * to the given position relative to the start of the stream. For more complex
2357 * operations like segment seeks (e.g. for looping) or changing the playback
2358 * rate or seeking relative to the last configured playback segment you should
2359 * use gst_element_seek().
2361 * In a completely prerolled PAUSED or PLAYING pipeline, seeking is always
2362 * guaranteed to return %TRUE on a seekable media type or %FALSE when the media
2363 * type is certainly not seekable (such as a live stream).
2365 * Some elements allow for seeking in the READY state, in this
2366 * case they will store the seek event and execute it when they are put to
2367 * PAUSED. If the element supports seek in READY, it will always return %TRUE when
2368 * it receives the event in the READY state.
2370 * Returns: %TRUE if the seek operation succeeded (the seek might not always be
2371 * executed instantly though)
2376 gst_element_seek_simple (GstElement * element, GstFormat format,
2377 GstSeekFlags seek_flags, gint64 seek_pos)
2379 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2380 g_return_val_if_fail (seek_pos >= 0, FALSE);
2382 return gst_element_seek (element, 1.0, format, seek_flags,
2383 GST_SEEK_TYPE_SET, seek_pos, GST_SEEK_TYPE_NONE, 0);
2387 * gst_pad_use_fixed_caps:
2388 * @pad: the pad to use
2390 * A helper function you can use that sets the FIXED_CAPS flag
2391 * This way the default getcaps function will always return the negotiated caps
2392 * or in case the pad is not negotiated, the padtemplate caps.
2394 * Use this function on a pad that, once gst_pad_set_caps() has been called
2395 * on it, cannot be renegotiated to something else.
2398 gst_pad_use_fixed_caps (GstPad * pad)
2400 GST_OBJECT_FLAG_SET (pad, GST_PAD_FLAG_FIXED_CAPS);
2404 * gst_pad_get_parent_element:
2407 * Gets the parent of @pad, cast to a #GstElement. If a @pad has no parent or
2408 * its parent is not an element, return NULL.
2410 * Returns: (transfer full): the parent of the pad. The caller has a
2411 * reference on the parent, so unref when you're finished with it.
2416 gst_pad_get_parent_element (GstPad * pad)
2420 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2422 p = gst_object_get_parent (GST_OBJECT_CAST (pad));
2424 if (p && !GST_IS_ELEMENT (p)) {
2425 gst_object_unref (p);
2428 return GST_ELEMENT_CAST (p);
2432 * gst_object_default_error:
2433 * @source: the #GstObject that initiated the error.
2434 * @error: (in): the GError.
2435 * @debug: (in) (allow-none): an additional debug information string, or NULL
2437 * A default error function that uses g_printerr() to display the error message
2438 * and the optional debug sting..
2440 * The default handler will simply print the error string using g_print.
2443 gst_object_default_error (GstObject * source, const GError * error,
2444 const gchar * debug)
2446 gchar *name = gst_object_get_path_string (source);
2448 g_printerr (_("ERROR: from element %s: %s\n"), name, error->message);
2450 g_printerr (_("Additional debug info:\n%s\n"), debug);
2458 * @element_1: (transfer full): the #GstElement element to add to the bin
2459 * @...: (transfer full): additional elements to add to the bin
2461 * Adds a NULL-terminated list of elements to a bin. This function is
2462 * equivalent to calling gst_bin_add() for each member of the list. The return
2463 * value of each gst_bin_add() is ignored.
2466 gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
2470 g_return_if_fail (GST_IS_BIN (bin));
2471 g_return_if_fail (GST_IS_ELEMENT (element_1));
2473 va_start (args, element_1);
2476 gst_bin_add (bin, element_1);
2478 element_1 = va_arg (args, GstElement *);
2485 * gst_bin_remove_many:
2487 * @element_1: (transfer none): the first #GstElement to remove from the bin
2488 * @...: (transfer none): NULL-terminated list of elements to remove from the bin
2490 * Remove a list of elements from a bin. This function is equivalent
2491 * to calling gst_bin_remove() with each member of the list.
2494 gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
2498 g_return_if_fail (GST_IS_BIN (bin));
2499 g_return_if_fail (GST_IS_ELEMENT (element_1));
2501 va_start (args, element_1);
2504 gst_bin_remove (bin, element_1);
2506 element_1 = va_arg (args, GstElement *);
2513 gst_element_populate_std_props (GObjectClass * klass, const gchar * prop_name,
2514 guint arg_id, GParamFlags flags)
2516 GQuark prop_id = g_quark_from_string (prop_name);
2519 static GQuark fd_id = 0;
2520 static GQuark blocksize_id;
2521 static GQuark bytesperread_id;
2522 static GQuark dump_id;
2523 static GQuark filesize_id;
2524 static GQuark mmapsize_id;
2525 static GQuark location_id;
2526 static GQuark offset_id;
2527 static GQuark silent_id;
2528 static GQuark touch_id;
2530 flags |= G_PARAM_STATIC_STRINGS;
2533 fd_id = g_quark_from_static_string ("fd");
2534 blocksize_id = g_quark_from_static_string ("blocksize");
2535 bytesperread_id = g_quark_from_static_string ("bytesperread");
2536 dump_id = g_quark_from_static_string ("dump");
2537 filesize_id = g_quark_from_static_string ("filesize");
2538 mmapsize_id = g_quark_from_static_string ("mmapsize");
2539 location_id = g_quark_from_static_string ("location");
2540 offset_id = g_quark_from_static_string ("offset");
2541 silent_id = g_quark_from_static_string ("silent");
2542 touch_id = g_quark_from_static_string ("touch");
2545 if (prop_id == fd_id) {
2546 pspec = g_param_spec_int ("fd", "File-descriptor",
2547 "File-descriptor for the file being read", 0, G_MAXINT, 0, flags);
2548 } else if (prop_id == blocksize_id) {
2549 pspec = g_param_spec_ulong ("blocksize", "Block Size",
2550 "Block size to read per buffer", 0, G_MAXULONG, 4096, flags);
2552 } else if (prop_id == bytesperread_id) {
2553 pspec = g_param_spec_int ("bytesperread", "Bytes per read",
2554 "Number of bytes to read per buffer", G_MININT, G_MAXINT, 0, flags);
2556 } else if (prop_id == dump_id) {
2557 pspec = g_param_spec_boolean ("dump", "Dump",
2558 "Dump bytes to stdout", FALSE, flags);
2560 } else if (prop_id == filesize_id) {
2561 pspec = g_param_spec_int64 ("filesize", "File Size",
2562 "Size of the file being read", 0, G_MAXINT64, 0, flags);
2564 } else if (prop_id == mmapsize_id) {
2565 pspec = g_param_spec_ulong ("mmapsize", "mmap() Block Size",
2566 "Size in bytes of mmap()d regions", 0, G_MAXULONG, 4 * 1048576, flags);
2568 } else if (prop_id == location_id) {
2569 pspec = g_param_spec_string ("location", "File Location",
2570 "Location of the file to read", NULL, flags);
2572 } else if (prop_id == offset_id) {
2573 pspec = g_param_spec_int64 ("offset", "File Offset",
2574 "Byte offset of current read pointer", 0, G_MAXINT64, 0, flags);
2576 } else if (prop_id == silent_id) {
2577 pspec = g_param_spec_boolean ("silent", "Silent", "Don't produce events",
2580 } else if (prop_id == touch_id) {
2581 pspec = g_param_spec_boolean ("touch", "Touch read data",
2582 "Touch data to force disk read before " "push ()", TRUE, flags);
2584 g_warning ("Unknown - 'standard' property '%s' id %d from klass %s",
2585 prop_name, arg_id, g_type_name (G_OBJECT_CLASS_TYPE (klass)));
2590 g_object_class_install_property (klass, arg_id, pspec);
2595 * gst_element_class_install_std_props:
2596 * @klass: the #GstElementClass to add the properties to.
2597 * @first_name: the name of the first property.
2598 * in a NULL terminated
2599 * @...: the id and flags of the first property, followed by
2600 * further 'name', 'id', 'flags' triplets and terminated by NULL.
2602 * Adds a list of standardized properties with types to the @klass.
2603 * the id is for the property switch in your get_prop method, and
2604 * the flags determine readability / writeability.
2607 gst_element_class_install_std_props (GstElementClass * klass,
2608 const gchar * first_name, ...)
2614 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
2616 va_start (args, first_name);
2621 int arg_id = va_arg (args, int);
2622 GParamFlags flags = (GParamFlags) va_arg (args, int);
2624 gst_element_populate_std_props ((GObjectClass *) klass, name, arg_id,
2627 name = va_arg (args, char *);
2636 * @buf1: (transfer none): the first source #GstBuffer to merge.
2637 * @buf2: (transfer none): the second source #GstBuffer to merge.
2639 * Create a new buffer that is the concatenation of the two source
2640 * buffers. The original source buffers will not be modified or
2641 * unref'd. Make sure you unref the source buffers if they are not used
2642 * anymore afterwards.
2644 * If the buffers point to contiguous areas of memory, the buffer
2645 * is created without copying the data.
2647 * Free-function: gst_buffer_unref
2649 * Returns: (transfer full): the new #GstBuffer which is the concatenation
2650 * of the source buffers.
2653 gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2)
2658 size1 = gst_buffer_get_size (buf1);
2659 size2 = gst_buffer_get_size (buf2);
2661 /* we're just a specific case of the more general gst_buffer_span() */
2662 result = gst_buffer_span (buf1, 0, buf2, size1 + size2);
2669 * @buf1: (transfer full): the first source #GstBuffer.
2670 * @buf2: (transfer full): the second source #GstBuffer.
2672 * Create a new buffer that is the concatenation of the two source
2673 * buffers, and unrefs the original source buffers.
2675 * If the buffers point to contiguous areas of memory, the buffer
2676 * is created without copying the data.
2678 * This is a convenience function for C programmers. See also
2679 * gst_buffer_merge(), which does the same thing without
2680 * unreffing the input parameters. Language bindings without
2681 * explicit reference counting should not wrap this function.
2683 * Returns: (transfer full): the new #GstBuffer which is the concatenation of
2684 * the source buffers.
2687 gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2)
2692 size1 = gst_buffer_get_size (buf1);
2693 size2 = gst_buffer_get_size (buf2);
2695 result = gst_buffer_span (buf1, 0, buf2, size1 + size2);
2696 gst_buffer_unref (buf1);
2697 gst_buffer_unref (buf2);
2706 } QueryAcceptCapsData;
2709 query_accept_caps_func (GstPad * pad, QueryAcceptCapsData * data)
2711 if (G_LIKELY (gst_pad_peer_query (pad, data->query))) {
2714 gst_query_parse_accept_caps_result (data->query, &result);
2715 data->ret &= result;
2721 * gst_pad_proxy_query_accept_caps:
2722 * @pad: a #GstPad to proxy.
2723 * @query: an ACCEPT_CAPS #GstQuery.
2725 * Calls gst_pad_accept_caps() for all internally linked pads of @pad and
2726 * returns the intersection of the results.
2728 * This function is useful as a default accept caps query function for an element
2729 * that can handle any stream format, but requires caps that are acceptable for
2732 * Returns: TRUE if @query could be executed
2735 gst_pad_proxy_query_accept_caps (GstPad * pad, GstQuery * query)
2737 QueryAcceptCapsData data;
2739 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2740 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2741 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS, FALSE);
2743 GST_CAT_DEBUG (GST_CAT_PADS, "proxying accept caps query for %s:%s",
2744 GST_DEBUG_PAD_NAME (pad));
2747 /* value to hold the return, by default it holds TRUE */
2750 gst_pad_forward (pad, (GstPadForwardFunction) query_accept_caps_func, &data);
2751 gst_query_set_accept_caps_result (query, data.ret);
2763 query_caps_func (GstPad * pad, QueryCapsData * data)
2765 gboolean empty = FALSE;
2767 if (G_LIKELY (gst_pad_peer_query (pad, data->query))) {
2768 GstCaps *peercaps, *intersection;
2770 gst_query_parse_caps_result (data->query, &peercaps);
2771 GST_DEBUG_OBJECT (pad, "intersect with result %" GST_PTR_FORMAT, peercaps);
2772 intersection = gst_caps_intersect (data->ret, peercaps);
2773 GST_DEBUG_OBJECT (pad, "intersected %" GST_PTR_FORMAT, intersection);
2775 gst_caps_unref (data->ret);
2776 data->ret = intersection;
2778 /* stop when empty */
2779 empty = gst_caps_is_empty (intersection);
2785 * gst_pad_proxy_query_caps:
2786 * @pad: a #GstPad to proxy.
2787 * @query: a CAPS #GstQuery.
2789 * Calls gst_pad_query_caps() for all internally linked pads fof @pad and returns
2790 * the intersection of the results.
2792 * This function is useful as a default caps query function for an element
2793 * that can handle any stream format, but requires all its pads to have
2794 * the same caps. Two such elements are tee and adder.
2796 * Returns: TRUE if @query could be executed
2799 gst_pad_proxy_query_caps (GstPad * pad, GstQuery * query)
2801 GstCaps *intersected;
2804 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2805 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
2806 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS, FALSE);
2808 GST_CAT_DEBUG (GST_CAT_PADS, "proxying caps query for %s:%s",
2809 GST_DEBUG_PAD_NAME (pad));
2812 /* value to hold the return, by default it holds ANY */
2813 data.ret = gst_caps_new_any ();
2815 gst_pad_forward (pad, (GstPadForwardFunction) query_caps_func, &data);
2819 gst_caps_intersect (data.ret, gst_pad_get_pad_template_caps (pad));
2820 gst_caps_unref (data.ret);
2822 intersected = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
2824 gst_query_set_caps_result (query, intersected);
2825 gst_caps_unref (intersected);
2831 * gst_pad_query_position:
2832 * @pad: a #GstPad to invoke the position query on.
2833 * @format: the #GstFormat requested
2834 * @cur: (out): A location in which to store the current position, or NULL.
2836 * Queries a pad for the stream position.
2838 * Returns: TRUE if the query could be performed.
2841 gst_pad_query_position (GstPad * pad, GstFormat format, gint64 * cur)
2846 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2847 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2849 query = gst_query_new_position (format);
2850 if ((ret = gst_pad_query (pad, query)))
2851 gst_query_parse_position (query, NULL, cur);
2852 gst_query_unref (query);
2858 * gst_pad_peer_query_position:
2859 * @pad: a #GstPad on whose peer to invoke the position query on.
2860 * Must be a sink pad.
2861 * @format: the #GstFormat requested
2862 * @cur: (out) (allow-none): a location in which to store the current
2863 * position, or NULL.
2865 * Queries the peer of a given sink pad for the stream position.
2867 * Returns: TRUE if the query could be performed.
2870 gst_pad_peer_query_position (GstPad * pad, GstFormat format, gint64 * cur)
2873 gboolean ret = FALSE;
2875 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2876 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2877 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2879 query = gst_query_new_position (format);
2880 if ((ret = gst_pad_peer_query (pad, query)))
2881 gst_query_parse_position (query, NULL, cur);
2882 gst_query_unref (query);
2888 * gst_pad_query_duration:
2889 * @pad: a #GstPad to invoke the duration query on.
2890 * @format: the #GstFormat requested
2891 * @duration: (out) (allow-none): a location in which to store the total
2892 * duration, or NULL.
2894 * Queries a pad for the total stream duration.
2896 * Returns: TRUE if the query could be performed.
2899 gst_pad_query_duration (GstPad * pad, GstFormat format, gint64 * duration)
2904 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2905 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2907 query = gst_query_new_duration (format);
2908 if ((ret = gst_pad_query (pad, query)))
2909 gst_query_parse_duration (query, NULL, duration);
2910 gst_query_unref (query);
2916 * gst_pad_peer_query_duration:
2917 * @pad: a #GstPad on whose peer pad to invoke the duration query on.
2918 * Must be a sink pad.
2919 * @format: the #GstFormat requested
2920 * @duration: (out) (allow-none): a location in which to store the total
2921 * duration, or NULL.
2923 * Queries the peer pad of a given sink pad for the total stream duration.
2925 * Returns: TRUE if the query could be performed.
2928 gst_pad_peer_query_duration (GstPad * pad, GstFormat format, gint64 * duration)
2931 gboolean ret = FALSE;
2933 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2934 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2935 g_return_val_if_fail (format != GST_FORMAT_UNDEFINED, FALSE);
2937 query = gst_query_new_duration (format);
2938 if ((ret = gst_pad_peer_query (pad, query)))
2939 gst_query_parse_duration (query, NULL, duration);
2940 gst_query_unref (query);
2946 * gst_pad_query_convert:
2947 * @pad: a #GstPad to invoke the convert query on.
2948 * @src_format: a #GstFormat to convert from.
2949 * @src_val: a value to convert.
2950 * @dest_format: the #GstFormat to convert to.
2951 * @dest_val: (out): a pointer to the result.
2953 * Queries a pad to convert @src_val in @src_format to @dest_format.
2955 * Returns: TRUE if the query could be performed.
2958 gst_pad_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2959 GstFormat dest_format, gint64 * dest_val)
2964 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2965 g_return_val_if_fail (dest_format != GST_FORMAT_UNDEFINED, FALSE);
2966 g_return_val_if_fail (dest_val != NULL, FALSE);
2968 if (dest_format == src_format || src_val == -1) {
2969 *dest_val = src_val;
2973 query = gst_query_new_convert (src_format, src_val, dest_format);
2974 if ((ret = gst_pad_query (pad, query)))
2975 gst_query_parse_convert (query, NULL, NULL, NULL, dest_val);
2976 gst_query_unref (query);
2982 * gst_pad_peer_query_convert:
2983 * @pad: a #GstPad, on whose peer pad to invoke the convert query on.
2984 * Must be a sink pad.
2985 * @src_format: a #GstFormat to convert from.
2986 * @src_val: a value to convert.
2987 * @dest_format: the #GstFormat to convert to.
2988 * @dest_val: (out): a pointer to the result.
2990 * Queries the peer pad of a given sink pad to convert @src_val in @src_format
2993 * Returns: TRUE if the query could be performed.
2996 gst_pad_peer_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2997 GstFormat dest_format, gint64 * dest_val)
3000 gboolean ret = FALSE;
3002 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3003 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
3004 g_return_val_if_fail (dest_format != GST_FORMAT_UNDEFINED, FALSE);
3005 g_return_val_if_fail (dest_val != NULL, FALSE);
3007 query = gst_query_new_convert (src_format, src_val, dest_format);
3008 if ((ret = gst_pad_peer_query (pad, query)))
3009 gst_query_parse_convert (query, NULL, NULL, NULL, dest_val);
3010 gst_query_unref (query);
3016 * gst_pad_query_caps:
3017 * @pad: a #GstPad to get the capabilities of.
3018 * @filter: suggested #GstCaps.
3020 * Gets the capabilities this pad can produce or consume.
3021 * Note that this method doesn't necessarily return the caps set by
3022 * gst_pad_set_caps() - use gst_pad_get_current_caps() for that instead.
3023 * gst_pad_query_caps returns all possible caps a pad can operate with, using
3024 * the pad's CAPS query function, If the query fails, this function will return
3025 * @filter, if not #NULL, otherwise ANY.
3027 * When called on sinkpads @filter contains the caps that
3028 * upstream could produce in the order preferred by upstream. When
3029 * called on srcpads @filter contains the caps accepted by
3030 * downstream in the preffered order. @filter might be %NULL but
3031 * if it is not %NULL the returned caps will be a subset of @filter.
3033 * Note that this function does not return writable #GstCaps, use
3034 * gst_caps_make_writable() before modifying the caps.
3036 * Returns: (transfer full): the caps of the pad with incremented ref-count.
3039 gst_pad_query_caps (GstPad * pad, GstCaps * filter)
3041 GstCaps *result = NULL;
3044 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3045 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
3047 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "get pad caps");
3049 query = gst_query_new_caps (filter);
3050 if (gst_pad_query (pad, query)) {
3051 gst_query_parse_caps_result (query, &result);
3052 gst_caps_ref (result);
3053 GST_DEBUG_OBJECT (pad, "query returned %" GST_PTR_FORMAT, result);
3054 } else if (filter) {
3055 result = gst_caps_ref (filter);
3057 result = gst_caps_new_any ();
3059 gst_query_unref (query);
3065 * gst_pad_peer_query_caps:
3066 * @pad: a #GstPad to get the capabilities of.
3067 * @filter: a #GstCaps filter.
3069 * Gets the capabilities of the peer connected to this pad. Similar to
3070 * gst_pad_query_caps().
3072 * When called on srcpads @filter contains the caps that
3073 * upstream could produce in the order preferred by upstream. When
3074 * called on sinkpads @filter contains the caps accepted by
3075 * downstream in the preffered order. @filter might be %NULL but
3076 * if it is not %NULL the returned caps will be a subset of @filter.
3078 * Returns: the caps of the peer pad with incremented ref-count. This function
3079 * returns %NULL when there is no peer pad.
3082 gst_pad_peer_query_caps (GstPad * pad, GstCaps * filter)
3084 GstCaps *result = NULL;
3087 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
3088 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), NULL);
3090 query = gst_query_new_caps (filter);
3091 if (gst_pad_peer_query (pad, query)) {
3092 gst_query_parse_caps_result (query, &result);
3093 gst_caps_ref (result);
3094 GST_DEBUG_OBJECT (pad, "peer query returned %" GST_PTR_FORMAT, result);
3095 } else if (filter) {
3096 result = gst_caps_ref (filter);
3098 result = gst_caps_new_any ();
3100 gst_query_unref (query);
3106 * gst_pad_query_accept_caps:
3107 * @pad: a #GstPad to check
3108 * @caps: a #GstCaps to check on the pad
3110 * Check if the given pad accepts the caps.
3112 * Returns: TRUE if the pad can accept the caps.
3115 gst_pad_query_accept_caps (GstPad * pad, GstCaps * caps)
3117 gboolean res = TRUE;
3120 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3121 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
3123 GST_CAT_DEBUG_OBJECT (GST_CAT_CAPS, pad, "accept caps of %p", caps);
3125 query = gst_query_new_accept_caps (caps);
3126 if (gst_pad_query (pad, query)) {
3127 gst_query_parse_accept_caps_result (query, &res);
3128 GST_DEBUG_OBJECT (pad, "query returned %d", res);
3130 gst_query_unref (query);
3136 * gst_pad_peer_query_accept_caps:
3137 * @pad: a #GstPad to check the peer of
3138 * @caps: a #GstCaps to check on the pad
3140 * Check if the peer of @pad accepts @caps. If @pad has no peer, this function
3143 * Returns: TRUE if the peer of @pad can accept the caps or @pad has no peer.
3146 gst_pad_peer_query_accept_caps (GstPad * pad, GstCaps * caps)
3148 gboolean res = TRUE;
3151 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3152 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
3154 query = gst_query_new_accept_caps (caps);
3155 if (gst_pad_peer_query (pad, query)) {
3156 gst_query_parse_accept_caps_result (query, &res);
3157 GST_DEBUG_OBJECT (pad, "query returned %d", res);
3159 gst_query_unref (query);
3165 element_find_unlinked_pad (GstElement * element, GstPadDirection direction)
3168 GstPad *unlinked_pad = NULL;
3170 GValue data = { 0, };
3172 switch (direction) {
3174 iter = gst_element_iterate_src_pads (element);
3177 iter = gst_element_iterate_sink_pads (element);
3180 g_return_val_if_reached (NULL);
3185 switch (gst_iterator_next (iter, &data)) {
3186 case GST_ITERATOR_OK:{
3188 GstPad *pad = g_value_get_object (&data);
3190 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
3191 GST_DEBUG_PAD_NAME (pad));
3193 peer = gst_pad_get_peer (pad);
3195 unlinked_pad = gst_object_ref (pad);
3197 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
3198 "found existing unlinked pad %s:%s",
3199 GST_DEBUG_PAD_NAME (unlinked_pad));
3201 gst_object_unref (peer);
3203 g_value_reset (&data);
3206 case GST_ITERATOR_DONE:
3209 case GST_ITERATOR_RESYNC:
3210 gst_iterator_resync (iter);
3212 case GST_ITERATOR_ERROR:
3213 g_return_val_if_reached (NULL);
3217 g_value_unset (&data);
3218 gst_iterator_free (iter);
3220 return unlinked_pad;
3224 * gst_bin_find_unlinked_pad:
3225 * @bin: bin in which to look for elements with unlinked pads
3226 * @direction: whether to look for an unlinked source or sink pad
3228 * Recursively looks for elements with an unlinked pad of the given
3229 * direction within the specified bin and returns an unlinked pad
3230 * if one is found, or NULL otherwise. If a pad is found, the caller
3231 * owns a reference to it and should use gst_object_unref() on the
3232 * pad when it is not needed any longer.
3234 * Returns: (transfer full): unlinked pad of the given direction, or NULL.
3239 gst_bin_find_unlinked_pad (GstBin * bin, GstPadDirection direction)
3244 GValue data = { 0, };
3246 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3247 g_return_val_if_fail (direction != GST_PAD_UNKNOWN, NULL);
3250 iter = gst_bin_iterate_recurse (bin);
3252 switch (gst_iterator_next (iter, &data)) {
3253 case GST_ITERATOR_OK:{
3254 GstElement *element = g_value_get_object (&data);
3256 pad = element_find_unlinked_pad (element, direction);
3259 g_value_reset (&data);
3262 case GST_ITERATOR_DONE:
3265 case GST_ITERATOR_RESYNC:
3266 gst_iterator_resync (iter);
3268 case GST_ITERATOR_ERROR:
3269 g_return_val_if_reached (NULL);
3273 g_value_unset (&data);
3274 gst_iterator_free (iter);
3280 * gst_parse_bin_from_description:
3281 * @bin_description: command line describing the bin
3282 * @ghost_unlinked_pads: whether to automatically create ghost pads
3283 * for unlinked source or sink pads within the bin
3284 * @err: where to store the error message in case of an error, or NULL
3286 * This is a convenience wrapper around gst_parse_launch() to create a
3287 * #GstBin from a gst-launch-style pipeline description. See
3288 * gst_parse_launch() and the gst-launch man page for details about the
3289 * syntax. Ghost pads on the bin for unlinked source or sink pads
3290 * within the bin can automatically be created (but only a maximum of
3291 * one ghost pad for each direction will be created; if you expect
3292 * multiple unlinked source pads or multiple unlinked sink pads
3293 * and want them all ghosted, you will have to create the ghost pads
3296 * Returns: (transfer full): a newly-created bin, or NULL if an error occurred.
3301 gst_parse_bin_from_description (const gchar * bin_description,
3302 gboolean ghost_unlinked_pads, GError ** err)
3304 return gst_parse_bin_from_description_full (bin_description,
3305 ghost_unlinked_pads, NULL, GST_PARSE_FLAG_NONE, err);
3309 * gst_parse_bin_from_description_full:
3310 * @bin_description: command line describing the bin
3311 * @ghost_unlinked_pads: whether to automatically create ghost pads
3312 * for unlinked source or sink pads within the bin
3313 * @context: (transfer none) (allow-none): a parse context allocated with
3314 * gst_parse_context_new(), or %NULL
3315 * @flags: parsing options, or #GST_PARSE_FLAG_NONE
3316 * @err: where to store the error message in case of an error, or NULL
3318 * This is a convenience wrapper around gst_parse_launch() to create a
3319 * #GstBin from a gst-launch-style pipeline description. See
3320 * gst_parse_launch() and the gst-launch man page for details about the
3321 * syntax. Ghost pads on the bin for unlinked source or sink pads
3322 * within the bin can automatically be created (but only a maximum of
3323 * one ghost pad for each direction will be created; if you expect
3324 * multiple unlinked source pads or multiple unlinked sink pads
3325 * and want them all ghosted, you will have to create the ghost pads
3328 * Returns: (transfer full): a newly-created bin, or NULL if an error occurred.
3333 gst_parse_bin_from_description_full (const gchar * bin_description,
3334 gboolean ghost_unlinked_pads, GstParseContext * context,
3335 GstParseFlags flags, GError ** err)
3337 #ifndef GST_DISABLE_PARSE
3342 g_return_val_if_fail (bin_description != NULL, NULL);
3343 g_return_val_if_fail (err == NULL || *err == NULL, NULL);
3345 GST_DEBUG ("Making bin from description '%s'", bin_description);
3347 /* parse the pipeline to a bin */
3348 desc = g_strdup_printf ("bin.( %s )", bin_description);
3349 bin = (GstBin *) gst_parse_launch_full (desc, context, flags, err);
3352 if (bin == NULL || (err && *err != NULL)) {
3354 gst_object_unref (bin);
3358 /* find pads and ghost them if necessary */
3359 if (ghost_unlinked_pads) {
3360 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC))) {
3361 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("src", pad));
3362 gst_object_unref (pad);
3364 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SINK))) {
3365 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("sink", pad));
3366 gst_object_unref (pad);
3370 return GST_ELEMENT (bin);
3374 GST_WARNING ("Disabled API called");
3376 msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
3377 g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
3385 * gst_util_get_timestamp:
3387 * Get a timestamp as GstClockTime to be used for interval measurements.
3388 * The timestamp should not be interpreted in any other way.
3390 * Returns: the timestamp
3395 gst_util_get_timestamp (void)
3397 #if defined (HAVE_POSIX_TIMERS) && defined(HAVE_MONOTONIC_CLOCK)
3398 struct timespec now;
3400 clock_gettime (CLOCK_MONOTONIC, &now);
3401 return GST_TIMESPEC_TO_TIME (now);
3405 g_get_current_time (&now);
3406 return GST_TIMEVAL_TO_TIME (now);
3411 * gst_util_array_binary_search:
3412 * @array: the sorted input array
3413 * @num_elements: number of elements in the array
3414 * @element_size: size of every element in bytes
3415 * @search_func: (scope call): function to compare two elements, @search_data will always be passed as second argument
3416 * @mode: search mode that should be used
3417 * @search_data: element that should be found
3418 * @user_data: (closure): data to pass to @search_func
3420 * Searches inside @array for @search_data by using the comparison function
3421 * @search_func. @array must be sorted ascending.
3423 * As @search_data is always passed as second argument to @search_func it's
3424 * not required that @search_data has the same type as the array elements.
3426 * The complexity of this search function is O(log (num_elements)).
3428 * Returns: (transfer none): The address of the found element or %NULL if nothing was found
3433 gst_util_array_binary_search (gpointer array, guint num_elements,
3434 gsize element_size, GCompareDataFunc search_func, GstSearchMode mode,
3435 gconstpointer search_data, gpointer user_data)
3437 glong left = 0, right = num_elements - 1, m;
3439 guint8 *data = (guint8 *) array;
3441 g_return_val_if_fail (array != NULL, NULL);
3442 g_return_val_if_fail (element_size > 0, NULL);
3443 g_return_val_if_fail (search_func != NULL, NULL);
3445 /* 0. No elements => return NULL */
3446 if (num_elements == 0)
3449 /* 1. If search_data is before the 0th element return the 0th element */
3450 ret = search_func (data, search_data, user_data);
3451 if ((ret >= 0 && mode == GST_SEARCH_MODE_AFTER) || ret == 0)
3456 /* 2. If search_data is after the last element return the last element */
3458 search_func (data + (num_elements - 1) * element_size, search_data,
3460 if ((ret <= 0 && mode == GST_SEARCH_MODE_BEFORE) || ret == 0)
3461 return data + (num_elements - 1) * element_size;
3465 /* 3. else binary search */
3467 m = left + (right - left) / 2;
3469 ret = search_func (data + m * element_size, search_data, user_data);
3472 return data + m * element_size;
3473 } else if (ret < 0) {
3479 /* No exact match found */
3481 if (mode == GST_SEARCH_MODE_EXACT) {
3483 } else if (mode == GST_SEARCH_MODE_AFTER) {
3485 return (m < num_elements) ? data + (m + 1) * element_size : NULL;
3487 return data + m * element_size;
3490 return data + m * element_size;
3492 return (m > 0) ? data + (m - 1) * element_size : NULL;
3498 /* Finds the greatest common divisor.
3499 * Returns 1 if none other found.
3500 * This is Euclid's algorithm. */
3503 * gst_util_greatest_common_divisor:
3504 * @a: First value as #gint
3505 * @b: Second value as #gint
3507 * Calculates the greatest common divisor of @a
3510 * Returns: Greatest common divisor of @a and @b
3515 gst_util_greatest_common_divisor (gint a, gint b)
3528 * gst_util_fraction_to_double:
3529 * @src_n: Fraction numerator as #gint
3530 * @src_d: Fraction denominator #gint
3531 * @dest: (out): pointer to a #gdouble for the result
3533 * Transforms a fraction to a #gdouble.
3538 gst_util_fraction_to_double (gint src_n, gint src_d, gdouble * dest)
3540 g_return_if_fail (dest != NULL);
3541 g_return_if_fail (src_d != 0);
3543 *dest = ((gdouble) src_n) / ((gdouble) src_d);
3546 #define MAX_TERMS 30
3547 #define MIN_DIVISOR 1.0e-10
3548 #define MAX_ERROR 1.0e-20
3550 /* use continued fractions to transform a double into a fraction,
3551 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
3552 * This algorithm takes care of overflows.
3556 * gst_util_double_to_fraction:
3557 * @src: #gdouble to transform
3558 * @dest_n: (out): pointer to a #gint to hold the result numerator
3559 * @dest_d: (out): pointer to a #gint to hold the result denominator
3561 * Transforms a #gdouble to a fraction and simplifies
3567 gst_util_double_to_fraction (gdouble src, gint * dest_n, gint * dest_d)
3570 gdouble V, F; /* double being converted */
3571 gint N, D; /* will contain the result */
3572 gint A; /* current term in continued fraction */
3573 gint64 N1, D1; /* numerator, denominator of last approx */
3574 gint64 N2, D2; /* numerator, denominator of previous approx */
3577 gboolean negative = FALSE;
3579 g_return_if_fail (dest_n != NULL);
3580 g_return_if_fail (dest_d != NULL);
3582 /* initialize fraction being converted */
3590 /* initialize fractions with 1/0, 0/1 */
3598 for (i = 0; i < MAX_TERMS; i++) {
3600 A = (gint) F; /* no floor() needed, F is always >= 0 */
3601 /* get new divisor */
3604 /* calculate new fraction in temp */
3608 /* guard against overflow */
3609 if (N2 > G_MAXINT || D2 > G_MAXINT) {
3616 /* save last two fractions */
3622 /* quit if dividing by zero or close enough to target */
3623 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
3627 /* Take reciprocal */
3630 /* fix for overflow */
3635 /* fix for negative */
3640 gcd = gst_util_greatest_common_divisor (N, D);
3652 * gst_util_fraction_multiply:
3653 * @a_n: Numerator of first value
3654 * @a_d: Denominator of first value
3655 * @b_n: Numerator of second value
3656 * @b_d: Denominator of second value
3657 * @res_n: (out): Pointer to #gint to hold the result numerator
3658 * @res_d: (out): Pointer to #gint to hold the result denominator
3660 * Multiplies the fractions @a_n/@a_d and @b_n/@b_d and stores
3661 * the result in @res_n and @res_d.
3663 * Returns: %FALSE on overflow, %TRUE otherwise.
3668 gst_util_fraction_multiply (gint a_n, gint a_d, gint b_n, gint b_d,
3669 gint * res_n, gint * res_d)
3673 g_return_val_if_fail (res_n != NULL, FALSE);
3674 g_return_val_if_fail (res_d != NULL, FALSE);
3675 g_return_val_if_fail (a_d != 0, FALSE);
3676 g_return_val_if_fail (b_d != 0, FALSE);
3678 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3682 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3686 gcd = gst_util_greatest_common_divisor (a_n, b_d);
3690 gcd = gst_util_greatest_common_divisor (a_d, b_n);
3694 /* This would result in overflow */
3695 if (a_n != 0 && G_MAXINT / ABS (a_n) < ABS (b_n))
3697 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3703 gcd = gst_util_greatest_common_divisor (*res_n, *res_d);
3711 * gst_util_fraction_add:
3712 * @a_n: Numerator of first value
3713 * @a_d: Denominator of first value
3714 * @b_n: Numerator of second value
3715 * @b_d: Denominator of second value
3716 * @res_n: (out): Pointer to #gint to hold the result numerator
3717 * @res_d: (out): Pointer to #gint to hold the result denominator
3719 * Adds the fractions @a_n/@a_d and @b_n/@b_d and stores
3720 * the result in @res_n and @res_d.
3722 * Returns: %FALSE on overflow, %TRUE otherwise.
3727 gst_util_fraction_add (gint a_n, gint a_d, gint b_n, gint b_d, gint * res_n,
3732 g_return_val_if_fail (res_n != NULL, FALSE);
3733 g_return_val_if_fail (res_d != NULL, FALSE);
3734 g_return_val_if_fail (a_d != 0, FALSE);
3735 g_return_val_if_fail (b_d != 0, FALSE);
3737 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3741 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3756 /* This would result in overflow */
3757 if (G_MAXINT / ABS (a_n) < ABS (b_n))
3759 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3761 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3764 *res_n = (a_n * b_d) + (a_d * b_n);
3767 gcd = gst_util_greatest_common_divisor (*res_n, *res_d);
3780 * gst_util_fraction_compare:
3781 * @a_n: Numerator of first value
3782 * @a_d: Denominator of first value
3783 * @b_n: Numerator of second value
3784 * @b_d: Denominator of second value
3786 * Compares the fractions @a_n/@a_d and @b_n/@b_d and returns
3787 * -1 if a < b, 0 if a = b and 1 if a > b.
3789 * Returns: -1 if a < b; 0 if a = b; 1 if a > b.
3794 gst_util_fraction_compare (gint a_n, gint a_d, gint b_n, gint b_d)
3800 g_return_val_if_fail (a_d != 0 && b_d != 0, 0);
3803 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3807 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3811 /* fractions are reduced when set, so we can quickly see if they're equal */
3812 if (a_n == b_n && a_d == b_d)
3815 /* extend to 64 bits */
3816 new_num_1 = ((gint64) a_n) * b_d;
3817 new_num_2 = ((gint64) b_n) * a_d;
3818 if (new_num_1 < new_num_2)
3820 if (new_num_1 > new_num_2)
3823 /* Should not happen because a_d and b_d are not 0 */
3824 g_return_val_if_reached (0);