2 #include <glib-object.h>
7 #include "../gstparse.h"
8 #include "../gstinfo.h"
15 #define YYERROR_VERBOSE 1
16 #define YYPARSE_PARAM graph
18 #ifdef __GST_PARSE_TRACE
19 static uint __strings;
23 __gst_parse_strdup (gchar *org)
28 /* g_print ("ALLOCATED STR (%3u): %p %s\n", __strings, ret, ret); */
32 __gst_parse_strfree (gchar *str)
35 /* g_print ("FREEING STR (%3u): %p %s\n", __strings - 1, str, str); */
37 g_return_if_fail (__strings > 0);
41 link_t *__gst_parse_link_new ()
45 ret = g_new0 (link_t, 1);
46 /* g_print ("ALLOCATED LINK (%3u): %p\n", __links, ret); */
50 __gst_parse_link_free (link_t *data)
53 /* g_print ("FREEING LINK (%3u): %p\n", __links - 1, data); */
55 g_return_if_fail (__links > 0);
60 __gst_parse_chain_new ()
64 ret = g_new0 (chain_t, 1);
65 /* g_print ("ALLOCATED CHAIN (%3u): %p\n", __chains, ret); */
69 __gst_parse_chain_free (chain_t *data)
72 /* g_print ("FREEING CHAIN (%3u): %p\n", __chains - 1, data); */
74 g_return_if_fail (__chains > 0);
79 #endif /* __GST_PARSE_TRACE */
87 /* FIXME: need to connect to "disposed" signal to clean up, but there is no such signal */
90 #ifdef G_HAVE_ISO_VARARGS
91 #define SET_ERROR(error, type, ...) G_STMT_START{ \
94 g_warning (__VA_ARGS__); \
96 g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
100 #define ERROR(type, ...) SET_ERROR (((graph_t *) graph)->error, (type), __VA_ARGS__ )
101 #ifdef GST_DEBUG_ENABLED
103 /* bison 1.35 calls this macro with side effects, we need to make sure the
104 side effects work - crappy bison
105 # define YYFPRINTF(a, ...) GST_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__)
107 # define YYFPRINTF(a, ...) G_STMT_START{ \
108 gchar *temp = g_strdup_printf (__VA_ARGS__); \
109 GST_DEBUG (GST_CAT_PIPELINE, temp); \
114 #elif defined(G_HAVE_GNUC_VARARGS)
116 #define SET_ERROR(error, type, args...) G_STMT_START{ \
119 g_warning ( ## args ); \
121 g_set_error ((error), GST_PARSE_ERROR, (type), ## args ); \
125 #define ERROR(type, args...) SET_ERROR (((graph_t *) graph)->error, (type), ## args )
126 #ifdef GST_DEBUG_ENABLED
128 /* bison 1.35 calls this macro with side effects, we need to make sure the
129 side effects work - crappy bison
130 # define YYFPRINTF(a, args...) GST_DEBUG (GST_CAT_PIPELINE, ## args )
132 # define YYFPRINTF(a, args...) G_STMT_START{ \
133 gchar *temp = g_strdup_printf ( ## args ); \
134 GST_DEBUG (GST_CAT_PIPELINE, temp); \
141 #define SET_ERROR(error, type, ...) G_STMT_START{ \
144 g_warning ("error while parsing"); \
146 g_set_error ((error), GST_PARSE_ERROR, (type), "error while parsing"); \
150 #define ERROR(type, ...) SET_ERROR (((graph_t *) graph)->error, (type), "error while parsing")
151 #ifdef GST_DEBUG_ENABLED
155 #endif /* G_HAVE_ISO_VARARGS */
157 #define GST_BIN_MAKE(res, type, chainval, assign) G_STMT_START{ \
158 chain_t *chain = chainval; \
160 GstBin *bin = (GstBin *) gst_element_factory_make (type, NULL); \
162 ERROR (GST_PARSE_ERROR_EMPTY_BIN, "Specified empty bin \"%s\", not allowed", type); \
163 g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
164 g_slist_free (assign); \
167 ERROR (GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No bin \"%s\", omitting...", type); \
168 g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
169 g_slist_free (assign); \
172 walk = chain->elements; \
174 gst_bin_add (bin, GST_ELEMENT (walk->data)); \
177 g_slist_free (chain->elements); \
178 chain->elements = g_slist_prepend (NULL, bin); \
180 /* set the properties now */ \
183 gst_parse_element_set ((gchar *) walk->data, GST_ELEMENT (bin), graph); \
184 walk = g_slist_next (walk); \
186 g_slist_free (assign); \
190 #define MAKE_LINK(link, _src, _src_name, _src_pads, _sink, _sink_name, _sink_pads) G_STMT_START{ \
191 link = gst_parse_link_new (); \
193 link->sink = _sink; \
194 link->src_name = _src_name; \
195 link->sink_name = _sink_name; \
196 link->src_pads = _src_pads; \
197 link->sink_pads = _sink_pads; \
201 #define MAKE_REF(link, _src, _pads) G_STMT_START{ \
202 gchar *padname = _src; \
203 GSList *pads = _pads; \
205 while (*padname != '.') padname++; \
208 if (*padname != '\0') \
209 pads = g_slist_prepend (pads, gst_parse_strdup (padname)); \
211 MAKE_LINK (link, NULL, _src, pads, NULL, NULL, NULL); \
214 static inline void gst_parse_unescape (gchar *str)
218 g_return_if_fail (str != NULL);
232 gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
236 /* parse the string, so the property name is null-terminated an pos points
237 to the beginning of the value */
238 while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++;
244 while (g_ascii_isspace (*pos)) pos++;
247 while (g_ascii_isspace (*pos)) pos++;
250 pos[strlen (pos) - 1] = '\0';
252 gst_parse_unescape (pos);
253 if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value))) {
256 g_value_init (&v, G_PARAM_SPEC_VALUE_TYPE(pspec));
257 switch (G_TYPE_FUNDAMENTAL (G_PARAM_SPEC_VALUE_TYPE (pspec))) {
259 g_value_set_string (&v, pos);
262 if (g_ascii_strcasecmp (pos, "true") && g_ascii_strcasecmp (pos, "yes") && g_ascii_strcasecmp (pos, "1")) {
263 g_value_set_boolean (&v, FALSE);
265 g_value_set_boolean (&v, TRUE);
270 gchar *endptr = NULL;
271 GEnumClass *klass = (GEnumClass *) g_type_class_peek (G_PARAM_SPEC_VALUE_TYPE (pspec));
272 if (klass == NULL) goto error;
273 if (!(en = g_enum_get_value_by_name (klass, pos))) {
274 if (!(en = g_enum_get_value_by_nick (klass, pos))) {
275 gint i = strtol (pos, &endptr, 0);
276 if (endptr && *endptr == '\0') {
277 en = g_enum_get_value (klass, i);
283 g_value_set_enum (&v, en->value);
289 g_value_init (&v2, G_TYPE_LONG);
290 g_value_set_long (&v2, strtol (pos, NULL, 0));
291 if (!g_value_transform (&v2, &v)) goto error;
296 g_value_init (&v2, G_TYPE_ULONG);
297 g_value_set_ulong (&v2, strtoul (pos, NULL, 0));
298 if (!g_value_transform (&v2, &v)) goto error;
302 g_value_init (&v2, G_TYPE_DOUBLE);
303 g_value_set_double (&v2, atof (pos));
304 if (!g_value_transform (&v2, &v)) goto error;
308 g_warning ("property \"%s\" in element %s cannot be set", value, GST_ELEMENT_NAME (element));
311 g_object_set_property (G_OBJECT (element), value, &v);
313 ERROR (GST_PARSE_ERROR_NO_SUCH_PROPERTY, "No property \"%s\" in element \"%s\"", value, GST_ELEMENT_NAME (element));
317 gst_parse_strfree (value);
321 ERROR (GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY, "Could not set property \"%s\" in element \"%s\" to \"%s\"", value, GST_ELEMENT_NAME (element), pos);
325 gst_parse_free_link (link_t *link)
327 gst_parse_strfree (link->src_name);
328 gst_parse_strfree (link->sink_name);
329 g_slist_foreach (link->src_pads, (GFunc) gst_parse_strfree, NULL);
330 g_slist_foreach (link->sink_pads, (GFunc) gst_parse_strfree, NULL);
331 g_slist_free (link->src_pads);
332 g_slist_free (link->sink_pads);
333 gst_caps_unref (link->caps);
334 gst_parse_link_free (link);
337 gst_parse_element_lock (GstElement *element, gboolean lock)
340 GList *walk = (GList *) gst_element_get_pad_list (element);
341 gboolean unlocked_peer = FALSE;
343 if (gst_element_is_locked_state (element) == lock)
345 /* check if we have an unlocked peer */
347 pad = (GstPad *) GST_PAD_REALIZE (walk->data);
349 if (GST_PAD_IS_SINK (pad) && GST_PAD_PEER (pad) &&
350 !gst_element_is_locked_state (GST_PAD_PARENT (GST_PAD_PEER (pad)))) {
351 unlocked_peer = TRUE;
356 if (!(lock && unlocked_peer)) {
357 gst_element_set_locked_state (element, lock);
359 gst_element_sync_state_with_parent (element);
364 /* check if there are other pads to (un)lock */
365 walk = (GList *) gst_element_get_pad_list (element);
367 pad = (GstPad *) GST_PAD_REALIZE (walk->data);
369 if (GST_PAD_IS_SRC (pad) && GST_PAD_PEER (pad)) {
370 GstElement *next = GST_ELEMENT (GST_OBJECT_PARENT (GST_PAD_PEER (pad)));
371 if (gst_element_is_locked_state (next) != lock)
372 gst_parse_element_lock (next, lock);
377 gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
379 DelayedLink *link = (DelayedLink *) data;
381 GST_INFO (GST_CAT_PIPELINE, "trying delayed linking %s:%s to %s:%s",
382 GST_ELEMENT_NAME (src), link->src_pad,
383 GST_ELEMENT_NAME (link->sink), link->sink_pad);
385 if (gst_element_link_pads_filtered (src, link->src_pad, link->sink, link->sink_pad, link->caps)) {
386 /* do this here, we don't want to get any problems later on when unlocking states */
387 GST_DEBUG (GST_CAT_PIPELINE, "delayed linking %s:%s to %s:%s worked",
388 GST_ELEMENT_NAME (src), link->src_pad,
389 GST_ELEMENT_NAME (link->sink), link->sink_pad);
390 g_signal_handler_disconnect (src, link->signal_id);
391 g_free (link->src_pad);
392 g_free (link->sink_pad);
393 gst_caps_unref (link->caps);
394 if (!gst_element_is_locked_state (src))
395 gst_parse_element_lock (link->sink, FALSE);
399 /* both padnames and the caps may be NULL */
401 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad,
402 GstElement *sink, const gchar *sink_pad, GstCaps *caps)
404 GList *templs = gst_element_get_pad_template_list (src);
407 GstPadTemplate *templ = (GstPadTemplate *) templs->data;
408 if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) && (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
410 DelayedLink *data = g_new (DelayedLink, 1);
412 /* TODO: maybe we should check if src_pad matches this template's names */
414 GST_DEBUG (GST_CAT_PIPELINE, "trying delayed link %s:%s to %s:%s",
415 GST_ELEMENT_NAME (src), src_pad, GST_ELEMENT_NAME (sink), sink_pad);
417 data->src_pad = g_strdup (src_pad);
419 data->sink_pad = g_strdup (sink_pad);
420 data->caps = gst_caps_ref (caps);
421 data->signal_id = g_signal_connect (G_OBJECT (src), "new_pad",
422 G_CALLBACK (gst_parse_found_pad), data);
425 templs = g_list_next (templs);
430 * performs a link and frees the struct. src and sink elements must be given
431 * return values: 0 - link performed
436 gst_parse_perform_link (link_t *link, graph_t *graph)
438 GstElement *src = link->src;
439 GstElement *sink = link->sink;
440 GSList *srcs = link->src_pads;
441 GSList *sinks = link->sink_pads;
442 g_assert (GST_IS_ELEMENT (src));
443 g_assert (GST_IS_ELEMENT (sink));
445 GST_INFO (GST_CAT_PIPELINE, "linking %s(%s):%u to %s(%s):%u",
446 GST_ELEMENT_NAME (src), link->src_name ? link->src_name : "---", g_slist_length (srcs),
447 GST_ELEMENT_NAME (sink), link->sink_name ? link->sink_name : "---", g_slist_length (sinks));
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,
453 gst_parse_element_lock (sink, gst_element_is_locked_state (src));
456 if (gst_parse_perform_delayed_link (src, srcs ? (const gchar *) srcs->data : NULL,
457 sink, sinks ? (const gchar *) sinks->data : NULL,
459 gst_parse_element_lock (sink, TRUE);
466 if (g_slist_length (link->src_pads) != g_slist_length (link->src_pads)) {
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));
478 if (gst_parse_perform_delayed_link (src, src_pad,
481 gst_parse_element_lock (sink, TRUE);
490 gst_parse_free_link (link);
494 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);
500 static int yylex (void *lvalp);
501 static int yyerror (const char *s);
513 %token <s> IDENTIFIER
514 %left <s> REF PADREF BINREF
515 %token <s> ASSIGNMENT
520 %type <l> linkpart link
523 %type <p> padlist pads assignments
525 %left '{' '}' '(' ')'
535 element: IDENTIFIER { $$ = gst_element_factory_make ($1, NULL);
537 ERROR (GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element \"%s\"", $1);
538 gst_parse_strfree ($1);
542 | element ASSIGNMENT { gst_parse_element_set ($2, $1, graph);
547 assignments: /* NOP */ { $$ = NULL; }
548 | assignments ASSIGNMENT { $$ = g_slist_prepend ($1, $2); }
550 bin: '{' assignments chain '}' { GST_BIN_MAKE ($$, "thread", $3, $2); }
551 | '(' assignments chain ')' { GST_BIN_MAKE ($$, "bin", $3, $2); }
552 | BINREF assignments chain ')' { GST_BIN_MAKE ($$, $1, $3, $2);
553 gst_parse_strfree ($1);
555 | '{' assignments '}' { GST_BIN_MAKE ($$, "thread", NULL, $2); }
556 | '(' assignments '}' { GST_BIN_MAKE ($$, "thread", NULL, $2); }
557 | BINREF assignments ')' { GST_BIN_MAKE ($$, $1, NULL, $2);
558 gst_parse_strfree ($1);
560 | '{' assignments error '}' { GST_BIN_MAKE ($$, "thread", NULL, $2); }
561 | '(' assignments error '}' { GST_BIN_MAKE ($$, "thread", NULL, $2); }
562 | BINREF assignments error ')' { GST_BIN_MAKE ($$, $1, NULL, $2);
563 gst_parse_strfree ($1);
567 pads: PADREF { $$ = g_slist_prepend (NULL, $1); }
568 | PADREF padlist { $$ = $2;
569 $$ = g_slist_prepend ($$, $1);
572 padlist: ',' IDENTIFIER { $$ = g_slist_prepend (NULL, $2); }
573 | ',' IDENTIFIER padlist { $$ = g_slist_prepend ($3, $2); }
576 reference: REF { MAKE_REF ($$, $1, NULL); }
577 | REF padlist { MAKE_REF ($$, $1, $2); }
580 linkpart: reference { $$ = $1; }
581 | pads { MAKE_REF ($$, NULL, $1); }
582 | /* NOP */ { MAKE_REF ($$, NULL, NULL); }
585 link: linkpart '!' linkpart { $$ = $1;
586 $$->sink_name = $3->src_name;
587 $$->sink_pads = $3->src_pads;
588 gst_parse_link_free ($3);
592 linklist: link { $$ = g_slist_prepend (NULL, $1); }
593 | link linklist { $$ = g_slist_prepend ($2, $1); }
594 | linklist error { $$ = $1; }
597 chain: element { $$ = gst_parse_chain_new ();
598 $$->first = $$->last = $1;
599 $$->front = $$->back = NULL;
600 $$->elements = g_slist_prepend (NULL, $1);
603 | chain chain { if ($1->back && $2->front) {
604 if (!$1->back->sink_name) {
605 ERROR (GST_PARSE_ERROR_LINK, "link without source element");
606 gst_parse_free_link ($1->back);
608 ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
610 if (!$2->front->src_name) {
611 ERROR (GST_PARSE_ERROR_LINK, "link without sink element");
612 gst_parse_free_link ($2->front);
614 ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
617 } else if ($1->back) {
618 if (!$1->back->sink_name) {
619 $1->back->sink = $2->first;
621 } else if ($2->front) {
622 if (!$2->front->src_name) {
623 $2->front->src = $1->last;
625 $1->back = $2->front;
629 if (!$1->back->sink || !$1->back->src) {
630 ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
633 gst_parse_perform_link ($1->back, (graph_t *) graph);
638 $1->elements = g_slist_concat ($1->elements, $2->elements);
639 gst_parse_chain_free ($2);
642 | link chain { if ($2->front) {
643 if (!$2->front->src_name) {
644 ERROR (GST_PARSE_ERROR_LINK, "link without source element");
645 gst_parse_free_link ($2->front);
647 ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
650 if (!$1->sink_name) {
651 $1->sink = $2->first;
657 | chain linklist { GSList *walk;
659 $2 = g_slist_prepend ($2, $1->back);
662 if (!((link_t *) $2->data)->src_name) {
663 ((link_t *) $2->data)->src = $1->last;
668 link_t *link = (link_t *) walk->data;
670 if (!link->sink_name && walk) {
671 ERROR (GST_PARSE_ERROR_LINK, "link without sink element");
672 gst_parse_free_link (link);
673 } else if (!link->src_name && !link->src) {
674 ERROR (GST_PARSE_ERROR_LINK, "link without source element");
675 gst_parse_free_link (link);
678 ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, link);
687 | chain error { $$ = $1; }
690 graph: /* NOP */ { ERROR (GST_PARSE_ERROR_EMPTY, "Empty pipeline not allowed");
691 $$ = (graph_t *) graph;
693 | chain { $$ = (graph_t *) graph;
695 if (!$1->front->src_name) {
696 ERROR (GST_PARSE_ERROR_LINK, "link without source element");
697 gst_parse_free_link ($1->front);
699 $$->links = g_slist_prepend ($$->links, $1->front);
704 if (!$1->back->sink_name) {
705 ERROR (GST_PARSE_ERROR_LINK, "link without sink element");
706 gst_parse_free_link ($1->back);
708 $$->links = g_slist_prepend ($$->links, $1->back);
718 extern FILE *_gst_parse_yyin;
719 int _gst_parse_yylex (YYSTYPE *lvalp);
721 static int yylex (void *lvalp) {
722 return _gst_parse_yylex ((YYSTYPE*) lvalp);
726 yyerror (const char *s)
728 /* FIXME: This should go into the GError somehow, but how? */
729 g_warning ("error: %s\n", s);
733 int _gst_parse_yy_scan_string (char*);
735 _gst_parse_launch (const gchar *str, GError **error)
743 g_return_val_if_fail (str != NULL, NULL);
749 #ifdef __GST_PARSE_TRACE
750 GST_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
751 __strings = __chains = __links = 0;
752 #endif /* __GST_PARSE_TRACE */
754 dstr = g_strdup (str);
755 _gst_parse_yy_scan_string (dstr);
757 #ifdef GST_DEBUG_ENABLED
761 if (yyparse (&g) != 0) {
762 SET_ERROR (error, GST_PARSE_ERROR_SYNTAX, "Unrecoverable syntax error while parsing pipeline");
768 GST_INFO (GST_CAT_PIPELINE, "got %u elements and %u links", g.chain ? g_slist_length (g.chain->elements) : 0, g_slist_length (g.links));
772 } else if (!(((chain_t *) g.chain)->elements->next)) {
773 /* only one toplevel element */
774 ret = (GstElement *) ((chain_t *) g.chain)->elements->data;
775 g_slist_free (((chain_t *) g.chain)->elements);
776 if (GST_IS_BIN (ret))
779 /* put all elements in our bin */
780 bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
782 walk = g.chain->elements;
784 gst_bin_add (bin, GST_ELEMENT (walk->data));
785 walk = g_slist_next (walk);
787 g_slist_free (g.chain->elements);
788 ret = GST_ELEMENT (bin);
790 gst_parse_chain_free (g.chain);
795 link_t *l = (link_t *) walk->data;
797 walk = g_slist_next (walk);
801 l->src = gst_bin_get_by_name_recurse_up (bin, l->src_name);
803 l->src = strcmp (GST_ELEMENT_NAME (ret), l->src_name) == 0 ? ret : NULL;
807 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element named \"%s\" - omitting link", l->src_name);
808 gst_parse_free_link (l);
815 l->sink = gst_bin_get_by_name_recurse_up (bin, l->sink_name);
817 l->sink = strcmp (GST_ELEMENT_NAME (ret), l->sink_name) == 0 ? ret : NULL;
821 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element named \"%s\" - omitting link", l->sink_name);
822 gst_parse_free_link (l);
827 gst_parse_perform_link (l, &g);
829 g_slist_free (g.links);
832 #ifdef __GST_PARSE_TRACE
833 GST_DEBUG (GST_CAT_PIPELINE, "TRACE: %u strings, %u chains and %u links left", __strings, __chains, __links);
834 if (__strings || __chains || __links) {
835 g_warning ("TRACE: %u strings, %u chains and %u links left", __strings, __chains, __links);
837 #endif /* __GST_PARSE_TRACE */
845 walk = g.chain->elements;
847 gst_object_unref (GST_OBJECT (walk->data));
850 g_slist_free (g.chain->elements);
852 gst_parse_chain_free (g.chain);
856 gst_parse_free_link ((link_t *) walk->data);
859 g_slist_free (g.links);