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