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...)
27 #define GST_PARSE_LISTPAD(list) ((GstPad*)(list->data))
31 #include "gst_private.h"
33 #include "gstpipeline.h"
34 #include "gstthread.h"
37 typedef struct _gst_parse_priv gst_parse_priv;
38 struct _gst_parse_priv {
42 GHashTable *elementcounts;
47 typedef struct _gst_parse_delayed_pad gst_parse_delayed_pad;
48 struct _gst_parse_delayed_pad {
53 /* FIXME need to either revive this, or have pad->padtemplate connections in core
55 gst_parse_newpad(GstElement *element,GstPad *pad,launch_delayed_pad *peer)
57 gst_info("have NEW_PAD signal\n");
58 // if it matches, connect it
59 if (!strcmp(GST_PAD_NAME(pad),peer->name)) {
60 gst_pad_connect(pad,peer->peer);
61 gst_info("delayed connect of '%s' to '%s'\n",
62 GST_PAD_NAME(pad),GST_PAD_NAME(peer->peer));
72 static void have_eos (void)
74 DEBUG ("I have eos on the first element\n");
79 dynamic_connect (GstElement *element, GstPad *newpad, gpointer data)
81 dyn_connect *connect = (dyn_connect *)data;
83 if (!strcmp (gst_pad_get_name (newpad), connect->srcpadname)) {
84 gst_pad_connect (newpad, connect->target);
89 gst_parse_unique_name(gchar *type,gst_parse_priv *priv)
93 count = GPOINTER_TO_INT(g_hash_table_lookup(priv->elementcounts,type));
95 g_hash_table_insert(priv->elementcounts,type,GINT_TO_POINTER(count));
97 return g_strdup_printf("%s%d",type,count-1);
103 gst_parse_launch_cmdline(int argc,char *argv[],GstBin *parent,gst_parse_priv *priv)
107 GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
108 gchar closingchar = '\0';
111 gchar *sinkpadname = NULL, *srcpadname = NULL, *tempname;
113 GSList *sinkpads = NULL, *srcpads = NULL;
114 gint numsrcpads = 0, numsinkpads = 0;
116 gint elementcount = 0;
121 if (GST_IS_PIPELINE(parent)) { closingchar = '\0';DEBUG("in pipeline "); }
122 else if (GST_IS_THREAD(parent)) { closingchar = '}';DEBUG("in thread "); }
123 else { closingchar = ')';DEBUG("in bin "); }
124 DEBUG_NOPREFIX("%s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
128 // FIXME this is a lame solution for problems with the first parser
129 if (arg == NULL) { i++;continue; }
132 DEBUG("** ARGUMENT is '%s'\n",arg);
134 // a null that slipped through the reconstruction
136 DEBUG("random arg, FIXME\n");
140 // end of the container
141 } else if (arg[0] == closingchar) {
142 // time to finish off this bin
143 DEBUG("exiting container %s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
148 } else if ((ptr = strchr(arg,'!'))) {
149 DEBUG("attempting to connect pads together....\n");
151 // if it starts with the !
154 // if there's a sinkpad...
156 sinkpadname = &arg[1];
160 srcpadname = g_strndup(arg,(ptr-arg));
161 // if there's a sinkpad
162 if (len > (ptr-arg)+1)
163 sinkpadname = &ptr[1];
168 GST_DEBUG(0,"have srcpad %s, sinkpad %s\n",srcpadname,sinkpadname);
170 g_slist_free(srcpads);
176 if (srcpadname != NULL) {
178 // split name at commas
179 if ((ptr = strchr(srcpadname,','))){
180 tempname = g_strndup(srcpadname,(ptr-srcpadname));
181 srcpadname = &ptr[1];
183 tempname = srcpadname;
186 // look for pad with that name
187 if ((temppad = gst_element_get_pad(previous,tempname))){
188 srcpads = g_slist_append(srcpads,temppad);
192 // try to create a pad using that padtemplate name
193 else if ((temppad = gst_element_request_pad_by_name(previous,tempname))) {
194 srcpads = g_slist_append(srcpads,temppad);
198 GST_DEBUG(0,"NO SUCH pad %s in element %s\n",tempname,GST_ELEMENT_NAME(previous));
200 GST_DEBUG(0,"have src pad %s:%s\n",GST_DEBUG_PAD_NAME(temppad));
203 // if there is no more commas in srcpadname then we're done
204 if (tempname == srcpadname) break;
209 // check through the list to find the first sink pad
210 GST_DEBUG(0,"CHECKING through element %s for pad named %s\n",GST_ELEMENT_NAME(previous),srcpadname);
211 pads = gst_element_get_pad_list(previous);
213 temppad = GST_PARSE_LISTPAD(pads);
214 GST_DEBUG(0,"have pad %s:%s\n",GST_DEBUG_PAD_NAME(temppad));
215 if (GST_IS_GHOST_PAD(temppad)) GST_DEBUG(0,"it's a ghost pad\n");
216 if (gst_pad_get_direction (temppad) == GST_PAD_SRC){
217 srcpads = g_slist_append(srcpads,temppad);
221 pads = g_list_next (pads);
223 if (!srcpads) GST_DEBUG(0,"error, can't find a src pad!!!\n");
224 else GST_DEBUG(0,"have src pad %s:%s\n",GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(srcpads)));
227 // argument with = in it
228 } else if (strstr(arg, "=")) {
231 gchar * pos = strstr(arg, "=");
232 // we have an argument
236 DEBUG("attempting to set argument '%s' to '%s' on element '%s'\n",
237 argname,argval,GST_ELEMENT_NAME(previous));
238 //gtk_object_set(GTK_OBJECT(previous),argname,argval,NULL);
239 gst_util_set_object_arg (GTK_OBJECT(previous), argname, argval);
242 // element or argument, or beginning of bin or thread
244 DEBUG("have element or bin/thread\n");
245 // if we have a bin or thread starting
246 if (strchr("({",arg[0])) {
248 // create a bin and add it to the current parent
249 element = gst_bin_new(g_strdup_printf("bin%d",priv->bincount++));
251 fprintf(stderr,"Couldn't create a bin!\n");
254 GST_DEBUG(0,"CREATED bin %s\n",GST_ELEMENT_NAME(element));
255 } else if (arg[0] == '{') {
256 // create a thread and add it to the current parent
257 element = gst_thread_new(g_strdup_printf("thread%d",priv->threadcount++));
259 fprintf(stderr,"Couldn't create a thread!\n");
262 GST_DEBUG(0,"CREATED thread %s\n",GST_ELEMENT_NAME(element));
265 i += gst_parse_launch_cmdline(argc - i, argv + i + 1, GST_BIN (element), priv);
268 // we have an element
269 DEBUG("attempting to create element '%s'\n",arg);
270 ptr = gst_parse_unique_name(arg,priv);
271 element = gst_elementfactory_make(arg,ptr);
274 fprintf(stderr,"Couldn't create a '%s', no such element or need to run gstreamer-register?\n",arg);
277 GST_DEBUG(0,"CREATED element %s\n",GST_ELEMENT_NAME(element));
280 gst_bin_add (GST_BIN (parent), element);
283 g_slist_free(sinkpads);
289 if (sinkpadname != NULL) {
291 // split name at commas
292 if ((ptr = strchr(sinkpadname,','))){
293 tempname = g_strndup(sinkpadname,(ptr-sinkpadname));
294 sinkpadname = &ptr[1];
296 tempname = sinkpadname;
299 // look for pad with that name
300 if ((temppad = gst_element_get_pad(element,tempname))){
301 sinkpads = g_slist_append(sinkpads,temppad);
305 // try to create a pad using that padtemplate name
306 else if ((temppad = gst_element_request_pad_by_name(element,tempname))) {
307 sinkpads = g_slist_append(sinkpads,temppad);
311 GST_DEBUG(0,"NO SUCH pad %s in element %s\n",tempname,GST_ELEMENT_NAME(element));
313 GST_DEBUG(0,"have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(temppad));
316 // if there is no more commas in sinkpadname then we're done
317 if (tempname == sinkpadname) break;
322 // check through the list to find the first sink pad
323 pads = gst_element_get_pad_list(element);
325 temppad = GST_PAD(pads->data);
326 pads = g_list_next (pads);
327 if (gst_pad_get_direction (temppad) == GST_PAD_SINK){
328 sinkpads = g_slist_append(sinkpads,temppad);
335 if (!sinkpads) GST_DEBUG(0,"can't find a sink pad for %s\n", gst_element_get_name (previous));
336 else GST_DEBUG(0,"have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(sinkpads)));
338 if (!srcpads && sinkpads && previous) {
339 dyn_connect *connect = g_malloc (sizeof (dyn_connect));
341 connect->srcpadname = srcpadname;
342 connect->target = GST_PARSE_LISTPAD(sinkpads);
344 GST_DEBUG(0,"SETTING UP dynamic connection %s:%s and %s:%s\n",
345 gst_element_get_name (previous),
347 GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(sinkpads)));
349 gtk_signal_connect (GTK_OBJECT (previous), "new_pad", dynamic_connect, connect);
350 gtk_signal_connect (GTK_OBJECT (previous), "new_ghost_pad", dynamic_connect, connect);
353 for (j=0; (j<numsrcpads) && (j<numsinkpads); j++){
354 GST_DEBUG(0,"CONNECTING %s:%s and %s:%s\n",
355 GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(g_slist_nth(srcpads,j))),
356 GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(g_slist_nth(sinkpads,j))));
358 GST_PARSE_LISTPAD(g_slist_nth(srcpads,j)),
359 GST_PARSE_LISTPAD(g_slist_nth(sinkpads,j)));
363 g_slist_free(srcpads);
366 g_slist_free(sinkpads);
369 // thomas: if we're the first element, connect eos signal
370 if (elementcount == 1)
372 gtk_signal_connect (GTK_OBJECT (element), "eos",
373 GTK_SIGNAL_FUNC (have_eos), NULL);
376 // if we're the first element, ghost all the sinkpads
377 if (elementcount == 1) {
378 DEBUG("first element, ghosting all of %s's sink pads to parent %s\n",
379 GST_ELEMENT_NAME(element),GST_ELEMENT_NAME(GST_ELEMENT(parent)));
380 pads = gst_element_get_pad_list (element);
382 temppad = GST_PAD (pads->data);
383 pads = g_list_next (pads);
384 if (!temppad) DEBUG("much oddness, pad doesn't seem to exist\n");
385 else if (gst_pad_get_direction (temppad) == GST_PAD_SINK) {
386 gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
387 g_strdup_printf("%s-ghost",GST_PAD_NAME(temppad)));
388 GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
389 GST_DEBUG_PAD_NAME(temppad),GST_ELEMENT_NAME(GST_ELEMENT(parent)),GST_PAD_NAME(temppad));
395 if (!GST_IS_BIN(element)) prevelement = element;
401 // ghost all the src pads of the bin
402 if (prevelement != NULL) {
403 DEBUG("last element, ghosting all of %s's src pads to parent %s\n",
404 GST_ELEMENT_NAME(prevelement),GST_ELEMENT_NAME(GST_ELEMENT(parent)));
405 pads = gst_element_get_pad_list (prevelement);
407 temppad = GST_PAD (pads->data);
408 pads = g_list_next (pads);
409 if (!temppad) DEBUG("much oddness, pad doesn't seem to exist\n");
410 else if (gst_pad_get_direction (temppad) == GST_PAD_SRC) {
411 gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
412 g_strdup_printf("%s-ghost",GST_PAD_NAME(temppad)));
413 GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
414 GST_DEBUG_PAD_NAME(temppad),GST_ELEMENT_NAME (parent),GST_PAD_NAME(temppad));
421 if (retval) return retval;
423 if (closingchar != '\0')
424 DEBUG("returning IN THE WRONG PLACE\n");
425 else DEBUG("ending pipeline\n");
431 * @cmdline: the command line describing the pipeline
432 * @parent: the parent bin for the resulting pipeline
434 * Create a new pipeline based on command line syntax.
439 gst_parse_launch(const gchar *cmdline,GstBin *parent)
445 const gchar *cp, *start, *end;
447 GSList *string_list = NULL, *slist;
450 priv.threadcount = 0;
452 priv.elementcounts = NULL;
453 priv.verbose = FALSE;
456 end = cmdline + strlen(cmdline);
461 // Extract the arguments to a gslist in reverse order
462 for (cp = cmdline; cp < end; ) {
463 i = strcspn(cp, "([{}]) \"\\");
466 temp = g_strconcat (temp, g_strndup (cp, i), NULL);
468 // see if we have an escape char
470 // normal argument - copy and add to the list
471 string_list = g_slist_prepend(string_list, temp);
476 temp = g_strconcat (temp, g_strndup (&cp[++i], 1), NULL);
482 while (cp < end && *cp == ' ') {
486 // handle quoted arguments
490 // find matching quote
491 while (cp < end && *cp != '"')
494 // make sure we got it
496 g_warning("gst_parse_launch: Unbalanced quote in command line");
497 // FIXME: The list leaks here
501 // copy the string sans quotes
502 string_list = g_slist_prepend(string_list, g_strndup(start, cp - start));
504 cp += 2; // skip the quote aswell
507 // brackets exist in a separate argument slot
508 if (*cp && strchr("([{}])", *cp)) {
509 string_list = g_slist_prepend(string_list, g_strndup(cp, 1));
515 // now allocate the new argv array
516 argvn = g_new0(char *,newargc);
517 GST_DEBUG(0,"got %d args\n",newargc);
519 // reverse the list and put the strings in the new array
522 for (slist = string_list; slist; slist = slist->next)
523 argvn[--i] = slist->data;
525 g_slist_free(string_list);
528 for (i=0;i<newargc;i++) {
529 GST_DEBUG(0,"arg %d is: %s\n",i,argvn[i]);
532 // set up the elementcounts hash
533 priv.elementcounts = g_hash_table_new(g_str_hash,g_str_equal);
536 i = gst_parse_launch_cmdline(newargc,argvn,parent,&priv);
538 // GST_DEBUG(0, "Finished - freeing temporary argument array");
539 // g_strfreev(argvn);