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