gstdevicemonitor: added cleanup of signal handlers and hidden providers list
[platform/upstream/gstreamer.git] / subprojects / gstreamer / 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) (nullable): a newly-allocated parse context. Free
79  *     with 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) (nullable): A copied #GstParseContext
103  *
104  * Since: 1.12.1
105  */
106 GstParseContext *
107 gst_parse_context_copy (const GstParseContext * context)
108 {
109   GstParseContext *ret = NULL;
110 #ifndef GST_DISABLE_PARSE
111
112   ret = gst_parse_context_new ();
113   if (context) {
114     GQueue missing_copy = G_QUEUE_INIT;
115     GList *l;
116
117     for (l = context->missing_elements; l != NULL; l = l->next)
118       g_queue_push_tail (&missing_copy, g_strdup ((const gchar *) l->data));
119
120     ret->missing_elements = missing_copy.head;
121   }
122 #endif
123   return ret;
124 }
125
126 /**
127  * gst_parse_context_free:
128  * @context: (transfer full): a #GstParseContext
129  *
130  * Frees a parse context previously allocated with gst_parse_context_new().
131  */
132 void
133 gst_parse_context_free (GstParseContext * context)
134 {
135 #ifndef GST_DISABLE_PARSE
136   if (context) {
137     g_list_foreach (context->missing_elements, (GFunc) g_free, NULL);
138     g_list_free (context->missing_elements);
139     g_slice_free (GstParseContext, context);
140   }
141 #endif
142 }
143
144 /**
145  * gst_parse_context_get_missing_elements:
146  * @context: a #GstParseContext
147  *
148  * Retrieve missing elements from a previous run of gst_parse_launch_full()
149  * or gst_parse_launchv_full(). Will only return results if an error code
150  * of %GST_PARSE_ERROR_NO_SUCH_ELEMENT was returned.
151  *
152  * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*) (nullable): a
153  *     %NULL-terminated array of element factory name strings of missing
154  *     elements. Free with g_strfreev() when no longer needed.
155  */
156 gchar **
157 gst_parse_context_get_missing_elements (GstParseContext * context)
158 {
159 #ifndef GST_DISABLE_PARSE
160   gchar **arr;
161   GList *l;
162   guint len, i;
163
164   g_return_val_if_fail (context != NULL, NULL);
165
166   len = g_list_length (context->missing_elements);
167
168   if (G_UNLIKELY (len == 0))
169     return NULL;
170
171   arr = g_new (gchar *, len + 1);
172
173   for (i = 0, l = context->missing_elements; l != NULL; l = l->next, ++i)
174     arr[i] = g_strdup (l->data);
175
176   arr[i] = NULL;
177
178   return arr;
179 #else
180   return NULL;
181 #endif
182 }
183
184 #ifndef GST_DISABLE_PARSE
185 static gchar *
186 _gst_parse_escape (const gchar * str)
187 {
188   GString *gstr = NULL;
189   gboolean in_quotes;
190
191   g_return_val_if_fail (str != NULL, NULL);
192
193   gstr = g_string_sized_new (strlen (str));
194
195   in_quotes = FALSE;
196
197   while (*str) {
198     if (*str == '"' && (!in_quotes || *(str - 1) != '\\'))
199       in_quotes = !in_quotes;
200
201     if (*str == ' ' && !in_quotes)
202       g_string_append_c (gstr, '\\');
203
204     g_string_append_c (gstr, *str);
205     str++;
206   }
207
208   return g_string_free (gstr, FALSE);
209 }
210 #endif /* !GST_DISABLE_PARSE */
211
212 /**
213  * gst_parse_launchv:
214  * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
215  * @error: pointer to a #GError
216  *
217  * Create a new element based on command line syntax.
218  * @error will contain an error message if an erroneous pipeline is specified.
219  * An error does not mean that the pipeline could not be constructed.
220  *
221  * Returns: (transfer floating): a new element on success and %NULL
222  * on failure.
223  */
224 GstElement *
225 gst_parse_launchv (const gchar ** argv, GError ** error)
226 {
227   return gst_parse_launchv_full (argv, NULL, GST_PARSE_FLAG_NONE, error);
228 }
229
230 /**
231  * gst_parse_launchv_full:
232  * @argv: (in) (array zero-terminated=1): null-terminated array of arguments
233  * @context: (allow-none): a parse context allocated with
234  *     gst_parse_context_new(), or %NULL
235  * @flags: parsing options, or #GST_PARSE_FLAG_NONE
236  * @error: pointer to a #GError (which must be initialised to %NULL)
237  *
238  * Create a new element based on command line syntax.
239  * @error will contain an error message if an erroneous pipeline is specified.
240  * An error does not mean that the pipeline could not be constructed.
241  *
242  * Returns: (transfer floating): a new element on success; on
243  *   failure, either %NULL or a partially-constructed bin or element will be
244  *   returned and @error will be set (unless you passed
245  *   #GST_PARSE_FLAG_FATAL_ERRORS in @flags, then %NULL will always be returned
246  *   on failure)
247  */
248 GstElement *
249 gst_parse_launchv_full (const gchar ** argv, GstParseContext * context,
250     GstParseFlags flags, GError ** error)
251 {
252 #ifndef GST_DISABLE_PARSE
253   GstElement *element;
254   GString *str;
255   const gchar **argvp, *arg;
256   gchar *tmp;
257
258   g_return_val_if_fail (argv != NULL, NULL);
259   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
260
261   /* let's give it a nice size. */
262   str = g_string_sized_new (1024);
263
264   argvp = argv;
265   while (*argvp) {
266     arg = *argvp;
267     GST_DEBUG ("escaping argument %s", arg);
268     tmp = _gst_parse_escape (arg);
269     g_string_append (str, tmp);
270     g_free (tmp);
271     g_string_append_c (str, ' ');
272     argvp++;
273   }
274
275   element = gst_parse_launch_full (str->str, context, flags, error);
276
277   g_string_free (str, TRUE);
278
279   return element;
280 #else
281   /* gst_parse_launch_full() will set a GST_CORE_ERROR_DISABLED error for us */
282   return gst_parse_launch_full ("", NULL, 0, error);
283 #endif
284 }
285
286 /**
287  * gst_parse_launch:
288  * @pipeline_description: the command line describing the pipeline
289  * @error: the error message in case of an erroneous pipeline.
290  *
291  * Create a new pipeline based on command line syntax.
292  * Please note that you might get a return value that is not %NULL even though
293  * the @error is set. In this case there was a recoverable parsing error and you
294  * can try to play the pipeline.
295  *
296  * To create a sub-pipeline (bin) for embedding into an existing pipeline
297  * use gst_parse_bin_from_description().
298  *
299  * Returns: (transfer floating): a new element on success, %NULL on
300  *   failure. If more than one toplevel element is specified by the
301  *   @pipeline_description, all elements are put into a #GstPipeline, which
302  *   than is returned.
303  */
304 GstElement *
305 gst_parse_launch (const gchar * pipeline_description, GError ** error)
306 {
307   return gst_parse_launch_full (pipeline_description, NULL, GST_PARSE_FLAG_NONE,
308       error);
309 }
310
311 /**
312  * gst_parse_launch_full:
313  * @pipeline_description: the command line describing the pipeline
314  * @context: (allow-none): a parse context allocated with
315  *      gst_parse_context_new(), or %NULL
316  * @flags: parsing options, or #GST_PARSE_FLAG_NONE
317  * @error: the error message in case of an erroneous pipeline.
318  *
319  * Create a new pipeline based on command line syntax.
320  * Please note that you might get a return value that is not %NULL even though
321  * the @error is set. In this case there was a recoverable parsing error and you
322  * can try to play the pipeline.
323  *
324  * To create a sub-pipeline (bin) for embedding into an existing pipeline
325  * use gst_parse_bin_from_description_full().
326  *
327  * Returns: (transfer floating): a new element on success, %NULL on
328  *    failure. If more than one toplevel element is specified by the
329  *    @pipeline_description, all elements are put into a #GstPipeline, which
330  *    then is returned (unless the GST_PARSE_FLAG_PLACE_IN_BIN flag is set, in
331  *    which case they are put in a #GstBin instead).
332  */
333 GstElement *
334 gst_parse_launch_full (const gchar * pipeline_description,
335     GstParseContext * context, GstParseFlags flags, GError ** error)
336 {
337 #ifndef GST_DISABLE_PARSE
338   GstElement *element;
339   GError *myerror = NULL;
340
341   g_return_val_if_fail (pipeline_description != NULL, NULL);
342   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
343
344   GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description '%s'",
345       pipeline_description);
346
347   element = priv_gst_parse_launch (pipeline_description, &myerror, context,
348       flags);
349
350   /* don't return partially constructed pipeline if FATAL_ERRORS was given */
351   if (G_UNLIKELY (myerror != NULL && element != NULL)) {
352     if ((flags & GST_PARSE_FLAG_FATAL_ERRORS)) {
353       gst_object_unref (element);
354       element = NULL;
355     }
356   }
357
358   if (myerror)
359     g_propagate_error (error, myerror);
360
361   return element;
362 #else
363   gchar *msg;
364
365   GST_WARNING ("Disabled API called");
366
367   msg = gst_error_get_message (GST_CORE_ERROR, GST_CORE_ERROR_DISABLED);
368   g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_DISABLED, "%s", msg);
369   g_free (msg);
370
371   return NULL;
372 #endif
373 }