more leak fixes
[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         ptr = gst_parse_unique_name(arg,priv);
209         element = gst_elementfactory_make(arg,ptr);
210         g_free(ptr);
211         if (!element) {
212           fprintf(stderr,"Couldn't create a '%s', no such element or need to run gstraemer-register?\n",arg);
213 //          exit(-1);
214         }   
215         VERBOSE("CREATED element %s\n",gst_element_get_name(element));
216         DEBUG("created element %s\n",gst_element_get_name(element));
217       }
218       
219       gst_bin_add (GST_BIN (parent), element);
220       elementcount++;
221   
222       if (srcpad != NULL) {
223         DEBUG("need to connect to sinkpad %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
224       
225         sinkpad = NULL;
226           
227         if (sinkpadname != NULL)
228           sinkpad = gst_element_get_pad(previous,sinkpadname);
229
230         if (!sinkpad) {
231           // check through the list to find the first sink pad   
232           pads = gst_element_get_pad_list(element); 
233           while (pads) {
234             sinkpad = GST_PAD(pads->data);
235             pads = g_list_next (pads);
236             if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) break;
237             sinkpad = NULL;
238           }
239         }
240   
241         if (!sinkpad) DEBUG("error, can't find a sink pad!!!\n");
242         else DEBUG("have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(sinkpad));
243        
244         VERBOSE("CONNECTING %s:%s and %s:%s\n",GST_DEBUG_PAD_NAME(srcpad),GST_DEBUG_PAD_NAME(sinkpad));
245         gst_pad_connect(srcpad,sinkpad);
246     
247         sinkpad = NULL;
248         srcpad = NULL;
249       }
250       
251       // if we're the first element, ghost all the sinkpads
252       if (elementcount == 1) {
253         DEBUG("first element, ghosting all of %s's sink pads to parent %s\n",
254               gst_element_get_name(element),gst_element_get_name(GST_ELEMENT(parent)));
255         pads = gst_element_get_pad_list (element);
256         while (pads) {
257           sinkpad = GST_PAD (pads->data);
258           pads = g_list_next (pads);
259           if (!sinkpad) DEBUG("much oddness, pad doesn't seem to exist\n");
260           else if (gst_pad_get_direction (sinkpad) == GST_PAD_SINK) {
261             gst_element_add_ghost_pad (GST_ELEMENT (parent), sinkpad);
262             DEBUG("ghosted %s:%s\n",GST_DEBUG_PAD_NAME(sinkpad));
263           }
264         }
265       }
266
267       previous = element;    
268       if (!GST_IS_BIN(element)) prevelement = element; 
269     }
270            
271     i++;
272   }
273           
274   // ghost all the src pads of the bin
275   if (prevelement != NULL) {
276     DEBUG("last element, ghosting all of %s's src pads to parent %s\n",
277           gst_element_get_name(prevelement),gst_element_get_name(GST_ELEMENT(parent)));
278     pads = gst_element_get_pad_list (prevelement);
279     while (pads) {
280       srcpad = GST_PAD (pads->data);
281       pads = g_list_next (pads);
282       if (!srcpad) DEBUG("much oddness, pad doesn't seem to exist\n");
283       else if (gst_pad_get_direction (srcpad) == GST_PAD_SRC) {
284         gst_element_add_ghost_pad (GST_ELEMENT (parent), srcpad);
285         DEBUG("ghosted %s:%s\n",GST_DEBUG_PAD_NAME(srcpad));
286       }
287     }
288   }
289           
290   priv->binlevel--;
291             
292   if (retval) return retval;
293         
294   if (closingchar != '\0')
295     DEBUG("returning IN THE WRONG PLACE\n");
296   else DEBUG("ending pipeline\n");
297   return i+1;
298
299
300 gint gst_parse_launch(const gchar *cmdline,GstBin *parent) {
301   gst_parse_priv priv;
302   gchar **argvn;
303   gint newargc;
304   gint len;
305   int i,j,k;
306
307   priv.bincount = 0;
308   priv.threadcount = 0;
309   priv.binlevel = 0;
310   priv.elementcounts = NULL;
311   priv.verbose = FALSE;
312   priv.debug = FALSE;
313
314   // first walk through quickly and see how many more slots we need
315   len = strlen(cmdline);
316   newargc = 1;
317   for (i=0;i<len;i++) {
318     // if it's a space, it denotes a new arg
319     if (cmdline[i] == ' ') newargc++;
320     // if it's a brace and isn't followed by a space, give it an arg
321     if (strchr("([{}])",cmdline[i])) {
322       // not followed by space, gets one
323       if (cmdline[i+1] != ' ') newargc++;
324     }
325   }
326
327   // now allocate the new argv array
328   argvn = g_new0(char *,newargc+1);
329   DEBUG("supposed to have %d args\n",newargc);
330
331   // now attempt to construct the new arg list
332   j = 0;k = 0;
333   for (i=0;i<len+1;i++) {
334     // if it's a delimiter
335     if (strchr("([{}]) ",cmdline[i]) || (cmdline[i] == '\0')) {
336       // extract the previous arg
337       if (i-k > 0) {
338         if (cmdline[k] == ' ') k++;
339         argvn[j] = g_new0(char,(i-k)+1);
340         memcpy(argvn[j],&cmdline[k],i-k);
341
342         // catch misparses
343         if (strlen(argvn[j]) > 0) j++;
344       }
345       k = i;
346
347       // if this is a bracket, construct a word
348       if ((cmdline[i] != ' ') && (cmdline[i] != '\0')) {
349         argvn[j++] = g_strdup_printf("%c",cmdline[i]);
350         k++;
351       }
352     }
353   }
354
355   // print them out
356   for (i=0;i<newargc;i++) {
357     DEBUG("arg %d is: %s\n",i,argvn[i]);
358   }
359
360   // set up the elementcounts hash
361   priv.elementcounts = g_hash_table_new(g_str_hash,g_str_equal);
362
363   return gst_parse_launch_cmdline(newargc,argvn,parent,&priv);
364 }