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