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 GstElementFactory *factory;
308 guint16 src_type = 0, sink_type = 0;
310 gboolean use_thread = FALSE, have_common = FALSE;
312 g_return_val_if_fail(pipeline != NULL, FALSE);
313 g_return_val_if_fail(GST_IS_PIPELINE(pipeline), FALSE);
315 g_print("GstPipeline: autopluging pipeline \"%s\"\n",
316 gst_element_get_name(GST_ELEMENT(pipeline)));
319 // fase 1, run typedetect on the source if needed...
320 if (!pipeline->src) {
321 g_print("GstPipeline: no source detected, can't autoplug pipeline \"%s\"\n",
322 gst_element_get_name(GST_ELEMENT(pipeline)));
326 factory = gst_element_get_factory(pipeline->src);
328 src_types = factory->src_types;
329 if (src_types == NULL) {
330 g_print("GstPipeline: source \"%s\" has no MIME type, running typefind...\n",
331 gst_element_get_name(pipeline->src));
333 src_type = gst_pipeline_typefind(pipeline, pipeline->src);
336 g_print("GstPipeline: source \"%s\" type found %d\n", gst_element_get_name(pipeline->src),
340 g_print("GstPipeline: source \"%s\" has no type\n", gst_element_get_name(pipeline->src));
346 // FIXME loop over types and find paths...
347 src_types = g_list_next(src_types);
351 srcelement = pipeline->src;
353 elements = pipeline->sinks;
355 numsinks = g_list_length(elements);
356 factories = g_new0(GList *, numsinks);
359 // fase 2, loop over all the sinks..
364 element = GST_ELEMENT(elements->data);
366 pads = gst_element_get_pad_list(element);
369 pad = (GstPad *)pads->data;
371 if (pad->direction == GST_PAD_SINK) {
372 sink_type = gst_pad_get_type_id(pad);
376 pads = g_list_next(pads);
379 factories[i] = gst_type_get_sink_to_src(src_type, sink_type);
380 //factories[i] = g_list_append(factories[i], element);
383 elements = g_list_next(elements);
386 while (factories[0]) {
387 // fase 3: add common elements
388 factory = (GstElementFactory *)(factories[0]->data);
390 // check to other paths for mathing elements (factories)
391 for (i=1; i<numsinks; i++) {
392 if (factory != (GstElementFactory *)(factories[i]->data)) {
395 factories[i] = g_list_next(factories[i]);
397 factory = (GstElementFactory *)(factories[0]->data);
399 g_print("GstPipeline: common factory \"%s\"\n", factory->name);
401 element = gst_elementfactory_create(factory, factory->name);
402 gst_bin_add(GST_BIN(pipeline), element);
404 gst_pipeline_pads_autoplug(srcelement, element);
406 srcelement = element;
408 factories[0] = g_list_next(factories[0]);
414 // loop over all the sink elements
415 elements = pipeline->sinks;
419 GstElement *thesrcelement = srcelement;
420 GstElement *thebin = GST_ELEMENT(pipeline);
422 sinkelement = (GstElement *)elements->data;
424 use_thread = have_common;
426 while (factories[i] || sinkelement) {
427 // fase 4: add other elements...
430 factory = (GstElementFactory *)(factories[i]->data);
431 g_print("GstPipeline: factory \"%s\"\n", factory->name);
432 element = gst_elementfactory_create(factory, factory->name);
433 factories[i] = g_list_next(factories[i]);
435 // we have arived to the final sink element
437 element = sinkelement;
441 // this element suggests the use of a thread, so we set one up...
442 if (GST_ELEMENT_IS_THREAD_SUGGESTED(element) || use_thread) {
445 GstPad *srcpad, *sinkpad;
449 g_print("GstPipeline: sugest new thread for \"%s\" %08x\n", element->name, GST_FLAGS(element));
451 // create a new queue and add to the previous bin
452 queue = gst_elementfactory_make("queue", g_strconcat("queue_", gst_element_get_name(element), NULL));
453 gst_bin_add(GST_BIN(thebin), queue);
455 // this will be the new bin for all following elements
456 thebin = gst_elementfactory_make("thread", g_strconcat("thread_", gst_element_get_name(element), NULL));
458 srcpad = gst_element_get_pad(queue, "src");
460 sinkpads = gst_element_get_pad_list(element);
462 sinkpad = (GstPad *)sinkpads->data;
464 // FIXME connect matching pads, not just the first one...
465 if (sinkpad->direction == GST_PAD_SINK &&
466 !GST_PAD_CONNECTED(sinkpad)) {
467 // the queue has the types of the element it connects
468 srcpad->type = sinkpad->type;
469 gst_element_get_pad(queue, "sink")->type = sinkpad->type;
472 sinkpads = g_list_next(sinkpads);
474 gst_pipeline_pads_autoplug(thesrcelement, queue);
476 gst_bin_add(GST_BIN(thebin), element);
477 gst_bin_add(GST_BIN(pipeline), thebin);
478 thesrcelement = queue;
480 // no thread needed, easy case
482 gst_bin_add(GST_BIN(thebin), element);
484 gst_pipeline_pads_autoplug(thesrcelement, element);
486 // this element is now the new source element
487 thesrcelement = element;
490 elements = g_list_next(elements);
495 g_print("GstPipeline: unable to autoplug pipeline \"%s\"\n",
496 gst_element_get_name(GST_ELEMENT(pipeline)));
500 static GstElementStateReturn gst_pipeline_change_state(GstElement *element) {
501 GstPipeline *pipeline;
503 g_return_val_if_fail(GST_IS_PIPELINE(element), FALSE);
504 pipeline = GST_PIPELINE(element);
507 switch (GST_STATE_PENDING(pipeline)) {
508 case GST_STATE_READY:
509 // we need to set up internal state
510 gst_pipeline_prepare(pipeline);
516 if (GST_ELEMENT_CLASS(parent_class)->change_state)
517 return GST_ELEMENT_CLASS(parent_class)->change_state(element);
518 return GST_STATE_SUCCESS;
523 * gst_pipeline_iterate:
524 * @pipeline: GstPipeline to iterate
526 * Cause the pipeline's contents to be run through one full 'iteration'.
528 void gst_pipeline_iterate(GstPipeline *pipeline) {
529 g_return_if_fail(pipeline != NULL);
530 g_return_if_fail(GST_IS_PIPELINE(pipeline));