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