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 "gstpipeline.h"
21 #include "gstthread.h"
26 GstElementDetails gst_pipeline_details = {
29 "Complete pipeline object",
31 "Erik Walthinsen <omega@cse.ogi.edu>",
36 /* Pipeline signals and args */
48 static void gst_pipeline_class_init (GstPipelineClass *klass);
49 static void gst_pipeline_init (GstPipeline *pipeline);
51 static GstElementStateReturn gst_pipeline_change_state (GstElement *element);
53 static void gst_pipeline_prepare (GstPipeline *pipeline);
55 static void gst_pipeline_have_type (GstSink *sink, GstSink *sink2, gpointer data);
56 static void gst_pipeline_pads_autoplug (GstElement *src, GstElement *sink);
58 static GstBinClass *parent_class = NULL;
59 //static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 };
62 gst_pipeline_get_type (void) {
63 static GtkType pipeline_type = 0;
66 static const GtkTypeInfo pipeline_info = {
69 sizeof(GstPipelineClass),
70 (GtkClassInitFunc)gst_pipeline_class_init,
71 (GtkObjectInitFunc)gst_pipeline_init,
74 (GtkClassInitFunc)NULL,
76 pipeline_type = gtk_type_unique (gst_bin_get_type (), &pipeline_info);
82 gst_pipeline_class_init (GstPipelineClass *klass)
84 GstElementClass *gstelement_class;
86 gstelement_class = (GstElementClass*)klass;
88 parent_class = gtk_type_class(gst_bin_get_type());
90 gstelement_class->change_state = gst_pipeline_change_state;
91 gstelement_class->elementfactory = gst_elementfactory_find ("pipeline");
95 gst_pipeline_init (GstPipeline *pipeline)
98 pipeline->sinks = NULL;
104 * @name: name of new pipeline
106 * Create a new pipeline with the given name.
108 * Returns: newly created GstPipeline
111 gst_pipeline_new (guchar *name)
113 GstPipeline *pipeline;
115 pipeline = gtk_type_new (gst_pipeline_get_type ());
116 gst_element_set_name (GST_ELEMENT (pipeline), name);
118 return GST_ELEMENT (pipeline);
122 gst_pipeline_prepare (GstPipeline *pipeline)
124 g_print("GstPipeline: preparing pipeline \"%s\" for playing\n",
125 gst_element_get_name(GST_ELEMENT(pipeline)));
129 gst_pipeline_have_type (GstSink *sink, GstSink *sink2, gpointer data)
131 g_print("GstPipeline: pipeline have type %p\n", (gboolean *)data);
133 *(gboolean *)data = TRUE;
137 gst_pipeline_typefind (GstPipeline *pipeline, GstElement *element)
139 gboolean found = FALSE;
140 GstElement *typefind;
141 GstCaps *caps = NULL;
144 g_print("GstPipeline: typefind for element \"%s\" %p\n",
145 gst_element_get_name(element), &found);
147 typefind = gst_elementfactory_make ("typefind", "typefind");
148 g_return_val_if_fail (typefind != NULL, FALSE);
150 gtk_signal_connect (GTK_OBJECT (typefind), "have_type",
151 GTK_SIGNAL_FUNC (gst_pipeline_have_type), &found);
153 gst_pad_connect (gst_element_get_pad (element, "src"),
154 gst_element_get_pad (typefind, "sink"));
156 gst_bin_add (GST_BIN (pipeline), typefind);
158 gst_bin_create_plan (GST_BIN (pipeline));
159 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
161 // keep pushing buffers... the have_type signal handler will set the found flag
163 gst_bin_iterate (GST_BIN (pipeline));
166 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
169 caps = gst_util_get_pointer_arg (GTK_OBJECT (typefind), "caps");
171 gst_pad_set_caps (gst_element_get_pad (element, "src"), caps);
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));
185 gst_pipeline_pads_autoplug_func (GstElement *src, GstPad *pad, GstElement *sink)
188 gboolean connected = FALSE;
190 g_print("gstpipeline: autoplug pad connect function for \"%s\" to \"%s\"\n",
191 gst_element_get_name(src), gst_element_get_name(sink));
193 sinkpads = gst_element_get_pad_list(sink);
195 GstPad *sinkpad = (GstPad *)sinkpads->data;
197 // if we have a match, connect the pads
198 if (sinkpad->direction == GST_PAD_SINK &&
199 !GST_PAD_CONNECTED(sinkpad))
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));
212 g_print("gstpipeline: not compatible, find intermediate element\n");
214 factories = gst_type_get_sink_to_src (pad->caps->id, sinkpad->caps->id);
217 GstElementFactory *factory = (GstElementFactory *)factories->data;
218 GstElement *element = gst_elementfactory_create (factory, factory->name);
220 g_print ("gstpipeline: trying element \"%s\"\n", element->name);
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);
228 factories = g_list_next (factories);
232 sinkpads = g_list_next(sinkpads);
236 g_print("gstpipeline: no path to sinks for type\n");
242 gst_pipeline_pads_autoplug (GstElement *src, GstElement *sink)
245 gboolean connected = FALSE;
247 srcpads = gst_element_get_pad_list(src);
249 while (srcpads && !connected) {
250 GstPad *srcpad = (GstPad *)srcpads->data;
252 if (srcpad->direction == GST_PAD_SRC)
253 connected = gst_pipeline_pads_autoplug_func (src, srcpad, sink);
255 srcpads = g_list_next(srcpads);
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);
267 * gst_pipeline_add_src:
268 * @pipeline: the pipeline to add the src to
269 * @src: the src to add to the pipeline
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
277 gst_pipeline_add_src (GstPipeline *pipeline, GstElement *src)
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));
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);
290 gst_bin_add(GST_BIN(pipeline), src);
294 * gst_pipeline_add_sink:
295 * @pipeline: the pipeline to add the sink to
296 * @sink: the sink to add to the pipeline
298 * Adds a sink element to the pipeline. This element
299 * will be used as a sink for autoplugging
302 gst_pipeline_add_sink (GstPipeline *pipeline, GstElement *sink)
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));
309 pipeline->sinks = g_list_prepend (pipeline->sinks, sink);
310 //gst_bin_add(GST_BIN(pipeline), sink);
314 * gst_pipeline_autoplug:
315 * @pipeline: the pipeline to autoplug
317 * Constructs a complete pipeline by automatically
318 * detecting the plugins needed.
320 * Returns: a gboolean indicating success or failure.
323 gst_pipeline_autoplug (GstPipeline *pipeline)
326 GstElement *element, *srcelement = NULL, *sinkelement= NULL;
328 GList **base_factories;
329 GstElementFactory *factory;
331 guint16 src_type = 0, sink_type = 0;
333 gboolean use_thread = FALSE, have_common = FALSE;
335 g_return_val_if_fail(pipeline != NULL, FALSE);
336 g_return_val_if_fail(GST_IS_PIPELINE(pipeline), FALSE);
338 g_print("GstPipeline: autopluging pipeline \"%s\"\n",
339 gst_element_get_name(GST_ELEMENT(pipeline)));
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)));
349 // FIXME check the factory???
350 factory = gst_element_get_factory(pipeline->src);
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));
358 src_type = gst_pipeline_typefind(pipeline, pipeline->src);
361 g_print("GstPipeline: source \"%s\" type found %d\n", gst_element_get_name(pipeline->src),
365 g_print("GstPipeline: source \"%s\" has no type\n", gst_element_get_name(pipeline->src));
371 // FIXME loop over types and find paths...
372 src_types = g_list_next(src_types);
376 srcelement = pipeline->src;
378 elements = pipeline->sinks;
380 numsinks = g_list_length(elements);
381 factories = g_new0(GList *, numsinks);
382 base_factories = g_new0(GList *, numsinks);
385 // fase 2, loop over all the sinks..
390 element = GST_ELEMENT(elements->data);
392 pads = gst_element_get_pad_list(element);
395 pad = (GstPad *)pads->data;
397 if (pad->direction == GST_PAD_SINK) {
398 GstCaps *caps = gst_pad_get_caps (pad);
400 sink_type = caps->id;
407 pads = g_list_next(pads);
410 base_factories[i] = factories[i] = gst_type_get_sink_to_src(src_type, sink_type);
413 elements = g_list_next(elements);
416 while (factories[0]) {
417 // fase 3: add common elements
418 factory = (GstElementFactory *)(factories[0]->data);
420 // check to other paths for mathing elements (factories)
421 for (i=1; i<numsinks; i++) {
422 if (factory != (GstElementFactory *)(factories[i]->data)) {
425 factories[i] = g_list_next(factories[i]);
427 factory = (GstElementFactory *)(factories[0]->data);
429 g_print("GstPipeline: common factory \"%s\"\n", factory->name);
431 element = gst_elementfactory_create(factory, factory->name);
432 gst_bin_add(GST_BIN(pipeline), element);
434 gst_pipeline_pads_autoplug(srcelement, element);
436 srcelement = element;
438 factories[0] = g_list_next(factories[0]);
444 // loop over all the sink elements
445 elements = pipeline->sinks;
449 GstElement *thesrcelement = srcelement;
450 GstElement *thebin = GST_ELEMENT(pipeline);
452 if (g_list_length(base_factories[i]) == 0) goto next;
454 sinkelement = (GstElement *)elements->data;
456 use_thread = have_common;
458 while (factories[i] || sinkelement) {
459 // fase 4: add other elements...
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]);
467 // we have arived to the final sink element
469 element = sinkelement;
473 // this element suggests the use of a thread, so we set one up...
474 if (GST_ELEMENT_IS_THREAD_SUGGESTED(element) || use_thread) {
477 GstPad *srcpad, *sinkpad;
481 g_print("GstPipeline: sugest new thread for \"%s\" %08x\n", element->name, GST_FLAGS(element));
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);
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));
490 srcpad = gst_element_get_pad(queue, "src");
492 sinkpads = gst_element_get_pad_list(element);
494 sinkpad = (GstPad *)sinkpads->data;
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);
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);
506 sinkpads = g_list_next(sinkpads);
508 gst_pipeline_pads_autoplug(thesrcelement, queue);
510 gst_bin_add(GST_BIN(thebin), element);
511 gst_bin_add(GST_BIN(pipeline), thebin);
512 thesrcelement = queue;
514 // no thread needed, easy case
516 gst_bin_add(GST_BIN(thebin), element);
518 gst_pipeline_pads_autoplug(thesrcelement, element);
520 // this element is now the new source element
521 thesrcelement = element;
524 elements = g_list_next(elements);
529 g_print("GstPipeline: unable to autoplug pipeline \"%s\"\n",
530 gst_element_get_name(GST_ELEMENT(pipeline)));
534 static GstElementStateReturn
535 gst_pipeline_change_state (GstElement *element)
537 GstPipeline *pipeline;
539 g_return_val_if_fail (GST_IS_PIPELINE (element), FALSE);
541 pipeline = GST_PIPELINE (element);
543 switch (GST_STATE_PENDING (pipeline)) {
544 case GST_STATE_READY:
545 // we need to set up internal state
546 gst_pipeline_prepare (pipeline);
552 if (GST_ELEMENT_CLASS (parent_class)->change_state)
553 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
555 return GST_STATE_SUCCESS;
560 * gst_pipeline_iterate:
561 * @pipeline: GstPipeline to iterate
563 * Cause the pipeline's contents to be run through one full 'iteration'.
566 gst_pipeline_iterate (GstPipeline *pipeline)
568 g_return_if_fail (pipeline != NULL);
569 g_return_if_fail (GST_IS_PIPELINE(pipeline));