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