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
28 * When defining own plugins, use the GST_BOILERPLATE ease gobject creation.
31 #include "gst_private.h"
35 #include "gstghostpad.h"
41 #include "gst-i18n-lib.h"
42 #include "glib-compat-private.h"
47 * @mem: a pointer to the memory to dump
48 * @size: the size of the memory block to dump
50 * Dumps the memory block into a hex representation. Useful for debugging.
53 gst_util_dump_mem (const guchar * mem, guint size)
56 GString *string = g_string_sized_new (50);
57 GString *chars = g_string_sized_new (18);
61 if (g_ascii_isprint (mem[i]))
62 g_string_append_c (chars, mem[i]);
64 g_string_append_c (chars, '.');
66 g_string_append_printf (string, "%02x ", mem[i]);
71 if (j == 16 || i == size) {
72 g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j, mem + i - j,
73 string->str, chars->str);
74 g_string_set_size (string, 0);
75 g_string_set_size (chars, 0);
79 g_string_free (string, TRUE);
80 g_string_free (chars, TRUE);
85 * gst_util_set_value_from_string:
86 * @value: (out caller-allocates): the value to set
87 * @value_str: the string to get the value from
89 * Converts the string to the type of the value and
90 * sets the value with it.
92 * Note that this function is dangerous as it does not return any indication
93 * if the conversion worked or not.
96 gst_util_set_value_from_string (GValue * value, const gchar * value_str)
100 g_return_if_fail (value != NULL);
101 g_return_if_fail (value_str != NULL);
103 GST_CAT_DEBUG (GST_CAT_PARAMS, "parsing '%s' to type %s", value_str,
104 g_type_name (G_VALUE_TYPE (value)));
106 res = gst_value_deserialize (value, value_str);
107 if (!res && G_VALUE_TYPE (value) == G_TYPE_BOOLEAN) {
108 /* backwards compat, all booleans that fail to parse are false */
109 g_value_set_boolean (value, FALSE);
112 g_return_if_fail (res);
116 * gst_util_set_object_arg:
117 * @object: the object to set the argument of
118 * @name: the name of the argument to set
119 * @value: the string value to set
121 * Convertes the string value to the type of the objects argument and
122 * sets the argument with it.
124 * Note that this function silently returns if @object has no property named
125 * @name or when @value cannot be converted to the type of the property.
128 gst_util_set_object_arg (GObject * object, const gchar * name,
135 g_return_if_fail (G_IS_OBJECT (object));
136 g_return_if_fail (name != NULL);
137 g_return_if_fail (value != NULL);
139 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
143 value_type = pspec->value_type;
145 GST_DEBUG ("pspec->flags is %d, pspec->value_type is %s",
146 pspec->flags, g_type_name (value_type));
148 if (!(pspec->flags & G_PARAM_WRITABLE))
151 g_value_init (&v, value_type);
153 /* special case for element <-> xml (de)serialisation */
154 if (GST_VALUE_HOLDS_STRUCTURE (&v) && strcmp (value, "NULL") == 0) {
155 g_value_set_boxed (&v, NULL);
159 if (!gst_value_deserialize (&v, value))
164 g_object_set_property (object, pspec->name, &v);
168 /* work around error C2520: conversion from unsigned __int64 to double
169 * not implemented, use signed __int64
171 * These are implemented as functions because on some platforms a 64bit int to
172 * double conversion is not defined/implemented.
176 gst_util_guint64_to_gdouble (guint64 value)
178 if (value & G_GINT64_CONSTANT (0x8000000000000000))
179 return (gdouble) ((gint64) value) + (gdouble) 18446744073709551616.;
181 return (gdouble) ((gint64) value);
185 gst_util_gdouble_to_guint64 (gdouble value)
187 if (value < (gdouble) 9223372036854775808.) /* 1 << 63 */
188 return ((guint64) ((gint64) value));
190 value -= (gdouble) 18446744073709551616.;
191 return ((guint64) ((gint64) value));
194 #ifndef HAVE_UINT128_T
195 /* convenience struct for getting high and low uint32 parts of
202 #if G_BYTE_ORDER == G_BIG_ENDIAN
210 #if defined (__x86_64__) && defined (__GNUC__)
212 gst_util_uint64_mul_uint64 (GstUInt64 * c1, GstUInt64 * c0, guint64 arg1,
215 __asm__ __volatile__ ("mulq %3":"=a" (c0->ll), "=d" (c1->ll)
216 :"a" (arg1), "g" (arg2)
219 #else /* defined (__x86_64__) */
220 /* multiply two 64-bit unsigned ints into a 128-bit unsigned int. the high
221 * and low 64 bits of the product are placed in c1 and c0 respectively.
222 * this operation cannot overflow. */
224 gst_util_uint64_mul_uint64 (GstUInt64 * c1, GstUInt64 * c0, guint64 arg1,
234 /* do 128 bits multiply
242 * -------------------
245 * "a0" is optimized away, result is stored directly in c0. "b1" is
246 * optimized away, result is stored directly in c1.
248 c0->ll = (guint64) v.l.low * n.l.low;
249 a1.ll = (guint64) v.l.low * n.l.high;
250 b0.ll = (guint64) v.l.high * n.l.low;
252 /* add the high word of a0 to the low words of a1 and b0 using c1 as
253 * scrach space to capture the carry. the low word of the result becomes
254 * the final high word of c0 */
255 c1->ll = (guint64) c0->l.high + a1.l.low + b0.l.low;
256 c0->l.high = c1->l.low;
258 /* add the carry from the result above (found in the high word of c1) and
259 * the high words of a1 and b0 to b1, the result is c1. */
260 c1->ll = (guint64) v.l.high * n.l.high + c1->l.high + a1.l.high + b0.l.high;
262 #endif /* defined (__x86_64__) */
264 #if defined (__x86_64__) && defined (__GNUC__)
265 static inline guint64
266 gst_util_div128_64 (GstUInt64 c1, GstUInt64 c0, guint64 denom)
270 __asm__ __volatile__ ("divq %3":"=a" (res)
271 :"d" (c1.ll), "a" (c0.ll), "g" (denom)
277 /* count leading zeros */
279 gst_util_clz (guint32 val)
283 s = val | (val >> 1);
287 s = ~(s | (s >> 16));
288 s = s - ((s >> 1) & 0x55555555);
289 s = (s & 0x33333333) + ((s >> 2) & 0x33333333);
290 s = (s + (s >> 4)) & 0x0f0f0f0f;
292 s = (s + (s >> 16)) & 0x3f;
297 /* based on Hacker's Delight p152 */
298 static inline guint64
299 gst_util_div128_64 (GstUInt64 c1, GstUInt64 c0, guint64 denom)
301 GstUInt64 q1, q0, rhat;
302 GstUInt64 v, cmp1, cmp2;
307 /* count number of leading zeroes, we know they must be in the high
308 * part of denom since denom > G_MAXUINT32. */
309 s = gst_util_clz (v.l.high);
312 /* normalize divisor and dividend */
314 c1.ll = (c1.ll << s) | (c0.l.high >> (32 - s));
318 q1.ll = c1.ll / v.l.high;
319 rhat.ll = c1.ll - q1.ll * v.l.high;
321 cmp1.l.high = rhat.l.low;
322 cmp1.l.low = c0.l.high;
323 cmp2.ll = q1.ll * v.l.low;
325 while (q1.l.high || cmp2.ll > cmp1.ll) {
330 cmp1.l.high = rhat.l.low;
333 c1.l.high = c1.l.low;
334 c1.l.low = c0.l.high;
335 c1.ll -= q1.ll * v.ll;
336 q0.ll = c1.ll / v.l.high;
337 rhat.ll = c1.ll - q0.ll * v.l.high;
339 cmp1.l.high = rhat.l.low;
340 cmp1.l.low = c0.l.low;
341 cmp2.ll = q0.ll * v.l.low;
343 while (q0.l.high || cmp2.ll > cmp1.ll) {
348 cmp1.l.high = rhat.l.low;
351 q0.l.high += q1.l.low;
355 #endif /* defined (__GNUC__) */
357 /* This always gives the correct result because:
358 * a) val <= G_MAXUINT64-1
359 * b) (c0,c1) <= G_MAXUINT64 * (G_MAXUINT64-1)
361 * (c0,c1) == G_MAXUINT64 * G_MAXUINT64 and denom < G_MAXUINT64
362 * (note: num==denom case is handled by short path)
363 * This means that (c0,c1) either has enough space for val
364 * or that the overall result will overflow anyway.
367 /* add correction with carry */
368 #define CORRECT(c0,c1,val) \
370 if (G_MAXUINT64 - c0.ll < val) { \
371 if (G_UNLIKELY (c1.ll == G_MAXUINT64)) \
373 return G_MAXUINT64; \
380 gst_util_uint64_scale_uint64_unchecked (guint64 val, guint64 num,
381 guint64 denom, guint64 correct)
385 /* compute 128-bit numerator product */
386 gst_util_uint64_mul_uint64 (&c1, &c0, val, num);
388 /* perform rounding correction */
389 CORRECT (c0, c1, correct);
391 /* high word as big as or bigger than denom --> overflow */
392 if (G_UNLIKELY (c1.ll >= denom))
395 /* compute quotient, fits in 64 bits */
396 return gst_util_div128_64 (c1, c0, denom);
400 #define GST_MAXUINT128 ((__uint128_t) -1)
402 gst_util_uint64_scale_uint64_unchecked (guint64 val, guint64 num,
403 guint64 denom, guint64 correct)
407 /* Calculate val * num */
408 tmp = ((__uint128_t) val) * ((__uint128_t) num);
410 /* overflow checks */
411 if (G_UNLIKELY (GST_MAXUINT128 - correct < tmp))
414 /* perform rounding correction */
417 /* Divide by denom */
420 /* if larger than G_MAXUINT64 --> overflow */
421 if (G_UNLIKELY (tmp > G_MAXUINT64))
424 /* compute quotient, fits in 64 bits */
425 return (guint64) tmp;
430 #if !defined (__x86_64__) && !defined (HAVE_UINT128_T)
432 gst_util_uint64_mul_uint32 (GstUInt64 * c1, GstUInt64 * c0, guint64 arg1,
439 c0->ll = (guint64) a.l.low * arg2;
440 c1->ll = (guint64) a.l.high * arg2 + c0->l.high;
444 /* divide a 96-bit unsigned int by a 32-bit unsigned int when we know the
445 * quotient fits into 64 bits. the high 64 bits and low 32 bits of the
446 * numerator are expected in c1 and c0 respectively. */
447 static inline guint64
448 gst_util_div96_32 (guint64 c1, guint64 c0, guint32 denom)
450 c0 += (c1 % denom) << 32;
451 return ((c1 / denom) << 32) + (c0 / denom);
454 static inline guint64
455 gst_util_uint64_scale_uint32_unchecked (guint64 val, guint32 num,
456 guint32 denom, guint32 correct)
460 /* compute 96-bit numerator product */
461 gst_util_uint64_mul_uint32 (&c1, &c0, val, num);
463 /* condition numerator based on rounding mode */
464 CORRECT (c0, c1, correct);
466 /* high 32 bits as big as or bigger than denom --> overflow */
467 if (G_UNLIKELY (c1.l.high >= denom))
470 /* compute quotient, fits in 64 bits */
471 return gst_util_div96_32 (c1.ll, c0.ll, denom);
475 /* the guts of the gst_util_uint64_scale() variants */
477 _gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom,
480 g_return_val_if_fail (denom != 0, G_MAXUINT64);
482 if (G_UNLIKELY (num == 0))
485 if (G_UNLIKELY (num == denom))
488 /* on 64bits we always use a full 128bits multiply/division */
489 #if !defined (__x86_64__) && !defined (HAVE_UINT128_T)
490 /* denom is low --> try to use 96 bit muldiv */
491 if (G_LIKELY (denom <= G_MAXUINT32)) {
492 /* num is low --> use 96 bit muldiv */
493 if (G_LIKELY (num <= G_MAXUINT32))
494 return gst_util_uint64_scale_uint32_unchecked (val, (guint32) num,
495 (guint32) denom, correct);
497 /* num is high but val is low --> swap and use 96-bit muldiv */
498 if (G_LIKELY (val <= G_MAXUINT32))
499 return gst_util_uint64_scale_uint32_unchecked (num, (guint32) val,
500 (guint32) denom, correct);
502 #endif /* !defined (__x86_64__) && !defined (HAVE_UINT128_T) */
504 /* val is high and num is high --> use 128-bit muldiv */
505 return gst_util_uint64_scale_uint64_unchecked (val, num, denom, correct);
509 * gst_util_uint64_scale:
510 * @val: the number to scale
511 * @num: the numerator of the scale ratio
512 * @denom: the denominator of the scale ratio
514 * Scale @val by the rational number @num / @denom, avoiding overflows and
515 * underflows and without loss of precision.
517 * This function can potentially be very slow if val and num are both
518 * greater than G_MAXUINT32.
520 * Returns: @val * @num / @denom. In the case of an overflow, this
521 * function returns G_MAXUINT64. If the result is not exactly
522 * representable as an integer it is truncated. See also
523 * gst_util_uint64_scale_round(), gst_util_uint64_scale_ceil(),
524 * gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
525 * gst_util_uint64_scale_int_ceil().
528 gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom)
530 return _gst_util_uint64_scale (val, num, denom, 0);
534 * gst_util_uint64_scale_round:
535 * @val: the number to scale
536 * @num: the numerator of the scale ratio
537 * @denom: the denominator of the scale ratio
539 * Scale @val by the rational number @num / @denom, avoiding overflows and
540 * underflows and without loss of precision.
542 * This function can potentially be very slow if val and num are both
543 * greater than G_MAXUINT32.
545 * Returns: @val * @num / @denom. In the case of an overflow, this
546 * function returns G_MAXUINT64. If the result is not exactly
547 * representable as an integer, it is rounded to the nearest integer
548 * (half-way cases are rounded up). See also gst_util_uint64_scale(),
549 * gst_util_uint64_scale_ceil(), gst_util_uint64_scale_int(),
550 * gst_util_uint64_scale_int_round(), gst_util_uint64_scale_int_ceil().
553 gst_util_uint64_scale_round (guint64 val, guint64 num, guint64 denom)
555 return _gst_util_uint64_scale (val, num, denom, denom >> 1);
559 * gst_util_uint64_scale_ceil:
560 * @val: the number to scale
561 * @num: the numerator of the scale ratio
562 * @denom: the denominator of the scale ratio
564 * Scale @val by the rational number @num / @denom, avoiding overflows and
565 * underflows and without loss of precision.
567 * This function can potentially be very slow if val and num are both
568 * greater than G_MAXUINT32.
570 * Returns: @val * @num / @denom. In the case of an overflow, this
571 * function returns G_MAXUINT64. If the result is not exactly
572 * representable as an integer, it is rounded up. See also
573 * gst_util_uint64_scale(), gst_util_uint64_scale_round(),
574 * gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
575 * gst_util_uint64_scale_int_ceil().
578 gst_util_uint64_scale_ceil (guint64 val, guint64 num, guint64 denom)
580 return _gst_util_uint64_scale (val, num, denom, denom - 1);
583 /* the guts of the gst_util_uint64_scale_int() variants */
585 _gst_util_uint64_scale_int (guint64 val, gint num, gint denom, gint correct)
587 g_return_val_if_fail (denom > 0, G_MAXUINT64);
588 g_return_val_if_fail (num >= 0, G_MAXUINT64);
590 if (G_UNLIKELY (num == 0))
593 if (G_UNLIKELY (num == denom))
596 if (val <= G_MAXUINT32) {
597 /* simple case. num and denom are not negative so casts are OK. when
598 * not truncating, the additions to the numerator cannot overflow
599 * because val*num <= G_MAXUINT32 * G_MAXINT32 < G_MAXUINT64 -
600 * G_MAXINT32, so there's room to add another gint32. */
601 val *= (guint64) num;
602 /* add rounding correction */
605 return val / (guint64) denom;
607 #if !defined (__x86_64__) && !defined (HAVE_UINT128_T)
608 /* num and denom are not negative so casts are OK */
609 return gst_util_uint64_scale_uint32_unchecked (val, (guint32) num,
610 (guint32) denom, (guint32) correct);
612 /* always use full 128bits scale */
613 return gst_util_uint64_scale_uint64_unchecked (val, num, denom, correct);
618 * gst_util_uint64_scale_int:
619 * @val: guint64 (such as a #GstClockTime) to scale.
620 * @num: numerator of the scale factor.
621 * @denom: denominator of the scale factor.
623 * Scale @val by the rational number @num / @denom, avoiding overflows and
624 * underflows and without loss of precision. @num must be non-negative and
625 * @denom must be positive.
627 * Returns: @val * @num / @denom. In the case of an overflow, this
628 * function returns G_MAXUINT64. If the result is not exactly
629 * representable as an integer, it is truncated. See also
630 * gst_util_uint64_scale_int_round(), gst_util_uint64_scale_int_ceil(),
631 * gst_util_uint64_scale(), gst_util_uint64_scale_round(),
632 * gst_util_uint64_scale_ceil().
635 gst_util_uint64_scale_int (guint64 val, gint num, gint denom)
637 return _gst_util_uint64_scale_int (val, num, denom, 0);
641 * gst_util_uint64_scale_int_round:
642 * @val: guint64 (such as a #GstClockTime) to scale.
643 * @num: numerator of the scale factor.
644 * @denom: denominator of the scale factor.
646 * Scale @val by the rational number @num / @denom, avoiding overflows and
647 * underflows and without loss of precision. @num must be non-negative and
648 * @denom must be positive.
650 * Returns: @val * @num / @denom. In the case of an overflow, this
651 * function returns G_MAXUINT64. If the result is not exactly
652 * representable as an integer, it is rounded to the nearest integer
653 * (half-way cases are rounded up). See also gst_util_uint64_scale_int(),
654 * gst_util_uint64_scale_int_ceil(), gst_util_uint64_scale(),
655 * gst_util_uint64_scale_round(), gst_util_uint64_scale_ceil().
658 gst_util_uint64_scale_int_round (guint64 val, gint num, gint denom)
660 /* we can use a shift to divide by 2 because denom is required to be
662 return _gst_util_uint64_scale_int (val, num, denom, denom >> 1);
666 * gst_util_uint64_scale_int_ceil:
667 * @val: guint64 (such as a #GstClockTime) to scale.
668 * @num: numerator of the scale factor.
669 * @denom: denominator of the scale factor.
671 * Scale @val by the rational number @num / @denom, avoiding overflows and
672 * underflows and without loss of precision. @num must be non-negative and
673 * @denom must be positive.
675 * Returns: @val * @num / @denom. In the case of an overflow, this
676 * function returns G_MAXUINT64. If the result is not exactly
677 * representable as an integer, it is rounded up. See also
678 * gst_util_uint64_scale_int(), gst_util_uint64_scale_int_round(),
679 * gst_util_uint64_scale(), gst_util_uint64_scale_round(),
680 * gst_util_uint64_scale_ceil().
683 gst_util_uint64_scale_int_ceil (guint64 val, gint num, gint denom)
685 return _gst_util_uint64_scale_int (val, num, denom, denom - 1);
689 * gst_util_seqnum_next:
691 * Return a constantly incrementing sequence number.
693 * This function is used internally to GStreamer to be able to determine which
694 * events and messages are "the same". For example, elements may set the seqnum
695 * on a segment-done message to be the same as that of the last seek event, to
696 * indicate that event and the message correspond to the same segment.
698 * Returns: A constantly incrementing 32-bit unsigned integer, which might
699 * overflow back to 0 at some point. Use gst_util_seqnum_compare() to make sure
700 * you handle wraparound correctly.
705 gst_util_seqnum_next (void)
707 static gint counter = 0;
708 return G_ATOMIC_INT_ADD (&counter, 1);
712 * gst_util_seqnum_compare:
713 * @s1: A sequence number.
714 * @s2: Another sequence number.
716 * Compare two sequence numbers, handling wraparound.
718 * The current implementation just returns (gint32)(@s1 - @s2).
720 * Returns: A negative number if @s1 is before @s2, 0 if they are equal, or a
721 * positive number if @s1 is after @s2.
726 gst_util_seqnum_compare (guint32 s1, guint32 s2)
728 return (gint32) (s1 - s2);
731 /* -----------------------------------------------------
733 * The following code will be moved out of the main
734 * gstreamer library someday.
740 string_append_indent (GString * str, gint count)
744 for (xx = 0; xx < count; xx++)
745 g_string_append_c (str, ' ');
749 * gst_print_pad_caps:
750 * @buf: the buffer to print the caps in
751 * @indent: initial indentation
752 * @pad: (transfer none): the pad to print the caps from
754 * Write the pad capabilities in a human readable format into
758 gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
765 string_append_indent (buf, indent);
766 g_string_printf (buf, "%s:%s has no capabilities",
767 GST_DEBUG_PAD_NAME (pad));
771 s = gst_caps_to_string (caps);
772 g_string_append (buf, s);
778 * gst_print_element_args:
779 * @buf: the buffer to print the args in
780 * @indent: initial indentation
781 * @element: (transfer none): the element to print the args of
783 * Print the element argument in a human readable format in the given
787 gst_print_element_args (GString * buf, gint indent, GstElement * element)
790 GValue value = { 0, }; /* the important thing is that value.type = 0 */
792 GParamSpec *spec, **specs, **walk;
794 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
797 for (walk = specs; *walk; walk++) {
799 if (width < strlen (spec->name))
800 width = strlen (spec->name);
803 for (walk = specs; *walk; walk++) {
806 if (spec->flags & G_PARAM_READABLE) {
807 g_value_init (&value, spec->value_type);
808 g_object_get_property (G_OBJECT (element), spec->name, &value);
809 str = g_strdup_value_contents (&value);
810 g_value_unset (&value);
812 str = g_strdup ("Parameter not readable.");
815 string_append_indent (buf, indent);
816 g_string_append (buf, spec->name);
817 string_append_indent (buf, 2 + width - strlen (spec->name));
818 g_string_append (buf, str);
819 g_string_append_c (buf, '\n');
828 * gst_element_create_all_pads:
829 * @element: (transfer none): a #GstElement to create pads for
831 * Creates a pad for each pad template that is always available.
832 * This function is only useful during object initialization of
833 * subclasses of #GstElement.
836 gst_element_create_all_pads (GstElement * element)
840 /* FIXME: lock element */
843 gst_element_class_get_pad_template_list (GST_ELEMENT_CLASS
844 (G_OBJECT_GET_CLASS (element)));
847 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
849 if (padtempl->presence == GST_PAD_ALWAYS) {
852 pad = gst_pad_new_from_template (padtempl, padtempl->name_template);
854 gst_element_add_pad (element, pad);
856 padlist = padlist->next;
861 * gst_element_get_compatible_pad_template:
862 * @element: (transfer none): a #GstElement to get a compatible pad template for
863 * @compattempl: (transfer none): the #GstPadTemplate to find a compatible
866 * Retrieves a pad template from @element that is compatible with @compattempl.
867 * Pads from compatible templates can be linked together.
869 * Returns: (transfer none): a compatible #GstPadTemplate, or NULL if none
870 * was found. No unreferencing is necessary.
873 gst_element_get_compatible_pad_template (GstElement * element,
874 GstPadTemplate * compattempl)
876 GstPadTemplate *newtempl = NULL;
878 GstElementClass *class;
881 g_return_val_if_fail (element != NULL, NULL);
882 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
883 g_return_val_if_fail (compattempl != NULL, NULL);
885 class = GST_ELEMENT_GET_CLASS (element);
887 padlist = gst_element_class_get_pad_template_list (class);
889 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
890 "Looking for a suitable pad template in %s out of %d templates...",
891 GST_ELEMENT_NAME (element), g_list_length (padlist));
894 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
898 * Check direction (must be opposite)
901 GST_CAT_LOG (GST_CAT_CAPS,
902 "checking pad template %s", padtempl->name_template);
903 if (padtempl->direction != compattempl->direction) {
904 GST_CAT_DEBUG (GST_CAT_CAPS,
905 "compatible direction: found %s pad template \"%s\"",
906 padtempl->direction == GST_PAD_SRC ? "src" : "sink",
907 padtempl->name_template);
909 GST_CAT_DEBUG (GST_CAT_CAPS,
910 "intersecting %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (compattempl));
911 GST_CAT_DEBUG (GST_CAT_CAPS,
912 "..and %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (padtempl));
914 compatible = gst_caps_can_intersect (GST_PAD_TEMPLATE_CAPS (compattempl),
915 GST_PAD_TEMPLATE_CAPS (padtempl));
917 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
918 (compatible ? "" : "not "));
926 padlist = g_list_next (padlist);
929 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
930 "Returning new pad template %p", newtempl);
932 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "No compatible pad template found");
938 * gst_element_get_pad_from_template:
939 * @element: (transfer none): a #GstElement.
940 * @templ: (transfer none): a #GstPadTemplate belonging to @element.
942 * Gets a pad from @element described by @templ. If the presence of @templ is
943 * #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
946 * Returns: (transfer full): the #GstPad, or NULL if one could not be found
950 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
953 GstPadPresence presence;
955 /* If this function is ever exported, we need check the validity of `element'
956 * and `templ', and to make sure the template actually belongs to the
959 presence = GST_PAD_TEMPLATE_PRESENCE (templ);
963 case GST_PAD_SOMETIMES:
964 ret = gst_element_get_static_pad (element, templ->name_template);
965 if (!ret && presence == GST_PAD_ALWAYS)
967 ("Element %s has an ALWAYS template %s, but no pad of the same name",
968 GST_OBJECT_NAME (element), templ->name_template);
971 case GST_PAD_REQUEST:
972 ret = gst_element_request_pad (element, templ, NULL, NULL);
980 * gst_element_request_compatible_pad:
981 * @element: a #GstElement.
982 * @templ: the #GstPadTemplate to which the new pad should be able to link.
984 * Requests a pad from @element. The returned pad should be unlinked and
985 * compatible with @templ. Might return an existing pad, or request a new one.
987 * Returns: a #GstPad, or %NULL if one could not be found or created.
990 gst_element_request_compatible_pad (GstElement * element,
991 GstPadTemplate * templ)
993 GstPadTemplate *templ_new;
996 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
997 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
999 /* FIXME: should really loop through the templates, testing each for
1000 * compatibility and pad availability. */
1001 templ_new = gst_element_get_compatible_pad_template (element, templ);
1003 pad = gst_element_get_pad_from_template (element, templ_new);
1005 /* This can happen for non-request pads. No need to unref. */
1006 if (pad && GST_PAD_PEER (pad))
1013 * Checks if the source pad and the sink pad can be linked.
1014 * Both @srcpad and @sinkpad must be unlinked and have a parent.
1017 gst_pad_check_link (GstPad * srcpad, GstPad * sinkpad)
1019 /* FIXME This function is gross. It's almost a direct copy of
1020 * gst_pad_link_filtered(). Any decent programmer would attempt
1021 * to merge the two functions, which I will do some day. --ds
1024 /* generic checks */
1025 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1026 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1028 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1029 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1031 /* FIXME: shouldn't we convert this to g_return_val_if_fail? */
1032 if (GST_PAD_PEER (srcpad) != NULL) {
1033 GST_CAT_INFO (GST_CAT_PADS, "Source pad %s:%s has a peer, failed",
1034 GST_DEBUG_PAD_NAME (srcpad));
1037 if (GST_PAD_PEER (sinkpad) != NULL) {
1038 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has a peer, failed",
1039 GST_DEBUG_PAD_NAME (sinkpad));
1042 if (!GST_PAD_IS_SRC (srcpad)) {
1043 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s is not source pad, failed",
1044 GST_DEBUG_PAD_NAME (srcpad));
1047 if (!GST_PAD_IS_SINK (sinkpad)) {
1048 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s is not sink pad, failed",
1049 GST_DEBUG_PAD_NAME (sinkpad));
1052 if (GST_PAD_PARENT (srcpad) == NULL) {
1053 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s has no parent, failed",
1054 GST_DEBUG_PAD_NAME (srcpad));
1057 if (GST_PAD_PARENT (sinkpad) == NULL) {
1058 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has no parent, failed",
1059 GST_DEBUG_PAD_NAME (srcpad));
1067 * gst_element_get_compatible_pad:
1068 * @element: (transfer none): a #GstElement in which the pad should be found.
1069 * @pad: (transfer none): the #GstPad to find a compatible one for.
1070 * @caps: the #GstCaps to use as a filter.
1072 * Looks for an unlinked pad to which the given pad can link. It is not
1073 * guaranteed that linking the pads will work, though it should work in most
1076 * This function will first attempt to find a compatible unlinked ALWAYS pad,
1077 * and if none can be found, it will request a compatible REQUEST pad by looking
1078 * at the templates of @element.
1080 * Returns: (transfer full): the #GstPad to which a link can be made, or %NULL
1081 * if one cannot be found. gst_object_unref() after usage.
1084 gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
1085 const GstCaps * caps)
1088 GstPadTemplate *templ;
1090 GstPad *foundpad = NULL;
1093 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1094 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1096 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1097 "finding pad in %s compatible with %s:%s",
1098 GST_ELEMENT_NAME (element), GST_DEBUG_PAD_NAME (pad));
1100 g_return_val_if_fail (GST_PAD_PEER (pad) == NULL, NULL);
1104 /* try to get an existing unlinked pad */
1105 if (GST_PAD_IS_SRC (pad)) {
1106 pads = gst_element_iterate_sink_pads (element);
1107 } else if (GST_PAD_IS_SINK (pad)) {
1108 pads = gst_element_iterate_src_pads (element);
1110 pads = gst_element_iterate_pads (element);
1116 switch (gst_iterator_next (pads, &padptr)) {
1117 case GST_ITERATOR_OK:
1124 current = GST_PAD (padptr);
1126 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
1127 GST_DEBUG_PAD_NAME (current));
1129 if (GST_PAD_IS_SRC (current)) {
1136 peer = gst_pad_get_peer (current);
1138 if (peer == NULL && gst_pad_check_link (srcpad, sinkpad)) {
1139 GstCaps *temp, *intersection;
1140 gboolean compatible;
1142 /* Now check if the two pads' caps are compatible */
1143 temp = gst_pad_get_caps_reffed (pad);
1145 intersection = gst_caps_intersect (temp, caps);
1146 gst_caps_unref (temp);
1148 intersection = temp;
1151 temp = gst_pad_get_caps_reffed (current);
1152 compatible = gst_caps_can_intersect (temp, intersection);
1153 gst_caps_unref (temp);
1154 gst_caps_unref (intersection);
1157 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1158 "found existing unlinked compatible pad %s:%s",
1159 GST_DEBUG_PAD_NAME (current));
1160 gst_iterator_free (pads);
1164 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "incompatible pads");
1167 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1168 "already linked or cannot be linked (peer = %p)", peer);
1170 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unreffing pads");
1172 gst_object_unref (current);
1174 gst_object_unref (peer);
1177 case GST_ITERATOR_DONE:
1180 case GST_ITERATOR_RESYNC:
1181 gst_iterator_resync (pads);
1183 case GST_ITERATOR_ERROR:
1184 g_assert_not_reached ();
1188 gst_iterator_free (pads);
1190 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
1191 "Could not find a compatible unlinked always pad to link to %s:%s, now checking request pads",
1192 GST_DEBUG_PAD_NAME (pad));
1194 /* try to create a new one */
1195 /* requesting is a little crazy, we need a template. Let's create one */
1196 /* FIXME: why not gst_pad_get_pad_template (pad); */
1197 templcaps = gst_pad_get_caps_reffed (pad);
1199 templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
1200 GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
1202 foundpad = gst_element_request_compatible_pad (element, templ);
1203 gst_object_unref (templ);
1206 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1207 "found existing request pad %s:%s", GST_DEBUG_PAD_NAME (foundpad));
1211 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element,
1212 "Could not find a compatible pad to link to %s:%s",
1213 GST_DEBUG_PAD_NAME (pad));
1218 * gst_element_state_get_name:
1219 * @state: a #GstState to get the name of.
1221 * Gets a string representing the given state.
1223 * Returns: (transfer none): a string with the name of the state.
1226 gst_element_state_get_name (GstState state)
1229 case GST_STATE_VOID_PENDING:
1230 return "VOID_PENDING";
1231 case GST_STATE_NULL:
1233 case GST_STATE_READY:
1235 case GST_STATE_PLAYING:
1237 case GST_STATE_PAUSED:
1240 /* This is a memory leak */
1241 return g_strdup_printf ("UNKNOWN!(%d)", state);
1246 * gst_element_state_change_return_get_name:
1247 * @state_ret: a #GstStateChangeReturn to get the name of.
1249 * Gets a string representing the given state change result.
1251 * Returns: (transfer none): a string with the name of the state
1257 gst_element_state_change_return_get_name (GstStateChangeReturn state_ret)
1259 switch (state_ret) {
1260 case GST_STATE_CHANGE_FAILURE:
1262 case GST_STATE_CHANGE_SUCCESS:
1264 case GST_STATE_CHANGE_ASYNC:
1266 case GST_STATE_CHANGE_NO_PREROLL:
1267 return "NO PREROLL";
1269 /* This is a memory leak */
1270 return g_strdup_printf ("UNKNOWN!(%d)", state_ret);
1276 gst_element_factory_can_accept_all_caps_in_direction (GstElementFactory *
1277 factory, const GstCaps * caps, GstPadDirection direction)
1281 g_return_val_if_fail (factory != NULL, FALSE);
1282 g_return_val_if_fail (caps != NULL, FALSE);
1284 templates = factory->staticpadtemplates;
1287 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1289 if (template->direction == direction) {
1290 GstCaps *templcaps = gst_static_caps_get (&template->static_caps);
1292 if (gst_caps_is_always_compatible (caps, templcaps)) {
1293 gst_caps_unref (templcaps);
1296 gst_caps_unref (templcaps);
1298 templates = g_list_next (templates);
1305 gst_element_factory_can_accept_any_caps_in_direction (GstElementFactory *
1306 factory, const GstCaps * caps, GstPadDirection direction)
1310 g_return_val_if_fail (factory != NULL, FALSE);
1311 g_return_val_if_fail (caps != NULL, FALSE);
1313 templates = factory->staticpadtemplates;
1316 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1318 if (template->direction == direction) {
1319 GstCaps *templcaps = gst_static_caps_get (&template->static_caps);
1321 if (gst_caps_can_intersect (caps, templcaps)) {
1322 gst_caps_unref (templcaps);
1325 gst_caps_unref (templcaps);
1327 templates = g_list_next (templates);
1334 * gst_element_factory_can_src_caps:
1335 * @factory: factory to query
1336 * @caps: the caps to check
1338 * Checks if the factory can source the given capability.
1340 * Returns: %TRUE if it can src the capabilities
1342 * Deprecated: use gst_element_factory_can_src_all_caps() instead.
1344 #ifndef GST_REMOVE_DEPRECATED
1345 #ifdef GST_DISABLE_DEPRECATED
1346 gboolean gst_element_factory_can_src_caps (GstElementFactory * factory,
1347 const GstCaps * caps);
1350 gst_element_factory_can_src_caps (GstElementFactory * factory,
1351 const GstCaps * caps)
1353 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1356 #endif /* GST_REMOVE_DEPRECATED */
1359 * gst_element_factory_can_sink_caps:
1360 * @factory: factory to query
1361 * @caps: the caps to check
1363 * Checks if the factory can sink the given capability.
1365 * Returns: %TRUE if it can sink the capabilities
1367 * Deprecated: use gst_element_factory_can_sink_all_caps() instead.
1369 #ifndef GST_REMOVE_DEPRECATED
1370 #ifdef GST_DISABLE_DEPRECATED
1371 gboolean gst_element_factory_can_sink_caps (GstElementFactory * factory,
1372 const GstCaps * caps);
1375 gst_element_factory_can_sink_caps (GstElementFactory * factory,
1376 const GstCaps * caps)
1378 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1381 #endif /* GST_REMOVE_DEPRECATED */
1384 * gst_element_factory_can_sink_all_caps:
1385 * @factory: factory to query
1386 * @caps: the caps to check
1388 * Checks if the factory can sink all possible capabilities.
1390 * Returns: %TRUE if the caps are fully compatible.
1395 gst_element_factory_can_sink_all_caps (GstElementFactory * factory,
1396 const GstCaps * caps)
1398 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1403 * gst_element_factory_can_src_all_caps:
1404 * @factory: factory to query
1405 * @caps: the caps to check
1407 * Checks if the factory can src all possible capabilities.
1409 * Returns: %TRUE if the caps are fully compatible.
1414 gst_element_factory_can_src_all_caps (GstElementFactory * factory,
1415 const GstCaps * caps)
1417 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1422 * gst_element_factory_can_sink_any_caps:
1423 * @factory: factory to query
1424 * @caps: the caps to check
1426 * Checks if the factory can sink any possible capability.
1428 * Returns: %TRUE if the caps have a common subset.
1433 gst_element_factory_can_sink_any_caps (GstElementFactory * factory,
1434 const GstCaps * caps)
1436 return gst_element_factory_can_accept_any_caps_in_direction (factory, caps,
1441 * gst_element_factory_can_src_any_caps:
1442 * @factory: factory to query
1443 * @caps: the caps to check
1445 * Checks if the factory can src any possible capability.
1447 * Returns: %TRUE if the caps have a common subset.
1452 gst_element_factory_can_src_any_caps (GstElementFactory * factory,
1453 const GstCaps * caps)
1455 return gst_element_factory_can_accept_any_caps_in_direction (factory, caps,
1459 /* if return val is true, *direct_child is a caller-owned ref on the direct
1460 * child of ancestor that is part of object's ancestry */
1462 object_has_ancestor (GstObject * object, GstObject * ancestor,
1463 GstObject ** direct_child)
1465 GstObject *child, *parent;
1468 *direct_child = NULL;
1470 child = gst_object_ref (object);
1471 parent = gst_object_get_parent (object);
1474 if (ancestor == parent) {
1476 *direct_child = child;
1478 gst_object_unref (child);
1479 gst_object_unref (parent);
1483 gst_object_unref (child);
1485 parent = gst_object_get_parent (parent);
1488 gst_object_unref (child);
1493 /* caller owns return */
1495 find_common_root (GstObject * o1, GstObject * o2)
1497 GstObject *top = o1;
1498 GstObject *kid1, *kid2;
1499 GstObject *root = NULL;
1501 while (GST_OBJECT_PARENT (top))
1502 top = GST_OBJECT_PARENT (top);
1504 /* the itsy-bitsy spider... */
1506 if (!object_has_ancestor (o2, top, &kid2))
1509 root = gst_object_ref (top);
1511 if (!object_has_ancestor (o1, kid2, &kid1)) {
1512 gst_object_unref (kid2);
1516 if (!object_has_ancestor (o2, kid1, &kid2)) {
1517 gst_object_unref (kid1);
1524 /* caller does not own return */
1526 ghost_up (GstElement * e, GstPad * pad)
1528 static gint ghost_pad_index = 0;
1533 GstObject *parent = GST_OBJECT_PARENT (e);
1535 name = g_strdup_printf ("ghost%d", ghost_pad_index++);
1536 gpad = gst_ghost_pad_new (name, pad);
1540 gst_element_get_state (e, ¤t, &next, 0);
1542 if (current > GST_STATE_READY || next == GST_STATE_PAUSED)
1543 gst_pad_set_active (gpad, TRUE);
1545 if (!gst_element_add_pad ((GstElement *) parent, gpad)) {
1546 g_warning ("Pad named %s already exists in element %s\n",
1547 GST_OBJECT_NAME (gpad), GST_OBJECT_NAME (parent));
1548 gst_object_unref ((GstObject *) gpad);
1549 GST_STATE_UNLOCK (e);
1552 GST_STATE_UNLOCK (e);
1558 remove_pad (gpointer ppad, gpointer unused)
1562 if (!gst_element_remove_pad ((GstElement *) GST_OBJECT_PARENT (pad), pad))
1563 g_warning ("Couldn't remove pad %s from element %s",
1564 GST_OBJECT_NAME (pad), GST_OBJECT_NAME (GST_OBJECT_PARENT (pad)));
1568 prepare_link_maybe_ghosting (GstPad ** src, GstPad ** sink,
1569 GSList ** pads_created)
1573 GSList *pads_created_local = NULL;
1575 g_assert (pads_created);
1577 e1 = GST_OBJECT_PARENT (*src);
1578 e2 = GST_OBJECT_PARENT (*sink);
1580 if (G_UNLIKELY (e1 == NULL)) {
1581 GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1582 GST_PTR_FORMAT, *src);
1585 if (G_UNLIKELY (e2 == NULL)) {
1586 GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1587 GST_PTR_FORMAT, *sink);
1591 if (GST_OBJECT_PARENT (e1) == GST_OBJECT_PARENT (e2)) {
1592 GST_CAT_INFO (GST_CAT_PADS, "%s and %s in same bin, no need for ghost pads",
1593 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1597 GST_CAT_INFO (GST_CAT_PADS, "%s and %s not in same bin, making ghost pads",
1598 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1600 /* we need to setup some ghost pads */
1601 root = find_common_root (e1, e2);
1603 g_warning ("Trying to connect elements that don't share a common "
1604 "ancestor: %s and %s", GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2));
1608 while (GST_OBJECT_PARENT (e1) != root) {
1609 *src = ghost_up ((GstElement *) e1, *src);
1612 e1 = GST_OBJECT_PARENT (*src);
1613 pads_created_local = g_slist_prepend (pads_created_local, *src);
1615 while (GST_OBJECT_PARENT (e2) != root) {
1616 *sink = ghost_up ((GstElement *) e2, *sink);
1619 e2 = GST_OBJECT_PARENT (*sink);
1620 pads_created_local = g_slist_prepend (pads_created_local, *sink);
1623 gst_object_unref (root);
1624 *pads_created = g_slist_concat (*pads_created, pads_created_local);
1628 gst_object_unref (root);
1629 g_slist_foreach (pads_created_local, remove_pad, NULL);
1630 g_slist_free (pads_created_local);
1635 pad_link_maybe_ghosting (GstPad * src, GstPad * sink, GstPadLinkCheck flags)
1637 GSList *pads_created = NULL;
1640 if (!prepare_link_maybe_ghosting (&src, &sink, &pads_created)) {
1643 ret = (gst_pad_link_full (src, sink, flags) == GST_PAD_LINK_OK);
1647 g_slist_foreach (pads_created, remove_pad, NULL);
1649 g_slist_free (pads_created);
1655 * gst_element_link_pads_full:
1656 * @src: a #GstElement containing the source pad.
1657 * @srcpadname: (allow-none): the name of the #GstPad in source element
1658 * or NULL for any pad.
1659 * @dest: (transfer none): the #GstElement containing the destination pad.
1660 * @destpadname: (allow-none): the name of the #GstPad in destination element,
1661 * or NULL for any pad.
1662 * @flags: the #GstPadLinkCheck to be performed when linking pads.
1664 * Links the two named pads of the source and destination elements.
1665 * Side effect is that if one of the pads has no parent, it becomes a
1666 * child of the parent of the other element. If they have different
1667 * parents, the link fails.
1669 * Calling gst_element_link_pads_full() with @flags == %GST_PAD_LINK_CHECK_DEFAULT
1670 * is the same as calling gst_element_link_pads() and the recommended way of
1671 * linking pads with safety checks applied.
1673 * This is a convenience function for gst_pad_link_full().
1675 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1680 gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
1681 GstElement * dest, const gchar * destpadname, GstPadLinkCheck flags)
1683 const GList *srcpads, *destpads, *srctempls, *desttempls, *l;
1684 GstPad *srcpad, *destpad;
1685 GstPadTemplate *srctempl, *desttempl;
1686 GstElementClass *srcclass, *destclass;
1689 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1690 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1692 GST_CAT_INFO (GST_CAT_ELEMENT_PADS,
1693 "trying to link element %s:%s to element %s:%s", GST_ELEMENT_NAME (src),
1694 srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
1695 destpadname ? destpadname : "(any)");
1699 /* name specified, look it up */
1700 if (!(srcpad = gst_element_get_static_pad (src, srcpadname)))
1701 srcpad = gst_element_get_request_pad (src, srcpadname);
1703 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1704 GST_ELEMENT_NAME (src), srcpadname);
1707 if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
1708 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
1709 GST_DEBUG_PAD_NAME (srcpad));
1710 gst_object_unref (srcpad);
1713 if (GST_PAD_PEER (srcpad) != NULL) {
1714 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1715 "pad %s:%s is already linked to %s:%s", GST_DEBUG_PAD_NAME (srcpad),
1716 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1717 gst_object_unref (srcpad);
1723 /* no name given, get the first available pad */
1724 GST_OBJECT_LOCK (src);
1725 srcpads = GST_ELEMENT_PADS (src);
1726 srcpad = srcpads ? GST_PAD_CAST (srcpads->data) : NULL;
1728 gst_object_ref (srcpad);
1729 GST_OBJECT_UNLOCK (src);
1732 /* get a destination pad */
1734 /* name specified, look it up */
1735 if (!(destpad = gst_element_get_static_pad (dest, destpadname)))
1736 destpad = gst_element_get_request_pad (dest, destpadname);
1738 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1739 GST_ELEMENT_NAME (dest), destpadname);
1742 if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
1743 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
1744 GST_DEBUG_PAD_NAME (destpad));
1745 gst_object_unref (destpad);
1748 if (GST_PAD_PEER (destpad) != NULL) {
1749 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1750 "pad %s:%s is already linked to %s:%s",
1751 GST_DEBUG_PAD_NAME (destpad),
1752 GST_DEBUG_PAD_NAME (GST_PAD_PEER (destpad)));
1753 gst_object_unref (destpad);
1759 /* no name given, get the first available pad */
1760 GST_OBJECT_LOCK (dest);
1761 destpads = GST_ELEMENT_PADS (dest);
1762 destpad = destpads ? GST_PAD_CAST (destpads->data) : NULL;
1764 gst_object_ref (destpad);
1765 GST_OBJECT_UNLOCK (dest);
1768 if (srcpadname && destpadname) {
1771 /* two explicitly specified pads */
1772 result = pad_link_maybe_ghosting (srcpad, destpad, flags);
1774 gst_object_unref (srcpad);
1775 gst_object_unref (destpad);
1781 /* loop through the allowed pads in the source, trying to find a
1782 * compatible destination pad */
1783 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1784 "looping through allowed src and dest pads");
1786 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying src pad %s:%s",
1787 GST_DEBUG_PAD_NAME (srcpad));
1788 if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
1789 (GST_PAD_PEER (srcpad) == NULL)) {
1794 gst_object_ref (temp);
1796 temp = gst_element_get_compatible_pad (dest, srcpad, NULL);
1799 if (temp && pad_link_maybe_ghosting (srcpad, temp, flags)) {
1800 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1801 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (temp));
1803 gst_object_unref (destpad);
1804 gst_object_unref (srcpad);
1805 gst_object_unref (temp);
1810 gst_object_unref (temp);
1813 /* find a better way for this mess */
1815 srcpads = g_list_next (srcpads);
1817 gst_object_unref (srcpad);
1818 srcpad = GST_PAD_CAST (srcpads->data);
1819 gst_object_ref (srcpad);
1825 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
1826 GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
1828 gst_object_unref (destpad);
1832 gst_object_unref (srcpad);
1836 /* loop through the existing pads in the destination */
1838 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying dest pad %s:%s",
1839 GST_DEBUG_PAD_NAME (destpad));
1840 if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
1841 (GST_PAD_PEER (destpad) == NULL)) {
1842 GstPad *temp = gst_element_get_compatible_pad (src, destpad, NULL);
1844 if (temp && pad_link_maybe_ghosting (temp, destpad, flags)) {
1845 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1846 GST_DEBUG_PAD_NAME (temp), GST_DEBUG_PAD_NAME (destpad));
1847 gst_object_unref (temp);
1848 gst_object_unref (destpad);
1852 gst_object_unref (temp);
1856 destpads = g_list_next (destpads);
1858 gst_object_unref (destpad);
1859 destpad = GST_PAD_CAST (destpads->data);
1860 gst_object_ref (destpad);
1867 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
1868 GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
1869 gst_object_unref (destpad);
1873 gst_object_unref (destpad);
1877 srcclass = GST_ELEMENT_GET_CLASS (src);
1878 destclass = GST_ELEMENT_GET_CLASS (dest);
1880 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1881 "we might have request pads on both sides, checking...");
1882 srctempls = gst_element_class_get_pad_template_list (srcclass);
1883 desttempls = gst_element_class_get_pad_template_list (destclass);
1885 if (srctempls && desttempls) {
1887 srctempl = (GstPadTemplate *) srctempls->data;
1888 if (srctempl->presence == GST_PAD_REQUEST) {
1889 for (l = desttempls; l; l = l->next) {
1890 desttempl = (GstPadTemplate *) l->data;
1891 if (desttempl->presence == GST_PAD_REQUEST &&
1892 desttempl->direction != srctempl->direction) {
1893 if (gst_caps_is_always_compatible (gst_pad_template_get_caps
1894 (srctempl), gst_pad_template_get_caps (desttempl))) {
1896 gst_element_request_pad (src, srctempl,
1897 srctempl->name_template, NULL);
1899 gst_element_request_pad (dest, desttempl,
1900 desttempl->name_template, NULL);
1901 if (srcpad && destpad
1902 && pad_link_maybe_ghosting (srcpad, destpad, flags)) {
1903 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1904 "linked pad %s:%s to pad %s:%s",
1905 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
1906 gst_object_unref (srcpad);
1907 gst_object_unref (destpad);
1910 /* it failed, so we release the request pads */
1912 gst_element_release_request_pad (src, srcpad);
1914 gst_element_release_request_pad (dest, destpad);
1919 srctempls = srctempls->next;
1923 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s",
1924 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1929 * gst_element_link_pads:
1930 * @src: a #GstElement containing the source pad.
1931 * @srcpadname: (allow-none): the name of the #GstPad in source element
1932 * or NULL for any pad.
1933 * @dest: (transfer none): the #GstElement containing the destination pad.
1934 * @destpadname: (allow-none): the name of the #GstPad in destination element,
1935 * or NULL for any pad.
1937 * Links the two named pads of the source and destination elements.
1938 * Side effect is that if one of the pads has no parent, it becomes a
1939 * child of the parent of the other element. If they have different
1940 * parents, the link fails.
1942 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1945 gst_element_link_pads (GstElement * src, const gchar * srcpadname,
1946 GstElement * dest, const gchar * destpadname)
1948 return gst_element_link_pads_full (src, srcpadname, dest, destpadname,
1949 GST_PAD_LINK_CHECK_DEFAULT);
1953 * gst_element_link_pads_filtered:
1954 * @src: a #GstElement containing the source pad.
1955 * @srcpadname: (allow-none): the name of the #GstPad in source element
1956 * or NULL for any pad.
1957 * @dest: (transfer none): the #GstElement containing the destination pad.
1958 * @destpadname: (allow-none): the name of the #GstPad in destination element
1959 * or NULL for any pad.
1960 * @filter: (transfer none) (allow-none): the #GstCaps to filter the link,
1961 * or #NULL for no filter.
1963 * Links the two named pads of the source and destination elements. Side effect
1964 * is that if one of the pads has no parent, it becomes a child of the parent of
1965 * the other element. If they have different parents, the link fails. If @caps
1966 * is not #NULL, makes sure that the caps of the link is a subset of @caps.
1968 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1971 gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
1972 GstElement * dest, const gchar * destpadname, GstCaps * filter)
1975 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1976 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1977 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), FALSE);
1980 GstElement *capsfilter;
1982 GstState state, pending;
1985 capsfilter = gst_element_factory_make ("capsfilter", NULL);
1987 GST_ERROR ("Could not make a capsfilter");
1991 parent = gst_object_get_parent (GST_OBJECT (src));
1992 g_return_val_if_fail (GST_IS_BIN (parent), FALSE);
1994 gst_element_get_state (GST_ELEMENT_CAST (parent), &state, &pending, 0);
1996 if (!gst_bin_add (GST_BIN (parent), capsfilter)) {
1997 GST_ERROR ("Could not add capsfilter");
1998 gst_object_unref (capsfilter);
1999 gst_object_unref (parent);
2003 if (pending != GST_STATE_VOID_PENDING)
2006 gst_element_set_state (capsfilter, state);
2008 gst_object_unref (parent);
2010 g_object_set (capsfilter, "caps", filter, NULL);
2012 lr1 = gst_element_link_pads (src, srcpadname, capsfilter, "sink");
2013 lr2 = gst_element_link_pads (capsfilter, "src", dest, destpadname);
2018 GST_INFO ("Could not link pads: %s:%s - capsfilter:sink",
2019 GST_ELEMENT_NAME (src), srcpadname);
2021 GST_INFO ("Could not link pads: capsfilter:src - %s:%s",
2022 GST_ELEMENT_NAME (dest), destpadname);
2024 gst_element_set_state (capsfilter, GST_STATE_NULL);
2025 /* this will unlink and unref as appropriate */
2026 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (capsfilter)), capsfilter);
2030 if (gst_element_link_pads (src, srcpadname, dest, destpadname)) {
2033 GST_INFO ("Could not link pads: %s:%s - %s:%s", GST_ELEMENT_NAME (src),
2034 srcpadname, GST_ELEMENT_NAME (dest), destpadname);
2042 * @src: (transfer none): a #GstElement containing the source pad.
2043 * @dest: (transfer none): the #GstElement containing the destination pad.
2045 * Links @src to @dest. The link must be from source to
2046 * destination; the other direction will not be tried. The function looks for
2047 * existing pads that aren't linked yet. It will request new pads if necessary.
2048 * Such pads need to be released manually when unlinking.
2049 * If multiple links are possible, only one is established.
2051 * Make sure you have added your elements to a bin or pipeline with
2052 * gst_bin_add() before trying to link them.
2054 * Returns: TRUE if the elements could be linked, FALSE otherwise.
2057 gst_element_link (GstElement * src, GstElement * dest)
2059 return gst_element_link_pads (src, NULL, dest, NULL);
2063 * gst_element_link_many:
2064 * @element_1: (transfer none): the first #GstElement in the link chain.
2065 * @element_2: (transfer none): the second #GstElement in the link chain.
2066 * @...: the NULL-terminated list of elements to link in order.
2068 * Chain together a series of elements. Uses gst_element_link().
2069 * Make sure you have added your elements to a bin or pipeline with
2070 * gst_bin_add() before trying to link them.
2072 * Returns: TRUE on success, FALSE otherwise.
2075 gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
2077 gboolean res = TRUE;
2080 g_return_val_if_fail (GST_IS_ELEMENT (element_1), FALSE);
2081 g_return_val_if_fail (GST_IS_ELEMENT (element_2), FALSE);
2083 va_start (args, element_2);
2086 if (!gst_element_link (element_1, element_2)) {
2091 element_1 = element_2;
2092 element_2 = va_arg (args, GstElement *);
2101 * gst_element_link_filtered:
2102 * @src: a #GstElement containing the source pad.
2103 * @dest: (transfer none): the #GstElement containing the destination pad.
2104 * @filter: (transfer none) (allow-none): the #GstCaps to filter the link,
2105 * or #NULL for no filter.
2107 * Links @src to @dest using the given caps as filtercaps.
2108 * The link must be from source to
2109 * destination; the other direction will not be tried. The function looks for
2110 * existing pads that aren't linked yet. It will request new pads if necessary.
2111 * If multiple links are possible, only one is established.
2113 * Make sure you have added your elements to a bin or pipeline with
2114 * gst_bin_add() before trying to link them.
2116 * Returns: TRUE if the pads could be linked, FALSE otherwise.
2119 gst_element_link_filtered (GstElement * src, GstElement * dest,
2122 return gst_element_link_pads_filtered (src, NULL, dest, NULL, filter);
2126 * gst_element_unlink_pads:
2127 * @src: a (transfer none): #GstElement containing the source pad.
2128 * @srcpadname: the name of the #GstPad in source element.
2129 * @dest: (transfer none): a #GstElement containing the destination pad.
2130 * @destpadname: the name of the #GstPad in destination element.
2132 * Unlinks the two named pads of the source and destination elements.
2134 * This is a convenience function for gst_pad_unlink().
2137 gst_element_unlink_pads (GstElement * src, const gchar * srcpadname,
2138 GstElement * dest, const gchar * destpadname)
2140 GstPad *srcpad, *destpad;
2141 gboolean srcrequest, destrequest;
2143 srcrequest = destrequest = FALSE;
2145 g_return_if_fail (src != NULL);
2146 g_return_if_fail (GST_IS_ELEMENT (src));
2147 g_return_if_fail (srcpadname != NULL);
2148 g_return_if_fail (dest != NULL);
2149 g_return_if_fail (GST_IS_ELEMENT (dest));
2150 g_return_if_fail (destpadname != NULL);
2152 /* obtain the pads requested */
2153 if (!(srcpad = gst_element_get_static_pad (src, srcpadname)))
2154 if ((srcpad = gst_element_get_request_pad (src, srcpadname)))
2156 if (srcpad == NULL) {
2157 GST_WARNING_OBJECT (src, "source element has no pad \"%s\"", srcpadname);
2160 if (!(destpad = gst_element_get_static_pad (dest, destpadname)))
2161 if ((destpad = gst_element_get_request_pad (dest, destpadname)))
2163 if (destpad == NULL) {
2164 GST_WARNING_OBJECT (dest, "destination element has no pad \"%s\"",
2169 /* we're satisfied they can be unlinked, let's do it */
2170 gst_pad_unlink (srcpad, destpad);
2173 gst_element_release_request_pad (dest, destpad);
2174 gst_object_unref (destpad);
2178 gst_element_release_request_pad (src, srcpad);
2179 gst_object_unref (srcpad);
2183 * gst_element_unlink_many:
2184 * @element_1: (transfer none): the first #GstElement in the link chain.
2185 * @element_2: (transfer none): the second #GstElement in the link chain.
2186 * @...: the NULL-terminated list of elements to unlink in order.
2188 * Unlinks a series of elements. Uses gst_element_unlink().
2191 gst_element_unlink_many (GstElement * element_1, GstElement * element_2, ...)
2195 g_return_if_fail (element_1 != NULL && element_2 != NULL);
2196 g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
2198 va_start (args, element_2);
2201 gst_element_unlink (element_1, element_2);
2203 element_1 = element_2;
2204 element_2 = va_arg (args, GstElement *);
2211 * gst_element_unlink:
2212 * @src: (transfer none): the source #GstElement to unlink.
2213 * @dest: (transfer none): the sink #GstElement to unlink.
2215 * Unlinks all source pads of the source element with all sink pads
2216 * of the sink element to which they are linked.
2218 * If the link has been made using gst_element_link(), it could have created an
2219 * requestpad, which has to be released using gst_element_release_request_pad().
2222 gst_element_unlink (GstElement * src, GstElement * dest)
2225 gboolean done = FALSE;
2227 g_return_if_fail (GST_IS_ELEMENT (src));
2228 g_return_if_fail (GST_IS_ELEMENT (dest));
2230 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unlinking \"%s\" and \"%s\"",
2231 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
2233 pads = gst_element_iterate_pads (src);
2237 switch (gst_iterator_next (pads, &data)) {
2238 case GST_ITERATOR_OK:
2240 GstPad *pad = GST_PAD_CAST (data);
2242 if (GST_PAD_IS_SRC (pad)) {
2243 GstPad *peerpad = gst_pad_get_peer (pad);
2245 /* see if the pad is linked and is really a pad of dest */
2247 GstElement *peerelem;
2249 peerelem = gst_pad_get_parent_element (peerpad);
2251 if (peerelem == dest) {
2252 gst_pad_unlink (pad, peerpad);
2255 gst_object_unref (peerelem);
2257 gst_object_unref (peerpad);
2260 gst_object_unref (pad);
2263 case GST_ITERATOR_RESYNC:
2264 gst_iterator_resync (pads);
2266 case GST_ITERATOR_DONE:
2270 g_assert_not_reached ();
2274 gst_iterator_free (pads);
2278 * gst_element_query_position:
2279 * @element: a #GstElement to invoke the position query on.
2280 * @format: (inout): a pointer to the #GstFormat asked for.
2281 * On return contains the #GstFormat used.
2282 * @cur: (out) (allow-none): a location in which to store the current
2283 * position, or NULL.
2285 * Queries an element for the stream position. If one repeatedly calls this
2286 * function one can also create and reuse it in gst_element_query().
2288 * Returns: TRUE if the query could be performed.
2291 gst_element_query_position (GstElement * element, GstFormat * format,
2297 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2298 g_return_val_if_fail (format != NULL, FALSE);
2300 query = gst_query_new_position (*format);
2301 ret = gst_element_query (element, query);
2304 gst_query_parse_position (query, format, cur);
2306 gst_query_unref (query);
2312 * gst_element_query_duration:
2313 * @element: a #GstElement to invoke the duration query on.
2314 * @format: (inout): a pointer to the #GstFormat asked for.
2315 * On return contains the #GstFormat used.
2316 * @duration: (out): A location in which to store the total duration, or NULL.
2318 * Queries an element for the total stream duration.
2320 * Returns: TRUE if the query could be performed.
2323 gst_element_query_duration (GstElement * element, GstFormat * format,
2329 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2330 g_return_val_if_fail (format != NULL, FALSE);
2332 query = gst_query_new_duration (*format);
2333 ret = gst_element_query (element, query);
2336 gst_query_parse_duration (query, format, duration);
2338 gst_query_unref (query);
2344 * gst_element_query_convert:
2345 * @element: a #GstElement to invoke the convert query on.
2346 * @src_format: (inout): a #GstFormat to convert from.
2347 * @src_val: a value to convert.
2348 * @dest_format: (inout): a pointer to the #GstFormat to convert to.
2349 * @dest_val: (out): a pointer to the result.
2351 * Queries an element to convert @src_val in @src_format to @dest_format.
2353 * Returns: TRUE if the query could be performed.
2356 gst_element_query_convert (GstElement * element, GstFormat src_format,
2357 gint64 src_val, GstFormat * dest_format, gint64 * dest_val)
2362 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2363 g_return_val_if_fail (dest_format != NULL, FALSE);
2364 g_return_val_if_fail (dest_val != NULL, FALSE);
2366 if (*dest_format == src_format || src_val == -1) {
2367 *dest_val = src_val;
2371 query = gst_query_new_convert (src_format, src_val, *dest_format);
2372 ret = gst_element_query (element, query);
2375 gst_query_parse_convert (query, NULL, NULL, dest_format, dest_val);
2377 gst_query_unref (query);
2383 * gst_element_seek_simple
2384 * @element: a #GstElement to seek on
2385 * @format: a #GstFormat to execute the seek in, such as #GST_FORMAT_TIME
2386 * @seek_flags: seek options; playback applications will usually want to use
2387 * GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT here
2388 * @seek_pos: position to seek to (relative to the start); if you are doing
2389 * a seek in #GST_FORMAT_TIME this value is in nanoseconds -
2390 * multiply with #GST_SECOND to convert seconds to nanoseconds or
2391 * with #GST_MSECOND to convert milliseconds to nanoseconds.
2393 * Simple API to perform a seek on the given element, meaning it just seeks
2394 * to the given position relative to the start of the stream. For more complex
2395 * operations like segment seeks (e.g. for looping) or changing the playback
2396 * rate or seeking relative to the last configured playback segment you should
2397 * use gst_element_seek().
2399 * In a completely prerolled PAUSED or PLAYING pipeline, seeking is always
2400 * guaranteed to return %TRUE on a seekable media type or %FALSE when the media
2401 * type is certainly not seekable (such as a live stream).
2403 * Some elements allow for seeking in the READY state, in this
2404 * case they will store the seek event and execute it when they are put to
2405 * PAUSED. If the element supports seek in READY, it will always return %TRUE when
2406 * it receives the event in the READY state.
2408 * Returns: %TRUE if the seek operation succeeded (the seek might not always be
2409 * executed instantly though)
2414 gst_element_seek_simple (GstElement * element, GstFormat format,
2415 GstSeekFlags seek_flags, gint64 seek_pos)
2417 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2418 g_return_val_if_fail (seek_pos >= 0, FALSE);
2420 return gst_element_seek (element, 1.0, format, seek_flags,
2421 GST_SEEK_TYPE_SET, seek_pos, GST_SEEK_TYPE_NONE, 0);
2425 * gst_pad_use_fixed_caps:
2426 * @pad: the pad to use
2428 * A helper function you can use that sets the
2429 * @gst_pad_get_fixed_caps_func as the getcaps function for the
2430 * pad. This way the function will always return the negotiated caps
2431 * or in case the pad is not negotiated, the padtemplate caps.
2433 * Use this function on a pad that, once gst_pad_set_caps() has been called
2434 * on it, cannot be renegotiated to something else.
2437 gst_pad_use_fixed_caps (GstPad * pad)
2439 gst_pad_set_getcaps_function (pad, gst_pad_get_fixed_caps_func);
2443 * gst_pad_get_fixed_caps_func:
2444 * @pad: the pad to use
2446 * A helper function you can use as a GetCaps function that
2447 * will return the currently negotiated caps or the padtemplate
2450 * Free-function: gst_caps_unref
2452 * Returns: (transfer full): the currently negotiated caps or the padtemplate.
2455 gst_pad_get_fixed_caps_func (GstPad * pad)
2459 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2461 GST_OBJECT_LOCK (pad);
2462 if (GST_PAD_CAPS (pad)) {
2463 result = GST_PAD_CAPS (pad);
2465 GST_CAT_DEBUG (GST_CAT_CAPS,
2466 "using pad caps %p %" GST_PTR_FORMAT, result, result);
2468 result = gst_caps_ref (result);
2469 } else if (GST_PAD_PAD_TEMPLATE (pad)) {
2470 GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
2472 result = GST_PAD_TEMPLATE_CAPS (templ);
2473 GST_CAT_DEBUG (GST_CAT_CAPS,
2474 "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2477 result = gst_caps_ref (result);
2479 GST_CAT_DEBUG (GST_CAT_CAPS, "pad has no caps");
2480 result = gst_caps_new_empty ();
2482 GST_OBJECT_UNLOCK (pad);
2488 * gst_pad_get_parent_element:
2491 * Gets the parent of @pad, cast to a #GstElement. If a @pad has no parent or
2492 * its parent is not an element, return NULL.
2494 * Returns: (transfer full): the parent of the pad. The caller has a
2495 * reference on the parent, so unref when you're finished with it.
2500 gst_pad_get_parent_element (GstPad * pad)
2504 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2506 p = gst_object_get_parent (GST_OBJECT_CAST (pad));
2508 if (p && !GST_IS_ELEMENT (p)) {
2509 gst_object_unref (p);
2512 return GST_ELEMENT_CAST (p);
2516 * gst_object_default_error:
2517 * @source: the #GstObject that initiated the error.
2518 * @error: (in): the GError.
2519 * @debug: (in) (allow-none): an additional debug information string, or NULL
2521 * A default error function.
2523 * The default handler will simply print the error string using g_print.
2526 gst_object_default_error (GstObject * source, const GError * error,
2527 const gchar * debug)
2529 gchar *name = gst_object_get_path_string (source);
2531 /* FIXME 0.11: should change this to g_printerr() */
2532 g_print (_("ERROR: from element %s: %s\n"), name, error->message);
2534 g_print (_("Additional debug info:\n%s\n"), debug);
2542 * @element_1: (transfer full): the #GstElement element to add to the bin
2543 * @...: (transfer full): additional elements to add to the bin
2545 * Adds a NULL-terminated list of elements to a bin. This function is
2546 * equivalent to calling gst_bin_add() for each member of the list. The return
2547 * value of each gst_bin_add() is ignored.
2550 gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
2554 g_return_if_fail (GST_IS_BIN (bin));
2555 g_return_if_fail (GST_IS_ELEMENT (element_1));
2557 va_start (args, element_1);
2560 gst_bin_add (bin, element_1);
2562 element_1 = va_arg (args, GstElement *);
2569 * gst_bin_remove_many:
2571 * @element_1: (transfer none): the first #GstElement to remove from the bin
2572 * @...: (transfer none): NULL-terminated list of elements to remove from the bin
2574 * Remove a list of elements from a bin. This function is equivalent
2575 * to calling gst_bin_remove() with each member of the list.
2578 gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
2582 g_return_if_fail (GST_IS_BIN (bin));
2583 g_return_if_fail (GST_IS_ELEMENT (element_1));
2585 va_start (args, element_1);
2588 gst_bin_remove (bin, element_1);
2590 element_1 = va_arg (args, GstElement *);
2597 gst_element_populate_std_props (GObjectClass * klass, const gchar * prop_name,
2598 guint arg_id, GParamFlags flags)
2600 GQuark prop_id = g_quark_from_string (prop_name);
2603 static GQuark fd_id = 0;
2604 static GQuark blocksize_id;
2605 static GQuark bytesperread_id;
2606 static GQuark dump_id;
2607 static GQuark filesize_id;
2608 static GQuark mmapsize_id;
2609 static GQuark location_id;
2610 static GQuark offset_id;
2611 static GQuark silent_id;
2612 static GQuark touch_id;
2614 flags |= G_PARAM_STATIC_STRINGS;
2617 fd_id = g_quark_from_static_string ("fd");
2618 blocksize_id = g_quark_from_static_string ("blocksize");
2619 bytesperread_id = g_quark_from_static_string ("bytesperread");
2620 dump_id = g_quark_from_static_string ("dump");
2621 filesize_id = g_quark_from_static_string ("filesize");
2622 mmapsize_id = g_quark_from_static_string ("mmapsize");
2623 location_id = g_quark_from_static_string ("location");
2624 offset_id = g_quark_from_static_string ("offset");
2625 silent_id = g_quark_from_static_string ("silent");
2626 touch_id = g_quark_from_static_string ("touch");
2629 if (prop_id == fd_id) {
2630 pspec = g_param_spec_int ("fd", "File-descriptor",
2631 "File-descriptor for the file being read", 0, G_MAXINT, 0, flags);
2632 } else if (prop_id == blocksize_id) {
2633 pspec = g_param_spec_ulong ("blocksize", "Block Size",
2634 "Block size to read per buffer", 0, G_MAXULONG, 4096, flags);
2636 } else if (prop_id == bytesperread_id) {
2637 pspec = g_param_spec_int ("bytesperread", "Bytes per read",
2638 "Number of bytes to read per buffer", G_MININT, G_MAXINT, 0, flags);
2640 } else if (prop_id == dump_id) {
2641 pspec = g_param_spec_boolean ("dump", "Dump",
2642 "Dump bytes to stdout", FALSE, flags);
2644 } else if (prop_id == filesize_id) {
2645 pspec = g_param_spec_int64 ("filesize", "File Size",
2646 "Size of the file being read", 0, G_MAXINT64, 0, flags);
2648 } else if (prop_id == mmapsize_id) {
2649 pspec = g_param_spec_ulong ("mmapsize", "mmap() Block Size",
2650 "Size in bytes of mmap()d regions", 0, G_MAXULONG, 4 * 1048576, flags);
2652 } else if (prop_id == location_id) {
2653 pspec = g_param_spec_string ("location", "File Location",
2654 "Location of the file to read", NULL, flags);
2656 } else if (prop_id == offset_id) {
2657 pspec = g_param_spec_int64 ("offset", "File Offset",
2658 "Byte offset of current read pointer", 0, G_MAXINT64, 0, flags);
2660 } else if (prop_id == silent_id) {
2661 pspec = g_param_spec_boolean ("silent", "Silent", "Don't produce events",
2664 } else if (prop_id == touch_id) {
2665 pspec = g_param_spec_boolean ("touch", "Touch read data",
2666 "Touch data to force disk read before " "push ()", TRUE, flags);
2668 g_warning ("Unknown - 'standard' property '%s' id %d from klass %s",
2669 prop_name, arg_id, g_type_name (G_OBJECT_CLASS_TYPE (klass)));
2674 g_object_class_install_property (klass, arg_id, pspec);
2679 * gst_element_class_install_std_props:
2680 * @klass: the #GstElementClass to add the properties to.
2681 * @first_name: the name of the first property.
2682 * in a NULL terminated
2683 * @...: the id and flags of the first property, followed by
2684 * further 'name', 'id', 'flags' triplets and terminated by NULL.
2686 * Adds a list of standardized properties with types to the @klass.
2687 * the id is for the property switch in your get_prop method, and
2688 * the flags determine readability / writeability.
2691 gst_element_class_install_std_props (GstElementClass * klass,
2692 const gchar * first_name, ...)
2698 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
2700 va_start (args, first_name);
2705 int arg_id = va_arg (args, int);
2706 GParamFlags flags = (GParamFlags) va_arg (args, int);
2708 gst_element_populate_std_props ((GObjectClass *) klass, name, arg_id,
2711 name = va_arg (args, char *);
2720 * @buf1: (transfer none): the first source #GstBuffer to merge.
2721 * @buf2: (transfer none): the second source #GstBuffer to merge.
2723 * Create a new buffer that is the concatenation of the two source
2724 * buffers. The original source buffers will not be modified or
2725 * unref'd. Make sure you unref the source buffers if they are not used
2726 * anymore afterwards.
2728 * If the buffers point to contiguous areas of memory, the buffer
2729 * is created without copying the data.
2731 * Free-function: gst_buffer_unref
2733 * Returns: (transfer full): the new #GstBuffer which is the concatenation
2734 * of the source buffers.
2737 gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2)
2741 /* we're just a specific case of the more general gst_buffer_span() */
2742 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
2749 * @buf1: the first source #GstBuffer.
2750 * @buf2: the second source #GstBuffer.
2752 * Create a new buffer that is the concatenation of the two source
2753 * buffers, and unrefs the original source buffers.
2755 * If the buffers point to contiguous areas of memory, the buffer
2756 * is created without copying the data.
2758 * This is a convenience function for C programmers. See also
2759 * gst_buffer_merge(), which does the same thing without
2760 * unreffing the input parameters. Language bindings without
2761 * explicit reference counting should not wrap this function.
2763 * Returns: (transfer full): the new #GstBuffer which is the concatenation of
2764 * the source buffers.
2767 gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2)
2771 result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
2772 gst_buffer_unref (buf1);
2773 gst_buffer_unref (buf2);
2781 * @dest: (transfer none): buffer to stamp
2782 * @src: buffer to stamp from
2784 * Copies additional information (the timestamp, duration, and offset start
2785 * and end) from one buffer to the other.
2787 * This function does not copy any buffer flags or caps and is equivalent to
2788 * gst_buffer_copy_metadata(@dest, @src, GST_BUFFER_COPY_TIMESTAMPS).
2790 * Deprecated: use gst_buffer_copy_metadata() instead, it provides more
2793 #ifndef GST_REMOVE_DEPRECATED
2794 #ifdef GST_DISABLE_DEPRECATED
2795 void gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src);
2798 gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src)
2800 gst_buffer_copy_metadata (dest, src, GST_BUFFER_COPY_TIMESTAMPS);
2802 #endif /* GST_REMOVE_DEPRECATED */
2805 getcaps_fold_func (GstPad * pad, GValue * ret, GstPad * orig)
2807 gboolean empty = FALSE;
2808 GstCaps *peercaps, *existing;
2810 existing = g_value_get_pointer (ret);
2811 peercaps = gst_pad_peer_get_caps_reffed (pad);
2812 if (G_LIKELY (peercaps)) {
2813 GstCaps *intersection = gst_caps_intersect (existing, peercaps);
2815 empty = gst_caps_is_empty (intersection);
2817 g_value_set_pointer (ret, intersection);
2818 gst_caps_unref (existing);
2819 gst_caps_unref (peercaps);
2821 gst_object_unref (pad);
2826 * gst_pad_proxy_getcaps:
2827 * @pad: a #GstPad to proxy.
2829 * Calls gst_pad_get_allowed_caps() for every other pad belonging to the
2830 * same element as @pad, and returns the intersection of the results.
2832 * This function is useful as a default getcaps function for an element
2833 * that can handle any stream format, but requires all its pads to have
2834 * the same caps. Two such elements are tee and adder.
2836 * Free-function: gst_caps_unref
2838 * Returns: (transfer full): the intersection of the other pads' allowed caps.
2841 gst_pad_proxy_getcaps (GstPad * pad)
2843 GstElement *element;
2844 GstCaps *caps, *intersected;
2846 GstIteratorResult res;
2847 GValue ret = { 0, };
2849 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2851 GST_CAT_DEBUG (GST_CAT_PADS, "proxying getcaps for %s:%s",
2852 GST_DEBUG_PAD_NAME (pad));
2854 element = gst_pad_get_parent_element (pad);
2855 if (element == NULL)
2858 /* value to hold the return, by default it holds ANY, the ref is taken by
2860 g_value_init (&ret, G_TYPE_POINTER);
2861 g_value_set_pointer (&ret, gst_caps_new_any ());
2863 /* only iterate the pads in the oposite direction */
2864 if (GST_PAD_IS_SRC (pad))
2865 iter = gst_element_iterate_sink_pads (element);
2867 iter = gst_element_iterate_src_pads (element);
2871 gst_iterator_fold (iter, (GstIteratorFoldFunction) getcaps_fold_func,
2874 case GST_ITERATOR_RESYNC:
2875 /* unref any value stored */
2876 if ((caps = g_value_get_pointer (&ret)))
2877 gst_caps_unref (caps);
2878 /* need to reset the result again to ANY */
2879 g_value_set_pointer (&ret, gst_caps_new_any ());
2880 gst_iterator_resync (iter);
2882 case GST_ITERATOR_DONE:
2883 /* all pads iterated, return collected value */
2885 case GST_ITERATOR_OK:
2886 /* premature exit (happens if caps intersection is empty) */
2889 /* iterator returned _ERROR, mark an error and exit */
2890 if ((caps = g_value_get_pointer (&ret)))
2891 gst_caps_unref (caps);
2892 g_value_set_pointer (&ret, NULL);
2897 gst_iterator_free (iter);
2899 gst_object_unref (element);
2901 caps = g_value_get_pointer (&ret);
2902 g_value_unset (&ret);
2906 gst_caps_intersect (caps, gst_pad_get_pad_template_caps (pad));
2907 gst_caps_unref (caps);
2909 intersected = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
2917 GST_DEBUG_OBJECT (pad, "no parent");
2918 return gst_caps_copy (gst_pad_get_pad_template_caps (pad));
2922 g_warning ("Pad list returned error on element %s",
2923 GST_ELEMENT_NAME (element));
2924 gst_iterator_free (iter);
2925 gst_object_unref (element);
2926 return gst_caps_copy (gst_pad_get_pad_template_caps (pad));
2937 setcaps_fold_func (GstPad * pad, GValue * ret, SetCapsFoldData * data)
2939 gboolean success = TRUE;
2941 if (pad != data->orig) {
2942 success = gst_pad_set_caps (pad, data->caps);
2943 g_value_set_boolean (ret, success);
2945 gst_object_unref (pad);
2951 * gst_pad_proxy_setcaps
2952 * @pad: a #GstPad to proxy from
2953 * @caps: (transfer none): the #GstCaps to link with
2955 * Calls gst_pad_set_caps() for every other pad belonging to the
2956 * same element as @pad. If gst_pad_set_caps() fails on any pad,
2957 * the proxy setcaps fails. May be used only during negotiation.
2959 * Returns: TRUE if successful
2962 gst_pad_proxy_setcaps (GstPad * pad, GstCaps * caps)
2964 GstElement *element;
2966 GstIteratorResult res;
2967 GValue ret = { 0, };
2968 SetCapsFoldData data;
2970 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2971 g_return_val_if_fail (caps != NULL, FALSE);
2973 GST_CAT_DEBUG (GST_CAT_PADS, "proxying pad link for %s:%s",
2974 GST_DEBUG_PAD_NAME (pad));
2976 element = gst_pad_get_parent_element (pad);
2977 if (element == NULL)
2980 /* only iterate the pads in the oposite direction */
2981 if (GST_PAD_IS_SRC (pad))
2982 iter = gst_element_iterate_sink_pads (element);
2984 iter = gst_element_iterate_src_pads (element);
2986 g_value_init (&ret, G_TYPE_BOOLEAN);
2987 g_value_set_boolean (&ret, TRUE);
2992 res = gst_iterator_fold (iter, (GstIteratorFoldFunction) setcaps_fold_func,
2996 case GST_ITERATOR_RESYNC:
2997 /* reset return value */
2998 g_value_set_boolean (&ret, TRUE);
2999 gst_iterator_resync (iter);
3001 case GST_ITERATOR_DONE:
3002 /* all pads iterated, return collected value */
3005 /* iterator returned _ERROR or premature end with _OK,
3006 * mark an error and exit */
3011 gst_iterator_free (iter);
3013 gst_object_unref (element);
3015 /* ok not to unset the gvalue */
3016 return g_value_get_boolean (&ret);
3021 g_warning ("Pad list return error on element %s",
3022 GST_ELEMENT_NAME (element));
3023 gst_iterator_free (iter);
3024 gst_object_unref (element);
3030 * gst_pad_query_position:
3031 * @pad: a #GstPad to invoke the position query on.
3032 * @format: (inout): a pointer to the #GstFormat asked for.
3033 * On return contains the #GstFormat used.
3034 * @cur: (out): A location in which to store the current position, or NULL.
3036 * Queries a pad for the stream position.
3038 * Returns: TRUE if the query could be performed.
3041 gst_pad_query_position (GstPad * pad, GstFormat * format, gint64 * cur)
3046 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3047 g_return_val_if_fail (format != NULL, FALSE);
3049 query = gst_query_new_position (*format);
3050 ret = gst_pad_query (pad, query);
3053 gst_query_parse_position (query, format, cur);
3055 gst_query_unref (query);
3061 * gst_pad_query_peer_position:
3062 * @pad: a #GstPad on whose peer to invoke the position query on.
3063 * Must be a sink pad.
3064 * @format: (inout): a pointer to the #GstFormat asked for.
3065 * On return contains the #GstFormat used.
3066 * @cur: (out) (allow-none): a location in which to store the current
3067 * position, or NULL.
3069 * Queries the peer of a given sink pad for the stream position.
3071 * Returns: TRUE if the query could be performed.
3074 gst_pad_query_peer_position (GstPad * pad, GstFormat * format, gint64 * cur)
3076 gboolean ret = FALSE;
3079 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3080 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
3081 g_return_val_if_fail (format != NULL, FALSE);
3083 peer = gst_pad_get_peer (pad);
3085 ret = gst_pad_query_position (peer, format, cur);
3086 gst_object_unref (peer);
3093 * gst_pad_query_duration:
3094 * @pad: a #GstPad to invoke the duration query on.
3095 * @format: (inout): a pointer to the #GstFormat asked for.
3096 * On return contains the #GstFormat used.
3097 * @duration: (out) (allow-none): a location in which to store the total
3098 * duration, or NULL.
3100 * Queries a pad for the total stream duration.
3102 * Returns: TRUE if the query could be performed.
3105 gst_pad_query_duration (GstPad * pad, GstFormat * format, gint64 * duration)
3110 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3111 g_return_val_if_fail (format != NULL, FALSE);
3113 query = gst_query_new_duration (*format);
3114 ret = gst_pad_query (pad, query);
3117 gst_query_parse_duration (query, format, duration);
3119 gst_query_unref (query);
3125 * gst_pad_query_peer_duration:
3126 * @pad: a #GstPad on whose peer pad to invoke the duration query on.
3127 * Must be a sink pad.
3128 * @format: (inout) :a pointer to the #GstFormat asked for.
3129 * On return contains the #GstFormat used.
3130 * @duration: (out) (allow-none): a location in which to store the total
3131 * duration, or NULL.
3133 * Queries the peer pad of a given sink pad for the total stream duration.
3135 * Returns: TRUE if the query could be performed.
3138 gst_pad_query_peer_duration (GstPad * pad, GstFormat * format,
3141 gboolean ret = FALSE;
3144 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3145 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
3146 g_return_val_if_fail (format != NULL, FALSE);
3148 peer = gst_pad_get_peer (pad);
3150 ret = gst_pad_query_duration (peer, format, duration);
3151 gst_object_unref (peer);
3158 * gst_pad_query_convert:
3159 * @pad: a #GstPad to invoke the convert query on.
3160 * @src_format: a #GstFormat to convert from.
3161 * @src_val: a value to convert.
3162 * @dest_format: (inout): a pointer to the #GstFormat to convert to.
3163 * @dest_val: (out): a pointer to the result.
3165 * Queries a pad to convert @src_val in @src_format to @dest_format.
3167 * Returns: TRUE if the query could be performed.
3170 gst_pad_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
3171 GstFormat * dest_format, gint64 * dest_val)
3176 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3177 g_return_val_if_fail (dest_format != NULL, FALSE);
3178 g_return_val_if_fail (dest_val != NULL, FALSE);
3180 if (*dest_format == src_format || src_val == -1) {
3181 *dest_val = src_val;
3185 query = gst_query_new_convert (src_format, src_val, *dest_format);
3186 ret = gst_pad_query (pad, query);
3189 gst_query_parse_convert (query, NULL, NULL, dest_format, dest_val);
3191 gst_query_unref (query);
3197 * gst_pad_query_peer_convert:
3198 * @pad: a #GstPad, on whose peer pad to invoke the convert query on.
3199 * Must be a sink pad.
3200 * @src_format: a #GstFormat to convert from.
3201 * @src_val: a value to convert.
3202 * @dest_format: (inout): a pointer to the #GstFormat to convert to.
3203 * @dest_val: (out): a pointer to the result.
3205 * Queries the peer pad of a given sink pad to convert @src_val in @src_format
3208 * Returns: TRUE if the query could be performed.
3211 gst_pad_query_peer_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
3212 GstFormat * dest_format, gint64 * dest_val)
3214 gboolean ret = FALSE;
3217 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3218 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
3219 g_return_val_if_fail (dest_format != NULL, FALSE);
3220 g_return_val_if_fail (dest_val != NULL, FALSE);
3222 peer = gst_pad_get_peer (pad);
3224 ret = gst_pad_query_convert (peer, src_format, src_val, dest_format,
3226 gst_object_unref (peer);
3233 * gst_atomic_int_set:
3234 * @atomic_int: (inout): pointer to an atomic integer
3235 * @value: value to set
3237 * Unconditionally sets the atomic integer to @value.
3239 * Deprecated: Use g_atomic_int_set().
3242 #ifndef GST_REMOVE_DEPRECATED
3243 #ifdef GST_DISABLE_DEPRECATED
3244 void gst_atomic_int_set (gint * atomic_int, gint value);
3247 gst_atomic_int_set (gint * atomic_int, gint value)
3249 g_atomic_int_set (atomic_int, value);
3254 * gst_pad_add_data_probe:
3255 * @pad: pad to add the data probe handler to
3256 * @handler: function to call when data is passed over pad
3257 * @data: (closure): data to pass along with the handler
3259 * Adds a "data probe" to a pad. This function will be called whenever data
3260 * passes through a pad. In this case data means both events and buffers. The
3261 * probe will be called with the data as an argument, meaning @handler should
3262 * have the same callback signature as the #GstPad::have-data signal.
3263 * Note that the data will have a reference count greater than 1, so it will
3264 * be immutable -- you must not change it.
3266 * For source pads, the probe will be called after the blocking function, if any
3267 * (see gst_pad_set_blocked_async()), but before looking up the peer to chain
3268 * to. For sink pads, the probe function will be called before configuring the
3269 * sink with new caps, if any, and before calling the pad's chain function.
3271 * Your data probe should return TRUE to let the data continue to flow, or FALSE
3272 * to drop it. Dropping data is rarely useful, but occasionally comes in handy
3275 * Although probes are implemented internally by connecting @handler to the
3276 * have-data signal on the pad, if you want to remove a probe it is insufficient
3277 * to only call g_signal_handler_disconnect on the returned handler id. To
3278 * remove a probe, use the appropriate function, such as
3279 * gst_pad_remove_data_probe().
3281 * Returns: The handler id.
3284 gst_pad_add_data_probe (GstPad * pad, GCallback handler, gpointer data)
3286 return gst_pad_add_data_probe_full (pad, handler, data, NULL);
3290 * gst_pad_add_data_probe_full:
3291 * @pad: pad to add the data probe handler to
3292 * @handler: function to call when data is passed over pad
3293 * @data: (closure): data to pass along with the handler
3294 * @notify: (allow-none): function to call when the probe is disconnected,
3297 * Adds a "data probe" to a pad. This function will be called whenever data
3298 * passes through a pad. In this case data means both events and buffers. The
3299 * probe will be called with the data as an argument, meaning @handler should
3300 * have the same callback signature as the #GstPad::have-data signal.
3301 * Note that the data will have a reference count greater than 1, so it will
3302 * be immutable -- you must not change it.
3304 * For source pads, the probe will be called after the blocking function, if any
3305 * (see gst_pad_set_blocked_async()), but before looking up the peer to chain
3306 * to. For sink pads, the probe function will be called before configuring the
3307 * sink with new caps, if any, and before calling the pad's chain function.
3309 * Your data probe should return TRUE to let the data continue to flow, or FALSE
3310 * to drop it. Dropping data is rarely useful, but occasionally comes in handy
3313 * Although probes are implemented internally by connecting @handler to the
3314 * have-data signal on the pad, if you want to remove a probe it is insufficient
3315 * to only call g_signal_handler_disconnect on the returned handler id. To
3316 * remove a probe, use the appropriate function, such as
3317 * gst_pad_remove_data_probe().
3319 * The @notify function is called when the probe is disconnected and usually
3320 * used to free @data.
3322 * Returns: The handler id.
3327 gst_pad_add_data_probe_full (GstPad * pad, GCallback handler,
3328 gpointer data, GDestroyNotify notify)
3332 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3333 g_return_val_if_fail (handler != NULL, 0);
3335 GST_OBJECT_LOCK (pad);
3337 /* we only expose a GDestroyNotify in our API because that's less confusing */
3338 sigid = g_signal_connect_data (pad, "have-data", handler, data,
3339 (GClosureNotify) notify, 0);
3341 GST_PAD_DO_EVENT_SIGNALS (pad)++;
3342 GST_PAD_DO_BUFFER_SIGNALS (pad)++;
3343 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3344 "adding data probe, now %d data, %d event probes",
3345 GST_PAD_DO_BUFFER_SIGNALS (pad), GST_PAD_DO_EVENT_SIGNALS (pad));
3346 _priv_gst_pad_invalidate_cache (pad);
3347 GST_OBJECT_UNLOCK (pad);
3353 * gst_pad_add_event_probe:
3354 * @pad: pad to add the event probe handler to
3355 * @handler: function to call when events are passed over pad
3356 * @data: (closure): data to pass along with the handler
3358 * Adds a probe that will be called for all events passing through a pad. See
3359 * gst_pad_add_data_probe() for more information.
3361 * Returns: The handler id
3364 gst_pad_add_event_probe (GstPad * pad, GCallback handler, gpointer data)
3366 return gst_pad_add_event_probe_full (pad, handler, data, NULL);
3370 * gst_pad_add_event_probe_full:
3371 * @pad: pad to add the event probe handler to
3372 * @handler: function to call when events are passed over pad
3373 * @data: (closure): data to pass along with the handler, or NULL
3374 * @notify: (allow-none): function to call when probe is disconnected, or NULL
3376 * Adds a probe that will be called for all events passing through a pad. See
3377 * gst_pad_add_data_probe() for more information.
3379 * The @notify function is called when the probe is disconnected and usually
3380 * used to free @data.
3382 * Returns: The handler id
3387 gst_pad_add_event_probe_full (GstPad * pad, GCallback handler,
3388 gpointer data, GDestroyNotify notify)
3392 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3393 g_return_val_if_fail (handler != NULL, 0);
3395 GST_OBJECT_LOCK (pad);
3397 /* we only expose a GDestroyNotify in our API because that's less confusing */
3398 sigid = g_signal_connect_data (pad, "have-data::event", handler, data,
3399 (GClosureNotify) notify, 0);
3401 GST_PAD_DO_EVENT_SIGNALS (pad)++;
3402 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "adding event probe, now %d probes",
3403 GST_PAD_DO_EVENT_SIGNALS (pad));
3404 _priv_gst_pad_invalidate_cache (pad);
3405 GST_OBJECT_UNLOCK (pad);
3411 * gst_pad_add_buffer_probe:
3412 * @pad: pad to add the buffer probe handler to
3413 * @handler: function to call when buffers are passed over pad
3414 * @data: (closure): data to pass along with the handler
3416 * Adds a probe that will be called for all buffers passing through a pad. See
3417 * gst_pad_add_data_probe() for more information.
3419 * Returns: The handler id
3422 gst_pad_add_buffer_probe (GstPad * pad, GCallback handler, gpointer data)
3424 return gst_pad_add_buffer_probe_full (pad, handler, data, NULL);
3428 * gst_pad_add_buffer_probe_full:
3429 * @pad: pad to add the buffer probe handler to
3430 * @handler: function to call when buffer are passed over pad
3431 * @data: (closure): data to pass along with the handler
3432 * @notify: (allow-none): function to call when the probe is disconnected,
3435 * Adds a probe that will be called for all buffers passing through a pad. See
3436 * gst_pad_add_data_probe() for more information.
3438 * The @notify function is called when the probe is disconnected and usually
3439 * used to free @data.
3441 * Returns: The handler id
3446 gst_pad_add_buffer_probe_full (GstPad * pad, GCallback handler,
3447 gpointer data, GDestroyNotify notify)
3451 g_return_val_if_fail (GST_IS_PAD (pad), 0);
3452 g_return_val_if_fail (handler != NULL, 0);
3454 GST_OBJECT_LOCK (pad);
3456 /* we only expose a GDestroyNotify in our API because that's less confusing */
3457 sigid = g_signal_connect_data (pad, "have-data::buffer", handler, data,
3458 (GClosureNotify) notify, 0);
3460 GST_PAD_DO_BUFFER_SIGNALS (pad)++;
3461 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "adding buffer probe, now %d probes",
3462 GST_PAD_DO_BUFFER_SIGNALS (pad));
3463 _priv_gst_pad_invalidate_cache (pad);
3464 GST_OBJECT_UNLOCK (pad);
3470 * gst_pad_remove_data_probe:
3471 * @pad: pad to remove the data probe handler from
3472 * @handler_id: handler id returned from gst_pad_add_data_probe
3474 * Removes a data probe from @pad.
3477 gst_pad_remove_data_probe (GstPad * pad, guint handler_id)
3479 g_return_if_fail (GST_IS_PAD (pad));
3480 g_return_if_fail (handler_id > 0);
3482 GST_OBJECT_LOCK (pad);
3483 g_signal_handler_disconnect (pad, handler_id);
3484 GST_PAD_DO_BUFFER_SIGNALS (pad)--;
3485 GST_PAD_DO_EVENT_SIGNALS (pad)--;
3486 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3487 "removed data probe, now %d event, %d buffer probes",
3488 GST_PAD_DO_EVENT_SIGNALS (pad), GST_PAD_DO_BUFFER_SIGNALS (pad));
3489 GST_OBJECT_UNLOCK (pad);
3494 * gst_pad_remove_event_probe:
3495 * @pad: pad to remove the event probe handler from
3496 * @handler_id: handler id returned from gst_pad_add_event_probe
3498 * Removes an event probe from @pad.
3501 gst_pad_remove_event_probe (GstPad * pad, guint handler_id)
3503 g_return_if_fail (GST_IS_PAD (pad));
3504 g_return_if_fail (handler_id > 0);
3506 GST_OBJECT_LOCK (pad);
3507 g_signal_handler_disconnect (pad, handler_id);
3508 GST_PAD_DO_EVENT_SIGNALS (pad)--;
3509 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3510 "removed event probe, now %d event probes",
3511 GST_PAD_DO_EVENT_SIGNALS (pad));
3512 GST_OBJECT_UNLOCK (pad);
3516 * gst_pad_remove_buffer_probe:
3517 * @pad: pad to remove the buffer probe handler from
3518 * @handler_id: handler id returned from gst_pad_add_buffer_probe
3520 * Removes a buffer probe from @pad.
3523 gst_pad_remove_buffer_probe (GstPad * pad, guint handler_id)
3525 g_return_if_fail (GST_IS_PAD (pad));
3526 g_return_if_fail (handler_id > 0);
3528 GST_OBJECT_LOCK (pad);
3529 g_signal_handler_disconnect (pad, handler_id);
3530 GST_PAD_DO_BUFFER_SIGNALS (pad)--;
3531 GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3532 "removed buffer probe, now %d buffer probes",
3533 GST_PAD_DO_BUFFER_SIGNALS (pad));
3534 GST_OBJECT_UNLOCK (pad);
3539 * gst_element_found_tags_for_pad:
3540 * @element: element for which to post taglist to bus.
3541 * @pad: (transfer none): pad on which to push tag-event
3542 * @list: (transfer full): the taglist to post on the bus and create event from
3544 * Posts a message to the bus that new tags were found and pushes the
3545 * tags as event. Takes ownership of the @list.
3547 * This is a utility method for elements. Applications should use the
3548 * #GstTagSetter interface.
3551 gst_element_found_tags_for_pad (GstElement * element,
3552 GstPad * pad, GstTagList * list)
3554 g_return_if_fail (element != NULL);
3555 g_return_if_fail (pad != NULL);
3556 g_return_if_fail (list != NULL);
3558 gst_pad_push_event (pad, gst_event_new_tag (gst_tag_list_copy (list)));
3559 /* FIXME 0.11: Set the pad as source. */
3560 gst_element_post_message (element,
3561 gst_message_new_tag_full (GST_OBJECT (element), pad, list));
3565 push_and_ref (GstPad * pad, GstEvent * event)
3567 gst_pad_push_event (pad, gst_event_ref (event));
3568 /* iterator refs pad, we unref when we are done with it */
3569 gst_object_unref (pad);
3573 * gst_element_found_tags:
3574 * @element: element for which we found the tags.
3575 * @list: (transfer full): list of tags.
3577 * Posts a message to the bus that new tags were found, and pushes an event
3578 * to all sourcepads. Takes ownership of the @list.
3580 * This is a utility method for elements. Applications should use the
3581 * #GstTagSetter interface.
3584 gst_element_found_tags (GstElement * element, GstTagList * list)
3589 g_return_if_fail (element != NULL);
3590 g_return_if_fail (list != NULL);
3592 iter = gst_element_iterate_src_pads (element);
3593 event = gst_event_new_tag (gst_tag_list_copy (list));
3594 gst_iterator_foreach (iter, (GFunc) push_and_ref, event);
3595 gst_iterator_free (iter);
3596 gst_event_unref (event);
3598 gst_element_post_message (element,
3599 gst_message_new_tag (GST_OBJECT (element), list));
3603 element_find_unlinked_pad (GstElement * element, GstPadDirection direction)
3606 GstPad *unlinked_pad = NULL;
3609 switch (direction) {
3611 iter = gst_element_iterate_src_pads (element);
3614 iter = gst_element_iterate_sink_pads (element);
3617 g_return_val_if_reached (NULL);
3624 switch (gst_iterator_next (iter, &pad)) {
3625 case GST_ITERATOR_OK:{
3628 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
3629 GST_DEBUG_PAD_NAME (pad));
3631 peer = gst_pad_get_peer (GST_PAD (pad));
3635 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
3636 "found existing unlinked pad %s:%s",
3637 GST_DEBUG_PAD_NAME (unlinked_pad));
3639 gst_object_unref (pad);
3640 gst_object_unref (peer);
3644 case GST_ITERATOR_DONE:
3647 case GST_ITERATOR_RESYNC:
3648 gst_iterator_resync (iter);
3650 case GST_ITERATOR_ERROR:
3651 g_return_val_if_reached (NULL);
3656 gst_iterator_free (iter);
3658 return unlinked_pad;
3662 * gst_bin_find_unlinked_pad:
3663 * @bin: bin in which to look for elements with unlinked pads
3664 * @direction: whether to look for an unlinked source or sink pad
3666 * Recursively looks for elements with an unlinked pad of the given
3667 * direction within the specified bin and returns an unlinked pad
3668 * if one is found, or NULL otherwise. If a pad is found, the caller
3669 * owns a reference to it and should use gst_object_unref() on the
3670 * pad when it is not needed any longer.
3672 * Returns: (transfer full): unlinked pad of the given direction, or NULL.
3677 gst_bin_find_unlinked_pad (GstBin * bin, GstPadDirection direction)
3683 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3684 g_return_val_if_fail (direction != GST_PAD_UNKNOWN, NULL);
3687 iter = gst_bin_iterate_recurse (bin);
3691 switch (gst_iterator_next (iter, &element)) {
3692 case GST_ITERATOR_OK:
3693 pad = element_find_unlinked_pad (GST_ELEMENT (element), direction);
3694 gst_object_unref (element);
3698 case GST_ITERATOR_DONE:
3701 case GST_ITERATOR_RESYNC:
3702 gst_iterator_resync (iter);
3704 case GST_ITERATOR_ERROR:
3705 g_return_val_if_reached (NULL);
3710 gst_iterator_free (iter);
3716 * gst_bin_find_unconnected_pad:
3717 * @bin: bin in which to look for elements with unlinked pads
3718 * @direction: whether to look for an unlinked source or sink pad
3720 * Recursively looks for elements with an unlinked pad of the given
3721 * direction within the specified bin and returns an unlinked pad
3722 * if one is found, or NULL otherwise. If a pad is found, the caller
3723 * owns a reference to it and should use gst_object_unref() on the
3724 * pad when it is not needed any longer.
3726 * Returns: (transfer full): unlinked pad of the given direction, or NULL.
3730 * Deprecated: use gst_bin_find_unlinked_pad() instead.
3732 #ifndef GST_REMOVE_DEPRECATED
3733 #ifdef GST_DISABLE_DEPRECATED
3734 GstPad *gst_bin_find_unconnected_pad (GstBin * bin, GstPadDirection direction);
3737 gst_bin_find_unconnected_pad (GstBin * bin, GstPadDirection direction)
3739 return gst_bin_find_unlinked_pad (bin, direction);
3744 * gst_parse_bin_from_description:
3745 * @bin_description: command line describing the bin
3746 * @ghost_unlinked_pads: whether to automatically create ghost pads
3747 * for unlinked source or sink pads within the bin
3748 * @err: where to store the error message in case of an error, or NULL
3750 * This is a convenience wrapper around gst_parse_launch() to create a
3751 * #GstBin from a gst-launch-style pipeline description. See
3752 * gst_parse_launch() and the gst-launch man page for details about the
3753 * syntax. Ghost pads on the bin for unlinked source or sink pads
3754 * within the bin can automatically be created (but only a maximum of
3755 * one ghost pad for each direction will be created; if you expect
3756 * multiple unlinked source pads or multiple unlinked sink pads
3757 * and want them all ghosted, you will have to create the ghost pads
3760 * Returns: (transfer full): a newly-created bin, or NULL if an error occurred.
3765 gst_parse_bin_from_description (const gchar * bin_description,
3766 gboolean ghost_unlinked_pads, GError ** err)
3768 return gst_parse_bin_from_description_full (bin_description,
3769 ghost_unlinked_pads, NULL, GST_PARSE_FLAG_NONE, err);
3773 * gst_parse_bin_from_description_full:
3774 * @bin_description: command line describing the bin
3775 * @ghost_unlinked_pads: whether to automatically create ghost pads
3776 * for unlinked source or sink pads within the bin
3777 * @context: (transfer none) (allow-none): a parse context allocated with
3778 * gst_parse_context_new(), or %NULL
3779 * @flags: parsing options, or #GST_PARSE_FLAG_NONE
3780 * @err: where to store the error message in case of an error, or NULL
3782 * This is a convenience wrapper around gst_parse_launch() to create a
3783 * #GstBin from a gst-launch-style pipeline description. See
3784 * gst_parse_launch() and the gst-launch man page for details about the
3785 * syntax. Ghost pads on the bin for unlinked source or sink pads
3786 * within the bin can automatically be created (but only a maximum of
3787 * one ghost pad for each direction will be created; if you expect
3788 * multiple unlinked source pads or multiple unlinked sink pads
3789 * and want them all ghosted, you will have to create the ghost pads
3792 * Returns: (transfer full): a newly-created bin, or NULL if an error occurred.
3797 gst_parse_bin_from_description_full (const gchar * bin_description,
3798 gboolean ghost_unlinked_pads, GstParseContext * context,
3799 GstParseFlags flags, GError ** err)
3801 #ifndef GST_DISABLE_PARSE
3806 g_return_val_if_fail (bin_description != NULL, NULL);
3807 g_return_val_if_fail (err == NULL || *err == NULL, NULL);
3809 GST_DEBUG ("Making bin from description '%s'", bin_description);
3811 /* parse the pipeline to a bin */
3812 desc = g_strdup_printf ("bin.( %s )", bin_description);
3813 bin = (GstBin *) gst_parse_launch_full (desc, context, flags, err);
3816 if (bin == NULL || (err && *err != NULL)) {
3818 gst_object_unref (bin);
3822 /* find pads and ghost them if necessary */
3823 if (ghost_unlinked_pads) {
3824 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC))) {
3825 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("src", pad));
3826 gst_object_unref (pad);
3828 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SINK))) {
3829 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("sink", pad));
3830 gst_object_unref (pad);
3834 return GST_ELEMENT (bin);
3838 GST_WARNING ("Disabled API called");
3840 msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
3841 g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
3849 * gst_type_register_static_full:
3850 * @parent_type: The GType of the parent type the newly registered type will
3852 * @type_name: NULL-terminated string used as the name of the new type
3853 * @class_size: Size of the class structure.
3854 * @base_init: Location of the base initialization function (optional).
3855 * @base_finalize: Location of the base finalization function (optional).
3856 * @class_init: Location of the class initialization function for class types
3857 * Location of the default vtable initialization function for interface
3859 * @class_finalize: Location of the class finalization function for class types.
3860 * Location of the default vtable finalization function for interface types.
3862 * @class_data: User-supplied data passed to the class init/finalize functions.
3863 * @instance_size: Size of the instance (object) structure (required for
3864 * instantiatable types only).
3865 * @n_preallocs: The number of pre-allocated (cached) instances to reserve
3866 * memory for (0 indicates no caching). Ignored on recent GLib's.
3867 * @instance_init: Location of the instance initialization function (optional,
3868 * for instantiatable types only).
3869 * @value_table: A GTypeValueTable function table for generic handling of
3870 * GValues of this type (usually only useful for fundamental types).
3871 * @flags: #GTypeFlags for this GType. E.g: G_TYPE_FLAG_ABSTRACT
3873 * Helper function which constructs a #GTypeInfo structure and registers a
3874 * GType, but which generates less linker overhead than a static const
3875 * #GTypeInfo structure. For further details of the parameters, please see
3876 * #GTypeInfo in the GLib documentation.
3878 * Registers type_name as the name of a new static type derived from
3879 * parent_type. The value of flags determines the nature (e.g. abstract or
3880 * not) of the type. It works by filling a GTypeInfo struct and calling
3881 * g_type_register_static().
3883 * Returns: A #GType for the newly-registered type.
3888 gst_type_register_static_full (GType parent_type,
3889 const gchar * type_name,
3891 GBaseInitFunc base_init,
3892 GBaseFinalizeFunc base_finalize,
3893 GClassInitFunc class_init,
3894 GClassFinalizeFunc class_finalize,
3895 gconstpointer class_data,
3896 guint instance_size,
3897 guint16 n_preallocs,
3898 GInstanceInitFunc instance_init,
3899 const GTypeValueTable * value_table, GTypeFlags flags)
3903 g_return_val_if_fail (class_size <= G_MAXUINT16, G_TYPE_INVALID);
3904 g_return_val_if_fail (instance_size <= G_MAXUINT16, G_TYPE_INVALID);
3906 info.class_size = class_size;
3907 info.base_init = base_init;
3908 info.base_finalize = base_finalize;
3909 info.class_init = class_init;
3910 info.class_finalize = class_finalize;
3911 info.class_data = class_data;
3912 info.instance_size = instance_size;
3913 info.n_preallocs = n_preallocs;
3914 info.instance_init = instance_init;
3915 info.value_table = value_table;
3917 return g_type_register_static (parent_type, type_name, &info, flags);
3922 * gst_util_get_timestamp:
3924 * Get a timestamp as GstClockTime to be used for interval measurements.
3925 * The timestamp should not be interpreted in any other way.
3927 * Returns: the timestamp
3932 gst_util_get_timestamp (void)
3934 #if defined (HAVE_POSIX_TIMERS) && defined(HAVE_MONOTONIC_CLOCK)
3935 struct timespec now;
3937 clock_gettime (CLOCK_MONOTONIC, &now);
3938 return GST_TIMESPEC_TO_TIME (now);
3942 g_get_current_time (&now);
3943 return GST_TIMEVAL_TO_TIME (now);
3948 * gst_util_array_binary_search:
3949 * @array: the sorted input array
3950 * @num_elements: number of elements in the array
3951 * @element_size: size of every element in bytes
3952 * @search_func: (scope call): function to compare two elements, @search_data will always be passed as second argument
3953 * @mode: search mode that should be used
3954 * @search_data: element that should be found
3955 * @user_data: (closure): data to pass to @search_func
3957 * Searches inside @array for @search_data by using the comparison function
3958 * @search_func. @array must be sorted ascending.
3960 * As @search_data is always passed as second argument to @search_func it's
3961 * not required that @search_data has the same type as the array elements.
3963 * The complexity of this search function is O(log (num_elements)).
3965 * Returns: (transfer none): The address of the found element or %NULL if nothing was found
3970 gst_util_array_binary_search (gpointer array, guint num_elements,
3971 gsize element_size, GCompareDataFunc search_func, GstSearchMode mode,
3972 gconstpointer search_data, gpointer user_data)
3974 glong left = 0, right = num_elements - 1, m;
3976 guint8 *data = (guint8 *) array;
3978 g_return_val_if_fail (array != NULL, NULL);
3979 g_return_val_if_fail (element_size > 0, NULL);
3980 g_return_val_if_fail (search_func != NULL, NULL);
3982 /* 0. No elements => return NULL */
3983 if (num_elements == 0)
3986 /* 1. If search_data is before the 0th element return the 0th element */
3987 ret = search_func (data, search_data, user_data);
3988 if ((ret >= 0 && mode == GST_SEARCH_MODE_AFTER) || ret == 0)
3993 /* 2. If search_data is after the last element return the last element */
3995 search_func (data + (num_elements - 1) * element_size, search_data,
3997 if ((ret <= 0 && mode == GST_SEARCH_MODE_BEFORE) || ret == 0)
3998 return data + (num_elements - 1) * element_size;
4002 /* 3. else binary search */
4004 m = left + (right - left) / 2;
4006 ret = search_func (data + m * element_size, search_data, user_data);
4009 return data + m * element_size;
4010 } else if (ret < 0) {
4016 /* No exact match found */
4018 if (mode == GST_SEARCH_MODE_EXACT) {
4020 } else if (mode == GST_SEARCH_MODE_AFTER) {
4022 return (m < num_elements) ? data + (m + 1) * element_size : NULL;
4024 return data + m * element_size;
4027 return data + m * element_size;
4029 return (m > 0) ? data + (m - 1) * element_size : NULL;
4035 /* Finds the greatest common divisor.
4036 * Returns 1 if none other found.
4037 * This is Euclid's algorithm. */
4040 * gst_util_greatest_common_divisor:
4041 * @a: First value as #gint
4042 * @b: Second value as #gint
4044 * Calculates the greatest common divisor of @a
4047 * Returns: Greatest common divisor of @a and @b
4052 gst_util_greatest_common_divisor (gint a, gint b)
4065 * gst_util_fraction_to_double:
4066 * @src_n: Fraction numerator as #gint
4067 * @src_d: Fraction denominator #gint
4068 * @dest: (out): pointer to a #gdouble for the result
4070 * Transforms a fraction to a #gdouble.
4075 gst_util_fraction_to_double (gint src_n, gint src_d, gdouble * dest)
4077 g_return_if_fail (dest != NULL);
4078 g_return_if_fail (src_d != 0);
4080 *dest = ((gdouble) src_n) / ((gdouble) src_d);
4083 #define MAX_TERMS 30
4084 #define MIN_DIVISOR 1.0e-10
4085 #define MAX_ERROR 1.0e-20
4087 /* use continued fractions to transform a double into a fraction,
4088 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
4089 * This algorithm takes care of overflows.
4093 * gst_util_double_to_fraction:
4094 * @src: #gdouble to transform
4095 * @dest_n: (out): pointer to a #gint to hold the result numerator
4096 * @dest_d: (out): pointer to a #gint to hold the result denominator
4098 * Transforms a #gdouble to a fraction and simplifies
4104 gst_util_double_to_fraction (gdouble src, gint * dest_n, gint * dest_d)
4107 gdouble V, F; /* double being converted */
4108 gint N, D; /* will contain the result */
4109 gint A; /* current term in continued fraction */
4110 gint64 N1, D1; /* numerator, denominator of last approx */
4111 gint64 N2, D2; /* numerator, denominator of previous approx */
4114 gboolean negative = FALSE;
4116 g_return_if_fail (dest_n != NULL);
4117 g_return_if_fail (dest_d != NULL);
4119 /* initialize fraction being converted */
4127 /* initialize fractions with 1/0, 0/1 */
4135 for (i = 0; i < MAX_TERMS; i++) {
4137 A = (gint) F; /* no floor() needed, F is always >= 0 */
4138 /* get new divisor */
4141 /* calculate new fraction in temp */
4145 /* guard against overflow */
4146 if (N2 > G_MAXINT || D2 > G_MAXINT) {
4153 /* save last two fractions */
4159 /* quit if dividing by zero or close enough to target */
4160 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
4164 /* Take reciprocal */
4167 /* fix for overflow */
4172 /* fix for negative */
4177 gcd = gst_util_greatest_common_divisor (N, D);
4189 * gst_util_fraction_multiply:
4190 * @a_n: Numerator of first value
4191 * @a_d: Denominator of first value
4192 * @b_n: Numerator of second value
4193 * @b_d: Denominator of second value
4194 * @res_n: (out): Pointer to #gint to hold the result numerator
4195 * @res_d: (out): Pointer to #gint to hold the result denominator
4197 * Multiplies the fractions @a_n/@a_d and @b_n/@b_d and stores
4198 * the result in @res_n and @res_d.
4200 * Returns: %FALSE on overflow, %TRUE otherwise.
4205 gst_util_fraction_multiply (gint a_n, gint a_d, gint b_n, gint b_d,
4206 gint * res_n, gint * res_d)
4210 g_return_val_if_fail (res_n != NULL, FALSE);
4211 g_return_val_if_fail (res_d != NULL, FALSE);
4212 g_return_val_if_fail (a_d != 0, FALSE);
4213 g_return_val_if_fail (b_d != 0, FALSE);
4215 gcd = gst_util_greatest_common_divisor (a_n, a_d);
4219 gcd = gst_util_greatest_common_divisor (b_n, b_d);
4223 gcd = gst_util_greatest_common_divisor (a_n, b_d);
4227 gcd = gst_util_greatest_common_divisor (a_d, b_n);
4231 /* This would result in overflow */
4232 if (a_n != 0 && G_MAXINT / ABS (a_n) < ABS (b_n))
4234 if (G_MAXINT / ABS (a_d) < ABS (b_d))
4240 gcd = gst_util_greatest_common_divisor (*res_n, *res_d);
4248 * gst_util_fraction_add:
4249 * @a_n: Numerator of first value
4250 * @a_d: Denominator of first value
4251 * @b_n: Numerator of second value
4252 * @b_d: Denominator of second value
4253 * @res_n: (out): Pointer to #gint to hold the result numerator
4254 * @res_d: (out): Pointer to #gint to hold the result denominator
4256 * Adds the fractions @a_n/@a_d and @b_n/@b_d and stores
4257 * the result in @res_n and @res_d.
4259 * Returns: %FALSE on overflow, %TRUE otherwise.
4264 gst_util_fraction_add (gint a_n, gint a_d, gint b_n, gint b_d, gint * res_n,
4269 g_return_val_if_fail (res_n != NULL, FALSE);
4270 g_return_val_if_fail (res_d != NULL, FALSE);
4271 g_return_val_if_fail (a_d != 0, FALSE);
4272 g_return_val_if_fail (b_d != 0, FALSE);
4274 gcd = gst_util_greatest_common_divisor (a_n, a_d);
4278 gcd = gst_util_greatest_common_divisor (b_n, b_d);
4293 /* This would result in overflow */
4294 if (G_MAXINT / ABS (a_n) < ABS (b_n))
4296 if (G_MAXINT / ABS (a_d) < ABS (b_d))
4298 if (G_MAXINT / ABS (a_d) < ABS (b_d))
4301 *res_n = (a_n * b_d) + (a_d * b_n);
4304 gcd = gst_util_greatest_common_divisor (*res_n, *res_d);
4317 * gst_util_fraction_compare:
4318 * @a_n: Numerator of first value
4319 * @a_d: Denominator of first value
4320 * @b_n: Numerator of second value
4321 * @b_d: Denominator of second value
4323 * Compares the fractions @a_n/@a_d and @b_n/@b_d and returns
4324 * -1 if a < b, 0 if a = b and 1 if a > b.
4326 * Returns: -1 if a < b; 0 if a = b; 1 if a > b.
4331 gst_util_fraction_compare (gint a_n, gint a_d, gint b_n, gint b_d)
4337 g_return_val_if_fail (a_d != 0 && b_d != 0, 0);
4340 gcd = gst_util_greatest_common_divisor (a_n, a_d);
4344 gcd = gst_util_greatest_common_divisor (b_n, b_d);
4348 /* fractions are reduced when set, so we can quickly see if they're equal */
4349 if (a_n == b_n && a_d == b_d)
4352 /* extend to 64 bits */
4353 new_num_1 = ((gint64) a_n) * b_d;
4354 new_num_2 = ((gint64) b_n) * a_d;
4355 if (new_num_1 < new_num_2)
4357 if (new_num_1 > new_num_2)
4360 /* Should not happen because a_d and b_d are not 0 */
4361 g_return_val_if_reached (0);