Merge branch 'master' into 0.11
[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., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 /**
26  * SECTION:gstparse
27  * @short_description: Get a pipeline from a text pipeline description
28  *
29  * These function allow to create a pipeline based on the syntax used in the
30  * gst-launch utility (see man-page for syntax documentation).
31  *
32  * Please note that these functions take several measures to create
33  * somewhat dynamic pipelines. Due to that such pipelines are not always
34  * reuseable (set the state to NULL and back to PLAYING).
35  */
36
37 #include "gst_private.h"
38 #include <string.h>
39
40 #include "gstparse.h"
41 #include "gsterror.h"
42 #include "gstinfo.h"
43 #ifndef GST_DISABLE_PARSE
44 #include "parse/types.h"
45 #endif
46
47 /**
48  * gst_parse_error_quark:
49  *
50  * Get the error quark used by the parsing subsystem.
51  *
52  * Returns: the quark of the parse errors.
53  */
54 GQuark
55 gst_parse_error_quark (void)
56 {
57   static GQuark quark = 0;
58
59   if (!quark)
60     quark = g_quark_from_static_string ("gst_parse_error");
61   return quark;
62 }
63
64
65 /**
66  * gst_parse_context_new:
67  *
68  * Allocates a parse context for use with gst_parse_launch_full() or
69  * gst_parse_launchv_full().
70  *
71  * Free-function: gst_parse_context_free
72  *
73  * Returns: (transfer full): a newly-allocated parse context. Free with
74  *     gst_parse_context_free() when no longer needed.
75  *
76  * Since: 0.10.20
77  */
78 GstParseContext *
79 gst_parse_context_new (void)
80 {
81 #ifndef GST_DISABLE_PARSE
82   GstParseContext *ctx;
83
84   ctx = g_slice_new (GstParseContext);
85   ctx->missing_elements = NULL;
86
87   return ctx;
88 #else
89   return NULL;
90 #endif
91 }
92
93 /**
94  * gst_parse_context_free:
95  * @context: (transfer full): a #GstParseContext
96  *
97  * Frees a parse context previously allocated with gst_parse_context_new().
98  *
99  * Since: 0.10.20
100  */
101 void
102 gst_parse_context_free (GstParseContext * context)
103 {
104 #ifndef GST_DISABLE_PARSE
105   if (context) {
106     g_list_foreach (context->missing_elements, (GFunc) g_free, NULL);
107     g_list_free (context->missing_elements);
108     g_slice_free (GstParseContext, context);
109   }
110 #endif
111 }
112
113 /**
114  * gst_parse_context_get_missing_elements:
115  * @context: a #GstParseContext
116  *
117  * Retrieve missing elements from a previous run of gst_parse_launch_full()
118  * or gst_parse_launchv_full(). Will only return results if an error code
119  * of %GST_PARSE_ERROR_NO_SUCH_ELEMENT was returned.
120  *
121  * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*): a
122  *     NULL-terminated array of element factory name strings of missing
123  *     elements. Free with g_strfreev() when no longer needed.
124  *
125  * Since: 0.10.20
126  */
127 gchar **
128 gst_parse_context_get_missing_elements (GstParseContext * context)
129 {
130 #ifndef GST_DISABLE_PARSE
131   gchar **arr;
132   GList *l;
133   guint len, i;
134
135   g_return_val_if_fail (context != NULL, NULL);
136
137   len = g_list_length (context->missing_elements);
138
139   if (G_UNLIKELY (len == 0))
140     return NULL;
141
142   arr = g_new (gchar *, len + 1);
143
144   for (i = 0, l = context->missing_elements; l != NULL; l = l->next, ++i)
145     arr[i] = g_strdup (l->data);
146
147   arr[i] = NULL;
148
149   return arr;
150 #else
151   return NULL;
152 #endif
153 }
154
155 #ifndef GST_DISABLE_PARSE
156 static gchar *
157 _gst_parse_escape (const gchar * str)
158 {
159   GString *gstr = NULL;
160
161   g_return_val_if_fail (str != NULL, NULL);
162
163   gstr = g_string_sized_new (strlen (str));
164
165   while (*str) {
166     if (*str == ' ')
167       g_string_append_c (gstr, '\\');
168     g_string_append_c (gstr, *str);
169     str++;
170   }
171
172   return g_string_free (gstr, FALSE);
173 }
174 #endif /* !GST_DISABLE_PARSE */
175
176 /**
177  * gst_parse_launchv:
178  * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
179  * @error: pointer to a #GError
180  *
181  * Create a new element based on command line syntax.
182  * @error will contain an error message if an erroneuos pipeline is specified.
183  * An error does not mean that the pipeline could not be constructed.
184  *
185  * Returns: (transfer full): a new element on success and %NULL on failure.
186  */
187 GstElement *
188 gst_parse_launchv (const gchar ** argv, GError ** error)
189 {
190   return gst_parse_launchv_full (argv, NULL, 0, error);
191 }
192
193 /**
194  * gst_parse_launchv_full:
195  * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
196  * @context: (allow-none): a parse context allocated with
197  *     gst_parse_context_new(), or %NULL
198  * @flags: parsing options, or #GST_PARSE_FLAG_NONE
199  * @error: pointer to a #GError (which must be initialised to %NULL)
200  *
201  * Create a new element based on command line syntax.
202  * @error will contain an error message if an erroneous pipeline is specified.
203  * An error does not mean that the pipeline could not be constructed.
204  *
205  * Returns: (transfer full): a new element on success; on failure, either %NULL
206  *   or a partially-constructed bin or element will be returned and @error will
207  *   be set (unless you passed #GST_PARSE_FLAG_FATAL_ERRORS in @flags, then
208  *   %NULL will always be returned on failure)
209  *
210  * Since: 0.10.20
211  */
212 GstElement *
213 gst_parse_launchv_full (const gchar ** argv, GstParseContext * context,
214     GstParseFlags flags, GError ** error)
215 {
216 #ifndef GST_DISABLE_PARSE
217   GstElement *element;
218   GString *str;
219   const gchar **argvp, *arg;
220   gchar *tmp;
221
222   g_return_val_if_fail (argv != NULL, NULL);
223   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
224
225   /* let's give it a nice size. */
226   str = g_string_sized_new (1024);
227
228   argvp = argv;
229   while (*argvp) {
230     arg = *argvp;
231     tmp = _gst_parse_escape (arg);
232     g_string_append (str, tmp);
233     g_free (tmp);
234     g_string_append_c (str, ' ');
235     argvp++;
236   }
237
238   element = gst_parse_launch_full (str->str, context, flags, error);
239
240   g_string_free (str, TRUE);
241
242   return element;
243 #else
244   /* gst_parse_launch_full() will set a GST_CORE_ERROR_DISABLED error for us */
245   return gst_parse_launch_full ("", NULL, 0, error);
246 #endif
247 }
248
249 /**
250  * gst_parse_launch:
251  * @pipeline_description: the command line describing the pipeline
252  * @error: the error message in case of an erroneous pipeline.
253  *
254  * Create a new pipeline based on command line syntax.
255  * Please note that you might get a return value that is not %NULL even though
256  * the @error is set. In this case there was a recoverable parsing error and you
257  * can try to play the pipeline.
258  *
259  * Returns: (transfer full): a new element on success, %NULL on failure. If
260  *    more than one toplevel element is specified by the @pipeline_description,
261  *   all elements are put into a #GstPipeline, which than is returned.
262  */
263 GstElement *
264 gst_parse_launch (const gchar * pipeline_description, GError ** error)
265 {
266   return gst_parse_launch_full (pipeline_description, NULL, 0, error);
267 }
268
269 /**
270  * gst_parse_launch_full:
271  * @pipeline_description: the command line describing the pipeline
272  * @context: (allow-none): a parse context allocated with
273  *      gst_parse_context_new(), or %NULL
274  * @flags: parsing options, or #GST_PARSE_FLAG_NONE
275  * @error: the error message in case of an erroneous pipeline.
276  *
277  * Create a new pipeline based on command line syntax.
278  * Please note that you might get a return value that is not %NULL even though
279  * the @error is set. In this case there was a recoverable parsing error and you
280  * can try to play the pipeline.
281  *
282  * Returns: (transfer full): a new element on success, %NULL on failure. If
283  *    more than one toplevel element is specified by the @pipeline_description,
284  *    all elements are put into a #GstPipeline, which then is returned.
285  *
286  * Since: 0.10.20
287  */
288 GstElement *
289 gst_parse_launch_full (const gchar * pipeline_description,
290     GstParseContext * context, GstParseFlags flags, GError ** error)
291 {
292 #ifndef GST_DISABLE_PARSE
293   GstElement *element;
294
295   g_return_val_if_fail (pipeline_description != NULL, NULL);
296   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
297
298   GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description '%s'",
299       pipeline_description);
300
301   element = _gst_parse_launch (pipeline_description, error, context, flags);
302
303   /* don't return partially constructed pipeline if FATAL_ERRORS was given */
304   if (G_UNLIKELY (error != NULL && *error != NULL && element != NULL)) {
305     if ((flags & GST_PARSE_FLAG_FATAL_ERRORS)) {
306       gst_object_unref (element);
307       element = NULL;
308     }
309   }
310
311   return element;
312 #else
313   gchar *msg;
314
315   GST_WARNING ("Disabled API called");
316
317   msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
318   g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
319   g_free (msg);
320
321   return NULL;
322 #endif
323 }