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"
25 #include "gstautoplug.h"
27 GstElementDetails gst_pipeline_details = {
30 "Complete pipeline object",
32 "Erik Walthinsen <omega@cse.ogi.edu>",
37 /* Pipeline signals and args */
49 static void gst_pipeline_class_init (GstPipelineClass *klass);
50 static void gst_pipeline_init (GstPipeline *pipeline);
52 static GstElementStateReturn gst_pipeline_change_state (GstElement *element);
54 static void gst_pipeline_prepare (GstPipeline *pipeline);
56 static void gst_pipeline_have_type (GstSink *sink, GstSink *sink2, gpointer data);
57 static void gst_pipeline_pads_autoplug (GstElement *src, GstElement *sink);
59 static GstBinClass *parent_class = NULL;
60 //static guint gst_pipeline_signals[LAST_SIGNAL] = { 0 };
63 gst_pipeline_get_type (void) {
64 static GtkType pipeline_type = 0;
67 static const GtkTypeInfo pipeline_info = {
70 sizeof(GstPipelineClass),
71 (GtkClassInitFunc)gst_pipeline_class_init,
72 (GtkObjectInitFunc)gst_pipeline_init,
75 (GtkClassInitFunc)NULL,
77 pipeline_type = gtk_type_unique (gst_bin_get_type (), &pipeline_info);
83 gst_pipeline_class_init (GstPipelineClass *klass)
85 GstElementClass *gstelement_class;
87 gstelement_class = (GstElementClass*)klass;
89 parent_class = gtk_type_class(gst_bin_get_type());
91 gstelement_class->change_state = gst_pipeline_change_state;
95 gst_pipeline_init (GstPipeline *pipeline)
97 // we're a manager by default
98 GST_FLAG_SET (pipeline, GST_BIN_FLAG_MANAGER);
100 pipeline->src = NULL;
101 pipeline->sinks = NULL;
107 * @name: name of new pipeline
109 * Create a new pipeline with the given name.
111 * Returns: newly created GstPipeline
114 gst_pipeline_new (guchar *name)
116 return gst_elementfactory_make ("pipeline", name);
120 gst_pipeline_prepare (GstPipeline *pipeline)
122 g_print("GstPipeline: preparing pipeline \"%s\" for playing\n",
123 gst_element_get_name(GST_ELEMENT(pipeline)));
127 gst_pipeline_have_type (GstSink *sink, GstSink *sink2, gpointer data)
129 g_print("GstPipeline: pipeline have type %p\n", (gboolean *)data);
131 *(gboolean *)data = TRUE;
135 gst_pipeline_typefind (GstPipeline *pipeline, GstElement *element)
137 gboolean found = FALSE;
138 GstElement *typefind;
139 GstCaps *caps = NULL;
141 g_print("GstPipeline: typefind for element \"%s\" %p\n",
142 gst_element_get_name(element), &found);
144 typefind = gst_elementfactory_make ("typefind", "typefind");
145 g_return_val_if_fail (typefind != NULL, FALSE);
147 gtk_signal_connect (GTK_OBJECT (typefind), "have_type",
148 GTK_SIGNAL_FUNC (gst_pipeline_have_type), &found);
150 gst_pad_connect (gst_element_get_pad (element, "src"),
151 gst_element_get_pad (typefind, "sink"));
153 gst_bin_add (GST_BIN (pipeline), typefind);
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);
159 // keep pushing buffers... the have_type signal handler will set the found flag
161 gst_bin_iterate (GST_BIN (pipeline));
164 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
167 caps = gst_util_get_pointer_arg (GTK_OBJECT (typefind), "caps");
169 gst_pad_set_caps (gst_element_get_pad (element, "src"), caps);
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));
181 gst_pipeline_pads_autoplug_func (GstElement *src, GstPad *pad, GstElement *sink)
184 gboolean connected = FALSE;
186 g_print("gstpipeline: autoplug pad connect function for \"%s\" to \"%s\"\n",
187 gst_element_get_name(src), gst_element_get_name(sink));
189 sinkpads = gst_element_get_pad_list(sink);
191 GstPad *sinkpad = (GstPad *)sinkpads->data;
193 // if we have a match, connect the pads
194 if (sinkpad->direction == GST_PAD_SINK &&
195 !GST_PAD_CONNECTED(sinkpad))
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));
207 sinkpads = g_list_next(sinkpads);
211 g_print("gstpipeline: no path to sinks for type\n");
217 gst_pipeline_pads_autoplug (GstElement *src, GstElement *sink)
220 gboolean connected = FALSE;
222 srcpads = gst_element_get_pad_list(src);
224 while (srcpads && !connected) {
225 GstPad *srcpad = (GstPad *)srcpads->data;
227 if (srcpad->direction == GST_PAD_SRC)
228 connected = gst_pipeline_pads_autoplug_func (src, srcpad, sink);
230 srcpads = g_list_next(srcpads);
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);
242 * gst_pipeline_add_src:
243 * @pipeline: the pipeline to add the src to
244 * @src: the src to add to the pipeline
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
252 gst_pipeline_add_src (GstPipeline *pipeline, GstElement *src)
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));
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);
265 gst_bin_add(GST_BIN(pipeline), src);
269 * gst_pipeline_add_sink:
270 * @pipeline: the pipeline to add the sink to
271 * @sink: the sink to add to the pipeline
273 * Adds a sink element to the pipeline. This element
274 * will be used as a sink for autoplugging
277 gst_pipeline_add_sink (GstPipeline *pipeline, GstElement *sink)
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));
284 pipeline->sinks = g_list_prepend (pipeline->sinks, sink);
285 //gst_bin_add(GST_BIN(pipeline), sink);
289 * gst_pipeline_autoplug:
290 * @pipeline: the pipeline to autoplug
292 * Constructs a complete pipeline by automatically
293 * detecting the plugins needed.
295 * Returns: a gboolean indicating success or failure.
298 gst_pipeline_autoplug (GstPipeline *pipeline)
301 GstElement *element, *srcelement = NULL, *sinkelement= NULL;
303 GList **base_factories;
304 GstElementFactory *factory;
306 GstCaps *src_caps = 0;
308 gboolean use_thread = FALSE, have_common = FALSE;
310 g_return_val_if_fail(pipeline != NULL, FALSE);
311 g_return_val_if_fail(GST_IS_PIPELINE(pipeline), FALSE);
313 g_print("GstPipeline: autopluging pipeline \"%s\"\n",
314 gst_element_get_name(GST_ELEMENT(pipeline)));
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)));
324 g_print("GstPipeline: source \"%s\" has no MIME type, running typefind...\n",
325 gst_element_get_name(pipeline->src));
327 src_caps = gst_pipeline_typefind(pipeline, pipeline->src);
330 g_print("GstPipeline: source \"%s\" type found %d\n", gst_element_get_name(pipeline->src),
334 g_print("GstPipeline: source \"%s\" has no type\n", gst_element_get_name(pipeline->src));
338 srcelement = pipeline->src;
340 elements = pipeline->sinks;
342 numsinks = g_list_length(elements);
343 factories = g_new0(GList *, numsinks);
344 base_factories = g_new0(GList *, numsinks);
347 // fase 2, loop over all the sinks..
352 element = GST_ELEMENT(elements->data);
354 pad = (GstPad *)gst_element_get_pad_list (element)->data;
356 base_factories[i] = factories[i] = gst_autoplug_caps (src_caps, pad->caps);
359 elements = g_list_next(elements);
362 while (factories[0]) {
363 // fase 3: add common elements
364 factory = (GstElementFactory *)(factories[0]->data);
366 // check to other paths for mathing elements (factories)
367 for (i=1; i<numsinks; i++) {
368 if (factory != (GstElementFactory *)(factories[i]->data)) {
371 factories[i] = g_list_next(factories[i]);
373 factory = (GstElementFactory *)(factories[0]->data);
375 g_print("GstPipeline: common factory \"%s\"\n", factory->name);
377 element = gst_elementfactory_create(factory, factory->name);
378 gst_bin_add(GST_BIN(pipeline), element);
380 gst_pipeline_pads_autoplug(srcelement, element);
382 srcelement = element;
384 factories[0] = g_list_next(factories[0]);
390 // loop over all the sink elements
391 elements = pipeline->sinks;
395 GstElement *thesrcelement = srcelement;
396 GstElement *thebin = GST_ELEMENT(pipeline);
398 if (g_list_length(base_factories[i]) == 0) goto next;
400 sinkelement = (GstElement *)elements->data;
402 use_thread = have_common;
404 while (factories[i] || sinkelement) {
405 // fase 4: add other elements...
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]);
413 // we have arived to the final sink element
415 element = sinkelement;
419 // this element suggests the use of a thread, so we set one up...
420 if (GST_ELEMENT_IS_THREAD_SUGGESTED(element) || use_thread) {
423 GstPad *srcpad, *sinkpad;
427 g_print("GstPipeline: sugest new thread for \"%s\" %08x\n", element->name, GST_FLAGS(element));
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);
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));
436 srcpad = gst_element_get_pad(queue, "src");
438 sinkpads = gst_element_get_pad_list(element);
440 sinkpad = (GstPad *)sinkpads->data;
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);
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);
452 sinkpads = g_list_next(sinkpads);
454 gst_pipeline_pads_autoplug(thesrcelement, queue);
456 gst_bin_add(GST_BIN(thebin), element);
457 gst_bin_add(GST_BIN(pipeline), thebin);
458 thesrcelement = queue;
460 // no thread needed, easy case
462 gst_bin_add(GST_BIN(thebin), element);
464 gst_pipeline_pads_autoplug(thesrcelement, element);
466 // this element is now the new source element
467 thesrcelement = element;
470 elements = g_list_next(elements);
475 g_print("GstPipeline: unable to autoplug pipeline \"%s\"\n",
476 gst_element_get_name(GST_ELEMENT(pipeline)));
480 static GstElementStateReturn
481 gst_pipeline_change_state (GstElement *element)
483 GstPipeline *pipeline;
485 g_return_val_if_fail (GST_IS_PIPELINE (element), FALSE);
487 pipeline = GST_PIPELINE (element);
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);
498 if (GST_ELEMENT_CLASS (parent_class)->change_state)
499 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
501 return GST_STATE_SUCCESS;
506 * gst_pipeline_iterate:
507 * @pipeline: GstPipeline to iterate
509 * Cause the pipeline's contents to be run through one full 'iteration'.
512 gst_pipeline_iterate (GstPipeline *pipeline)
514 g_return_if_fail (pipeline != NULL);
515 g_return_if_fail (GST_IS_PIPELINE(pipeline));