2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 GstElementDetails gst_bin_details = {
25 "Simple container object",
27 "Erik Walthinsen <omega@cse.ogi.edu>",
32 void gst_bin_real_destroy(GtkObject *object);
34 static GstElementStateReturn gst_bin_change_state(GstElement *element);
35 static GstElementStateReturn gst_bin_change_state_norecurse(GstBin *bin);
36 static gboolean gst_bin_change_state_type(GstBin *bin,
37 GstElementState state,
40 static void gst_bin_create_plan_func(GstBin *bin);
41 static void gst_bin_iterate_func(GstBin *bin);
43 static xmlNodePtr gst_bin_save_thyself(GstElement *element,xmlNodePtr parent);
45 /* Bin signals and args */
57 static void gst_bin_class_init(GstBinClass *klass);
58 static void gst_bin_init(GstBin *bin);
61 static GstElementClass *parent_class = NULL;
62 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
65 gst_bin_get_type(void) {
66 static GtkType bin_type = 0;
69 static const GtkTypeInfo bin_info = {
73 (GtkClassInitFunc)gst_bin_class_init,
74 (GtkObjectInitFunc)gst_bin_init,
77 (GtkClassInitFunc)NULL,
79 bin_type = gtk_type_unique(GST_TYPE_ELEMENT,&bin_info);
85 gst_bin_class_init(GstBinClass *klass) {
86 GtkObjectClass *gtkobject_class;
87 GstElementClass *gstelement_class;
89 gtkobject_class = (GtkObjectClass*)klass;
90 gstelement_class = (GstElementClass*)klass;
92 parent_class = gtk_type_class(GST_TYPE_ELEMENT);
94 gst_bin_signals[OBJECT_ADDED] =
95 gtk_signal_new("object_added",GTK_RUN_FIRST,gtkobject_class->type,
96 GTK_SIGNAL_OFFSET(GstBinClass,object_added),
97 gtk_marshal_NONE__POINTER,GTK_TYPE_NONE,1,
99 gtk_object_class_add_signals(gtkobject_class,gst_bin_signals,LAST_SIGNAL);
101 klass->change_state_type = gst_bin_change_state_type;
102 klass->create_plan = gst_bin_create_plan_func;
103 klass->iterate = gst_bin_iterate_func;
105 gstelement_class->change_state = gst_bin_change_state;
106 gstelement_class->save_thyself = gst_bin_save_thyself;
108 gtkobject_class->destroy = gst_bin_real_destroy;
111 static void gst_bin_init(GstBin *bin) {
112 bin->numchildren = 0;
113 bin->children = NULL;
118 * @name: name of new bin
120 * Create a new bin with given name.
124 GstElement *gst_bin_new(gchar *name) {
125 GstElement *bin = GST_ELEMENT(gtk_type_new(GST_TYPE_BIN));
126 gst_element_set_name(GST_ELEMENT(bin),name);
132 * @bin: #GstBin to add element to
133 * @element: #GstElement to add to bin
135 * Add the given element to the bin. Set the elements parent, and thus
138 void gst_bin_add(GstBin *bin,GstElement *element) {
139 g_return_if_fail(bin != NULL);
140 g_return_if_fail(GST_IS_BIN(bin));
141 g_return_if_fail(element != NULL);
142 g_return_if_fail(GST_IS_ELEMENT(element));
144 // must be NULL or PAUSED state in order to modify bin
145 g_return_if_fail((GST_STATE(bin) == GST_STATE_NULL) ||
146 (GST_STATE(bin) == GST_STATE_PAUSED));
148 bin->children = g_list_append(bin->children,element);
150 gst_object_set_parent(GST_OBJECT(element),GST_OBJECT(bin));
153 /* FIXME: this isn't right, the bin should be complete whether or not
154 the children are, I think. */
155 // if (GST_STATE_IS_SET(element,GST_STATE_COMPLETE)) {
156 if (!GST_STATE_IS_SET(bin,GST_STATE_COMPLETE)) {
157 g_print("GstBin: adding complete element - ");
158 gst_bin_change_state_norecurse(GST_ELEMENT(bin));
161 // g_print("GstBin: adding element - ");
162 // gst_bin_change_state_norecurse(GST_ELEMENT(bin),~GST_STATE_COMPLETE);
165 /* we know we have at least one child, we just added one... */
166 // if (GST_STATE(element) < GST_STATE_READY)
167 // gst_bin_change_state_norecurse(bin,GST_STATE_READY);
170 gtk_signal_emit(GTK_OBJECT(bin),gst_bin_signals[OBJECT_ADDED],element);
175 * @bin: #GstBin to remove element from
176 * @element: #GstElement to remove
178 * Remove the element from its associated bin, unparenting as well.
180 void gst_bin_remove(GstBin *bin,GstElement *element) {
181 g_return_if_fail(bin != NULL);
182 g_return_if_fail(GST_IS_BIN(bin));
183 g_return_if_fail(element != NULL);
184 g_return_if_fail(GST_IS_ELEMENT(element));
185 g_return_if_fail(bin->children != NULL);
187 // must be NULL or PAUSED state in order to modify bin
188 g_return_if_fail((GST_STATE(bin) == GST_STATE_NULL) ||
189 (GST_STATE(bin) == GST_STATE_PAUSED));
191 gst_object_unparent(GST_OBJECT(element));
192 bin->children = g_list_remove(bin->children,element);
195 /* if we're down to zero children, force state to NULL */
196 if (bin->numchildren == 0)
197 gst_element_set_state(GST_ELEMENT(bin),GST_STATE_NULL);
201 static GstElementStateReturn gst_bin_change_state(GstElement *element) {
206 g_return_val_if_fail(GST_IS_BIN(element), GST_STATE_FAILURE);
207 bin = GST_BIN(element);
209 g_print("gst_bin_change_state(\"%s\"): currently %d(%s), %d(%s) pending\n",
210 gst_element_get_name(element),GST_STATE(element),
211 _gst_print_statename(GST_STATE(element)),GST_STATE_PENDING(element),
212 _gst_print_statename(GST_STATE_PENDING(element)));
214 // g_return_val_if_fail(bin->numchildren != 0, GST_STATE_FAILURE);
217 children = bin->children;
219 child = GST_ELEMENT(children->data);
220 g_print("gst_bin_change_state: setting state on '%s'\n",
221 gst_element_get_name(child));
222 switch (gst_element_set_state(child,GST_STATE_PENDING(element))) {
223 case GST_STATE_FAILURE:
224 GST_STATE_PENDING(element) = GST_STATE_NONE_PENDING;
225 g_print("child '%s' failed to go to state %d(%s)\n",gst_element_get_name(child),
226 GST_STATE_PENDING(element),_gst_print_statename(GST_STATE_PENDING(element)));
227 return GST_STATE_FAILURE;
229 case GST_STATE_ASYNC:
230 g_print("child '%s' is changing state asynchronously\n",gst_element_get_name(child));
234 children = g_list_next(children);
236 // g_print("<-- \"%s\"\n",gst_object_get_name(GST_OBJECT(bin)));
238 // if (GST_STATE_PENDING(element),
240 return gst_bin_change_state_norecurse(bin);
244 static GstElementStateReturn gst_bin_change_state_norecurse(GstBin *bin) {
246 if ((state == GST_STATE_READY) && (GST_STATE(bin) < GST_STATE_READY)) {
247 // gst_bin_create_plan(
251 if (GST_ELEMENT_CLASS(parent_class)->change_state)
252 return GST_ELEMENT_CLASS(parent_class)->change_state(bin);
254 return GST_STATE_FAILURE;
257 static gboolean gst_bin_change_state_type(GstBin *bin,
258 GstElementState state,
263 // g_print("gst_bin_change_state_type(\"%s\",%d,%d);\n",
264 // gst_object_get_name(GST_OBJECT(bin)),state,type);
266 g_return_val_if_fail(GST_IS_BIN(bin), FALSE);
267 g_return_val_if_fail(bin->numchildren != 0, FALSE);
270 children = bin->children;
272 child = GST_ELEMENT(children->data);
273 if (GST_IS_BIN(child)) {
274 if (!gst_bin_set_state_type(GST_BIN(child),state,type))
276 } else if (GTK_CHECK_TYPE(child,type)) {
277 if (!gst_element_set_state(child,state))
281 children = g_list_next(children);
283 if (type == GST_TYPE_BIN)
284 gst_element_change_state(GST_ELEMENT(bin),state);
290 gboolean gst_bin_set_state_type(GstBin *bin,
291 GstElementState state,
295 // g_print("gst_bin_set_state_type(\"%s\",%d,%d)\n",
296 // gst_object_get_name(GST_OBJECT(bin)),state,type);
298 g_return_val_if_fail(bin != NULL, FALSE);
299 g_return_val_if_fail(GST_IS_BIN(bin), FALSE);
301 oclass = GST_BIN_CLASS(GTK_OBJECT(bin)->klass);
303 if (oclass->change_state_type)
304 (oclass->change_state_type)(bin,state,type);
308 void gst_bin_real_destroy(GtkObject *object) {
309 GstBin *bin = GST_BIN(object);
313 // g_print("in gst_bin_real_destroy()\n");
315 children = bin->children;
317 child = GST_ELEMENT(children->data);
318 gst_element_destroy(child);
319 children = g_list_next(children);
322 g_list_free(bin->children);
326 * gst_bin_get_by_name:
327 * @bin: #Gstbin to search
328 * @name: the element name to search for
330 * get the element with the given name from this bin
332 * Returns: the element with the given name
334 GstElement *gst_bin_get_by_name(GstBin *bin,gchar *name) {
338 g_return_val_if_fail(bin != NULL, NULL);
339 g_return_val_if_fail(GST_IS_BIN(bin), NULL);
340 g_return_val_if_fail(name != NULL, NULL);
342 children = bin->children;
344 child = GST_ELEMENT(children->data);
345 if (!strcmp(child->name,name))
347 children = g_list_next(children);
355 * @bin: #Gstbin to get the list from
357 * get the list of elements in this bin
359 * Returns: a GList of elements
361 GList *gst_bin_get_list(GstBin *bin) {
362 g_return_val_if_fail(bin != NULL, NULL);
363 g_return_val_if_fail(GST_IS_BIN(bin), NULL);
365 return bin->children;
368 static xmlNodePtr gst_bin_save_thyself(GstElement *element,xmlNodePtr parent) {
369 GstBin *bin = GST_BIN(element);
370 xmlNodePtr childlist;
374 if (GST_ELEMENT_CLASS(parent_class)->save_thyself)
375 GST_ELEMENT_CLASS(parent_class)->save_thyself(GST_ELEMENT(bin),parent);
377 childlist = xmlNewChild(parent,NULL,"children",NULL);
379 children = bin->children;
381 child = GST_ELEMENT(children->data);
382 gst_element_save_thyself(child,childlist);
383 children = g_list_next(children);
390 * @bin: #Gstbin to iterate
392 * iterates over the elements in this bin
394 void gst_bin_iterate(GstBin *bin) {
397 oclass = GST_BIN_CLASS(GTK_OBJECT(bin)->klass);
399 g_print("gst_bin_iterate()\n");
401 (oclass->iterate)(bin);
404 void gst_bin_create_plan(GstBin *bin) {
407 oclass = GST_BIN_CLASS(GTK_OBJECT(bin)->klass);
409 if (oclass->create_plan)
410 (oclass->create_plan)(bin);
414 static void gst_bin_create_plan_func(GstBin *bin) {
423 g_print("GstBin: attempting to create a plan for bin %p\n",bin);
425 /* walk through all the elements to figure out all kinds of things */
426 elements = GST_BIN(bin)->children;
428 element = GST_ELEMENT(elements->data);
430 // have to use cothreads if any elements use loop functions, or if any
431 // of them have nontrivial chain functions
432 if (element->loopfunc != NULL) {
433 if (bin->threadcontext == NULL) {
434 g_print("GstBin: initializing cothread context\n");
435 bin->threadcontext = cothread_init();
437 if (element->threadstate == NULL) {
438 g_print("GstBin: creating thread state for element\n");
439 element->threadstate = cothread_create(bin->threadcontext);
440 cothread_setfunc(element->threadstate,gst_element_loopfunc_wrapper,
445 // we need to find all the entry points into the bin
446 if (GST_IS_SRC(element)) {
447 g_print("GstBin: element '%s' is a source entry point for the bin\n",
448 gst_element_get_name(GST_ELEMENT(element)));
449 bin->entries = g_list_prepend(bin->entries,element);
452 // go through the list of pads to see if there's a Connection
453 pads = gst_element_get_pad_list(element);
455 pad = GST_PAD(pads->data);
456 /* we only worry about sink pads */
457 if (gst_pad_get_direction(pad) == GST_PAD_SINK) {
458 /* get the pad's peer */
459 peer = gst_pad_get_peer(pad);
461 /* get the parent of the peer of the pad */
462 outside = GST_ELEMENT(gst_pad_get_parent(peer));
464 /* if it's a connection and it's not ours... */
465 if (GST_IS_CONNECTION(outside) &&
466 (gst_object_get_parent(GST_OBJECT(outside)) != GST_OBJECT(bin))) {
467 g_print("GstBin: element '%s' is the external source Connection \
468 for internal element '%s'\n",
469 gst_element_get_name(GST_ELEMENT(outside)),
470 gst_element_get_name(GST_ELEMENT(element)));
471 bin->entries = g_list_prepend(bin->entries,outside);
475 pads = g_list_next(pads);
478 elements = g_list_next(elements);
480 g_print("have %d entries into bin\n",bin->numentries);
484 static int gst_bin_loopfunc_wrapper(int argc,char *argv[]) {
485 GstElement *element = GST_ELEMENT(argv);
490 // g_print("** gst_bin_loopfunc_wrapper(%d,\"%s\")\n",
491 // argc,gst_element_get_name(element));
493 if (element->loopfunc != NULL) {
494 // g_print("** gst_bin_loopfunc_wrapper(): element has loop function, calling it\n");
496 (element->loopfunc)(element);
499 // g_print("** gst_bin_loopfunc_wrapper(): element is chain-based, calling in infinite loop\n");
500 if (GST_IS_SRC(element)) {
502 // g_print("** gst_bin_loopfunc_wrapper(): calling push function of source\n");
503 gst_src_push(GST_SRC(element));
507 pads = element->pads;
509 pad = GST_PAD(pads->data);
510 if (pad->direction == GST_PAD_SINK) {
511 // g_print("** gst_bin_loopfunc_wrapper(): pulling a buffer\n");
512 buf = gst_pad_pull(pad);
513 // g_print("** gst_bin_loopfunc_wrapper(): calling chain function\n");
514 (pad->chainfunc)(pad,buf);
516 pads = g_list_next(pads);
523 static void gst_bin_pullfunc_wrapper(GstPad *pad) {
524 // g_print("** in gst_bin_pullfunc_wrapper()============================= %s\n",
525 // gst_element_get_name(GST_ELEMENT(pad->parent)));
526 cothread_switch(GST_ELEMENT(pad->parent)->threadstate);
529 static void gst_bin_pushfunc_wrapper(GstPad *pad) {
530 // g_print("** in gst_bin_pushfunc_wrapper()============================= %s\n",
531 // gst_element_get_name(GST_ELEMENT(pad->parent)));
532 cothread_switch(GST_ELEMENT(pad->parent)->threadstate);
535 static void gst_bin_create_plan_func(GstBin *bin) {
543 g_print("creating plan for bin\n");
545 // first loop through all children to see if we need cothreads
546 // we break immediately when we find we need to, why keep searching?
547 elements = bin->children;
549 element = GST_ELEMENT(elements->data);
550 // if it's a loop-based element, use cothreads
551 if (element->loopfunc != NULL) {
552 bin->need_cothreads = TRUE;
555 // if it's a complex element, use cothreads
556 if (GST_ELEMENT_IS_MULTI_IN(element)) {
557 bin->need_cothreads = TRUE;
560 // if it has more than one input pad, use cothreads
562 pads = gst_element_get_pad_list(element);
564 pad = GST_PAD(pads->data);
565 if (pad->direction == GST_PAD_SINK)
567 pads = g_list_next(pads);
570 bin->need_cothreads = TRUE;
573 elements = g_list_next(elements);
576 if (bin->need_cothreads) {
577 g_print("BIN: need cothreads\n");
579 // first create thread context
580 if (bin->threadcontext == NULL) {
581 bin->threadcontext = cothread_init();
582 g_print("initialized cothread context\n");
585 // walk through all the children
586 elements = bin->children;
588 element = GST_ELEMENT(elements->data);
590 // start by creating thread state for the element
591 if (element->threadstate == NULL) {
592 element->threadstate = cothread_create(bin->threadcontext);
593 cothread_setfunc(element->threadstate,gst_bin_loopfunc_wrapper,
597 pads = gst_element_get_pad_list(element);
599 pad = GST_PAD(pads->data);
600 g_print("setting push&pull handlers for %s:%s\n",
601 gst_element_get_name(element),gst_pad_get_name(pad));
602 // if (pad->direction == GST_PAD_SRC)
603 pad->pushfunc = gst_bin_pushfunc_wrapper;
605 pad->pullfunc = gst_bin_pullfunc_wrapper;
606 pads = g_list_next(pads);
608 elements = g_list_next(elements);
611 g_print("BIN: don't need cothreads, looking for entry points\n");
612 // clear previous plan state
613 g_list_free(bin->entries);
615 // we have to find which elements will drive an iteration
616 elements = bin->children;
618 element = GST_ELEMENT(elements->data);
619 if (GST_IS_SRC(element)) {
620 g_print("adding '%s' as entry point\n",gst_element_get_name(element));
621 bin->entries = g_list_prepend(bin->entries,element);
624 elements = g_list_next(elements);
629 void gst_bin_iterate_func(GstBin *bin) {
633 g_print("gst_bin_iterate_func()\n");
635 g_return_if_fail(bin != NULL);
636 g_return_if_fail(GST_IS_BIN(bin));
637 g_return_if_fail(GST_STATE(bin) == GST_STATE_PLAYING);
638 g_return_if_fail(bin->numentries > 0);
640 g_print("GstBin: iterating\n");
642 if (bin->need_cothreads) {
643 // all we really have to do is switch to the first child
644 // FIXME this should be lots more intelligent about where to start
645 // g_print("** in gst_bin_iterate_func()==================================%s\n",
646 // gst_element_get_name(GST_ELEMENT(bin->children->data)));
647 cothread_switch(GST_ELEMENT(bin->children->data)->threadstate);
649 entries = bin->entries;
652 entry = GST_ELEMENT(entries->data);
653 if (GST_IS_SRC(entry))
654 gst_src_push(GST_SRC(entry));
655 else if (GST_IS_CONNECTION(entry))
656 gst_connection_push(GST_CONNECTION(entry));
658 g_assert_not_reached();
659 entries = g_list_next(entries);