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