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