2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2002 Andy Wingo <wingo@pobox.com>
6 * gstparse.c: get a pipeline from a text pipeline description
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
28 #include "parse/types.h"
30 typedef struct _gst_parse_delayed_pad gst_parse_delayed_pad;
31 struct _gst_parse_delayed_pad
47 gst_parse_error_quark (void)
49 static GQuark quark = 0;
51 quark = g_quark_from_static_string ("gst_parse_error");
55 G_GNUC_UNUSED static void
56 dynamic_connect (GstElement * element, GstPad * newpad, gpointer data)
58 dynamic_connection_t *dc = (dynamic_connection_t *) data;
60 if (!strcmp (gst_pad_get_name (newpad), dc->srcpadname)) {
61 gst_element_set_state (dc->pipeline, GST_STATE_PAUSED);
62 if (!gst_pad_connect (newpad, dc->target))
63 g_warning ("could not connect %s:%s to %s:%s", GST_DEBUG_PAD_NAME (newpad),
64 GST_DEBUG_PAD_NAME (dc->target));
65 gst_element_set_state (dc->pipeline, GST_STATE_PLAYING);
70 make_elements (graph_t *g, GError **error)
76 if (!(g->bins || g->elements)) {
79 GST_PARSE_ERROR_SYNTAX,
84 if (g->current_bin_type)
85 bin_type = g->current_bin_type;
87 bin_type = "pipeline";
89 if (!(g->bin = gst_element_factory_make (bin_type, NULL))) {
92 GST_PARSE_ERROR_NO_SUCH_ELEMENT,
93 "No such bin type %s", bin_type);
99 e = (element_t*)l->data;
100 if (!(e->element = gst_element_factory_make (e->type, NULL))) {
103 GST_PARSE_ERROR_NO_SUCH_ELEMENT,
104 "No such element %s", e->type);
107 gst_bin_add (GST_BIN (g->bin), e->element);
113 if (!make_elements ((graph_t*)l->data, error))
115 gst_bin_add (GST_BIN (g->bin), ((graph_t*)l->data)->bin);
123 set_properties (graph_t *g, GError **error)
132 e = (element_t*)l->data;
133 l2 = e->property_values;
135 p = (property_t*)l2->data;
136 if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (e->element), p->name))) {
137 g_object_set_property (G_OBJECT (e->element), p->name, p->value);
141 GST_PARSE_ERROR_NO_SUCH_PROPERTY,
142 "No such property '%s' in element '%s'",
143 p->name, GST_OBJECT_NAME (GST_OBJECT (e->element)));
146 l2 = g_list_next (l2);
153 if (!set_properties ((graph_t*)l->data, error))
162 find_element_by_index_recurse (graph_t *g, gint i)
170 e = (element_t*)l->data;
179 if ((element = find_element_by_index_recurse ((graph_t*)l->data, i)))
188 find_element_by_index (graph_t *g, gint i)
193 return find_element_by_index_recurse (g, i);
197 make_connections (graph_t *g, GError **error)
201 GstElement *src, *sink;
207 c = (connection_t*)l->data;
209 if (!(src = gst_bin_get_by_name (GST_BIN (g->bin), c->src_name))) {
212 GST_PARSE_ERROR_NO_SUCH_ELEMENT,
213 "No such element '%s'",
218 src = find_element_by_index (g, c->src_index);
222 if (!(sink = gst_bin_get_by_name (GST_BIN (g->bin), c->sink_name))) {
225 GST_PARSE_ERROR_NO_SUCH_ELEMENT,
226 "No such element '%s'",
231 sink = find_element_by_index (g, c->sink_index);
238 /* balanced multipad connection */
240 p1 = gst_element_get_pad (src, (gchar*)a->data);
241 p2 = gst_element_get_pad (sink, (gchar*)b->data);
243 if (!p1 && p2 && (pt1 = gst_element_get_pad_template (src, (gchar*)a->data)) &&
244 pt1->presence == GST_PAD_SOMETIMES) {
245 dynamic_connection_t *dc = g_new0 (dynamic_connection_t, 1);
246 dc->srcpadname = (gchar*)a->data;
248 dc->pipeline = g->bin;
250 GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s and %s:%s",
251 GST_OBJECT_NAME (GST_OBJECT (src)),
252 (gchar*)a->data, GST_DEBUG_PAD_NAME (p2));
254 g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
255 } else if (!p1 || !p2 || !gst_pad_connect (p1, p2)) {
258 GST_PARSE_ERROR_CONNECT,
259 "Could not connect %s:%s to %s:%s",
260 GST_OBJECT_NAME (src), (gchar*)a->data,
261 GST_OBJECT_NAME (sink), (gchar*)b->data);
268 if ((pt1 = gst_element_get_pad_template (src, (gchar*)a->data))) {
269 if ((p1 = gst_element_get_pad (src, (gchar*)a->data)) || pt1->presence == GST_PAD_SOMETIMES) {
272 if ((p2 = gst_element_get_pad (sink, (gchar*)a->data))) {
273 dynamic_connection_t *dc = g_new0 (dynamic_connection_t, 1);
274 dc->srcpadname = (gchar*)a->data;
276 dc->pipeline = g->bin;
278 GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s and %s:%s",
279 GST_OBJECT_NAME (GST_OBJECT (src)),
280 (gchar*)a->data, GST_DEBUG_PAD_NAME (p2));
282 g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
288 GST_PARSE_ERROR_CONNECT,
289 "Could not get a pad %s from element %s",
290 (gchar*)a->data, GST_OBJECT_NAME (src));
296 GST_PARSE_ERROR_CONNECT,
297 "Could not get a pad %s from element %s",
298 (gchar*)a->data, GST_OBJECT_NAME (src));
302 if (!(p2 = gst_element_get_compatible_pad (sink, p1))) {
305 GST_PARSE_ERROR_CONNECT,
306 "Could not find a compatible pad in element %s to for %s:%s",
307 GST_OBJECT_NAME (sink), GST_OBJECT_NAME (src), (gchar*)a->data);
310 if (!gst_pad_connect (p1, p2)) {
313 GST_PARSE_ERROR_CONNECT,
314 "Could not connect %s:%s to %s:%s",
315 GST_OBJECT_NAME (src), GST_OBJECT_NAME (p1),
316 GST_OBJECT_NAME (sink), GST_OBJECT_NAME (p1));
320 if (!(p2 = gst_element_get_pad (sink, (gchar*)b->data))) {
323 GST_PARSE_ERROR_CONNECT,
324 "Could not get a pad %s from element %s",
325 (gchar*)b->data, GST_OBJECT_NAME (sink));
328 if (!(p1 = gst_element_get_compatible_pad (src, p2))) {
331 GST_PARSE_ERROR_CONNECT,
332 "Could not find a compatible pad in element %s to for %s:%s",
333 GST_OBJECT_NAME (src), GST_OBJECT_NAME (sink), (gchar*)b->data);
336 if (!gst_pad_connect (p1, p2)) {
339 GST_PARSE_ERROR_CONNECT,
340 "Could not connect %s:%s to %s:%s",
341 GST_OBJECT_NAME (src), GST_OBJECT_NAME (p1),
342 GST_OBJECT_NAME (sink), GST_OBJECT_NAME (p1));
346 if (!gst_element_connect (src, sink)) {
349 GST_PARSE_ERROR_CONNECT,
350 "Could not connect %s to %s",
351 GST_OBJECT_NAME (src), GST_OBJECT_NAME (sink));
360 if (!make_connections ((graph_t*)l->data, error))
369 pipeline_from_graph (graph_t *g, GError **error)
371 if (!make_elements (g, error))
374 if (!set_properties (g, error))
377 if (!make_connections (g, error))
380 return (GstBin*)g->bin;
385 * @argv: null-terminated array of arguments
387 * Create a new pipeline based on command line syntax.
389 * Returns: a new pipeline on success, NULL on failure
392 gst_parse_launchv (const gchar **argv, GError **error)
395 gchar *pipeline_description;
397 /* i think this cast works out ok... */
398 pipeline_description = g_strjoinv (" ", (gchar**)argv);
400 pipeline = gst_parse_launch (pipeline_description, error);
407 * @pipeline_description: the command line describing the pipeline
409 * Create a new pipeline based on command line syntax.
411 * Returns: a new bin on success, NULL on failure. By default the bin is
412 * a GstPipeline, but it depends on the pipeline_description.
415 gst_parse_launch (const gchar * pipeline_description, GError **error)
418 static GStaticMutex flex_lock = G_STATIC_MUTEX_INIT;
420 g_return_val_if_fail (pipeline_description != NULL, NULL);
422 GST_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
423 pipeline_description);
425 /* the need for the mutex will go away with flex 2.5.6 */
426 g_static_mutex_lock (&flex_lock);
427 graph = _gst_parse_launch (pipeline_description, error);
428 g_static_mutex_unlock (&flex_lock);
433 return pipeline_from_graph (graph, error);