rewritten pipeline parsing lands. Have fun breaking it.
[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 #include <string.h>
25
26 #include "gstparse.h"
27 #include "gstinfo.h"
28 #include "gstlog.h"
29
30 extern GstElement *_gst_parse_launch (const gchar *, GError **);
31
32 GQuark 
33 gst_parse_error_quark (void)
34 {
35   static GQuark quark = 0;
36   if (!quark)
37     quark = g_quark_from_static_string ("gst_parse_error");
38   return quark;
39 }
40
41 static gchar *_gst_parse_escape (const gchar *str)
42 {
43   GString *gstr = NULL;
44   
45   g_return_val_if_fail (str != NULL, NULL);
46   
47   gstr = g_string_sized_new (strlen (str));
48   
49   while (*str) {
50     if (*str == ' ')
51       g_string_append_c (gstr, '\\');
52     g_string_append_c (gstr, *str);
53     str++;
54   }
55   
56   return gstr->str;
57 }
58 /**
59  * gst_parse_launchv:
60  * @argv: null-terminated array of arguments
61  * @error: pointer to a #GError
62  *
63  * Create a new element based on command line syntax.
64  * #error will contain an error message if pipeline creation fails and can
65  * contain an error message, when a recoverable error happened.
66  *
67  * Returns: a new element on success and NULL on failure.
68  */
69 GstElement *
70 gst_parse_launchv (const gchar **argv, GError **error)
71 {
72   GstElement *element;
73   GString *str;
74   const gchar **argvp, *arg;
75   gchar *tmp;
76
77   g_return_val_if_fail (argv != NULL, NULL);
78
79   /* let's give it a nice size. */
80   str = g_string_sized_new (1024);
81
82   argvp = argv;
83   while (*argvp) {
84     arg = *argvp;
85     tmp = _gst_parse_escape (arg);
86     g_string_append (str, tmp);
87     g_free (tmp);
88     g_string_append (str, " ");
89     argvp++;
90   }
91   
92   element = gst_parse_launch (str->str, error);
93
94   g_string_free (str, TRUE);
95
96   return element;
97 }
98
99 /**
100  * gst_parse_launch:
101  * @pipeline_description: the command line describing the pipeline
102  * @error: the error message in case of a failure
103  *
104  * Create a new pipeline based on command line syntax.
105  *
106  * Returns: a new bin on success, NULL on failure. By default the bin is
107  * a GstPipeline, but it depends on the pipeline_description.
108  */
109 GstElement *
110 gst_parse_launch (const gchar * pipeline_description, GError **error)
111 {
112   GstElement *element;
113   static GStaticMutex flex_lock = G_STATIC_MUTEX_INIT;
114
115   g_return_val_if_fail (pipeline_description != NULL, NULL);
116
117   GST_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
118             pipeline_description);
119
120   /* the need for the mutex will go away with flex 2.5.6 */
121   g_static_mutex_lock (&flex_lock);
122   element = _gst_parse_launch (pipeline_description, error);
123   g_static_mutex_unlock (&flex_lock);
124
125   return element;
126 }