2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #define DEBUG(format,args...)
24 #define DEBUG_NOPREFIX(format,args...)
25 #define VERBOSE(format,args...)
31 typedef struct _gst_parse_priv gst_parse_priv;
32 struct _gst_parse_priv {
36 GHashTable *elementcounts;
41 typedef struct _gst_parse_delayed_pad gst_parse_delayed_pad;
42 struct _gst_parse_delayed_pad {
49 gst_parse_newpad(GstElement *element,GstPad *pad,launch_delayed_pad *peer)
51 gst_info("have NEW_PAD signal\n");
52 // if it matches, connect it
53 if (!strcmp(gst_pad_get_name(pad),peer->name)) {
54 gst_pad_connect(pad,peer->peer);
55 gst_info("delayed connect of '%s' to '%s'\n",
56 gst_pad_get_name(pad),gst_pad_get_name(peer->peer));
62 gst_parse_unique_name(gchar *type,gst_parse_priv *priv)
66 count = GPOINTER_TO_INT(g_hash_table_lookup(priv->elementcounts,type));
68 g_hash_table_insert(priv->elementcounts,type,GINT_TO_POINTER(count));
70 return g_strdup_printf("%s%d",type,count-1);
76 gst_parse_launch_cmdline(int argc,char *argv[],GstBin *parent,gst_parse_priv *priv)
80 GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
81 gchar closingchar = '\0';
84 gchar *sinkpadname = NULL, *srcpadname = NULL;
85 GstPad *sinkpad = NULL, *srcpad = NULL;
87 gint elementcount = 0;
92 if (GST_IS_PIPELINE(parent)) { closingchar = '\0';DEBUG("in pipeline "); }
93 else if (GST_IS_THREAD(parent)) { closingchar = '}';DEBUG("in thread "); }
94 else { closingchar = ')';DEBUG("in bin "); }
95 DEBUG_NOPREFIX("%s\n",gst_element_get_name (GST_ELEMENT (parent)));
99 // FIXME this is a lame solution for problems with the first parser
100 if (arg == NULL) { i++;continue; }
103 DEBUG("** ARGUMENT is '%s'\n",arg);
105 // a null that slipped through the reconstruction
107 DEBUG("random arg, FIXME\n");
111 // end of the container
112 } else if (arg[0] == closingchar) {
113 // time to finish off this bin
114 DEBUG("exiting container %s\n",gst_element_get_name (GST_ELEMENT (parent)));
119 } else if ((ptr = strchr(arg,'!'))) {
120 DEBUG("attempting to connect pads together....\n");
122 // if it starts with the !
125 // if there's a sinkpad...
127 sinkpadname = &arg[1];
131 srcpadname = g_strndup(arg,(ptr-arg));
132 // if there's a sinkpad
133 if (len > (ptr-arg)+1)
134 sinkpadname = &ptr[1];
139 DEBUG("have sinkpad %s, srcpad %s\n",sinkpadname,srcpadname);
143 // if the srcpadname doesn't have any commas in it, find an actual pad
144 if (!srcpadname || !strchr(srcpadname,',')) {
145 if (srcpadname != NULL) {
146 srcpad = gst_element_get_pad(previous,srcpadname);
148 VERBOSE("NO SUCH pad %s in element %s\n",srcpadname,gst_element_get_name(previous));
151 if (srcpad == NULL) {
152 // check through the list to find the first sink pad
153 pads = gst_element_get_pad_list(previous);
155 srcpad = GST_PAD(pads->data);
156 pads = g_list_next (pads);
157 if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) break;
162 if (!srcpad) DEBUG("error, can't find a src pad!!!\n");
163 else DEBUG("have src pad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
166 // argument with = in it
167 } else if (strstr(arg, "=")) {
170 gchar * pos = strstr(arg, "=");
171 // we have an argument
175 DEBUG("attempting to set argument '%s' to '%s' on element '%s'\n",
176 argname,argval,gst_element_get_name(previous));
177 gtk_object_set(GTK_OBJECT(previous),argname,argval,NULL);
180 // element or argument, or beginning of bin or thread
182 DEBUG("have element or bin/thread\n");
183 // if we have a bin or thread starting
184 if (strchr("({",arg[0])) {
186 // create a bin and add it to the current parent
187 element = gst_bin_new(g_strdup_printf("bin%d",priv->bincount++));
189 fprintf(stderr,"Couldn't create a bin!\n");
192 VERBOSE("CREATED bin %s\n",gst_element_get_name(element));
193 } else if (arg[0] == '{') {
194 // create a thread and add it to the current parent
195 element = gst_thread_new(g_strdup_printf("thread%d",priv->threadcount++));
197 fprintf(stderr,"Couldn't create a thread!\n");
200 VERBOSE("CREATED thread %s\n",gst_element_get_name(element));
203 i += gst_parse_launch_cmdline(argc - i, argv + i + 1, GST_BIN (element), priv);
206 // we have an element
207 DEBUG("attempting to create element '%s'\n",arg);
208 element = gst_elementfactory_make(arg,gst_parse_unique_name(arg,priv));
210 fprintf(stderr,"Couldn't create a '%s', no such element or need to run gstraemer-register?\n",arg);
213 VERBOSE("CREATED element %s\n",gst_element_get_name(element));
214 DEBUG("created element %s\n",gst_element_get_name(element));
217 gst_bin_add (GST_BIN (parent), element);
220 if (srcpad != NULL) {
221 DEBUG("need to connect to sinkpad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
225 if (sinkpadname != NULL)
226 sinkpad = gst_element_get_pad(previous,sinkpadname);
229 // check through the list to find the first sink pad
230 pads = gst_element_get_pad_list(element);
232 sinkpad = GST_PAD(pads->data);
233 pads = g_list_next (pads);
234 if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) break;
239 if (!sinkpad) DEBUG("error, can't find a sink pad!!!\n");
240 else DEBUG("have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(sinkpad));
242 VERBOSE("CONNECTING %s:%s and %s:%s\n",GST_DEBUG_PAD_NAME(srcpad),GST_DEBUG_PAD_NAME(sinkpad));
243 gst_pad_connect(srcpad,sinkpad);
249 // if we're the first element, ghost all the sinkpads
250 if (elementcount == 1) {
251 DEBUG("first element, ghosting all of %s's sink pads to parent %s\n",
252 gst_element_get_name(element),gst_element_get_name(GST_ELEMENT(parent)));
253 pads = gst_element_get_pad_list (element);
255 sinkpad = GST_PAD (pads->data);
256 pads = g_list_next (pads);
257 if (!sinkpad) DEBUG("much oddness, pad doesn't seem to exist\n");
258 else if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) {
259 gst_element_add_ghost_pad (GST_ELEMENT (parent), sinkpad);
260 DEBUG("ghosted %s:%s\n",GST_DEBUG_PAD_NAME(sinkpad));
266 if (!GST_IS_BIN(element)) prevelement = element;
272 // ghost all the src pads of the bin
273 if (prevelement != NULL) {
274 DEBUG("last element, ghosting all of %s's src pads to parent %s\n",
275 gst_element_get_name(prevelement),gst_element_get_name(GST_ELEMENT(parent)));
276 pads = gst_element_get_pad_list (prevelement);
278 srcpad = GST_PAD (pads->data);
279 pads = g_list_next (pads);
280 if (!srcpad) DEBUG("much oddness, pad doesn't seem to exist\n");
281 else if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) {
282 gst_element_add_ghost_pad (GST_ELEMENT (parent), srcpad);
283 DEBUG("ghosted %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
290 if (retval) return retval;
292 if (closingchar != '\0')
293 DEBUG("returning IN THE WRONG PLACE\n");
294 else DEBUG("ending pipeline\n");
298 gint gst_parse_launch(const gchar *cmdline,GstBin *parent) {
306 priv.threadcount = 0;
308 priv.elementcounts = NULL;
309 priv.verbose = FALSE;
312 // first walk through quickly and see how many more slots we need
313 len = strlen(cmdline);
315 for (i=0;i<len;i++) {
316 // if it's a space, it denotes a new arg
317 if (cmdline[i] == ' ') newargc++;
318 // if it's a brace and isn't followed by a space, give it an arg
319 if (strchr("([{}])",cmdline[i])) {
320 // not followed by space, gets one
321 if (cmdline[i+1] != ' ') newargc++;
325 // now allocate the new argv array
326 argvn = g_new0(char *,newargc+1);
327 DEBUG("supposed to have %d args\n",newargc);
329 // now attempt to construct the new arg list
331 for (i=0;i<len+1;i++) {
332 // if it's a delimiter
333 if (strchr("([{}]) ",cmdline[i]) || (cmdline[i] == '\0')) {
334 // extract the previous arg
336 if (cmdline[k] == ' ') k++;
337 argvn[j] = g_new0(char,(i-k)+1);
338 memcpy(argvn[j],&cmdline[k],i-k);
341 if (strlen(argvn[j]) > 0) j++;
345 // if this is a bracket, construct a word
346 if ((cmdline[i] != ' ') && (cmdline[i] != '\0')) {
347 argvn[j++] = g_strdup_printf("%c",cmdline[i]);
354 for (i=0;i<newargc;i++) {
355 DEBUG("arg %d is: %s\n",i,argvn[i]);
358 // set up the elementcounts hash
359 priv.elementcounts = g_hash_table_new(g_str_hash,g_str_equal);
361 return gst_parse_launch_cmdline(newargc,argvn,parent,&priv);