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