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