parse: do delayed set only if the target child was not found and fail otherwise
[platform/upstream/gstreamer.git] / subprojects / gstreamer / gst / parse / grammar.y.in
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 <glib/gi18n-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 static guint __elements;
45 gchar *
46 __gst_parse_strdup (gchar *org)
47 {
48   gchar *ret;
49   __strings++;
50   ret = g_strdup (org);
51   /* g_print ("ALLOCATED STR   (%3u): %p %s\n", __strings, ret, ret); */
52   return ret;
53 }
54 void
55 __gst_parse_strfree (gchar *str)
56 {
57   if (str) {
58     /* g_print ("FREEING STR     (%3u): %p %s\n", __strings - 1, str, str); */
59     g_free (str);
60     g_return_if_fail (__strings > 0);
61     __strings--;
62   }
63 }
64 link_t *__gst_parse_link_new (void)
65 {
66   link_t *ret;
67   __links++;
68   ret = g_slice_new0 (link_t);
69   /* g_print ("ALLOCATED LINK  (%3u): %p\n", __links, ret); */
70   return ret;
71 }
72 void
73 __gst_parse_link_free (link_t *data)
74 {
75   if (data) {
76     /* g_print ("FREEING LINK    (%3u): %p\n", __links - 1, data); */
77     g_slice_free (link_t, data);
78     g_return_if_fail (__links > 0);
79     __links--;
80   }
81 }
82 chain_t *
83 __gst_parse_chain_new (void)
84 {
85   chain_t *ret;
86   __chains++;
87   ret = g_slice_new0 (chain_t);
88   /* g_print ("@%p: ALLOCATED CHAIN (%3u):\n", ret, __chains); */
89   return ret;
90 }
91 void
92 __gst_parse_chain_free (chain_t *data)
93 {
94   /* g_print ("@%p: FREEING CHAIN   (%3u):\n", data, __chains - 1); */
95   g_slice_free (chain_t, data);
96   g_return_if_fail (__chains > 0);
97   __chains--;
98 }
99
100 element_t *
101 __gst_parse_element_new (void)
102 {
103   element_t *ret;
104   __elements++;
105   ret = g_slice_new0 (element_t);
106   /* g_print ("@%p: ALLOCATED ELEMENT (%3u):\n", ret, __elements); */
107   return ret;
108 }
109 void
110 __gst_parse_element_free (element_t *data)
111 {
112   /* g_print ("@%p: FREEING ELEMENT   (%3u):\n", data, __elements - 1); */
113   g_slice_free (element_t, data);
114   g_return_if_fail (__elements > 0);
115   __elements--;
116 }
117
118 #endif /* __GST_PARSE_TRACE */
119
120 /*******************************************************************************************
121 *** define SET_ERROR macro/function
122 *******************************************************************************************/
123 #ifdef G_HAVE_ISO_VARARGS
124
125 #  define SET_ERROR(error, type, ...) \
126 G_STMT_START { \
127   GST_CAT_ERROR (GST_CAT_PIPELINE, __VA_ARGS__); \
128   if ((error) && !*(error)) { \
129     g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
130   } \
131 } G_STMT_END
132
133 #elif defined(G_HAVE_GNUC_VARARGS)
134
135 #  define SET_ERROR(error, type, args...) \
136 G_STMT_START { \
137   GST_CAT_ERROR (GST_CAT_PIPELINE, args ); \
138   if ((error) && !*(error)) { \
139     g_set_error ((error), GST_PARSE_ERROR, (type), args ); \
140   } \
141 } G_STMT_END
142
143 #else
144
145 static inline void
146 SET_ERROR (GError **error, gint type, const char *format, ...)
147 {
148   if (error) {
149     if (*error) {
150       g_warning ("error while parsing");
151     } else {
152       va_list varargs;
153       char *string;
154
155       va_start (varargs, format);
156       string = g_strdup_vprintf (format, varargs);
157       va_end (varargs);
158
159       g_set_error (error, GST_PARSE_ERROR, type, string);
160
161       g_free (string);
162     }
163   }
164 }
165
166 #endif /* G_HAVE_ISO_VARARGS */
167
168 /*** define YYPRINTF macro/function if we're debugging */
169
170 /* bison 1.35 calls this macro with side effects, we need to make sure the
171    side effects work - crappy bison */
172
173 #ifndef GST_DISABLE_GST_DEBUG
174 #  define YYDEBUG 1
175
176 #  ifdef G_HAVE_ISO_VARARGS
177
178 /* #  define YYFPRINTF(a, ...) GST_CAT_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__) */
179 #    define YYFPRINTF(a, ...) \
180 G_STMT_START { \
181      GST_CAT_LOG (GST_CAT_PIPELINE, __VA_ARGS__); \
182 } G_STMT_END
183
184 #  elif defined(G_HAVE_GNUC_VARARGS)
185
186 #    define YYFPRINTF(a, args...) \
187 G_STMT_START { \
188      GST_CAT_LOG (GST_CAT_PIPELINE, args); \
189 } G_STMT_END
190
191 #  else
192
193 static inline void
194 YYPRINTF(const char *format, ...)
195 {
196   va_list varargs;
197   gchar *temp;
198
199   va_start (varargs, format);
200   temp = g_strdup_vprintf (format, varargs);
201   GST_CAT_LOG (GST_CAT_PIPELINE, "%s", temp);
202   g_free (temp);
203   va_end (varargs);
204 }
205
206 #  endif /* G_HAVE_ISO_VARARGS */
207
208 #endif /* GST_DISABLE_GST_DEBUG */
209
210
211 /*
212  * include headers generated by bison & flex, after defining (or not defining) YYDEBUG
213  */
214 #include "grammar.tab.h"
215 #include "parse_lex.h"
216
217 /*******************************************************************************************
218 *** report missing elements/bins/..
219 *******************************************************************************************/
220
221
222 static void  add_missing_element(graph_t *graph,gchar *name){
223   if ((graph)->ctx){
224     (graph)->ctx->missing_elements = g_list_append ((graph)->ctx->missing_elements, g_strdup (name));
225     }
226 }
227
228
229 /*******************************************************************************************
230 *** helpers for pipeline-setup
231 *******************************************************************************************/
232
233 #define TRY_SETUP_LINK(l) G_STMT_START { \
234    if( (!(l)->src.element) && (!(l)->src.name) ){ \
235      SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link has no source [sink=%s@%p]"), \
236         (l)->sink.name ? (l)->sink.name : "", \
237         (l)->sink.element); \
238      gst_parse_free_link (l); \
239    }else if( (!(l)->sink.element) && (!(l)->sink.name) ){ \
240      SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link has no sink [source=%s@%p]"), \
241         (l)->src.name ? (l)->src.name : "", \
242         (l)->src.element); \
243      gst_parse_free_link (l); \
244    }else{ \
245      graph->links = g_slist_append (graph->links, l ); \
246    }   \
247 } G_STMT_END
248
249 typedef struct {
250   gchar *src_pad;
251   gchar *sink_pad;
252   GstElement *sink;
253   GstCaps *caps;
254   gulong pad_added_signal_id, no_more_pads_signal_id;
255   gboolean all_pads;
256 } DelayedLink;
257
258 typedef struct {
259   gchar *name;
260   gchar *value_str;
261   gulong signal_id;
262 } DelayedSet;
263
264 static int  gst_resolve_reference(reference_t *rr, GstElement *pipeline){
265   GstBin *bin;
266
267   if(rr->element) return  0;  /* already resolved! */
268   if(!rr->name)   return -2;  /* no chance! */
269
270   if (GST_IS_BIN (pipeline)){
271     bin = GST_BIN (pipeline);
272     rr->element = gst_bin_get_by_name_recurse_up (bin, rr->name);
273   } else {
274     rr->element = strcmp (GST_ELEMENT_NAME (pipeline), rr->name) == 0 ?
275                 gst_object_ref(pipeline) : NULL;
276   }
277   if(rr->element) return 0; /* resolved */
278   else            return -1; /* not found */
279 }
280
281 static void gst_parse_free_delayed_set (DelayedSet *set)
282 {
283   g_free(set->name);
284   g_free(set->value_str);
285   g_slice_free(DelayedSet, set);
286 }
287
288 static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
289     const gchar * name, gpointer data);
290
291 static void gst_parse_add_delayed_set (GstElement *element, gchar *name, gchar *value_str)
292 {
293   DelayedSet *data = g_slice_new0 (DelayedSet);
294
295   GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "delaying property set %s to %s",
296     name, value_str);
297
298   data->name = g_strdup(name);
299   data->value_str = g_strdup(value_str);
300   data->signal_id = g_signal_connect_data(element, "child-added",
301       G_CALLBACK (gst_parse_new_child), data, (GClosureNotify)
302       gst_parse_free_delayed_set, (GConnectFlags) 0);
303
304   /* FIXME: we would need to listen on all intermediate bins too */
305   if (GST_IS_BIN (element)) {
306     gchar **names, **current;
307     GstElement *parent, *child;
308
309     current = names = g_strsplit (name, "::", -1);
310     parent = gst_bin_get_by_name (GST_BIN_CAST (element), current[0]);
311     current++;
312     while (parent && current[0]) {
313       child = gst_bin_get_by_name (GST_BIN (parent), current[0]);
314       if (!child && current[1]) {
315         char *sub_name = g_strjoinv ("::", &current[0]);
316
317         gst_parse_add_delayed_set(parent, sub_name, value_str);
318         g_free (sub_name);
319       }
320       gst_object_unref (parent);
321       parent = child;
322       current++;
323     }
324     if (parent)
325       gst_object_unref (parent);
326     g_strfreev (names);
327   }
328 }
329
330 static gboolean
331 gst_parse_separate_prop_from_children (const gchar *name, gchar **children, gchar **property)
332 {
333   static const gchar *separator = "::";
334   const gchar *prop = NULL;
335
336   g_return_val_if_fail (name, FALSE);
337   g_return_val_if_fail (children, FALSE);
338   g_return_val_if_fail (property, FALSE);
339
340   /* Given "child1::child2::prop" isolate "prop" */
341   prop = g_strrstr (name, separator);
342   if (!prop) {
343     GST_WARNING ("%s is not a valid childproxy path", name);
344     return FALSE;
345   }
346
347   /* Make a copy of prop skipping "::" */
348   *property = g_strdup (prop + 2);
349
350   /* Extract "child1::child2" from "child1::child2::prop" */
351   *children =
352     g_strndup (name, strlen (name) - strlen (prop));
353
354   return TRUE;
355 }
356
357 static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
358     const gchar * name, gpointer data)
359 {
360   DelayedSet *set = (DelayedSet *) data;
361   GParamSpec *pspec;
362   GValue v = { 0, };
363   GObject *target = NULL;
364   GType value_type;
365
366   GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "new child %s, checking property %s",
367       name, set->name);
368
369   if (gst_child_proxy_lookup (child_proxy, set->name, &target, &pspec)) {
370     gboolean got_value = FALSE;
371
372     value_type = pspec->value_type;
373
374     GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "parsing delayed property %s as a %s from %s",
375       pspec->name, g_type_name (value_type), set->value_str);
376     g_value_init (&v, value_type);
377     if (gst_value_deserialize_with_pspec (&v, set->value_str, pspec))
378       got_value = TRUE;
379     else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
380        GstElement *bin;
381
382        bin = gst_parse_bin_from_description_full (set->value_str, TRUE, NULL,
383            GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS | GST_PARSE_FLAG_PLACE_IN_BIN, NULL);
384        if (bin) {
385          g_value_set_object (&v, bin);
386          got_value = TRUE;
387        }
388     }
389     g_signal_handler_disconnect (child_proxy, set->signal_id);
390     if (!got_value)
391       goto error;
392     g_object_set_property (target, pspec->name, &v);
393   } else {
394     const gchar *obj_name = GST_OBJECT_NAME(object);
395     gint len = strlen (obj_name);
396
397     /*
398      * We've been notified that a new child has beed added, but the
399      * property was still not found. Three things could be happening:
400      *
401      * 1. The target property is of the form obj_name::child_name::property
402      * and is for a child that does not (yet) exist:
403      *    We need to add the delayed property setting handler on that new child.
404      *
405      * 2. The target property is of the form obj_name::property or
406      * obj_name::child_name::property and the child already exists:
407      *    We warn about a nonexistent property
408      *
409      * 3. The target property is of the form other_obj_name::child_name::property
410      * or other_obj_name::property:
411      *    We ignore this case as another delayed set will catch it.
412      */
413
414     /* Cases 1,2: The child just added corresponds to this delayed set */
415     if ((strlen (set->name) > (len+2)) && !strncmp (set->name, obj_name, len)
416         && !strncmp (&set->name[len], "::", 2)) {
417       gchar *children = NULL;
418       gchar *prop = NULL;
419       GObject *child = NULL;
420
421       if (!gst_parse_separate_prop_from_children (set->name, &children, &prop)) {
422         /* Malformed property name, ignore */
423         return;
424       }
425
426       child = gst_child_proxy_get_child_by_name_recurse (child_proxy, children);
427       g_free (children);
428       g_free (prop);
429
430       /* Case 1: A child in the hierarchy does not exist yet, add a new delayed set */
431       if (NULL == child) {
432         gst_parse_add_delayed_set (GST_ELEMENT (child_proxy), set->name, set->value_str);
433       }
434       /* Case 2: The target child exists already but there's no such property */
435       else {
436         gst_object_unref (child);
437         GST_ELEMENT_WARNING(GST_ELEMENT (child_proxy), PARSE, NO_SUCH_PROPERTY,
438           (_("No such property.")), (_("no property \"%s\" in element \"%s\""),
439         set->name, GST_ELEMENT_NAME(child_proxy)));
440       }
441     }
442     /* Case 3: The child just added does not correspond to this delayed set, just ignore
443      * else { }
444      */
445   }
446
447 out:
448   if (G_IS_VALUE (&v))
449     g_value_unset (&v);
450   if (target)
451     gst_object_unref (target);
452   return;
453
454 error:
455   GST_CAT_ERROR (GST_CAT_PIPELINE, "could not set property \"%s\" in %"
456       GST_PTR_FORMAT, pspec->name, target);
457   goto out;
458 }
459
460 static gchar *
461 gst_parse_split_assignment (gchar *value) {
462   gchar *pos = value;
463
464   /* parse the string, so the property name is null-terminated and pos points
465      to the beginning of the value */
466   while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++;
467   if (*pos == '=') {
468     *pos = '\0';
469   } else {
470     *pos = '\0';
471     pos++;
472     while (g_ascii_isspace (*pos)) pos++;
473   }
474   pos++;
475   while (g_ascii_isspace (*pos)) pos++;
476   /* truncate a string if it is delimited with double quotes */
477   if (*pos == '"' && pos[strlen (pos) - 1] == '"') {
478     pos++;
479     pos[strlen (pos) - 1] = '\0';
480   }
481   gst_parse_unescape (pos);
482
483   return pos;
484 }
485
486 static gboolean
487 collect_value (GParamSpec *pspec, gchar *value_str, GValue *v)
488 {
489   gboolean got_value = FALSE;
490
491   GST_CAT_LOG (GST_CAT_PIPELINE, "parsing property %s as a %s",
492       pspec->name, g_type_name (pspec->value_type));
493
494   g_value_init (v, pspec->value_type);
495   if (gst_value_deserialize_with_pspec (v, value_str, pspec))
496     got_value = TRUE;
497   else if (g_type_is_a (pspec->value_type, GST_TYPE_ELEMENT)) {
498      GstElement *bin;
499
500      bin = gst_parse_bin_from_description_full (value_str, TRUE, NULL,
501          GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS | GST_PARSE_FLAG_PLACE_IN_BIN, NULL);
502      if (bin) {
503        g_value_set_object (v, bin);
504        got_value = TRUE;
505      }
506   }
507
508   return got_value;
509 }
510
511 static void gst_parse_element_preset (gchar *value, GstElement *element, graph_t *graph)
512 {
513   /* do nothing if preset is for missing element or its not a preset element */
514   if (element == NULL)
515     goto out;
516
517   if (!GST_IS_PRESET(element))
518     goto not_a_preset;
519
520   /* do nothing if no preset is given */
521   if (value == NULL || *value == '\0')
522     goto out;
523
524   gst_parse_unescape (value);
525   if (!gst_preset_load_preset (GST_PRESET (element), value))
526     goto error;
527
528 out:
529   return;
530
531 not_a_preset:
532   SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
533          _("Element \"%s\" is not a GstPreset"),
534          GST_ELEMENT_NAME (element));
535   goto out;
536
537 error:
538   SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
539          _("could not set preset \"%s\" in element \"%s\""),
540          value, GST_ELEMENT_NAME (element));
541   goto out;
542 }
543
544 typedef struct
545 {
546   gchar *name;
547   gchar *value;
548 } proxied_property_t;
549
550 static void
551 proxied_property_free (proxied_property_t *pp) {
552   g_slice_free (proxied_property_t, pp);
553 }
554
555 static GstElement * gst_parse_element_make (graph_t *graph, element_t *data) {
556   GstElementFactory *loaded_factory;
557   GstElementFactory *factory = gst_element_factory_find (data->factory_name);
558   GObjectClass *klass;
559   GParamSpec *pspec = NULL;
560   GSList *tmp;
561   gboolean is_proxy;
562   GSList *proxied = NULL;
563   guint n_params = 0;
564   guint n_params_alloc = 16;
565   const gchar **names_array;
566   GValue *values_array;
567   GstElement *ret = NULL;
568
569   if (!factory) {
570                 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, _("no element \"%s\""), data->factory_name);
571     return NULL;
572   }
573
574   loaded_factory =
575     GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
576         (factory)));
577
578   gst_object_unref (factory);
579
580   klass = g_type_class_ref (gst_element_factory_get_element_type (loaded_factory));
581
582   is_proxy = g_type_is_a (gst_element_factory_get_element_type (loaded_factory), GST_TYPE_CHILD_PROXY);
583
584   names_array = g_new0 (const gchar *, n_params_alloc);
585   values_array = g_new0 (GValue, n_params_alloc);
586
587   for (tmp = data->values; tmp; tmp = tmp->next) {
588     gchar *name = tmp->data;
589     gchar *value = gst_parse_split_assignment (tmp->data);
590
591     if (is_proxy && strstr (name, "::") != NULL) {
592       proxied_property_t *pp = g_slice_new (proxied_property_t);
593       pp->name = name;
594       pp->value = value;
595       proxied = g_slist_prepend (proxied, pp);
596       continue;
597     }
598
599     pspec = g_object_class_find_property (klass, name);
600
601     if (pspec != NULL) {
602       if (G_UNLIKELY (n_params == n_params_alloc)) {
603         n_params_alloc *= 2u;
604         names_array =
605             g_realloc (names_array, sizeof (const gchar *) * n_params_alloc);
606         values_array = g_realloc (values_array, sizeof (GValue) * n_params_alloc);
607         memset (&values_array[n_params], 0,
608             sizeof (GValue) * (n_params_alloc - n_params));
609       }
610
611       if (!collect_value (pspec, value, &values_array[n_params])) {
612         SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
613                _("could not set property \"%s\" in element \"%s\" to \"%s\""),
614          name, data->factory_name, value);
615         g_value_unset (&values_array[n_params]);
616         goto done;
617       } else {
618         names_array[n_params] = name;
619       }
620
621       ++n_params;
622     } else {
623       SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
624           _("no property \"%s\" in element \"%s\""), name, \
625           data->factory_name);
626       goto done;
627     }
628   }
629
630   ret = gst_element_factory_create_with_properties (factory, n_params, names_array,
631       values_array);
632
633   for (tmp = proxied; tmp; tmp = tmp->next) {
634     GObject *target = NULL;
635     proxied_property_t *pp = tmp->data;
636
637     if (!gst_child_proxy_lookup (GST_CHILD_PROXY (ret), pp->name, &target, &pspec)) {
638       /* the property was not found. if the target child doesn't exist
639          then we do a delayed set waiting for new elements to be added. If
640          the child was found, we fail since the property doesn't exist.
641       */
642       gchar *children = NULL;
643       gchar *property = NULL;
644       if (!gst_parse_separate_prop_from_children (pp->name, &children, &property)) {
645         /* malformed childproxy path, skip */
646         continue;
647       }
648
649       target = gst_child_proxy_get_child_by_name_recurse (GST_CHILD_PROXY (ret), children);
650       g_free (children);
651       g_free (property);
652
653       if (target == NULL) {
654         gst_parse_add_delayed_set (ret, pp->name, pp->value);
655       } else {
656         gst_object_unref (target);
657         SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY,      \
658           _("no property \"%s\" in element \"%s\""), pp->name, \
659           GST_ELEMENT_NAME (ret));
660         goto done;
661       }
662     } else {
663       GValue v = { 0, };
664
665       if (!collect_value (pspec, pp->value, &v)) {
666         SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
667                _("could not set property \"%s\" in child of element \"%s\" to \"%s\""),
668          pp->name, data->factory_name, pp->value);
669         g_value_unset (&v);
670         goto done;
671       } else {
672         g_object_set_property (target, pspec->name, &v);
673         g_value_unset (&v);
674       }
675     }
676   }
677
678   for (tmp = data->presets; tmp; tmp = tmp->next) {
679     gst_parse_element_preset (tmp->data, ret, graph);
680   }
681
682 done:
683   g_slist_free_full (proxied, (GDestroyNotify) proxied_property_free);
684   gst_object_unref (loaded_factory);
685   g_type_class_unref (klass);
686   g_free (names_array);
687   while (n_params--)
688     g_value_unset (&values_array[n_params]);
689   g_free (values_array);
690
691   return ret;
692 }
693
694 static gboolean gst_parse_child_proxy_find_child (
695     GstChildProxy * child_proxy, const gchar *name)
696 {
697   gchar **names = NULL, **current = NULL;
698   GObject *obj = NULL;
699   gboolean found = FALSE;
700
701   obj = G_OBJECT (g_object_ref (child_proxy));
702
703   current = names = g_strsplit (name, "::", -1);
704
705   /* find the owner of the property */
706   while (current[1]) {
707     GObject *next = NULL;
708
709     /* Cannot ask for the child of a non-childproxy */
710     if (!GST_IS_CHILD_PROXY (obj)) {
711       break;
712     }
713
714     next = gst_child_proxy_get_child_by_name (GST_CHILD_PROXY (obj),
715         current[0]);
716
717     /* The child doesn't exist yet */
718     if (!next) {
719       break;
720     }
721
722     gst_object_unref (obj);
723     obj = next;
724     current++;
725   }
726
727   gst_object_unref (obj);
728   g_strfreev (names);
729
730   /* The remaining name is the property, we have found the object */
731   if (current[1] == NULL) {
732     found = TRUE;
733   }
734
735   return found;
736 }
737
738 static void gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
739 {
740   GParamSpec *pspec = NULL;
741   gchar *pos;
742   GValue v = { 0, };
743   GObject *target = NULL;
744
745   /* do nothing if assignment is for missing element */
746   if (element == NULL)
747     goto out;
748
749   pos = gst_parse_split_assignment (value);
750
751   if (GST_IS_CHILD_PROXY (element) && strstr (value, "::") != NULL) {
752     if (!gst_child_proxy_lookup (GST_CHILD_PROXY (element), value, &target, &pspec)) {
753       /* the property was not found. if the target child doesn't exist
754          then we do a delayed set waiting for new elements to be added. If
755          the child was found, we fail since the property doesn't exist.
756       */
757       if (!gst_parse_child_proxy_find_child (GST_CHILD_PROXY (element), value)) {
758         gst_parse_add_delayed_set (element, value, pos);
759       } else {
760         goto error;
761       }
762     }
763   } else {
764     pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value);
765     if (pspec != NULL) {
766       target = G_OBJECT (g_object_ref (element));
767       GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, target, "found %s property", value);
768     } else {
769       SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
770           _("no property \"%s\" in element \"%s\""), value, \
771           GST_ELEMENT_NAME (element));
772     }
773   }
774
775   if (pspec != NULL && target != NULL) {
776     if (!collect_value (pspec, pos, &v)) {
777       goto error;
778     } else {
779       g_object_set_property (target, pspec->name, &v);
780     }
781   }
782
783 out:
784   gst_parse_strfree (value);
785   if (G_IS_VALUE (&v))
786     g_value_unset (&v);
787   if (target)
788     gst_object_unref (target);
789   return;
790
791 error:
792   SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
793          _("could not set property \"%s\" in element \"%s\" to \"%s\""),
794          value, GST_ELEMENT_NAME (element), pos);
795   goto out;
796 }
797
798 static void gst_parse_free_reference (reference_t *rr)
799 {
800   if(rr->element) gst_object_unref(rr->element);
801   gst_parse_strfree (rr->name);
802   g_slist_foreach (rr->pads, (GFunc) gst_parse_strfree, NULL);
803   g_slist_free (rr->pads);
804 }
805
806 static void gst_parse_free_link (link_t *link)
807 {
808   gst_parse_free_reference (&(link->src));
809   gst_parse_free_reference (&(link->sink));
810   if (link->caps) gst_caps_unref (link->caps);
811   gst_parse_link_free (link);
812 }
813
814 static void gst_parse_free_chain (chain_t *ch)
815 {
816   GSList *walk;
817   gst_parse_free_reference (&(ch->first));
818   gst_parse_free_reference (&(ch->last));
819   for(walk=ch->elements;walk;walk=walk->next)
820     gst_object_unref (walk->data);
821   g_slist_free (ch->elements);
822   gst_parse_chain_free (ch);
823 }
824
825 static void gst_parse_free_element (element_t *el)
826 {
827   g_slist_free_full (el->values, gst_parse_strfree);
828   g_slist_free_full (el->presets, gst_parse_strfree);
829   gst_parse_strfree (el->factory_name);
830   gst_parse_element_free (el);
831 }
832
833 static void gst_parse_free_delayed_link (DelayedLink *link)
834 {
835   g_free (link->src_pad);
836   g_free (link->sink_pad);
837   if (link->caps) gst_caps_unref (link->caps);
838   g_slice_free (DelayedLink, link);
839 }
840
841 #define PRETTY_PAD_NAME_FMT "%s %s of %s named %s"
842 #define PRETTY_PAD_NAME_ARGS(elem, pad_name) \
843   (pad_name ? "pad " : "some"), (pad_name ? pad_name : "pad"), \
844   G_OBJECT_TYPE_NAME(elem), GST_STR_NULL (GST_ELEMENT_NAME (elem))
845
846 static void gst_parse_no_more_pads (GstElement *src, gpointer data)
847 {
848   DelayedLink *link = data;
849
850   /* Don't warn for all-pads links, as we expect those to
851    * still be active at no-more-pads */
852   if (!link->all_pads) {
853     GST_ELEMENT_WARNING(src, PARSE, DELAYED_LINK,
854       (_("Delayed linking failed.")),
855       ("failed delayed linking " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
856           PRETTY_PAD_NAME_ARGS (src, link->src_pad),
857           PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad)));
858   }
859   /* we keep the handlers connected, so that in case an element still adds a pad
860    * despite no-more-pads, we will consider it for pending delayed links */
861 }
862
863 static void gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
864 {
865   DelayedLink *link = data;
866
867   GST_CAT_INFO (GST_CAT_PIPELINE,
868                 "trying delayed linking %s " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
869                             link->all_pads ? "all pads" : "one pad",
870                 PRETTY_PAD_NAME_ARGS (src, link->src_pad),
871                 PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad));
872
873   if (gst_element_link_pads_filtered (src, link->src_pad, link->sink,
874       link->sink_pad, link->caps)) {
875     /* do this here, we don't want to get any problems later on when
876      * unlocking states */
877     GST_CAT_DEBUG (GST_CAT_PIPELINE,
878                    "delayed linking %s " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT " worked",
879                                link->all_pads ? "all pads" : "one pad",
880                    PRETTY_PAD_NAME_ARGS (src, link->src_pad),
881                    PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad));
882     /* releases 'link' */
883     if (!link->all_pads) {
884       g_signal_handler_disconnect (src, link->no_more_pads_signal_id);
885       g_signal_handler_disconnect (src, link->pad_added_signal_id);
886     }
887   }
888 }
889
890 /* both padnames and the caps may be NULL */
891 static gboolean
892 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad,
893                                 GstElement *sink, const gchar *sink_pad,
894                                 GstCaps *caps, gboolean all_pads)
895 {
896   GList *templs = gst_element_class_get_pad_template_list (
897       GST_ELEMENT_GET_CLASS (src));
898
899   for (; templs; templs = templs->next) {
900     GstPadTemplate *templ = (GstPadTemplate *) templs->data;
901     if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) &&
902         (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
903     {
904       DelayedLink *data = g_slice_new (DelayedLink);
905
906       data->all_pads = all_pads;
907
908       /* TODO: maybe we should check if src_pad matches this template's names */
909
910       GST_CAT_DEBUG (GST_CAT_PIPELINE,
911                      "trying delayed link " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
912                      PRETTY_PAD_NAME_ARGS (src, src_pad),
913                      PRETTY_PAD_NAME_ARGS (sink, sink_pad));
914
915       data->src_pad = g_strdup (src_pad);
916       data->sink = sink;
917       data->sink_pad = g_strdup (sink_pad);
918       if (caps) {
919         data->caps = gst_caps_copy (caps);
920       } else {
921         data->caps = NULL;
922       }
923       data->pad_added_signal_id = g_signal_connect_data (src, "pad-added",
924           G_CALLBACK (gst_parse_found_pad), data,
925           (GClosureNotify) gst_parse_free_delayed_link, (GConnectFlags) 0);
926       data->no_more_pads_signal_id = g_signal_connect (src, "no-more-pads",
927           G_CALLBACK (gst_parse_no_more_pads), data);
928       return TRUE;
929     }
930   }
931   return FALSE;
932 }
933
934 static gboolean
935 gst_parse_element_can_do_caps (GstElement * e, GstPadDirection dir,
936     GstCaps * link_caps)
937 {
938   gboolean can_do = FALSE, done = FALSE;
939   GstIterator *it;
940
941   it = (dir == GST_PAD_SRC) ? gst_element_iterate_src_pads (e) : gst_element_iterate_sink_pads (e);
942
943   while (!done && !can_do) {
944     GValue v = G_VALUE_INIT;
945     GstPad *pad;
946     GstCaps *caps;
947
948     switch (gst_iterator_next (it, &v)) {
949       case GST_ITERATOR_OK:
950         pad = g_value_get_object (&v);
951
952         caps = gst_pad_get_current_caps (pad);
953         if (caps == NULL)
954           caps = gst_pad_query_caps (pad, NULL);
955
956         can_do = gst_caps_can_intersect (caps, link_caps);
957
958         GST_TRACE ("can_do: %d for %" GST_PTR_FORMAT " and %" GST_PTR_FORMAT,
959             can_do, caps, link_caps);
960
961         gst_caps_unref (caps);
962
963         g_value_unset (&v);
964         break;
965       case GST_ITERATOR_DONE:
966       case GST_ITERATOR_ERROR:
967         done = TRUE;
968         break;
969       case GST_ITERATOR_RESYNC:
970         gst_iterator_resync (it);
971         break;
972     }
973   }
974
975   gst_iterator_free (it);
976
977   return can_do;
978 }
979
980 /*
981  * performs a link and frees the struct. src and sink elements must be given
982  * return values   0 - link performed
983  *                 1 - link delayed
984  *                <0 - error
985  */
986 static gint
987 gst_parse_perform_link (link_t *link, graph_t *graph)
988 {
989   GstElement *src = link->src.element;
990   GstElement *sink = link->sink.element;
991   GSList *srcs = link->src.pads;
992   GSList *sinks = link->sink.pads;
993   g_assert (GST_IS_ELEMENT (src));
994   g_assert (GST_IS_ELEMENT (sink));
995
996   GST_CAT_INFO (GST_CAT_PIPELINE,
997       "linking " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT " (%u/%u) with caps \"%" GST_PTR_FORMAT "\"",
998       PRETTY_PAD_NAME_ARGS (src, link->src.name),
999       PRETTY_PAD_NAME_ARGS (sink, link->sink.name),
1000       g_slist_length (srcs), g_slist_length (sinks), link->caps);
1001
1002   if (!srcs || !sinks) {
1003     gboolean found_one = gst_element_link_pads_filtered (src,
1004         srcs ? (const gchar *) srcs->data : NULL, sink,
1005         sinks ? (const gchar *) sinks->data : NULL, link->caps);
1006
1007     if (found_one) {
1008       if (!link->all_pads)
1009         goto success; /* Linked one, and not an all-pads link = we're done */
1010
1011       /* Try and link more available pads */
1012       while (gst_element_link_pads_filtered (src,
1013         srcs ? (const gchar *) srcs->data : NULL, sink,
1014         sinks ? (const gchar *) sinks->data : NULL, link->caps));
1015     }
1016
1017     /* We either didn't find any static pads, or this is a all-pads link,
1018      * in which case watch for future pads and link those. Not a failure
1019      * in the all-pads case if there's no sometimes pads to watch */
1020     if (gst_parse_perform_delayed_link (src,
1021           srcs ? (const gchar *) srcs->data : NULL,
1022           sink, sinks ? (const gchar *) sinks->data : NULL, link->caps,
1023           link->all_pads) || link->all_pads) {
1024       goto success;
1025     } else {
1026       goto error;
1027     }
1028   }
1029   if (g_slist_length (link->src.pads) != g_slist_length (link->sink.pads)) {
1030     goto error;
1031   }
1032   while (srcs && sinks) {
1033     const gchar *src_pad = (const gchar *) srcs->data;
1034     const gchar *sink_pad = (const gchar *) sinks->data;
1035     srcs = g_slist_next (srcs);
1036     sinks = g_slist_next (sinks);
1037     if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad,
1038         link->caps)) {
1039       continue;
1040     } else {
1041       if (gst_parse_perform_delayed_link (src, src_pad,
1042                                           sink, sink_pad,
1043                                           link->caps, link->all_pads)) {
1044         continue;
1045       } else {
1046         goto error;
1047       }
1048     }
1049   }
1050
1051 success:
1052   gst_parse_free_link (link);
1053   return 0;
1054
1055 error:
1056   if (link->caps != NULL) {
1057     gboolean src_can_do_caps, sink_can_do_caps;
1058     gchar *caps_str = gst_caps_to_string (link->caps);
1059
1060     src_can_do_caps =
1061         gst_parse_element_can_do_caps (src, GST_PAD_SRC, link->caps);
1062     sink_can_do_caps =
1063         gst_parse_element_can_do_caps (sink, GST_PAD_SINK, link->caps);
1064
1065     if (!src_can_do_caps && sink_can_do_caps) {
1066       SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1067           _("could not link %s to %s, %s can't handle caps %s"),
1068           GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink),
1069           GST_ELEMENT_NAME (src), caps_str);
1070     } else if (src_can_do_caps && !sink_can_do_caps) {
1071       SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1072           _("could not link %s to %s, %s can't handle caps %s"),
1073           GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink),
1074           GST_ELEMENT_NAME (sink), caps_str);
1075     } else if (!src_can_do_caps && !sink_can_do_caps) {
1076       SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1077           _("could not link %s to %s, neither element can handle caps %s"),
1078           GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink), caps_str);
1079     } else {
1080       SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1081           _("could not link %s to %s with caps %s"),
1082           GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink), caps_str);
1083     }
1084     g_free (caps_str);
1085   } else {
1086     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1087         _("could not link %s to %s"), GST_ELEMENT_NAME (src),
1088         GST_ELEMENT_NAME (sink));
1089   }
1090   gst_parse_free_link (link);
1091   return -1;
1092 }
1093
1094
1095 static int yyerror (void *scanner, graph_t *graph, const char *s);
1096 %}
1097
1098 %union {
1099     gchar *ss;
1100     chain_t *cc;
1101     link_t *ll;
1102     reference_t rr;
1103     element_t *ee;
1104     GSList *pp;
1105     graph_t *gg;
1106 }
1107
1108 /* No grammar ambiguities expected, FAIL otherwise */
1109 %expect 0
1110
1111 %token <ss> PARSE_URL
1112 %token <ss> IDENTIFIER
1113 %left  <ss> REF PADREF BINREF
1114 %token <ss> ASSIGNMENT PRESET
1115 %token <ss> LINK
1116 %token <ss> LINK_ALL
1117
1118 %type <ss> binopener
1119 %type <gg> graph
1120 %type <cc> chain bin chainlist openchain elementary
1121 %type <rr> reference
1122 %type <ll> link
1123 %type <ee> element
1124 %type <pp> morepads pads assignments
1125
1126 %destructor {   gst_parse_strfree ($$);         } <ss>
1127 %destructor {   if($$)
1128                   gst_parse_free_chain($$);     } <cc>
1129 %destructor {   gst_parse_free_link ($$);       } <ll>
1130 %destructor {   gst_parse_free_reference(&($$));} <rr>
1131 %destructor {   gst_parse_free_element ($$);            } <ee>
1132 %destructor {   GSList *walk;
1133                 for(walk=$$;walk;walk=walk->next)
1134                   gst_parse_strfree (walk->data);
1135                 g_slist_free ($$);              } <pp>
1136
1137
1138
1139 %left '(' ')'
1140 %left ','
1141 %right '.'
1142 %left '!' '=' ':'
1143
1144 %lex-param { void *scanner }
1145 %parse-param { void *scanner }
1146 %parse-param { graph_t *graph }
1147 @BISON_PURE_PARSER@
1148
1149 %start graph
1150 %%
1151
1152 /*************************************************************
1153 * Grammar explanation:
1154 *   _element_s are specified by an identifier of their type.
1155 *   a name can be give in the optional property-assignments
1156 *       coffeeelement
1157 *       fakesrc name=john
1158 *       identity silence=false name=frodo
1159 *   (cont'd)
1160 **************************************************************/
1161 element: IDENTIFIER              {
1162             $$ = gst_parse_element_new();
1163             $$->factory_name = $1;
1164                                               }
1165   | element PRESET                {
1166             $$->presets = g_slist_append ($$->presets, $2);
1167                                                 $$ = $1;
1168                                               }
1169         |       element ASSIGNMENT            {
1170             $$->values = g_slist_append ($$->values, $2);
1171                                                 $$ = $1;
1172                                               }
1173         ;
1174
1175 /*************************************************************
1176 * Grammar explanation: (cont'd)
1177 *   a graph has (pure) _element_s, _bin_s and _link_s.
1178 *   since bins are special elements, bins and elements can
1179 *   be generalized as _elementary_.
1180 *   The construction of _bin_s will be discussed later.
1181 *   (cont'd)
1182 *
1183 **************************************************************/
1184 elementary:
1185         element                               {
1186             GstElement *element = NULL;
1187
1188             $$ = gst_parse_chain_new ();
1189
1190             if ($1 && !(element = gst_parse_element_make (graph, $1))) {
1191                                                   add_missing_element(graph, $1->factory_name);
1192             } else {
1193               /* g_print ("@%p: CHAINing elementary\n", $$); */
1194               $$->first.element = element ? gst_object_ref(element) : NULL;
1195               $$->last.element = element ? gst_object_ref(element) : NULL;
1196               $$->first.name = $$->last.name = NULL;
1197               $$->first.pads = $$->last.pads = NULL;
1198               $$->elements = element ? g_slist_prepend (NULL, element) : NULL;
1199             }
1200
1201             gst_parse_free_element ($1);
1202
1203                                               }
1204         | bin                                 { $$=$1; }
1205         ;
1206
1207 /*************************************************************
1208 * Grammar explanation: (cont'd)
1209 *   a _chain_ is a list of _elementary_s that have _link_s in between
1210 *   which are represented through infix-notation.
1211 *
1212 *       fakesrc ! sometransformation ! fakesink
1213 *
1214 *   every _link_ can be augmented with _pads_.
1215 *
1216 *       coffeesrc .sound ! speakersink
1217 *       multisrc  .movie,ads ! .projector,smallscreen multisink
1218 *
1219 *   and every _link_ can be setup to filter media-types
1220 *       mediasrc ! audio/x-raw, signed=TRUE ! stereosink
1221 *
1222 * User HINT:
1223 *   if the lexer does not recognize your media-type it
1224 *   will make it an element name. that results in errors
1225 *   like
1226 *       NO SUCH ELEMENT: no element audio7x-raw
1227 *   '7' vs. '/' in https://en.wikipedia.org/wiki/QWERTZ
1228 *
1229 * Parsing HINT:
1230 *   in the parser we need to differ between chains that can
1231 *   be extended by more elementaries (_openchain_) and others
1232 *   that are syntactically closed (handled later in this file).
1233 *       (e.g. fakesrc ! sinkreferencename.padname)
1234 **************************************************************/
1235 chain:  openchain                             { $$=$1;
1236                                                 if($$->last.name){
1237                                                         SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
1238                                                         _("unexpected reference \"%s\" - ignoring"), $$->last.name);
1239                                                         gst_parse_strfree($$->last.name);
1240                                                         $$->last.name=NULL;
1241                                                 }
1242                                                 if($$->last.pads){
1243                                                         SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
1244                                                         _("unexpected pad-reference \"%s\" - ignoring"), (gchar*)$$->last.pads->data);
1245                                                         g_slist_foreach ($$->last.pads, (GFunc) gst_parse_strfree, NULL);
1246                                                         g_slist_free ($$->last.pads);
1247                                                         $$->last.pads=NULL;
1248                                                 }
1249                                               }
1250         ;
1251
1252 openchain:
1253         elementary pads                       { $$=$1;
1254                                                 $$->last.pads = g_slist_concat ($$->last.pads, $2);
1255                                                 /* g_print ("@%p@%p: FKI elementary pads\n", $1, $$->last.pads); */
1256                                               }
1257         | openchain link pads elementary pads
1258                                               {
1259                                                 $2->src  = $1->last;
1260                                                 $2->sink = $4->first;
1261                                                 $2->sink.pads = g_slist_concat ($3, $2->sink.pads);
1262                                                 TRY_SETUP_LINK($2);
1263                                                 $4->first = $1->first;
1264                                                 $4->elements = g_slist_concat ($1->elements, $4->elements);
1265                                                 gst_parse_chain_free($1);
1266                                                 $$ = $4;
1267                                                 $$->last.pads = g_slist_concat ($$->last.pads, $5);
1268                                               }
1269         ;
1270
1271 link:   LINK                                  { $$ = gst_parse_link_new ();
1272                                                 $$->all_pads = FALSE;
1273                                                 if ($1) {
1274                                                   $$->caps = gst_caps_from_string ($1);
1275                                                   if ($$->caps == NULL)
1276                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $1);
1277                                                   gst_parse_strfree ($1);
1278                                                 }
1279                                               }
1280         | LINK_ALL                            { $$ = gst_parse_link_new ();
1281                                                 $$->all_pads = TRUE;
1282                                                 if ($1) {
1283                                                   $$->caps = gst_caps_from_string ($1);
1284                                                   if ($$->caps == NULL)
1285                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $1);
1286                                                   gst_parse_strfree ($1);
1287                                                 }
1288                                               }
1289         ;
1290 pads:           /* NOP */                     { $$ = NULL; }
1291         |       PADREF morepads               { $$ = $2;
1292                                                 $$ = g_slist_prepend ($$, $1);
1293                                               }
1294         ;
1295 morepads:       /* NOP */                     { $$ = NULL; }
1296         |       ',' IDENTIFIER morepads       { $$ = g_slist_prepend ($3, $2); }
1297         ;
1298
1299 /*************************************************************
1300 * Grammar explanation: (cont'd)
1301 *   the first and last elements of a _chain_ can be give
1302 *   as URL. This creates special elements that fit the URL.
1303 *
1304 *       fakesrc ! http://fake-sink.org
1305 *       http://somesource.org ! fakesink
1306 **************************************************************/
1307
1308 chain:  openchain link PARSE_URL              { GstElement *element =
1309                                                           gst_element_make_from_uri (GST_URI_SINK, $3, NULL, NULL);
1310                                                 /* FIXME: get and parse error properly */
1311                                                 if (!element) {
1312                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1313                                                           _("no sink element for URI \"%s\""), $3);
1314                                                 }
1315                                                 $$ = $1;
1316                                                 $2->sink.element = element?gst_object_ref(element):NULL;
1317                                                 $2->src = $1->last;
1318                                                 TRY_SETUP_LINK($2);
1319                                                 $$->last.element = NULL;
1320                                                 $$->last.name = NULL;
1321                                                 $$->last.pads = NULL;
1322                                                 if(element) $$->elements = g_slist_append ($$->elements, element);
1323                                                 g_free ($3);
1324                                               }
1325         ;
1326 openchain:
1327         PARSE_URL                             { GstElement *element =
1328                                                           gst_element_make_from_uri (GST_URI_SRC, $1, NULL, NULL);
1329                                                 /* FIXME: get and parse error properly */
1330                                                 if (!element) {
1331                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1332                                                     _("no source element for URI \"%s\""), $1);
1333                                                 }
1334                                                 $$ = gst_parse_chain_new ();
1335                                                 /* g_print ("@%p: CHAINing srcURL\n", $$); */
1336                                                 $$->first.element = NULL;
1337                                                 $$->first.name = NULL;
1338                                                 $$->first.pads = NULL;
1339                                                 $$->last.element = element ? gst_object_ref(element):NULL;
1340                                                 $$->last.name = NULL;
1341                                                 $$->last.pads = NULL;
1342                                                 $$->elements = element ? g_slist_prepend (NULL, element)  : NULL;
1343                                                 g_free($1);
1344                                               }
1345         ;
1346
1347
1348 /*************************************************************
1349 * Grammar explanation: (cont'd)
1350 *   the first and last elements of a _chain_ can be linked
1351 *   to a named _reference_ (with optional pads).
1352 *
1353 *       fakesrc ! nameOfSinkElement.
1354 *       fakesrc ! nameOfSinkElement.Padname
1355 *       fakesrc ! nameOfSinkElement.Padname, anotherPad
1356 *       nameOfSource.Padname ! fakesink
1357 **************************************************************/
1358
1359 chain:  openchain link reference              { $$ = $1;
1360                                                 $2->sink= $3;
1361                                                 $2->src = $1->last;
1362                                                 TRY_SETUP_LINK($2);
1363                                                 $$->last.element = NULL;
1364                                                 $$->last.name = NULL;
1365                                                 $$->last.pads = NULL;
1366                                               }
1367         ;
1368
1369
1370 openchain:
1371         reference                             { $$ = gst_parse_chain_new ();
1372                                                 $$->last=$1;
1373                                                 $$->first.element = NULL;
1374                                                 $$->first.name = NULL;
1375                                                 $$->first.pads = NULL;
1376                                                 $$->elements = NULL;
1377                                               }
1378         ;
1379 reference:      REF morepads                  {
1380                                                 gchar *padname = $1;
1381                                                 GSList *pads = $2;
1382                                                 if (padname) {
1383                                                   while (*padname != '.') padname++;
1384                                                   *padname = '\0';
1385                                                   padname++;
1386                                                   if (*padname != '\0')
1387                                                     pads = g_slist_prepend (pads, gst_parse_strdup (padname));
1388                                                 }
1389                                                 $$.element=NULL;
1390                                                 $$.name=$1;
1391                                                 $$.pads=pads;
1392                                               }
1393         ;
1394
1395
1396 /*************************************************************
1397 * Grammar explanation: (cont'd)
1398 *   a _chainlist_ is just a list of _chain_s.
1399 *
1400 *   You can specify _link_s with named
1401 *   _reference_ on each side. That
1402 *   works already after the explanations above.
1403 *       someSourceName.Pad ! someSinkName.
1404 *       someSourceName.Pad,anotherPad ! someSinkName.Apad,Bpad
1405 *
1406 *   If a syntax error occurs, the already finished _chain_s
1407 *   and _links_ are kept intact.
1408 *************************************************************/
1409
1410 chainlist: /* NOP */                          { $$ = NULL; }
1411         | chainlist chain                     { if ($1){
1412                                                   gst_parse_free_reference(&($1->last));
1413                                                   gst_parse_free_reference(&($2->first));
1414                                                   $2->first = $1->first;
1415                                                   $2->elements = g_slist_concat ($1->elements, $2->elements);
1416                                                   gst_parse_chain_free ($1);
1417                                                 }
1418                                                 $$ = $2;
1419                                               }
1420         | chainlist error                     { $$=$1;
1421                                                 GST_CAT_DEBUG (GST_CAT_PIPELINE,"trying to recover from syntax error");
1422                                                 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX, _("syntax error"));
1423                                               }
1424         ;
1425
1426 /*************************************************************
1427 * Grammar explanation: (cont'd)
1428 *   _bins_
1429 *************************************************************/
1430
1431
1432 assignments:    /* NOP */                     { $$ = NULL; }
1433         |       ASSIGNMENT assignments        { $$ = g_slist_prepend ($2, $1); }
1434         ;
1435
1436 binopener:      '('                           { $$ = gst_parse_strdup("bin"); }
1437         |       BINREF                        { $$ = $1; }
1438         ;
1439 bin:    binopener assignments chainlist ')'   {
1440                                                 chain_t *chain = $3;
1441                                                 GSList *walk;
1442                                                 GstBin *bin = (GstBin *) gst_element_factory_make ($1, NULL);
1443                                                 if (!chain) {
1444                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY_BIN,
1445                                                     _("specified empty bin \"%s\", not allowed"), $1);
1446                                                   chain = gst_parse_chain_new ();
1447                                                   chain->first.element = chain->last.element = NULL;
1448                                                   chain->first.name    = chain->last.name    = NULL;
1449                                                   chain->first.pads    = chain->last.pads    = NULL;
1450                                                   chain->elements = NULL;
1451                                                 }
1452                                                 if (!bin) {
1453                                                   add_missing_element(graph, $1);
1454                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1455                                                     _("no bin \"%s\", unpacking elements"), $1);
1456                                                   /* clear property-list */
1457                                                   g_slist_foreach ($2, (GFunc) gst_parse_strfree, NULL);
1458                                                   g_slist_free ($2);
1459                                                   $2 = NULL;
1460                                                 } else {
1461                                                   for (walk = chain->elements; walk; walk = walk->next )
1462                                                     gst_bin_add (bin, GST_ELEMENT (walk->data));
1463                                                   g_slist_free (chain->elements);
1464                                                   chain->elements = g_slist_prepend (NULL, bin);
1465                                                 }
1466                                                 $$ = chain;
1467                                                 /* set the properties now
1468                                                  * HINT: property-list cleared above, if bin==NULL
1469                                                  */
1470                                                 for (walk = $2; walk; walk = walk->next)
1471                                                   gst_parse_element_set ((gchar *) walk->data,
1472                                                         GST_ELEMENT (bin), graph);
1473                                                 g_slist_free ($2);
1474                                                 gst_parse_strfree ($1);
1475                                               }
1476         ;
1477
1478 /*************************************************************
1479 * Grammar explanation: (cont'd)
1480 *   _graph_
1481 *************************************************************/
1482
1483 graph:  chainlist                             { $$ = graph;
1484                                                 $$->chain = $1;
1485                                                 if(!$1) {
1486                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
1487                                                 }
1488                                               }
1489         ;
1490
1491 %%
1492
1493
1494 static int
1495 yyerror (void *scanner, graph_t *graph, const char *s)
1496 {
1497   /* FIXME: This should go into the GError somehow, but how? */
1498   GST_WARNING ("Error during parsing: %s", s);
1499   return -1;
1500 }
1501
1502
1503 GstElement *
1504 priv_gst_parse_launch (const gchar *str, GError **error, GstParseContext *ctx,
1505     GstParseFlags flags)
1506 {
1507   graph_t g;
1508   gchar *dstr;
1509   GSList *walk;
1510   GstElement *ret;
1511   yyscan_t scanner;
1512
1513   g_return_val_if_fail (str != NULL, NULL);
1514   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1515
1516   g.chain = NULL;
1517   g.links = NULL;
1518   g.error = error;
1519   g.ctx = ctx;
1520   g.flags = flags;
1521
1522 #ifdef __GST_PARSE_TRACE
1523   GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
1524   __strings = __chains = __links = __elements = 0;
1525 #endif /* __GST_PARSE_TRACE */
1526
1527   /* g_print("Now scanning: %s\n", str); */
1528
1529   dstr = g_strdup (str);
1530   priv_gst_parse_yylex_init (&scanner);
1531   priv_gst_parse_yy_scan_string (dstr, scanner);
1532
1533 #if YYDEBUG
1534   yydebug = 1;
1535 #endif
1536
1537   if (yyparse (scanner, &g) != 0) {
1538     SET_ERROR (error, GST_PARSE_ERROR_SYNTAX,
1539         "Unrecoverable syntax error while parsing pipeline %s", str);
1540
1541     priv_gst_parse_yylex_destroy (scanner);
1542     g_free (dstr);
1543
1544     goto error1;
1545   }
1546   priv_gst_parse_yylex_destroy (scanner);
1547   g_free (dstr);
1548
1549   GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links",
1550       g.chain ? g_slist_length (g.chain->elements) : 0,
1551       g_slist_length (g.links));
1552
1553   /* ensure chain is not NULL */
1554   if (!g.chain){
1555     g.chain=gst_parse_chain_new ();
1556     g.chain->elements=NULL;
1557     g.chain->first.element=NULL;
1558     g.chain->first.name=NULL;
1559     g.chain->first.pads=NULL;
1560     g.chain->last.element=NULL;
1561     g.chain->last.name=NULL;
1562     g.chain->last.pads=NULL;
1563   };
1564
1565   /* ensure elements is not empty */
1566   if(!g.chain->elements){
1567     g.chain->elements= g_slist_prepend (NULL, NULL);
1568   };
1569
1570   /* put all elements in our bin if necessary */
1571   if(g.chain->elements->next){
1572     GstBin *bin;
1573     if (flags & GST_PARSE_FLAG_PLACE_IN_BIN)
1574       bin = GST_BIN (gst_element_factory_make ("bin", NULL));
1575     else
1576       bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
1577     g_assert (bin);
1578
1579     for (walk = g.chain->elements; walk; walk = walk->next) {
1580       if (walk->data != NULL)
1581         gst_bin_add (bin, GST_ELEMENT (walk->data));
1582     }
1583     g_slist_free (g.chain->elements);
1584     g.chain->elements = g_slist_prepend (NULL, bin);
1585   }
1586
1587   ret = (GstElement *) g.chain->elements->data;
1588   g_slist_free (g.chain->elements);
1589   g.chain->elements=NULL;
1590   gst_parse_free_chain (g.chain);
1591   g.chain = NULL;
1592
1593
1594   /* resolve and perform links */
1595   for (walk = g.links; walk; walk = walk->next) {
1596     link_t *l = (link_t *) walk->data;
1597     int err;
1598     err=gst_resolve_reference( &(l->src), ret);
1599     if (err) {
1600        if(-1==err){
1601           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1602               "No src-element named \"%s\" - omitting link", l->src.name);
1603        }else{
1604           /* probably a missing element which we've handled already */
1605           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1606               "No src-element found - omitting link");
1607        }
1608        gst_parse_free_link (l);
1609        continue;
1610     }
1611
1612     err=gst_resolve_reference( &(l->sink), ret);
1613     if (err) {
1614        if(-1==err){
1615           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1616               "No sink-element named \"%s\" - omitting link", l->src.name);
1617        }else{
1618           /* probably a missing element which we've handled already */
1619           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1620               "No sink-element found - omitting link");
1621        }
1622        gst_parse_free_link (l);
1623        continue;
1624     }
1625     gst_parse_perform_link (l, &g);
1626   }
1627   g_slist_free (g.links);
1628
1629 out:
1630 #ifdef __GST_PARSE_TRACE
1631   GST_CAT_DEBUG (GST_CAT_PIPELINE,
1632       "TRACE: %u strings, %u chains, %u links and %u elements left", __strings, __chains,
1633       __links, __elements);
1634   if (__strings || __chains || __links || __elements) {
1635     g_warning ("TRACE: %u strings, %u chains, %u links and %u elements left", __strings,
1636         __chains, __links, __elements);
1637   }
1638 #endif /* __GST_PARSE_TRACE */
1639
1640   return ret;
1641
1642 error1:
1643   if (g.chain) {
1644     gst_parse_free_chain (g.chain);
1645     g.chain=NULL;
1646   }
1647
1648   g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
1649   g_slist_free (g.links);
1650
1651   if (error)
1652     g_assert (*error);
1653   ret = NULL;
1654
1655   goto out;
1656 }