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.
29 #include "parse/types.h"
31 typedef struct _gst_parse_delayed_pad gst_parse_delayed_pad;
32 struct _gst_parse_delayed_pad
42 GstElement *target_element;
49 gst_parse_error_quark (void)
51 static GQuark quark = 0;
53 quark = g_quark_from_static_string ("gst_parse_error");
57 G_GNUC_UNUSED static void
58 dynamic_connect (GstElement * element, GstPad * newpad, gpointer data)
60 dynamic_connection_t *dc = (dynamic_connection_t *) data;
63 /* do we know the exact srcpadname? */
65 GstPadTemplate *templ = gst_pad_get_pad_template (newpad);
67 /* see if this is the one */
68 if (strcmp (gst_pad_get_name (newpad), dc->srcpadname) &&
69 strcmp (gst_object_get_name (GST_OBJECT (templ)), dc->srcpadname)) {
74 /* try to find a target pad if we don't know it yet */
75 if (!dc->target_pad) {
76 if (!GST_PAD_IS_CONNECTED (newpad)) {
77 dc->target_pad = gst_element_get_compatible_pad (dc->target_element, newpad);
84 if (!GST_PAD_IS_CONNECTED (dc->target_pad) && !GST_PAD_IS_CONNECTED (newpad)) {
85 gst_element_set_state (dc->pipeline, GST_STATE_PAUSED);
86 if (!gst_pad_connect (newpad, dc->target_pad) && warn) {
87 g_warning ("could not connect %s:%s to %s:%s", GST_DEBUG_PAD_NAME (newpad),
88 GST_DEBUG_PAD_NAME (dc->target_pad));
90 gst_element_set_state (dc->pipeline, GST_STATE_PLAYING);
95 make_elements (graph_t *g, GError **error)
101 if (!(g->bins || g->elements)) {
104 GST_PARSE_ERROR_SYNTAX,
109 if (g->current_bin_type)
110 bin_type = g->current_bin_type;
112 bin_type = "pipeline";
114 if (!(g->bin = gst_element_factory_make (bin_type, NULL))) {
117 GST_PARSE_ERROR_NO_SUCH_ELEMENT,
118 "No such bin type %s", bin_type);
124 e = (element_t*)l->data;
125 if (!(e->element = gst_element_factory_make (e->type, NULL))) {
128 GST_PARSE_ERROR_NO_SUCH_ELEMENT,
129 "No such element %s", e->type);
132 gst_bin_add (GST_BIN (g->bin), e->element);
138 if (!make_elements ((graph_t*)l->data, error))
140 gst_bin_add (GST_BIN (g->bin), ((graph_t*)l->data)->bin);
148 set_properties (graph_t *g, GError **error)
157 e = (element_t*)l->data;
158 l2 = e->property_values;
160 p = (property_t*)l2->data;
161 if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (e->element), p->name))) {
162 g_object_set_property (G_OBJECT (e->element), p->name, p->value);
166 GST_PARSE_ERROR_NO_SUCH_PROPERTY,
167 "No such property '%s' in element '%s'",
168 p->name, GST_OBJECT_NAME (GST_OBJECT (e->element)));
171 l2 = g_list_next (l2);
178 if (!set_properties ((graph_t*)l->data, error))
187 find_element_by_index_recurse (graph_t *g, gint i)
195 e = (element_t*)l->data;
204 if ((element = find_element_by_index_recurse ((graph_t*)l->data, i)))
213 find_element_by_index (graph_t *g, gint i)
218 return find_element_by_index_recurse (g, i);
222 make_connections (graph_t *g, GError **error)
226 dynamic_connection_t *dc;
227 GstElement *src, *sink;
229 GstPadTemplate *pt1, *pt2;
234 c = (connection_t*)l->data;
236 if (!(src = gst_bin_get_by_name (GST_BIN (g->bin), c->src_name))) {
239 GST_PARSE_ERROR_NO_SUCH_ELEMENT,
240 "No such element '%s'",
245 src = find_element_by_index (g, c->src_index);
249 if (!(sink = gst_bin_get_by_name (GST_BIN (g->bin), c->sink_name))) {
252 GST_PARSE_ERROR_NO_SUCH_ELEMENT,
253 "No such element '%s'",
258 sink = find_element_by_index (g, c->sink_index);
265 gst_caps_debug (caps, "foo");
266 /* g_print ("a: %p, b: %p\n", a, b); */
268 /* balanced multipad connection */
270 p1 = gst_element_get_pad (src, (gchar*)a->data);
271 p2 = gst_element_get_pad (sink, (gchar*)b->data);
274 goto could_not_get_pad_b;
276 if (!p1 && p2 && (pt1 = gst_element_get_pad_template (src, (gchar*)a->data)) &&
277 pt1->presence == GST_PAD_SOMETIMES) {
278 dc = g_new0 (dynamic_connection_t, 1);
279 dc->srcpadname = (gchar*)a->data;
281 dc->target_element = sink;
282 dc->pipeline = g->bin;
284 GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s and %s:%s",
285 GST_OBJECT_NAME (GST_OBJECT (src)),
286 (gchar*)a->data, GST_DEBUG_PAD_NAME (p2));
288 g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
290 goto could_not_get_pad_a;
291 } else if (!gst_pad_connect_filtered (p1, p2, caps)) {
292 goto could_not_connect_pads;
298 if ((pt1 = gst_element_get_pad_template (src, (gchar*)a->data))) {
299 if ((p1 = gst_element_get_pad (src, (gchar*)a->data)) || pt1->presence == GST_PAD_SOMETIMES) {
301 /* sigh, a hack until i fix the gstelement api... */
302 if ((pt2 = gst_element_get_compatible_pad_template (sink, pt1))) {
303 if ((p2 = gst_element_get_pad (sink, pt2->name_template))) {
304 dc = g_new0 (dynamic_connection_t, 1);
305 dc->srcpadname = (gchar*)a->data;
307 dc->target_element = NULL;
308 dc->pipeline = g->bin;
310 GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s and %s:%s",
311 GST_OBJECT_NAME (GST_OBJECT (src)),
312 (gchar*)a->data, GST_DEBUG_PAD_NAME (p2));
314 g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
317 /* both pt1 and pt2 are sometimes templates. sheesh. */
318 goto both_templates_have_sometimes_presence;
321 /* if the target pad has no padtemplate we will figure out a target
323 dc = g_new0 (dynamic_connection_t, 1);
324 dc->srcpadname = (gchar*)a->data;
325 dc->target_pad = NULL;
326 dc->target_element = sink;
327 dc->pipeline = g->bin;
329 GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s, and some pad in %s",
330 GST_OBJECT_NAME (GST_OBJECT (src)),
331 (gchar*)a->data, GST_OBJECT_NAME (sink));
333 g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
337 goto could_not_get_compatible_to_a;
340 goto could_not_get_pad_a;
343 goto could_not_get_pad_a;
346 if (!gst_pad_connect_filtered (p1, p2, caps)) {
347 goto could_not_connect_pads;
350 /* we don't support dynamic connections on this side yet, if ever */
351 if (!(p2 = gst_element_get_pad (sink, (gchar*)b->data))) {
352 goto could_not_get_pad_b;
354 if (!(p1 = gst_element_get_compatible_pad (src, p2))) {
355 goto could_not_get_compatible_to_b;
357 if (!gst_pad_connect_filtered (p1, p2, caps)) {
358 goto could_not_connect_pads;
361 if (!gst_element_connect_filtered (src, sink, caps)) {
362 goto could_not_connect_elements;
371 if (!make_connections ((graph_t*)l->data, error))
381 GST_PARSE_ERROR_CONNECT,
382 "Could not get a pad %s from element %s",
383 (gchar*)a->data, GST_OBJECT_NAME (src));
388 GST_PARSE_ERROR_CONNECT,
389 "Could not get a pad %s from element %s",
390 (gchar*)b->data, GST_OBJECT_NAME (sink));
392 could_not_get_compatible_to_a:
395 GST_PARSE_ERROR_CONNECT,
396 "Could not find a compatible pad in element %s to for %s:%s",
397 GST_OBJECT_NAME (sink), GST_OBJECT_NAME (src), (gchar*)a->data);
399 could_not_get_compatible_to_b:
402 GST_PARSE_ERROR_CONNECT,
403 "Could not find a compatible pad in element %s to for %s:%s",
404 GST_OBJECT_NAME (src), GST_OBJECT_NAME (sink), (gchar*)b->data);
406 both_templates_have_sometimes_presence:
409 GST_PARSE_ERROR_CONNECT,
410 "Both %s:%s and %s:%s have GST_PAD_SOMETIMES presence, operation not supported",
411 GST_OBJECT_NAME (src), pt1->name_template, GST_OBJECT_NAME (sink), pt2->name_template);
413 could_not_connect_pads:
416 GST_PARSE_ERROR_CONNECT,
417 "Could not connect %s:%s to %s:%s",
418 GST_DEBUG_PAD_NAME (p1),
419 GST_DEBUG_PAD_NAME (p2));
421 could_not_connect_elements:
424 GST_PARSE_ERROR_CONNECT,
425 "Could not connect element %s to %s",
426 GST_OBJECT_NAME (src),
427 GST_OBJECT_NAME (sink));
432 pipeline_from_graph (graph_t *g, GError **error)
434 if (!make_elements (g, error))
437 if (!set_properties (g, error))
440 if (!make_connections (g, error))
443 return (GstBin*)g->bin;
448 * @argv: null-terminated array of arguments
449 * @error: pointer to GError
451 * Create a new pipeline based on command line syntax.
453 * Returns: a new pipeline on success, NULL on failure and error
454 * will contain the error message.
457 gst_parse_launchv (const gchar **argv, GError **error)
461 const gchar **argvp, *arg;
464 g_return_val_if_fail (argv != NULL, NULL);
466 /* let's give it a nice size. */
467 str = g_string_sized_new (1024);
472 tmp = _gst_parse_escape (arg);
473 g_string_append (str, tmp);
475 g_string_append (str, " ");
479 pipeline = gst_parse_launch (str->str, error);
481 g_string_free (str, TRUE);
486 gchar *_gst_parse_escape (const gchar *str)
488 GString *gstr = NULL;
490 g_return_val_if_fail (str != NULL, NULL);
492 gstr = g_string_sized_new (strlen (str));
496 g_string_append_c (gstr, '\\');
497 g_string_append_c (gstr, *str);
504 void _gst_parse_unescape (gchar *str)
508 g_return_if_fail (str != NULL);
524 * @pipeline_description: the command line describing the pipeline
525 * @error: the error message in case of a failure
527 * Create a new pipeline based on command line syntax.
529 * Returns: a new bin on success, NULL on failure. By default the bin is
530 * a GstPipeline, but it depends on the pipeline_description.
533 gst_parse_launch (const gchar * pipeline_description, GError **error)
536 static GStaticMutex flex_lock = G_STATIC_MUTEX_INIT;
538 g_return_val_if_fail (pipeline_description != NULL, NULL);
540 GST_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
541 pipeline_description);
543 /* the need for the mutex will go away with flex 2.5.6 */
544 g_static_mutex_lock (&flex_lock);
545 graph = _gst_parse_launch (pipeline_description, error);
546 g_static_mutex_unlock (&flex_lock);
551 return pipeline_from_graph (graph, error);