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