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