Move GstAggregator from -bad to core
[platform/upstream/gstreamer.git] / gst / gstparse.c
1 /* GStreamer
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>
6  *
7  * gstparse.c: get a pipeline from a text pipeline description
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 /**
26  * SECTION:gstparse
27  * @title: GstParse
28  * @short_description: Get a pipeline from a text pipeline description
29  *
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).
32  *
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).
36  */
37
38 #include "gst_private.h"
39 #include <string.h>
40
41 #include "gstparse.h"
42 #include "gsterror.h"
43 #include "gstinfo.h"
44 #ifndef GST_DISABLE_PARSE
45 #include "parse/types.h"
46 #endif
47
48 G_DEFINE_BOXED_TYPE (GstParseContext, gst_parse_context,
49     (GBoxedCopyFunc) gst_parse_context_copy,
50     (GBoxedFreeFunc) gst_parse_context_free);
51
52 /**
53  * gst_parse_error_quark:
54  *
55  * Get the error quark used by the parsing subsystem.
56  *
57  * Returns: the quark of the parse errors.
58  */
59 GQuark
60 gst_parse_error_quark (void)
61 {
62   static GQuark quark = 0;
63
64   if (!quark)
65     quark = g_quark_from_static_string ("gst_parse_error");
66   return quark;
67 }
68
69
70 /**
71  * gst_parse_context_new:
72  *
73  * Allocates a parse context for use with gst_parse_launch_full() or
74  * gst_parse_launchv_full().
75  *
76  * Free-function: gst_parse_context_free
77  *
78  * Returns: (transfer full): a newly-allocated parse context. Free with
79  *     gst_parse_context_free() when no longer needed.
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_copy:
98  * @context: a #GstParseContext
99  *
100  * Copies the @context.
101  *
102  * Returns: (transfer full): A copied #GstParseContext
103  */
104 GstParseContext *
105 gst_parse_context_copy (const GstParseContext * context)
106 {
107   GstParseContext *ret = NULL;
108 #ifndef GST_DISABLE_PARSE
109
110   ret = gst_parse_context_new ();
111   if (context) {
112     GQueue missing_copy = G_QUEUE_INIT;
113     GList *l;
114
115     for (l = context->missing_elements; l != NULL; l = l->next)
116       g_queue_push_tail (&missing_copy, g_strdup ((const gchar *) l->data));
117
118     ret->missing_elements = missing_copy.head;
119   }
120 #endif
121   return ret;
122 }
123
124 /**
125  * gst_parse_context_free:
126  * @context: (transfer full): a #GstParseContext
127  *
128  * Frees a parse context previously allocated with gst_parse_context_new().
129  */
130 void
131 gst_parse_context_free (GstParseContext * context)
132 {
133 #ifndef GST_DISABLE_PARSE
134   if (context) {
135     g_list_foreach (context->missing_elements, (GFunc) g_free, NULL);
136     g_list_free (context->missing_elements);
137     g_slice_free (GstParseContext, context);
138   }
139 #endif
140 }
141
142 /**
143  * gst_parse_context_get_missing_elements:
144  * @context: a #GstParseContext
145  *
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.
149  *
150  * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*): a
151  *     %NULL-terminated array of element factory name strings of missing
152  *     elements. Free with g_strfreev() when no longer needed.
153  */
154 gchar **
155 gst_parse_context_get_missing_elements (GstParseContext * context)
156 {
157 #ifndef GST_DISABLE_PARSE
158   gchar **arr;
159   GList *l;
160   guint len, i;
161
162   g_return_val_if_fail (context != NULL, NULL);
163
164   len = g_list_length (context->missing_elements);
165
166   if (G_UNLIKELY (len == 0))
167     return NULL;
168
169   arr = g_new (gchar *, len + 1);
170
171   for (i = 0, l = context->missing_elements; l != NULL; l = l->next, ++i)
172     arr[i] = g_strdup (l->data);
173
174   arr[i] = NULL;
175
176   return arr;
177 #else
178   return NULL;
179 #endif
180 }
181
182 #ifndef GST_DISABLE_PARSE
183 static gchar *
184 _gst_parse_escape (const gchar * str)
185 {
186   GString *gstr = NULL;
187   gboolean in_quotes;
188
189   g_return_val_if_fail (str != NULL, NULL);
190
191   gstr = g_string_sized_new (strlen (str));
192
193   in_quotes = FALSE;
194
195   while (*str) {
196     if (*str == '"' && (!in_quotes || (in_quotes && *(str - 1) != '\\')))
197       in_quotes = !in_quotes;
198
199     if (*str == ' ' && !in_quotes)
200       g_string_append_c (gstr, '\\');
201
202     g_string_append_c (gstr, *str);
203     str++;
204   }
205
206   return g_string_free (gstr, FALSE);
207 }
208 #endif /* !GST_DISABLE_PARSE */
209
210 /**
211  * gst_parse_launchv:
212  * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
213  * @error: pointer to a #GError
214  *
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.
218  *
219  * Returns: (transfer floating): a new element on success and %NULL on failure.
220  */
221 GstElement *
222 gst_parse_launchv (const gchar ** argv, GError ** error)
223 {
224   return gst_parse_launchv_full (argv, NULL, GST_PARSE_FLAG_NONE, error);
225 }
226
227 /**
228  * gst_parse_launchv_full:
229  * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
230  * @context: (allow-none): a parse context allocated with
231  *     gst_parse_context_new(), or %NULL
232  * @flags: parsing options, or #GST_PARSE_FLAG_NONE
233  * @error: pointer to a #GError (which must be initialised to %NULL)
234  *
235  * Create a new element based on command line syntax.
236  * @error will contain an error message if an erroneous pipeline is specified.
237  * An error does not mean that the pipeline could not be constructed.
238  *
239  * Returns: (transfer floating): a new element on success; on failure, either %NULL
240  *   or a partially-constructed bin or element will be returned and @error will
241  *   be set (unless you passed #GST_PARSE_FLAG_FATAL_ERRORS in @flags, then
242  *   %NULL will always be returned on failure)
243  */
244 GstElement *
245 gst_parse_launchv_full (const gchar ** argv, GstParseContext * context,
246     GstParseFlags flags, GError ** error)
247 {
248 #ifndef GST_DISABLE_PARSE
249   GstElement *element;
250   GString *str;
251   const gchar **argvp, *arg;
252   gchar *tmp;
253
254   g_return_val_if_fail (argv != NULL, NULL);
255   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
256
257   /* let's give it a nice size. */
258   str = g_string_sized_new (1024);
259
260   argvp = argv;
261   while (*argvp) {
262     arg = *argvp;
263     GST_DEBUG ("escaping argument %s", arg);
264     tmp = _gst_parse_escape (arg);
265     g_string_append (str, tmp);
266     g_free (tmp);
267     g_string_append_c (str, ' ');
268     argvp++;
269   }
270
271   element = gst_parse_launch_full (str->str, context, flags, error);
272
273   g_string_free (str, TRUE);
274
275   return element;
276 #else
277   /* gst_parse_launch_full() will set a GST_CORE_ERROR_DISABLED error for us */
278   return gst_parse_launch_full ("", NULL, 0, error);
279 #endif
280 }
281
282 /**
283  * gst_parse_launch:
284  * @pipeline_description: the command line describing the pipeline
285  * @error: the error message in case of an erroneous pipeline.
286  *
287  * Create a new pipeline based on command line syntax.
288  * Please note that you might get a return value that is not %NULL even though
289  * the @error is set. In this case there was a recoverable parsing error and you
290  * can try to play the pipeline.
291  *
292  * Returns: (transfer floating): a new element on success, %NULL on failure. If
293  *    more than one toplevel element is specified by the @pipeline_description,
294  *   all elements are put into a #GstPipeline, which than is returned.
295  */
296 GstElement *
297 gst_parse_launch (const gchar * pipeline_description, GError ** error)
298 {
299   return gst_parse_launch_full (pipeline_description, NULL, GST_PARSE_FLAG_NONE,
300       error);
301 }
302
303 /**
304  * gst_parse_launch_full:
305  * @pipeline_description: the command line describing the pipeline
306  * @context: (allow-none): a parse context allocated with
307  *      gst_parse_context_new(), or %NULL
308  * @flags: parsing options, or #GST_PARSE_FLAG_NONE
309  * @error: the error message in case of an erroneous pipeline.
310  *
311  * Create a new pipeline based on command line syntax.
312  * Please note that you might get a return value that is not %NULL even though
313  * the @error is set. In this case there was a recoverable parsing error and you
314  * can try to play the pipeline.
315  *
316  * Returns: (transfer floating): a new element on success, %NULL on failure. If
317  *    more than one toplevel element is specified by the @pipeline_description,
318  *    all elements are put into a #GstPipeline, which then is returned (unless
319  *    the GST_PARSE_FLAG_PLACE_IN_BIN flag is set, in which case they are put
320  *    in a #GstBin instead).
321  */
322 GstElement *
323 gst_parse_launch_full (const gchar * pipeline_description,
324     GstParseContext * context, GstParseFlags flags, GError ** error)
325 {
326 #ifndef GST_DISABLE_PARSE
327   GstElement *element;
328   GError *myerror = NULL;
329
330   g_return_val_if_fail (pipeline_description != NULL, NULL);
331   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
332
333   GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description '%s'",
334       pipeline_description);
335
336   element = priv_gst_parse_launch (pipeline_description, &myerror, context,
337       flags);
338
339   /* don't return partially constructed pipeline if FATAL_ERRORS was given */
340   if (G_UNLIKELY (myerror != NULL && element != NULL)) {
341     if ((flags & GST_PARSE_FLAG_FATAL_ERRORS)) {
342       gst_object_unref (element);
343       element = NULL;
344     }
345   }
346
347   if (myerror)
348     g_propagate_error (error, myerror);
349
350   return element;
351 #else
352   gchar *msg;
353
354   GST_WARNING ("Disabled API called");
355
356   msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
357   g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
358   g_free (msg);
359
360   return NULL;
361 #endif
362 }