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