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)
94 tmp = g_hash_table_lookup (priv->elementcounts,type);
95 count = GPOINTER_TO_INT (tmp);
97 g_hash_table_insert(priv->elementcounts,type,GINT_TO_POINTER(count));
99 return g_strdup_printf("%s%d",type,count-1);
105 gst_parse_launch_cmdline(int argc,char *argv[],GstBin *parent,gst_parse_priv *priv)
109 GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
110 gchar closingchar = '\0';
113 gchar *sinkpadname = NULL, *srcpadname = NULL, *tempname;
115 GSList *sinkpads = NULL, *srcpads = NULL;
116 gint numsrcpads = 0, numsinkpads = 0;
118 gint elementcount = 0;
123 if (GST_IS_PIPELINE(parent)) { closingchar = '\0';DEBUG("in pipeline "); }
124 else if (GST_IS_THREAD(parent)) { closingchar = '}';DEBUG("in thread "); }
125 else { closingchar = ')';DEBUG("in bin "); }
126 DEBUG_NOPREFIX("%s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
130 // FIXME this is a lame solution for problems with the first parser
131 if (arg == NULL) { i++;continue; }
134 DEBUG("** ARGUMENT is '%s'\n",arg);
136 // a null that slipped through the reconstruction
138 DEBUG("random arg, FIXME\n");
142 // end of the container
143 } else if (arg[0] == closingchar) {
144 // time to finish off this bin
145 DEBUG("exiting container %s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
150 } else if ((ptr = strchr(arg,'!'))) {
151 DEBUG("attempting to connect pads together....\n");
153 // if it starts with the !
156 // if there's a sinkpad...
158 sinkpadname = &arg[1];
162 srcpadname = g_strndup(arg,(ptr-arg));
163 // if there's a sinkpad
164 if (len > (ptr-arg)+1)
165 sinkpadname = &ptr[1];
170 GST_DEBUG(0,"have srcpad %s, sinkpad %s\n",srcpadname,sinkpadname);
172 g_slist_free(srcpads);
178 if (srcpadname != NULL) {
180 // split name at commas
181 if ((ptr = strchr(srcpadname,','))){
182 tempname = g_strndup(srcpadname,(ptr-srcpadname));
183 srcpadname = &ptr[1];
185 tempname = srcpadname;
188 // look for pad with that name
189 if ((temppad = gst_element_get_pad(previous,tempname))){
190 srcpads = g_slist_append(srcpads,temppad);
194 // try to create a pad using that padtemplate name
195 else if ((temppad = gst_element_request_pad_by_name(previous,tempname))) {
196 srcpads = g_slist_append(srcpads,temppad);
200 GST_DEBUG(0,"NO SUCH pad %s in element %s\n",tempname,GST_ELEMENT_NAME(previous));
202 GST_DEBUG(0,"have src pad %s:%s\n",GST_DEBUG_PAD_NAME(temppad));
205 // if there is no more commas in srcpadname then we're done
206 if (tempname == srcpadname) break;
211 // check through the list to find the first sink pad
212 GST_DEBUG(0,"CHECKING through element %s for pad named %s\n",GST_ELEMENT_NAME(previous),srcpadname);
213 pads = gst_element_get_pad_list(previous);
215 temppad = GST_PARSE_LISTPAD(pads);
216 GST_DEBUG(0,"have pad %s:%s\n",GST_DEBUG_PAD_NAME(temppad));
217 if (GST_IS_GHOST_PAD(temppad)) GST_DEBUG(0,"it's a ghost pad\n");
218 if (gst_pad_get_direction (temppad) == GST_PAD_SRC){
219 srcpads = g_slist_append(srcpads,temppad);
223 pads = g_list_next (pads);
225 if (!srcpads) GST_DEBUG(0,"error, can't find a src pad!!!\n");
226 else GST_DEBUG(0,"have src pad %s:%s\n",GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(srcpads)));
229 // argument with = in it
230 } else if (strstr(arg, "=")) {
233 gchar * pos = strstr(arg, "=");
234 // we have an argument
239 // parse dparams values
240 if (strstr(arg, "@") == arg){
241 GstDParamManager *dpman;
246 GST_DEBUG(0,"attempting to set dynamic param '%s' to '%s' on element '%s'\n",
247 argname,argval,GST_ELEMENT_NAME(previous));
249 dpman = GST_ELEMENT_DPARAM_MANAGER(previous);
250 dparam_type = gst_dpman_get_dparam_type(dpman, argname);
251 if (dparam_type > 0){
252 gst_dpman_set_mode(dpman, "synchronous");
253 dparam = gst_dparam_new(dparam_type);
254 gst_dpman_attach_dparam (dpman, argname, dparam);
255 gst_dparam_set_value_from_string(GST_DPARAM_GET_POINT(dparam, 0LL)[0], argval);
256 GST_DPARAM_READY_FOR_UPDATE(dparam) = TRUE;
260 GST_DEBUG(0,"attempting to set argument '%s' to '%s' on element '%s'\n",
261 argname,argval,GST_ELEMENT_NAME(previous));
262 gst_util_set_object_arg (G_OBJECT(previous), argname, argval);
266 // element or argument, or beginning of bin or thread
267 } else if (arg[0] == '[') {
268 // we have the start of a name of the preceding element.
269 // rename previous element to next arg.
270 if (arg[1] != '\0') {
271 fprintf(stderr,"error, unexpected junk after [\n");
276 gst_element_set_name(previous, argv[i]);
278 fprintf(stderr,"error, expected element name, found end of arguments\n");
283 fprintf(stderr,"error, expected ], found end of arguments\n");
285 } else if (strcmp(argv[i], "]") != 0) {
286 fprintf(stderr,"error, expected ], found '%s'\n", argv[i]);
290 DEBUG("have element or bin/thread\n");
291 // if we have a bin or thread starting
292 if (strchr("({",arg[0])) {
294 // create a bin and add it to the current parent
295 element = gst_bin_new(g_strdup_printf("bin%d",priv->bincount++));
297 fprintf(stderr,"Couldn't create a bin!\n");
300 GST_DEBUG(0,"CREATED bin %s\n",GST_ELEMENT_NAME(element));
301 } else if (arg[0] == '{') {
302 // create a thread and add it to the current parent
303 element = gst_thread_new(g_strdup_printf("thread%d",priv->threadcount++));
305 fprintf(stderr,"Couldn't create a thread!\n");
308 GST_DEBUG(0,"CREATED thread %s\n",GST_ELEMENT_NAME(element));
310 DEBUG("error in parser, unexpected symbol, FIXME\n");
315 i += gst_parse_launch_cmdline(argc - i, argv + i + 1, GST_BIN (element), priv);
318 // we have an element
319 DEBUG("attempting to create element '%s'\n",arg);
320 ptr = gst_parse_unique_name(arg,priv);
321 element = gst_elementfactory_make(arg,ptr);
324 fprintf(stderr,"Couldn't create a '%s', no such element or need to run gstreamer-register?\n",arg);
327 GST_DEBUG(0,"CREATED element %s\n",GST_ELEMENT_NAME(element));
330 gst_bin_add (GST_BIN (parent), element);
333 g_slist_free(sinkpads);
339 if (sinkpadname != NULL) {
341 // split name at commas
342 if ((ptr = strchr(sinkpadname,','))){
343 tempname = g_strndup(sinkpadname,(ptr-sinkpadname));
344 sinkpadname = &ptr[1];
346 tempname = sinkpadname;
349 // look for pad with that name
350 if ((temppad = gst_element_get_pad(element,tempname))){
351 sinkpads = g_slist_append(sinkpads,temppad);
355 // try to create a pad using that padtemplate name
356 else if ((temppad = gst_element_request_pad_by_name(element,tempname))) {
357 sinkpads = g_slist_append(sinkpads,temppad);
361 GST_DEBUG(0,"NO SUCH pad %s in element %s\n",tempname,GST_ELEMENT_NAME(element));
363 GST_DEBUG(0,"have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(temppad));
366 // if there is no more commas in sinkpadname then we're done
367 if (tempname == sinkpadname) break;
372 // check through the list to find the first sink pad
373 pads = gst_element_get_pad_list(element);
375 temppad = GST_PAD(pads->data);
376 pads = g_list_next (pads);
377 if (gst_pad_get_direction (temppad) == GST_PAD_SINK){
378 sinkpads = g_slist_append(sinkpads,temppad);
385 if (!sinkpads) GST_DEBUG(0,"can't find a sink pad for element\n");
386 else GST_DEBUG(0,"have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(sinkpads)));
388 if (!srcpads && sinkpads && previous) {
389 dyn_connect *connect = g_malloc (sizeof (dyn_connect));
391 connect->srcpadname = srcpadname;
392 connect->target = GST_PARSE_LISTPAD(sinkpads);
394 GST_DEBUG(0,"SETTING UP dynamic connection %s:%s and %s:%s\n",
395 gst_element_get_name (previous),
397 GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(sinkpads)));
399 g_signal_connect (G_OBJECT (previous), "new_pad",
400 G_CALLBACK (dynamic_connect), connect);
401 g_signal_connect (G_OBJECT (previous), "new_ghost_pad",
402 G_CALLBACK (dynamic_connect), connect);
405 for (j=0; (j<numsrcpads) && (j<numsinkpads); j++){
406 GST_DEBUG(0,"CONNECTING %s:%s and %s:%s\n",
407 GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(g_slist_nth(srcpads,j))),
408 GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(g_slist_nth(sinkpads,j))));
410 GST_PARSE_LISTPAD(g_slist_nth(srcpads,j)),
411 GST_PARSE_LISTPAD(g_slist_nth(sinkpads,j)));
415 g_slist_free(srcpads);
418 g_slist_free(sinkpads);
421 // thomas: if we're the first element, connect eos signal
422 if (elementcount == 1)
424 g_signal_connect (G_OBJECT (element), "eos", have_eos, NULL);
427 // if we're the first element, ghost all the sinkpads
428 if (elementcount == 1) {
429 DEBUG("first element, ghosting all of %s's sink pads to parent %s\n",
430 GST_ELEMENT_NAME(element),GST_ELEMENT_NAME(GST_ELEMENT(parent)));
431 pads = gst_element_get_pad_list (element);
433 temppad = GST_PAD (pads->data);
434 pads = g_list_next (pads);
435 if (!temppad) DEBUG("much oddness, pad doesn't seem to exist\n");
436 else if (gst_pad_get_direction (temppad) == GST_PAD_SINK) {
437 gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
438 g_strdup_printf("%s-ghost",GST_PAD_NAME(temppad)));
439 GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
440 GST_DEBUG_PAD_NAME(temppad),GST_ELEMENT_NAME(GST_ELEMENT(parent)),GST_PAD_NAME(temppad));
446 if (!GST_IS_BIN(element)) prevelement = element;
452 // ghost all the src pads of the bin
453 if (prevelement != NULL) {
454 DEBUG("last element, ghosting all of %s's src pads to parent %s\n",
455 GST_ELEMENT_NAME(prevelement),GST_ELEMENT_NAME(GST_ELEMENT(parent)));
456 pads = gst_element_get_pad_list (prevelement);
458 temppad = GST_PAD (pads->data);
459 pads = g_list_next (pads);
460 if (!temppad) DEBUG("much oddness, pad doesn't seem to exist\n");
461 else if (gst_pad_get_direction (temppad) == GST_PAD_SRC) {
462 gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
463 g_strdup_printf("%s-ghost",GST_PAD_NAME(temppad)));
464 GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
465 GST_DEBUG_PAD_NAME(temppad),GST_ELEMENT_NAME (parent),GST_PAD_NAME(temppad));
472 if (retval) return retval;
474 DEBUG (closingchar != '\0'?
475 "returning IN THE WRONG PLACE\n" :
476 "ending pipeline\n");
483 * @cmdline: the command line describing the pipeline
484 * @parent: the parent bin for the resulting pipeline
486 * Create a new pipeline based on command line syntax.
491 gst_parse_launch(const gchar *cmdline,GstBin *parent)
497 const gchar *cp, *start, *end;
499 GSList *string_list = NULL, *slist;
502 priv.threadcount = 0;
504 priv.elementcounts = NULL;
505 priv.verbose = FALSE;
508 end = cmdline + strlen(cmdline);
513 // Extract the arguments to a gslist in reverse order
514 for (cp = cmdline; cp < end; ) {
515 i = strcspn(cp, "([{}]) \"\\");
518 temp = g_strconcat (temp, g_strndup (cp, i), NULL);
520 // see if we have an escape char
522 // normal argument - copy and add to the list
523 string_list = g_slist_prepend(string_list, temp);
528 temp = g_strconcat (temp, g_strndup (&cp[++i], 1), NULL);
534 while (cp < end && *cp == ' ') {
538 // handle quoted arguments
542 // find matching quote
543 while (cp < end && *cp != '"')
546 // make sure we got it
548 g_warning("gst_parse_launch: Unbalanced quote in command line");
549 // FIXME: The list leaks here
553 // copy the string sans quotes
554 string_list = g_slist_prepend(string_list, g_strndup(start, cp - start));
556 cp += 2; // skip the quote aswell
559 // brackets exist in a separate argument slot
560 if (*cp && strchr("([{}])", *cp)) {
561 string_list = g_slist_prepend(string_list, g_strndup(cp, 1));
567 // now allocate the new argv array
568 argvn = g_new0(char *,newargc);
569 GST_DEBUG(0,"got %d args\n",newargc);
571 // reverse the list and put the strings in the new array
574 for (slist = string_list; slist; slist = slist->next)
575 argvn[--i] = slist->data;
577 g_slist_free(string_list);
580 for (i=0;i<newargc;i++) {
581 GST_DEBUG(0,"arg %d is: %s\n",i,argvn[i]);
584 // set up the elementcounts hash
585 priv.elementcounts = g_hash_table_new(g_str_hash,g_str_equal);
588 i = gst_parse_launch_cmdline(newargc,argvn,parent,&priv);
590 // GST_DEBUG(0, "Finished - freeing temporary argument array");
591 // g_strfreev(argvn);