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"
32 typedef struct _gst_parse_priv gst_parse_priv;
33 struct _gst_parse_priv {
37 GHashTable *elementcounts;
42 typedef struct _gst_parse_delayed_pad gst_parse_delayed_pad;
43 struct _gst_parse_delayed_pad {
48 /* FIXME need to either revive this, or have pad->padtemplate connections in core
50 gst_parse_newpad(GstElement *element,GstPad *pad,launch_delayed_pad *peer)
52 gst_info("have NEW_PAD signal\n");
53 // if it matches, connect it
54 if (!strcmp(gst_pad_get_name(pad),peer->name)) {
55 gst_pad_connect(pad,peer->peer);
56 gst_info("delayed connect of '%s' to '%s'\n",
57 gst_pad_get_name(pad),gst_pad_get_name(peer->peer));
63 gst_parse_unique_name(gchar *type,gst_parse_priv *priv)
67 count = GPOINTER_TO_INT(g_hash_table_lookup(priv->elementcounts,type));
69 g_hash_table_insert(priv->elementcounts,type,GINT_TO_POINTER(count));
71 return g_strdup_printf("%s%d",type,count-1);
77 gst_parse_launch_cmdline(int argc,char *argv[],GstBin *parent,gst_parse_priv *priv)
81 GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
82 gchar closingchar = '\0';
85 gchar *sinkpadname = NULL, *srcpadname = NULL;
86 GstPad *sinkpad = NULL, *srcpad = NULL;
88 gint elementcount = 0;
93 if (GST_IS_PIPELINE(parent)) { closingchar = '\0';DEBUG("in pipeline "); }
94 else if (GST_IS_THREAD(parent)) { closingchar = '}';DEBUG("in thread "); }
95 else { closingchar = ')';DEBUG("in bin "); }
96 DEBUG_NOPREFIX("%s\n",gst_element_get_name (GST_ELEMENT (parent)));
100 // FIXME this is a lame solution for problems with the first parser
101 if (arg == NULL) { i++;continue; }
104 DEBUG("** ARGUMENT is '%s'\n",arg);
106 // a null that slipped through the reconstruction
108 DEBUG("random arg, FIXME\n");
112 // end of the container
113 } else if (arg[0] == closingchar) {
114 // time to finish off this bin
115 DEBUG("exiting container %s\n",gst_element_get_name (GST_ELEMENT (parent)));
120 } else if ((ptr = strchr(arg,'!'))) {
121 DEBUG("attempting to connect pads together....\n");
123 // if it starts with the !
126 // if there's a sinkpad...
128 sinkpadname = &arg[1];
132 srcpadname = g_strndup(arg,(ptr-arg));
133 // if there's a sinkpad
134 if (len > (ptr-arg)+1)
135 sinkpadname = &ptr[1];
140 GST_DEBUG(0,"have srcpad %s, sinkpad %s\n",srcpadname,sinkpadname);
144 // if the srcpadname doesn't have any commas in it, find an actual pad
145 if (!srcpadname || !strchr(srcpadname,',')) {
146 if (srcpadname != NULL) {
147 srcpad = gst_element_get_pad(previous,srcpadname);
149 GST_DEBUG(0,"NO SUCH pad %s in element %s\n",srcpadname,gst_element_get_name(previous));
152 if (srcpad == NULL) {
153 // check through the list to find the first sink pad
154 GST_DEBUG(0,"CHECKING through element %s for pad named %s\n",gst_element_get_name(previous),srcpadname);
155 pads = gst_element_get_pad_list(previous);
157 srcpad = GST_PAD(pads->data);
158 GST_DEBUG(0,"have pad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
159 if (GST_IS_GHOST_PAD(srcpad)) GST_DEBUG(0,"it's a ghost pad\n");
160 pads = g_list_next (pads);
161 if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) break;
166 if (!srcpad) GST_DEBUG(0,"error, can't find a src pad!!!\n");
167 else GST_DEBUG(0,"have src pad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
170 // argument with = in it
171 } else if (strstr(arg, "=")) {
174 gchar * pos = strstr(arg, "=");
175 // we have an argument
179 DEBUG("attempting to set argument '%s' to '%s' on element '%s'\n",
180 argname,argval,gst_element_get_name(previous));
181 gtk_object_set(GTK_OBJECT(previous),argname,argval,NULL);
184 // element or argument, or beginning of bin or thread
186 DEBUG("have element or bin/thread\n");
187 // if we have a bin or thread starting
188 if (strchr("({",arg[0])) {
190 // create a bin and add it to the current parent
191 element = gst_bin_new(g_strdup_printf("bin%d",priv->bincount++));
193 fprintf(stderr,"Couldn't create a bin!\n");
196 GST_DEBUG(0,"CREATED bin %s\n",gst_element_get_name(element));
197 } else if (arg[0] == '{') {
198 // create a thread and add it to the current parent
199 element = gst_thread_new(g_strdup_printf("thread%d",priv->threadcount++));
201 fprintf(stderr,"Couldn't create a thread!\n");
204 GST_DEBUG(0,"CREATED thread %s\n",gst_element_get_name(element));
207 i += gst_parse_launch_cmdline(argc - i, argv + i + 1, GST_BIN (element), priv);
210 // we have an element
211 DEBUG("attempting to create element '%s'\n",arg);
212 ptr = gst_parse_unique_name(arg,priv);
213 element = gst_elementfactory_make(arg,ptr);
216 fprintf(stderr,"Couldn't create a '%s', no such element or need to run gstraemer-register?\n",arg);
219 GST_DEBUG(0,"CREATED element %s\n",gst_element_get_name(element));
222 gst_bin_add (GST_BIN (parent), element);
225 if (srcpad != NULL) {
226 DEBUG("need to connect to sinkpad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
230 if (sinkpadname != NULL)
231 sinkpad = gst_element_get_pad(previous,sinkpadname);
234 // check through the list to find the first sink pad
235 pads = gst_element_get_pad_list(element);
237 sinkpad = GST_PAD(pads->data);
238 pads = g_list_next (pads);
239 if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) break;
244 if (!sinkpad) DEBUG("error, can't find a sink pad!!!\n");
245 else DEBUG("have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(sinkpad));
247 GST_DEBUG(0,"CONNECTING %s:%s and %s:%s\n",GST_DEBUG_PAD_NAME(srcpad),GST_DEBUG_PAD_NAME(sinkpad));
248 gst_pad_connect(srcpad,sinkpad);
254 // if we're the first element, ghost all the sinkpads
255 if (elementcount == 1) {
256 DEBUG("first element, ghosting all of %s's sink pads to parent %s\n",
257 gst_element_get_name(element),gst_element_get_name(GST_ELEMENT(parent)));
258 pads = gst_element_get_pad_list (element);
260 sinkpad = GST_PAD (pads->data);
261 pads = g_list_next (pads);
262 if (!sinkpad) DEBUG("much oddness, pad doesn't seem to exist\n");
263 else if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) {
264 gst_element_add_ghost_pad (GST_ELEMENT (parent), sinkpad,
265 g_strdup_printf("%s-ghost",gst_pad_get_name(sinkpad)));
266 GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
267 GST_DEBUG_PAD_NAME(sinkpad),gst_element_get_name(GST_ELEMENT(parent)),gst_pad_get_name(sinkpad));
273 if (!GST_IS_BIN(element)) prevelement = element;
279 // ghost all the src pads of the bin
280 if (prevelement != NULL) {
281 DEBUG("last element, ghosting all of %s's src pads to parent %s\n",
282 gst_element_get_name(prevelement),gst_element_get_name(GST_ELEMENT(parent)));
283 pads = gst_element_get_pad_list (prevelement);
285 srcpad = GST_PAD (pads->data);
286 pads = g_list_next (pads);
287 if (!srcpad) DEBUG("much oddness, pad doesn't seem to exist\n");
288 else if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) {
289 gst_element_add_ghost_pad (GST_ELEMENT (parent), srcpad,
290 g_strdup_printf("%s-ghost",gst_pad_get_name(srcpad)));
291 GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
292 GST_DEBUG_PAD_NAME(srcpad),gst_element_get_name(GST_ELEMENT(parent)),gst_pad_get_name(srcpad));
299 if (retval) return retval;
301 if (closingchar != '\0')
302 DEBUG("returning IN THE WRONG PLACE\n");
303 else DEBUG("ending pipeline\n");
309 * @cmdline: the command line describing the pipeline
310 * @parent: the parent bin for the resulting pipeline
312 * Create a new pipeline based on command line syntax.
317 gst_parse_launch(const gchar *cmdline,GstBin *parent)
326 priv.threadcount = 0;
328 priv.elementcounts = NULL;
329 priv.verbose = FALSE;
332 // first walk through quickly and see how many more slots we need
333 len = strlen(cmdline);
335 for (i=0;i<len;i++) {
336 // if it's a space, it denotes a new arg
337 if (cmdline[i] == ' ') newargc++;
338 // if it's a brace and isn't followed by a space, give it an arg
339 if (strchr("([{}])",cmdline[i])) {
340 // not followed by space, gets one
341 if (cmdline[i+1] != ' ') newargc++;
345 // now allocate the new argv array
346 argvn = g_new0(char *,newargc+1);
347 GST_DEBUG(0,"supposed to have %d args\n",newargc);
349 // now attempt to construct the new arg list
351 for (i=0;i<len+1;i++) {
352 // if it's a delimiter
353 if (strchr("([{}]) ",cmdline[i]) || (cmdline[i] == '\0')) {
354 // extract the previous arg
356 if (cmdline[k] == ' ') k++;
357 argvn[j] = g_new0(char,(i-k)+1);
358 memcpy(argvn[j],&cmdline[k],i-k);
361 if (strlen(argvn[j]) > 0) j++;
365 // if this is a bracket, construct a word
366 if ((cmdline[i] != ' ') && (cmdline[i] != '\0')) {
367 argvn[j++] = g_strdup_printf("%c",cmdline[i]);
374 for (i=0;i<newargc;i++) {
375 GST_DEBUG(0,"arg %d is: %s\n",i,argvn[i]);
378 // set up the elementcounts hash
379 priv.elementcounts = g_hash_table_new(g_str_hash,g_str_equal);
381 return gst_parse_launch_cmdline(newargc,argvn,parent,&priv);