rtsp-server: fix memory leak
[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       gst_object_unref (target);
677     }
678   }
679
680   for (tmp = data->presets; tmp; tmp = tmp->next) {
681     gst_parse_element_preset (tmp->data, ret, graph);
682   }
683
684 done:
685   g_slist_free_full (proxied, (GDestroyNotify) proxied_property_free);
686   gst_object_unref (loaded_factory);
687   g_type_class_unref (klass);
688   g_free (names_array);
689   while (n_params--)
690     g_value_unset (&values_array[n_params]);
691   g_free (values_array);
692
693   return ret;
694 }
695
696 static gboolean gst_parse_child_proxy_find_child (
697     GstChildProxy * child_proxy, const gchar *name)
698 {
699   gchar **names = NULL, **current = NULL;
700   GObject *obj = NULL;
701   gboolean found = FALSE;
702
703   obj = G_OBJECT (g_object_ref (child_proxy));
704
705   current = names = g_strsplit (name, "::", -1);
706
707   /* find the owner of the property */
708   while (current[1]) {
709     GObject *next = NULL;
710
711     /* Cannot ask for the child of a non-childproxy */
712     if (!GST_IS_CHILD_PROXY (obj)) {
713       break;
714     }
715
716     next = gst_child_proxy_get_child_by_name (GST_CHILD_PROXY (obj),
717         current[0]);
718
719     /* The child doesn't exist yet */
720     if (!next) {
721       break;
722     }
723
724     gst_object_unref (obj);
725     obj = next;
726     current++;
727   }
728
729   gst_object_unref (obj);
730   g_strfreev (names);
731
732   /* The remaining name is the property, we have found the object */
733   if (current[1] == NULL) {
734     found = TRUE;
735   }
736
737   return found;
738 }
739
740 static void gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
741 {
742   GParamSpec *pspec = NULL;
743   gchar *pos;
744   GValue v = { 0, };
745   GObject *target = NULL;
746
747   /* do nothing if assignment is for missing element */
748   if (element == NULL)
749     goto out;
750
751   pos = gst_parse_split_assignment (value);
752
753   if (GST_IS_CHILD_PROXY (element) && strstr (value, "::") != NULL) {
754     if (!gst_child_proxy_lookup (GST_CHILD_PROXY (element), value, &target, &pspec)) {
755       /* the property was not found. if the target child doesn't exist
756          then we do a delayed set waiting for new elements to be added. If
757          the child was found, we fail since the property doesn't exist.
758       */
759       if (!gst_parse_child_proxy_find_child (GST_CHILD_PROXY (element), value)) {
760         gst_parse_add_delayed_set (element, value, pos);
761       } else {
762         goto error;
763       }
764     }
765   } else {
766     pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value);
767     if (pspec != NULL) {
768       target = G_OBJECT (g_object_ref (element));
769       GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, target, "found %s property", value);
770     } else {
771       SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
772           _("no property \"%s\" in element \"%s\""), value, \
773           GST_ELEMENT_NAME (element));
774     }
775   }
776
777   if (pspec != NULL && target != NULL) {
778     if (!collect_value (pspec, pos, &v)) {
779       goto error;
780     } else {
781       g_object_set_property (target, pspec->name, &v);
782     }
783   }
784
785 out:
786   gst_parse_strfree (value);
787   if (G_IS_VALUE (&v))
788     g_value_unset (&v);
789   if (target)
790     gst_object_unref (target);
791   return;
792
793 error:
794   SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
795          _("could not set property \"%s\" in element \"%s\" to \"%s\""),
796          value, GST_ELEMENT_NAME (element), pos);
797   goto out;
798 }
799
800 static void gst_parse_free_reference (reference_t *rr)
801 {
802   if(rr->element) gst_object_unref(rr->element);
803   gst_parse_strfree (rr->name);
804   g_slist_foreach (rr->pads, (GFunc) gst_parse_strfree, NULL);
805   g_slist_free (rr->pads);
806 }
807
808 static void gst_parse_free_link (link_t *link)
809 {
810   gst_parse_free_reference (&(link->src));
811   gst_parse_free_reference (&(link->sink));
812   if (link->caps) gst_caps_unref (link->caps);
813   gst_parse_link_free (link);
814 }
815
816 static void gst_parse_free_chain (chain_t *ch)
817 {
818   GSList *walk;
819   gst_parse_free_reference (&(ch->first));
820   gst_parse_free_reference (&(ch->last));
821   for(walk=ch->elements;walk;walk=walk->next)
822     gst_object_unref (walk->data);
823   g_slist_free (ch->elements);
824   gst_parse_chain_free (ch);
825 }
826
827 static void gst_parse_free_element (element_t *el)
828 {
829   g_slist_free_full (el->values, gst_parse_strfree);
830   g_slist_free_full (el->presets, gst_parse_strfree);
831   gst_parse_strfree (el->factory_name);
832   gst_parse_element_free (el);
833 }
834
835 static void gst_parse_free_delayed_link (DelayedLink *link)
836 {
837   g_free (link->src_pad);
838   g_free (link->sink_pad);
839   if (link->caps) gst_caps_unref (link->caps);
840   g_slice_free (DelayedLink, link);
841 }
842
843 #define PRETTY_PAD_NAME_FMT "%s %s of %s named %s"
844 #define PRETTY_PAD_NAME_ARGS(elem, pad_name) \
845   (pad_name ? "pad " : "some"), (pad_name ? pad_name : "pad"), \
846   G_OBJECT_TYPE_NAME(elem), GST_STR_NULL (GST_ELEMENT_NAME (elem))
847
848 static void gst_parse_no_more_pads (GstElement *src, gpointer data)
849 {
850   DelayedLink *link = data;
851
852   /* Don't warn for all-pads links, as we expect those to
853    * still be active at no-more-pads */
854   if (!link->all_pads) {
855     GST_ELEMENT_WARNING(src, PARSE, DELAYED_LINK,
856       (_("Delayed linking failed.")),
857       ("failed delayed linking " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
858           PRETTY_PAD_NAME_ARGS (src, link->src_pad),
859           PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad)));
860   }
861   /* we keep the handlers connected, so that in case an element still adds a pad
862    * despite no-more-pads, we will consider it for pending delayed links */
863 }
864
865 static void gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
866 {
867   DelayedLink *link = data;
868
869   GST_CAT_INFO (GST_CAT_PIPELINE,
870                 "trying delayed linking %s " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
871                             link->all_pads ? "all pads" : "one pad",
872                 PRETTY_PAD_NAME_ARGS (src, link->src_pad),
873                 PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad));
874
875   if (gst_element_link_pads_filtered (src, link->src_pad, link->sink,
876       link->sink_pad, link->caps)) {
877     /* do this here, we don't want to get any problems later on when
878      * unlocking states */
879     GST_CAT_DEBUG (GST_CAT_PIPELINE,
880                    "delayed linking %s " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT " worked",
881                                link->all_pads ? "all pads" : "one pad",
882                    PRETTY_PAD_NAME_ARGS (src, link->src_pad),
883                    PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad));
884     /* releases 'link' */
885     if (!link->all_pads) {
886       g_signal_handler_disconnect (src, link->no_more_pads_signal_id);
887       g_signal_handler_disconnect (src, link->pad_added_signal_id);
888     }
889   }
890 }
891
892 /* both padnames and the caps may be NULL */
893 static gboolean
894 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad,
895                                 GstElement *sink, const gchar *sink_pad,
896                                 GstCaps *caps, gboolean all_pads)
897 {
898   GList *templs = gst_element_class_get_pad_template_list (
899       GST_ELEMENT_GET_CLASS (src));
900
901   for (; templs; templs = templs->next) {
902     GstPadTemplate *templ = (GstPadTemplate *) templs->data;
903     if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) &&
904         (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
905     {
906       DelayedLink *data = g_slice_new (DelayedLink);
907
908       data->all_pads = all_pads;
909
910       /* TODO: maybe we should check if src_pad matches this template's names */
911
912       GST_CAT_DEBUG (GST_CAT_PIPELINE,
913                      "trying delayed link " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
914                      PRETTY_PAD_NAME_ARGS (src, src_pad),
915                      PRETTY_PAD_NAME_ARGS (sink, sink_pad));
916
917       data->src_pad = g_strdup (src_pad);
918       data->sink = sink;
919       data->sink_pad = g_strdup (sink_pad);
920       if (caps) {
921         data->caps = gst_caps_copy (caps);
922       } else {
923         data->caps = NULL;
924       }
925       data->pad_added_signal_id = g_signal_connect_data (src, "pad-added",
926           G_CALLBACK (gst_parse_found_pad), data,
927           (GClosureNotify) gst_parse_free_delayed_link, (GConnectFlags) 0);
928       data->no_more_pads_signal_id = g_signal_connect (src, "no-more-pads",
929           G_CALLBACK (gst_parse_no_more_pads), data);
930       return TRUE;
931     }
932   }
933   return FALSE;
934 }
935
936 static gboolean
937 gst_parse_element_can_do_caps (GstElement * e, GstPadDirection dir,
938     GstCaps * link_caps)
939 {
940   gboolean can_do = FALSE, done = FALSE;
941   GstIterator *it;
942
943   it = (dir == GST_PAD_SRC) ? gst_element_iterate_src_pads (e) : gst_element_iterate_sink_pads (e);
944
945   while (!done && !can_do) {
946     GValue v = G_VALUE_INIT;
947     GstPad *pad;
948     GstCaps *caps;
949
950     switch (gst_iterator_next (it, &v)) {
951       case GST_ITERATOR_OK:
952         pad = g_value_get_object (&v);
953
954         caps = gst_pad_get_current_caps (pad);
955         if (caps == NULL)
956           caps = gst_pad_query_caps (pad, NULL);
957
958         can_do = gst_caps_can_intersect (caps, link_caps);
959
960         GST_TRACE ("can_do: %d for %" GST_PTR_FORMAT " and %" GST_PTR_FORMAT,
961             can_do, caps, link_caps);
962
963         gst_caps_unref (caps);
964
965         g_value_unset (&v);
966         break;
967       case GST_ITERATOR_DONE:
968       case GST_ITERATOR_ERROR:
969         done = TRUE;
970         break;
971       case GST_ITERATOR_RESYNC:
972         gst_iterator_resync (it);
973         break;
974     }
975   }
976
977   gst_iterator_free (it);
978
979   return can_do;
980 }
981
982 /*
983  * performs a link and frees the struct. src and sink elements must be given
984  * return values   0 - link performed
985  *                 1 - link delayed
986  *                <0 - error
987  */
988 static gint
989 gst_parse_perform_link (link_t *link, graph_t *graph)
990 {
991   GstElement *src = link->src.element;
992   GstElement *sink = link->sink.element;
993   GSList *srcs = link->src.pads;
994   GSList *sinks = link->sink.pads;
995   g_assert (GST_IS_ELEMENT (src));
996   g_assert (GST_IS_ELEMENT (sink));
997
998   GST_CAT_INFO (GST_CAT_PIPELINE,
999       "linking " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT " (%u/%u) with caps \"%" GST_PTR_FORMAT "\"",
1000       PRETTY_PAD_NAME_ARGS (src, link->src.name),
1001       PRETTY_PAD_NAME_ARGS (sink, link->sink.name),
1002       g_slist_length (srcs), g_slist_length (sinks), link->caps);
1003
1004   if (!srcs || !sinks) {
1005     gboolean found_one = gst_element_link_pads_filtered (src,
1006         srcs ? (const gchar *) srcs->data : NULL, sink,
1007         sinks ? (const gchar *) sinks->data : NULL, link->caps);
1008
1009     if (found_one) {
1010       if (!link->all_pads)
1011         goto success; /* Linked one, and not an all-pads link = we're done */
1012
1013       /* Try and link more available pads */
1014       while (gst_element_link_pads_filtered (src,
1015         srcs ? (const gchar *) srcs->data : NULL, sink,
1016         sinks ? (const gchar *) sinks->data : NULL, link->caps));
1017     }
1018
1019     /* We either didn't find any static pads, or this is a all-pads link,
1020      * in which case watch for future pads and link those. Not a failure
1021      * in the all-pads case if there's no sometimes pads to watch */
1022     if (gst_parse_perform_delayed_link (src,
1023           srcs ? (const gchar *) srcs->data : NULL,
1024           sink, sinks ? (const gchar *) sinks->data : NULL, link->caps,
1025           link->all_pads) || link->all_pads) {
1026       goto success;
1027     } else {
1028       goto error;
1029     }
1030   }
1031   if (g_slist_length (link->src.pads) != g_slist_length (link->sink.pads)) {
1032     goto error;
1033   }
1034   while (srcs && sinks) {
1035     const gchar *src_pad = (const gchar *) srcs->data;
1036     const gchar *sink_pad = (const gchar *) sinks->data;
1037     srcs = g_slist_next (srcs);
1038     sinks = g_slist_next (sinks);
1039     if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad,
1040         link->caps)) {
1041       continue;
1042     } else {
1043       if (gst_parse_perform_delayed_link (src, src_pad,
1044                                           sink, sink_pad,
1045                                           link->caps, link->all_pads)) {
1046         continue;
1047       } else {
1048         goto error;
1049       }
1050     }
1051   }
1052
1053 success:
1054   gst_parse_free_link (link);
1055   return 0;
1056
1057 error:
1058   if (link->caps != NULL) {
1059     gboolean src_can_do_caps, sink_can_do_caps;
1060     gchar *caps_str = gst_caps_to_string (link->caps);
1061
1062     src_can_do_caps =
1063         gst_parse_element_can_do_caps (src, GST_PAD_SRC, link->caps);
1064     sink_can_do_caps =
1065         gst_parse_element_can_do_caps (sink, GST_PAD_SINK, link->caps);
1066
1067     if (!src_can_do_caps && sink_can_do_caps) {
1068       SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1069           _("could not link %s to %s, %s can't handle caps %s"),
1070           GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink),
1071           GST_ELEMENT_NAME (src), caps_str);
1072     } else if (src_can_do_caps && !sink_can_do_caps) {
1073       SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1074           _("could not link %s to %s, %s can't handle caps %s"),
1075           GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink),
1076           GST_ELEMENT_NAME (sink), caps_str);
1077     } else if (!src_can_do_caps && !sink_can_do_caps) {
1078       SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1079           _("could not link %s to %s, neither element can handle caps %s"),
1080           GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink), caps_str);
1081     } else {
1082       SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1083           _("could not link %s to %s with caps %s"),
1084           GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink), caps_str);
1085     }
1086     g_free (caps_str);
1087   } else {
1088     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1089         _("could not link %s to %s"), GST_ELEMENT_NAME (src),
1090         GST_ELEMENT_NAME (sink));
1091   }
1092   gst_parse_free_link (link);
1093   return -1;
1094 }
1095
1096
1097 static int yyerror (void *scanner, graph_t *graph, const char *s);
1098 %}
1099
1100 %union {
1101     gchar *ss;
1102     chain_t *cc;
1103     link_t *ll;
1104     reference_t rr;
1105     element_t *ee;
1106     GSList *pp;
1107     graph_t *gg;
1108 }
1109
1110 /* No grammar ambiguities expected, FAIL otherwise */
1111 %expect 0
1112
1113 %token <ss> PARSE_URL
1114 %token <ss> IDENTIFIER
1115 %left  <ss> REF PADREF BINREF
1116 %token <ss> ASSIGNMENT PRESET
1117 %token <ss> LINK
1118 %token <ss> LINK_ALL
1119
1120 %type <ss> binopener
1121 %type <gg> graph
1122 %type <cc> chain bin chainlist openchain elementary
1123 %type <rr> reference
1124 %type <ll> link
1125 %type <ee> element
1126 %type <pp> morepads pads assignments
1127
1128 %destructor {   gst_parse_strfree ($$);         } <ss>
1129 %destructor {   if($$)
1130                   gst_parse_free_chain($$);     } <cc>
1131 %destructor {   gst_parse_free_link ($$);       } <ll>
1132 %destructor {   gst_parse_free_reference(&($$));} <rr>
1133 %destructor {   gst_parse_free_element ($$);            } <ee>
1134 %destructor {   GSList *walk;
1135                 for(walk=$$;walk;walk=walk->next)
1136                   gst_parse_strfree (walk->data);
1137                 g_slist_free ($$);              } <pp>
1138
1139
1140
1141 %left '(' ')'
1142 %left ','
1143 %right '.'
1144 %left '!' '=' ':'
1145
1146 %lex-param { void *scanner }
1147 %parse-param { void *scanner }
1148 %parse-param { graph_t *graph }
1149 @BISON_PURE_PARSER@
1150
1151 %start graph
1152 %%
1153
1154 /*************************************************************
1155 * Grammar explanation:
1156 *   _element_s are specified by an identifier of their type.
1157 *   a name can be give in the optional property-assignments
1158 *       coffeeelement
1159 *       fakesrc name=john
1160 *       identity silence=false name=frodo
1161 *   (cont'd)
1162 **************************************************************/
1163 element: IDENTIFIER              {
1164             $$ = gst_parse_element_new();
1165             $$->factory_name = $1;
1166                                               }
1167   | element PRESET                {
1168             $$->presets = g_slist_append ($$->presets, $2);
1169                                                 $$ = $1;
1170                                               }
1171         |       element ASSIGNMENT            {
1172             $$->values = g_slist_append ($$->values, $2);
1173                                                 $$ = $1;
1174                                               }
1175         ;
1176
1177 /*************************************************************
1178 * Grammar explanation: (cont'd)
1179 *   a graph has (pure) _element_s, _bin_s and _link_s.
1180 *   since bins are special elements, bins and elements can
1181 *   be generalized as _elementary_.
1182 *   The construction of _bin_s will be discussed later.
1183 *   (cont'd)
1184 *
1185 **************************************************************/
1186 elementary:
1187         element                               {
1188             GstElement *element = NULL;
1189
1190             $$ = gst_parse_chain_new ();
1191
1192             if ($1 && !(element = gst_parse_element_make (graph, $1))) {
1193                                                   add_missing_element(graph, $1->factory_name);
1194             } else {
1195               /* g_print ("@%p: CHAINing elementary\n", $$); */
1196               $$->first.element = element ? gst_object_ref(element) : NULL;
1197               $$->last.element = element ? gst_object_ref(element) : NULL;
1198               $$->first.name = $$->last.name = NULL;
1199               $$->first.pads = $$->last.pads = NULL;
1200               $$->elements = element ? g_slist_prepend (NULL, element) : NULL;
1201             }
1202
1203             gst_parse_free_element ($1);
1204
1205                                               }
1206         | bin                                 { $$=$1; }
1207         ;
1208
1209 /*************************************************************
1210 * Grammar explanation: (cont'd)
1211 *   a _chain_ is a list of _elementary_s that have _link_s in between
1212 *   which are represented through infix-notation.
1213 *
1214 *       fakesrc ! sometransformation ! fakesink
1215 *
1216 *   every _link_ can be augmented with _pads_.
1217 *
1218 *       coffeesrc .sound ! speakersink
1219 *       multisrc  .movie,ads ! .projector,smallscreen multisink
1220 *
1221 *   and every _link_ can be setup to filter media-types
1222 *       mediasrc ! audio/x-raw, signed=TRUE ! stereosink
1223 *
1224 * User HINT:
1225 *   if the lexer does not recognize your media-type it
1226 *   will make it an element name. that results in errors
1227 *   like
1228 *       NO SUCH ELEMENT: no element audio7x-raw
1229 *   '7' vs. '/' in https://en.wikipedia.org/wiki/QWERTZ
1230 *
1231 * Parsing HINT:
1232 *   in the parser we need to differ between chains that can
1233 *   be extended by more elementaries (_openchain_) and others
1234 *   that are syntactically closed (handled later in this file).
1235 *       (e.g. fakesrc ! sinkreferencename.padname)
1236 **************************************************************/
1237 chain:  openchain                             { $$=$1;
1238                                                 if($$->last.name){
1239                                                         SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
1240                                                         _("unexpected reference \"%s\" - ignoring"), $$->last.name);
1241                                                         gst_parse_strfree($$->last.name);
1242                                                         $$->last.name=NULL;
1243                                                 }
1244                                                 if($$->last.pads){
1245                                                         SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
1246                                                         _("unexpected pad-reference \"%s\" - ignoring"), (gchar*)$$->last.pads->data);
1247                                                         g_slist_foreach ($$->last.pads, (GFunc) gst_parse_strfree, NULL);
1248                                                         g_slist_free ($$->last.pads);
1249                                                         $$->last.pads=NULL;
1250                                                 }
1251                                               }
1252         ;
1253
1254 openchain:
1255         elementary pads                       { $$=$1;
1256                                                 $$->last.pads = g_slist_concat ($$->last.pads, $2);
1257                                                 /* g_print ("@%p@%p: FKI elementary pads\n", $1, $$->last.pads); */
1258                                               }
1259         | openchain link pads elementary pads
1260                                               {
1261                                                 $2->src  = $1->last;
1262                                                 $2->sink = $4->first;
1263                                                 $2->sink.pads = g_slist_concat ($3, $2->sink.pads);
1264                                                 TRY_SETUP_LINK($2);
1265                                                 $4->first = $1->first;
1266                                                 $4->elements = g_slist_concat ($1->elements, $4->elements);
1267                                                 gst_parse_chain_free($1);
1268                                                 $$ = $4;
1269                                                 $$->last.pads = g_slist_concat ($$->last.pads, $5);
1270                                               }
1271         ;
1272
1273 link:   LINK                                  { $$ = gst_parse_link_new ();
1274                                                 $$->all_pads = FALSE;
1275                                                 if ($1) {
1276                                                   $$->caps = gst_caps_from_string ($1);
1277                                                   if ($$->caps == NULL)
1278                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $1);
1279                                                   gst_parse_strfree ($1);
1280                                                 }
1281                                               }
1282         | LINK_ALL                            { $$ = gst_parse_link_new ();
1283                                                 $$->all_pads = TRUE;
1284                                                 if ($1) {
1285                                                   $$->caps = gst_caps_from_string ($1);
1286                                                   if ($$->caps == NULL)
1287                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $1);
1288                                                   gst_parse_strfree ($1);
1289                                                 }
1290                                               }
1291         ;
1292 pads:           /* NOP */                     { $$ = NULL; }
1293         |       PADREF morepads               { $$ = $2;
1294                                                 $$ = g_slist_prepend ($$, $1);
1295                                               }
1296         ;
1297 morepads:       /* NOP */                     { $$ = NULL; }
1298         |       ',' IDENTIFIER morepads       { $$ = g_slist_prepend ($3, $2); }
1299         ;
1300
1301 /*************************************************************
1302 * Grammar explanation: (cont'd)
1303 *   the first and last elements of a _chain_ can be give
1304 *   as URL. This creates special elements that fit the URL.
1305 *
1306 *       fakesrc ! http://fake-sink.org
1307 *       http://somesource.org ! fakesink
1308 **************************************************************/
1309
1310 chain:  openchain link PARSE_URL              { GstElement *element =
1311                                                           gst_element_make_from_uri (GST_URI_SINK, $3, NULL, NULL);
1312                                                 /* FIXME: get and parse error properly */
1313                                                 if (!element) {
1314                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1315                                                           _("no sink element for URI \"%s\""), $3);
1316                                                 }
1317                                                 $$ = $1;
1318                                                 $2->sink.element = element?gst_object_ref(element):NULL;
1319                                                 $2->src = $1->last;
1320                                                 TRY_SETUP_LINK($2);
1321                                                 $$->last.element = NULL;
1322                                                 $$->last.name = NULL;
1323                                                 $$->last.pads = NULL;
1324                                                 if(element) $$->elements = g_slist_append ($$->elements, element);
1325                                                 g_free ($3);
1326                                               }
1327         ;
1328 openchain:
1329         PARSE_URL                             { GstElement *element =
1330                                                           gst_element_make_from_uri (GST_URI_SRC, $1, NULL, NULL);
1331                                                 /* FIXME: get and parse error properly */
1332                                                 if (!element) {
1333                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1334                                                     _("no source element for URI \"%s\""), $1);
1335                                                 }
1336                                                 $$ = gst_parse_chain_new ();
1337                                                 /* g_print ("@%p: CHAINing srcURL\n", $$); */
1338                                                 $$->first.element = NULL;
1339                                                 $$->first.name = NULL;
1340                                                 $$->first.pads = NULL;
1341                                                 $$->last.element = element ? gst_object_ref(element):NULL;
1342                                                 $$->last.name = NULL;
1343                                                 $$->last.pads = NULL;
1344                                                 $$->elements = element ? g_slist_prepend (NULL, element)  : NULL;
1345                                                 g_free($1);
1346                                               }
1347         ;
1348
1349
1350 /*************************************************************
1351 * Grammar explanation: (cont'd)
1352 *   the first and last elements of a _chain_ can be linked
1353 *   to a named _reference_ (with optional pads).
1354 *
1355 *       fakesrc ! nameOfSinkElement.
1356 *       fakesrc ! nameOfSinkElement.Padname
1357 *       fakesrc ! nameOfSinkElement.Padname, anotherPad
1358 *       nameOfSource.Padname ! fakesink
1359 **************************************************************/
1360
1361 chain:  openchain link reference              { $$ = $1;
1362                                                 $2->sink= $3;
1363                                                 $2->src = $1->last;
1364                                                 TRY_SETUP_LINK($2);
1365                                                 $$->last.element = NULL;
1366                                                 $$->last.name = NULL;
1367                                                 $$->last.pads = NULL;
1368                                               }
1369         ;
1370
1371
1372 openchain:
1373         reference                             { $$ = gst_parse_chain_new ();
1374                                                 $$->last=$1;
1375                                                 $$->first.element = NULL;
1376                                                 $$->first.name = NULL;
1377                                                 $$->first.pads = NULL;
1378                                                 $$->elements = NULL;
1379                                               }
1380         ;
1381 reference:      REF morepads                  {
1382                                                 gchar *padname = $1;
1383                                                 GSList *pads = $2;
1384                                                 if (padname) {
1385                                                   while (*padname != '.') padname++;
1386                                                   *padname = '\0';
1387                                                   padname++;
1388                                                   if (*padname != '\0')
1389                                                     pads = g_slist_prepend (pads, gst_parse_strdup (padname));
1390                                                 }
1391                                                 $$.element=NULL;
1392                                                 $$.name=$1;
1393                                                 $$.pads=pads;
1394                                               }
1395         ;
1396
1397
1398 /*************************************************************
1399 * Grammar explanation: (cont'd)
1400 *   a _chainlist_ is just a list of _chain_s.
1401 *
1402 *   You can specify _link_s with named
1403 *   _reference_ on each side. That
1404 *   works already after the explanations above.
1405 *       someSourceName.Pad ! someSinkName.
1406 *       someSourceName.Pad,anotherPad ! someSinkName.Apad,Bpad
1407 *
1408 *   If a syntax error occurs, the already finished _chain_s
1409 *   and _links_ are kept intact.
1410 *************************************************************/
1411
1412 chainlist: /* NOP */                          { $$ = NULL; }
1413         | chainlist chain                     { if ($1){
1414                                                   gst_parse_free_reference(&($1->last));
1415                                                   gst_parse_free_reference(&($2->first));
1416                                                   $2->first = $1->first;
1417                                                   $2->elements = g_slist_concat ($1->elements, $2->elements);
1418                                                   gst_parse_chain_free ($1);
1419                                                 }
1420                                                 $$ = $2;
1421                                               }
1422         | chainlist error                     { $$=$1;
1423                                                 GST_CAT_DEBUG (GST_CAT_PIPELINE,"trying to recover from syntax error");
1424                                                 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX, _("syntax error"));
1425                                               }
1426         ;
1427
1428 /*************************************************************
1429 * Grammar explanation: (cont'd)
1430 *   _bins_
1431 *************************************************************/
1432
1433
1434 assignments:    /* NOP */                     { $$ = NULL; }
1435         |       ASSIGNMENT assignments        { $$ = g_slist_prepend ($2, $1); }
1436         ;
1437
1438 binopener:      '('                           { $$ = gst_parse_strdup("bin"); }
1439         |       BINREF                        { $$ = $1; }
1440         ;
1441 bin:    binopener assignments chainlist ')'   {
1442                                                 chain_t *chain = $3;
1443                                                 GSList *walk;
1444                                                 GstBin *bin = (GstBin *) gst_element_factory_make ($1, NULL);
1445                                                 if (!chain) {
1446                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY_BIN,
1447                                                     _("specified empty bin \"%s\", not allowed"), $1);
1448                                                   chain = gst_parse_chain_new ();
1449                                                   chain->first.element = chain->last.element = NULL;
1450                                                   chain->first.name    = chain->last.name    = NULL;
1451                                                   chain->first.pads    = chain->last.pads    = NULL;
1452                                                   chain->elements = NULL;
1453                                                 }
1454                                                 if (!bin) {
1455                                                   add_missing_element(graph, $1);
1456                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1457                                                     _("no bin \"%s\", unpacking elements"), $1);
1458                                                   /* clear property-list */
1459                                                   g_slist_foreach ($2, (GFunc) gst_parse_strfree, NULL);
1460                                                   g_slist_free ($2);
1461                                                   $2 = NULL;
1462                                                 } else {
1463                                                   for (walk = chain->elements; walk; walk = walk->next )
1464                                                     gst_bin_add (bin, GST_ELEMENT (walk->data));
1465                                                   g_slist_free (chain->elements);
1466                                                   chain->elements = g_slist_prepend (NULL, bin);
1467                                                 }
1468                                                 $$ = chain;
1469                                                 /* set the properties now
1470                                                  * HINT: property-list cleared above, if bin==NULL
1471                                                  */
1472                                                 for (walk = $2; walk; walk = walk->next)
1473                                                   gst_parse_element_set ((gchar *) walk->data,
1474                                                         GST_ELEMENT (bin), graph);
1475                                                 g_slist_free ($2);
1476                                                 gst_parse_strfree ($1);
1477                                               }
1478         ;
1479
1480 /*************************************************************
1481 * Grammar explanation: (cont'd)
1482 *   _graph_
1483 *************************************************************/
1484
1485 graph:  chainlist                             { $$ = graph;
1486                                                 $$->chain = $1;
1487                                                 if(!$1) {
1488                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
1489                                                 }
1490                                               }
1491         ;
1492
1493 %%
1494
1495
1496 static int
1497 yyerror (void *scanner, graph_t *graph, const char *s)
1498 {
1499   /* FIXME: This should go into the GError somehow, but how? */
1500   GST_WARNING ("Error during parsing: %s", s);
1501   return -1;
1502 }
1503
1504
1505 GstElement *
1506 priv_gst_parse_launch (const gchar *str, GError **error, GstParseContext *ctx,
1507     GstParseFlags flags)
1508 {
1509   graph_t g;
1510   gchar *dstr;
1511   GSList *walk;
1512   GstElement *ret;
1513   yyscan_t scanner;
1514
1515   g_return_val_if_fail (str != NULL, NULL);
1516   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1517
1518   g.chain = NULL;
1519   g.links = NULL;
1520   g.error = error;
1521   g.ctx = ctx;
1522   g.flags = flags;
1523
1524 #ifdef __GST_PARSE_TRACE
1525   GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
1526   __strings = __chains = __links = __elements = 0;
1527 #endif /* __GST_PARSE_TRACE */
1528
1529   /* g_print("Now scanning: %s\n", str); */
1530
1531   dstr = g_strdup (str);
1532   priv_gst_parse_yylex_init (&scanner);
1533   priv_gst_parse_yy_scan_string (dstr, scanner);
1534
1535 #if YYDEBUG
1536   yydebug = 1;
1537 #endif
1538
1539   if (yyparse (scanner, &g) != 0) {
1540     SET_ERROR (error, GST_PARSE_ERROR_SYNTAX,
1541         "Unrecoverable syntax error while parsing pipeline %s", str);
1542
1543     priv_gst_parse_yylex_destroy (scanner);
1544     g_free (dstr);
1545
1546     goto error1;
1547   }
1548   priv_gst_parse_yylex_destroy (scanner);
1549   g_free (dstr);
1550
1551   GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links",
1552       g.chain ? g_slist_length (g.chain->elements) : 0,
1553       g_slist_length (g.links));
1554
1555   /* ensure chain is not NULL */
1556   if (!g.chain){
1557     g.chain=gst_parse_chain_new ();
1558     g.chain->elements=NULL;
1559     g.chain->first.element=NULL;
1560     g.chain->first.name=NULL;
1561     g.chain->first.pads=NULL;
1562     g.chain->last.element=NULL;
1563     g.chain->last.name=NULL;
1564     g.chain->last.pads=NULL;
1565   };
1566
1567   /* ensure elements is not empty */
1568   if(!g.chain->elements){
1569     g.chain->elements= g_slist_prepend (NULL, NULL);
1570   };
1571
1572   /* put all elements in our bin if necessary */
1573   if(g.chain->elements->next){
1574     GstBin *bin;
1575     if (flags & GST_PARSE_FLAG_PLACE_IN_BIN)
1576       bin = GST_BIN (gst_element_factory_make ("bin", NULL));
1577     else
1578       bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
1579     g_assert (bin);
1580
1581     for (walk = g.chain->elements; walk; walk = walk->next) {
1582       if (walk->data != NULL)
1583         gst_bin_add (bin, GST_ELEMENT (walk->data));
1584     }
1585     g_slist_free (g.chain->elements);
1586     g.chain->elements = g_slist_prepend (NULL, bin);
1587   }
1588
1589   ret = (GstElement *) g.chain->elements->data;
1590   g_slist_free (g.chain->elements);
1591   g.chain->elements=NULL;
1592   gst_parse_free_chain (g.chain);
1593   g.chain = NULL;
1594
1595
1596   /* resolve and perform links */
1597   for (walk = g.links; walk; walk = walk->next) {
1598     link_t *l = (link_t *) walk->data;
1599     int err;
1600     err=gst_resolve_reference( &(l->src), ret);
1601     if (err) {
1602        if(-1==err){
1603           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1604               "No src-element named \"%s\" - omitting link", l->src.name);
1605        }else{
1606           /* probably a missing element which we've handled already */
1607           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1608               "No src-element found - omitting link");
1609        }
1610        gst_parse_free_link (l);
1611        continue;
1612     }
1613
1614     err=gst_resolve_reference( &(l->sink), ret);
1615     if (err) {
1616        if(-1==err){
1617           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1618               "No sink-element named \"%s\" - omitting link", l->src.name);
1619        }else{
1620           /* probably a missing element which we've handled already */
1621           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1622               "No sink-element found - omitting link");
1623        }
1624        gst_parse_free_link (l);
1625        continue;
1626     }
1627     gst_parse_perform_link (l, &g);
1628   }
1629   g_slist_free (g.links);
1630
1631 out:
1632 #ifdef __GST_PARSE_TRACE
1633   GST_CAT_DEBUG (GST_CAT_PIPELINE,
1634       "TRACE: %u strings, %u chains, %u links and %u elements left", __strings, __chains,
1635       __links, __elements);
1636   if (__strings || __chains || __links || __elements) {
1637     g_warning ("TRACE: %u strings, %u chains, %u links and %u elements left", __strings,
1638         __chains, __links, __elements);
1639   }
1640 #endif /* __GST_PARSE_TRACE */
1641
1642   return ret;
1643
1644 error1:
1645   if (g.chain) {
1646     gst_parse_free_chain (g.chain);
1647     g.chain=NULL;
1648   }
1649
1650   g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
1651   g_slist_free (g.links);
1652
1653   if (error)
1654     g_assert (*error);
1655   ret = NULL;
1656
1657   goto out;
1658 }