commit to make gstreamer follow the gtk function/macro naming conventions:
[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   GstElement *src, *sink;
202   GstPad *p1, *p2;
203   GstPadTemplate *pt1;
204   
205   l = g->connections;
206   while (l) {
207     c = (connection_t*)l->data;
208     if (c->src_name) {
209       if (!(src = gst_bin_get_by_name (GST_BIN (g->bin), c->src_name))) {
210         g_set_error (error,
211                      GST_PARSE_ERROR,
212                      GST_PARSE_ERROR_NO_SUCH_ELEMENT,
213                      "No such element '%s'",
214                      c->src_name);
215         return FALSE;
216       }
217     } else {
218       src = find_element_by_index (g, c->src_index);
219       g_assert (src);
220     }
221     if (c->sink_name) {
222       if (!(sink = gst_bin_get_by_name (GST_BIN (g->bin), c->sink_name))) {
223         g_set_error (error,
224                      GST_PARSE_ERROR,
225                      GST_PARSE_ERROR_NO_SUCH_ELEMENT,
226                      "No such element '%s'",
227                      c->sink_name);
228         return FALSE;
229       }
230     } else {
231       sink = find_element_by_index (g, c->sink_index);
232       g_assert (sink);
233     }
234     
235     a = c->src_pads;
236     b = c->sink_pads;
237     if (a && b) {
238       /* balanced multipad connection */
239       while (a && b) {
240         p1 = gst_element_get_pad (src, (gchar*)a->data);
241         p2 = gst_element_get_pad (sink, (gchar*)b->data);
242         
243         if (!p1 && p2 && (pt1 = gst_element_get_pad_template (src, (gchar*)a->data)) &&
244             pt1->presence == GST_PAD_SOMETIMES) {
245           dynamic_connection_t *dc = g_new0 (dynamic_connection_t, 1);
246           dc->srcpadname = (gchar*)a->data;
247           dc->target = p2;
248           dc->pipeline = g->bin;
249           
250           GST_DEBUG (GST_CAT_PIPELINE, "setting up dynamic connection %s:%s and %s:%s",
251                      GST_OBJECT_NAME (GST_OBJECT (src)),
252                      (gchar*)a->data, GST_DEBUG_PAD_NAME (p2));
253           
254           g_signal_connect (G_OBJECT (src), "new_pad", G_CALLBACK (dynamic_connect), dc);
255         } else if (!p1 || !p2 || !gst_pad_connect (p1, p2)) {
256           g_set_error (error,
257                        GST_PARSE_ERROR,
258                        GST_PARSE_ERROR_CONNECT,
259                        "Could not connect %s:%s to %s:%s",
260                        GST_OBJECT_NAME (src), (gchar*)a->data,
261                        GST_OBJECT_NAME (sink), (gchar*)b->data);
262           return FALSE;
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 /*
272           if ((p2 = gst_element_get_pad (sink, (gchar*)a->data))) {
273             dynamic_connection_t *dc = g_new0 (dynamic_connection_t, 1);
274             dc->srcpadname = (gchar*)a->data;
275             dc->target = p2;
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 */
284           }
285         } else {
286           g_set_error (error,
287                        GST_PARSE_ERROR,
288                        GST_PARSE_ERROR_CONNECT,
289                        "Could not get a pad %s from element %s",
290                        (gchar*)a->data, GST_OBJECT_NAME (src));
291           return FALSE;
292         }
293       } else {
294         g_set_error (error,
295                      GST_PARSE_ERROR,
296                      GST_PARSE_ERROR_CONNECT,
297                      "Could not get a pad %s from element %s",
298                      (gchar*)a->data, GST_OBJECT_NAME (src));
299         return FALSE;
300       }
301       
302       if (!(p2 = gst_element_get_compatible_pad (sink, p1))) {
303         g_set_error (error,
304                      GST_PARSE_ERROR,
305                      GST_PARSE_ERROR_CONNECT,
306                      "Could not find a compatible pad in element %s to for %s:%s",
307                      GST_OBJECT_NAME (sink), GST_OBJECT_NAME (src), (gchar*)a->data);
308         return FALSE;
309       }
310       if (!gst_pad_connect (p1, p2)) {
311         g_set_error (error,
312                      GST_PARSE_ERROR,
313                      GST_PARSE_ERROR_CONNECT,
314                      "Could not connect %s:%s to %s:%s",
315                      GST_OBJECT_NAME (src), GST_OBJECT_NAME (p1),
316                      GST_OBJECT_NAME (sink), GST_OBJECT_NAME (p1));
317         return FALSE;
318       }
319     } else if (b) {
320       if (!(p2 = gst_element_get_pad (sink, (gchar*)b->data))) {
321         g_set_error (error,
322                      GST_PARSE_ERROR,
323                      GST_PARSE_ERROR_CONNECT,
324                      "Could not get a pad %s from element %s",
325                      (gchar*)b->data, GST_OBJECT_NAME (sink));
326         return FALSE;
327       }
328       if (!(p1 = gst_element_get_compatible_pad (src, p2))) {
329         g_set_error (error,
330                      GST_PARSE_ERROR,
331                      GST_PARSE_ERROR_CONNECT,
332                      "Could not find a compatible pad in element %s to for %s:%s",
333                      GST_OBJECT_NAME (src), GST_OBJECT_NAME (sink), (gchar*)b->data);
334         return FALSE;
335       }
336       if (!gst_pad_connect (p1, p2)) {
337         g_set_error (error,
338                      GST_PARSE_ERROR,
339                      GST_PARSE_ERROR_CONNECT,
340                      "Could not connect %s:%s to %s:%s",
341                      GST_OBJECT_NAME (src), GST_OBJECT_NAME (p1),
342                      GST_OBJECT_NAME (sink), GST_OBJECT_NAME (p1));
343         return FALSE;
344       }
345     } else {
346       if (!gst_element_connect (src, sink)) {
347         g_set_error (error,
348                      GST_PARSE_ERROR,
349                      GST_PARSE_ERROR_CONNECT,
350                      "Could not connect %s to %s",
351                      GST_OBJECT_NAME (src), GST_OBJECT_NAME (sink));
352         return FALSE;
353       }
354     }
355     l = g_list_next (l);
356   }
357   
358   l = g->bins;
359   while (l) {
360     if (!make_connections ((graph_t*)l->data, error))
361       return FALSE;
362     l = g_list_next (l);
363   }
364   
365   return TRUE;
366 }
367
368 static GstBin*
369 pipeline_from_graph (graph_t *g, GError **error)
370 {
371   if (!make_elements (g, error))
372     return NULL;
373   
374   if (!set_properties (g, error))
375     return NULL;
376   
377   if (!make_connections (g, error))
378     return NULL;
379   
380   return (GstBin*)g->bin;
381 }
382
383 /**
384  * gst_parse_launchv:
385  * @argv: null-terminated array of arguments
386  *
387  * Create a new pipeline based on command line syntax.
388  *
389  * Returns: a new pipeline on success, NULL on failure
390  */
391 GstBin *
392 gst_parse_launchv (const gchar **argv, GError **error)
393 {
394   GstBin *pipeline;
395   gchar *pipeline_description;
396
397   /* i think this cast works out ok... */
398   pipeline_description = g_strjoinv (" ", (gchar**)argv);
399   
400   pipeline = gst_parse_launch (pipeline_description, error);
401
402   return pipeline;
403 }
404
405 /**
406  * gst_parse_launch:
407  * @pipeline_description: the command line describing the pipeline
408  *
409  * Create a new pipeline based on command line syntax.
410  *
411  * Returns: a new bin on success, NULL on failure. By default the bin is
412  * a GstPipeline, but it depends on the pipeline_description.
413  */
414 GstBin *
415 gst_parse_launch (const gchar * pipeline_description, GError **error)
416 {
417   graph_t *graph;
418   static GStaticMutex flex_lock = G_STATIC_MUTEX_INIT;
419
420   g_return_val_if_fail (pipeline_description != NULL, NULL);
421
422   GST_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
423             pipeline_description);
424
425   /* the need for the mutex will go away with flex 2.5.6 */
426   g_static_mutex_lock (&flex_lock);
427   graph = _gst_parse_launch (pipeline_description, error);
428   g_static_mutex_unlock (&flex_lock);
429
430   if (!graph)
431     return NULL;
432   
433   return pipeline_from_graph (graph, error);
434 }