/* 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...)
-#define DEBUG_NOPREFIX(format,args...)
-#define VERBOSE(format,args...)
+/**
+ * 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"
-
-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;
-};
-
-/* FIXME need to either revive this, or have pad->padtemplate connections in core
-static void
-gst_parse_newpad(GstElement *element,GstPad *pad,launch_delayed_pad *peer)
+#include "gsterror.h"
+#include "gstinfo.h"
+#ifndef GST_DISABLE_PARSE
+#include "parse/types.h"
+#endif
+
+G_DEFINE_BOXED_TYPE (GstParseContext, gst_parse_context,
+ (GBoxedCopyFunc) gst_parse_context_copy,
+ (GBoxedFreeFunc) gst_parse_context_free);
+
+/**
+ * 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)
{
- gst_info("have NEW_PAD signal\n");
- // if it matches, connect it
- if (!strcmp(GST_PAD_NAME(pad),peer->name)) {
- gst_pad_connect(pad,peer->peer);
- gst_info("delayed connect of '%s' to '%s'\n",
- GST_PAD_NAME(pad),GST_PAD_NAME(peer->peer));
- }
+ static GQuark quark = 0;
+
+ if (!quark)
+ quark = g_quark_from_static_string ("gst_parse_error");
+ return quark;
}
-*/
-static gchar *
-gst_parse_unique_name(gchar *type,gst_parse_priv *priv)
+
+/**
+ * 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)
{
- gint count;
+#ifndef GST_DISABLE_PARSE
+ GstParseContext *ctx;
- count = GPOINTER_TO_INT(g_hash_table_lookup(priv->elementcounts,type));
- count++;
- g_hash_table_insert(priv->elementcounts,type,GINT_TO_POINTER(count));
+ ctx = g_slice_new (GstParseContext);
+ ctx->missing_elements = NULL;
- return g_strdup_printf("%s%d",type,count-1);
+ return ctx;
+#else
+ return NULL;
+#endif
}
+/**
+ * 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)
+{
+ GstParseContext *ret = NULL;
+#ifndef GST_DISABLE_PARSE
+ ret = gst_parse_context_new ();
+ if (context) {
+ GQueue missing_copy = G_QUEUE_INIT;
+ GList *l;
-static gint
-gst_parse_launch_cmdline(int argc,char *argv[],GstBin *parent,gst_parse_priv *priv)
+ for (l = context->missing_elements; l != NULL; l = l->next)
+ g_queue_push_tail (&missing_copy, g_strdup ((const gchar *) l->data));
+
+ ret->missing_elements = missing_copy.head;
+ }
+#endif
+ return ret;
+}
+
+/**
+ * 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 = 0;
- gchar *arg;
- GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
- gchar closingchar = '\0';
- gint len;
- gchar *ptr;
- gchar *sinkpadname = NULL, *srcpadname = NULL;
- GstPad *sinkpad = NULL, *srcpad = NULL;
- GList *pads;
- gint elementcount = 0;
- gint retval = 0;
-
- 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 (i < argc) {
- arg = argv[i];
- // FIXME this is a lame solution for problems with the first parser
- if (arg == NULL) { i++;continue; }
- len = strlen(arg);
- element = NULL;
- DEBUG("** ARGUMENT is '%s'\n",arg);
-
- // a null that slipped through the reconstruction
- if (len == 0) {
- DEBUG("random arg, FIXME\n");
- i++;
- continue;
-
- // end of the container
- } else if (arg[0] == closingchar) {
- // time to finish off this bin
- DEBUG("exiting container %s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
- retval = i+1;
- break;
-
- // a pad connection
- } else if ((ptr = 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 = &arg[1];
- else
- sinkpadname = NULL;
- } else {
- srcpadname = g_strndup(arg,(ptr-arg));
- // if there's a sinkpad
- if (len > (ptr-arg)+1)
- sinkpadname = &ptr[1];
- else
- sinkpadname = NULL;
- }
-
- GST_DEBUG(0,"have srcpad %s, sinkpad %s\n",srcpadname,sinkpadname);
-
- srcpad = NULL;
-
- // if the srcpadname doesn't have any commas in it, find an actual pad
- if (!srcpadname || !strchr(srcpadname,',')) {
- if (srcpadname != NULL) {
- srcpad = gst_element_get_pad(previous,srcpadname);
- if (!srcpad)
- GST_DEBUG(0,"NO SUCH pad %s in element %s\n",srcpadname,GST_ELEMENT_NAME(previous));
- }
-
- if (srcpad == NULL) {
- // check through the list to find the first sink pad
- GST_DEBUG(0,"CHECKING through element %s for pad named %s\n",GST_ELEMENT_NAME(previous),srcpadname);
- pads = gst_element_get_pad_list(previous);
- while (pads) {
- srcpad = GST_PAD(pads->data);
-GST_DEBUG(0,"have pad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
-if (GST_IS_GHOST_PAD(srcpad)) GST_DEBUG(0,"it's a ghost pad\n");
- pads = g_list_next (pads);
- if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) break;
- srcpad = NULL;
- }
- }
-
- if (!srcpad) 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(srcpad));
- }
-
- // argument with = in it
- } else if (strstr(arg, "=")) {
- gchar * argname;
- gchar * argval;
- gchar * pos = strstr(arg, "=");
- // we have an argument
- argname = arg;
- pos[0] = '\0';
- argval = pos+1;
- DEBUG("attempting to set argument '%s' to '%s' on element '%s'\n",
- argname,argval,GST_ELEMENT_NAME(previous));
- gtk_object_set(GTK_OBJECT(previous),argname,argval,NULL);
- g_free(argname);
-
- // element or argument, or beginning of bin or thread
- } else {
- DEBUG("have element or bin/thread\n");
- // if we have a bin or thread starting
- if (strchr("({",arg[0])) {
- 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");
-// exit(-1);
- }
- 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");
-// exit(-1);
- }
- GST_DEBUG(0,"CREATED thread %s\n",GST_ELEMENT_NAME(element));
- }
-
- i += gst_parse_launch_cmdline(argc - i, argv + i + 1, GST_BIN (element), priv);
-
- } 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) {
- fprintf(stderr,"Couldn't create a '%s', no such element or need to run gstraemer-register?\n",arg);
-// exit(-1);
- }
- GST_DEBUG(0,"CREATED element %s\n",GST_ELEMENT_NAME(element));
- }
-
- gst_bin_add (GST_BIN (parent), element);
- elementcount++;
-
- if (srcpad != NULL) {
- DEBUG("need to connect to sinkpad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
-
- sinkpad = NULL;
-
- if (sinkpadname != NULL)
- sinkpad = gst_element_get_pad(previous,sinkpadname);
-
- if (!sinkpad) {
- // check through the list to find the first sink pad
- pads = gst_element_get_pad_list(element);
- while (pads) {
- sinkpad = GST_PAD(pads->data);
- pads = g_list_next (pads);
- if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) break;
- sinkpad = NULL;
- }
- }
-
- if (!sinkpad) DEBUG("error, can't find a sink pad!!!\n");
- else DEBUG("have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(sinkpad));
-
- GST_DEBUG(0,"CONNECTING %s:%s and %s:%s\n",GST_DEBUG_PAD_NAME(srcpad),GST_DEBUG_PAD_NAME(sinkpad));
- gst_pad_connect(srcpad,sinkpad);
-
- sinkpad = NULL;
- srcpad = NULL;
- }
-
- // if we're the first element, ghost all the sinkpads
- if (elementcount == 1) {
- 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) {
- sinkpad = GST_PAD (pads->data);
- pads = g_list_next (pads);
- if (!sinkpad) DEBUG("much oddness, pad doesn't seem to exist\n");
- else if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) {
- gst_element_add_ghost_pad (GST_ELEMENT (parent), sinkpad,
-g_strdup_printf("%s-ghost",GST_PAD_NAME(sinkpad)));
- GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
- GST_DEBUG_PAD_NAME(sinkpad),GST_ELEMENT_NAME(GST_ELEMENT(parent)),GST_PAD_NAME(sinkpad));
- }
- }
- }
-
- previous = element;
- if (!GST_IS_BIN(element)) prevelement = element;
- }
+#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
+}
+
+/**
+ * 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;
- i++;
+ 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
+ return NULL;
+#endif
+}
+
+#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++;
}
- // 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) {
- srcpad = GST_PAD (pads->data);
- pads = g_list_next (pads);
- if (!srcpad) DEBUG("much oddness, pad doesn't seem to exist\n");
- else if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) {
- gst_element_add_ghost_pad (GST_ELEMENT (parent), srcpad,
-g_strdup_printf("%s-ghost",GST_PAD_NAME(srcpad)));
- GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
-GST_DEBUG_PAD_NAME(srcpad),GST_ELEMENT_NAME (parent),GST_PAD_NAME(srcpad));
- }
- }
+ return g_string_free (gstr, FALSE);
+}
+#endif /* !GST_DISABLE_PARSE */
+
+/**
+ * gst_parse_launchv:
+ * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
+ * @error: pointer to a #GError
+ *
+ * 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 and %NULL
+ * on failure.
+ */
+GstElement *
+gst_parse_launchv (const gchar ** argv, GError ** error)
+{
+ return gst_parse_launchv_full (argv, NULL, GST_PARSE_FLAG_NONE, error);
+}
+
+/**
+ * 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++;
}
- priv->binlevel--;
+ element = gst_parse_launch_full (str->str, context, flags, error);
- if (retval) return retval;
+ g_string_free (str, TRUE);
- if (closingchar != '\0')
- DEBUG("returning IN THE WRONG PLACE\n");
- else DEBUG("ending pipeline\n");
- return i+1;
+ 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:
- * @cmdline: the command line describing the pipeline
- * @parent: the parent bin for the resulting pipeline
+ * @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: ?
+ * 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.
*/
-gint
-gst_parse_launch(const gchar *cmdline,GstBin *parent)
+GstElement *
+gst_parse_launch (const gchar * pipeline_description, GError ** error)
{
- gst_parse_priv priv;
- gchar **argvn;
- gint newargc;
- gint len;
- int i,j,k;
-
- priv.bincount = 0;
- priv.threadcount = 0;
- priv.binlevel = 0;
- priv.elementcounts = NULL;
- priv.verbose = FALSE;
- priv.debug = FALSE;
-
- // first walk through quickly and see how many more slots we need
- len = strlen(cmdline);
- newargc = 1;
- for (i=0;i<len;i++) {
- // if it's a space, it denotes a new arg
- if (cmdline[i] == ' ') newargc++;
- // if it's a brace and isn't followed by a space, give it an arg
- if (strchr("([{}])",cmdline[i])) {
- // not followed by space, gets one
- if (cmdline[i+1] != ' ') newargc++;
- }
- }
+ return gst_parse_launch_full (pipeline_description, NULL, GST_PARSE_FLAG_NONE,
+ error);
+}
+
+/**
+ * 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;
+
+ g_return_val_if_fail (pipeline_description != NULL, NULL);
+ g_return_val_if_fail (error == NULL || *error == NULL, NULL);
- // now allocate the new argv array
- argvn = g_new0(char *,newargc+1);
- GST_DEBUG(0,"supposed to have %d args\n",newargc);
-
- // now attempt to construct the new arg list
- j = 0;k = 0;
- for (i=0;i<len+1;i++) {
- // if it's a delimiter
- if (strchr("([{}]) ",cmdline[i]) || (cmdline[i] == '\0')) {
- // extract the previous arg
- if (i-k > 0) {
- if (cmdline[k] == ' ') k++;
- argvn[j] = g_new0(char,(i-k)+1);
- memcpy(argvn[j],&cmdline[k],i-k);
-
- // catch misparses
- if (strlen(argvn[j]) > 0) j++;
- }
- k = i;
-
- // if this is a bracket, construct a word
- if ((cmdline[i] != ' ') && (cmdline[i] != '\0')) {
- argvn[j++] = g_strdup_printf("%c",cmdline[i]);
- k++;
- }
+ GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description '%s'",
+ pipeline_description);
+
+ element = priv_gst_parse_launch (pipeline_description, &myerror, context,
+ flags);
+
+ /* 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;
}
}
- // print them out
- for (i=0;i<newargc;i++) {
- GST_DEBUG(0,"arg %d is: %s\n",i,argvn[i]);
- }
+ if (myerror)
+ g_propagate_error (error, myerror);
+
+ return element;
+#else
+ gchar *msg;
+
+ GST_WARNING ("Disabled API called");
- // set up the elementcounts hash
- priv.elementcounts = g_hash_table_new(g_str_hash,g_str_equal);
+ 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 gst_parse_launch_cmdline(newargc,argvn,parent,&priv);
+ return NULL;
+#endif
}