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) {
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;
92 gstelement_class->elementfactory = gst_elementfactory_find("pipeline");
95 static void gst_pipeline_init(GstPipeline *pipeline) {
97 pipeline->sinks = NULL;
103 * @name: name of new pipeline
105 * Create a new pipeline with the given name.
107 * Returns: newly created GstPipeline
109 GstElement *gst_pipeline_new(guchar *name) {
110 GstPipeline *pipeline;
112 pipeline = gtk_type_new(gst_pipeline_get_type());
113 gst_element_set_name(GST_ELEMENT(pipeline),name);
114 return GST_ELEMENT(pipeline);
117 static void gst_pipeline_prepare(GstPipeline *pipeline) {
118 g_print("GstPipeline: preparing pipeline \"%s\" for playing\n",
119 gst_element_get_name(GST_ELEMENT(pipeline)));
122 static void gst_pipeline_have_type(GstSink *sink, GstSink *sink2, gpointer data) {
123 g_print("GstPipeline: pipeline have type %p\n", (gboolean *)data);
125 *(gboolean *)data = TRUE;
128 static guint16 gst_pipeline_typefind(GstPipeline *pipeline, GstElement *element) {
129 gboolean found = FALSE;
130 GstElement *typefind;
133 g_print("GstPipeline: typefind for element \"%s\" %p\n",
134 gst_element_get_name(element), &found);
136 typefind = gst_elementfactory_make("typefind","typefind");
137 g_return_val_if_fail(typefind != NULL, FALSE);
139 gtk_signal_connect(GTK_OBJECT(typefind),"have_type",
140 GTK_SIGNAL_FUNC(gst_pipeline_have_type), &found);
142 gst_pad_connect(gst_element_get_pad(element,"src"),
143 gst_element_get_pad(typefind,"sink"));
145 gst_bin_add(GST_BIN(pipeline), typefind);
147 gst_bin_create_plan(GST_BIN(pipeline));
148 gst_element_set_state(GST_ELEMENT(element),GST_STATE_READY);
149 gst_element_set_state(GST_ELEMENT(element),GST_STATE_PLAYING);
151 // keep pushing buffers... the have_type signal handler will set the found flag
153 gst_src_push(GST_SRC(element));
156 gst_element_set_state(GST_ELEMENT(element),GST_STATE_NULL);
159 type_id = gst_util_get_int_arg(GTK_OBJECT(typefind),"type");
160 gst_pad_set_type_id(gst_element_get_pad(element, "src"), type_id);
163 gst_pad_disconnect(gst_element_get_pad(element,"src"),
164 gst_element_get_pad(typefind,"sink"));
165 gst_bin_remove(GST_BIN(pipeline), typefind);
166 gst_object_unref(GST_OBJECT(typefind));
171 static void gst_pipeline_pads_autoplug_func(GstElement *src, GstPad *pad, GstElement *sink) {
174 gboolean connected = FALSE;
176 g_print("gstpipeline: autoplug pad connect function type %d for \"%s\" to \"%s\"\n", pad->type,
177 gst_element_get_name(src), gst_element_get_name(sink));
179 sinkpads = gst_element_get_pad_list(sink);
181 sinkpad = (GstPad *)sinkpads->data;
183 // if we have a match, connect the pads
184 if (sinkpad->type == pad->type &&
185 sinkpad->direction == GST_PAD_SINK &&
186 !GST_PAD_CONNECTED(sinkpad))
188 gst_pad_connect(pad, sinkpad);
189 g_print("gstpipeline: autoconnect pad \"%s\" (%d) in element %s <-> ", pad->name,
190 pad->type, gst_element_get_name(src));
191 g_print("pad \"%s\" (%d) in element %s\n", sinkpad->name, sinkpad->type,
192 gst_element_get_name(sink));
196 sinkpads = g_list_next(sinkpads);
200 g_print("gstpipeline: no path to sinks for type %d\n", pad->type);
204 static void gst_pipeline_pads_autoplug(GstElement *src, GstElement *sink) {
205 GList *srcpads, *sinkpads;
206 gboolean connected = FALSE;
208 srcpads = gst_element_get_pad_list(src);
211 GstPad *srcpad = (GstPad *)srcpads->data;
214 if (srcpad->direction == GST_PAD_SRC && !GST_PAD_CONNECTED(srcpad)) {
216 sinkpads = gst_element_get_pad_list(sink);
217 // FIXME could O(n) if the types were sorted...
219 sinkpad = (GstPad *)sinkpads->data;
221 // if we have a match, connect the pads
222 if (sinkpad->type == srcpad->type &&
223 sinkpad->direction == GST_PAD_SINK &&
224 !GST_PAD_CONNECTED(sinkpad)) {
225 gst_pad_connect(srcpad, sinkpad);
226 g_print("gstpipeline: autoconnect pad \"%s\" (%d) in element %s <-> ",
227 srcpad->name, srcpad->type, gst_element_get_name(src));
228 g_print("pad \"%s\" (%d) in element %s\n", sinkpad->name,
229 sinkpad->type, gst_element_get_name(sink));
233 sinkpads = g_list_next(sinkpads);
236 srcpads = g_list_next(srcpads);
241 g_print("gstpipeline: delaying pad connections for \"%s\" to \"%s\"\n",
242 gst_element_get_name(src), gst_element_get_name(sink));
243 gtk_signal_connect(GTK_OBJECT(src),"new_pad",
244 GTK_SIGNAL_FUNC(gst_pipeline_pads_autoplug_func), sink);
249 * gst_pipeline_add_src:
250 * @pipeline: the pipeline to add the src to
251 * @src: the src to add to the pipeline
253 * Adds a src element to the pipeline. This element
254 * will be used as a src for autoplugging. If you add more
255 * than one src element, the previously added element will
258 void gst_pipeline_add_src(GstPipeline *pipeline, GstElement *src)
260 g_return_if_fail(pipeline != NULL);
261 g_return_if_fail(GST_IS_PIPELINE(pipeline));
262 g_return_if_fail(src != NULL);
263 g_return_if_fail(GST_IS_ELEMENT(src));
266 printf("gstpipeline: *WARNING* removing previously added element \"%s\"\n",
267 gst_element_get_name(pipeline->src));
268 gst_bin_remove(GST_BIN(pipeline), pipeline->src);
271 gst_bin_add(GST_BIN(pipeline), src);
275 * gst_pipeline_add_sink:
276 * @pipeline: the pipeline to add the sink to
277 * @sink: the sink to add to the pipeline
279 * Adds a sink element to the pipeline. This element
280 * will be used as a sink for autoplugging
282 void gst_pipeline_add_sink(GstPipeline *pipeline, GstElement *sink)
284 g_return_if_fail(pipeline != NULL);
285 g_return_if_fail(GST_IS_PIPELINE(pipeline));
286 g_return_if_fail(sink != NULL);
287 g_return_if_fail(GST_IS_ELEMENT(sink));
289 pipeline->sinks = g_list_prepend(pipeline->sinks, sink);
290 //gst_bin_add(GST_BIN(pipeline), sink);
294 * gst_pipeline_autoplug:
295 * @pipeline: the pipeline to autoplug
297 * Constructs a complete pipeline by automatically
298 * detecting the plugins needed.
300 * Returns: a gboolean indicating success or failure.
302 gboolean gst_pipeline_autoplug(GstPipeline *pipeline) {
304 GstElement *element, *srcelement = NULL, *sinkelement= NULL;
306 GList **base_factories;
307 GstElementFactory *factory;
309 guint16 src_type = 0, sink_type = 0;
311 gboolean use_thread = FALSE, have_common = FALSE;
313 g_return_val_if_fail(pipeline != NULL, FALSE);
314 g_return_val_if_fail(GST_IS_PIPELINE(pipeline), FALSE);
316 g_print("GstPipeline: autopluging pipeline \"%s\"\n",
317 gst_element_get_name(GST_ELEMENT(pipeline)));
320 // fase 1, run typedetect on the source if needed...
321 if (!pipeline->src) {
322 g_print("GstPipeline: no source detected, can't autoplug pipeline \"%s\"\n",
323 gst_element_get_name(GST_ELEMENT(pipeline)));
327 factory = gst_element_get_factory(pipeline->src);
329 src_types = factory->src_types;
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) {
374 sink_type = gst_pad_get_type_id(pad);
378 pads = g_list_next(pads);
381 base_factories[i] = factories[i] = gst_type_get_sink_to_src(src_type, sink_type);
384 elements = g_list_next(elements);
387 while (factories[0]) {
388 // fase 3: add common elements
389 factory = (GstElementFactory *)(factories[0]->data);
391 // check to other paths for mathing elements (factories)
392 for (i=1; i<numsinks; i++) {
393 if (factory != (GstElementFactory *)(factories[i]->data)) {
396 factories[i] = g_list_next(factories[i]);
398 factory = (GstElementFactory *)(factories[0]->data);
400 g_print("GstPipeline: common factory \"%s\"\n", factory->name);
402 element = gst_elementfactory_create(factory, factory->name);
403 gst_bin_add(GST_BIN(pipeline), element);
405 gst_pipeline_pads_autoplug(srcelement, element);
407 srcelement = element;
409 factories[0] = g_list_next(factories[0]);
415 // loop over all the sink elements
416 elements = pipeline->sinks;
420 GstElement *thesrcelement = srcelement;
421 GstElement *thebin = GST_ELEMENT(pipeline);
423 if (g_list_length(base_factories[i]) == 0) goto next;
425 sinkelement = (GstElement *)elements->data;
427 use_thread = have_common;
429 while (factories[i] || sinkelement) {
430 // fase 4: add other elements...
433 factory = (GstElementFactory *)(factories[i]->data);
434 g_print("GstPipeline: factory \"%s\"\n", factory->name);
435 element = gst_elementfactory_create(factory, factory->name);
436 factories[i] = g_list_next(factories[i]);
438 // we have arived to the final sink element
440 element = sinkelement;
444 // this element suggests the use of a thread, so we set one up...
445 if (GST_ELEMENT_IS_THREAD_SUGGESTED(element) || use_thread) {
448 GstPad *srcpad, *sinkpad;
452 g_print("GstPipeline: sugest new thread for \"%s\" %08x\n", element->name, GST_FLAGS(element));
454 // create a new queue and add to the previous bin
455 queue = gst_elementfactory_make("queue", g_strconcat("queue_", gst_element_get_name(element), NULL));
456 gst_bin_add(GST_BIN(thebin), queue);
458 // this will be the new bin for all following elements
459 thebin = gst_elementfactory_make("thread", g_strconcat("thread_", gst_element_get_name(element), NULL));
461 srcpad = gst_element_get_pad(queue, "src");
463 sinkpads = gst_element_get_pad_list(element);
465 sinkpad = (GstPad *)sinkpads->data;
467 // FIXME connect matching pads, not just the first one...
468 if (sinkpad->direction == GST_PAD_SINK &&
469 !GST_PAD_CONNECTED(sinkpad)) {
470 // the queue has the types of the element it connects
471 srcpad->type = sinkpad->type;
472 gst_element_get_pad(queue, "sink")->type = sinkpad->type;
475 sinkpads = g_list_next(sinkpads);
477 gst_pipeline_pads_autoplug(thesrcelement, queue);
479 gst_bin_add(GST_BIN(thebin), element);
480 gst_bin_add(GST_BIN(pipeline), thebin);
481 thesrcelement = queue;
483 // no thread needed, easy case
485 gst_bin_add(GST_BIN(thebin), element);
487 gst_pipeline_pads_autoplug(thesrcelement, element);
489 // this element is now the new source element
490 thesrcelement = element;
493 elements = g_list_next(elements);
498 g_print("GstPipeline: unable to autoplug pipeline \"%s\"\n",
499 gst_element_get_name(GST_ELEMENT(pipeline)));
503 static GstElementStateReturn gst_pipeline_change_state(GstElement *element) {
504 GstPipeline *pipeline;
506 g_return_val_if_fail(GST_IS_PIPELINE(element), FALSE);
507 pipeline = GST_PIPELINE(element);
510 switch (GST_STATE_PENDING(pipeline)) {
511 case GST_STATE_READY:
512 // we need to set up internal state
513 gst_pipeline_prepare(pipeline);
519 if (GST_ELEMENT_CLASS(parent_class)->change_state)
520 return GST_ELEMENT_CLASS(parent_class)->change_state(element);
521 return GST_STATE_SUCCESS;
526 * gst_pipeline_iterate:
527 * @pipeline: GstPipeline to iterate
529 * Cause the pipeline's contents to be run through one full 'iteration'.
531 void gst_pipeline_iterate(GstPipeline *pipeline) {
532 g_return_if_fail(pipeline != NULL);
533 g_return_if_fail(GST_IS_PIPELINE(pipeline));