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...)
29 #include "gst_private.h"
31 #include "gstpipeline.h"
32 #include "gstthread.h"
34 typedef struct _gst_parse_priv gst_parse_priv;
35 struct _gst_parse_priv {
39 GHashTable *elementcounts;
44 typedef struct _gst_parse_delayed_pad gst_parse_delayed_pad;
45 struct _gst_parse_delayed_pad {
50 /* FIXME need to either revive this, or have pad->padtemplate connections in core
52 gst_parse_newpad(GstElement *element,GstPad *pad,launch_delayed_pad *peer)
54 gst_info("have NEW_PAD signal\n");
55 // if it matches, connect it
56 if (!strcmp(GST_PAD_NAME(pad),peer->name)) {
57 gst_pad_connect(pad,peer->peer);
58 gst_info("delayed connect of '%s' to '%s'\n",
59 GST_PAD_NAME(pad),GST_PAD_NAME(peer->peer));
65 gst_parse_unique_name(gchar *type,gst_parse_priv *priv)
69 count = GPOINTER_TO_INT(g_hash_table_lookup(priv->elementcounts,type));
71 g_hash_table_insert(priv->elementcounts,type,GINT_TO_POINTER(count));
73 return g_strdup_printf("%s%d",type,count-1);
79 gst_parse_launch_cmdline(int argc,char *argv[],GstBin *parent,gst_parse_priv *priv)
83 GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
84 gchar closingchar = '\0';
87 gchar *sinkpadname = NULL, *srcpadname = NULL;
88 GstPad *sinkpad = NULL, *srcpad = NULL;
90 gint elementcount = 0;
95 if (GST_IS_PIPELINE(parent)) { closingchar = '\0';DEBUG("in pipeline "); }
96 else if (GST_IS_THREAD(parent)) { closingchar = '}';DEBUG("in thread "); }
97 else { closingchar = ')';DEBUG("in bin "); }
98 DEBUG_NOPREFIX("%s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
102 // FIXME this is a lame solution for problems with the first parser
103 if (arg == NULL) { i++;continue; }
106 DEBUG("** ARGUMENT is '%s'\n",arg);
108 // a null that slipped through the reconstruction
110 DEBUG("random arg, FIXME\n");
114 // end of the container
115 } else if (arg[0] == closingchar) {
116 // time to finish off this bin
117 DEBUG("exiting container %s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
122 } else if ((ptr = strchr(arg,'!'))) {
123 DEBUG("attempting to connect pads together....\n");
125 // if it starts with the !
128 // if there's a sinkpad...
130 sinkpadname = &arg[1];
134 srcpadname = g_strndup(arg,(ptr-arg));
135 // if there's a sinkpad
136 if (len > (ptr-arg)+1)
137 sinkpadname = &ptr[1];
142 GST_DEBUG(0,"have srcpad %s, sinkpad %s\n",srcpadname,sinkpadname);
146 // if the srcpadname doesn't have any commas in it, find an actual pad
147 if (!srcpadname || !strchr(srcpadname,',')) {
148 if (srcpadname != NULL) {
149 srcpad = gst_element_get_pad(previous,srcpadname);
151 GST_DEBUG(0,"NO SUCH pad %s in element %s\n",srcpadname,GST_ELEMENT_NAME(previous));
154 if (srcpad == NULL) {
155 // check through the list to find the first sink pad
156 GST_DEBUG(0,"CHECKING through element %s for pad named %s\n",GST_ELEMENT_NAME(previous),srcpadname);
157 pads = gst_element_get_pad_list(previous);
159 srcpad = GST_PAD(pads->data);
160 GST_DEBUG(0,"have pad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
161 if (GST_IS_GHOST_PAD(srcpad)) GST_DEBUG(0,"it's a ghost pad\n");
162 pads = g_list_next (pads);
163 if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) break;
168 if (!srcpad) GST_DEBUG(0,"error, can't find a src pad!!!\n");
169 else GST_DEBUG(0,"have src pad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
172 // argument with = in it
173 } else if (strstr(arg, "=")) {
176 gchar * pos = strstr(arg, "=");
177 // we have an argument
181 DEBUG("attempting to set argument '%s' to '%s' on element '%s'\n",
182 argname,argval,GST_ELEMENT_NAME(previous));
183 gtk_object_set(GTK_OBJECT(previous),argname,argval,NULL);
186 // element or argument, or beginning of bin or thread
188 DEBUG("have element or bin/thread\n");
189 // if we have a bin or thread starting
190 if (strchr("({",arg[0])) {
192 // create a bin and add it to the current parent
193 element = gst_bin_new(g_strdup_printf("bin%d",priv->bincount++));
195 fprintf(stderr,"Couldn't create a bin!\n");
198 GST_DEBUG(0,"CREATED bin %s\n",GST_ELEMENT_NAME(element));
199 } else if (arg[0] == '{') {
200 // create a thread and add it to the current parent
201 element = gst_thread_new(g_strdup_printf("thread%d",priv->threadcount++));
203 fprintf(stderr,"Couldn't create a thread!\n");
206 GST_DEBUG(0,"CREATED thread %s\n",GST_ELEMENT_NAME(element));
209 i += gst_parse_launch_cmdline(argc - i, argv + i + 1, GST_BIN (element), priv);
212 // we have an element
213 DEBUG("attempting to create element '%s'\n",arg);
214 ptr = gst_parse_unique_name(arg,priv);
215 element = gst_elementfactory_make(arg,ptr);
218 fprintf(stderr,"Couldn't create a '%s', no such element or need to run gstraemer-register?\n",arg);
221 GST_DEBUG(0,"CREATED element %s\n",GST_ELEMENT_NAME(element));
224 gst_bin_add (GST_BIN (parent), element);
227 if (srcpad != NULL) {
228 DEBUG("need to connect to sinkpad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
232 if (sinkpadname != NULL)
233 sinkpad = gst_element_get_pad(previous,sinkpadname);
236 // check through the list to find the first sink pad
237 pads = gst_element_get_pad_list(element);
239 sinkpad = GST_PAD(pads->data);
240 pads = g_list_next (pads);
241 if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) break;
246 if (!sinkpad) DEBUG("error, can't find a sink pad!!!\n");
247 else DEBUG("have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(sinkpad));
249 GST_DEBUG(0,"CONNECTING %s:%s and %s:%s\n",GST_DEBUG_PAD_NAME(srcpad),GST_DEBUG_PAD_NAME(sinkpad));
250 gst_pad_connect(srcpad,sinkpad);
256 // if we're the first element, ghost all the sinkpads
257 if (elementcount == 1) {
258 DEBUG("first element, ghosting all of %s's sink pads to parent %s\n",
259 GST_ELEMENT_NAME(element),GST_ELEMENT_NAME(GST_ELEMENT(parent)));
260 pads = gst_element_get_pad_list (element);
262 sinkpad = GST_PAD (pads->data);
263 pads = g_list_next (pads);
264 if (!sinkpad) DEBUG("much oddness, pad doesn't seem to exist\n");
265 else if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) {
266 gst_element_add_ghost_pad (GST_ELEMENT (parent), sinkpad,
267 g_strdup_printf("%s-ghost",GST_PAD_NAME(sinkpad)));
268 GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
269 GST_DEBUG_PAD_NAME(sinkpad),GST_ELEMENT_NAME(GST_ELEMENT(parent)),GST_PAD_NAME(sinkpad));
275 if (!GST_IS_BIN(element)) prevelement = element;
281 // ghost all the src pads of the bin
282 if (prevelement != NULL) {
283 DEBUG("last element, ghosting all of %s's src pads to parent %s\n",
284 GST_ELEMENT_NAME(prevelement),GST_ELEMENT_NAME(GST_ELEMENT(parent)));
285 pads = gst_element_get_pad_list (prevelement);
287 srcpad = GST_PAD (pads->data);
288 pads = g_list_next (pads);
289 if (!srcpad) DEBUG("much oddness, pad doesn't seem to exist\n");
290 else if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) {
291 gst_element_add_ghost_pad (GST_ELEMENT (parent), srcpad,
292 g_strdup_printf("%s-ghost",GST_PAD_NAME(srcpad)));
293 GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
294 GST_DEBUG_PAD_NAME(srcpad),GST_ELEMENT_NAME (parent),GST_PAD_NAME(srcpad));
301 if (retval) return retval;
303 if (closingchar != '\0')
304 DEBUG("returning IN THE WRONG PLACE\n");
305 else DEBUG("ending pipeline\n");
311 * @cmdline: the command line describing the pipeline
312 * @parent: the parent bin for the resulting pipeline
314 * Create a new pipeline based on command line syntax.
319 gst_parse_launch(const gchar *cmdline,GstBin *parent)
328 priv.threadcount = 0;
330 priv.elementcounts = NULL;
331 priv.verbose = FALSE;
334 // first walk through quickly and see how many more slots we need
335 len = strlen(cmdline);
337 for (i=0;i<len;i++) {
338 // if it's a space, it denotes a new arg
339 if (cmdline[i] == ' ') newargc++;
340 // if it's a brace and isn't followed by a space, give it an arg
341 if (strchr("([{}])",cmdline[i])) {
342 // not followed by space, gets one
343 if (cmdline[i+1] != ' ') newargc++;
347 // now allocate the new argv array
348 argvn = g_new0(char *,newargc+1);
349 GST_DEBUG(0,"supposed to have %d args\n",newargc);
351 // now attempt to construct the new arg list
353 for (i=0;i<len+1;i++) {
354 // if it's a delimiter
355 if (strchr("([{}]) ",cmdline[i]) || (cmdline[i] == '\0')) {
356 // extract the previous arg
358 if (cmdline[k] == ' ') k++;
359 argvn[j] = g_new0(char,(i-k)+1);
360 memcpy(argvn[j],&cmdline[k],i-k);
363 if (strlen(argvn[j]) > 0) j++;
367 // if this is a bracket, construct a word
368 if ((cmdline[i] != ' ') && (cmdline[i] != '\0')) {
369 argvn[j++] = g_strdup_printf("%c",cmdline[i]);
376 for (i=0;i<newargc;i++) {
377 GST_DEBUG(0,"arg %d is: %s\n",i,argvn[i]);
380 // set up the elementcounts hash
381 priv.elementcounts = g_hash_table_new(g_str_hash,g_str_equal);
383 return gst_parse_launch_cmdline(newargc,argvn,parent,&priv);