Cleanups.
[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 <gst/gstpipeline.h>
21 #include <gst/gstsink.h>
22 #include <gst/gstutils.h>
23 #include <gst/gsttype.h>
24
25 #include "config.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 GstBin *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   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 gst_pipeline_init(GstPipeline *pipeline) {
95   pipeline->src = NULL;
96   pipeline->sinks = NULL;
97 }
98
99
100 /**
101  * gst_pipeline_new:
102  * @name: name of new pipeline
103  *
104  * Create a new pipeline with the given name.
105  *
106  * Returns: newly created GstPipeline
107  */
108 GstElement *gst_pipeline_new(guchar *name) {
109   GstPipeline *pipeline;
110
111   pipeline = gtk_type_new(gst_pipeline_get_type());
112   gst_element_set_name(GST_ELEMENT(pipeline),name);
113   return GST_ELEMENT(pipeline);
114 }
115
116 static void gst_pipeline_prepare(GstPipeline *pipeline) {
117   g_print("GstPipeline: preparing pipeline \"%s\" for playing\n", 
118                   gst_element_get_name(GST_ELEMENT(pipeline)));
119 }
120
121 static void gst_pipeline_have_type(GstSink *sink, GstSink *sink2, gpointer data) {
122   g_print("GstPipeline: pipeline have type %p\n", (gboolean *)data);
123
124   *(gboolean *)data = TRUE;
125 }
126
127 static guint16 gst_pipeline_typefind(GstPipeline *pipeline, GstElement *element) {
128   gboolean found = FALSE;
129   GstElement *typefind;
130   guint16 type_id = 0;
131
132   g_print("GstPipeline: typefind for element \"%s\" %p\n", 
133                   gst_element_get_name(element), &found);
134
135   typefind = gst_elementfactory_make("typefind","typefind");
136   g_return_val_if_fail(typefind != NULL, FALSE);
137
138   gtk_signal_connect(GTK_OBJECT(typefind),"have_type",
139                     GTK_SIGNAL_FUNC(gst_pipeline_have_type), &found);
140
141   gst_pad_connect(gst_element_get_pad(element,"src"),
142                   gst_element_get_pad(typefind,"sink"));
143
144   gst_bin_add(GST_BIN(pipeline), typefind);
145
146   gst_bin_create_plan(GST_BIN(pipeline));
147   gst_element_set_state(GST_ELEMENT(element),GST_STATE_READY);
148   gst_element_set_state(GST_ELEMENT(element),GST_STATE_PLAYING);
149
150   // keep pushing buffers... the have_type signal handler will set the found flag
151   while (!found) {
152     gst_src_push(GST_SRC(element));
153   }
154
155   gst_element_set_state(GST_ELEMENT(element),GST_STATE_NULL);
156
157   if (found) {
158     type_id = gst_util_get_int_arg(GTK_OBJECT(typefind),"type");
159     gst_pad_set_type_id(gst_element_get_pad(element, "src"), type_id);
160   }
161
162   gst_pad_disconnect(gst_element_get_pad(element,"src"),
163                     gst_element_get_pad(typefind,"sink"));
164   gst_bin_remove(GST_BIN(pipeline), typefind);
165   gst_object_unref(GST_OBJECT(typefind));
166
167   return type_id;
168 }
169
170 static void gst_pipeline_pads_autoplug_func(GstElement *src, GstPad *pad, GstElement *sink) {
171   GList *sinkpads;
172   GstPad *sinkpad;
173   gboolean connected = FALSE;
174
175   g_print("gstpipeline: autoplug pad connect function type %d\n", pad->type);
176
177   sinkpads = gst_element_get_pad_list(sink);
178   while (sinkpads) {
179     sinkpad = (GstPad *)sinkpads->data;
180
181     // if we have a match, connect the pads
182     if (sinkpad->type == pad->type && 
183         sinkpad->direction == GST_PAD_SINK && 
184         !GST_PAD_CONNECTED(sinkpad)) 
185     {
186       gst_pad_connect(pad, sinkpad);
187       g_print("gstpipeline: autoconnect pad \"%s\" (%d) in element %s <-> ", pad->name, 
188                       pad->type, gst_element_get_name(src));
189       g_print("pad \"%s\" (%d) in element %s\n", sinkpad->name, sinkpad->type, 
190                       gst_element_get_name(sink));
191       connected = TRUE;
192       break;
193     }
194     sinkpads = g_list_next(sinkpads);
195   }
196
197   if (!connected) {
198     g_print("gstpipeline: no path to sinks for type %d\n", pad->type);
199   }
200 }
201
202 static void gst_pipeline_pads_autoplug(GstElement *src, GstElement *sink) {
203   GList *srcpads, *sinkpads;
204   gboolean connected = FALSE;
205
206   srcpads = gst_element_get_pad_list(src);
207
208   while (srcpads) {
209     GstPad *srcpad = (GstPad *)srcpads->data;
210     GstPad *sinkpad;
211
212     if (srcpad->direction == GST_PAD_SRC && !GST_PAD_CONNECTED(srcpad)) {
213
214       sinkpads = gst_element_get_pad_list(sink);
215       // FIXME could O(n) if the types were sorted...
216       while (sinkpads) {
217         sinkpad = (GstPad *)sinkpads->data;
218
219         // if we have a match, connect the pads
220         if (sinkpad->type == srcpad->type && 
221             sinkpad->direction == GST_PAD_SINK && 
222             !GST_PAD_CONNECTED(sinkpad)) {
223           gst_pad_connect(srcpad, sinkpad);
224           g_print("gstpipeline: autoconnect pad \"%s\" (%d) in element %s <-> ", 
225                           srcpad->name, srcpad->type, gst_element_get_name(src));
226           g_print("pad \"%s\" (%d) in element %s\n", sinkpad->name, 
227                           sinkpad->type, gst_element_get_name(sink));
228           connected = TRUE;
229           goto end;
230         }
231         sinkpads = g_list_next(sinkpads);
232       }
233     }
234     srcpads = g_list_next(srcpads);
235   }
236   
237 end:
238   if (!connected) {
239     g_print("gstpipeline: delaying pad connections\n");
240     gtk_signal_connect(GTK_OBJECT(src),"new_pad",
241                  GTK_SIGNAL_FUNC(gst_pipeline_pads_autoplug_func), sink);
242   }
243 }
244
245 /**
246  * gst_pipeline_add_src:
247  * @pipeline: the pipeline to add the src to
248  * @src: the src to add to the pipeline
249  *
250  * Adds a src element to the pipeline. This element
251  * will be used as a src for autoplugging. If you add more
252  * than one src element, the previously added element will
253  * be removed.
254  */
255 void gst_pipeline_add_src(GstPipeline *pipeline, GstElement *src) 
256 {
257   g_return_if_fail(pipeline != NULL);
258   g_return_if_fail(GST_IS_PIPELINE(pipeline));
259   g_return_if_fail(src != NULL);
260   g_return_if_fail(GST_IS_ELEMENT(src));
261
262   if (pipeline->src) {
263     printf("gstpipeline: *WARNING* removing previously added element \"%s\"\n",
264                           gst_element_get_name(pipeline->src));
265     gst_bin_remove(GST_BIN(pipeline), pipeline->src);
266   }
267   pipeline->src = src;
268   gst_bin_add(GST_BIN(pipeline), src);
269 }
270
271 /**
272  * gst_pipeline_add_sink:
273  * @pipeline: the pipeline to add the sink to
274  * @sink: the sink to add to the pipeline
275  *
276  * Adds a sink element to the pipeline. This element
277  * will be used as a sink for autoplugging
278  */
279 void gst_pipeline_add_sink(GstPipeline *pipeline, GstElement *sink) 
280 {
281   g_return_if_fail(pipeline != NULL);
282   g_return_if_fail(GST_IS_PIPELINE(pipeline));
283   g_return_if_fail(sink != NULL);
284   g_return_if_fail(GST_IS_ELEMENT(sink));
285
286   pipeline->sinks = g_list_prepend(pipeline->sinks, sink);
287   gst_bin_add(GST_BIN(pipeline), sink);
288 }
289
290 /**
291  * gst_pipeline_autoplug:
292  * @pipeline: the pipeline to autoplug
293  *
294  * Constructs a complete pipeline by automatically
295  * detecting the plugins needed.
296  *
297  * Returns: a gboolean indicating success or failure.
298  */
299 gboolean gst_pipeline_autoplug(GstPipeline *pipeline) {
300   GList *elements;
301   GstElement *element, *srcelement = NULL, *sinkelement= NULL;
302   GList *factories;
303   GstElementFactory *factory;
304   GList *src_types;
305   guint16 src_type = 0, sink_type = 0;
306   gboolean complete = FALSE;
307
308   g_return_val_if_fail(pipeline != NULL, FALSE);
309   g_return_val_if_fail(GST_IS_PIPELINE(pipeline), FALSE);
310
311   g_print("GstPipeline: autopluging pipeline \"%s\"\n", 
312                   gst_element_get_name(GST_ELEMENT(pipeline)));
313
314
315   // fase 1, run typedetect on the source if needed... 
316   if (!pipeline->src) {
317     g_print("GstPipeline: no source detected, can't autoplug pipeline \"%s\"\n", 
318                 gst_element_get_name(GST_ELEMENT(pipeline)));
319     return FALSE;
320   }
321
322   factory = gst_element_get_factory(pipeline->src);
323
324   src_types = factory->src_types;
325   if (src_types == NULL) {
326     g_print("GstPipeline: source \"%s\" has no MIME type, running typefind...\n", 
327                 gst_element_get_name(pipeline->src));
328
329     src_type = gst_pipeline_typefind(pipeline, pipeline->src);
330
331     if (src_type) {
332       g_print("GstPipeline: source \"%s\" type found %d\n", gst_element_get_name(pipeline->src), 
333                   src_type);
334     }
335     else {
336       g_print("GstPipeline: source \"%s\" has no type\n", gst_element_get_name(pipeline->src));
337       return FALSE;
338     }
339   }
340   else {
341     while (src_types) {
342       // FIXME loop over types and find paths...
343       src_types = g_list_next(src_types);
344     }
345   }
346
347   srcelement = pipeline->src;
348
349   elements = pipeline->sinks;
350
351   // fase 2, loop over all the sinks.. 
352   while (elements) {
353     GList *pads;
354     GstPad *pad;
355
356     element = GST_ELEMENT(elements->data);
357
358     pads = gst_element_get_pad_list(element);
359
360     while (pads) {
361       pad = (GstPad *)pads->data;
362
363       if (pad->direction == GST_PAD_SINK) {
364         sink_type = gst_pad_get_type_id(pad);
365         sinkelement = element;
366         break;
367       }
368
369       pads = g_list_next(pads);
370     }
371     elements = g_list_next(elements);
372   }
373
374   factories = gst_type_get_sink_to_src(src_type, sink_type);
375
376   while (factories) {
377     // fase 3: find elements to form a pad
378        
379     factory = (GstElementFactory *)(factories->data);
380
381     g_print("GstPipeline: factory \"%s\"\n", factory->name);
382
383     element = gst_elementfactory_create(factory, factory->name);
384     gst_bin_add(GST_BIN(pipeline), element);
385
386     gst_pipeline_pads_autoplug(srcelement, element);
387
388     srcelement = element;
389
390     factories = g_list_next(factories);
391
392     complete = TRUE;
393   }
394
395   if (complete) {
396     gst_pipeline_pads_autoplug(srcelement, sinkelement);
397     return TRUE;
398   }
399   
400   g_print("GstPipeline: unable to autoplug pipeline \"%s\"\n", 
401                   gst_element_get_name(GST_ELEMENT(pipeline)));
402   return FALSE;
403 }
404
405 static GstElementStateReturn gst_pipeline_change_state(GstElement *element) {
406   GstPipeline *pipeline;
407
408   g_return_val_if_fail(GST_IS_PIPELINE(element), FALSE);
409   pipeline = GST_PIPELINE(element);
410
411
412   switch (GST_STATE_PENDING(pipeline)) {
413     case GST_STATE_READY:
414       // we need to set up internal state
415       gst_pipeline_prepare(pipeline);
416       break;
417     default:
418       break;
419   }
420     
421   if (GST_ELEMENT_CLASS(parent_class)->change_state)
422     return GST_ELEMENT_CLASS(parent_class)->change_state(element);
423   return GST_STATE_SUCCESS;
424 }
425
426
427 /**
428  * gst_pipeline_iterate:
429  * @pipeline: GstPipeline to iterate
430  *
431  * Cause the pipeline's contents to be run through one full 'iteration'.
432  */
433 void gst_pipeline_iterate(GstPipeline *pipeline) {
434   g_return_if_fail(pipeline != NULL);
435   g_return_if_fail(GST_IS_PIPELINE(pipeline));
436 }