6dfc1029fdb7b4147bcfe86b7d1420a6ef408075
[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  *
5  * gstutils.c: Utility functions: gtk_get_property stuff, etc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "gst_private.h"
27 #include "gstghostpad.h"
28 #include "gstutils.h"
29 #include "gsturitype.h"
30 #include "gstinfo.h"
31 #include "gst-i18n-lib.h"
32
33
34 /**
35  * gst_util_dump_mem:
36  * @mem: a pointer to the memory to dump
37  * @size: the size of the memory block to dump
38  *
39  * Dumps the memory block into a hex representation. Useful for debugging.
40  */
41 void
42 gst_util_dump_mem (const guchar * mem, guint size)
43 {
44   guint i, j;
45   GString *string = g_string_sized_new (50);
46   GString *chars = g_string_sized_new (18);
47
48   i = j = 0;
49   while (i < size) {
50     if (g_ascii_isprint (mem[i]))
51       g_string_append_printf (chars, "%c", mem[i]);
52     else
53       g_string_append_printf (chars, ".");
54
55     g_string_append_printf (string, "%02x ", mem[i]);
56
57     j++;
58     i++;
59
60     if (j == 16 || i == size) {
61       g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j, mem + i - j,
62           string->str, chars->str);
63       g_string_set_size (string, 0);
64       g_string_set_size (chars, 0);
65       j = 0;
66     }
67   }
68   g_string_free (string, TRUE);
69   g_string_free (chars, TRUE);
70 }
71
72
73 /**
74  * gst_util_set_value_from_string:
75  * @value: the value to set
76  * @value_str: the string to get the value from
77  *
78  * Converts the string to the type of the value and
79  * sets the value with it.
80  */
81 void
82 gst_util_set_value_from_string (GValue * value, const gchar * value_str)
83 {
84
85   g_return_if_fail (value != NULL);
86   g_return_if_fail (value_str != NULL);
87
88   GST_CAT_DEBUG (GST_CAT_PARAMS, "parsing '%s' to type %s", value_str,
89       g_type_name (G_VALUE_TYPE (value)));
90
91   switch (G_VALUE_TYPE (value)) {
92     case G_TYPE_STRING:
93       g_value_set_string (value, g_strdup (value_str));
94       break;
95     case G_TYPE_ENUM:
96     case G_TYPE_INT:{
97       gint i;
98
99       sscanf (value_str, "%d", &i);
100       g_value_set_int (value, i);
101       break;
102     }
103     case G_TYPE_UINT:{
104       guint i;
105
106       sscanf (value_str, "%u", &i);
107       g_value_set_uint (value, i);
108       break;
109     }
110     case G_TYPE_LONG:{
111       glong i;
112
113       sscanf (value_str, "%ld", &i);
114       g_value_set_long (value, i);
115       break;
116     }
117     case G_TYPE_ULONG:{
118       gulong i;
119
120       sscanf (value_str, "%lu", &i);
121       g_value_set_ulong (value, i);
122       break;
123     }
124     case G_TYPE_BOOLEAN:{
125       gboolean i = FALSE;
126
127       if (!strncmp ("true", value_str, 4))
128         i = TRUE;
129       g_value_set_boolean (value, i);
130       break;
131     }
132     case G_TYPE_CHAR:{
133       gchar i;
134
135       sscanf (value_str, "%c", &i);
136       g_value_set_char (value, i);
137       break;
138     }
139     case G_TYPE_UCHAR:{
140       guchar i;
141
142       sscanf (value_str, "%c", &i);
143       g_value_set_uchar (value, i);
144       break;
145     }
146     case G_TYPE_FLOAT:{
147       gfloat i;
148
149       sscanf (value_str, "%f", &i);
150       g_value_set_float (value, i);
151       break;
152     }
153     case G_TYPE_DOUBLE:{
154       gfloat i;
155
156       sscanf (value_str, "%g", &i);
157       g_value_set_double (value, (gdouble) i);
158       break;
159     }
160     default:
161       break;
162   }
163 }
164
165 /**
166  * gst_util_set_object_arg:
167  * @object: the object to set the argument of
168  * @name: the name of the argument to set
169  * @value: the string value to set
170  *
171  * Convertes the string value to the type of the objects argument and
172  * sets the argument with it.
173  */
174 void
175 gst_util_set_object_arg (GObject * object, const gchar * name,
176     const gchar * value)
177 {
178   if (name && value) {
179     GParamSpec *paramspec;
180
181     paramspec =
182         g_object_class_find_property (G_OBJECT_GET_CLASS (object), name);
183
184     if (!paramspec) {
185       return;
186     }
187
188     GST_DEBUG ("paramspec->flags is %d, paramspec->value_type is %d",
189         paramspec->flags, (gint) paramspec->value_type);
190
191     if (paramspec->flags & G_PARAM_WRITABLE) {
192       switch (paramspec->value_type) {
193         case G_TYPE_STRING:
194           g_object_set (G_OBJECT (object), name, value, NULL);
195           break;
196         case G_TYPE_ENUM:
197         case G_TYPE_INT:{
198           gint i;
199
200           sscanf (value, "%d", &i);
201           g_object_set (G_OBJECT (object), name, i, NULL);
202           break;
203         }
204         case G_TYPE_UINT:{
205           guint i;
206
207           sscanf (value, "%u", &i);
208           g_object_set (G_OBJECT (object), name, i, NULL);
209           break;
210         }
211         case G_TYPE_LONG:{
212           glong i;
213
214           sscanf (value, "%ld", &i);
215           g_object_set (G_OBJECT (object), name, i, NULL);
216           break;
217         }
218         case G_TYPE_ULONG:{
219           gulong i;
220
221           sscanf (value, "%lu", &i);
222           g_object_set (G_OBJECT (object), name, i, NULL);
223           break;
224         }
225         case G_TYPE_BOOLEAN:{
226           gboolean i = FALSE;
227
228           if (!g_ascii_strncasecmp ("true", value, 4))
229             i = TRUE;
230           g_object_set (G_OBJECT (object), name, i, NULL);
231           break;
232         }
233         case G_TYPE_CHAR:{
234           gchar i;
235
236           sscanf (value, "%c", &i);
237           g_object_set (G_OBJECT (object), name, i, NULL);
238           break;
239         }
240         case G_TYPE_UCHAR:{
241           guchar i;
242
243           sscanf (value, "%c", &i);
244           g_object_set (G_OBJECT (object), name, i, NULL);
245           break;
246         }
247         case G_TYPE_FLOAT:{
248           gfloat i;
249
250           sscanf (value, "%f", &i);
251           g_object_set (G_OBJECT (object), name, i, NULL);
252           break;
253         }
254         case G_TYPE_DOUBLE:{
255           gfloat i;
256
257           sscanf (value, "%g", &i);
258           g_object_set (G_OBJECT (object), name, (gdouble) i, NULL);
259           break;
260         }
261         default:
262           if (G_IS_PARAM_SPEC_ENUM (paramspec)) {
263             gint i;
264
265             sscanf (value, "%d", &i);
266             g_object_set (G_OBJECT (object), name, i, NULL);
267           } else if (paramspec->value_type == GST_TYPE_URI) {
268             g_object_set (G_OBJECT (object), name, value, NULL);
269           }
270           break;
271       }
272     }
273   }
274 }
275
276 /* -----------------------------------------------------
277  *
278  *  The following code will be moved out of the main
279  * gstreamer library someday.
280  */
281
282 #include "gstpad.h"
283
284 static void
285 string_append_indent (GString * str, gint count)
286 {
287   gint xx;
288
289   for (xx = 0; xx < count; xx++)
290     g_string_append_c (str, ' ');
291 }
292
293 /**
294  * gst_print_pad_caps:
295  * @buf: the buffer to print the caps in
296  * @indent: initial indentation
297  * @pad: the pad to print the caps from
298  *
299  * Write the pad capabilities in a human readable format into
300  * the given GString.
301  */
302 void
303 gst_print_pad_caps (GString * buf, gint indent, GstPad * pad)
304 {
305   GstCaps *caps;
306
307   caps = pad->caps;
308
309   if (!caps) {
310     string_append_indent (buf, indent);
311     g_string_printf (buf, "%s:%s has no capabilities",
312         GST_DEBUG_PAD_NAME (pad));
313   } else {
314     char *s;
315
316     s = gst_caps_to_string (caps);
317     g_string_append (buf, s);
318     g_free (s);
319   }
320 }
321
322 /**
323  * gst_print_element_args:
324  * @buf: the buffer to print the args in
325  * @indent: initial indentation
326  * @element: the element to print the args of
327  *
328  * Print the element argument in a human readable format in the given
329  * GString.
330  */
331 void
332 gst_print_element_args (GString * buf, gint indent, GstElement * element)
333 {
334   guint width;
335   GValue value = { 0, };        /* the important thing is that value.type = 0 */
336   gchar *str = NULL;
337   GParamSpec *spec, **specs, **walk;
338
339   specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (element), NULL);
340
341   width = 0;
342   for (walk = specs; *walk; walk++) {
343     spec = *walk;
344     if (width < strlen (spec->name))
345       width = strlen (spec->name);
346   }
347
348   for (walk = specs; *walk; walk++) {
349     spec = *walk;
350
351     if (spec->flags & G_PARAM_READABLE) {
352       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
353       g_object_get_property (G_OBJECT (element), spec->name, &value);
354       str = g_strdup_value_contents (&value);
355       g_value_unset (&value);
356     } else {
357       str = g_strdup ("Parameter not readable.");
358     }
359
360     string_append_indent (buf, indent);
361     g_string_append (buf, spec->name);
362     string_append_indent (buf, 2 + width - strlen (spec->name));
363     g_string_append (buf, str);
364     g_string_append_c (buf, '\n');
365
366     g_free (str);
367   }
368
369   g_free (specs);
370 }
371
372 /**
373  * gst_element_create_all_pads:
374  * @element: a #GstElement to create pads for
375  *
376  * Creates a pad for each pad template that is always available.
377  * This function is only useful during object intialization of
378  * subclasses of #GstElement.
379  */
380 void
381 gst_element_create_all_pads (GstElement * element)
382 {
383   GList *padlist;
384
385   /* FIXME: lock element */
386
387   padlist =
388       gst_element_class_get_pad_template_list (GST_ELEMENT_CLASS
389       (G_OBJECT_GET_CLASS (element)));
390
391   while (padlist) {
392     GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
393
394     if (padtempl->presence == GST_PAD_ALWAYS) {
395       GstPad *pad;
396
397       pad = gst_pad_new_from_template (padtempl, padtempl->name_template);
398
399       gst_element_add_pad (element, pad);
400     }
401     padlist = padlist->next;
402   }
403 }
404
405 /**
406  * gst_element_get_compatible_pad_template:
407  * @element: a #GstElement to get a compatible pad template for.
408  * @compattempl: the #GstPadTemplate to find a compatible template for.
409  *
410  * Retrieves a pad template from @element that is compatible with @compattempl.
411  * Pads from compatible templates can be linked together.
412  *
413  * Returns: a compatible #GstPadTemplate, or NULL if none was found. No
414  * unreferencing is necessary.
415  */
416 GstPadTemplate *
417 gst_element_get_compatible_pad_template (GstElement * element,
418     GstPadTemplate * compattempl)
419 {
420   GstPadTemplate *newtempl = NULL;
421   GList *padlist;
422   GstElementClass *class;
423
424   g_return_val_if_fail (element != NULL, NULL);
425   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
426   g_return_val_if_fail (compattempl != NULL, NULL);
427
428   class = GST_ELEMENT_GET_CLASS (element);
429
430   padlist = gst_element_class_get_pad_template_list (class);
431
432   GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
433       "Looking for a suitable pad template in %s out of %d templates...",
434       GST_ELEMENT_NAME (element), g_list_length (padlist));
435
436   while (padlist) {
437     GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
438     GstCaps *intersection;
439
440     /* Ignore name
441      * Ignore presence
442      * Check direction (must be opposite)
443      * Check caps
444      */
445     GST_CAT_LOG (GST_CAT_CAPS,
446         "checking pad template %s", padtempl->name_template);
447     if (padtempl->direction != compattempl->direction) {
448       GST_CAT_DEBUG (GST_CAT_CAPS,
449           "compatible direction: found %s pad template \"%s\"",
450           padtempl->direction == GST_PAD_SRC ? "src" : "sink",
451           padtempl->name_template);
452
453       GST_CAT_DEBUG (GST_CAT_CAPS,
454           "intersecting %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (compattempl));
455       GST_CAT_DEBUG (GST_CAT_CAPS,
456           "..and %" GST_PTR_FORMAT, GST_PAD_TEMPLATE_CAPS (padtempl));
457
458       intersection = gst_caps_intersect (GST_PAD_TEMPLATE_CAPS (compattempl),
459           GST_PAD_TEMPLATE_CAPS (padtempl));
460
461       GST_CAT_DEBUG (GST_CAT_CAPS, "caps are %scompatible %" GST_PTR_FORMAT,
462           (intersection ? "" : "not "), intersection);
463
464       if (!gst_caps_is_empty (intersection))
465         newtempl = padtempl;
466       gst_caps_unref (intersection);
467       if (newtempl)
468         break;
469     }
470
471     padlist = g_list_next (padlist);
472   }
473   if (newtempl)
474     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
475         "Returning new pad template %p", newtempl);
476   else
477     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "No compatible pad template found");
478
479   return newtempl;
480 }
481
482 static GstPad *
483 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
484     const gchar * name)
485 {
486   GstPad *newpad = NULL;
487   GstElementClass *oclass;
488
489   oclass = GST_ELEMENT_GET_CLASS (element);
490
491   if (oclass->request_new_pad)
492     newpad = (oclass->request_new_pad) (element, templ, name);
493
494   if (newpad)
495     gst_object_ref (GST_OBJECT (newpad));
496
497   return newpad;
498 }
499
500
501
502 /**
503  * gst_element_get_pad_from_template:
504  * @element: a #GstElement.
505  * @templ: a #GstPadTemplate belonging to @element.
506  *
507  * Gets a pad from @element described by @templ. If the presence of @templ is
508  * #GST_PAD_REQUEST, requests a new pad. Can return %NULL for #GST_PAD_SOMETIMES
509  * templates.
510  *
511  * Returns: the #GstPad, or NULL if one could not be found or created.
512  */
513 static GstPad *
514 gst_element_get_pad_from_template (GstElement * element, GstPadTemplate * templ)
515 {
516   GstPad *ret = NULL;
517   GstPadPresence presence;
518
519   /* If this function is ever exported, we need check the validity of `element'
520    * and `templ', and to make sure the template actually belongs to the
521    * element. */
522
523   presence = GST_PAD_TEMPLATE_PRESENCE (templ);
524
525   switch (presence) {
526     case GST_PAD_ALWAYS:
527     case GST_PAD_SOMETIMES:
528       ret = gst_element_get_static_pad (element, templ->name_template);
529       if (!ret && presence == GST_PAD_ALWAYS)
530         g_warning
531             ("Element %s has an ALWAYS template %s, but no pad of the same name",
532             GST_OBJECT_NAME (element), templ->name_template);
533       break;
534
535     case GST_PAD_REQUEST:
536       ret = gst_element_request_pad (element, templ, NULL);
537       break;
538   }
539
540   return ret;
541 }
542
543 /**
544  * gst_element_request_compatible_pad:
545  * @element: a #GstElement.
546  * @templ: the #GstPadTemplate to which the new pad should be able to link.
547  *
548  * Requests a pad from @element. The returned pad should be unlinked and
549  * compatible with @templ. Might return an existing pad, or request a new one.
550  *
551  * Returns: a #GstPad, or %NULL if one could not be found or created.
552  */
553 GstPad *
554 gst_element_request_compatible_pad (GstElement * element,
555     GstPadTemplate * templ)
556 {
557   GstPadTemplate *templ_new;
558   GstPad *pad = NULL;
559
560   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
561   g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
562
563   /* FIXME: should really loop through the templates, testing each for
564    *      compatibility and pad availability. */
565   templ_new = gst_element_get_compatible_pad_template (element, templ);
566   if (templ_new)
567     pad = gst_element_get_pad_from_template (element, templ_new);
568
569   /* This can happen for non-request pads. No need to unref. */
570   if (pad && GST_PAD_PEER (pad))
571     pad = NULL;
572
573   return pad;
574 }
575
576 /**
577  * gst_element_get_compatible_pad:
578  * @element: a #GstElement in which the pad should be found.
579  * @pad: the #GstPad to find a compatible one for.
580  * @filtercaps: the #GstCaps to use as a filter.
581  *
582  * Looks for an unlinked pad to which the given pad can link. It is not
583  * guaranteed that linking the pads will work, though it should work in most
584  * cases.
585  *
586  * Returns: the #GstPad to which a link can be made, or %NULL if one cannot be
587  * found.
588  */
589 GstPad *
590 gst_element_get_compatible_pad (GstElement * element, GstPad * pad,
591     const GstCaps * caps)
592 {
593   GstIterator *pads;
594   GstPadTemplate *templ;
595   GstCaps *templcaps;
596   GstPad *foundpad = NULL;
597   gboolean done;
598
599   /* FIXME check for caps compatibility */
600
601   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
602   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
603
604   GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
605       "finding pad in %s compatible with %s:%s",
606       GST_ELEMENT_NAME (element), GST_DEBUG_PAD_NAME (pad));
607
608   g_return_val_if_fail (GST_PAD_PEER (pad) == NULL, NULL);
609
610   done = FALSE;
611   /* try to get an existing unlinked pad */
612   pads = gst_element_iterate_pads (element);
613   while (!done) {
614     gpointer padptr;
615
616     switch (gst_iterator_next (pads, &padptr)) {
617       case GST_ITERATOR_OK:
618       {
619         GstPad *peer;
620         GstPad *current;
621
622         current = GST_PAD (padptr);
623
624         GST_CAT_LOG (GST_CAT_ELEMENT_PADS, "examining pad %s:%s",
625             GST_DEBUG_PAD_NAME (current));
626
627         peer = gst_pad_get_peer (current);
628
629         if (peer == NULL && gst_pad_can_link (pad, current)) {
630
631           GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
632               "found existing unlinked pad %s:%s",
633               GST_DEBUG_PAD_NAME (current));
634
635           gst_iterator_free (pads);
636
637           return current;
638         } else {
639           GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unreffing pads");
640
641           gst_object_unref (GST_OBJECT (current));
642           if (peer)
643             gst_object_unref (GST_OBJECT (peer));
644         }
645         break;
646       }
647       case GST_ITERATOR_DONE:
648         done = TRUE;
649         break;
650       case GST_ITERATOR_RESYNC:
651         gst_iterator_resync (pads);
652         break;
653       case GST_ITERATOR_ERROR:
654         g_assert_not_reached ();
655         break;
656     }
657   }
658   gst_iterator_free (pads);
659
660   /* try to create a new one */
661   /* requesting is a little crazy, we need a template. Let's create one */
662   templcaps = gst_pad_get_caps (pad);
663
664   templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad),
665       GST_PAD_DIRECTION (pad), GST_PAD_ALWAYS, templcaps);
666   foundpad = gst_element_request_compatible_pad (element, templ);
667   gst_object_unref (GST_OBJECT (templ));
668
669   if (foundpad) {
670     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
671         "found existing request pad %s:%s", GST_DEBUG_PAD_NAME (foundpad));
672     return foundpad;
673   }
674
675   GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element,
676       "Could not find a compatible pad to link to %s:%s",
677       GST_DEBUG_PAD_NAME (pad));
678   return NULL;
679 }
680
681 /**
682  * gst_element_state_get_name:
683  * @state: a #GstElementState to get the name of.
684  *
685  * Gets a string representing the given state.
686  *
687  * Returns: a string with the name of the state.
688  */
689 const gchar *
690 gst_element_state_get_name (GstElementState state)
691 {
692   switch (state) {
693 #ifdef GST_DEBUG_COLOR
694     case GST_STATE_VOID_PENDING:
695       return "NONE_PENDING";
696       break;
697     case GST_STATE_NULL:
698       return "\033[01;34mNULL\033[00m";
699       break;
700     case GST_STATE_READY:
701       return "\033[01;31mREADY\033[00m";
702       break;
703     case GST_STATE_PLAYING:
704       return "\033[01;32mPLAYING\033[00m";
705       break;
706     case GST_STATE_PAUSED:
707       return "\033[01;33mPAUSED\033[00m";
708       break;
709     default:
710       /* This is a memory leak */
711       return g_strdup_printf ("\033[01;35;41mUNKNOWN!\033[00m(%d)", state);
712 #else
713     case GST_STATE_VOID_PENDING:
714       return "NONE_PENDING";
715       break;
716     case GST_STATE_NULL:
717       return "NULL";
718       break;
719     case GST_STATE_READY:
720       return "READY";
721       break;
722     case GST_STATE_PLAYING:
723       return "PLAYING";
724       break;
725     case GST_STATE_PAUSED:
726       return "PAUSED";
727       break;
728     default:
729       return g_strdup_printf ("UNKNOWN!(%d)", state);
730 #endif
731   }
732   return "";
733 }
734
735 /* if return val is true, *direct_child is a caller-owned ref on the direct
736  * child of ancestor that is part of object's ancestry */
737 static gboolean
738 object_has_ancestor (GstObject * object, GstObject * ancestor,
739     GstObject ** direct_child)
740 {
741   GstObject *child, *parent;
742
743   if (direct_child)
744     *direct_child = NULL;
745
746   child = gst_object_ref (object);
747   parent = gst_object_get_parent (object);
748
749   while (parent) {
750     if (ancestor == parent) {
751       if (direct_child)
752         *direct_child = child;
753       else
754         gst_object_unref (child);
755       gst_object_unref (parent);
756       return TRUE;
757     }
758
759     gst_object_unref (child);
760     child = parent;
761     parent = gst_object_get_parent (parent);
762   }
763
764   gst_object_unref (child);
765
766   return FALSE;
767 }
768
769 /* caller owns return */
770 static GstObject *
771 find_common_root (GstObject * o1, GstObject * o2)
772 {
773   GstObject *top = o1;
774   GstObject *kid1, *kid2;
775   GstObject *root = NULL;
776
777   while (GST_OBJECT_PARENT (top))
778     top = GST_OBJECT_PARENT (top);
779
780   /* the itsy-bitsy spider... */
781
782   if (!object_has_ancestor (o2, top, &kid2))
783     return NULL;
784
785   root = gst_object_ref (top);
786   while (TRUE) {
787     if (!object_has_ancestor (o1, kid2, &kid1)) {
788       gst_object_unref (kid2);
789       return root;
790     }
791     root = kid2;
792     if (!object_has_ancestor (o2, kid1, &kid2)) {
793       gst_object_unref (kid1);
794       return root;
795     }
796     root = kid1;
797   }
798 }
799
800 /* caller does not own return */
801 static GstPad *
802 ghost_up (GstElement * e, GstPad * pad)
803 {
804   static gint ghost_pad_index = 0;
805   GstPad *gpad;
806   gchar *name;
807   GstObject *parent = GST_OBJECT_PARENT (e);
808
809   name = g_strdup_printf ("ghost%d", ghost_pad_index++);
810   gpad = gst_ghost_pad_new (name, pad);
811   g_free (name);
812
813   if (!gst_element_add_pad ((GstElement *) parent, gpad)) {
814     g_warning ("Pad named %s already exists in element %s\n",
815         GST_OBJECT_NAME (gpad), GST_OBJECT_NAME (parent));
816     gst_object_unref ((GstObject *) gpad);
817     return NULL;
818   }
819
820   return gpad;
821 }
822
823 static void
824 remove_pad (gpointer ppad, gpointer unused)
825 {
826   GstPad *pad = ppad;
827
828   if (!gst_element_remove_pad ((GstElement *) GST_OBJECT_PARENT (pad), pad))
829     g_warning ("Couldn't remove pad %s from element %s",
830         GST_OBJECT_NAME (pad), GST_OBJECT_NAME (GST_OBJECT_PARENT (pad)));
831 }
832
833 static gboolean
834 prepare_link_maybe_ghosting (GstPad ** src, GstPad ** sink,
835     GSList ** pads_created)
836 {
837   GstObject *root;
838   GstObject *e1, *e2;
839   GSList *pads_created_local = NULL;
840
841   g_assert (pads_created);
842
843   e1 = GST_OBJECT_PARENT (*src);
844   e2 = GST_OBJECT_PARENT (*sink);
845
846   if (GST_OBJECT_PARENT (e1) == GST_OBJECT_PARENT (e2)) {
847     GST_CAT_INFO (GST_CAT_PADS, "%s and %s in same bin, no need for ghost pads",
848         GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
849     return TRUE;
850   }
851
852   GST_CAT_INFO (GST_CAT_PADS, "%s and %s not in same bin, making ghost pads",
853       GST_OBJECT_NAME (e1), GST_OBJECT_NAME (e2));
854
855   /* we need to setup some ghost pads */
856   root = find_common_root (e1, e2);
857   if (!root) {
858     g_warning
859         ("Trying to connect elements that don't share a common ancestor: %s and %s\n",
860         GST_ELEMENT_NAME (e1), GST_ELEMENT_NAME (e2));
861     return FALSE;
862   }
863
864   while (GST_OBJECT_PARENT (e1) != root) {
865     *src = ghost_up ((GstElement *) e1, *src);
866     if (!*src)
867       goto cleanup_fail;
868     e1 = GST_OBJECT_PARENT (*src);
869     pads_created_local = g_slist_prepend (pads_created_local, *src);
870   }
871   while (GST_OBJECT_PARENT (e2) != root) {
872     *sink = ghost_up ((GstElement *) e2, *sink);
873     if (!*sink)
874       goto cleanup_fail;
875     e2 = GST_OBJECT_PARENT (*sink);
876     pads_created_local = g_slist_prepend (pads_created_local, *sink);
877   }
878
879   gst_object_unref (root);
880   *pads_created = g_slist_concat (*pads_created, pads_created_local);
881   return TRUE;
882
883 cleanup_fail:
884   gst_object_unref (root);
885   g_slist_foreach (pads_created_local, remove_pad, NULL);
886   g_slist_free (pads_created_local);
887   return FALSE;
888 }
889
890 static gboolean
891 pad_link_maybe_ghosting (GstPad * src, GstPad * sink)
892 {
893   GSList *pads_created = NULL;
894   gboolean ret;
895
896   if (!prepare_link_maybe_ghosting (&src, &sink, &pads_created)) {
897     ret = FALSE;
898   } else {
899     ret = (gst_pad_link (src, sink) == GST_PAD_LINK_OK);
900   }
901
902   if (!ret) {
903     g_slist_foreach (pads_created, remove_pad, NULL);
904   }
905   g_slist_free (pads_created);
906
907   return ret;
908 }
909
910 /**
911  * gst_element_link_pads:
912  * @src: a #GstElement containing the source pad.
913  * @srcpadname: the name of the #GstPad in source element or NULL for any pad.
914  * @dest: the #GstElement containing the destination pad.
915  * @destpadname: the name of the #GstPad in destination element or NULL for any pad.
916  *
917  * Links the two named pads of the source and destination elements.
918  * Side effect is that if one of the pads has no parent, it becomes a
919  * child of the parent of the other element.  If they have different
920  * parents, the link fails.
921  *
922  * Returns: TRUE if the pads could be linked, FALSE otherwise.
923  */
924 gboolean
925 gst_element_link_pads (GstElement * src, const gchar * srcpadname,
926     GstElement * dest, const gchar * destpadname)
927 {
928   const GList *srcpads, *destpads, *srctempls, *desttempls, *l;
929   GstPad *srcpad, *destpad;
930   GstPadTemplate *srctempl, *desttempl;
931   GstElementClass *srcclass, *destclass;
932
933   /* checks */
934   g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
935   g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
936
937   srcclass = GST_ELEMENT_GET_CLASS (src);
938   destclass = GST_ELEMENT_GET_CLASS (dest);
939
940   GST_CAT_INFO (GST_CAT_ELEMENT_PADS,
941       "trying to link element %s:%s to element %s:%s", GST_ELEMENT_NAME (src),
942       srcpadname ? srcpadname : "(any)", GST_ELEMENT_NAME (dest),
943       destpadname ? destpadname : "(any)");
944
945   /* now get the pads we're trying to link and a list of all remaining pads */
946   if (srcpadname) {
947     srcpad = gst_element_get_pad (src, srcpadname);
948     if (!srcpad) {
949       GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
950           GST_ELEMENT_NAME (src), srcpadname);
951       return FALSE;
952     } else {
953       if (!(GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC)) {
954         GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no src pad",
955             GST_DEBUG_PAD_NAME (srcpad));
956         gst_object_unref (GST_OBJECT (srcpad));
957         return FALSE;
958       }
959       if (GST_PAD_PEER (srcpad) != NULL) {
960         GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is already linked",
961             GST_DEBUG_PAD_NAME (srcpad));
962         gst_object_unref (GST_OBJECT (srcpad));
963         return FALSE;
964       }
965     }
966     srcpads = NULL;
967   } else {
968     GST_LOCK (src);
969     srcpads = GST_ELEMENT_PADS (src);
970     srcpad = srcpads ? GST_PAD_CAST (srcpads->data) : NULL;
971     if (srcpad)
972       gst_object_ref (GST_OBJECT (srcpad));
973     GST_UNLOCK (src);
974   }
975   if (destpadname) {
976     destpad = gst_element_get_pad (dest, destpadname);
977     if (!destpad) {
978       GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no pad %s:%s",
979           GST_ELEMENT_NAME (dest), destpadname);
980       return FALSE;
981     } else {
982       if (!(GST_PAD_DIRECTION (destpad) == GST_PAD_SINK)) {
983         GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is no sink pad",
984             GST_DEBUG_PAD_NAME (destpad));
985         gst_object_unref (GST_OBJECT (destpad));
986         return FALSE;
987       }
988       if (GST_PAD_PEER (destpad) != NULL) {
989         GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is already linked",
990             GST_DEBUG_PAD_NAME (destpad));
991         gst_object_unref (GST_OBJECT (destpad));
992         return FALSE;
993       }
994     }
995     destpads = NULL;
996   } else {
997     GST_LOCK (dest);
998     destpads = GST_ELEMENT_PADS (dest);
999     destpad = destpads ? GST_PAD_CAST (destpads->data) : NULL;
1000     if (destpad)
1001       gst_object_ref (GST_OBJECT (destpad));
1002     GST_UNLOCK (dest);
1003   }
1004
1005   if (srcpadname && destpadname) {
1006     gboolean result;
1007
1008     /* two explicitly specified pads */
1009     result = pad_link_maybe_ghosting (srcpad, destpad);
1010
1011     gst_object_unref (GST_OBJECT (srcpad));
1012     gst_object_unref (GST_OBJECT (destpad));
1013
1014     return result;
1015   }
1016   if (srcpad) {
1017     /* loop through the allowed pads in the source, trying to find a
1018      * compatible destination pad */
1019     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1020         "looping through allowed src and dest pads");
1021     do {
1022       GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying src pad %s:%s",
1023           GST_DEBUG_PAD_NAME (srcpad));
1024       if ((GST_PAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
1025           (GST_PAD_PEER (srcpad) == NULL)) {
1026         GstPad *temp;
1027
1028         if (destpadname) {
1029           temp = destpad;
1030           gst_object_ref (GST_OBJECT (temp));
1031         } else {
1032           temp = gst_element_get_compatible_pad (dest, srcpad, NULL);
1033         }
1034
1035         if (temp && pad_link_maybe_ghosting (srcpad, temp)) {
1036           GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1037               GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (temp));
1038           if (destpad)
1039             gst_object_unref (GST_OBJECT (destpad));
1040           gst_object_unref (GST_OBJECT (srcpad));
1041           gst_object_unref (GST_OBJECT (temp));
1042           return TRUE;
1043         }
1044
1045         if (temp) {
1046           gst_object_unref (GST_OBJECT (temp));
1047         }
1048       }
1049       /* find a better way for this mess */
1050       if (srcpads) {
1051         srcpads = g_list_next (srcpads);
1052         if (srcpads) {
1053           gst_object_unref (GST_OBJECT (srcpad));
1054           srcpad = GST_PAD_CAST (srcpads->data);
1055           gst_object_ref (GST_OBJECT (srcpad));
1056         }
1057       }
1058     } while (srcpads);
1059   }
1060   if (srcpadname) {
1061     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s:%s to %s",
1062         GST_DEBUG_PAD_NAME (srcpad), GST_ELEMENT_NAME (dest));
1063     gst_object_unref (GST_OBJECT (srcpad));
1064     if (destpad)
1065       gst_object_unref (GST_OBJECT (destpad));
1066     return FALSE;
1067   } else {
1068     if (srcpad)
1069       gst_object_unref (GST_OBJECT (srcpad));
1070     srcpad = NULL;
1071   }
1072   if (destpad) {
1073     /* loop through the existing pads in the destination */
1074     do {
1075       GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "trying dest pad %s:%s",
1076           GST_DEBUG_PAD_NAME (destpad));
1077       if ((GST_PAD_DIRECTION (destpad) == GST_PAD_SINK) &&
1078           (GST_PAD_PEER (destpad) == NULL)) {
1079         GstPad *temp = gst_element_get_compatible_pad (src, destpad, NULL);
1080
1081         if (temp && pad_link_maybe_ghosting (temp, destpad)) {
1082           GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "linked pad %s:%s to pad %s:%s",
1083               GST_DEBUG_PAD_NAME (temp), GST_DEBUG_PAD_NAME (destpad));
1084           gst_object_unref (GST_OBJECT (temp));
1085           gst_object_unref (GST_OBJECT (destpad));
1086           if (srcpad)
1087             gst_object_unref (GST_OBJECT (srcpad));
1088           return TRUE;
1089         }
1090         if (temp) {
1091           gst_object_unref (GST_OBJECT (temp));
1092         }
1093       }
1094       if (destpads) {
1095         destpads = g_list_next (destpads);
1096         if (destpads) {
1097           gst_object_unref (GST_OBJECT (destpad));
1098           destpad = GST_PAD_CAST (destpads->data);
1099           gst_object_ref (GST_OBJECT (destpad));
1100         }
1101       }
1102     } while (destpads);
1103   }
1104   if (destpadname) {
1105     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s:%s",
1106         GST_ELEMENT_NAME (src), GST_DEBUG_PAD_NAME (destpad));
1107     gst_object_unref (GST_OBJECT (destpad));
1108     if (srcpad)
1109       gst_object_unref (GST_OBJECT (srcpad));
1110     return FALSE;
1111   } else {
1112     gst_object_unref (GST_OBJECT (destpad));
1113     if (srcpad)
1114       gst_object_unref (GST_OBJECT (srcpad));
1115     srcpad = NULL;
1116     destpad = NULL;
1117   }
1118
1119   GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1120       "we might have request pads on both sides, checking...");
1121   srctempls = gst_element_class_get_pad_template_list (srcclass);
1122   desttempls = gst_element_class_get_pad_template_list (destclass);
1123
1124   if (srctempls && desttempls) {
1125     while (srctempls) {
1126       srctempl = (GstPadTemplate *) srctempls->data;
1127       if (srctempl->presence == GST_PAD_REQUEST) {
1128         for (l = desttempls; l; l = l->next) {
1129           desttempl = (GstPadTemplate *) l->data;
1130           if (desttempl->presence == GST_PAD_REQUEST &&
1131               desttempl->direction != srctempl->direction) {
1132             if (gst_caps_is_always_compatible (gst_pad_template_get_caps
1133                     (srctempl), gst_pad_template_get_caps (desttempl))) {
1134               srcpad =
1135                   gst_element_get_request_pad (src, srctempl->name_template);
1136               destpad =
1137                   gst_element_get_request_pad (dest, desttempl->name_template);
1138               if (pad_link_maybe_ghosting (srcpad, destpad)) {
1139                 GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1140                     "linked pad %s:%s to pad %s:%s",
1141                     GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
1142                 gst_object_unref (GST_OBJECT (srcpad));
1143                 gst_object_unref (GST_OBJECT (destpad));
1144                 return TRUE;
1145               }
1146               /* it failed, so we release the request pads */
1147               gst_element_release_request_pad (src, srcpad);
1148               gst_element_release_request_pad (dest, destpad);
1149             }
1150           }
1151         }
1152       }
1153       srctempls = srctempls->next;
1154     }
1155   }
1156
1157   GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "no link possible from %s to %s",
1158       GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1159   return FALSE;
1160 }
1161
1162 /**
1163  * gst_element_link:
1164  * @src: a #GstElement containing the source pad.
1165  * @dest: the #GstElement containing the destination pad.
1166  *
1167  * Links @src to @dest. The link must be from source to
1168  * destination; the other direction will not be tried. The function looks for
1169  * existing pads that aren't linked yet. It will request new pads if necessary.
1170  * If multiple links are possible, only one is established.
1171  *
1172  * Returns: TRUE if the elements could be linked, FALSE otherwise.
1173  */
1174 gboolean
1175 gst_element_link (GstElement * src, GstElement * dest)
1176 {
1177   return gst_element_link_pads (src, NULL, dest, NULL);
1178 }
1179
1180 /**
1181  * gst_element_link_many:
1182  * @element_1: the first #GstElement in the link chain.
1183  * @element_2: the second #GstElement in the link chain.
1184  * @...: the NULL-terminated list of elements to link in order.
1185  *
1186  * Chain together a series of elements. Uses gst_element_link().
1187  *
1188  * Returns: TRUE on success, FALSE otherwise.
1189  */
1190 gboolean
1191 gst_element_link_many (GstElement * element_1, GstElement * element_2, ...)
1192 {
1193   va_list args;
1194
1195   g_return_val_if_fail (GST_IS_ELEMENT (element_1), FALSE);
1196   g_return_val_if_fail (GST_IS_ELEMENT (element_2), FALSE);
1197
1198   va_start (args, element_2);
1199
1200   while (element_2) {
1201     if (!gst_element_link (element_1, element_2))
1202       return FALSE;
1203
1204     element_1 = element_2;
1205     element_2 = va_arg (args, GstElement *);
1206   }
1207
1208   va_end (args);
1209
1210   return TRUE;
1211 }
1212
1213 /**
1214  * gst_element_unlink_pads:
1215  * @src: a #GstElement containing the source pad.
1216  * @srcpadname: the name of the #GstPad in source element.
1217  * @dest: a #GstElement containing the destination pad.
1218  * @destpadname: the name of the #GstPad in destination element.
1219  *
1220  * Unlinks the two named pads of the source and destination elements.
1221  */
1222 void
1223 gst_element_unlink_pads (GstElement * src, const gchar * srcpadname,
1224     GstElement * dest, const gchar * destpadname)
1225 {
1226   GstPad *srcpad, *destpad;
1227
1228   g_return_if_fail (src != NULL);
1229   g_return_if_fail (GST_IS_ELEMENT (src));
1230   g_return_if_fail (srcpadname != NULL);
1231   g_return_if_fail (dest != NULL);
1232   g_return_if_fail (GST_IS_ELEMENT (dest));
1233   g_return_if_fail (destpadname != NULL);
1234
1235   /* obtain the pads requested */
1236   srcpad = gst_element_get_pad (src, srcpadname);
1237   if (srcpad == NULL) {
1238     GST_WARNING_OBJECT (src, "source element has no pad \"%s\"", srcpadname);
1239     return;
1240   }
1241   destpad = gst_element_get_pad (dest, destpadname);
1242   if (srcpad == NULL) {
1243     GST_WARNING_OBJECT (dest, "destination element has no pad \"%s\"",
1244         destpadname);
1245     return;
1246   }
1247
1248   /* we're satisified they can be unlinked, let's do it */
1249   gst_pad_unlink (srcpad, destpad);
1250 }
1251
1252 /**
1253  * gst_element_unlink_many:
1254  * @element_1: the first #GstElement in the link chain.
1255  * @element_2: the second #GstElement in the link chain.
1256  * @...: the NULL-terminated list of elements to unlink in order.
1257  *
1258  * Unlinks a series of elements. Uses gst_element_unlink().
1259  */
1260 void
1261 gst_element_unlink_many (GstElement * element_1, GstElement * element_2, ...)
1262 {
1263   va_list args;
1264
1265   g_return_if_fail (element_1 != NULL && element_2 != NULL);
1266   g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
1267
1268   va_start (args, element_2);
1269
1270   while (element_2) {
1271     gst_element_unlink (element_1, element_2);
1272
1273     element_1 = element_2;
1274     element_2 = va_arg (args, GstElement *);
1275   }
1276
1277   va_end (args);
1278 }
1279
1280 /**
1281  * gst_element_unlink:
1282  * @src: the source #GstElement to unlink.
1283  * @dest: the sink #GstElement to unlink.
1284  *
1285  * Unlinks all source pads of the source element with all sink pads
1286  * of the sink element to which they are linked.
1287  */
1288 void
1289 gst_element_unlink (GstElement * src, GstElement * dest)
1290 {
1291   GstIterator *pads;
1292   gboolean done = FALSE;
1293
1294   g_return_if_fail (GST_IS_ELEMENT (src));
1295   g_return_if_fail (GST_IS_ELEMENT (dest));
1296
1297   GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "unlinking \"%s\" and \"%s\"",
1298       GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
1299
1300   pads = gst_element_iterate_pads (src);
1301   while (!done) {
1302     gpointer data;
1303
1304     switch (gst_iterator_next (pads, &data)) {
1305       case GST_ITERATOR_OK:
1306       {
1307         GstPad *pad = GST_PAD_CAST (data);
1308
1309         if (GST_PAD_IS_SRC (pad)) {
1310           GstPad *peerpad = gst_pad_get_peer (pad);
1311
1312           /* see if the pad is connected and is really a pad
1313            * of dest */
1314           if (peerpad) {
1315             GstElement *peerelem = gst_pad_get_parent (peerpad);
1316
1317             if (peerelem == dest) {
1318               gst_pad_unlink (pad, peerpad);
1319             }
1320             if (peerelem)
1321               gst_object_unref (GST_OBJECT (peerelem));
1322
1323             gst_object_unref (GST_OBJECT (peerpad));
1324           }
1325         }
1326         gst_object_unref (GST_OBJECT (pad));
1327         break;
1328       }
1329       case GST_ITERATOR_RESYNC:
1330         gst_iterator_resync (pads);
1331         break;
1332       case GST_ITERATOR_DONE:
1333         done = TRUE;
1334         break;
1335       default:
1336         g_assert_not_reached ();
1337         break;
1338     }
1339   }
1340 }
1341
1342 gboolean
1343 gst_element_query_position (GstElement * element, GstFormat * format,
1344     gint64 * cur, gint64 * end)
1345 {
1346   GstQuery *query;
1347   gboolean ret;
1348
1349   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1350   g_return_val_if_fail (format != NULL, FALSE);
1351
1352   query = gst_query_new_position (*format);
1353   ret = gst_element_query (element, query);
1354
1355   if (ret)
1356     gst_query_parse_position (query, format, cur, end);
1357
1358   gst_query_unref (query);
1359
1360   return ret;
1361 }
1362
1363 gboolean
1364 gst_element_query_convert (GstElement * element, GstFormat src_format,
1365     gint64 src_val, GstFormat * dest_fmt, gint64 * dest_val)
1366 {
1367   GstQuery *query;
1368   gboolean ret;
1369
1370   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1371   g_return_val_if_fail (dest_fmt != NULL, FALSE);
1372   g_return_val_if_fail (dest_val != NULL, FALSE);
1373
1374   if (*dest_fmt == src_format) {
1375     *dest_val = src_val;
1376     return TRUE;
1377   }
1378
1379   query = gst_query_new_convert (src_format, src_val, *dest_fmt);
1380   ret = gst_element_query (element, query);
1381
1382   if (ret)
1383     gst_query_parse_convert (query, NULL, NULL, dest_fmt, dest_val);
1384
1385   gst_query_unref (query);
1386
1387   return ret;
1388 }
1389
1390
1391 /**
1392  * gst_pad_can_link:
1393  * @srcpad: the source #GstPad to link.
1394  * @sinkpad: the sink #GstPad to link.
1395  *
1396  * Checks if the source pad and the sink pad can be linked.
1397  * Both @srcpad and @sinkpad must be unlinked.
1398  *
1399  * Returns: TRUE if the pads can be linked, FALSE otherwise.
1400  */
1401 gboolean
1402 gst_pad_can_link (GstPad * srcpad, GstPad * sinkpad)
1403 {
1404   /* FIXME This function is gross.  It's almost a direct copy of
1405    * gst_pad_link_filtered().  Any decent programmer would attempt
1406    * to merge the two functions, which I will do some day. --ds
1407    */
1408
1409   /* generic checks */
1410   g_return_val_if_fail (GST_IS_PAD (srcpad), FALSE);
1411   g_return_val_if_fail (GST_IS_PAD (sinkpad), FALSE);
1412
1413   GST_CAT_INFO (GST_CAT_PADS, "trying to link %s:%s and %s:%s",
1414       GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1415
1416   /* FIXME: shouldn't we convert this to g_return_val_if_fail? */
1417   if (GST_PAD_PEER (srcpad) != NULL) {
1418     GST_CAT_INFO (GST_CAT_PADS, "Source pad %s:%s has a peer, failed",
1419         GST_DEBUG_PAD_NAME (srcpad));
1420     return FALSE;
1421   }
1422   if (GST_PAD_PEER (sinkpad) != NULL) {
1423     GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has a peer, failed",
1424         GST_DEBUG_PAD_NAME (sinkpad));
1425     return FALSE;
1426   }
1427   if (!GST_PAD_IS_SRC (srcpad)) {
1428     GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s is not source pad, failed",
1429         GST_DEBUG_PAD_NAME (srcpad));
1430     return FALSE;
1431   }
1432   if (!GST_PAD_IS_SINK (sinkpad)) {
1433     GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s is not sink pad, failed",
1434         GST_DEBUG_PAD_NAME (sinkpad));
1435     return FALSE;
1436   }
1437   if (GST_PAD_PARENT (srcpad) == NULL) {
1438     GST_CAT_INFO (GST_CAT_PADS, "Src pad %s:%s has no parent, failed",
1439         GST_DEBUG_PAD_NAME (srcpad));
1440     return FALSE;
1441   }
1442   if (GST_PAD_PARENT (sinkpad) == NULL) {
1443     GST_CAT_INFO (GST_CAT_PADS, "Sink pad %s:%s has no parent, failed",
1444         GST_DEBUG_PAD_NAME (srcpad));
1445     return FALSE;
1446   }
1447
1448   return TRUE;
1449 }
1450
1451 /**
1452  * gst_pad_use_fixed_caps:
1453  * @pad: the pad to use
1454  *
1455  * A helper function you can use that sets the 
1456  * @gst_pad_get_fixed_caps_func as the gstcaps function for the
1457  * pad. This way the function will always return the negotiated caps
1458  * or in case the pad is not negotiated, the padtemplate caps.
1459  */
1460 void
1461 gst_pad_use_fixed_caps (GstPad * pad)
1462 {
1463   gst_pad_set_getcaps_function (pad, gst_pad_get_fixed_caps_func);
1464 }
1465
1466 /**
1467  * gst_pad_get_fixed_caps_func:
1468  * @pad: the pad to use
1469  *
1470  * A helper function you can use as a GetCaps function that
1471  * will return the currently negotiated caps or the padtemplate
1472  * when NULL.
1473  *
1474  * Returns: The currently negotiated caps or the padtemplate.
1475  */
1476 GstCaps *
1477 gst_pad_get_fixed_caps_func (GstPad * pad)
1478 {
1479   GstCaps *result;
1480
1481   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1482
1483   if (GST_PAD_CAPS (pad)) {
1484     result = GST_PAD_CAPS (pad);
1485
1486     GST_CAT_DEBUG (GST_CAT_CAPS,
1487         "using pad caps %p %" GST_PTR_FORMAT, result, result);
1488
1489     result = gst_caps_ref (result);
1490     goto done;
1491   }
1492   if (GST_PAD_PAD_TEMPLATE (pad)) {
1493     GstPadTemplate *templ = GST_PAD_PAD_TEMPLATE (pad);
1494
1495     result = GST_PAD_TEMPLATE_CAPS (templ);
1496     GST_CAT_DEBUG (GST_CAT_CAPS,
1497         "using pad template %p with caps %p %" GST_PTR_FORMAT, templ, result,
1498         result);
1499
1500     result = gst_caps_ref (result);
1501     goto done;
1502   }
1503   GST_CAT_DEBUG (GST_CAT_CAPS, "pad has no caps");
1504   result = gst_caps_new_empty ();
1505
1506 done:
1507   return result;
1508 }
1509
1510 /**
1511  * gst_object_default_error:
1512  * @object: a #GObject that signalled the error.
1513  * @orig: the #GstObject that initiated the error.
1514  * @error: the GError.
1515  * @debug: an additional debug information string, or NULL.
1516  *
1517  * A default error function.
1518  *
1519  * The default handler will simply print the error string using g_print.
1520  */
1521 void
1522 gst_object_default_error (GstObject * source, GError * error, gchar * debug)
1523 {
1524   gchar *name = gst_object_get_path_string (source);
1525
1526   g_print (_("ERROR: from element %s: %s\n"), name, error->message);
1527   if (debug)
1528     g_print (_("Additional debug info:\n%s\n"), debug);
1529
1530   g_free (name);
1531 }
1532
1533 /**
1534  * gst_bin_add_many:
1535  * @bin: the bin to add the elements to
1536  * @element_1: the first element to add to the bin
1537  * @...: additional elements to add to the bin
1538  *
1539  * Adds a NULL-terminated list of elements to a bin.  This function is
1540  * equivalent to calling #gst_bin_add() for each member of the list.
1541  */
1542 void
1543 gst_bin_add_many (GstBin * bin, GstElement * element_1, ...)
1544 {
1545   va_list args;
1546
1547   g_return_if_fail (GST_IS_BIN (bin));
1548   g_return_if_fail (GST_IS_ELEMENT (element_1));
1549
1550   va_start (args, element_1);
1551
1552   while (element_1) {
1553     gst_bin_add (bin, element_1);
1554
1555     element_1 = va_arg (args, GstElement *);
1556   }
1557
1558   va_end (args);
1559 }
1560
1561 /**
1562  * gst_bin_remove_many:
1563  * @bin: the bin to remove the elements from
1564  * @element_1: the first element to remove from the bin
1565  * @...: NULL-terminated list of elements to remove from the bin
1566  *
1567  * Remove a list of elements from a bin. This function is equivalent
1568  * to calling #gst_bin_remove with each member of the list.
1569  */
1570 void
1571 gst_bin_remove_many (GstBin * bin, GstElement * element_1, ...)
1572 {
1573   va_list args;
1574
1575   g_return_if_fail (GST_IS_BIN (bin));
1576   g_return_if_fail (GST_IS_ELEMENT (element_1));
1577
1578   va_start (args, element_1);
1579
1580   while (element_1) {
1581     gst_bin_remove (bin, element_1);
1582
1583     element_1 = va_arg (args, GstElement *);
1584   }
1585
1586   va_end (args);
1587 }
1588
1589 static void
1590 gst_element_populate_std_props (GObjectClass * klass, const gchar * prop_name,
1591     guint arg_id, GParamFlags flags)
1592 {
1593   GQuark prop_id = g_quark_from_string (prop_name);
1594   GParamSpec *pspec;
1595
1596   static GQuark fd_id = 0;
1597   static GQuark blocksize_id;
1598   static GQuark bytesperread_id;
1599   static GQuark dump_id;
1600   static GQuark filesize_id;
1601   static GQuark mmapsize_id;
1602   static GQuark location_id;
1603   static GQuark offset_id;
1604   static GQuark silent_id;
1605   static GQuark touch_id;
1606
1607   if (!fd_id) {
1608     fd_id = g_quark_from_static_string ("fd");
1609     blocksize_id = g_quark_from_static_string ("blocksize");
1610     bytesperread_id = g_quark_from_static_string ("bytesperread");
1611     dump_id = g_quark_from_static_string ("dump");
1612     filesize_id = g_quark_from_static_string ("filesize");
1613     mmapsize_id = g_quark_from_static_string ("mmapsize");
1614     location_id = g_quark_from_static_string ("location");
1615     offset_id = g_quark_from_static_string ("offset");
1616     silent_id = g_quark_from_static_string ("silent");
1617     touch_id = g_quark_from_static_string ("touch");
1618   }
1619
1620   if (prop_id == fd_id) {
1621     pspec = g_param_spec_int ("fd", "File-descriptor",
1622         "File-descriptor for the file being read", 0, G_MAXINT, 0, flags);
1623   } else if (prop_id == blocksize_id) {
1624     pspec = g_param_spec_ulong ("blocksize", "Block Size",
1625         "Block size to read per buffer", 0, G_MAXULONG, 4096, flags);
1626
1627   } else if (prop_id == bytesperread_id) {
1628     pspec = g_param_spec_int ("bytesperread", "Bytes per read",
1629         "Number of bytes to read per buffer", G_MININT, G_MAXINT, 0, flags);
1630
1631   } else if (prop_id == dump_id) {
1632     pspec = g_param_spec_boolean ("dump", "Dump",
1633         "Dump bytes to stdout", FALSE, flags);
1634
1635   } else if (prop_id == filesize_id) {
1636     pspec = g_param_spec_int64 ("filesize", "File Size",
1637         "Size of the file being read", 0, G_MAXINT64, 0, flags);
1638
1639   } else if (prop_id == mmapsize_id) {
1640     pspec = g_param_spec_ulong ("mmapsize", "mmap() Block Size",
1641         "Size in bytes of mmap()d regions", 0, G_MAXULONG, 4 * 1048576, flags);
1642
1643   } else if (prop_id == location_id) {
1644     pspec = g_param_spec_string ("location", "File Location",
1645         "Location of the file to read", NULL, flags);
1646
1647   } else if (prop_id == offset_id) {
1648     pspec = g_param_spec_int64 ("offset", "File Offset",
1649         "Byte offset of current read pointer", 0, G_MAXINT64, 0, flags);
1650
1651   } else if (prop_id == silent_id) {
1652     pspec = g_param_spec_boolean ("silent", "Silent", "Don't produce events",
1653         FALSE, flags);
1654
1655   } else if (prop_id == touch_id) {
1656     pspec = g_param_spec_boolean ("touch", "Touch read data",
1657         "Touch data to force disk read before " "push ()", TRUE, flags);
1658   } else {
1659     g_warning ("Unknown - 'standard' property '%s' id %d from klass %s",
1660         prop_name, arg_id, g_type_name (G_OBJECT_CLASS_TYPE (klass)));
1661     pspec = NULL;
1662   }
1663
1664   if (pspec) {
1665     g_object_class_install_property (klass, arg_id, pspec);
1666   }
1667 }
1668
1669 /**
1670  * gst_element_class_install_std_props:
1671  * @klass: the #GstElementClass to add the properties to.
1672  * @first_name: the name of the first property.
1673  * in a NULL terminated
1674  * @...: the id and flags of the first property, followed by
1675  * further 'name', 'id', 'flags' triplets and terminated by NULL.
1676  *
1677  * Adds a list of standardized properties with types to the @klass.
1678  * the id is for the property switch in your get_prop method, and
1679  * the flags determine readability / writeability.
1680  **/
1681 void
1682 gst_element_class_install_std_props (GstElementClass * klass,
1683     const gchar * first_name, ...)
1684 {
1685   const char *name;
1686
1687   va_list args;
1688
1689   g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1690
1691   va_start (args, first_name);
1692
1693   name = first_name;
1694
1695   while (name) {
1696     int arg_id = va_arg (args, int);
1697     int flags = va_arg (args, int);
1698
1699     gst_element_populate_std_props ((GObjectClass *) klass, name, arg_id,
1700         flags);
1701
1702     name = va_arg (args, char *);
1703   }
1704
1705   va_end (args);
1706 }
1707
1708
1709 /**
1710  * gst_buffer_merge:
1711  * @buf1: a first source #GstBuffer to merge.
1712  * @buf2: the second source #GstBuffer to merge.
1713  *
1714  * Create a new buffer that is the concatenation of the two source
1715  * buffers.  The original source buffers will not be modified or
1716  * unref'd.  Make sure you unref the source buffers if they are not used
1717  * anymore afterwards.
1718  *
1719  * If the buffers point to contiguous areas of memory, the buffer
1720  * is created without copying the data.
1721  *
1722  * Returns: the new #GstBuffer that's the concatenation of the source buffers.
1723  */
1724 GstBuffer *
1725 gst_buffer_merge (GstBuffer * buf1, GstBuffer * buf2)
1726 {
1727   GstBuffer *result;
1728
1729   /* we're just a specific case of the more general gst_buffer_span() */
1730   result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
1731
1732   return result;
1733 }
1734
1735 /**
1736  * gst_buffer_merge:
1737  * @buf1: a first source #GstBuffer to merge.
1738  * @buf2: the second source #GstBuffer to merge.
1739  *
1740  * Create a new buffer that is the concatenation of the two source
1741  * buffers, and takes ownership of the original source buffers.
1742  *
1743  * If the buffers point to contiguous areas of memory, the buffer
1744  * is created without copying the data.
1745  *
1746  * Returns: the new #GstBuffer that's the concatenation of the source buffers.
1747  */
1748 GstBuffer *
1749 gst_buffer_join (GstBuffer * buf1, GstBuffer * buf2)
1750 {
1751   GstBuffer *result;
1752
1753   result = gst_buffer_span (buf1, 0, buf2, buf1->size + buf2->size);
1754   gst_buffer_unref (buf1);
1755   gst_buffer_unref (buf2);
1756
1757   return result;
1758 }
1759
1760
1761 /**
1762  * gst_buffer_stamp:
1763  * @dest: buffer to stamp
1764  * @src: buffer to stamp from
1765  *
1766  * Copies additional information (timestamps and offsets) from one buffer to
1767  * the other.
1768  */
1769 void
1770 gst_buffer_stamp (GstBuffer * dest, const GstBuffer * src)
1771 {
1772   g_return_if_fail (dest != NULL);
1773   g_return_if_fail (src != NULL);
1774
1775   GST_BUFFER_TIMESTAMP (dest) = GST_BUFFER_TIMESTAMP (src);
1776   GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
1777   GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
1778   GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
1779 }
1780
1781 static gboolean
1782 intersect_caps_func (GstPad * pad, GValue * ret, GstPad * orig)
1783 {
1784   if (pad != orig) {
1785     GstCaps *peercaps, *existing;
1786
1787     existing = g_value_get_pointer (ret);
1788     peercaps = gst_pad_peer_get_caps (pad);
1789     if (peercaps == NULL)
1790       peercaps = gst_caps_new_any ();
1791     g_value_set_pointer (ret, gst_caps_intersect (existing, peercaps));
1792     gst_caps_unref (existing);
1793     gst_caps_unref (peercaps);
1794   }
1795   return TRUE;
1796 }
1797
1798 /**
1799  * gst_pad_proxy_getcaps:
1800  * @pad: a #GstPad to proxy.
1801  *
1802  * Calls gst_pad_get_allowed_caps() for every other pad belonging to the
1803  * same element as @pad, and returns the intersection of the results.
1804  *
1805  * This function is useful as a default getcaps function for an element
1806  * that can handle any stream format, but requires all its pads to have
1807  * the same caps.  Two such elements are tee and aggregator.
1808  *
1809  * Returns: the intersection of the other pads' allowed caps.
1810  */
1811 GstCaps *
1812 gst_pad_proxy_getcaps (GstPad * pad)
1813 {
1814   GstElement *element;
1815   GstCaps *caps, *intersected;
1816   GstIterator *iter;
1817   GstIteratorResult res;
1818   GValue ret = { 0, };
1819
1820   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1821
1822   GST_DEBUG ("proxying getcaps for %s:%s", GST_DEBUG_PAD_NAME (pad));
1823
1824   element = gst_pad_get_parent (pad);
1825   if (element == NULL)
1826     return NULL;
1827
1828   iter = gst_element_iterate_pads (element);
1829
1830   g_value_init (&ret, G_TYPE_POINTER);
1831   g_value_set_pointer (&ret, gst_caps_new_any ());
1832
1833   res = gst_iterator_fold (iter, (GstIteratorFoldFunction) intersect_caps_func,
1834       &ret, pad);
1835   gst_iterator_free (iter);
1836
1837   if (res != GST_ITERATOR_DONE) {
1838     g_warning ("Pad list changed during capsnego for element %s",
1839         GST_ELEMENT_NAME (element));
1840     return NULL;
1841   }
1842
1843   caps = g_value_get_pointer (&ret);
1844   g_value_unset (&ret);
1845
1846   intersected = gst_caps_intersect (caps, gst_pad_get_pad_template_caps (pad));
1847   gst_caps_unref (caps);
1848
1849   return intersected;
1850 }
1851
1852 typedef struct
1853 {
1854   GstPad *orig;
1855   GstCaps *caps;
1856 } LinkData;
1857
1858 static gboolean
1859 link_fold_func (GstPad * pad, GValue * ret, LinkData * data)
1860 {
1861   gboolean success = TRUE;
1862
1863   if (pad != data->orig) {
1864     success = gst_pad_set_caps (pad, data->caps);
1865     g_value_set_boolean (ret, success);
1866   }
1867
1868   return success;
1869 }
1870
1871 /**
1872  * gst_pad_proxy_setcaps
1873  * @pad: a #GstPad to proxy from
1874  * @caps: the #GstCaps to link with
1875  *
1876  * Calls gst_pad_set_caps() for every other pad belonging to the
1877  * same element as @pad.  If gst_pad_set_caps() fails on any pad,
1878  * the proxy setcaps fails. May be used only during negotiation.
1879  *
1880  * Returns: TRUE if sucessful
1881  */
1882 gboolean
1883 gst_pad_proxy_setcaps (GstPad * pad, GstCaps * caps)
1884 {
1885   GstElement *element;
1886   GstIterator *iter;
1887   GstIteratorResult res;
1888   GValue ret = { 0, };
1889   LinkData data;
1890
1891   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1892   g_return_val_if_fail (caps != NULL, FALSE);
1893
1894   GST_DEBUG ("proxying pad link for %s:%s", GST_DEBUG_PAD_NAME (pad));
1895
1896   element = gst_pad_get_parent (pad);
1897
1898   iter = gst_element_iterate_pads (element);
1899
1900   g_value_init (&ret, G_TYPE_BOOLEAN);
1901   g_value_set_boolean (&ret, TRUE);
1902   data.orig = pad;
1903   data.caps = caps;
1904
1905   res = gst_iterator_fold (iter, (GstIteratorFoldFunction) link_fold_func,
1906       &ret, &data);
1907   gst_iterator_free (iter);
1908
1909   if (res != GST_ITERATOR_DONE) {
1910     g_warning ("Pad list changed during proxy_pad_link for element %s",
1911         GST_ELEMENT_NAME (element));
1912     return FALSE;
1913   }
1914
1915   /* ok not to unset the gvalue */
1916   return g_value_get_boolean (&ret);
1917 }
1918
1919 /**
1920  * gst_pad_query_position:
1921  * @pad: a #GstPad to invoke the position query on.
1922  * @format: a pointer to the #GstFormat asked for.
1923  *          On return contains the #GstFormat used.
1924  * @cur: A location in which to store the current position, or NULL.
1925  * @end: A location in which to store the end position (length), or NULL.
1926  *
1927  * Queries a pad for the stream position and length.
1928  *
1929  * Returns: TRUE if the query could be performed.
1930  */
1931 gboolean
1932 gst_pad_query_position (GstPad * pad, GstFormat * format, gint64 * cur,
1933     gint64 * end)
1934 {
1935   GstQuery *query;
1936   gboolean ret;
1937
1938   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1939   g_return_val_if_fail (format != NULL, FALSE);
1940
1941   query = gst_query_new_position (*format);
1942   ret = gst_pad_query (pad, query);
1943
1944   if (ret)
1945     gst_query_parse_position (query, format, cur, end);
1946
1947   gst_query_unref (query);
1948
1949   return ret;
1950 }
1951
1952 /**
1953  * gst_pad_query_convert:
1954  * @pad: a #GstPad to invoke the convert query on.
1955  * @src_format: a #GstFormat to convert from.
1956  * @src_val: a value to convert.
1957  * @dest_format: a pointer to the #GstFormat to convert to. 
1958  * @dest_val: a pointer to the result.
1959  *
1960  * Queries a pad to convert @src_val in @src_format to @dest_format.
1961  *
1962  * Returns: TRUE if the query could be performed.
1963  */
1964 gboolean
1965 gst_pad_query_convert (GstPad * pad, GstFormat src_format, gint64 src_val,
1966     GstFormat * dest_fmt, gint64 * dest_val)
1967 {
1968   GstQuery *query;
1969   gboolean ret;
1970
1971   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
1972   g_return_val_if_fail (dest_fmt != NULL, FALSE);
1973   g_return_val_if_fail (dest_val != NULL, FALSE);
1974
1975   if (*dest_fmt == src_format) {
1976     *dest_val = src_val;
1977     return TRUE;
1978   }
1979
1980   query = gst_query_new_convert (src_format, src_val, *dest_fmt);
1981   ret = gst_pad_query (pad, query);
1982
1983   if (ret)
1984     gst_query_parse_convert (query, NULL, NULL, dest_fmt, dest_val);
1985
1986   gst_query_unref (query);
1987
1988   return ret;
1989 }
1990
1991 /**
1992  * gst_atomic_int_set:
1993  * @atomic_int: pointer to an atomic integer
1994  * @value: value to set
1995  *
1996  * Unconditionally sets the atomic integer to @value.
1997  */
1998 void
1999 gst_atomic_int_set (gint * atomic_int, gint value)
2000 {
2001   int ignore;
2002
2003   *atomic_int = value;
2004   ignore = g_atomic_int_get (atomic_int);
2005 }