Merge CAPS branch
[platform/upstream/gstreamer.git] / gst / parse / grammar.y
1 %{
2 #include <glib-object.h>
3 #include <glib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7
8 #include "../gst_private.h"
9
10 #include "../gstconfig.h"
11 #include "../gstparse.h"
12 #include "../gstinfo.h"
13 #include "../gsturi.h"
14 #include "types.h"
15
16 #define YYERROR_VERBOSE 1
17 #define YYPARSE_PARAM graph
18
19 #ifdef __GST_PARSE_TRACE
20 static guint __strings;
21 static guint __links;
22 static guint __chains;
23 gchar *
24 __gst_parse_strdup (gchar *org)
25 {
26   gchar *ret; 
27   __strings++;
28   ret = g_strdup (org);
29   /* g_print ("ALLOCATED STR   (%3u): %p %s\n", __strings, ret, ret); */
30   return ret;
31 }
32 void
33 __gst_parse_strfree (gchar *str)
34 {
35   if (str) {
36     /* g_print ("FREEING STR     (%3u): %p %s\n", __strings - 1, str, str); */
37     g_free (str);
38     g_return_if_fail (__strings > 0);
39     __strings--;
40   }
41 }
42 link_t *__gst_parse_link_new ()
43 {
44   link_t *ret;
45   __links++;
46   ret = g_new0 (link_t, 1);
47   /* g_print ("ALLOCATED LINK  (%3u): %p\n", __links, ret); */
48   return ret;
49 }
50 void
51 __gst_parse_link_free (link_t *data)
52 {
53   if (data) {
54     /* g_print ("FREEING LINK    (%3u): %p\n", __links - 1, data); */
55     g_free (data);
56     g_return_if_fail (__links > 0);
57     __links--;
58   }
59 }
60 chain_t *
61 __gst_parse_chain_new ()
62 {
63   chain_t *ret;
64   __chains++;
65   ret = g_new0 (chain_t, 1);
66   /* g_print ("ALLOCATED CHAIN (%3u): %p\n", __chains, ret); */
67   return ret;
68 }
69 void
70 __gst_parse_chain_free (chain_t *data)
71 {
72   if (data) {
73     /* g_print ("FREEING CHAIN   (%3u): %p\n", __chains - 1, data); */
74     g_free (data);
75     g_return_if_fail (__chains > 0);
76     __chains--;
77   }
78 }
79
80 #endif /* __GST_PARSE_TRACE */
81
82 typedef struct {
83   gchar *src_pad;
84   gchar *sink_pad;
85   GstElement *sink;
86   GstCaps *caps;
87   gulong signal_id;
88   /* FIXME: need to connect to "disposed" signal to clean up, but there is no such signal */
89 } DelayedLink;
90
91 #ifdef G_HAVE_ISO_VARARGS
92 #define SET_ERROR(error, type, ...) G_STMT_START{ \
93   if (error) { \
94     if (*(error)) { \
95       g_warning (__VA_ARGS__); \
96     } else { \
97       g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
98     }\
99   } \
100 }G_STMT_END
101 #define ERROR(type, ...) SET_ERROR (((graph_t *) graph)->error, (type), __VA_ARGS__ )
102 #ifndef GST_DISABLE_GST_DEBUG
103 #  define YYDEBUG 1
104    /* bison 1.35 calls this macro with side effects, we need to make sure the
105       side effects work - crappy bison
106 #  define YYFPRINTF(a, ...) GST_CAT_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__)
107  */
108 #  define YYFPRINTF(a, ...) G_STMT_START{ \
109      gchar *temp = g_strdup_printf (__VA_ARGS__); \
110      GST_CAT_DEBUG (GST_CAT_PIPELINE, temp); \
111      g_free (temp); \
112    }G_STMT_END
113 #endif
114
115 #elif defined(G_HAVE_GNUC_VARARGS)
116
117 #define SET_ERROR(error, type, args...) G_STMT_START{ \
118   if (error) { \
119     if (*(error)) { \
120       g_warning ( ## args ); \
121     } else { \
122       g_set_error ((error), GST_PARSE_ERROR, (type), ## args ); \
123     }\
124   } \
125 }G_STMT_END
126 #define ERROR(type, args...) SET_ERROR (((graph_t *) graph)->error, (type), ## args )
127 #ifndef GST_DISABLE_GST_DEBUG
128 #  define YYDEBUG 1
129    /* bison 1.35 calls this macro with side effects, we need to make sure the
130       side effects work - crappy bison
131 #  define YYFPRINTF(a, args...) GST_CAT_DEBUG (GST_CAT_PIPELINE, ## args )
132  */
133 #  define YYFPRINTF(a, args...) G_STMT_START{ \
134      gchar *temp = g_strdup_printf ( ## args ); \
135      GST_CAT_DEBUG (GST_CAT_PIPELINE, temp); \
136      g_free (temp); \
137    }G_STMT_END
138 #endif
139
140 #else
141
142 #define SET_ERROR(error, type, ...) G_STMT_START{ \
143   if (error) { \
144     if (*(error)) { \
145       g_warning ("error while parsing"); \
146     } else { \
147       g_set_error ((error), GST_PARSE_ERROR, (type), "error while parsing"); \
148     }\
149   } \
150 }G_STMT_END
151 #define ERROR(type, ...) SET_ERROR (((graph_t *) graph)->error, (type), "error while parsing")
152 #ifndef GST_DISABLE_GST_DEBUG
153 #  define YYDEBUG 1
154 #endif
155
156 #endif /* G_HAVE_ISO_VARARGS */
157
158 #define GST_BIN_MAKE(res, type, chainval, assign) G_STMT_START{ \
159   chain_t *chain = chainval; \
160   GSList *walk; \
161   GstBin *bin = (GstBin *) gst_element_factory_make (type, NULL); \
162   if (!chain) { \
163     ERROR (GST_PARSE_ERROR_EMPTY_BIN, "Specified empty bin \"%s\", not allowed", type); \
164     g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
165     g_slist_free (assign); \
166     YYERROR; \
167   } else if (!bin) { \
168     ERROR (GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No bin \"%s\", omitting...", type); \
169     g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
170     g_slist_free (assign); \
171     res = chain; \
172   } else { \
173     walk = chain->elements; \
174     while (walk) { \
175       gst_bin_add (bin, GST_ELEMENT (walk->data)); \
176       walk = walk->next; \
177     } \
178     g_slist_free (chain->elements); \
179     chain->elements = g_slist_prepend (NULL, bin); \
180     res = chain; \
181     /* set the properties now */ \
182     walk = assign; \
183     while (walk) { \
184       gst_parse_element_set ((gchar *) walk->data, GST_ELEMENT (bin), graph); \
185       walk = g_slist_next (walk); \
186     } \
187     g_slist_free (assign); \
188   } \
189 }G_STMT_END
190
191 #define MAKE_LINK(link, _src, _src_name, _src_pads, _sink, _sink_name, _sink_pads) G_STMT_START{ \
192   link = gst_parse_link_new (); \
193   link->src = _src; \
194   link->sink = _sink; \
195   link->src_name = _src_name; \
196   link->sink_name = _sink_name; \
197   link->src_pads = _src_pads; \
198   link->sink_pads = _sink_pads; \
199   link->caps = NULL; \
200 }G_STMT_END
201
202 #define MAKE_REF(link, _src, _pads) G_STMT_START{ \
203   gchar *padname = _src; \
204   GSList *pads = _pads; \
205   if (padname) { \
206     while (*padname != '.') padname++; \
207     *padname = '\0'; \
208     padname++; \
209     if (*padname != '\0') \
210       pads = g_slist_prepend (pads, gst_parse_strdup (padname)); \
211   } \
212   MAKE_LINK (link, NULL, _src, pads, NULL, NULL, NULL); \
213 }G_STMT_END
214
215 static void
216 gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
217 {
218   GParamSpec *pspec;
219   gchar *pos = value;
220   /* parse the string, so the property name is null-terminated an pos points
221      to the beginning of the value */
222   while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++; 
223   if (*pos == '=') { 
224     *pos = '\0'; 
225   } else { 
226     *pos = '\0'; 
227     pos++;
228     while (g_ascii_isspace (*pos)) pos++; 
229   } 
230   pos++; 
231   while (g_ascii_isspace (*pos)) pos++; 
232   if (*pos == '"') {
233     pos++;
234     pos[strlen (pos) - 1] = '\0';
235   }
236   gst_parse_unescape (pos); 
237   if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value))) { 
238     GValue v = { 0, }; 
239     GValue v2 = { 0, };
240     g_value_init (&v, G_PARAM_SPEC_VALUE_TYPE(pspec)); 
241     switch (G_TYPE_FUNDAMENTAL (G_PARAM_SPEC_VALUE_TYPE (pspec))) {
242     case G_TYPE_STRING:
243       g_value_set_string (&v, pos);
244       break;      
245     case G_TYPE_BOOLEAN:
246       if (g_ascii_strcasecmp (pos, "true") && g_ascii_strcasecmp (pos, "yes") && g_ascii_strcasecmp (pos, "1")) {
247         g_value_set_boolean (&v, FALSE);
248       } else {
249         g_value_set_boolean (&v, TRUE);
250       }
251       break;
252     case G_TYPE_ENUM: {
253       GEnumValue *en;
254       gchar *endptr = NULL;
255       GEnumClass *klass = (GEnumClass *) g_type_class_peek (G_PARAM_SPEC_VALUE_TYPE (pspec));
256       if (klass == NULL) goto error;
257       if (!(en = g_enum_get_value_by_name (klass, pos))) {
258         if (!(en = g_enum_get_value_by_nick (klass, pos))) {
259           gint i = strtol (pos, &endptr, 0);
260           if (endptr && *endptr == '\0') {
261             en = g_enum_get_value (klass, i);
262           }
263         }
264       }
265       if (!en)
266         goto error;
267       g_value_set_enum (&v, en->value);
268       break;
269     }
270     case G_TYPE_INT:
271     case G_TYPE_LONG:
272     case G_TYPE_INT64: {
273       gchar *endptr;
274       glong l;
275       g_value_init (&v2, G_TYPE_LONG); 
276       l = strtol (pos, &endptr, 0);
277       if (*endptr != '\0') goto error_conversion;
278       g_value_set_long (&v2, l);
279       if (!g_value_transform (&v2, &v)) goto error_conversion;
280       break;      
281     }
282     case G_TYPE_UINT:
283     case G_TYPE_ULONG:
284     case G_TYPE_UINT64: {
285       gchar *endptr;
286       gulong ul;
287       g_value_init (&v2, G_TYPE_ULONG); 
288       ul = strtoul (pos, &endptr, 0);
289       if (*endptr != '\0') goto error_conversion;
290       g_value_set_ulong (&v2, ul);
291       if (!g_value_transform (&v2, &v)) goto error_conversion;
292       break;      
293     }
294     case G_TYPE_FLOAT:
295     case G_TYPE_DOUBLE: {
296       gchar *endptr;
297       gdouble d;
298       g_value_init (&v2, G_TYPE_DOUBLE); 
299       d = g_ascii_strtod (pos, &endptr);
300       if (*endptr != '\0') goto error_conversion;
301       g_value_set_double (&v2, d);
302       if (!g_value_transform (&v2, &v)) goto error_conversion;
303       break;      
304     }
305     default:
306       /* add more */
307       g_warning ("property \"%s\" in element %s cannot be set", value, GST_ELEMENT_NAME (element)); 
308       goto error;
309     }
310     g_object_set_property (G_OBJECT (element), value, &v); 
311   } else { 
312     ERROR (GST_PARSE_ERROR_NO_SUCH_PROPERTY, "No property \"%s\" in element \"%s\"", value, GST_ELEMENT_NAME (element)); 
313   }
314
315 out:
316   gst_parse_strfree (value);
317   return;
318   
319 error:
320   ERROR (GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
321          "Could not set property \"%s\" in element \"%s\" to \"%s\"", 
322          value, GST_ELEMENT_NAME (element), pos); 
323   goto out;
324 error_conversion:
325   ERROR (GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
326          "Could not convert \"%s\" so that it fits property \"%s\" in element \"%s\"",
327          pos, value, GST_ELEMENT_NAME (element)); 
328   goto out;
329 }
330 static inline void
331 gst_parse_free_link (link_t *link)
332 {
333   gst_parse_strfree (link->src_name);
334   gst_parse_strfree (link->sink_name);
335   g_slist_foreach (link->src_pads, (GFunc) gst_parse_strfree, NULL);
336   g_slist_foreach (link->sink_pads, (GFunc) gst_parse_strfree, NULL);
337   g_slist_free (link->src_pads);
338   g_slist_free (link->sink_pads);
339   if (link->caps) gst_caps_free (link->caps);
340   gst_parse_link_free (link);  
341 }
342 static void
343 gst_parse_element_lock (GstElement *element, gboolean lock)
344 {
345   GstPad *pad;
346   GList *walk = (GList *) gst_element_get_pad_list (element);
347   gboolean unlocked_peer = FALSE;
348   
349   if (gst_element_is_locked_state (element) == lock)
350     return;
351   /* check if we have an unlocked peer */
352   while (walk) {
353     pad = (GstPad *) GST_PAD_REALIZE (walk->data);
354     walk = walk->next;
355     if (GST_PAD_IS_SINK (pad) && GST_PAD_PEER (pad) &&
356         !gst_element_is_locked_state (GST_PAD_PARENT (GST_PAD_PEER (pad)))) {
357       unlocked_peer = TRUE;
358       break;
359     }
360   }  
361   
362   if (!(lock && unlocked_peer)) {
363     gst_element_set_locked_state (element, lock);
364     if (!lock)
365       gst_element_sync_state_with_parent (element);
366   } else {
367     return;
368   }
369   
370   /* check if there are other pads to (un)lock */
371   walk = (GList *) gst_element_get_pad_list (element);
372   while (walk) {
373     pad = (GstPad *) GST_PAD_REALIZE (walk->data);
374     walk = walk->next;
375     if (GST_PAD_IS_SRC (pad) && GST_PAD_PEER (pad)) {
376       GstElement *next = GST_ELEMENT (GST_OBJECT_PARENT (GST_PAD_PEER (pad)));
377       if (gst_element_is_locked_state (next) != lock)
378         gst_parse_element_lock (next, lock);
379     }
380   }
381 }
382 static void
383 gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
384 {
385   DelayedLink *link = (DelayedLink *) data;
386   
387   GST_CAT_INFO (GST_CAT_PIPELINE, "trying delayed linking %s:%s to %s:%s", 
388                 GST_ELEMENT_NAME (src), link->src_pad,
389                 GST_ELEMENT_NAME (link->sink), link->sink_pad);
390
391   if (gst_element_link_pads_filtered (src, link->src_pad, link->sink, link->sink_pad, link->caps)) {
392     /* do this here, we don't want to get any problems later on when unlocking states */
393     GST_CAT_DEBUG (GST_CAT_PIPELINE, "delayed linking %s:%s to %s:%s worked", 
394                    GST_ELEMENT_NAME (src), link->src_pad,
395                    GST_ELEMENT_NAME (link->sink), link->sink_pad);
396     g_signal_handler_disconnect (src, link->signal_id);
397     g_free (link->src_pad);
398     g_free (link->sink_pad);
399     if (link->caps) gst_caps_free (link->caps);
400     if (!gst_element_is_locked_state (src))
401       gst_parse_element_lock (link->sink, FALSE);
402     g_free (link);
403   }
404 }
405 /* both padnames and the caps may be NULL */
406 static gboolean
407 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad, 
408                                 GstElement *sink, const gchar *sink_pad, GstCaps *caps)
409 {
410   GList *templs = gst_element_get_pad_template_list (src);
411          
412   while (templs) {
413     GstPadTemplate *templ = (GstPadTemplate *) templs->data;
414     if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) && (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
415     {
416       DelayedLink *data = g_new (DelayedLink, 1); 
417       
418       /* TODO: maybe we should check if src_pad matches this template's names */
419
420       GST_CAT_DEBUG (GST_CAT_PIPELINE, "trying delayed link %s:%s to %s:%s", 
421                      GST_ELEMENT_NAME (src), src_pad, GST_ELEMENT_NAME (sink), sink_pad);
422
423       data->src_pad = g_strdup (src_pad);
424       data->sink = sink;
425       data->sink_pad = g_strdup (sink_pad);
426       data->caps = gst_caps_copy (caps);
427       data->signal_id = g_signal_connect (G_OBJECT (src), "new_pad", 
428                                           G_CALLBACK (gst_parse_found_pad), data);
429       return TRUE;
430     }
431     templs = g_list_next (templs);
432   }
433   return FALSE;
434 }
435 /**
436  * performs a link and frees the struct. src and sink elements must be given
437  * return values   0 - link performed
438  *                 1 - link delayed
439  *                <0 - error
440  */
441 static gint
442 gst_parse_perform_link (link_t *link, graph_t *graph)
443 {
444   GstElement *src = link->src;
445   GstElement *sink = link->sink;
446   GSList *srcs = link->src_pads;
447   GSList *sinks = link->sink_pads;
448   g_assert (GST_IS_ELEMENT (src));
449   g_assert (GST_IS_ELEMENT (sink));
450   
451   GST_CAT_INFO (GST_CAT_PIPELINE, "linking %s(%s):%u to %s(%s):%u with caps \"%s\"", 
452                 GST_ELEMENT_NAME (src), link->src_name ? link->src_name : "---", g_slist_length (srcs),
453                 GST_ELEMENT_NAME (sink), link->sink_name ? link->sink_name : "---", g_slist_length (sinks),
454                 link->caps ? gst_caps_to_string (link->caps) : "-");
455
456   if (!srcs || !sinks) {
457     if (gst_element_link_pads_filtered (src, srcs ? (const gchar *) srcs->data : NULL,
458                                         sink, sinks ? (const gchar *) sinks->data : NULL,
459                                         link->caps)) {
460       gst_parse_element_lock (sink, gst_element_is_locked_state (src));
461       goto success;
462     } else {
463       if (gst_parse_perform_delayed_link (src, srcs ? (const gchar *) srcs->data : NULL,
464                                           sink, sinks ? (const gchar *) sinks->data : NULL,
465                                           link->caps)) {
466         gst_parse_element_lock (sink, TRUE);
467         goto success;
468       } else {
469         goto error;
470       }
471     }
472   }
473   if (g_slist_length (link->src_pads) != g_slist_length (link->src_pads)) {
474     goto error;
475   }
476   while (srcs && sinks) {
477     const gchar *src_pad = (const gchar *) srcs->data;
478     const gchar *sink_pad = (const gchar *) sinks->data;
479     srcs = g_slist_next (srcs);
480     sinks = g_slist_next (sinks);
481     if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad, link->caps)) {
482       gst_parse_element_lock (sink, gst_element_is_locked_state (src));
483       continue;
484     } else {
485       if (gst_parse_perform_delayed_link (src, src_pad,
486                                           sink, sink_pad,
487                                           link->caps)) {
488         gst_parse_element_lock (sink, TRUE);
489         continue;
490       } else {
491         goto error;
492       }
493     }
494   }
495   
496 success:
497   gst_parse_free_link (link);
498   return 0;
499   
500 error:
501   ERROR (GST_PARSE_ERROR_LINK, "could not link %s to %s", GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink));
502   gst_parse_free_link (link);
503   return -1;
504 }
505
506
507 static int yylex (void *lvalp);
508 static int yyerror (const char *s);
509 %}
510
511 %union {
512     gchar *s;
513     chain_t *c;
514     link_t *l;
515     GstElement *e;
516     GSList *p;
517     graph_t *g;
518 }
519
520 %token <s> PARSE_URL
521 %token <s> IDENTIFIER
522 %left <s> REF PADREF BINREF
523 %token <s> ASSIGNMENT
524 %token <s> LINK
525
526 %type <g> graph
527 %type <c> chain bin
528 %type <l> reference
529 %type <l> linkpart link
530 %type <p> linklist
531 %type <e> element 
532 %type <p> padlist pads assignments
533
534 %left '{' '}' '(' ')'
535 %left ','
536 %right '.'
537 %left '!' '='
538
539 %pure_parser
540
541 %start graph
542 %%
543
544 element:        IDENTIFIER                    { $$ = gst_element_factory_make ($1, NULL); 
545                                                 if (!$$)
546                                                   ERROR (GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element \"%s\"", $1);
547                                                 gst_parse_strfree ($1);
548                                                 if (!$$)
549                                                   YYERROR;
550                                               }
551         |       element ASSIGNMENT            { gst_parse_element_set ($2, $1, graph);
552                                                 $$ = $1;
553                                               }
554         ;
555 assignments:    /* NOP */                     { $$ = NULL; }
556         |       assignments ASSIGNMENT        { $$ = g_slist_prepend ($1, $2); }
557         ;               
558 bin:            '{' assignments chain '}' { GST_BIN_MAKE ($$, "thread", $3, $2); }
559         |       '(' assignments chain ')' { GST_BIN_MAKE ($$, "bin", $3, $2); }
560         |       BINREF assignments chain ')'  { GST_BIN_MAKE ($$, $1, $3, $2); 
561                                                 gst_parse_strfree ($1);
562                                               }
563         |       '{' assignments '}'           { GST_BIN_MAKE ($$, "thread", NULL, $2); }
564         |       '(' assignments '}'           { GST_BIN_MAKE ($$, "thread", NULL, $2); }
565         |       BINREF assignments ')'        { GST_BIN_MAKE ($$, $1, NULL, $2); 
566                                                 gst_parse_strfree ($1);
567                                               }
568         |       '{' assignments error '}'     { GST_BIN_MAKE ($$, "thread", NULL, $2); }
569         |       '(' assignments error '}'     { GST_BIN_MAKE ($$, "thread", NULL, $2); }
570         |       BINREF assignments error ')'  { GST_BIN_MAKE ($$, $1, NULL, $2); 
571                                                 gst_parse_strfree ($1);
572                                               }
573         ;
574         
575 pads:           PADREF                        { $$ = g_slist_prepend (NULL, $1); }
576         |       PADREF padlist                { $$ = $2;
577                                                 $$ = g_slist_prepend ($$, $1);
578                                               }                              
579         ;
580 padlist:        ',' IDENTIFIER                { $$ = g_slist_prepend (NULL, $2); }
581         |       ',' IDENTIFIER padlist        { $$ = g_slist_prepend ($3, $2); }
582         ;
583         
584 reference:      REF                           { MAKE_REF ($$, $1, NULL); }
585         |       REF padlist                   { MAKE_REF ($$, $1, $2); }
586         ;
587
588 linkpart:       reference                     { $$ = $1; }
589         |       pads                          { MAKE_REF ($$, NULL, $1); }
590         |       /* NOP */                     { MAKE_REF ($$, NULL, NULL); }
591         ;
592         
593 link:           linkpart LINK linkpart        { $$ = $1;
594                                                 if ($2) {
595                                                   $$->caps = gst_caps_from_string ($2);
596                                                   if (!$$->caps)
597                                                     ERROR (GST_PARSE_ERROR_LINK, "could not parse caps \"%s\"", $2);
598                                                   gst_parse_strfree ($2);
599                                                 }
600                                                 $$->sink_name = $3->src_name;
601                                                 $$->sink_pads = $3->src_pads;
602                                                 gst_parse_link_free ($3);
603                                               }
604         ;
605         
606 linklist:       link                          { $$ = g_slist_prepend (NULL, $1); }
607         |       link linklist                 { $$ = g_slist_prepend ($2, $1); }
608         |       linklist error                { $$ = $1; }
609         ;       
610
611 chain:          element                       { $$ = gst_parse_chain_new ();
612                                                 $$->first = $$->last = $1;
613                                                 $$->front = $$->back = NULL;
614                                                 $$->elements = g_slist_prepend (NULL, $1);
615                                               }
616         |       bin                           { $$ = $1; }
617         |       chain chain                   { if ($1->back && $2->front) {
618                                                   if (!$1->back->sink_name) {
619                                                     ERROR (GST_PARSE_ERROR_LINK, "link without source element");
620                                                     gst_parse_free_link ($1->back);
621                                                   } else {
622                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
623                                                   }
624                                                   if (!$2->front->src_name) {
625                                                     ERROR (GST_PARSE_ERROR_LINK, "link without sink element");
626                                                     gst_parse_free_link ($2->front);
627                                                   } else {
628                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
629                                                   }
630                                                   $1->back = NULL;
631                                                 } else if ($1->back) {
632                                                   if (!$1->back->sink_name) {
633                                                     $1->back->sink = $2->first;
634                                                   }
635                                                 } else if ($2->front) {
636                                                   if (!$2->front->src_name) {
637                                                     $2->front->src = $1->last;
638                                                   }
639                                                   $1->back = $2->front;
640                                                 }
641                                                 
642                                                 if ($1->back) {
643                                                   if (!$1->back->sink || !$1->back->src) {
644                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
645                                                     $1->back = NULL;
646                                                   } else {
647                                                     gst_parse_perform_link ($1->back, (graph_t *) graph);
648                                                   }
649                                                 }
650                                                 $1->last = $2->last;
651                                                 $1->back = $2->back;
652                                                 $1->elements = g_slist_concat ($1->elements, $2->elements);
653                                                 gst_parse_chain_free ($2);
654                                                 $$ = $1;
655                                               }
656         |       chain linklist                { GSList *walk;
657                                                 if ($1->back) {
658                                                   $2 = g_slist_prepend ($2, $1->back);
659                                                   $1->back = NULL;
660                                                 } else {
661                                                   if (!((link_t *) $2->data)->src_name) {
662                                                     ((link_t *) $2->data)->src = $1->last;
663                                                   }                                               
664                                                 }
665                                                 walk = $2;
666                                                 while (walk) {
667                                                   link_t *link = (link_t *) walk->data;
668                                                   walk = walk->next;
669                                                   if (!link->sink_name && walk) {
670                                                     ERROR (GST_PARSE_ERROR_LINK, "link without sink element");
671                                                     gst_parse_free_link (link);
672                                                   } else if (!link->src_name && !link->src) {
673                                                     ERROR (GST_PARSE_ERROR_LINK, "link without source element");
674                                                     gst_parse_free_link (link);
675                                                   } else {
676                                                     if (walk) {
677                                                       ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, link);
678                                                     } else {
679                                                       $1->back = link;
680                                                     }
681                                                   }
682                                                 }
683                                                 g_slist_free ($2);
684                                                 $$ = $1;
685                                               }
686         |       chain error                   { $$ = $1; }
687         |       link chain                    { if ($2->front) {
688                                                   if (!$2->front->src_name) {
689                                                     ERROR (GST_PARSE_ERROR_LINK, "link without source element");
690                                                     gst_parse_free_link ($2->front);
691                                                   } else {
692                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
693                                                   }
694                                                 }
695                                                 if (!$1->sink_name) {
696                                                   $1->sink = $2->first;
697                                                 }
698                                                 $2->front = $1;
699                                                 $$ = $2;
700                                               }
701         |       PARSE_URL chain               { $$ = $2;
702                                                 if ($$->front) {
703                                                   GstElement *element = 
704                                                           gst_element_make_from_uri (GST_URI_SRC, $1, NULL);
705                                                   if (!element) {
706                                                     ERROR (GST_PARSE_ERROR_NO_SUCH_ELEMENT, 
707                                                             "No source element for URI \"%s\"", $1);
708                                                   } else {
709                                                     $$->front->src = element;
710                                                     ((graph_t *) graph)->links = g_slist_prepend (
711                                                             ((graph_t *) graph)->links, $$->front);
712                                                     $$->front = NULL;
713                                                     $$->elements = g_slist_prepend ($$->elements, element);
714                                                   }
715                                                 } else {
716                                                   ERROR (GST_PARSE_ERROR_LINK, 
717                                                           "No element to link URI \"%s\" to", $1);
718                                                 }
719                                                 g_free ($1);
720                                               }
721         |       link PARSE_URL                { GstElement *element =
722                                                           gst_element_make_from_uri (GST_URI_SINK, $2, NULL);
723                                                 if (!element) {
724                                                   ERROR (GST_PARSE_ERROR_NO_SUCH_ELEMENT, 
725                                                           "No sink element for URI \"%s\"", $2);
726                                                   YYERROR;
727                                                 } else if ($1->sink_name || $1->sink_pads) {
728                                                   ERROR (GST_PARSE_ERROR_LINK, 
729                                                           "could not link sink element for URI \"%s\"", $2);
730                                                   YYERROR;
731                                                 } else {
732                                                   $$ = gst_parse_chain_new ();
733                                                   $$->first = $$->last = element;
734                                                   $$->front = $1;
735                                                   $$->front->sink = element;
736                                                   $$->elements = g_slist_prepend (NULL, element);
737                                                 }
738                                                 g_free ($2);
739                                               }
740         ;
741 graph:          /* NOP */                     { ERROR (GST_PARSE_ERROR_EMPTY, "Empty pipeline not allowed");
742                                                 $$ = (graph_t *) graph;
743                                               }
744         |       chain                         { $$ = (graph_t *) graph;
745                                                 if ($1->front) {
746                                                   if (!$1->front->src_name) {
747                                                     ERROR (GST_PARSE_ERROR_LINK, "link without source element");
748                                                     gst_parse_free_link ($1->front);
749                                                   } else {
750                                                     $$->links = g_slist_prepend ($$->links, $1->front);
751                                                   }
752                                                   $1->front = NULL;
753                                                 }
754                                                 if ($1->back) {
755                                                   if (!$1->back->sink_name) {
756                                                     ERROR (GST_PARSE_ERROR_LINK, "link without sink element");
757                                                     gst_parse_free_link ($1->back);
758                                                   } else {
759                                                     $$->links = g_slist_prepend ($$->links, $1->back);
760                                                   }
761                                                   $1->back = NULL;
762                                                 }
763                                                 $$->chain = $1;
764                                               }
765         ;
766
767 %%
768
769 extern FILE *_gst_parse_yyin;
770 int _gst_parse_yylex (YYSTYPE *lvalp);
771
772 static int yylex (void *lvalp) {
773     return _gst_parse_yylex ((YYSTYPE*) lvalp);
774 }
775
776 static int
777 yyerror (const char *s)
778 {
779   /* FIXME: This should go into the GError somehow, but how? */
780   g_warning ("error: %s\n", s);
781   return -1;
782 }
783
784 int _gst_parse_yy_scan_string (char*);
785 GstElement *
786 _gst_parse_launch (const gchar *str, GError **error)
787 {
788   graph_t g;
789   gchar *dstr;
790   GSList *walk;
791   GstBin *bin = NULL;
792   GstElement *ret;
793   
794   g_return_val_if_fail (str != NULL, NULL);
795
796   g.chain = NULL;
797   g.links = NULL;
798   g.error = error;
799   
800 #ifdef __GST_PARSE_TRACE
801   GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
802   __strings = __chains = __links = 0;
803 #endif /* __GST_PARSE_TRACE */
804
805   dstr = g_strdup (str);
806   _gst_parse_yy_scan_string (dstr);
807
808 #ifndef GST_DISABLE_GST_DEBUG
809   yydebug = 1;
810 #endif
811
812   if (yyparse (&g) != 0) {
813     SET_ERROR (error, GST_PARSE_ERROR_SYNTAX, "Unrecoverable syntax error while parsing pipeline");
814     
815     goto error1;
816   }
817   g_free (dstr);
818   
819   GST_CAT_INFO (GST_CAT_PIPELINE, "got %u elements and %u links", g.chain ? g_slist_length (g.chain->elements) : 0, g_slist_length (g.links));
820   
821   if (!g.chain) {
822     ret = NULL;
823   } else if (!(((chain_t *) g.chain)->elements->next)) {
824     /* only one toplevel element */  
825     ret = (GstElement *) ((chain_t *) g.chain)->elements->data;
826     g_slist_free (((chain_t *) g.chain)->elements);
827     if (GST_IS_BIN (ret))
828       bin = GST_BIN (ret);
829   } else {  
830     /* put all elements in our bin */
831     bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
832     g_assert (bin);
833     walk = g.chain->elements;
834     while (walk) {
835       gst_bin_add (bin, GST_ELEMENT (walk->data));
836       walk = g_slist_next (walk);  
837     }
838     g_slist_free (g.chain->elements);
839     ret = GST_ELEMENT (bin);
840   }
841   gst_parse_chain_free (g.chain);
842   
843   /* remove links */
844   walk = g.links;
845   while (walk) {
846     link_t *l = (link_t *) walk->data;
847     GstElement *sink;
848     walk = g_slist_next (walk);
849     if (!l->src) {
850       if (l->src_name) {
851         if (bin) {
852           l->src = gst_bin_get_by_name_recurse_up (bin, l->src_name);
853         } else {
854           l->src = strcmp (GST_ELEMENT_NAME (ret), l->src_name) == 0 ? ret : NULL;
855         }
856       }
857       if (!l->src) {
858         SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element named \"%s\" - omitting link", l->src_name);
859         gst_parse_free_link (l);
860         continue;
861       }
862     }
863     if (!l->sink) {
864       if (l->sink_name) {
865         if (bin) {
866           l->sink = gst_bin_get_by_name_recurse_up (bin, l->sink_name);
867         } else {
868           l->sink = strcmp (GST_ELEMENT_NAME (ret), l->sink_name) == 0 ? ret : NULL;
869         }
870       }
871       if (!l->sink) {
872         SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element named \"%s\" - omitting link", l->sink_name);
873         gst_parse_free_link (l);
874         continue;
875       }
876     }
877     sink = l->sink;
878     gst_parse_perform_link (l, &g);
879   }
880   g_slist_free (g.links);
881
882 out:
883 #ifdef __GST_PARSE_TRACE
884   GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: %u strings, %u chains and %u links left", __strings, __chains, __links);
885   if (__strings || __chains || __links) {
886     g_warning ("TRACE: %u strings, %u chains and %u links left", __strings, __chains, __links);
887   }
888 #endif /* __GST_PARSE_TRACE */
889
890   return ret;
891   
892 error1:
893   g_free (dstr);
894   
895   if (g.chain) {
896     walk = g.chain->elements;
897     while (walk) {
898       gst_object_unref (GST_OBJECT (walk->data));
899       walk = walk->next;
900     }
901     g_slist_free (g.chain->elements);
902   }
903   gst_parse_chain_free (g.chain);
904   
905   walk = g.links;
906   while (walk) {
907     gst_parse_free_link ((link_t *) walk->data);
908     walk = walk->next;
909   }
910   g_slist_free (g.links);
911   
912   g_assert (*error);
913   ret = NULL;
914   
915   goto out;
916 }