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