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