2 #include "../gst_private.h"
4 #include <glib-object.h>
10 #include <glib/gi18n-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;
44 static guint __elements;
46 __gst_parse_strdup (gchar *org)
51 /* g_print ("ALLOCATED STR (%3u): %p %s\n", __strings, ret, ret); */
55 __gst_parse_strfree (gchar *str)
58 /* g_print ("FREEING STR (%3u): %p %s\n", __strings - 1, str, str); */
60 g_return_if_fail (__strings > 0);
64 link_t *__gst_parse_link_new (void)
68 ret = g_slice_new0 (link_t);
69 /* g_print ("ALLOCATED LINK (%3u): %p\n", __links, ret); */
73 __gst_parse_link_free (link_t *data)
76 /* g_print ("FREEING LINK (%3u): %p\n", __links - 1, data); */
77 g_slice_free (link_t, data);
78 g_return_if_fail (__links > 0);
83 __gst_parse_chain_new (void)
87 ret = g_slice_new0 (chain_t);
88 /* g_print ("@%p: ALLOCATED CHAIN (%3u):\n", ret, __chains); */
92 __gst_parse_chain_free (chain_t *data)
94 /* g_print ("@%p: FREEING CHAIN (%3u):\n", data, __chains - 1); */
95 g_slice_free (chain_t, data);
96 g_return_if_fail (__chains > 0);
101 __gst_parse_element_new (void)
105 ret = g_slice_new0 (element_t);
106 /* g_print ("@%p: ALLOCATED ELEMENT (%3u):\n", ret, __elements); */
110 __gst_parse_element_free (element_t *data)
112 /* g_print ("@%p: FREEING ELEMENT (%3u):\n", data, __elements - 1); */
113 g_slice_free (element_t, data);
114 g_return_if_fail (__elements > 0);
118 #endif /* __GST_PARSE_TRACE */
120 /*******************************************************************************************
121 *** define SET_ERROR macro/function
122 *******************************************************************************************/
123 #ifdef G_HAVE_ISO_VARARGS
125 # define SET_ERROR(error, type, ...) \
127 GST_CAT_ERROR (GST_CAT_PIPELINE, __VA_ARGS__); \
128 if ((error) && !*(error)) { \
129 g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
133 #elif defined(G_HAVE_GNUC_VARARGS)
135 # define SET_ERROR(error, type, args...) \
137 GST_CAT_ERROR (GST_CAT_PIPELINE, args ); \
138 if ((error) && !*(error)) { \
139 g_set_error ((error), GST_PARSE_ERROR, (type), args ); \
146 SET_ERROR (GError **error, gint type, const char *format, ...)
150 g_warning ("error while parsing");
155 va_start (varargs, format);
156 string = g_strdup_vprintf (format, varargs);
159 g_set_error (error, GST_PARSE_ERROR, type, string);
166 #endif /* G_HAVE_ISO_VARARGS */
168 /*** define YYPRINTF macro/function if we're debugging */
170 /* bison 1.35 calls this macro with side effects, we need to make sure the
171 side effects work - crappy bison */
173 #ifndef GST_DISABLE_GST_DEBUG
176 # ifdef G_HAVE_ISO_VARARGS
178 /* # define YYFPRINTF(a, ...) GST_CAT_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__) */
179 # define YYFPRINTF(a, ...) \
181 GST_CAT_LOG (GST_CAT_PIPELINE, __VA_ARGS__); \
184 # elif defined(G_HAVE_GNUC_VARARGS)
186 # define YYFPRINTF(a, args...) \
188 GST_CAT_LOG (GST_CAT_PIPELINE, args); \
194 YYPRINTF(const char *format, ...)
199 va_start (varargs, format);
200 temp = g_strdup_vprintf (format, varargs);
201 GST_CAT_LOG (GST_CAT_PIPELINE, "%s", temp);
206 # endif /* G_HAVE_ISO_VARARGS */
208 #endif /* GST_DISABLE_GST_DEBUG */
212 * include headers generated by bison & flex, after defining (or not defining) YYDEBUG
214 #include "grammar.tab.h"
215 #include "parse_lex.h"
217 /*******************************************************************************************
218 *** report missing elements/bins/..
219 *******************************************************************************************/
222 static void add_missing_element(graph_t *graph,gchar *name){
224 (graph)->ctx->missing_elements = g_list_append ((graph)->ctx->missing_elements, g_strdup (name));
229 /*******************************************************************************************
230 *** helpers for pipeline-setup
231 *******************************************************************************************/
233 #define TRY_SETUP_LINK(l) G_STMT_START { \
234 if( (!(l)->src.element) && (!(l)->src.name) ){ \
235 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link has no source [sink=%s@%p]"), \
236 (l)->sink.name ? (l)->sink.name : "", \
237 (l)->sink.element); \
238 gst_parse_free_link (l); \
239 }else if( (!(l)->sink.element) && (!(l)->sink.name) ){ \
240 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link has no sink [source=%s@%p]"), \
241 (l)->src.name ? (l)->src.name : "", \
243 gst_parse_free_link (l); \
245 graph->links = g_slist_append (graph->links, l ); \
254 gulong pad_added_signal_id, no_more_pads_signal_id;
264 static int gst_resolve_reference(reference_t *rr, GstElement *pipeline){
267 if(rr->element) return 0; /* already resolved! */
268 if(!rr->name) return -2; /* no chance! */
270 if (GST_IS_BIN (pipeline)){
271 bin = GST_BIN (pipeline);
272 rr->element = gst_bin_get_by_name_recurse_up (bin, rr->name);
274 rr->element = strcmp (GST_ELEMENT_NAME (pipeline), rr->name) == 0 ?
275 gst_object_ref(pipeline) : NULL;
277 if(rr->element) return 0; /* resolved */
278 else return -1; /* not found */
281 static void gst_parse_free_delayed_set (DelayedSet *set)
284 g_free(set->value_str);
285 g_slice_free(DelayedSet, set);
288 static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
289 const gchar * name, gpointer data);
291 static void gst_parse_add_delayed_set (GstElement *element, gchar *name, gchar *value_str)
293 DelayedSet *data = g_slice_new0 (DelayedSet);
295 GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "delaying property set %s to %s",
298 data->name = g_strdup(name);
299 data->value_str = g_strdup(value_str);
300 data->signal_id = g_signal_connect_data(element, "child-added",
301 G_CALLBACK (gst_parse_new_child), data, (GClosureNotify)
302 gst_parse_free_delayed_set, (GConnectFlags) 0);
304 /* FIXME: we would need to listen on all intermediate bins too */
305 if (GST_IS_BIN (element)) {
306 gchar **names, **current;
307 GstElement *parent, *child;
309 current = names = g_strsplit (name, "::", -1);
310 parent = gst_bin_get_by_name (GST_BIN_CAST (element), current[0]);
312 while (parent && current[0]) {
313 child = gst_bin_get_by_name (GST_BIN (parent), current[0]);
314 if (!child && current[1]) {
315 char *sub_name = g_strjoinv ("::", ¤t[0]);
317 gst_parse_add_delayed_set(parent, sub_name, value_str);
320 gst_object_unref (parent);
325 gst_object_unref (parent);
331 gst_parse_separate_prop_from_children (const gchar *name, gchar **children, gchar **property)
333 static const gchar *separator = "::";
334 const gchar *prop = NULL;
336 g_return_val_if_fail (name, FALSE);
337 g_return_val_if_fail (children, FALSE);
338 g_return_val_if_fail (property, FALSE);
340 /* Given "child1::child2::prop" isolate "prop" */
341 prop = g_strrstr (name, separator);
343 GST_WARNING ("%s is not a valid childproxy path", name);
347 /* Make a copy of prop skipping "::" */
348 *property = g_strdup (prop + 2);
350 /* Extract "child1::child2" from "child1::child2::prop" */
352 g_strndup (name, strlen (name) - strlen (prop));
357 static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
358 const gchar * name, gpointer data)
360 DelayedSet *set = (DelayedSet *) data;
363 GObject *target = NULL;
366 GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "new child %s, checking property %s",
369 if (gst_child_proxy_lookup (child_proxy, set->name, &target, &pspec)) {
370 gboolean got_value = FALSE;
372 value_type = pspec->value_type;
374 GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "parsing delayed property %s as a %s from %s",
375 pspec->name, g_type_name (value_type), set->value_str);
376 g_value_init (&v, value_type);
377 if (gst_value_deserialize_with_pspec (&v, set->value_str, pspec))
379 else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
382 bin = gst_parse_bin_from_description_full (set->value_str, TRUE, NULL,
383 GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS | GST_PARSE_FLAG_PLACE_IN_BIN, NULL);
385 g_value_set_object (&v, bin);
389 g_signal_handler_disconnect (child_proxy, set->signal_id);
392 g_object_set_property (target, pspec->name, &v);
394 const gchar *obj_name = GST_OBJECT_NAME(object);
395 gint len = strlen (obj_name);
398 * We've been notified that a new child has beed added, but the
399 * property was still not found. Three things could be happening:
401 * 1. The target property is of the form obj_name::child_name::property
402 * and is for a child that does not (yet) exist:
403 * We need to add the delayed property setting handler on that new child.
405 * 2. The target property is of the form obj_name::property or
406 * obj_name::child_name::property and the child already exists:
407 * We warn about a nonexistent property
409 * 3. The target property is of the form other_obj_name::child_name::property
410 * or other_obj_name::property:
411 * We ignore this case as another delayed set will catch it.
414 /* Cases 1,2: The child just added corresponds to this delayed set */
415 if ((strlen (set->name) > (len+2)) && !strncmp (set->name, obj_name, len)
416 && !strncmp (&set->name[len], "::", 2)) {
417 gchar *children = NULL;
419 GObject *child = NULL;
421 if (!gst_parse_separate_prop_from_children (set->name, &children, &prop)) {
422 /* Malformed property name, ignore */
426 child = gst_child_proxy_get_child_by_name_recurse (child_proxy, children);
430 /* Case 1: A child in the hierarchy does not exist yet, add a new delayed set */
432 gst_parse_add_delayed_set (GST_ELEMENT (child_proxy), set->name, set->value_str);
434 /* Case 2: The target child exists already but there's no such property */
436 gst_object_unref (child);
437 GST_ELEMENT_WARNING(GST_ELEMENT (child_proxy), PARSE, NO_SUCH_PROPERTY,
438 (_("No such property.")), (_("no property \"%s\" in element \"%s\""),
439 set->name, GST_ELEMENT_NAME(child_proxy)));
442 /* Case 3: The child just added does not correspond to this delayed set, just ignore
451 gst_object_unref (target);
455 GST_CAT_ERROR (GST_CAT_PIPELINE, "could not set property \"%s\" in %"
456 GST_PTR_FORMAT, pspec->name, target);
461 gst_parse_split_assignment (gchar *value) {
464 /* parse the string, so the property name is null-terminated and pos points
465 to the beginning of the value */
466 while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++;
472 while (g_ascii_isspace (*pos)) pos++;
475 while (g_ascii_isspace (*pos)) pos++;
476 /* truncate a string if it is delimited with double quotes */
477 if (*pos == '"' && pos[strlen (pos) - 1] == '"') {
479 pos[strlen (pos) - 1] = '\0';
481 gst_parse_unescape (pos);
487 collect_value (GParamSpec *pspec, gchar *value_str, GValue *v)
489 gboolean got_value = FALSE;
491 GST_CAT_LOG (GST_CAT_PIPELINE, "parsing property %s as a %s",
492 pspec->name, g_type_name (pspec->value_type));
494 g_value_init (v, pspec->value_type);
495 if (gst_value_deserialize_with_pspec (v, value_str, pspec))
497 else if (g_type_is_a (pspec->value_type, GST_TYPE_ELEMENT)) {
500 bin = gst_parse_bin_from_description_full (value_str, TRUE, NULL,
501 GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS | GST_PARSE_FLAG_PLACE_IN_BIN, NULL);
503 g_value_set_object (v, bin);
511 static void gst_parse_element_preset (gchar *value, GstElement *element, graph_t *graph)
513 /* do nothing if preset is for missing element or its not a preset element */
517 if (!GST_IS_PRESET(element))
520 /* do nothing if no preset is given */
521 if (value == NULL || *value == '\0')
524 gst_parse_unescape (value);
525 if (!gst_preset_load_preset (GST_PRESET (element), value))
532 SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
533 _("Element \"%s\" is not a GstPreset"),
534 GST_ELEMENT_NAME (element));
538 SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
539 _("could not set preset \"%s\" in element \"%s\""),
540 value, GST_ELEMENT_NAME (element));
548 } proxied_property_t;
551 proxied_property_free (proxied_property_t *pp) {
552 g_slice_free (proxied_property_t, pp);
555 static GstElement * gst_parse_element_make (graph_t *graph, element_t *data) {
556 GstElementFactory *loaded_factory;
557 GstElementFactory *factory = gst_element_factory_find (data->factory_name);
559 GParamSpec *pspec = NULL;
562 GSList *proxied = NULL;
564 guint n_params_alloc = 16;
565 const gchar **names_array;
566 GValue *values_array;
567 GstElement *ret = NULL;
570 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, _("no element \"%s\""), data->factory_name);
575 GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
578 gst_object_unref (factory);
580 klass = g_type_class_ref (gst_element_factory_get_element_type (loaded_factory));
582 is_proxy = g_type_is_a (gst_element_factory_get_element_type (loaded_factory), GST_TYPE_CHILD_PROXY);
584 names_array = g_new0 (const gchar *, n_params_alloc);
585 values_array = g_new0 (GValue, n_params_alloc);
587 for (tmp = data->values; tmp; tmp = tmp->next) {
588 gchar *name = tmp->data;
589 gchar *value = gst_parse_split_assignment (tmp->data);
591 if (is_proxy && strstr (name, "::") != NULL) {
592 proxied_property_t *pp = g_slice_new (proxied_property_t);
595 proxied = g_slist_prepend (proxied, pp);
599 pspec = g_object_class_find_property (klass, name);
602 if (G_UNLIKELY (n_params == n_params_alloc)) {
603 n_params_alloc *= 2u;
605 g_realloc (names_array, sizeof (const gchar *) * n_params_alloc);
606 values_array = g_realloc (values_array, sizeof (GValue) * n_params_alloc);
607 memset (&values_array[n_params], 0,
608 sizeof (GValue) * (n_params_alloc - n_params));
611 if (!collect_value (pspec, value, &values_array[n_params])) {
612 SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
613 _("could not set property \"%s\" in element \"%s\" to \"%s\""),
614 name, data->factory_name, value);
615 g_value_unset (&values_array[n_params]);
618 names_array[n_params] = name;
623 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
624 _("no property \"%s\" in element \"%s\""), name, \
630 ret = gst_element_factory_create_with_properties (factory, n_params, names_array,
633 for (tmp = proxied; tmp; tmp = tmp->next) {
634 GObject *target = NULL;
635 proxied_property_t *pp = tmp->data;
637 if (!gst_child_proxy_lookup (GST_CHILD_PROXY (ret), pp->name, &target, &pspec)) {
638 /* the property was not found. if the target child doesn't exist
639 then we do a delayed set waiting for new elements to be added. If
640 the child was found, we fail since the property doesn't exist.
642 gchar *children = NULL;
643 gchar *property = NULL;
644 if (!gst_parse_separate_prop_from_children (pp->name, &children, &property)) {
645 /* malformed childproxy path, skip */
649 target = gst_child_proxy_get_child_by_name_recurse (GST_CHILD_PROXY (ret), children);
653 if (target == NULL) {
654 gst_parse_add_delayed_set (ret, pp->name, pp->value);
656 gst_object_unref (target);
657 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
658 _("no property \"%s\" in element \"%s\""), pp->name, \
659 GST_ELEMENT_NAME (ret));
665 if (!collect_value (pspec, pp->value, &v)) {
666 SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
667 _("could not set property \"%s\" in child of element \"%s\" to \"%s\""),
668 pp->name, data->factory_name, pp->value);
672 g_object_set_property (target, pspec->name, &v);
678 for (tmp = data->presets; tmp; tmp = tmp->next) {
679 gst_parse_element_preset (tmp->data, ret, graph);
683 g_slist_free_full (proxied, (GDestroyNotify) proxied_property_free);
684 gst_object_unref (loaded_factory);
685 g_type_class_unref (klass);
686 g_free (names_array);
688 g_value_unset (&values_array[n_params]);
689 g_free (values_array);
694 static gboolean gst_parse_child_proxy_find_child (
695 GstChildProxy * child_proxy, const gchar *name)
697 gchar **names = NULL, **current = NULL;
699 gboolean found = FALSE;
701 obj = G_OBJECT (g_object_ref (child_proxy));
703 current = names = g_strsplit (name, "::", -1);
705 /* find the owner of the property */
707 GObject *next = NULL;
709 /* Cannot ask for the child of a non-childproxy */
710 if (!GST_IS_CHILD_PROXY (obj)) {
714 next = gst_child_proxy_get_child_by_name (GST_CHILD_PROXY (obj),
717 /* The child doesn't exist yet */
722 gst_object_unref (obj);
727 gst_object_unref (obj);
730 /* The remaining name is the property, we have found the object */
731 if (current[1] == NULL) {
738 static void gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
740 GParamSpec *pspec = NULL;
743 GObject *target = NULL;
745 /* do nothing if assignment is for missing element */
749 pos = gst_parse_split_assignment (value);
751 if (GST_IS_CHILD_PROXY (element) && strstr (value, "::") != NULL) {
752 if (!gst_child_proxy_lookup (GST_CHILD_PROXY (element), value, &target, &pspec)) {
753 /* the property was not found. if the target child doesn't exist
754 then we do a delayed set waiting for new elements to be added. If
755 the child was found, we fail since the property doesn't exist.
757 if (!gst_parse_child_proxy_find_child (GST_CHILD_PROXY (element), value)) {
758 gst_parse_add_delayed_set (element, value, pos);
764 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value);
766 target = G_OBJECT (g_object_ref (element));
767 GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, target, "found %s property", value);
769 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
770 _("no property \"%s\" in element \"%s\""), value, \
771 GST_ELEMENT_NAME (element));
775 if (pspec != NULL && target != NULL) {
776 if (!collect_value (pspec, pos, &v)) {
779 g_object_set_property (target, pspec->name, &v);
784 gst_parse_strfree (value);
788 gst_object_unref (target);
792 SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
793 _("could not set property \"%s\" in element \"%s\" to \"%s\""),
794 value, GST_ELEMENT_NAME (element), pos);
798 static void gst_parse_free_reference (reference_t *rr)
800 if(rr->element) gst_object_unref(rr->element);
801 gst_parse_strfree (rr->name);
802 g_slist_foreach (rr->pads, (GFunc) gst_parse_strfree, NULL);
803 g_slist_free (rr->pads);
806 static void gst_parse_free_link (link_t *link)
808 gst_parse_free_reference (&(link->src));
809 gst_parse_free_reference (&(link->sink));
810 if (link->caps) gst_caps_unref (link->caps);
811 gst_parse_link_free (link);
814 static void gst_parse_free_chain (chain_t *ch)
817 gst_parse_free_reference (&(ch->first));
818 gst_parse_free_reference (&(ch->last));
819 for(walk=ch->elements;walk;walk=walk->next)
820 gst_object_unref (walk->data);
821 g_slist_free (ch->elements);
822 gst_parse_chain_free (ch);
825 static void gst_parse_free_element (element_t *el)
827 g_slist_free_full (el->values, gst_parse_strfree);
828 g_slist_free_full (el->presets, gst_parse_strfree);
829 gst_parse_strfree (el->factory_name);
830 gst_parse_element_free (el);
833 static void gst_parse_free_delayed_link (DelayedLink *link)
835 g_free (link->src_pad);
836 g_free (link->sink_pad);
837 if (link->caps) gst_caps_unref (link->caps);
838 g_slice_free (DelayedLink, link);
841 #define PRETTY_PAD_NAME_FMT "%s %s of %s named %s"
842 #define PRETTY_PAD_NAME_ARGS(elem, pad_name) \
843 (pad_name ? "pad " : "some"), (pad_name ? pad_name : "pad"), \
844 G_OBJECT_TYPE_NAME(elem), GST_STR_NULL (GST_ELEMENT_NAME (elem))
846 static void gst_parse_no_more_pads (GstElement *src, gpointer data)
848 DelayedLink *link = data;
850 /* Don't warn for all-pads links, as we expect those to
851 * still be active at no-more-pads */
852 if (!link->all_pads) {
853 GST_ELEMENT_WARNING(src, PARSE, DELAYED_LINK,
854 (_("Delayed linking failed.")),
855 ("failed delayed linking " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
856 PRETTY_PAD_NAME_ARGS (src, link->src_pad),
857 PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad)));
859 /* we keep the handlers connected, so that in case an element still adds a pad
860 * despite no-more-pads, we will consider it for pending delayed links */
863 static void gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
865 DelayedLink *link = data;
867 GST_CAT_INFO (GST_CAT_PIPELINE,
868 "trying delayed linking %s " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
869 link->all_pads ? "all pads" : "one pad",
870 PRETTY_PAD_NAME_ARGS (src, link->src_pad),
871 PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad));
873 if (gst_element_link_pads_filtered (src, link->src_pad, link->sink,
874 link->sink_pad, link->caps)) {
875 /* do this here, we don't want to get any problems later on when
876 * unlocking states */
877 GST_CAT_DEBUG (GST_CAT_PIPELINE,
878 "delayed linking %s " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT " worked",
879 link->all_pads ? "all pads" : "one pad",
880 PRETTY_PAD_NAME_ARGS (src, link->src_pad),
881 PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad));
882 /* releases 'link' */
883 if (!link->all_pads) {
884 g_signal_handler_disconnect (src, link->no_more_pads_signal_id);
885 g_signal_handler_disconnect (src, link->pad_added_signal_id);
890 /* both padnames and the caps may be NULL */
892 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad,
893 GstElement *sink, const gchar *sink_pad,
894 GstCaps *caps, gboolean all_pads)
896 GList *templs = gst_element_class_get_pad_template_list (
897 GST_ELEMENT_GET_CLASS (src));
899 for (; templs; templs = templs->next) {
900 GstPadTemplate *templ = (GstPadTemplate *) templs->data;
901 if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) &&
902 (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
904 DelayedLink *data = g_slice_new (DelayedLink);
906 data->all_pads = all_pads;
908 /* TODO: maybe we should check if src_pad matches this template's names */
910 GST_CAT_DEBUG (GST_CAT_PIPELINE,
911 "trying delayed link " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
912 PRETTY_PAD_NAME_ARGS (src, src_pad),
913 PRETTY_PAD_NAME_ARGS (sink, sink_pad));
915 data->src_pad = g_strdup (src_pad);
917 data->sink_pad = g_strdup (sink_pad);
919 data->caps = gst_caps_copy (caps);
923 data->pad_added_signal_id = g_signal_connect_data (src, "pad-added",
924 G_CALLBACK (gst_parse_found_pad), data,
925 (GClosureNotify) gst_parse_free_delayed_link, (GConnectFlags) 0);
926 data->no_more_pads_signal_id = g_signal_connect (src, "no-more-pads",
927 G_CALLBACK (gst_parse_no_more_pads), data);
935 gst_parse_element_can_do_caps (GstElement * e, GstPadDirection dir,
938 gboolean can_do = FALSE, done = FALSE;
941 it = (dir == GST_PAD_SRC) ? gst_element_iterate_src_pads (e) : gst_element_iterate_sink_pads (e);
943 while (!done && !can_do) {
944 GValue v = G_VALUE_INIT;
948 switch (gst_iterator_next (it, &v)) {
949 case GST_ITERATOR_OK:
950 pad = g_value_get_object (&v);
952 caps = gst_pad_get_current_caps (pad);
954 caps = gst_pad_query_caps (pad, NULL);
956 can_do = gst_caps_can_intersect (caps, link_caps);
958 GST_TRACE ("can_do: %d for %" GST_PTR_FORMAT " and %" GST_PTR_FORMAT,
959 can_do, caps, link_caps);
961 gst_caps_unref (caps);
965 case GST_ITERATOR_DONE:
966 case GST_ITERATOR_ERROR:
969 case GST_ITERATOR_RESYNC:
970 gst_iterator_resync (it);
975 gst_iterator_free (it);
981 * performs a link and frees the struct. src and sink elements must be given
982 * return values 0 - link performed
987 gst_parse_perform_link (link_t *link, graph_t *graph)
989 GstElement *src = link->src.element;
990 GstElement *sink = link->sink.element;
991 GSList *srcs = link->src.pads;
992 GSList *sinks = link->sink.pads;
993 g_assert (GST_IS_ELEMENT (src));
994 g_assert (GST_IS_ELEMENT (sink));
996 GST_CAT_INFO (GST_CAT_PIPELINE,
997 "linking " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT " (%u/%u) with caps \"%" GST_PTR_FORMAT "\"",
998 PRETTY_PAD_NAME_ARGS (src, link->src.name),
999 PRETTY_PAD_NAME_ARGS (sink, link->sink.name),
1000 g_slist_length (srcs), g_slist_length (sinks), link->caps);
1002 if (!srcs || !sinks) {
1003 gboolean found_one = gst_element_link_pads_filtered (src,
1004 srcs ? (const gchar *) srcs->data : NULL, sink,
1005 sinks ? (const gchar *) sinks->data : NULL, link->caps);
1008 if (!link->all_pads)
1009 goto success; /* Linked one, and not an all-pads link = we're done */
1011 /* Try and link more available pads */
1012 while (gst_element_link_pads_filtered (src,
1013 srcs ? (const gchar *) srcs->data : NULL, sink,
1014 sinks ? (const gchar *) sinks->data : NULL, link->caps));
1017 /* We either didn't find any static pads, or this is a all-pads link,
1018 * in which case watch for future pads and link those. Not a failure
1019 * in the all-pads case if there's no sometimes pads to watch */
1020 if (gst_parse_perform_delayed_link (src,
1021 srcs ? (const gchar *) srcs->data : NULL,
1022 sink, sinks ? (const gchar *) sinks->data : NULL, link->caps,
1023 link->all_pads) || link->all_pads) {
1029 if (g_slist_length (link->src.pads) != g_slist_length (link->sink.pads)) {
1032 while (srcs && sinks) {
1033 const gchar *src_pad = (const gchar *) srcs->data;
1034 const gchar *sink_pad = (const gchar *) sinks->data;
1035 srcs = g_slist_next (srcs);
1036 sinks = g_slist_next (sinks);
1037 if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad,
1041 if (gst_parse_perform_delayed_link (src, src_pad,
1043 link->caps, link->all_pads)) {
1052 gst_parse_free_link (link);
1056 if (link->caps != NULL) {
1057 gboolean src_can_do_caps, sink_can_do_caps;
1058 gchar *caps_str = gst_caps_to_string (link->caps);
1061 gst_parse_element_can_do_caps (src, GST_PAD_SRC, link->caps);
1063 gst_parse_element_can_do_caps (sink, GST_PAD_SINK, link->caps);
1065 if (!src_can_do_caps && sink_can_do_caps) {
1066 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1067 _("could not link %s to %s, %s can't handle caps %s"),
1068 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink),
1069 GST_ELEMENT_NAME (src), caps_str);
1070 } else if (src_can_do_caps && !sink_can_do_caps) {
1071 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1072 _("could not link %s to %s, %s can't handle caps %s"),
1073 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink),
1074 GST_ELEMENT_NAME (sink), caps_str);
1075 } else if (!src_can_do_caps && !sink_can_do_caps) {
1076 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1077 _("could not link %s to %s, neither element can handle caps %s"),
1078 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink), caps_str);
1080 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1081 _("could not link %s to %s with caps %s"),
1082 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink), caps_str);
1086 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1087 _("could not link %s to %s"), GST_ELEMENT_NAME (src),
1088 GST_ELEMENT_NAME (sink));
1090 gst_parse_free_link (link);
1095 static int yyerror (void *scanner, graph_t *graph, const char *s);
1108 /* No grammar ambiguities expected, FAIL otherwise */
1111 %token <ss> PARSE_URL
1112 %token <ss> IDENTIFIER
1113 %left <ss> REF PADREF BINREF
1114 %token <ss> ASSIGNMENT PRESET
1116 %token <ss> LINK_ALL
1118 %type <ss> binopener
1120 %type <cc> chain bin chainlist openchain elementary
1121 %type <rr> reference
1124 %type <pp> morepads pads assignments
1126 %destructor { gst_parse_strfree ($$); } <ss>
1127 %destructor { if($$)
1128 gst_parse_free_chain($$); } <cc>
1129 %destructor { gst_parse_free_link ($$); } <ll>
1130 %destructor { gst_parse_free_reference(&($$));} <rr>
1131 %destructor { gst_parse_free_element ($$); } <ee>
1132 %destructor { GSList *walk;
1133 for(walk=$$;walk;walk=walk->next)
1134 gst_parse_strfree (walk->data);
1135 g_slist_free ($$); } <pp>
1144 %lex-param { void *scanner }
1145 %parse-param { void *scanner }
1146 %parse-param { graph_t *graph }
1152 /*************************************************************
1153 * Grammar explanation:
1154 * _element_s are specified by an identifier of their type.
1155 * a name can be give in the optional property-assignments
1158 * identity silence=false name=frodo
1160 **************************************************************/
1161 element: IDENTIFIER {
1162 $$ = gst_parse_element_new();
1163 $$->factory_name = $1;
1166 $$->presets = g_slist_append ($$->presets, $2);
1169 | element ASSIGNMENT {
1170 $$->values = g_slist_append ($$->values, $2);
1175 /*************************************************************
1176 * Grammar explanation: (cont'd)
1177 * a graph has (pure) _element_s, _bin_s and _link_s.
1178 * since bins are special elements, bins and elements can
1179 * be generalized as _elementary_.
1180 * The construction of _bin_s will be discussed later.
1183 **************************************************************/
1186 GstElement *element = NULL;
1188 $$ = gst_parse_chain_new ();
1190 if ($1 && !(element = gst_parse_element_make (graph, $1))) {
1191 add_missing_element(graph, $1->factory_name);
1193 /* g_print ("@%p: CHAINing elementary\n", $$); */
1194 $$->first.element = element ? gst_object_ref(element) : NULL;
1195 $$->last.element = element ? gst_object_ref(element) : NULL;
1196 $$->first.name = $$->last.name = NULL;
1197 $$->first.pads = $$->last.pads = NULL;
1198 $$->elements = element ? g_slist_prepend (NULL, element) : NULL;
1201 gst_parse_free_element ($1);
1207 /*************************************************************
1208 * Grammar explanation: (cont'd)
1209 * a _chain_ is a list of _elementary_s that have _link_s in between
1210 * which are represented through infix-notation.
1212 * fakesrc ! sometransformation ! fakesink
1214 * every _link_ can be augmented with _pads_.
1216 * coffeesrc .sound ! speakersink
1217 * multisrc .movie,ads ! .projector,smallscreen multisink
1219 * and every _link_ can be setup to filter media-types
1220 * mediasrc ! audio/x-raw, signed=TRUE ! stereosink
1223 * if the lexer does not recognize your media-type it
1224 * will make it an element name. that results in errors
1226 * NO SUCH ELEMENT: no element audio7x-raw
1227 * '7' vs. '/' in https://en.wikipedia.org/wiki/QWERTZ
1230 * in the parser we need to differ between chains that can
1231 * be extended by more elementaries (_openchain_) and others
1232 * that are syntactically closed (handled later in this file).
1233 * (e.g. fakesrc ! sinkreferencename.padname)
1234 **************************************************************/
1235 chain: openchain { $$=$1;
1237 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
1238 _("unexpected reference \"%s\" - ignoring"), $$->last.name);
1239 gst_parse_strfree($$->last.name);
1243 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
1244 _("unexpected pad-reference \"%s\" - ignoring"), (gchar*)$$->last.pads->data);
1245 g_slist_foreach ($$->last.pads, (GFunc) gst_parse_strfree, NULL);
1246 g_slist_free ($$->last.pads);
1253 elementary pads { $$=$1;
1254 $$->last.pads = g_slist_concat ($$->last.pads, $2);
1255 /* g_print ("@%p@%p: FKI elementary pads\n", $1, $$->last.pads); */
1257 | openchain link pads elementary pads
1260 $2->sink = $4->first;
1261 $2->sink.pads = g_slist_concat ($3, $2->sink.pads);
1263 $4->first = $1->first;
1264 $4->elements = g_slist_concat ($1->elements, $4->elements);
1265 gst_parse_chain_free($1);
1267 $$->last.pads = g_slist_concat ($$->last.pads, $5);
1271 link: LINK { $$ = gst_parse_link_new ();
1272 $$->all_pads = FALSE;
1274 $$->caps = gst_caps_from_string ($1);
1275 if ($$->caps == NULL)
1276 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $1);
1277 gst_parse_strfree ($1);
1280 | LINK_ALL { $$ = gst_parse_link_new ();
1281 $$->all_pads = TRUE;
1283 $$->caps = gst_caps_from_string ($1);
1284 if ($$->caps == NULL)
1285 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $1);
1286 gst_parse_strfree ($1);
1290 pads: /* NOP */ { $$ = NULL; }
1291 | PADREF morepads { $$ = $2;
1292 $$ = g_slist_prepend ($$, $1);
1295 morepads: /* NOP */ { $$ = NULL; }
1296 | ',' IDENTIFIER morepads { $$ = g_slist_prepend ($3, $2); }
1299 /*************************************************************
1300 * Grammar explanation: (cont'd)
1301 * the first and last elements of a _chain_ can be give
1302 * as URL. This creates special elements that fit the URL.
1304 * fakesrc ! http://fake-sink.org
1305 * http://somesource.org ! fakesink
1306 **************************************************************/
1308 chain: openchain link PARSE_URL { GstElement *element =
1309 gst_element_make_from_uri (GST_URI_SINK, $3, NULL, NULL);
1310 /* FIXME: get and parse error properly */
1312 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1313 _("no sink element for URI \"%s\""), $3);
1316 $2->sink.element = element?gst_object_ref(element):NULL;
1319 $$->last.element = NULL;
1320 $$->last.name = NULL;
1321 $$->last.pads = NULL;
1322 if(element) $$->elements = g_slist_append ($$->elements, element);
1327 PARSE_URL { GstElement *element =
1328 gst_element_make_from_uri (GST_URI_SRC, $1, NULL, NULL);
1329 /* FIXME: get and parse error properly */
1331 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1332 _("no source element for URI \"%s\""), $1);
1334 $$ = gst_parse_chain_new ();
1335 /* g_print ("@%p: CHAINing srcURL\n", $$); */
1336 $$->first.element = NULL;
1337 $$->first.name = NULL;
1338 $$->first.pads = NULL;
1339 $$->last.element = element ? gst_object_ref(element):NULL;
1340 $$->last.name = NULL;
1341 $$->last.pads = NULL;
1342 $$->elements = element ? g_slist_prepend (NULL, element) : NULL;
1348 /*************************************************************
1349 * Grammar explanation: (cont'd)
1350 * the first and last elements of a _chain_ can be linked
1351 * to a named _reference_ (with optional pads).
1353 * fakesrc ! nameOfSinkElement.
1354 * fakesrc ! nameOfSinkElement.Padname
1355 * fakesrc ! nameOfSinkElement.Padname, anotherPad
1356 * nameOfSource.Padname ! fakesink
1357 **************************************************************/
1359 chain: openchain link reference { $$ = $1;
1363 $$->last.element = NULL;
1364 $$->last.name = NULL;
1365 $$->last.pads = NULL;
1371 reference { $$ = gst_parse_chain_new ();
1373 $$->first.element = NULL;
1374 $$->first.name = NULL;
1375 $$->first.pads = NULL;
1376 $$->elements = NULL;
1379 reference: REF morepads {
1380 gchar *padname = $1;
1383 while (*padname != '.') padname++;
1386 if (*padname != '\0')
1387 pads = g_slist_prepend (pads, gst_parse_strdup (padname));
1396 /*************************************************************
1397 * Grammar explanation: (cont'd)
1398 * a _chainlist_ is just a list of _chain_s.
1400 * You can specify _link_s with named
1401 * _reference_ on each side. That
1402 * works already after the explanations above.
1403 * someSourceName.Pad ! someSinkName.
1404 * someSourceName.Pad,anotherPad ! someSinkName.Apad,Bpad
1406 * If a syntax error occurs, the already finished _chain_s
1407 * and _links_ are kept intact.
1408 *************************************************************/
1410 chainlist: /* NOP */ { $$ = NULL; }
1411 | chainlist chain { if ($1){
1412 gst_parse_free_reference(&($1->last));
1413 gst_parse_free_reference(&($2->first));
1414 $2->first = $1->first;
1415 $2->elements = g_slist_concat ($1->elements, $2->elements);
1416 gst_parse_chain_free ($1);
1420 | chainlist error { $$=$1;
1421 GST_CAT_DEBUG (GST_CAT_PIPELINE,"trying to recover from syntax error");
1422 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX, _("syntax error"));
1426 /*************************************************************
1427 * Grammar explanation: (cont'd)
1429 *************************************************************/
1432 assignments: /* NOP */ { $$ = NULL; }
1433 | ASSIGNMENT assignments { $$ = g_slist_prepend ($2, $1); }
1436 binopener: '(' { $$ = gst_parse_strdup("bin"); }
1437 | BINREF { $$ = $1; }
1439 bin: binopener assignments chainlist ')' {
1440 chain_t *chain = $3;
1442 GstBin *bin = (GstBin *) gst_element_factory_make ($1, NULL);
1444 SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY_BIN,
1445 _("specified empty bin \"%s\", not allowed"), $1);
1446 chain = gst_parse_chain_new ();
1447 chain->first.element = chain->last.element = NULL;
1448 chain->first.name = chain->last.name = NULL;
1449 chain->first.pads = chain->last.pads = NULL;
1450 chain->elements = NULL;
1453 add_missing_element(graph, $1);
1454 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1455 _("no bin \"%s\", unpacking elements"), $1);
1456 /* clear property-list */
1457 g_slist_foreach ($2, (GFunc) gst_parse_strfree, NULL);
1461 for (walk = chain->elements; walk; walk = walk->next )
1462 gst_bin_add (bin, GST_ELEMENT (walk->data));
1463 g_slist_free (chain->elements);
1464 chain->elements = g_slist_prepend (NULL, bin);
1467 /* set the properties now
1468 * HINT: property-list cleared above, if bin==NULL
1470 for (walk = $2; walk; walk = walk->next)
1471 gst_parse_element_set ((gchar *) walk->data,
1472 GST_ELEMENT (bin), graph);
1474 gst_parse_strfree ($1);
1478 /*************************************************************
1479 * Grammar explanation: (cont'd)
1481 *************************************************************/
1483 graph: chainlist { $$ = graph;
1486 SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
1495 yyerror (void *scanner, graph_t *graph, const char *s)
1497 /* FIXME: This should go into the GError somehow, but how? */
1498 GST_WARNING ("Error during parsing: %s", s);
1504 priv_gst_parse_launch (const gchar *str, GError **error, GstParseContext *ctx,
1505 GstParseFlags flags)
1513 g_return_val_if_fail (str != NULL, NULL);
1514 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1522 #ifdef __GST_PARSE_TRACE
1523 GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
1524 __strings = __chains = __links = __elements = 0;
1525 #endif /* __GST_PARSE_TRACE */
1527 /* g_print("Now scanning: %s\n", str); */
1529 dstr = g_strdup (str);
1530 priv_gst_parse_yylex_init (&scanner);
1531 priv_gst_parse_yy_scan_string (dstr, scanner);
1537 if (yyparse (scanner, &g) != 0) {
1538 SET_ERROR (error, GST_PARSE_ERROR_SYNTAX,
1539 "Unrecoverable syntax error while parsing pipeline %s", str);
1541 priv_gst_parse_yylex_destroy (scanner);
1546 priv_gst_parse_yylex_destroy (scanner);
1549 GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links",
1550 g.chain ? g_slist_length (g.chain->elements) : 0,
1551 g_slist_length (g.links));
1553 /* ensure chain is not NULL */
1555 g.chain=gst_parse_chain_new ();
1556 g.chain->elements=NULL;
1557 g.chain->first.element=NULL;
1558 g.chain->first.name=NULL;
1559 g.chain->first.pads=NULL;
1560 g.chain->last.element=NULL;
1561 g.chain->last.name=NULL;
1562 g.chain->last.pads=NULL;
1565 /* ensure elements is not empty */
1566 if(!g.chain->elements){
1567 g.chain->elements= g_slist_prepend (NULL, NULL);
1570 /* put all elements in our bin if necessary */
1571 if(g.chain->elements->next){
1573 if (flags & GST_PARSE_FLAG_PLACE_IN_BIN)
1574 bin = GST_BIN (gst_element_factory_make ("bin", NULL));
1576 bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
1579 for (walk = g.chain->elements; walk; walk = walk->next) {
1580 if (walk->data != NULL)
1581 gst_bin_add (bin, GST_ELEMENT (walk->data));
1583 g_slist_free (g.chain->elements);
1584 g.chain->elements = g_slist_prepend (NULL, bin);
1587 ret = (GstElement *) g.chain->elements->data;
1588 g_slist_free (g.chain->elements);
1589 g.chain->elements=NULL;
1590 gst_parse_free_chain (g.chain);
1594 /* resolve and perform links */
1595 for (walk = g.links; walk; walk = walk->next) {
1596 link_t *l = (link_t *) walk->data;
1598 err=gst_resolve_reference( &(l->src), ret);
1601 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1602 "No src-element named \"%s\" - omitting link", l->src.name);
1604 /* probably a missing element which we've handled already */
1605 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1606 "No src-element found - omitting link");
1608 gst_parse_free_link (l);
1612 err=gst_resolve_reference( &(l->sink), ret);
1615 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1616 "No sink-element named \"%s\" - omitting link", l->src.name);
1618 /* probably a missing element which we've handled already */
1619 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1620 "No sink-element found - omitting link");
1622 gst_parse_free_link (l);
1625 gst_parse_perform_link (l, &g);
1627 g_slist_free (g.links);
1630 #ifdef __GST_PARSE_TRACE
1631 GST_CAT_DEBUG (GST_CAT_PIPELINE,
1632 "TRACE: %u strings, %u chains, %u links and %u elements left", __strings, __chains,
1633 __links, __elements);
1634 if (__strings || __chains || __links || __elements) {
1635 g_warning ("TRACE: %u strings, %u chains, %u links and %u elements left", __strings,
1636 __chains, __links, __elements);
1638 #endif /* __GST_PARSE_TRACE */
1644 gst_parse_free_chain (g.chain);
1648 g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
1649 g_slist_free (g.links);