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 dynamic_connection_t *dc;
202 GstElement *src, *sink;
204 GstPadTemplate *pt1, *pt2;
208 c = (connection_t*)l->data;
210 if (!(src = gst_bin_get_by_name (GST_BIN (g->bin), c->src_name))) {
213 GST_PARSE_ERROR_NO_SUCH_ELEMENT,
214 "No such element '%s'",
219 src = find_element_by_index (g, c->src_index);
223 if (!(sink = gst_bin_get_by_name (GST_BIN (g->bin), c->sink_name))) {
226 GST_PARSE_ERROR_NO_SUCH_ELEMENT,
227 "No such element '%s'",
232 sink = find_element_by_index (g, c->sink_index);
238 // g_print ("a: %p, b: %p\n", a, b);
240 /* balanced multipad connection */
242 p1 = gst_element_get_pad (src, (gchar*)a->data);
243 p2 = gst_element_get_pad (sink, (gchar*)b->data);
246 goto could_not_get_pad_b;
248 if (!p1 && p2 && (pt1 = gst_element_get_pad_template (src, (gchar*)a->data)) &&
249 pt1->presence == GST_PAD_SOMETIMES) {
250 dc = g_new0 (dynamic_connection_t, 1);
251 dc->srcpadname = (gchar*)a->data;
253 dc->pipeline = g->bin;
255 GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s and %s:%s",
256 GST_OBJECT_NAME (GST_OBJECT (src)),
257 (gchar*)a->data, GST_DEBUG_PAD_NAME (p2));
259 g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
261 goto could_not_get_pad_a;
262 } else if (!gst_pad_connect (p1, p2)) {
263 goto could_not_connect_pads;
269 if ((pt1 = gst_element_get_pad_template (src, (gchar*)a->data))) {
270 // g_print ("have padtemplate %s, SOMETIMES=%s\n", pt1->name_template, pt1->presence == GST_PAD_SOMETIMES ? "TRUE" : "FALSE");
271 if ((p1 = gst_element_get_pad (src, (gchar*)a->data)) || pt1->presence == GST_PAD_SOMETIMES) {
273 /* sigh, a hack until i fix the gstelement api... */
274 if ((pt2 = gst_element_get_compatible_pad_template (sink, pt1))) {
275 // g_print ("have compatible pad template %s\n", pt2->name_template);
276 if ((p2 = gst_element_get_pad (sink, pt2->name_template))) {
277 // g_print ("got the pad\n");
278 dc = g_new0 (dynamic_connection_t, 1);
279 dc->srcpadname = (gchar*)a->data;
281 dc->pipeline = g->bin;
283 GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s and %s:%s",
284 GST_OBJECT_NAME (GST_OBJECT (src)),
285 (gchar*)a->data, GST_DEBUG_PAD_NAME (p2));
287 g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
289 /* both pt1 and pt2 are sometimes templates. sheesh. */
290 goto both_templates_have_sometimes_presence;
293 goto could_not_get_compatible_to_a;
296 if (!(p2 = gst_element_get_compatible_pad (sink, p1))) {
297 goto could_not_get_compatible_to_a;
301 goto could_not_get_pad_a;
304 goto could_not_get_pad_a;
307 if (!gst_pad_connect (p1, p2)) {
308 goto could_not_connect_pads;
311 /* we don't support dynamic connections on this side yet, if ever */
312 if (!(p2 = gst_element_get_pad (sink, (gchar*)b->data))) {
313 goto could_not_get_pad_b;
315 if (!(p1 = gst_element_get_compatible_pad (src, p2))) {
316 goto could_not_get_compatible_to_b;
318 if (!gst_pad_connect (p1, p2)) {
319 goto could_not_connect_pads;
322 if (!gst_element_connect (src, sink)) {
323 goto could_not_connect_elements;
331 if (!make_connections ((graph_t*)l->data, error))
341 GST_PARSE_ERROR_CONNECT,
342 "Could not get a pad %s from element %s",
343 (gchar*)a->data, GST_OBJECT_NAME (src));
348 GST_PARSE_ERROR_CONNECT,
349 "Could not get a pad %s from element %s",
350 (gchar*)b->data, GST_OBJECT_NAME (sink));
352 could_not_get_compatible_to_a:
355 GST_PARSE_ERROR_CONNECT,
356 "Could not find a compatible pad in element %s to for %s:%s",
357 GST_OBJECT_NAME (sink), GST_OBJECT_NAME (src), (gchar*)a->data);
359 could_not_get_compatible_to_b:
362 GST_PARSE_ERROR_CONNECT,
363 "Could not find a compatible pad in element %s to for %s:%s",
364 GST_OBJECT_NAME (src), GST_OBJECT_NAME (sink), (gchar*)b->data);
366 both_templates_have_sometimes_presence:
369 GST_PARSE_ERROR_CONNECT,
370 "Both %s:%s and %s:%s have GST_PAD_SOMETIMES presence, operation not supported",
371 GST_OBJECT_NAME (src), pt1->name_template, GST_OBJECT_NAME (sink), pt2->name_template);
373 could_not_connect_pads:
376 GST_PARSE_ERROR_CONNECT,
377 "Could not connect %s:%s to %s:%s",
378 GST_DEBUG_PAD_NAME (p1),
379 GST_DEBUG_PAD_NAME (p2));
381 could_not_connect_elements:
384 GST_PARSE_ERROR_CONNECT,
385 "Could not connect element %s to %s",
386 GST_OBJECT_NAME (src),
387 GST_OBJECT_NAME (sink));
392 pipeline_from_graph (graph_t *g, GError **error)
394 if (!make_elements (g, error))
397 if (!set_properties (g, error))
400 if (!make_connections (g, error))
403 return (GstBin*)g->bin;
408 * @argv: null-terminated array of arguments
410 * Create a new pipeline based on command line syntax.
412 * Returns: a new pipeline on success, NULL on failure
415 gst_parse_launchv (const gchar **argv, GError **error)
419 const gchar **argvp, *arg;
422 /* let's give it a nice size. */
423 str = g_string_sized_new (1024);
428 tmp = _gst_parse_escape (arg);
429 g_string_append (str, tmp);
431 g_string_append (str, " ");
435 pipeline = gst_parse_launch (str->str, error);
437 g_string_free (str, TRUE);
442 gchar *_gst_parse_escape (const gchar *str)
444 GString *gstr = NULL;
446 g_return_val_if_fail (str != NULL, NULL);
448 gstr = g_string_sized_new (strlen (str));
452 g_string_append_c (gstr, '\\');
453 g_string_append_c (gstr, *str);
460 void _gst_parse_unescape (gchar *str)
464 g_return_if_fail (str != NULL);
480 * @pipeline_description: the command line describing the pipeline
482 * Create a new pipeline based on command line syntax.
484 * Returns: a new bin on success, NULL on failure. By default the bin is
485 * a GstPipeline, but it depends on the pipeline_description.
488 gst_parse_launch (const gchar * pipeline_description, GError **error)
491 static GStaticMutex flex_lock = G_STATIC_MUTEX_INIT;
493 g_return_val_if_fail (pipeline_description != NULL, NULL);
495 GST_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
496 pipeline_description);
498 /* the need for the mutex will go away with flex 2.5.6 */
499 g_static_mutex_lock (&flex_lock);
500 graph = _gst_parse_launch (pipeline_description, error);
501 g_static_mutex_unlock (&flex_lock);
506 return pipeline_from_graph (graph, error);