Mostly minor little changes, but two interesting things: 1) removed a pthread_join...
[platform/upstream/gstreamer.git] / gst / gstpipeline.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 "gstpipeline.h"
21 #include "gstthread.h"
22 #include "gstsink.h"
23 #include "gstutils.h"
24 #include "gsttype.h"
25 #include "gstautoplug.h"
26
27 GstElementDetails gst_pipeline_details = {
28   "Pipeline object",
29   "Bin",
30   "Complete pipeline object",
31   VERSION,
32   "Erik Walthinsen <omega@cse.ogi.edu>",
33   "(C) 1999",
34 };
35
36
37 /* Pipeline signals and args */
38 enum {
39   /* FILL ME */
40   LAST_SIGNAL
41 };
42
43 enum {
44   ARG_0,
45   /* FILL ME */
46 };
47
48
49 static void                     gst_pipeline_class_init         (GstPipelineClass *klass);
50 static void                     gst_pipeline_init               (GstPipeline *pipeline);
51
52 static GstElementStateReturn    gst_pipeline_change_state       (GstElement *element);
53
54 static void                     gst_pipeline_prepare            (GstPipeline *pipeline);
55
56 static void                     gst_pipeline_have_type          (GstSink *sink, GstSink *sink2, gpointer data);
57 static void                     gst_pipeline_pads_autoplug      (GstElement *src, GstElement *sink);
58
59 static GstBinClass *parent_class = NULL;
60 //static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 };
61
62 GtkType
63 gst_pipeline_get_type (void) {
64   static GtkType pipeline_type = 0;
65
66   if (!pipeline_type) {
67     static const GtkTypeInfo pipeline_info = {
68       "GstPipeline",
69       sizeof(GstPipeline),
70       sizeof(GstPipelineClass),
71       (GtkClassInitFunc)gst_pipeline_class_init,
72       (GtkObjectInitFunc)gst_pipeline_init,
73       (GtkArgSetFunc)NULL,
74       (GtkArgGetFunc)NULL,
75       (GtkClassInitFunc)NULL,
76     };
77     pipeline_type = gtk_type_unique (gst_bin_get_type (), &pipeline_info);
78   }
79   return pipeline_type;
80 }
81
82 static void
83 gst_pipeline_class_init (GstPipelineClass *klass) 
84 {
85   GstElementClass *gstelement_class;
86
87   gstelement_class = (GstElementClass*)klass;
88
89   parent_class = gtk_type_class(gst_bin_get_type());
90
91   gstelement_class->change_state = gst_pipeline_change_state;
92 }
93
94 static void 
95 gst_pipeline_init (GstPipeline *pipeline) 
96 {
97   // we're a manager by default
98   GST_FLAG_SET (pipeline, GST_BIN_FLAG_MANAGER);
99
100   pipeline->src = NULL;
101   pipeline->sinks = NULL;
102 }
103
104
105 /**
106  * gst_pipeline_new:
107  * @name: name of new pipeline
108  *
109  * Create a new pipeline with the given name.
110  *
111  * Returns: newly created GstPipeline
112  */
113 GstElement*
114 gst_pipeline_new (guchar *name) 
115 {
116   return gst_elementfactory_make ("pipeline", name);
117 }
118
119 static void 
120 gst_pipeline_prepare (GstPipeline *pipeline) 
121 {
122   g_print("GstPipeline: preparing pipeline \"%s\" for playing\n", 
123                   gst_element_get_name(GST_ELEMENT(pipeline)));
124 }
125
126 static void 
127 gst_pipeline_have_type (GstSink *sink, GstSink *sink2, gpointer data) 
128 {
129   g_print("GstPipeline: pipeline have type %p\n", (gboolean *)data);
130
131   *(gboolean *)data = TRUE;
132 }
133
134 static GstCaps* 
135 gst_pipeline_typefind (GstPipeline *pipeline, GstElement *element) 
136 {
137   gboolean found = FALSE;
138   GstElement *typefind;
139   GstCaps *caps = NULL;
140
141   g_print("GstPipeline: typefind for element \"%s\" %p\n", 
142                   gst_element_get_name(element), &found);
143
144   typefind = gst_elementfactory_make ("typefind", "typefind");
145   g_return_val_if_fail (typefind != NULL, FALSE);
146
147   gtk_signal_connect (GTK_OBJECT (typefind), "have_type",
148                       GTK_SIGNAL_FUNC (gst_pipeline_have_type), &found);
149
150   gst_pad_connect (gst_element_get_pad (element, "src"),
151                    gst_element_get_pad (typefind, "sink"));
152
153   gst_bin_add (GST_BIN (pipeline), typefind);
154
155   //gst_bin_create_plan (GST_BIN (pipeline));
156   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
157   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
158
159   // keep pushing buffers... the have_type signal handler will set the found flag
160   while (!found) {
161     gst_bin_iterate (GST_BIN (pipeline));
162   }
163
164   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
165
166   if (found) {
167     caps = gst_util_get_pointer_arg (GTK_OBJECT (typefind), "caps");
168     
169     gst_pad_set_caps (gst_element_get_pad (element, "src"), caps);
170   }
171
172   gst_pad_disconnect (gst_element_get_pad (element, "src"),
173                       gst_element_get_pad (typefind, "sink"));
174   gst_bin_remove (GST_BIN (pipeline), typefind);
175   gst_object_unref (GST_OBJECT (typefind));
176
177   return caps;
178 }
179
180 static gboolean 
181 gst_pipeline_pads_autoplug_func (GstElement *src, GstPad *pad, GstElement *sink) 
182 {
183   GList *sinkpads;
184   gboolean connected = FALSE;
185
186   g_print("gstpipeline: autoplug pad connect function for \"%s\" to \"%s\"\n", 
187                   gst_element_get_name(src), gst_element_get_name(sink));
188
189   sinkpads = gst_element_get_pad_list(sink);
190   while (sinkpads) {
191     GstPad *sinkpad = (GstPad *)sinkpads->data;
192
193     // if we have a match, connect the pads
194     if (sinkpad->direction == GST_PAD_SINK && 
195         !GST_PAD_CONNECTED(sinkpad)) 
196     {
197       if (gst_caps_check_compatibility (pad->caps, sinkpad->caps)) {
198         gst_pad_connect(pad, sinkpad);
199         g_print("gstpipeline: autoconnect pad \"%s\" in element %s <-> ", pad->name, 
200                        gst_element_get_name(src));
201         g_print("pad \"%s\" in element %s\n", sinkpad->name,  
202                       gst_element_get_name(sink));
203         connected = TRUE;
204         break;
205       }
206     }
207     sinkpads = g_list_next(sinkpads);
208   }
209
210   if (!connected) {
211     g_print("gstpipeline: no path to sinks for type\n");
212   }
213   return connected;
214 }
215
216 static void 
217 gst_pipeline_pads_autoplug (GstElement *src, GstElement *sink) 
218 {
219   GList *srcpads;
220   gboolean connected = FALSE;
221
222   srcpads = gst_element_get_pad_list(src);
223
224   while (srcpads && !connected) {
225     GstPad *srcpad = (GstPad *)srcpads->data;
226
227     if (srcpad->direction == GST_PAD_SRC) 
228       connected = gst_pipeline_pads_autoplug_func (src, srcpad, sink);
229
230     srcpads = g_list_next(srcpads);
231   }
232   
233   if (!connected) {
234     g_print("gstpipeline: delaying pad connections for \"%s\" to \"%s\"\n",
235                     gst_element_get_name(src), gst_element_get_name(sink));
236     gtk_signal_connect(GTK_OBJECT(src),"new_pad",
237                  GTK_SIGNAL_FUNC(gst_pipeline_pads_autoplug_func), sink);
238   }
239 }
240
241 /**
242  * gst_pipeline_add_src:
243  * @pipeline: the pipeline to add the src to
244  * @src: the src to add to the pipeline
245  *
246  * Adds a src element to the pipeline. This element
247  * will be used as a src for autoplugging. If you add more
248  * than one src element, the previously added element will
249  * be removed.
250  */
251 void 
252 gst_pipeline_add_src (GstPipeline *pipeline, GstElement *src) 
253 {
254   g_return_if_fail (pipeline != NULL);
255   g_return_if_fail (GST_IS_PIPELINE (pipeline));
256   g_return_if_fail (src != NULL);
257   g_return_if_fail (GST_IS_ELEMENT (src));
258
259   if (pipeline->src) {
260     printf("gstpipeline: *WARNING* removing previously added element \"%s\"\n",
261                           gst_element_get_name(pipeline->src));
262     gst_bin_remove(GST_BIN(pipeline), pipeline->src);
263   }
264   pipeline->src = src;
265   gst_bin_add(GST_BIN(pipeline), src);
266 }
267
268 /**
269  * gst_pipeline_add_sink:
270  * @pipeline: the pipeline to add the sink to
271  * @sink: the sink to add to the pipeline
272  *
273  * Adds a sink element to the pipeline. This element
274  * will be used as a sink for autoplugging
275  */
276 void 
277 gst_pipeline_add_sink (GstPipeline *pipeline, GstElement *sink) 
278 {
279   g_return_if_fail (pipeline != NULL);
280   g_return_if_fail (GST_IS_PIPELINE (pipeline));
281   g_return_if_fail (sink != NULL);
282   g_return_if_fail (GST_IS_ELEMENT (sink));
283
284   pipeline->sinks = g_list_prepend (pipeline->sinks, sink);
285   //gst_bin_add(GST_BIN(pipeline), sink);
286 }
287
288 /**
289  * gst_pipeline_autoplug:
290  * @pipeline: the pipeline to autoplug
291  *
292  * Constructs a complete pipeline by automatically
293  * detecting the plugins needed.
294  *
295  * Returns: a gboolean indicating success or failure.
296  */
297 gboolean 
298 gst_pipeline_autoplug (GstPipeline *pipeline) 
299 {
300   GList *elements;
301   GstElement *element, *srcelement = NULL, *sinkelement= NULL;
302   GList **factories;
303   GList **base_factories;
304   GstElementFactory *factory;
305   GList *src_pads;
306   GstCaps *src_caps = 0;
307   guint i, numsinks;
308   gboolean use_thread = FALSE, have_common = FALSE;
309
310   g_return_val_if_fail(pipeline != NULL, FALSE);
311   g_return_val_if_fail(GST_IS_PIPELINE(pipeline), FALSE);
312
313   g_print("GstPipeline: autopluging pipeline \"%s\"\n", 
314                   gst_element_get_name(GST_ELEMENT(pipeline)));
315
316
317   // fase 1, run typedetect on the source if needed... 
318   if (!pipeline->src) {
319     g_print("GstPipeline: no source detected, can't autoplug pipeline \"%s\"\n", 
320                 gst_element_get_name(GST_ELEMENT(pipeline)));
321     return FALSE;
322   }
323
324   g_print("GstPipeline: source \"%s\" has no MIME type, running typefind...\n", 
325         gst_element_get_name(pipeline->src));
326
327   src_caps = gst_pipeline_typefind(pipeline, pipeline->src);
328
329   if (src_caps) {
330     g_print("GstPipeline: source \"%s\" type found %d\n", gst_element_get_name(pipeline->src), 
331           src_caps->id);
332   }
333   else {
334     g_print("GstPipeline: source \"%s\" has no type\n", gst_element_get_name(pipeline->src));
335     return FALSE;
336   }
337
338   srcelement = pipeline->src;
339
340   elements = pipeline->sinks;
341
342   numsinks = g_list_length(elements);
343   factories = g_new0(GList *, numsinks);
344   base_factories = g_new0(GList *, numsinks);
345
346   i = 0;
347   // fase 2, loop over all the sinks..
348   while (elements) {
349     GList *pads;
350     GstPad *pad;
351
352     element = GST_ELEMENT(elements->data);
353
354     pad = (GstPad *)gst_element_get_pad_list (element)->data;
355
356     base_factories[i] = factories[i] = gst_autoplug_caps (src_caps, pad->caps);
357     i++;
358
359     elements = g_list_next(elements);
360   }
361   
362   while (factories[0]) {
363     // fase 3: add common elements 
364     factory = (GstElementFactory *)(factories[0]->data);
365
366     // check to other paths for mathing elements (factories)
367     for (i=1; i<numsinks; i++) {
368       if (factory != (GstElementFactory *)(factories[i]->data)) {
369         goto differ;
370       }
371       factories[i] = g_list_next(factories[i]);
372     }
373     factory = (GstElementFactory *)(factories[0]->data);
374
375     g_print("GstPipeline: common factory \"%s\"\n", factory->name);
376
377     element = gst_elementfactory_create(factory, factory->name);
378     gst_bin_add(GST_BIN(pipeline), element);
379
380     gst_pipeline_pads_autoplug(srcelement, element);
381
382     srcelement = element;
383
384     factories[0] = g_list_next(factories[0]);
385
386     have_common = TRUE;
387   }
388
389 differ:
390   // loop over all the sink elements
391   elements = pipeline->sinks;
392
393   i = 0;
394   while (elements) {
395     GstElement *thesrcelement = srcelement;
396     GstElement *thebin = GST_ELEMENT(pipeline);
397
398     if (g_list_length(base_factories[i]) == 0) goto next;
399
400     sinkelement = (GstElement *)elements->data;
401
402     use_thread = have_common;
403
404     while (factories[i] || sinkelement) {
405       // fase 4: add other elements...
406        
407       if (factories[i]) {
408         factory = (GstElementFactory *)(factories[i]->data);
409         g_print("GstPipeline: factory \"%s\"\n", factory->name);
410         element = gst_elementfactory_create(factory, factory->name);
411         factories[i] = g_list_next(factories[i]);
412       }
413       // we have arived to the final sink element
414       else {
415         element = sinkelement;
416         sinkelement = NULL;
417       }
418
419       // this element suggests the use of a thread, so we set one up...
420       if (GST_ELEMENT_IS_THREAD_SUGGESTED(element) || use_thread) {
421         GstElement *queue;
422         GList *sinkpads;
423         GstPad *srcpad, *sinkpad;
424
425         use_thread = FALSE;
426
427         g_print("GstPipeline: sugest new thread for \"%s\" %08x\n", element->name, GST_FLAGS(element));
428
429         // create a new queue and add to the previous bin
430         queue = gst_elementfactory_make("queue", g_strconcat("queue_", gst_element_get_name(element), NULL));
431         gst_bin_add(GST_BIN(thebin), queue);
432
433         // this will be the new bin for all following elements
434         thebin = gst_elementfactory_make("thread", g_strconcat("thread_", gst_element_get_name(element), NULL));
435
436         srcpad = gst_element_get_pad(queue, "src");
437
438         sinkpads = gst_element_get_pad_list(element);
439         while (sinkpads) {
440           sinkpad = (GstPad *)sinkpads->data;
441
442           // FIXME connect matching pads, not just the first one...
443           if (sinkpad->direction == GST_PAD_SINK && 
444               !GST_PAD_CONNECTED(sinkpad)) {
445             GstCaps *caps = gst_pad_get_caps (sinkpad);
446
447             // the queue has the type of the elements it connects
448             gst_pad_set_caps (srcpad, caps);
449             gst_pad_set_caps (gst_element_get_pad(queue, "sink"), caps);
450             break;
451           }
452           sinkpads = g_list_next(sinkpads);
453         }
454         gst_pipeline_pads_autoplug(thesrcelement, queue);
455
456         gst_bin_add(GST_BIN(thebin), element);
457         gst_bin_add(GST_BIN(pipeline), thebin);
458         thesrcelement = queue;
459       }
460       // no thread needed, easy case
461       else {
462         gst_bin_add(GST_BIN(thebin), element);
463       }
464       gst_pipeline_pads_autoplug(thesrcelement, element);
465
466       // this element is now the new source element
467       thesrcelement = element;
468     }
469 next:
470     elements = g_list_next(elements);
471     i++;
472   }
473   return TRUE;
474   
475   g_print("GstPipeline: unable to autoplug pipeline \"%s\"\n", 
476                   gst_element_get_name(GST_ELEMENT(pipeline)));
477   return FALSE;
478 }
479
480 static GstElementStateReturn 
481 gst_pipeline_change_state (GstElement *element) 
482 {
483   GstPipeline *pipeline;
484
485   g_return_val_if_fail (GST_IS_PIPELINE (element), FALSE);
486   
487   pipeline = GST_PIPELINE (element);
488
489   switch (GST_STATE_TRANSITION (pipeline)) {
490     case GST_STATE_NULL_TO_READY:
491       // we need to set up internal state
492       gst_pipeline_prepare (pipeline);
493       break;
494     default:
495       break;
496   }
497     
498   if (GST_ELEMENT_CLASS (parent_class)->change_state)
499     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
500   
501   return GST_STATE_SUCCESS;
502 }
503
504
505 /**
506  * gst_pipeline_iterate:
507  * @pipeline: GstPipeline to iterate
508  *
509  * Cause the pipeline's contents to be run through one full 'iteration'.
510  */
511 void 
512 gst_pipeline_iterate (GstPipeline *pipeline) 
513 {
514   g_return_if_fail (pipeline != NULL);
515   g_return_if_fail (GST_IS_PIPELINE(pipeline));
516 }