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