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