parse: Unref reference to enclosing bins
[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
30 #define YYENABLE_NLS 0
31
32 #ifndef YYLTYPE_IS_TRIVIAL
33 #define YYLTYPE_IS_TRIVIAL 0
34 #endif
35
36 /*******************************************************************************************
37 *** Tracing memory leaks
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 (void)
64 {
65   link_t *ret;
66   __links++;
67   ret = g_slice_new0 (link_t);
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_slice_free (link_t, data);
77     g_return_if_fail (__links > 0);
78     __links--;
79   }
80 }
81 chain_t *
82 __gst_parse_chain_new (void)
83 {
84   chain_t *ret;
85   __chains++;
86   ret = g_slice_new0 (chain_t);
87   /* g_print ("@%p: ALLOCATED CHAIN (%3u):\n", ret, __chains); */
88   return ret;
89 }
90 void
91 __gst_parse_chain_free (chain_t *data)
92 {
93   /* g_print ("@%p: FREEING CHAIN   (%3u):\n", data, __chains - 1); */
94   g_slice_free (chain_t, data);
95   g_return_if_fail (__chains > 0);
96   __chains--;
97 }
98
99 #endif /* __GST_PARSE_TRACE */
100
101 /*******************************************************************************************
102 *** define SET_ERROR macro/function
103 *******************************************************************************************/
104 #ifdef G_HAVE_ISO_VARARGS
105
106 #  define SET_ERROR(error, type, ...) \
107 G_STMT_START { \
108   GST_CAT_ERROR (GST_CAT_PIPELINE, __VA_ARGS__); \
109   if ((error) && !*(error)) { \
110     g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
111   } \
112 } G_STMT_END
113
114 #elif defined(G_HAVE_GNUC_VARARGS)
115
116 #  define SET_ERROR(error, type, args...) \
117 G_STMT_START { \
118   GST_CAT_ERROR (GST_CAT_PIPELINE, args ); \
119   if ((error) && !*(error)) { \
120     g_set_error ((error), GST_PARSE_ERROR, (type), args ); \
121   } \
122 } G_STMT_END
123
124 #else
125
126 static inline void
127 SET_ERROR (GError **error, gint type, const char *format, ...)
128 {
129   if (error) {
130     if (*error) {
131       g_warning ("error while parsing");
132     } else {
133       va_list varargs;
134       char *string;
135
136       va_start (varargs, format);
137       string = g_strdup_vprintf (format, varargs);
138       va_end (varargs);
139
140       g_set_error (error, GST_PARSE_ERROR, type, string);
141
142       g_free (string);
143     }
144   }
145 }
146
147 #endif /* G_HAVE_ISO_VARARGS */
148
149 /*** define YYPRINTF macro/function if we're debugging */
150
151 /* bison 1.35 calls this macro with side effects, we need to make sure the
152    side effects work - crappy bison */
153
154 #ifndef GST_DISABLE_GST_DEBUG
155 #  define YYDEBUG 1
156
157 #  ifdef G_HAVE_ISO_VARARGS
158
159 /* #  define YYFPRINTF(a, ...) GST_CAT_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__) */
160 #    define YYFPRINTF(a, ...) \
161 G_STMT_START { \
162      GST_CAT_LOG (GST_CAT_PIPELINE, __VA_ARGS__); \
163 } G_STMT_END
164
165 #  elif defined(G_HAVE_GNUC_VARARGS)
166
167 #    define YYFPRINTF(a, args...) \
168 G_STMT_START { \
169      GST_CAT_LOG (GST_CAT_PIPELINE, args); \
170 } G_STMT_END
171
172 #  else
173
174 static inline void
175 YYPRINTF(const char *format, ...)
176 {
177   va_list varargs;
178   gchar *temp;
179
180   va_start (varargs, format);
181   temp = g_strdup_vprintf (format, varargs);
182   GST_CAT_LOG (GST_CAT_PIPELINE, "%s", temp);
183   g_free (temp);
184   va_end (varargs);
185 }
186
187 #  endif /* G_HAVE_ISO_VARARGS */
188
189 #endif /* GST_DISABLE_GST_DEBUG */
190
191
192 /*
193  * include headers generated by bison & flex, after defining (or not defining) YYDEBUG
194  */
195 #include "grammar.tab.h"
196 #include "parse_lex.h"
197
198 /*******************************************************************************************
199 *** report missing elements/bins/..
200 *******************************************************************************************/
201
202
203 static void  add_missing_element(graph_t *graph,gchar *name){
204   if ((graph)->ctx){
205     (graph)->ctx->missing_elements = g_list_append ((graph)->ctx->missing_elements, g_strdup (name));
206     }
207 }
208
209
210 /*******************************************************************************************
211 *** helpers for pipeline-setup
212 *******************************************************************************************/
213
214 #define TRY_SETUP_LINK(l) G_STMT_START { \
215    if( (!(l)->src.element) && (!(l)->src.name) ){ \
216      SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link has no source [sink=%s@%p]"), \
217         (l)->sink.name ? (l)->sink.name : _(""), \
218         (l)->sink.element); \
219      gst_parse_free_link (l); \
220    }else if( (!(l)->sink.element) && (!(l)->sink.name) ){ \
221      SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link has no sink [source=%s@%p]"), \
222         (l)->src.name ? (l)->src.name :_(""), \
223         (l)->src.element); \
224      gst_parse_free_link (l); \
225    }else{ \
226      graph->links = g_slist_append (graph->links, l ); \
227    }   \
228 } G_STMT_END
229
230 typedef struct {
231   gchar *src_pad;
232   gchar *sink_pad;
233   GstElement *sink;
234   GstCaps *caps;
235   gulong signal_id;
236 } DelayedLink;
237
238 typedef struct {
239   gchar *name;
240   gchar *value_str;
241   gulong signal_id;
242 } DelayedSet;
243
244 static int  gst_resolve_reference(reference_t *rr, GstElement *pipeline){
245   GstBin *bin;
246
247   if(rr->element) return  0;  /* already resolved! */
248   if(!rr->name)   return -2;  /* no chance! */
249
250   if (GST_IS_BIN (pipeline)){
251     bin = GST_BIN (pipeline);
252     rr->element = gst_bin_get_by_name_recurse_up (bin, rr->name);
253   } else {
254     rr->element = strcmp (GST_ELEMENT_NAME (pipeline), rr->name) == 0 ? 
255                 gst_object_ref(pipeline) : NULL;
256   }
257   if(rr->element) return 0; /* resolved */
258   else            return -1; /* not found */
259 }
260
261 static void gst_parse_free_delayed_set (DelayedSet *set)
262 {
263   g_free(set->name);
264   g_free(set->value_str);
265   g_slice_free(DelayedSet, set);
266 }
267
268 static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
269     const gchar * name, gpointer data);
270
271 static void gst_parse_add_delayed_set (GstElement *element, gchar *name, gchar *value_str)
272 {
273   DelayedSet *data = g_slice_new0 (DelayedSet);
274
275   GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "delaying property set %s to %s",
276     name, value_str);
277
278   data->name = g_strdup(name);
279   data->value_str = g_strdup(value_str);
280   data->signal_id = g_signal_connect_data(element, "child-added",
281       G_CALLBACK (gst_parse_new_child), data, (GClosureNotify)
282       gst_parse_free_delayed_set, (GConnectFlags) 0);
283
284   /* FIXME: we would need to listen on all intermediate bins too */
285   if (GST_IS_BIN (element)) {
286     gchar **names, **current;
287     GstElement *parent, *child;
288
289     current = names = g_strsplit (name, "::", -1);
290     parent = gst_bin_get_by_name (GST_BIN_CAST (element), current[0]);
291     current++;
292     while (parent && current[0]) {
293       child = gst_bin_get_by_name (GST_BIN (parent), current[0]);
294       if (!child && current[1]) {
295         char *sub_name = g_strjoinv ("::", &current[0]);
296
297         gst_parse_add_delayed_set(parent, sub_name, value_str);
298         g_free (sub_name);
299       }
300       gst_object_unref (parent);
301       parent = child;
302       current++;
303     }
304     if (parent)
305       gst_object_unref (parent);
306     g_strfreev (names);
307   }
308 }
309
310 static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
311     const gchar * name, gpointer data)
312 {
313   DelayedSet *set = (DelayedSet *) data;
314   GParamSpec *pspec;
315   GValue v = { 0, };
316   GObject *target = NULL;
317   GType value_type;
318
319   GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "new child %s, checking property %s",
320       name, set->name);
321
322   if (gst_child_proxy_lookup (child_proxy, set->name, &target, &pspec)) {
323     gboolean got_value = FALSE;
324
325     value_type = pspec->value_type;
326
327     GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "parsing delayed property %s as a %s from %s",
328       pspec->name, g_type_name (value_type), set->value_str);
329     g_value_init (&v, value_type);
330     if (gst_value_deserialize (&v, set->value_str))
331       got_value = TRUE;
332     else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
333        GstElement *bin;
334
335        bin = gst_parse_bin_from_description_full (set->value_str, TRUE, NULL,
336            GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS, NULL);
337        if (bin) {
338          g_value_set_object (&v, bin);
339          got_value = TRUE;
340        }
341     }
342     g_signal_handler_disconnect (child_proxy, set->signal_id);
343     if (!got_value)
344       goto error;
345     g_object_set_property (target, pspec->name, &v);
346   } else {
347     const gchar *obj_name = GST_OBJECT_NAME(object);
348     gint len = strlen (obj_name);
349
350     /* do a delayed set */
351     if ((strlen (set->name) > (len + 2)) && !strncmp (set->name, obj_name, len) && !strncmp (&set->name[len], "::", 2)) {
352       gst_parse_add_delayed_set (GST_ELEMENT(child_proxy), set->name, set->value_str);
353     }
354   }
355
356 out:
357   if (G_IS_VALUE (&v))
358     g_value_unset (&v);
359   if (target)
360     g_object_unref (target);
361   return;
362
363 error:
364   GST_CAT_ERROR (GST_CAT_PIPELINE, "could not set property \"%s\" in %"
365       GST_PTR_FORMAT, pspec->name, target);
366   goto out;
367 }
368
369 static void gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
370 {
371   GParamSpec *pspec = NULL;
372   gchar *pos = value;
373   GValue v = { 0, };
374   GObject *target = NULL;
375   GType value_type;
376
377   /* do nothing if assignment is for missing element */
378   if (element == NULL)
379     goto out;
380
381   /* parse the string, so the property name is null-terminated and pos points
382      to the beginning of the value */
383   while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++;
384   if (*pos == '=') {
385     *pos = '\0';
386   } else {
387     *pos = '\0';
388     pos++;
389     while (g_ascii_isspace (*pos)) pos++;
390   }
391   pos++;
392   while (g_ascii_isspace (*pos)) pos++;
393   if (*pos == '"') {
394     pos++;
395     pos[strlen (pos) - 1] = '\0';
396   }
397   gst_parse_unescape (pos);
398
399   if (GST_IS_CHILD_PROXY (element)) {
400     if (!gst_child_proxy_lookup (GST_CHILD_PROXY (element), value, &target, &pspec)) {
401       /* do a delayed set */
402       gst_parse_add_delayed_set (element, value, pos);
403     }
404   } else {
405     pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value);
406     if (pspec != NULL) {
407       target = g_object_ref (element);
408       GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, target, "found %s property", value);
409     } else {
410       SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
411           _("no property \"%s\" in element \"%s\""), value, \
412           GST_ELEMENT_NAME (element));
413     }
414   }
415
416   if (pspec != NULL && target != NULL) {
417     gboolean got_value = FALSE;
418
419     value_type = pspec->value_type;
420
421     GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "parsing property %s as a %s",
422         pspec->name, g_type_name (value_type));
423
424     g_value_init (&v, value_type);
425     if (gst_value_deserialize (&v, pos))
426       got_value = TRUE;
427     else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
428        GstElement *bin;
429
430        bin = gst_parse_bin_from_description_full (pos, TRUE, NULL,
431            GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS, NULL);
432        if (bin) {
433          g_value_set_object (&v, bin);
434          got_value = TRUE;
435        }
436     }
437     if (!got_value)
438       goto error;
439     g_object_set_property (target, pspec->name, &v);
440   }
441
442 out:
443   gst_parse_strfree (value);
444   if (G_IS_VALUE (&v))
445     g_value_unset (&v);
446   if (target)
447     g_object_unref (target);
448   return;
449
450 error:
451   SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
452          _("could not set property \"%s\" in element \"%s\" to \"%s\""),
453          value, GST_ELEMENT_NAME (element), pos);
454   goto out;
455 }
456
457 static void gst_parse_free_reference (reference_t *rr)
458 {
459   if(rr->element) gst_object_unref(rr->element);
460   gst_parse_strfree (rr->name);
461   g_slist_foreach (rr->pads, (GFunc) gst_parse_strfree, NULL);
462   g_slist_free (rr->pads);
463 }
464
465 static void gst_parse_free_link (link_t *link)
466 {
467   gst_parse_free_reference (&(link->src));
468   gst_parse_free_reference (&(link->sink));
469   if (link->caps) gst_caps_unref (link->caps);
470   gst_parse_link_free (link);
471 }
472
473 static void gst_parse_free_chain (chain_t *ch)
474 {
475   GSList *walk;
476   gst_parse_free_reference (&(ch->first));
477   gst_parse_free_reference (&(ch->last));
478   for(walk=ch->elements;walk;walk=walk->next)
479     gst_object_unref (walk->data);
480   g_slist_free (ch->elements);
481   gst_parse_chain_free (ch);
482 }
483
484 static void gst_parse_free_delayed_link (DelayedLink *link)
485 {
486   g_free (link->src_pad);
487   g_free (link->sink_pad);
488   if (link->caps) gst_caps_unref (link->caps);
489   g_slice_free (DelayedLink, link);
490 }
491
492 static void gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
493 {
494   DelayedLink *link = data;
495
496   GST_CAT_INFO (GST_CAT_PIPELINE, "trying delayed linking %s:%s to %s:%s",
497                 GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
498                 GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
499
500   if (gst_element_link_pads_filtered (src, link->src_pad, link->sink,
501       link->sink_pad, link->caps)) {
502     /* do this here, we don't want to get any problems later on when
503      * unlocking states */
504     GST_CAT_DEBUG (GST_CAT_PIPELINE, "delayed linking %s:%s to %s:%s worked",
505                    GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
506                    GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
507     g_signal_handler_disconnect (src, link->signal_id);
508   }
509 }
510
511 /* both padnames and the caps may be NULL */
512 static gboolean
513 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad,
514                                 GstElement *sink, const gchar *sink_pad,
515                                 GstCaps *caps)
516 {
517   GList *templs = gst_element_class_get_pad_template_list (
518       GST_ELEMENT_GET_CLASS (src));
519
520   for (; templs; templs = templs->next) {
521     GstPadTemplate *templ = (GstPadTemplate *) templs->data;
522     if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) &&
523         (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
524     {
525       DelayedLink *data = g_slice_new (DelayedLink);
526
527       /* TODO: maybe we should check if src_pad matches this template's names */
528
529       GST_CAT_DEBUG (GST_CAT_PIPELINE, "trying delayed link %s:%s to %s:%s",
530                      GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (src_pad),
531                      GST_STR_NULL (GST_ELEMENT_NAME (sink)), GST_STR_NULL (sink_pad));
532
533       data->src_pad = g_strdup (src_pad);
534       data->sink = sink;
535       data->sink_pad = g_strdup (sink_pad);
536       if (caps) {
537         data->caps = gst_caps_copy (caps);
538       } else {
539         data->caps = NULL;
540       }
541       data->signal_id = g_signal_connect_data (src, "pad-added",
542           G_CALLBACK (gst_parse_found_pad), data,
543           (GClosureNotify) gst_parse_free_delayed_link, (GConnectFlags) 0);
544       return TRUE;
545     }
546   }
547   return FALSE;
548 }
549
550 /*
551  * performs a link and frees the struct. src and sink elements must be given
552  * return values   0 - link performed
553  *                 1 - link delayed
554  *                <0 - error
555  */
556 static gint
557 gst_parse_perform_link (link_t *link, graph_t *graph)
558 {
559   GstElement *src = link->src.element;
560   GstElement *sink = link->sink.element;
561   GSList *srcs = link->src.pads;
562   GSList *sinks = link->sink.pads;
563   g_assert (GST_IS_ELEMENT (src));
564   g_assert (GST_IS_ELEMENT (sink));
565
566   GST_CAT_INFO (GST_CAT_PIPELINE,
567       "linking %s:%s to %s:%s (%u/%u) with caps \"%" GST_PTR_FORMAT "\"",
568       GST_ELEMENT_NAME (src), link->src.name ? link->src.name : "(any)",
569       GST_ELEMENT_NAME (sink), link->sink.name ? link->sink.name : "(any)",
570       g_slist_length (srcs), g_slist_length (sinks), link->caps);
571
572   if (!srcs || !sinks) {
573     if (gst_element_link_pads_filtered (src,
574         srcs ? (const gchar *) srcs->data : NULL, sink,
575         sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
576       goto success;
577     } else {
578       if (gst_parse_perform_delayed_link (src,
579           srcs ? (const gchar *) srcs->data : NULL,
580           sink, sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
581         goto success;
582       } else {
583         goto error;
584       }
585     }
586   }
587   if (g_slist_length (link->src.pads) != g_slist_length (link->sink.pads)) {
588     goto error;
589   }
590   while (srcs && sinks) {
591     const gchar *src_pad = (const gchar *) srcs->data;
592     const gchar *sink_pad = (const gchar *) sinks->data;
593     srcs = g_slist_next (srcs);
594     sinks = g_slist_next (sinks);
595     if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad,
596         link->caps)) {
597       continue;
598     } else {
599       if (gst_parse_perform_delayed_link (src, src_pad,
600                                           sink, sink_pad,
601                                           link->caps)) {
602         continue;
603       } else {
604         goto error;
605       }
606     }
607   }
608
609 success:
610   gst_parse_free_link (link);
611   return 0;
612
613 error:
614   SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
615       _("could not link %s to %s"), GST_ELEMENT_NAME (src),
616       GST_ELEMENT_NAME (sink));
617   gst_parse_free_link (link);
618   return -1;
619 }
620
621
622 static int yyerror (void *scanner, graph_t *graph, const char *s);
623 %}
624
625 %union {
626     gchar *ss;
627     chain_t *cc;
628     link_t *ll;
629     reference_t rr;
630     GstElement *ee;
631     GSList *pp;
632     graph_t *gg;
633 }
634
635 /* No grammar ambiguities expected, FAIL otherwise */
636 %expect 0
637
638 %token <ss> PARSE_URL
639 %token <ss> IDENTIFIER
640 %left  <ss> REF PADREF BINREF 
641 %token <ss> ASSIGNMENT
642 %token <ss> LINK
643
644 %type <ss> binopener
645 %type <gg> graph
646 %type <cc> chain bin chainlist openchain elementary
647 %type <rr> reference
648 %type <ll> link
649 %type <ee> element
650 %type <pp> morepads pads assignments
651
652 %destructor {   gst_parse_strfree ($$);         } <ss>
653 %destructor {   if($$)
654                   gst_parse_free_chain($$);     } <cc>
655 %destructor {   gst_parse_free_link ($$);       } <ll>
656 %destructor {   gst_parse_free_reference(&($$));} <rr>
657 %destructor {   gst_object_unref ($$);          } <ee>
658 %destructor {   GSList *walk;
659                 for(walk=$$;walk;walk=walk->next)
660                   gst_parse_strfree (walk->data);
661                 g_slist_free ($$);              } <pp>
662
663
664
665 %left '(' ')'
666 %left ','
667 %right '.'
668 %left '!' '='
669
670 %lex-param { void *scanner }
671 %parse-param { void *scanner }
672 %parse-param { graph_t *graph }
673 %pure-parser
674
675 %start graph
676 %%
677
678 /*************************************************************
679 * Grammar explanation:
680 *   _element_s are specified by an identifier of their type.
681 *   a name can be give in the optional property-assignments
682 *       coffeeelement
683 *       fakesrc name=john
684 *       identity silence=false name=frodo
685 *   (cont'd)
686 **************************************************************/
687 element:        IDENTIFIER                    { $$ = gst_element_factory_make ($1, NULL);
688                                                 if ($$ == NULL) {
689                                                   add_missing_element(graph, $1);
690                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, _("no element \"%s\""), $1);
691                                                 }
692                                                 gst_parse_strfree ($1);
693                                               }
694         |       element ASSIGNMENT            { gst_parse_element_set ($2, $1, graph);
695                                                 $$ = $1;
696                                               }
697         ;
698
699 /*************************************************************
700 * Grammar explanation: (cont'd)
701 *   a graph has (pure) _element_s, _bin_s and _link_s.
702 *   since bins are special elements, bins and elements can
703 *   be generalized as _elementary_.
704 *   The construction of _bin_s will be discussed later.
705 *   (cont'd)
706 *
707 **************************************************************/
708 elementary:
709         element                               { $$ = gst_parse_chain_new ();
710                                                 /* g_print ("@%p: CHAINing elementary\n", $$); */
711                                                 $$->first.element = $1? gst_object_ref($1) : NULL;
712                                                 $$->last.element = $1? gst_object_ref($1) : NULL;
713                                                 $$->first.name = $$->last.name = NULL;
714                                                 $$->first.pads = $$->last.pads = NULL;
715                                                 $$->elements = $1 ? g_slist_prepend (NULL, $1) : NULL;
716                                               }
717         | bin                                 { $$=$1; }
718         ;
719
720 /*************************************************************
721 * Grammar explanation: (cont'd)
722 *   a _chain_ is a list of _elementary_s that have _link_s inbetween
723 *   which are represented through infix-notation.
724 *
725 *       fakesrc ! sometransformation ! fakesink
726 *
727 *   every _link_ can be augmented with _pads_.
728 *
729 *       coffeesrc .sound ! speakersink
730 *       multisrc  .movie,ads ! .projector,smallscreen multisink
731 *
732 *   and every _link_ can be setup to filter media-types
733 *       mediasrc ! audio/x-raw, signed=TRUE ! stereosink
734 *
735 * User HINT:
736 *   if the lexer does not recognize your media-type it
737 *   will make it an element name. that results in errors
738 *   like
739 *       NO SUCH ELEMENT: no element audio7x-raw
740 *   '7' vs. '/' in https://en.wikipedia.org/wiki/QWERTZ
741 *
742 * Parsing HINT:
743 *   in the parser we need to differ between chains that can
744 *   be extended by more elementaries (_openchain_) and others
745 *   that are syntactically closed (handled later in this file).
746 *       (e.g. fakesrc ! sinkreferencename.padname)
747 **************************************************************/
748 chain:  openchain                             { $$=$1; 
749                                                 if($$->last.name){
750                                                         SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
751                                                         _("unexpected reference \"%s\" - ignoring"), $$->last.name);
752                                                         gst_parse_strfree($$->last.name);
753                                                         $$->last.name=NULL;
754                                                 }
755                                                 if($$->last.pads){
756                                                         SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
757                                                         _("unexpected pad-reference \"%s\" - ignoring"), (gchar*)$$->last.pads->data);
758                                                         g_slist_foreach ($$->last.pads, (GFunc) gst_parse_strfree, NULL);
759                                                         g_slist_free ($$->last.pads);
760                                                         $$->last.pads=NULL;
761                                                 }
762                                               }
763         ;
764
765 openchain:
766         elementary pads                       { $$=$1;
767                                                 $$->last.pads = g_slist_concat ($$->last.pads, $2);
768                                                 /* g_print ("@%p@%p: FKI elementary pads\n", $1, $$->last.pads); */
769                                               }
770         | openchain link pads elementary pads
771                                               {
772                                                 $2->src  = $1->last;
773                                                 $2->sink = $4->first;
774                                                 $2->sink.pads = g_slist_concat ($3, $2->sink.pads);
775                                                 TRY_SETUP_LINK($2);
776                                                 $4->first = $1->first;
777                                                 $4->elements = g_slist_concat ($1->elements, $4->elements);
778                                                 gst_parse_chain_free($1);
779                                                 $$ = $4;
780                                                 $$->last.pads = g_slist_concat ($$->last.pads, $5);
781                                               }
782
783         ;
784
785 link:   LINK                                  { $$ = gst_parse_link_new ();
786                                                 $$->src.element = NULL;
787                                                 $$->sink.element = NULL;
788                                                 $$->src.name = NULL;
789                                                 $$->sink.name = NULL;
790                                                 $$->src.pads = NULL;
791                                                 $$->sink.pads = NULL;
792                                                 $$->caps = NULL; 
793                                                 if ($1) {
794                                                   $$->caps = gst_caps_from_string ($1);
795                                                   if ($$->caps == NULL)
796                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $1);
797                                                   gst_parse_strfree ($1);
798                                                 }
799                                               }
800         ;
801 pads:           /* NOP */                     { $$ = NULL; }
802         |       PADREF morepads               { $$ = $2;
803                                                 $$ = g_slist_prepend ($$, $1);
804                                               }
805         ;
806 morepads:       /* NOP */                     { $$ = NULL; }
807         |       ',' IDENTIFIER morepads       { $$ = g_slist_prepend ($3, $2); }
808         ;
809
810 /*************************************************************
811 * Grammar explanation: (cont'd)
812 *   the first and last elements of a _chain_ can be give
813 *   as URL. This creates special elements that fit the URL.
814 *
815 *       fakesrc ! http://fake-sink.org
816 *       http://somesource.org ! fakesink
817 **************************************************************/
818
819 chain:  openchain link PARSE_URL              { GstElement *element =
820                                                           gst_element_make_from_uri (GST_URI_SINK, $3, NULL, NULL);
821                                                 /* FIXME: get and parse error properly */
822                                                 if (!element) {
823                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
824                                                           _("no sink element for URI \"%s\""), $3);
825                                                 }
826                                                 $$ = $1;
827                                                 $2->sink.element = element?gst_object_ref(element):NULL;
828                                                 $2->src = $1->last;
829                                                 TRY_SETUP_LINK($2);
830                                                 $$->last.element = NULL;
831                                                 $$->last.name = NULL;
832                                                 $$->last.pads = NULL;
833                                                 if(element) $$->elements = g_slist_append ($$->elements, element);
834                                                 g_free ($3);
835                                               }
836         ;
837 openchain:
838         PARSE_URL                             { GstElement *element =
839                                                           gst_element_make_from_uri (GST_URI_SRC, $1, NULL, NULL);
840                                                 /* FIXME: get and parse error properly */
841                                                 if (!element) {
842                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
843                                                     _("no source element for URI \"%s\""), $1);
844                                                 }
845                                                 $$ = gst_parse_chain_new ();
846                                                 /* g_print ("@%p: CHAINing srcURL\n", $$); */
847                                                 $$->first.element = NULL;
848                                                 $$->first.name = NULL;
849                                                 $$->first.pads = NULL;
850                                                 $$->last.element = element ? gst_object_ref(element):NULL;
851                                                 $$->last.name = NULL;
852                                                 $$->last.pads = NULL;
853                                                 $$->elements = element ? g_slist_prepend (NULL, element)  : NULL;
854                                                 g_free($1);
855                                               }
856         ;
857
858
859 /*************************************************************
860 * Grammar explanation: (cont'd)
861 *   the first and last elements of a _chain_ can be linked
862 *   to a named _reference_ (with optional pads).
863 *
864 *       fakesrc ! nameOfSinkElement.
865 *       fakesrc ! nameOfSinkElement.Padname
866 *       fakesrc ! nameOfSinkElement.Padname, anotherPad
867 *       nameOfSource.Padname ! fakesink
868 **************************************************************/
869
870 chain:  openchain link reference              { $$ = $1;
871                                                 $2->sink= $3;
872                                                 $2->src = $1->last;
873                                                 TRY_SETUP_LINK($2);
874                                                 $$->last.element = NULL;
875                                                 $$->last.name = NULL;
876                                                 $$->last.pads = NULL;
877                                               }
878         ;
879
880
881 openchain:
882         reference                             { $$ = gst_parse_chain_new ();
883                                                 $$->last=$1;
884                                                 $$->first.element = NULL;
885                                                 $$->first.name = NULL;
886                                                 $$->first.pads = NULL;
887                                                 $$->elements = NULL;
888                                               }
889         ;
890 reference:      REF morepads                  {
891                                                 gchar *padname = $1;
892                                                 GSList *pads = $2;
893                                                 if (padname) {
894                                                   while (*padname != '.') padname++;
895                                                   *padname = '\0';
896                                                   padname++;
897                                                   if (*padname != '\0')
898                                                     pads = g_slist_prepend (pads, gst_parse_strdup (padname));
899                                                 }
900                                                 $$.element=NULL;
901                                                 $$.name=$1;
902                                                 $$.pads=pads;
903                                               }
904         ;
905
906
907 /*************************************************************
908 * Grammar explanation: (cont'd)
909 *   a _chainlist_ is just a list of _chain_s.
910 *
911 *   You can specify _link_s with named
912 *   _reference_ on each side. That
913 *   works already after the explanations above.
914 *       someSourceName.Pad ! someSinkName.
915 *       someSourceName.Pad,anotherPad ! someSinkName.Apad,Bpad  
916 *
917 *   If a syntax error occurs, the already finished _chain_s
918 *   and _links_ are kept intact.
919 *************************************************************/
920
921 chainlist: /* NOP */                          { $$ = NULL; }
922         | chainlist chain                     { if ($1){
923                                                   gst_parse_free_reference(&($1->last));
924                                                   gst_parse_free_reference(&($2->first));
925                                                   $2->first = $1->first;
926                                                   $2->elements = g_slist_concat ($1->elements, $2->elements);
927                                                   gst_parse_chain_free ($1);
928                                                 }
929                                                 $$ = $2;
930                                               }
931         | chainlist error                     { $$=$1;
932                                                 GST_CAT_DEBUG (GST_CAT_PIPELINE,"trying to recover from syntax error");
933                                                 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX, _("syntax error"));
934                                               }
935         ;
936
937 /*************************************************************
938 * Grammar explanation: (cont'd)
939 *   _bins_
940 *************************************************************/
941
942
943 assignments:    /* NOP */                     { $$ = NULL; }
944         |       ASSIGNMENT assignments        { $$ = g_slist_prepend ($2, $1); }
945         ;
946
947 binopener:      '('                           { $$ = gst_parse_strdup(_("bin")); }
948         |       BINREF                        { $$ = $1; }
949         ;
950 bin:    binopener assignments chainlist ')'   { 
951                                                 chain_t *chain = $3;
952                                                 GSList *walk;
953                                                 GstBin *bin = (GstBin *) gst_element_factory_make ($1, NULL);
954                                                 if (!chain) {
955                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY_BIN,
956                                                     _("specified empty bin \"%s\", not allowed"), $1);
957                                                   chain = gst_parse_chain_new ();
958                                                   chain->first.element = chain->last.element = NULL;
959                                                   chain->first.name    = chain->last.name    = NULL;
960                                                   chain->first.pads    = chain->last.pads    = NULL;
961                                                   chain->elements = NULL;
962                                                 }
963                                                 if (!bin) {
964                                                   add_missing_element(graph, $1);
965                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
966                                                     _("no bin \"%s\", unpacking elements"), $1);
967                                                   /* clear property-list */
968                                                   g_slist_foreach ($2, (GFunc) gst_parse_strfree, NULL);
969                                                   g_slist_free ($2);
970                                                   $2 = NULL;
971                                                 } else {
972                                                   for (walk = chain->elements; walk; walk = walk->next )
973                                                     gst_bin_add (bin, GST_ELEMENT (walk->data));
974                                                   g_slist_free (chain->elements);
975                                                   chain->elements = g_slist_prepend (NULL, bin);
976                                                 }
977                                                 $$ = chain;
978                                                 /* set the properties now
979                                                  * HINT: property-list cleared above, if bin==NULL
980                                                  */
981                                                 for (walk = $2; walk; walk = walk->next)
982                                                   gst_parse_element_set ((gchar *) walk->data,
983                                                         GST_ELEMENT (bin), graph);
984                                                 g_slist_free ($2);
985                                                 gst_parse_strfree ($1);
986                                               }
987         ;
988
989 /*************************************************************
990 * Grammar explanation: (cont'd)
991 *   _graph_
992 *************************************************************/
993
994 graph:  chainlist                             { $$ = graph;
995                                                 $$->chain = $1;
996                                                 if(!$1) {
997                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
998                                                 }
999                                               }
1000         ;
1001
1002 %%
1003
1004
1005 static int
1006 yyerror (void *scanner, graph_t *graph, const char *s)
1007 {
1008   /* FIXME: This should go into the GError somehow, but how? */
1009   GST_WARNING ("Error during parsing: %s", s);
1010   return -1;
1011 }
1012
1013
1014 GstElement *
1015 priv_gst_parse_launch (const gchar *str, GError **error, GstParseContext *ctx,
1016     GstParseFlags flags)
1017 {
1018   graph_t g;
1019   gchar *dstr;
1020   GSList *walk;
1021   GstBin *bin = NULL;
1022   GstElement *ret;
1023   yyscan_t scanner;
1024
1025   g_return_val_if_fail (str != NULL, NULL);
1026   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1027
1028   g.chain = NULL;
1029   g.links = NULL;
1030   g.error = error;
1031   g.ctx = ctx;
1032   g.flags = flags;
1033
1034 #ifdef __GST_PARSE_TRACE
1035   GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
1036   __strings = __chains = __links = 0;
1037 #endif /* __GST_PARSE_TRACE */
1038
1039   /* g_print("Now scanning: %s\n", str); */
1040
1041   dstr = g_strdup (str);
1042   priv_gst_parse_yylex_init (&scanner);
1043   priv_gst_parse_yy_scan_string (dstr, scanner);
1044
1045 #if YYDEBUG
1046   yydebug = 1;
1047 #endif
1048
1049   if (yyparse (scanner, &g) != 0) {
1050     SET_ERROR (error, GST_PARSE_ERROR_SYNTAX,
1051         "Unrecoverable syntax error while parsing pipeline %s", str);
1052
1053     priv_gst_parse_yylex_destroy (scanner);
1054     g_free (dstr);
1055
1056     goto error1;
1057   }
1058   priv_gst_parse_yylex_destroy (scanner);
1059   g_free (dstr);
1060
1061   GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links",
1062       g.chain ? g_slist_length (g.chain->elements) : 0,
1063       g_slist_length (g.links));
1064
1065   /* ensure chain is not NULL */
1066   if (!g.chain){
1067     g.chain=gst_parse_chain_new ();
1068     g.chain->elements=NULL;
1069     g.chain->first.element=NULL;
1070     g.chain->first.name=NULL;
1071     g.chain->first.pads=NULL;
1072     g.chain->last.element=NULL;
1073     g.chain->last.name=NULL;
1074     g.chain->last.pads=NULL;
1075   };
1076   
1077   /* ensure elements is not empty */
1078   if(!g.chain->elements){
1079     g.chain->elements= g_slist_prepend (NULL, NULL);
1080   };
1081   
1082   /* put all elements in our bin if necessary */
1083   if(g.chain->elements->next){
1084     bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
1085     g_assert (bin);
1086
1087     for (walk = g.chain->elements; walk; walk = walk->next) {
1088       if (walk->data != NULL)
1089         gst_bin_add (bin, GST_ELEMENT (walk->data));
1090     }
1091     g_slist_free (g.chain->elements);
1092     g.chain->elements = g_slist_prepend (NULL, bin);
1093   } 
1094
1095   ret = (GstElement *) g.chain->elements->data;
1096   g_slist_free (g.chain->elements);
1097   g.chain->elements=NULL;
1098   if (GST_IS_BIN (ret))
1099     bin = GST_BIN (ret);
1100   gst_parse_free_chain (g.chain);
1101   g.chain = NULL;
1102
1103
1104   /* resolve and perform links */
1105   for (walk = g.links; walk; walk = walk->next) {
1106     link_t *l = (link_t *) walk->data;
1107     int err;
1108     err=gst_resolve_reference( &(l->src), ret);
1109     if (err) {
1110        if(-1==err){
1111           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1112               "No src-element named \"%s\" - omitting link", l->src.name);
1113        }else{
1114           /* probably a missing element which we've handled already */
1115           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1116               "No src-element found - omitting link");
1117        }
1118        gst_parse_free_link (l);
1119        continue;
1120     }
1121
1122     err=gst_resolve_reference( &(l->sink), ret);
1123     if (err) {
1124        if(-1==err){
1125           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1126               "No sink-element named \"%s\" - omitting link", l->src.name);
1127        }else{
1128           /* probably a missing element which we've handled already */
1129           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1130               "No sink-element found - omitting link");
1131        }
1132        gst_parse_free_link (l);
1133        continue;
1134     }
1135     gst_parse_perform_link (l, &g);
1136   }
1137   g_slist_free (g.links);
1138
1139 out:
1140 #ifdef __GST_PARSE_TRACE
1141   GST_CAT_DEBUG (GST_CAT_PIPELINE,
1142       "TRACE: %u strings, %u chains and %u links left", __strings, __chains,
1143       __links);
1144   if (__strings || __chains || __links) {
1145     g_warning ("TRACE: %u strings, %u chains and %u links left", __strings,
1146         __chains, __links);
1147   }
1148 #endif /* __GST_PARSE_TRACE */
1149
1150   return ret;
1151
1152 error1:
1153   if (g.chain) {
1154     gst_parse_free_chain (g.chain);
1155     g.chain=NULL;
1156   }
1157
1158   g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
1159   g_slist_free (g.links);
1160
1161   if (error)
1162     g_assert (*error);
1163   ret = NULL;
1164
1165   goto out;
1166 }