gst/gstelement.c: Handle INFO messages from the GST_ELEMENT_INFO macro as well.
[platform/upstream/gstreamer.git] / gst / gstelement.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2004 Wim Taymans <wim@fluendo.com>
4  *
5  * gstelement.c: The base element, all elements derive from this
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 /**
24  * SECTION:gstelement
25  * @short_description: Abstract base class for all pipeline elements
26  * @see_also: #GstElementFactory, #GstPad
27  *
28  * GstElement is the abstract base class needed to construct an element that 
29  * can be used in a GStreamer pipeline. Please refer to the plugin writers
30  * guide for more information on creating #GstElement subclasses.
31  *
32  * The name of a #GstElement can be get with gst_element_get_name() and set with
33  * gst_element_set_name().  For speed, GST_ELEMENT_NAME() can be used in the
34  * core when using the appropriate locking. Do not use this in plug-ins or
35  * applications in order to retain ABI compatibility.
36  *
37  * All elements have pads (of the type #GstPad).  These pads link to pads on
38  * other elements.  #GstBuffer flow between these linked pads.
39  * A #GstElement has a #GList of #GstPad structures for all their input (or sink)
40  * and output (or source) pads.
41  * Core and plug-in writers can add and remove pads with gst_element_add_pad()
42  * and gst_element_remove_pad().
43  *
44  * A pad of an element can be retrieved by name with gst_element_get_pad().
45  * An iterator of all pads can be retrieved with gst_element_iterate_pads().
46  *
47  * Elements can be linked through their pads.
48  * If the link is straightforward, use the gst_element_link()
49  * convenience function to link two elements, or gst_element_link_many()
50  * for more elements in a row.
51  * Use gst_element_link_filtered() to link two elements constrained by
52  * a specified set of #GstCaps.
53  * For finer control, use gst_element_link_pads() and
54  * gst_element_link_pads_filtered() to specify the pads to link on
55  * each element by name.
56  *
57  * Each element has a state (see #GstState).  You can get and set the state
58  * of an element with gst_element_get_state() and gst_element_set_state().
59  * To get a string representation of a #GstState, use
60  * gst_element_state_get_name().
61  *
62  * You can get and set a #GstClock on an element using gst_element_get_clock()
63  * and gst_element_set_clock().
64  * Some elements can provide a clock for the pipeline if
65  * gst_element_provides_clock() returns %TRUE. With the
66  * gst_element_provide_clock() method one can retrieve the clock provided by
67  * such an element.
68  * Not all elements require a clock to operate correctly. If
69  * gst_element_requires_clock() returns %TRUE, a clock should be set on the
70  * element with gst_element_set_clock().
71  *
72  * Note that clock slection and distribution is normally handled by the
73  * toplevel #GstPipeline so the clock functions are only to be used in very
74  * specific situations.
75  *
76  * Last reviewed on 2006-03-12 (0.10.5)
77  */
78
79 #include "gst_private.h"
80 #include <glib.h>
81 #include <stdarg.h>
82 #include <gobject/gvaluecollector.h>
83
84 #include "gstelement.h"
85 #include "gstbus.h"
86 #include "gstmarshal.h"
87 #include "gsterror.h"
88 #include "gstevent.h"
89 #include "gstutils.h"
90 #include "gstinfo.h"
91 #include "gst-i18n-lib.h"
92
93 /* Element signals and args */
94 enum
95 {
96   PAD_ADDED,
97   PAD_REMOVED,
98   NO_MORE_PADS,
99   /* add more above */
100   LAST_SIGNAL
101 };
102
103 enum
104 {
105   ARG_0
106       /* FILL ME */
107 };
108
109 extern void __gst_element_details_clear (GstElementDetails * dp);
110 extern void __gst_element_details_copy (GstElementDetails * dest,
111     const GstElementDetails * src);
112
113 static void gst_element_class_init (GstElementClass * klass);
114 static void gst_element_init (GstElement * element);
115 static void gst_element_base_class_init (gpointer g_class);
116 static void gst_element_base_class_finalize (gpointer g_class);
117
118 static void gst_element_dispose (GObject * object);
119 static void gst_element_finalize (GObject * object);
120
121 static GstStateChangeReturn gst_element_change_state (GstElement * element,
122     GstStateChange transition);
123 static GstStateChangeReturn gst_element_change_state_func (GstElement * element,
124     GstStateChange transition);
125 static GstStateChangeReturn gst_element_get_state_func (GstElement * element,
126     GstState * state, GstState * pending, GstClockTime timeout);
127 static GstStateChangeReturn gst_element_set_state_func (GstElement * element,
128     GstState state);
129 static void gst_element_set_bus_func (GstElement * element, GstBus * bus);
130
131 static gboolean gst_element_default_send_event (GstElement * element,
132     GstEvent * event);
133 static gboolean gst_element_default_query (GstElement * element,
134     GstQuery * query);
135
136 #ifndef GST_DISABLE_LOADSAVE
137 static xmlNodePtr gst_element_save_thyself (GstObject * object,
138     xmlNodePtr parent);
139 static void gst_element_restore_thyself (GstObject * parent, xmlNodePtr self);
140 #endif
141
142 static GstObjectClass *parent_class = NULL;
143 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
144
145 GType
146 gst_element_get_type (void)
147 {
148   static GType gst_element_type = 0;
149
150   if (G_UNLIKELY (gst_element_type == 0)) {
151     static const GTypeInfo element_info = {
152       sizeof (GstElementClass),
153       gst_element_base_class_init,
154       gst_element_base_class_finalize,
155       (GClassInitFunc) gst_element_class_init,
156       NULL,
157       NULL,
158       sizeof (GstElement),
159       0,
160       (GInstanceInitFunc) gst_element_init,
161       NULL
162     };
163
164     gst_element_type = g_type_register_static (GST_TYPE_OBJECT, "GstElement",
165         &element_info, G_TYPE_FLAG_ABSTRACT);
166   }
167   return gst_element_type;
168 }
169
170 static void
171 gst_element_class_init (GstElementClass * klass)
172 {
173   GObjectClass *gobject_class;
174   GstObjectClass *gstobject_class;
175
176   gobject_class = (GObjectClass *) klass;
177   gstobject_class = (GstObjectClass *) klass;
178
179   parent_class = g_type_class_peek_parent (klass);
180
181   /**
182    * GstElement::pad-added:
183    * @gstelement: the object which received the signal
184    * @new_pad: the pad that has been added
185    *
186    * a new #GstPad has been added to the element.
187    */
188   gst_element_signals[PAD_ADDED] =
189       g_signal_new ("pad-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
190       G_STRUCT_OFFSET (GstElementClass, pad_added), NULL, NULL,
191       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
192   /**
193    * GstElement::pad-removed:
194    * @gstelement: the object which received the signal
195    * @old_pad: the pad that has been removed
196    *
197    * a #GstPad has been removed from the element
198    */
199   gst_element_signals[PAD_REMOVED] =
200       g_signal_new ("pad-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
201       G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
202       gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
203   /**
204    * GstElement::no-more-pads:
205    * @gstelement: the object which received the signal
206    *
207    * This signals that the element will not generate more dynamic pads.
208    */
209   gst_element_signals[NO_MORE_PADS] =
210       g_signal_new ("no-more-pads", G_TYPE_FROM_CLASS (klass),
211       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstElementClass, no_more_pads), NULL,
212       NULL, gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
213
214   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_element_dispose);
215   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_element_finalize);
216
217 #ifndef GST_DISABLE_LOADSAVE
218   gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_element_save_thyself);
219   gstobject_class->restore_thyself =
220       GST_DEBUG_FUNCPTR (gst_element_restore_thyself);
221 #endif
222
223   klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state_func);
224   klass->set_state = GST_DEBUG_FUNCPTR (gst_element_set_state_func);
225   klass->get_state = GST_DEBUG_FUNCPTR (gst_element_get_state_func);
226   klass->set_bus = GST_DEBUG_FUNCPTR (gst_element_set_bus_func);
227   klass->query = GST_DEBUG_FUNCPTR (gst_element_default_query);
228   klass->send_event = GST_DEBUG_FUNCPTR (gst_element_default_send_event);
229   klass->numpadtemplates = 0;
230
231   klass->elementfactory = NULL;
232 }
233
234 static void
235 gst_element_base_class_init (gpointer g_class)
236 {
237   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
238
239   memset (&element_class->details, 0, sizeof (GstElementDetails));
240   element_class->padtemplates = NULL;
241 }
242
243 static void
244 gst_element_base_class_finalize (gpointer g_class)
245 {
246   GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
247
248   g_list_foreach (klass->padtemplates, (GFunc) gst_object_unref, NULL);
249   g_list_free (klass->padtemplates);
250   __gst_element_details_clear (&klass->details);
251 }
252
253 static void
254 gst_element_init (GstElement * element)
255 {
256   GST_STATE (element) = GST_STATE_NULL;
257   GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
258   GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
259   GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
260
261   element->state_lock = g_new0 (GStaticRecMutex, 1);
262   g_static_rec_mutex_init (element->state_lock);
263   element->state_cond = g_cond_new ();
264 }
265
266 /**
267  * gst_element_default_error:
268  * @object: a #GObject that signalled the error.
269  * @orig: the #GstObject that initiated the error.
270  * @error: the GError.
271  * @debug: an additional debug information string, or %NULL.
272  *
273  * A default error signal callback to attach to an element.
274  * The user data passed to the g_signal_connect is ignored.
275  *
276  * The default handler will simply print the error string using g_print.
277  *
278  * MT safe.
279  */
280 void
281 gst_element_default_error (GObject * object, GstObject * source, GError * error,
282     gchar * debug)
283 {
284   gchar *name = gst_object_get_path_string (source);
285
286   g_print (_("ERROR: from element %s: %s\n"), name, error->message);
287   if (debug)
288     g_print (_("Additional debug info:\n%s\n"), debug);
289
290   g_free (name);
291 }
292
293 /**
294  * gst_element_release_request_pad:
295  * @element: a #GstElement to release the request pad of.
296  * @pad: the #GstPad to release.
297  *
298  * Makes the element free the previously requested pad as obtained
299  * with gst_element_get_request_pad().
300  *
301  * MT safe.
302  */
303 void
304 gst_element_release_request_pad (GstElement * element, GstPad * pad)
305 {
306   GstElementClass *oclass;
307
308   g_return_if_fail (GST_IS_ELEMENT (element));
309   g_return_if_fail (GST_IS_PAD (pad));
310
311   oclass = GST_ELEMENT_GET_CLASS (element);
312
313   /* if the element implements a custom release function we call that, else we
314    * simply remove the pad from the element */
315   if (oclass->release_pad)
316     (oclass->release_pad) (element, pad);
317   else
318     gst_element_remove_pad (element, pad);
319 }
320
321 /**
322  * gst_element_requires_clock:
323  * @element: a #GstElement to query
324  *
325  * Query if the element requires a clock.
326  *
327  * Returns: %TRUE if the element requires a clock
328  *
329  * MT safe.
330  */
331 gboolean
332 gst_element_requires_clock (GstElement * element)
333 {
334   gboolean result;
335
336   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
337
338   result = (GST_ELEMENT_GET_CLASS (element)->set_clock != NULL);
339
340   return result;
341 }
342
343 /**
344  * gst_element_provides_clock:
345  * @element: a #GstElement to query
346  *
347  * Query if the element provides a clock. A #GstClock provided by an
348  * element can be used as the global #GstClock for the pipeline. 
349  * An element that can provide a clock is only required to do so in the PAUSED
350  * state, this means when it is fully negotiated and has allocated the resources
351  * to operate the clock.
352  *
353  * Returns: %TRUE if the element provides a clock
354  *
355  * MT safe.
356  */
357 gboolean
358 gst_element_provides_clock (GstElement * element)
359 {
360   gboolean result;
361
362   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
363
364   result = (GST_ELEMENT_GET_CLASS (element)->provide_clock != NULL);
365
366   return result;
367 }
368
369 /**
370  * gst_element_provide_clock:
371  * @element: a #GstElement to query
372  *
373  * Get the clock provided by the given element. 
374  * <note>An element is only required to provide a clock in the PAUSED
375  * state. Some elements can provide a clock in other states.</note>
376  *
377  * Returns: the GstClock provided by the element or %NULL
378  * if no clock could be provided.  Unref after usage.
379  *
380  * MT safe.
381  */
382 GstClock *
383 gst_element_provide_clock (GstElement * element)
384 {
385   GstClock *result = NULL;
386   GstElementClass *oclass;
387
388   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
389
390   oclass = GST_ELEMENT_GET_CLASS (element);
391
392   if (oclass->provide_clock)
393     result = oclass->provide_clock (element);
394
395   return result;
396 }
397
398 /**
399  * gst_element_set_clock:
400  * @element: a #GstElement to set the clock for.
401  * @clock: the #GstClock to set for the element.
402  *
403  * Sets the clock for the element. This function increases the
404  * refcount on the clock. Any previously set clock on the object
405  * is unreffed.
406  *
407  * Returns: %TRUE if the element accepted the clock. An element can refuse a
408  * clock when it, for example, is not able to slave its internal clock to the
409  * @clock or when it requires a specific clock to operate.
410  *
411  * MT safe.
412  */
413 gboolean
414 gst_element_set_clock (GstElement * element, GstClock * clock)
415 {
416   GstElementClass *oclass;
417   gboolean res = TRUE;
418   GstClock **clock_p;
419
420   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
421
422   oclass = GST_ELEMENT_GET_CLASS (element);
423
424   GST_DEBUG_OBJECT (element, "setting clock %p", clock);
425
426   if (oclass->set_clock)
427     res = oclass->set_clock (element, clock);
428
429   if (res) {
430     /* only update the clock pointer if the element accepted the clock */
431     GST_OBJECT_LOCK (element);
432     clock_p = &element->clock;
433     gst_object_replace ((GstObject **) clock_p, (GstObject *) clock);
434     GST_OBJECT_UNLOCK (element);
435   }
436   return res;
437 }
438
439 /**
440  * gst_element_get_clock:
441  * @element: a #GstElement to get the clock of.
442  *
443  * Gets the currently configured clock of the element. This is the clock as was
444  * last set with gst_element_set_clock().
445  *
446  * Returns: the #GstClock of the element. unref after usage.
447  *
448  * MT safe.
449  */
450 GstClock *
451 gst_element_get_clock (GstElement * element)
452 {
453   GstClock *result;
454
455   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
456
457   GST_OBJECT_LOCK (element);
458   if ((result = element->clock))
459     gst_object_ref (result);
460   GST_OBJECT_UNLOCK (element);
461
462   return result;
463 }
464
465 /**
466  * gst_element_set_base_time:
467  * @element: a #GstElement.
468  * @time: the base time to set.
469  *
470  * Set the base time of an element. See gst_element_get_base_time().
471  *
472  * MT safe.
473  */
474 void
475 gst_element_set_base_time (GstElement * element, GstClockTime time)
476 {
477   g_return_if_fail (GST_IS_ELEMENT (element));
478
479   GST_OBJECT_LOCK (element);
480   element->base_time = time;
481   GST_OBJECT_UNLOCK (element);
482
483   GST_DEBUG_OBJECT (element, "set base_time=%" GST_TIME_FORMAT,
484       GST_TIME_ARGS (time));
485 }
486
487 /**
488  * gst_element_get_base_time:
489  * @element: a #GstElement.
490  *
491  * Returns the base time of the element. The base time is the
492  * absolute time of the clock when this element was last put to
493  * PLAYING. Subtracting the base time from the clock time gives
494  * the stream time of the element.
495  *
496  * Returns: the base time of the element.
497  *
498  * MT safe.
499  */
500 GstClockTime
501 gst_element_get_base_time (GstElement * element)
502 {
503   GstClockTime result;
504
505   g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_TIME_NONE);
506
507   GST_OBJECT_LOCK (element);
508   result = element->base_time;
509   GST_OBJECT_UNLOCK (element);
510
511   return result;
512 }
513
514 #ifndef GST_DISABLE_INDEX
515 /**
516  * gst_element_is_indexable:
517  * @element: a #GstElement.
518  *
519  * Queries if the element can be indexed.
520  *
521  * Returns: TRUE if the element can be indexed.
522  *
523  * MT safe.
524  */
525 gboolean
526 gst_element_is_indexable (GstElement * element)
527 {
528   gboolean result;
529
530   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
531
532   result = (GST_ELEMENT_GET_CLASS (element)->set_index != NULL);
533
534   return result;
535 }
536
537 /**
538  * gst_element_set_index:
539  * @element: a #GstElement.
540  * @index: a #GstIndex.
541  *
542  * Set @index on the element. The refcount of the index
543  * will be increased, any previously set index is unreffed.
544  *
545  * MT safe.
546  */
547 void
548 gst_element_set_index (GstElement * element, GstIndex * index)
549 {
550   GstElementClass *oclass;
551
552   g_return_if_fail (GST_IS_ELEMENT (element));
553   g_return_if_fail (GST_IS_INDEX (index));
554
555   oclass = GST_ELEMENT_GET_CLASS (element);
556
557   if (oclass->set_index)
558     oclass->set_index (element, index);
559 }
560
561 /**
562  * gst_element_get_index:
563  * @element: a #GstElement.
564  *
565  * Gets the index from the element.
566  *
567  * Returns: a #GstIndex or %NULL when no index was set on the
568  * element. unref after usage.
569  *
570  * MT safe.
571  */
572 GstIndex *
573 gst_element_get_index (GstElement * element)
574 {
575   GstElementClass *oclass;
576   GstIndex *result = NULL;
577
578   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
579
580   oclass = GST_ELEMENT_GET_CLASS (element);
581
582   if (oclass->get_index)
583     result = oclass->get_index (element);
584
585   return result;
586 }
587 #endif
588
589 /**
590  * gst_element_add_pad:
591  * @element: a #GstElement to add the pad to.
592  * @pad: the #GstPad to add to the element.
593  *
594  * Adds a pad (link point) to @element. @pad's parent will be set to @element;
595  * see gst_object_set_parent() for refcounting information.
596  *
597  * Pads are not automatically activated so elements should perform the needed
598  * steps to activate the pad in case this pad is added in the PAUSED or PLAYING
599  * state. See gst_pad_set_active() for more information about activating pads.
600  *
601  * The pad and the element should be unlocked when calling this function.
602  *
603  * This function will emit the #GstElement::pad-added signal on the element.
604  *
605  * Returns: %TRUE if the pad could be added. This function can fail when
606  * a pad with the same name already existed or the pad already had another
607  * parent.
608  *
609  * MT safe.
610  */
611 gboolean
612 gst_element_add_pad (GstElement * element, GstPad * pad)
613 {
614   gchar *pad_name;
615   gboolean flushing;
616
617   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
618   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
619
620   /* locking pad to look at the name */
621   GST_OBJECT_LOCK (pad);
622   pad_name = g_strdup (GST_PAD_NAME (pad));
623   GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "adding pad '%s'",
624       GST_STR_NULL (pad_name));
625   flushing = GST_PAD_IS_FLUSHING (pad);
626   GST_OBJECT_UNLOCK (pad);
627
628   /* then check to see if there's already a pad by that name here */
629   GST_OBJECT_LOCK (element);
630   if (G_UNLIKELY (!gst_object_check_uniqueness (element->pads, pad_name)))
631     goto name_exists;
632
633   /* try to set the pad's parent */
634   if (G_UNLIKELY (!gst_object_set_parent (GST_OBJECT_CAST (pad),
635               GST_OBJECT_CAST (element))))
636     goto had_parent;
637
638   /* check for flushing pads */
639   if (flushing && (GST_STATE (element) > GST_STATE_READY ||
640           GST_STATE_NEXT (element) == GST_STATE_PAUSED)) {
641     g_warning ("adding flushing pad '%s' to running element '%s'",
642         GST_STR_NULL (pad_name), GST_ELEMENT_NAME (element));
643     /* unset flushing */
644     GST_OBJECT_LOCK (pad);
645     GST_PAD_UNSET_FLUSHING (pad);
646     GST_OBJECT_UNLOCK (pad);
647   }
648
649   g_free (pad_name);
650
651   /* add it to the list */
652   switch (gst_pad_get_direction (pad)) {
653     case GST_PAD_SRC:
654       element->srcpads = g_list_prepend (element->srcpads, pad);
655       element->numsrcpads++;
656       break;
657     case GST_PAD_SINK:
658       element->sinkpads = g_list_prepend (element->sinkpads, pad);
659       element->numsinkpads++;
660       break;
661     default:
662       goto no_direction;
663   }
664   element->pads = g_list_prepend (element->pads, pad);
665   element->numpads++;
666   element->pads_cookie++;
667   GST_OBJECT_UNLOCK (element);
668
669   /* emit the PAD_ADDED signal */
670   g_signal_emit (element, gst_element_signals[PAD_ADDED], 0, pad);
671
672   return TRUE;
673
674   /* ERROR cases */
675 name_exists:
676   {
677     g_critical ("Padname %s is not unique in element %s, not adding",
678         pad_name, GST_ELEMENT_NAME (element));
679     GST_OBJECT_UNLOCK (element);
680     g_free (pad_name);
681     return FALSE;
682   }
683 had_parent:
684   {
685     g_critical
686         ("Pad %s already has parent when trying to add to element %s",
687         pad_name, GST_ELEMENT_NAME (element));
688     GST_OBJECT_UNLOCK (element);
689     g_free (pad_name);
690     return FALSE;
691   }
692 no_direction:
693   {
694     GST_OBJECT_LOCK (pad);
695     g_critical
696         ("Trying to add pad %s to element %s, but it has no direction",
697         GST_OBJECT_NAME (pad), GST_ELEMENT_NAME (element));
698     GST_OBJECT_UNLOCK (pad);
699     GST_OBJECT_UNLOCK (element);
700     return FALSE;
701   }
702 }
703
704 /**
705  * gst_element_remove_pad:
706  * @element: a #GstElement to remove pad from.
707  * @pad: the #GstPad to remove from the element.
708  *
709  * Removes @pad from @element. @pad will be destroyed if it has not been
710  * referenced elsewhere using gst_object_unparent().
711  *
712  * This function is used by plugin developers and should not be used
713  * by applications. Pads that were dynamically requested from elements
714  * with gst_element_get_request_pad() should be released with the
715  * gst_element_release_request_pad() function instead.
716  *
717  * Pads are not automatically deactivated so elements should perform the needed
718  * steps to deactivate the pad in case this pad is removed in the PAUSED or
719  * PLAYING state. See gst_pad_set_active() for more information about
720  * deactivating pads.
721  *
722  * The pad and the element should be unlocked when calling this function.
723  *
724  * This function will emit the #GstElement::pad-removed signal on the element.
725  *
726  * Returns: %TRUE if the pad could be removed. Can return %FALSE if the
727  * pad does not belong to the provided element.
728  *
729  * MT safe.
730  */
731 gboolean
732 gst_element_remove_pad (GstElement * element, GstPad * pad)
733 {
734   GstPad *peer;
735
736   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
737   g_return_val_if_fail (GST_IS_PAD (pad), FALSE);
738
739   /* locking pad to look at the name and parent */
740   GST_OBJECT_LOCK (pad);
741   GST_CAT_INFO_OBJECT (GST_CAT_ELEMENT_PADS, element, "removing pad '%s'",
742       GST_STR_NULL (GST_PAD_NAME (pad)));
743
744   if (G_UNLIKELY (GST_PAD_PARENT (pad) != element))
745     goto not_our_pad;
746   GST_OBJECT_UNLOCK (pad);
747
748   /* unlink */
749   if ((peer = gst_pad_get_peer (pad))) {
750     /* window for MT unsafeness, someone else could unlink here
751      * and then we call unlink with wrong pads. The unlink
752      * function would catch this and safely return failed. */
753     if (GST_PAD_IS_SRC (pad))
754       gst_pad_unlink (pad, peer);
755     else
756       gst_pad_unlink (peer, pad);
757
758     gst_object_unref (peer);
759   }
760
761   GST_OBJECT_LOCK (element);
762   /* remove it from the list */
763   switch (gst_pad_get_direction (pad)) {
764     case GST_PAD_SRC:
765       element->srcpads = g_list_remove (element->srcpads, pad);
766       element->numsrcpads--;
767       break;
768     case GST_PAD_SINK:
769       element->sinkpads = g_list_remove (element->sinkpads, pad);
770       element->numsinkpads--;
771       break;
772     default:
773       g_critical ("Removing pad without direction???");
774       break;
775   }
776   element->pads = g_list_remove (element->pads, pad);
777   element->numpads--;
778   element->pads_cookie++;
779   GST_OBJECT_UNLOCK (element);
780
781   /* emit the PAD_REMOVED signal before unparenting and losing the last ref. */
782   g_signal_emit (element, gst_element_signals[PAD_REMOVED], 0, pad);
783
784   gst_object_unparent (GST_OBJECT_CAST (pad));
785
786   return TRUE;
787
788   /* ERRORS */
789 not_our_pad:
790   {
791     /* FIXME, locking order? */
792     GST_OBJECT_LOCK (element);
793     g_critical ("Padname %s:%s does not belong to element %s when removing",
794         GST_DEBUG_PAD_NAME (pad), GST_ELEMENT_NAME (element));
795     GST_OBJECT_UNLOCK (element);
796     GST_OBJECT_UNLOCK (pad);
797     return FALSE;
798   }
799 }
800
801 /**
802  * gst_element_no_more_pads:
803  * @element: a #GstElement
804  *
805  * Use this function to signal that the element does not expect any more pads
806  * to show up in the current pipeline. This function should be called whenever
807  * pads have been added by the element itself. Elements with #GST_PAD_SOMETIMES
808  * pad templates use this in combination with autopluggers to figure out that
809  * the element is done initializing its pads.
810  *
811  * This function emits the #GstElement::no-more-pads signal.
812  *
813  * MT safe.
814  */
815 void
816 gst_element_no_more_pads (GstElement * element)
817 {
818   g_return_if_fail (GST_IS_ELEMENT (element));
819
820   g_signal_emit (element, gst_element_signals[NO_MORE_PADS], 0);
821 }
822
823 static gint
824 pad_compare_name (GstPad * pad1, const gchar * name)
825 {
826   gint result;
827
828   GST_OBJECT_LOCK (pad1);
829   result = strcmp (GST_PAD_NAME (pad1), name);
830   GST_OBJECT_UNLOCK (pad1);
831
832   return result;
833 }
834
835 /**
836  * gst_element_get_static_pad:
837  * @element: a #GstElement to find a static pad of.
838  * @name: the name of the static #GstPad to retrieve.
839  *
840  * Retrieves a pad from @element by name. This version only retrieves
841  * already-existing (i.e. 'static') pads.
842  *
843  * Returns: the requested #GstPad if found, otherwise %NULL. unref after
844  * usage.
845  *
846  * MT safe.
847  */
848 GstPad *
849 gst_element_get_static_pad (GstElement * element, const gchar * name)
850 {
851   GList *find;
852   GstPad *result = NULL;
853
854   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
855   g_return_val_if_fail (name != NULL, NULL);
856
857   GST_OBJECT_LOCK (element);
858   find =
859       g_list_find_custom (element->pads, name, (GCompareFunc) pad_compare_name);
860   if (find) {
861     result = GST_PAD_CAST (find->data);
862     gst_object_ref (result);
863   }
864
865   if (result == NULL) {
866     GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "no such pad '%s' in element \"%s\"",
867         name, GST_ELEMENT_NAME (element));
868   } else {
869     GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
870         GST_ELEMENT_NAME (element), name);
871   }
872   GST_OBJECT_UNLOCK (element);
873
874   return result;
875 }
876
877 static GstPad *
878 gst_element_request_pad (GstElement * element, GstPadTemplate * templ,
879     const gchar * name)
880 {
881   GstPad *newpad = NULL;
882   GstElementClass *oclass;
883
884   oclass = GST_ELEMENT_GET_CLASS (element);
885
886   if (oclass->request_new_pad)
887     newpad = (oclass->request_new_pad) (element, templ, name);
888
889   if (newpad)
890     gst_object_ref (newpad);
891
892   return newpad;
893 }
894
895 /**
896  * gst_element_get_request_pad:
897  * @element: a #GstElement to find a request pad of.
898  * @name: the name of the request #GstPad to retrieve.
899  *
900  * Retrieves a pad from the element by name. This version only retrieves
901  * request pads. The pad should be released with 
902  * gst_element_release_request_pad().
903  *
904  * Returns: requested #GstPad if found, otherwise %NULL. Release after usage.
905  */
906 GstPad *
907 gst_element_get_request_pad (GstElement * element, const gchar * name)
908 {
909   GstPadTemplate *templ = NULL;
910   GstPad *pad;
911   const gchar *req_name = NULL;
912   gboolean templ_found = FALSE;
913   GList *list;
914   gint n;
915   const gchar *data;
916   gchar *str, *endptr = NULL;
917   GstElementClass *class;
918
919   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
920   g_return_val_if_fail (name != NULL, NULL);
921
922   class = GST_ELEMENT_GET_CLASS (element);
923
924   if (strstr (name, "%")) {
925     templ = gst_element_class_get_pad_template (class, name);
926     req_name = NULL;
927     if (templ)
928       templ_found = TRUE;
929   } else {
930     list = gst_element_class_get_pad_template_list (class);
931     while (!templ_found && list) {
932       templ = (GstPadTemplate *) list->data;
933       if (templ->presence == GST_PAD_REQUEST) {
934         /* Because of sanity checks in gst_pad_template_new(), we know that %s
935            and %d, occurring at the end of the name_template, are the only
936            possibilities. */
937         GST_CAT_DEBUG (GST_CAT_PADS, "comparing %s to %s", name,
938             templ->name_template);
939         if ((str = strchr (templ->name_template, '%'))
940             && strncmp (templ->name_template, name,
941                 str - templ->name_template) == 0
942             && strlen (name) > str - templ->name_template) {
943           data = name + (str - templ->name_template);
944           if (*(str + 1) == 'd') {
945             /* it's an int */
946             n = (gint) strtol (data, &endptr, 10);
947             if (endptr && *endptr == '\0') {
948               templ_found = TRUE;
949               req_name = name;
950               break;
951             }
952           } else {
953             /* it's a string */
954             templ_found = TRUE;
955             req_name = name;
956             break;
957           }
958         }
959       }
960       list = list->next;
961     }
962   }
963
964   if (!templ_found)
965     return NULL;
966
967   pad = gst_element_request_pad (element, templ, req_name);
968
969   return pad;
970 }
971
972 /**
973  * gst_element_get_pad:
974  * @element: a #GstElement.
975  * @name: the name of the pad to retrieve.
976  *
977  * Retrieves a pad from @element by name. Tries gst_element_get_static_pad()
978  * first, then gst_element_get_request_pad().
979  *
980  * <note>Usage of this function is not recommended as it is unclear if the reference
981  * to the result pad should be released with gst_object_unref() in case of a static pad
982  * or gst_element_release_request_pad() in case of a request pad.</note>
983  *
984  * Returns: the #GstPad if found, otherwise %NULL. Unref or Release after usage,
985  * depending on the type of the pad.
986  */
987 GstPad *
988 gst_element_get_pad (GstElement * element, const gchar * name)
989 {
990   GstPad *pad;
991
992   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
993   g_return_val_if_fail (name != NULL, NULL);
994
995   pad = gst_element_get_static_pad (element, name);
996   if (!pad)
997     pad = gst_element_get_request_pad (element, name);
998
999   return pad;
1000 }
1001
1002 static GstIteratorItem
1003 iterate_pad (GstIterator * it, GstPad * pad)
1004 {
1005   gst_object_ref (pad);
1006   return GST_ITERATOR_ITEM_PASS;
1007 }
1008
1009 static GstIterator *
1010 gst_element_iterate_pad_list (GstElement * element, GList ** padlist)
1011 {
1012   GstIterator *result;
1013
1014   GST_OBJECT_LOCK (element);
1015   gst_object_ref (element);
1016   result = gst_iterator_new_list (GST_TYPE_PAD,
1017       GST_OBJECT_GET_LOCK (element),
1018       &element->pads_cookie,
1019       padlist,
1020       element,
1021       (GstIteratorItemFunction) iterate_pad,
1022       (GstIteratorDisposeFunction) gst_object_unref);
1023   GST_OBJECT_UNLOCK (element);
1024
1025   return result;
1026 }
1027
1028 /**
1029  * gst_element_iterate_pads:
1030  * @element: a #GstElement to iterate pads of.
1031  *
1032  * Retrieves an iterattor of @element's pads. The iterator should
1033  * be freed after usage.
1034  *
1035  * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1036  *
1037  * MT safe.
1038  */
1039 GstIterator *
1040 gst_element_iterate_pads (GstElement * element)
1041 {
1042   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1043
1044   return gst_element_iterate_pad_list (element, &element->pads);
1045 }
1046
1047 /**
1048  * gst_element_iterate_src_pads:
1049  * @element: a #GstElement.
1050  *
1051  * Retrieves an iterator of @element's source pads.
1052  *
1053  * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1054  *
1055  * MT safe.
1056  */
1057 GstIterator *
1058 gst_element_iterate_src_pads (GstElement * element)
1059 {
1060   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1061
1062   return gst_element_iterate_pad_list (element, &element->srcpads);
1063 }
1064
1065 /**
1066  * gst_element_iterate_sink_pads:
1067  * @element: a #GstElement.
1068  *
1069  * Retrieves an iterator of @element's sink pads.
1070  *
1071  * Returns: the #GstIterator of #GstPad. Unref each pad after use.
1072  *
1073  * MT safe.
1074  */
1075 GstIterator *
1076 gst_element_iterate_sink_pads (GstElement * element)
1077 {
1078   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1079
1080   return gst_element_iterate_pad_list (element, &element->sinkpads);
1081 }
1082
1083 /**
1084  * gst_element_class_add_pad_template:
1085  * @klass: the #GstElementClass to add the pad template to.
1086  * @templ: a #GstPadTemplate to add to the element class.
1087  *
1088  * Adds a padtemplate to an element class. This is mainly used in the _base_init
1089  * functions of classes.
1090  */
1091 void
1092 gst_element_class_add_pad_template (GstElementClass * klass,
1093     GstPadTemplate * templ)
1094 {
1095   g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1096   g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
1097
1098   /* avoid registering pad templates with the same name */
1099   g_return_if_fail (gst_element_class_get_pad_template (klass,
1100           templ->name_template) == NULL);
1101
1102   klass->padtemplates = g_list_append (klass->padtemplates,
1103       gst_object_ref (templ));
1104   klass->numpadtemplates++;
1105 }
1106
1107 /**
1108  * gst_element_class_set_details:
1109  * @klass: class to set details for
1110  * @details: details to set
1111  *
1112  * Sets the detailed information for a #GstElementClass.
1113  * <note>This function is for use in _base_init functions only.</note>
1114  *
1115  * The @details are copied.
1116  */
1117 void
1118 gst_element_class_set_details (GstElementClass * klass,
1119     const GstElementDetails * details)
1120 {
1121   g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1122   g_return_if_fail (GST_IS_ELEMENT_DETAILS (details));
1123
1124   __gst_element_details_copy (&klass->details, details);
1125 }
1126
1127 /**
1128  * gst_element_class_get_pad_template_list:
1129  * @element_class: a #GstElementClass to get pad templates of.
1130  *
1131  * Retrieves a list of the pad templates associated with @element_class. The
1132  * list must not be modified by the calling code.
1133  * <note>If you use this function in the #GInstanceInitFunc of an object class
1134  * that has subclasses, make sure to pass the g_class parameter of the
1135  * #GInstanceInitFunc here.</note>
1136  *
1137  * Returns: the #GList of padtemplates.
1138  */
1139 GList *
1140 gst_element_class_get_pad_template_list (GstElementClass * element_class)
1141 {
1142   g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1143
1144   return element_class->padtemplates;
1145 }
1146
1147 /**
1148  * gst_element_class_get_pad_template:
1149  * @element_class: a #GstElementClass to get the pad template of.
1150  * @name: the name of the #GstPadTemplate to get.
1151  *
1152  * Retrieves a padtemplate from @element_class with the given name.
1153  * <note>If you use this function in the #GInstanceInitFunc of an object class
1154  * that has subclasses, make sure to pass the g_class parameter of the
1155  * #GInstanceInitFunc here.</note>
1156  *
1157  * Returns: the #GstPadTemplate with the given name, or %NULL if none was found.
1158  * No unreferencing is necessary.
1159  */
1160 GstPadTemplate *
1161 gst_element_class_get_pad_template (GstElementClass * element_class,
1162     const gchar * name)
1163 {
1164   GList *padlist;
1165
1166   g_return_val_if_fail (GST_IS_ELEMENT_CLASS (element_class), NULL);
1167   g_return_val_if_fail (name != NULL, NULL);
1168
1169   padlist = gst_element_class_get_pad_template_list (element_class);
1170
1171   while (padlist) {
1172     GstPadTemplate *padtempl = (GstPadTemplate *) padlist->data;
1173
1174     if (strcmp (padtempl->name_template, name) == 0)
1175       return padtempl;
1176
1177     padlist = g_list_next (padlist);
1178   }
1179
1180   return NULL;
1181 }
1182
1183 /* get a random pad on element of the given direction.
1184  * The pad is random in a sense that it is the first pad that is (optionaly) linked.
1185  */
1186 static GstPad *
1187 gst_element_get_random_pad (GstElement * element, gboolean need_linked,
1188     GstPadDirection dir)
1189 {
1190   GstPad *result = NULL;
1191   GList *pads;
1192
1193   GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "getting a random pad");
1194
1195   switch (dir) {
1196     case GST_PAD_SRC:
1197       GST_OBJECT_LOCK (element);
1198       pads = element->srcpads;
1199       break;
1200     case GST_PAD_SINK:
1201       GST_OBJECT_LOCK (element);
1202       pads = element->sinkpads;
1203       break;
1204     default:
1205       goto wrong_direction;
1206   }
1207   for (; pads; pads = g_list_next (pads)) {
1208     GstPad *pad = GST_PAD_CAST (pads->data);
1209
1210     GST_OBJECT_LOCK (pad);
1211     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "checking pad %s:%s",
1212         GST_DEBUG_PAD_NAME (pad));
1213
1214     if (need_linked && !GST_PAD_IS_LINKED (pad)) {
1215       /* if we require a linked pad, and it is not linked, continue the
1216        * search */
1217       GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "pad %s:%s is not linked",
1218           GST_DEBUG_PAD_NAME (pad));
1219       GST_OBJECT_UNLOCK (pad);
1220       continue;
1221     } else {
1222       /* found a pad, stop search */
1223       GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "found pad %s:%s",
1224           GST_DEBUG_PAD_NAME (pad));
1225       GST_OBJECT_UNLOCK (pad);
1226       result = pad;
1227       break;
1228     }
1229   }
1230   if (result)
1231     gst_object_ref (result);
1232
1233   GST_OBJECT_UNLOCK (element);
1234
1235   return result;
1236
1237   /* ERROR handling */
1238 wrong_direction:
1239   {
1240     g_warning ("unknown pad direction %d", dir);
1241     return NULL;
1242   }
1243 }
1244
1245 static gboolean
1246 gst_element_default_send_event (GstElement * element, GstEvent * event)
1247 {
1248   gboolean result = FALSE;
1249   GstPad *pad;
1250
1251   pad = GST_EVENT_IS_DOWNSTREAM (event) ?
1252       gst_element_get_random_pad (element, TRUE, GST_PAD_SRC) :
1253       gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1254
1255   if (pad) {
1256     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS,
1257         "pushing %s event to random %s pad %s:%s",
1258         GST_EVENT_TYPE_NAME (event),
1259         (GST_PAD_DIRECTION (pad) == GST_PAD_SRC ? "src" : "sink"),
1260         GST_DEBUG_PAD_NAME (pad));
1261
1262     result = gst_pad_push_event (pad, event);
1263     gst_object_unref (pad);
1264   } else {
1265     GST_CAT_INFO (GST_CAT_ELEMENT_PADS, "can't send %s event on element %s",
1266         GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1267   }
1268   return result;
1269 }
1270
1271 /**
1272  * gst_element_send_event:
1273  * @element: a #GstElement to send the event to.
1274  * @event: the #GstEvent to send to the element.
1275  *
1276  * Sends an event to an element. If the element doesn't implement an
1277  * event handler, the event will be pushed on a random linked sink pad for
1278  * upstream events or a random linked source pad for downstream events. 
1279  *
1280  * This function takes owership of the provided event so you should
1281  * gst_event_ref() it if you want to reuse the event after this call.
1282  *
1283  * Returns: %TRUE if the event was handled.
1284  *
1285  * MT safe.
1286  */
1287 gboolean
1288 gst_element_send_event (GstElement * element, GstEvent * event)
1289 {
1290   GstElementClass *oclass;
1291   gboolean result = FALSE;
1292
1293   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1294   g_return_val_if_fail (event != NULL, FALSE);
1295
1296   oclass = GST_ELEMENT_GET_CLASS (element);
1297
1298   if (oclass->send_event) {
1299     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send %s event on element %s",
1300         GST_EVENT_TYPE_NAME (event), GST_ELEMENT_NAME (element));
1301     result = oclass->send_event (element, event);
1302   } else {
1303     result = gst_element_default_send_event (element, event);
1304   }
1305   return result;
1306 }
1307
1308 /**
1309  * gst_element_seek:
1310  * @element: a #GstElement to send the event to.
1311  * @rate: The new playback rate
1312  * @format: The format of the seek values
1313  * @flags: The optional seek flags.
1314  * @cur_type: The type and flags for the new current position
1315  * @cur: The value of the new current position
1316  * @stop_type: The type and flags for the new stop position
1317  * @stop: The value of the new stop position
1318  *
1319  * Sends a seek event to an element. See gst_event_new_seek() for the details of
1320  * the parameters. The seek event is sent to the element using
1321  * gst_element_send_event().
1322  *
1323  * Returns: %TRUE if the event was handled.
1324  *
1325  * MT safe.
1326  */
1327 gboolean
1328 gst_element_seek (GstElement * element, gdouble rate, GstFormat format,
1329     GstSeekFlags flags, GstSeekType cur_type, gint64 cur,
1330     GstSeekType stop_type, gint64 stop)
1331 {
1332   GstEvent *event;
1333   gboolean result;
1334
1335   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1336
1337   event =
1338       gst_event_new_seek (rate, format, flags, cur_type, cur, stop_type, stop);
1339   result = gst_element_send_event (element, event);
1340
1341   return result;
1342 }
1343
1344 /**
1345  * gst_element_get_query_types:
1346  * @element: a #GstElement to query
1347  *
1348  * Get an array of query types from the element.
1349  * If the element doesn't implement a query types function,
1350  * the query will be forwarded to the peer of a random linked sink pad.
1351  *
1352  * Returns: An array of #GstQueryType elements that should not
1353  * be freed or modified.
1354  *
1355  * MT safe.
1356  */
1357 const GstQueryType *
1358 gst_element_get_query_types (GstElement * element)
1359 {
1360   GstElementClass *oclass;
1361   const GstQueryType *result = NULL;
1362
1363   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1364
1365   oclass = GST_ELEMENT_GET_CLASS (element);
1366
1367   if (oclass->get_query_types) {
1368     result = oclass->get_query_types (element);
1369   } else {
1370     GstPad *pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1371
1372     if (pad) {
1373       GstPad *peer = gst_pad_get_peer (pad);
1374
1375       if (peer) {
1376         result = gst_pad_get_query_types (peer);
1377
1378         gst_object_unref (peer);
1379       }
1380       gst_object_unref (pad);
1381     }
1382   }
1383   return result;
1384 }
1385
1386 static gboolean
1387 gst_element_default_query (GstElement * element, GstQuery * query)
1388 {
1389   gboolean result = FALSE;
1390   GstPad *pad;
1391
1392   pad = gst_element_get_random_pad (element, FALSE, GST_PAD_SRC);
1393   if (pad) {
1394     result = gst_pad_query (pad, query);
1395
1396     gst_object_unref (pad);
1397   } else {
1398     pad = gst_element_get_random_pad (element, TRUE, GST_PAD_SINK);
1399     if (pad) {
1400       GstPad *peer = gst_pad_get_peer (pad);
1401
1402       if (peer) {
1403         result = gst_pad_query (peer, query);
1404
1405         gst_object_unref (peer);
1406       }
1407       gst_object_unref (pad);
1408     }
1409   }
1410   return result;
1411 }
1412
1413 /**
1414  * gst_element_query:
1415  * @element: a #GstElement to perform the query on.
1416  * @query: the #GstQuery.
1417  *
1418  * Performs a query on the given element.
1419  *
1420  * For elements that don't implement a query handler, this function
1421  * forwards the query to a random srcpad or to the peer of a
1422  * random linked sinkpad of this element.
1423  *
1424  * Returns: TRUE if the query could be performed.
1425  *
1426  * MT safe.
1427  */
1428 gboolean
1429 gst_element_query (GstElement * element, GstQuery * query)
1430 {
1431   GstElementClass *oclass;
1432   gboolean result = FALSE;
1433
1434   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1435   g_return_val_if_fail (query != NULL, FALSE);
1436
1437   oclass = GST_ELEMENT_GET_CLASS (element);
1438
1439   if (oclass->query) {
1440     GST_CAT_DEBUG (GST_CAT_ELEMENT_PADS, "send query on element %s",
1441         GST_ELEMENT_NAME (element));
1442     result = oclass->query (element, query);
1443   } else {
1444     result = gst_element_default_query (element, query);
1445   }
1446   return result;
1447 }
1448
1449 /**
1450  * gst_element_post_message:
1451  * @element: a #GstElement posting the message
1452  * @message: a #GstMessage to post
1453  *
1454  * Post a message on the element's #GstBus. This function takes ownership of the
1455  * message; if you want to access the message after this call, you should add an
1456  * additional reference before calling.
1457  *
1458  * Returns: %TRUE if the message was successfully posted. The function returns
1459  * %FALSE if the element did not have a bus.
1460  *
1461  * MT safe.
1462  */
1463 gboolean
1464 gst_element_post_message (GstElement * element, GstMessage * message)
1465 {
1466   GstBus *bus;
1467   gboolean result = FALSE;
1468
1469   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1470   g_return_val_if_fail (message != NULL, FALSE);
1471
1472   GST_OBJECT_LOCK (element);
1473   bus = element->bus;
1474
1475   if (G_UNLIKELY (bus == NULL))
1476     goto no_bus;
1477
1478   gst_object_ref (bus);
1479   GST_OBJECT_UNLOCK (element);
1480
1481   /* we release the element lock when posting the message so that any
1482    * (synchronous) message handlers can operate on the element */
1483   result = gst_bus_post (bus, message);
1484   gst_object_unref (bus);
1485
1486   return result;
1487
1488   /* ERRORS */
1489 no_bus:
1490   {
1491     GST_DEBUG_OBJECT (element, "not posting message %p: no bus", message);
1492     GST_OBJECT_UNLOCK (element);
1493     gst_message_unref (message);
1494     return FALSE;
1495   }
1496 }
1497
1498 /**
1499  * _gst_element_error_printf:
1500  * @format: the printf-like format to use, or %NULL
1501  *
1502  * This function is only used internally by the gst_element_error() macro.
1503  *
1504  * Returns: a newly allocated string, or %NULL if the format was %NULL or ""
1505  *
1506  * MT safe.
1507  */
1508 gchar *
1509 _gst_element_error_printf (const gchar * format, ...)
1510 {
1511   va_list args;
1512   gchar *buffer;
1513
1514   if (format == NULL)
1515     return NULL;
1516   if (format[0] == 0)
1517     return NULL;
1518
1519   va_start (args, format);
1520   buffer = g_strdup_vprintf (format, args);
1521   va_end (args);
1522   return buffer;
1523 }
1524
1525 /**
1526  * gst_element_message_full:
1527  * @element:  a #GstElement to send message from
1528  * @type:     the #GstMessageType
1529  * @domain:   the GStreamer GError domain this message belongs to
1530  * @code:     the GError code belonging to the domain
1531  * @text:     an allocated text string to be used as a replacement for the
1532  *            default message connected to code, or %NULL
1533  * @debug:    an allocated debug message to be used as a replacement for the
1534  *            default debugging information, or %NULL
1535  * @file:     the source code file where the error was generated
1536  * @function: the source code function where the error was generated
1537  * @line:     the source code line where the error was generated
1538  *
1539  * Post an error or warning message on the bus from inside an element.
1540  *
1541  * MT safe.
1542  */
1543 void gst_element_message_full
1544     (GstElement * element, GstMessageType type,
1545     GQuark domain, gint code, gchar * text,
1546     gchar * debug, const gchar * file, const gchar * function, gint line)
1547 {
1548   GError *gerror = NULL;
1549   gchar *name;
1550   gchar *sent_text;
1551   gchar *sent_debug;
1552   gboolean has_debug = TRUE;
1553   GstMessage *message = NULL;
1554
1555   /* checks */
1556   GST_DEBUG_OBJECT (element, "start");
1557   g_return_if_fail (GST_IS_ELEMENT (element));
1558   g_return_if_fail ((type == GST_MESSAGE_ERROR) ||
1559       (type == GST_MESSAGE_WARNING) || (type == GST_MESSAGE_INFO));
1560
1561   /* check if we send the given text or the default error text */
1562   if ((text == NULL) || (text[0] == 0)) {
1563     /* text could have come from g_strdup_printf (""); */
1564     g_free (text);
1565     sent_text = gst_error_get_message (domain, code);
1566   } else
1567     sent_text = text;
1568
1569   /* construct a sent_debug with extra information from source */
1570   if ((debug == NULL) || (debug[0] == 0)) {
1571     /* debug could have come from g_strdup_printf (""); */
1572     has_debug = FALSE;
1573   }
1574
1575   name = gst_object_get_path_string (GST_OBJECT_CAST (element));
1576   if (has_debug)
1577     sent_debug = g_strdup_printf ("%s(%d): %s (): %s:\n%s",
1578         file, line, function, name, debug);
1579   else
1580     sent_debug = g_strdup_printf ("%s(%d): %s (): %s",
1581         file, line, function, name);
1582   g_free (name);
1583   g_free (debug);
1584
1585   /* create gerror and post message */
1586   GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posting message: %s",
1587       sent_text);
1588   gerror = g_error_new_literal (domain, code, sent_text);
1589
1590   switch (type) {
1591     case GST_MESSAGE_ERROR:
1592       message =
1593           gst_message_new_error (GST_OBJECT_CAST (element), gerror, sent_debug);
1594       break;
1595     case GST_MESSAGE_WARNING:
1596       message = gst_message_new_warning (GST_OBJECT_CAST (element), gerror,
1597           sent_debug);
1598       break;
1599     case GST_MESSAGE_INFO:
1600       message = gst_message_new_info (GST_OBJECT_CAST (element), gerror,
1601           sent_debug);
1602       break;
1603     default:
1604       g_assert_not_reached ();
1605       break;
1606   }
1607   gst_element_post_message (element, message);
1608
1609   GST_CAT_INFO_OBJECT (GST_CAT_ERROR_SYSTEM, element, "posted %s message: %s",
1610       (type == GST_MESSAGE_ERROR ? "error" : "warning"), sent_text);
1611
1612   /* cleanup */
1613   g_error_free (gerror);
1614   g_free (sent_debug);
1615   g_free (sent_text);
1616 }
1617
1618 /**
1619  * gst_element_is_locked_state:
1620  * @element: a #GstElement.
1621  *
1622  * Checks if the state of an element is locked.
1623  * If the state of an element is locked, state changes of the parent don't
1624  * affect the element.
1625  * This way you can leave currently unused elements inside bins. Just lock their
1626  * state before changing the state from #GST_STATE_NULL.
1627  *
1628  * MT safe.
1629  *
1630  * Returns: TRUE, if the element's state is locked.
1631  */
1632 gboolean
1633 gst_element_is_locked_state (GstElement * element)
1634 {
1635   gboolean result;
1636
1637   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1638
1639   GST_OBJECT_LOCK (element);
1640   result = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1641   GST_OBJECT_UNLOCK (element);
1642
1643   return result;
1644 }
1645
1646 /**
1647  * gst_element_set_locked_state:
1648  * @element: a #GstElement
1649  * @locked_state: TRUE to lock the element's state
1650  *
1651  * Locks the state of an element, so state changes of the parent don't affect
1652  * this element anymore.
1653  *
1654  * MT safe.
1655  *
1656  * Returns: TRUE if the state was changed, FALSE if bad parameters were given
1657  * or the elements state-locking needed no change.
1658  */
1659 gboolean
1660 gst_element_set_locked_state (GstElement * element, gboolean locked_state)
1661 {
1662   gboolean old;
1663
1664   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1665
1666   GST_OBJECT_LOCK (element);
1667   old = GST_OBJECT_FLAG_IS_SET (element, GST_ELEMENT_LOCKED_STATE);
1668
1669   if (G_UNLIKELY (old == locked_state))
1670     goto was_ok;
1671
1672   if (locked_state) {
1673     GST_CAT_DEBUG (GST_CAT_STATES, "locking state of element %s",
1674         GST_ELEMENT_NAME (element));
1675     GST_OBJECT_FLAG_SET (element, GST_ELEMENT_LOCKED_STATE);
1676   } else {
1677     GST_CAT_DEBUG (GST_CAT_STATES, "unlocking state of element %s",
1678         GST_ELEMENT_NAME (element));
1679     GST_OBJECT_FLAG_UNSET (element, GST_ELEMENT_LOCKED_STATE);
1680   }
1681   GST_OBJECT_UNLOCK (element);
1682
1683   return TRUE;
1684
1685 was_ok:
1686   {
1687     GST_CAT_DEBUG (GST_CAT_STATES, "elements %s was in locked state %d",
1688         GST_ELEMENT_NAME (element), old);
1689     GST_OBJECT_UNLOCK (element);
1690
1691     return FALSE;
1692   }
1693 }
1694
1695 /**
1696  * gst_element_sync_state_with_parent:
1697  * @element: a #GstElement.
1698  *
1699  * Tries to change the state of the element to the same as its parent.
1700  * If this function returns FALSE, the state of element is undefined.
1701  *
1702  * Returns: TRUE, if the element's state could be synced to the parent's state.
1703  *
1704  * MT safe.
1705  */
1706 gboolean
1707 gst_element_sync_state_with_parent (GstElement * element)
1708 {
1709   GstElement *parent;
1710
1711   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
1712
1713   if ((parent = GST_ELEMENT_CAST (gst_element_get_parent (element)))) {
1714     GstState parent_current, parent_pending;
1715     GstStateChangeReturn ret;
1716
1717     GST_OBJECT_LOCK (parent);
1718     parent_current = GST_STATE (parent);
1719     parent_pending = GST_STATE_PENDING (parent);
1720     GST_OBJECT_UNLOCK (parent);
1721
1722     GST_CAT_DEBUG (GST_CAT_STATES,
1723         "syncing state of element %s (%s) to %s (%s, %s)",
1724         GST_ELEMENT_NAME (element),
1725         gst_element_state_get_name (GST_STATE (element)),
1726         GST_ELEMENT_NAME (parent), gst_element_state_get_name (parent_current),
1727         gst_element_state_get_name (parent_pending));
1728
1729     ret = gst_element_set_state (element, parent_current);
1730     if (ret == GST_STATE_CHANGE_FAILURE)
1731       goto failed;
1732
1733     gst_object_unref (parent);
1734
1735     return TRUE;
1736   }
1737   return FALSE;
1738
1739   /* ERROR */
1740 failed:
1741   {
1742     return FALSE;
1743   }
1744 }
1745
1746 /* MT safe */
1747 static GstStateChangeReturn
1748 gst_element_get_state_func (GstElement * element,
1749     GstState * state, GstState * pending, GstClockTime timeout)
1750 {
1751   GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
1752   GstState old_pending;
1753
1754   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "getting state, timeout %"
1755       GST_TIME_FORMAT, GST_TIME_ARGS (timeout));
1756
1757   GST_OBJECT_LOCK (element);
1758   ret = GST_STATE_RETURN (element);
1759   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "RETURN is %s",
1760       gst_element_state_change_return_get_name (ret));
1761
1762   /* we got an error, report immediatly */
1763   if (ret == GST_STATE_CHANGE_FAILURE)
1764     goto done;
1765
1766   /* we got no_preroll, report immediatly */
1767   if (ret == GST_STATE_CHANGE_NO_PREROLL)
1768     goto done;
1769
1770   /* no need to wait async if we are not async */
1771   if (ret != GST_STATE_CHANGE_ASYNC)
1772     goto done;
1773
1774   old_pending = GST_STATE_PENDING (element);
1775   if (old_pending != GST_STATE_VOID_PENDING) {
1776     GTimeVal *timeval, abstimeout;
1777     guint32 cookie;
1778
1779     if (timeout != GST_CLOCK_TIME_NONE) {
1780       glong add = timeout / 1000;
1781
1782       if (add == 0)
1783         goto done;
1784
1785       /* make timeout absolute */
1786       g_get_current_time (&abstimeout);
1787       g_time_val_add (&abstimeout, add);
1788       timeval = &abstimeout;
1789     } else {
1790       timeval = NULL;
1791     }
1792     /* get cookie to dected state change during waiting */
1793     cookie = element->state_cookie;
1794
1795     GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1796         "waiting for element to commit state");
1797
1798     /* we have a pending state change, wait for it to complete */
1799     if (!GST_STATE_TIMED_WAIT (element, timeval)) {
1800       GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "timed out");
1801       /* timeout triggered */
1802       ret = GST_STATE_CHANGE_ASYNC;
1803     } else {
1804       if (cookie != element->state_cookie)
1805         goto interrupted;
1806
1807       /* could be success or failure */
1808       if (old_pending == GST_STATE (element)) {
1809         GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got success");
1810         ret = GST_STATE_CHANGE_SUCCESS;
1811       } else {
1812         GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "got failure");
1813         ret = GST_STATE_CHANGE_FAILURE;
1814       }
1815     }
1816   }
1817   /* if nothing is pending anymore we can return SUCCESS */
1818   if (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING) {
1819     GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "nothing pending");
1820     ret = GST_STATE_CHANGE_SUCCESS;
1821   }
1822
1823 done:
1824   if (state)
1825     *state = GST_STATE (element);
1826   if (pending)
1827     *pending = GST_STATE_PENDING (element);
1828
1829   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
1830       "state current: %s, pending: %s, result: %s",
1831       gst_element_state_get_name (GST_STATE (element)),
1832       gst_element_state_get_name (GST_STATE_PENDING (element)),
1833       gst_element_state_change_return_get_name (ret));
1834   GST_OBJECT_UNLOCK (element);
1835
1836   return ret;
1837
1838 interrupted:
1839   {
1840     if (state)
1841       *state = GST_STATE_VOID_PENDING;
1842     if (pending)
1843       *pending = GST_STATE_VOID_PENDING;
1844
1845     GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "get_state() interruped");
1846
1847     GST_OBJECT_UNLOCK (element);
1848
1849     return GST_STATE_CHANGE_FAILURE;
1850   }
1851 }
1852
1853 /**
1854  * gst_element_get_state:
1855  * @element: a #GstElement to get the state of.
1856  * @state: a pointer to #GstState to hold the state. Can be %NULL.
1857  * @pending: a pointer to #GstState to hold the pending state.
1858  *           Can be %NULL.
1859  * @timeout: a #GstClockTime to specify the timeout for an async
1860  *           state change or %GST_CLOCK_TIME_NONE for infinite timeout.
1861  *
1862  * Gets the state of the element.
1863  *
1864  * For elements that performed an ASYNC state change, as reported by
1865  * gst_element_set_state(), this function will block up to the
1866  * specified timeout value for the state change to complete.
1867  * If the element completes the state change or goes into
1868  * an error, this function returns immediately with a return value of
1869  * %GST_STATE_CHANGE_SUCCESS or %GST_STATE_CHANGE_FAILURE respectively.
1870  *
1871  * For elements that did not return %GST_STATE_CHANGE_ASYNC, this function
1872  * returns the current and pending state immediately.
1873  *
1874  * This function returns %GST_STATE_CHANGE_NO_PREROLL if the element
1875  * successfully changed its state but is not able to provide data yet.
1876  * This mostly happens for live sources that only produce data in the PLAYING
1877  * state. While the state change return is equivalent to
1878  * %GST_STATE_CHANGE_SUCCESS, it is returned to the application to signal that
1879  * some sink elements might not be able to complete their state change because
1880  * an element is not producing data to complete the preroll. When setting the
1881  * element to playing, the preroll will complete and playback will start.
1882  *
1883  * Returns: %GST_STATE_CHANGE_SUCCESS if the element has no more pending state
1884  *          and the last state change succeeded, %GST_STATE_CHANGE_ASYNC if the
1885  *          element is still performing a state change or
1886  *          %GST_STATE_CHANGE_FAILURE if the last state change failed.
1887  *
1888  * MT safe.
1889  */
1890 GstStateChangeReturn
1891 gst_element_get_state (GstElement * element,
1892     GstState * state, GstState * pending, GstClockTime timeout)
1893 {
1894   GstElementClass *oclass;
1895   GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
1896
1897   g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
1898
1899   oclass = GST_ELEMENT_GET_CLASS (element);
1900
1901   if (oclass->get_state)
1902     result = (oclass->get_state) (element, state, pending, timeout);
1903
1904   return result;
1905 }
1906
1907 /**
1908  * gst_element_abort_state:
1909  * @element: a #GstElement to abort the state of.
1910  *
1911  * Abort the state change of the element. This function is used
1912  * by elements that do asynchronous state changes and find out
1913  * something is wrong.
1914  *
1915  * This function should be called with the STATE_LOCK held.
1916  *
1917  * MT safe.
1918  */
1919 void
1920 gst_element_abort_state (GstElement * element)
1921 {
1922   GstState pending;
1923
1924 #ifndef GST_DISABLE_GST_DEBUG
1925   GstState old_state;
1926 #endif
1927
1928   g_return_if_fail (GST_IS_ELEMENT (element));
1929
1930   GST_OBJECT_LOCK (element);
1931   pending = GST_STATE_PENDING (element);
1932
1933   if (pending == GST_STATE_VOID_PENDING ||
1934       GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
1935     goto nothing_aborted;
1936
1937 #ifndef GST_DISABLE_GST_DEBUG
1938   old_state = GST_STATE (element);
1939
1940   GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
1941       "aborting state from %s to %s", gst_element_state_get_name (old_state),
1942       gst_element_state_get_name (pending));
1943 #endif
1944
1945   /* flag error */
1946   GST_STATE_RETURN (element) = GST_STATE_CHANGE_FAILURE;
1947
1948   GST_STATE_BROADCAST (element);
1949   GST_OBJECT_UNLOCK (element);
1950
1951   return;
1952
1953 nothing_aborted:
1954   {
1955     GST_OBJECT_UNLOCK (element);
1956     return;
1957   }
1958 }
1959
1960 /**
1961  * gst_element_continue_state:   
1962  * @element: a #GstElement to continue the state change of.      
1963  * @ret: The previous state return value
1964  *       
1965  * Commit the state change of the element and proceed to the next 
1966  * pending state if any. This function is used   
1967  * by elements that do asynchronous state changes.       
1968  * The core will normally call this method automatically when an         
1969  * element returned %GST_STATE_CHANGE_SUCCESS from the state change function.      
1970  *
1971  * If after calling this method the element still has not reached        
1972  * the pending state, the next state change is performed.        
1973  *       
1974  * This method is used internally and should normally not be called by plugins
1975  * or applications.
1976  *       
1977  * Returns: The result of the commit state change.       
1978  *       
1979  * MT safe.      
1980  */
1981 GstStateChangeReturn
1982 gst_element_continue_state (GstElement * element, GstStateChangeReturn ret)
1983 {
1984   GstState pending;
1985   GstState old_ret, old_state, old_next;
1986   GstState current, next;
1987   GstMessage *message;
1988   GstStateChange transition;
1989
1990   GST_OBJECT_LOCK (element);
1991   old_ret = (GstState) GST_STATE_RETURN (element);
1992   GST_STATE_RETURN (element) = ret;
1993   pending = GST_STATE_PENDING (element);
1994
1995   /* check if there is something to commit */
1996   if (pending == GST_STATE_VOID_PENDING)
1997     goto nothing_pending;
1998
1999   old_state = GST_STATE (element);
2000   /* this is the state we should go to next */
2001   old_next = GST_STATE_NEXT (element);
2002   /* update current state */
2003   current = GST_STATE (element) = old_next;
2004
2005   /* see if we reached the final state */
2006   if (pending == current)
2007     goto complete;
2008
2009   next = GST_STATE_GET_NEXT (current, pending);
2010   transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2011
2012   GST_STATE_NEXT (element) = next;
2013   /* mark busy */
2014   GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2015   GST_OBJECT_UNLOCK (element);
2016
2017   GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2018       "committing state from %s to %s, pending %s",
2019       gst_element_state_get_name (old_state),
2020       gst_element_state_get_name (old_next),
2021       gst_element_state_get_name (pending));
2022
2023   message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2024       old_state, old_next, pending);
2025   gst_element_post_message (element, message);
2026
2027   GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2028       "continue state change %s to %s, final %s",
2029       gst_element_state_get_name (current),
2030       gst_element_state_get_name (next), gst_element_state_get_name (pending));
2031
2032   ret = gst_element_change_state (element, transition);
2033
2034   return ret;
2035
2036 nothing_pending:
2037   {
2038     GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "nothing pending");
2039     GST_OBJECT_UNLOCK (element);
2040     return ret;
2041   }
2042 complete:
2043   {
2044     GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2045     GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2046
2047     GST_CAT_INFO_OBJECT (GST_CAT_STATES, element, "completed state change");
2048     GST_OBJECT_UNLOCK (element);
2049
2050     /* don't post silly messages with the same state. This can happen
2051      * when an element state is changed to what it already was. For bins
2052      * this can be the result of a lost state, which we check with the
2053      * previous return value. 
2054      * We do signal the cond though as a _get_state() might be blocking 
2055      * on it. */
2056     if (old_state != old_next || old_ret == GST_STATE_CHANGE_ASYNC) {
2057       message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2058           old_state, old_next, GST_STATE_VOID_PENDING);
2059       gst_element_post_message (element, message);
2060     }
2061
2062     GST_STATE_BROADCAST (element);
2063
2064     return ret;
2065   }
2066 }
2067
2068 /**
2069  * gst_element_lost_state:
2070  * @element: a #GstElement the state is lost of
2071  *
2072  * Brings the element to the lost state. The current state of the
2073  * element is copied to the pending state so that any call to
2074  * gst_element_get_state() will return %GST_STATE_CHANGE_ASYNC.
2075  *
2076  * This is mostly used for elements that lost their preroll buffer
2077  * in the %GST_STATE_PAUSED state after a flush, they become %GST_STATE_PAUSED
2078  * again if a new preroll buffer is queued.
2079  * This function can only be called when the element is currently
2080  * not in error or an async state change.
2081  *
2082  * This function is used internally and should normally not be called from
2083  * plugins or applications.
2084  *
2085  * MT safe.
2086  */
2087 void
2088 gst_element_lost_state (GstElement * element)
2089 {
2090   GstState current_state;
2091   GstMessage *message;
2092
2093   g_return_if_fail (GST_IS_ELEMENT (element));
2094
2095   GST_OBJECT_LOCK (element);
2096   if (GST_STATE_PENDING (element) != GST_STATE_VOID_PENDING ||
2097       GST_STATE_RETURN (element) == GST_STATE_CHANGE_FAILURE)
2098     goto nothing_lost;
2099
2100   current_state = GST_STATE (element);
2101
2102   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2103       "lost state of %s", gst_element_state_get_name (current_state));
2104
2105   GST_STATE_NEXT (element) = current_state;
2106   GST_STATE_PENDING (element) = current_state;
2107   GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2108   GST_OBJECT_UNLOCK (element);
2109
2110   message = gst_message_new_state_changed (GST_OBJECT_CAST (element),
2111       current_state, current_state, current_state);
2112   gst_element_post_message (element, message);
2113
2114   /* and mark us dirty */
2115   message = gst_message_new_state_dirty (GST_OBJECT_CAST (element));
2116   gst_element_post_message (element, message);
2117
2118   return;
2119
2120 nothing_lost:
2121   {
2122     GST_OBJECT_UNLOCK (element);
2123     return;
2124   }
2125 }
2126
2127 /**
2128  * gst_element_set_state:
2129  * @element: a #GstElement to change state of.
2130  * @state: the element's new #GstState.
2131  *
2132  * Sets the state of the element. This function will try to set the
2133  * requested state by going through all the intermediary states and calling
2134  * the class's state change function for each.
2135  *
2136  * This function can return #GST_STATE_CHANGE_ASYNC, in which case the
2137  * element will perform the remainder of the state change asynchronously in
2138  * another thread.
2139  * An application can use gst_element_get_state() to wait for the completion
2140  * of the state change or it can wait for a state change message on the bus.
2141  *
2142  * Returns: Result of the state change using #GstStateChangeReturn.
2143  *
2144  * MT safe.
2145  */
2146 GstStateChangeReturn
2147 gst_element_set_state (GstElement * element, GstState state)
2148 {
2149   GstElementClass *oclass;
2150   GstStateChangeReturn result = GST_STATE_CHANGE_FAILURE;
2151
2152   g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2153
2154   oclass = GST_ELEMENT_GET_CLASS (element);
2155
2156   if (oclass->set_state)
2157     result = (oclass->set_state) (element, state);
2158
2159   return result;
2160 }
2161
2162 /*
2163  * default set state function, calculates the next state based
2164  * on current state and calls the change_state function 
2165  */
2166 static GstStateChangeReturn
2167 gst_element_set_state_func (GstElement * element, GstState state)
2168 {
2169   GstState current, next, old_pending;
2170   GstStateChangeReturn ret;
2171   GstStateChange transition;
2172   GstStateChangeReturn old_ret;
2173
2174   g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2175
2176   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "set_state to %s",
2177       gst_element_state_get_name (state));
2178
2179   /* state lock is taken to protect the set_state() and get_state() 
2180    * procedures, it does not lock any variables. */
2181   GST_STATE_LOCK (element);
2182
2183   /* now calculate how to get to the new state */
2184   GST_OBJECT_LOCK (element);
2185   old_ret = GST_STATE_RETURN (element);
2186   /* previous state change returned an error, remove all pending
2187    * and next states */
2188   if (old_ret == GST_STATE_CHANGE_FAILURE) {
2189     GST_STATE_NEXT (element) = GST_STATE_VOID_PENDING;
2190     GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
2191     GST_STATE_RETURN (element) = GST_STATE_CHANGE_SUCCESS;
2192   }
2193
2194   current = GST_STATE (element);
2195   next = GST_STATE_NEXT (element);
2196   old_pending = GST_STATE_PENDING (element);
2197   /* increment state cookie so that we can track each state change */
2198   element->state_cookie++;
2199
2200   /* this is the (new) state we should go to */
2201   GST_STATE_PENDING (element) = state;
2202
2203   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2204       "current %s, old_pending %s, next %s, old return %s",
2205       gst_element_state_get_name (current),
2206       gst_element_state_get_name (old_pending),
2207       gst_element_state_get_name (next),
2208       gst_element_state_change_return_get_name (old_ret));
2209
2210   /* if the element was busy doing a state change, we just update the
2211    * target state, it'll get to it async then. */
2212   if (old_pending != GST_STATE_VOID_PENDING) {
2213     /* upwards state change will happen ASYNC */
2214     if (old_pending <= state)
2215       goto was_busy;
2216     /* element is going to this state already */
2217     else if (next == state)
2218       goto was_busy;
2219     /* element was performing an ASYNC upward state change and
2220      * we request to go downward again. Start from the next pending
2221      * state then. */
2222     else if (next > state
2223         && GST_STATE_RETURN (element) == GST_STATE_CHANGE_ASYNC) {
2224       current = next;
2225     }
2226   }
2227   next = GST_STATE_GET_NEXT (current, state);
2228   /* now we store the next state */
2229   GST_STATE_NEXT (element) = next;
2230   /* mark busy, we need to check that there is actually a state change
2231    * to be done else we could accidentally override SUCCESS/NO_PREROLL and
2232    * the default element change_state function has no way to know what the
2233    * old value was... could consider this a FIXME...*/
2234   if (current != next)
2235     GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2236
2237   transition = (GstStateChange) GST_STATE_TRANSITION (current, next);
2238
2239   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2240       "%s: setting state from %s to %s",
2241       (next != state ? "intermediate" : "final"),
2242       gst_element_state_get_name (current), gst_element_state_get_name (next));
2243
2244   /* now signal any waiters, they will error since the cookie was increased */
2245   GST_STATE_BROADCAST (element);
2246
2247   GST_OBJECT_UNLOCK (element);
2248
2249   ret = gst_element_change_state (element, transition);
2250
2251   GST_STATE_UNLOCK (element);
2252
2253   GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element, "returned %s",
2254       gst_element_state_change_return_get_name (ret));
2255
2256   return ret;
2257
2258 was_busy:
2259   {
2260     GST_STATE_RETURN (element) = GST_STATE_CHANGE_ASYNC;
2261     GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2262         "element was busy with async state change");
2263     GST_OBJECT_UNLOCK (element);
2264
2265     GST_STATE_UNLOCK (element);
2266
2267     return GST_STATE_CHANGE_ASYNC;
2268   }
2269 }
2270
2271 /* with STATE_LOCK */
2272 static GstStateChangeReturn
2273 gst_element_change_state (GstElement * element, GstStateChange transition)
2274 {
2275   GstElementClass *oclass;
2276   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2277   GstState current;
2278   GstState next;
2279
2280   oclass = GST_ELEMENT_GET_CLASS (element);
2281
2282   /* start with the current state. */
2283   current = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2284   next = GST_STATE_TRANSITION_NEXT (transition);
2285
2286   /* call the state change function so it can set the state */
2287   if (oclass->change_state)
2288     ret = (oclass->change_state) (element, transition);
2289   else
2290     ret = GST_STATE_CHANGE_FAILURE;
2291
2292   switch (ret) {
2293     case GST_STATE_CHANGE_FAILURE:
2294       GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2295           "have FAILURE change_state return");
2296       /* state change failure */
2297       gst_element_abort_state (element);
2298       break;
2299     case GST_STATE_CHANGE_ASYNC:
2300       GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2301           "element will change state ASYNC");
2302
2303       /* if we go upwards, we give the app a change to wait for
2304        * completion */
2305       if (current < next)
2306         goto async;
2307
2308       /* else we just continue the state change downwards */
2309       GST_CAT_INFO_OBJECT (GST_CAT_STATES, element,
2310           "forcing commit state %s < %s",
2311           gst_element_state_get_name (current),
2312           gst_element_state_get_name (next));
2313
2314       ret = gst_element_continue_state (element, GST_STATE_CHANGE_SUCCESS);
2315       break;
2316     case GST_STATE_CHANGE_SUCCESS:
2317       GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2318           "element changed state SUCCESS");
2319       /* we can commit the state now which will proceeed to
2320        * the next state */
2321       ret = gst_element_continue_state (element, ret);
2322       break;
2323     case GST_STATE_CHANGE_NO_PREROLL:
2324       GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2325           "element changed state NO_PREROLL");
2326       /* we can commit the state now which will proceeed to
2327        * the next state */
2328       ret = gst_element_continue_state (element, ret);
2329       break;
2330     default:
2331       goto invalid_return;
2332   }
2333
2334   GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit state change %d", ret);
2335
2336   return ret;
2337
2338 async:
2339   GST_CAT_LOG_OBJECT (GST_CAT_STATES, element, "exit async state change %d",
2340       ret);
2341
2342   return ret;
2343
2344   /* ERROR */
2345 invalid_return:
2346   {
2347     GST_OBJECT_LOCK (element);
2348     /* somebody added a GST_STATE_ and forgot to do stuff here ! */
2349     g_critical ("%s: unknown return value %d from a state change function",
2350         GST_ELEMENT_NAME (element), ret);
2351
2352     /* we are in error now */
2353     ret = GST_STATE_CHANGE_FAILURE;
2354     GST_STATE_RETURN (element) = ret;
2355     GST_OBJECT_UNLOCK (element);
2356
2357     return ret;
2358   }
2359 }
2360
2361 /* gst_iterator_fold functions for pads_activate
2362  * Note how we don't stop the iterator when we fail an activation. This is
2363  * probably a FIXME since when one pad activation fails, we don't want to
2364  * continue our state change. */
2365 static gboolean
2366 activate_pads (GstPad * pad, GValue * ret, gboolean * active)
2367 {
2368   if (!gst_pad_set_active (pad, *active))
2369     g_value_set_boolean (ret, FALSE);
2370
2371   /* unref the object that was reffed for us by _fold */
2372   gst_object_unref (pad);
2373   return TRUE;
2374 }
2375
2376 /* set the caps on the pad to NULL */
2377 static gboolean
2378 clear_caps (GstPad * pad, GValue * ret, gboolean * active)
2379 {
2380   gst_pad_set_caps (pad, NULL);
2381   gst_object_unref (pad);
2382   return TRUE;
2383 }
2384
2385 /* returns false on error or early cutout (will never happen because the fold
2386  * function always returns TRUE, see FIXME above) of the fold, true if all
2387  * pads in @iter were (de)activated successfully. */
2388 static gboolean
2389 iterator_activate_fold_with_resync (GstIterator * iter,
2390     GstIteratorFoldFunction func, gpointer user_data)
2391 {
2392   GstIteratorResult ires;
2393   GValue ret = { 0 };
2394
2395   /* no need to unset this later, it's just a boolean */
2396   g_value_init (&ret, G_TYPE_BOOLEAN);
2397   g_value_set_boolean (&ret, TRUE);
2398
2399   while (1) {
2400     ires = gst_iterator_fold (iter, func, &ret, user_data);
2401     switch (ires) {
2402       case GST_ITERATOR_RESYNC:
2403         /* need to reset the result again */
2404         g_value_set_boolean (&ret, TRUE);
2405         gst_iterator_resync (iter);
2406         break;
2407       case GST_ITERATOR_DONE:
2408         /* all pads iterated, return collected value */
2409         goto done;
2410       default:
2411         /* iterator returned _ERROR or premature end with _OK, 
2412          * mark an error and exit */
2413         g_value_set_boolean (&ret, FALSE);
2414         goto done;
2415     }
2416   }
2417 done:
2418   /* return collected value */
2419   return g_value_get_boolean (&ret);
2420 }
2421
2422 /* is called with STATE_LOCK
2423  *
2424  * Pads are activated from source pads to sinkpads. 
2425  */
2426 static gboolean
2427 gst_element_pads_activate (GstElement * element, gboolean active)
2428 {
2429   GstIterator *iter;
2430   gboolean res;
2431
2432   GST_DEBUG_OBJECT (element, "pads_activate with active %d", active);
2433
2434   iter = gst_element_iterate_src_pads (element);
2435   res =
2436       iterator_activate_fold_with_resync (iter,
2437       (GstIteratorFoldFunction) activate_pads, &active);
2438   gst_iterator_free (iter);
2439   if (G_UNLIKELY (!res))
2440     goto src_failed;
2441
2442   iter = gst_element_iterate_sink_pads (element);
2443   res =
2444       iterator_activate_fold_with_resync (iter,
2445       (GstIteratorFoldFunction) activate_pads, &active);
2446   gst_iterator_free (iter);
2447   if (G_UNLIKELY (!res))
2448     goto sink_failed;
2449
2450   if (!active) {
2451     /* clear the caps on all pads, this should never fail */
2452     iter = gst_element_iterate_pads (element);
2453     res =
2454         iterator_activate_fold_with_resync (iter,
2455         (GstIteratorFoldFunction) clear_caps, &active);
2456     gst_iterator_free (iter);
2457     if (G_UNLIKELY (!res))
2458       goto caps_failed;
2459   }
2460
2461   GST_DEBUG_OBJECT (element, "pads_activate successful");
2462
2463   return TRUE;
2464
2465   /* ERRORS */
2466 src_failed:
2467   {
2468     GST_DEBUG_OBJECT (element, "source pads_activate failed");
2469     return FALSE;
2470   }
2471 sink_failed:
2472   {
2473     GST_DEBUG_OBJECT (element, "sink pads_activate failed");
2474     return FALSE;
2475   }
2476 caps_failed:
2477   {
2478     GST_DEBUG_OBJECT (element, "failed to clear caps on pads");
2479     return FALSE;
2480   }
2481 }
2482
2483 /* is called with STATE_LOCK */
2484 static GstStateChangeReturn
2485 gst_element_change_state_func (GstElement * element, GstStateChange transition)
2486 {
2487   GstState state, next;
2488   GstStateChangeReturn result = GST_STATE_CHANGE_SUCCESS;
2489
2490   g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_CHANGE_FAILURE);
2491
2492   state = (GstState) GST_STATE_TRANSITION_CURRENT (transition);
2493   next = GST_STATE_TRANSITION_NEXT (transition);
2494
2495   /* if the element already is in the given state, we just return success */
2496   if (next == GST_STATE_VOID_PENDING || state == next)
2497     goto was_ok;
2498
2499   GST_CAT_LOG_OBJECT (GST_CAT_STATES, element,
2500       "default handler tries setting state from %s to %s (%04x)",
2501       gst_element_state_get_name (state),
2502       gst_element_state_get_name (next), transition);
2503
2504   switch (transition) {
2505     case GST_STATE_CHANGE_NULL_TO_READY:
2506       break;
2507     case GST_STATE_CHANGE_READY_TO_PAUSED:
2508       if (!gst_element_pads_activate (element, TRUE)) {
2509         result = GST_STATE_CHANGE_FAILURE;
2510       }
2511       break;
2512     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2513       break;
2514     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2515       break;
2516     case GST_STATE_CHANGE_PAUSED_TO_READY:
2517     case GST_STATE_CHANGE_READY_TO_NULL:
2518       /* deactivate pads in both cases, since they are activated on
2519          ready->paused but the element might not have made it to paused */
2520       if (!gst_element_pads_activate (element, FALSE)) {
2521         result = GST_STATE_CHANGE_FAILURE;
2522       } else {
2523         gst_element_set_base_time (element, 0);
2524       }
2525       break;
2526     default:
2527       /* this will catch real but unhandled state changes;
2528        * can only be caused by:
2529        * - a new state was added
2530        * - somehow the element was asked to jump across an intermediate state
2531        */
2532       g_warning ("Unhandled state change from %s to %s",
2533           gst_element_state_get_name (state),
2534           gst_element_state_get_name (next));
2535       break;
2536   }
2537   return result;
2538
2539 was_ok:
2540   {
2541     GST_OBJECT_LOCK (element);
2542     result = GST_STATE_RETURN (element);
2543     GST_CAT_DEBUG_OBJECT (GST_CAT_STATES, element,
2544         "element is already in the %s state",
2545         gst_element_state_get_name (state));
2546     GST_OBJECT_UNLOCK (element);
2547
2548     return result;
2549   }
2550 }
2551
2552 /**
2553  * gst_element_get_factory:
2554  * @element: a #GstElement to request the element factory of.
2555  *
2556  * Retrieves the factory that was used to create this element.
2557  *
2558  * Returns: the #GstElementFactory used for creating this element.
2559  * no refcounting is needed.
2560  */
2561 GstElementFactory *
2562 gst_element_get_factory (GstElement * element)
2563 {
2564   g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
2565
2566   return GST_ELEMENT_GET_CLASS (element)->elementfactory;
2567 }
2568
2569 static void
2570 gst_element_dispose (GObject * object)
2571 {
2572   GstElement *element = GST_ELEMENT (object);
2573   GstClock **clock_p;
2574   GstBus **bus_p;
2575
2576   GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "dispose");
2577
2578   if (GST_STATE (element) != GST_STATE_NULL)
2579     goto not_null;
2580
2581   g_return_if_fail (GST_STATE_PENDING (element) == GST_STATE_VOID_PENDING);
2582
2583   GST_DEBUG_OBJECT (element, "removing %d pads", g_list_length (element->pads));
2584   /* first we break all our links with the outside */
2585   while (element->pads && element->pads->data) {
2586     /* don't call _remove_pad with NULL */
2587     gst_element_remove_pad (element, GST_PAD_CAST (element->pads->data));
2588   }
2589   if (G_UNLIKELY (element->pads != 0)) {
2590     g_critical ("could not remove pads from element %s",
2591         GST_STR_NULL (GST_OBJECT_NAME (object)));
2592   }
2593
2594   GST_OBJECT_LOCK (element);
2595   clock_p = &element->clock;
2596   bus_p = &element->bus;
2597   gst_object_replace ((GstObject **) clock_p, NULL);
2598   gst_object_replace ((GstObject **) bus_p, NULL);
2599   GST_OBJECT_UNLOCK (element);
2600
2601   GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "parent class dispose");
2602
2603   G_OBJECT_CLASS (parent_class)->dispose (object);
2604
2605   return;
2606
2607   /* ERRORS */
2608 not_null:
2609   {
2610     g_critical
2611         ("\nTrying to dispose element %s, but it is not in the NULL state.\n"
2612         "You need to explicitly set elements to the NULL state before\n"
2613         "dropping the final reference, to allow them to clean up.\n",
2614         GST_OBJECT_NAME (element));
2615     return;
2616   }
2617 }
2618
2619 static void
2620 gst_element_finalize (GObject * object)
2621 {
2622   GstElement *element = GST_ELEMENT (object);
2623
2624   GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize");
2625
2626   GST_STATE_LOCK (element);
2627   if (element->state_cond)
2628     g_cond_free (element->state_cond);
2629   element->state_cond = NULL;
2630   GST_STATE_UNLOCK (element);
2631   g_static_rec_mutex_free (element->state_lock);
2632   g_free (element->state_lock);
2633   element->state_lock = NULL;
2634
2635   GST_CAT_INFO_OBJECT (GST_CAT_REFCOUNTING, element, "finalize parent");
2636
2637   G_OBJECT_CLASS (parent_class)->finalize (object);
2638 }
2639
2640 #ifndef GST_DISABLE_LOADSAVE
2641 /**
2642  * gst_element_save_thyself:
2643  * @element: a #GstElement to save.
2644  * @parent: the xml parent node.
2645  *
2646  * Saves the element as part of the given XML structure.
2647  *
2648  * Returns: the new #xmlNodePtr.
2649  */
2650 static xmlNodePtr
2651 gst_element_save_thyself (GstObject * object, xmlNodePtr parent)
2652 {
2653   GList *pads;
2654   GstElementClass *oclass;
2655   GParamSpec **specs, *spec;
2656   guint nspecs;
2657   guint i;
2658   GValue value = { 0, };
2659   GstElement *element;
2660
2661   g_return_val_if_fail (GST_IS_ELEMENT (object), parent);
2662
2663   element = GST_ELEMENT (object);
2664
2665   oclass = GST_ELEMENT_GET_CLASS (element);
2666
2667   xmlNewChild (parent, NULL, (xmlChar *) "name",
2668       (xmlChar *) GST_ELEMENT_NAME (element));
2669
2670   if (oclass->elementfactory != NULL) {
2671     GstElementFactory *factory = (GstElementFactory *) oclass->elementfactory;
2672
2673     xmlNewChild (parent, NULL, (xmlChar *) "type",
2674         (xmlChar *) GST_PLUGIN_FEATURE (factory)->name);
2675   }
2676
2677   /* params */
2678   specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &nspecs);
2679
2680   for (i = 0; i < nspecs; i++) {
2681     spec = specs[i];
2682     if (spec->flags & G_PARAM_READABLE) {
2683       xmlNodePtr param;
2684       char *contents;
2685
2686       g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (spec));
2687
2688       g_object_get_property (G_OBJECT (element), spec->name, &value);
2689       param = xmlNewChild (parent, NULL, (xmlChar *) "param", NULL);
2690       xmlNewChild (param, NULL, (xmlChar *) "name", (xmlChar *) spec->name);
2691
2692       if (G_IS_PARAM_SPEC_STRING (spec))
2693         contents = g_value_dup_string (&value);
2694       else if (G_IS_PARAM_SPEC_ENUM (spec))
2695         contents = g_strdup_printf ("%d", g_value_get_enum (&value));
2696       else if (G_IS_PARAM_SPEC_INT64 (spec))
2697         contents = g_strdup_printf ("%" G_GINT64_FORMAT,
2698             g_value_get_int64 (&value));
2699       else
2700         contents = g_strdup_value_contents (&value);
2701
2702       xmlNewChild (param, NULL, (xmlChar *) "value", (xmlChar *) contents);
2703       g_free (contents);
2704
2705       g_value_unset (&value);
2706     }
2707   }
2708
2709   g_free (specs);
2710
2711   pads = GST_ELEMENT_PADS (element);
2712
2713   while (pads) {
2714     GstPad *pad = GST_PAD_CAST (pads->data);
2715
2716     /* figure out if it's a direct pad or a ghostpad */
2717     if (GST_ELEMENT (GST_OBJECT_PARENT (pad)) == element) {
2718       xmlNodePtr padtag = xmlNewChild (parent, NULL, (xmlChar *) "pad", NULL);
2719
2720       gst_object_save_thyself (GST_OBJECT_CAST (pad), padtag);
2721     }
2722     pads = g_list_next (pads);
2723   }
2724
2725   return parent;
2726 }
2727
2728 static void
2729 gst_element_restore_thyself (GstObject * object, xmlNodePtr self)
2730 {
2731   xmlNodePtr children;
2732   GstElement *element;
2733   gchar *name = NULL;
2734   gchar *value = NULL;
2735
2736   element = GST_ELEMENT (object);
2737   g_return_if_fail (element != NULL);
2738
2739   /* parameters */
2740   children = self->xmlChildrenNode;
2741   while (children) {
2742     if (!strcmp ((char *) children->name, "param")) {
2743       xmlNodePtr child = children->xmlChildrenNode;
2744
2745       while (child) {
2746         if (!strcmp ((char *) child->name, "name")) {
2747           name = (gchar *) xmlNodeGetContent (child);
2748         } else if (!strcmp ((char *) child->name, "value")) {
2749           value = (gchar *) xmlNodeGetContent (child);
2750         }
2751         child = child->next;
2752       }
2753       /* FIXME: can this just be g_object_set ? */
2754       gst_util_set_object_arg (G_OBJECT (element), name, value);
2755       /* g_object_set (G_OBJECT (element), name, value, NULL); */
2756       g_free (name);
2757       g_free (value);
2758     }
2759     children = children->next;
2760   }
2761
2762   /* pads */
2763   children = self->xmlChildrenNode;
2764   while (children) {
2765     if (!strcmp ((char *) children->name, "pad")) {
2766       gst_pad_load_and_link (children, GST_OBJECT_CAST (element));
2767     }
2768     children = children->next;
2769   }
2770
2771   if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
2772     (GST_OBJECT_CLASS (parent_class)->restore_thyself) (object, self);
2773 }
2774 #endif /* GST_DISABLE_LOADSAVE */
2775
2776 static void
2777 gst_element_set_bus_func (GstElement * element, GstBus * bus)
2778 {
2779   GstBus **bus_p;
2780
2781   g_return_if_fail (GST_IS_ELEMENT (element));
2782
2783   GST_CAT_DEBUG_OBJECT (GST_CAT_PARENTAGE, element, "setting bus to %p", bus);
2784
2785   GST_OBJECT_LOCK (element);
2786   bus_p = &GST_ELEMENT_BUS (element);
2787   gst_object_replace ((GstObject **) bus_p, GST_OBJECT_CAST (bus));
2788   GST_OBJECT_UNLOCK (element);
2789 }
2790
2791 /**
2792  * gst_element_set_bus:
2793  * @element: a #GstElement to set the bus of.
2794  * @bus: the #GstBus to set.
2795  *
2796  * Sets the bus of the element. Increases the refcount on the bus.
2797  * For internal use only, unless you're testing elements.
2798  *
2799  * MT safe.
2800  */
2801 void
2802 gst_element_set_bus (GstElement * element, GstBus * bus)
2803 {
2804   GstElementClass *oclass;
2805
2806   g_return_if_fail (GST_IS_ELEMENT (element));
2807
2808   oclass = GST_ELEMENT_GET_CLASS (element);
2809
2810   if (oclass->set_bus)
2811     oclass->set_bus (element, bus);
2812 }
2813
2814 /**
2815  * gst_element_get_bus:
2816  * @element: a #GstElement to get the bus of.
2817  *
2818  * Returns the bus of the element.
2819  *
2820  * Returns: the element's #GstBus. unref after usage.
2821  *
2822  * MT safe.
2823  */
2824 GstBus *
2825 gst_element_get_bus (GstElement * element)
2826 {
2827   GstBus *result = NULL;
2828
2829   g_return_val_if_fail (GST_IS_ELEMENT (element), result);
2830
2831   GST_OBJECT_LOCK (element);
2832   if ((result = GST_ELEMENT_BUS (element)))
2833     gst_object_ref (result);
2834   GST_OBJECT_UNLOCK (element);
2835
2836   GST_DEBUG_OBJECT (element, "got bus %" GST_PTR_FORMAT, result);
2837
2838   return result;
2839 }