turned -launch capabilities into gst_parse_launch()
[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 "gstparse.h"
30
31 typedef struct _gst_parse_priv gst_parse_priv;
32 struct _gst_parse_priv {
33   guint bincount;
34   guint threadcount;
35   gint binlevel;
36   GHashTable *elementcounts;
37   gboolean verbose;
38   gboolean debug;
39 };
40
41 typedef struct _gst_parse_delayed_pad gst_parse_delayed_pad;
42 struct _gst_parse_delayed_pad {
43   gchar *name;
44   GstPad *peer;
45 };
46
47 /*
48 static void
49 gst_parse_newpad(GstElement *element,GstPad *pad,launch_delayed_pad *peer)
50 {
51   gst_info("have NEW_PAD signal\n");
52   // if it matches, connect it
53   if (!strcmp(gst_pad_get_name(pad),peer->name)) {
54     gst_pad_connect(pad,peer->peer);
55     gst_info("delayed connect of '%s' to '%s'\n",
56              gst_pad_get_name(pad),gst_pad_get_name(peer->peer));
57   }
58 }
59 */
60
61 gchar *
62 gst_parse_unique_name(gchar *type,gst_parse_priv *priv)
63 {
64   gint count;
65
66   count = GPOINTER_TO_INT(g_hash_table_lookup(priv->elementcounts,type));
67   count++;
68   g_hash_table_insert(priv->elementcounts,type,GINT_TO_POINTER(count));
69
70   return g_strdup_printf("%s%d",type,count-1);
71 }
72
73
74
75 static gint
76 gst_parse_launch_cmdline(int argc,char *argv[],GstBin *parent,gst_parse_priv *priv)
77 {
78   gint i = 0;
79   gchar *arg;
80   GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
81   gchar closingchar = '\0';
82   gint len;
83   gchar *ptr;
84   gchar *sinkpadname = NULL, *srcpadname = NULL;
85   GstPad *sinkpad = NULL, *srcpad = NULL;
86   GList *pads;
87   gint elementcount = 0;
88   gint retval = 0;
89           
90   priv->binlevel++; 
91
92   if (GST_IS_PIPELINE(parent)) { closingchar = '\0';DEBUG("in pipeline "); }
93   else if (GST_IS_THREAD(parent)) { closingchar = '}';DEBUG("in thread "); }
94   else { closingchar = ')';DEBUG("in bin "); }
95   DEBUG_NOPREFIX("%s\n",gst_element_get_name (GST_ELEMENT (parent)));
96           
97   while (i < argc) {
98     arg = argv[i];
99     // FIXME this is a lame solution for problems with the first parser
100     if (arg == NULL) { i++;continue; }
101     len = strlen(arg);
102     element = NULL;
103     DEBUG("** ARGUMENT is '%s'\n",arg);
104       
105     // a null that slipped through the reconstruction
106     if (len == 0) {
107       DEBUG("random arg, FIXME\n");
108       i++;
109       continue;
110
111     // end of the container  
112     } else if (arg[0] == closingchar) {
113       // time to finish off this bin
114       DEBUG("exiting container %s\n",gst_element_get_name (GST_ELEMENT (parent)));
115       retval = i+1;
116       break;
117             
118     // a pad connection   
119     } else if ((ptr = strchr(arg,'!'))) {
120       DEBUG("attempting to connect pads together....\n");
121
122       // if it starts with the !
123       if (arg[0] == '!') {
124         srcpadname = NULL;
125         // if there's a sinkpad...
126         if (len > 1)
127           sinkpadname = &arg[1];  
128         else
129           sinkpadname = NULL;
130       } else {
131         srcpadname = g_strndup(arg,(ptr-arg));
132         // if there's a sinkpad
133         if (len > (ptr-arg)+1)
134           sinkpadname = &ptr[1];
135         else
136           sinkpadname = NULL;
137       }
138     
139       DEBUG("have sinkpad %s, srcpad %s\n",sinkpadname,srcpadname);
140   
141       srcpad = NULL;
142   
143       // if the srcpadname doesn't have any commas in it, find an actual pad
144       if (!srcpadname || !strchr(srcpadname,',')) {
145         if (srcpadname != NULL) {
146           srcpad = gst_element_get_pad(previous,srcpadname);
147           if (!srcpad)
148             VERBOSE("NO SUCH pad %s in element %s\n",srcpadname,gst_element_get_name(previous));
149         }
150
151         if (srcpad == NULL) {
152           // check through the list to find the first sink pad   
153           pads = gst_element_get_pad_list(previous);
154           while (pads) {
155             srcpad = GST_PAD(pads->data);
156             pads = g_list_next (pads);
157             if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) break;
158             srcpad = NULL;
159           }
160         }
161   
162         if (!srcpad) DEBUG("error, can't find a src pad!!!\n");
163         else DEBUG("have src pad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
164       }
165
166     // argument with = in it
167     } else if (strstr(arg, "=")) {
168       gchar * argname;
169       gchar * argval;
170       gchar * pos = strstr(arg, "=");
171       // we have an argument
172       argname = arg;
173       pos[0] = '\0';
174       argval = pos+1;
175       DEBUG("attempting to set argument '%s' to '%s' on element '%s'\n",
176             argname,argval,gst_element_get_name(previous));
177       gtk_object_set(GTK_OBJECT(previous),argname,argval,NULL);
178       g_free(argname);
179
180     // element or argument, or beginning of bin or thread
181     } else {
182       DEBUG("have element or bin/thread\n");
183       // if we have a bin or thread starting
184       if (strchr("({",arg[0])) {   
185         if (arg[0] == '(') {
186           // create a bin and add it to the current parent
187           element = gst_bin_new(g_strdup_printf("bin%d",priv->bincount++));
188           if (!element) {    
189             fprintf(stderr,"Couldn't create a bin!\n");
190 //            exit(-1);
191           }
192           VERBOSE("CREATED bin %s\n",gst_element_get_name(element));
193         } else if (arg[0] == '{') {
194           // create a thread and add it to the current parent
195           element = gst_thread_new(g_strdup_printf("thread%d",priv->threadcount++));
196           if (!element) {
197             fprintf(stderr,"Couldn't create a thread!\n");
198 //            exit(-1);
199           }
200           VERBOSE("CREATED thread %s\n",gst_element_get_name(element));
201         }
202
203         i += gst_parse_launch_cmdline(argc - i, argv + i + 1, GST_BIN (element), priv);
204           
205       } else {
206         // we have an element
207         DEBUG("attempting to create element '%s'\n",arg);
208         element = gst_elementfactory_make(arg,gst_parse_unique_name(arg,priv));
209         if (!element) {
210           fprintf(stderr,"Couldn't create a '%s', no such element or need to run gstraemer-register?\n",arg);
211 //          exit(-1);
212         }   
213         VERBOSE("CREATED element %s\n",gst_element_get_name(element));
214         DEBUG("created element %s\n",gst_element_get_name(element));
215       }
216       
217       gst_bin_add (GST_BIN (parent), element);
218       elementcount++;
219   
220       if (srcpad != NULL) {
221         DEBUG("need to connect to sinkpad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
222       
223         sinkpad = NULL;
224           
225         if (sinkpadname != NULL)
226           sinkpad = gst_element_get_pad(previous,sinkpadname);
227
228         if (!sinkpad) {
229           // check through the list to find the first sink pad   
230           pads = gst_element_get_pad_list(element); 
231           while (pads) {
232             sinkpad = GST_PAD(pads->data);
233             pads = g_list_next (pads);
234             if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) break;
235             sinkpad = NULL;
236           }
237         }
238   
239         if (!sinkpad) DEBUG("error, can't find a sink pad!!!\n");
240         else DEBUG("have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(sinkpad));
241        
242         VERBOSE("CONNECTING %s:%s and %s:%s\n",GST_DEBUG_PAD_NAME(srcpad),GST_DEBUG_PAD_NAME(sinkpad));
243         gst_pad_connect(srcpad,sinkpad);
244     
245         sinkpad = NULL;
246         srcpad = NULL;
247       }
248       
249       // if we're the first element, ghost all the sinkpads
250       if (elementcount == 1) {
251         DEBUG("first element, ghosting all of %s's sink pads to parent %s\n",
252               gst_element_get_name(element),gst_element_get_name(GST_ELEMENT(parent)));
253         pads = gst_element_get_pad_list (element);
254         while (pads) {
255           sinkpad = GST_PAD (pads->data);
256           pads = g_list_next (pads);
257           if (!sinkpad) DEBUG("much oddness, pad doesn't seem to exist\n");
258           else if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) {
259             gst_element_add_ghost_pad (GST_ELEMENT (parent), sinkpad);
260             DEBUG("ghosted %s:%s\n",GST_DEBUG_PAD_NAME(sinkpad));
261           }
262         }
263       }
264
265       previous = element;    
266       if (!GST_IS_BIN(element)) prevelement = element; 
267     }
268            
269     i++;
270   }
271           
272   // ghost all the src pads of the bin
273   if (prevelement != NULL) {
274     DEBUG("last element, ghosting all of %s's src pads to parent %s\n",
275           gst_element_get_name(prevelement),gst_element_get_name(GST_ELEMENT(parent)));
276     pads = gst_element_get_pad_list (prevelement);
277     while (pads) {
278       srcpad = GST_PAD (pads->data);
279       pads = g_list_next (pads);
280       if (!srcpad) DEBUG("much oddness, pad doesn't seem to exist\n");
281       else if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) {
282         gst_element_add_ghost_pad (GST_ELEMENT (parent), srcpad);
283         DEBUG("ghosted %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
284       }
285     }
286   }
287           
288   priv->binlevel--;
289             
290   if (retval) return retval;
291         
292   if (closingchar != '\0')
293     DEBUG("returning IN THE WRONG PLACE\n");
294   else DEBUG("ending pipeline\n");
295   return i+1;
296
297
298 gint gst_parse_launch(const gchar *cmdline,GstBin *parent) {
299   gst_parse_priv priv;
300   gchar **argvn;
301   gint newargc;
302   gint len;
303   int i,j,k;
304
305   priv.bincount = 0;
306   priv.threadcount = 0;
307   priv.binlevel = 0;
308   priv.elementcounts = NULL;
309   priv.verbose = FALSE;
310   priv.debug = FALSE;
311
312   // first walk through quickly and see how many more slots we need
313   len = strlen(cmdline);
314   newargc = 1;
315   for (i=0;i<len;i++) {
316     // if it's a space, it denotes a new arg
317     if (cmdline[i] == ' ') newargc++;
318     // if it's a brace and isn't followed by a space, give it an arg
319     if (strchr("([{}])",cmdline[i])) {
320       // not followed by space, gets one
321       if (cmdline[i+1] != ' ') newargc++;
322     }
323   }
324
325   // now allocate the new argv array
326   argvn = g_new0(char *,newargc+1);
327   DEBUG("supposed to have %d args\n",newargc);
328
329   // now attempt to construct the new arg list
330   j = 0;k = 0;
331   for (i=0;i<len+1;i++) {
332     // if it's a delimiter
333     if (strchr("([{}]) ",cmdline[i]) || (cmdline[i] == '\0')) {
334       // extract the previous arg
335       if (i-k > 0) {
336         if (cmdline[k] == ' ') k++;
337         argvn[j] = g_new0(char,(i-k)+1);
338         memcpy(argvn[j],&cmdline[k],i-k);
339
340         // catch misparses
341         if (strlen(argvn[j]) > 0) j++;
342       }
343       k = i;
344
345       // if this is a bracket, construct a word
346       if ((cmdline[i] != ' ') && (cmdline[i] != '\0')) {
347         argvn[j++] = g_strdup_printf("%c",cmdline[i]);
348         k++;
349       }
350     }
351   }
352
353   // print them out
354   for (i=0;i<newargc;i++) {
355     DEBUG("arg %d is: %s\n",i,argvn[i]);
356   }
357
358   // set up the elementcounts hash
359   priv.elementcounts = g_hash_table_new(g_str_hash,g_str_equal);
360
361   return gst_parse_launch_cmdline(newargc,argvn,parent,&priv);
362 }