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