2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2002 Andy Wingo <wingo@pobox.com>
5 * 2008 Tim-Philipp Müller <tim centricular net>
7 * gstparse.c: get a pipeline from a text pipeline description
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
28 * @short_description: Get a pipeline from a text pipeline description
30 * These function allow to create a pipeline based on the syntax used in the
31 * gst-launch-1.0 utility (see man-page for syntax documentation).
33 * Please note that these functions take several measures to create
34 * somewhat dynamic pipelines. Due to that such pipelines are not always
35 * reusable (set the state to NULL and back to PLAYING).
38 #include "gst_private.h"
44 #ifndef GST_DISABLE_PARSE
45 #include "parse/types.h"
48 G_DEFINE_BOXED_TYPE (GstParseContext, gst_parse_context,
49 (GBoxedCopyFunc) gst_parse_context_copy,
50 (GBoxedFreeFunc) gst_parse_context_free);
53 * gst_parse_error_quark:
55 * Get the error quark used by the parsing subsystem.
57 * Returns: the quark of the parse errors.
60 gst_parse_error_quark (void)
62 static GQuark quark = 0;
65 quark = g_quark_from_static_string ("gst_parse_error");
71 * gst_parse_context_new:
73 * Allocates a parse context for use with gst_parse_launch_full() or
74 * gst_parse_launchv_full().
76 * Free-function: gst_parse_context_free
78 * Returns: (transfer full) (nullable): a newly-allocated parse context. Free
79 * with gst_parse_context_free() when no longer needed.
82 gst_parse_context_new (void)
84 #ifndef GST_DISABLE_PARSE
87 ctx = g_slice_new (GstParseContext);
88 ctx->missing_elements = NULL;
97 * gst_parse_context_copy:
98 * @context: a #GstParseContext
100 * Copies the @context.
102 * Returns: (transfer full) (nullable): A copied #GstParseContext
105 gst_parse_context_copy (const GstParseContext * context)
107 GstParseContext *ret = NULL;
108 #ifndef GST_DISABLE_PARSE
110 ret = gst_parse_context_new ();
112 GQueue missing_copy = G_QUEUE_INIT;
115 for (l = context->missing_elements; l != NULL; l = l->next)
116 g_queue_push_tail (&missing_copy, g_strdup ((const gchar *) l->data));
118 ret->missing_elements = missing_copy.head;
125 * gst_parse_context_free:
126 * @context: (transfer full): a #GstParseContext
128 * Frees a parse context previously allocated with gst_parse_context_new().
131 gst_parse_context_free (GstParseContext * context)
133 #ifndef GST_DISABLE_PARSE
135 g_list_foreach (context->missing_elements, (GFunc) g_free, NULL);
136 g_list_free (context->missing_elements);
137 g_slice_free (GstParseContext, context);
143 * gst_parse_context_get_missing_elements:
144 * @context: a #GstParseContext
146 * Retrieve missing elements from a previous run of gst_parse_launch_full()
147 * or gst_parse_launchv_full(). Will only return results if an error code
148 * of %GST_PARSE_ERROR_NO_SUCH_ELEMENT was returned.
150 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*) (nullable): a
151 * %NULL-terminated array of element factory name strings of missing
152 * elements. Free with g_strfreev() when no longer needed.
155 gst_parse_context_get_missing_elements (GstParseContext * context)
157 #ifndef GST_DISABLE_PARSE
162 g_return_val_if_fail (context != NULL, NULL);
164 len = g_list_length (context->missing_elements);
166 if (G_UNLIKELY (len == 0))
169 arr = g_new (gchar *, len + 1);
171 for (i = 0, l = context->missing_elements; l != NULL; l = l->next, ++i)
172 arr[i] = g_strdup (l->data);
182 #ifndef GST_DISABLE_PARSE
184 _gst_parse_escape (const gchar * str)
186 GString *gstr = NULL;
189 g_return_val_if_fail (str != NULL, NULL);
191 gstr = g_string_sized_new (strlen (str));
196 if (*str == '"' && (!in_quotes || (in_quotes && *(str - 1) != '\\')))
197 in_quotes = !in_quotes;
199 if (*str == ' ' && !in_quotes)
200 g_string_append_c (gstr, '\\');
202 g_string_append_c (gstr, *str);
206 return g_string_free (gstr, FALSE);
208 #endif /* !GST_DISABLE_PARSE */
212 * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
213 * @error: pointer to a #GError
215 * Create a new element based on command line syntax.
216 * @error will contain an error message if an erroneous pipeline is specified.
217 * An error does not mean that the pipeline could not be constructed.
219 * Returns: (transfer floating) (nullable): a new element on success and %NULL
223 gst_parse_launchv (const gchar ** argv, GError ** error)
225 return gst_parse_launchv_full (argv, NULL, GST_PARSE_FLAG_NONE, error);
229 * gst_parse_launchv_full:
230 * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
231 * @context: (allow-none): a parse context allocated with
232 * gst_parse_context_new(), or %NULL
233 * @flags: parsing options, or #GST_PARSE_FLAG_NONE
234 * @error: pointer to a #GError (which must be initialised to %NULL)
236 * Create a new element based on command line syntax.
237 * @error will contain an error message if an erroneous pipeline is specified.
238 * An error does not mean that the pipeline could not be constructed.
240 * Returns: (transfer floating) (nullable): a new element on success; on
241 * failure, either %NULL or a partially-constructed bin or element will be
242 * returned and @error will be set (unless you passed
243 * #GST_PARSE_FLAG_FATAL_ERRORS in @flags, then %NULL will always be returned
247 gst_parse_launchv_full (const gchar ** argv, GstParseContext * context,
248 GstParseFlags flags, GError ** error)
250 #ifndef GST_DISABLE_PARSE
253 const gchar **argvp, *arg;
256 g_return_val_if_fail (argv != NULL, NULL);
257 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
259 /* let's give it a nice size. */
260 str = g_string_sized_new (1024);
265 GST_DEBUG ("escaping argument %s", arg);
266 tmp = _gst_parse_escape (arg);
267 g_string_append (str, tmp);
269 g_string_append_c (str, ' ');
273 element = gst_parse_launch_full (str->str, context, flags, error);
275 g_string_free (str, TRUE);
279 /* gst_parse_launch_full() will set a GST_CORE_ERROR_DISABLED error for us */
280 return gst_parse_launch_full ("", NULL, 0, error);
286 * @pipeline_description: the command line describing the pipeline
287 * @error: the error message in case of an erroneous pipeline.
289 * Create a new pipeline based on command line syntax.
290 * Please note that you might get a return value that is not %NULL even though
291 * the @error is set. In this case there was a recoverable parsing error and you
292 * can try to play the pipeline.
294 * Returns: (transfer floating) (nullable): a new element on success, %NULL on
295 * failure. If more than one toplevel element is specified by the
296 * @pipeline_description, all elements are put into a #GstPipeline, which
300 gst_parse_launch (const gchar * pipeline_description, GError ** error)
302 return gst_parse_launch_full (pipeline_description, NULL, GST_PARSE_FLAG_NONE,
307 * gst_parse_launch_full:
308 * @pipeline_description: the command line describing the pipeline
309 * @context: (allow-none): a parse context allocated with
310 * gst_parse_context_new(), or %NULL
311 * @flags: parsing options, or #GST_PARSE_FLAG_NONE
312 * @error: the error message in case of an erroneous pipeline.
314 * Create a new pipeline based on command line syntax.
315 * Please note that you might get a return value that is not %NULL even though
316 * the @error is set. In this case there was a recoverable parsing error and you
317 * can try to play the pipeline.
319 * Returns: (transfer floating) (nullable): a new element on success, %NULL on
320 * failure. If more than one toplevel element is specified by the
321 * @pipeline_description, all elements are put into a #GstPipeline, which
322 * then is returned (unless the GST_PARSE_FLAG_PLACE_IN_BIN flag is set, in
323 * which case they are put in a #GstBin instead).
326 gst_parse_launch_full (const gchar * pipeline_description,
327 GstParseContext * context, GstParseFlags flags, GError ** error)
329 #ifndef GST_DISABLE_PARSE
331 GError *myerror = NULL;
333 g_return_val_if_fail (pipeline_description != NULL, NULL);
334 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
336 GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description '%s'",
337 pipeline_description);
339 element = priv_gst_parse_launch (pipeline_description, &myerror, context,
342 /* don't return partially constructed pipeline if FATAL_ERRORS was given */
343 if (G_UNLIKELY (myerror != NULL && element != NULL)) {
344 if ((flags & GST_PARSE_FLAG_FATAL_ERRORS)) {
345 gst_object_unref (element);
351 g_propagate_error (error, myerror);
357 GST_WARNING ("Disabled API called");
359 msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
360 g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);