gst/: Assign debug statements to relevant categories instead of the 'default' categor...
[platform/upstream/gstreamer.git] / gst / gstutils.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2002 Thomas Vander Stichele <thomas@apestaart.org>
5  *
6  * gstutils.c: Utility functions
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /**
25  * SECTION:gstutils
26  * @short_description: Various utility functions
27  *
28  * When defining own plugins, use the GST_BOILERPLATE ease gobject creation.
29  */
30
31 #include "gst_private.h"
32 #include <stdio.h>
33 #include <string.h>
34
35 #include "gstghostpad.h"
36 #include "gstutils.h"
37 #include "gsterror.h"
38 #include "gstinfo.h"
39 #include "gstparse.h"
40 #include "gst-i18n-lib.h"
41
42
43 /**
44  * gst_util_dump_mem:
45  * @mem: a pointer to the memory to dump
46  * @size: the size of the memory block to dump
47  *
48  * Dumps the memory block into a hex representation. Useful for debugging.
49  */
50 void
51 gst_util_dump_mem (const guchar * mem, guint size)
52 {
53   guint i, j;
54   GString *string = g_string_sized_new (50);
55   GString *chars = g_string_sized_new (18);
56
57   i = j = 0;
58   while (i < size) {
59     if (g_ascii_isprint (mem[i]))
60       g_string_append_c (chars, mem[i]);
61     else
62       g_string_append_c (chars, '.');
63
64     g_string_append_printf (string, "%02x ", mem[i]);
65
66     j++;
67     i++;
68
69     if (j == 16 || i == size) {
70       g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j, mem + i - j,
71           string->str, chars->str);
72       g_string_set_size (string, 0);
73       g_string_set_size (chars, 0);
74       j = 0;
75     }
76   }
77   g_string_free (string, TRUE);
78   g_string_free (chars, TRUE);
79 }
80
81
82 /**
83  * gst_util_set_value_from_string:
84  * @value: the value to set
85  * @value_str: the string to get the value from
86  *
87  * Converts the string to the type of the value and
88  * sets the value with it.
89  */
90 void
91 gst_util_set_value_from_string (GValue * value, const gchar * value_str)
92 {
93   gint sscanf_ret;
94
95   g_return_if_fail (value != NULL);
96   g_return_if_fail (value_str != NULL);
97
98   GST_CAT_DEBUG (GST_CAT_PARAMS, "parsing '%s' to type %s", value_str,
99       g_type_name (G_VALUE_TYPE (value)));
100
101   switch (G_VALUE_TYPE (value)) {
102     case G_TYPE_STRING:
103       g_value_set_string (value, value_str);
104       break;
105     case G_TYPE_ENUM:
106     case G_TYPE_INT:{
107       gint i;
108
109       sscanf_ret = sscanf (value_str, "%d", &i);
110       g_return_if_fail (sscanf_ret == 1);
111       g_value_set_int (value, i);
112       break;
113     }
114     case G_TYPE_UINT:{
115       guint i;
116
117       sscanf_ret = sscanf (value_str, "%u", &i);
118       g_return_if_fail (sscanf_ret == 1);
119       g_value_set_uint (value, i);
120       break;
121     }
122     case G_TYPE_LONG:{
123       glong i;
124
125       sscanf_ret = sscanf (value_str, "%ld", &i);
126       g_return_if_fail (sscanf_ret == 1);
127       g_value_set_long (value, i);
128       break;
129     }
130     case G_TYPE_ULONG:{
131       gulong i;
132
133       sscanf_ret = sscanf (value_str, "%lu", &i);
134       g_return_if_fail (sscanf_ret == 1);
135       g_value_set_ulong (value, i);
136       break;
137     }
138     case G_TYPE_BOOLEAN:{
139       gboolean i = FALSE;
140
141       if (!g_ascii_strncasecmp ("true", value_str, 4))
142         i = TRUE;
143       g_value_set_boolean (value, i);
144       break;
145     }
146     case G_TYPE_CHAR:{
147       gchar i;
148
149       sscanf_ret = sscanf (value_str, "%c", &i);
150       g_return_if_fail (sscanf_ret == 1);
151       g_value_set_char (value, i);
152       break;
153     }
154     case G_TYPE_UCHAR:{
155       guchar i;
156
157       sscanf_ret = sscanf (value_str, "%c", &i);
158       g_return_if_fail (sscanf_ret == 1);
159       g_value_set_uchar (value, i);
160       break;
161     }
162     case G_TYPE_FLOAT:{
163       gfloat i;
164
165       sscanf_ret = sscanf (value_str, "%f", &i);
166       g_return_if_fail (sscanf_ret == 1);
167       g_value_set_float (value, i);
168       break;
169     }
170     case G_TYPE_DOUBLE:{
171       gfloat i;
172
173       sscanf_ret = sscanf (value_str, "%g", &i);
174       g_return_if_fail (sscanf_ret == 1);
175       g_value_set_double (value, (gdouble) i);
176       break;
177     }
178     default:
179       break;
180   }
181 }
182
183 /**
184  * gst_util_set_object_arg:
185  * @object: the object to set the argument of
186  * @name: the name of the argument to set
187  * @value: the string value to set
188  *
189  * Convertes the string value to the type of the objects argument and
190  * sets the argument with it.
191  */
192 void
193 gst_util_set_object_arg (GObject * object, const gchar * name,
194     const gchar * value)
195 {
196   gboolean sscanf_ret;
197
198   if (name && value) {
199     GParamSpec *paramspec;
200
201     paramspec =
202         g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
203
204     if (!paramspec) {
205       return;
206     }
207
208     GST_DEBUG ("paramspec->flags is %d, paramspec->value_type is %d",
209         paramspec->flags, (gint) paramspec->value_type);
210
211     if (paramspec->flags & G_PARAM_WRITABLE) {
212       switch (paramspec->value_type) {
213         case G_TYPE_STRING:
214           g_object_set (G_OBJECT (object), name, value, NULL);
215           break;
216         case G_TYPE_ENUM:
217         case G_TYPE_INT:{
218           gint i;
219
220           sscanf_ret = sscanf (value, "%d", &i);
221           g_return_if_fail (sscanf_ret == 1);
222           g_object_set (G_OBJECT (object), name, i, NULL);
223           break;
224         }
225         case G_TYPE_UINT:{
226           guint i;
227
228           sscanf_ret = sscanf (value, "%u", &i);
229           g_return_if_fail (sscanf_ret == 1);
230           g_object_set (G_OBJECT (object), name, i, NULL);
231           break;
232         }
233         case G_TYPE_LONG:{
234           glong i;
235
236           sscanf_ret = sscanf (value, "%ld", &i);
237           g_return_if_fail (sscanf_ret == 1);
238           g_object_set (G_OBJECT (object), name, i, NULL);
239           break;
240         }
241         case G_TYPE_ULONG:{
242           gulong i;
243
244           sscanf_ret = sscanf (value, "%lu", &i);
245           g_return_if_fail (sscanf_ret == 1);
246           g_object_set (G_OBJECT (object), name, i, NULL);
247           break;
248         }
249         case G_TYPE_BOOLEAN:{
250           gboolean i = FALSE;
251
252           if (!g_ascii_strncasecmp ("true", value, 4))
253             i = TRUE;
254           g_object_set (G_OBJECT (object), name, i, NULL);
255           break;
256         }
257         case G_TYPE_CHAR:{
258           gchar i;
259
260           sscanf_ret = sscanf (value, "%c", &i);
261           g_return_if_fail (sscanf_ret == 1);
262           g_object_set (G_OBJECT (object), name, i, NULL);
263           break;
264         }
265         case G_TYPE_UCHAR:{
266           guchar i;
267
268           sscanf_ret = sscanf (value, "%c", &i);
269           g_return_if_fail (sscanf_ret == 1);
270           g_object_set (G_OBJECT (object), name, i, NULL);
271           break;
272         }
273         case G_TYPE_FLOAT:{
274           gfloat i;
275
276           sscanf_ret = sscanf (value, "%f", &i);
277           g_return_if_fail (sscanf_ret == 1);
278           g_object_set (G_OBJECT (object), name, i, NULL);
279           break;
280         }
281         case G_TYPE_DOUBLE:{
282           gfloat i;
283
284           sscanf_ret = sscanf (value, "%g", &i);
285           g_return_if_fail (sscanf_ret == 1);
286           g_object_set (G_OBJECT (object), name, (gdouble) i, NULL);
287           break;
288         }
289         default:
290           if (G_IS_PARAM_SPEC_ENUM (paramspec)) {
291             gint i;
292
293             sscanf_ret = sscanf (value, "%d", &i);
294             g_return_if_fail (sscanf_ret == 1);
295             g_object_set (G_OBJECT (object), name, i, NULL);
296           }
297           break;
298       }
299     }
300   }
301 }
302
303 /* work around error C2520: conversion from unsigned __int64 to double
304  * not implemented, use signed __int64
305  *
306  * These are implemented as functions because on some platforms a 64bit int to
307  * double conversion is not defined/implemented.
308  */
309
310 gdouble
311 gst_util_guint64_to_gdouble (guint64 value)
312 {
313   if (value & G_GINT64_CONSTANT (0x8000000000000000))
314     return (gdouble) ((gint64) value) + (gdouble) 18446744073709551616.;
315   else
316     return (gdouble) ((gint64) value);
317 }
318
319 guint64
320 gst_util_gdouble_to_guint64 (gdouble value)
321 {
322   if (value < (gdouble) 9223372036854775808.)   /* 1 << 63 */
323     return ((guint64) ((gint64) value));
324
325   value -= (gdouble) 18446744073709551616.;
326   return ((guint64) ((gint64) value));
327 }
328
329 /* convenience struct for getting high and low uint32 parts of
330  * a guint64 */
331 typedef union
332 {
333   guint64 ll;
334   struct
335   {
336 #if G_BYTE_ORDER == G_BIG_ENDIAN
337     guint32 high, low;
338 #else
339     guint32 low, high;
340 #endif
341   } l;
342 } GstUInt64;
343
344 /* based on Hacker's Delight p152 */
345 static guint64
346 gst_util_div128_64 (GstUInt64 c1, GstUInt64 c0, guint64 denom)
347 {
348   GstUInt64 q1, q0, rhat;
349   GstUInt64 v, cmp1, cmp2;
350   guint s;
351
352   v.ll = denom;
353
354   /* count number of leading zeroes, we know they must be in the high
355    * part of denom since denom > G_MAXUINT32. */
356   s = v.l.high | (v.l.high >> 1);
357   s |= (s >> 2);
358   s |= (s >> 4);
359   s |= (s >> 8);
360   s = ~(s | (s >> 16));
361   s = s - ((s >> 1) & 0x55555555);
362   s = (s & 0x33333333) + ((s >> 2) & 0x33333333);
363   s = (s + (s >> 4)) & 0x0f0f0f0f;
364   s += (s >> 8);
365   s = (s + (s >> 16)) & 0x3f;
366
367   if (s > 0) {
368     /* normalize divisor and dividend */
369     v.ll <<= s;
370     c1.ll = (c1.ll << s) | (c0.l.high >> (32 - s));
371     c0.ll <<= s;
372   }
373
374   q1.ll = c1.ll / v.l.high;
375   rhat.ll = c1.ll - q1.ll * v.l.high;
376
377   cmp1.l.high = rhat.l.low;
378   cmp1.l.low = c0.l.high;
379   cmp2.ll = q1.ll * v.l.low;
380
381   while (q1.l.high || cmp2.ll > cmp1.ll) {
382     q1.ll--;
383     rhat.ll += v.l.high;
384     if (rhat.l.high)
385       break;
386     cmp1.l.high = rhat.l.low;
387     cmp2.ll -= v.l.low;
388   }
389   c1.l.high = c1.l.low;
390   c1.l.low = c0.l.high;
391   c1.ll -= q1.ll * v.ll;
392   q0.ll = c1.ll / v.l.high;
393   rhat.ll = c1.ll - q0.ll * v.l.high;
394
395   cmp1.l.high = rhat.l.low;
396   cmp1.l.low = c0.l.low;
397   cmp2.ll = q0.ll * v.l.low;
398
399   while (q0.l.high || cmp2.ll > cmp1.ll) {
400     q0.ll--;
401     rhat.ll += v.l.high;
402     if (rhat.l.high)
403       break;
404     cmp1.l.high = rhat.l.low;
405     cmp2.ll -= v.l.low;
406   }
407   q0.l.high += q1.l.low;
408
409   return q0.ll;
410 }
411
412 static guint64
413 gst_util_uint64_scale_int64 (guint64 val, guint64 num, guint64 denom)
414 {
415   GstUInt64 a0, a1, b0, b1, c0, ct, c1, result;
416   GstUInt64 v, n;
417
418   /* prepare input */
419   v.ll = val;
420   n.ll = num;
421
422   /* do 128 bits multiply
423    *                   nh   nl
424    *                *  vh   vl
425    *                ----------
426    * a0 =              vl * nl
427    * a1 =         vl * nh
428    * b0 =         vh * nl
429    * b1 =  + vh * nh
430    *       -------------------
431    * c1,c0
432    */
433   a0.ll = (guint64) v.l.low * n.l.low;
434   a1.ll = (guint64) v.l.low * n.l.high;
435   b0.ll = (guint64) v.l.high * n.l.low;
436   b1.ll = (guint64) v.l.high * n.l.high;
437
438   /* and sum together with carry into 128 bits c1, c0 */
439   c0.l.low = a0.l.low;
440   ct.ll = (guint64) a0.l.high + a1.l.low + b0.l.low;
441   c0.l.high = ct.l.low;
442   c1.ll = (guint64) a1.l.high + b0.l.high + ct.l.high + b1.ll;
443
444   /* if high bits bigger than denom, we overflow */
445   if (c1.ll >= denom)
446     goto overflow;
447
448   /* shortcut for division by 1, c1.ll should be 0 because of the
449    * overflow check above. */
450   if (denom == 1)
451     return c0.ll;
452
453   /* and 128/64 bits division, result fits 64 bits */
454   if (denom <= G_MAXUINT32) {
455     guint32 den = (guint32) denom;
456
457     /* easy case, (c1,c0)128/(den)32 division */
458     c1.l.high %= den;
459     c1.l.high = c1.ll % den;
460     c1.l.low = c0.l.high;
461     c0.l.high = c1.ll % den;
462     result.l.high = c1.ll / den;
463     result.l.low = c0.ll / den;
464   } else {
465     result.ll = gst_util_div128_64 (c1, c0, denom);
466   }
467   return result.ll;
468
469 overflow:
470   {
471     return G_MAXUINT64;
472   }
473 }
474
475 /**
476  * gst_util_uint64_scale:
477  * @val: the number to scale
478  * @num: the numerator of the scale ratio
479  * @denom: the denominator of the scale ratio
480  *
481  * Scale @val by @num / @denom, trying to avoid overflows.
482  *
483  * This function can potentially be very slow if denom > G_MAXUINT32.
484  *
485  * Returns: @val * @num / @denom, trying to avoid overflows.
486  * In the case of an overflow, this function returns G_MAXUINT64.
487  */
488 guint64
489 gst_util_uint64_scale (guint64 val, guint64 num, guint64 denom)
490 {
491   g_return_val_if_fail (denom != 0, G_MAXUINT64);
492
493   if (num == 0)
494     return 0;
495
496   if (num == 1 && denom == 1)
497     return val;
498
499   /* if the denom is high, we need to do a 64 muldiv */
500   if (denom > G_MAXINT32)
501     goto do_int64;
502
503   /* if num and denom are low we can do a 32 bit muldiv */
504   if (num <= G_MAXINT32)
505     goto do_int32;
506
507   /* val and num are high, we need 64 muldiv */
508   if (val > G_MAXINT32)
509     goto do_int64;
510
511   /* val is low and num is high, we can swap them and do 32 muldiv */
512   return gst_util_uint64_scale_int (num, (gint) val, (gint) denom);
513
514 do_int32:
515   return gst_util_uint64_scale_int (val, (gint) num, (gint) denom);
516
517 do_int64:
518   /* to the more heavy implementations... */
519   return gst_util_uint64_scale_int64 (val, num, denom);
520 }
521
522 /**
523  * gst_util_uint64_scale_int:
524  * @val: guint64 (such as a #GstClockTime) to scale.
525  * @num: numerator of the scale factor.
526  * @denom: denominator of the scale factor.
527  *
528  * Scale a guint64 by a factor expressed as a fraction (num/denom), avoiding
529  * overflows and loss of precision.
530  *
531  * @num and @denom must be positive integers. @denom cannot be 0.
532  *
533  * Returns: @val * @num / @denom, avoiding overflow and loss of precision.
534  * In the case of an overflow, this function returns G_MAXUINT64.
535  */
536 guint64
537 gst_util_uint64_scale_int (guint64 val, gint num, gint denom)
538 {
539   GstUInt64 result;
540   GstUInt64 low, high;
541
542   g_return_val_if_fail (denom > 0, G_MAXUINT64);
543   g_return_val_if_fail (num >= 0, G_MAXUINT64);
544
545   if (num == 0)
546     return 0;
547
548   if (num == 1 && denom == 1)
549     return val;
550
551   if (val <= G_MAXUINT32)
552     /* simple case */
553     return val * num / denom;
554
555   /* do 96 bits mult/div */
556   low.ll = val;
557   result.ll = ((guint64) low.l.low) * num;
558   high.ll = ((guint64) low.l.high) * num + (result.l.high);
559
560   low.ll = high.ll / denom;
561   result.l.high = high.ll % denom;
562   result.ll /= denom;
563
564   /* avoid overflow */
565   if (low.ll + result.l.high > G_MAXUINT32)
566     goto overflow;
567
568   result.l.high += low.l.low;
569
570   return result.ll;
571
572 overflow:
573   {
574     return G_MAXUINT64;
575   }
576 }
577
578 /**
579  * gst_util_seqnum_next:
580  *
581  * Return a constantly incrementing sequence number.
582  *
583  * This function is used internally to GStreamer to be able to determine which
584  * events and messages are "the same". For example, elements may set the seqnum
585  * on a segment-done message to be the same as that of the last seek event, to
586  * indicate that event and the message correspond to the same segment.
587  *
588  * Returns: A constantly incrementing 32-bit unsigned integer, which might
589  * overflow back to 0 at some point. Use gst_util_seqnum_compare() to make sure
590  * you handle wraparound correctly.
591  *
592  * Since: 0.10.22
593  */
594 guint32
595 gst_util_seqnum_next (void)
596 {
597   static gint counter = 0;
598   return g_atomic_int_exchange_and_add (&counter, 1);
599 }
600
601 /**
602  * gst_util_seqnum_compare:
603  * @s1: A sequence number.
604  * @s2: Another sequence number.
605  *
606  * Compare two sequence numbers, handling wraparound.
607  * 
608  * The current implementation just returns (gint32)(@s1 - @s2).
609  *
610  * Returns: A negative number if @s1 is before @s2, 0 if they are equal, or a
611  * positive number if @s1 is after @s2.
612  *
613  * Since: 0.10.22
614  */
615 gint32
616 gst_util_seqnum_compare (guint32 s1, guint32 s2)
617 {
618   return (gint32) (s1 - s2);
619 }
620
621 /* -----------------------------------------------------
622  *
623  *  The following code will be moved out of the main
624  * gstreamer library someday.
625  */
626
627 #include "gstpad.h"
628
629 static void
630 string_append_indent (GString * str, gint count)
631 {
632   gint xx;
633
634   for (xx = 0; xx < count; xx++)
635     g_string_append_c (str, ' ');
636 }
637
638 /**
639  * gst_print_pad_caps:
640  * @buf: the buffer to print the caps in
641  * @indent: initial indentation
642  * @pad: the pad to print the caps from
643  *
644  * Write the pad capabilities in a human readable format into
645  * the given GString.
646  */
647 void
648 gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
649 {
650   GstCaps *caps;
651
652   caps = pad->caps;
653
654   if (!caps) {
655     string_append_indent (buf, indent);
656     g_string_printf (buf, "%s:%s has no capabilities",
657         GST_DEBUG_PAD_NAME (pad));
658   } else {
659     char *s;
660
661     s = gst_caps_to_string (caps);
662     g_string_append (buf, s);
663     g_free (s);
664   }
665 }
666
667 /**
668  * gst_print_element_args:
669  * @buf: the buffer to print the args in
670  * @indent: initial indentation
671  * @element: the element to print the args of
672  *
673  * Print the element argument in a human readable format in the given
674  * GString.
675  */
676 void
677 gst_print_element_args (GString * buf, gint indent, GstElement * element)
678 {
679   guint width;
680   GValue value = { 0, };        /* the important thing is that value.type = 0 */
681   gchar *str = NULL;
682   GParamSpec *spec, **specs, **walk;
683
684   specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
685
686   width = 0;
687   for (walk = specs; *walk; walk++) {
688     spec = *walk;
689     if (width < strlen (spec->name))
690       width = strlen (spec->name);
691   }
692
693   for (walk = specs; *walk; walk++) {
694     spec = *walk;
695
696     if (spec->flags & G_PARAM_READABLE) {
697       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
698       g_object_get_property (G_OBJECT (element), spec->name, &value);
699       str = g_strdup_value_contents (&value);
700       g_value_unset (&value);
701     } else {
702       str = g_strdup ("Parameter not readable.");
703     }
704
705     string_append_indent (buf, indent);
706     g_string_append (buf, spec->name);
707     string_append_indent (buf, 2 + width - strlen (spec->name));
708     g_string_append (buf, str);
709     g_string_append_c (buf, '\n');
710
711     g_free (str);
712   }
713
714   g_free (specs);
715 }
716
717 /**
718  * gst_element_create_all_pads:
719  * @element: a #GstElement to create pads for
720  *
721  * Creates a pad for each pad template that is always available.
722  * This function is only useful during object intialization of
723  * subclasses of #GstElement.
724  */
725 void
726 gst_element_create_all_pads (GstElement * element)
727 {
728   GList *padlist;
729
730   /* FIXME: lock element */
731
732   padlist =
733       gst_element_class_get_pad_template_list (GST_ELEMENT_CLASS
734       (G_OBJECT_GET_CLASS (element)));
735
736   while (padlist) {
737     GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
738
739     if (padtempl->presence == GST_PAD_ALWAYS) {
740       GstPad *pad;
741
742       pad = gst_pad_new_from_template (padtempl, padtempl->name_template);
743
744       gst_element_add_pad (element, pad);
745     }
746     padlist = padlist->next;
747   }
748 }
749
750 /**
751  * gst_element_get_compatible_pad_template:
752  * @element: a #GstElement to get a compatible pad template for.
753  * @compattempl: the #GstPadTemplate to find a compatible template for.
754  *
755  * Retrieves a pad template from @element that is compatible with @compattempl.
756  * Pads from compatible templates can be linked together.
757  *
758  * Returns: a compatible #GstPadTemplate, or NULL if none was found. No
759  * unreferencing is necessary.
760  */
761 GstPadTemplate *
762 gst_element_get_compatible_pad_template (GstElement * element,
763     GstPadTemplate * compattempl)
764 {
765   GstPadTemplate *newtempl = NULL;
766   GList *padlist;
767   GstElementClass *class;
768
769   g_return_val_if_fail (element != NULL, NULL);
770   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
771   g_return_val_if_fail (compattempl != NULL, NULL);
772
773   class = GST_ELEMENT_GET_CLASS (element);
774
775   padlist = gst_element_class_get_pad_template_list (class);
776
777   GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
778       "Looking for a suitable pad template in %s out of %d templates...",
779       GST_ELEMENT_NAME (element), g_list_length (padlist));
780
781   while (padlist) {
782     GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
783     GstCaps *intersection;
784
785     /* Ignore name
786      * Ignore presence
787      * Check direction (must be opposite)
788      * Check caps
789      */
790     GST_CAT_LOG (GST_CAT_CAPS,
791         "checking pad template %s", padtempl->name_template);
792     if (padtempl->direction != compattempl->direction) {
793       GST_CAT_DEBUG (GST_CAT_CAPS,
794           "compatible direction: found %s pad template \"%s\"",
795           padtempl->direction == GST_PAD_SRC ? "src" : "sink",
796           padtempl->name_template);
797
798       GST_CAT_DEBUG (GST_CAT_CAPS,
799           "intersecting %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (compattempl));
800       GST_CAT_DEBUG (GST_CAT_CAPS,
801           "..and %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (padtempl));
802
803       intersection = gst_caps_intersect (GST_PAD_TEMPLATE_CAPS (compattempl),
804           GST_PAD_TEMPLATE_CAPS (padtempl));
805
806       GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible %" GST_PTR_FORMAT,
807           (intersection ? "" : "not "), intersection);
808
809       if (!gst_caps_is_empty (intersection))
810         newtempl = padtempl;
811       gst_caps_unref (intersection);
812       if (newtempl)
813         break;
814     }
815
816     padlist = g_list_next (padlist);
817   }
818   if (newtempl)
819     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
820         "Returning new pad template %p", newtempl);
821   else
822     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "No compatible pad template found");
823
824   return newtempl;
825 }
826
827 static GstPad *
828 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
829     const gchar * name)
830 {
831   GstPad *newpad = NULL;
832   GstElementClass *oclass;
833
834   oclass = GST_ELEMENT_GET_CLASS (element);
835
836   if (oclass->request_new_pad)
837     newpad = (oclass->request_new_pad) (element, templ, name);
838
839   if (newpad)
840     gst_object_ref (newpad);
841
842   return newpad;
843 }
844
845
846
847 /**
848  * gst_element_get_pad_from_template:
849  * @element: a #GstElement.
850  * @templ: a #GstPadTemplate belonging to @element.
851  *
852  * Gets a pad from @element described by @templ. If the presence of @templ is
853  * #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
854  * templates.
855  *
856  * Returns: the #GstPad, or NULL if one could not be found or created.
857  */
858 static GstPad *
859 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
860 {
861   GstPad *ret = NULL;
862   GstPadPresence presence;
863
864   /* If this function is ever exported, we need check the validity of `element'
865    * and `templ', and to make sure the template actually belongs to the
866    * element. */
867
868   presence = GST_PAD_TEMPLATE_PRESENCE (templ);
869
870   switch (presence) {
871     case GST_PAD_ALWAYS:
872     case GST_PAD_SOMETIMES:
873       ret = gst_element_get_static_pad (element, templ->name_template);
874       if (!ret && presence == GST_PAD_ALWAYS)
875         g_warning
876             ("Element %s has an ALWAYS template %s, but no pad of the same name",
877             GST_OBJECT_NAME (element), templ->name_template);
878       break;
879
880     case GST_PAD_REQUEST:
881       ret = gst_element_request_pad (element, templ, NULL);
882       break;
883   }
884
885   return ret;
886 }
887
888 /**
889  * gst_element_request_compatible_pad:
890  * @element: a #GstElement.
891  * @templ: the #GstPadTemplate to which the new pad should be able to link.
892  *
893  * Requests a pad from @element. The returned pad should be unlinked and
894  * compatible with @templ. Might return an existing pad, or request a new one.
895  *
896  * Returns: a #GstPad, or %NULL if one could not be found or created.
897  */
898 GstPad *
899 gst_element_request_compatible_pad (GstElement * element,
900     GstPadTemplate * templ)
901 {
902   GstPadTemplate *templ_new;
903   GstPad *pad = NULL;
904
905   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
906   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
907
908   /* FIXME: should really loop through the templates, testing each for
909    *      compatibility and pad availability. */
910   templ_new = gst_element_get_compatible_pad_template (element, templ);
911   if (templ_new)
912     pad = gst_element_get_pad_from_template (element, templ_new);
913
914   /* This can happen for non-request pads. No need to unref. */
915   if (pad && GST_PAD_PEER (pad))
916     pad = NULL;
917
918   return pad;
919 }
920
921 /**
922  * gst_element_get_compatible_pad:
923  * @element: a #GstElement in which the pad should be found.
924  * @pad: the #GstPad to find a compatible one for.
925  * @caps: the #GstCaps to use as a filter.
926  *
927  * Looks for an unlinked pad to which the given pad can link. It is not
928  * guaranteed that linking the pads will work, though it should work in most
929  * cases.
930  *
931  * Returns: the #GstPad to which a link can be made, or %NULL if one cannot be
932  * found. gst_object_unref() after usage.
933  */
934 GstPad *
935 gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
936     const GstCaps * caps)
937 {
938   GstIterator *pads;
939   GstPadTemplate *templ;
940   GstCaps *templcaps;
941   GstPad *foundpad = NULL;
942   gboolean done;
943
944   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
945   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
946
947   GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
948       "finding pad in %s compatible with %s:%s",
949       GST_ELEMENT_NAME (element), GST_DEBUG_PAD_NAME (pad));
950
951   g_return_val_if_fail (GST_PAD_PEER (pad) == NULL, NULL);
952
953   done = FALSE;
954   /* try to get an existing unlinked pad */
955   pads = gst_element_iterate_pads (element);
956   while (!done) {
957     gpointer padptr;
958
959     switch (gst_iterator_next (pads, &padptr)) {
960       case GST_ITERATOR_OK:
961       {
962         GstPad *peer;
963         GstPad *current;
964
965         current = GST_PAD (padptr);
966
967         GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
968             GST_DEBUG_PAD_NAME (current));
969
970         peer = gst_pad_get_peer (current);
971
972         if (peer == NULL && gst_pad_can_link (pad, current)) {
973           GstCaps *temp, *temp2, *intersection;
974
975           /* Now check if the two pads' caps are compatible */
976           temp = gst_pad_get_caps (pad);
977           if (caps) {
978             intersection = gst_caps_intersect (temp, caps);
979             gst_caps_unref (temp);
980           } else {
981             intersection = temp;
982           }
983
984           temp = gst_pad_get_caps (current);
985           temp2 = gst_caps_intersect (temp, intersection);
986           gst_caps_unref (temp);
987           gst_caps_unref (intersection);
988
989           intersection = temp2;
990
991           if (!gst_caps_is_empty (intersection)) {
992             gst_caps_unref (intersection);
993
994             GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
995                 "found existing unlinked compatible pad %s:%s",
996                 GST_DEBUG_PAD_NAME (current));
997             gst_iterator_free (pads);
998
999             return current;
1000           }
1001           gst_caps_unref (intersection);
1002         }
1003         GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unreffing pads");
1004
1005         gst_object_unref (current);
1006         if (peer)
1007           gst_object_unref (peer);
1008         break;
1009       }
1010       case GST_ITERATOR_DONE:
1011         done = TRUE;
1012         break;
1013       case GST_ITERATOR_RESYNC:
1014         gst_iterator_resync (pads);
1015         break;
1016       case GST_ITERATOR_ERROR:
1017         g_assert_not_reached ();
1018         break;
1019     }
1020   }
1021   gst_iterator_free (pads);
1022
1023   GST_CAT_DEBUG_OBJECT (GST_CAT_ELEMENT_PADS, element,
1024       "Could not find a compatible unlinked always pad to link to %s:%s, now checking request pads",
1025       GST_DEBUG_PAD_NAME (pad));
1026
1027   /* try to create a new one */
1028   /* requesting is a little crazy, we need a template. Let's create one */
1029   /* FIXME: why not gst_pad_get_pad_template (pad); */
1030   templcaps = gst_pad_get_caps (pad);
1031
1032   templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
1033       GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
1034
1035   /* FIXME : Because of a bug in gst_pad_template_new() by which is does not
1036    * properly steal the refcount of the given caps, we have to unref these caps
1037    * REVERT THIS WHEN FIXED !*/
1038   gst_caps_unref (templcaps);
1039
1040   foundpad = gst_element_request_compatible_pad (element, templ);
1041   gst_object_unref (templ);
1042
1043   if (foundpad) {
1044     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1045         "found existing request pad %s:%s", GST_DEBUG_PAD_NAME (foundpad));
1046     return foundpad;
1047   }
1048
1049   GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element,
1050       "Could not find a compatible pad to link to %s:%s",
1051       GST_DEBUG_PAD_NAME (pad));
1052   return NULL;
1053 }
1054
1055 /**
1056  * gst_element_state_get_name:
1057  * @state: a #GstState to get the name of.
1058  *
1059  * Gets a string representing the given state.
1060  *
1061  * Returns: a string with the name of the state.
1062  */
1063 G_CONST_RETURN gchar *
1064 gst_element_state_get_name (GstState state)
1065 {
1066   switch (state) {
1067 #ifdef GST_DEBUG_COLOR
1068     case GST_STATE_VOID_PENDING:
1069       return "VOID_PENDING";
1070     case GST_STATE_NULL:
1071       return "\033[01;34mNULL\033[00m";
1072     case GST_STATE_READY:
1073       return "\033[01;31mREADY\033[00m";
1074     case GST_STATE_PLAYING:
1075       return "\033[01;32mPLAYING\033[00m";
1076     case GST_STATE_PAUSED:
1077       return "\033[01;33mPAUSED\033[00m";
1078     default:
1079       /* This is a memory leak */
1080       return g_strdup_printf ("\033[01;35;41mUNKNOWN!\033[00m(%d)", state);
1081 #else
1082     case GST_STATE_VOID_PENDING:
1083       return "VOID_PENDING";
1084     case GST_STATE_NULL:
1085       return "NULL";
1086     case GST_STATE_READY:
1087       return "READY";
1088     case GST_STATE_PLAYING:
1089       return "PLAYING";
1090     case GST_STATE_PAUSED:
1091       return "PAUSED";
1092     default:
1093       /* This is a memory leak */
1094       return g_strdup_printf ("UNKNOWN!(%d)", state);
1095 #endif
1096   }
1097 }
1098
1099 /**
1100  * gst_element_state_change_return_get_name:
1101  * @state_ret: a #GstStateChangeReturn to get the name of.
1102  *
1103  * Gets a string representing the given state change result.
1104  *
1105  * Returns: a string with the name of the state change result.
1106  *
1107  * Since: 0.10.11
1108  */
1109 G_CONST_RETURN gchar *
1110 gst_element_state_change_return_get_name (GstStateChangeReturn state_ret)
1111 {
1112   switch (state_ret) {
1113 #ifdef GST_DEBUG_COLOR
1114     case GST_STATE_CHANGE_FAILURE:
1115       return "\033[01;31mFAILURE\033[00m";
1116     case GST_STATE_CHANGE_SUCCESS:
1117       return "\033[01;32mSUCCESS\033[00m";
1118     case GST_STATE_CHANGE_ASYNC:
1119       return "\033[01;33mASYNC\033[00m";
1120     case GST_STATE_CHANGE_NO_PREROLL:
1121       return "\033[01;34mNO_PREROLL\033[00m";
1122     default:
1123       /* This is a memory leak */
1124       return g_strdup_printf ("\033[01;35;41mUNKNOWN!\033[00m(%d)", state_ret);
1125 #else
1126     case GST_STATE_CHANGE_FAILURE:
1127       return "FAILURE";
1128     case GST_STATE_CHANGE_SUCCESS:
1129       return "SUCCESS";
1130     case GST_STATE_CHANGE_ASYNC:
1131       return "ASYNC";
1132     case GST_STATE_CHANGE_NO_PREROLL:
1133       return "NO PREROLL";
1134     default:
1135       /* This is a memory leak */
1136       return g_strdup_printf ("UNKNOWN!(%d)", state_ret);
1137 #endif
1138   }
1139 }
1140
1141
1142 /**
1143  * gst_element_factory_can_src_caps :
1144  * @factory: factory to query
1145  * @caps: the caps to check
1146  *
1147  * Checks if the factory can source the given capability.
1148  *
1149  * Returns: true if it can src the capabilities
1150  */
1151 gboolean
1152 gst_element_factory_can_src_caps (GstElementFactory * factory,
1153     const GstCaps * caps)
1154 {
1155   GList *templates;
1156
1157   g_return_val_if_fail (factory != NULL, FALSE);
1158   g_return_val_if_fail (caps != NULL, FALSE);
1159
1160   templates = factory->staticpadtemplates;
1161
1162   while (templates) {
1163     GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1164
1165     if (template->direction == GST_PAD_SRC) {
1166       if (gst_caps_is_always_compatible (gst_static_caps_get
1167               (&template->static_caps), caps))
1168         return TRUE;
1169     }
1170     templates = g_list_next (templates);
1171   }
1172
1173   return FALSE;
1174 }
1175
1176 /**
1177  * gst_element_factory_can_sink_caps :
1178  * @factory: factory to query
1179  * @caps: the caps to check
1180  *
1181  * Checks if the factory can sink the given capability.
1182  *
1183  * Returns: true if it can sink the capabilities
1184  */
1185 gboolean
1186 gst_element_factory_can_sink_caps (GstElementFactory * factory,
1187     const GstCaps * caps)
1188 {
1189   GList *templates;
1190
1191   g_return_val_if_fail (factory != NULL, FALSE);
1192   g_return_val_if_fail (caps != NULL, FALSE);
1193
1194   templates = factory->staticpadtemplates;
1195
1196   while (templates) {
1197     GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
1198
1199     if (template->direction == GST_PAD_SINK) {
1200       if (gst_caps_is_always_compatible (caps,
1201               gst_static_caps_get (&template->static_caps)))
1202         return TRUE;
1203     }
1204     templates = g_list_next (templates);
1205   }
1206
1207   return FALSE;
1208 }
1209
1210
1211 /* if return val is true, *direct_child is a caller-owned ref on the direct
1212  * child of ancestor that is part of object's ancestry */
1213 static gboolean
1214 object_has_ancestor (GstObject * object, GstObject * ancestor,
1215     GstObject ** direct_child)
1216 {
1217   GstObject *child, *parent;
1218
1219   if (direct_child)
1220     *direct_child = NULL;
1221
1222   child = gst_object_ref (object);
1223   parent = gst_object_get_parent (object);
1224
1225   while (parent) {
1226     if (ancestor == parent) {
1227       if (direct_child)
1228         *direct_child = child;
1229       else
1230         gst_object_unref (child);
1231       gst_object_unref (parent);
1232       return TRUE;
1233     }
1234
1235     gst_object_unref (child);
1236     child = parent;
1237     parent = gst_object_get_parent (parent);
1238   }
1239
1240   gst_object_unref (child);
1241
1242   return FALSE;
1243 }
1244
1245 /* caller owns return */
1246 static GstObject *
1247 find_common_root (GstObject * o1, GstObject * o2)
1248 {
1249   GstObject *top = o1;
1250   GstObject *kid1, *kid2;
1251   GstObject *root = NULL;
1252
1253   while (GST_OBJECT_PARENT (top))
1254     top = GST_OBJECT_PARENT (top);
1255
1256   /* the itsy-bitsy spider... */
1257
1258   if (!object_has_ancestor (o2, top, &kid2))
1259     return NULL;
1260
1261   root = gst_object_ref (top);
1262   while (TRUE) {
1263     if (!object_has_ancestor (o1, kid2, &kid1)) {
1264       gst_object_unref (kid2);
1265       return root;
1266     }
1267     root = kid2;
1268     if (!object_has_ancestor (o2, kid1, &kid2)) {
1269       gst_object_unref (kid1);
1270       return root;
1271     }
1272     root = kid1;
1273   }
1274 }
1275
1276 /* caller does not own return */
1277 static GstPad *
1278 ghost_up (GstElement * e, GstPad * pad)
1279 {
1280   static gint ghost_pad_index = 0;
1281   GstPad *gpad;
1282   gchar *name;
1283   GstObject *parent = GST_OBJECT_PARENT (e);
1284
1285   name = g_strdup_printf ("ghost%d", ghost_pad_index++);
1286   gpad = gst_ghost_pad_new (name, pad);
1287   g_free (name);
1288
1289   if (!gst_element_add_pad ((GstElement *) parent, gpad)) {
1290     g_warning ("Pad named %s already exists in element %s\n",
1291         GST_OBJECT_NAME (gpad), GST_OBJECT_NAME (parent));
1292     gst_object_unref ((GstObject *) gpad);
1293     return NULL;
1294   }
1295
1296   return gpad;
1297 }
1298
1299 static void
1300 remove_pad (gpointer ppad, gpointer unused)
1301 {
1302   GstPad *pad = ppad;
1303
1304   if (!gst_element_remove_pad ((GstElement *) GST_OBJECT_PARENT (pad), pad))
1305     g_warning ("Couldn't remove pad %s from element %s",
1306         GST_OBJECT_NAME (pad), GST_OBJECT_NAME (GST_OBJECT_PARENT (pad)));
1307 }
1308
1309 static gboolean
1310 prepare_link_maybe_ghosting (GstPad ** src, GstPad ** sink,
1311     GSList ** pads_created)
1312 {
1313   GstObject *root;
1314   GstObject *e1, *e2;
1315   GSList *pads_created_local = NULL;
1316
1317   g_assert (pads_created);
1318
1319   e1 = GST_OBJECT_PARENT (*src);
1320   e2 = GST_OBJECT_PARENT (*sink);
1321
1322   if (G_UNLIKELY (e1 == NULL)) {
1323     GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1324         GST_PTR_FORMAT, *src);
1325     return FALSE;
1326   }
1327   if (G_UNLIKELY (e2 == NULL)) {
1328     GST_WARNING ("Trying to ghost a pad that doesn't have a parent: %"
1329         GST_PTR_FORMAT, *sink);
1330     return FALSE;
1331   }
1332
1333   if (GST_OBJECT_PARENT (e1) == GST_OBJECT_PARENT (e2)) {
1334     GST_CAT_INFO (GST_CAT_PADS, "%s and %s in same bin, no need for ghost pads",
1335         GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1336     return TRUE;
1337   }
1338
1339   GST_CAT_INFO (GST_CAT_PADS, "%s and %s not in same bin, making ghost pads",
1340       GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
1341
1342   /* we need to setup some ghost pads */
1343   root = find_common_root (e1, e2);
1344   if (!root) {
1345     g_warning ("Trying to connect elements that don't share a common "
1346         "ancestor: %s and %s", GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2));
1347     return FALSE;
1348   }
1349
1350   while (GST_OBJECT_PARENT (e1) != root) {
1351     *src = ghost_up ((GstElement *) e1, *src);
1352     if (!*src)
1353       goto cleanup_fail;
1354     e1 = GST_OBJECT_PARENT (*src);
1355     pads_created_local = g_slist_prepend (pads_created_local, *src);
1356   }
1357   while (GST_OBJECT_PARENT (e2) != root) {
1358     *sink = ghost_up ((GstElement *) e2, *sink);
1359     if (!*sink)
1360       goto cleanup_fail;
1361     e2 = GST_OBJECT_PARENT (*sink);
1362     pads_created_local = g_slist_prepend (pads_created_local, *sink);
1363   }
1364
1365   gst_object_unref (root);
1366   *pads_created = g_slist_concat (*pads_created, pads_created_local);
1367   return TRUE;
1368
1369 cleanup_fail:
1370   gst_object_unref (root);
1371   g_slist_foreach (pads_created_local, remove_pad, NULL);
1372   g_slist_free (pads_created_local);
1373   return FALSE;
1374 }
1375
1376 static gboolean
1377 pad_link_maybe_ghosting (GstPad * src, GstPad * sink)
1378 {
1379   GSList *pads_created = NULL;
1380   gboolean ret;
1381
1382   if (!prepare_link_maybe_ghosting (&src, &sink, &pads_created)) {
1383     ret = FALSE;
1384   } else {
1385     ret = (gst_pad_link (src, sink) == GST_PAD_LINK_OK);
1386   }
1387
1388   if (!ret) {
1389     g_slist_foreach (pads_created, remove_pad, NULL);
1390   }
1391   g_slist_free (pads_created);
1392
1393   return ret;
1394 }
1395
1396 /**
1397  * gst_element_link_pads:
1398  * @src: a #GstElement containing the source pad.
1399  * @srcpadname: the name of the #GstPad in source element or NULL for any pad.
1400  * @dest: the #GstElement containing the destination pad.
1401  * @destpadname: the name of the #GstPad in destination element,
1402  * or NULL for any pad.
1403  *
1404  * Links the two named pads of the source and destination elements.
1405  * Side effect is that if one of the pads has no parent, it becomes a
1406  * child of the parent of the other element.  If they have different
1407  * parents, the link fails.
1408  *
1409  * Returns: TRUE if the pads could be linked, FALSE otherwise.
1410  */
1411 gboolean
1412 gst_element_link_pads (GstElement * src, const gchar * srcpadname,
1413     GstElement * dest, const gchar * destpadname)
1414 {
1415   const GList *srcpads, *destpads, *srctempls, *desttempls, *l;
1416   GstPad *srcpad, *destpad;
1417   GstPadTemplate *srctempl, *desttempl;
1418   GstElementClass *srcclass, *destclass;
1419
1420   /* checks */
1421   g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1422   g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1423
1424   srcclass = GST_ELEMENT_GET_CLASS (src);
1425   destclass = GST_ELEMENT_GET_CLASS (dest);
1426
1427   GST_CAT_INFO (GST_CAT_ELEMENT_PADS,
1428       "trying to link element %s:%s to element %s:%s", GST_ELEMENT_NAME (src),
1429       srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
1430       destpadname ? destpadname : "(any)");
1431
1432   /* get a src pad */
1433   if (srcpadname) {
1434     /* name specified, look it up */
1435     if (!(srcpad = gst_element_get_static_pad (src, srcpadname)))
1436       srcpad = gst_element_get_request_pad (src, srcpadname);
1437     if (!srcpad) {
1438       GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1439           GST_ELEMENT_NAME (src), srcpadname);
1440       return FALSE;
1441     } else {
1442       if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
1443         GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
1444             GST_DEBUG_PAD_NAME (srcpad));
1445         gst_object_unref (srcpad);
1446         return FALSE;
1447       }
1448       if (GST_PAD_PEER (srcpad) != NULL) {
1449         GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is already linked",
1450             GST_DEBUG_PAD_NAME (srcpad));
1451         gst_object_unref (srcpad);
1452         return FALSE;
1453       }
1454     }
1455     srcpads = NULL;
1456   } else {
1457     /* no name given, get the first available pad */
1458     GST_OBJECT_LOCK (src);
1459     srcpads = GST_ELEMENT_PADS (src);
1460     srcpad = srcpads ? GST_PAD_CAST (srcpads->data) : NULL;
1461     if (srcpad)
1462       gst_object_ref (srcpad);
1463     GST_OBJECT_UNLOCK (src);
1464   }
1465
1466   /* get a destination pad */
1467   if (destpadname) {
1468     /* name specified, look it up */
1469     if (!(destpad = gst_element_get_static_pad (dest, destpadname)))
1470       destpad = gst_element_get_request_pad (dest, destpadname);
1471     if (!destpad) {
1472       GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
1473           GST_ELEMENT_NAME (dest), destpadname);
1474       return FALSE;
1475     } else {
1476       if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
1477         GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
1478             GST_DEBUG_PAD_NAME (destpad));
1479         gst_object_unref (destpad);
1480         return FALSE;
1481       }
1482       if (GST_PAD_PEER (destpad) != NULL) {
1483         GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is already linked",
1484             GST_DEBUG_PAD_NAME (destpad));
1485         gst_object_unref (destpad);
1486         return FALSE;
1487       }
1488     }
1489     destpads = NULL;
1490   } else {
1491     /* no name given, get the first available pad */
1492     GST_OBJECT_LOCK (dest);
1493     destpads = GST_ELEMENT_PADS (dest);
1494     destpad = destpads ? GST_PAD_CAST (destpads->data) : NULL;
1495     if (destpad)
1496       gst_object_ref (destpad);
1497     GST_OBJECT_UNLOCK (dest);
1498   }
1499
1500   if (srcpadname && destpadname) {
1501     gboolean result;
1502
1503     /* two explicitly specified pads */
1504     result = pad_link_maybe_ghosting (srcpad, destpad);
1505
1506     gst_object_unref (srcpad);
1507     gst_object_unref (destpad);
1508
1509     return result;
1510   }
1511
1512   if (srcpad) {
1513     /* loop through the allowed pads in the source, trying to find a
1514      * compatible destination pad */
1515     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1516         "looping through allowed src and dest pads");
1517     do {
1518       GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying src pad %s:%s",
1519           GST_DEBUG_PAD_NAME (srcpad));
1520       if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
1521           (GST_PAD_PEER (srcpad) == NULL)) {
1522         GstPad *temp;
1523
1524         if (destpadname) {
1525           temp = destpad;
1526           gst_object_ref (temp);
1527         } else {
1528           temp = gst_element_get_compatible_pad (dest, srcpad, NULL);
1529         }
1530
1531         if (temp && pad_link_maybe_ghosting (srcpad, temp)) {
1532           GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1533               GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (temp));
1534           if (destpad)
1535             gst_object_unref (destpad);
1536           gst_object_unref (srcpad);
1537           gst_object_unref (temp);
1538           return TRUE;
1539         }
1540
1541         if (temp) {
1542           gst_object_unref (temp);
1543         }
1544       }
1545       /* find a better way for this mess */
1546       if (srcpads) {
1547         srcpads = g_list_next (srcpads);
1548         if (srcpads) {
1549           gst_object_unref (srcpad);
1550           srcpad = GST_PAD_CAST (srcpads->data);
1551           gst_object_ref (srcpad);
1552         }
1553       }
1554     } while (srcpads);
1555   }
1556   if (srcpadname) {
1557     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
1558         GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
1559     if (destpad)
1560       gst_object_unref (destpad);
1561     destpad = NULL;
1562   }
1563   if (srcpad)
1564     gst_object_unref (srcpad);
1565   srcpad = NULL;
1566
1567   if (destpad) {
1568     /* loop through the existing pads in the destination */
1569     do {
1570       GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying dest pad %s:%s",
1571           GST_DEBUG_PAD_NAME (destpad));
1572       if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
1573           (GST_PAD_PEER (destpad) == NULL)) {
1574         GstPad *temp = gst_element_get_compatible_pad (src, destpad, NULL);
1575
1576         if (temp && pad_link_maybe_ghosting (temp, destpad)) {
1577           GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1578               GST_DEBUG_PAD_NAME (temp), GST_DEBUG_PAD_NAME (destpad));
1579           gst_object_unref (temp);
1580           gst_object_unref (destpad);
1581           return TRUE;
1582         }
1583         if (temp) {
1584           gst_object_unref (temp);
1585         }
1586       }
1587       if (destpads) {
1588         destpads = g_list_next (destpads);
1589         if (destpads) {
1590           gst_object_unref (destpad);
1591           destpad = GST_PAD_CAST (destpads->data);
1592           gst_object_ref (destpad);
1593         }
1594       }
1595     } while (destpads);
1596   }
1597
1598   if (destpadname) {
1599     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
1600         GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
1601     gst_object_unref (destpad);
1602     return FALSE;
1603   } else {
1604     if (destpad)
1605       gst_object_unref (destpad);
1606     destpad = NULL;
1607   }
1608
1609   GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1610       "we might have request pads on both sides, checking...");
1611   srctempls = gst_element_class_get_pad_template_list (srcclass);
1612   desttempls = gst_element_class_get_pad_template_list (destclass);
1613
1614   if (srctempls && desttempls) {
1615     while (srctempls) {
1616       srctempl = (GstPadTemplate *) srctempls->data;
1617       if (srctempl->presence == GST_PAD_REQUEST) {
1618         for (l = desttempls; l; l = l->next) {
1619           desttempl = (GstPadTemplate *) l->data;
1620           if (desttempl->presence == GST_PAD_REQUEST &&
1621               desttempl->direction != srctempl->direction) {
1622             if (gst_caps_is_always_compatible (gst_pad_template_get_caps
1623                     (srctempl), gst_pad_template_get_caps (desttempl))) {
1624               srcpad =
1625                   gst_element_get_request_pad (src, srctempl->name_template);
1626               destpad =
1627                   gst_element_get_request_pad (dest, desttempl->name_template);
1628               if (srcpad && destpad
1629                   && pad_link_maybe_ghosting (srcpad, destpad)) {
1630                 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1631                     "linked pad %s:%s to pad %s:%s",
1632                     GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
1633                 gst_object_unref (srcpad);
1634                 gst_object_unref (destpad);
1635                 return TRUE;
1636               }
1637               /* it failed, so we release the request pads */
1638               if (srcpad)
1639                 gst_element_release_request_pad (src, srcpad);
1640               if (destpad)
1641                 gst_element_release_request_pad (dest, destpad);
1642             }
1643           }
1644         }
1645       }
1646       srctempls = srctempls->next;
1647     }
1648   }
1649
1650   GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s",
1651       GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1652   return FALSE;
1653 }
1654
1655 /**
1656  * gst_element_link_pads_filtered:
1657  * @src: a #GstElement containing the source pad.
1658  * @srcpadname: the name of the #GstPad in source element or NULL for any pad.
1659  * @dest: the #GstElement containing the destination pad.
1660  * @destpadname: the name of the #GstPad in destination element or NULL for any pad.
1661  * @filter: the #GstCaps to filter the link, or #NULL for no filter.
1662  *
1663  * Links the two named pads of the source and destination elements. Side effect
1664  * is that if one of the pads has no parent, it becomes a child of the parent of
1665  * the other element. If they have different parents, the link fails. If @caps
1666  * is not #NULL, makes sure that the caps of the link is a subset of @caps.
1667  *
1668  * Returns: TRUE if the pads could be linked, FALSE otherwise.
1669  */
1670 gboolean
1671 gst_element_link_pads_filtered (GstElement * src, const gchar * srcpadname,
1672     GstElement * dest, const gchar * destpadname, GstCaps * filter)
1673 {
1674   /* checks */
1675   g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1676   g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1677   g_return_val_if_fail (filter == NULL || GST_IS_CAPS (filter), FALSE);
1678
1679   if (filter) {
1680     GstElement *capsfilter;
1681     GstObject *parent;
1682     GstState state, pending;
1683
1684     capsfilter = gst_element_factory_make ("capsfilter", NULL);
1685     if (!capsfilter) {
1686       GST_ERROR ("Could not make a capsfilter");
1687       return FALSE;
1688     }
1689
1690     parent = gst_object_get_parent (GST_OBJECT (src));
1691     g_return_val_if_fail (GST_IS_BIN (parent), FALSE);
1692
1693     gst_element_get_state (GST_ELEMENT_CAST (parent), &state, &pending, 0);
1694
1695     if (!gst_bin_add (GST_BIN (parent), capsfilter)) {
1696       GST_ERROR ("Could not add capsfilter");
1697       gst_object_unref (capsfilter);
1698       gst_object_unref (parent);
1699       return FALSE;
1700     }
1701
1702     if (pending != GST_STATE_VOID_PENDING)
1703       state = pending;
1704
1705     gst_element_set_state (capsfilter, state);
1706
1707     gst_object_unref (parent);
1708
1709     g_object_set (capsfilter, "caps", filter, NULL);
1710
1711     if (gst_element_link_pads (src, srcpadname, capsfilter, "sink")
1712         && gst_element_link_pads (capsfilter, "src", dest, destpadname)) {
1713       return TRUE;
1714     } else {
1715       GST_INFO ("Could not link elements");
1716       gst_element_set_state (capsfilter, GST_STATE_NULL);
1717       /* this will unlink and unref as appropriate */
1718       gst_bin_remove (GST_BIN (GST_OBJECT_PARENT (capsfilter)), capsfilter);
1719       return FALSE;
1720     }
1721   } else {
1722     return gst_element_link_pads (src, srcpadname, dest, destpadname);
1723   }
1724 }
1725
1726 /**
1727  * gst_element_link:
1728  * @src: a #GstElement containing the source pad.
1729  * @dest: the #GstElement containing the destination pad.
1730  *
1731  * Links @src to @dest. The link must be from source to
1732  * destination; the other direction will not be tried. The function looks for
1733  * existing pads that aren't linked yet. It will request new pads if necessary.
1734  * Such pads need to be released manualy when unlinking.
1735  * If multiple links are possible, only one is established.
1736  *
1737  * Make sure you have added your elements to a bin or pipeline with
1738  * gst_bin_add() before trying to link them.
1739  *
1740  * Returns: TRUE if the elements could be linked, FALSE otherwise.
1741  */
1742 gboolean
1743 gst_element_link (GstElement * src, GstElement * dest)
1744 {
1745   return gst_element_link_pads_filtered (src, NULL, dest, NULL, NULL);
1746 }
1747
1748 /**
1749  * gst_element_link_many:
1750  * @element_1: the first #GstElement in the link chain.
1751  * @element_2: the second #GstElement in the link chain.
1752  * @...: the NULL-terminated list of elements to link in order.
1753  *
1754  * Chain together a series of elements. Uses gst_element_link().
1755  * Make sure you have added your elements to a bin or pipeline with
1756  * gst_bin_add() before trying to link them.
1757  *
1758  * Returns: TRUE on success, FALSE otherwise.
1759  */
1760 gboolean
1761 gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
1762 {
1763   va_list args;
1764
1765   g_return_val_if_fail (GST_IS_ELEMENT (element_1), FALSE);
1766   g_return_val_if_fail (GST_IS_ELEMENT (element_2), FALSE);
1767
1768   va_start (args, element_2);
1769
1770   while (element_2) {
1771     if (!gst_element_link (element_1, element_2))
1772       return FALSE;
1773
1774     element_1 = element_2;
1775     element_2 = va_arg (args, GstElement *);
1776   }
1777
1778   va_end (args);
1779
1780   return TRUE;
1781 }
1782
1783 /**
1784  * gst_element_link_filtered:
1785  * @src: a #GstElement containing the source pad.
1786  * @dest: the #GstElement containing the destination pad.
1787  * @filter: the #GstCaps to filter the link, or #NULL for no filter.
1788  *
1789  * Links @src to @dest using the given caps as filtercaps.
1790  * The link must be from source to
1791  * destination; the other direction will not be tried. The function looks for
1792  * existing pads that aren't linked yet. It will request new pads if necessary.
1793  * If multiple links are possible, only one is established.
1794  *
1795  * Make sure you have added your elements to a bin or pipeline with
1796  * gst_bin_add() before trying to link them.
1797  *
1798  * Returns: TRUE if the pads could be linked, FALSE otherwise.
1799  */
1800 gboolean
1801 gst_element_link_filtered (GstElement * src, GstElement * dest,
1802     GstCaps * filter)
1803 {
1804   return gst_element_link_pads_filtered (src, NULL, dest, NULL, filter);
1805 }
1806
1807 /**
1808  * gst_element_unlink_pads:
1809  * @src: a #GstElement containing the source pad.
1810  * @srcpadname: the name of the #GstPad in source element.
1811  * @dest: a #GstElement containing the destination pad.
1812  * @destpadname: the name of the #GstPad in destination element.
1813  *
1814  * Unlinks the two named pads of the source and destination elements.
1815  */
1816 void
1817 gst_element_unlink_pads (GstElement * src, const gchar * srcpadname,
1818     GstElement * dest, const gchar * destpadname)
1819 {
1820   GstPad *srcpad, *destpad;
1821   gboolean srcrequest, destrequest;
1822
1823   srcrequest = destrequest = FALSE;
1824
1825   g_return_if_fail (src != NULL);
1826   g_return_if_fail (GST_IS_ELEMENT (src));
1827   g_return_if_fail (srcpadname != NULL);
1828   g_return_if_fail (dest != NULL);
1829   g_return_if_fail (GST_IS_ELEMENT (dest));
1830   g_return_if_fail (destpadname != NULL);
1831
1832   /* obtain the pads requested */
1833   if (!(srcpad = gst_element_get_static_pad (src, srcpadname)))
1834     if ((srcpad = gst_element_get_request_pad (src, srcpadname)))
1835       srcrequest = TRUE;
1836   if (srcpad == NULL) {
1837     GST_WARNING_OBJECT (src, "source element has no pad \"%s\"", srcpadname);
1838     return;
1839   }
1840   if (!(destpad = gst_element_get_static_pad (dest, destpadname)))
1841     if ((destpad = gst_element_get_request_pad (dest, destpadname)))
1842       destrequest = TRUE;
1843   if (destpad == NULL) {
1844     GST_WARNING_OBJECT (dest, "destination element has no pad \"%s\"",
1845         destpadname);
1846     goto free_src;
1847   }
1848
1849   /* we're satisified they can be unlinked, let's do it */
1850   gst_pad_unlink (srcpad, destpad);
1851
1852   if (destrequest)
1853     gst_element_release_request_pad (dest, destpad);
1854   gst_object_unref (destpad);
1855
1856 free_src:
1857   if (srcrequest)
1858     gst_element_release_request_pad (src, srcpad);
1859   gst_object_unref (srcpad);
1860 }
1861
1862 /**
1863  * gst_element_unlink_many:
1864  * @element_1: the first #GstElement in the link chain.
1865  * @element_2: the second #GstElement in the link chain.
1866  * @...: the NULL-terminated list of elements to unlink in order.
1867  *
1868  * Unlinks a series of elements. Uses gst_element_unlink().
1869  */
1870 void
1871 gst_element_unlink_many (GstElement * element_1, GstElement * element_2, ...)
1872 {
1873   va_list args;
1874
1875   g_return_if_fail (element_1 != NULL && element_2 != NULL);
1876   g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
1877
1878   va_start (args, element_2);
1879
1880   while (element_2) {
1881     gst_element_unlink (element_1, element_2);
1882
1883     element_1 = element_2;
1884     element_2 = va_arg (args, GstElement *);
1885   }
1886
1887   va_end (args);
1888 }
1889
1890 /**
1891  * gst_element_unlink:
1892  * @src: the source #GstElement to unlink.
1893  * @dest: the sink #GstElement to unlink.
1894  *
1895  * Unlinks all source pads of the source element with all sink pads
1896  * of the sink element to which they are linked.
1897  *
1898  * If the link has been made using gst_element_link(), it could have created an
1899  * requestpad, which has to be released using gst_element_release_request_pad().
1900  */
1901 void
1902 gst_element_unlink (GstElement * src, GstElement * dest)
1903 {
1904   GstIterator *pads;
1905   gboolean done = FALSE;
1906
1907   g_return_if_fail (GST_IS_ELEMENT (src));
1908   g_return_if_fail (GST_IS_ELEMENT (dest));
1909
1910   GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unlinking \"%s\" and \"%s\"",
1911       GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1912
1913   pads = gst_element_iterate_pads (src);
1914   while (!done) {
1915     gpointer data;
1916
1917     switch (gst_iterator_next (pads, &data)) {
1918       case GST_ITERATOR_OK:
1919       {
1920         GstPad *pad = GST_PAD_CAST (data);
1921
1922         if (GST_PAD_IS_SRC (pad)) {
1923           GstPad *peerpad = gst_pad_get_peer (pad);
1924
1925           /* see if the pad is linked and is really a pad of dest */
1926           if (peerpad) {
1927             GstElement *peerelem;
1928
1929             peerelem = gst_pad_get_parent_element (peerpad);
1930
1931             if (peerelem == dest) {
1932               gst_pad_unlink (pad, peerpad);
1933             }
1934             if (peerelem)
1935               gst_object_unref (peerelem);
1936
1937             gst_object_unref (peerpad);
1938           }
1939         }
1940         gst_object_unref (pad);
1941         break;
1942       }
1943       case GST_ITERATOR_RESYNC:
1944         gst_iterator_resync (pads);
1945         break;
1946       case GST_ITERATOR_DONE:
1947         done = TRUE;
1948         break;
1949       default:
1950         g_assert_not_reached ();
1951         break;
1952     }
1953   }
1954   gst_iterator_free (pads);
1955 }
1956
1957 /**
1958  * gst_element_query_position:
1959  * @element: a #GstElement to invoke the position query on.
1960  * @format: a pointer to the #GstFormat asked for.
1961  *          On return contains the #GstFormat used.
1962  * @cur: A location in which to store the current position, or NULL.
1963  *
1964  * Queries an element for the stream position.
1965  *
1966  * Returns: TRUE if the query could be performed.
1967  */
1968 gboolean
1969 gst_element_query_position (GstElement * element, GstFormat * format,
1970     gint64 * cur)
1971 {
1972   GstQuery *query;
1973   gboolean ret;
1974
1975   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1976   g_return_val_if_fail (format != NULL, FALSE);
1977
1978   query = gst_query_new_position (*format);
1979   ret = gst_element_query (element, query);
1980
1981   if (ret)
1982     gst_query_parse_position (query, format, cur);
1983
1984   gst_query_unref (query);
1985
1986   return ret;
1987 }
1988
1989 /**
1990  * gst_element_query_duration:
1991  * @element: a #GstElement to invoke the duration query on.
1992  * @format: a pointer to the #GstFormat asked for.
1993  *          On return contains the #GstFormat used.
1994  * @duration: A location in which to store the total duration, or NULL.
1995  *
1996  * Queries an element for the total stream duration.
1997  *
1998  * Returns: TRUE if the query could be performed.
1999  */
2000 gboolean
2001 gst_element_query_duration (GstElement * element, GstFormat * format,
2002     gint64 * duration)
2003 {
2004   GstQuery *query;
2005   gboolean ret;
2006
2007   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2008   g_return_val_if_fail (format != NULL, FALSE);
2009
2010   query = gst_query_new_duration (*format);
2011   ret = gst_element_query (element, query);
2012
2013   if (ret)
2014     gst_query_parse_duration (query, format, duration);
2015
2016   gst_query_unref (query);
2017
2018   return ret;
2019 }
2020
2021 /**
2022  * gst_element_query_convert:
2023  * @element: a #GstElement to invoke the convert query on.
2024  * @src_format: a #GstFormat to convert from.
2025  * @src_val: a value to convert.
2026  * @dest_format: a pointer to the #GstFormat to convert to.
2027  * @dest_val: a pointer to the result.
2028  *
2029  * Queries an element to convert @src_val in @src_format to @dest_format.
2030  *
2031  * Returns: TRUE if the query could be performed.
2032  */
2033 gboolean
2034 gst_element_query_convert (GstElement * element, GstFormat src_format,
2035     gint64 src_val, GstFormat * dest_format, gint64 * dest_val)
2036 {
2037   GstQuery *query;
2038   gboolean ret;
2039
2040   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2041   g_return_val_if_fail (dest_format != NULL, FALSE);
2042   g_return_val_if_fail (dest_val != NULL, FALSE);
2043
2044   if (*dest_format == src_format) {
2045     *dest_val = src_val;
2046     return TRUE;
2047   }
2048
2049   query = gst_query_new_convert (src_format, src_val, *dest_format);
2050   ret = gst_element_query (element, query);
2051
2052   if (ret)
2053     gst_query_parse_convert (query, NULL, NULL, dest_format, dest_val);
2054
2055   gst_query_unref (query);
2056
2057   return ret;
2058 }
2059
2060 /**
2061  * gst_element_seek_simple
2062  * @element: a #GstElement to seek on
2063  * @format: a #GstFormat to execute the seek in, such as #GST_FORMAT_TIME
2064  * @seek_flags: seek options; playback applications will usually want to use
2065  *            GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT here
2066  * @seek_pos: position to seek to (relative to the start); if you are doing
2067  *            a seek in #GST_FORMAT_TIME this value is in nanoseconds -
2068  *            multiply with #GST_SECOND to convert seconds to nanoseconds or
2069  *            with #GST_MSECOND to convert milliseconds to nanoseconds.
2070  *
2071  * Simple API to perform a seek on the given element, meaning it just seeks
2072  * to the given position relative to the start of the stream. For more complex
2073  * operations like segment seeks (e.g. for looping) or changing the playback
2074  * rate or seeking relative to the last configured playback segment you should
2075  * use gst_element_seek().
2076  *
2077  * In a completely prerolled PAUSED or PLAYING pipeline, seeking is always
2078  * guaranteed to return %TRUE on a seekable media type or %FALSE when the media
2079  * type is certainly not seekable (such as a live stream).
2080  *
2081  * Some elements allow for seeking in the READY state, in this
2082  * case they will store the seek event and execute it when they are put to
2083  * PAUSED. If the element supports seek in READY, it will always return %TRUE when
2084  * it receives the event in the READY state.
2085  *
2086  * Returns: %TRUE if the seek operation succeeded (the seek might not always be
2087  * executed instantly though)
2088  *
2089  * Since: 0.10.7
2090  */
2091 gboolean
2092 gst_element_seek_simple (GstElement * element, GstFormat format,
2093     GstSeekFlags seek_flags, gint64 seek_pos)
2094 {
2095   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
2096   g_return_val_if_fail (seek_pos >= 0, FALSE);
2097
2098   return gst_element_seek (element, 1.0, format, seek_flags,
2099       GST_SEEK_TYPE_SET, seek_pos, GST_SEEK_TYPE_NONE, 0);
2100 }
2101
2102 /**
2103  * gst_pad_can_link:
2104  * @srcpad: the source #GstPad to link.
2105  * @sinkpad: the sink #GstPad to link.
2106  *
2107  * Checks if the source pad and the sink pad can be linked.
2108  * Both @srcpad and @sinkpad must be unlinked.
2109  *
2110  * Returns: TRUE if the pads can be linked, FALSE otherwise.
2111  */
2112 gboolean
2113 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
2114 {
2115   /* FIXME This function is gross.  It's almost a direct copy of
2116    * gst_pad_link_filtered().  Any decent programmer would attempt
2117    * to merge the two functions, which I will do some day. --ds
2118    */
2119
2120   /* generic checks */
2121   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
2122   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
2123
2124   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
2125       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
2126
2127   /* FIXME: shouldn't we convert this to g_return_val_if_fail? */
2128   if (GST_PAD_PEER (srcpad) != NULL) {
2129     GST_CAT_INFO (GST_CAT_PADS, "Source pad %s:%s has a peer, failed",
2130         GST_DEBUG_PAD_NAME (srcpad));
2131     return FALSE;
2132   }
2133   if (GST_PAD_PEER (sinkpad) != NULL) {
2134     GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has a peer, failed",
2135         GST_DEBUG_PAD_NAME (sinkpad));
2136     return FALSE;
2137   }
2138   if (!GST_PAD_IS_SRC (srcpad)) {
2139     GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s is not source pad, failed",
2140         GST_DEBUG_PAD_NAME (srcpad));
2141     return FALSE;
2142   }
2143   if (!GST_PAD_IS_SINK (sinkpad)) {
2144     GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s is not sink pad, failed",
2145         GST_DEBUG_PAD_NAME (sinkpad));
2146     return FALSE;
2147   }
2148   if (GST_PAD_PARENT (srcpad) == NULL) {
2149     GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s has no parent, failed",
2150         GST_DEBUG_PAD_NAME (srcpad));
2151     return FALSE;
2152   }
2153   if (GST_PAD_PARENT (sinkpad) == NULL) {
2154     GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has no parent, failed",
2155         GST_DEBUG_PAD_NAME (srcpad));
2156     return FALSE;
2157   }
2158
2159   return TRUE;
2160 }
2161
2162 /**
2163  * gst_pad_use_fixed_caps:
2164  * @pad: the pad to use
2165  *
2166  * A helper function you can use that sets the
2167  * @gst_pad_get_fixed_caps_func as the getcaps function for the
2168  * pad. This way the function will always return the negotiated caps
2169  * or in case the pad is not negotiated, the padtemplate caps.
2170  *
2171  * Use this function on a pad that, once _set_caps() has been called
2172  * on it, cannot be renegotiated to something else.
2173  */
2174 void
2175 gst_pad_use_fixed_caps (GstPad * pad)
2176 {
2177   gst_pad_set_getcaps_function (pad, gst_pad_get_fixed_caps_func);
2178 }
2179
2180 /**
2181  * gst_pad_get_fixed_caps_func:
2182  * @pad: the pad to use
2183  *
2184  * A helper function you can use as a GetCaps function that
2185  * will return the currently negotiated caps or the padtemplate
2186  * when NULL.
2187  *
2188  * Returns: The currently negotiated caps or the padtemplate.
2189  */
2190 GstCaps *
2191 gst_pad_get_fixed_caps_func (GstPad * pad)
2192 {
2193   GstCaps *result;
2194
2195   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2196
2197   GST_OBJECT_LOCK (pad);
2198   if (GST_PAD_CAPS (pad)) {
2199     result = GST_PAD_CAPS (pad);
2200
2201     GST_CAT_DEBUG (GST_CAT_CAPS,
2202         "using pad caps %p %" GST_PTR_FORMAT, result, result);
2203
2204     result = gst_caps_ref (result);
2205     goto done;
2206   }
2207   if (GST_PAD_PAD_TEMPLATE (pad)) {
2208     GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
2209
2210     result = GST_PAD_TEMPLATE_CAPS (templ);
2211     GST_CAT_DEBUG (GST_CAT_CAPS,
2212         "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
2213         result);
2214
2215     result = gst_caps_ref (result);
2216     goto done;
2217   }
2218   GST_CAT_DEBUG (GST_CAT_CAPS, "pad has no caps");
2219   result = gst_caps_new_empty ();
2220
2221 done:
2222   GST_OBJECT_UNLOCK (pad);
2223
2224   return result;
2225 }
2226
2227 /**
2228  * gst_pad_get_parent_element:
2229  * @pad: a pad
2230  *
2231  * Gets the parent of @pad, cast to a #GstElement. If a @pad has no parent or
2232  * its parent is not an element, return NULL.
2233  *
2234  * Returns: The parent of the pad. The caller has a reference on the parent, so
2235  * unref when you're finished with it.
2236  *
2237  * MT safe.
2238  */
2239 GstElement *
2240 gst_pad_get_parent_element (GstPad * pad)
2241 {
2242   GstObject *p;
2243
2244   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2245
2246   p = gst_object_get_parent (GST_OBJECT_CAST (pad));
2247
2248   if (p && !GST_IS_ELEMENT (p)) {
2249     gst_object_unref (p);
2250     p = NULL;
2251   }
2252   return GST_ELEMENT_CAST (p);
2253 }
2254
2255 /**
2256  * gst_object_default_error:
2257  * @source: the #GstObject that initiated the error.
2258  * @error: the GError.
2259  * @debug: an additional debug information string, or NULL.
2260  *
2261  * A default error function.
2262  *
2263  * The default handler will simply print the error string using g_print.
2264  */
2265 void
2266 gst_object_default_error (GstObject * source, GError * error, gchar * debug)
2267 {
2268   gchar *name = gst_object_get_path_string (source);
2269
2270   g_print (_("ERROR: from element %s: %s\n"), name, error->message);
2271   if (debug)
2272     g_print (_("Additional debug info:\n%s\n"), debug);
2273
2274   g_free (name);
2275 }
2276
2277 /**
2278  * gst_bin_add_many:
2279  * @bin: a #GstBin
2280  * @element_1: the #GstElement element to add to the bin
2281  * @...: additional elements to add to the bin
2282  *
2283  * Adds a NULL-terminated list of elements to a bin.  This function is
2284  * equivalent to calling gst_bin_add() for each member of the list. The return
2285  * value of each gst_bin_add() is ignored.
2286  */
2287 void
2288 gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
2289 {
2290   va_list args;
2291
2292   g_return_if_fail (GST_IS_BIN (bin));
2293   g_return_if_fail (GST_IS_ELEMENT (element_1));
2294
2295   va_start (args, element_1);
2296
2297   while (element_1) {
2298     gst_bin_add (bin, element_1);
2299
2300     element_1 = va_arg (args, GstElement *);
2301   }
2302
2303   va_end (args);
2304 }
2305
2306 /**
2307  * gst_bin_remove_many:
2308  * @bin: a #GstBin
2309  * @element_1: the first #GstElement to remove from the bin
2310  * @...: NULL-terminated list of elements to remove from the bin
2311  *
2312  * Remove a list of elements from a bin. This function is equivalent
2313  * to calling gst_bin_remove() with each member of the list.
2314  */
2315 void
2316 gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
2317 {
2318   va_list args;
2319
2320   g_return_if_fail (GST_IS_BIN (bin));
2321   g_return_if_fail (GST_IS_ELEMENT (element_1));
2322
2323   va_start (args, element_1);
2324
2325   while (element_1) {
2326     gst_bin_remove (bin, element_1);
2327
2328     element_1 = va_arg (args, GstElement *);
2329   }
2330
2331   va_end (args);
2332 }
2333
2334 static void
2335 gst_element_populate_std_props (GObjectClass * klass, const gchar * prop_name,
2336     guint arg_id, GParamFlags flags)
2337 {
2338   GQuark prop_id = g_quark_from_string (prop_name);
2339   GParamSpec *pspec;
2340
2341   static GQuark fd_id = 0;
2342   static GQuark blocksize_id;
2343   static GQuark bytesperread_id;
2344   static GQuark dump_id;
2345   static GQuark filesize_id;
2346   static GQuark mmapsize_id;
2347   static GQuark location_id;
2348   static GQuark offset_id;
2349   static GQuark silent_id;
2350   static GQuark touch_id;
2351
2352   if (!fd_id) {
2353     fd_id = g_quark_from_static_string ("fd");
2354     blocksize_id = g_quark_from_static_string ("blocksize");
2355     bytesperread_id = g_quark_from_static_string ("bytesperread");
2356     dump_id = g_quark_from_static_string ("dump");
2357     filesize_id = g_quark_from_static_string ("filesize");
2358     mmapsize_id = g_quark_from_static_string ("mmapsize");
2359     location_id = g_quark_from_static_string ("location");
2360     offset_id = g_quark_from_static_string ("offset");
2361     silent_id = g_quark_from_static_string ("silent");
2362     touch_id = g_quark_from_static_string ("touch");
2363   }
2364
2365   if (prop_id == fd_id) {
2366     pspec = g_param_spec_int ("fd", "File-descriptor",
2367         "File-descriptor for the file being read", 0, G_MAXINT, 0, flags);
2368   } else if (prop_id == blocksize_id) {
2369     pspec = g_param_spec_ulong ("blocksize", "Block Size",
2370         "Block size to read per buffer", 0, G_MAXULONG, 4096, flags);
2371
2372   } else if (prop_id == bytesperread_id) {
2373     pspec = g_param_spec_int ("bytesperread", "Bytes per read",
2374         "Number of bytes to read per buffer", G_MININT, G_MAXINT, 0, flags);
2375
2376   } else if (prop_id == dump_id) {
2377     pspec = g_param_spec_boolean ("dump", "Dump",
2378         "Dump bytes to stdout", FALSE, flags);
2379
2380   } else if (prop_id == filesize_id) {
2381     pspec = g_param_spec_int64 ("filesize", "File Size",
2382         "Size of the file being read", 0, G_MAXINT64, 0, flags);
2383
2384   } else if (prop_id == mmapsize_id) {
2385     pspec = g_param_spec_ulong ("mmapsize", "mmap() Block Size",
2386         "Size in bytes of mmap()d regions", 0, G_MAXULONG, 4 * 1048576, flags);
2387
2388   } else if (prop_id == location_id) {
2389     pspec = g_param_spec_string ("location", "File Location",
2390         "Location of the file to read", NULL, flags);
2391
2392   } else if (prop_id == offset_id) {
2393     pspec = g_param_spec_int64 ("offset", "File Offset",
2394         "Byte offset of current read pointer", 0, G_MAXINT64, 0, flags);
2395
2396   } else if (prop_id == silent_id) {
2397     pspec = g_param_spec_boolean ("silent", "Silent", "Don't produce events",
2398         FALSE, flags);
2399
2400   } else if (prop_id == touch_id) {
2401     pspec = g_param_spec_boolean ("touch", "Touch read data",
2402         "Touch data to force disk read before " "push ()", TRUE, flags);
2403   } else {
2404     g_warning ("Unknown - 'standard' property '%s' id %d from klass %s",
2405         prop_name, arg_id, g_type_name (G_OBJECT_CLASS_TYPE (klass)));
2406     pspec = NULL;
2407   }
2408
2409   if (pspec) {
2410     g_object_class_install_property (klass, arg_id, pspec);
2411   }
2412 }
2413
2414 /**
2415  * gst_element_class_install_std_props:
2416  * @klass: the #GstElementClass to add the properties to.
2417  * @first_name: the name of the first property.
2418  * in a NULL terminated
2419  * @...: the id and flags of the first property, followed by
2420  * further 'name', 'id', 'flags' triplets and terminated by NULL.
2421  *
2422  * Adds a list of standardized properties with types to the @klass.
2423  * the id is for the property switch in your get_prop method, and
2424  * the flags determine readability / writeability.
2425  **/
2426 void
2427 gst_element_class_install_std_props (GstElementClass * klass,
2428     const gchar * first_name, ...)
2429 {
2430   const char *name;
2431
2432   va_list args;
2433
2434   g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
2435
2436   va_start (args, first_name);
2437
2438   name = first_name;
2439
2440   while (name) {
2441     int arg_id = va_arg (args, int);
2442     int flags = va_arg (args, int);
2443
2444     gst_element_populate_std_props ((GObjectClass *) klass, name, arg_id,
2445         flags);
2446
2447     name = va_arg (args, char *);
2448   }
2449
2450   va_end (args);
2451 }
2452
2453
2454 /**
2455  * gst_buffer_merge:
2456  * @buf1: the first source #GstBuffer to merge.
2457  * @buf2: the second source #GstBuffer to merge.
2458  *
2459  * Create a new buffer that is the concatenation of the two source
2460  * buffers.  The original source buffers will not be modified or
2461  * unref'd.  Make sure you unref the source buffers if they are not used
2462  * anymore afterwards.
2463  *
2464  * If the buffers point to contiguous areas of memory, the buffer
2465  * is created without copying the data.
2466  *
2467  * Returns: the new #GstBuffer which is the concatenation of the source buffers.
2468  */
2469 GstBuffer *
2470 gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2)
2471 {
2472   GstBuffer *result;
2473
2474   /* we're just a specific case of the more general gst_buffer_span() */
2475   result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
2476
2477   return result;
2478 }
2479
2480 /**
2481  * gst_buffer_join:
2482  * @buf1: the first source #GstBuffer.
2483  * @buf2: the second source #GstBuffer.
2484  *
2485  * Create a new buffer that is the concatenation of the two source
2486  * buffers, and unrefs the original source buffers.
2487  *
2488  * If the buffers point to contiguous areas of memory, the buffer
2489  * is created without copying the data.
2490  *
2491  * This is a convenience function for C programmers. See also 
2492  * gst_buffer_merge(), which does the same thing without 
2493  * unreffing the input parameters. Language bindings without 
2494  * explicit reference counting should not wrap this function.
2495  *
2496  * Returns: the new #GstBuffer which is the concatenation of the source buffers.
2497  */
2498 GstBuffer *
2499 gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2)
2500 {
2501   GstBuffer *result;
2502
2503   result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
2504   gst_buffer_unref (buf1);
2505   gst_buffer_unref (buf2);
2506
2507   return result;
2508 }
2509
2510
2511 /**
2512  * gst_buffer_stamp:
2513  * @dest: buffer to stamp
2514  * @src: buffer to stamp from
2515  *
2516  * Copies additional information (the timestamp, duration, and offset start
2517  * and end) from one buffer to the other.
2518  *
2519  * This function does not copy any buffer flags or caps and is equivalent to
2520  * gst_buffer_copy_metadata(@dest, @src, GST_BUFFER_COPY_TIMESTAMPS).
2521  *
2522  * Deprecated: use gst_buffer_copy_metadata() instead, it provides more
2523  * control.
2524  */
2525 #ifndef GST_REMOVE_DEPRECATED
2526 void
2527 gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src)
2528 {
2529   gst_buffer_copy_metadata (dest, src, GST_BUFFER_COPY_TIMESTAMPS);
2530 }
2531 #endif /* GST_REMOVE_DEPRECATED */
2532
2533 static gboolean
2534 intersect_caps_func (GstPad * pad, GValue * ret, GstPad * orig)
2535 {
2536   /* skip the pad, the request came from */
2537   if (pad != orig) {
2538     GstCaps *peercaps, *existing;
2539
2540     existing = g_value_get_pointer (ret);
2541     peercaps = gst_pad_peer_get_caps (pad);
2542     if (peercaps == NULL)
2543       peercaps = gst_caps_new_any ();
2544     g_value_set_pointer (ret, gst_caps_intersect (existing, peercaps));
2545     gst_caps_unref (existing);
2546     gst_caps_unref (peercaps);
2547   }
2548   gst_object_unref (pad);
2549   return TRUE;
2550 }
2551
2552 /**
2553  * gst_pad_proxy_getcaps:
2554  * @pad: a #GstPad to proxy.
2555  *
2556  * Calls gst_pad_get_allowed_caps() for every other pad belonging to the
2557  * same element as @pad, and returns the intersection of the results.
2558  *
2559  * This function is useful as a default getcaps function for an element
2560  * that can handle any stream format, but requires all its pads to have
2561  * the same caps.  Two such elements are tee and aggregator.
2562  *
2563  * Returns: the intersection of the other pads' allowed caps.
2564  */
2565 GstCaps *
2566 gst_pad_proxy_getcaps (GstPad * pad)
2567 {
2568   GstElement *element;
2569   GstCaps *caps, *intersected;
2570   GstIterator *iter;
2571   GstIteratorResult res;
2572   GValue ret = { 0, };
2573
2574   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
2575
2576   GST_CAT_DEBUG (GST_CAT_PADS, "proxying getcaps for %s:%s",
2577       GST_DEBUG_PAD_NAME (pad));
2578
2579   element = gst_pad_get_parent_element (pad);
2580   if (element == NULL)
2581     return NULL;
2582
2583   /* value to hold the return, by default it holds ANY, the ref is taken by
2584    * the GValue. */
2585   g_value_init (&ret, G_TYPE_POINTER);
2586   g_value_set_pointer (&ret, gst_caps_new_any ());
2587
2588   iter = gst_element_iterate_pads (element);
2589   while (1) {
2590     res =
2591         gst_iterator_fold (iter, (GstIteratorFoldFunction) intersect_caps_func,
2592         &ret, pad);
2593     switch (res) {
2594       case GST_ITERATOR_RESYNC:
2595         /* unref any value stored */
2596         if ((caps = g_value_get_pointer (&ret)))
2597           gst_caps_unref (caps);
2598         /* need to reset the result again to ANY */
2599         g_value_set_pointer (&ret, gst_caps_new_any ());
2600         gst_iterator_resync (iter);
2601         break;
2602       case GST_ITERATOR_DONE:
2603         /* all pads iterated, return collected value */
2604         goto done;
2605       default:
2606         /* iterator returned _ERROR or premature end with _OK,
2607          * mark an error and exit */
2608         if ((caps = g_value_get_pointer (&ret)))
2609           gst_caps_unref (caps);
2610         g_value_set_pointer (&ret, NULL);
2611         goto error;
2612     }
2613   }
2614 done:
2615   gst_iterator_free (iter);
2616
2617   gst_object_unref (element);
2618
2619   caps = g_value_get_pointer (&ret);
2620   g_value_unset (&ret);
2621
2622   intersected = gst_caps_intersect (caps, gst_pad_get_pad_template_caps (pad));
2623   gst_caps_unref (caps);
2624
2625   return intersected;
2626
2627   /* ERRORS */
2628 error:
2629   {
2630     g_warning ("Pad list returned error on element %s",
2631         GST_ELEMENT_NAME (element));
2632     gst_iterator_free (iter);
2633     gst_object_unref (element);
2634     return NULL;
2635   }
2636 }
2637
2638 typedef struct
2639 {
2640   GstPad *orig;
2641   GstCaps *caps;
2642 } LinkData;
2643
2644 static gboolean
2645 link_fold_func (GstPad * pad, GValue * ret, LinkData * data)
2646 {
2647   gboolean success = TRUE;
2648
2649   if (pad != data->orig) {
2650     success = gst_pad_set_caps (pad, data->caps);
2651     g_value_set_boolean (ret, success);
2652   }
2653   gst_object_unref (pad);
2654
2655   return success;
2656 }
2657
2658 /**
2659  * gst_pad_proxy_setcaps
2660  * @pad: a #GstPad to proxy from
2661  * @caps: the #GstCaps to link with
2662  *
2663  * Calls gst_pad_set_caps() for every other pad belonging to the
2664  * same element as @pad.  If gst_pad_set_caps() fails on any pad,
2665  * the proxy setcaps fails. May be used only during negotiation.
2666  *
2667  * Returns: TRUE if sucessful
2668  */
2669 gboolean
2670 gst_pad_proxy_setcaps (GstPad * pad, GstCaps * caps)
2671 {
2672   GstElement *element;
2673   GstIterator *iter;
2674   GstIteratorResult res;
2675   GValue ret = { 0, };
2676   LinkData data;
2677
2678   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2679   g_return_val_if_fail (caps != NULL, FALSE);
2680
2681   GST_CAT_DEBUG (GST_CAT_PADS, "proxying pad link for %s:%s",
2682       GST_DEBUG_PAD_NAME (pad));
2683
2684   element = gst_pad_get_parent_element (pad);
2685   if (element == NULL)
2686     return FALSE;
2687
2688   iter = gst_element_iterate_pads (element);
2689
2690   g_value_init (&ret, G_TYPE_BOOLEAN);
2691   g_value_set_boolean (&ret, TRUE);
2692   data.orig = pad;
2693   data.caps = caps;
2694
2695   res = gst_iterator_fold (iter, (GstIteratorFoldFunction) link_fold_func,
2696       &ret, &data);
2697   gst_iterator_free (iter);
2698
2699   if (res != GST_ITERATOR_DONE)
2700     goto pads_changed;
2701
2702   gst_object_unref (element);
2703
2704   /* ok not to unset the gvalue */
2705   return g_value_get_boolean (&ret);
2706
2707   /* ERRORS */
2708 pads_changed:
2709   {
2710     g_warning ("Pad list changed during proxy_pad_link for element %s",
2711         GST_ELEMENT_NAME (element));
2712     gst_object_unref (element);
2713     return FALSE;
2714   }
2715 }
2716
2717 /**
2718  * gst_pad_query_position:
2719  * @pad: a #GstPad to invoke the position query on.
2720  * @format: a pointer to the #GstFormat asked for.
2721  *          On return contains the #GstFormat used.
2722  * @cur: A location in which to store the current position, or NULL.
2723  *
2724  * Queries a pad for the stream position.
2725  *
2726  * Returns: TRUE if the query could be performed.
2727  */
2728 gboolean
2729 gst_pad_query_position (GstPad * pad, GstFormat * format, gint64 * cur)
2730 {
2731   GstQuery *query;
2732   gboolean ret;
2733
2734   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2735   g_return_val_if_fail (format != NULL, FALSE);
2736
2737   query = gst_query_new_position (*format);
2738   ret = gst_pad_query (pad, query);
2739
2740   if (ret)
2741     gst_query_parse_position (query, format, cur);
2742
2743   gst_query_unref (query);
2744
2745   return ret;
2746 }
2747
2748 /**
2749  * gst_pad_query_peer_position:
2750  * @pad: a #GstPad on whose peer to invoke the position query on.
2751  *       Must be a sink pad.
2752  * @format: a pointer to the #GstFormat asked for.
2753  *          On return contains the #GstFormat used.
2754  * @cur: A location in which to store the current position, or NULL.
2755  *
2756  * Queries the peer of a given sink pad for the stream position.
2757  *
2758  * Returns: TRUE if the query could be performed.
2759  */
2760 gboolean
2761 gst_pad_query_peer_position (GstPad * pad, GstFormat * format, gint64 * cur)
2762 {
2763   gboolean ret = FALSE;
2764   GstPad *peer;
2765
2766   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2767   g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2768   g_return_val_if_fail (format != NULL, FALSE);
2769
2770   peer = gst_pad_get_peer (pad);
2771   if (peer) {
2772     ret = gst_pad_query_position (peer, format, cur);
2773     gst_object_unref (peer);
2774   }
2775
2776   return ret;
2777 }
2778
2779 /**
2780  * gst_pad_query_duration:
2781  * @pad: a #GstPad to invoke the duration query on.
2782  * @format: a pointer to the #GstFormat asked for.
2783  *          On return contains the #GstFormat used.
2784  * @duration: A location in which to store the total duration, or NULL.
2785  *
2786  * Queries a pad for the total stream duration.
2787  *
2788  * Returns: TRUE if the query could be performed.
2789  */
2790 gboolean
2791 gst_pad_query_duration (GstPad * pad, GstFormat * format, gint64 * duration)
2792 {
2793   GstQuery *query;
2794   gboolean ret;
2795
2796   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2797   g_return_val_if_fail (format != NULL, FALSE);
2798
2799   query = gst_query_new_duration (*format);
2800   ret = gst_pad_query (pad, query);
2801
2802   if (ret)
2803     gst_query_parse_duration (query, format, duration);
2804
2805   gst_query_unref (query);
2806
2807   return ret;
2808 }
2809
2810 /**
2811  * gst_pad_query_peer_duration:
2812  * @pad: a #GstPad on whose peer pad to invoke the duration query on.
2813  *       Must be a sink pad.
2814  * @format: a pointer to the #GstFormat asked for.
2815  *          On return contains the #GstFormat used.
2816  * @duration: A location in which to store the total duration, or NULL.
2817  *
2818  * Queries the peer pad of a given sink pad for the total stream duration.
2819  *
2820  * Returns: TRUE if the query could be performed.
2821  */
2822 gboolean
2823 gst_pad_query_peer_duration (GstPad * pad, GstFormat * format,
2824     gint64 * duration)
2825 {
2826   gboolean ret = FALSE;
2827   GstPad *peer;
2828
2829   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2830   g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2831   g_return_val_if_fail (format != NULL, FALSE);
2832
2833   peer = gst_pad_get_peer (pad);
2834   if (peer) {
2835     ret = gst_pad_query_duration (peer, format, duration);
2836     gst_object_unref (peer);
2837   }
2838
2839   return ret;
2840 }
2841
2842 /**
2843  * gst_pad_query_convert:
2844  * @pad: a #GstPad to invoke the convert query on.
2845  * @src_format: a #GstFormat to convert from.
2846  * @src_val: a value to convert.
2847  * @dest_format: a pointer to the #GstFormat to convert to.
2848  * @dest_val: a pointer to the result.
2849  *
2850  * Queries a pad to convert @src_val in @src_format to @dest_format.
2851  *
2852  * Returns: TRUE if the query could be performed.
2853  */
2854 gboolean
2855 gst_pad_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2856     GstFormat * dest_format, gint64 * dest_val)
2857 {
2858   GstQuery *query;
2859   gboolean ret;
2860
2861   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2862   g_return_val_if_fail (dest_format != NULL, FALSE);
2863   g_return_val_if_fail (dest_val != NULL, FALSE);
2864
2865   if (*dest_format == src_format) {
2866     *dest_val = src_val;
2867     return TRUE;
2868   }
2869
2870   query = gst_query_new_convert (src_format, src_val, *dest_format);
2871   ret = gst_pad_query (pad, query);
2872
2873   if (ret)
2874     gst_query_parse_convert (query, NULL, NULL, dest_format, dest_val);
2875
2876   gst_query_unref (query);
2877
2878   return ret;
2879 }
2880
2881 /**
2882  * gst_pad_query_peer_convert:
2883  * @pad: a #GstPad, on whose peer pad to invoke the convert query on.
2884  *       Must be a sink pad.
2885  * @src_format: a #GstFormat to convert from.
2886  * @src_val: a value to convert.
2887  * @dest_format: a pointer to the #GstFormat to convert to.
2888  * @dest_val: a pointer to the result.
2889  *
2890  * Queries the peer pad of a given sink pad to convert @src_val in @src_format
2891  * to @dest_format.
2892  *
2893  * Returns: TRUE if the query could be performed.
2894  */
2895 gboolean
2896 gst_pad_query_peer_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
2897     GstFormat * dest_format, gint64 * dest_val)
2898 {
2899   gboolean ret = FALSE;
2900   GstPad *peer;
2901
2902   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
2903   g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
2904   g_return_val_if_fail (src_val >= 0, FALSE);
2905   g_return_val_if_fail (dest_format != NULL, FALSE);
2906   g_return_val_if_fail (dest_val != NULL, FALSE);
2907
2908   peer = gst_pad_get_peer (pad);
2909   if (peer) {
2910     ret = gst_pad_query_convert (peer, src_format, src_val, dest_format,
2911         dest_val);
2912     gst_object_unref (peer);
2913   }
2914
2915   return ret;
2916 }
2917
2918 /**
2919  * gst_atomic_int_set:
2920  * @atomic_int: pointer to an atomic integer
2921  * @value: value to set
2922  *
2923  * Unconditionally sets the atomic integer to @value.
2924  * 
2925  * Deprecated: Use g_atomic_int_set().
2926  *
2927  */
2928 #ifndef GST_REMOVE_DEPRECATED
2929 void
2930 gst_atomic_int_set (gint * atomic_int, gint value)
2931 {
2932   g_atomic_int_set (atomic_int, value);
2933 }
2934 #endif
2935
2936 /**
2937  * gst_pad_add_data_probe:
2938  * @pad: pad to add the data probe handler to
2939  * @handler: function to call when data is passed over pad
2940  * @data: data to pass along with the handler
2941  *
2942  * Adds a "data probe" to a pad. This function will be called whenever data
2943  * passes through a pad. In this case data means both events and buffers. The
2944  * probe will be called with the data as an argument, meaning @handler should
2945  * have the same callback signature as the #GstPad::have-data signal.
2946  * Note that the data will have a reference count greater than 1, so it will
2947  * be immutable -- you must not change it.
2948  *
2949  * For source pads, the probe will be called after the blocking function, if any
2950  * (see gst_pad_set_blocked_async()), but before looking up the peer to chain
2951  * to. For sink pads, the probe function will be called before configuring the
2952  * sink with new caps, if any, and before calling the pad's chain function.
2953  *
2954  * Your data probe should return TRUE to let the data continue to flow, or FALSE
2955  * to drop it. Dropping data is rarely useful, but occasionally comes in handy
2956  * with events.
2957  *
2958  * Although probes are implemented internally by connecting @handler to the
2959  * have-data signal on the pad, if you want to remove a probe it is insufficient
2960  * to only call g_signal_handler_disconnect on the returned handler id. To
2961  * remove a probe, use the appropriate function, such as
2962  * gst_pad_remove_data_probe().
2963  *
2964  * Returns: The handler id.
2965  */
2966 gulong
2967 gst_pad_add_data_probe (GstPad * pad, GCallback handler, gpointer data)
2968 {
2969   return gst_pad_add_data_probe_full (pad, handler, data, NULL);
2970 }
2971
2972 /**
2973  * gst_pad_add_data_probe_full:
2974  * @pad: pad to add the data probe handler to
2975  * @handler: function to call when data is passed over pad
2976  * @data: data to pass along with the handler
2977  * @notify: function to call when the probe is disconnected, or NULL
2978  *
2979  * Adds a "data probe" to a pad. This function will be called whenever data
2980  * passes through a pad. In this case data means both events and buffers. The
2981  * probe will be called with the data as an argument, meaning @handler should
2982  * have the same callback signature as the #GstPad::have-data signal.
2983  * Note that the data will have a reference count greater than 1, so it will
2984  * be immutable -- you must not change it.
2985  *
2986  * For source pads, the probe will be called after the blocking function, if any
2987  * (see gst_pad_set_blocked_async()), but before looking up the peer to chain
2988  * to. For sink pads, the probe function will be called before configuring the
2989  * sink with new caps, if any, and before calling the pad's chain function.
2990  *
2991  * Your data probe should return TRUE to let the data continue to flow, or FALSE
2992  * to drop it. Dropping data is rarely useful, but occasionally comes in handy
2993  * with events.
2994  *
2995  * Although probes are implemented internally by connecting @handler to the
2996  * have-data signal on the pad, if you want to remove a probe it is insufficient
2997  * to only call g_signal_handler_disconnect on the returned handler id. To
2998  * remove a probe, use the appropriate function, such as
2999  * gst_pad_remove_data_probe().
3000  *
3001  * The @notify function is called when the probe is disconnected and usually
3002  * used to free @data.
3003  *
3004  * Returns: The handler id.
3005  *
3006  * Since: 0.10.20
3007  */
3008 gulong
3009 gst_pad_add_data_probe_full (GstPad * pad, GCallback handler,
3010     gpointer data, GDestroyNotify notify)
3011 {
3012   gulong sigid;
3013
3014   g_return_val_if_fail (GST_IS_PAD (pad), 0);
3015   g_return_val_if_fail (handler != NULL, 0);
3016
3017   GST_OBJECT_LOCK (pad);
3018
3019   /* we only expose a GDestroyNotify in our API because that's less confusing */
3020   sigid = g_signal_connect_data (pad, "have-data", handler, data,
3021       (GClosureNotify) notify, 0);
3022
3023   GST_PAD_DO_EVENT_SIGNALS (pad)++;
3024   GST_PAD_DO_BUFFER_SIGNALS (pad)++;
3025   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3026       "adding data probe, now %d data, %d event probes",
3027       GST_PAD_DO_BUFFER_SIGNALS (pad), GST_PAD_DO_EVENT_SIGNALS (pad));
3028   GST_OBJECT_UNLOCK (pad);
3029
3030   return sigid;
3031 }
3032
3033 /**
3034  * gst_pad_add_event_probe:
3035  * @pad: pad to add the event probe handler to
3036  * @handler: function to call when events are passed over pad
3037  * @data: data to pass along with the handler
3038  *
3039  * Adds a probe that will be called for all events passing through a pad. See
3040  * gst_pad_add_data_probe() for more information.
3041  *
3042  * Returns: The handler id
3043  */
3044 gulong
3045 gst_pad_add_event_probe (GstPad * pad, GCallback handler, gpointer data)
3046 {
3047   return gst_pad_add_event_probe_full (pad, handler, data, NULL);
3048 }
3049
3050 /**
3051  * gst_pad_add_event_probe_full:
3052  * @pad: pad to add the event probe handler to
3053  * @handler: function to call when events are passed over pad
3054  * @data: data to pass along with the handler, or NULL
3055  * @notify: function to call when probe is disconnected, or NULL
3056  *
3057  * Adds a probe that will be called for all events passing through a pad. See
3058  * gst_pad_add_data_probe() for more information.
3059  *
3060  * The @notify function is called when the probe is disconnected and usually
3061  * used to free @data.
3062  *
3063  * Returns: The handler id
3064  *
3065  * Since: 0.10.20
3066  */
3067 gulong
3068 gst_pad_add_event_probe_full (GstPad * pad, GCallback handler,
3069     gpointer data, GDestroyNotify notify)
3070 {
3071   gulong sigid;
3072
3073   g_return_val_if_fail (GST_IS_PAD (pad), 0);
3074   g_return_val_if_fail (handler != NULL, 0);
3075
3076   GST_OBJECT_LOCK (pad);
3077
3078   /* we only expose a GDestroyNotify in our API because that's less confusing */
3079   sigid = g_signal_connect_data (pad, "have-data::event", handler, data,
3080       (GClosureNotify) notify, 0);
3081
3082   GST_PAD_DO_EVENT_SIGNALS (pad)++;
3083   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "adding event probe, now %d probes",
3084       GST_PAD_DO_EVENT_SIGNALS (pad));
3085   GST_OBJECT_UNLOCK (pad);
3086
3087   return sigid;
3088 }
3089
3090 /**
3091  * gst_pad_add_buffer_probe:
3092  * @pad: pad to add the buffer probe handler to
3093  * @handler: function to call when buffers are passed over pad
3094  * @data: data to pass along with the handler
3095  *
3096  * Adds a probe that will be called for all buffers passing through a pad. See
3097  * gst_pad_add_data_probe() for more information.
3098  *
3099  * Returns: The handler id
3100  */
3101 gulong
3102 gst_pad_add_buffer_probe (GstPad * pad, GCallback handler, gpointer data)
3103 {
3104   return gst_pad_add_buffer_probe_full (pad, handler, data, NULL);
3105 }
3106
3107 /**
3108  * gst_pad_add_buffer_probe_full:
3109  * @pad: pad to add the buffer probe handler to
3110  * @handler: function to call when buffer are passed over pad
3111  * @data: data to pass along with the handler
3112  * @notify: function to call when the probe is disconnected, or NULL
3113  *
3114  * Adds a probe that will be called for all buffers passing through a pad. See
3115  * gst_pad_add_data_probe() for more information.
3116  *
3117  * The @notify function is called when the probe is disconnected and usually
3118  * used to free @data.
3119  *
3120  * Returns: The handler id
3121  *
3122  * Since: 0.10.20
3123  */
3124 gulong
3125 gst_pad_add_buffer_probe_full (GstPad * pad, GCallback handler,
3126     gpointer data, GDestroyNotify notify)
3127 {
3128   gulong sigid;
3129
3130   g_return_val_if_fail (GST_IS_PAD (pad), 0);
3131   g_return_val_if_fail (handler != NULL, 0);
3132
3133   GST_OBJECT_LOCK (pad);
3134
3135   /* we only expose a GDestroyNotify in our API because that's less confusing */
3136   sigid = g_signal_connect_data (pad, "have-data::buffer", handler, data,
3137       (GClosureNotify) notify, 0);
3138
3139   GST_PAD_DO_BUFFER_SIGNALS (pad)++;
3140   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad, "adding buffer probe, now %d probes",
3141       GST_PAD_DO_BUFFER_SIGNALS (pad));
3142   GST_OBJECT_UNLOCK (pad);
3143
3144   return sigid;
3145 }
3146
3147 /**
3148  * gst_pad_remove_data_probe:
3149  * @pad: pad to remove the data probe handler from
3150  * @handler_id: handler id returned from gst_pad_add_data_probe
3151  *
3152  * Removes a data probe from @pad.
3153  */
3154 void
3155 gst_pad_remove_data_probe (GstPad * pad, guint handler_id)
3156 {
3157   g_return_if_fail (GST_IS_PAD (pad));
3158   g_return_if_fail (handler_id > 0);
3159
3160   GST_OBJECT_LOCK (pad);
3161   g_signal_handler_disconnect (pad, handler_id);
3162   GST_PAD_DO_BUFFER_SIGNALS (pad)--;
3163   GST_PAD_DO_EVENT_SIGNALS (pad)--;
3164   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3165       "removed data probe, now %d event, %d buffer probes",
3166       GST_PAD_DO_EVENT_SIGNALS (pad), GST_PAD_DO_BUFFER_SIGNALS (pad));
3167   GST_OBJECT_UNLOCK (pad);
3168
3169 }
3170
3171 /**
3172  * gst_pad_remove_event_probe:
3173  * @pad: pad to remove the event probe handler from
3174  * @handler_id: handler id returned from gst_pad_add_event_probe
3175  *
3176  * Removes an event probe from @pad.
3177  */
3178 void
3179 gst_pad_remove_event_probe (GstPad * pad, guint handler_id)
3180 {
3181   g_return_if_fail (GST_IS_PAD (pad));
3182   g_return_if_fail (handler_id > 0);
3183
3184   GST_OBJECT_LOCK (pad);
3185   g_signal_handler_disconnect (pad, handler_id);
3186   GST_PAD_DO_EVENT_SIGNALS (pad)--;
3187   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3188       "removed event probe, now %d event probes",
3189       GST_PAD_DO_EVENT_SIGNALS (pad));
3190   GST_OBJECT_UNLOCK (pad);
3191 }
3192
3193 /**
3194  * gst_pad_remove_buffer_probe:
3195  * @pad: pad to remove the buffer probe handler from
3196  * @handler_id: handler id returned from gst_pad_add_buffer_probe
3197  *
3198  * Removes a buffer probe from @pad.
3199  */
3200 void
3201 gst_pad_remove_buffer_probe (GstPad * pad, guint handler_id)
3202 {
3203   g_return_if_fail (GST_IS_PAD (pad));
3204   g_return_if_fail (handler_id > 0);
3205
3206   GST_OBJECT_LOCK (pad);
3207   g_signal_handler_disconnect (pad, handler_id);
3208   GST_PAD_DO_BUFFER_SIGNALS (pad)--;
3209   GST_CAT_DEBUG_OBJECT (GST_CAT_PADS, pad,
3210       "removed buffer probe, now %d buffer probes",
3211       GST_PAD_DO_BUFFER_SIGNALS (pad));
3212   GST_OBJECT_UNLOCK (pad);
3213
3214 }
3215
3216 /**
3217  * gst_element_found_tags_for_pad:
3218  * @element: element for which to post taglist to bus.
3219  * @pad: pad on which to push tag-event.
3220  * @list: the taglist to post on the bus and create event from.
3221  *
3222  * Posts a message to the bus that new tags were found and pushes the
3223  * tags as event. Takes ownership of the @list.
3224  *
3225  * This is a utility method for elements. Applications should use the
3226  * #GstTagSetter interface.
3227  */
3228 void
3229 gst_element_found_tags_for_pad (GstElement * element,
3230     GstPad * pad, GstTagList * list)
3231 {
3232   g_return_if_fail (element != NULL);
3233   g_return_if_fail (pad != NULL);
3234   g_return_if_fail (list != NULL);
3235
3236   gst_pad_push_event (pad, gst_event_new_tag (gst_tag_list_copy (list)));
3237   /* FIXME 0.11: Set the pad as source to make it possible to detect for
3238    * which pad the tags are actually found.
3239    */
3240   gst_element_post_message (element,
3241       gst_message_new_tag (GST_OBJECT (element), list));
3242 }
3243
3244 static void
3245 push_and_ref (GstPad * pad, GstEvent * event)
3246 {
3247   gst_pad_push_event (pad, gst_event_ref (event));
3248   /* iterator refs pad, we unref when we are done with it */
3249   gst_object_unref (pad);
3250 }
3251
3252 /**
3253  * gst_element_found_tags:
3254  * @element: element for which we found the tags.
3255  * @list: list of tags.
3256  *
3257  * Posts a message to the bus that new tags were found, and pushes an event
3258  * to all sourcepads. Takes ownership of the @list.
3259  *
3260  * This is a utility method for elements. Applications should use the
3261  * #GstTagSetter interface.
3262  */
3263 void
3264 gst_element_found_tags (GstElement * element, GstTagList * list)
3265 {
3266   GstIterator *iter;
3267   GstEvent *event;
3268
3269   g_return_if_fail (element != NULL);
3270   g_return_if_fail (list != NULL);
3271
3272   iter = gst_element_iterate_src_pads (element);
3273   event = gst_event_new_tag (gst_tag_list_copy (list));
3274   gst_iterator_foreach (iter, (GFunc) push_and_ref, event);
3275   gst_iterator_free (iter);
3276   gst_event_unref (event);
3277
3278   gst_element_post_message (element,
3279       gst_message_new_tag (GST_OBJECT (element), list));
3280 }
3281
3282 static GstPad *
3283 element_find_unlinked_pad (GstElement * element, GstPadDirection direction)
3284 {
3285   GstIterator *iter;
3286   GstPad *unlinked_pad = NULL;
3287   gboolean done;
3288
3289   switch (direction) {
3290     case GST_PAD_SRC:
3291       iter = gst_element_iterate_src_pads (element);
3292       break;
3293     case GST_PAD_SINK:
3294       iter = gst_element_iterate_sink_pads (element);
3295       break;
3296     default:
3297       g_return_val_if_reached (NULL);
3298   }
3299
3300   done = FALSE;
3301   while (!done) {
3302     gpointer pad;
3303
3304     switch (gst_iterator_next (iter, &pad)) {
3305       case GST_ITERATOR_OK:{
3306         GstPad *peer;
3307
3308         GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
3309             GST_DEBUG_PAD_NAME (pad));
3310
3311         peer = gst_pad_get_peer (GST_PAD (pad));
3312         if (peer == NULL) {
3313           unlinked_pad = pad;
3314           done = TRUE;
3315           GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
3316               "found existing unlinked pad %s:%s",
3317               GST_DEBUG_PAD_NAME (unlinked_pad));
3318         } else {
3319           gst_object_unref (pad);
3320           gst_object_unref (peer);
3321         }
3322         break;
3323       }
3324       case GST_ITERATOR_DONE:
3325         done = TRUE;
3326         break;
3327       case GST_ITERATOR_RESYNC:
3328         gst_iterator_resync (iter);
3329         break;
3330       case GST_ITERATOR_ERROR:
3331         g_return_val_if_reached (NULL);
3332         break;
3333     }
3334   }
3335
3336   gst_iterator_free (iter);
3337
3338   return unlinked_pad;
3339 }
3340
3341 /**
3342  * gst_bin_find_unlinked_pad:
3343  * @bin: bin in which to look for elements with unlinked pads
3344  * @direction: whether to look for an unlinked source or sink pad
3345  *
3346  * Recursively looks for elements with an unlinked pad of the given
3347  * direction within the specified bin and returns an unlinked pad
3348  * if one is found, or NULL otherwise. If a pad is found, the caller
3349  * owns a reference to it and should use gst_object_unref() on the
3350  * pad when it is not needed any longer.
3351  *
3352  * Returns: unlinked pad of the given direction, or NULL.
3353  *
3354  * Since: 0.10.20
3355  */
3356 GstPad *
3357 gst_bin_find_unlinked_pad (GstBin * bin, GstPadDirection direction)
3358 {
3359   GstIterator *iter;
3360   gboolean done;
3361   GstPad *pad = NULL;
3362
3363   g_return_val_if_fail (GST_IS_BIN (bin), NULL);
3364   g_return_val_if_fail (direction != GST_PAD_UNKNOWN, NULL);
3365
3366   done = FALSE;
3367   iter = gst_bin_iterate_recurse (bin);
3368   while (!done) {
3369     gpointer element;
3370
3371     switch (gst_iterator_next (iter, &element)) {
3372       case GST_ITERATOR_OK:
3373         pad = element_find_unlinked_pad (GST_ELEMENT (element), direction);
3374         gst_object_unref (element);
3375         if (pad != NULL)
3376           done = TRUE;
3377         break;
3378       case GST_ITERATOR_DONE:
3379         done = TRUE;
3380         break;
3381       case GST_ITERATOR_RESYNC:
3382         gst_iterator_resync (iter);
3383         break;
3384       case GST_ITERATOR_ERROR:
3385         g_return_val_if_reached (NULL);
3386         break;
3387     }
3388   }
3389
3390   gst_iterator_free (iter);
3391
3392   return pad;
3393 }
3394
3395 /**
3396  * gst_bin_find_unconnected_pad:
3397  * @bin: bin in which to look for elements with unlinked pads
3398  * @direction: whether to look for an unlinked source or sink pad
3399  *
3400  * Recursively looks for elements with an unlinked pad of the given
3401  * direction within the specified bin and returns an unlinked pad
3402  * if one is found, or NULL otherwise. If a pad is found, the caller
3403  * owns a reference to it and should use gst_object_unref() on the
3404  * pad when it is not needed any longer.
3405  *
3406  * Returns: unlinked pad of the given direction, or NULL.
3407  *
3408  * Since: 0.10.3
3409  *
3410  * Deprecated: use gst_bin_find_unlinked_pad() instead.
3411  */
3412 #ifndef GST_REMOVE_DEPRECATED
3413 GstPad *
3414 gst_bin_find_unconnected_pad (GstBin * bin, GstPadDirection direction)
3415 {
3416   return gst_bin_find_unlinked_pad (bin, direction);
3417 }
3418 #endif
3419
3420 /**
3421  * gst_parse_bin_from_description:
3422  * @bin_description: command line describing the bin
3423  * @ghost_unlinked_pads: whether to automatically create ghost pads
3424  *     for unlinked source or sink pads within the bin
3425  * @err: where to store the error message in case of an error, or NULL
3426  *
3427  * This is a convenience wrapper around gst_parse_launch() to create a
3428  * #GstBin from a gst-launch-style pipeline description. See
3429  * gst_parse_launch() and the gst-launch man page for details about the
3430  * syntax. Ghost pads on the bin for unlinked source or sink pads
3431  * within the bin can automatically be created (but only a maximum of
3432  * one ghost pad for each direction will be created; if you expect
3433  * multiple unlinked source pads or multiple unlinked sink pads
3434  * and want them all ghosted, you will have to create the ghost pads
3435  * yourself).
3436  *
3437  * Returns: a newly-created bin, or NULL if an error occurred.
3438  *
3439  * Since: 0.10.3
3440  */
3441 GstElement *
3442 gst_parse_bin_from_description (const gchar * bin_description,
3443     gboolean ghost_unlinked_pads, GError ** err)
3444 {
3445   return gst_parse_bin_from_description_full (bin_description,
3446       ghost_unlinked_pads, NULL, 0, err);
3447 }
3448
3449 /**
3450  * gst_parse_bin_from_description_full:
3451  * @bin_description: command line describing the bin
3452  * @ghost_unlinked_pads: whether to automatically create ghost pads
3453  *     for unlinked source or sink pads within the bin
3454  * @context: a parse context allocated with gst_parse_context_new(), or %NULL
3455  * @flags: parsing options, or #GST_PARSE_FLAG_NONE
3456  * @err: where to store the error message in case of an error, or NULL
3457  *
3458  * This is a convenience wrapper around gst_parse_launch() to create a
3459  * #GstBin from a gst-launch-style pipeline description. See
3460  * gst_parse_launch() and the gst-launch man page for details about the
3461  * syntax. Ghost pads on the bin for unlinked source or sink pads
3462  * within the bin can automatically be created (but only a maximum of
3463  * one ghost pad for each direction will be created; if you expect
3464  * multiple unlinked source pads or multiple unlinked sink pads
3465  * and want them all ghosted, you will have to create the ghost pads
3466  * yourself).
3467  *
3468  * Returns: a newly-created bin, or NULL if an error occurred.
3469  *
3470  * Since: 0.10.20
3471  */
3472 GstElement *
3473 gst_parse_bin_from_description_full (const gchar * bin_description,
3474     gboolean ghost_unlinked_pads, GstParseContext * context,
3475     GstParseFlags flags, GError ** err)
3476 {
3477 #ifndef GST_DISABLE_PARSE
3478   GstPad *pad = NULL;
3479   GstBin *bin;
3480   gchar *desc;
3481
3482   g_return_val_if_fail (bin_description != NULL, NULL);
3483   g_return_val_if_fail (err == NULL || *err == NULL, NULL);
3484
3485   GST_DEBUG ("Making bin from description '%s'", bin_description);
3486
3487   /* parse the pipeline to a bin */
3488   desc = g_strdup_printf ("bin.( %s )", bin_description);
3489   bin = (GstBin *) gst_parse_launch_full (desc, context, flags, err);
3490   g_free (desc);
3491
3492   if (bin == NULL || (err && *err != NULL)) {
3493     if (bin)
3494       gst_object_unref (bin);
3495     return NULL;
3496   }
3497
3498   /* find pads and ghost them if necessary */
3499   if (ghost_unlinked_pads) {
3500     if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SRC))) {
3501       gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("src", pad));
3502       gst_object_unref (pad);
3503     }
3504     if ((pad = gst_bin_find_unlinked_pad (bin, GST_PAD_SINK))) {
3505       gst_element_add_pad (GST_ELEMENT (bin), gst_ghost_pad_new ("sink", pad));
3506       gst_object_unref (pad);
3507     }
3508   }
3509
3510   return GST_ELEMENT (bin);
3511 #else
3512   gchar *msg;
3513
3514   GST_WARNING ("Disabled API called");
3515
3516   msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
3517   g_set_error (err, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
3518   g_free (msg);
3519
3520   return NULL;
3521 #endif
3522 }
3523
3524 /**
3525  * gst_type_register_static_full:
3526  * @parent_type: The GType of the parent type the newly registered type will 
3527  *   derive from
3528  * @type_name: NULL-terminated string used as the name of the new type
3529  * @class_size: Size of the class structure.
3530  * @base_init: Location of the base initialization function (optional).
3531  * @base_finalize: Location of the base finalization function (optional).
3532  * @class_init: Location of the class initialization function for class types 
3533  *   Location of the default vtable inititalization function for interface 
3534  *   types. (optional)
3535  * @class_finalize: Location of the class finalization function for class types.
3536  *   Location of the default vtable finalization function for interface types. 
3537  *   (optional)
3538  * @class_data: User-supplied data passed to the class init/finalize functions.
3539  * @instance_size: Size of the instance (object) structure (required for 
3540  *   instantiatable types only).
3541  * @n_preallocs: The number of pre-allocated (cached) instances to reserve 
3542  *   memory for (0 indicates no caching). Ignored on recent GLib's.
3543  * @instance_init: Location of the instance initialization function (optional, 
3544  *   for instantiatable types only).
3545  * @value_table: A GTypeValueTable function table for generic handling of 
3546  *   GValues of this type (usually only useful for fundamental types). 
3547  * @flags: #GTypeFlags for this GType. E.g: G_TYPE_FLAG_ABSTRACT
3548  *
3549  * Helper function which constructs a #GTypeInfo structure and registers a 
3550  * GType, but which generates less linker overhead than a static const 
3551  * #GTypeInfo structure. For further details of the parameters, please see
3552  * #GTypeInfo in the GLib documentation.
3553  *
3554  * Registers type_name as the name of a new static type derived from 
3555  * parent_type. The value of flags determines the nature (e.g. abstract or 
3556  * not) of the type. It works by filling a GTypeInfo struct and calling 
3557  * g_type_info_register_static().
3558  *
3559  * Returns: A #GType for the newly-registered type.
3560  *
3561  * Since: 0.10.14
3562  */
3563 GType
3564 gst_type_register_static_full (GType parent_type,
3565     const gchar * type_name,
3566     guint class_size,
3567     GBaseInitFunc base_init,
3568     GBaseFinalizeFunc base_finalize,
3569     GClassInitFunc class_init,
3570     GClassFinalizeFunc class_finalize,
3571     gconstpointer class_data,
3572     guint instance_size,
3573     guint16 n_preallocs,
3574     GInstanceInitFunc instance_init,
3575     const GTypeValueTable * value_table, GTypeFlags flags)
3576 {
3577   GTypeInfo info;
3578
3579   info.class_size = class_size;
3580   info.base_init = base_init;
3581   info.base_finalize = base_finalize;
3582   info.class_init = class_init;
3583   info.class_finalize = class_finalize;
3584   info.class_data = class_data;
3585   info.instance_size = instance_size;
3586   info.n_preallocs = n_preallocs;
3587   info.instance_init = instance_init;
3588   info.value_table = value_table;
3589
3590   return g_type_register_static (parent_type, type_name, &info, flags);
3591 }
3592
3593
3594 /**
3595  * gst_util_get_timestamp:
3596  *
3597  * Get a timestamp as GstClockTime to be used for interval meassurements.
3598  * The timestamp should not be interpreted in any other way.
3599  *
3600  * Returns: the timestamp
3601  *
3602  * Since: 0.10.16
3603  */
3604 GstClockTime
3605 gst_util_get_timestamp (void)
3606 {
3607 #if defined (HAVE_POSIX_TIMERS) && defined(HAVE_MONOTONIC_CLOCK)
3608   struct timespec now;
3609
3610   clock_gettime (CLOCK_MONOTONIC, &now);
3611   return GST_TIMESPEC_TO_TIME (now);
3612 #else
3613   GTimeVal now;
3614
3615   g_get_current_time (&now);
3616   return GST_TIMEVAL_TO_TIME (now);
3617 #endif
3618 }