2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstelement.c: The base element, all elements derive from this
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.
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.
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.
23 /* #define GST_DEBUG_ENABLED */
26 #include "gst_private.h"
28 #include "gstelement.h"
29 #include "gstextratypes.h"
31 #include "gstscheduler.h"
35 /* Element signals and args */
50 #define CLASS(element) GST_ELEMENT_CLASS (G_OBJECT_GET_CLASS (element))
52 static void gst_element_class_init (GstElementClass *klass);
53 static void gst_element_init (GstElement *element);
54 static void gst_element_base_class_init (GstElementClass *klass);
56 static void gst_element_set_property (GObject *object, guint prop_id,
57 const GValue *value, GParamSpec *pspec);
58 static void gst_element_get_property (GObject *object, guint prop_id, GValue *value,
61 static void gst_element_dispose (GObject *object);
63 static GstElementStateReturn gst_element_change_state (GstElement *element);
64 static void gst_element_error_func (GstElement* element, GstElement *source, gchar *errormsg);
66 #ifndef GST_DISABLE_LOADSAVE
67 static xmlNodePtr gst_element_save_thyself (GstObject *object, xmlNodePtr parent);
68 static void gst_element_restore_thyself (GstObject *parent, xmlNodePtr self);
71 GType _gst_element_type = 0;
73 static GstObjectClass *parent_class = NULL;
74 static guint gst_element_signals[LAST_SIGNAL] = { 0 };
76 GType gst_element_get_type (void)
78 if (!_gst_element_type) {
79 static const GTypeInfo element_info = {
80 sizeof(GstElementClass),
81 (GBaseInitFunc)gst_element_base_class_init,
83 (GClassInitFunc)gst_element_class_init,
88 (GInstanceInitFunc)gst_element_init,
91 _gst_element_type = g_type_register_static(GST_TYPE_OBJECT, "GstElement", &element_info, G_TYPE_FLAG_ABSTRACT);
93 return _gst_element_type;
97 gst_element_class_init (GstElementClass *klass)
99 GObjectClass *gobject_class;
100 GstObjectClass *gstobject_class;
102 gobject_class = (GObjectClass*) klass;
103 gstobject_class = (GstObjectClass*) klass;
105 parent_class = g_type_class_ref(GST_TYPE_OBJECT);
107 gst_element_signals[STATE_CHANGE] =
108 g_signal_new ("state_change", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
109 G_STRUCT_OFFSET (GstElementClass, state_change), NULL, NULL,
110 gst_marshal_VOID__INT_INT, G_TYPE_NONE, 2,
111 G_TYPE_INT, G_TYPE_INT);
112 gst_element_signals[NEW_PAD] =
113 g_signal_new ("new_pad", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
114 G_STRUCT_OFFSET (GstElementClass, new_pad), NULL, NULL,
115 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
117 gst_element_signals[PAD_REMOVED] =
118 g_signal_new ("pad_removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
119 G_STRUCT_OFFSET (GstElementClass, pad_removed), NULL, NULL,
120 gst_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
122 gst_element_signals[ERROR] =
123 g_signal_new ("error", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
124 G_STRUCT_OFFSET (GstElementClass, error), NULL, NULL,
125 gst_marshal_VOID__OBJECT_STRING, G_TYPE_NONE, 2,
126 G_TYPE_OBJECT, G_TYPE_STRING);
127 gst_element_signals[EOS] =
128 g_signal_new ("eos", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
129 G_STRUCT_OFFSET (GstElementClass,eos), NULL, NULL,
130 gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
134 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_element_set_property);
135 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_element_get_property);
136 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_element_dispose);
138 #ifndef GST_DISABLE_LOADSAVE
139 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_element_save_thyself);
140 gstobject_class->restore_thyself = GST_DEBUG_FUNCPTR (gst_element_restore_thyself);
143 klass->change_state = GST_DEBUG_FUNCPTR (gst_element_change_state);
144 klass->error = GST_DEBUG_FUNCPTR (gst_element_error_func);
145 klass->elementfactory = NULL;
146 klass->padtemplates = NULL;
147 klass->numpadtemplates = 0;
151 gst_element_base_class_init (GstElementClass *klass)
153 GObjectClass *gobject_class;
155 gobject_class = (GObjectClass*) klass;
157 gobject_class->set_property = GST_DEBUG_FUNCPTR(gst_element_set_property);
158 gobject_class->get_property = GST_DEBUG_FUNCPTR(gst_element_get_property);
162 gst_element_init (GstElement *element)
164 element->current_state = GST_STATE_NULL;
165 element->pending_state = GST_STATE_VOID_PENDING;
166 element->numpads = 0;
167 element->numsrcpads = 0;
168 element->numsinkpads = 0;
169 element->pads = NULL;
170 element->loopfunc = NULL;
171 element->sched = NULL;
172 element->sched_private = NULL;
173 element->state_mutex = g_mutex_new ();
174 element->state_cond = g_cond_new ();
178 gst_element_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
180 GstElementClass *oclass = CLASS (object);
182 if (oclass->set_property)
183 (oclass->set_property) (object, prop_id, value, pspec);
187 gst_element_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
189 GstElementClass *oclass = CLASS (object);
191 if (oclass->get_property)
192 (oclass->get_property) (object, prop_id, value, pspec);
196 gst_element_request_pad (GstElement *element, GstPadTemplate *templ, const gchar* name)
198 GstPad *newpad = NULL;
199 GstElementClass *oclass;
201 oclass = CLASS (element);
202 if (oclass->request_new_pad)
203 newpad = (oclass->request_new_pad)(element, templ, name);
210 * gst_element_set_name:
211 * @element: GstElement to set name of
212 * @name: new name of element
214 * Set the name of the element, getting rid of the old name if there was
218 gst_element_set_name (GstElement *element, const gchar *name)
220 g_return_if_fail (element != NULL);
221 g_return_if_fail (GST_IS_ELEMENT (element));
223 gst_object_set_name (GST_OBJECT (element), name);
227 * gst_element_get_name:
228 * @element: GstElement to get name of
230 * Get the name of the element.
232 * Returns: name of the element
235 gst_element_get_name (GstElement *element)
237 g_return_val_if_fail (element != NULL, NULL);
238 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
240 return GST_OBJECT_NAME (element);
244 * gst_element_set_parent:
245 * @element: GstElement to set parent of
246 * @parent: new parent of the object
248 * Set the parent of the element.
251 gst_element_set_parent (GstElement *element, GstObject *parent)
253 g_return_if_fail (element != NULL);
254 g_return_if_fail (GST_IS_ELEMENT (element));
255 g_return_if_fail (GST_OBJECT_PARENT (element) == NULL);
256 g_return_if_fail (parent != NULL);
257 g_return_if_fail (GST_IS_OBJECT (parent));
258 g_return_if_fail ((gpointer)element != (gpointer)parent);
260 gst_object_set_parent (GST_OBJECT (element), parent);
264 * gst_element_get_parent:
265 * @element: GstElement to get the parent of
267 * Get the parent of the element.
269 * Returns: parent of the element
272 gst_element_get_parent (GstElement *element)
274 g_return_val_if_fail (element != NULL, NULL);
275 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
277 return GST_OBJECT_PARENT (element);
281 * gst_element_set_clock:
282 * @element: GstElement to set the clock to
283 * @clock: the clock to set for the element
285 * Set the clock for the element
288 gst_element_set_clock (GstElement *element, GstClock *clock)
290 g_return_if_fail (element != NULL);
291 g_return_if_fail (GST_IS_ELEMENT (element));
293 if (element->setclockfunc)
294 element->setclockfunc (element, clock);
298 * gst_element_get_clock:
299 * @element: GstElement to get the clock of
301 * Get the clock of the element
303 * Returns: the clock of the element.
306 gst_element_get_clock (GstElement *element)
308 g_return_val_if_fail (element != NULL, NULL);
309 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
311 if (element->getclockfunc)
312 return element->getclockfunc (element);
318 * gst_element_clock_wait:
319 * @element: GstElement to wait for the clock
320 * @clock: the clock to wait for
321 * @time: the time to wait for
323 * Wait for a specific time on the clock
325 * Returns: the result of the wait operation
328 gst_element_clock_wait (GstElement *element, GstClock *clock, GstClockTime time)
330 g_return_val_if_fail (element != NULL, GST_CLOCK_ERROR);
331 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_CLOCK_ERROR);
333 if (GST_ELEMENT_SCHED (element)) {
334 return gst_scheduler_clock_wait (GST_ELEMENT_SCHED (element), element, clock, time);
337 return GST_CLOCK_TIMEOUT;
341 * gst_element_add_pad:
342 * @element: element to add pad to
345 * Add a pad (connection point) to the element, setting the parent of the
346 * pad to the element (and thus adding a reference).
349 gst_element_add_pad (GstElement *element, GstPad *pad)
351 g_return_if_fail (element != NULL);
352 g_return_if_fail (GST_IS_ELEMENT (element));
353 g_return_if_fail (pad != NULL);
354 g_return_if_fail (GST_IS_PAD (pad));
356 /* first check to make sure the pad's parent is already set */
357 g_return_if_fail (GST_PAD_PARENT (pad) == NULL);
359 /* then check to see if there's already a pad by that name here */
360 g_return_if_fail (gst_object_check_uniqueness (element->pads, GST_PAD_NAME(pad)) == TRUE);
362 /* set the pad's parent */
363 GST_DEBUG (GST_CAT_ELEMENT_PADS,"setting parent of pad '%s' to '%s'",
364 GST_PAD_NAME (pad), GST_ELEMENT_NAME (element));
365 gst_object_set_parent (GST_OBJECT (pad), GST_OBJECT (element));
367 /* add it to the list */
368 element->pads = g_list_append (element->pads, pad);
370 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
371 element->numsrcpads++;
373 element->numsinkpads++;
375 /* emit the NEW_PAD signal */
376 g_signal_emit (G_OBJECT (element), gst_element_signals[NEW_PAD], 0, pad);
380 * gst_element_remove_pad:
381 * @element: element to remove pad from
382 * @pad: pad to remove
384 * Remove a pad (connection point) from the element,
387 gst_element_remove_pad (GstElement *element, GstPad *pad)
389 g_return_if_fail (element != NULL);
390 g_return_if_fail (GST_IS_ELEMENT (element));
391 g_return_if_fail (pad != NULL);
392 g_return_if_fail (GST_IS_PAD (pad));
394 g_return_if_fail (GST_PAD_PARENT (pad) == element);
396 /* check to see if the pad is still connected */
397 /* FIXME: what if someone calls _remove_pad instead of
398 _remove_ghost_pad? */
399 if (GST_IS_REAL_PAD (pad)) {
400 g_return_if_fail (GST_RPAD_PEER (pad) == NULL);
403 /* remove it from the list */
404 element->pads = g_list_remove (element->pads, pad);
406 if (gst_pad_get_direction (pad) == GST_PAD_SRC)
407 element->numsrcpads--;
409 element->numsinkpads--;
411 g_signal_emit (G_OBJECT (element), gst_element_signals[PAD_REMOVED], 0, pad);
413 gst_object_unparent (GST_OBJECT (pad));
417 * gst_element_add_ghost_pad:
418 * @element: element to add ghost pad to
419 * @pad: pad from which the new ghost pad will be created
420 * @name: name of the new ghost pad
422 * Create a ghost pad from the given pad, and add it to the list of pads
425 * Returns: the added ghost pad or NULL, if no ghost pad was created.
428 gst_element_add_ghost_pad (GstElement *element, GstPad *pad, gchar *name)
432 g_return_val_if_fail (element != NULL, NULL);
433 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
434 g_return_val_if_fail (pad != NULL, NULL);
435 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
437 /* then check to see if there's already a pad by that name here */
438 g_return_val_if_fail (gst_object_check_uniqueness (element->pads, name) == TRUE, NULL);
440 GST_DEBUG(GST_CAT_ELEMENT_PADS,"creating new ghost pad called %s, from pad %s:%s",
441 name,GST_DEBUG_PAD_NAME(pad));
442 ghostpad = gst_ghost_pad_new (name, pad);
444 /* add it to the list */
445 GST_DEBUG(GST_CAT_ELEMENT_PADS,"adding ghost pad %s to element %s",
446 name, GST_ELEMENT_NAME (element));
447 element->pads = g_list_append (element->pads, ghostpad);
449 /* set the parent of the ghostpad */
450 gst_object_set_parent (GST_OBJECT (ghostpad), GST_OBJECT (element));
452 GST_DEBUG(GST_CAT_ELEMENT_PADS,"added ghostpad %s:%s",GST_DEBUG_PAD_NAME(ghostpad));
454 /* emit the NEW_GHOST_PAD signal */
455 g_signal_emit (G_OBJECT (element), gst_element_signals[NEW_PAD], 0, ghostpad);
461 * gst_element_remove_ghost_pad:
462 * @element: element to remove the ghost pad from
463 * @pad: ghost pad to remove
465 * removes a ghost pad from an element
468 gst_element_remove_ghost_pad (GstElement *element, GstPad *pad)
470 g_return_if_fail (element != NULL);
471 g_return_if_fail (GST_IS_ELEMENT (element));
472 g_return_if_fail (pad != NULL);
473 g_return_if_fail (GST_IS_GHOST_PAD (pad));
475 /* FIXME this is redundant?
476 * wingo 10-july-2001: I don't think so, you have to actually remove the pad
477 * from the element. gst_pad_remove_ghost_pad just removes the ghostpad from
478 * the real pad's ghost pad list
480 gst_pad_remove_ghost_pad (GST_PAD (GST_PAD_REALIZE (pad)), pad);
481 gst_element_remove_pad (element, pad);
486 * gst_element_get_pad:
487 * @element: element to find pad of
488 * @name: name of pad to retrieve
490 * Retrieve a pad from the element by name.
492 * Returns: requested pad if found, otherwise NULL.
495 gst_element_get_pad (GstElement *element, const gchar *name)
499 g_return_val_if_fail (element != NULL, NULL);
500 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
501 g_return_val_if_fail (name != NULL, NULL);
503 if ((pad = gst_element_get_static_pad (element, name)))
506 pad = gst_element_get_request_pad (element, name);
512 * gst_element_get_static_pad:
513 * @element: element to find pad of
514 * @name: name of pad to retrieve
516 * Retrieve a pad from the element by name. This version only retrieves
517 * already-existing (i.e. 'static') pads.
519 * Returns: requested pad if found, otherwise NULL.
522 gst_element_get_static_pad (GstElement *element, const gchar *name)
526 g_return_val_if_fail (element != NULL, NULL);
527 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
528 g_return_val_if_fail (name != NULL, NULL);
530 walk = element->pads;
534 pad = GST_PAD(walk->data);
535 if (strcmp (GST_PAD_NAME(pad), name) == 0) {
536 GST_INFO (GST_CAT_ELEMENT_PADS, "found pad %s:%s", GST_DEBUG_PAD_NAME (pad));
539 walk = g_list_next (walk);
542 GST_INFO (GST_CAT_ELEMENT_PADS, "no such pad '%s' in element \"%s\"", name, GST_OBJECT_NAME (element));
547 * gst_element_get_request_pad:
548 * @element: element to find pad of
549 * @name: name of pad to retrieve
551 * Retrieve a pad from the element by name. This version only retrieves
554 * Returns: requested pad if found, otherwise NULL.
557 gst_element_get_request_pad (GstElement *element, const gchar *name)
559 GstPadTemplate *templ = NULL;
561 const gchar *req_name = NULL;
562 gboolean templ_found = FALSE;
566 gchar *str, *endptr = NULL;
568 g_return_val_if_fail (element != NULL, NULL);
569 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
570 g_return_val_if_fail (name != NULL, NULL);
572 if (strstr (name, "%")) {
573 templ = gst_element_get_pad_template (element, name);
578 list = gst_element_get_pad_template_list(element);
579 while (!templ_found && list) {
580 templ = (GstPadTemplate*) list->data;
581 if (templ->presence == GST_PAD_REQUEST) {
582 /* we know that %s and %d are the only possibilities because of sanity
583 checks in gst_pad_template_new */
584 GST_DEBUG (GST_CAT_PADS, "comparing %s to %s", name, templ->name_template);
585 if ((str = strchr (templ->name_template, '%')) &&
586 strncmp (templ->name_template, name, str - templ->name_template) == 0 &&
587 strlen (name) > str - templ->name_template) {
588 data = name + (str - templ->name_template);
589 if (*(str+1) == 'd') {
591 n = (gint) strtol (data, &endptr, 10);
592 if (endptr && *endptr == '\0') {
612 pad = gst_element_request_pad (element, templ, req_name);
618 * gst_element_get_pad_list:
619 * @element: element to get pads of
621 * Retrieve a list of the pads associated with the element.
623 * Returns: GList of pads
626 gst_element_get_pad_list (GstElement *element)
628 g_return_val_if_fail (element != NULL, NULL);
629 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
631 /* return the list of pads */
632 return element->pads;
636 * gst_element_class_add_pad_template:
637 * @klass: element class to add padtemplate to
638 * @templ: padtemplate to add
640 * Add a padtemplate to an element class. This is useful if you have derived a custom
641 * bin and wish to provide an on-request pad at runtime. Plugin writers should use
642 * gst_element_factory_add_pad_template instead.
645 gst_element_class_add_pad_template (GstElementClass *klass, GstPadTemplate *templ)
647 g_return_if_fail (klass != NULL);
648 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
649 g_return_if_fail (templ != NULL);
650 g_return_if_fail (GST_IS_PAD_TEMPLATE (templ));
652 klass->padtemplates = g_list_append (klass->padtemplates, templ);
653 klass->numpadtemplates++;
657 * gst_element_get_pad_template_list:
658 * @element: element to get padtemplates of
660 * Retrieve a list of the padtemplates associated with the element.
662 * Returns: GList of padtemplates
665 gst_element_get_pad_template_list (GstElement *element)
667 g_return_val_if_fail (element != NULL, NULL);
668 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
670 return CLASS (element)->padtemplates;
674 * gst_element_get_pad_template:
675 * @element: element to get padtemplate of
676 * @name: the name of the padtemplate to get.
678 * Retrieve a padtemplate from this element with the
681 * Returns: the padtemplate with the given name. No unreferencing is necessary.
684 gst_element_get_pad_template (GstElement *element, const guchar *name)
688 g_return_val_if_fail (element != NULL, NULL);
689 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
690 g_return_val_if_fail (name != NULL, NULL);
692 padlist = gst_element_get_pad_template_list (element);
695 GstPadTemplate *padtempl = (GstPadTemplate*) padlist->data;
697 if (!strcmp (padtempl->name_template, name))
700 padlist = g_list_next (padlist);
707 * gst_element_get_compatible_pad_template:
708 * @element: element to get padtemplate of
709 * @templ: a template to find a compatible template for
711 * Generate a padtemplate for this element compatible with the given
712 * template, ie able to link to it.
714 * Returns: the padtemplate. No unreferencing is necessary.
717 gst_element_get_compatible_pad_template (GstElement *element, GstPadTemplate *compattempl)
719 GstPadTemplate *newtempl = NULL;
722 GST_DEBUG(GST_CAT_ELEMENT_PADS,"gst_element_get_pad_template_by_compatible()");
724 g_return_val_if_fail (element != NULL, NULL);
725 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
726 g_return_val_if_fail (compattempl != NULL, NULL);
728 padlist = gst_element_get_pad_template_list (element);
731 GstPadTemplate *padtempl = (GstPadTemplate*) padlist->data;
732 gboolean compat = FALSE;
736 * Check direction (must be opposite)
739 GST_DEBUG(GST_CAT_CAPS,"checking direction and caps");
740 if (padtempl->direction == GST_PAD_SRC &&
741 compattempl->direction == GST_PAD_SINK) {
742 GST_DEBUG(GST_CAT_CAPS,"compatible direction: found src pad template");
743 compat = gst_caps_check_compatibility(GST_PAD_TEMPLATE_CAPS (padtempl),
744 GST_PAD_TEMPLATE_CAPS (compattempl));
745 GST_DEBUG(GST_CAT_CAPS,"caps are %scompatible", (compat?"":"not "));
746 } else if (padtempl->direction == GST_PAD_SINK &&
747 compattempl->direction == GST_PAD_SRC) {
748 GST_DEBUG(GST_CAT_CAPS,"compatible direction: found sink pad template");
749 compat = gst_caps_check_compatibility(GST_PAD_TEMPLATE_CAPS (compattempl),
750 GST_PAD_TEMPLATE_CAPS (padtempl));
751 GST_DEBUG(GST_CAT_CAPS,"caps are %scompatible", (compat?"":"not "));
759 padlist = g_list_next (padlist);
766 * gst_element_request_compatible_pad:
767 * @element: element to request a new pad from
768 * @templ: a pad template to which the new pad should be able to connect
770 * Request a new pad from the element. The template will
771 * be used to decide what type of pad to create. This function
772 * is typically used for elements with a padtemplate with presence
775 * Returns: the new pad that was created.
778 gst_element_request_compatible_pad (GstElement *element, GstPadTemplate *templ)
780 GstPadTemplate *templ_new;
783 g_return_val_if_fail (element != NULL, NULL);
784 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
785 g_return_val_if_fail (templ != NULL, NULL);
787 templ_new = gst_element_get_compatible_pad_template (element, templ);
788 if (templ_new != NULL)
789 pad = gst_element_request_pad (element, templ_new, NULL);
796 * gst_element_get_compatible_pad_filtered:
797 * @element: the element in which the pad should be found
798 * @pad: the pad to find a compatible one for
799 * @filtercaps: the caps to use as a filter
801 * Looks for an unconnected pad to which the given pad can connect to.
802 * It is not guaranteed that connecting the pads will work, though
803 * it should work in most cases.
805 * Returns: the pad to which a connection can be made
808 gst_element_get_compatible_pad_filtered (GstElement *element, GstPad *pad, GstCaps *filtercaps)
811 GstPadTemplate *templ;
813 GstPad *foundpad = NULL;
816 g_return_val_if_fail (element != NULL, NULL);
817 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
818 g_return_val_if_fail (pad != NULL, NULL);
819 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
821 /* let's use the real pad */
822 pad = (GstPad *) GST_PAD_REALIZE (pad);
823 g_return_val_if_fail (pad != NULL, NULL);
824 g_return_val_if_fail (GST_RPAD_PEER (pad) == NULL, NULL);
826 /* try to get an existing unconnected pad */
827 pads = gst_element_get_pad_list (element);
829 GstPad *current = GST_PAD (pads->data);
830 if ((GST_PAD_PEER (GST_PAD_REALIZE (current)) == NULL) &&
831 gst_pad_can_connect_filtered (pad, current, filtercaps)) {
834 pads = g_list_next (pads);
837 /* try to create a new one */
838 /* requesting is a little crazy, we need a template. Let's create one */
839 if (filtercaps != NULL) {
840 templcaps = gst_caps_intersect (filtercaps, (GstCaps *) GST_RPAD_CAPS (pad));
841 if (templcaps == NULL)
844 templcaps = gst_caps_copy (gst_pad_get_caps (pad));
847 templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad), GST_RPAD_DIRECTION (pad),
848 GST_PAD_ALWAYS, templcaps, NULL);
849 foundpad = gst_element_request_compatible_pad (element, templ);
850 gst_object_unref (GST_OBJECT (templ)); /* this will take care of the caps too */
852 /* FIXME: this is broken, but it's in here so autoplugging elements that don't
853 have caps on their source padtemplates (spider) can connect... */
854 if (!foundpad && !filtercaps) {
855 templ = gst_pad_template_new ((gchar *) GST_PAD_NAME (pad), GST_RPAD_DIRECTION (pad),
856 GST_PAD_ALWAYS, NULL, NULL);
857 foundpad = gst_element_request_compatible_pad (element, templ);
858 gst_object_unref (GST_OBJECT (templ));
865 * gst_element_get_compatible_pad:
866 * @element: the element in which the pad should be found
867 * @pad: the pad to find a compatible one for
869 * Looks for an unconnected pad to which the given pad can connect to.
870 * It is not guaranteed that connecting the pads will work, though
871 * it should work in most cases.
873 * Returns: the pad to which a connection can be made
876 gst_element_get_compatible_pad (GstElement *element, GstPad *pad)
878 return gst_element_get_compatible_pad_filtered (element, pad, NULL);
882 * gst_element_connect_filtered:
883 * @src: the element containing source pad
884 * @dest: the element containing destination pad
885 * @filtercaps: the caps to use as filter
887 * Connect the source to the destination element using the filtercaps.
888 * The connection must be from source to destination, the other
889 * direction will not be tried.
890 * The functions looks for existing pads that aren't connected yet.
891 + It will use request pads if possible. But both pads will not be requested.
892 * If multiple connections are possible, only one is established.
894 * Returns: TRUE if the elements could be connected.
897 gst_element_connect_filtered (GstElement *src, GstElement *dest,
900 GList *srcpads, *destpads, *srctempls, *desttempls, *l;
901 GstPad *srcpad, *destpad;
902 GstPadTemplate *srctempl, *desttempl;
905 g_return_val_if_fail (src != NULL, FALSE);
906 g_return_val_if_fail (GST_IS_ELEMENT(src), FALSE);
907 g_return_val_if_fail (dest != NULL, FALSE);
908 g_return_val_if_fail (GST_IS_ELEMENT(dest), FALSE);
910 GST_DEBUG (GST_CAT_ELEMENT_PADS, "attempting to connect element %s to element %s", GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
912 /* loop through the existing pads in the source */
913 srcpads = gst_element_get_pad_list (src);
914 destpads = gst_element_get_pad_list (dest);
916 if (srcpads || destpads) {
918 srcpad = (GstPad *) GST_PAD_REALIZE (srcpads->data);
919 if ((GST_RPAD_DIRECTION (srcpad) == GST_PAD_SRC) &&
920 (GST_PAD_PEER (srcpad) == NULL)) {
921 destpad = gst_element_get_compatible_pad_filtered (dest, srcpad, filtercaps);
922 if (destpad && gst_pad_connect_filtered (srcpad, destpad, filtercaps)) {
923 GST_DEBUG (GST_CAT_ELEMENT_PADS, "connected pad %s:%s to pad %s:%s", GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
927 srcpads = g_list_next (srcpads);
930 /* loop through the existing pads in the destination */
932 destpad = (GstPad *) GST_PAD_REALIZE (destpads->data);
933 if ((GST_RPAD_DIRECTION (destpad) == GST_PAD_SINK) &&
934 (GST_PAD_PEER (destpad) == NULL)) {
935 srcpad = gst_element_get_compatible_pad_filtered (src, destpad, filtercaps);
936 if (srcpad && gst_pad_connect_filtered (srcpad, destpad, filtercaps)) {
937 GST_DEBUG (GST_CAT_ELEMENT_PADS, "connected pad %s:%s to pad %s:%s", GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
941 destpads = g_list_next (destpads);
945 GST_DEBUG (GST_CAT_ELEMENT_PADS, "we might have request pads on both sides, checking...");
946 srctempls = gst_element_get_pad_template_list (src);
947 desttempls = gst_element_get_pad_template_list (dest);
949 if (srctempls && desttempls) {
951 srctempl = (GstPadTemplate*) srctempls->data;
952 if (srctempl->presence == GST_PAD_REQUEST) {
953 for (l=desttempls; l; l=l->next) {
954 desttempl = (GstPadTemplate*) desttempls->data;
955 if (desttempl->presence == GST_PAD_REQUEST && desttempl->direction != srctempl->direction) {
956 if (gst_caps_check_compatibility (gst_pad_template_get_caps (srctempl),
957 gst_pad_template_get_caps (desttempl))) {
958 srcpad = gst_element_get_request_pad (src, srctempl->name_template);
959 destpad = gst_element_get_request_pad (dest, desttempl->name_template);
960 if (gst_pad_connect_filtered (srcpad, destpad, filtercaps)) {
961 GST_DEBUG (GST_CAT_ELEMENT_PADS, "connected pad %s:%s to pad %s:%s",
962 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (destpad));
965 /* FIXME: we have extraneous request pads lying around */
970 srctempls = srctempls->next;
974 GST_DEBUG (GST_CAT_ELEMENT_PADS, "no connection possible from %s to %s", GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (dest));
979 * gst_element_connect_many:
980 * @element_1: the first element in the connection chain
981 * @element_2: the second element in the connection chain
982 * @...: NULL-terminated list of elements to connect in order
984 * Chain together a series of elements. Uses #gst_element_connect.
986 * Returns: TRUE on success, FALSE otherwise.
989 gst_element_connect_many (GstElement *element_1, GstElement *element_2, ...)
993 g_return_val_if_fail (element_1 != NULL && element_2 != NULL, FALSE);
994 g_return_val_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2), FALSE);
996 va_start (args, element_2);
999 if (!gst_element_connect (element_1, element_2))
1002 element_1 = element_2;
1003 element_2 = va_arg (args, GstElement*);
1012 * gst_element_connect:
1013 * @src: element containing source pad
1014 * @dest: element containing destination pad
1016 * Connect the source to the destination element.
1017 * The connection must be from source to destination, the other
1018 * direction will not be tried.
1019 * The functions looks for existing pads and request pads that aren't
1020 * connected yet. If multiple connections are possible, only one is
1023 * Returns: TRUE if the elements could be connected.
1026 gst_element_connect (GstElement *src, GstElement *dest)
1028 return gst_element_connect_filtered (src, dest, NULL);
1032 * gst_element_connect_pads_filtered:
1033 * @src: element containing source pad
1034 * @srcpadname: name of pad in source element
1035 * @dest: element containing destination pad
1036 * @destpadname: name of pad in destination element
1037 * @filtercaps: the caps to use as a filter
1039 * Connect the two named pads of the source and destination elements.
1040 * Side effect is that if one of the pads has no parent, it becomes a
1041 * child of the parent of the other element. If they have different
1042 * parents, the connection fails.
1044 * Returns: TRUE if the pads could be connected.
1047 gst_element_connect_pads_filtered (GstElement *src, const gchar *srcpadname,
1048 GstElement *dest, const gchar *destpadname,
1049 GstCaps *filtercaps)
1051 GstPad *srcpad,*destpad;
1053 g_return_val_if_fail (src != NULL, FALSE);
1054 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
1055 g_return_val_if_fail (srcpadname != NULL, FALSE);
1056 g_return_val_if_fail (dest != NULL, FALSE);
1057 g_return_val_if_fail (GST_IS_ELEMENT (dest), FALSE);
1058 g_return_val_if_fail (destpadname != NULL, FALSE);
1060 /* obtain the pads requested */
1061 srcpad = gst_element_get_pad (src, srcpadname);
1062 if (srcpad == NULL) {
1063 GST_ERROR (src, "source element has no pad \"%s\"", srcpadname);
1066 destpad = gst_element_get_pad (dest, destpadname);
1067 if (srcpad == NULL) {
1068 GST_ERROR (dest, "destination element has no pad \"%s\"", destpadname);
1072 /* we're satisified they can be connected, let's do it */
1073 return gst_pad_connect_filtered (srcpad, destpad, filtercaps);
1077 * gst_element_connect_pads:
1078 * @src: element containing source pad
1079 * @srcpadname: name of pad in source element
1080 * @dest: element containing destination pad
1081 * @destpadname: name of pad in destination element
1083 * Connect the two named pads of the source and destination elements.
1084 * Side effect is that if one of the pads has no parent, it becomes a
1085 * child of the parent of the other element. If they have different
1086 * parents, the connection fails.
1088 * Returns: TRUE if the pads could be connected.
1091 gst_element_connect_pads (GstElement *src, const gchar *srcpadname,
1092 GstElement *dest, const gchar *destpadname)
1094 return gst_element_connect_pads_filtered (src, srcpadname, dest, destpadname, NULL);
1098 * gst_element_disconnect_pads:
1099 * @src: element containing source pad
1100 * @srcpadname: name of pad in source element
1101 * @dest: element containing destination pad
1102 * @destpadname: name of pad in destination element
1104 * Disconnect the two named pads of the source and destination elements.
1107 gst_element_disconnect_pads (GstElement *src, const gchar *srcpadname,
1108 GstElement *dest, const gchar *destpadname)
1110 GstPad *srcpad,*destpad;
1112 g_return_if_fail (src != NULL);
1113 g_return_if_fail (GST_IS_ELEMENT(src));
1114 g_return_if_fail (srcpadname != NULL);
1115 g_return_if_fail (dest != NULL);
1116 g_return_if_fail (GST_IS_ELEMENT(dest));
1117 g_return_if_fail (destpadname != NULL);
1119 /* obtain the pads requested */
1120 srcpad = gst_element_get_pad (src, srcpadname);
1121 if (srcpad == NULL) {
1122 GST_ERROR(src,"source element has no pad \"%s\"",srcpadname);
1125 destpad = gst_element_get_pad (dest, destpadname);
1126 if (srcpad == NULL) {
1127 GST_ERROR(dest,"destination element has no pad \"%s\"",destpadname);
1131 /* we're satisified they can be disconnected, let's do it */
1132 gst_pad_disconnect(srcpad,destpad);
1136 * gst_element_disconnect_many:
1137 * @element_1: the first element in the connection chain
1138 * @element_2: the second element in the connection chain
1139 * @...: NULL-terminated list of elements to disconnect in order
1141 * Disconnect a series of elements. Uses #gst_element_disconnect.
1144 gst_element_disconnect_many (GstElement *element_1, GstElement *element_2, ...)
1148 g_return_if_fail (element_1 != NULL && element_2 != NULL);
1149 g_return_if_fail (GST_IS_ELEMENT (element_1) && GST_IS_ELEMENT (element_2));
1151 va_start (args, element_2);
1154 gst_element_disconnect (element_1, element_2);
1156 element_1 = element_2;
1157 element_2 = va_arg (args, GstElement*);
1164 * gst_element_disconnect:
1165 * @src: source element
1166 * @dest: sink element
1168 * Disconnect all pads connecting the two elements in the direction src -> dest.
1171 gst_element_disconnect (GstElement *src, GstElement *dest)
1176 g_return_if_fail (GST_IS_ELEMENT(src));
1177 g_return_if_fail (GST_IS_ELEMENT(dest));
1179 srcpads = gst_element_get_pad_list (src);
1182 pad = GST_PAD (srcpads->data);
1184 if (GST_PAD_DIRECTION (pad) == GST_PAD_SRC)
1185 if (GST_OBJECT_PARENT (GST_PAD_PEER (pad)) == (GstObject*) dest)
1186 gst_pad_disconnect (pad, GST_PAD_PEER (pad));
1188 srcpads = g_list_next (srcpads);
1193 gst_element_error_func (GstElement* element, GstElement *source, gchar *errormsg)
1195 /* tell the parent */
1196 if (GST_OBJECT_PARENT (element)) {
1197 GST_DEBUG (GST_CAT_EVENT, "forwarding error \"%s\" from %s to %s", errormsg,
1198 GST_ELEMENT_NAME (element), GST_OBJECT_NAME (GST_OBJECT_PARENT (element)));
1200 gst_object_ref (GST_OBJECT (element));
1201 g_signal_emit (G_OBJECT (GST_OBJECT_PARENT (element)), gst_element_signals[ERROR], 0, source, errormsg);
1202 gst_object_unref (GST_OBJECT (element));
1207 * gst_element_error:
1208 * @element: Element with the error
1209 * @error: A printf-like string describing the error
1210 * @...: optional arguments for the string
1212 * This function is used internally by elements to signal an error
1213 * condition. It results in the "error" signal.
1216 gst_element_error (GstElement *element, const gchar *error, ...)
1222 g_return_if_fail (GST_IS_ELEMENT (element));
1223 g_return_if_fail (element != NULL);
1224 g_return_if_fail (error != NULL);
1226 /* create error message */
1227 va_start (var_args, error);
1228 string = g_strdup_vprintf (error, var_args);
1230 GST_INFO (GST_CAT_EVENT, "ERROR in %s: %s", GST_ELEMENT_NAME (element), string);
1232 /* emit the signal, make sure the element stays available */
1233 gst_object_ref (GST_OBJECT (element));
1234 g_signal_emit (G_OBJECT (element), gst_element_signals[ERROR], 0, element, string);
1236 /* tell the scheduler */
1237 if (element->sched) {
1238 gst_scheduler_error (element->sched, element);
1242 gst_object_unref (GST_OBJECT (element));
1247 * gst_element_get_state:
1248 * @element: a #GstElement to get state of
1250 * Gets the state of the element.
1252 * Returns: The #GstElementState of the element
1255 gst_element_get_state (GstElement *element)
1257 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_VOID_PENDING);
1259 return GST_STATE (element);
1263 * gst_element_wait_state_change:
1264 * @element: a #GstElement to wait for
1266 * Waits and blocks until the element changed its state.
1269 gst_element_wait_state_change (GstElement *element)
1271 g_mutex_lock (element->state_mutex);
1272 g_cond_wait (element->state_cond, element->state_mutex);
1273 g_mutex_unlock (element->state_mutex);
1277 * gst_element_set_state:
1278 * @element: a #GstElement to change state of
1279 * @state: the element's new #GstElementState
1281 * Sets the state of the element. This function will try to set the
1282 * requested state by going through all the intermediary states and calling
1283 * the class's state change function for each.
1285 * Returns: whether or not the state was successfully set
1286 * (using #GstElementStateReturn).
1289 gst_element_set_state (GstElement *element, GstElementState state)
1291 GstElementClass *oclass;
1292 GstElementState curpending;
1293 GstElementStateReturn return_val = GST_STATE_SUCCESS;
1295 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_FAILURE);
1297 /* start with the current state */
1298 curpending = GST_STATE(element);
1300 GST_DEBUG_ELEMENT (GST_CAT_STATES, element, "setting state from %s to %s",
1301 gst_element_state_get_name (curpending),
1302 gst_element_state_get_name (state));
1304 /* loop until the final requested state is set */
1305 while (GST_STATE (element) != state
1306 && GST_STATE (element) != GST_STATE_VOID_PENDING) {
1307 /* move the curpending state in the correct direction */
1308 if (curpending < state)
1313 /* set the pending state variable */
1314 /* FIXME: should probably check to see that we don't already have one */
1315 GST_STATE_PENDING (element) = curpending;
1317 if (curpending != state) {
1318 GST_DEBUG_ELEMENT (GST_CAT_STATES, element,
1319 "intermediate: setting state from %s to %s",
1320 gst_element_state_get_name (state),
1321 gst_element_state_get_name (curpending));
1324 /* call the state change function so it can set the state */
1325 oclass = CLASS (element);
1326 if (oclass->change_state)
1327 return_val = (oclass->change_state) (element);
1329 switch (return_val) {
1330 case GST_STATE_FAILURE:
1331 GST_DEBUG_ELEMENT (GST_CAT_STATES, element, "have failed change_state return");
1333 case GST_STATE_ASYNC:
1334 GST_DEBUG_ELEMENT (GST_CAT_STATES, element, "element will change state async");
1336 case GST_STATE_SUCCESS:
1337 /* Last thing we do is verify that a successful state change really
1338 * did change the state... */
1339 if (GST_STATE (element) != curpending) {
1340 GST_DEBUG_ELEMENT (GST_CAT_STATES, element,
1341 "element claimed state-change success, but state didn't change %s, %s <-> %s",
1342 gst_element_state_get_name (GST_STATE (element)),
1343 gst_element_state_get_name (GST_STATE_PENDING (element)),
1344 gst_element_state_get_name (curpending));
1345 return GST_STATE_FAILURE;
1349 /* somebody added a GST_STATE_ and forgot to do stuff here ! */
1350 g_assert_not_reached ();
1358 gst_element_negotiate_pads (GstElement *element)
1360 GList *pads = GST_ELEMENT_PADS (element);
1362 GST_DEBUG_ELEMENT (GST_CAT_CAPS, element, "negotiating pads");
1365 GstPad *pad = GST_PAD (pads->data);
1368 pads = g_list_next (pads);
1370 if (!GST_IS_REAL_PAD (pad))
1373 srcpad = GST_PAD_REALIZE (pad);
1375 /* if we have a connection on this pad and it doesn't have caps
1376 * allready, try to negotiate */
1377 if (GST_PAD_IS_CONNECTED (srcpad) && !GST_PAD_CAPS (srcpad)) {
1378 GstRealPad *sinkpad;
1379 GstElementState otherstate;
1382 sinkpad = GST_RPAD_PEER (GST_PAD_REALIZE (srcpad));
1384 /* check the parent of the peer pad, if there is no parent do nothing */
1385 parent = GST_PAD_PARENT (sinkpad);
1389 otherstate = GST_STATE (parent);
1391 /* swap pads if needed */
1392 if (!GST_PAD_IS_SRC (srcpad)) {
1400 /* only try to negotiate if the peer element is in PAUSED or higher too */
1401 if (otherstate >= GST_STATE_READY) {
1402 GST_DEBUG_ELEMENT (GST_CAT_CAPS, element, "perform negotiate for %s:%s and %s:%s",
1403 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1404 if (!gst_pad_perform_negotiate (GST_PAD (srcpad), GST_PAD (sinkpad)))
1408 GST_DEBUG_ELEMENT (GST_CAT_CAPS, element, "not negotiatiating %s:%s and %s:%s, not in READY yet",
1409 GST_DEBUG_PAD_NAME (srcpad), GST_DEBUG_PAD_NAME (sinkpad));
1418 gst_element_clear_pad_caps (GstElement *element)
1420 GList *pads = GST_ELEMENT_PADS (element);
1422 GST_DEBUG_ELEMENT (GST_CAT_CAPS, element, "clearing pad caps");
1425 GstRealPad *pad = GST_PAD_REALIZE (pads->data);
1427 if (GST_PAD_CAPS (pad)) {
1428 GST_PAD_CAPS (pad) = NULL;
1430 pads = g_list_next (pads);
1434 static GstElementStateReturn
1435 gst_element_change_state (GstElement *element)
1437 GstElementState old_state;
1439 gint old_pending, old_transition;
1441 g_return_val_if_fail (GST_IS_ELEMENT (element), GST_STATE_FAILURE);
1443 old_state = GST_STATE (element);
1444 old_pending = GST_STATE_PENDING (element);
1445 old_transition = GST_STATE_TRANSITION (element);
1447 if (old_pending == GST_STATE_VOID_PENDING || old_state == GST_STATE_PENDING (element)) {
1448 GST_INFO (GST_CAT_STATES, "no state change needed for element %s (VOID_PENDING)", GST_ELEMENT_NAME (element));
1449 return GST_STATE_SUCCESS;
1452 GST_INFO (GST_CAT_STATES, "%s default handler sets state from %s to %s %d", GST_ELEMENT_NAME (element),
1453 gst_element_state_get_name (old_state),
1454 gst_element_state_get_name (old_pending),
1455 GST_STATE_TRANSITION (element));
1457 /* we set the state change early for the negotiation functions */
1458 GST_STATE (element) = old_pending;
1459 GST_STATE_PENDING (element) = GST_STATE_VOID_PENDING;
1461 /* if we are going to paused, we try to negotiate the pads */
1462 if (old_transition == GST_STATE_NULL_TO_READY) {
1463 if (!gst_element_negotiate_pads (element))
1466 /* going to the READY state clears all pad caps */
1467 else if (old_transition == GST_STATE_READY_TO_NULL) {
1468 gst_element_clear_pad_caps (element);
1471 /* tell the scheduler if we have one */
1472 if (element->sched) {
1473 if (gst_scheduler_state_transition (element->sched, element, old_transition)
1474 != GST_STATE_SUCCESS) {
1479 parent = GST_ELEMENT_PARENT (element);
1481 /* tell our parent about the state change */
1482 if (parent && GST_IS_BIN (parent)) {
1483 gst_bin_child_state_change (GST_BIN (parent), old_state, GST_STATE (element), element);
1486 g_signal_emit (G_OBJECT (element), gst_element_signals[STATE_CHANGE],
1487 0, old_state, GST_STATE (element));
1489 /* signal the state change in case somebody is waiting for us */
1490 g_mutex_lock (element->state_mutex);
1491 g_cond_signal (element->state_cond);
1492 g_mutex_unlock (element->state_mutex);
1494 return GST_STATE_SUCCESS;
1497 /* undo the state change */
1498 GST_STATE (element) = old_state;
1499 GST_STATE_PENDING (element) = old_pending;
1501 return GST_STATE_FAILURE;
1505 * gst_element_get_factory:
1506 * @element: element to request the factory
1508 * Retrieves the factory that was used to create this element
1510 * Returns: the factory used for creating this element
1513 gst_element_get_factory (GstElement *element)
1515 GstElementClass *oclass;
1517 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1519 oclass = CLASS (element);
1521 return oclass->elementfactory;
1525 gst_element_dispose (GObject *object)
1527 GstElement *element = GST_ELEMENT (object);
1531 GST_DEBUG_ELEMENT (GST_CAT_REFCOUNTING, element, "dispose");
1533 gst_element_set_state (element, GST_STATE_NULL);
1535 /* first we break all our connections with the ouside */
1536 if (element->pads) {
1538 orig = pads = g_list_copy (element->pads);
1540 pad = GST_PAD (pads->data);
1542 if (GST_PAD_PEER (pad)) {
1543 GST_DEBUG (GST_CAT_REFCOUNTING, "disconnecting pad '%s'",
1544 GST_OBJECT_NAME (GST_OBJECT (GST_PAD (GST_PAD_PEER (pad)))));
1545 gst_pad_disconnect (pad, GST_PAD (GST_PAD_PEER (pad)));
1547 gst_element_remove_pad (element, pad);
1549 pads = g_list_next (pads);
1552 g_list_free (element->pads);
1553 element->pads = NULL;
1556 element->numsrcpads = 0;
1557 element->numsinkpads = 0;
1558 element->numpads = 0;
1559 g_mutex_free (element->state_mutex);
1560 g_cond_free (element->state_cond);
1562 G_OBJECT_CLASS (parent_class)->dispose (object);
1565 #ifndef GST_DISABLE_LOADSAVE
1567 * gst_element_save_thyself:
1568 * @element: GstElement to save
1569 * @parent: the xml parent node
1571 * Saves the element as part of the given XML structure
1573 * Returns: the new xml node
1576 gst_element_save_thyself (GstObject *object,
1580 GstElementClass *oclass;
1581 GParamSpec **specs, *spec;
1583 GValue value = { 0, };
1584 GstElement *element;
1586 g_return_val_if_fail (GST_IS_ELEMENT (object), parent);
1588 element = GST_ELEMENT (object);
1590 oclass = CLASS (element);
1592 xmlNewChild(parent, NULL, "name", GST_ELEMENT_NAME(element));
1594 if (oclass->elementfactory != NULL) {
1595 GstElementFactory *factory = (GstElementFactory *)oclass->elementfactory;
1597 xmlNewChild (parent, NULL, "type", GST_OBJECT_NAME (factory));
1598 xmlNewChild (parent, NULL, "version", factory->details->version);
1601 /* FIXME: what is this? */
1602 /* if (element->manager) */
1603 /* xmlNewChild(parent, NULL, "manager", GST_ELEMENT_NAME(element->manager)); */
1606 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object), &nspecs);
1608 for (i=0; i<nspecs; i++) {
1610 if (spec->flags & G_PARAM_READABLE) {
1613 g_value_init(&value, G_PARAM_SPEC_VALUE_TYPE (spec));
1615 g_object_get_property (G_OBJECT (element), spec->name, &value);
1616 param = xmlNewChild (parent, NULL, "param", NULL);
1617 xmlNewChild (param, NULL, "name", spec->name);
1619 if (G_IS_PARAM_SPEC_STRING (spec))
1620 xmlNewChild (param, NULL, "value", g_value_dup_string (&value));
1621 else if (G_IS_PARAM_SPEC_ENUM (spec))
1622 xmlNewChild (param, NULL, "value", g_strdup_printf ("%d", g_value_get_enum (&value)));
1623 else if (G_IS_PARAM_SPEC_INT64 (spec))
1624 xmlNewChild (param, NULL, "value", g_strdup_printf ("%lld", g_value_get_int64 (&value)));
1626 xmlNewChild (param, NULL, "value", g_strdup_value_contents (&value));
1628 g_value_unset(&value);
1632 pads = GST_ELEMENT_PADS (element);
1635 GstPad *pad = GST_PAD (pads->data);
1636 /* figure out if it's a direct pad or a ghostpad */
1637 if (GST_ELEMENT (GST_OBJECT_PARENT (pad)) == element) {
1638 xmlNodePtr padtag = xmlNewChild (parent, NULL, "pad", NULL);
1639 gst_object_save_thyself (GST_OBJECT (pad), padtag);
1641 pads = g_list_next (pads);
1648 gst_element_restore_thyself (GstObject *object, xmlNodePtr self)
1650 xmlNodePtr children;
1651 GstElement *element;
1652 guchar *name = NULL;
1653 guchar *value = NULL;
1655 element = GST_ELEMENT (object);
1656 g_return_if_fail (element != NULL);
1659 children = self->xmlChildrenNode;
1661 if (!strcmp (children->name, "param")) {
1662 xmlNodePtr child = children->xmlChildrenNode;
1665 if (!strcmp (child->name, "name")) {
1666 name = xmlNodeGetContent (child);
1668 else if (!strcmp (child->name, "value")) {
1669 value = xmlNodeGetContent (child);
1671 child = child->next;
1673 /* FIXME: can this just be g_object_set ? */
1674 gst_util_set_object_arg ((GObject *)G_OBJECT (element), name, value);
1676 children = children->next;
1680 children = self->xmlChildrenNode;
1682 if (!strcmp (children->name, "pad")) {
1683 gst_pad_load_and_connect (children, GST_OBJECT (element));
1685 children = children->next;
1688 if (GST_OBJECT_CLASS(parent_class)->restore_thyself)
1689 (GST_OBJECT_CLASS(parent_class)->restore_thyself) (object, self);
1691 #endif /* GST_DISABLE_LOADSAVE */
1694 * gst_element_yield:
1695 * @element: Element to yield
1697 * Request a yield operation for the child. The scheduler will typically
1698 * give control to another element.
1701 gst_element_yield (GstElement *element)
1703 if (GST_ELEMENT_SCHED (element)) {
1704 gst_scheduler_yield (GST_ELEMENT_SCHED (element), element);
1709 * gst_element_interrupt:
1710 * @element: Element to interrupt
1712 * Request the scheduler of this element to interrupt the execution of
1713 * this element and scheduler another one.
1715 * Returns: a boolean indicating that the child should exit its chain/loop/get
1716 * function ASAP, depending on the scheduler implementation.
1719 gst_element_interrupt (GstElement *element)
1721 if (GST_ELEMENT_SCHED (element)) {
1722 return gst_scheduler_interrupt (GST_ELEMENT_SCHED (element), element);
1729 * gst_element_set_sched:
1730 * @element: Element to set manager of.
1731 * @sched: @GstScheduler to set.
1733 * Sets the scheduler of the element. For internal use only, unless you're
1734 * writing a new bin subclass.
1737 gst_element_set_sched (GstElement *element,
1738 GstScheduler *sched)
1740 g_return_if_fail (GST_IS_ELEMENT (element));
1742 GST_INFO_ELEMENT (GST_CAT_PARENTAGE, element, "setting scheduler to %p", sched);
1744 element->sched = sched;
1748 * gst_element_get_sched:
1749 * @element: Element to get manager of.
1751 * Returns the scheduler of the element.
1753 * Returns: Element's scheduler
1756 gst_element_get_sched (GstElement *element)
1758 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
1760 return element->sched;
1764 * gst_element_set_loop_function:
1765 * @element: Element to set loop function of.
1766 * @loop: Pointer to loop function.
1768 * This sets the loop function for the element. The function pointed to
1769 * can deviate from the GstElementLoopFunction definition in type of
1772 * NOTE: in order for this to take effect, the current loop function *must*
1773 * exit. Assuming the loop function itself is the only one who will cause
1774 * a new loopfunc to be assigned, this should be no problem.
1777 gst_element_set_loop_function (GstElement *element,
1778 GstElementLoopFunction loop)
1780 g_return_if_fail (GST_IS_ELEMENT (element));
1782 /* set the loop function */
1783 element->loopfunc = loop;
1785 /* set the NEW_LOOPFUNC flag so everyone knows to go try again */
1786 GST_FLAG_SET (element, GST_ELEMENT_NEW_LOOPFUNC);
1790 * gst_element_set_eos:
1791 * @element: element to set to the EOS state
1793 * Perform the actions needed to bring the element in the EOS state.
1796 gst_element_set_eos (GstElement *element)
1798 g_return_if_fail (GST_IS_ELEMENT (element));
1800 GST_DEBUG (GST_CAT_EVENT, "setting EOS on element %s", GST_OBJECT_NAME (element));
1802 gst_element_set_state (element, GST_STATE_PAUSED);
1804 g_signal_emit (G_OBJECT (element), gst_element_signals[EOS], 0);
1809 * gst_element_state_get_name:
1810 * @state: a #GstElementState to get the name of
1812 * Gets a string representing the given state.
1814 * Returns: a string with the statename.
1817 gst_element_state_get_name (GstElementState state)
1820 #ifdef GST_DEBUG_COLOR
1821 case GST_STATE_VOID_PENDING: return "NONE_PENDING";break;
1822 case GST_STATE_NULL: return "\033[01;37mNULL\033[00m";break;
1823 case GST_STATE_READY: return "\033[01;31mREADY\033[00m";break;
1824 case GST_STATE_PLAYING: return "\033[01;32mPLAYING\033[00m";break;
1825 case GST_STATE_PAUSED: return "\033[01;33mPAUSED\033[00m";break;
1826 default: return g_strdup_printf ("\033[01;37;41mUNKNOWN!\033[00m(%d)", state);
1828 case GST_STATE_VOID_PENDING: return "NONE_PENDING";break;
1829 case GST_STATE_NULL: return "NULL";break;
1830 case GST_STATE_READY: return "READY";break;
1831 case GST_STATE_PLAYING: return "PLAYING";break;
1832 case GST_STATE_PAUSED: return "PAUSED";break;
1833 default: return "UNKNOWN!";
1840 gst_element_populate_std_props (GObjectClass * klass,
1841 const char *prop_name, guint arg_id, GParamFlags flags)
1843 GQuark prop_id = g_quark_from_string (prop_name);
1846 static GQuark fd_id = 0;
1847 static GQuark blocksize_id;
1848 static GQuark bytesperread_id;
1849 static GQuark dump_id;
1850 static GQuark filesize_id;
1851 static GQuark mmapsize_id;
1852 static GQuark location_id;
1853 static GQuark offset_id;
1854 static GQuark silent_id;
1855 static GQuark touch_id;
1858 fd_id = g_quark_from_static_string ("fd");
1859 blocksize_id = g_quark_from_static_string ("blocksize");
1860 bytesperread_id = g_quark_from_static_string ("bytesperread");
1861 dump_id = g_quark_from_static_string ("dump");
1862 filesize_id = g_quark_from_static_string ("filesize");
1863 mmapsize_id = g_quark_from_static_string ("mmapsize");
1864 location_id = g_quark_from_static_string ("location");
1865 offset_id = g_quark_from_static_string ("offset");
1866 silent_id = g_quark_from_static_string ("silent");
1867 touch_id = g_quark_from_static_string ("touch");
1870 if (prop_id == fd_id) {
1871 pspec = g_param_spec_int ("fd", "File-descriptor",
1872 "File-descriptor for the file being read",
1873 0, G_MAXINT, 0, flags);
1875 else if (prop_id == blocksize_id) {
1876 pspec = g_param_spec_ulong ("blocksize", "Block Size",
1877 "Block size to read per buffer",
1878 0, G_MAXULONG, 4096, flags);
1881 else if (prop_id == bytesperread_id) {
1882 pspec = g_param_spec_int ("bytesperread", "bytesperread",
1884 G_MININT, G_MAXINT, 0, flags);
1887 else if (prop_id == dump_id) {
1888 pspec = g_param_spec_boolean ("dump", "dump", "dump", FALSE, flags);
1891 else if (prop_id == filesize_id) {
1892 pspec = g_param_spec_int64 ("filesize", "File Size",
1893 "Size of the file being read",
1894 0, G_MAXINT64, 0, flags);
1897 else if (prop_id == mmapsize_id) {
1898 pspec = g_param_spec_ulong ("mmapsize", "mmap() Block Size",
1899 "Size in bytes of mmap()d regions",
1900 0, G_MAXULONG, 4 * 1048576, flags);
1903 else if (prop_id == location_id) {
1904 pspec = g_param_spec_string ("location", "File Location",
1905 "Location of the file to read",
1909 else if (prop_id == offset_id) {
1910 pspec = g_param_spec_int64 ("offset", "File Offset",
1911 "Byte offset of current read pointer",
1912 0, G_MAXINT64, 0, flags);
1915 else if (prop_id == silent_id) {
1916 pspec = g_param_spec_boolean ("silent", "silent", "silent",
1920 else if (prop_id == touch_id) {
1921 pspec = g_param_spec_boolean ("touch", "Touch read data",
1922 "Touch data to force disk read before "
1923 "push ()", TRUE, flags);
1926 g_warning ("Unknown - 'standard' property '%s' id %d from klass %s",
1927 prop_name, arg_id, g_type_name (G_OBJECT_CLASS_TYPE (klass)));
1932 g_object_class_install_property (klass, arg_id, pspec);
1937 * gst_element_class_install_std_props:
1938 * @klass: the class to add the properties to
1939 * @first_name: the first in a NULL terminated
1940 * 'name', 'id', 'flags' triplet list.
1941 * @...: the triplet list
1943 * Add a list of standardized properties with types to the @klass.
1944 * the id is for the property switch in your get_prop method, and
1945 * the flags determine readability / writeability.
1948 gst_element_class_install_std_props (GstElementClass * klass, const char *first_name, ...)
1954 g_return_if_fail (GST_IS_ELEMENT_CLASS (klass));
1956 va_start (args, first_name);
1961 int arg_id = va_arg (args, int);
1962 int flags = va_arg (args, int);
1964 gst_element_populate_std_props ((GObjectClass *) klass, name, arg_id, flags);
1966 name = va_arg (args, char *);
1973 * gst_element_get_managing_bin:
1974 * @element: the element in question
1976 * Get the managing bin (a pipeline or a thread, for example) of an element.
1978 * Returns: the bin, or NULL on failure
1981 gst_element_get_managing_bin (GstElement *element)
1985 g_return_val_if_fail (element != NULL, NULL);
1987 bin = GST_BIN (gst_object_get_parent (GST_OBJECT_CAST (element)));
1989 while (bin && !GST_FLAG_IS_SET (GST_OBJECT_CAST (bin), GST_BIN_FLAG_MANAGER))
1990 bin = GST_BIN (gst_object_get_parent (GST_OBJECT_CAST (bin)));