bdcfaa77e6fa2c3b44d56a8348c914c78108ddff
[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_pad;
41   GstElement *target_element;
42   GstElement *pipeline;
43 }
44 dynamic_connection_t;
45
46
47 GQuark 
48 gst_parse_error_quark (void)
49 {
50   static GQuark quark = 0;
51   if (!quark)
52     quark = g_quark_from_static_string ("gst_parse_error");
53   return quark;
54 }
55
56 G_GNUC_UNUSED static void
57 dynamic_connect (GstElement * element, GstPad * newpad, gpointer data)
58 {
59   dynamic_connection_t *dc = (dynamic_connection_t *) data;
60   gboolean warn = TRUE;
61
62   /* do we know the exact srcpadname? */
63   if (dc->srcpadname) {
64     /* see if this is the one */
65     if (strcmp (gst_pad_get_name (newpad), dc->srcpadname)) {
66       return;
67     }
68   }
69
70   /* try to find a target pad if we don't know it yet */
71   if (!dc->target_pad) {
72     if (!GST_PAD_IS_CONNECTED (newpad)) {
73       dc->target_pad = gst_element_get_compatible_pad (dc->target_element, newpad);
74       warn = FALSE;
75     }
76     else {
77       return;
78     }
79   }
80   if (!GST_PAD_IS_CONNECTED (dc->target_pad) && !GST_PAD_IS_CONNECTED (newpad)) {
81     gst_element_set_state (dc->pipeline, GST_STATE_PAUSED);
82     if (!gst_pad_connect (newpad, dc->target_pad) && warn) {
83       g_warning ("could not connect %s:%s to %s:%s", GST_DEBUG_PAD_NAME (newpad), 
84                  GST_DEBUG_PAD_NAME (dc->target_pad));
85     }
86     gst_element_set_state (dc->pipeline, GST_STATE_PLAYING);
87   }
88 }
89
90 static gboolean
91 make_elements (graph_t *g, GError **error) 
92 {
93   GList *l = NULL;
94   gchar *bin_type;
95   element_t *e;
96   
97   if (!(g->bins || g->elements)) {
98     g_set_error (error,
99                  GST_PARSE_ERROR,
100                  GST_PARSE_ERROR_SYNTAX,
101                  "Empty bin");
102     return FALSE;
103   }
104
105   if (g->current_bin_type)
106     bin_type = g->current_bin_type;
107   else
108     bin_type = "pipeline";
109   
110   if (!(g->bin = gst_element_factory_make (bin_type, NULL))) {
111     g_set_error (error,
112                  GST_PARSE_ERROR,
113                  GST_PARSE_ERROR_NO_SUCH_ELEMENT,
114                  "No such bin type %s", bin_type);
115     return FALSE;
116   }
117   
118   l = g->elements;
119   while (l) {
120     e = (element_t*)l->data;
121     if (!(e->element = gst_element_factory_make (e->type, NULL))) {
122       g_set_error (error,
123                    GST_PARSE_ERROR,
124                    GST_PARSE_ERROR_NO_SUCH_ELEMENT,
125                    "No such element %s", e->type);
126       return FALSE;
127     }
128     gst_bin_add (GST_BIN (g->bin), e->element);
129     l = g_list_next (l);
130   }
131   
132   l = g->bins;
133   while (l) {
134     if (!make_elements ((graph_t*)l->data, error))
135       return FALSE;
136     gst_bin_add (GST_BIN (g->bin), ((graph_t*)l->data)->bin);
137     l = g_list_next (l);
138   }
139
140   return TRUE;
141 }
142
143 static gboolean
144 set_properties (graph_t *g, GError **error)
145 {
146   GList *l, *l2;
147   element_t *e;
148   property_t *p;
149   GParamSpec *pspec;
150   
151   l = g->elements;
152   while (l) {
153     e = (element_t*)l->data;
154     l2 = e->property_values;
155     while (l2) {
156       p = (property_t*)l2->data;
157       if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (e->element), p->name))) {
158         g_object_set_property (G_OBJECT (e->element), p->name, p->value);
159       } else {
160         g_set_error (error,
161                      GST_PARSE_ERROR,
162                      GST_PARSE_ERROR_NO_SUCH_PROPERTY,
163                      "No such property '%s' in element '%s'",
164                      p->name, GST_OBJECT_NAME (GST_OBJECT (e->element)));
165         return FALSE;
166       }
167       l2 = g_list_next (l2);
168     }
169     l = g_list_next (l);
170   }
171   
172   l = g->bins;
173   while (l) {
174     if (!set_properties ((graph_t*)l->data, error))
175       return FALSE;
176     l = g_list_next (l);
177   }
178   
179   return TRUE;
180 }
181
182 static GstElement*
183 find_element_by_index_recurse (graph_t *g, gint i)
184 {
185   GList *l;
186   element_t *e;
187   GstElement *element;
188   
189   l = g->elements;
190   while (l) {
191     e = (element_t*)l->data;
192     if (e->index == i) {
193       return e->element;
194     }
195     l = g_list_next (l);
196   }
197   
198   l = g->bins;
199   while (l) {
200     if ((element = find_element_by_index_recurse ((graph_t*)l->data, i)))
201       return element;
202     l = g_list_next (l);
203   }
204   
205   return NULL;
206 }
207
208 static GstElement*
209 find_element_by_index (graph_t *g, gint i) 
210 {
211   while (g->parent)
212     g = g->parent;
213
214   return find_element_by_index_recurse (g, i);
215 }
216
217 static gboolean
218 make_connections (graph_t *g, GError **error)
219 {
220   GList *l, *a, *b;
221   connection_t *c;
222   dynamic_connection_t *dc;
223   GstElement *src, *sink;
224   GstPad *p1, *p2;
225   GstPadTemplate *pt1, *pt2;
226   
227   l = g->connections;
228   while (l) {
229     c = (connection_t*)l->data;
230     if (c->src_name) {
231       if (!(src = gst_bin_get_by_name (GST_BIN (g->bin), c->src_name))) {
232         g_set_error (error,
233                      GST_PARSE_ERROR,
234                      GST_PARSE_ERROR_NO_SUCH_ELEMENT,
235                      "No such element '%s'",
236                      c->src_name);
237         return FALSE;
238       }
239     } else {
240       src = find_element_by_index (g, c->src_index);
241       g_assert (src);
242     }
243     if (c->sink_name) {
244       if (!(sink = gst_bin_get_by_name (GST_BIN (g->bin), c->sink_name))) {
245         g_set_error (error,
246                      GST_PARSE_ERROR,
247                      GST_PARSE_ERROR_NO_SUCH_ELEMENT,
248                      "No such element '%s'",
249                      c->sink_name);
250         return FALSE;
251       }
252     } else {
253       sink = find_element_by_index (g, c->sink_index);
254       g_assert (sink);
255     }
256     
257     a = c->src_pads;
258     b = c->sink_pads;
259     /* g_print ("a: %p, b: %p\n", a, b); */
260     if (a && b) {
261       /* balanced multipad connection */
262       while (a && b) {
263         p1 = gst_element_get_pad (src, (gchar*)a->data);
264         p2 = gst_element_get_pad (sink, (gchar*)b->data);
265
266         if (!p2)
267           goto could_not_get_pad_b;
268         
269         if (!p1 && p2 && (pt1 = gst_element_get_pad_template (src, (gchar*)a->data)) &&
270             pt1->presence == GST_PAD_SOMETIMES) {
271           dc = g_new0 (dynamic_connection_t, 1);
272           dc->srcpadname = (gchar*)a->data;
273           dc->target_pad = p2;
274           dc->target_element = sink;
275           dc->pipeline = g->bin;
276           
277           GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s and %s:%s",
278                      GST_OBJECT_NAME (GST_OBJECT (src)),
279                      (gchar*)a->data, GST_DEBUG_PAD_NAME (p2));
280           
281           g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
282         } else if (!p1) {
283           goto could_not_get_pad_a;
284         } else if (!gst_pad_connect (p1, p2)) {
285           goto could_not_connect_pads;
286         }
287         a = g_list_next (a);
288         b = g_list_next (b);
289       }
290     } else if (a) {
291       if ((pt1 = gst_element_get_pad_template (src, (gchar*)a->data))) {
292       /* g_print ("have padtemplate %s, SOMETIMES=%s\n", pt1->name_template, pt1->presence == GST_PAD_SOMETIMES ? "TRUE" : "FALSE"); */
293         if ((p1 = gst_element_get_pad (src, (gchar*)a->data)) || pt1->presence == GST_PAD_SOMETIMES) {
294           if (!p1) {
295             /* sigh, a hack until i fix the gstelement api... */
296             if ((pt2 = gst_element_get_compatible_pad_template (sink, pt1))) {
297               /* g_print ("have compatible pad template %s\n", pt2->name_template); */
298               if ((p2 = gst_element_get_pad (sink, pt2->name_template))) {
299                 /*  g_print ("got the pad\n"); */
300                 dc = g_new0 (dynamic_connection_t, 1);
301                 dc->srcpadname = (gchar*)a->data;
302                 dc->target_pad = p2;
303                 dc->target_element = NULL;
304                 dc->pipeline = g->bin;
305               
306                 GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s and %s:%s",
307                            GST_OBJECT_NAME (GST_OBJECT (src)),
308                            (gchar*)a->data, GST_DEBUG_PAD_NAME (p2));
309               
310                 g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
311                 goto next;
312               } else {
313                 /* both pt1 and pt2 are sometimes templates. sheesh. */
314                 goto both_templates_have_sometimes_presence;
315               }
316             } else {
317               /* if the target pad has no padtemplate we will figure out a target 
318                * pad later on */
319               dc = g_new0 (dynamic_connection_t, 1);
320               dc->srcpadname = NULL;
321               dc->target_pad = NULL;
322               dc->target_element = sink;
323               dc->pipeline = g->bin;
324               
325               GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s, and some pad in %s",
326                            GST_OBJECT_NAME (GST_OBJECT (src)),
327                            (gchar*)a->data, GST_OBJECT_NAME (sink));
328               
329               g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
330               goto next;
331             }
332           } else {
333             goto could_not_get_compatible_to_a;
334           }
335         } else {
336           goto could_not_get_pad_a;
337         }
338       } else {
339         goto could_not_get_pad_a;
340       }
341       
342       if (!gst_pad_connect (p1, p2)) {
343         goto could_not_connect_pads;
344       }
345     } else if (b) {
346       /* we don't support dynamic connections on this side yet, if ever */
347       if (!(p2 = gst_element_get_pad (sink, (gchar*)b->data))) {
348         goto could_not_get_pad_b;
349       }
350       if (!(p1 = gst_element_get_compatible_pad (src, p2))) {
351         goto could_not_get_compatible_to_b;
352       }
353       if (!gst_pad_connect (p1, p2)) {
354         goto could_not_connect_pads;
355       }
356     } else {
357       if (!gst_element_connect (src, sink)) {
358         goto could_not_connect_elements;
359       }
360     }
361 next:
362     l = g_list_next (l);
363   }
364   
365   l = g->bins;
366   while (l) {
367     if (!make_connections ((graph_t*)l->data, error))
368       return FALSE;
369     l = g_list_next (l);
370   }
371   
372   return TRUE;
373
374 could_not_get_pad_a:
375   g_set_error (error,
376                GST_PARSE_ERROR,
377                GST_PARSE_ERROR_CONNECT,
378                "Could not get a pad %s from element %s",
379                (gchar*)a->data, GST_OBJECT_NAME (src));
380   return FALSE;
381 could_not_get_pad_b:
382   g_set_error (error,
383                GST_PARSE_ERROR,
384                GST_PARSE_ERROR_CONNECT,
385                "Could not get a pad %s from element %s",
386                (gchar*)b->data, GST_OBJECT_NAME (sink));
387   return FALSE;
388 could_not_get_compatible_to_a:
389   g_set_error (error,
390                GST_PARSE_ERROR,
391                GST_PARSE_ERROR_CONNECT,
392                "Could not find a compatible pad in element %s to for %s:%s",
393                GST_OBJECT_NAME (sink), GST_OBJECT_NAME (src), (gchar*)a->data);
394   return FALSE;
395 could_not_get_compatible_to_b:
396   g_set_error (error,
397                GST_PARSE_ERROR,
398                GST_PARSE_ERROR_CONNECT,
399                "Could not find a compatible pad in element %s to for %s:%s",
400                GST_OBJECT_NAME (src), GST_OBJECT_NAME (sink), (gchar*)b->data);
401   return FALSE;
402 both_templates_have_sometimes_presence:
403   g_set_error (error,
404                GST_PARSE_ERROR,
405                GST_PARSE_ERROR_CONNECT,
406                "Both %s:%s and %s:%s have GST_PAD_SOMETIMES presence, operation not supported",
407                GST_OBJECT_NAME (src), pt1->name_template, GST_OBJECT_NAME (sink), pt2->name_template);
408   return FALSE;
409 could_not_connect_pads:
410   g_set_error (error,
411                GST_PARSE_ERROR,
412                GST_PARSE_ERROR_CONNECT,
413                "Could not connect %s:%s to %s:%s",
414                GST_DEBUG_PAD_NAME (p1),
415                GST_DEBUG_PAD_NAME (p2));
416   return FALSE;
417 could_not_connect_elements:
418   g_set_error (error,
419                GST_PARSE_ERROR,
420                GST_PARSE_ERROR_CONNECT,
421                "Could not connect element %s to %s",
422                GST_OBJECT_NAME (src),
423                GST_OBJECT_NAME (sink));
424   return FALSE;
425 }
426
427 static GstBin*
428 pipeline_from_graph (graph_t *g, GError **error)
429 {
430   if (!make_elements (g, error))
431     return NULL;
432   
433   if (!set_properties (g, error))
434     return NULL;
435   
436   if (!make_connections (g, error))
437     return NULL;
438   
439   return (GstBin*)g->bin;
440 }
441
442 /**
443  * gst_parse_launchv:
444  * @argv: null-terminated array of arguments
445  *
446  * Create a new pipeline based on command line syntax.
447  *
448  * Returns: a new pipeline on success, NULL on failure
449  */
450 GstBin *
451 gst_parse_launchv (const gchar **argv, GError **error)
452 {
453   GstBin *pipeline;
454   GString *str;
455   const gchar **argvp, *arg;
456   gchar *tmp;
457
458   /* let's give it a nice size. */
459   str = g_string_sized_new (1024);
460
461   argvp = argv;
462   while (*argvp) {
463     arg = *argvp;
464     tmp = _gst_parse_escape (arg);
465     g_string_append (str, tmp);
466     g_free (tmp);
467     g_string_append (str, " ");
468     argvp++;
469   }
470   
471   pipeline = gst_parse_launch (str->str, error);
472
473   g_string_free (str, TRUE);
474
475   return pipeline;
476 }
477
478 gchar *_gst_parse_escape (const gchar *str)
479 {
480   GString *gstr = NULL;
481   
482   g_return_val_if_fail (str != NULL, NULL);
483   
484   gstr = g_string_sized_new (strlen (str));
485   
486   while (*str) {
487     if (*str == ' ')
488       g_string_append_c (gstr, '\\');
489     g_string_append_c (gstr, *str);
490     str++;
491   }
492   
493   return gstr->str;
494 }
495
496 void _gst_parse_unescape (gchar *str)
497 {
498   gchar *walk;
499   
500   g_return_if_fail (str != NULL);
501   
502   walk = str;
503   
504   while (*walk) {
505     if (*walk == '\\')
506       walk++;
507     *str = *walk;
508     str++;
509     walk++;
510   }
511   *str = '\0';
512 }
513
514 /**
515  * gst_parse_launch:
516  * @pipeline_description: the command line describing the pipeline
517  *
518  * Create a new pipeline based on command line syntax.
519  *
520  * Returns: a new bin on success, NULL on failure. By default the bin is
521  * a GstPipeline, but it depends on the pipeline_description.
522  */
523 GstBin *
524 gst_parse_launch (const gchar * pipeline_description, GError **error)
525 {
526   graph_t *graph;
527   static GStaticMutex flex_lock = G_STATIC_MUTEX_INIT;
528
529   g_return_val_if_fail (pipeline_description != NULL, NULL);
530
531   GST_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
532             pipeline_description);
533
534   /* the need for the mutex will go away with flex 2.5.6 */
535   g_static_mutex_lock (&flex_lock);
536   graph = _gst_parse_launch (pipeline_description, error);
537   g_static_mutex_unlock (&flex_lock);
538
539   if (!graph)
540     return NULL;
541   
542   return pipeline_from_graph (graph, error);
543 }