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 multipy/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)
762 caps = gst_pad_get_current_caps (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);
775 gst_caps_unref (caps);
780 * gst_print_element_args:
781 * @buf: the buffer to print the args in
782 * @indent: initial indentation
783 * @element: (transfer none): the element to print the args of
785 * Print the element argument in a human readable format in the given
789 gst_print_element_args (GString * buf, gint indent, GstElement * element)
792 GValue value = { 0, }; /* the important thing is that value.type = 0 */
794 GParamSpec *spec, **specs, **walk;
796 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
799 for (walk = specs; *walk; walk++) {
801 if (width < strlen (spec->name))
802 width = strlen (spec->name);
805 for (walk = specs; *walk; walk++) {
808 if (spec->flags & G_PARAM_READABLE) {
809 g_value_init (&value, spec->value_type);
810 g_object_get_property (G_OBJECT (element), spec->name, &value);
811 str = g_strdup_value_contents (&value);
812 g_value_unset (&value);
814 str = g_strdup ("Parameter not readable.");
817 string_append_indent (buf, indent);
818 g_string_append (buf, spec->name);
819 string_append_indent (buf, 2 + width - strlen (spec->name));
820 g_string_append (buf, str);
821 g_string_append_c (buf, '\n');
830 * gst_element_create_all_pads:
831 * @element: (transfer none): a #GstElement to create pads for
833 * Creates a pad for each pad template that is always available.
834 * This function is only useful during object intialization of
835 * subclasses of #GstElement.
838 gst_element_create_all_pads (GstElement * element)
842 /* FIXME: lock element */
845 gst_element_class_get_pad_template_list (GST_ELEMENT_CLASS
846 (G_OBJECT_GET_CLASS (element)));
849 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
851 if (padtempl->presence == GST_PAD_ALWAYS) {
854 pad = gst_pad_new_from_template (padtempl, padtempl->name_template);
856 gst_element_add_pad (element, pad);
858 padlist = padlist->next;
863 * gst_element_get_compatible_pad_template:
864 * @element: (transfer none): a #GstElement to get a compatible pad template for
865 * @compattempl: (transfer none): the #GstPadTemplate to find a compatible
868 * Retrieves a pad template from @element that is compatible with @compattempl.
869 * Pads from compatible templates can be linked together.
871 * Returns: (transfer none): a compatible #GstPadTemplate, or NULL if none
872 * was found. No unreferencing is necessary.
875 gst_element_get_compatible_pad_template (GstElement * element,
876 GstPadTemplate * compattempl)
878 GstPadTemplate *newtempl = NULL;
880 GstElementClass *class;
883 g_return_val_if_fail (element != NULL, NULL);
884 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
885 g_return_val_if_fail (compattempl != NULL, NULL);
887 class = GST_ELEMENT_GET_CLASS (element);
889 padlist = gst_element_class_get_pad_template_list (class);
891 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
892 "Looking for a suitable pad template in %s out of %d templates...",
893 GST_ELEMENT_NAME (element), g_list_length (padlist));
896 GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
900 * Check direction (must be opposite)
903 GST_CAT_LOG (GST_CAT_CAPS,
904 "checking pad template %s", padtempl->name_template);
905 if (padtempl->direction != compattempl->direction) {
906 GST_CAT_DEBUG (GST_CAT_CAPS,
907 "compatible direction: found %s pad template \"%s\"",
908 padtempl->direction == GST_PAD_SRC ? "src" : "sink",
909 padtempl->name_template);
911 GST_CAT_DEBUG (GST_CAT_CAPS,
912 "intersecting %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (compattempl));
913 GST_CAT_DEBUG (GST_CAT_CAPS,
914 "..and %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (padtempl));
916 compatible = gst_caps_can_intersect (GST_PAD_TEMPLATE_CAPS (compattempl),
917 GST_PAD_TEMPLATE_CAPS (padtempl));
919 GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible",
920 (compatible ? "" : "not "));
928 padlist = g_list_next (padlist);
931 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
932 "Returning new pad template %p", newtempl);
934 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "No compatible pad template found");
940 * gst_element_get_pad_from_template:
941 * @element: (transfer none): a #GstElement.
942 * @templ: (transfer none): a #GstPadTemplate belonging to @element.
944 * Gets a pad from @element described by @templ. If the presence of @templ is
945 * #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
948 * Returns: (transfer full): the #GstPad, or NULL if one could not be found
952 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
955 GstPadPresence presence;
957 /* If this function is ever exported, we need check the validity of `element'
958 * and `templ', and to make sure the template actually belongs to the
961 presence = GST_PAD_TEMPLATE_PRESENCE (templ);
965 case GST_PAD_SOMETIMES:
966 ret = gst_element_get_static_pad (element, templ->name_template);
967 if (!ret && presence == GST_PAD_ALWAYS)
969 ("Element %s has an ALWAYS template %s, but no pad of the same name",
970 GST_OBJECT_NAME (element), templ->name_template);
973 case GST_PAD_REQUEST:
974 ret = gst_element_request_pad (element, templ, NULL, NULL);
982 * gst_element_request_compatible_pad:
983 * @element: a #GstElement.
984 * @templ: the #GstPadTemplate to which the new pad should be able to link.
986 * Requests a pad from @element. The returned pad should be unlinked and
987 * compatible with @templ. Might return an existing pad, or request a new one.
989 * Returns: a #GstPad, or %NULL if one could not be found or created.
992 gst_element_request_compatible_pad (GstElement * element,
993 GstPadTemplate * templ)
995 GstPadTemplate *templ_new;
998 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
999 g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
1001 /* FIXME: should really loop through the templates, testing each for
1002 * compatibility and pad availability. */
1003 templ_new = gst_element_get_compatible_pad_template (element, templ);
1005 pad = gst_element_get_pad_from_template (element, templ_new);
1007 /* This can happen for non-request pads. No need to unref. */
1008 if (pad && GST_PAD_PEER (pad))
1015 * Checks if the source pad and the sink pad can be linked.
1016 * Both @srcpad and @sinkpad must be unlinked and have a parent.
1019 gst_pad_check_link (GstPad * srcpad, GstPad * sinkpad)
1021 /* FIXME This function is gross. It's almost a direct copy of
1022 * gst_pad_link_filtered(). Any decent programmer would attempt
1023 * to merge the two functions, which I will do some day. --ds
1026 /* generic checks */
1027 g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1028 g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1030 GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1031 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1033 /* FIXME: shouldn't we convert this to g_return_val_if_fail? */
1034 if (GST_PAD_PEER (srcpad) != NULL) {
1035 GST_CAT_INFO (GST_CAT_PADS, "Source pad %s:%s has a peer, failed",
1036 GST_DEBUG_PAD_NAME (srcpad));
1039 if (GST_PAD_PEER (sinkpad) != NULL) {
1040 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has a peer, failed",
1041 GST_DEBUG_PAD_NAME (sinkpad));
1044 if (!GST_PAD_IS_SRC (srcpad)) {
1045 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s is not source pad, failed",
1046 GST_DEBUG_PAD_NAME (srcpad));
1049 if (!GST_PAD_IS_SINK (sinkpad)) {
1050 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s is not sink pad, failed",
1051 GST_DEBUG_PAD_NAME (sinkpad));
1054 if (GST_PAD_PARENT (srcpad) == NULL) {
1055 GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s has no parent, failed",
1056 GST_DEBUG_PAD_NAME (srcpad));
1059 if (GST_PAD_PARENT (sinkpad) == NULL) {
1060 GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has no parent, failed",
1061 GST_DEBUG_PAD_NAME (srcpad));
1069 * gst_element_get_compatible_pad:
1070 * @element: (transfer none): a #GstElement in which the pad should be found.
1071 * @pad: (transfer none): the #GstPad to find a compatible one for.
1072 * @caps: the #GstCaps to use as a filter.
1074 * Looks for an unlinked pad to which the given pad can link. It is not
1075 * guaranteed that linking the pads will work, though it should work in most
1078 * This function will first attempt to find a compatible unlinked ALWAYS pad,
1079 * and if none can be found, it will request a compatible REQUEST pad by looking
1080 * at the templates of @element.
1082 * Returns: (transfer full): the #GstPad to which a link can be made, or %NULL
1083 * if one cannot be found. gst_object_unref() after usage.
1086 gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
1087 const GstCaps * caps)
1090 GstPadTemplate *templ;
1092 GstPad *foundpad = NULL;
1094 GValue padptr = { 0, };
1096 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1097 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1099 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1100 "finding pad in %s compatible with %s:%s",
1101 GST_ELEMENT_NAME (element), GST_DEBUG_PAD_NAME (pad));
1103 g_return_val_if_fail (GST_PAD_PEER (pad) == NULL, NULL);
1107 /* try to get an existing unlinked pad */
1108 if (GST_PAD_IS_SRC (pad)) {
1109 pads = gst_element_iterate_sink_pads (element);
1110 } else if (GST_PAD_IS_SINK (pad)) {
1111 pads = gst_element_iterate_src_pads (element);
1113 pads = gst_element_iterate_pads (element);
1117 switch (gst_iterator_next (pads, &padptr)) {
1118 case GST_ITERATOR_OK:
1125 current = g_value_get_object (&padptr);
1127 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
1128 GST_DEBUG_PAD_NAME (current));
1130 if (GST_PAD_IS_SRC (current)) {
1137 peer = gst_pad_get_peer (current);
1139 if (peer == NULL && gst_pad_check_link (srcpad, sinkpad)) {
1140 GstCaps *temp, *intersection;
1141 gboolean compatible;
1143 /* Now check if the two pads' caps are compatible */
1144 temp = gst_pad_get_caps (pad, NULL);
1146 intersection = gst_caps_intersect (temp, caps);
1147 gst_caps_unref (temp);
1149 intersection = temp;
1152 temp = gst_pad_get_caps (current, NULL);
1153 compatible = gst_caps_can_intersect (temp, intersection);
1154 gst_caps_unref (temp);
1155 gst_caps_unref (intersection);
1158 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1159 "found existing unlinked compatible pad %s:%s",
1160 GST_DEBUG_PAD_NAME (current));
1161 gst_iterator_free (pads);
1163 current = gst_object_ref (current);
1164 g_value_unset (&padptr);
1168 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "incompatible pads");
1171 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1172 "already linked or cannot be linked (peer = %p)", peer);
1174 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unreffing pads");
1176 g_value_reset (&padptr);
1178 gst_object_unref (peer);
1181 case GST_ITERATOR_DONE:
1184 case GST_ITERATOR_RESYNC:
1185 gst_iterator_resync (pads);
1187 case GST_ITERATOR_ERROR:
1188 g_assert_not_reached ();
1192 g_value_unset (&padptr);
1193 gst_iterator_free (pads);
1195 GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
1196 "Could not find a compatible unlinked always pad to link to %s:%s, now checking request pads",
1197 GST_DEBUG_PAD_NAME (pad));
1199 /* try to create a new one */
1200 /* requesting is a little crazy, we need a template. Let's create one */
1201 /* FIXME: why not gst_pad_get_pad_template (pad); */
1202 templcaps = gst_pad_get_caps (pad, NULL);
1203 templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
1204 GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
1205 gst_caps_unref (templcaps);
1207 foundpad = gst_element_request_compatible_pad (element, templ);
1208 gst_object_unref (templ);
1211 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1212 "found existing request pad %s:%s", GST_DEBUG_PAD_NAME (foundpad));
1216 GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element,
1217 "Could not find a compatible pad to link to %s:%s",
1218 GST_DEBUG_PAD_NAME (pad));
1223 * gst_element_state_get_name:
1224 * @state: a #GstState to get the name of.
1226 * Gets a string representing the given state.
1228 * Returns: (transfer none): a string with the name of the state.
1230 G_CONST_RETURN gchar *
1231 gst_element_state_get_name (GstState state)
1234 case GST_STATE_VOID_PENDING:
1235 return "VOID_PENDING";
1236 case GST_STATE_NULL:
1238 case GST_STATE_READY:
1240 case GST_STATE_PLAYING:
1242 case GST_STATE_PAUSED:
1245 /* This is a memory leak */
1246 return g_strdup_printf ("UNKNOWN!(%d)", state);
1251 * gst_element_state_change_return_get_name:
1252 * @state_ret: a #GstStateChangeReturn to get the name of.
1254 * Gets a string representing the given state change result.
1256 * Returns: (transfer none): a string with the name of the state
1261 G_CONST_RETURN gchar *
1262 gst_element_state_change_return_get_name (GstStateChangeReturn state_ret)
1264 switch (state_ret) {
1265 case GST_STATE_CHANGE_FAILURE:
1267 case GST_STATE_CHANGE_SUCCESS:
1269 case GST_STATE_CHANGE_ASYNC:
1271 case GST_STATE_CHANGE_NO_PREROLL:
1272 return "NO PREROLL";
1274 /* This is a memory leak */
1275 return g_strdup_printf ("UNKNOWN!(%d)", state_ret);
1281 gst_element_factory_can_accept_all_caps_in_direction (GstElementFactory *
1282 factory, const GstCaps * caps, GstPadDirection direction)
1286 g_return_val_if_fail (factory != NULL, FALSE);
1287 g_return_val_if_fail (caps != NULL, FALSE);
1289 templates = factory->staticpadtemplates;
1292 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1294 if (template->direction == direction) {
1295 GstCaps *templcaps = gst_static_caps_get (&template->static_caps);
1297 if (gst_caps_is_always_compatible (caps, templcaps)) {
1298 gst_caps_unref (templcaps);
1301 gst_caps_unref (templcaps);
1303 templates = g_list_next (templates);
1310 gst_element_factory_can_accept_any_caps_in_direction (GstElementFactory *
1311 factory, const GstCaps * caps, GstPadDirection direction)
1315 g_return_val_if_fail (factory != NULL, FALSE);
1316 g_return_val_if_fail (caps != NULL, FALSE);
1318 templates = factory->staticpadtemplates;
1321 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1323 if (template->direction == direction) {
1324 GstCaps *templcaps = gst_static_caps_get (&template->static_caps);
1326 if (gst_caps_can_intersect (caps, templcaps)) {
1327 gst_caps_unref (templcaps);
1330 gst_caps_unref (templcaps);
1332 templates = g_list_next (templates);
1339 * gst_element_factory_can_sink_all_caps:
1340 * @factory: factory to query
1341 * @caps: the caps to check
1343 * Checks if the factory can sink all possible capabilities.
1345 * Returns: %TRUE if the caps are fully compatible.
1350 gst_element_factory_can_sink_all_caps (GstElementFactory * factory,
1351 const GstCaps * caps)
1353 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1358 * gst_element_factory_can_src_all_caps:
1359 * @factory: factory to query
1360 * @caps: the caps to check
1362 * Checks if the factory can src all possible capabilities.
1364 * Returns: %TRUE if the caps are fully compatible.
1369 gst_element_factory_can_src_all_caps (GstElementFactory * factory,
1370 const GstCaps * caps)
1372 return gst_element_factory_can_accept_all_caps_in_direction (factory, caps,
1377 * gst_element_factory_can_sink_any_caps:
1378 * @factory: factory to query
1379 * @caps: the caps to check
1381 * Checks if the factory can sink any possible capability.
1383 * Returns: %TRUE if the caps have a common subset.
1388 gst_element_factory_can_sink_any_caps (GstElementFactory * factory,
1389 const GstCaps * caps)
1391 return gst_element_factory_can_accept_any_caps_in_direction (factory, caps,
1396 * gst_element_factory_can_src_any_caps:
1397 * @factory: factory to query
1398 * @caps: the caps to check
1400 * Checks if the factory can src any possible capability.
1402 * Returns: %TRUE if the caps have a common subset.
1407 gst_element_factory_can_src_any_caps (GstElementFactory * factory,
1408 const GstCaps * caps)
1410 return gst_element_factory_can_accept_any_caps_in_direction (factory, caps,
1414 /* if return val is true, *direct_child is a caller-owned ref on the direct
1415 * child of ancestor that is part of object's ancestry */
1417 object_has_ancestor (GstObject * object, GstObject * ancestor,
1418 GstObject ** direct_child)
1420 GstObject *child, *parent;
1423 *direct_child = NULL;
1425 child = gst_object_ref (object);
1426 parent = gst_object_get_parent (object);
1429 if (ancestor == parent) {
1431 *direct_child = child;
1433 gst_object_unref (child);
1434 gst_object_unref (parent);
1438 gst_object_unref (child);
1440 parent = gst_object_get_parent (parent);
1443 gst_object_unref (child);
1448 /* caller owns return */
1450 find_common_root (GstObject * o1, GstObject * o2)
1452 GstObject *top = o1;
1453 GstObject *kid1, *kid2;
1454 GstObject *root = NULL;
1456 while (GST_OBJECT_PARENT (top))
1457 top = GST_OBJECT_PARENT (top);
1459 /* the itsy-bitsy spider... */
1461 if (!object_has_ancestor (o2, top, &kid2))
1464 root = gst_object_ref (top);
1466 if (!object_has_ancestor (o1, kid2, &kid1)) {
1467 gst_object_unref (kid2);
1471 if (!object_has_ancestor (o2, kid1, &kid2)) {
1472 gst_object_unref (kid1);
1479 /* caller does not own return */
1481 ghost_up (GstElement * e, GstPad * pad)
1483 static gint ghost_pad_index = 0;
1488 GstObject *parent = GST_OBJECT_PARENT (e);
1490 name = g_strdup_printf ("ghost%d", ghost_pad_index++);
1491 gpad = gst_ghost_pad_new (name, pad);
1495 gst_element_get_state (e, ¤t, &next, 0);
1497 if (current > GST_STATE_READY || next == GST_STATE_PAUSED)
1498 gst_pad_set_active (gpad, TRUE);
1500 if (!gst_element_add_pad ((GstElement *) parent, gpad)) {
1501 g_warning ("Pad named %s already exists in element %s\n",
1502 GST_OBJECT_NAME (gpad), GST_OBJECT_NAME (parent));
1503 gst_object_unref ((GstObject *) gpad);
1504 GST_STATE_UNLOCK (e);
1507 GST_STATE_UNLOCK (e);
1513 remove_pad (gpointer ppad, gpointer unused)
1517 if (!gst_element_remove_pad ((GstElement *) GST_OBJECT_PARENT (pad), pad))
1518 g_warning ("Couldn't remove pad %s from element %s",
1519 GST_OBJECT_NAME (pad), GST_OBJECT_NAME (GST_OBJECT_PARENT (pad)));
1523 prepare_link_maybe_ghosting (GstPad ** src, GstPad ** sink,
1524 GSList ** pads_created)
1528 GSList *pads_created_local = NULL;
1530 g_assert (pads_created);
1532 e1 = GST_OBJECT_PARENT (*src);
1533 e2 = GST_OBJECT_PARENT (*sink);
1535 if (G_UNLIKELY (e1 == NULL)) {
1536 GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1537 GST_PTR_FORMAT, *src);
1540 if (G_UNLIKELY (e2 == NULL)) {
1541 GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1542 GST_PTR_FORMAT, *sink);
1546 if (GST_OBJECT_PARENT (e1) == GST_OBJECT_PARENT (e2)) {
1547 GST_CAT_INFO (GST_CAT_PADS, "%s and %s in same bin, no need for ghost pads",
1548 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1552 GST_CAT_INFO (GST_CAT_PADS, "%s and %s not in same bin, making ghost pads",
1553 GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1555 /* we need to setup some ghost pads */
1556 root = find_common_root (e1, e2);
1558 g_warning ("Trying to connect elements that don't share a common "
1559 "ancestor: %s and %s", GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2));
1563 while (GST_OBJECT_PARENT (e1) != root) {
1564 *src = ghost_up ((GstElement *) e1, *src);
1567 e1 = GST_OBJECT_PARENT (*src);
1568 pads_created_local = g_slist_prepend (pads_created_local, *src);
1570 while (GST_OBJECT_PARENT (e2) != root) {
1571 *sink = ghost_up ((GstElement *) e2, *sink);
1574 e2 = GST_OBJECT_PARENT (*sink);
1575 pads_created_local = g_slist_prepend (pads_created_local, *sink);
1578 gst_object_unref (root);
1579 *pads_created = g_slist_concat (*pads_created, pads_created_local);
1583 gst_object_unref (root);
1584 g_slist_foreach (pads_created_local, remove_pad, NULL);
1585 g_slist_free (pads_created_local);
1590 pad_link_maybe_ghosting (GstPad * src, GstPad * sink, GstPadLinkCheck flags)
1592 GSList *pads_created = NULL;
1595 if (!prepare_link_maybe_ghosting (&src, &sink, &pads_created)) {
1598 ret = (gst_pad_link_full (src, sink, flags) == GST_PAD_LINK_OK);
1602 g_slist_foreach (pads_created, remove_pad, NULL);
1604 g_slist_free (pads_created);
1610 * gst_element_link_pads_full:
1611 * @src: a #GstElement containing the source pad.
1612 * @srcpadname: (allow-none): the name of the #GstPad in source element
1613 * or NULL for any pad.
1614 * @dest: (transfer none): the #GstElement containing the destination pad.
1615 * @destpadname: (allow-none): the name of the #GstPad in destination element,
1616 * or NULL for any pad.
1617 * @flags: the #GstPadLinkCheck to be performed when linking pads.
1619 * Links the two named pads of the source and destination elements.
1620 * Side effect is that if one of the pads has no parent, it becomes a
1621 * child of the parent of the other element. If they have different
1622 * parents, the link fails.
1624 * Calling gst_element_link_pads_full() with @flags == %GST_PAD_LINK_CHECK_DEFAULT
1625 * is the same as calling gst_element_link_pads() and the recommended way of
1626 * linking pads with safety checks applied.
1628 * This is a convenience function for gst_pad_link_full().
1630 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1635 gst_element_link_pads_full (GstElement * src, const gchar * srcpadname,
1636 GstElement * dest, const gchar * destpadname, GstPadLinkCheck flags)
1638 const GList *srcpads, *destpads, *srctempls, *desttempls, *l;
1639 GstPad *srcpad, *destpad;
1640 GstPadTemplate *srctempl, *desttempl;
1641 GstElementClass *srcclass, *destclass;
1644 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1645 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1647 GST_CAT_INFO (GST_CAT_ELEMENT_PADS,
1648 "trying to link element %s:%s to element %s:%s", GST_ELEMENT_NAME (src),
1649 srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
1650 destpadname ? destpadname : "(any)");
1654 /* name specified, look it up */
1655 if (!(srcpad = gst_element_get_static_pad (src, srcpadname)))
1656 srcpad = gst_element_get_request_pad (src, srcpadname);
1658 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1659 GST_ELEMENT_NAME (src), srcpadname);
1662 if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
1663 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
1664 GST_DEBUG_PAD_NAME (srcpad));
1665 gst_object_unref (srcpad);
1668 if (GST_PAD_PEER (srcpad) != NULL) {
1669 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1670 "pad %s:%s is already linked to %s:%s", GST_DEBUG_PAD_NAME (srcpad),
1671 GST_DEBUG_PAD_NAME (GST_PAD_PEER (srcpad)));
1672 gst_object_unref (srcpad);
1678 /* no name given, get the first available pad */
1679 GST_OBJECT_LOCK (src);
1680 srcpads = GST_ELEMENT_PADS (src);
1681 srcpad = srcpads ? GST_PAD_CAST (srcpads->data) : NULL;
1683 gst_object_ref (srcpad);
1684 GST_OBJECT_UNLOCK (src);
1687 /* get a destination pad */
1689 /* name specified, look it up */
1690 if (!(destpad = gst_element_get_static_pad (dest, destpadname)))
1691 destpad = gst_element_get_request_pad (dest, destpadname);
1693 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1694 GST_ELEMENT_NAME (dest), destpadname);
1697 if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
1698 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
1699 GST_DEBUG_PAD_NAME (destpad));
1700 gst_object_unref (destpad);
1703 if (GST_PAD_PEER (destpad) != NULL) {
1704 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1705 "pad %s:%s is already linked to %s:%s",
1706 GST_DEBUG_PAD_NAME (destpad),
1707 GST_DEBUG_PAD_NAME (GST_PAD_PEER (destpad)));
1708 gst_object_unref (destpad);
1714 /* no name given, get the first available pad */
1715 GST_OBJECT_LOCK (dest);
1716 destpads = GST_ELEMENT_PADS (dest);
1717 destpad = destpads ? GST_PAD_CAST (destpads->data) : NULL;
1719 gst_object_ref (destpad);
1720 GST_OBJECT_UNLOCK (dest);
1723 if (srcpadname && destpadname) {
1726 /* two explicitly specified pads */
1727 result = pad_link_maybe_ghosting (srcpad, destpad, flags);
1729 gst_object_unref (srcpad);
1730 gst_object_unref (destpad);
1736 /* loop through the allowed pads in the source, trying to find a
1737 * compatible destination pad */
1738 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1739 "looping through allowed src and dest pads");
1741 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying src pad %s:%s",
1742 GST_DEBUG_PAD_NAME (srcpad));
1743 if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
1744 (GST_PAD_PEER (srcpad) == NULL)) {
1749 gst_object_ref (temp);
1751 temp = gst_element_get_compatible_pad (dest, srcpad, NULL);
1754 if (temp && pad_link_maybe_ghosting (srcpad, temp, flags)) {
1755 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1756 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (temp));
1758 gst_object_unref (destpad);
1759 gst_object_unref (srcpad);
1760 gst_object_unref (temp);
1765 gst_object_unref (temp);
1768 /* find a better way for this mess */
1770 srcpads = g_list_next (srcpads);
1772 gst_object_unref (srcpad);
1773 srcpad = GST_PAD_CAST (srcpads->data);
1774 gst_object_ref (srcpad);
1780 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
1781 GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
1783 gst_object_unref (destpad);
1787 gst_object_unref (srcpad);
1791 /* loop through the existing pads in the destination */
1793 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying dest pad %s:%s",
1794 GST_DEBUG_PAD_NAME (destpad));
1795 if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
1796 (GST_PAD_PEER (destpad) == NULL)) {
1797 GstPad *temp = gst_element_get_compatible_pad (src, destpad, NULL);
1799 if (temp && pad_link_maybe_ghosting (temp, destpad, flags)) {
1800 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1801 GST_DEBUG_PAD_NAME (temp), GST_DEBUG_PAD_NAME (destpad));
1802 gst_object_unref (temp);
1803 gst_object_unref (destpad);
1807 gst_object_unref (temp);
1811 destpads = g_list_next (destpads);
1813 gst_object_unref (destpad);
1814 destpad = GST_PAD_CAST (destpads->data);
1815 gst_object_ref (destpad);
1822 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
1823 GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
1824 gst_object_unref (destpad);
1828 gst_object_unref (destpad);
1832 srcclass = GST_ELEMENT_GET_CLASS (src);
1833 destclass = GST_ELEMENT_GET_CLASS (dest);
1835 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1836 "we might have request pads on both sides, checking...");
1837 srctempls = gst_element_class_get_pad_template_list (srcclass);
1838 desttempls = gst_element_class_get_pad_template_list (destclass);
1840 if (srctempls && desttempls) {
1842 srctempl = (GstPadTemplate *) srctempls->data;
1843 if (srctempl->presence == GST_PAD_REQUEST) {
1844 for (l = desttempls; l; l = l->next) {
1845 desttempl = (GstPadTemplate *) l->data;
1846 if (desttempl->presence == GST_PAD_REQUEST &&
1847 desttempl->direction != srctempl->direction) {
1848 GstCaps *srccaps, *destcaps;
1850 srccaps = gst_pad_template_get_caps (srctempl);
1851 destcaps = gst_pad_template_get_caps (desttempl);
1852 if (gst_caps_is_always_compatible (srccaps, destcaps)) {
1854 gst_element_request_pad (src, srctempl,
1855 srctempl->name_template, NULL);
1857 gst_element_request_pad (dest, desttempl,
1858 desttempl->name_template, NULL);
1859 if (srcpad && destpad
1860 && pad_link_maybe_ghosting (srcpad, destpad, flags)) {
1861 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1862 "linked pad %s:%s to pad %s:%s",
1863 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
1864 gst_object_unref (srcpad);
1865 gst_object_unref (destpad);
1866 gst_caps_unref (srccaps);
1867 gst_caps_unref (destcaps);
1870 /* it failed, so we release the request pads */
1872 gst_element_release_request_pad (src, srcpad);
1874 gst_element_release_request_pad (dest, destpad);
1876 gst_caps_unref (srccaps);
1877 gst_caps_unref (destcaps);
1881 srctempls = srctempls->next;
1885 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s",
1886 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1891 * gst_element_link_pads:
1892 * @src: a #GstElement containing the source pad.
1893 * @srcpadname: (allow-none): the name of the #GstPad in source element
1894 * or NULL for any pad.
1895 * @dest: (transfer none): the #GstElement containing the destination pad.
1896 * @destpadname: (allow-none): the name of the #GstPad in destination element,
1897 * or NULL for any pad.
1899 * Links the two named pads of the source and destination elements.
1900 * Side effect is that if one of the pads has no parent, it becomes a
1901 * child of the parent of the other element. If they have different
1902 * parents, the link fails.
1904 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1907 gst_element_link_pads (GstElement * src, const gchar * srcpadname,
1908 GstElement * dest, const gchar * destpadname)
1910 return gst_element_link_pads_full (src, srcpadname, dest, destpadname,
1911 GST_PAD_LINK_CHECK_DEFAULT);
1915 * gst_element_link_pads_filtered:
1916 * @src: a #GstElement containing the source pad.
1917 * @srcpadname: (allow-none): the name of the #GstPad in source element
1918 * or NULL for any pad.
1919 * @dest: (transfer none): the #GstElement containing the destination pad.
1920 * @destpadname: (allow-none): the name of the #GstPad in destination element
1921 * or NULL for any pad.
1922 * @filter: (transfer none) (allow-none): the #GstCaps to filter the link,
1923 * or #NULL for no filter.
1925 * Links the two named pads of the source and destination elements. Side effect
1926 * is that if one of the pads has no parent, it becomes a child of the parent of
1927 * the other element. If they have different parents, the link fails. If @caps
1928 * is not #NULL, makes sure that the caps of the link is a subset of @caps.
1930 * Returns: TRUE if the pads could be linked, FALSE otherwise.
1933 gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
1934 GstElement * dest, const gchar * destpadname, GstCaps * filter)
1937 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1938 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1939 g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), FALSE);
1942 GstElement *capsfilter;
1944 GstState state, pending;
1947 capsfilter = gst_element_factory_make ("capsfilter", NULL);
1949 GST_ERROR ("Could not make a capsfilter");
1953 parent = gst_object_get_parent (GST_OBJECT (src));
1954 g_return_val_if_fail (GST_IS_BIN (parent), FALSE);
1956 gst_element_get_state (GST_ELEMENT_CAST (parent), &state, &pending, 0);
1958 if (!gst_bin_add (GST_BIN (parent), capsfilter)) {
1959 GST_ERROR ("Could not add capsfilter");
1960 gst_object_unref (capsfilter);
1961 gst_object_unref (parent);
1965 if (pending != GST_STATE_VOID_PENDING)
1968 gst_element_set_state (capsfilter, state);
1970 gst_object_unref (parent);
1972 g_object_set (capsfilter, "caps", filter, NULL);
1974 lr1 = gst_element_link_pads (src, srcpadname, capsfilter, "sink");
1975 lr2 = gst_element_link_pads (capsfilter, "src", dest, destpadname);
1980 GST_INFO ("Could not link pads: %s:%s - capsfilter:sink",
1981 GST_ELEMENT_NAME (src), srcpadname);
1983 GST_INFO ("Could not link pads: capsfilter:src - %s:%s",
1984 GST_ELEMENT_NAME (dest), destpadname);
1986 gst_element_set_state (capsfilter, GST_STATE_NULL);
1987 /* this will unlink and unref as appropriate */
1988 gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (capsfilter)), capsfilter);
1992 if (gst_element_link_pads (src, srcpadname, dest, destpadname)) {
1995 GST_INFO ("Could not link pads: %s:%s - %s:%s", GST_ELEMENT_NAME (src),
1996 srcpadname, GST_ELEMENT_NAME (dest), destpadname);
2004 * @src: (transfer none): a #GstElement containing the source pad.
2005 * @dest: (transfer none): the #GstElement containing the destination pad.
2007 * Links @src to @dest. The link must be from source to
2008 * destination; the other direction will not be tried. The function looks for
2009 * existing pads that aren't linked yet. It will request new pads if necessary.
2010 * Such pads need to be released manualy when unlinking.
2011 * If multiple links are possible, only one is established.
2013 * Make sure you have added your elements to a bin or pipeline with
2014 * gst_bin_add() before trying to link them.
2016 * Returns: TRUE if the elements could be linked, FALSE otherwise.
2019 gst_element_link (GstElement * src, GstElement * dest)
2021 return gst_element_link_pads (src, NULL, dest, NULL);
2025 * gst_element_link_many:
2026 * @element_1: (transfer none): the first #GstElement in the link chain.
2027 * @element_2: (transfer none): the second #GstElement in the link chain.
2028 * @...: the NULL-terminated list of elements to link in order.
2030 * Chain together a series of elements. Uses gst_element_link().
2031 * Make sure you have added your elements to a bin or pipeline with
2032 * gst_bin_add() before trying to link them.
2034 * Returns: TRUE on success, FALSE otherwise.
2037 gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
2039 gboolean res = TRUE;
2042 g_return_val_if_fail (GST_IS_ELEMENT (element_1), FALSE);
2043 g_return_val_if_fail (GST_IS_ELEMENT (element_2), FALSE);
2045 va_start (args, element_2);
2048 if (!gst_element_link (element_1, element_2)) {
2053 element_1 = element_2;
2054 element_2 = va_arg (args, GstElement *);
2063 * gst_element_link_filtered:
2064 * @src: a #GstElement containing the source pad.
2065 * @dest: (transfer none): the #GstElement containing the destination pad.
2066 * @filter: (transfer none) (allow-none): the #GstCaps to filter the link,
2067 * or #NULL for no filter.
2069 * Links @src to @dest using the given caps as filtercaps.
2070 * The link must be from source to
2071 * destination; the other direction will not be tried. The function looks for
2072 * existing pads that aren't linked yet. It will request new pads if necessary.
2073 * If multiple links are possible, only one is established.
2075 * Make sure you have added your elements to a bin or pipeline with
2076 * gst_bin_add() before trying to link them.
2078 * Returns: TRUE if the pads could be linked, FALSE otherwise.
2081 gst_element_link_filtered (GstElement * src, GstElement * dest,
2084 return gst_element_link_pads_filtered (src, NULL, dest, NULL, filter);
2088 * gst_element_unlink_pads:
2089 * @src: a (transfer none): #GstElement containing the source pad.
2090 * @srcpadname: the name of the #GstPad in source element.
2091 * @dest: (transfer none): a #GstElement containing the destination pad.
2092 * @destpadname: the name of the #GstPad in destination element.
2094 * Unlinks the two named pads of the source and destination elements.
2096 * This is a convenience function for gst_pad_unlink().
2099 gst_element_unlink_pads (GstElement * src, const gchar * srcpadname,
2100 GstElement * dest, const gchar * destpadname)
2102 GstPad *srcpad, *destpad;
2103 gboolean srcrequest, destrequest;
2105 srcrequest = destrequest = FALSE;
2107 g_return_if_fail (src != NULL);
2108 g_return_if_fail (GST_IS_ELEMENT (src));
2109 g_return_if_fail (srcpadname != NULL);
2110 g_return_if_fail (dest != NULL);
2111 g_return_if_fail (GST_IS_ELEMENT (dest));
2112 g_return_if_fail (destpadname != NULL);
2114 /* obtain the pads requested */
2115 if (!(srcpad = gst_element_get_static_pad (src, srcpadname)))
2116 if ((srcpad = gst_element_get_request_pad (src, srcpadname)))
2118 if (srcpad == NULL) {
2119 GST_WARNING_OBJECT (src, "source element has no pad \"%s\"", srcpadname);
2122 if (!(destpad = gst_element_get_static_pad (dest, destpadname)))
2123 if ((destpad = gst_element_get_request_pad (dest, destpadname)))
2125 if (destpad == NULL) {
2126 GST_WARNING_OBJECT (dest, "destination element has no pad \"%s\"",
2131 /* we're satisified they can be unlinked, let's do it */
2132 gst_pad_unlink (srcpad, destpad);
2135 gst_element_release_request_pad (dest, destpad);
2136 gst_object_unref (destpad);
2140 gst_element_release_request_pad (src, srcpad);
2141 gst_object_unref (srcpad);
2145 * gst_element_unlink_many:
2146 * @element_1: (transfer none): the first #GstElement in the link chain.
2147 * @element_2: (transfer none): the second #GstElement in the link chain.
2148 * @...: the NULL-terminated list of elements to unlink in order.
2150 * Unlinks a series of elements. Uses gst_element_unlink().
2153 gst_element_unlink_many (GstElement * element_1, GstElement * element_2, ...)
2157 g_return_if_fail (element_1 != NULL && element_2 != NULL);
2158 g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
2160 va_start (args, element_2);
2163 gst_element_unlink (element_1, element_2);
2165 element_1 = element_2;
2166 element_2 = va_arg (args, GstElement *);
2173 * gst_element_unlink:
2174 * @src: (transfer none): the source #GstElement to unlink.
2175 * @dest: (transfer none): the sink #GstElement to unlink.
2177 * Unlinks all source pads of the source element with all sink pads
2178 * of the sink element to which they are linked.
2180 * If the link has been made using gst_element_link(), it could have created an
2181 * requestpad, which has to be released using gst_element_release_request_pad().
2184 gst_element_unlink (GstElement * src, GstElement * dest)
2187 gboolean done = FALSE;
2188 GValue data = { 0, };
2190 g_return_if_fail (GST_IS_ELEMENT (src));
2191 g_return_if_fail (GST_IS_ELEMENT (dest));
2193 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unlinking \"%s\" and \"%s\"",
2194 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
2196 pads = gst_element_iterate_pads (src);
2198 switch (gst_iterator_next (pads, &data)) {
2199 case GST_ITERATOR_OK:
2201 GstPad *pad = g_value_get_object (&data);
2203 if (GST_PAD_IS_SRC (pad)) {
2204 GstPad *peerpad = gst_pad_get_peer (pad);
2206 /* see if the pad is linked and is really a pad of dest */
2208 GstElement *peerelem;
2210 peerelem = gst_pad_get_parent_element (peerpad);
2212 if (peerelem == dest) {
2213 gst_pad_unlink (pad, peerpad);
2216 gst_object_unref (peerelem);
2218 gst_object_unref (peerpad);
2221 g_value_reset (&data);
2224 case GST_ITERATOR_RESYNC:
2225 gst_iterator_resync (pads);
2227 case GST_ITERATOR_DONE:
2231 g_assert_not_reached ();
2235 g_value_unset (&data);
2236 gst_iterator_free (pads);
2240 * gst_element_query_position:
2241 * @element: a #GstElement to invoke the position query on.
2242 * @format: (inout): a pointer to the #GstFormat asked for.
2243 * On return contains the #GstFormat used.
2244 * @cur: (out) (allow-none): a location in which to store the current
2245 * position, or NULL.
2247 * Queries an element for the stream position. If one repeatedly calls this
2248 * function one can also create and reuse it in gst_element_query().
2250 * Returns: TRUE if the query could be performed.
2253 gst_element_query_position (GstElement * element, GstFormat * format,
2259 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2260 g_return_val_if_fail (format != NULL, FALSE);
2262 query = gst_query_new_position (*format);
2263 ret = gst_element_query (element, query);
2266 gst_query_parse_position (query, format, cur);
2268 gst_query_unref (query);
2274 * gst_element_query_duration:
2275 * @element: a #GstElement to invoke the duration query on.
2276 * @format: (inout): a pointer to the #GstFormat asked for.
2277 * On return contains the #GstFormat used.
2278 * @duration: (out): A location in which to store the total duration, or NULL.
2280 * Queries an element for the total stream duration.
2282 * Returns: TRUE if the query could be performed.
2285 gst_element_query_duration (GstElement * element, GstFormat * format,
2291 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2292 g_return_val_if_fail (format != NULL, FALSE);
2294 query = gst_query_new_duration (*format);
2295 ret = gst_element_query (element, query);
2298 gst_query_parse_duration (query, format, duration);
2300 gst_query_unref (query);
2306 * gst_element_query_convert:
2307 * @element: a #GstElement to invoke the convert query on.
2308 * @src_format: (inout): a #GstFormat to convert from.
2309 * @src_val: a value to convert.
2310 * @dest_format: (inout): a pointer to the #GstFormat to convert to.
2311 * @dest_val: (out): a pointer to the result.
2313 * Queries an element to convert @src_val in @src_format to @dest_format.
2315 * Returns: TRUE if the query could be performed.
2318 gst_element_query_convert (GstElement * element, GstFormat src_format,
2319 gint64 src_val, GstFormat * dest_format, gint64 * dest_val)
2324 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2325 g_return_val_if_fail (dest_format != NULL, FALSE);
2326 g_return_val_if_fail (dest_val != NULL, FALSE);
2328 if (*dest_format == src_format || src_val == -1) {
2329 *dest_val = src_val;
2333 query = gst_query_new_convert (src_format, src_val, *dest_format);
2334 ret = gst_element_query (element, query);
2337 gst_query_parse_convert (query, NULL, NULL, dest_format, dest_val);
2339 gst_query_unref (query);
2345 * gst_element_seek_simple
2346 * @element: a #GstElement to seek on
2347 * @format: a #GstFormat to execute the seek in, such as #GST_FORMAT_TIME
2348 * @seek_flags: seek options; playback applications will usually want to use
2349 * GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT here
2350 * @seek_pos: position to seek to (relative to the start); if you are doing
2351 * a seek in #GST_FORMAT_TIME this value is in nanoseconds -
2352 * multiply with #GST_SECOND to convert seconds to nanoseconds or
2353 * with #GST_MSECOND to convert milliseconds to nanoseconds.
2355 * Simple API to perform a seek on the given element, meaning it just seeks
2356 * to the given position relative to the start of the stream. For more complex
2357 * operations like segment seeks (e.g. for looping) or changing the playback
2358 * rate or seeking relative to the last configured playback segment you should
2359 * use gst_element_seek().
2361 * In a completely prerolled PAUSED or PLAYING pipeline, seeking is always
2362 * guaranteed to return %TRUE on a seekable media type or %FALSE when the media
2363 * type is certainly not seekable (such as a live stream).
2365 * Some elements allow for seeking in the READY state, in this
2366 * case they will store the seek event and execute it when they are put to
2367 * PAUSED. If the element supports seek in READY, it will always return %TRUE when
2368 * it receives the event in the READY state.
2370 * Returns: %TRUE if the seek operation succeeded (the seek might not always be
2371 * executed instantly though)
2376 gst_element_seek_simple (GstElement * element, GstFormat format,
2377 GstSeekFlags seek_flags, gint64 seek_pos)
2379 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2380 g_return_val_if_fail (seek_pos >= 0, FALSE);
2382 return gst_element_seek (element, 1.0, format, seek_flags,
2383 GST_SEEK_TYPE_SET, seek_pos, GST_SEEK_TYPE_NONE, 0);
2387 * gst_pad_use_fixed_caps:
2388 * @pad: the pad to use
2390 * A helper function you can use that sets the FIXED_CAPS flag
2391 * This way the default getcaps function will always return the negotiated caps
2392 * or in case the pad is not negotiated, the padtemplate caps.
2394 * Use this function on a pad that, once gst_pad_set_caps() has been called
2395 * on it, cannot be renegotiated to something else.
2398 gst_pad_use_fixed_caps (GstPad * pad)
2400 GST_OBJECT_FLAG_SET (pad, GST_PAD_FIXED_CAPS);
2404 * gst_pad_get_parent_element:
2407 * Gets the parent of @pad, cast to a #GstElement. If a @pad has no parent or
2408 * its parent is not an element, return NULL.
2410 * Returns: (transfer full): the parent of the pad. The caller has a
2411 * reference on the parent, so unref when you're finished with it.
2416 gst_pad_get_parent_element (GstPad * pad)
2420 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2422 p = gst_object_get_parent (GST_OBJECT_CAST (pad));
2424 if (p && !GST_IS_ELEMENT (p)) {
2425 gst_object_unref (p);
2428 return GST_ELEMENT_CAST (p);
2432 * gst_object_default_error:
2433 * @source: the #GstObject that initiated the error.
2434 * @error: (in): the GError.
2435 * @debug: (in) (allow-none): an additional debug information string, or NULL
2437 * A default error function that uses g_printerr() to display the error message
2438 * and the optional debug sting..
2440 * The default handler will simply print the error string using g_print.
2443 gst_object_default_error (GstObject * source, const GError * error,
2444 const gchar * debug)
2446 gchar *name = gst_object_get_path_string (source);
2448 g_printerr (_("ERROR: from element %s: %s\n"), name, error->message);
2450 g_printerr (_("Additional debug info:\n%s\n"), debug);
2458 * @element_1: (transfer full): the #GstElement element to add to the bin
2459 * @...: (transfer full): additional elements to add to the bin
2461 * Adds a NULL-terminated list of elements to a bin. This function is
2462 * equivalent to calling gst_bin_add() for each member of the list. The return
2463 * value of each gst_bin_add() is ignored.
2466 gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
2470 g_return_if_fail (GST_IS_BIN (bin));
2471 g_return_if_fail (GST_IS_ELEMENT (element_1));
2473 va_start (args, element_1);
2476 gst_bin_add (bin, element_1);
2478 element_1 = va_arg (args, GstElement *);
2485 * gst_bin_remove_many:
2487 * @element_1: (transfer none): the first #GstElement to remove from the bin
2488 * @...: (transfer none): NULL-terminated list of elements to remove from the bin
2490 * Remove a list of elements from a bin. This function is equivalent
2491 * to calling gst_bin_remove() with each member of the list.
2494 gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
2498 g_return_if_fail (GST_IS_BIN (bin));
2499 g_return_if_fail (GST_IS_ELEMENT (element_1));
2501 va_start (args, element_1);
2504 gst_bin_remove (bin, element_1);
2506 element_1 = va_arg (args, GstElement *);
2513 gst_element_populate_std_props (GObjectClass * klass, const gchar * prop_name,
2514 guint arg_id, GParamFlags flags)
2516 GQuark prop_id = g_quark_from_string (prop_name);
2519 static GQuark fd_id = 0;
2520 static GQuark blocksize_id;
2521 static GQuark bytesperread_id;
2522 static GQuark dump_id;
2523 static GQuark filesize_id;
2524 static GQuark mmapsize_id;
2525 static GQuark location_id;
2526 static GQuark offset_id;
2527 static GQuark silent_id;
2528 static GQuark touch_id;
2530 flags |= G_PARAM_STATIC_STRINGS;
2533 fd_id = g_quark_from_static_string ("fd");
2534 blocksize_id = g_quark_from_static_string ("blocksize");
2535 bytesperread_id = g_quark_from_static_string ("bytesperread");
2536 dump_id = g_quark_from_static_string ("dump");
2537 filesize_id = g_quark_from_static_string ("filesize");
2538 mmapsize_id = g_quark_from_static_string ("mmapsize");
2539 location_id = g_quark_from_static_string ("location");
2540 offset_id = g_quark_from_static_string ("offset");
2541 silent_id = g_quark_from_static_string ("silent");
2542 touch_id = g_quark_from_static_string ("touch");
2545 if (prop_id == fd_id) {
2546 pspec = g_param_spec_int ("fd", "File-descriptor",
2547 "File-descriptor for the file being read", 0, G_MAXINT, 0, flags);
2548 } else if (prop_id == blocksize_id) {
2549 pspec = g_param_spec_ulong ("blocksize", "Block Size",
2550 "Block size to read per buffer", 0, G_MAXULONG, 4096, flags);
2552 } else if (prop_id == bytesperread_id) {
2553 pspec = g_param_spec_int ("bytesperread", "Bytes per read",
2554 "Number of bytes to read per buffer", G_MININT, G_MAXINT, 0, flags);
2556 } else if (prop_id == dump_id) {
2557 pspec = g_param_spec_boolean ("dump", "Dump",
2558 "Dump bytes to stdout", FALSE, flags);
2560 } else if (prop_id == filesize_id) {
2561 pspec = g_param_spec_int64 ("filesize", "File Size",
2562 "Size of the file being read", 0, G_MAXINT64, 0, flags);
2564 } else if (prop_id == mmapsize_id) {
2565 pspec = g_param_spec_ulong ("mmapsize", "mmap() Block Size",
2566 "Size in bytes of mmap()d regions", 0, G_MAXULONG, 4 * 1048576, flags);
2568 } else if (prop_id == location_id) {
2569 pspec = g_param_spec_string ("location", "File Location",
2570 "Location of the file to read", NULL, flags);
2572 } else if (prop_id == offset_id) {
2573 pspec = g_param_spec_int64 ("offset", "File Offset",
2574 "Byte offset of current read pointer", 0, G_MAXINT64, 0, flags);
2576 } else if (prop_id == silent_id) {
2577 pspec = g_param_spec_boolean ("silent", "Silent", "Don't produce events",
2580 } else if (prop_id == touch_id) {
2581 pspec = g_param_spec_boolean ("touch", "Touch read data",
2582 "Touch data to force disk read before " "push ()", TRUE, flags);
2584 g_warning ("Unknown - 'standard' property '%s' id %d from klass %s",
2585 prop_name, arg_id, g_type_name (G_OBJECT_CLASS_TYPE (klass)));
2590 g_object_class_install_property (klass, arg_id, pspec);
2595 * gst_element_class_install_std_props:
2596 * @klass: the #GstElementClass to add the properties to.
2597 * @first_name: the name of the first property.
2598 * in a NULL terminated
2599 * @...: the id and flags of the first property, followed by
2600 * further 'name', 'id', 'flags' triplets and terminated by NULL.
2602 * Adds a list of standardized properties with types to the @klass.
2603 * the id is for the property switch in your get_prop method, and
2604 * the flags determine readability / writeability.
2607 gst_element_class_install_std_props (GstElementClass * klass,
2608 const gchar * first_name, ...)
2614 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
2616 va_start (args, first_name);
2621 int arg_id = va_arg (args, int);
2622 int flags = va_arg (args, int);
2624 gst_element_populate_std_props ((GObjectClass *) klass, name, arg_id,
2627 name = va_arg (args, char *);
2636 * @buf1: (transfer none): the first source #GstBuffer to merge.
2637 * @buf2: (transfer none): the second source #GstBuffer to merge.
2639 * Create a new buffer that is the concatenation of the two source
2640 * buffers. The original source buffers will not be modified or
2641 * unref'd. Make sure you unref the source buffers if they are not used
2642 * anymore afterwards.
2644 * If the buffers point to contiguous areas of memory, the buffer
2645 * is created without copying the data.
2647 * Free-function: gst_buffer_unref
2649 * Returns: (transfer full): the new #GstBuffer which is the concatenation
2650 * of the source buffers.
2653 gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2)
2658 size1 = gst_buffer_get_size (buf1);
2659 size2 = gst_buffer_get_size (buf2);
2661 /* we're just a specific case of the more general gst_buffer_span() */
2662 result = gst_buffer_span (buf1, 0, buf2, size1 + size2);
2669 * @buf1: the first source #GstBuffer.
2670 * @buf2: the second source #GstBuffer.
2672 * Create a new buffer that is the concatenation of the two source
2673 * buffers, and unrefs the original source buffers.
2675 * If the buffers point to contiguous areas of memory, the buffer
2676 * is created without copying the data.
2678 * This is a convenience function for C programmers. See also
2679 * gst_buffer_merge(), which does the same thing without
2680 * unreffing the input parameters. Language bindings without
2681 * explicit reference counting should not wrap this function.
2683 * Returns: (transfer full): the new #GstBuffer which is the concatenation of
2684 * the source buffers.
2687 gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2)
2692 size1 = gst_buffer_get_size (buf1);
2693 size2 = gst_buffer_get_size (buf2);
2695 result = gst_buffer_span (buf1, 0, buf2, size1 + size2);
2696 gst_buffer_unref (buf1);
2697 gst_buffer_unref (buf2);
2703 getcaps_fold_func (const GValue * vpad, GValue * ret, GstCaps * filter)
2705 GstPad *pad = g_value_get_object (vpad);
2706 gboolean empty = FALSE;
2707 GstCaps *peercaps, *existing;
2709 existing = g_value_get_pointer (ret);
2710 peercaps = gst_pad_peer_get_caps (pad, filter);
2711 if (G_LIKELY (peercaps)) {
2712 GstCaps *intersection = gst_caps_intersect (existing, peercaps);
2714 empty = gst_caps_is_empty (intersection);
2716 g_value_set_pointer (ret, intersection);
2717 gst_caps_unref (existing);
2718 gst_caps_unref (peercaps);
2724 * gst_pad_proxy_getcaps:
2725 * @pad: a #GstPad to proxy.
2726 * @filter: a #GstCaps filter.
2728 * Calls gst_pad_get_allowed_caps() for every other pad belonging to the
2729 * same element as @pad, and returns the intersection of the results.
2731 * This function is useful as a default getcaps function for an element
2732 * that can handle any stream format, but requires all its pads to have
2733 * the same caps. Two such elements are tee and adder.
2735 * Free-function: gst_caps_unref
2737 * Returns: (transfer full): the intersection of the other pads' allowed caps.
2740 gst_pad_proxy_getcaps (GstPad * pad, GstCaps * filter)
2742 GstElement *element;
2743 GstCaps *caps, *intersected;
2745 GstIteratorResult res;
2746 GValue ret = { 0, };
2748 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2750 GST_CAT_DEBUG (GST_CAT_PADS, "proxying getcaps for %s:%s",
2751 GST_DEBUG_PAD_NAME (pad));
2753 element = gst_pad_get_parent_element (pad);
2754 if (element == NULL)
2757 /* value to hold the return, by default it holds ANY, the ref is taken by
2759 g_value_init (&ret, G_TYPE_POINTER);
2760 g_value_set_pointer (&ret, gst_caps_new_any ());
2762 /* only iterate the pads in the oposite direction */
2763 if (GST_PAD_IS_SRC (pad))
2764 iter = gst_element_iterate_sink_pads (element);
2766 iter = gst_element_iterate_src_pads (element);
2770 gst_iterator_fold (iter, (GstIteratorFoldFunction) getcaps_fold_func,
2773 case GST_ITERATOR_RESYNC:
2774 /* unref any value stored */
2775 if ((caps = g_value_get_pointer (&ret)))
2776 gst_caps_unref (caps);
2777 /* need to reset the result again to ANY */
2778 g_value_set_pointer (&ret, gst_caps_new_any ());
2779 gst_iterator_resync (iter);
2781 case GST_ITERATOR_DONE:
2782 /* all pads iterated, return collected value */
2784 case GST_ITERATOR_OK:
2785 /* premature exit (happens if caps intersection is empty) */
2788 /* iterator returned _ERROR, mark an error and exit */
2789 if ((caps = g_value_get_pointer (&ret)))
2790 gst_caps_unref (caps);
2791 g_value_set_pointer (&ret, NULL);
2796 gst_iterator_free (iter);
2798 gst_object_unref (element);
2800 caps = g_value_get_pointer (&ret);
2801 g_value_unset (&ret);
2805 gst_caps_intersect (caps, gst_pad_get_pad_template_caps (pad));
2806 gst_caps_unref (caps);
2808 intersected = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
2816 GST_DEBUG_OBJECT (pad, "no parent");
2817 return gst_caps_copy (gst_pad_get_pad_template_caps (pad));
2821 g_warning ("Pad list returned error on element %s",
2822 GST_ELEMENT_NAME (element));
2823 gst_iterator_free (iter);
2824 gst_object_unref (element);
2825 return gst_caps_copy (gst_pad_get_pad_template_caps (pad));
2830 * gst_pad_query_position:
2831 * @pad: a #GstPad to invoke the position query on.
2832 * @format: (inout): a pointer to the #GstFormat asked for.
2833 * On return contains the #GstFormat used.
2834 * @cur: (out): A location in which to store the current position, or NULL.
2836 * Queries a pad for the stream position.
2838 * Returns: TRUE if the query could be performed.
2841 gst_pad_query_position (GstPad * pad, GstFormat * format, gint64 * cur)
2846 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2847 g_return_val_if_fail (format != NULL, FALSE);
2849 query = gst_query_new_position (*format);
2850 ret = gst_pad_query (pad, query);
2853 gst_query_parse_position (query, format, cur);
2855 gst_query_unref (query);
2861 * gst_pad_query_peer_position:
2862 * @pad: a #GstPad on whose peer to invoke the position query on.
2863 * Must be a sink pad.
2864 * @format: (inout): a pointer to the #GstFormat asked for.
2865 * On return contains the #GstFormat used.
2866 * @cur: (out) (allow-none): a location in which to store the current
2867 * position, or NULL.
2869 * Queries the peer of a given sink pad for the stream position.
2871 * Returns: TRUE if the query could be performed.
2874 gst_pad_query_peer_position (GstPad * pad, GstFormat * format, gint64 * cur)
2876 gboolean ret = FALSE;
2879 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2880 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2881 g_return_val_if_fail (format != NULL, FALSE);
2883 peer = gst_pad_get_peer (pad);
2885 ret = gst_pad_query_position (peer, format, cur);
2886 gst_object_unref (peer);
2893 * gst_pad_query_duration:
2894 * @pad: a #GstPad to invoke the duration query on.
2895 * @format: (inout): a pointer to the #GstFormat asked for.
2896 * On return contains the #GstFormat used.
2897 * @duration: (out) (allow-none): a location in which to store the total
2898 * duration, or NULL.
2900 * Queries a pad for the total stream duration.
2902 * Returns: TRUE if the query could be performed.
2905 gst_pad_query_duration (GstPad * pad, GstFormat * format, gint64 * duration)
2910 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2911 g_return_val_if_fail (format != NULL, FALSE);
2913 query = gst_query_new_duration (*format);
2914 ret = gst_pad_query (pad, query);
2917 gst_query_parse_duration (query, format, duration);
2919 gst_query_unref (query);
2925 * gst_pad_query_peer_duration:
2926 * @pad: a #GstPad on whose peer pad to invoke the duration query on.
2927 * Must be a sink pad.
2928 * @format: (inout) :a pointer to the #GstFormat asked for.
2929 * On return contains the #GstFormat used.
2930 * @duration: (out) (allow-none): a location in which to store the total
2931 * duration, or NULL.
2933 * Queries the peer pad of a given sink pad for the total stream duration.
2935 * Returns: TRUE if the query could be performed.
2938 gst_pad_query_peer_duration (GstPad * pad, GstFormat * format,
2941 gboolean ret = FALSE;
2944 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2945 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2946 g_return_val_if_fail (format != NULL, FALSE);
2948 peer = gst_pad_get_peer (pad);
2950 ret = gst_pad_query_duration (peer, format, duration);
2951 gst_object_unref (peer);
2958 * gst_pad_query_convert:
2959 * @pad: a #GstPad to invoke the convert query on.
2960 * @src_format: a #GstFormat to convert from.
2961 * @src_val: a value to convert.
2962 * @dest_format: (inout): a pointer to the #GstFormat to convert to.
2963 * @dest_val: (out): a pointer to the result.
2965 * Queries a pad to convert @src_val in @src_format to @dest_format.
2967 * Returns: TRUE if the query could be performed.
2970 gst_pad_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2971 GstFormat * dest_format, gint64 * dest_val)
2976 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2977 g_return_val_if_fail (dest_format != NULL, FALSE);
2978 g_return_val_if_fail (dest_val != NULL, FALSE);
2980 if (*dest_format == src_format || src_val == -1) {
2981 *dest_val = src_val;
2985 query = gst_query_new_convert (src_format, src_val, *dest_format);
2986 ret = gst_pad_query (pad, query);
2989 gst_query_parse_convert (query, NULL, NULL, dest_format, dest_val);
2991 gst_query_unref (query);
2997 * gst_pad_query_peer_convert:
2998 * @pad: a #GstPad, on whose peer pad to invoke the convert query on.
2999 * Must be a sink pad.
3000 * @src_format: a #GstFormat to convert from.
3001 * @src_val: a value to convert.
3002 * @dest_format: (inout): a pointer to the #GstFormat to convert to.
3003 * @dest_val: (out): a pointer to the result.
3005 * Queries the peer pad of a given sink pad to convert @src_val in @src_format
3008 * Returns: TRUE if the query could be performed.
3011 gst_pad_query_peer_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
3012 GstFormat * dest_format, gint64 * dest_val)
3014 gboolean ret = FALSE;
3017 g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
3018 g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
3019 g_return_val_if_fail (dest_format != NULL, FALSE);
3020 g_return_val_if_fail (dest_val != NULL, FALSE);
3022 peer = gst_pad_get_peer (pad);
3024 ret = gst_pad_query_convert (peer, src_format, src_val, dest_format,
3026 gst_object_unref (peer);
3033 * gst_element_found_tags_for_pad:
3034 * @element: element for which to post taglist to bus.
3035 * @pad: (transfer none): pad on which to push tag-event
3036 * @list: (transfer full): the taglist to post on the bus and create event from
3038 * Posts a message to the bus that new tags were found and pushes the
3039 * tags as event. Takes ownership of the @list.
3041 * This is a utility method for elements. Applications should use the
3042 * #GstTagSetter interface.
3045 gst_element_found_tags_for_pad (GstElement * element,
3046 GstPad * pad, GstTagList * list)
3048 g_return_if_fail (element != NULL);
3049 g_return_if_fail (pad != NULL);
3050 g_return_if_fail (list != NULL);
3052 gst_pad_push_event (pad, gst_event_new_tag (gst_tag_list_copy (list)));
3053 /* FIXME 0.11: Set the pad as source. */
3054 gst_element_post_message (element,
3055 gst_message_new_tag_full (GST_OBJECT (element), pad, list));
3059 push_and_ref (const GValue * vpad, GstEvent * event)
3061 GstPad *pad = g_value_get_object (vpad);
3063 gst_pad_push_event (pad, gst_event_ref (event));
3067 * gst_element_found_tags:
3068 * @element: element for which we found the tags.
3069 * @list: (transfer full): list of tags.
3071 * Posts a message to the bus that new tags were found, and pushes an event
3072 * to all sourcepads. Takes ownership of the @list.
3074 * This is a utility method for elements. Applications should use the
3075 * #GstTagSetter interface.
3078 gst_element_found_tags (GstElement * element, GstTagList * list)
3083 g_return_if_fail (element != NULL);
3084 g_return_if_fail (list != NULL);
3086 iter = gst_element_iterate_src_pads (element);
3087 event = gst_event_new_tag (gst_tag_list_copy (list));
3088 gst_iterator_foreach (iter, (GstIteratorForeachFunction) push_and_ref, event);
3089 gst_iterator_free (iter);
3090 gst_event_unref (event);
3092 gst_element_post_message (element,
3093 gst_message_new_tag (GST_OBJECT (element), list));
3097 element_find_unlinked_pad (GstElement * element, GstPadDirection direction)
3100 GstPad *unlinked_pad = NULL;
3102 GValue data = { 0, };
3104 switch (direction) {
3106 iter = gst_element_iterate_src_pads (element);
3109 iter = gst_element_iterate_sink_pads (element);
3112 g_return_val_if_reached (NULL);
3117 switch (gst_iterator_next (iter, &data)) {
3118 case GST_ITERATOR_OK:{
3120 GstPad *pad = g_value_get_object (&data);
3122 GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
3123 GST_DEBUG_PAD_NAME (pad));
3125 peer = gst_pad_get_peer (pad);
3127 unlinked_pad = gst_object_ref (pad);
3129 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
3130 "found existing unlinked pad %s:%s",
3131 GST_DEBUG_PAD_NAME (unlinked_pad));
3133 gst_object_unref (peer);
3135 g_value_reset (&data);
3138 case GST_ITERATOR_DONE:
3141 case GST_ITERATOR_RESYNC:
3142 gst_iterator_resync (iter);
3144 case GST_ITERATOR_ERROR:
3145 g_return_val_if_reached (NULL);
3149 g_value_unset (&data);
3150 gst_iterator_free (iter);
3152 return unlinked_pad;
3156 * gst_bin_find_unlinked_pad:
3157 * @bin: bin in which to look for elements with unlinked pads
3158 * @direction: whether to look for an unlinked source or sink pad
3160 * Recursively looks for elements with an unlinked pad of the given
3161 * direction within the specified bin and returns an unlinked pad
3162 * if one is found, or NULL otherwise. If a pad is found, the caller
3163 * owns a reference to it and should use gst_object_unref() on the
3164 * pad when it is not needed any longer.
3166 * Returns: (transfer full): unlinked pad of the given direction, or NULL.
3171 gst_bin_find_unlinked_pad (GstBin * bin, GstPadDirection direction)
3176 GValue data = { 0, };
3178 g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3179 g_return_val_if_fail (direction != GST_PAD_UNKNOWN, NULL);
3182 iter = gst_bin_iterate_recurse (bin);
3184 switch (gst_iterator_next (iter, &data)) {
3185 case GST_ITERATOR_OK:{
3186 GstElement *element = g_value_get_object (&data);
3188 pad = element_find_unlinked_pad (element, direction);
3191 g_value_reset (&data);
3194 case GST_ITERATOR_DONE:
3197 case GST_ITERATOR_RESYNC:
3198 gst_iterator_resync (iter);
3200 case GST_ITERATOR_ERROR:
3201 g_return_val_if_reached (NULL);
3205 g_value_unset (&data);
3206 gst_iterator_free (iter);
3212 * gst_parse_bin_from_description:
3213 * @bin_description: command line describing the bin
3214 * @ghost_unlinked_pads: whether to automatically create ghost pads
3215 * for unlinked source or sink pads within the bin
3216 * @err: where to store the error message in case of an error, or NULL
3218 * This is a convenience wrapper around gst_parse_launch() to create a
3219 * #GstBin from a gst-launch-style pipeline description. See
3220 * gst_parse_launch() and the gst-launch man page for details about the
3221 * syntax. Ghost pads on the bin for unlinked source or sink pads
3222 * within the bin can automatically be created (but only a maximum of
3223 * one ghost pad for each direction will be created; if you expect
3224 * multiple unlinked source pads or multiple unlinked sink pads
3225 * and want them all ghosted, you will have to create the ghost pads
3228 * Returns: (transfer full): a newly-created bin, or NULL if an error occurred.
3233 gst_parse_bin_from_description (const gchar * bin_description,
3234 gboolean ghost_unlinked_pads, GError ** err)
3236 return gst_parse_bin_from_description_full (bin_description,
3237 ghost_unlinked_pads, NULL, GST_PARSE_FLAG_NONE, err);
3241 * gst_parse_bin_from_description_full:
3242 * @bin_description: command line describing the bin
3243 * @ghost_unlinked_pads: whether to automatically create ghost pads
3244 * for unlinked source or sink pads within the bin
3245 * @context: (transfer none) (allow-none): a parse context allocated with
3246 * gst_parse_context_new(), or %NULL
3247 * @flags: parsing options, or #GST_PARSE_FLAG_NONE
3248 * @err: where to store the error message in case of an error, or NULL
3250 * This is a convenience wrapper around gst_parse_launch() to create a
3251 * #GstBin from a gst-launch-style pipeline description. See
3252 * gst_parse_launch() and the gst-launch man page for details about the
3253 * syntax. Ghost pads on the bin for unlinked source or sink pads
3254 * within the bin can automatically be created (but only a maximum of
3255 * one ghost pad for each direction will be created; if you expect
3256 * multiple unlinked source pads or multiple unlinked sink pads
3257 * and want them all ghosted, you will have to create the ghost pads
3260 * Returns: (transfer full): a newly-created bin, or NULL if an error occurred.
3265 gst_parse_bin_from_description_full (const gchar * bin_description,
3266 gboolean ghost_unlinked_pads, GstParseContext * context,
3267 GstParseFlags flags, GError ** err)
3269 #ifndef GST_DISABLE_PARSE
3274 g_return_val_if_fail (bin_description != NULL, NULL);
3275 g_return_val_if_fail (err == NULL || *err == NULL, NULL);
3277 GST_DEBUG ("Making bin from description '%s'", bin_description);
3279 /* parse the pipeline to a bin */
3280 desc = g_strdup_printf ("bin.( %s )", bin_description);
3281 bin = (GstBin *) gst_parse_launch_full (desc, context, flags, err);
3284 if (bin == NULL || (err && *err != NULL)) {
3286 gst_object_unref (bin);
3290 /* find pads and ghost them if necessary */
3291 if (ghost_unlinked_pads) {
3292 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC))) {
3293 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("src", pad));
3294 gst_object_unref (pad);
3296 if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SINK))) {
3297 gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("sink", pad));
3298 gst_object_unref (pad);
3302 return GST_ELEMENT (bin);
3306 GST_WARNING ("Disabled API called");
3308 msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
3309 g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
3317 * gst_type_register_static_full:
3318 * @parent_type: The GType of the parent type the newly registered type will
3320 * @type_name: NULL-terminated string used as the name of the new type
3321 * @class_size: Size of the class structure.
3322 * @base_init: Location of the base initialization function (optional).
3323 * @base_finalize: Location of the base finalization function (optional).
3324 * @class_init: Location of the class initialization function for class types
3325 * Location of the default vtable inititalization function for interface
3327 * @class_finalize: Location of the class finalization function for class types.
3328 * Location of the default vtable finalization function for interface types.
3330 * @class_data: User-supplied data passed to the class init/finalize functions.
3331 * @instance_size: Size of the instance (object) structure (required for
3332 * instantiatable types only).
3333 * @n_preallocs: The number of pre-allocated (cached) instances to reserve
3334 * memory for (0 indicates no caching). Ignored on recent GLib's.
3335 * @instance_init: Location of the instance initialization function (optional,
3336 * for instantiatable types only).
3337 * @value_table: A GTypeValueTable function table for generic handling of
3338 * GValues of this type (usually only useful for fundamental types).
3339 * @flags: #GTypeFlags for this GType. E.g: G_TYPE_FLAG_ABSTRACT
3341 * Helper function which constructs a #GTypeInfo structure and registers a
3342 * GType, but which generates less linker overhead than a static const
3343 * #GTypeInfo structure. For further details of the parameters, please see
3344 * #GTypeInfo in the GLib documentation.
3346 * Registers type_name as the name of a new static type derived from
3347 * parent_type. The value of flags determines the nature (e.g. abstract or
3348 * not) of the type. It works by filling a GTypeInfo struct and calling
3349 * g_type_register_static().
3351 * Returns: A #GType for the newly-registered type.
3356 gst_type_register_static_full (GType parent_type,
3357 const gchar * type_name,
3359 GBaseInitFunc base_init,
3360 GBaseFinalizeFunc base_finalize,
3361 GClassInitFunc class_init,
3362 GClassFinalizeFunc class_finalize,
3363 gconstpointer class_data,
3364 guint instance_size,
3365 guint16 n_preallocs,
3366 GInstanceInitFunc instance_init,
3367 const GTypeValueTable * value_table, GTypeFlags flags)
3371 info.class_size = class_size;
3372 info.base_init = base_init;
3373 info.base_finalize = base_finalize;
3374 info.class_init = class_init;
3375 info.class_finalize = class_finalize;
3376 info.class_data = class_data;
3377 info.instance_size = instance_size;
3378 info.n_preallocs = n_preallocs;
3379 info.instance_init = instance_init;
3380 info.value_table = value_table;
3382 return g_type_register_static (parent_type, type_name, &info, flags);
3387 * gst_util_get_timestamp:
3389 * Get a timestamp as GstClockTime to be used for interval meassurements.
3390 * The timestamp should not be interpreted in any other way.
3392 * Returns: the timestamp
3397 gst_util_get_timestamp (void)
3399 #if defined (HAVE_POSIX_TIMERS) && defined(HAVE_MONOTONIC_CLOCK)
3400 struct timespec now;
3402 clock_gettime (CLOCK_MONOTONIC, &now);
3403 return GST_TIMESPEC_TO_TIME (now);
3407 g_get_current_time (&now);
3408 return GST_TIMEVAL_TO_TIME (now);
3413 * gst_util_array_binary_search:
3414 * @array: the sorted input array
3415 * @num_elements: number of elements in the array
3416 * @element_size: size of every element in bytes
3417 * @search_func: (scope call): function to compare two elements, @search_data will always be passed as second argument
3418 * @mode: search mode that should be used
3419 * @search_data: element that should be found
3420 * @user_data: (closure): data to pass to @search_func
3422 * Searches inside @array for @search_data by using the comparison function
3423 * @search_func. @array must be sorted ascending.
3425 * As @search_data is always passed as second argument to @search_func it's
3426 * not required that @search_data has the same type as the array elements.
3428 * The complexity of this search function is O(log (num_elements)).
3430 * Returns: (transfer none): The address of the found element or %NULL if nothing was found
3435 gst_util_array_binary_search (gpointer array, guint num_elements,
3436 gsize element_size, GCompareDataFunc search_func, GstSearchMode mode,
3437 gconstpointer search_data, gpointer user_data)
3439 glong left = 0, right = num_elements - 1, m;
3441 guint8 *data = (guint8 *) array;
3443 g_return_val_if_fail (array != NULL, NULL);
3444 g_return_val_if_fail (element_size > 0, NULL);
3445 g_return_val_if_fail (search_func != NULL, NULL);
3447 /* 0. No elements => return NULL */
3448 if (num_elements == 0)
3451 /* 1. If search_data is before the 0th element return the 0th element */
3452 ret = search_func (data, search_data, user_data);
3453 if ((ret >= 0 && mode == GST_SEARCH_MODE_AFTER) || ret == 0)
3458 /* 2. If search_data is after the last element return the last element */
3460 search_func (data + (num_elements - 1) * element_size, search_data,
3462 if ((ret <= 0 && mode == GST_SEARCH_MODE_BEFORE) || ret == 0)
3463 return data + (num_elements - 1) * element_size;
3467 /* 3. else binary search */
3469 m = left + (right - left) / 2;
3471 ret = search_func (data + m * element_size, search_data, user_data);
3474 return data + m * element_size;
3475 } else if (ret < 0) {
3481 /* No exact match found */
3483 if (mode == GST_SEARCH_MODE_EXACT) {
3485 } else if (mode == GST_SEARCH_MODE_AFTER) {
3487 return (m < num_elements) ? data + (m + 1) * element_size : NULL;
3489 return data + m * element_size;
3492 return data + m * element_size;
3494 return (m > 0) ? data + (m - 1) * element_size : NULL;
3500 /* Finds the greatest common divisor.
3501 * Returns 1 if none other found.
3502 * This is Euclid's algorithm. */
3505 * gst_util_greatest_common_divisor:
3506 * @a: First value as #gint
3507 * @b: Second value as #gint
3509 * Calculates the greatest common divisor of @a
3512 * Returns: Greatest common divisor of @a and @b
3517 gst_util_greatest_common_divisor (gint a, gint b)
3530 * gst_util_fraction_to_double:
3531 * @src_n: Fraction numerator as #gint
3532 * @src_d: Fraction denominator #gint
3533 * @dest: (out): pointer to a #gdouble for the result
3535 * Transforms a fraction to a #gdouble.
3540 gst_util_fraction_to_double (gint src_n, gint src_d, gdouble * dest)
3542 g_return_if_fail (dest != NULL);
3543 g_return_if_fail (src_d != 0);
3545 *dest = ((gdouble) src_n) / ((gdouble) src_d);
3548 #define MAX_TERMS 30
3549 #define MIN_DIVISOR 1.0e-10
3550 #define MAX_ERROR 1.0e-20
3552 /* use continued fractions to transform a double into a fraction,
3553 * see http://mathforum.org/dr.math/faq/faq.fractions.html#decfrac.
3554 * This algorithm takes care of overflows.
3558 * gst_util_double_to_fraction:
3559 * @src: #gdouble to transform
3560 * @dest_n: (out): pointer to a #gint to hold the result numerator
3561 * @dest_d: (out): pointer to a #gint to hold the result denominator
3563 * Transforms a #gdouble to a fraction and simplifies
3569 gst_util_double_to_fraction (gdouble src, gint * dest_n, gint * dest_d)
3572 gdouble V, F; /* double being converted */
3573 gint N, D; /* will contain the result */
3574 gint A; /* current term in continued fraction */
3575 gint64 N1, D1; /* numerator, denominator of last approx */
3576 gint64 N2, D2; /* numerator, denominator of previous approx */
3579 gboolean negative = FALSE;
3581 g_return_if_fail (dest_n != NULL);
3582 g_return_if_fail (dest_d != NULL);
3584 /* initialize fraction being converted */
3592 /* initialize fractions with 1/0, 0/1 */
3600 for (i = 0; i < MAX_TERMS; i++) {
3602 A = (gint) F; /* no floor() needed, F is always >= 0 */
3603 /* get new divisor */
3606 /* calculate new fraction in temp */
3610 /* guard against overflow */
3611 if (N2 > G_MAXINT || D2 > G_MAXINT) {
3618 /* save last two fractions */
3624 /* quit if dividing by zero or close enough to target */
3625 if (F < MIN_DIVISOR || fabs (V - ((gdouble) N) / D) < MAX_ERROR) {
3629 /* Take reciprocal */
3632 /* fix for overflow */
3637 /* fix for negative */
3642 gcd = gst_util_greatest_common_divisor (N, D);
3654 * gst_util_fraction_multiply:
3655 * @a_n: Numerator of first value
3656 * @a_d: Denominator of first value
3657 * @b_n: Numerator of second value
3658 * @b_d: Denominator of second value
3659 * @res_n: (out): Pointer to #gint to hold the result numerator
3660 * @res_d: (out): Pointer to #gint to hold the result denominator
3662 * Multiplies the fractions @a_n/@a_d and @b_n/@b_d and stores
3663 * the result in @res_n and @res_d.
3665 * Returns: %FALSE on overflow, %TRUE otherwise.
3670 gst_util_fraction_multiply (gint a_n, gint a_d, gint b_n, gint b_d,
3671 gint * res_n, gint * res_d)
3675 g_return_val_if_fail (res_n != NULL, FALSE);
3676 g_return_val_if_fail (res_d != NULL, FALSE);
3677 g_return_val_if_fail (a_d != 0, FALSE);
3678 g_return_val_if_fail (b_d != 0, FALSE);
3680 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3684 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3688 gcd = gst_util_greatest_common_divisor (a_n, b_d);
3692 gcd = gst_util_greatest_common_divisor (a_d, b_n);
3696 /* This would result in overflow */
3697 if (a_n != 0 && G_MAXINT / ABS (a_n) < ABS (b_n))
3699 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3705 gcd = gst_util_greatest_common_divisor (*res_n, *res_d);
3713 * gst_util_fraction_add:
3714 * @a_n: Numerator of first value
3715 * @a_d: Denominator of first value
3716 * @b_n: Numerator of second value
3717 * @b_d: Denominator of second value
3718 * @res_n: (out): Pointer to #gint to hold the result numerator
3719 * @res_d: (out): Pointer to #gint to hold the result denominator
3721 * Adds the fractions @a_n/@a_d and @b_n/@b_d and stores
3722 * the result in @res_n and @res_d.
3724 * Returns: %FALSE on overflow, %TRUE otherwise.
3729 gst_util_fraction_add (gint a_n, gint a_d, gint b_n, gint b_d, gint * res_n,
3734 g_return_val_if_fail (res_n != NULL, FALSE);
3735 g_return_val_if_fail (res_d != NULL, FALSE);
3736 g_return_val_if_fail (a_d != 0, FALSE);
3737 g_return_val_if_fail (b_d != 0, FALSE);
3739 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3743 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3758 /* This would result in overflow */
3759 if (G_MAXINT / ABS (a_n) < ABS (b_n))
3761 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3763 if (G_MAXINT / ABS (a_d) < ABS (b_d))
3766 *res_n = (a_n * b_d) + (a_d * b_n);
3769 gcd = gst_util_greatest_common_divisor (*res_n, *res_d);
3782 * gst_util_fraction_compare:
3783 * @a_n: Numerator of first value
3784 * @a_d: Denominator of first value
3785 * @b_n: Numerator of second value
3786 * @b_d: Denominator of second value
3788 * Compares the fractions @a_n/@a_d and @b_n/@b_d and returns
3789 * -1 if a < b, 0 if a = b and 1 if a > b.
3791 * Returns: -1 if a < b; 0 if a = b; 1 if a > b.
3796 gst_util_fraction_compare (gint a_n, gint a_d, gint b_n, gint b_d)
3802 g_return_val_if_fail (a_d != 0 && b_d != 0, 0);
3805 gcd = gst_util_greatest_common_divisor (a_n, a_d);
3809 gcd = gst_util_greatest_common_divisor (b_n, b_d);
3813 /* fractions are reduced when set, so we can quickly see if they're equal */
3814 if (a_n == b_n && a_d == b_d)
3817 /* extend to 64 bits */
3818 new_num_1 = ((gint64) a_n) * b_d;
3819 new_num_2 = ((gint64) b_n) * a_d;
3820 if (new_num_1 < new_num_2)
3822 if (new_num_1 > new_num_2)
3825 /* Should not happen because a_d and b_d are not 0 */
3826 g_return_val_if_reached (0);