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