2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
20 #include <gst/gstpipeline.h>
21 #include <gst/gstthread.h>
22 #include <gst/gstsink.h>
23 #include <gst/gstutils.h>
24 #include <gst/gsttype.h>
28 GstElementDetails gst_pipeline_details = {
31 "Complete pipeline object",
33 "Erik Walthinsen <omega@cse.ogi.edu>",
38 /* Pipeline signals and args */
50 static void gst_pipeline_class_init (GstPipelineClass *klass);
51 static void gst_pipeline_init (GstPipeline *pipeline);
53 static GstElementStateReturn gst_pipeline_change_state (GstElement *element);
55 static void gst_pipeline_prepare (GstPipeline *pipeline);
57 static void gst_pipeline_have_type (GstSink *sink, GstSink *sink2, gpointer data);
58 static void gst_pipeline_pads_autoplug (GstElement *src, GstElement *sink);
60 static GstBin *parent_class = NULL;
61 //static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 };
64 gst_pipeline_get_type (void) {
65 static GtkType pipeline_type = 0;
68 static const GtkTypeInfo pipeline_info = {
71 sizeof(GstPipelineClass),
72 (GtkClassInitFunc)gst_pipeline_class_init,
73 (GtkObjectInitFunc)gst_pipeline_init,
76 (GtkClassInitFunc)NULL,
78 pipeline_type = gtk_type_unique (gst_bin_get_type (), &pipeline_info);
84 gst_pipeline_class_init (GstPipelineClass *klass)
86 GstElementClass *gstelement_class;
88 gstelement_class = (GstElementClass*)klass;
90 parent_class = gtk_type_class(gst_bin_get_type());
92 gstelement_class->change_state = gst_pipeline_change_state;
93 gstelement_class->elementfactory = gst_elementfactory_find ("pipeline");
97 gst_pipeline_init (GstPipeline *pipeline)
100 pipeline->sinks = NULL;
106 * @name: name of new pipeline
108 * Create a new pipeline with the given name.
110 * Returns: newly created GstPipeline
113 gst_pipeline_new (guchar *name)
115 GstPipeline *pipeline;
117 pipeline = gtk_type_new (gst_pipeline_get_type ());
118 gst_element_set_name (GST_ELEMENT (pipeline), name);
120 return GST_ELEMENT (pipeline);
124 gst_pipeline_prepare (GstPipeline *pipeline)
126 g_print("GstPipeline: preparing pipeline \"%s\" for playing\n",
127 gst_element_get_name(GST_ELEMENT(pipeline)));
131 gst_pipeline_have_type (GstSink *sink, GstSink *sink2, gpointer data)
133 g_print("GstPipeline: pipeline have type %p\n", (gboolean *)data);
135 *(gboolean *)data = TRUE;
139 gst_pipeline_typefind (GstPipeline *pipeline, GstElement *element)
141 gboolean found = FALSE;
142 GstElement *typefind;
145 g_print("GstPipeline: typefind for element \"%s\" %p\n",
146 gst_element_get_name(element), &found);
148 typefind = gst_elementfactory_make ("typefind", "typefind");
149 g_return_val_if_fail (typefind != NULL, FALSE);
151 gtk_signal_connect (GTK_OBJECT (typefind), "have_type",
152 GTK_SIGNAL_FUNC (gst_pipeline_have_type), &found);
154 gst_pad_connect (gst_element_get_pad (element, "src"),
155 gst_element_get_pad (typefind, "sink"));
157 gst_bin_add (GST_BIN (pipeline), typefind);
159 gst_bin_create_plan (GST_BIN (pipeline));
160 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_READY);
161 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
163 // keep pushing buffers... the have_type signal handler will set the found flag
165 gst_bin_iterate (GST_BIN (pipeline));
168 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
171 type_id = gst_util_get_int_arg (GTK_OBJECT (typefind), "type");
172 //gst_pad_add_type_id (gst_element_get_pad (element, "src"), type_id);
175 gst_pad_disconnect (gst_element_get_pad (element, "src"),
176 gst_element_get_pad (typefind, "sink"));
177 gst_bin_remove (GST_BIN (pipeline), typefind);
178 gst_object_unref (GST_OBJECT (typefind));
184 gst_pipeline_pads_autoplug_func (GstElement *src, GstPad *pad, GstElement *sink)
187 gboolean connected = FALSE;
189 g_print("gstpipeline: autoplug pad connect function for \"%s\" to \"%s\"\n",
190 gst_element_get_name(src), gst_element_get_name(sink));
192 sinkpads = gst_element_get_pad_list(sink);
194 GstPad *sinkpad = (GstPad *)sinkpads->data;
196 // if we have a match, connect the pads
197 if (sinkpad->direction == GST_PAD_SINK &&
198 !GST_PAD_CONNECTED(sinkpad) &&
199 gst_caps_check_compatibility (pad->caps, sinkpad->caps))
201 gst_pad_connect(pad, sinkpad);
202 g_print("gstpipeline: autoconnect pad \"%s\" in element %s <-> ", pad->name,
203 gst_element_get_name(src));
204 g_print("pad \"%s\" in element %s\n", sinkpad->name,
205 gst_element_get_name(sink));
209 sinkpads = g_list_next(sinkpads);
213 g_print("gstpipeline: no path to sinks for type\n");
219 gst_pipeline_pads_autoplug (GstElement *src, GstElement *sink)
222 gboolean connected = FALSE;
224 srcpads = gst_element_get_pad_list(src);
226 while (srcpads && !connected) {
227 GstPad *srcpad = (GstPad *)srcpads->data;
229 connected = gst_pipeline_pads_autoplug_func (src, srcpad, sink);
231 srcpads = g_list_next(srcpads);
235 g_print("gstpipeline: delaying pad connections for \"%s\" to \"%s\"\n",
236 gst_element_get_name(src), gst_element_get_name(sink));
237 gtk_signal_connect(GTK_OBJECT(src),"new_pad",
238 GTK_SIGNAL_FUNC(gst_pipeline_pads_autoplug_func), sink);
243 * gst_pipeline_add_src:
244 * @pipeline: the pipeline to add the src to
245 * @src: the src to add to the pipeline
247 * Adds a src element to the pipeline. This element
248 * will be used as a src for autoplugging. If you add more
249 * than one src element, the previously added element will
253 gst_pipeline_add_src (GstPipeline *pipeline, GstElement *src)
255 g_return_if_fail (pipeline != NULL);
256 g_return_if_fail (GST_IS_PIPELINE (pipeline));
257 g_return_if_fail (src != NULL);
258 g_return_if_fail (GST_IS_ELEMENT (src));
261 printf("gstpipeline: *WARNING* removing previously added element \"%s\"\n",
262 gst_element_get_name(pipeline->src));
263 gst_bin_remove(GST_BIN(pipeline), pipeline->src);
266 gst_bin_add(GST_BIN(pipeline), src);
270 * gst_pipeline_add_sink:
271 * @pipeline: the pipeline to add the sink to
272 * @sink: the sink to add to the pipeline
274 * Adds a sink element to the pipeline. This element
275 * will be used as a sink for autoplugging
278 gst_pipeline_add_sink (GstPipeline *pipeline, GstElement *sink)
280 g_return_if_fail (pipeline != NULL);
281 g_return_if_fail (GST_IS_PIPELINE (pipeline));
282 g_return_if_fail (sink != NULL);
283 g_return_if_fail (GST_IS_ELEMENT (sink));
285 pipeline->sinks = g_list_prepend (pipeline->sinks, sink);
286 //gst_bin_add(GST_BIN(pipeline), sink);
290 * gst_pipeline_autoplug:
291 * @pipeline: the pipeline to autoplug
293 * Constructs a complete pipeline by automatically
294 * detecting the plugins needed.
296 * Returns: a gboolean indicating success or failure.
299 gst_pipeline_autoplug (GstPipeline *pipeline)
302 GstElement *element, *srcelement = NULL, *sinkelement= NULL;
304 GList **base_factories;
305 GstElementFactory *factory;
307 guint16 src_type = 0, sink_type = 0;
309 gboolean use_thread = FALSE, have_common = FALSE;
311 g_return_val_if_fail(pipeline != NULL, FALSE);
312 g_return_val_if_fail(GST_IS_PIPELINE(pipeline), FALSE);
314 g_print("GstPipeline: autopluging pipeline \"%s\"\n",
315 gst_element_get_name(GST_ELEMENT(pipeline)));
318 // fase 1, run typedetect on the source if needed...
319 if (!pipeline->src) {
320 g_print("GstPipeline: no source detected, can't autoplug pipeline \"%s\"\n",
321 gst_element_get_name(GST_ELEMENT(pipeline)));
325 // FIXME check the factory???
326 factory = gst_element_get_factory(pipeline->src);
328 //src_types = factory->pads;
329 src_types = NULL; // FIXME
330 if (src_types == NULL) {
331 g_print("GstPipeline: source \"%s\" has no MIME type, running typefind...\n",
332 gst_element_get_name(pipeline->src));
334 src_type = gst_pipeline_typefind(pipeline, pipeline->src);
337 g_print("GstPipeline: source \"%s\" type found %d\n", gst_element_get_name(pipeline->src),
341 g_print("GstPipeline: source \"%s\" has no type\n", gst_element_get_name(pipeline->src));
347 // FIXME loop over types and find paths...
348 src_types = g_list_next(src_types);
352 srcelement = pipeline->src;
354 elements = pipeline->sinks;
356 numsinks = g_list_length(elements);
357 factories = g_new0(GList *, numsinks);
358 base_factories = g_new0(GList *, numsinks);
361 // fase 2, loop over all the sinks..
366 element = GST_ELEMENT(elements->data);
368 pads = gst_element_get_pad_list(element);
371 pad = (GstPad *)pads->data;
373 if (pad->direction == GST_PAD_SINK) {
375 GList *types = gst_pad_get_type_ids(pad);
377 sink_type = GPOINTER_TO_INT (types->data);
385 g_print ("type %d\n", sink_type);
387 pads = g_list_next(pads);
390 base_factories[i] = factories[i] = gst_type_get_sink_to_src(src_type, sink_type);
393 elements = g_list_next(elements);
396 while (factories[0]) {
397 // fase 3: add common elements
398 factory = (GstElementFactory *)(factories[0]->data);
400 // check to other paths for mathing elements (factories)
401 for (i=1; i<numsinks; i++) {
402 if (factory != (GstElementFactory *)(factories[i]->data)) {
405 factories[i] = g_list_next(factories[i]);
407 factory = (GstElementFactory *)(factories[0]->data);
409 g_print("GstPipeline: common factory \"%s\"\n", factory->name);
411 element = gst_elementfactory_create(factory, factory->name);
412 gst_bin_add(GST_BIN(pipeline), element);
414 gst_pipeline_pads_autoplug(srcelement, element);
416 srcelement = element;
418 factories[0] = g_list_next(factories[0]);
424 // loop over all the sink elements
425 elements = pipeline->sinks;
429 GstElement *thesrcelement = srcelement;
430 GstElement *thebin = GST_ELEMENT(pipeline);
432 if (g_list_length(base_factories[i]) == 0) goto next;
434 sinkelement = (GstElement *)elements->data;
436 use_thread = have_common;
438 while (factories[i] || sinkelement) {
439 // fase 4: add other elements...
442 factory = (GstElementFactory *)(factories[i]->data);
443 g_print("GstPipeline: factory \"%s\"\n", factory->name);
444 element = gst_elementfactory_create(factory, factory->name);
445 factories[i] = g_list_next(factories[i]);
447 // we have arived to the final sink element
449 element = sinkelement;
453 // this element suggests the use of a thread, so we set one up...
454 if (GST_ELEMENT_IS_THREAD_SUGGESTED(element) || use_thread) {
457 GstPad *srcpad, *sinkpad;
461 g_print("GstPipeline: sugest new thread for \"%s\" %08x\n", element->name, GST_FLAGS(element));
463 // create a new queue and add to the previous bin
464 queue = gst_elementfactory_make("queue", g_strconcat("queue_", gst_element_get_name(element), NULL));
465 gst_bin_add(GST_BIN(thebin), queue);
467 // this will be the new bin for all following elements
468 thebin = gst_elementfactory_make("thread", g_strconcat("thread_", gst_element_get_name(element), NULL));
470 srcpad = gst_element_get_pad(queue, "src");
472 sinkpads = gst_element_get_pad_list(element);
474 sinkpad = (GstPad *)sinkpads->data;
477 // FIXME connect matching pads, not just the first one...
478 if (sinkpad->direction == GST_PAD_SINK &&
479 !GST_PAD_CONNECTED(sinkpad)) {
480 guint16 sinktype = 0;
481 GList *types = gst_pad_get_type_ids(sinkpad);
483 sinktype = GPOINTER_TO_INT (types->data);
484 // the queue has the type of the elements it connects
485 gst_pad_set_type_id (srcpad, sinktype);
486 gst_pad_set_type_id (gst_element_get_pad(queue, "sink"), sinktype);
490 sinkpads = g_list_next(sinkpads);
492 gst_pipeline_pads_autoplug(thesrcelement, queue);
494 gst_bin_add(GST_BIN(thebin), element);
495 gst_bin_add(GST_BIN(pipeline), thebin);
496 thesrcelement = queue;
498 // no thread needed, easy case
500 gst_bin_add(GST_BIN(thebin), element);
502 gst_pipeline_pads_autoplug(thesrcelement, element);
504 // this element is now the new source element
505 thesrcelement = element;
508 elements = g_list_next(elements);
513 g_print("GstPipeline: unable to autoplug pipeline \"%s\"\n",
514 gst_element_get_name(GST_ELEMENT(pipeline)));
518 static GstElementStateReturn
519 gst_pipeline_change_state (GstElement *element)
521 GstPipeline *pipeline;
523 g_return_val_if_fail (GST_IS_PIPELINE (element), FALSE);
525 pipeline = GST_PIPELINE (element);
527 switch (GST_STATE_PENDING (pipeline)) {
528 case GST_STATE_READY:
529 // we need to set up internal state
530 gst_pipeline_prepare (pipeline);
536 if (GST_ELEMENT_CLASS (parent_class)->change_state)
537 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
539 return GST_STATE_SUCCESS;
544 * gst_pipeline_iterate:
545 * @pipeline: GstPipeline to iterate
547 * Cause the pipeline's contents to be run through one full 'iteration'.
550 gst_pipeline_iterate (GstPipeline *pipeline)
552 g_return_if_fail (pipeline != NULL);
553 g_return_if_fail (GST_IS_PIPELINE(pipeline));