gst/gstelement.h (GstState): Renamed from GstElementState, changed to be a normal...
[platform/upstream/gstreamer.git] / gst / parse / grammar.y
1 %{
2 #include <glib-object.h>
3 #include <glib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7
8 #include "../gst_private.h"
9 #include "../gst-i18n-lib.h"
10
11 #include "../gstconfig.h"
12 #include "../gstparse.h"
13 #include "../gstinfo.h"
14 #include "../gsterror.h"
15 #include "../gsturi.h"
16 #include "../gstutils.h"
17 #include "../gstvalue.h"
18 #include "../gstchildproxy.h"
19 #include "types.h"
20
21 /* All error messages in this file are user-visible and need to be translated.
22  * Don't start the message with a capital, and don't end them with a period,
23  * as they will be presented inside a sentence/error.
24  */
25   
26 #define YYERROR_VERBOSE 1
27 #define YYPARSE_PARAM graph
28
29 #ifdef __GST_PARSE_TRACE
30 static guint __strings;
31 static guint __links;
32 static guint __chains;
33 gchar *
34 __gst_parse_strdup (gchar *org)
35 {
36   gchar *ret; 
37   __strings++;
38   ret = g_strdup (org);
39   /* g_print ("ALLOCATED STR   (%3u): %p %s\n", __strings, ret, ret); */
40   return ret;
41 }
42 void
43 __gst_parse_strfree (gchar *str)
44 {
45   if (str) {
46     /* g_print ("FREEING STR     (%3u): %p %s\n", __strings - 1, str, str); */
47     g_free (str);
48     g_return_if_fail (__strings > 0);
49     __strings--;
50   }
51 }
52 link_t *__gst_parse_link_new ()
53 {
54   link_t *ret;
55   __links++;
56   ret = g_new0 (link_t, 1);
57   /* g_print ("ALLOCATED LINK  (%3u): %p\n", __links, ret); */
58   return ret;
59 }
60 void
61 __gst_parse_link_free (link_t *data)
62 {
63   if (data) {
64     /* g_print ("FREEING LINK    (%3u): %p\n", __links - 1, data); */
65     g_free (data);
66     g_return_if_fail (__links > 0);
67     __links--;
68   }
69 }
70 chain_t *
71 __gst_parse_chain_new ()
72 {
73   chain_t *ret;
74   __chains++;
75   ret = g_new0 (chain_t, 1);
76   /* g_print ("ALLOCATED CHAIN (%3u): %p\n", __chains, ret); */
77   return ret;
78 }
79 void
80 __gst_parse_chain_free (chain_t *data)
81 {
82   /* g_print ("FREEING CHAIN   (%3u): %p\n", __chains - 1, data); */
83   g_free (data);
84   g_return_if_fail (__chains > 0);
85   __chains--;
86 }
87
88 #endif /* __GST_PARSE_TRACE */
89
90 typedef struct {
91   gchar *src_pad;
92   gchar *sink_pad;
93   GstElement *sink;
94   GstCaps *caps;
95   gulong signal_id;
96   /* FIXME: need to connect to "disposed" signal to clean up, but there is no such signal */
97 } DelayedLink;
98
99 #ifdef G_HAVE_ISO_VARARGS
100 #define SET_ERROR(error, type, ...) G_STMT_START{ \
101   GST_CAT_ERROR (GST_CAT_PIPELINE, __VA_ARGS__); \
102   if ((error) && !*(error)) { \
103     g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
104   } \
105 }G_STMT_END
106 #define ERROR(type, ...) SET_ERROR (((graph_t *) graph)->error, (type), __VA_ARGS__ )
107 #ifndef GST_DISABLE_GST_DEBUG
108 #  define YYDEBUG 1
109    /* bison 1.35 calls this macro with side effects, we need to make sure the
110       side effects work - crappy bison
111 #  define YYFPRINTF(a, ...) GST_CAT_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__)
112  */
113 #  define YYFPRINTF(a, ...) G_STMT_START{ \
114      gchar *temp = g_strdup_printf (__VA_ARGS__); \
115      GST_CAT_LOG (GST_CAT_PIPELINE, temp); \
116      g_free (temp); \
117    }G_STMT_END
118 #endif
119
120 #elif defined(G_HAVE_GNUC_VARARGS)
121
122 #define SET_ERROR(error, type, args...) G_STMT_START{ \
123   GST_CAT_ERROR (GST_CAT_PIPELINE, args ); \
124   if ((error) && !*(error)) { \
125     g_set_error ((error), GST_PARSE_ERROR, (type), args ); \
126   } \
127 }G_STMT_END
128 #define ERROR(type, args...) SET_ERROR (((graph_t *) graph)->error, (type) , args )
129 #ifndef GST_DISABLE_GST_DEBUG
130 #  define YYDEBUG 1
131    /* bison 1.35 calls this macro with side effects, we need to make sure the
132       side effects work - crappy bison
133 #  define YYFPRINTF(a, args...) GST_CAT_DEBUG (GST_CAT_PIPELINE, args )
134  */
135 #  define YYFPRINTF(a, args...) G_STMT_START{ \
136      gchar *temp = g_strdup_printf ( args ); \
137      GST_CAT_LOG (GST_CAT_PIPELINE, temp); \
138      g_free (temp); \
139    }G_STMT_END
140 #endif
141
142 #else
143
144 static inline void
145 SET_ERROR (GError **error, gint type, const char *format, ...)
146 {
147   if (error) {
148     if (*error) {
149       g_warning ("error while parsing");
150     } else {
151       va_list varargs;
152       char *string;
153
154       va_start (varargs, format);
155       string = g_strdup_vprintf (format, varargs);
156       va_end (varargs);
157       
158       g_set_error (error, GST_PARSE_ERROR, type, string);
159
160       g_free (string);
161     }
162   }
163 }
164
165 #ifndef GST_DISABLE_GST_DEBUG
166 #  define YYDEBUG 1
167 static inline void
168 YYPRINTF(const char *format, ...)
169 {
170   va_list varargs;
171   gchar *temp;
172   
173   va_start (varargs, format);
174   temp = g_strdup_vprintf ( format, varargs );
175   GST_CAT_LOG (GST_CAT_PIPELINE, "%s", temp);
176   g_free (temp);
177   va_end (varargs);
178 }
179 #endif
180
181 #endif
182
183 #define GST_BIN_MAKE(res, type, chainval, assign) G_STMT_START{ \
184   chain_t *chain = chainval; \
185   GSList *walk; \
186   GstBin *bin = (GstBin *) gst_element_factory_make (type, NULL); \
187   if (!chain) { \
188     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_EMPTY_BIN, _("specified empty bin \"%s\", not allowed"), type); \
189     g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
190     g_slist_free (assign); \
191     YYERROR; \
192   } else if (!bin) { \
193     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, _("no bin \"%s\", skipping"), type); \
194     g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
195     g_slist_free (assign); \
196     res = chain; \
197   } else { \
198     for (walk = chain->elements; walk; walk = walk->next ) \
199       gst_bin_add (bin, GST_ELEMENT (walk->data)); \
200     g_slist_free (chain->elements); \
201     chain->elements = g_slist_prepend (NULL, bin); \
202     res = chain; \
203     /* set the properties now */ \
204     for (walk = assign; walk; walk = walk->next) \
205       gst_parse_element_set ((gchar *) walk->data, GST_ELEMENT (bin), graph); \
206     g_slist_free (assign); \
207   } \
208 }G_STMT_END
209
210 #define MAKE_LINK(link, _src, _src_name, _src_pads, _sink, _sink_name, _sink_pads) G_STMT_START{ \
211   link = gst_parse_link_new (); \
212   link->src = _src; \
213   link->sink = _sink; \
214   link->src_name = _src_name; \
215   link->sink_name = _sink_name; \
216   link->src_pads = _src_pads; \
217   link->sink_pads = _sink_pads; \
218   link->caps = NULL; \
219 }G_STMT_END
220
221 #define MAKE_REF(link, _src, _pads) G_STMT_START{ \
222   gchar *padname = _src; \
223   GSList *pads = _pads; \
224   if (padname) { \
225     while (*padname != '.') padname++; \
226     *padname = '\0'; \
227     padname++; \
228     if (*padname != '\0') \
229       pads = g_slist_prepend (pads, gst_parse_strdup (padname)); \
230   } \
231   MAKE_LINK (link, NULL, _src, pads, NULL, NULL, NULL); \
232 }G_STMT_END
233
234 static void
235 gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
236 {
237   GParamSpec *pspec;
238   gchar *pos = value;
239   GValue v = { 0, }; 
240   GValue v2 = { 0, };
241   GstObject *target;
242   GType value_type;
243
244   /* parse the string, so the property name is null-terminated an pos points
245      to the beginning of the value */
246   while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++; 
247   if (*pos == '=') { 
248     *pos = '\0'; 
249   } else { 
250     *pos = '\0'; 
251     pos++;
252     while (g_ascii_isspace (*pos)) pos++; 
253   } 
254   pos++; 
255   while (g_ascii_isspace (*pos)) pos++; 
256   if (*pos == '"') {
257     pos++;
258     pos[strlen (pos) - 1] = '\0';
259   }
260   gst_parse_unescape (pos);
261
262   if (gst_child_proxy_lookup (GST_OBJECT (element), value, &target, &pspec)) { 
263     value_type = G_PARAM_SPEC_VALUE_TYPE (pspec); 
264     GST_LOG ("parsing property %s as a %s", pspec->name,
265       g_type_name (value_type));
266     g_value_init (&v, value_type);
267     if (!gst_value_deserialize (&v, pos))
268       goto error;
269     g_object_set_property (G_OBJECT (target), pspec->name, &v);
270   } else { 
271     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, _("no property \"%s\" in element \"%s\""), value, GST_ELEMENT_NAME (element)); 
272   }
273
274 out:
275   gst_parse_strfree (value);
276   if (G_IS_VALUE (&v))
277     g_value_unset (&v);
278   if (G_IS_VALUE (&v2))
279     g_value_unset (&v2);
280   return;
281   
282 error:
283   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
284          _("could not set property \"%s\" in element \"%s\" to \"%s\""), 
285          value, GST_ELEMENT_NAME (element), pos); 
286   goto out;
287 }
288 static inline void
289 gst_parse_free_link (link_t *link)
290 {
291   gst_parse_strfree (link->src_name);
292   gst_parse_strfree (link->sink_name);
293   g_slist_foreach (link->src_pads, (GFunc) gst_parse_strfree, NULL);
294   g_slist_foreach (link->sink_pads, (GFunc) gst_parse_strfree, NULL);
295   g_slist_free (link->src_pads);
296   g_slist_free (link->sink_pads);
297   if (link->caps) gst_caps_unref (link->caps);
298   gst_parse_link_free (link);  
299 }
300
301 static void
302 gst_parse_element_lock (GstElement *element, gboolean lock)
303 {
304   GstPad *pad;
305   GstIterator *pads;
306   gboolean unlocked_peer = FALSE;
307   gboolean done = FALSE;
308   GList *walk;
309   
310   if (gst_element_is_locked_state (element) == lock)
311     return;
312
313   return;
314
315   /* check if we have an unlocked peer */
316   pads = gst_element_iterate_pads (element);
317   while (!done) {
318     gpointer data;
319     switch (gst_iterator_next (pads, &data)) {
320       case GST_ITERATOR_OK:
321       {
322         GstPad *pad = GST_PAD_CAST (data);
323
324         if (GST_PAD_IS_SINK (pad) && GST_PAD_PEER (pad) &&
325             !gst_element_is_locked_state (GST_PAD_PARENT (GST_PAD_PEER (pad)))) {
326           unlocked_peer = TRUE;
327           done = TRUE;
328         }
329         gst_object_unref (GST_OBJECT (pad));
330         break;
331       }
332       case GST_ITERATOR_RESYNC:
333         unlocked_peer = FALSE;
334         gst_iterator_resync (pads);
335         break;
336       case GST_ITERATOR_DONE:
337         done = TRUE;
338         break;
339       default:
340         g_assert_not_reached ();
341         break;
342     }
343   }
344   gst_iterator_free (pads);
345
346   if (!(lock && unlocked_peer)) {
347     GST_CAT_DEBUG (GST_CAT_PIPELINE, "setting locked state on element");
348     gst_element_set_locked_state (element, lock);
349     if (!lock)
350     {
351       /* try to sync state with parent */
352       GST_CAT_DEBUG (GST_CAT_PIPELINE, "trying to sync state of element with parent");
353       /* FIXME: it would be nice if we can figure out why it failed
354          (e.g. caps nego) and give an error about that instead. */
355       if (gst_element_set_state (element, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
356         GST_ELEMENT_ERROR (element, CORE, STATE_CHANGE, (NULL), (NULL));
357     }
358   } else {
359     return;
360   }
361   
362   /* check if there are other pads to (un)lock */
363   walk = (GList *) element->pads;
364   for  (; walk; walk = walk->next) {
365     pad = GST_PAD_CAST (walk->data);
366     if (GST_PAD_IS_SRC (pad) && GST_PAD_PEER (pad)) {
367       GstElement *next = GST_ELEMENT (GST_OBJECT_PARENT (GST_PAD_PEER (pad)));
368       if (gst_element_is_locked_state (next) != lock)
369         gst_parse_element_lock (next, lock);
370     }
371   }
372 }
373 static void
374 gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
375 {
376   DelayedLink *link = (DelayedLink *) data;
377   
378   GST_CAT_INFO (GST_CAT_PIPELINE, "trying delayed linking %s:%s to %s:%s", 
379                 GST_ELEMENT_NAME (src), link->src_pad,
380                 GST_ELEMENT_NAME (link->sink), link->sink_pad);
381
382   if (gst_element_link_pads_filtered (src, link->src_pad, link->sink, link->sink_pad, link->caps)) {
383     /* do this here, we don't want to get any problems later on when unlocking states */
384     GST_CAT_DEBUG (GST_CAT_PIPELINE, "delayed linking %s:%s to %s:%s worked", 
385                    GST_ELEMENT_NAME (src), link->src_pad,
386                    GST_ELEMENT_NAME (link->sink), link->sink_pad);
387     g_signal_handler_disconnect (src, link->signal_id);
388     g_free (link->src_pad);
389     g_free (link->sink_pad);
390     if (link->caps) gst_caps_unref (link->caps);
391     if (!gst_element_is_locked_state (src))
392       gst_parse_element_lock (link->sink, FALSE);
393     g_free (link);
394   }
395 }
396 /* both padnames and the caps may be NULL */
397 static gboolean
398 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad, 
399                                 GstElement *sink, const gchar *sink_pad, GstCaps *caps)
400 {
401   GList *templs = gst_element_class_get_pad_template_list (GST_ELEMENT_GET_CLASS (src));
402          
403   for (; templs; templs = templs->next) {
404     GstPadTemplate *templ = (GstPadTemplate *) templs->data;
405     if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) && (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
406     {
407       DelayedLink *data = g_new (DelayedLink, 1); 
408       
409       /* TODO: maybe we should check if src_pad matches this template's names */
410
411       GST_CAT_DEBUG (GST_CAT_PIPELINE, "trying delayed link %s:%s to %s:%s", 
412                      GST_ELEMENT_NAME (src), src_pad, GST_ELEMENT_NAME (sink), sink_pad);
413
414       data->src_pad = g_strdup (src_pad);
415       data->sink = sink;
416       data->sink_pad = g_strdup (sink_pad);
417       if (caps) {
418         data->caps = gst_caps_copy (caps);
419       } else {
420         data->caps = NULL;
421       }
422       data->signal_id = g_signal_connect (G_OBJECT (src), "pad-added", 
423                                           G_CALLBACK (gst_parse_found_pad), data);
424       return TRUE;
425     }
426   }
427   return FALSE;
428 }
429 /*
430  * performs a link and frees the struct. src and sink elements must be given
431  * return values   0 - link performed
432  *                 1 - link delayed
433  *                <0 - error
434  */
435 static gint
436 gst_parse_perform_link (link_t *link, graph_t *graph)
437 {
438   GstElement *src = link->src;
439   GstElement *sink = link->sink;
440   GSList *srcs = link->src_pads;
441   GSList *sinks = link->sink_pads;
442   g_assert (GST_IS_ELEMENT (src));
443   g_assert (GST_IS_ELEMENT (sink));
444   
445   GST_CAT_INFO (GST_CAT_PIPELINE, "linking %s:%s to %s:%s (%u/%u) with caps \"%" GST_PTR_FORMAT "\"", 
446                 GST_ELEMENT_NAME (src), link->src_name ? link->src_name : "(any)",
447                 GST_ELEMENT_NAME (sink), link->sink_name ? link->sink_name : "(any)",
448                 g_slist_length (srcs), g_slist_length (sinks),
449                 link->caps);
450
451   if (!srcs || !sinks) {
452     if (gst_element_link_pads_filtered (src, srcs ? (const gchar *) srcs->data : NULL,
453                                         sink, sinks ? (const gchar *) sinks->data : NULL,
454                                         link->caps)) {
455       gst_parse_element_lock (sink, gst_element_is_locked_state (src));
456       goto success;
457     } else {
458       if (gst_parse_perform_delayed_link (src, srcs ? (const gchar *) srcs->data : NULL,
459                                           sink, sinks ? (const gchar *) sinks->data : NULL,
460                                           link->caps)) {
461         gst_parse_element_lock (sink, TRUE);
462         goto success;
463       } else {
464         goto error;
465       }
466     }
467   }
468   if (g_slist_length (link->src_pads) != g_slist_length (link->src_pads)) {
469     goto error;
470   }
471   while (srcs && sinks) {
472     const gchar *src_pad = (const gchar *) srcs->data;
473     const gchar *sink_pad = (const gchar *) sinks->data;
474     srcs = g_slist_next (srcs);
475     sinks = g_slist_next (sinks);
476     if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad, link->caps)) {
477       gst_parse_element_lock (sink, gst_element_is_locked_state (src));
478       continue;
479     } else {
480       if (gst_parse_perform_delayed_link (src, src_pad,
481                                           sink, sink_pad,
482                                           link->caps)) {
483         gst_parse_element_lock (sink, TRUE);
484         continue;
485       } else {
486         goto error;
487       }
488     }
489   }
490   
491 success:
492   gst_parse_free_link (link);
493   return 0;
494   
495 error:
496   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("could not link %s to %s"), GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink));
497   gst_parse_free_link (link);
498   return -1;
499 }
500
501
502 static int yylex (void *lvalp);
503 static int yyerror (const char *s);
504 %}
505
506 %union {
507     gchar *s;
508     chain_t *c;
509     link_t *l;
510     GstElement *e;
511     GSList *p;
512     graph_t *g;
513 }
514
515 %token <s> PARSE_URL
516 %token <s> IDENTIFIER
517 %left <s> REF PADREF BINREF
518 %token <s> ASSIGNMENT
519 %token <s> LINK
520
521 %type <g> graph
522 %type <c> chain bin
523 %type <l> reference
524 %type <l> linkpart link
525 %type <p> linklist
526 %type <e> element 
527 %type <p> padlist pads assignments
528
529 %left '{' '}' '(' ')'
530 %left ','
531 %right '.'
532 %left '!' '='
533
534 %pure_parser
535
536 %start graph
537 %%
538
539 element:        IDENTIFIER                    { $$ = gst_element_factory_make ($1, NULL); 
540                                                 if (!$$)
541                                                   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, _("no element \"%s\""), $1);
542                                                 gst_parse_strfree ($1);
543                                                 if (!$$)
544                                                   YYERROR;
545                                               }
546         |       element ASSIGNMENT            { gst_parse_element_set ($2, $1, graph);
547                                                 $$ = $1;
548                                               }
549         ;
550 assignments:    /* NOP */                     { $$ = NULL; }
551         |       assignments ASSIGNMENT        { $$ = g_slist_prepend ($1, $2); }
552         ;               
553 bin:            '{' assignments chain '}' { GST_BIN_MAKE ($$, "thread", $3, $2); }
554         |       '(' assignments chain ')' { GST_BIN_MAKE ($$, "bin", $3, $2); }
555         |       BINREF assignments chain ')'  { GST_BIN_MAKE ($$, $1, $3, $2); 
556                                                 gst_parse_strfree ($1);
557                                               }
558         |       '{' assignments '}'           { GST_BIN_MAKE ($$, "thread", NULL, $2); }
559         |       '(' assignments '}'           { GST_BIN_MAKE ($$, "thread", NULL, $2); }
560         |       BINREF assignments ')'        { GST_BIN_MAKE ($$, $1, NULL, $2); 
561                                                 gst_parse_strfree ($1);
562                                               }
563         |       '{' assignments error '}'     { GST_BIN_MAKE ($$, "thread", NULL, $2); }
564         |       '(' assignments error '}'     { GST_BIN_MAKE ($$, "thread", NULL, $2); }
565         |       BINREF assignments error ')'  { GST_BIN_MAKE ($$, $1, NULL, $2); 
566                                                 gst_parse_strfree ($1);
567                                               }
568         ;
569         
570 pads:           PADREF                        { $$ = g_slist_prepend (NULL, $1); }
571         |       PADREF padlist                { $$ = $2;
572                                                 $$ = g_slist_prepend ($$, $1);
573                                               }                              
574         ;
575 padlist:        ',' IDENTIFIER                { $$ = g_slist_prepend (NULL, $2); }
576         |       ',' IDENTIFIER padlist        { $$ = g_slist_prepend ($3, $2); }
577         ;
578         
579 reference:      REF                           { MAKE_REF ($$, $1, NULL); }
580         |       REF padlist                   { MAKE_REF ($$, $1, $2); }
581         ;
582
583 linkpart:       reference                     { $$ = $1; }
584         |       pads                          { MAKE_REF ($$, NULL, $1); }
585         |       /* NOP */                     { MAKE_REF ($$, NULL, NULL); }
586         ;
587         
588 link:           linkpart LINK linkpart        { $$ = $1;
589                                                 if ($2) {
590                                                   $$->caps = gst_caps_from_string ($2);
591                                                   if (!$$->caps)
592                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $2);
593                                                   gst_parse_strfree ($2);
594                                                 }
595                                                 $$->sink_name = $3->src_name;
596                                                 $$->sink_pads = $3->src_pads;
597                                                 gst_parse_link_free ($3);
598                                               }
599         ;
600         
601 linklist:       link                          { $$ = g_slist_prepend (NULL, $1); }
602         |       link linklist                 { $$ = g_slist_prepend ($2, $1); }
603         |       linklist error                { $$ = $1; }
604         ;       
605
606 chain:          element                       { $$ = gst_parse_chain_new ();
607                                                 $$->first = $$->last = $1;
608                                                 $$->front = $$->back = NULL;
609                                                 $$->elements = g_slist_prepend (NULL, $1);
610                                               }
611         |       bin                           { $$ = $1; }
612         |       chain chain                   { if ($1->back && $2->front) {
613                                                   if (!$1->back->sink_name) {
614                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without source element"));
615                                                     gst_parse_free_link ($1->back);
616                                                   } else {
617                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
618                                                   }
619                                                   if (!$2->front->src_name) {
620                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
621                                                     gst_parse_free_link ($2->front);
622                                                   } else {
623                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
624                                                   }
625                                                   $1->back = NULL;
626                                                 } else if ($1->back) {
627                                                   if (!$1->back->sink_name) {
628                                                     $1->back->sink = $2->first;
629                                                   }
630                                                 } else if ($2->front) {
631                                                   if (!$2->front->src_name) {
632                                                     $2->front->src = $1->last;
633                                                   }
634                                                   $1->back = $2->front;
635                                                 }
636                                                 
637                                                 if ($1->back) {
638                                                   ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
639                                                 }
640                                                 $1->last = $2->last;
641                                                 $1->back = $2->back;
642                                                 $1->elements = g_slist_concat ($1->elements, $2->elements);
643                                                 if ($2)
644                                                   gst_parse_chain_free ($2);
645                                                 $$ = $1;
646                                               }
647         |       chain linklist                { GSList *walk;
648                                                 if ($1->back) {
649                                                   $2 = g_slist_prepend ($2, $1->back);
650                                                   $1->back = NULL;
651                                                 } else {
652                                                   if (!((link_t *) $2->data)->src_name) {
653                                                     ((link_t *) $2->data)->src = $1->last;
654                                                   }                                               
655                                                 }
656                                                 for (walk = $2; walk; walk = walk->next) {
657                                                   link_t *link = (link_t *) walk->data;
658                                                   if (!link->sink_name && walk->next) {
659                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
660                                                     gst_parse_free_link (link);
661                                                   } else if (!link->src_name && !link->src) {
662                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without source element"));
663                                                     gst_parse_free_link (link);
664                                                   } else {
665                                                     if (walk->next) {
666                                                       ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, link);
667                                                     } else {
668                                                       $1->back = link;
669                                                     }
670                                                   }
671                                                 }
672                                                 g_slist_free ($2);
673                                                 $$ = $1;
674                                               }
675         |       chain error                   { $$ = $1; }
676         |       link chain                    { if ($2->front) {
677                                                   if (!$2->front->src_name) {
678                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without source element"));
679                                                     gst_parse_free_link ($2->front);
680                                                   } else {
681                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
682                                                   }
683                                                 }
684                                                 if (!$1->sink_name) {
685                                                   $1->sink = $2->first;
686                                                 }
687                                                 $2->front = $1;
688                                                 $$ = $2;
689                                               }
690         |       PARSE_URL chain               { $$ = $2;
691                                                 if ($$->front) {
692                                                   GstElement *element = 
693                                                           gst_element_make_from_uri (GST_URI_SRC, $1, NULL);
694                                                   if (!element) {
695                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, 
696                                                             _("no source element for URI \"%s\""), $1);
697                                                   } else {
698                                                     $$->front->src = element;
699                                                     ((graph_t *) graph)->links = g_slist_prepend (
700                                                             ((graph_t *) graph)->links, $$->front);
701                                                     $$->front = NULL;
702                                                     $$->elements = g_slist_prepend ($$->elements, element);
703                                                   }
704                                                 } else {
705                                                   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, 
706                                                           _("no element to link URI \"%s\" to"), $1);
707                                                 }
708                                                 g_free ($1);
709                                               }
710         |       link PARSE_URL                { GstElement *element =
711                                                           gst_element_make_from_uri (GST_URI_SINK, $2, NULL);
712                                                 if (!element) {
713                                                   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, 
714                                                           _("no sink element for URI \"%s\""), $2);
715                                                   YYERROR;
716                                                 } else if ($1->sink_name || $1->sink_pads) {
717                                                   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, 
718                                                           _("could not link sink element for URI \"%s\""), $2);
719                                                   YYERROR;
720                                                 } else {
721                                                   $$ = gst_parse_chain_new ();
722                                                   $$->first = $$->last = element;
723                                                   $$->front = $1;
724                                                   $$->front->sink = element;
725                                                   $$->elements = g_slist_prepend (NULL, element);
726                                                 }
727                                                 g_free ($2);
728                                               }
729         ;
730 graph:          /* NOP */                     { SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
731                                                 $$ = (graph_t *) graph;
732                                               }
733         |       chain                         { $$ = (graph_t *) graph;
734                                                 if ($1->front) {
735                                                   if (!$1->front->src_name) {
736                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without source element"));
737                                                     gst_parse_free_link ($1->front);
738                                                   } else {
739                                                     $$->links = g_slist_prepend ($$->links, $1->front);
740                                                   }
741                                                   $1->front = NULL;
742                                                 }
743                                                 if ($1->back) {
744                                                   if (!$1->back->sink_name) {
745                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
746                                                     gst_parse_free_link ($1->back);
747                                                   } else {
748                                                     $$->links = g_slist_prepend ($$->links, $1->back);
749                                                   }
750                                                   $1->back = NULL;
751                                                 }
752                                                 $$->chain = $1;
753                                               }
754         ;
755
756 %%
757
758 extern FILE *_gst_parse_yyin;
759 int _gst_parse_yylex (YYSTYPE *lvalp);
760
761 static int yylex (void *lvalp) {
762     return _gst_parse_yylex ((YYSTYPE*) lvalp);
763 }
764
765 static int
766 yyerror (const char *s)
767 {
768   /* FIXME: This should go into the GError somehow, but how? */
769   g_warning ("error: %s", s);
770   return -1;
771 }
772
773 struct yy_buffer_state * _gst_parse_yy_scan_string (char*);
774 void _gst_parse_yy_delete_buffer (struct yy_buffer_state *);
775 GstElement *
776 _gst_parse_launch (const gchar *str, GError **error)
777 {
778   graph_t g;
779   gchar *dstr;
780   GSList *walk;
781   GstBin *bin = NULL;
782   GstElement *ret;
783   struct yy_buffer_state *buf;
784   
785   g_return_val_if_fail (str != NULL, NULL);
786
787   g.chain = NULL;
788   g.links = NULL;
789   g.error = error;
790   
791 #ifdef __GST_PARSE_TRACE
792   GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
793   __strings = __chains = __links = 0;
794 #endif /* __GST_PARSE_TRACE */
795
796   dstr = g_strdup (str);
797   buf = _gst_parse_yy_scan_string (dstr);
798
799 #ifndef GST_DISABLE_GST_DEBUG
800   yydebug = 1;
801 #endif
802
803   if (yyparse (&g) != 0) {
804     SET_ERROR (error, GST_PARSE_ERROR_SYNTAX, "Unrecoverable syntax error while parsing pipeline %s", str);
805     
806     goto error1;
807   }
808   g_free (dstr);
809   _gst_parse_yy_delete_buffer (buf);
810   
811   GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links", g.chain ? g_slist_length (g.chain->elements) : 0, g_slist_length (g.links));
812   
813   if (!g.chain) {
814     ret = NULL;
815   } else if (!(((chain_t *) g.chain)->elements->next)) {
816     /* only one toplevel element */  
817     ret = (GstElement *) ((chain_t *) g.chain)->elements->data;
818     g_slist_free (((chain_t *) g.chain)->elements);
819     if (GST_IS_BIN (ret))
820       bin = GST_BIN (ret);
821     gst_parse_chain_free (g.chain);
822   } else {  
823     /* put all elements in our bin */
824     bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
825     g_assert (bin);
826     
827     for (walk = g.chain->elements; walk; walk = walk->next)
828       gst_bin_add (bin, GST_ELEMENT (walk->data));
829     
830     g_slist_free (g.chain->elements);
831     ret = GST_ELEMENT (bin);
832     gst_parse_chain_free (g.chain);
833   }
834   
835   /* remove links */
836   for (walk = g.links; walk; walk = walk->next) {
837     link_t *l = (link_t *) walk->data;
838     if (!l->src) {
839       if (l->src_name) {
840         if (bin) {
841           l->src = gst_bin_get_by_name_recurse_up (bin, l->src_name);
842         } else {
843           l->src = strcmp (GST_ELEMENT_NAME (ret), l->src_name) == 0 ? ret : NULL;
844         }
845       }
846       if (!l->src) {
847         SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element named \"%s\" - omitting link", l->src_name);
848         gst_parse_free_link (l);
849         continue;
850       }
851     }
852     if (!l->sink) {
853       if (l->sink_name) {
854         if (bin) {
855           l->sink = gst_bin_get_by_name_recurse_up (bin, l->sink_name);
856         } else {
857           l->sink = strcmp (GST_ELEMENT_NAME (ret), l->sink_name) == 0 ? ret : NULL;
858         }
859       }
860       if (!l->sink) {
861         SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element named \"%s\" - omitting link", l->sink_name);
862         gst_parse_free_link (l);
863         continue;
864       }
865     }
866     gst_parse_perform_link (l, &g);
867   }
868   g_slist_free (g.links);
869
870 out:
871 #ifdef __GST_PARSE_TRACE
872   GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: %u strings, %u chains and %u links left", __strings, __chains, __links);
873   if (__strings || __chains || __links) {
874     g_warning ("TRACE: %u strings, %u chains and %u links left", __strings, __chains, __links);
875   }
876 #endif /* __GST_PARSE_TRACE */
877
878   return ret;
879   
880 error1:
881   g_free (dstr);
882   
883   if (g.chain) {
884     g_slist_foreach (g.chain->elements, (GFunc)gst_object_unref, NULL);
885     g_slist_free (g.chain->elements);
886     gst_parse_chain_free (g.chain);
887   }
888
889   g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
890   g_slist_free (g.links);
891   
892   if (error)
893     g_assert (*error);
894   ret = NULL;
895   
896   goto out;
897 }