2 #include "../gst_private.h"
4 #include <glib-object.h>
10 #include "../gst-i18n-lib.h"
12 #include "../gstconfig.h"
13 #include "../gstparse.h"
14 #include "../gstinfo.h"
15 #include "../gsterror.h"
16 #include "../gststructure.h"
17 #include "../gsturi.h"
18 #include "../gstutils.h"
19 #include "../gstvalue.h"
20 #include "../gstchildproxy.h"
23 /* All error messages in this file are user-visible and need to be translated.
24 * Don't start the message with a capital, and don't end them with a period,
25 * as they will be presented inside a sentence/error.
28 #define YYERROR_VERBOSE 1
30 #define YYENABLE_NLS 0
32 #ifndef YYLTYPE_IS_TRIVIAL
33 #define YYLTYPE_IS_TRIVIAL 0
36 /*******************************************************************************************
37 *** Tracing memory leaks
38 *******************************************************************************************/
40 #ifdef __GST_PARSE_TRACE
41 static guint __strings;
43 static guint __chains;
45 __gst_parse_strdup (gchar *org)
50 /* g_print ("ALLOCATED STR (%3u): %p %s\n", __strings, ret, ret); */
54 __gst_parse_strfree (gchar *str)
57 /* g_print ("FREEING STR (%3u): %p %s\n", __strings - 1, str, str); */
59 g_return_if_fail (__strings > 0);
63 link_t *__gst_parse_link_new (void)
67 ret = g_slice_new0 (link_t);
68 /* g_print ("ALLOCATED LINK (%3u): %p\n", __links, ret); */
72 __gst_parse_link_free (link_t *data)
75 /* g_print ("FREEING LINK (%3u): %p\n", __links - 1, data); */
76 g_slice_free (link_t, data);
77 g_return_if_fail (__links > 0);
82 __gst_parse_chain_new (void)
86 ret = g_slice_new0 (chain_t);
87 /* g_print ("@%p: ALLOCATED CHAIN (%3u):\n", ret, __chains); */
91 __gst_parse_chain_free (chain_t *data)
93 /* g_print ("@%p: FREEING CHAIN (%3u):\n", data, __chains - 1); */
94 g_slice_free (chain_t, data);
95 g_return_if_fail (__chains > 0);
99 #endif /* __GST_PARSE_TRACE */
101 /*******************************************************************************************
102 *** define SET_ERROR macro/function
103 *******************************************************************************************/
104 #ifdef G_HAVE_ISO_VARARGS
106 # define SET_ERROR(error, type, ...) \
108 GST_CAT_ERROR (GST_CAT_PIPELINE, __VA_ARGS__); \
109 if ((error) && !*(error)) { \
110 g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
114 #elif defined(G_HAVE_GNUC_VARARGS)
116 # define SET_ERROR(error, type, args...) \
118 GST_CAT_ERROR (GST_CAT_PIPELINE, args ); \
119 if ((error) && !*(error)) { \
120 g_set_error ((error), GST_PARSE_ERROR, (type), args ); \
127 SET_ERROR (GError **error, gint type, const char *format, ...)
131 g_warning ("error while parsing");
136 va_start (varargs, format);
137 string = g_strdup_vprintf (format, varargs);
140 g_set_error (error, GST_PARSE_ERROR, type, string);
147 #endif /* G_HAVE_ISO_VARARGS */
149 /*** define YYPRINTF macro/function if we're debugging */
151 /* bison 1.35 calls this macro with side effects, we need to make sure the
152 side effects work - crappy bison */
154 #ifndef GST_DISABLE_GST_DEBUG
157 # ifdef G_HAVE_ISO_VARARGS
159 /* # define YYFPRINTF(a, ...) GST_CAT_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__) */
160 # define YYFPRINTF(a, ...) \
162 GST_CAT_LOG (GST_CAT_PIPELINE, __VA_ARGS__); \
165 # elif defined(G_HAVE_GNUC_VARARGS)
167 # define YYFPRINTF(a, args...) \
169 GST_CAT_LOG (GST_CAT_PIPELINE, args); \
175 YYPRINTF(const char *format, ...)
180 va_start (varargs, format);
181 temp = g_strdup_vprintf (format, varargs);
182 GST_CAT_LOG (GST_CAT_PIPELINE, "%s", temp);
187 # endif /* G_HAVE_ISO_VARARGS */
189 #endif /* GST_DISABLE_GST_DEBUG */
193 * include headers generated by bison & flex, after defining (or not defining) YYDEBUG
195 #include "grammar.tab.h"
196 #include "parse_lex.h"
198 /*******************************************************************************************
199 *** report missing elements/bins/..
200 *******************************************************************************************/
203 static void add_missing_element(graph_t *graph,gchar *name){
205 (graph)->ctx->missing_elements = g_list_append ((graph)->ctx->missing_elements, g_strdup (name));
210 /*******************************************************************************************
211 *** helpers for pipeline-setup
212 *******************************************************************************************/
214 #define TRY_SETUP_LINK(l) G_STMT_START { \
215 if( (!(l)->src.element) && (!(l)->src.name) ){ \
216 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link has no source [sink=%s@%p]"), \
217 (l)->sink.name ? (l)->sink.name : "", \
218 (l)->sink.element); \
219 gst_parse_free_link (l); \
220 }else if( (!(l)->sink.element) && (!(l)->sink.name) ){ \
221 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link has no sink [source=%s@%p]"), \
222 (l)->src.name ? (l)->src.name : "", \
224 gst_parse_free_link (l); \
226 graph->links = g_slist_append (graph->links, l ); \
235 gulong pad_added_signal_id, no_more_pads_signal_id;
245 static int gst_resolve_reference(reference_t *rr, GstElement *pipeline){
248 if(rr->element) return 0; /* already resolved! */
249 if(!rr->name) return -2; /* no chance! */
251 if (GST_IS_BIN (pipeline)){
252 bin = GST_BIN (pipeline);
253 rr->element = gst_bin_get_by_name_recurse_up (bin, rr->name);
255 rr->element = strcmp (GST_ELEMENT_NAME (pipeline), rr->name) == 0 ?
256 gst_object_ref(pipeline) : NULL;
258 if(rr->element) return 0; /* resolved */
259 else return -1; /* not found */
262 static void gst_parse_free_delayed_set (DelayedSet *set)
265 g_free(set->value_str);
266 g_slice_free(DelayedSet, set);
269 static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
270 const gchar * name, gpointer data);
272 static void gst_parse_add_delayed_set (GstElement *element, gchar *name, gchar *value_str)
274 DelayedSet *data = g_slice_new0 (DelayedSet);
276 GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "delaying property set %s to %s",
279 data->name = g_strdup(name);
280 data->value_str = g_strdup(value_str);
281 data->signal_id = g_signal_connect_data(element, "child-added",
282 G_CALLBACK (gst_parse_new_child), data, (GClosureNotify)
283 gst_parse_free_delayed_set, (GConnectFlags) 0);
285 /* FIXME: we would need to listen on all intermediate bins too */
286 if (GST_IS_BIN (element)) {
287 gchar **names, **current;
288 GstElement *parent, *child;
290 current = names = g_strsplit (name, "::", -1);
291 parent = gst_bin_get_by_name (GST_BIN_CAST (element), current[0]);
293 while (parent && current[0]) {
294 child = gst_bin_get_by_name (GST_BIN (parent), current[0]);
295 if (!child && current[1]) {
296 char *sub_name = g_strjoinv ("::", ¤t[0]);
298 gst_parse_add_delayed_set(parent, sub_name, value_str);
301 gst_object_unref (parent);
306 gst_object_unref (parent);
311 static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
312 const gchar * name, gpointer data)
314 DelayedSet *set = (DelayedSet *) data;
317 GObject *target = NULL;
320 GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "new child %s, checking property %s",
323 if (gst_child_proxy_lookup (child_proxy, set->name, &target, &pspec)) {
324 gboolean got_value = FALSE;
326 value_type = pspec->value_type;
328 GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "parsing delayed property %s as a %s from %s",
329 pspec->name, g_type_name (value_type), set->value_str);
330 g_value_init (&v, value_type);
331 if (gst_value_deserialize_with_pspec (&v, set->value_str, pspec))
333 else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
336 bin = gst_parse_bin_from_description_full (set->value_str, TRUE, NULL,
337 GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS | GST_PARSE_FLAG_PLACE_IN_BIN, NULL);
339 g_value_set_object (&v, bin);
343 g_signal_handler_disconnect (child_proxy, set->signal_id);
346 g_object_set_property (target, pspec->name, &v);
348 const gchar *obj_name = GST_OBJECT_NAME(object);
349 gint len = strlen (obj_name);
351 /* do a delayed set */
352 if ((strlen (set->name) > (len + 2)) && !strncmp (set->name, obj_name, len) && !strncmp (&set->name[len], "::", 2)) {
353 gst_parse_add_delayed_set (GST_ELEMENT(child_proxy), set->name, set->value_str);
361 gst_object_unref (target);
365 GST_CAT_ERROR (GST_CAT_PIPELINE, "could not set property \"%s\" in %"
366 GST_PTR_FORMAT, pspec->name, target);
370 static void gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
372 GParamSpec *pspec = NULL;
375 GObject *target = NULL;
378 /* do nothing if assignment is for missing element */
382 /* parse the string, so the property name is null-terminated and pos points
383 to the beginning of the value */
384 while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++;
390 while (g_ascii_isspace (*pos)) pos++;
393 while (g_ascii_isspace (*pos)) pos++;
394 /* truncate a string if it is delimited with double quotes */
395 if (*pos == '"' && pos[strlen (pos) - 1] == '"') {
397 pos[strlen (pos) - 1] = '\0';
399 gst_parse_unescape (pos);
401 if (GST_IS_CHILD_PROXY (element) && strstr (value, "::") != NULL) {
402 if (!gst_child_proxy_lookup (GST_CHILD_PROXY (element), value, &target, &pspec)) {
403 /* do a delayed set */
404 gst_parse_add_delayed_set (element, value, pos);
407 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value);
409 target = G_OBJECT (g_object_ref (element));
410 GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, target, "found %s property", value);
412 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
413 _("no property \"%s\" in element \"%s\""), value, \
414 GST_ELEMENT_NAME (element));
418 if (pspec != NULL && target != NULL) {
419 gboolean got_value = FALSE;
421 value_type = pspec->value_type;
423 GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "parsing property %s as a %s",
424 pspec->name, g_type_name (value_type));
426 g_value_init (&v, value_type);
427 if (gst_value_deserialize_with_pspec (&v, pos, pspec))
429 else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
432 bin = gst_parse_bin_from_description_full (pos, TRUE, NULL,
433 GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS | GST_PARSE_FLAG_PLACE_IN_BIN, NULL);
435 g_value_set_object (&v, bin);
441 g_object_set_property (target, pspec->name, &v);
445 gst_parse_strfree (value);
449 gst_object_unref (target);
453 SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
454 _("could not set property \"%s\" in element \"%s\" to \"%s\""),
455 value, GST_ELEMENT_NAME (element), pos);
459 static void gst_parse_element_preset (gchar *value, GstElement *element, graph_t *graph)
461 /* do nothing if preset is for missing element or its not a preset element */
465 if (!GST_IS_PRESET(element))
468 /* do nothing if no preset is given */
469 if (value == NULL || *value == '\0')
472 gst_parse_unescape (value);
473 if (!gst_preset_load_preset (GST_PRESET (element), value))
477 gst_parse_strfree (value);
481 SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
482 _("Element \"%s\" is not a GstPreset"),
483 GST_ELEMENT_NAME (element));
487 SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
488 _("could not set preset \"%s\" in element \"%s\""),
489 value, GST_ELEMENT_NAME (element));
493 static void gst_parse_free_reference (reference_t *rr)
495 if(rr->element) gst_object_unref(rr->element);
496 gst_parse_strfree (rr->name);
497 g_slist_foreach (rr->pads, (GFunc) gst_parse_strfree, NULL);
498 g_slist_free (rr->pads);
501 static void gst_parse_free_link (link_t *link)
503 gst_parse_free_reference (&(link->src));
504 gst_parse_free_reference (&(link->sink));
505 if (link->caps) gst_caps_unref (link->caps);
506 gst_parse_link_free (link);
509 static void gst_parse_free_chain (chain_t *ch)
512 gst_parse_free_reference (&(ch->first));
513 gst_parse_free_reference (&(ch->last));
514 for(walk=ch->elements;walk;walk=walk->next)
515 gst_object_unref (walk->data);
516 g_slist_free (ch->elements);
517 gst_parse_chain_free (ch);
520 static void gst_parse_free_delayed_link (DelayedLink *link)
522 g_free (link->src_pad);
523 g_free (link->sink_pad);
524 if (link->caps) gst_caps_unref (link->caps);
525 g_slice_free (DelayedLink, link);
528 #define PRETTY_PAD_NAME_FMT "%s %s of %s named %s"
529 #define PRETTY_PAD_NAME_ARGS(elem, pad_name) \
530 (pad_name ? "pad " : "some"), (pad_name ? pad_name : "pad"), \
531 G_OBJECT_TYPE_NAME(elem), GST_STR_NULL (GST_ELEMENT_NAME (elem))
533 static void gst_parse_no_more_pads (GstElement *src, gpointer data)
535 DelayedLink *link = data;
537 /* Don't warn for all-pads links, as we expect those to
538 * still be active at no-more-pads */
539 if (!link->all_pads) {
540 GST_ELEMENT_WARNING(src, PARSE, DELAYED_LINK,
541 (_("Delayed linking failed.")),
542 ("failed delayed linking " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
543 PRETTY_PAD_NAME_ARGS (src, link->src_pad),
544 PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad)));
546 /* we keep the handlers connected, so that in case an element still adds a pad
547 * despite no-more-pads, we will consider it for pending delayed links */
550 static void gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
552 DelayedLink *link = data;
554 GST_CAT_INFO (GST_CAT_PIPELINE,
555 "trying delayed linking %s " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
556 link->all_pads ? "all pads" : "one pad",
557 PRETTY_PAD_NAME_ARGS (src, link->src_pad),
558 PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad));
560 if (gst_element_link_pads_filtered (src, link->src_pad, link->sink,
561 link->sink_pad, link->caps)) {
562 /* do this here, we don't want to get any problems later on when
563 * unlocking states */
564 GST_CAT_DEBUG (GST_CAT_PIPELINE,
565 "delayed linking %s " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT " worked",
566 link->all_pads ? "all pads" : "one pad",
567 PRETTY_PAD_NAME_ARGS (src, link->src_pad),
568 PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad));
569 /* releases 'link' */
570 if (!link->all_pads) {
571 g_signal_handler_disconnect (src, link->no_more_pads_signal_id);
572 g_signal_handler_disconnect (src, link->pad_added_signal_id);
577 /* both padnames and the caps may be NULL */
579 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad,
580 GstElement *sink, const gchar *sink_pad,
581 GstCaps *caps, gboolean all_pads)
583 GList *templs = gst_element_class_get_pad_template_list (
584 GST_ELEMENT_GET_CLASS (src));
586 for (; templs; templs = templs->next) {
587 GstPadTemplate *templ = (GstPadTemplate *) templs->data;
588 if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) &&
589 (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
591 DelayedLink *data = g_slice_new (DelayedLink);
593 data->all_pads = all_pads;
595 /* TODO: maybe we should check if src_pad matches this template's names */
597 GST_CAT_DEBUG (GST_CAT_PIPELINE,
598 "trying delayed link " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
599 PRETTY_PAD_NAME_ARGS (src, src_pad),
600 PRETTY_PAD_NAME_ARGS (sink, sink_pad));
602 data->src_pad = g_strdup (src_pad);
604 data->sink_pad = g_strdup (sink_pad);
606 data->caps = gst_caps_copy (caps);
610 data->pad_added_signal_id = g_signal_connect_data (src, "pad-added",
611 G_CALLBACK (gst_parse_found_pad), data,
612 (GClosureNotify) gst_parse_free_delayed_link, (GConnectFlags) 0);
613 data->no_more_pads_signal_id = g_signal_connect (src, "no-more-pads",
614 G_CALLBACK (gst_parse_no_more_pads), data);
622 gst_parse_element_can_do_caps (GstElement * e, GstPadDirection dir,
625 gboolean can_do = FALSE, done = FALSE;
628 it = (dir == GST_PAD_SRC) ? gst_element_iterate_src_pads (e) : gst_element_iterate_sink_pads (e);
630 while (!done && !can_do) {
631 GValue v = G_VALUE_INIT;
635 switch (gst_iterator_next (it, &v)) {
636 case GST_ITERATOR_OK:
637 pad = g_value_get_object (&v);
639 caps = gst_pad_get_current_caps (pad);
641 caps = gst_pad_query_caps (pad, NULL);
643 can_do = gst_caps_can_intersect (caps, link_caps);
645 GST_TRACE ("can_do: %d for %" GST_PTR_FORMAT " and %" GST_PTR_FORMAT,
646 can_do, caps, link_caps);
648 gst_caps_unref (caps);
652 case GST_ITERATOR_DONE:
653 case GST_ITERATOR_ERROR:
656 case GST_ITERATOR_RESYNC:
657 gst_iterator_resync (it);
662 gst_iterator_free (it);
668 * performs a link and frees the struct. src and sink elements must be given
669 * return values 0 - link performed
674 gst_parse_perform_link (link_t *link, graph_t *graph)
676 GstElement *src = link->src.element;
677 GstElement *sink = link->sink.element;
678 GSList *srcs = link->src.pads;
679 GSList *sinks = link->sink.pads;
680 g_assert (GST_IS_ELEMENT (src));
681 g_assert (GST_IS_ELEMENT (sink));
683 GST_CAT_INFO (GST_CAT_PIPELINE,
684 "linking " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT " (%u/%u) with caps \"%" GST_PTR_FORMAT "\"",
685 PRETTY_PAD_NAME_ARGS (src, link->src.name),
686 PRETTY_PAD_NAME_ARGS (sink, link->sink.name),
687 g_slist_length (srcs), g_slist_length (sinks), link->caps);
689 if (!srcs || !sinks) {
690 gboolean found_one = gst_element_link_pads_filtered (src,
691 srcs ? (const gchar *) srcs->data : NULL, sink,
692 sinks ? (const gchar *) sinks->data : NULL, link->caps);
696 goto success; /* Linked one, and not an all-pads link = we're done */
698 /* Try and link more available pads */
699 while (gst_element_link_pads_filtered (src,
700 srcs ? (const gchar *) srcs->data : NULL, sink,
701 sinks ? (const gchar *) sinks->data : NULL, link->caps));
704 /* We either didn't find any static pads, or this is a all-pads link,
705 * in which case watch for future pads and link those. Not a failure
706 * in the all-pads case if there's no sometimes pads to watch */
707 if (gst_parse_perform_delayed_link (src,
708 srcs ? (const gchar *) srcs->data : NULL,
709 sink, sinks ? (const gchar *) sinks->data : NULL, link->caps,
710 link->all_pads) || link->all_pads) {
716 if (g_slist_length (link->src.pads) != g_slist_length (link->sink.pads)) {
719 while (srcs && sinks) {
720 const gchar *src_pad = (const gchar *) srcs->data;
721 const gchar *sink_pad = (const gchar *) sinks->data;
722 srcs = g_slist_next (srcs);
723 sinks = g_slist_next (sinks);
724 if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad,
728 if (gst_parse_perform_delayed_link (src, src_pad,
730 link->caps, link->all_pads)) {
739 gst_parse_free_link (link);
743 if (link->caps != NULL) {
744 gboolean src_can_do_caps, sink_can_do_caps;
745 gchar *caps_str = gst_caps_to_string (link->caps);
748 gst_parse_element_can_do_caps (src, GST_PAD_SRC, link->caps);
750 gst_parse_element_can_do_caps (sink, GST_PAD_SINK, link->caps);
752 if (!src_can_do_caps && sink_can_do_caps) {
753 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
754 _("could not link %s to %s, %s can't handle caps %s"),
755 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink),
756 GST_ELEMENT_NAME (src), caps_str);
757 } else if (src_can_do_caps && !sink_can_do_caps) {
758 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
759 _("could not link %s to %s, %s can't handle caps %s"),
760 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink),
761 GST_ELEMENT_NAME (sink), caps_str);
762 } else if (!src_can_do_caps && !sink_can_do_caps) {
763 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
764 _("could not link %s to %s, neither element can handle caps %s"),
765 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink), caps_str);
767 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
768 _("could not link %s to %s with caps %s"),
769 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink), caps_str);
773 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
774 _("could not link %s to %s"), GST_ELEMENT_NAME (src),
775 GST_ELEMENT_NAME (sink));
777 gst_parse_free_link (link);
782 static int yyerror (void *scanner, graph_t *graph, const char *s);
795 /* No grammar ambiguities expected, FAIL otherwise */
798 %token <ss> PARSE_URL
799 %token <ss> IDENTIFIER
800 %left <ss> REF PADREF BINREF
801 %token <ss> ASSIGNMENT PRESET
807 %type <cc> chain bin chainlist openchain elementary
811 %type <pp> morepads pads assignments
813 %destructor { gst_parse_strfree ($$); } <ss>
815 gst_parse_free_chain($$); } <cc>
816 %destructor { gst_parse_free_link ($$); } <ll>
817 %destructor { gst_parse_free_reference(&($$));} <rr>
818 %destructor { gst_object_unref ($$); } <ee>
819 %destructor { GSList *walk;
820 for(walk=$$;walk;walk=walk->next)
821 gst_parse_strfree (walk->data);
822 g_slist_free ($$); } <pp>
831 %lex-param { void *scanner }
832 %parse-param { void *scanner }
833 %parse-param { graph_t *graph }
839 /*************************************************************
840 * Grammar explanation:
841 * _element_s are specified by an identifier of their type.
842 * a name can be give in the optional property-assignments
845 * identity silence=false name=frodo
847 **************************************************************/
848 element: IDENTIFIER { $$ = gst_element_factory_make ($1, NULL);
850 add_missing_element(graph, $1);
851 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, _("no element \"%s\""), $1);
853 gst_parse_strfree ($1);
855 | element PRESET { gst_parse_element_preset ($2, $1, graph);
858 | element ASSIGNMENT { gst_parse_element_set ($2, $1, graph);
863 /*************************************************************
864 * Grammar explanation: (cont'd)
865 * a graph has (pure) _element_s, _bin_s and _link_s.
866 * since bins are special elements, bins and elements can
867 * be generalized as _elementary_.
868 * The construction of _bin_s will be discussed later.
871 **************************************************************/
873 element { $$ = gst_parse_chain_new ();
874 /* g_print ("@%p: CHAINing elementary\n", $$); */
875 $$->first.element = $1? gst_object_ref($1) : NULL;
876 $$->last.element = $1? gst_object_ref($1) : NULL;
877 $$->first.name = $$->last.name = NULL;
878 $$->first.pads = $$->last.pads = NULL;
879 $$->elements = $1 ? g_slist_prepend (NULL, $1) : NULL;
884 /*************************************************************
885 * Grammar explanation: (cont'd)
886 * a _chain_ is a list of _elementary_s that have _link_s in between
887 * which are represented through infix-notation.
889 * fakesrc ! sometransformation ! fakesink
891 * every _link_ can be augmented with _pads_.
893 * coffeesrc .sound ! speakersink
894 * multisrc .movie,ads ! .projector,smallscreen multisink
896 * and every _link_ can be setup to filter media-types
897 * mediasrc ! audio/x-raw, signed=TRUE ! stereosink
900 * if the lexer does not recognize your media-type it
901 * will make it an element name. that results in errors
903 * NO SUCH ELEMENT: no element audio7x-raw
904 * '7' vs. '/' in https://en.wikipedia.org/wiki/QWERTZ
907 * in the parser we need to differ between chains that can
908 * be extended by more elementaries (_openchain_) and others
909 * that are syntactically closed (handled later in this file).
910 * (e.g. fakesrc ! sinkreferencename.padname)
911 **************************************************************/
912 chain: openchain { $$=$1;
914 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
915 _("unexpected reference \"%s\" - ignoring"), $$->last.name);
916 gst_parse_strfree($$->last.name);
920 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
921 _("unexpected pad-reference \"%s\" - ignoring"), (gchar*)$$->last.pads->data);
922 g_slist_foreach ($$->last.pads, (GFunc) gst_parse_strfree, NULL);
923 g_slist_free ($$->last.pads);
930 elementary pads { $$=$1;
931 $$->last.pads = g_slist_concat ($$->last.pads, $2);
932 /* g_print ("@%p@%p: FKI elementary pads\n", $1, $$->last.pads); */
934 | openchain link pads elementary pads
937 $2->sink = $4->first;
938 $2->sink.pads = g_slist_concat ($3, $2->sink.pads);
940 $4->first = $1->first;
941 $4->elements = g_slist_concat ($1->elements, $4->elements);
942 gst_parse_chain_free($1);
944 $$->last.pads = g_slist_concat ($$->last.pads, $5);
948 link: LINK { $$ = gst_parse_link_new ();
949 $$->all_pads = FALSE;
951 $$->caps = gst_caps_from_string ($1);
952 if ($$->caps == NULL)
953 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $1);
954 gst_parse_strfree ($1);
957 | LINK_ALL { $$ = gst_parse_link_new ();
960 $$->caps = gst_caps_from_string ($1);
961 if ($$->caps == NULL)
962 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $1);
963 gst_parse_strfree ($1);
967 pads: /* NOP */ { $$ = NULL; }
968 | PADREF morepads { $$ = $2;
969 $$ = g_slist_prepend ($$, $1);
972 morepads: /* NOP */ { $$ = NULL; }
973 | ',' IDENTIFIER morepads { $$ = g_slist_prepend ($3, $2); }
976 /*************************************************************
977 * Grammar explanation: (cont'd)
978 * the first and last elements of a _chain_ can be give
979 * as URL. This creates special elements that fit the URL.
981 * fakesrc ! http://fake-sink.org
982 * http://somesource.org ! fakesink
983 **************************************************************/
985 chain: openchain link PARSE_URL { GstElement *element =
986 gst_element_make_from_uri (GST_URI_SINK, $3, NULL, NULL);
987 /* FIXME: get and parse error properly */
989 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
990 _("no sink element for URI \"%s\""), $3);
993 $2->sink.element = element?gst_object_ref(element):NULL;
996 $$->last.element = NULL;
997 $$->last.name = NULL;
998 $$->last.pads = NULL;
999 if(element) $$->elements = g_slist_append ($$->elements, element);
1004 PARSE_URL { GstElement *element =
1005 gst_element_make_from_uri (GST_URI_SRC, $1, NULL, NULL);
1006 /* FIXME: get and parse error properly */
1008 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1009 _("no source element for URI \"%s\""), $1);
1011 $$ = gst_parse_chain_new ();
1012 /* g_print ("@%p: CHAINing srcURL\n", $$); */
1013 $$->first.element = NULL;
1014 $$->first.name = NULL;
1015 $$->first.pads = NULL;
1016 $$->last.element = element ? gst_object_ref(element):NULL;
1017 $$->last.name = NULL;
1018 $$->last.pads = NULL;
1019 $$->elements = element ? g_slist_prepend (NULL, element) : NULL;
1025 /*************************************************************
1026 * Grammar explanation: (cont'd)
1027 * the first and last elements of a _chain_ can be linked
1028 * to a named _reference_ (with optional pads).
1030 * fakesrc ! nameOfSinkElement.
1031 * fakesrc ! nameOfSinkElement.Padname
1032 * fakesrc ! nameOfSinkElement.Padname, anotherPad
1033 * nameOfSource.Padname ! fakesink
1034 **************************************************************/
1036 chain: openchain link reference { $$ = $1;
1040 $$->last.element = NULL;
1041 $$->last.name = NULL;
1042 $$->last.pads = NULL;
1048 reference { $$ = gst_parse_chain_new ();
1050 $$->first.element = NULL;
1051 $$->first.name = NULL;
1052 $$->first.pads = NULL;
1053 $$->elements = NULL;
1056 reference: REF morepads {
1057 gchar *padname = $1;
1060 while (*padname != '.') padname++;
1063 if (*padname != '\0')
1064 pads = g_slist_prepend (pads, gst_parse_strdup (padname));
1073 /*************************************************************
1074 * Grammar explanation: (cont'd)
1075 * a _chainlist_ is just a list of _chain_s.
1077 * You can specify _link_s with named
1078 * _reference_ on each side. That
1079 * works already after the explanations above.
1080 * someSourceName.Pad ! someSinkName.
1081 * someSourceName.Pad,anotherPad ! someSinkName.Apad,Bpad
1083 * If a syntax error occurs, the already finished _chain_s
1084 * and _links_ are kept intact.
1085 *************************************************************/
1087 chainlist: /* NOP */ { $$ = NULL; }
1088 | chainlist chain { if ($1){
1089 gst_parse_free_reference(&($1->last));
1090 gst_parse_free_reference(&($2->first));
1091 $2->first = $1->first;
1092 $2->elements = g_slist_concat ($1->elements, $2->elements);
1093 gst_parse_chain_free ($1);
1097 | chainlist error { $$=$1;
1098 GST_CAT_DEBUG (GST_CAT_PIPELINE,"trying to recover from syntax error");
1099 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX, _("syntax error"));
1103 /*************************************************************
1104 * Grammar explanation: (cont'd)
1106 *************************************************************/
1109 assignments: /* NOP */ { $$ = NULL; }
1110 | ASSIGNMENT assignments { $$ = g_slist_prepend ($2, $1); }
1113 binopener: '(' { $$ = gst_parse_strdup("bin"); }
1114 | BINREF { $$ = $1; }
1116 bin: binopener assignments chainlist ')' {
1117 chain_t *chain = $3;
1119 GstBin *bin = (GstBin *) gst_element_factory_make ($1, NULL);
1121 SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY_BIN,
1122 _("specified empty bin \"%s\", not allowed"), $1);
1123 chain = gst_parse_chain_new ();
1124 chain->first.element = chain->last.element = NULL;
1125 chain->first.name = chain->last.name = NULL;
1126 chain->first.pads = chain->last.pads = NULL;
1127 chain->elements = NULL;
1130 add_missing_element(graph, $1);
1131 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1132 _("no bin \"%s\", unpacking elements"), $1);
1133 /* clear property-list */
1134 g_slist_foreach ($2, (GFunc) gst_parse_strfree, NULL);
1138 for (walk = chain->elements; walk; walk = walk->next )
1139 gst_bin_add (bin, GST_ELEMENT (walk->data));
1140 g_slist_free (chain->elements);
1141 chain->elements = g_slist_prepend (NULL, bin);
1144 /* set the properties now
1145 * HINT: property-list cleared above, if bin==NULL
1147 for (walk = $2; walk; walk = walk->next)
1148 gst_parse_element_set ((gchar *) walk->data,
1149 GST_ELEMENT (bin), graph);
1151 gst_parse_strfree ($1);
1155 /*************************************************************
1156 * Grammar explanation: (cont'd)
1158 *************************************************************/
1160 graph: chainlist { $$ = graph;
1163 SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
1172 yyerror (void *scanner, graph_t *graph, const char *s)
1174 /* FIXME: This should go into the GError somehow, but how? */
1175 GST_WARNING ("Error during parsing: %s", s);
1181 priv_gst_parse_launch (const gchar *str, GError **error, GstParseContext *ctx,
1182 GstParseFlags flags)
1190 g_return_val_if_fail (str != NULL, NULL);
1191 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1199 #ifdef __GST_PARSE_TRACE
1200 GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
1201 __strings = __chains = __links = 0;
1202 #endif /* __GST_PARSE_TRACE */
1204 /* g_print("Now scanning: %s\n", str); */
1206 dstr = g_strdup (str);
1207 priv_gst_parse_yylex_init (&scanner);
1208 priv_gst_parse_yy_scan_string (dstr, scanner);
1214 if (yyparse (scanner, &g) != 0) {
1215 SET_ERROR (error, GST_PARSE_ERROR_SYNTAX,
1216 "Unrecoverable syntax error while parsing pipeline %s", str);
1218 priv_gst_parse_yylex_destroy (scanner);
1223 priv_gst_parse_yylex_destroy (scanner);
1226 GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links",
1227 g.chain ? g_slist_length (g.chain->elements) : 0,
1228 g_slist_length (g.links));
1230 /* ensure chain is not NULL */
1232 g.chain=gst_parse_chain_new ();
1233 g.chain->elements=NULL;
1234 g.chain->first.element=NULL;
1235 g.chain->first.name=NULL;
1236 g.chain->first.pads=NULL;
1237 g.chain->last.element=NULL;
1238 g.chain->last.name=NULL;
1239 g.chain->last.pads=NULL;
1242 /* ensure elements is not empty */
1243 if(!g.chain->elements){
1244 g.chain->elements= g_slist_prepend (NULL, NULL);
1247 /* put all elements in our bin if necessary */
1248 if(g.chain->elements->next){
1250 if (flags & GST_PARSE_FLAG_PLACE_IN_BIN)
1251 bin = GST_BIN (gst_element_factory_make ("bin", NULL));
1253 bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
1256 for (walk = g.chain->elements; walk; walk = walk->next) {
1257 if (walk->data != NULL)
1258 gst_bin_add (bin, GST_ELEMENT (walk->data));
1260 g_slist_free (g.chain->elements);
1261 g.chain->elements = g_slist_prepend (NULL, bin);
1264 ret = (GstElement *) g.chain->elements->data;
1265 g_slist_free (g.chain->elements);
1266 g.chain->elements=NULL;
1267 gst_parse_free_chain (g.chain);
1271 /* resolve and perform links */
1272 for (walk = g.links; walk; walk = walk->next) {
1273 link_t *l = (link_t *) walk->data;
1275 err=gst_resolve_reference( &(l->src), ret);
1278 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1279 "No src-element named \"%s\" - omitting link", l->src.name);
1281 /* probably a missing element which we've handled already */
1282 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1283 "No src-element found - omitting link");
1285 gst_parse_free_link (l);
1289 err=gst_resolve_reference( &(l->sink), ret);
1292 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1293 "No sink-element named \"%s\" - omitting link", l->src.name);
1295 /* probably a missing element which we've handled already */
1296 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1297 "No sink-element found - omitting link");
1299 gst_parse_free_link (l);
1302 gst_parse_perform_link (l, &g);
1304 g_slist_free (g.links);
1307 #ifdef __GST_PARSE_TRACE
1308 GST_CAT_DEBUG (GST_CAT_PIPELINE,
1309 "TRACE: %u strings, %u chains and %u links left", __strings, __chains,
1311 if (__strings || __chains || __links) {
1312 g_warning ("TRACE: %u strings, %u chains and %u links left", __strings,
1315 #endif /* __GST_PARSE_TRACE */
1321 gst_parse_free_chain (g.chain);
1325 g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
1326 g_slist_free (g.links);