Port gtk-doc comments to their equivalent markdown syntax
[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 static GstParseContext *
49 gst_parse_context_copy (const GstParseContext * context)
50 {
51   GstParseContext *ret = NULL;
52 #ifndef GST_DISABLE_PARSE
53
54   ret = gst_parse_context_new ();
55   if (context) {
56     GQueue missing_copy = G_QUEUE_INIT;
57     GList *l;
58
59     for (l = context->missing_elements; l != NULL; l = l->next)
60       g_queue_push_tail (&missing_copy, g_strdup ((const gchar *) l->data));
61
62     ret->missing_elements = missing_copy.head;
63   }
64 #endif
65   return ret;
66 }
67
68 G_DEFINE_BOXED_TYPE (GstParseContext, gst_parse_context,
69     (GBoxedCopyFunc) gst_parse_context_copy,
70     (GBoxedFreeFunc) gst_parse_context_free);
71
72 /**
73  * gst_parse_error_quark:
74  *
75  * Get the error quark used by the parsing subsystem.
76  *
77  * Returns: the quark of the parse errors.
78  */
79 GQuark
80 gst_parse_error_quark (void)
81 {
82   static GQuark quark = 0;
83
84   if (!quark)
85     quark = g_quark_from_static_string ("gst_parse_error");
86   return quark;
87 }
88
89
90 /**
91  * gst_parse_context_new:
92  *
93  * Allocates a parse context for use with gst_parse_launch_full() or
94  * gst_parse_launchv_full().
95  *
96  * Free-function: gst_parse_context_free
97  *
98  * Returns: (transfer full): a newly-allocated parse context. Free with
99  *     gst_parse_context_free() when no longer needed.
100  */
101 GstParseContext *
102 gst_parse_context_new (void)
103 {
104 #ifndef GST_DISABLE_PARSE
105   GstParseContext *ctx;
106
107   ctx = g_slice_new (GstParseContext);
108   ctx->missing_elements = NULL;
109
110   return ctx;
111 #else
112   return NULL;
113 #endif
114 }
115
116 /**
117  * gst_parse_context_free:
118  * @context: (transfer full): a #GstParseContext
119  *
120  * Frees a parse context previously allocated with gst_parse_context_new().
121  */
122 void
123 gst_parse_context_free (GstParseContext * context)
124 {
125 #ifndef GST_DISABLE_PARSE
126   if (context) {
127     g_list_foreach (context->missing_elements, (GFunc) g_free, NULL);
128     g_list_free (context->missing_elements);
129     g_slice_free (GstParseContext, context);
130   }
131 #endif
132 }
133
134 /**
135  * gst_parse_context_get_missing_elements:
136  * @context: a #GstParseContext
137  *
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.
141  *
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.
145  */
146 gchar **
147 gst_parse_context_get_missing_elements (GstParseContext * context)
148 {
149 #ifndef GST_DISABLE_PARSE
150   gchar **arr;
151   GList *l;
152   guint len, i;
153
154   g_return_val_if_fail (context != NULL, NULL);
155
156   len = g_list_length (context->missing_elements);
157
158   if (G_UNLIKELY (len == 0))
159     return NULL;
160
161   arr = g_new (gchar *, len + 1);
162
163   for (i = 0, l = context->missing_elements; l != NULL; l = l->next, ++i)
164     arr[i] = g_strdup (l->data);
165
166   arr[i] = NULL;
167
168   return arr;
169 #else
170   return NULL;
171 #endif
172 }
173
174 #ifndef GST_DISABLE_PARSE
175 static gchar *
176 _gst_parse_escape (const gchar * str)
177 {
178   GString *gstr = NULL;
179   gboolean in_quotes;
180
181   g_return_val_if_fail (str != NULL, NULL);
182
183   gstr = g_string_sized_new (strlen (str));
184
185   in_quotes = FALSE;
186
187   while (*str) {
188     if (*str == '"' && (!in_quotes || (in_quotes && *(str - 1) != '\\')))
189       in_quotes = !in_quotes;
190
191     if (*str == ' ' && !in_quotes)
192       g_string_append_c (gstr, '\\');
193
194     g_string_append_c (gstr, *str);
195     str++;
196   }
197
198   return g_string_free (gstr, FALSE);
199 }
200 #endif /* !GST_DISABLE_PARSE */
201
202 /**
203  * gst_parse_launchv:
204  * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
205  * @error: pointer to a #GError
206  *
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.
210  *
211  * Returns: (transfer floating): a new element on success and %NULL on failure.
212  */
213 GstElement *
214 gst_parse_launchv (const gchar ** argv, GError ** error)
215 {
216   return gst_parse_launchv_full (argv, NULL, GST_PARSE_FLAG_NONE, error);
217 }
218
219 /**
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)
226  *
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.
230  *
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)
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     GST_DEBUG ("escaping argument %s", arg);
256     tmp = _gst_parse_escape (arg);
257     g_string_append (str, tmp);
258     g_free (tmp);
259     g_string_append_c (str, ' ');
260     argvp++;
261   }
262
263   element = gst_parse_launch_full (str->str, context, flags, error);
264
265   g_string_free (str, TRUE);
266
267   return element;
268 #else
269   /* gst_parse_launch_full() will set a GST_CORE_ERROR_DISABLED error for us */
270   return gst_parse_launch_full ("", NULL, 0, error);
271 #endif
272 }
273
274 /**
275  * gst_parse_launch:
276  * @pipeline_description: the command line describing the pipeline
277  * @error: the error message in case of an erroneous pipeline.
278  *
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.
283  *
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.
287  */
288 GstElement *
289 gst_parse_launch (const gchar * pipeline_description, GError ** error)
290 {
291   return gst_parse_launch_full (pipeline_description, NULL, GST_PARSE_FLAG_NONE,
292       error);
293 }
294
295 /**
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.
302  *
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.
307  *
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).
313  */
314 GstElement *
315 gst_parse_launch_full (const gchar * pipeline_description,
316     GstParseContext * context, GstParseFlags flags, GError ** error)
317 {
318 #ifndef GST_DISABLE_PARSE
319   GstElement *element;
320   GError *myerror = NULL;
321
322   g_return_val_if_fail (pipeline_description != NULL, NULL);
323   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
324
325   GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description '%s'",
326       pipeline_description);
327
328   element = priv_gst_parse_launch (pipeline_description, &myerror, context,
329       flags);
330
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);
335       element = NULL;
336     }
337   }
338
339   if (myerror)
340     g_propagate_error (error, myerror);
341
342   return element;
343 #else
344   gchar *msg;
345
346   GST_WARNING ("Disabled API called");
347
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);
350   g_free (msg);
351
352   return NULL;
353 #endif
354 }