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