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