Megapatch, changes which states are available, how they're used, and how they're...
[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 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,
38                                           GtkType type);
39
40 static void gst_bin_create_plan_func(GstBin *bin);
41 static void gst_bin_iterate_func(GstBin *bin);
42
43 static xmlNodePtr gst_bin_save_thyself(GstElement *element,xmlNodePtr parent);
44
45 /* Bin signals and args */
46 enum {
47   OBJECT_ADDED,
48   LAST_SIGNAL
49 };
50
51 enum {
52   ARG_0,
53   /* FILL ME */
54 };
55
56
57 static void gst_bin_class_init(GstBinClass *klass);
58 static void gst_bin_init(GstBin *bin);
59
60
61 static GstElementClass *parent_class = NULL;
62 static guint gst_bin_signals[LAST_SIGNAL] = { 0 };
63
64 GtkType
65 gst_bin_get_type(void) {
66   static GtkType bin_type = 0;
67
68   if (!bin_type) {
69     static const GtkTypeInfo bin_info = {
70       "GstBin",
71       sizeof(GstBin),
72       sizeof(GstBinClass),
73       (GtkClassInitFunc)gst_bin_class_init,
74       (GtkObjectInitFunc)gst_bin_init,
75       (GtkArgSetFunc)NULL,
76       (GtkArgGetFunc)NULL,
77       (GtkClassInitFunc)NULL,
78     };
79     bin_type = gtk_type_unique(GST_TYPE_ELEMENT,&bin_info);
80   }
81   return bin_type;
82 }
83
84 static void
85 gst_bin_class_init(GstBinClass *klass) {
86   GtkObjectClass *gtkobject_class;
87   GstElementClass *gstelement_class;
88
89   gtkobject_class = (GtkObjectClass*)klass;
90   gstelement_class = (GstElementClass*)klass;
91
92   parent_class = gtk_type_class(GST_TYPE_ELEMENT);
93
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,
98                    GST_TYPE_ELEMENT);
99   gtk_object_class_add_signals(gtkobject_class,gst_bin_signals,LAST_SIGNAL);
100
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;
104
105   gstelement_class->change_state = gst_bin_change_state;
106   gstelement_class->save_thyself = gst_bin_save_thyself;
107
108   gtkobject_class->destroy = gst_bin_real_destroy;
109 }
110
111 static void gst_bin_init(GstBin *bin) {
112   bin->numchildren = 0;
113   bin->children = NULL;
114 }
115
116 /**
117  * gst_bin_new:
118  * @name: name of new bin
119  *
120  * Create a new bin with given name.
121  *
122  * Returns: new bin
123  */
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);
127   return bin;
128 }
129
130 /**
131  * gst_bin_add:
132  * @bin: #GstBin to add element to
133  * @element: #GstElement to add to bin
134  *
135  * Add the given element to the bin.  Set the elements parent, and thus
136  * add a reference.
137  */
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));
143
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));
147
148   bin->children = g_list_append(bin->children,element);
149   bin->numchildren++;
150   gst_object_set_parent(GST_OBJECT(element),GST_OBJECT(bin));
151
152 #ifdef OLDSTATE
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));
159     }
160 //  } else {
161 //    g_print("GstBin: adding element - ");
162 //  gst_bin_change_state_norecurse(GST_ELEMENT(bin),~GST_STATE_COMPLETE);
163 //  }
164 #else
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);
168 #endif
169
170   gtk_signal_emit(GTK_OBJECT(bin),gst_bin_signals[OBJECT_ADDED],element);
171 }
172
173 /**
174  * gst_bin_remove:
175  * @bin: #GstBin to remove element from
176  * @element: #GstElement to remove
177  *
178  * Remove the element from its associated bin, unparenting as well.
179  */
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);
186
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));
190
191   gst_object_unparent(GST_OBJECT(element));
192   bin->children = g_list_remove(bin->children,element);
193   bin->numchildren--;
194
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);
198 }
199
200
201 static GstElementStateReturn gst_bin_change_state(GstElement *element) {
202   GstBin *bin;
203   GList *children;
204   GstElement *child;
205
206   g_return_val_if_fail(GST_IS_BIN(element), GST_STATE_FAILURE);
207   bin = GST_BIN(element);
208
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)));
213
214 //  g_return_val_if_fail(bin->numchildren != 0, GST_STATE_FAILURE);
215
216 //  g_print("-->\n");
217   children = bin->children;
218   while (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;
228         break;
229       case GST_STATE_ASYNC:
230         g_print("child '%s' is changing state asynchronously\n",gst_element_get_name(child));
231         break;
232     }
233 //    g_print("\n");
234     children = g_list_next(children);
235   }
236 //  g_print("<-- \"%s\"\n",gst_object_get_name(GST_OBJECT(bin)));
237
238 //  if (GST_STATE_PENDING(element),
239
240   return gst_bin_change_state_norecurse(bin);
241 }
242
243
244 static GstElementStateReturn gst_bin_change_state_norecurse(GstBin *bin) {
245 /*
246   if ((state == GST_STATE_READY) && (GST_STATE(bin) < GST_STATE_READY)) {
247 //    gst_bin_create_plan(
248   }
249 */
250
251   if (GST_ELEMENT_CLASS(parent_class)->change_state)
252     return GST_ELEMENT_CLASS(parent_class)->change_state(bin);
253   else
254     return GST_STATE_FAILURE;
255 }
256
257 static gboolean gst_bin_change_state_type(GstBin *bin,
258                                           GstElementState state,
259                                           GtkType type) {
260   GList *children;
261   GstElement *child;
262
263 //  g_print("gst_bin_change_state_type(\"%s\",%d,%d);\n",
264 //          gst_object_get_name(GST_OBJECT(bin)),state,type);
265
266   g_return_val_if_fail(GST_IS_BIN(bin), FALSE);
267   g_return_val_if_fail(bin->numchildren != 0, FALSE);
268
269 //  g_print("-->\n");
270   children = bin->children;
271   while (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))
275         return FALSE;
276     } else if (GTK_CHECK_TYPE(child,type)) {
277       if (!gst_element_set_state(child,state))
278         return FALSE;
279     }
280 //    g_print("\n");
281     children = g_list_next(children);
282   }
283   if (type == GST_TYPE_BIN)
284     gst_element_change_state(GST_ELEMENT(bin),state);
285
286   return TRUE;
287 }
288
289
290 gboolean gst_bin_set_state_type(GstBin *bin,
291                                 GstElementState state,
292                                 GtkType type) {
293   GstBinClass *oclass;
294
295 //  g_print("gst_bin_set_state_type(\"%s\",%d,%d)\n",
296 //          gst_object_get_name(GST_OBJECT(bin)),state,type);
297
298   g_return_val_if_fail(bin != NULL, FALSE);
299   g_return_val_if_fail(GST_IS_BIN(bin), FALSE);
300
301   oclass = GST_BIN_CLASS(GTK_OBJECT(bin)->klass);
302
303   if (oclass->change_state_type)
304     (oclass->change_state_type)(bin,state,type);
305         return TRUE;
306 }
307
308 void gst_bin_real_destroy(GtkObject *object) {
309   GstBin *bin = GST_BIN(object);
310   GList *children;
311   GstElement *child;
312
313 //  g_print("in gst_bin_real_destroy()\n");
314
315   children = bin->children;
316   while (children) {
317     child = GST_ELEMENT(children->data);
318     gst_element_destroy(child);
319     children = g_list_next(children);
320   }
321
322   g_list_free(bin->children);
323 }
324
325 /**
326  * gst_bin_get_by_name:
327  * @bin: #Gstbin to search
328  * @name: the element name to search for
329  *
330  * get the element with the given name from this bin
331  *
332  * Returns: the element with the given name
333  */
334 GstElement *gst_bin_get_by_name(GstBin *bin,gchar *name) {
335   GList *children;
336   GstElement *child;
337
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);
341
342   children = bin->children;
343   while (children) {
344     child = GST_ELEMENT(children->data);
345     if (!strcmp(child->name,name))
346       return child;
347     children = g_list_next(children);
348   }
349
350   return NULL;
351 }
352
353 /**
354  * gst_bin_get_list:
355  * @bin: #Gstbin to get the list from
356  *
357  * get the list of elements in this bin
358  *
359  * Returns: a GList of elements
360  */
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);
364
365   return bin->children;
366 }
367
368 static xmlNodePtr gst_bin_save_thyself(GstElement *element,xmlNodePtr parent) {
369   GstBin *bin = GST_BIN(element);
370   xmlNodePtr childlist;
371   GList *children;
372   GstElement *child;
373
374   if (GST_ELEMENT_CLASS(parent_class)->save_thyself)
375     GST_ELEMENT_CLASS(parent_class)->save_thyself(GST_ELEMENT(bin),parent);
376
377   childlist = xmlNewChild(parent,NULL,"children",NULL);
378
379   children = bin->children;
380   while (children) {
381     child = GST_ELEMENT(children->data);
382     gst_element_save_thyself(child,childlist);
383     children = g_list_next(children);
384   }
385         return childlist;
386 }
387
388 /**
389  * gst_bin_iterate:
390  * @bin: #Gstbin to iterate
391  *
392  * iterates over the elements in this bin
393  */
394 void gst_bin_iterate(GstBin *bin) {
395   GstBinClass *oclass;
396
397   oclass = GST_BIN_CLASS(GTK_OBJECT(bin)->klass);
398
399 g_print("gst_bin_iterate()\n");
400   if (oclass->iterate)
401     (oclass->iterate)(bin);
402 }
403
404 void gst_bin_create_plan(GstBin *bin) {
405   GstBinClass *oclass;
406
407   oclass = GST_BIN_CLASS(GTK_OBJECT(bin)->klass);
408
409   if (oclass->create_plan)
410     (oclass->create_plan)(bin);
411 }
412
413 #ifdef OLD_STUFF
414 static void gst_bin_create_plan_func(GstBin *bin) {
415   GList *elements;
416   GstElement *element;
417   GList *pads;
418   GstPad *pad, *peer;
419   GstElement *outside;
420
421   bin->numentries = 0;
422
423   g_print("GstBin: attempting to create a plan for bin %p\n",bin);
424
425   /* walk through all the elements to figure out all kinds of things */
426   elements = GST_BIN(bin)->children;
427   while (elements) {
428     element = GST_ELEMENT(elements->data);
429
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();
436       }
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, 
441                          0,element);
442       }
443     }
444
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);
450       bin->numentries++;
451     } else {
452       // go through the list of pads to see if there's a Connection
453       pads = gst_element_get_pad_list(element);
454       while (pads) {
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);
460           if (!peer) break;
461           /* get the parent of the peer of the pad */
462           outside = GST_ELEMENT(gst_pad_get_parent(peer));
463           if (!outside) break;
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);
472             bin->numentries++;
473           }
474         }
475         pads = g_list_next(pads);
476       }
477     }
478     elements = g_list_next(elements);
479   }
480   g_print("have %d entries into bin\n",bin->numentries);
481 }
482 #endif
483
484 static int gst_bin_loopfunc_wrapper(int argc,char *argv[]) {
485   GstElement *element = GST_ELEMENT(argv);
486   GList *pads;
487   GstPad *pad;
488   GstBuffer *buf;
489
490 //  g_print("** gst_bin_loopfunc_wrapper(%d,\"%s\")\n",
491 //          argc,gst_element_get_name(element));
492
493   if (element->loopfunc != NULL) {
494 //    g_print("** gst_bin_loopfunc_wrapper(): element has loop function, calling it\n");
495     while (1) {
496       (element->loopfunc)(element);
497     }
498   } else {
499 //    g_print("** gst_bin_loopfunc_wrapper(): element is chain-based, calling in infinite loop\n");
500     if (GST_IS_SRC(element)) {
501       while (1) {
502 //        g_print("** gst_bin_loopfunc_wrapper(): calling push function of source\n");
503         gst_src_push(GST_SRC(element));
504       }
505     } else {
506       while (1) {
507         pads = element->pads;
508         while (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);
515           }
516           pads = g_list_next(pads);
517         }
518       }
519     }
520   }
521 }
522
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);
527 }
528
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);
533 }
534
535 static void gst_bin_create_plan_func(GstBin *bin) {
536   GList *elements;
537   GstElement *element;
538   int sink_pads;
539   GList *pads;
540   GstPad *pad, *peer;
541   GstElement *outside;
542
543   g_print("creating plan for bin\n");
544
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;
548   while (elements) {
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;
553       break;
554     }
555     // if it's a complex element, use cothreads
556     if (GST_ELEMENT_IS_MULTI_IN(element)) {
557       bin->need_cothreads = TRUE;
558       break;
559     }
560     // if it has more than one input pad, use cothreads
561     sink_pads = 0;
562     pads = gst_element_get_pad_list(element);
563     while (pads) {
564       pad = GST_PAD(pads->data);
565       if (pad->direction == GST_PAD_SINK)
566         sink_pads++;
567       pads = g_list_next(pads);
568     }
569     if (sink_pads > 1) {
570       bin->need_cothreads = TRUE;
571       break;
572     }
573     elements = g_list_next(elements);
574   }
575
576   if (bin->need_cothreads) {
577     g_print("BIN: need cothreads\n");
578
579     // first create thread context
580     if (bin->threadcontext == NULL) {
581       bin->threadcontext = cothread_init();
582       g_print("initialized cothread context\n");
583     }
584
585     // walk through all the children
586     elements = bin->children;
587     while (elements) {
588       element = GST_ELEMENT(elements->data);
589
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,
594                          0,(char **)element);
595       }
596
597       pads = gst_element_get_pad_list(element);
598       while (pads) {
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;
604 //        else
605           pad->pullfunc = gst_bin_pullfunc_wrapper;
606         pads = g_list_next(pads);
607       }
608       elements = g_list_next(elements);
609    }
610   } else {
611     g_print("BIN: don't need cothreads, looking for entry points\n");
612     // clear previous plan state
613     g_list_free(bin->entries);
614     bin->numentries = 0;
615     // we have to find which elements will drive an iteration
616     elements = bin->children;
617     while (elements) {
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);
622         bin->numentries++;
623       }
624       elements = g_list_next(elements);
625     }
626   }
627 }
628
629 void gst_bin_iterate_func(GstBin *bin) {
630   GList *entries;
631   GstElement *entry;
632
633   g_print("gst_bin_iterate_func()\n");
634
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);
639
640   g_print("GstBin: iterating\n");
641
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);
648   } else {
649     entries = bin->entries;
650
651     while (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));
657       else
658         g_assert_not_reached();
659       entries = g_list_next(entries);
660     }
661   }
662 //  g_print(",");
663 }