b3093b527284d518d4960397b5684b38846736bd
[platform/upstream/gstreamer.git] / gst / gstbin.c
1 /* Gnome-Streamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <gst/gst.h>
21
22 GstElementDetails gst_bin_details = { 
23   "Generic bin",
24   "Bin",
25   "Simple container object",
26   VERSION,
27   "Erik Walthinsen <omega@cse.ogi.edu>",
28   "(C) 1999",
29 };
30
31
32 void gst_bin_real_destroy(GtkObject *object);
33
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,
38                                           GtkType type);
39 static gboolean gst_bin_change_state_norecurse(GstElement *element,
40                                      GstElementState state);
41
42 static void gst_bin_create_plan_func(GstBin *bin);
43 static void gst_bin_iterate_func(GstBin *bin);
44
45 static xmlNodePtr gst_bin_save_thyself(GstElement *element,xmlNodePtr parent);
46
47 /* Bin signals and args */
48 enum {
49   OBJECT_ADDED,
50   LAST_SIGNAL
51 };
52
53 enum {
54   ARG_0,
55   /* FILL ME */
56 };
57
58
59 static void gst_bin_class_init(GstBinClass *klass);
60 static void gst_bin_init(GstBin *bin);
61
62
63 static GstElementClass *parent_class = NULL;
64 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
65
66 GtkType
67 gst_bin_get_type(void) {
68   static GtkType bin_type = 0;
69
70   if (!bin_type) {
71     static const GtkTypeInfo bin_info = {
72       "GstBin",
73       sizeof(GstBin),
74       sizeof(GstBinClass),
75       (GtkClassInitFunc)gst_bin_class_init,
76       (GtkObjectInitFunc)gst_bin_init,
77       (GtkArgSetFunc)NULL,
78       (GtkArgGetFunc)NULL,
79       (GtkClassInitFunc)NULL,
80     };
81     bin_type = gtk_type_unique(GST_TYPE_ELEMENT,&bin_info);
82   }
83   return bin_type;
84 }
85
86 static void
87 gst_bin_class_init(GstBinClass *klass) {
88   GtkObjectClass *gtkobject_class;
89   GstElementClass *gstelement_class;
90
91   gtkobject_class = (GtkObjectClass*)klass;
92   gstelement_class = (GstElementClass*)klass;
93
94   parent_class = gtk_type_class(GST_TYPE_ELEMENT);
95
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,
100                    GST_TYPE_ELEMENT);
101   gtk_object_class_add_signals(gtkobject_class,gst_bin_signals,LAST_SIGNAL);
102
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;
106
107   gstelement_class->change_state = gst_bin_change_state;
108   gstelement_class->save_thyself = gst_bin_save_thyself;
109
110   gtkobject_class->destroy = gst_bin_real_destroy;
111 }
112
113 static void gst_bin_init(GstBin *bin) {
114   bin->numchildren = 0;
115   bin->children = NULL;
116 }
117
118 /**
119  * gst_bin_new:
120  * @name: name of new bin
121  *
122  * Create a new bin with given name.
123  *
124  * Returns: new bin
125  */
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);
129   return bin;
130 }
131
132 /**
133  * gst_bin_add:
134  * @bin: #GstBin to add element to
135  * @element: #GstElement to add to bin
136  *
137  * Add the given element to the bin.  Set the elements parent, and thus
138  * add a reference.
139  */
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));
145
146   bin->children = g_list_append(bin->children,element);
147   bin->numchildren++;
148   gst_object_set_parent(GST_OBJECT(element),GST_OBJECT(bin));
149
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);
156     }
157 //  } else {
158 //    g_print("GstBin: adding element - ");
159 //  gst_bin_change_state_norecurse(GST_ELEMENT(bin),~GST_STATE_COMPLETE);
160 //  }
161
162   gtk_signal_emit(GTK_OBJECT(bin),gst_bin_signals[OBJECT_ADDED],element);
163 }
164
165 /**
166  * gst_bin_remove:
167  * @bin: #Gstbin to remove element from
168  * @element: #GstElement to remove
169  *
170  * Remove the element from its associated bin, unparenting as well.
171  */
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);
178
179   gst_object_unparent(GST_OBJECT(element));
180   bin->children = g_list_remove(bin->children,element);
181   bin->numchildren--;
182 }
183
184
185 static gboolean gst_bin_change_state(GstElement *element,
186                                      GstElementState state) {
187   GstBin *bin;
188   GList *children;
189   GstElement *child;
190
191 //  g_print("gst_bin_change_state(\"%s\",%d);\n",
192 //          gst_object_get_name(GST_OBJECT(bin)),state);
193
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);
197
198 //  g_print("-->\n");
199   children = bin->children;
200   while (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);
206       return FALSE;
207     }
208 //    g_print("\n");
209     children = g_list_next(children);
210   }
211 //  g_print("<-- \"%s\"\n",gst_object_get_name(GST_OBJECT(bin)));
212
213   if (GST_ELEMENT_CLASS(parent_class)->change_state)
214     return GST_ELEMENT_CLASS(parent_class)->change_state(element,state);
215   return TRUE;
216 }
217
218
219 static gboolean gst_bin_change_state_norecurse(GstElement *element,
220                                      GstElementState state) {
221   GstBin *bin;
222
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);
226
227   if (GST_ELEMENT_CLASS(parent_class)->change_state)
228     return GST_ELEMENT_CLASS(parent_class)->change_state(element,state);
229   return TRUE;
230 }
231
232 static gboolean gst_bin_change_state_type(GstBin *bin,
233                                           GstElementState state,
234                                           GtkType type) {
235   GList *children;
236   GstElement *child;
237
238 //  g_print("gst_bin_change_state_type(\"%s\",%d,%d);\n",
239 //          gst_object_get_name(GST_OBJECT(bin)),state,type);
240
241   g_return_val_if_fail(GST_IS_BIN(bin), FALSE);
242   g_return_val_if_fail(bin->numchildren != 0, FALSE);
243
244 //  g_print("-->\n");
245   children = bin->children;
246   while (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))
250         return FALSE;
251     } else if (GTK_CHECK_TYPE(child,type)) {
252       if (!gst_element_set_state(child,state))
253         return FALSE;
254     }
255 //    g_print("\n");
256     children = g_list_next(children);
257   }
258   if (type == GST_TYPE_BIN)
259     gst_element_change_state(GST_ELEMENT(bin),state);
260
261   return TRUE;
262 }
263
264
265 gboolean gst_bin_set_state_type(GstBin *bin,
266                                 GstElementState state,
267                                 GtkType type) {
268   GstBinClass *oclass;
269
270 //  g_print("gst_bin_set_state_type(\"%s\",%d,%d)\n",
271 //          gst_object_get_name(GST_OBJECT(bin)),state,type);
272
273   g_return_val_if_fail(bin != NULL, FALSE);
274   g_return_val_if_fail(GST_IS_BIN(bin), FALSE);
275
276   oclass = GST_BIN_CLASS(GTK_OBJECT(bin)->klass);
277
278   if (oclass->change_state_type)
279     (oclass->change_state_type)(bin,state,type);
280         return TRUE;
281 }
282
283 void gst_bin_real_destroy(GtkObject *object) {
284   GstBin *bin = GST_BIN(object);
285   GList *children;
286   GstElement *child;
287
288 //  g_print("in gst_bin_real_destroy()\n");
289
290   children = bin->children;
291   while (children) {
292     child = GST_ELEMENT(children->data);
293     gst_element_destroy(child);
294     children = g_list_next(children);
295   }
296
297   g_list_free(bin->children);
298 }
299
300 /**
301  * gst_bin_get_by_name:
302  * @bin: #Gstbin to search
303  * @name: the element name to search for
304  *
305  * get the element with the given name from this bin
306  *
307  * Returns: the element with the given name
308  */
309 GstElement *gst_bin_get_by_name(GstBin *bin,gchar *name) {
310   GList *children;
311   GstElement *child;
312
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);
316
317   children = bin->children;
318   while (children) {
319     child = GST_ELEMENT(children->data);
320     if (!strcmp(child->name,name))
321       return child;
322     children = g_list_next(children);
323   }
324
325   return NULL;
326 }
327
328 /**
329  * gst_bin_get_list:
330  * @bin: #Gstbin to get the list from
331  *
332  * get the list of elements in this bin
333  *
334  * Returns: a GList of elements
335  */
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);
339
340   return bin->children;
341 }
342
343 static xmlNodePtr gst_bin_save_thyself(GstElement *element,xmlNodePtr parent) {
344   GstBin *bin = GST_BIN(element);
345   xmlNodePtr childlist;
346   GList *children;
347   GstElement *child;
348
349   if (GST_ELEMENT_CLASS(parent_class)->save_thyself)
350     GST_ELEMENT_CLASS(parent_class)->save_thyself(GST_ELEMENT(bin),parent);
351
352   childlist = xmlNewChild(parent,NULL,"children",NULL);
353
354   children = bin->children;
355   while (children) {
356     child = GST_ELEMENT(children->data);
357     gst_element_save_thyself(child,childlist);
358     children = g_list_next(children);
359   }
360         return childlist;
361 }
362
363 /**
364  * gst_bin_iterate:
365  * @bin: #Gstbin to iterate
366  *
367  * iterates over the elements in this bin
368  */
369 void gst_bin_iterate(GstBin *bin) {
370   GstBinClass *oclass;
371
372   oclass = GST_BIN_CLASS(GTK_OBJECT(bin)->klass);
373
374   if (oclass->iterate)
375     (oclass->iterate)(bin);
376 }
377
378 void gst_bin_create_plan(GstBin *bin) {
379   GstBinClass *oclass;
380
381   oclass = GST_BIN_CLASS(GTK_OBJECT(bin)->klass);
382
383   if (oclass->create_plan)
384     (oclass->create_plan)(bin);
385 }
386
387 static void gst_bin_create_plan_func(GstBin *bin) {
388   GList *elements;
389   GstElement *element;
390   GList *pads;
391   GstPad *pad, *peer;
392   GstElement *outside;
393
394   bin->numentries = 0;
395
396   g_print("GstBin: attempting to create a plan for bin %p\n",bin);
397
398   /* walk through all the elements to figure out all kinds of things */
399   elements = GST_BIN(bin)->children;
400   while (elements) {
401     element = GST_ELEMENT(elements->data);
402
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();
408       }
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, 
413                          0,element);
414       }
415     }
416
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);
422       bin->numentries++;
423     } else {
424       /* go through the list of pads to see if there's a Connection */
425       pads = gst_element_get_pad_list(element);
426       while (pads) {
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);
432           if (!peer) break;
433           /* get the parent of the peer of the pad */
434           outside = GST_ELEMENT(gst_pad_get_parent(peer));
435           if (!outside) break;
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);
444             bin->numentries++;
445           }
446         }
447         pads = g_list_next(pads);
448       }
449     }
450     elements = g_list_next(elements);
451   }
452   g_print("GstBin: have %d entries into bin\n",bin->numentries);
453 }
454
455 void gst_bin_iterate_func(GstBin *bin) {
456   GList *entries;
457   GstElement *entry;
458    
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);
463   
464   entries = bin->entries;  
465
466   g_print("GstBin: iterating\n");
467
468   while (entries) {
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));
474     else
475       g_assert_not_reached();
476     entries = g_list_next(entries);
477   }
478 //  g_print(",");
479 }