feeble attempts to handle dynamic connections
[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     if (a && b) {
239       /* balanced multipad connection */
240       while (a && b) {
241         p1 = gst_element_get_pad (src, (gchar*)a->data);
242         p2 = gst_element_get_pad (sink, (gchar*)b->data);
243
244         if (!p2)
245           goto could_not_get_pad_b;
246         
247         if (!p1 && p2 && (pt1 = gst_element_get_pad_template (src, (gchar*)a->data)) &&
248             pt1->presence == GST_PAD_SOMETIMES) {
249           dc = g_new0 (dynamic_connection_t, 1);
250           dc->srcpadname = (gchar*)a->data;
251           dc->target = p2;
252           dc->pipeline = g->bin;
253           
254           GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s and %s:%s",
255                      GST_OBJECT_NAME (GST_OBJECT (src)),
256                      (gchar*)a->data, GST_DEBUG_PAD_NAME (p2));
257           
258           g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
259         } else if (!p1) {
260           goto could_not_get_pad_a;
261         } else if (!gst_pad_connect (p1, p2)) {
262           goto could_not_connect_pads;
263         }
264         a = g_list_next (a);
265         b = g_list_next (b);
266       }
267     } else if (a) {
268       if ((pt1 = gst_element_get_pad_template (src, (gchar*)a->data))) {
269         if ((p1 = gst_element_get_pad (src, (gchar*)a->data)) || pt1->presence == GST_PAD_SOMETIMES) {
270           if (!p1) {
271             /* sigh, a hack until i fix the gstelement api... */
272             if ((pt2 = gst_element_get_compatible_pad_template (sink, pt1))) {
273               if ((p2 = gst_element_get_pad (sink, pt2->name_template))) {
274                 dc = g_new0 (dynamic_connection_t, 1);
275                 dc->srcpadname = (gchar*)a->data;
276                 dc->target = p2;
277                 dc->pipeline = g->bin;
278               
279                 GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s and %s:%s",
280                            GST_OBJECT_NAME (GST_OBJECT (src)),
281                            (gchar*)a->data, GST_DEBUG_PAD_NAME (p2));
282               
283                 g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
284               } else {
285                 /* both pt1 and pt2 are sometimes templates. sheesh. */
286                 goto both_templates_have_sometimes_presence;
287               }
288             } else {
289               goto could_not_get_compatible_to_a;
290             }
291           } else {
292             if (!(p2 = gst_element_get_compatible_pad (sink, p1))) {
293               goto could_not_get_compatible_to_a;
294             }
295           }
296         } else {
297           goto could_not_get_pad_a;
298         }
299       } else {
300         goto could_not_get_pad_a;
301       }
302       
303       if (!gst_pad_connect (p1, p2)) {
304         goto could_not_connect_pads;
305       }
306     } else if (b) {
307       /* we don't support dynamic connections on this side yet, if ever */
308       if (!(p2 = gst_element_get_pad (sink, (gchar*)b->data))) {
309         goto could_not_get_pad_b;
310       }
311       if (!(p1 = gst_element_get_compatible_pad (src, p2))) {
312         goto could_not_get_compatible_to_b;
313       }
314       if (!gst_pad_connect (p1, p2)) {
315         goto could_not_connect_pads;
316       }
317     } else {
318       if (!gst_element_connect (src, sink)) {
319         goto could_not_connect_elements;
320       }
321     }
322     l = g_list_next (l);
323   }
324   
325   l = g->bins;
326   while (l) {
327     if (!make_connections ((graph_t*)l->data, error))
328       return FALSE;
329     l = g_list_next (l);
330   }
331   
332   return TRUE;
333
334 could_not_get_pad_a:
335   g_set_error (error,
336                GST_PARSE_ERROR,
337                GST_PARSE_ERROR_CONNECT,
338                "Could not get a pad %s from element %s",
339                (gchar*)a->data, GST_OBJECT_NAME (src));
340   return FALSE;
341 could_not_get_pad_b:
342   g_set_error (error,
343                GST_PARSE_ERROR,
344                GST_PARSE_ERROR_CONNECT,
345                "Could not get a pad %s from element %s",
346                (gchar*)b->data, GST_OBJECT_NAME (sink));
347   return FALSE;
348 could_not_get_compatible_to_a:
349   g_set_error (error,
350                GST_PARSE_ERROR,
351                GST_PARSE_ERROR_CONNECT,
352                "Could not find a compatible pad in element %s to for %s:%s",
353                GST_OBJECT_NAME (sink), GST_OBJECT_NAME (src), (gchar*)a->data);
354   return FALSE;
355 could_not_get_compatible_to_b:
356   g_set_error (error,
357                GST_PARSE_ERROR,
358                GST_PARSE_ERROR_CONNECT,
359                "Could not find a compatible pad in element %s to for %s:%s",
360                GST_OBJECT_NAME (src), GST_OBJECT_NAME (sink), (gchar*)b->data);
361   return FALSE;
362 both_templates_have_sometimes_presence:
363   g_set_error (error,
364                GST_PARSE_ERROR,
365                GST_PARSE_ERROR_CONNECT,
366                "Both %s:%s and %s:%s have GST_PAD_SOMETIMES presence, operation not supported",
367                GST_OBJECT_NAME (src), pt1->name_template, GST_OBJECT_NAME (sink), pt2->name_template);
368   return FALSE;
369 could_not_connect_pads:
370   g_set_error (error,
371                GST_PARSE_ERROR,
372                GST_PARSE_ERROR_CONNECT,
373                "Could not connect %s:%s to %s:%s",
374                GST_DEBUG_PAD_NAME (p1),
375                GST_DEBUG_PAD_NAME (p2));
376   return FALSE;
377 could_not_connect_elements:
378   g_set_error (error,
379                GST_PARSE_ERROR,
380                GST_PARSE_ERROR_CONNECT,
381                "Could not connect element %s to %s",
382                GST_OBJECT_NAME (src),
383                GST_OBJECT_NAME (sink));
384   return FALSE;
385 }
386
387 static GstBin*
388 pipeline_from_graph (graph_t *g, GError **error)
389 {
390   if (!make_elements (g, error))
391     return NULL;
392   
393   if (!set_properties (g, error))
394     return NULL;
395   
396   if (!make_connections (g, error))
397     return NULL;
398   
399   return (GstBin*)g->bin;
400 }
401
402 /**
403  * gst_parse_launchv:
404  * @argv: null-terminated array of arguments
405  *
406  * Create a new pipeline based on command line syntax.
407  *
408  * Returns: a new pipeline on success, NULL on failure
409  */
410 GstBin *
411 gst_parse_launchv (const gchar **argv, GError **error)
412 {
413   GstBin *pipeline;
414   gchar *pipeline_description;
415
416   /* i think this cast works out ok... */
417   pipeline_description = g_strjoinv (" ", (gchar**)argv);
418   
419   pipeline = gst_parse_launch (pipeline_description, error);
420
421   return pipeline;
422 }
423
424 /**
425  * gst_parse_launch:
426  * @pipeline_description: the command line describing the pipeline
427  *
428  * Create a new pipeline based on command line syntax.
429  *
430  * Returns: a new bin on success, NULL on failure. By default the bin is
431  * a GstPipeline, but it depends on the pipeline_description.
432  */
433 GstBin *
434 gst_parse_launch (const gchar * pipeline_description, GError **error)
435 {
436   graph_t *graph;
437   static GStaticMutex flex_lock = G_STATIC_MUTEX_INIT;
438
439   g_return_val_if_fail (pipeline_description != NULL, NULL);
440
441   GST_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
442             pipeline_description);
443
444   /* the need for the mutex will go away with flex 2.5.6 */
445   g_static_mutex_lock (&flex_lock);
446   graph = _gst_parse_launch (pipeline_description, error);
447   g_static_mutex_unlock (&flex_lock);
448
449   if (!graph)
450     return NULL;
451   
452   return pipeline_from_graph (graph, error);
453 }