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