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