check/pipelines/simple_launch_lines.c: test for pipe!=NULL
[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  * SECTION:gstparse
25  * @short_description: Get a pipeline from a text pipeline description
26  *
27  * These function allow to create a pipeline based on the syntax used in the
28  * gst-launch utillity.
29  */
30
31 #include <string.h>
32
33 #include "gst_private.h"
34
35 #include "gstparse.h"
36 #include "gstinfo.h"
37
38 extern GstElement *_gst_parse_launch (const gchar *, GError **);
39
40 GQuark
41 gst_parse_error_quark (void)
42 {
43   static GQuark quark = 0;
44
45   if (!quark)
46     quark = g_quark_from_static_string ("gst_parse_error");
47   return quark;
48 }
49
50 static gchar *
51 _gst_parse_escape (const gchar * str)
52 {
53   GString *gstr = NULL;
54   gchar *newstr = NULL;
55
56   g_return_val_if_fail (str != NULL, NULL);
57
58   gstr = g_string_sized_new (strlen (str));
59
60   while (*str) {
61     if (*str == ' ')
62       g_string_append_c (gstr, '\\');
63     g_string_append_c (gstr, *str);
64     str++;
65   }
66
67   newstr = gstr->str;
68   g_string_free (gstr, FALSE);
69
70   return newstr;
71 }
72
73 /**
74  * gst_parse_launchv:
75  * @argv: null-terminated array of arguments
76  * @error: pointer to a #GError
77  *
78  * Create a new element based on command line syntax.
79  * @error will contain an error message if an erroneuos pipeline is specified.
80  * An error does not mean that the pipeline could not be constructed.
81  *
82  * Returns: a new element on success and %NULL on failure.
83  */
84 GstElement *
85 gst_parse_launchv (const gchar ** argv, GError ** error)
86 {
87   GstElement *element;
88   GString *str;
89   const gchar **argvp, *arg;
90   gchar *tmp;
91
92   g_return_val_if_fail (argv != NULL, NULL);
93
94   /* let's give it a nice size. */
95   str = g_string_sized_new (1024);
96
97   argvp = argv;
98   while (*argvp) {
99     arg = *argvp;
100     tmp = _gst_parse_escape (arg);
101     g_string_append (str, tmp);
102     g_free (tmp);
103     g_string_append (str, " ");
104     argvp++;
105   }
106
107   element = gst_parse_launch (str->str, error);
108
109   g_string_free (str, TRUE);
110
111   return element;
112 }
113
114 /**
115  * gst_parse_launch:
116  * @pipeline_description: the command line describing the pipeline
117  * @error: the error message in case of an erroneous pipeline.
118  *
119  * Create a new pipeline based on command line syntax.
120  * Please note that you might get a return value that is not %NULL even though
121  * the @error is set. In this case there was a recoverable parsing error and you
122  * can try to play the pipeline.
123  *
124  * Returns: a new element on success, %NULL on failure. If more than one toplevel
125  * element is specified by the @pipeline_description, all elements are put into
126  * a #GstPipeline, which than is returned.
127  */
128 GstElement *
129 gst_parse_launch (const gchar * pipeline_description, GError ** error)
130 {
131   GstElement *element;
132   static GStaticMutex flex_lock = G_STATIC_MUTEX_INIT;
133
134   g_return_val_if_fail (pipeline_description != NULL, NULL);
135
136   GST_CAT_INFO (GST_CAT_PIPELINE, "parsing pipeline description %s",
137       pipeline_description);
138
139   /* the need for the mutex will go away with flex 2.5.6 */
140   g_static_mutex_lock (&flex_lock);
141   element = _gst_parse_launch (pipeline_description, error);
142   g_static_mutex_unlock (&flex_lock);
143
144   return element;
145 }