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));
73 static void have_eos (void)
75 DEBUG ("I have eos on the first element\n");
80 dynamic_connect (GstElement *element, GstPad *newpad, gpointer data)
82 dyn_connect *connect = (dyn_connect *)data;
84 if (!strcmp (gst_pad_get_name (newpad), connect->srcpadname)) {
85 gst_element_set_state (connect->pipeline, GST_STATE_PAUSED);
86 gst_pad_connect (newpad, connect->target);
87 gst_element_set_state (connect->pipeline, GST_STATE_PLAYING);
92 gst_parse_unique_name(gchar *type,gst_parse_priv *priv)
97 tmp = g_hash_table_lookup (priv->elementcounts,type);
98 count = GPOINTER_TO_INT (tmp);
100 g_hash_table_insert(priv->elementcounts,type,GINT_TO_POINTER(count));
102 return g_strdup_printf("%s%d",type,count-1);
108 gst_parse_launch_cmdline(int argc,char *argv[],GstBin *parent,gst_parse_priv *priv)
112 GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
113 gchar closingchar = '\0';
116 gchar *sinkpadname = NULL, *srcpadname = NULL, *tempname;
118 GSList *sinkpads = NULL, *srcpads = NULL;
119 gint numsrcpads = 0, numsinkpads = 0;
121 gint elementcount = 0;
126 if (GST_IS_PIPELINE(parent)) { closingchar = '\0';DEBUG("in pipeline "); }
127 else if (GST_IS_THREAD(parent)) { closingchar = '}';DEBUG("in thread "); }
128 else { closingchar = ')';DEBUG("in bin "); }
129 DEBUG_NOPREFIX("%s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
133 // FIXME this is a lame solution for problems with the first parser
134 if (arg == NULL) { i++;continue; }
137 DEBUG("** ARGUMENT is '%s'\n",arg);
139 // a null that slipped through the reconstruction
141 DEBUG("random arg, FIXME\n");
145 // end of the container
146 } else if (arg[0] == closingchar) {
147 // time to finish off this bin
148 DEBUG("exiting container %s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
153 } else if ((ptr = strchr(arg,'!'))) {
154 DEBUG("attempting to connect pads together....\n");
156 // if it starts with the !
159 // if there's a sinkpad...
161 sinkpadname = &arg[1];
165 srcpadname = g_strndup(arg,(ptr-arg));
166 // if there's a sinkpad
167 if (len > (ptr-arg)+1)
168 sinkpadname = &ptr[1];
173 GST_DEBUG(0,"have srcpad %s, sinkpad %s\n",srcpadname,sinkpadname);
175 g_slist_free(srcpads);
181 if (srcpadname != NULL) {
183 // split name at commas
184 if ((ptr = strchr(srcpadname,','))){
185 tempname = g_strndup(srcpadname,(ptr-srcpadname));
186 srcpadname = &ptr[1];
188 tempname = srcpadname;
191 // look for pad with that name
192 if ((temppad = gst_element_get_pad(previous,tempname))){
193 srcpads = g_slist_append(srcpads,temppad);
197 // try to create a pad using that padtemplate name
198 else if ((temppad = gst_element_request_pad_by_name(previous,tempname))) {
199 srcpads = g_slist_append(srcpads,temppad);
203 GST_DEBUG(0,"NO SUCH pad %s in element %s\n",tempname,GST_ELEMENT_NAME(previous));
205 GST_DEBUG(0,"have src pad %s:%s\n",GST_DEBUG_PAD_NAME(temppad));
208 // if there is no more commas in srcpadname then we're done
209 if (tempname == srcpadname) break;
214 // check through the list to find the first sink pad
215 GST_DEBUG(0,"CHECKING through element %s for pad named %s\n",GST_ELEMENT_NAME(previous),srcpadname);
216 pads = gst_element_get_pad_list(previous);
218 temppad = GST_PARSE_LISTPAD(pads);
219 GST_DEBUG(0,"have pad %s:%s\n",GST_DEBUG_PAD_NAME(temppad));
220 if (GST_IS_GHOST_PAD(temppad)) GST_DEBUG(0,"it's a ghost pad\n");
221 if (gst_pad_get_direction (temppad) == GST_PAD_SRC){
222 srcpads = g_slist_append(srcpads,temppad);
226 pads = g_list_next (pads);
228 if (!srcpads) GST_DEBUG(0,"error, can't find a src pad!!!\n");
229 else GST_DEBUG(0,"have src pad %s:%s\n",GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(srcpads)));
232 // argument with = in it
233 } else if (strstr(arg, "=")) {
236 gchar * pos = strstr(arg, "=");
237 // we have an argument
242 GST_DEBUG(0,"attempting to set argument '%s' to '%s' on element '%s'\n",
243 argname,argval,GST_ELEMENT_NAME(previous));
244 gst_util_set_object_arg (G_OBJECT(previous), argname, argval);
247 // element or argument, or beginning of bin or thread
248 } else if (arg[0] == '[') {
249 // we have the start of a name of the preceding element.
250 // rename previous element to next arg.
251 if (arg[1] != '\0') {
252 fprintf(stderr,"error, unexpected junk after [\n");
253 return GST_PARSE_ERROR_SYNTAX;
257 gst_element_set_name(previous, argv[i]);
259 fprintf(stderr,"error, expected element name, found end of arguments\n");
260 return GST_PARSE_ERROR_SYNTAX;
264 fprintf(stderr,"error, expected ], found end of arguments\n");
265 return GST_PARSE_ERROR_SYNTAX;
266 } else if (strcmp(argv[i], "]") != 0) {
267 fprintf(stderr,"error, expected ], found '%s'\n", argv[i]);
268 return GST_PARSE_ERROR_SYNTAX;
271 DEBUG("have element or bin/thread\n");
272 // if we have a bin or thread starting
273 if (strchr("({",arg[0])) {
275 // create a bin and add it to the current parent
276 element = gst_bin_new(g_strdup_printf("bin%d",priv->bincount++));
278 fprintf(stderr,"Couldn't create a bin!\n");
279 return GST_PARSE_ERROR_CREATING_ELEMENT;
281 GST_DEBUG(0,"CREATED bin %s\n",GST_ELEMENT_NAME(element));
282 } else if (arg[0] == '{') {
283 // create a thread and add it to the current parent
284 element = gst_thread_new(g_strdup_printf("thread%d",priv->threadcount++));
286 fprintf(stderr,"Couldn't create a thread!\n");
287 return GST_PARSE_ERROR_CREATING_ELEMENT;
289 GST_DEBUG(0,"CREATED thread %s\n",GST_ELEMENT_NAME(element));
291 DEBUG("error in parser, unexpected symbol, FIXME\n");
296 j = gst_parse_launch_cmdline(argc - i, argv + i + 1, GST_BIN (element), priv);
297 //check for parse error
302 // we have an element
303 DEBUG("attempting to create element '%s'\n",arg);
304 ptr = gst_parse_unique_name(arg,priv);
305 element = gst_elementfactory_make(arg,ptr);
308 #ifndef GST_DISABLE_REGISTRY
309 fprintf(stderr,"Couldn't create a '%s', no such element or need to run gstreamer-register?\n",arg);
311 fprintf(stderr,"Couldn't create a '%s', no such element or need to load pluginn?\n",arg);
313 return GST_PARSE_ERROR_NOSUCH_ELEMENT;
315 GST_DEBUG(0,"CREATED element %s\n",GST_ELEMENT_NAME(element));
318 gst_bin_add (GST_BIN (parent), element);
321 g_slist_free(sinkpads);
327 if (sinkpadname != NULL) {
329 // split name at commas
330 if ((ptr = strchr(sinkpadname,','))){
331 tempname = g_strndup(sinkpadname,(ptr-sinkpadname));
332 sinkpadname = &ptr[1];
334 tempname = sinkpadname;
337 // look for pad with that name
338 if ((temppad = gst_element_get_pad(element,tempname))){
339 sinkpads = g_slist_append(sinkpads,temppad);
343 // try to create a pad using that padtemplate name
344 else if ((temppad = gst_element_request_pad_by_name(element,tempname))) {
345 sinkpads = g_slist_append(sinkpads,temppad);
349 GST_DEBUG(0,"NO SUCH pad %s in element %s\n",tempname,GST_ELEMENT_NAME(element));
351 GST_DEBUG(0,"have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(temppad));
354 // if there is no more commas in sinkpadname then we're done
355 if (tempname == sinkpadname) break;
360 // check through the list to find the first sink pad
361 pads = gst_element_get_pad_list(element);
363 temppad = GST_PAD(pads->data);
364 pads = g_list_next (pads);
365 if (gst_pad_get_direction (temppad) == GST_PAD_SINK){
366 sinkpads = g_slist_append(sinkpads,temppad);
373 if (!sinkpads) GST_DEBUG(0,"can't find a sink pad for element\n");
374 else GST_DEBUG(0,"have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(sinkpads)));
376 if (!srcpads && sinkpads && previous) {
377 dyn_connect *connect = g_malloc (sizeof (dyn_connect));
379 connect->srcpadname = srcpadname;
380 connect->target = GST_PARSE_LISTPAD(sinkpads);
381 connect->pipeline = parent;
383 GST_DEBUG(0,"SETTING UP dynamic connection %s:%s and %s:%s\n",
384 gst_element_get_name (previous),
386 GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(sinkpads)));
388 g_signal_connect (G_OBJECT (previous), "new_pad",
389 G_CALLBACK (dynamic_connect), connect);
392 for (j=0; (j<numsrcpads) && (j<numsinkpads); j++){
393 GST_DEBUG(0,"CONNECTING %s:%s and %s:%s\n",
394 GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(g_slist_nth(srcpads,j))),
395 GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(g_slist_nth(sinkpads,j))));
397 GST_PARSE_LISTPAD(g_slist_nth(srcpads,j)),
398 GST_PARSE_LISTPAD(g_slist_nth(sinkpads,j)));
402 g_slist_free(srcpads);
405 g_slist_free(sinkpads);
408 // thomas: if we're the first element, connect eos signal
409 if (elementcount == 1)
411 g_signal_connect (G_OBJECT (element), "eos", have_eos, NULL);
414 // if we're the first element, ghost all the sinkpads
415 if (elementcount == 1) {
416 DEBUG("first element, ghosting all of %s's sink pads to parent %s\n",
417 GST_ELEMENT_NAME(element),GST_ELEMENT_NAME(GST_ELEMENT(parent)));
418 pads = gst_element_get_pad_list (element);
420 temppad = GST_PAD (pads->data);
421 pads = g_list_next (pads);
422 if (!temppad) DEBUG("much oddness, pad doesn't seem to exist\n");
423 else if (gst_pad_get_direction (temppad) == GST_PAD_SINK) {
424 gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
425 g_strdup_printf("%s-ghost",GST_PAD_NAME(temppad)));
426 GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
427 GST_DEBUG_PAD_NAME(temppad),GST_ELEMENT_NAME(GST_ELEMENT(parent)),GST_PAD_NAME(temppad));
433 if (!GST_IS_BIN(element)) prevelement = element;
439 // ghost all the src pads of the bin
440 if (prevelement != NULL) {
441 DEBUG("last element, ghosting all of %s's src pads to parent %s\n",
442 GST_ELEMENT_NAME(prevelement),GST_ELEMENT_NAME(GST_ELEMENT(parent)));
443 pads = gst_element_get_pad_list (prevelement);
445 temppad = GST_PAD (pads->data);
446 pads = g_list_next (pads);
447 if (!temppad) DEBUG("much oddness, pad doesn't seem to exist\n");
448 else if (gst_pad_get_direction (temppad) == GST_PAD_SRC) {
449 gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
450 g_strdup_printf("%s-ghost",GST_PAD_NAME(temppad)));
451 GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
452 GST_DEBUG_PAD_NAME(temppad),GST_ELEMENT_NAME (parent),GST_PAD_NAME(temppad));
459 if (retval) return retval;
461 DEBUG (closingchar != '\0'?
462 "returning IN THE WRONG PLACE\n" :
463 "ending pipeline\n");
470 * @cmdline: the command line describing the pipeline
471 * @parent: the parent bin for the resulting pipeline
473 * Create a new pipeline based on command line syntax.
478 gst_parse_launch(const gchar *cmdline,GstBin *parent)
484 const gchar *cp, *start, *end;
486 GSList *string_list = NULL, *slist;
489 priv.threadcount = 0;
491 priv.elementcounts = NULL;
492 priv.verbose = FALSE;
495 end = cmdline + strlen(cmdline);
500 // Extract the arguments to a gslist in reverse order
501 for (cp = cmdline; cp < end; ) {
502 i = strcspn(cp, "([{}]) \"\\");
505 temp = g_strconcat (temp, g_strndup (cp, i), NULL);
507 // see if we have an escape char
509 // normal argument - copy and add to the list
510 string_list = g_slist_prepend(string_list, temp);
515 temp = g_strconcat (temp, g_strndup (&cp[++i], 1), NULL);
521 while (cp < end && *cp == ' ') {
525 // handle quoted arguments
529 // find matching quote
530 while (cp < end && *cp != '"')
533 // make sure we got it
535 g_warning("gst_parse_launch: Unbalanced quote in command line");
536 // FIXME: The list leaks here
540 // copy the string sans quotes
541 string_list = g_slist_prepend(string_list, g_strndup(start, cp - start));
543 cp += 2; // skip the quote aswell
546 // brackets exist in a separate argument slot
547 if (*cp && strchr("([{}])", *cp)) {
548 string_list = g_slist_prepend(string_list, g_strndup(cp, 1));
554 // now allocate the new argv array
555 argvn = g_new0(char *,newargc);
556 GST_DEBUG(0,"got %d args\n",newargc);
558 // reverse the list and put the strings in the new array
561 for (slist = string_list; slist; slist = slist->next)
562 argvn[--i] = slist->data;
564 g_slist_free(string_list);
567 for (i=0;i<newargc;i++) {
568 GST_DEBUG(0,"arg %d is: %s\n",i,argvn[i]);
571 // set up the elementcounts hash
572 priv.elementcounts = g_hash_table_new(g_str_hash,g_str_equal);
575 i = gst_parse_launch_cmdline(newargc,argvn,parent,&priv);
577 // GST_DEBUG(0, "Finished - freeing temporary argument array");
578 // g_strfreev(argvn);