Added dynamic pads to the command line parser. ./gstreamer-launch disksrc location...
[platform/upstream/gstreamer.git] / gst / gstparse.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * :
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #define DEBUG(format,args...)
24 #define DEBUG_NOPREFIX(format,args...)
25 #define VERBOSE(format,args...)
26
27 #include <string.h>
28
29 #include "gst_private.h"
30 #include "gstparse.h"
31 #include "gstpipeline.h"
32 #include "gstthread.h"
33 #include "gstutils.h"
34
35 typedef struct _gst_parse_priv gst_parse_priv;
36 struct _gst_parse_priv {
37   guint bincount;
38   guint threadcount;
39   gint binlevel;
40   GHashTable *elementcounts;
41   gboolean verbose;
42   gboolean debug;
43 };
44
45 typedef struct _gst_parse_delayed_pad gst_parse_delayed_pad;
46 struct _gst_parse_delayed_pad {
47   gchar *name;
48   GstPad *peer;
49 };
50
51 /* FIXME need to either revive this, or have pad->padtemplate connections in core
52 static void
53 gst_parse_newpad(GstElement *element,GstPad *pad,launch_delayed_pad *peer)
54 {
55   gst_info("have NEW_PAD signal\n");
56   // if it matches, connect it
57   if (!strcmp(GST_PAD_NAME(pad),peer->name)) {
58     gst_pad_connect(pad,peer->peer);
59     gst_info("delayed connect of '%s' to '%s'\n",
60              GST_PAD_NAME(pad),GST_PAD_NAME(peer->peer));
61   }
62 }
63 */
64
65 typedef struct {
66   gchar *srcpadname;
67   GstPad *target;
68 } dyn_connect;
69
70 static void
71 dynamic_connect (GstElement *element, GstPad *newpad, gpointer data)
72 {
73   dyn_connect *connect = (dyn_connect *)data;
74
75   if (!strcmp (gst_pad_get_name (newpad), connect->srcpadname)) {
76     gst_pad_connect (newpad, connect->target);
77   }
78 }
79
80 static gchar *
81 gst_parse_unique_name(gchar *type,gst_parse_priv *priv)
82 {
83   gint count;
84
85   count = GPOINTER_TO_INT(g_hash_table_lookup(priv->elementcounts,type));
86   count++;
87   g_hash_table_insert(priv->elementcounts,type,GINT_TO_POINTER(count));
88
89   return g_strdup_printf("%s%d",type,count-1);
90 }
91
92
93
94 static gint
95 gst_parse_launch_cmdline(int argc,char *argv[],GstBin *parent,gst_parse_priv *priv)
96 {
97   gint i = 0;
98   gchar *arg;
99   GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
100   gchar closingchar = '\0';
101   gint len;
102   gchar *ptr;
103   gchar *sinkpadname = NULL, *srcpadname = NULL;
104   GstPad *sinkpad = NULL, *srcpad = NULL;
105   GList *pads;
106   gint elementcount = 0;
107   gint retval = 0;
108
109   priv->binlevel++;
110
111   if (GST_IS_PIPELINE(parent)) { closingchar = '\0';DEBUG("in pipeline "); }
112   else if (GST_IS_THREAD(parent)) { closingchar = '}';DEBUG("in thread "); }
113   else { closingchar = ')';DEBUG("in bin "); }
114   DEBUG_NOPREFIX("%s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
115
116   while (i < argc) {
117     arg = argv[i];
118     // FIXME this is a lame solution for problems with the first parser
119     if (arg == NULL) { i++;continue; }
120     len = strlen(arg);
121     element = NULL;
122     DEBUG("** ARGUMENT is '%s'\n",arg);
123
124     // a null that slipped through the reconstruction
125     if (len == 0) {
126       DEBUG("random arg, FIXME\n");
127       i++;
128       continue;
129
130     // end of the container
131     } else if (arg[0] == closingchar) {
132       // time to finish off this bin
133       DEBUG("exiting container %s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
134       retval = i+1;
135       break;
136
137     // a pad connection
138     } else if ((ptr = strchr(arg,'!'))) {
139       DEBUG("attempting to connect pads together....\n");
140
141       // if it starts with the !
142       if (arg[0] == '!') {
143         srcpadname = NULL;
144         // if there's a sinkpad...
145         if (len > 1)
146           sinkpadname = &arg[1];
147         else
148           sinkpadname = NULL;
149       } else {
150         srcpadname = g_strndup(arg,(ptr-arg));
151         // if there's a sinkpad
152         if (len > (ptr-arg)+1)
153           sinkpadname = &ptr[1];
154         else
155           sinkpadname = NULL;
156       }
157
158       GST_DEBUG(0,"have srcpad %s, sinkpad %s\n",srcpadname,sinkpadname);
159
160       srcpad = NULL;
161
162       // if the srcpadname doesn't have any commas in it, find an actual pad
163       if (!srcpadname || !strchr(srcpadname,',')) {
164         if (srcpadname != NULL) {
165           srcpad = gst_element_get_pad(previous,srcpadname);
166           if (!srcpad) {
167             GST_DEBUG(0,"NO SUCH pad %s in element %s\n",srcpadname,GST_ELEMENT_NAME(previous));
168           }
169         }
170         else if (srcpad == NULL) {
171           // check through the list to find the first sink pad
172           GST_DEBUG(0,"CHECKING through element %s for pad named %s\n",GST_ELEMENT_NAME(previous),srcpadname);
173           pads = gst_element_get_pad_list(previous);
174           while (pads) {
175             srcpad = GST_PAD(pads->data);
176 GST_DEBUG(0,"have pad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
177 if (GST_IS_GHOST_PAD(srcpad)) GST_DEBUG(0,"it's a ghost pad\n");
178             pads = g_list_next (pads);
179             if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) break;
180             srcpad = NULL;
181           }
182           if (!srcpad) GST_DEBUG(0,"error, can't find a src pad!!!\n");
183           else GST_DEBUG(0,"have src pad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
184         }
185       }
186
187     // argument with = in it
188     } else if (strstr(arg, "=")) {
189       gchar * argname;
190       gchar * argval;
191       gchar * pos = strstr(arg, "=");
192       // we have an argument
193       argname = arg;
194       pos[0] = '\0';
195       argval = pos+1;
196       DEBUG("attempting to set argument '%s' to '%s' on element '%s'\n",
197             argname,argval,GST_ELEMENT_NAME(previous));
198       //gtk_object_set(GTK_OBJECT(previous),argname,argval,NULL);
199       gst_util_set_object_arg (GTK_OBJECT(previous), argname, argval);
200       g_free(argname);
201
202     // element or argument, or beginning of bin or thread
203     } else {
204       DEBUG("have element or bin/thread\n");
205       // if we have a bin or thread starting
206       if (strchr("({",arg[0])) {
207         if (arg[0] == '(') {
208           // create a bin and add it to the current parent
209           element = gst_bin_new(g_strdup_printf("bin%d",priv->bincount++));
210           if (!element) {
211             fprintf(stderr,"Couldn't create a bin!\n");
212 //            exit(-1);
213           }
214           GST_DEBUG(0,"CREATED bin %s\n",GST_ELEMENT_NAME(element));
215         } else if (arg[0] == '{') {
216           // create a thread and add it to the current parent
217           element = gst_thread_new(g_strdup_printf("thread%d",priv->threadcount++));
218           if (!element) {
219             fprintf(stderr,"Couldn't create a thread!\n");
220 //            exit(-1);
221           }
222           GST_DEBUG(0,"CREATED thread %s\n",GST_ELEMENT_NAME(element));
223         }
224
225         i += gst_parse_launch_cmdline(argc - i, argv + i + 1, GST_BIN (element), priv);
226
227       } else {
228         // we have an element
229         DEBUG("attempting to create element '%s'\n",arg);
230         ptr = gst_parse_unique_name(arg,priv);
231         element = gst_elementfactory_make(arg,ptr);
232         g_free(ptr);
233         if (!element) {
234           fprintf(stderr,"Couldn't create a '%s', no such element or need to run gstraemer-register?\n",arg);
235 //          exit(-1);
236         }
237         GST_DEBUG(0,"CREATED element %s\n",GST_ELEMENT_NAME(element));
238       }
239
240       gst_bin_add (GST_BIN (parent), element);
241       elementcount++;
242
243       if (srcpad != NULL || srcpadname != NULL) {
244         if (srcpad)
245           DEBUG("need to connect to srcpad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
246
247         sinkpad = NULL;
248
249         if (sinkpadname != NULL)
250           sinkpad = gst_element_get_pad(previous,sinkpadname);
251
252         if (!sinkpad) {
253           // check through the list to find the first sink pad
254           pads = gst_element_get_pad_list(element);
255           while (pads) {
256             sinkpad = GST_PAD(pads->data);
257             pads = g_list_next (pads);
258             if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) break;
259             sinkpad = NULL;
260           }
261         }
262
263         if (!sinkpad) DEBUG("error, can't find a sink pad!!!\n");
264         else DEBUG("have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(sinkpad));
265
266         if (!srcpad) {
267           dyn_connect *connect = g_malloc (sizeof (dyn_connect));
268
269           connect->srcpadname = srcpadname;
270           connect->target = sinkpad;
271
272           GST_DEBUG(0,"SETTING UP dynamic connection %s:%s and %s:%s\n",gst_element_get_name (previous),
273                           srcpadname,GST_DEBUG_PAD_NAME(sinkpad));
274
275           gtk_signal_connect (GTK_OBJECT (previous), "new_pad", dynamic_connect, connect);
276         }
277         else {
278           GST_DEBUG(0,"CONNECTING %s:%s and %s:%s\n",GST_DEBUG_PAD_NAME(srcpad),GST_DEBUG_PAD_NAME(sinkpad));
279           gst_pad_connect(srcpad,sinkpad);
280         }
281
282         sinkpad = NULL;
283         srcpad = NULL;
284       }
285       
286
287       // if we're the first element, ghost all the sinkpads
288       if (elementcount == 1) {
289         DEBUG("first element, ghosting all of %s's sink pads to parent %s\n",
290               GST_ELEMENT_NAME(element),GST_ELEMENT_NAME(GST_ELEMENT(parent)));
291         pads = gst_element_get_pad_list (element);
292         while (pads) {
293           sinkpad = GST_PAD (pads->data);
294           pads = g_list_next (pads);
295           if (!sinkpad) DEBUG("much oddness, pad doesn't seem to exist\n");
296           else if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) {
297             gst_element_add_ghost_pad (GST_ELEMENT (parent), sinkpad,
298 g_strdup_printf("%s-ghost",GST_PAD_NAME(sinkpad)));
299             GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
300                       GST_DEBUG_PAD_NAME(sinkpad),GST_ELEMENT_NAME(GST_ELEMENT(parent)),GST_PAD_NAME(sinkpad));
301           }
302         }
303       }
304
305       previous = element;
306       if (!GST_IS_BIN(element)) prevelement = element;
307     }
308
309     i++;
310   }
311
312   // ghost all the src pads of the bin
313   if (prevelement != NULL) {
314     DEBUG("last element, ghosting all of %s's src pads to parent %s\n",
315           GST_ELEMENT_NAME(prevelement),GST_ELEMENT_NAME(GST_ELEMENT(parent)));
316     pads = gst_element_get_pad_list (prevelement);
317     while (pads) {
318       srcpad = GST_PAD (pads->data);
319       pads = g_list_next (pads);
320       if (!srcpad) DEBUG("much oddness, pad doesn't seem to exist\n");
321       else if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) {
322         gst_element_add_ghost_pad (GST_ELEMENT (parent), srcpad,
323 g_strdup_printf("%s-ghost",GST_PAD_NAME(srcpad)));
324         GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
325 GST_DEBUG_PAD_NAME(srcpad),GST_ELEMENT_NAME (parent),GST_PAD_NAME(srcpad));
326       }
327     }
328   }
329
330   priv->binlevel--;
331
332   if (retval) return retval;
333
334   if (closingchar != '\0')
335     DEBUG("returning IN THE WRONG PLACE\n");
336   else DEBUG("ending pipeline\n");
337   return i+1;
338 }
339
340 /**
341  * gst_parse_launch:
342  * @cmdline: the command line describing the pipeline
343  * @parent: the parent bin for the resulting pipeline
344  *
345  * Create a new pipeline based on command line syntax.
346  *
347  * Returns: ?
348  */
349 gint
350 gst_parse_launch(const gchar *cmdline,GstBin *parent)
351 {
352   gst_parse_priv priv;
353   gchar **argvn;
354   gint newargc;
355   gint i;
356   const gchar *cp, *start, *end;
357   GSList *string_list = NULL, *slist;
358
359   priv.bincount = 0;
360   priv.threadcount = 0;
361   priv.binlevel = 0;
362   priv.elementcounts = NULL;
363   priv.verbose = FALSE;
364   priv.debug = FALSE;
365
366   end = cmdline + strlen(cmdline);
367   newargc = 0;
368
369   // Extract the arguments to a gslist in reverse order
370   for (cp = cmdline; cp < end; ) {
371     i = strcspn(cp, "([{}]) \"");
372
373     if (i > 0) {
374       // normal argument - copy and add to the list
375       string_list = g_slist_prepend(string_list, g_strndup(cp, i));
376       newargc++;
377       cp += i;
378     }
379
380     // skip spaces
381     while (cp < end && *cp == ' ')
382       cp++;
383
384     // handle quoted arguments
385     if (*cp == '"') {
386       start = ++cp;
387
388       // find matching quote
389       while (cp < end && *cp != '"')
390         cp++;
391
392       // make sure we got it
393       if (cp == end) {
394         g_warning("gst_parse_launch: Unbalanced quote in command line");
395         // FIXME: The list leaks here
396         return 0;
397       }
398
399       // copy the string sans quotes
400       string_list = g_slist_prepend(string_list, g_strndup(start, cp - start));
401       newargc++;
402       cp += 2; // skip the quote aswell
403     }
404
405     // brackets exist in a separate argument slot
406     if (*cp && strchr("([{}])", *cp)) {
407       string_list = g_slist_prepend(string_list, g_strndup(cp, 1));
408       newargc++;
409       cp++;
410     }
411   }
412
413   // now allocate the new argv array
414   argvn = g_new0(char *,newargc);
415   GST_DEBUG(0,"got %d args\n",newargc);
416
417   // reverse the list and put the strings in the new array
418   i = newargc;
419
420   for (slist = string_list; slist; slist = slist->next)
421     argvn[--i] = slist->data;
422
423   g_slist_free(string_list);
424
425   // print them out
426   for (i=0;i<newargc;i++) {
427     GST_DEBUG(0,"arg %d is: %s\n",i,argvn[i]);
428   }
429
430   // set up the elementcounts hash
431   priv.elementcounts = g_hash_table_new(g_str_hash,g_str_equal);
432
433   // do it!
434   i = gst_parse_launch_cmdline(newargc,argvn,parent,&priv);
435
436 //  GST_DEBUG(0, "Finished - freeing temporary argument array");
437 //  g_strfreev(argvn);
438
439   return i;
440 }