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 static GstParseContext *
49 gst_parse_context_copy (const GstParseContext * context)
51 GstParseContext *ret = NULL;
52 #ifndef GST_DISABLE_PARSE
54 ret = gst_parse_context_new ();
56 GQueue missing_copy = G_QUEUE_INIT;
59 for (l = context->missing_elements; l != NULL; l = l->next)
60 g_queue_push_tail (&missing_copy, g_strdup ((const gchar *) l->data));
62 ret->missing_elements = missing_copy.head;
68 G_DEFINE_BOXED_TYPE (GstParseContext, gst_parse_context,
69 (GBoxedCopyFunc) gst_parse_context_copy,
70 (GBoxedFreeFunc) gst_parse_context_free);
73 * gst_parse_error_quark:
75 * Get the error quark used by the parsing subsystem.
77 * Returns: the quark of the parse errors.
80 gst_parse_error_quark (void)
82 static GQuark quark = 0;
85 quark = g_quark_from_static_string ("gst_parse_error");
91 * gst_parse_context_new:
93 * Allocates a parse context for use with gst_parse_launch_full() or
94 * gst_parse_launchv_full().
96 * Free-function: gst_parse_context_free
98 * Returns: (transfer full): a newly-allocated parse context. Free with
99 * gst_parse_context_free() when no longer needed.
102 gst_parse_context_new (void)
104 #ifndef GST_DISABLE_PARSE
105 GstParseContext *ctx;
107 ctx = g_slice_new (GstParseContext);
108 ctx->missing_elements = NULL;
117 * gst_parse_context_free:
118 * @context: (transfer full): a #GstParseContext
120 * Frees a parse context previously allocated with gst_parse_context_new().
123 gst_parse_context_free (GstParseContext * context)
125 #ifndef GST_DISABLE_PARSE
127 g_list_foreach (context->missing_elements, (GFunc) g_free, NULL);
128 g_list_free (context->missing_elements);
129 g_slice_free (GstParseContext, context);
135 * gst_parse_context_get_missing_elements:
136 * @context: a #GstParseContext
138 * Retrieve missing elements from a previous run of gst_parse_launch_full()
139 * or gst_parse_launchv_full(). Will only return results if an error code
140 * of %GST_PARSE_ERROR_NO_SUCH_ELEMENT was returned.
142 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*): a
143 * %NULL-terminated array of element factory name strings of missing
144 * elements. Free with g_strfreev() when no longer needed.
147 gst_parse_context_get_missing_elements (GstParseContext * context)
149 #ifndef GST_DISABLE_PARSE
154 g_return_val_if_fail (context != NULL, NULL);
156 len = g_list_length (context->missing_elements);
158 if (G_UNLIKELY (len == 0))
161 arr = g_new (gchar *, len + 1);
163 for (i = 0, l = context->missing_elements; l != NULL; l = l->next, ++i)
164 arr[i] = g_strdup (l->data);
174 #ifndef GST_DISABLE_PARSE
176 _gst_parse_escape (const gchar * str)
178 GString *gstr = NULL;
181 g_return_val_if_fail (str != NULL, NULL);
183 gstr = g_string_sized_new (strlen (str));
188 if (*str == '"' && (!in_quotes || (in_quotes && *(str - 1) != '\\')))
189 in_quotes = !in_quotes;
191 if (*str == ' ' && !in_quotes)
192 g_string_append_c (gstr, '\\');
194 g_string_append_c (gstr, *str);
198 return g_string_free (gstr, FALSE);
200 #endif /* !GST_DISABLE_PARSE */
204 * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
205 * @error: pointer to a #GError
207 * Create a new element based on command line syntax.
208 * @error will contain an error message if an erroneous pipeline is specified.
209 * An error does not mean that the pipeline could not be constructed.
211 * Returns: (transfer floating): a new element on success and %NULL on failure.
214 gst_parse_launchv (const gchar ** argv, GError ** error)
216 return gst_parse_launchv_full (argv, NULL, GST_PARSE_FLAG_NONE, error);
220 * gst_parse_launchv_full:
221 * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
222 * @context: (allow-none): a parse context allocated with
223 * gst_parse_context_new(), or %NULL
224 * @flags: parsing options, or #GST_PARSE_FLAG_NONE
225 * @error: pointer to a #GError (which must be initialised to %NULL)
227 * Create a new element based on command line syntax.
228 * @error will contain an error message if an erroneous pipeline is specified.
229 * An error does not mean that the pipeline could not be constructed.
231 * Returns: (transfer floating): a new element on success; on failure, either %NULL
232 * or a partially-constructed bin or element will be returned and @error will
233 * be set (unless you passed #GST_PARSE_FLAG_FATAL_ERRORS in @flags, then
234 * %NULL will always be returned on failure)
237 gst_parse_launchv_full (const gchar ** argv, GstParseContext * context,
238 GstParseFlags flags, GError ** error)
240 #ifndef GST_DISABLE_PARSE
243 const gchar **argvp, *arg;
246 g_return_val_if_fail (argv != NULL, NULL);
247 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
249 /* let's give it a nice size. */
250 str = g_string_sized_new (1024);
255 GST_DEBUG ("escaping argument %s", arg);
256 tmp = _gst_parse_escape (arg);
257 g_string_append (str, tmp);
259 g_string_append_c (str, ' ');
263 element = gst_parse_launch_full (str->str, context, flags, error);
265 g_string_free (str, TRUE);
269 /* gst_parse_launch_full() will set a GST_CORE_ERROR_DISABLED error for us */
270 return gst_parse_launch_full ("", NULL, 0, error);
276 * @pipeline_description: the command line describing the pipeline
277 * @error: the error message in case of an erroneous pipeline.
279 * Create a new pipeline based on command line syntax.
280 * Please note that you might get a return value that is not %NULL even though
281 * the @error is set. In this case there was a recoverable parsing error and you
282 * can try to play the pipeline.
284 * Returns: (transfer floating): a new element on success, %NULL on failure. If
285 * more than one toplevel element is specified by the @pipeline_description,
286 * all elements are put into a #GstPipeline, which than is returned.
289 gst_parse_launch (const gchar * pipeline_description, GError ** error)
291 return gst_parse_launch_full (pipeline_description, NULL, GST_PARSE_FLAG_NONE,
296 * gst_parse_launch_full:
297 * @pipeline_description: the command line describing the pipeline
298 * @context: (allow-none): a parse context allocated with
299 * gst_parse_context_new(), or %NULL
300 * @flags: parsing options, or #GST_PARSE_FLAG_NONE
301 * @error: the error message in case of an erroneous pipeline.
303 * Create a new pipeline based on command line syntax.
304 * Please note that you might get a return value that is not %NULL even though
305 * the @error is set. In this case there was a recoverable parsing error and you
306 * can try to play the pipeline.
308 * Returns: (transfer floating): a new element on success, %NULL on failure. If
309 * more than one toplevel element is specified by the @pipeline_description,
310 * all elements are put into a #GstPipeline, which then is returned (unless
311 * the GST_PARSE_FLAG_PLACE_IN_BIN flag is set, in which case they are put
312 * in a #GstBin instead).
315 gst_parse_launch_full (const gchar * pipeline_description,
316 GstParseContext * context, GstParseFlags flags, GError ** error)
318 #ifndef GST_DISABLE_PARSE
320 GError *myerror = NULL;
322 g_return_val_if_fail (pipeline_description != NULL, NULL);
323 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
325 GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description '%s'",
326 pipeline_description);
328 element = priv_gst_parse_launch (pipeline_description, &myerror, context,
331 /* don't return partially constructed pipeline if FATAL_ERRORS was given */
332 if (G_UNLIKELY (myerror != NULL && element != NULL)) {
333 if ((flags & GST_PARSE_FLAG_FATAL_ERRORS)) {
334 gst_object_unref (element);
340 g_propagate_error (error, myerror);
346 GST_WARNING ("Disabled API called");
348 msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
349 g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);