quote spaces in tokens passed to gst_parse_launchv. this restores the old (shell...
[platform/upstream/gstreamer.git] / gst / gstparse.c
1 /* GStreamer
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>
5  *
6  * gstparse.c: get a pipeline from a text pipeline description
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #include <string.h>
25
26 #include "gstparse.h"
27 #include "gstinfo.h"
28 #include "parse/types.h"
29
30 typedef struct _gst_parse_delayed_pad gst_parse_delayed_pad;
31 struct _gst_parse_delayed_pad
32 {
33   gchar *name;
34   GstPad *peer;
35 };
36
37 typedef struct
38 {
39   gchar *srcpadname;
40   GstPad *target;
41   GstElement *pipeline;
42 }
43 dynamic_connection_t;
44
45
46 GQuark 
47 gst_parse_error_quark (void)
48 {
49   static GQuark quark = 0;
50   if (!quark)
51     quark = g_quark_from_static_string ("gst_parse_error");
52   return quark;
53 }
54
55 G_GNUC_UNUSED static void
56 dynamic_connect (GstElement * element, GstPad * newpad, gpointer data)
57 {
58   dynamic_connection_t *dc = (dynamic_connection_t *) data;
59
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);
66   }
67 }
68
69 static gboolean
70 make_elements (graph_t *g, GError **error) 
71 {
72   GList *l = NULL;
73   gchar *bin_type;
74   element_t *e;
75   
76   if (!(g->bins || g->elements)) {
77     g_set_error (error,
78                  GST_PARSE_ERROR,
79                  GST_PARSE_ERROR_SYNTAX,
80                  "Empty bin");
81     return FALSE;
82   }
83
84   if (g->current_bin_type)
85     bin_type = g->current_bin_type;
86   else
87     bin_type = "pipeline";
88   
89   if (!(g->bin = gst_element_factory_make (bin_type, NULL))) {
90     g_set_error (error,
91                  GST_PARSE_ERROR,
92                  GST_PARSE_ERROR_NO_SUCH_ELEMENT,
93                  "No such bin type %s", bin_type);
94     return FALSE;
95   }
96   
97   l = g->elements;
98   while (l) {
99     e = (element_t*)l->data;
100     if (!(e->element = gst_element_factory_make (e->type, NULL))) {
101       g_set_error (error,
102                    GST_PARSE_ERROR,
103                    GST_PARSE_ERROR_NO_SUCH_ELEMENT,
104                    "No such element %s", e->type);
105       return FALSE;
106     }
107     gst_bin_add (GST_BIN (g->bin), e->element);
108     l = g_list_next (l);
109   }
110   
111   l = g->bins;
112   while (l) {
113     if (!make_elements ((graph_t*)l->data, error))
114       return FALSE;
115     gst_bin_add (GST_BIN (g->bin), ((graph_t*)l->data)->bin);
116     l = g_list_next (l);
117   }
118
119   return TRUE;
120 }
121
122 static gboolean
123 set_properties (graph_t *g, GError **error)
124 {
125   GList *l, *l2;
126   element_t *e;
127   property_t *p;
128   GParamSpec *pspec;
129   
130   l = g->elements;
131   while (l) {
132     e = (element_t*)l->data;
133     l2 = e->property_values;
134     while (l2) {
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);
138       } else {
139         g_set_error (error,
140                      GST_PARSE_ERROR,
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)));
144         return FALSE;
145       }
146       l2 = g_list_next (l2);
147     }
148     l = g_list_next (l);
149   }
150   
151   l = g->bins;
152   while (l) {
153     if (!set_properties ((graph_t*)l->data, error))
154       return FALSE;
155     l = g_list_next (l);
156   }
157   
158   return TRUE;
159 }
160
161 static GstElement*
162 find_element_by_index_recurse (graph_t *g, gint i)
163 {
164   GList *l;
165   element_t *e;
166   GstElement *element;
167   
168   l = g->elements;
169   while (l) {
170     e = (element_t*)l->data;
171     if (e->index == i) {
172       return e->element;
173     }
174     l = g_list_next (l);
175   }
176   
177   l = g->bins;
178   while (l) {
179     if ((element = find_element_by_index_recurse ((graph_t*)l->data, i)))
180       return element;
181     l = g_list_next (l);
182   }
183   
184   return NULL;
185 }
186
187 static GstElement*
188 find_element_by_index (graph_t *g, gint i) 
189 {
190   while (g->parent)
191     g = g->parent;
192
193   return find_element_by_index_recurse (g, i);
194 }
195
196 static gboolean
197 make_connections (graph_t *g, GError **error)
198 {
199   GList *l, *a, *b;
200   connection_t *c;
201   dynamic_connection_t *dc;
202   GstElement *src, *sink;
203   GstPad *p1, *p2;
204   GstPadTemplate *pt1, *pt2;
205   
206   l = g->connections;
207   while (l) {
208     c = (connection_t*)l->data;
209     if (c->src_name) {
210       if (!(src = gst_bin_get_by_name (GST_BIN (g->bin), c->src_name))) {
211         g_set_error (error,
212                      GST_PARSE_ERROR,
213                      GST_PARSE_ERROR_NO_SUCH_ELEMENT,
214                      "No such element '%s'",
215                      c->src_name);
216         return FALSE;
217       }
218     } else {
219       src = find_element_by_index (g, c->src_index);
220       g_assert (src);
221     }
222     if (c->sink_name) {
223       if (!(sink = gst_bin_get_by_name (GST_BIN (g->bin), c->sink_name))) {
224         g_set_error (error,
225                      GST_PARSE_ERROR,
226                      GST_PARSE_ERROR_NO_SUCH_ELEMENT,
227                      "No such element '%s'",
228                      c->sink_name);
229         return FALSE;
230       }
231     } else {
232       sink = find_element_by_index (g, c->sink_index);
233       g_assert (sink);
234     }
235     
236     a = c->src_pads;
237     b = c->sink_pads;
238 //    g_print ("a: %p, b: %p\n", a, b);
239     if (a && b) {
240       /* balanced multipad connection */
241       while (a && b) {
242         p1 = gst_element_get_pad (src, (gchar*)a->data);
243         p2 = gst_element_get_pad (sink, (gchar*)b->data);
244
245         if (!p2)
246           goto could_not_get_pad_b;
247         
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;
252           dc->target = p2;
253           dc->pipeline = g->bin;
254           
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));
258           
259           g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
260         } else if (!p1) {
261           goto could_not_get_pad_a;
262         } else if (!gst_pad_connect (p1, p2)) {
263           goto could_not_connect_pads;
264         }
265         a = g_list_next (a);
266         b = g_list_next (b);
267       }
268     } else if (a) {
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) {
272           if (!p1) {
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;
280                 dc->target = p2;
281                 dc->pipeline = g->bin;
282               
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));
286               
287                 g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
288               } else {
289                 /* both pt1 and pt2 are sometimes templates. sheesh. */
290                 goto both_templates_have_sometimes_presence;
291               }
292             } else {
293               goto could_not_get_compatible_to_a;
294             }
295           } else {
296             if (!(p2 = gst_element_get_compatible_pad (sink, p1))) {
297               goto could_not_get_compatible_to_a;
298             }
299           }
300         } else {
301           goto could_not_get_pad_a;
302         }
303       } else {
304         goto could_not_get_pad_a;
305       }
306       
307       if (!gst_pad_connect (p1, p2)) {
308         goto could_not_connect_pads;
309       }
310     } else if (b) {
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;
314       }
315       if (!(p1 = gst_element_get_compatible_pad (src, p2))) {
316         goto could_not_get_compatible_to_b;
317       }
318       if (!gst_pad_connect (p1, p2)) {
319         goto could_not_connect_pads;
320       }
321     } else {
322       if (!gst_element_connect (src, sink)) {
323         goto could_not_connect_elements;
324       }
325     }
326     l = g_list_next (l);
327   }
328   
329   l = g->bins;
330   while (l) {
331     if (!make_connections ((graph_t*)l->data, error))
332       return FALSE;
333     l = g_list_next (l);
334   }
335   
336   return TRUE;
337
338 could_not_get_pad_a:
339   g_set_error (error,
340                GST_PARSE_ERROR,
341                GST_PARSE_ERROR_CONNECT,
342                "Could not get a pad %s from element %s",
343                (gchar*)a->data, GST_OBJECT_NAME (src));
344   return FALSE;
345 could_not_get_pad_b:
346   g_set_error (error,
347                GST_PARSE_ERROR,
348                GST_PARSE_ERROR_CONNECT,
349                "Could not get a pad %s from element %s",
350                (gchar*)b->data, GST_OBJECT_NAME (sink));
351   return FALSE;
352 could_not_get_compatible_to_a:
353   g_set_error (error,
354                GST_PARSE_ERROR,
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);
358   return FALSE;
359 could_not_get_compatible_to_b:
360   g_set_error (error,
361                GST_PARSE_ERROR,
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);
365   return FALSE;
366 both_templates_have_sometimes_presence:
367   g_set_error (error,
368                GST_PARSE_ERROR,
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);
372   return FALSE;
373 could_not_connect_pads:
374   g_set_error (error,
375                GST_PARSE_ERROR,
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));
380   return FALSE;
381 could_not_connect_elements:
382   g_set_error (error,
383                GST_PARSE_ERROR,
384                GST_PARSE_ERROR_CONNECT,
385                "Could not connect element %s to %s",
386                GST_OBJECT_NAME (src),
387                GST_OBJECT_NAME (sink));
388   return FALSE;
389 }
390
391 static GstBin*
392 pipeline_from_graph (graph_t *g, GError **error)
393 {
394   if (!make_elements (g, error))
395     return NULL;
396   
397   if (!set_properties (g, error))
398     return NULL;
399   
400   if (!make_connections (g, error))
401     return NULL;
402   
403   return (GstBin*)g->bin;
404 }
405
406 /**
407  * gst_parse_launchv:
408  * @argv: null-terminated array of arguments
409  *
410  * Create a new pipeline based on command line syntax.
411  *
412  * Returns: a new pipeline on success, NULL on failure
413  */
414 GstBin *
415 gst_parse_launchv (const gchar **argv, GError **error)
416 {
417   GstBin *pipeline;
418   GString *str;
419   const gchar **argvp, *arg;
420   gchar *tmp;
421
422   /* let's give it a nice size. */
423   str = g_string_sized_new (1024);
424
425   argvp = argv;
426   while (*argvp) {
427     arg = *argvp;
428     tmp = _gst_parse_escape (arg);
429     g_string_append (str, tmp);
430     g_free (tmp);
431     g_string_append (str, " ");
432     argvp++;
433   }
434   
435   pipeline = gst_parse_launch (str->str, error);
436
437   g_string_free (str, TRUE);
438
439   return pipeline;
440 }
441
442 gchar *_gst_parse_escape (const gchar *str)
443 {
444   GString *gstr = NULL;
445   
446   g_return_val_if_fail (str != NULL, NULL);
447   
448   gstr = g_string_sized_new (strlen (str));
449   
450   while (*str) {
451     if (*str == ' ')
452       g_string_append_c (gstr, '\\');
453     g_string_append_c (gstr, *str);
454     str++;
455   }
456   
457   return gstr->str;
458 }
459
460 void _gst_parse_unescape (gchar *str)
461 {
462   gchar *walk;
463   
464   g_return_if_fail (str != NULL);
465   
466   walk = str;
467   
468   while (*walk) {
469     if (*walk == '\\')
470       walk++;
471     *str = *walk;
472     str++;
473     walk++;
474   }
475   *str = '\0';
476 }
477
478 /**
479  * gst_parse_launch:
480  * @pipeline_description: the command line describing the pipeline
481  *
482  * Create a new pipeline based on command line syntax.
483  *
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.
486  */
487 GstBin *
488 gst_parse_launch (const gchar * pipeline_description, GError **error)
489 {
490   graph_t *graph;
491   static GStaticMutex flex_lock = G_STATIC_MUTEX_INIT;
492
493   g_return_val_if_fail (pipeline_description != NULL, NULL);
494
495   GST_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
496             pipeline_description);
497
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);
502
503   if (!graph)
504     return NULL;
505   
506   return pipeline_from_graph (graph, error);
507 }