sync with the current CVS glib API
[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 #define GST_PARSE_LISTPAD(list)   ((GstPad*)(list->data))
28
29 #include <string.h>
30
31 #include "gst_private.h"
32 #include "gstparse.h"
33 #include "gstpipeline.h"
34 #include "gstthread.h"
35 #include "gstutils.h"
36
37 typedef struct _gst_parse_priv gst_parse_priv;
38 struct _gst_parse_priv {
39   guint bincount;
40   guint threadcount;
41   gint binlevel;
42   GHashTable *elementcounts;
43   gboolean verbose;
44   gboolean debug;
45 };
46
47 typedef struct _gst_parse_delayed_pad gst_parse_delayed_pad;
48 struct _gst_parse_delayed_pad {
49   gchar *name;
50   GstPad *peer;
51 };
52
53 /* FIXME need to either revive this, or have pad->padtemplate connections in core
54 static void
55 gst_parse_newpad(GstElement *element,GstPad *pad,launch_delayed_pad *peer)
56 {
57   gst_info("have NEW_PAD signal\n");
58   // if it matches, connect it
59   if (!strcmp(GST_PAD_NAME(pad),peer->name)) {
60     gst_pad_connect(pad,peer->peer);
61     gst_info("delayed connect of '%s' to '%s'\n",
62              GST_PAD_NAME(pad),GST_PAD_NAME(peer->peer));
63   }
64 }
65 */
66
67 typedef struct {
68   gchar *srcpadname;
69   GstPad *target;
70 } dyn_connect;
71
72 static void have_eos (void)
73 {
74   DEBUG ("I have eos on the first element\n");
75   exit (0);
76 }
77
78 static void
79 dynamic_connect (GstElement *element, GstPad *newpad, gpointer data)
80 {
81   dyn_connect *connect = (dyn_connect *)data;
82
83   if (!strcmp (gst_pad_get_name (newpad), connect->srcpadname)) {
84     gst_pad_connect (newpad, connect->target);
85   }
86 }
87
88 static gchar *
89 gst_parse_unique_name(gchar *type,gst_parse_priv *priv)
90 {
91   gint count;
92
93   count = GPOINTER_TO_INT(g_hash_table_lookup(priv->elementcounts,type));
94   count++;
95   g_hash_table_insert(priv->elementcounts,type,GINT_TO_POINTER(count));
96
97   return g_strdup_printf("%s%d",type,count-1);
98 }
99
100
101
102 static gint
103 gst_parse_launch_cmdline(int argc,char *argv[],GstBin *parent,gst_parse_priv *priv)
104 {
105   gint i = 0, j = 0;
106   gchar *arg;
107   GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
108   gchar closingchar = '\0';
109   gint len;
110   gchar *ptr;
111   gchar *sinkpadname = NULL, *srcpadname = NULL, *tempname;
112   GstPad *temppad;
113   GSList *sinkpads = NULL, *srcpads = NULL;
114   gint numsrcpads = 0, numsinkpads = 0;
115   GList *pads;
116   gint elementcount = 0;
117   gint retval = 0;
118
119   priv->binlevel++;
120
121   if (GST_IS_PIPELINE(parent)) { closingchar = '\0';DEBUG("in pipeline "); }
122   else if (GST_IS_THREAD(parent)) { closingchar = '}';DEBUG("in thread "); }
123   else { closingchar = ')';DEBUG("in bin "); }
124   DEBUG_NOPREFIX("%s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
125
126   while (i < argc) {
127     arg = argv[i];
128     // FIXME this is a lame solution for problems with the first parser
129     if (arg == NULL) { i++;continue; }
130     len = strlen(arg);
131     element = NULL;
132     DEBUG("** ARGUMENT is '%s'\n",arg);
133
134     // a null that slipped through the reconstruction
135     if (len == 0) {
136       DEBUG("random arg, FIXME\n");
137       i++;
138       continue;
139
140     // end of the container
141     } else if (arg[0] == closingchar) {
142       // time to finish off this bin
143       DEBUG("exiting container %s\n",GST_ELEMENT_NAME (GST_ELEMENT (parent)));
144       retval = i+1;
145       break;
146
147     // a pad connection
148     } else if ((ptr = strchr(arg,'!'))) {
149       DEBUG("attempting to connect pads together....\n");
150
151       // if it starts with the !
152       if (arg[0] == '!') {
153         srcpadname = NULL;
154         // if there's a sinkpad...
155         if (len > 1)
156           sinkpadname = &arg[1];
157         else
158           sinkpadname = NULL;
159       } else {
160         srcpadname = g_strndup(arg,(ptr-arg));
161         // if there's a sinkpad
162         if (len > (ptr-arg)+1)
163           sinkpadname = &ptr[1];
164         else
165           sinkpadname = NULL;
166       }
167
168       GST_DEBUG(0,"have srcpad %s, sinkpad %s\n",srcpadname,sinkpadname);
169
170       g_slist_free(srcpads);
171       srcpads = NULL;
172       numsrcpads=0;
173       tempname=NULL;
174
175       // find src pads
176       if (srcpadname != NULL) {
177         while (1){
178           // split name at commas
179           if ((ptr = strchr(srcpadname,','))){
180             tempname = g_strndup(srcpadname,(ptr-srcpadname));
181             srcpadname = &ptr[1];
182           } else {
183             tempname = srcpadname;
184           }
185           
186           // look for pad with that name
187           if ((temppad = gst_element_get_pad(previous,tempname))){
188             srcpads = g_slist_append(srcpads,temppad);
189             numsrcpads++;
190           }
191           
192           // try to create a pad using that padtemplate name
193           else if ((temppad = gst_element_request_pad_by_name(previous,tempname))) {
194             srcpads = g_slist_append(srcpads,temppad);
195             numsrcpads++;
196           }
197           if (!temppad) {
198             GST_DEBUG(0,"NO SUCH pad %s in element %s\n",tempname,GST_ELEMENT_NAME(previous));
199           } else {
200             GST_DEBUG(0,"have src pad %s:%s\n",GST_DEBUG_PAD_NAME(temppad));
201           }
202           
203           // if there is no more commas in srcpadname then we're done
204           if (tempname == srcpadname) break;
205           g_free(tempname);
206         }
207       }
208       else {
209         // check through the list to find the first sink pad
210         GST_DEBUG(0,"CHECKING through element %s for pad named %s\n",GST_ELEMENT_NAME(previous),srcpadname);
211         pads = gst_element_get_pad_list(previous);
212         while (pads) {
213           temppad = GST_PARSE_LISTPAD(pads);
214           GST_DEBUG(0,"have pad %s:%s\n",GST_DEBUG_PAD_NAME(temppad));
215           if (GST_IS_GHOST_PAD(temppad)) GST_DEBUG(0,"it's a ghost pad\n");
216           if (gst_pad_get_direction (temppad) == GST_PAD_SRC){
217             srcpads = g_slist_append(srcpads,temppad);
218             numsrcpads++;
219             break;
220           }
221           pads = g_list_next (pads);
222         }
223         if (!srcpads) GST_DEBUG(0,"error, can't find a src pad!!!\n");
224         else GST_DEBUG(0,"have src pad %s:%s\n",GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(srcpads)));
225       }
226
227     // argument with = in it
228     } else if (strstr(arg, "=")) {
229       gchar * argname;
230       gchar * argval;
231       gchar * pos = strstr(arg, "=");
232       // we have an argument
233       argname = arg;
234       pos[0] = '\0';
235       argval = pos+1;
236       GST_DEBUG(0,"attempting to set argument '%s' to '%s' on element '%s'\n",
237             argname,argval,GST_ELEMENT_NAME(previous));
238       gst_util_set_object_arg (G_OBJECT(previous), argname, argval);
239       g_free(argname);
240
241     // element or argument, or beginning of bin or thread
242     } else if (arg[0] == '[') {
243       // we have the start of a name of the preceding element.
244       // rename previous element to next arg.
245       if (arg[1] != '\0') {
246         fprintf(stderr,"error, unexpected junk after [\n");
247         exit(-1);
248       }
249       i++;
250       if (i < argc) {
251         gst_element_set_name(previous, argv[i]);
252       } else {
253         fprintf(stderr,"error, expected element name, found end of arguments\n");
254         exit(-1);
255       }
256       i++;
257       if (i >= argc) {
258         fprintf(stderr,"error, expected ], found end of arguments\n");
259         exit(-1);
260       } else if (strcmp(argv[i], "]") != 0) {
261         fprintf(stderr,"error, expected ], found '%s'\n", argv[i]);
262         exit(-1);
263       }
264     } else {
265       DEBUG("have element or bin/thread\n");
266       // if we have a bin or thread starting
267       if (strchr("({",arg[0])) {
268         if (arg[0] == '(') {
269           // create a bin and add it to the current parent
270           element = gst_bin_new(g_strdup_printf("bin%d",priv->bincount++));
271           if (!element) {
272             fprintf(stderr,"Couldn't create a bin!\n");
273 //            exit(-1);
274           }
275           GST_DEBUG(0,"CREATED bin %s\n",GST_ELEMENT_NAME(element));
276         } else if (arg[0] == '{') {
277           // create a thread and add it to the current parent
278           element = gst_thread_new(g_strdup_printf("thread%d",priv->threadcount++));
279           if (!element) {
280             fprintf(stderr,"Couldn't create a thread!\n");
281 //            exit(-1);
282           }
283           GST_DEBUG(0,"CREATED thread %s\n",GST_ELEMENT_NAME(element));
284         } else {
285           DEBUG("error in parser, unexpected symbol, FIXME\n");
286           i++;
287           continue;
288         }
289
290         i += gst_parse_launch_cmdline(argc - i, argv + i + 1, GST_BIN (element), priv);
291
292       } else {
293         // we have an element
294         DEBUG("attempting to create element '%s'\n",arg);
295         ptr = gst_parse_unique_name(arg,priv);
296         element = gst_elementfactory_make(arg,ptr);
297         g_free(ptr);
298         if (!element) {
299           fprintf(stderr,"Couldn't create a '%s', no such element or need to run gstreamer-register?\n",arg);
300           exit(-1);
301         }
302         GST_DEBUG(0,"CREATED element %s\n",GST_ELEMENT_NAME(element));
303       }
304
305       gst_bin_add (GST_BIN (parent), element);
306       elementcount++;
307
308       g_slist_free(sinkpads);
309       sinkpads = NULL;
310       numsinkpads=0;
311       tempname=NULL;
312
313       // find sink pads
314       if (sinkpadname != NULL) {
315         while (1){
316           // split name at commas
317           if ((ptr = strchr(sinkpadname,','))){
318             tempname = g_strndup(sinkpadname,(ptr-sinkpadname));
319             sinkpadname = &ptr[1];
320           } else {
321             tempname = sinkpadname;
322           }
323           
324           // look for pad with that name
325           if ((temppad = gst_element_get_pad(element,tempname))){
326             sinkpads = g_slist_append(sinkpads,temppad);
327             numsinkpads++;
328           }
329           
330           // try to create a pad using that padtemplate name
331           else if ((temppad = gst_element_request_pad_by_name(element,tempname))) {
332             sinkpads = g_slist_append(sinkpads,temppad);
333             numsinkpads++;
334           }
335           if (!temppad) {
336             GST_DEBUG(0,"NO SUCH pad %s in element %s\n",tempname,GST_ELEMENT_NAME(element));
337           } else {
338             GST_DEBUG(0,"have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(temppad));
339           }
340           
341           // if there is no more commas in sinkpadname then we're done
342           if (tempname == sinkpadname) break;
343           g_free(tempname);
344         }
345       }
346       else {
347         // check through the list to find the first sink pad
348         pads = gst_element_get_pad_list(element);
349         while (pads) {
350           temppad = GST_PAD(pads->data);
351           pads = g_list_next (pads);
352           if (gst_pad_get_direction (temppad) == GST_PAD_SINK){
353             sinkpads = g_slist_append(sinkpads,temppad);
354             numsinkpads++;
355             break;
356           }
357         }
358       }
359
360       if (!sinkpads) GST_DEBUG(0,"can't find a sink pad for %s\n", gst_element_get_name (previous));
361       else GST_DEBUG(0,"have sink pad %s:%s\n",GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(sinkpads)));
362
363       if (!srcpads && sinkpads && previous) {
364         dyn_connect *connect = g_malloc (sizeof (dyn_connect));
365
366         connect->srcpadname = srcpadname;
367         connect->target = GST_PARSE_LISTPAD(sinkpads);
368
369         GST_DEBUG(0,"SETTING UP dynamic connection %s:%s and %s:%s\n",
370           gst_element_get_name (previous),
371           srcpadname,
372           GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(sinkpads)));
373
374         g_signal_connect (G_OBJECT (previous), "new_pad", dynamic_connect, connect);
375         g_signal_connect (G_OBJECT (previous), "new_ghost_pad", dynamic_connect, connect);
376       }
377       else {
378         for (j=0; (j<numsrcpads) && (j<numsinkpads); j++){
379           GST_DEBUG(0,"CONNECTING %s:%s and %s:%s\n",
380             GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(g_slist_nth(srcpads,j))),
381             GST_DEBUG_PAD_NAME(GST_PARSE_LISTPAD(g_slist_nth(sinkpads,j))));
382           gst_pad_connect(
383             GST_PARSE_LISTPAD(g_slist_nth(srcpads,j)),
384             GST_PARSE_LISTPAD(g_slist_nth(sinkpads,j)));
385         }
386       }
387               
388       g_slist_free(srcpads);
389       srcpads = NULL;
390       
391       g_slist_free(sinkpads);
392       sinkpads = NULL;      
393
394       // thomas: if we're the first element, connect eos signal
395       if (elementcount == 1) 
396       {
397         g_signal_connect (G_OBJECT (element), "eos", have_eos, NULL);
398
399       }
400       // if we're the first element, ghost all the sinkpads
401       if (elementcount == 1) {
402         DEBUG("first element, ghosting all of %s's sink pads to parent %s\n",
403               GST_ELEMENT_NAME(element),GST_ELEMENT_NAME(GST_ELEMENT(parent)));
404         pads = gst_element_get_pad_list (element);
405         while (pads) {
406           temppad = GST_PAD (pads->data);
407           pads = g_list_next (pads);
408           if (!temppad) DEBUG("much oddness, pad doesn't seem to exist\n");
409           else if (gst_pad_get_direction (temppad) == GST_PAD_SINK) {
410             gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
411 g_strdup_printf("%s-ghost",GST_PAD_NAME(temppad)));
412             GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
413                       GST_DEBUG_PAD_NAME(temppad),GST_ELEMENT_NAME(GST_ELEMENT(parent)),GST_PAD_NAME(temppad));
414           }
415         }
416       }
417
418       previous = element;
419       if (!GST_IS_BIN(element)) prevelement = element;
420     }
421
422     i++;
423   }
424
425   // ghost all the src pads of the bin
426   if (prevelement != NULL) {
427     DEBUG("last element, ghosting all of %s's src pads to parent %s\n",
428           GST_ELEMENT_NAME(prevelement),GST_ELEMENT_NAME(GST_ELEMENT(parent)));
429     pads = gst_element_get_pad_list (prevelement);
430     while (pads) {
431       temppad = GST_PAD (pads->data);
432       pads = g_list_next (pads);
433       if (!temppad) DEBUG("much oddness, pad doesn't seem to exist\n");
434       else if (gst_pad_get_direction (temppad) == GST_PAD_SRC) {
435         gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
436 g_strdup_printf("%s-ghost",GST_PAD_NAME(temppad)));
437         GST_DEBUG(0,"GHOSTED %s:%s to %s as %s-ghost\n",
438 GST_DEBUG_PAD_NAME(temppad),GST_ELEMENT_NAME (parent),GST_PAD_NAME(temppad));
439       }
440     }
441   }
442
443   priv->binlevel--;
444
445   if (retval) return retval;
446
447   if (closingchar != '\0')
448     DEBUG("returning IN THE WRONG PLACE\n");
449   else DEBUG("ending pipeline\n");
450   return i+1;
451 }
452
453 /**
454  * gst_parse_launch:
455  * @cmdline: the command line describing the pipeline
456  * @parent: the parent bin for the resulting pipeline
457  *
458  * Create a new pipeline based on command line syntax.
459  *
460  * Returns: ?
461  */
462 gint
463 gst_parse_launch(const gchar *cmdline,GstBin *parent)
464 {
465   gst_parse_priv priv;
466   gchar **argvn;
467   gint newargc;
468   gint i;
469   const gchar *cp, *start, *end;
470   gchar *temp;
471   GSList *string_list = NULL, *slist;
472
473   priv.bincount = 0;
474   priv.threadcount = 0;
475   priv.binlevel = 0;
476   priv.elementcounts = NULL;
477   priv.verbose = FALSE;
478   priv.debug = FALSE;
479
480   end = cmdline + strlen(cmdline);
481   newargc = 0;
482
483   temp = "";
484
485   // Extract the arguments to a gslist in reverse order
486   for (cp = cmdline; cp < end; ) {
487     i = strcspn(cp, "([{}]) \"\\");
488
489     if (i > 0) {
490       temp = g_strconcat (temp, g_strndup (cp, i), NULL);
491       
492       // see if we have an escape char
493       if (cp[i] != '\\') {
494         // normal argument - copy and add to the list
495         string_list = g_slist_prepend(string_list, temp);
496         newargc++;
497         temp = "";
498       }
499       else {
500         temp = g_strconcat (temp, g_strndup (&cp[++i], 1), NULL);
501       }
502       cp += i;
503     }
504
505     // skip spaces
506     while (cp < end && *cp == ' ') {
507       cp++;
508     }
509
510     // handle quoted arguments
511     if (*cp == '"') {
512       start = ++cp;
513
514       // find matching quote
515       while (cp < end && *cp != '"')
516         cp++;
517
518       // make sure we got it
519       if (cp == end) {
520         g_warning("gst_parse_launch: Unbalanced quote in command line");
521         // FIXME: The list leaks here
522         return 0;
523       }
524
525       // copy the string sans quotes
526       string_list = g_slist_prepend(string_list, g_strndup(start, cp - start));
527       newargc++;
528       cp += 2; // skip the quote aswell
529     }
530
531     // brackets exist in a separate argument slot
532     if (*cp && strchr("([{}])", *cp)) {
533       string_list = g_slist_prepend(string_list, g_strndup(cp, 1));
534       newargc++;
535       cp++;
536     }
537   }
538
539   // now allocate the new argv array
540   argvn = g_new0(char *,newargc);
541   GST_DEBUG(0,"got %d args\n",newargc);
542
543   // reverse the list and put the strings in the new array
544   i = newargc;
545
546   for (slist = string_list; slist; slist = slist->next)
547     argvn[--i] = slist->data;
548
549   g_slist_free(string_list);
550
551   // print them out
552   for (i=0;i<newargc;i++) {
553     GST_DEBUG(0,"arg %d is: %s\n",i,argvn[i]);
554   }
555
556   // set up the elementcounts hash
557   priv.elementcounts = g_hash_table_new(g_str_hash,g_str_equal);
558
559   // do it!
560   i = gst_parse_launch_cmdline(newargc,argvn,parent,&priv);
561
562 //  GST_DEBUG(0, "Finished - freeing temporary argument array");
563 //  g_strfreev(argvn);
564
565   return i;
566 }
567