/* 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...)
+/**
+ * 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 "gsterror.h"
+#include "gstinfo.h"
+#ifndef GST_DISABLE_PARSE
#include "parse/types.h"
+#endif
-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
-{
- gchar *srcpadname;
- GstPad *target;
- GstElement *pipeline;
-}
-dyn_connect;
-
-
-GQuark
+/**
+ * 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)
{
static GQuark quark = 0;
+
if (!quark)
quark = g_quark_from_static_string ("gst_parse_error");
return quark;
}
-G_GNUC_UNUSED 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 gboolean
-make_elements (graph_t *g, GError **error)
+/**
+ * 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)
{
- GList *l = NULL;
- gchar *bin_type;
- element_t *e;
-
- if (!(g->bins || g->elements)) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_SYNTAX,
- "Empty bin");
- return FALSE;
- }
+ GstParseContext *ret = NULL;
+#ifndef GST_DISABLE_PARSE
- if (g->current_bin_type)
- bin_type = g->current_bin_type;
- else
- bin_type = "pipeline";
-
- if (!(g->bin = gst_elementfactory_make (bin_type, NULL))) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_NO_SUCH_ELEMENT,
- "No such bin type %s", bin_type);
- return FALSE;
- }
-
- l = g->elements;
- while (l) {
- e = (element_t*)l->data;
- if (!(e->element = gst_elementfactory_make (e->type, NULL))) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_NO_SUCH_ELEMENT,
- "No such element %s", e->type);
- return FALSE;
- }
- gst_bin_add (GST_BIN (g->bin), e->element);
- l = g_list_next (l);
- }
-
- l = g->bins;
- while (l) {
- if (!make_elements ((graph_t*)l->data, error))
- return FALSE;
- gst_bin_add (GST_BIN (g->bin), ((graph_t*)l->data)->bin);
- l = g_list_next (l);
- }
+ ret = gst_parse_context_new ();
+ if (context) {
+ GQueue missing_copy = G_QUEUE_INIT;
+ GList *l;
+
+ for (l = context->missing_elements; l != NULL; l = l->next)
+ g_queue_push_tail (&missing_copy, g_strdup ((const gchar *) l->data));
- return TRUE;
+ ret->missing_elements = missing_copy.head;
+ }
+#endif
+ return ret;
}
-static gboolean
-set_properties (graph_t *g, GError **error)
+/**
+ * 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)
{
- GList *l, *l2;
- element_t *e;
- property_t *p;
- GParamSpec *pspec;
-
- l = g->elements;
- while (l) {
- e = (element_t*)l->data;
- l2 = e->property_values;
- while (l2) {
- p = (property_t*)l2->data;
- if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (e->element), p->name))) {
- g_object_set_property (G_OBJECT (e->element), p->name, p->value);
- } else {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_NO_SUCH_PROPERTY,
- "No such property '%s' in element '%s'",
- p->name, GST_OBJECT_NAME (GST_OBJECT (e->element)));
- return FALSE;
- }
- l2 = g_list_next (l2);
- }
- l = g_list_next (l);
+#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);
}
-
- l = g->bins;
- while (l) {
- if (!set_properties ((graph_t*)l->data, error))
- return FALSE;
- l = g_list_next (l);
- }
-
- return TRUE;
+#endif
}
-static GstElement*
-find_element_by_index_recurse (graph_t *g, gint i)
+/**
+ * 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;
- element_t *e;
- GstElement *element;
-
- l = g->elements;
- while (l) {
- e = (element_t*)l->data;
- if (e->index == i) {
- return e->element;
- }
- l = g_list_next (l);
- }
-
- l = g->bins;
- while (l) {
- if ((element = find_element_by_index_recurse ((graph_t*)l->data, i)))
- return element;
- l = g_list_next (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
return NULL;
+#endif
}
-static GstElement*
-find_element_by_index (graph_t *g, gint i)
+#ifndef GST_DISABLE_PARSE
+static gchar *
+_gst_parse_escape (const gchar * str)
{
- while (g->parent)
- g = g->parent;
+ GString *gstr = NULL;
+ gboolean in_quotes;
- return find_element_by_index_recurse (g, i);
-}
+ g_return_val_if_fail (str != NULL, NULL);
-static gboolean
-make_connections (graph_t *g, GError **error)
-{
- GList *l, *a, *b;
- connection_t *c;
- GstElement *src, *sink;
- GstPad *p1, *p2;
-
- l = g->connections;
- while (l) {
- c = (connection_t*)l->data;
- if (c->src_name) {
- if (!(src = gst_bin_get_by_name (GST_BIN (g->bin), c->src_name))) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_NO_SUCH_ELEMENT,
- "No such element '%s'",
- c->src_name);
- return FALSE;
- }
- } else {
- src = find_element_by_index (g, c->src_index);
- g_assert (src);
- }
- if (c->sink_name) {
- if (!(sink = gst_bin_get_by_name (GST_BIN (g->bin), c->sink_name))) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_NO_SUCH_ELEMENT,
- "No such element '%s'",
- c->sink_name);
- return FALSE;
- }
- } else {
- sink = find_element_by_index (g, c->sink_index);
- g_assert (sink);
- }
-
- a = c->src_pads;
- b = c->sink_pads;
- if (a && b) {
- /* balanced multipad connection */
- while (a && b) {
- if (!gst_element_connect_pads (src, (gchar*)a->data, sink, (gchar*)b->data)) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_CONNECT,
- "Could not connect %s:%s to %s:%s",
- GST_OBJECT_NAME (src), (gchar*)a->data,
- GST_OBJECT_NAME (sink), (gchar*)b->data);
- return FALSE;
- }
- a = g_list_next (a);
- b = g_list_next (b);
- }
- } else if (a) {
- if (!(p1 = gst_element_get_pad (src, (gchar*)a->data))) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_CONNECT,
- "Could not get a pad %s from element %s",
- (gchar*)a->data, GST_OBJECT_NAME (src));
- return FALSE;
- }
- if (!(p2 = gst_element_get_compatible_pad (sink, p1))) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_CONNECT,
- "Could not find a compatible pad in element %s to for %s:%s",
- GST_OBJECT_NAME (sink), GST_OBJECT_NAME (src), (gchar*)a->data);
- return FALSE;
- }
- if (!gst_pad_connect (p1, p2)) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_CONNECT,
- "Could not connect %s:%s to %s:%s",
- GST_OBJECT_NAME (src), GST_OBJECT_NAME (p1),
- GST_OBJECT_NAME (sink), GST_OBJECT_NAME (p1));
- return FALSE;
- }
- } else if (b) {
- if (!(p2 = gst_element_get_pad (sink, (gchar*)b->data))) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_CONNECT,
- "Could not get a pad %s from element %s",
- (gchar*)b->data, GST_OBJECT_NAME (sink));
- return FALSE;
- }
- if (!(p1 = gst_element_get_compatible_pad (src, p2))) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_CONNECT,
- "Could not find a compatible pad in element %s to for %s:%s",
- GST_OBJECT_NAME (src), GST_OBJECT_NAME (sink), (gchar*)b->data);
- return FALSE;
- }
- if (!gst_pad_connect (p1, p2)) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_CONNECT,
- "Could not connect %s:%s to %s:%s",
- GST_OBJECT_NAME (src), GST_OBJECT_NAME (p1),
- GST_OBJECT_NAME (sink), GST_OBJECT_NAME (p1));
- return FALSE;
- }
- } else {
- if (!gst_element_connect (src, sink)) {
- g_set_error (error,
- GST_PARSE_ERROR,
- GST_PARSE_ERROR_CONNECT,
- "Could not connect %s to %s",
- GST_OBJECT_NAME (src), GST_OBJECT_NAME (sink));
- return FALSE;
- }
- }
- l = g_list_next (l);
- }
-
- l = g->bins;
- while (l) {
- if (!make_connections ((graph_t*)l->data, error))
- return FALSE;
- l = g_list_next (l);
+ 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 TRUE;
+
+ return g_string_free (gstr, FALSE);
}
+#endif /* !GST_DISABLE_PARSE */
-static GstBin*
-pipeline_from_graph (graph_t *g, GError **error)
+/**
+ * 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)
{
- if (!make_elements (g, error))
- return NULL;
-
- if (!set_properties (g, error))
- return NULL;
-
- if (!make_connections (g, error))
- return NULL;
-
- return (GstBin*)g->bin;
+ return gst_parse_launchv_full (argv, NULL, GST_PARSE_FLAG_NONE, error);
}
/**
- * gst_parse_launchv:
- * @argv: null-terminated array of arguments
+ * 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 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; 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)
*/
-GstBin *
-gst_parse_launchv (const gchar **argv, GError **error)
+GstElement *
+gst_parse_launchv_full (const gchar ** argv, GstParseContext * context,
+ GstParseFlags flags, GError ** error)
{
- GstBin *pipeline;
- gchar *pipeline_description;
+#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++;
+ }
+
+ element = gst_parse_launch_full (str->str, context, flags, error);
- /* i think this cast works out ok... */
- pipeline_description = g_strjoinv (" ", (gchar**)argv);
-
- pipeline = gst_parse_launch (pipeline_description, error);
+ g_string_free (str, TRUE);
- 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: (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.
+ */
+GstElement *
+gst_parse_launch (const gchar * pipeline_description, GError ** error)
+{
+ 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: 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
+ * then is returned (unless the GST_PARSE_FLAG_PLACE_IN_BIN flag is set, in
+ * which case they are put in a #GstBin instead).
*/
-GstBin *
-gst_parse_launch (const gchar * pipeline_description, GError **error)
+GstElement *
+gst_parse_launch_full (const gchar * pipeline_description,
+ GstParseContext * context, GstParseFlags flags, GError ** error)
{
- graph_t *graph;
- static GStaticMutex flex_lock = G_STATIC_MUTEX_INIT;
+#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);
- GST_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
- pipeline_description);
+ GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description '%s'",
+ pipeline_description);
- /* the need for the mutex will go away with flex 2.5.6 */
- g_static_mutex_lock (&flex_lock);
- graph = _gst_parse_launch (pipeline_description, error);
- g_static_mutex_unlock (&flex_lock);
+ element = priv_gst_parse_launch (pipeline_description, &myerror, context,
+ flags);
- if (!graph)
- return NULL;
-
- return pipeline_from_graph (graph, error);
+ /* 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;
+ }
+ }
+
+ if (myerror)
+ g_propagate_error (error, myerror);
+
+ return element;
+#else
+ gchar *msg;
+
+ GST_WARNING ("Disabled API called");
+
+ 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 NULL;
+#endif
}