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 gboolean gst_bin_change_state(GstElement *element,
35 GstElementState state);
36 static gboolean gst_bin_change_state_type(GstBin *bin,
37 GstElementState state,
39 static gboolean gst_bin_change_state_norecurse(GstElement *element,
40 GstElementState state);
42 static void gst_bin_create_plan_func(GstBin *bin);
43 static void gst_bin_iterate_func(GstBin *bin);
45 static xmlNodePtr gst_bin_save_thyself(GstElement *element,xmlNodePtr parent);
47 /* Bin signals and args */
59 static void gst_bin_class_init(GstBinClass *klass);
60 static void gst_bin_init(GstBin *bin);
63 static GstElementClass *parent_class = NULL;
64 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
67 gst_bin_get_type(void) {
68 static GtkType bin_type = 0;
71 static const GtkTypeInfo bin_info = {
75 (GtkClassInitFunc)gst_bin_class_init,
76 (GtkObjectInitFunc)gst_bin_init,
79 (GtkClassInitFunc)NULL,
81 bin_type = gtk_type_unique(GST_TYPE_ELEMENT,&bin_info);
87 gst_bin_class_init(GstBinClass *klass) {
88 GtkObjectClass *gtkobject_class;
89 GstElementClass *gstelement_class;
91 gtkobject_class = (GtkObjectClass*)klass;
92 gstelement_class = (GstElementClass*)klass;
94 parent_class = gtk_type_class(GST_TYPE_ELEMENT);
96 gst_bin_signals[OBJECT_ADDED] =
97 gtk_signal_new("object_added",GTK_RUN_FIRST,gtkobject_class->type,
98 GTK_SIGNAL_OFFSET(GstBinClass,object_added),
99 gtk_marshal_NONE__POINTER,GTK_TYPE_NONE,1,
101 gtk_object_class_add_signals(gtkobject_class,gst_bin_signals,LAST_SIGNAL);
103 klass->change_state_type = gst_bin_change_state_type;
104 klass->create_plan = gst_bin_create_plan_func;
105 klass->iterate = gst_bin_iterate_func;
107 gstelement_class->change_state = gst_bin_change_state;
108 gstelement_class->save_thyself = gst_bin_save_thyself;
110 gtkobject_class->destroy = gst_bin_real_destroy;
113 static void gst_bin_init(GstBin *bin) {
114 bin->numchildren = 0;
115 bin->children = NULL;
120 * @name: name of new bin
122 * Create a new bin with given name.
126 GstElement *gst_bin_new(gchar *name) {
127 GstElement *bin = GST_ELEMENT(gtk_type_new(GST_TYPE_BIN));
128 gst_element_set_name(GST_ELEMENT(bin),name);
134 * @bin: #GstBin to add element to
135 * @element: #GstElement to add to bin
137 * Add the given element to the bin. Set the elements parent, and thus
140 void gst_bin_add(GstBin *bin,GstElement *element) {
141 g_return_if_fail(bin != NULL);
142 g_return_if_fail(GST_IS_BIN(bin));
143 g_return_if_fail(element != NULL);
144 g_return_if_fail(GST_IS_ELEMENT(element));
146 bin->children = g_list_append(bin->children,element);
148 gst_object_set_parent(GST_OBJECT(element),GST_OBJECT(bin));
150 /* FIXME: this isn't right, the bin should be complete whether or not
151 the children are, I think. */
152 // if (GST_STATE_IS_SET(element,GST_STATE_COMPLETE)) {
153 if (!GST_STATE_IS_SET(bin,GST_STATE_COMPLETE)) {
154 g_print("GstBin: adding complete element - \n");
155 gst_bin_change_state_norecurse(GST_ELEMENT(bin),GST_STATE_COMPLETE);
158 // g_print("GstBin: adding element - ");
159 // gst_bin_change_state_norecurse(GST_ELEMENT(bin),~GST_STATE_COMPLETE);
162 gtk_signal_emit(GTK_OBJECT(bin),gst_bin_signals[OBJECT_ADDED],element);
167 * @bin: #Gstbin to remove element from
168 * @element: #GstElement to remove
170 * Remove the element from its associated bin, unparenting as well.
172 void gst_bin_remove(GstBin *bin,GstElement *element) {
173 g_return_if_fail(bin != NULL);
174 g_return_if_fail(GST_IS_BIN(bin));
175 g_return_if_fail(element != NULL);
176 g_return_if_fail(GST_IS_ELEMENT(element));
177 g_return_if_fail(bin->children != NULL);
179 gst_object_unparent(GST_OBJECT(element));
180 bin->children = g_list_remove(bin->children,element);
185 static gboolean gst_bin_change_state(GstElement *element,
186 GstElementState state) {
191 // g_print("gst_bin_change_state(\"%s\",%d);\n",
192 // gst_object_get_name(GST_OBJECT(bin)),state);
194 g_return_val_if_fail(GST_IS_BIN(element), FALSE);
195 bin = GST_BIN(element);
196 g_return_val_if_fail(bin->numchildren != 0, FALSE);
199 children = bin->children;
201 child = GST_ELEMENT(children->data);
202 //g_print("gst_bin_change_state setting state on \"%s\"\n",
203 // gst_element_get_name(GST_ELEMENT(child)));
204 if (!gst_element_set_state(child,state)) {
205 g_print("GstBin: child %p failed to set state 0x%08x\n",child,state);
209 children = g_list_next(children);
211 // g_print("<-- \"%s\"\n",gst_object_get_name(GST_OBJECT(bin)));
213 if (GST_ELEMENT_CLASS(parent_class)->change_state)
214 return GST_ELEMENT_CLASS(parent_class)->change_state(element,state);
219 static gboolean gst_bin_change_state_norecurse(GstElement *element,
220 GstElementState state) {
223 g_return_val_if_fail(GST_IS_BIN(element), FALSE);
224 bin = GST_BIN(element);
225 g_return_val_if_fail(bin->numchildren != 0, FALSE);
227 if (GST_ELEMENT_CLASS(parent_class)->change_state)
228 return GST_ELEMENT_CLASS(parent_class)->change_state(element,state);
232 static gboolean gst_bin_change_state_type(GstBin *bin,
233 GstElementState state,
238 // g_print("gst_bin_change_state_type(\"%s\",%d,%d);\n",
239 // gst_object_get_name(GST_OBJECT(bin)),state,type);
241 g_return_val_if_fail(GST_IS_BIN(bin), FALSE);
242 g_return_val_if_fail(bin->numchildren != 0, FALSE);
245 children = bin->children;
247 child = GST_ELEMENT(children->data);
248 if (GST_IS_BIN(child)) {
249 if (!gst_bin_set_state_type(GST_BIN(child),state,type))
251 } else if (GTK_CHECK_TYPE(child,type)) {
252 if (!gst_element_set_state(child,state))
256 children = g_list_next(children);
258 if (type == GST_TYPE_BIN)
259 gst_element_change_state(GST_ELEMENT(bin),state);
265 gboolean gst_bin_set_state_type(GstBin *bin,
266 GstElementState state,
270 // g_print("gst_bin_set_state_type(\"%s\",%d,%d)\n",
271 // gst_object_get_name(GST_OBJECT(bin)),state,type);
273 g_return_val_if_fail(bin != NULL, FALSE);
274 g_return_val_if_fail(GST_IS_BIN(bin), FALSE);
276 oclass = GST_BIN_CLASS(GTK_OBJECT(bin)->klass);
278 if (oclass->change_state_type)
279 (oclass->change_state_type)(bin,state,type);
283 void gst_bin_real_destroy(GtkObject *object) {
284 GstBin *bin = GST_BIN(object);
288 // g_print("in gst_bin_real_destroy()\n");
290 children = bin->children;
292 child = GST_ELEMENT(children->data);
293 gst_element_destroy(child);
294 children = g_list_next(children);
297 g_list_free(bin->children);
301 * gst_bin_get_by_name:
302 * @bin: #Gstbin to search
303 * @name: the element name to search for
305 * get the element with the given name from this bin
307 * Returns: the element with the given name
309 GstElement *gst_bin_get_by_name(GstBin *bin,gchar *name) {
313 g_return_val_if_fail(bin != NULL, NULL);
314 g_return_val_if_fail(GST_IS_BIN(bin), NULL);
315 g_return_val_if_fail(name != NULL, NULL);
317 children = bin->children;
319 child = GST_ELEMENT(children->data);
320 if (!strcmp(child->name,name))
322 children = g_list_next(children);
330 * @bin: #Gstbin to get the list from
332 * get the list of elements in this bin
334 * Returns: a GList of elements
336 GList *gst_bin_get_list(GstBin *bin) {
337 g_return_val_if_fail(bin != NULL, NULL);
338 g_return_val_if_fail(GST_IS_BIN(bin), NULL);
340 return bin->children;
343 static xmlNodePtr gst_bin_save_thyself(GstElement *element,xmlNodePtr parent) {
344 GstBin *bin = GST_BIN(element);
345 xmlNodePtr childlist;
349 if (GST_ELEMENT_CLASS(parent_class)->save_thyself)
350 GST_ELEMENT_CLASS(parent_class)->save_thyself(GST_ELEMENT(bin),parent);
352 childlist = xmlNewChild(parent,NULL,"children",NULL);
354 children = bin->children;
356 child = GST_ELEMENT(children->data);
357 gst_element_save_thyself(child,childlist);
358 children = g_list_next(children);
365 * @bin: #Gstbin to iterate
367 * iterates over the elements in this bin
369 void gst_bin_iterate(GstBin *bin) {
372 oclass = GST_BIN_CLASS(GTK_OBJECT(bin)->klass);
375 (oclass->iterate)(bin);
378 void gst_bin_create_plan(GstBin *bin) {
381 oclass = GST_BIN_CLASS(GTK_OBJECT(bin)->klass);
383 if (oclass->create_plan)
384 (oclass->create_plan)(bin);
387 static void gst_bin_create_plan_func(GstBin *bin) {
396 g_print("GstBin: attempting to create a plan for bin %p\n",bin);
398 /* walk through all the elements to figure out all kinds of things */
399 elements = GST_BIN(bin)->children;
401 element = GST_ELEMENT(elements->data);
403 // have to use cothreads if any elements use loop functions
404 if (element->loopfunc != NULL) {
405 if (bin->threadcontext == NULL) {
406 g_print("GstBin: initializing cothread context\n");
407 bin->threadcontext = cothread_init();
409 if (element->threadstate == NULL) {
410 g_print("GstBin: creating thread state for element\n");
411 element->threadstate = cothread_create(bin->threadcontext);
412 cothread_setfunc(element->threadstate,gst_element_loopfunc_wrapper,
417 /* we need to find all the entry points into the bin */
418 if (GST_IS_SRC(element)) {
419 g_print("GstBin: element '%s' is a source entry point for the bin\n",
420 gst_element_get_name(GST_ELEMENT(element)));
421 bin->entries = g_list_prepend(bin->entries,element);
424 /* go through the list of pads to see if there's a Connection */
425 pads = gst_element_get_pad_list(element);
427 pad = GST_PAD(pads->data);
428 /* we only worry about sink pads */
429 if (gst_pad_get_direction(pad) == GST_PAD_SINK) {
430 /* get the pad's peer */
431 peer = gst_pad_get_peer(pad);
433 /* get the parent of the peer of the pad */
434 outside = GST_ELEMENT(gst_pad_get_parent(peer));
436 /* if it's a connection and it's not ours... */
437 if (GST_IS_CONNECTION(outside) &&
438 (gst_object_get_parent(GST_OBJECT(outside)) != GST_OBJECT(bin))) {
439 g_print("GstBin: element '%s' is the external source Connection \
440 for internal element '%s'\n",
441 gst_element_get_name(GST_ELEMENT(outside)),
442 gst_element_get_name(GST_ELEMENT(element)));
443 bin->entries = g_list_prepend(bin->entries,outside);
447 pads = g_list_next(pads);
450 elements = g_list_next(elements);
452 g_print("GstBin: have %d entries into bin\n",bin->numentries);
455 void gst_bin_iterate_func(GstBin *bin) {
459 g_return_if_fail(bin != NULL);
460 g_return_if_fail(GST_IS_BIN(bin));
461 // g_return_if_fail(GST_FLAG_IS_SET(thread,GST_STATE_RUNNING));
462 g_return_if_fail(bin->numentries > 0);
464 entries = bin->entries;
466 g_print("GstBin: iterating\n");
469 entry = GST_ELEMENT(entries->data);
470 if (GST_IS_SRC(entry))
471 gst_src_push(GST_SRC(entry));
472 else if (GST_IS_CONNECTION(entry))
473 gst_connection_push(GST_CONNECTION(entry));
475 g_assert_not_reached();
476 entries = g_list_next(entries);