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);
676 gst_object_unref (target);
680 for (tmp = data->presets; tmp; tmp = tmp->next) {
681 gst_parse_element_preset (tmp->data, ret, graph);
685 g_slist_free_full (proxied, (GDestroyNotify) proxied_property_free);
686 gst_object_unref (loaded_factory);
687 g_type_class_unref (klass);
688 g_free (names_array);
690 g_value_unset (&values_array[n_params]);
691 g_free (values_array);
696 static gboolean gst_parse_child_proxy_find_child (
697 GstChildProxy * child_proxy, const gchar *name)
699 gchar **names = NULL, **current = NULL;
701 gboolean found = FALSE;
703 obj = G_OBJECT (g_object_ref (child_proxy));
705 current = names = g_strsplit (name, "::", -1);
707 /* find the owner of the property */
709 GObject *next = NULL;
711 /* Cannot ask for the child of a non-childproxy */
712 if (!GST_IS_CHILD_PROXY (obj)) {
716 next = gst_child_proxy_get_child_by_name (GST_CHILD_PROXY (obj),
719 /* The child doesn't exist yet */
724 gst_object_unref (obj);
729 gst_object_unref (obj);
732 /* The remaining name is the property, we have found the object */
733 if (current[1] == NULL) {
740 static void gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
742 GParamSpec *pspec = NULL;
745 GObject *target = NULL;
747 /* do nothing if assignment is for missing element */
751 pos = gst_parse_split_assignment (value);
753 if (GST_IS_CHILD_PROXY (element) && strstr (value, "::") != NULL) {
754 if (!gst_child_proxy_lookup (GST_CHILD_PROXY (element), value, &target, &pspec)) {
755 /* the property was not found. if the target child doesn't exist
756 then we do a delayed set waiting for new elements to be added. If
757 the child was found, we fail since the property doesn't exist.
759 if (!gst_parse_child_proxy_find_child (GST_CHILD_PROXY (element), value)) {
760 gst_parse_add_delayed_set (element, value, pos);
766 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value);
768 target = G_OBJECT (g_object_ref (element));
769 GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, target, "found %s property", value);
771 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
772 _("no property \"%s\" in element \"%s\""), value, \
773 GST_ELEMENT_NAME (element));
777 if (pspec != NULL && target != NULL) {
778 if (!collect_value (pspec, pos, &v)) {
781 g_object_set_property (target, pspec->name, &v);
786 gst_parse_strfree (value);
790 gst_object_unref (target);
794 SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
795 _("could not set property \"%s\" in element \"%s\" to \"%s\""),
796 value, GST_ELEMENT_NAME (element), pos);
800 static void gst_parse_free_reference (reference_t *rr)
802 if(rr->element) gst_object_unref(rr->element);
803 gst_parse_strfree (rr->name);
804 g_slist_foreach (rr->pads, (GFunc) gst_parse_strfree, NULL);
805 g_slist_free (rr->pads);
808 static void gst_parse_free_link (link_t *link)
810 gst_parse_free_reference (&(link->src));
811 gst_parse_free_reference (&(link->sink));
812 if (link->caps) gst_caps_unref (link->caps);
813 gst_parse_link_free (link);
816 static void gst_parse_free_chain (chain_t *ch)
819 gst_parse_free_reference (&(ch->first));
820 gst_parse_free_reference (&(ch->last));
821 for(walk=ch->elements;walk;walk=walk->next)
822 gst_object_unref (walk->data);
823 g_slist_free (ch->elements);
824 gst_parse_chain_free (ch);
827 static void gst_parse_free_element (element_t *el)
829 g_slist_free_full (el->values, gst_parse_strfree);
830 g_slist_free_full (el->presets, gst_parse_strfree);
831 gst_parse_strfree (el->factory_name);
832 gst_parse_element_free (el);
835 static void gst_parse_free_delayed_link (DelayedLink *link)
837 g_free (link->src_pad);
838 g_free (link->sink_pad);
839 if (link->caps) gst_caps_unref (link->caps);
840 g_slice_free (DelayedLink, link);
843 #define PRETTY_PAD_NAME_FMT "%s %s of %s named %s"
844 #define PRETTY_PAD_NAME_ARGS(elem, pad_name) \
845 (pad_name ? "pad " : "some"), (pad_name ? pad_name : "pad"), \
846 G_OBJECT_TYPE_NAME(elem), GST_STR_NULL (GST_ELEMENT_NAME (elem))
848 static void gst_parse_no_more_pads (GstElement *src, gpointer data)
850 DelayedLink *link = data;
852 /* Don't warn for all-pads links, as we expect those to
853 * still be active at no-more-pads */
854 if (!link->all_pads) {
855 GST_ELEMENT_WARNING(src, PARSE, DELAYED_LINK,
856 (_("Delayed linking failed.")),
857 ("failed delayed linking " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
858 PRETTY_PAD_NAME_ARGS (src, link->src_pad),
859 PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad)));
861 /* we keep the handlers connected, so that in case an element still adds a pad
862 * despite no-more-pads, we will consider it for pending delayed links */
865 static void gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
867 DelayedLink *link = data;
869 GST_CAT_INFO (GST_CAT_PIPELINE,
870 "trying delayed linking %s " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
871 link->all_pads ? "all pads" : "one pad",
872 PRETTY_PAD_NAME_ARGS (src, link->src_pad),
873 PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad));
875 if (gst_element_link_pads_filtered (src, link->src_pad, link->sink,
876 link->sink_pad, link->caps)) {
877 /* do this here, we don't want to get any problems later on when
878 * unlocking states */
879 GST_CAT_DEBUG (GST_CAT_PIPELINE,
880 "delayed linking %s " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT " worked",
881 link->all_pads ? "all pads" : "one pad",
882 PRETTY_PAD_NAME_ARGS (src, link->src_pad),
883 PRETTY_PAD_NAME_ARGS (link->sink, link->sink_pad));
884 /* releases 'link' */
885 if (!link->all_pads) {
886 g_signal_handler_disconnect (src, link->no_more_pads_signal_id);
887 g_signal_handler_disconnect (src, link->pad_added_signal_id);
892 /* both padnames and the caps may be NULL */
894 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad,
895 GstElement *sink, const gchar *sink_pad,
896 GstCaps *caps, gboolean all_pads)
898 GList *templs = gst_element_class_get_pad_template_list (
899 GST_ELEMENT_GET_CLASS (src));
901 for (; templs; templs = templs->next) {
902 GstPadTemplate *templ = (GstPadTemplate *) templs->data;
903 if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) &&
904 (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
906 DelayedLink *data = g_slice_new (DelayedLink);
908 data->all_pads = all_pads;
910 /* TODO: maybe we should check if src_pad matches this template's names */
912 GST_CAT_DEBUG (GST_CAT_PIPELINE,
913 "trying delayed link " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT,
914 PRETTY_PAD_NAME_ARGS (src, src_pad),
915 PRETTY_PAD_NAME_ARGS (sink, sink_pad));
917 data->src_pad = g_strdup (src_pad);
919 data->sink_pad = g_strdup (sink_pad);
921 data->caps = gst_caps_copy (caps);
925 data->pad_added_signal_id = g_signal_connect_data (src, "pad-added",
926 G_CALLBACK (gst_parse_found_pad), data,
927 (GClosureNotify) gst_parse_free_delayed_link, (GConnectFlags) 0);
928 data->no_more_pads_signal_id = g_signal_connect (src, "no-more-pads",
929 G_CALLBACK (gst_parse_no_more_pads), data);
937 gst_parse_element_can_do_caps (GstElement * e, GstPadDirection dir,
940 gboolean can_do = FALSE, done = FALSE;
943 it = (dir == GST_PAD_SRC) ? gst_element_iterate_src_pads (e) : gst_element_iterate_sink_pads (e);
945 while (!done && !can_do) {
946 GValue v = G_VALUE_INIT;
950 switch (gst_iterator_next (it, &v)) {
951 case GST_ITERATOR_OK:
952 pad = g_value_get_object (&v);
954 caps = gst_pad_get_current_caps (pad);
956 caps = gst_pad_query_caps (pad, NULL);
958 can_do = gst_caps_can_intersect (caps, link_caps);
960 GST_TRACE ("can_do: %d for %" GST_PTR_FORMAT " and %" GST_PTR_FORMAT,
961 can_do, caps, link_caps);
963 gst_caps_unref (caps);
967 case GST_ITERATOR_DONE:
968 case GST_ITERATOR_ERROR:
971 case GST_ITERATOR_RESYNC:
972 gst_iterator_resync (it);
977 gst_iterator_free (it);
983 * performs a link and frees the struct. src and sink elements must be given
984 * return values 0 - link performed
989 gst_parse_perform_link (link_t *link, graph_t *graph)
991 GstElement *src = link->src.element;
992 GstElement *sink = link->sink.element;
993 GSList *srcs = link->src.pads;
994 GSList *sinks = link->sink.pads;
995 g_assert (GST_IS_ELEMENT (src));
996 g_assert (GST_IS_ELEMENT (sink));
998 GST_CAT_INFO (GST_CAT_PIPELINE,
999 "linking " PRETTY_PAD_NAME_FMT " to " PRETTY_PAD_NAME_FMT " (%u/%u) with caps \"%" GST_PTR_FORMAT "\"",
1000 PRETTY_PAD_NAME_ARGS (src, link->src.name),
1001 PRETTY_PAD_NAME_ARGS (sink, link->sink.name),
1002 g_slist_length (srcs), g_slist_length (sinks), link->caps);
1004 if (!srcs || !sinks) {
1005 gboolean found_one = gst_element_link_pads_filtered (src,
1006 srcs ? (const gchar *) srcs->data : NULL, sink,
1007 sinks ? (const gchar *) sinks->data : NULL, link->caps);
1010 if (!link->all_pads)
1011 goto success; /* Linked one, and not an all-pads link = we're done */
1013 /* Try and link more available pads */
1014 while (gst_element_link_pads_filtered (src,
1015 srcs ? (const gchar *) srcs->data : NULL, sink,
1016 sinks ? (const gchar *) sinks->data : NULL, link->caps));
1019 /* We either didn't find any static pads, or this is a all-pads link,
1020 * in which case watch for future pads and link those. Not a failure
1021 * in the all-pads case if there's no sometimes pads to watch */
1022 if (gst_parse_perform_delayed_link (src,
1023 srcs ? (const gchar *) srcs->data : NULL,
1024 sink, sinks ? (const gchar *) sinks->data : NULL, link->caps,
1025 link->all_pads) || link->all_pads) {
1031 if (g_slist_length (link->src.pads) != g_slist_length (link->sink.pads)) {
1034 while (srcs && sinks) {
1035 const gchar *src_pad = (const gchar *) srcs->data;
1036 const gchar *sink_pad = (const gchar *) sinks->data;
1037 srcs = g_slist_next (srcs);
1038 sinks = g_slist_next (sinks);
1039 if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad,
1043 if (gst_parse_perform_delayed_link (src, src_pad,
1045 link->caps, link->all_pads)) {
1054 gst_parse_free_link (link);
1058 if (link->caps != NULL) {
1059 gboolean src_can_do_caps, sink_can_do_caps;
1060 gchar *caps_str = gst_caps_to_string (link->caps);
1063 gst_parse_element_can_do_caps (src, GST_PAD_SRC, link->caps);
1065 gst_parse_element_can_do_caps (sink, GST_PAD_SINK, link->caps);
1067 if (!src_can_do_caps && sink_can_do_caps) {
1068 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1069 _("could not link %s to %s, %s can't handle caps %s"),
1070 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink),
1071 GST_ELEMENT_NAME (src), caps_str);
1072 } else if (src_can_do_caps && !sink_can_do_caps) {
1073 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1074 _("could not link %s to %s, %s can't handle caps %s"),
1075 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink),
1076 GST_ELEMENT_NAME (sink), caps_str);
1077 } else if (!src_can_do_caps && !sink_can_do_caps) {
1078 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1079 _("could not link %s to %s, neither element can handle caps %s"),
1080 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink), caps_str);
1082 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1083 _("could not link %s to %s with caps %s"),
1084 GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink), caps_str);
1088 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
1089 _("could not link %s to %s"), GST_ELEMENT_NAME (src),
1090 GST_ELEMENT_NAME (sink));
1092 gst_parse_free_link (link);
1097 static int yyerror (void *scanner, graph_t *graph, const char *s);
1110 /* No grammar ambiguities expected, FAIL otherwise */
1113 %token <ss> PARSE_URL
1114 %token <ss> IDENTIFIER
1115 %left <ss> REF PADREF BINREF
1116 %token <ss> ASSIGNMENT PRESET
1118 %token <ss> LINK_ALL
1120 %type <ss> binopener
1122 %type <cc> chain bin chainlist openchain elementary
1123 %type <rr> reference
1126 %type <pp> morepads pads assignments
1128 %destructor { gst_parse_strfree ($$); } <ss>
1129 %destructor { if($$)
1130 gst_parse_free_chain($$); } <cc>
1131 %destructor { gst_parse_free_link ($$); } <ll>
1132 %destructor { gst_parse_free_reference(&($$));} <rr>
1133 %destructor { gst_parse_free_element ($$); } <ee>
1134 %destructor { GSList *walk;
1135 for(walk=$$;walk;walk=walk->next)
1136 gst_parse_strfree (walk->data);
1137 g_slist_free ($$); } <pp>
1146 %lex-param { void *scanner }
1147 %parse-param { void *scanner }
1148 %parse-param { graph_t *graph }
1154 /*************************************************************
1155 * Grammar explanation:
1156 * _element_s are specified by an identifier of their type.
1157 * a name can be give in the optional property-assignments
1160 * identity silence=false name=frodo
1162 **************************************************************/
1163 element: IDENTIFIER {
1164 $$ = gst_parse_element_new();
1165 $$->factory_name = $1;
1168 $$->presets = g_slist_append ($$->presets, $2);
1171 | element ASSIGNMENT {
1172 $$->values = g_slist_append ($$->values, $2);
1177 /*************************************************************
1178 * Grammar explanation: (cont'd)
1179 * a graph has (pure) _element_s, _bin_s and _link_s.
1180 * since bins are special elements, bins and elements can
1181 * be generalized as _elementary_.
1182 * The construction of _bin_s will be discussed later.
1185 **************************************************************/
1188 GstElement *element = NULL;
1190 $$ = gst_parse_chain_new ();
1192 if ($1 && !(element = gst_parse_element_make (graph, $1))) {
1193 add_missing_element(graph, $1->factory_name);
1195 /* g_print ("@%p: CHAINing elementary\n", $$); */
1196 $$->first.element = element ? gst_object_ref(element) : NULL;
1197 $$->last.element = element ? gst_object_ref(element) : NULL;
1198 $$->first.name = $$->last.name = NULL;
1199 $$->first.pads = $$->last.pads = NULL;
1200 $$->elements = element ? g_slist_prepend (NULL, element) : NULL;
1203 gst_parse_free_element ($1);
1209 /*************************************************************
1210 * Grammar explanation: (cont'd)
1211 * a _chain_ is a list of _elementary_s that have _link_s in between
1212 * which are represented through infix-notation.
1214 * fakesrc ! sometransformation ! fakesink
1216 * every _link_ can be augmented with _pads_.
1218 * coffeesrc .sound ! speakersink
1219 * multisrc .movie,ads ! .projector,smallscreen multisink
1221 * and every _link_ can be setup to filter media-types
1222 * mediasrc ! audio/x-raw, signed=TRUE ! stereosink
1225 * if the lexer does not recognize your media-type it
1226 * will make it an element name. that results in errors
1228 * NO SUCH ELEMENT: no element audio7x-raw
1229 * '7' vs. '/' in https://en.wikipedia.org/wiki/QWERTZ
1232 * in the parser we need to differ between chains that can
1233 * be extended by more elementaries (_openchain_) and others
1234 * that are syntactically closed (handled later in this file).
1235 * (e.g. fakesrc ! sinkreferencename.padname)
1236 **************************************************************/
1237 chain: openchain { $$=$1;
1239 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
1240 _("unexpected reference \"%s\" - ignoring"), $$->last.name);
1241 gst_parse_strfree($$->last.name);
1245 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
1246 _("unexpected pad-reference \"%s\" - ignoring"), (gchar*)$$->last.pads->data);
1247 g_slist_foreach ($$->last.pads, (GFunc) gst_parse_strfree, NULL);
1248 g_slist_free ($$->last.pads);
1255 elementary pads { $$=$1;
1256 $$->last.pads = g_slist_concat ($$->last.pads, $2);
1257 /* g_print ("@%p@%p: FKI elementary pads\n", $1, $$->last.pads); */
1259 | openchain link pads elementary pads
1262 $2->sink = $4->first;
1263 $2->sink.pads = g_slist_concat ($3, $2->sink.pads);
1265 $4->first = $1->first;
1266 $4->elements = g_slist_concat ($1->elements, $4->elements);
1267 gst_parse_chain_free($1);
1269 $$->last.pads = g_slist_concat ($$->last.pads, $5);
1273 link: LINK { $$ = gst_parse_link_new ();
1274 $$->all_pads = FALSE;
1276 $$->caps = gst_caps_from_string ($1);
1277 if ($$->caps == NULL)
1278 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $1);
1279 gst_parse_strfree ($1);
1282 | LINK_ALL { $$ = gst_parse_link_new ();
1283 $$->all_pads = TRUE;
1285 $$->caps = gst_caps_from_string ($1);
1286 if ($$->caps == NULL)
1287 SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $1);
1288 gst_parse_strfree ($1);
1292 pads: /* NOP */ { $$ = NULL; }
1293 | PADREF morepads { $$ = $2;
1294 $$ = g_slist_prepend ($$, $1);
1297 morepads: /* NOP */ { $$ = NULL; }
1298 | ',' IDENTIFIER morepads { $$ = g_slist_prepend ($3, $2); }
1301 /*************************************************************
1302 * Grammar explanation: (cont'd)
1303 * the first and last elements of a _chain_ can be give
1304 * as URL. This creates special elements that fit the URL.
1306 * fakesrc ! http://fake-sink.org
1307 * http://somesource.org ! fakesink
1308 **************************************************************/
1310 chain: openchain link PARSE_URL { GstElement *element =
1311 gst_element_make_from_uri (GST_URI_SINK, $3, NULL, NULL);
1312 /* FIXME: get and parse error properly */
1314 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1315 _("no sink element for URI \"%s\""), $3);
1318 $2->sink.element = element?gst_object_ref(element):NULL;
1321 $$->last.element = NULL;
1322 $$->last.name = NULL;
1323 $$->last.pads = NULL;
1324 if(element) $$->elements = g_slist_append ($$->elements, element);
1329 PARSE_URL { GstElement *element =
1330 gst_element_make_from_uri (GST_URI_SRC, $1, NULL, NULL);
1331 /* FIXME: get and parse error properly */
1333 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1334 _("no source element for URI \"%s\""), $1);
1336 $$ = gst_parse_chain_new ();
1337 /* g_print ("@%p: CHAINing srcURL\n", $$); */
1338 $$->first.element = NULL;
1339 $$->first.name = NULL;
1340 $$->first.pads = NULL;
1341 $$->last.element = element ? gst_object_ref(element):NULL;
1342 $$->last.name = NULL;
1343 $$->last.pads = NULL;
1344 $$->elements = element ? g_slist_prepend (NULL, element) : NULL;
1350 /*************************************************************
1351 * Grammar explanation: (cont'd)
1352 * the first and last elements of a _chain_ can be linked
1353 * to a named _reference_ (with optional pads).
1355 * fakesrc ! nameOfSinkElement.
1356 * fakesrc ! nameOfSinkElement.Padname
1357 * fakesrc ! nameOfSinkElement.Padname, anotherPad
1358 * nameOfSource.Padname ! fakesink
1359 **************************************************************/
1361 chain: openchain link reference { $$ = $1;
1365 $$->last.element = NULL;
1366 $$->last.name = NULL;
1367 $$->last.pads = NULL;
1373 reference { $$ = gst_parse_chain_new ();
1375 $$->first.element = NULL;
1376 $$->first.name = NULL;
1377 $$->first.pads = NULL;
1378 $$->elements = NULL;
1381 reference: REF morepads {
1382 gchar *padname = $1;
1385 while (*padname != '.') padname++;
1388 if (*padname != '\0')
1389 pads = g_slist_prepend (pads, gst_parse_strdup (padname));
1398 /*************************************************************
1399 * Grammar explanation: (cont'd)
1400 * a _chainlist_ is just a list of _chain_s.
1402 * You can specify _link_s with named
1403 * _reference_ on each side. That
1404 * works already after the explanations above.
1405 * someSourceName.Pad ! someSinkName.
1406 * someSourceName.Pad,anotherPad ! someSinkName.Apad,Bpad
1408 * If a syntax error occurs, the already finished _chain_s
1409 * and _links_ are kept intact.
1410 *************************************************************/
1412 chainlist: /* NOP */ { $$ = NULL; }
1413 | chainlist chain { if ($1){
1414 gst_parse_free_reference(&($1->last));
1415 gst_parse_free_reference(&($2->first));
1416 $2->first = $1->first;
1417 $2->elements = g_slist_concat ($1->elements, $2->elements);
1418 gst_parse_chain_free ($1);
1422 | chainlist error { $$=$1;
1423 GST_CAT_DEBUG (GST_CAT_PIPELINE,"trying to recover from syntax error");
1424 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX, _("syntax error"));
1428 /*************************************************************
1429 * Grammar explanation: (cont'd)
1431 *************************************************************/
1434 assignments: /* NOP */ { $$ = NULL; }
1435 | ASSIGNMENT assignments { $$ = g_slist_prepend ($2, $1); }
1438 binopener: '(' { $$ = gst_parse_strdup("bin"); }
1439 | BINREF { $$ = $1; }
1441 bin: binopener assignments chainlist ')' {
1442 chain_t *chain = $3;
1444 GstBin *bin = (GstBin *) gst_element_factory_make ($1, NULL);
1446 SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY_BIN,
1447 _("specified empty bin \"%s\", not allowed"), $1);
1448 chain = gst_parse_chain_new ();
1449 chain->first.element = chain->last.element = NULL;
1450 chain->first.name = chain->last.name = NULL;
1451 chain->first.pads = chain->last.pads = NULL;
1452 chain->elements = NULL;
1455 add_missing_element(graph, $1);
1456 SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1457 _("no bin \"%s\", unpacking elements"), $1);
1458 /* clear property-list */
1459 g_slist_foreach ($2, (GFunc) gst_parse_strfree, NULL);
1463 for (walk = chain->elements; walk; walk = walk->next )
1464 gst_bin_add (bin, GST_ELEMENT (walk->data));
1465 g_slist_free (chain->elements);
1466 chain->elements = g_slist_prepend (NULL, bin);
1469 /* set the properties now
1470 * HINT: property-list cleared above, if bin==NULL
1472 for (walk = $2; walk; walk = walk->next)
1473 gst_parse_element_set ((gchar *) walk->data,
1474 GST_ELEMENT (bin), graph);
1476 gst_parse_strfree ($1);
1480 /*************************************************************
1481 * Grammar explanation: (cont'd)
1483 *************************************************************/
1485 graph: chainlist { $$ = graph;
1488 SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
1497 yyerror (void *scanner, graph_t *graph, const char *s)
1499 /* FIXME: This should go into the GError somehow, but how? */
1500 GST_WARNING ("Error during parsing: %s", s);
1506 priv_gst_parse_launch (const gchar *str, GError **error, GstParseContext *ctx,
1507 GstParseFlags flags)
1515 g_return_val_if_fail (str != NULL, NULL);
1516 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1524 #ifdef __GST_PARSE_TRACE
1525 GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
1526 __strings = __chains = __links = __elements = 0;
1527 #endif /* __GST_PARSE_TRACE */
1529 /* g_print("Now scanning: %s\n", str); */
1531 dstr = g_strdup (str);
1532 priv_gst_parse_yylex_init (&scanner);
1533 priv_gst_parse_yy_scan_string (dstr, scanner);
1539 if (yyparse (scanner, &g) != 0) {
1540 SET_ERROR (error, GST_PARSE_ERROR_SYNTAX,
1541 "Unrecoverable syntax error while parsing pipeline %s", str);
1543 priv_gst_parse_yylex_destroy (scanner);
1548 priv_gst_parse_yylex_destroy (scanner);
1551 GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links",
1552 g.chain ? g_slist_length (g.chain->elements) : 0,
1553 g_slist_length (g.links));
1555 /* ensure chain is not NULL */
1557 g.chain=gst_parse_chain_new ();
1558 g.chain->elements=NULL;
1559 g.chain->first.element=NULL;
1560 g.chain->first.name=NULL;
1561 g.chain->first.pads=NULL;
1562 g.chain->last.element=NULL;
1563 g.chain->last.name=NULL;
1564 g.chain->last.pads=NULL;
1567 /* ensure elements is not empty */
1568 if(!g.chain->elements){
1569 g.chain->elements= g_slist_prepend (NULL, NULL);
1572 /* put all elements in our bin if necessary */
1573 if(g.chain->elements->next){
1575 if (flags & GST_PARSE_FLAG_PLACE_IN_BIN)
1576 bin = GST_BIN (gst_element_factory_make ("bin", NULL));
1578 bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
1581 for (walk = g.chain->elements; walk; walk = walk->next) {
1582 if (walk->data != NULL)
1583 gst_bin_add (bin, GST_ELEMENT (walk->data));
1585 g_slist_free (g.chain->elements);
1586 g.chain->elements = g_slist_prepend (NULL, bin);
1589 ret = (GstElement *) g.chain->elements->data;
1590 g_slist_free (g.chain->elements);
1591 g.chain->elements=NULL;
1592 gst_parse_free_chain (g.chain);
1596 /* resolve and perform links */
1597 for (walk = g.links; walk; walk = walk->next) {
1598 link_t *l = (link_t *) walk->data;
1600 err=gst_resolve_reference( &(l->src), ret);
1603 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1604 "No src-element named \"%s\" - omitting link", l->src.name);
1606 /* probably a missing element which we've handled already */
1607 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1608 "No src-element found - omitting link");
1610 gst_parse_free_link (l);
1614 err=gst_resolve_reference( &(l->sink), ret);
1617 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1618 "No sink-element named \"%s\" - omitting link", l->src.name);
1620 /* probably a missing element which we've handled already */
1621 SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1622 "No sink-element found - omitting link");
1624 gst_parse_free_link (l);
1627 gst_parse_perform_link (l, &g);
1629 g_slist_free (g.links);
1632 #ifdef __GST_PARSE_TRACE
1633 GST_CAT_DEBUG (GST_CAT_PIPELINE,
1634 "TRACE: %u strings, %u chains, %u links and %u elements left", __strings, __chains,
1635 __links, __elements);
1636 if (__strings || __chains || __links || __elements) {
1637 g_warning ("TRACE: %u strings, %u chains, %u links and %u elements left", __strings,
1638 __chains, __links, __elements);
1640 #endif /* __GST_PARSE_TRACE */
1646 gst_parse_free_chain (g.chain);
1650 g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
1651 g_slist_free (g.links);