gstpad: Probes that return HANDLED can reset the data info field
[platform/upstream/gstreamer.git] / gst / gstparse.c
index d41b97e..9f97637 100644 (file)
  *
  * 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.
  */
 
 /**
  * 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 utility.
+ * 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 "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:
  *
@@ -65,10 +73,10 @@ gst_parse_error_quark (void)
  * Allocates a parse context for use with gst_parse_launch_full() or
  * gst_parse_launchv_full().
  *
- * Returns: a newly-allocated parse context. Free with gst_parse_context_free()
- *     when no longer needed.
+ * Free-function: gst_parse_context_free
  *
- * Since: 0.10.20
+ * 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)
@@ -86,12 +94,38 @@ gst_parse_context_new (void)
 }
 
 /**
- * gst_parse_context_free:
+ * gst_parse_context_copy:
  * @context: a #GstParseContext
  *
- * Frees a parse context previously allocated with gst_parse_context_new().
+ * Copies the @context.
  *
- * Since: 0.10.20
+ * 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;
+
+    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)
@@ -113,10 +147,9 @@ gst_parse_context_free (GstParseContext * context)
  * or gst_parse_launchv_full(). Will only return results if an error code
  * of %GST_PARSE_ERROR_NO_SUCH_ELEMENT was returned.
  *
- * Returns: a NULL-terminated array of element factory name strings of
- *     missing elements. Free with g_strfreev() when no longer needed.
- *
- * Since: 0.10.20
+ * 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)
@@ -151,14 +184,21 @@ 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 == ' ')
+    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++;
   }
@@ -169,25 +209,27 @@ _gst_parse_escape (const gchar * str)
 
 /**
  * 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 element based on command line syntax.
- * @error will contain an error message if an erroneuos pipeline is specified.
+ * @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 element on success and %NULL on failure.
+ * 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, 0, error);
+  return gst_parse_launchv_full (argv, NULL, GST_PARSE_FLAG_NONE, error);
 }
 
 /**
  * gst_parse_launchv_full:
- * @argv: null-terminated array of arguments
- * @context: a parse context allocated with gst_parse_context_new(), or %NULL
+ * @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)
  *
@@ -195,12 +237,11 @@ gst_parse_launchv (const gchar ** argv, GError ** error)
  * @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 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)
- *
- * Since: 0.10.20
+ * 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,
@@ -221,6 +262,7 @@ gst_parse_launchv_full (const gchar ** argv, GstParseContext * context,
   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);
@@ -249,20 +291,23 @@ gst_parse_launchv_full (const gchar ** argv, GstParseContext * context,
  * the @error is set. In this case there was a recoverable parsing error and you
  * can try to play the pipeline.
  *
- * Returns: 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.
+ * 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, 0, 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: a parse context allocated with gst_parse_context_new(), or %NULL
+ * @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.
  *
@@ -271,11 +316,11 @@ gst_parse_launch (const gchar * pipeline_description, GError ** error)
  * the @error is set. In this case there was a recoverable parsing error and you
  * can try to play the pipeline.
  *
- * Returns: 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.
- *
- * Since: 0.10.20
+ * 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,
@@ -283,6 +328,7 @@ gst_parse_launch_full (const gchar * pipeline_description,
 {
 #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);
@@ -290,16 +336,20 @@ gst_parse_launch_full (const gchar * pipeline_description,
   GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description '%s'",
       pipeline_description);
 
-  element = _gst_parse_launch (pipeline_description, error, context, flags);
+  element = priv_gst_parse_launch (pipeline_description, &myerror, context,
+      flags);
 
   /* don't return partially constructed pipeline if FATAL_ERRORS was given */
-  if (G_UNLIKELY (error != NULL && *error != NULL && element != NULL)) {
+  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;