/* GStreamer
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
* 2000 Wim Taymans <wtay@chello.be>
+ * 2002 Andy Wingo <wingo@pobox.com>
+ * 2008 Tim-Philipp Müller <tim centricular net>
*
- * :
+ * gstparse.c: get a pipeline from a text pipeline description
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
*/
-/* #define DEBUG(format,args...) g_print (format, ##args) */
-#define DEBUG(format,args...)
-#define DEBUG_NOPREFIX(format,args...)
-#define VERBOSE(format,args...)
-
-#define GST_PARSE_LISTPAD(list) ((GstPad*)(list->data))
+/**
+ * SECTION:gstparse
+ * @title: GstParse
+ * @short_description: Get a pipeline from a text pipeline description
+ *
+ * These function allow to create a pipeline based on the syntax used in the
+ * gst-launch-1.0 utility (see man-page for syntax documentation).
+ *
+ * Please note that these functions take several measures to create
+ * somewhat dynamic pipelines. Due to that such pipelines are not always
+ * reusable (set the state to NULL and back to PLAYING).
+ */
+#include "gst_private.h"
#include <string.h>
-#include "gst_private.h"
#include "gstparse.h"
-#include "gstpipeline.h"
-#include "gstthread.h"
-#include "gstutils.h"
+#include "gsterror.h"
+#include "gstinfo.h"
+#ifndef GST_DISABLE_PARSE
+#include "parse/types.h"
+#endif
-typedef struct _gst_parse_priv gst_parse_priv;
-struct _gst_parse_priv
-{
- guint bincount;
- guint threadcount;
- gint binlevel;
- GHashTable *elementcounts;
- gboolean verbose;
- gboolean debug;
-};
-
-typedef struct _gst_parse_delayed_pad gst_parse_delayed_pad;
-struct _gst_parse_delayed_pad
-{
- gchar *name;
- GstPad *peer;
-};
+G_DEFINE_BOXED_TYPE (GstParseContext, gst_parse_context,
+ (GBoxedCopyFunc) gst_parse_context_copy,
+ (GBoxedFreeFunc) gst_parse_context_free);
-typedef struct
+/**
+ * gst_parse_error_quark:
+ *
+ * Get the error quark used by the parsing subsystem.
+ *
+ * Returns: the quark of the parse errors.
+ */
+GQuark
+gst_parse_error_quark (void)
{
- gchar *srcpadname;
- GstPad *target;
- GstElement *pipeline;
+ static GQuark quark = 0;
+
+ if (!quark)
+ quark = g_quark_from_static_string ("gst_parse_error");
+ return quark;
}
-dyn_connect;
-static void
-dynamic_connect (GstElement * element, GstPad * newpad, gpointer data)
+
+/**
+ * gst_parse_context_new:
+ *
+ * Allocates a parse context for use with gst_parse_launch_full() or
+ * gst_parse_launchv_full().
+ *
+ * Free-function: gst_parse_context_free
+ *
+ * Returns: (transfer full) (nullable): a newly-allocated parse context. Free
+ * with gst_parse_context_free() when no longer needed.
+ */
+GstParseContext *
+gst_parse_context_new (void)
{
- dyn_connect *connect = (dyn_connect *) data;
-
- if (!strcmp (gst_pad_get_name (newpad), connect->srcpadname)) {
- gst_element_set_state (connect->pipeline, GST_STATE_PAUSED);
- if (!gst_pad_connect (newpad, connect->target))
- g_warning ("could not connect %s:%s to %s:%s", GST_DEBUG_PAD_NAME (newpad),
- GST_DEBUG_PAD_NAME (connect->target));
- gst_element_set_state (connect->pipeline, GST_STATE_PLAYING);
- }
+#ifndef GST_DISABLE_PARSE
+ GstParseContext *ctx;
+
+ ctx = g_slice_new (GstParseContext);
+ ctx->missing_elements = NULL;
+
+ return ctx;
+#else
+ return NULL;
+#endif
}
-static gchar *
-gst_parse_unique_name (const gchar * type, gst_parse_priv * priv)
+/**
+ * gst_parse_context_copy:
+ * @context: a #GstParseContext
+ *
+ * Copies the @context.
+ *
+ * Returns: (transfer full) (nullable): A copied #GstParseContext
+ */
+GstParseContext *
+gst_parse_context_copy (const GstParseContext * context)
{
- gpointer tmp;
- gint count;
+ GstParseContext *ret = NULL;
+#ifndef GST_DISABLE_PARSE
+
+ ret = gst_parse_context_new ();
+ if (context) {
+ GQueue missing_copy = G_QUEUE_INIT;
+ GList *l;
- tmp = g_hash_table_lookup (priv->elementcounts, type);
- count = GPOINTER_TO_INT (tmp);
- count++;
- g_hash_table_insert (priv->elementcounts, g_strdup (type), GINT_TO_POINTER (count));
+ for (l = context->missing_elements; l != NULL; l = l->next)
+ g_queue_push_tail (&missing_copy, g_strdup ((const gchar *) l->data));
- return g_strdup_printf ("%s%d", type, count - 1);
+ ret->missing_elements = missing_copy.head;
+ }
+#endif
+ return ret;
}
-static gint
-gst_parse_launchv_recurse (const gchar **argv, GstBin * parent, gst_parse_priv * priv)
+/**
+ * gst_parse_context_free:
+ * @context: (transfer full): a #GstParseContext
+ *
+ * Frees a parse context previously allocated with gst_parse_context_new().
+ */
+void
+gst_parse_context_free (GstParseContext * context)
{
- gint i = -1, j = 0;
- const gchar *arg;
- GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
- gchar closingchar = '\0';
- gint len;
- const gchar *cptr;
- gchar *ptr;
- gchar *sinkpadname = NULL, *srcpadname = NULL, *tempname;
- GstPad *temppad;
- GSList *sinkpads = NULL, *srcpads = NULL;
- gint numsrcpads = 0, numsinkpads = 0;
- GList *pads;
- gint elementcount = 0;
- gint retval = 0;
- gboolean backref = FALSE;
-
- if (!priv) {
- priv = g_new0 (gst_parse_priv, 1);
- priv->elementcounts = g_hash_table_new (g_str_hash, g_str_equal);
+#ifndef GST_DISABLE_PARSE
+ if (context) {
+ g_list_foreach (context->missing_elements, (GFunc) g_free, NULL);
+ g_list_free (context->missing_elements);
+ g_slice_free (GstParseContext, context);
}
+#endif
+}
- priv->binlevel++;
-
- if (GST_IS_PIPELINE (parent)) {
- closingchar = '\0';
- DEBUG ("in pipeline ");
- } else if (GST_IS_THREAD (parent)) {
- closingchar = '}';
- DEBUG ("in thread ");
- } else {
- closingchar = ')';
- DEBUG ("in bin ");
- }
- DEBUG_NOPREFIX ("%s\n", GST_ELEMENT_NAME (GST_ELEMENT (parent)));
-
- while ((arg = argv[++i])) {
- len = strlen (arg);
- element = NULL;
- DEBUG ("** ARGUMENT is '%s'\n", arg);
-
- if (len == 0) {
- DEBUG ("random arg, FIXME\n");
-
- continue;
- } else if (arg[0] == closingchar) {
- DEBUG ("exiting container %s\n", GST_ELEMENT_NAME (GST_ELEMENT (parent)));
-
- retval = i + 1;
- break;
- } else if ((cptr = strchr (arg, '!'))) {
- DEBUG ("attempting to connect pads together....\n");
-
- /* if it starts with the ! */
- if (arg[0] == '!') {
- srcpadname = NULL;
- /* if there's a sinkpad... */
- if (len > 1)
- sinkpadname = g_strdup (&arg[1]);
- else
- sinkpadname = NULL;
- } else {
- srcpadname = g_strndup (arg, (cptr - arg));
- /* if there's a sinkpad */
- if (len > (cptr - arg) + 1)
- sinkpadname = g_strdup(&cptr[1]);
- else
- sinkpadname = NULL;
- }
-
- if (srcpadname && (ptr = strchr (srcpadname, '.'))) {
- gchar *element_name = srcpadname;
- GstElement *new;
-
- *ptr = '\0'; /* it was a '.' before */
-
- GST_DEBUG (0, "have pad for element %s\n", element_name);
- new = gst_bin_get_by_name_recurse_up (parent, element_name);
- if (!new) {
- GST_DEBUG (0, "element %s does not exist! trying to continue\n", element_name);
- } else {
- previous = new;
- srcpadname = ptr + 1;
- backref = TRUE;
- }
- }
-
- GST_DEBUG (0, "have srcpad %s, sinkpad %s\n", srcpadname, sinkpadname);
-
- g_slist_free (srcpads);
- srcpads = NULL;
- numsrcpads = 0;
- tempname = NULL;
-
- /* find src pads */
- if (srcpadname != NULL) {
- while (1) {
- /* split name at commas */
- if ((ptr = strchr (srcpadname, ','))) {
- tempname = srcpadname;
- srcpadname = &ptr[1];
- *ptr = '\0';
- } else {
- tempname = srcpadname;
- }
-
- /* look for pad with that name */
- if ((temppad = gst_element_get_pad (previous, tempname))) {
- srcpads = g_slist_append (srcpads, temppad);
- numsrcpads++;
- }
-
- /* try to create a pad using that padtemplate name */
- else if ((temppad = gst_element_request_pad_by_name (previous, tempname))) {
- srcpads = g_slist_append (srcpads, temppad);
- numsrcpads++;
- }
- if (!temppad) {
- GST_DEBUG (0, "NO SUCH pad %s in element %s\n", tempname, GST_ELEMENT_NAME (previous));
- } else {
- GST_DEBUG (0, "have src pad %s:%s\n", GST_DEBUG_PAD_NAME (temppad));
- }
-
- /* if there is no more commas in srcpadname then we're done */
- if (tempname == srcpadname)
- break;
- g_free (tempname);
- }
- } else {
- /* check through the list to find the first src pad */
- GST_DEBUG (0, "CHECKING element %s for pad named %s\n", GST_ELEMENT_NAME (previous),
- srcpadname);
- pads = gst_element_get_pad_list (previous);
- while (pads) {
- temppad = GST_PARSE_LISTPAD (pads);
- GST_DEBUG (0, "have pad %s:%s\n", GST_DEBUG_PAD_NAME (temppad));
- if (GST_IS_GHOST_PAD (temppad))
- GST_DEBUG (0, "it's a ghost pad\n");
- if (gst_pad_get_direction (temppad) == GST_PAD_SRC) {
- srcpads = g_slist_append (srcpads, temppad);
- numsrcpads++;
- break;
- }
- pads = g_list_next (pads);
- }
- if (!srcpads)
- GST_DEBUG (0, "error, can't find a src pad!!!\n");
- else
- GST_DEBUG (0, "have src pad %s:%s\n", GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (srcpads)));
- }
- } else if (strchr (arg, '=')) {
- gchar *propname;
- gchar *propval;
- gchar *pos;
-
- DEBUG ("have a property\n");
-
- /* we have a property */
- propname = g_strdup (arg);
- pos = strchr (propname, '=');
- *pos = '\0';
- propval = pos + 1;
-
- /* use g_object_set in the future when gst_util_{set|get} go away */
- GST_DEBUG (0, "attempting to set property '%s' to '%s' on element '%s'\n",
- propname, propval, GST_ELEMENT_NAME (previous));
- gst_util_set_object_arg (G_OBJECT (previous), propname, propval);
- g_free (propname);
- } else if (arg[0] == '[') {
- DEBUG ("have element name\n");
-
- if (arg[1] != '\0') {
- fprintf (stderr, "error, unexpected junk after [\n");
- return GST_PARSE_ERROR_SYNTAX;
- }
-
- if ((arg = argv[++i])) {
- if (!previous) {
- g_critical("parser error, please report");
- return GST_PARSE_ERROR_INTERNAL;
- }
- gst_element_set_name (previous, arg);
- } else {
- fprintf (stderr, "error, expected element name, found end of arguments\n");
- return GST_PARSE_ERROR_SYNTAX;
- }
-
- if ((arg = argv[++i])) {
- if (strcmp (arg, "]") != 0) {
- fprintf (stderr, "error, expected ], found '%s'\n", arg);
- return GST_PARSE_ERROR_SYNTAX;
- }
- } else {
- fprintf (stderr, "error, expected ], found end of arguments\n");
- return GST_PARSE_ERROR_SYNTAX;
- }
- } else {
- /* we have an element, a bin, or a thread. we treat these together because
- * once we rech them we have to resolve any dangling connections from
- * previous array arguments. */
-
- if (strchr ("({", arg[0])) {
- DEBUG ("have bin or thread\n");
-
- if (arg[0] == '(') {
- /* create a bin and add it to the current parent */
- element = gst_bin_new (g_strdup_printf ("bin%d", priv->bincount++));
- if (!element) {
- fprintf (stderr, "Couldn't create a bin!\n");
- return GST_PARSE_ERROR_CREATING_ELEMENT;
- }
- GST_DEBUG (0, "CREATED bin %s\n", GST_ELEMENT_NAME (element));
- } else if (arg[0] == '{') {
- /* create a thread and add it to the current parent */
- element = gst_thread_new (g_strdup_printf ("thread%d", priv->threadcount++));
- if (!element) {
- fprintf (stderr, "Couldn't create a thread!\n");
- return GST_PARSE_ERROR_CREATING_ELEMENT;
- }
- GST_DEBUG (0, "CREATED thread %s\n", GST_ELEMENT_NAME (element));
- } else {
- fprintf (stderr, "Illegal argument: %s\n", arg);
- return GST_PARSE_ERROR_CREATING_ELEMENT;
- }
-
- gst_bin_add (GST_BIN (parent), element);
-
- j = gst_parse_launchv_recurse (argv + i + 1, GST_BIN (element), priv);
- /* check for parse error */
- if (j < 0)
- return j;
-
- /* we will get autoincremented at the while */
- i += j;
-
- } else {
- /* we have an element */
- DEBUG ("attempting to create element '%s'\n", arg);
-
- ptr = gst_parse_unique_name (arg, priv);
- element = gst_elementfactory_make (arg, ptr);
- g_free (ptr);
- if (!element) {
-#ifndef GST_DISABLE_REGISTRY
- fprintf (stderr,
- "Couldn't create a '%s', no such element or need to run gst-register?\n", arg);
+/**
+ * gst_parse_context_get_missing_elements:
+ * @context: a #GstParseContext
+ *
+ * Retrieve missing elements from a previous run of gst_parse_launch_full()
+ * or gst_parse_launchv_full(). Will only return results if an error code
+ * of %GST_PARSE_ERROR_NO_SUCH_ELEMENT was returned.
+ *
+ * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*) (nullable): a
+ * %NULL-terminated array of element factory name strings of missing
+ * elements. Free with g_strfreev() when no longer needed.
+ */
+gchar **
+gst_parse_context_get_missing_elements (GstParseContext * context)
+{
+#ifndef GST_DISABLE_PARSE
+ gchar **arr;
+ GList *l;
+ guint len, i;
+
+ g_return_val_if_fail (context != NULL, NULL);
+
+ len = g_list_length (context->missing_elements);
+
+ if (G_UNLIKELY (len == 0))
+ return NULL;
+
+ arr = g_new (gchar *, len + 1);
+
+ for (i = 0, l = context->missing_elements; l != NULL; l = l->next, ++i)
+ arr[i] = g_strdup (l->data);
+
+ arr[i] = NULL;
+
+ return arr;
#else
- fprintf (stderr, "Couldn't create a '%s', no such element or need to load plugin?\n",
- arg);
+ return NULL;
#endif
- return GST_PARSE_ERROR_NOSUCH_ELEMENT;
- }
-
- GST_DEBUG (0, "CREATED element %s\n", GST_ELEMENT_NAME (element));
- gst_bin_add (GST_BIN (parent), element);
- }
-
- elementcount++;
-
- g_slist_free (sinkpads);
- sinkpads = NULL;
- numsinkpads = 0;
- tempname = NULL;
-
- /* find sink pads */
- if (sinkpadname != NULL) {
- while (1) {
- /* split name at commas */
- if ((ptr = strchr (sinkpadname, ','))) {
- tempname = g_strndup (sinkpadname, (ptr - sinkpadname));
- sinkpadname = &ptr[1];
- } else {
- tempname = sinkpadname;
- }
-
- /* look for pad with that name */
- if ((temppad = gst_element_get_pad (element, tempname))) {
- sinkpads = g_slist_append (sinkpads, temppad);
- numsinkpads++;
- }
-
- /* try to create a pad using that padtemplate name */
- else if ((temppad = gst_element_request_pad_by_name (element, tempname))) {
- sinkpads = g_slist_append (sinkpads, temppad);
- numsinkpads++;
- }
- if (!temppad) {
- GST_DEBUG (0, "NO SUCH pad %s in element %s\n", tempname, GST_ELEMENT_NAME (element));
- } else {
- GST_DEBUG (0, "have sink pad %s:%s\n", GST_DEBUG_PAD_NAME (temppad));
- }
-
- /* if there is no more commas in sinkpadname then we're done */
- if (tempname == sinkpadname)
- break;
- g_free (tempname);
- }
- } else {
- /* check through the list to find the first sink pad */
- pads = gst_element_get_pad_list (element);
- while (pads) {
- temppad = GST_PAD (pads->data);
- pads = g_list_next (pads);
- if (gst_pad_get_direction (temppad) == GST_PAD_SINK) {
- sinkpads = g_slist_append (sinkpads, temppad);
- numsinkpads++;
- break;
- }
- }
- }
-
- if (!sinkpads)
- GST_DEBUG (0, "can't find a sink pad for element\n");
- else
- GST_DEBUG (0, "have sink pad %s:%s\n", GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (sinkpads)));
-
- if (!srcpads && sinkpads && previous && srcpadname) {
- dyn_connect *connect = g_malloc (sizeof (dyn_connect));
-
- connect->srcpadname = srcpadname;
- connect->target = GST_PARSE_LISTPAD (sinkpads);
- connect->pipeline = GST_ELEMENT (parent);
-
- GST_DEBUG (0, "SETTING UP dynamic connection %s:%s and %s:%s\n",
- gst_element_get_name (previous),
- srcpadname, GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (sinkpads)));
-
- g_signal_connect (G_OBJECT (previous), "new_pad", G_CALLBACK (dynamic_connect), connect);
- } else {
- for (j = 0; (j < numsrcpads) && (j < numsinkpads); j++) {
- GST_DEBUG (0, "CONNECTING %s:%s and %s:%s\n",
- GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (g_slist_nth (srcpads, j))),
- GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (g_slist_nth (sinkpads, j))));
- if (!gst_pad_connect (GST_PARSE_LISTPAD (g_slist_nth (srcpads, j)),
- GST_PARSE_LISTPAD (g_slist_nth (sinkpads, j)))) {
- g_warning ("could not connect %s:%s to %s:%s",
- GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (g_slist_nth (srcpads, j))),
- GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (g_slist_nth (sinkpads, j))));
- return GST_PARSE_ERROR_CONNECT;
- }
- }
- }
-
- g_slist_free (srcpads);
- srcpads = NULL;
-
- g_slist_free (sinkpads);
- sinkpads = NULL;
-
- /* if we're the first element, ghost all the sinkpads */
- if (elementcount == 1 && !backref) {
- DEBUG ("first element, ghosting all of %s's sink pads to parent %s\n",
- GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (GST_ELEMENT (parent)));
- pads = gst_element_get_pad_list (element);
- while (pads) {
- temppad = GST_PAD (pads->data);
- pads = g_list_next (pads);
- if (!temppad)
- DEBUG ("much oddness, pad doesn't seem to exist\n");
- else if (gst_pad_get_direction (temppad) == GST_PAD_SINK) {
- gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
- g_strdup_printf ("%s-ghost", GST_PAD_NAME (temppad)));
- GST_DEBUG (0, "GHOSTED %s:%s to %s as %s-ghost\n",
- GST_DEBUG_PAD_NAME (temppad), GST_ELEMENT_NAME (GST_ELEMENT (parent)),
- GST_PAD_NAME (temppad));
- }
- }
- }
-
- previous = element;
- if (!GST_IS_BIN (element))
- prevelement = element;
- }
- }
-
- /* ghost all the src pads of the bin */
- if (prevelement != NULL) {
- DEBUG ("last element, ghosting all of %s's src pads to parent %s\n",
- GST_ELEMENT_NAME (prevelement), GST_ELEMENT_NAME (GST_ELEMENT (parent)));
- pads = gst_element_get_pad_list (prevelement);
- while (pads) {
- temppad = GST_PAD (pads->data);
- pads = g_list_next (pads);
- if (!temppad)
- DEBUG ("much oddness, pad doesn't seem to exist\n");
- else if (gst_pad_get_direction (temppad) == GST_PAD_SRC) {
- gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
- g_strdup_printf ("%s-ghost", GST_PAD_NAME (temppad)));
- GST_DEBUG (0, "GHOSTED %s:%s to %s as %s-ghost\n",
- GST_DEBUG_PAD_NAME (temppad), GST_ELEMENT_NAME (parent), GST_PAD_NAME (temppad));
- }
- }
- }
-
- priv->binlevel--;
-
- if (retval)
- return retval;
-
- DEBUG (closingchar != '\0' ? "returning IN THE WRONG PLACE\n" : "ending pipeline\n");
-
- return i + 1;
}
+#ifndef GST_DISABLE_PARSE
+static gchar *
+_gst_parse_escape (const gchar * str)
+{
+ GString *gstr = NULL;
+ gboolean in_quotes;
+
+ g_return_val_if_fail (str != NULL, NULL);
+
+ gstr = g_string_sized_new (strlen (str));
+
+ in_quotes = FALSE;
+
+ while (*str) {
+ if (*str == '"' && (!in_quotes || *(str - 1) != '\\'))
+ in_quotes = !in_quotes;
+
+ if (*str == ' ' && !in_quotes)
+ g_string_append_c (gstr, '\\');
+
+ g_string_append_c (gstr, *str);
+ str++;
+ }
+
+ return g_string_free (gstr, FALSE);
+}
+#endif /* !GST_DISABLE_PARSE */
/**
* gst_parse_launchv:
- * @argv: null-terminated array of arguments
+ * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
+ * @error: pointer to a #GError
*
- * Create a new pipeline based on command line syntax.
+ * Create a new element based on command line syntax.
+ * @error will contain an error message if an erroneous pipeline is specified.
+ * An error does not mean that the pipeline could not be constructed.
*
- * Returns: a new pipeline on success, NULL on failure
+ * Returns: (transfer floating) (nullable): a new element on success and %NULL
+ * on failure.
*/
-GstPipeline *
-gst_parse_launchv (const gchar **argv)
+GstElement *
+gst_parse_launchv (const gchar ** argv, GError ** error)
{
- GstPipeline *pipeline;
- gint ret;
+ return gst_parse_launchv_full (argv, NULL, GST_PARSE_FLAG_NONE, error);
+}
- /* defer error detection to the _recurse function */
+/**
+ * gst_parse_launchv_full:
+ * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
+ * @context: (allow-none): a parse context allocated with
+ * gst_parse_context_new(), or %NULL
+ * @flags: parsing options, or #GST_PARSE_FLAG_NONE
+ * @error: pointer to a #GError (which must be initialised to %NULL)
+ *
+ * Create a new element based on command line syntax.
+ * @error will contain an error message if an erroneous pipeline is specified.
+ * An error does not mean that the pipeline could not be constructed.
+ *
+ * Returns: (transfer floating) (nullable): a new element on success; on
+ * failure, either %NULL or a partially-constructed bin or element will be
+ * returned and @error will be set (unless you passed
+ * #GST_PARSE_FLAG_FATAL_ERRORS in @flags, then %NULL will always be returned
+ * on failure)
+ */
+GstElement *
+gst_parse_launchv_full (const gchar ** argv, GstParseContext * context,
+ GstParseFlags flags, GError ** error)
+{
+#ifndef GST_DISABLE_PARSE
+ GstElement *element;
+ GString *str;
+ const gchar **argvp, *arg;
+ gchar *tmp;
+
+ g_return_val_if_fail (argv != NULL, NULL);
+ g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+ /* let's give it a nice size. */
+ str = g_string_sized_new (1024);
+
+ argvp = argv;
+ while (*argvp) {
+ arg = *argvp;
+ GST_DEBUG ("escaping argument %s", arg);
+ tmp = _gst_parse_escape (arg);
+ g_string_append (str, tmp);
+ g_free (tmp);
+ g_string_append_c (str, ' ');
+ argvp++;
+ }
- pipeline = (GstPipeline*) gst_pipeline_new ("launch");
+ element = gst_parse_launch_full (str->str, context, flags, error);
- ret = gst_parse_launchv_recurse (argv, GST_BIN (pipeline), NULL);
+ g_string_free (str, TRUE);
- if (ret <= 0) {
- // print an error
- gst_object_unref (GST_OBJECT (pipeline));
- return NULL;
- } else {
- return pipeline;
- }
+ return element;
+#else
+ /* gst_parse_launch_full() will set a GST_CORE_ERROR_DISABLED error for us */
+ return gst_parse_launch_full ("", NULL, 0, error);
+#endif
}
/**
* gst_parse_launch:
* @pipeline_description: the command line describing the pipeline
+ * @error: the error message in case of an erroneous pipeline.
*
* Create a new pipeline based on command line syntax.
+ * Please note that you might get a return value that is not %NULL even though
+ * the @error is set. In this case there was a recoverable parsing error and you
+ * can try to play the pipeline.
*
- * Returns: a new GstPipeline (cast to a Bin) on success, NULL on failure
+ * Returns: (transfer floating) (nullable): a new element on success, %NULL on
+ * failure. If more than one toplevel element is specified by the
+ * @pipeline_description, all elements are put into a #GstPipeline, which
+ * than is returned.
*/
-GstPipeline *
-gst_parse_launch (const gchar * pipeline_description)
+GstElement *
+gst_parse_launch (const gchar * pipeline_description, GError ** error)
{
- gchar **argvn;
- gint newargc;
- gint i;
- const gchar *cp, *start, *end;
- gchar *temp;
- GSList *string_list = NULL, *slist;
- GstPipeline *pipeline;
-
- end = pipeline_description + strlen (pipeline_description);
- newargc = 0;
-
- temp = "";
-
- /* Extract the arguments to a gslist in reverse order */
- for (cp = pipeline_description; cp < end;) {
- i = strcspn (cp, "([{}]) \"\\");
-
- if (i > 0) {
- temp = g_strconcat (temp, g_strndup (cp, i), NULL);
-
- /* see if we have an escape char */
- if (cp[i] != '\\') {
- /* normal argument - copy and add to the list */
- string_list = g_slist_prepend (string_list, temp);
- newargc++;
- temp = "";
- } else {
- temp = g_strconcat (temp, g_strndup (&cp[++i], 1), NULL);
- }
- cp += i;
- }
-
- /* skip spaces */
- while (cp < end && *cp == ' ') {
- cp++;
- }
-
- /* handle quoted arguments */
- if (*cp == '"') {
- start = ++cp;
-
- /* find matching quote */
- while (cp < end && *cp != '"')
- cp++;
-
- /* make sure we got it */
- if (cp == end) {
- g_warning ("gst_parse_launch: Unbalanced quote in command line");
- /* FIXME: The list leaks here */
- return 0;
- }
-
- /* copy the string sans quotes */
- string_list = g_slist_prepend (string_list, g_strndup (start, cp - start));
- newargc++;
- cp += 2; /* skip the quote aswell */
- }
+ return gst_parse_launch_full (pipeline_description, NULL, GST_PARSE_FLAG_NONE,
+ error);
+}
- /* brackets exist in a separate argument slot */
- if (*cp && strchr ("([{}])", *cp)) {
- string_list = g_slist_prepend (string_list, g_strndup (cp, 1));
- newargc++;
- cp++;
- }
- }
+/**
+ * gst_parse_launch_full:
+ * @pipeline_description: the command line describing the pipeline
+ * @context: (allow-none): a parse context allocated with
+ * gst_parse_context_new(), or %NULL
+ * @flags: parsing options, or #GST_PARSE_FLAG_NONE
+ * @error: the error message in case of an erroneous pipeline.
+ *
+ * Create a new pipeline based on command line syntax.
+ * Please note that you might get a return value that is not %NULL even though
+ * the @error is set. In this case there was a recoverable parsing error and you
+ * can try to play the pipeline.
+ *
+ * Returns: (transfer floating) (nullable): a new element on success, %NULL on
+ * failure. If more than one toplevel element is specified by the
+ * @pipeline_description, all elements are put into a #GstPipeline, which
+ * then is returned (unless the GST_PARSE_FLAG_PLACE_IN_BIN flag is set, in
+ * which case they are put in a #GstBin instead).
+ */
+GstElement *
+gst_parse_launch_full (const gchar * pipeline_description,
+ GstParseContext * context, GstParseFlags flags, GError ** error)
+{
+#ifndef GST_DISABLE_PARSE
+ GstElement *element;
+ GError *myerror = NULL;
- /* now allocate the new argv array, with room for NULL termination */
- argvn = g_new0 (char *, newargc + 1);
+ g_return_val_if_fail (pipeline_description != NULL, NULL);
+ g_return_val_if_fail (error == NULL || *error == NULL, NULL);
- GST_DEBUG (0, "got %d args\n", newargc);
+ GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description '%s'",
+ pipeline_description);
- /* reverse the list and put the strings in the new array */
- i = newargc;
+ element = priv_gst_parse_launch (pipeline_description, &myerror, context,
+ flags);
- for (slist = string_list; slist; slist = slist->next)
- argvn[--i] = slist->data;
+ /* don't return partially constructed pipeline if FATAL_ERRORS was given */
+ if (G_UNLIKELY (myerror != NULL && element != NULL)) {
+ if ((flags & GST_PARSE_FLAG_FATAL_ERRORS)) {
+ gst_object_unref (element);
+ element = NULL;
+ }
+ }
- g_slist_free (string_list);
+ if (myerror)
+ g_propagate_error (error, myerror);
- /* print them out */
- while (argvn[i]) {
- GST_DEBUG (0, "arg %d is: %s\n", i, argvn[i++]);
- }
+ return element;
+#else
+ gchar *msg;
- /* do it! */
- pipeline = gst_parse_launchv ((const char**) argvn);
+ GST_WARNING ("Disabled API called");
- GST_DEBUG(0, "Finished - freeing temporary argument array");
- g_strfreev(argvn);
+ msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
+ g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
+ g_free (msg);
- return pipeline;
+ return NULL;
+#endif
}