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