Original commit message from CVS:
[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  *
6  * gstparse.c: get a pipeline from a text pipeline description
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gstparse
26  * @short_description: Get a pipeline from a text pipeline description
27  *
28  * These function allow to create a pipeline based on the syntax used in the
29  * gst-launch utillity.
30  */
31
32
33 #include "gst_private.h"
34 #include <string.h>
35
36 #include "gstparse.h"
37 #include "gstinfo.h"
38
39 /* the need for the mutex will go away with flex 2.5.6 */
40 static gboolean flex_busy = FALSE;
41
42 #ifndef HAVE_MT_SAVE_FLEX
43 static GStaticRecMutex flex_lock = G_STATIC_REC_MUTEX_INIT;
44 #endif
45
46 extern GstElement *_gst_parse_launch (const gchar *, GError **);
47
48 /**
49  * gst_parse_error_quark:
50  *
51  * Get the error quark used by the parsing subsystem.
52  *
53  * Returns: the quark of the parse errors.
54  */
55 GQuark
56 gst_parse_error_quark (void)
57 {
58   static GQuark quark = 0;
59
60   if (!quark)
61     quark = g_quark_from_static_string ("gst_parse_error");
62   return quark;
63 }
64
65 static gchar *
66 _gst_parse_escape (const gchar * str)
67 {
68   GString *gstr = NULL;
69   gchar *newstr = NULL;
70
71   g_return_val_if_fail (str != NULL, NULL);
72
73   gstr = g_string_sized_new (strlen (str));
74
75   while (*str) {
76     if (*str == ' ')
77       g_string_append_c (gstr, '\\');
78     g_string_append_c (gstr, *str);
79     str++;
80   }
81
82   newstr = gstr->str;
83   g_string_free (gstr, FALSE);
84
85   return newstr;
86 }
87
88 /**
89  * gst_parse_launchv:
90  * @argv: null-terminated array of arguments
91  * @error: pointer to a #GError
92  *
93  * Create a new element based on command line syntax.
94  * @error will contain an error message if an erroneuos pipeline is specified.
95  * An error does not mean that the pipeline could not be constructed.
96  *
97  * Returns: a new element on success and %NULL on failure.
98  */
99 GstElement *
100 gst_parse_launchv (const gchar ** argv, GError ** error)
101 {
102   GstElement *element;
103   GString *str;
104   const gchar **argvp, *arg;
105   gchar *tmp;
106
107   g_return_val_if_fail (argv != NULL, NULL);
108
109   /* let's give it a nice size. */
110   str = g_string_sized_new (1024);
111
112   argvp = argv;
113   while (*argvp) {
114     arg = *argvp;
115     tmp = _gst_parse_escape (arg);
116     g_string_append (str, tmp);
117     g_free (tmp);
118     g_string_append (str, " ");
119     argvp++;
120   }
121
122   element = gst_parse_launch (str->str, error);
123
124   g_string_free (str, TRUE);
125
126   return element;
127 }
128
129 /**
130  * gst_parse_launch:
131  * @pipeline_description: the command line describing the pipeline
132  * @error: the error message in case of an erroneous pipeline.
133  *
134  * Create a new pipeline based on command line syntax.
135  * Please note that you might get a return value that is not %NULL even though
136  * the @error is set. In this case there was a recoverable parsing error and you
137  * can try to play the pipeline.
138  *
139  * Returns: a new element on success, %NULL on failure. If more than one toplevel
140  * element is specified by the @pipeline_description, all elements are put into
141  * a #GstPipeline, which than is returned.
142  */
143 GstElement *
144 gst_parse_launch (const gchar * pipeline_description, GError ** error)
145 {
146   GstElement *element;
147
148   g_return_val_if_fail (pipeline_description != NULL, NULL);
149
150   GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
151       pipeline_description);
152
153 #ifndef HAVE_MT_SAVE_FLEX
154   g_static_rec_mutex_lock (&flex_lock);
155 #endif
156   if (flex_busy)
157     goto recursive_call;
158   flex_busy = TRUE;
159
160   element = _gst_parse_launch (pipeline_description, error);
161
162   flex_busy = FALSE;
163 #ifndef HAVE_MT_SAVE_FLEX
164   g_static_rec_mutex_unlock (&flex_lock);
165 #endif
166
167   return element;
168
169   /* ERRORS */
170 recursive_call:
171   {
172     GST_WARNING ("calls to gst_parse_launch() cannot be nested");
173 #ifndef HAVE_MT_SAVE_FLEX
174     g_static_rec_mutex_unlock (&flex_lock);
175 #endif
176     g_warning ("calls to gst_parse_launch() cannot be nested");
177     return NULL;
178   }
179 }