gstreamer_core/gst/gstparse.c
branchRCL_3
changeset 30 7e817e7e631c
parent 29 567bb019e3e3
equal deleted inserted replaced
29:567bb019e3e3 30:7e817e7e631c
     1 /* GStreamer
     1 /* GStreamer
     2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
     2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
     3  *                    2000 Wim Taymans <wtay@chello.be>
     3  *                    2000 Wim Taymans <wtay@chello.be>
     4  *                    2002 Andy Wingo <wingo@pobox.com>
     4  *                    2002 Andy Wingo <wingo@pobox.com>
     5  *                    2008 Tim-Philipp Müller <tim centricular net>
       
     6  *
     5  *
     7  * gstparse.c: get a pipeline from a text pipeline description
     6  * gstparse.c: get a pipeline from a text pipeline description
     8  *
     7  *
     9  * This library is free software; you can redistribute it and/or
     8  * This library is free software; you can redistribute it and/or
    10  * modify it under the terms of the GNU Library General Public
     9  * modify it under the terms of the GNU Library General Public
    25 /**
    24 /**
    26  * SECTION:gstparse
    25  * SECTION:gstparse
    27  * @short_description: Get a pipeline from a text pipeline description
    26  * @short_description: Get a pipeline from a text pipeline description
    28  *
    27  *
    29  * These function allow to create a pipeline based on the syntax used in the
    28  * These function allow to create a pipeline based on the syntax used in the
    30  * gst-launch utility.
    29  * gst-launch utillity.
    31  */
    30  */
    32 
    31 
    33 
    32 
    34 #include "gst_private.h"
    33 #include "gst_private.h"
    35 #include <string.h>
    34 #include <string.h>
    36 
    35 
    37 #include "gstparse.h"
    36 #include "gstparse.h"
    38 #include "gsterror.h"
    37 #include "gsterror.h"
    39 #include "gstinfo.h"
    38 #include "gstinfo.h"
    40 
    39 
    41 extern GstElement *_gst_parse_launch (const gchar *, GError **,
    40 extern GstElement *_gst_parse_launch (const gchar *, GError **);
    42     GstParseContext *, GstParseFlags);
       
    43 
    41 
    44 /**
    42 /**
    45  * gst_parse_error_quark:
    43  * gst_parse_error_quark:
    46  *
    44  *
    47  * Get the error quark used by the parsing subsystem.
    45  * Get the error quark used by the parsing subsystem.
    60   if (!quark)
    58   if (!quark)
    61     quark = g_quark_from_static_string ("gst_parse_error");
    59     quark = g_quark_from_static_string ("gst_parse_error");
    62   return quark;
    60   return quark;
    63 }
    61 }
    64 
    62 
    65 
       
    66 /**
       
    67  * gst_parse_context_new:
       
    68  *
       
    69  * Allocates a parse context for use with gst_parse_launch_full() or
       
    70  * gst_parse_launchv_full().
       
    71  *
       
    72  * Returns: a newly-allocated parse context. Free with gst_parse_context_free()
       
    73  *     when no longer needed.
       
    74  *
       
    75  * Since: 0.10.20
       
    76  */
       
    77 #ifdef __SYMBIAN32__
       
    78 EXPORT_C
       
    79 #endif
       
    80 
       
    81 GstParseContext *
       
    82 gst_parse_context_new (void)
       
    83 {
       
    84 #ifndef GST_DISABLE_PARSE
       
    85   GstParseContext *ctx;
       
    86 
       
    87   ctx = g_slice_new (GstParseContext);
       
    88   ctx->missing_elements = NULL;
       
    89 
       
    90   return ctx;
       
    91 #else
       
    92   return NULL;
       
    93 #endif
       
    94 }
       
    95 
       
    96 /**
       
    97  * gst_parse_context_free:
       
    98  * @context: a #GstParseContext
       
    99  *
       
   100  * Frees a parse context previously allocated with gst_parse_context_new().
       
   101  *
       
   102  * Since: 0.10.20
       
   103  */
       
   104 #ifdef __SYMBIAN32__
       
   105 EXPORT_C
       
   106 #endif
       
   107 
       
   108 void
       
   109 gst_parse_context_free (GstParseContext * context)
       
   110 {
       
   111 #ifndef GST_DISABLE_PARSE
       
   112   if (context) {
       
   113     g_list_foreach (context->missing_elements, (GFunc) g_free, NULL);
       
   114     g_list_free (context->missing_elements);
       
   115     g_slice_free (GstParseContext, context);
       
   116   }
       
   117 #endif
       
   118 }
       
   119 
       
   120 /**
       
   121  * gst_parse_context_get_missing_elements:
       
   122  * @context: a #GstParseContext
       
   123  *
       
   124  * Retrieve missing elements from a previous run of gst_parse_launch_full()
       
   125  * or gst_parse_launchv_full(). Will only return results if an error code
       
   126  * of %GST_PARSE_ERROR_NO_SUCH_ELEMENT was returned.
       
   127  *
       
   128  * Returns: a NULL-terminated array of element factory name strings of
       
   129  *     missing elements. Free with g_strfreev() when no longer needed.
       
   130  *
       
   131  * Since: 0.10.20
       
   132  */
       
   133 #ifdef __SYMBIAN32__
       
   134 EXPORT_C
       
   135 #endif
       
   136 
       
   137 gchar **
       
   138 gst_parse_context_get_missing_elements (GstParseContext * context)
       
   139 {
       
   140 #ifndef GST_DISABLE_PARSE
       
   141   gchar **arr;
       
   142   GList *l;
       
   143   guint len, i;
       
   144 
       
   145   g_return_val_if_fail (context != NULL, NULL);
       
   146 
       
   147   len = g_list_length (context->missing_elements);
       
   148 
       
   149   if (G_UNLIKELY (len == 0))
       
   150     return NULL;
       
   151 
       
   152   arr = g_new (gchar *, len + 1);
       
   153 
       
   154   for (i = 0, l = context->missing_elements; l != NULL; l = l->next, ++i)
       
   155     arr[i] = g_strdup (l->data);
       
   156 
       
   157   arr[i] = NULL;
       
   158 
       
   159   return arr;
       
   160 #else
       
   161   return NULL;
       
   162 #endif
       
   163 }
       
   164 
       
   165 #ifndef GST_DISABLE_PARSE
    63 #ifndef GST_DISABLE_PARSE
   166 static gchar *
    64 static gchar *
   167 _gst_parse_escape (const gchar * str)
    65 _gst_parse_escape (const gchar * str)
   168 {
    66 {
   169   GString *gstr = NULL;
    67   GString *gstr = NULL;
       
    68   gchar *newstr = NULL;
   170 
    69 
   171   g_return_val_if_fail (str != NULL, NULL);
    70   g_return_val_if_fail (str != NULL, NULL);
   172 
    71 
   173   gstr = g_string_sized_new (strlen (str));
    72   gstr = g_string_sized_new (strlen (str));
   174 
    73 
   177       g_string_append_c (gstr, '\\');
    76       g_string_append_c (gstr, '\\');
   178     g_string_append_c (gstr, *str);
    77     g_string_append_c (gstr, *str);
   179     str++;
    78     str++;
   180   }
    79   }
   181 
    80 
   182   return g_string_free (gstr, FALSE);
    81   newstr = gstr->str;
       
    82   g_string_free (gstr, FALSE);
       
    83 
       
    84   return newstr;
   183 }
    85 }
   184 #endif /* !GST_DISABLE_PARSE */
    86 #endif /* !GST_DISABLE_PARSE */
   185 
    87 
   186 /**
    88 /**
   187  * gst_parse_launchv:
    89  * gst_parse_launchv:
   199 #endif
   101 #endif
   200 
   102 
   201 GstElement *
   103 GstElement *
   202 gst_parse_launchv (const gchar ** argv, GError ** error)
   104 gst_parse_launchv (const gchar ** argv, GError ** error)
   203 {
   105 {
   204   return gst_parse_launchv_full (argv, NULL, 0, error);
       
   205 }
       
   206 
       
   207 /**
       
   208  * gst_parse_launchv_full:
       
   209  * @argv: null-terminated array of arguments
       
   210  * @context: a parse context allocated with gst_parse_context_new(), or %NULL
       
   211  * @flags: parsing options, or #GST_PARSE_FLAG_NONE
       
   212  * @error: pointer to a #GError (which must be initialised to %NULL)
       
   213  *
       
   214  * Create a new element based on command line syntax.
       
   215  * @error will contain an error message if an erroneous pipeline is specified.
       
   216  * An error does not mean that the pipeline could not be constructed.
       
   217  *
       
   218  * Returns: a new element on success; on failure, either %NULL or a
       
   219  * partially-constructed bin or element will be returned and @error will be set
       
   220  * (unless you passed #GST_PARSE_FLAG_FATAL_ERRORS in @flags, then %NULL will
       
   221  * always be returned on failure)
       
   222  *
       
   223  * Since: 0.10.20
       
   224  */
       
   225 #ifdef __SYMBIAN32__
       
   226 EXPORT_C
       
   227 #endif
       
   228 
       
   229 GstElement *
       
   230 gst_parse_launchv_full (const gchar ** argv, GstParseContext * context,
       
   231     GstParseFlags flags, GError ** error)
       
   232 {
       
   233 #ifndef GST_DISABLE_PARSE
   106 #ifndef GST_DISABLE_PARSE
   234   GstElement *element;
   107   GstElement *element;
   235   GString *str;
   108   GString *str;
   236   const gchar **argvp, *arg;
   109   const gchar **argvp, *arg;
   237   gchar *tmp;
   110   gchar *tmp;
   238 
   111 
   239   g_return_val_if_fail (argv != NULL, NULL);
   112   g_return_val_if_fail (argv != NULL, NULL);
   240   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
       
   241 
   113 
   242   /* let's give it a nice size. */
   114   /* let's give it a nice size. */
   243   str = g_string_sized_new (1024);
   115   str = g_string_sized_new (1024);
   244 
   116 
   245   argvp = argv;
   117   argvp = argv;
   250     g_free (tmp);
   122     g_free (tmp);
   251     g_string_append_c (str, ' ');
   123     g_string_append_c (str, ' ');
   252     argvp++;
   124     argvp++;
   253   }
   125   }
   254 
   126 
   255   element = gst_parse_launch_full (str->str, context, flags, error);
   127   element = gst_parse_launch (str->str, error);
   256 
   128 
   257   g_string_free (str, TRUE);
   129   g_string_free (str, TRUE);
   258 
   130 
   259   return element;
   131   return element;
   260 #else
   132 #else
   261   /* gst_parse_launch_full() will set a GST_CORE_ERROR_DISABLED error for us */
   133   gchar *msg;
   262   return gst_parse_launch_full ("", NULL, 0, error);
   134 
       
   135   GST_WARNING ("Disabled API called: gst_parse_launchv()");
       
   136 
       
   137   msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
       
   138   g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
       
   139   g_free (msg);
       
   140 
       
   141   return NULL;
   263 #endif
   142 #endif
   264 }
   143 }
   265 
   144 
   266 /**
   145 /**
   267  * gst_parse_launch:
   146  * gst_parse_launch:
   282 #endif
   161 #endif
   283 
   162 
   284 GstElement *
   163 GstElement *
   285 gst_parse_launch (const gchar * pipeline_description, GError ** error)
   164 gst_parse_launch (const gchar * pipeline_description, GError ** error)
   286 {
   165 {
   287   return gst_parse_launch_full (pipeline_description, NULL, 0, error);
       
   288 }
       
   289 
       
   290 /**
       
   291  * gst_parse_launch_full:
       
   292  * @pipeline_description: the command line describing the pipeline
       
   293  * @context: a parse context allocated with gst_parse_context_new(), or %NULL
       
   294  * @flags: parsing options, or #GST_PARSE_FLAG_NONE
       
   295  * @error: the error message in case of an erroneous pipeline.
       
   296  *
       
   297  * Create a new pipeline based on command line syntax.
       
   298  * Please note that you might get a return value that is not %NULL even though
       
   299  * the @error is set. In this case there was a recoverable parsing error and you
       
   300  * can try to play the pipeline.
       
   301  *
       
   302  * Returns: a new element on success, %NULL on failure. If more than one toplevel
       
   303  * element is specified by the @pipeline_description, all elements are put into
       
   304  * a #GstPipeline, which then is returned.
       
   305  *
       
   306  * Since: 0.10.20
       
   307  */
       
   308 #ifdef __SYMBIAN32__
       
   309 EXPORT_C
       
   310 #endif
       
   311 
       
   312 GstElement *
       
   313 gst_parse_launch_full (const gchar * pipeline_description,
       
   314     GstParseContext * context, GstParseFlags flags, GError ** error)
       
   315 {
       
   316 #ifndef GST_DISABLE_PARSE
   166 #ifndef GST_DISABLE_PARSE
   317   GstElement *element;
   167   GstElement *element;
   318 
   168 
   319   g_return_val_if_fail (pipeline_description != NULL, NULL);
   169   g_return_val_if_fail (pipeline_description != NULL, NULL);
   320   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
       
   321 
   170 
   322   GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description '%s'",
   171   GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
   323       pipeline_description);
   172       pipeline_description);
   324 
   173 
   325   element = _gst_parse_launch (pipeline_description, error, context, flags);
   174   element = _gst_parse_launch (pipeline_description, error);
   326 
       
   327   /* don't return partially constructed pipeline if FATAL_ERRORS was given */
       
   328   if (G_UNLIKELY (error != NULL && *error != NULL && element != NULL)) {
       
   329     if ((flags & GST_PARSE_FLAG_FATAL_ERRORS)) {
       
   330       gst_object_unref (element);
       
   331       element = NULL;
       
   332     }
       
   333   }
       
   334 
   175 
   335   return element;
   176   return element;
   336 #else
   177 #else
   337   gchar *msg;
   178   gchar *msg;
   338 
   179 
   339   GST_WARNING ("Disabled API called");
   180   GST_WARNING ("Disabled API called: gst_parse_launch()");
   340 
   181 
   341   msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
   182   msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
   342   g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
   183   g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
   343   g_free (msg);
   184   g_free (msg);
   344 
   185