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)
26 /* g_print ("ALLOCATED: %p %s\n", org, org); */
27 return g_strdup (org);
30 __gst_parse_strfree (gchar *str)
33 /* g_print ("FREEING : %p %s\n", str, str); */
35 g_return_if_fail (__strings > 0);
39 link_t *__gst_parse_link_new ()
42 return g_new0 (link_t, 1);
45 __gst_parse_link_free (link_t *data)
49 g_return_if_fail (__links > 0);
54 __gst_parse_chain_new ()
57 return g_new0 (chain_t, 1);
60 __gst_parse_chain_free (chain_t *data)
64 g_return_if_fail (__chains > 0);
69 #endif /* __GST_PARSE_TRACE */
77 /* FIXME: need to connect to "disposed" signal to clean up, but there is no such signal */
80 #ifdef G_HAVE_ISO_VARARGS
81 #define SET_ERROR(error, type, ...) G_STMT_START{ \
84 g_warning (__VA_ARGS__); \
86 g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
90 #define ERROR(type, ...) SET_ERROR (((graph_t *) graph)->error, (type), __VA_ARGS__ )
91 #ifdef GST_DEBUG_ENABLED
93 # define YYFPRINTF(a, ...) GST_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__)
96 #elif defined(G_HAVE_GNUC_VARARGS)
98 #define SET_ERROR(error, type, args...) G_STMT_START{ \
101 g_warning ( ## args ); \
103 g_set_error ((error), GST_PARSE_ERROR, (type), ## args ); \
107 #define ERROR(type, args...) SET_ERROR (((graph_t *) graph)->error, (type), ## args )
108 #ifdef GST_DEBUG_ENABLED
110 # define YYFPRINTF(a, args...) GST_DEBUG (GST_CAT_PIPELINE, ## args )
115 #define SET_ERROR(error, type, ...) G_STMT_START{ \
118 g_warning ("error while parsing"); \
120 g_set_error ((error), GST_PARSE_ERROR, (type), "error while parsing"); \
124 #define ERROR(type, ...) SET_ERROR (((graph_t *) graph)->error, (type), "error while parsing")
125 #ifdef GST_DEBUG_ENABLED
129 #endif /* G_HAVE_ISO_VARARGS */
131 #define GST_BIN_MAKE(res, type, chainval, assign) G_STMT_START{ \
132 chain_t *chain = chainval; \
134 GstBin *bin = (GstBin *) gst_element_factory_make (type, NULL); \
136 ERROR (GST_PARSE_ERROR_EMPTY_BIN, "Specified empty bin \"%s\", not allowed", type); \
137 g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
138 g_slist_free (assign); \
141 ERROR (GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No bin \"%s\", omitting...", type); \
142 g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
143 g_slist_free (assign); \
146 walk = chain->elements; \
148 gst_bin_add (bin, GST_ELEMENT (walk->data)); \
151 g_slist_free (chain->elements); \
152 chain->elements = g_slist_prepend (NULL, bin); \
154 /* set the properties now */ \
157 gst_parse_element_set ((gchar *) walk->data, GST_ELEMENT (bin), graph); \
158 walk = g_slist_next (walk); \
160 g_slist_free (assign); \
164 #define MAKE_LINK(link, _src, _src_name, _src_pads, _sink, _sink_name, _sink_pads) G_STMT_START{ \
165 link = gst_parse_link_new (); \
167 link->sink = _sink; \
168 link->src_name = _src_name; \
169 link->sink_name = _sink_name; \
170 link->src_pads = _src_pads; \
171 link->sink_pads = _sink_pads; \
175 #define MAKE_REF(link, _src, _pads) G_STMT_START{ \
176 gchar *padname = _src; \
177 GSList *pads = _pads; \
179 while (*padname != '.') padname++; \
182 if (*padname != '\0') \
183 pads = g_slist_prepend (pads, gst_parse_strdup (padname)); \
185 MAKE_LINK (link, NULL, _src, pads, NULL, NULL, NULL); \
188 static inline void gst_parse_unescape (gchar *str)
192 g_return_if_fail (str != NULL);
206 gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
210 /* parse the string, so the property name is null-terminated an pos points
211 to the beginning of the value */
212 while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++;
218 while (g_ascii_isspace (*pos)) pos++;
221 while (g_ascii_isspace (*pos)) pos++;
224 pos[strlen (pos) - 1] = '\0';
226 gst_parse_unescape (pos);
227 if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value))) {
230 g_value_init (&v, G_PARAM_SPEC_VALUE_TYPE(pspec));
231 switch (G_TYPE_FUNDAMENTAL (G_PARAM_SPEC_VALUE_TYPE (pspec))) {
233 g_value_set_string (&v, pos);
236 if (g_ascii_strcasecmp (pos, "true") && g_ascii_strcasecmp (pos, "yes") && g_ascii_strcasecmp (pos, "1")) {
237 g_value_set_boolean (&v, FALSE);
239 g_value_set_boolean (&v, TRUE);
244 gchar **endptr = NULL;
245 GEnumClass *klass = (GEnumClass *) g_type_class_peek (G_PARAM_SPEC_VALUE_TYPE (pspec));
246 if (klass == NULL) goto error;
247 if (!(en = g_enum_get_value_by_name (klass, pos)))
248 en = g_enum_get_value_by_nick (klass, pos);
250 g_value_set_enum (&v, en->value);
252 gint i = strtol (value, endptr, 0);
253 if (**endptr == '\0') {
254 g_value_set_enum (&v, i);
264 g_value_init (&v2, G_TYPE_LONG);
265 g_value_set_long (&v2, strtol (pos, NULL, 0));
266 if (!g_value_transform (&v2, &v)) goto error;
271 g_value_init (&v2, G_TYPE_ULONG);
272 g_value_set_ulong (&v2, strtoul (pos, NULL, 0));
273 if (!g_value_transform (&v2, &v)) goto error;
277 g_value_init (&v2, G_TYPE_DOUBLE);
278 g_value_set_double (&v2, atof (pos));
279 if (!g_value_transform (&v2, &v)) goto error;
283 g_warning ("property \"%s\" in element %s cannot be set", value, GST_ELEMENT_NAME (element));
286 g_object_set_property (G_OBJECT (element), value, &v);
288 ERROR (GST_PARSE_ERROR_NO_SUCH_PROPERTY, "No property \"%s\" in element \"%s\"", value, GST_ELEMENT_NAME (element));
292 gst_parse_strfree (value);
296 ERROR (GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY, "Could not set property \"%s\" in element \"%s\" to \"%s\"", value, GST_ELEMENT_NAME (element), pos);
300 gst_parse_free_link (link_t *link)
302 gst_parse_strfree (link->src_name);
303 gst_parse_strfree (link->sink_name);
304 g_slist_foreach (link->src_pads, (GFunc) gst_parse_strfree, NULL);
305 g_slist_foreach (link->sink_pads, (GFunc) gst_parse_strfree, NULL);
306 g_slist_free (link->src_pads);
307 g_slist_free (link->sink_pads);
308 gst_caps_unref (link->caps);
309 gst_parse_link_free (link);
312 gst_parse_element_lock (GstElement *element, gboolean lock)
315 GList *walk = (GList *) gst_element_get_pad_list (element);
316 gboolean unlocked_peer = FALSE;
318 if (gst_element_is_locked_state (element) == lock)
320 /* check if we have an unlocked peer */
322 pad = (GstPad *) GST_PAD_REALIZE (walk->data);
324 if (GST_PAD_IS_SINK (pad) && GST_PAD_PEER (pad) &&
325 !gst_element_is_locked_state (GST_PAD_PARENT (GST_PAD_PEER (pad)))) {
326 unlocked_peer = TRUE;
331 if (!(lock && unlocked_peer)) {
332 gst_element_set_locked_state (element, lock);
334 gst_element_sync_state_with_parent (element);
339 /* check if there are other pads to (un)lock */
340 walk = (GList *) gst_element_get_pad_list (element);
342 pad = (GstPad *) GST_PAD_REALIZE (walk->data);
344 if (GST_PAD_IS_SRC (pad) && GST_PAD_PEER (pad)) {
345 GstElement *next = GST_ELEMENT (GST_OBJECT_PARENT (GST_PAD_PEER (pad)));
346 if (gst_element_is_locked_state (next) != lock)
347 gst_parse_element_lock (next, lock);
352 gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
354 DelayedLink *link = (DelayedLink *) data;
356 GST_INFO (GST_CAT_PIPELINE, "trying delayed linking %s:%s to %s:%s",
357 GST_ELEMENT_NAME (src), link->src_pad,
358 GST_ELEMENT_NAME (link->sink), link->sink_pad);
360 if (gst_element_link_pads_filtered (src, link->src_pad, link->sink, link->sink_pad, link->caps)) {
361 /* do this here, we don't want to get any problems later on when unlocking states */
362 GST_DEBUG (GST_CAT_PIPELINE, "delayed linking %s:%s to %s:%s worked",
363 GST_ELEMENT_NAME (src), link->src_pad,
364 GST_ELEMENT_NAME (link->sink), link->sink_pad);
365 g_signal_handler_disconnect (src, link->signal_id);
366 g_free (link->src_pad);
367 g_free (link->sink_pad);
368 gst_caps_unref (link->caps);
369 if (!gst_element_is_locked_state (src))
370 gst_parse_element_lock (link->sink, FALSE);
374 /* both padnames and the caps may be NULL */
376 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad,
377 GstElement *sink, const gchar *sink_pad, GstCaps *caps)
379 GList *templs = gst_element_get_pad_template_list (src);
382 GstPadTemplate *templ = (GstPadTemplate *) templs->data;
383 if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) && (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
385 /* TODO: maybe we should check if src_pad matches this template's names */
387 GST_DEBUG (GST_CAT_PIPELINE, "trying delayed link %s:%s to %s:%s",
388 GST_ELEMENT_NAME (src), src_pad, GST_ELEMENT_NAME (sink), sink_pad);
390 DelayedLink *data = g_new (DelayedLink, 1);
391 data->src_pad = g_strdup (src_pad);
393 data->sink_pad = g_strdup (sink_pad);
394 data->caps = gst_caps_ref (caps);
395 data->signal_id = g_signal_connect (G_OBJECT (src), "new_pad",
396 G_CALLBACK (gst_parse_found_pad), data);
399 templs = g_list_next (templs);
404 * performs a link and frees the struct. src and sink elements must be given
405 * return values: 0 - link performed
410 gst_parse_perform_link (link_t *link, graph_t *graph)
412 GstElement *src = link->src;
413 GstElement *sink = link->sink;
414 GSList *srcs = link->src_pads;
415 GSList *sinks = link->sink_pads;
416 g_assert (GST_IS_ELEMENT (src));
417 g_assert (GST_IS_ELEMENT (sink));
419 GST_INFO (GST_CAT_PIPELINE, "linking %s(%s):%u to %s(%s):%u",
420 GST_ELEMENT_NAME (src), link->src_name ? link->src_name : "---", g_slist_length (srcs),
421 GST_ELEMENT_NAME (sink), link->sink_name ? link->sink_name : "---", g_slist_length (sinks));
423 if (!srcs || !sinks) {
424 if (gst_element_link_pads_filtered (src, srcs ? (const gchar *) srcs->data : NULL,
425 sink, sinks ? (const gchar *) sinks->data : NULL,
427 gst_parse_element_lock (sink, gst_element_is_locked_state (src));
430 if (gst_parse_perform_delayed_link (src, srcs ? (const gchar *) srcs->data : NULL,
431 sink, sinks ? (const gchar *) sinks->data : NULL,
433 gst_parse_element_lock (sink, TRUE);
440 if (g_slist_length (link->src_pads) != g_slist_length (link->src_pads)) {
443 while (srcs && sinks) {
444 const gchar *src_pad = (const gchar *) srcs->data;
445 const gchar *sink_pad = (const gchar *) sinks->data;
446 srcs = g_slist_next (srcs);
447 sinks = g_slist_next (sinks);
448 if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad, link->caps)) {
449 gst_parse_element_lock (sink, gst_element_is_locked_state (src));
452 if (gst_parse_perform_delayed_link (src, src_pad,
455 gst_parse_element_lock (sink, TRUE);
464 gst_parse_free_link (link);
468 ERROR (GST_PARSE_ERROR_LINK, "could not link %s to %s", GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink));
469 gst_parse_free_link (link);
474 static int yylex (void *lvalp);
475 static int yyerror (const char *s);
487 %token <s> IDENTIFIER
488 %left <s> REF PADREF BINREF
489 %token <s> ASSIGNMENT
494 %type <l> linkpart link
497 %type <p> padlist pads assignments
500 %left '{' '}' '(' ')'
510 element: IDENTIFIER { $$ = gst_element_factory_make ($1, NULL);
512 ERROR (GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element \"%s\"", $1);
513 gst_parse_strfree ($1);
517 | element ASSIGNMENT { gst_parse_element_set ($2, $1, graph);
522 assignments: /* NOP */ { $$ = NULL; }
523 | assignments ASSIGNMENT { $$ = g_slist_prepend ($1, $2); }
525 bin: '{' assignments chain '}' { GST_BIN_MAKE ($$, "thread", $3, $2); }
526 | '(' assignments chain ')' { GST_BIN_MAKE ($$, "bin", $3, $2); }
527 | BINREF assignments chain ')' { GST_BIN_MAKE ($$, $1, $3, $2);
528 gst_parse_strfree ($1);
530 | '{' assignments '}' { GST_BIN_MAKE ($$, "thread", NULL, $2); }
531 | '(' assignments '}' { GST_BIN_MAKE ($$, "thread", NULL, $2); }
532 | BINREF assignments ')' { GST_BIN_MAKE ($$, $1, NULL, $2);
533 gst_parse_strfree ($1);
535 | '{' assignments error '}' { GST_BIN_MAKE ($$, "thread", NULL, $2); }
536 | '(' assignments error '}' { GST_BIN_MAKE ($$, "thread", NULL, $2); }
537 | BINREF assignments error ')' { GST_BIN_MAKE ($$, $1, NULL, $2);
538 gst_parse_strfree ($1);
542 pads: PADREF { $$ = g_slist_prepend (NULL, $1); }
543 | PADREF padlist { $$ = $2;
544 $$ = g_slist_prepend ($$, $1);
547 padlist: ',' IDENTIFIER { $$ = g_slist_prepend (NULL, $2); }
548 | ',' IDENTIFIER padlist { $$ = g_slist_prepend ($3, $2); }
551 reference: REF { MAKE_REF ($$, $1, NULL); }
552 | REF padlist { MAKE_REF ($$, $1, $2); }
555 linkpart: reference { $$ = $1; }
556 | pads { MAKE_REF ($$, NULL, $1); }
557 | /* NOP */ { MAKE_REF ($$, NULL, NULL); }
558 | linkpart error { $$ = $1; }
561 link: linkpart '!' linkpart { $$ = $1;
562 $$->sink_name = $3->src_name;
563 $$->sink_pads = $3->src_pads;
564 gst_parse_link_free ($3);
568 linklist: link { $$ = g_slist_prepend (NULL, $1); }
569 | link linklist { $$ = g_slist_prepend ($2, $1); }
570 | linklist error { $$ = $1; }
573 chain: element { $$ = gst_parse_chain_new ();
574 $$->first = $$->last = $1;
575 $$->front = $$->back = NULL;
576 $$->elements = g_slist_prepend (NULL, $1);
579 | chain chain { if ($1->back && $2->front) {
580 if (!$1->back->sink_name) {
581 ERROR (GST_PARSE_ERROR_LINK, "link without source element");
582 gst_parse_free_link ($1->back);
584 ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
586 if (!$2->front->src_name) {
587 ERROR (GST_PARSE_ERROR_LINK, "link without sink element");
588 gst_parse_free_link ($2->front);
590 ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
593 } else if ($1->back) {
594 if (!$1->back->sink_name) {
595 $1->back->sink = $2->first;
597 ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
600 } else if ($2->front) {
601 if (!$2->front->src_name) {
602 $2->front->src = $1->last;
603 $1->back = $2->front;
605 ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
610 gst_parse_perform_link ($1->back, (graph_t *) graph);
613 $1->elements = g_slist_concat ($1->elements, $2->elements);
614 gst_parse_chain_free ($2);
617 | link chain { if ($2->front) {
618 if (!$2->front->src_name) {
619 ERROR (GST_PARSE_ERROR_LINK, "link without source element");
620 gst_parse_free_link ($2->front);
622 ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
625 if (!$1->sink_name) {
626 $1->sink = $2->first;
632 | chain linklist { GSList *walk;
634 g_slist_prepend ($2, $1->back);
637 if (!((link_t *) $2->data)->src_name) {
638 ((link_t *) $2->data)->src = $1->last;
643 link_t *link = (link_t *) walk->data;
645 if (!link->sink_name) {
646 ERROR (GST_PARSE_ERROR_LINK, "link without sink element");
647 gst_parse_free_link (link);
648 } else if (!link->src_name && !link->src) {
649 ERROR (GST_PARSE_ERROR_LINK, "link without source element");
650 gst_parse_free_link (link);
653 ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, link);
662 | chain error { $$ = $1; }
665 graph: chain { $$ = (graph_t *) graph;
667 if (!$1->front->src_name) {
668 ERROR (GST_PARSE_ERROR_LINK, "link without source element");
669 gst_parse_free_link ($1->front);
671 $$->links = g_slist_prepend ($$->links, $1->front);
676 if (!$1->back->sink_name) {
677 ERROR (GST_PARSE_ERROR_LINK, "link without sink element");
678 gst_parse_free_link ($1->back);
680 $$->links = g_slist_prepend ($$->links, $1->back);
690 extern FILE *_gst_parse_yyin;
691 int _gst_parse_yylex (YYSTYPE *lvalp);
693 static int yylex (void *lvalp) {
694 return _gst_parse_yylex ((YYSTYPE*) lvalp);
698 yyerror (const char *s)
700 /* FIXME: This should go into the GError somehow, but how? */
701 g_warning ("error: %s\n", s);
705 int _gst_parse_yy_scan_string (char*);
707 _gst_parse_launch (const gchar *str, GError **error)
715 g_return_val_if_fail (str != NULL, NULL);
721 #ifdef __GST_PARSE_TRACE
722 GST_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
723 __strings = __chains = __links = 0;
724 #endif /* __GST_PARSE_TRACE */
726 dstr = g_strdup (str);
727 _gst_parse_yy_scan_string (dstr);
729 #ifdef GST_DEBUG_ENABLED
733 if (yyparse (&g) != 0) {
734 SET_ERROR (error, GST_PARSE_ERROR_SYNTAX, "Unrecoverable syntax error while parsing pipeline");
740 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));
744 } else if (!(((chain_t *) g.chain)->elements->next)) {
745 /* only one toplevel element */
746 ret = (GstElement *) ((chain_t *) g.chain)->elements->data;
747 g_slist_free (((chain_t *) g.chain)->elements);
748 if (GST_IS_BIN (ret))
751 /* put all elements in our bin */
752 bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
754 walk = g.chain->elements;
756 gst_bin_add (bin, GST_ELEMENT (walk->data));
757 walk = g_slist_next (walk);
759 g_slist_free (g.chain->elements);
760 ret = GST_ELEMENT (bin);
762 gst_parse_chain_free (g.chain);
767 link_t *l = (link_t *) walk->data;
769 walk = g_slist_next (walk);
773 l->src = gst_bin_get_by_name_recurse_up (bin, l->src_name);
775 l->src = strcmp (GST_ELEMENT_NAME (ret), l->src_name) == 0 ? ret : NULL;
779 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element named \"%s\" - omitting link", l->src_name);
780 gst_parse_free_link (l);
787 l->sink = gst_bin_get_by_name_recurse_up (bin, l->sink_name);
789 l->sink = strcmp (GST_ELEMENT_NAME (ret), l->sink_name) == 0 ? ret : NULL;
793 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element named \"%s\" - omitting link", l->sink_name);
794 gst_parse_free_link (l);
799 gst_parse_perform_link (l, &g);
801 g_slist_free (g.links);
804 #ifdef __GST_PARSE_TRACE
805 GST_DEBUG (GST_CAT_PIPELINE, "TRACE: %u strings, %u chains and %u links left", __strings, __chains, __links);
806 if (__strings || __chains || __links) {
807 g_warning ("TRACE: %u strings, %u chains and %u links left", __strings, __chains, __links);
809 #endif /* __GST_PARSE_TRACE */
817 walk = g.chain->elements;
819 gst_object_unref (GST_OBJECT (walk->data));
822 g_slist_free (g.chain->elements);
824 gst_parse_chain_free (g.chain);
828 gst_parse_free_link ((link_t *) walk->data);
831 g_slist_free (g.links);