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