2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
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.
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.
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.
23 /* #define DEBUG(format,args...) g_print (format, ##args) */
24 #define DEBUG(format,args...)
25 #define DEBUG_NOPREFIX(format,args...)
26 #define VERBOSE(format,args...)
28 #define GST_PARSE_LISTPAD(list) ((GstPad*)(list->data))
32 #include "gst_private.h"
34 #include "gstpipeline.h"
35 #include "gstthread.h"
38 typedef struct _gst_parse_priv gst_parse_priv;
39 struct _gst_parse_priv
44 GHashTable *elementcounts;
49 typedef struct _gst_parse_delayed_pad gst_parse_delayed_pad;
50 struct _gst_parse_delayed_pad
65 dynamic_connect (GstElement * element, GstPad * newpad, gpointer data)
67 dyn_connect *connect = (dyn_connect *) data;
69 if (!strcmp (gst_pad_get_name (newpad), connect->srcpadname)) {
70 gst_element_set_state (connect->pipeline, GST_STATE_PAUSED);
71 gst_pad_connect (newpad, connect->target);
72 gst_element_set_state (connect->pipeline, GST_STATE_PLAYING);
77 gst_parse_unique_name (const gchar * type, gst_parse_priv * priv)
82 tmp = g_hash_table_lookup (priv->elementcounts, type);
83 count = GPOINTER_TO_INT (tmp);
85 g_hash_table_insert (priv->elementcounts, g_strdup (type), GINT_TO_POINTER (count));
87 return g_strdup_printf ("%s%d", type, count - 1);
91 gst_parse_launchv_recurse (const gchar **argv, GstBin * parent, gst_parse_priv * priv)
95 GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
96 gchar closingchar = '\0';
100 gchar *sinkpadname = NULL, *srcpadname = NULL, *tempname;
102 GSList *sinkpads = NULL, *srcpads = NULL;
103 gint numsrcpads = 0, numsinkpads = 0;
105 gint elementcount = 0;
107 gboolean backref = FALSE;
110 priv = g_new0 (gst_parse_priv, 1);
111 priv->elementcounts = g_hash_table_new (g_str_hash, g_str_equal);
116 if (GST_IS_PIPELINE (parent)) {
118 DEBUG ("in pipeline ");
119 } else if (GST_IS_THREAD (parent)) {
121 DEBUG ("in thread ");
126 DEBUG_NOPREFIX ("%s\n", GST_ELEMENT_NAME (GST_ELEMENT (parent)));
128 while ((arg = argv[++i])) {
131 DEBUG ("** ARGUMENT is '%s'\n", arg);
134 DEBUG ("random arg, FIXME\n");
137 } else if (arg[0] == closingchar) {
138 DEBUG ("exiting container %s\n", GST_ELEMENT_NAME (GST_ELEMENT (parent)));
142 } else if ((cptr = strchr (arg, '!'))) {
143 DEBUG ("attempting to connect pads together....\n");
145 /* if it starts with the ! */
148 /* if there's a sinkpad... */
150 sinkpadname = g_strdup (&arg[1]);
154 srcpadname = g_strndup (arg, (cptr - arg));
155 /* if there's a sinkpad */
156 if (len > (cptr - arg) + 1)
157 sinkpadname = g_strdup(&cptr[1]);
162 if (srcpadname && (ptr = strchr (srcpadname, '.'))) {
163 gchar *element_name = srcpadname;
166 *ptr = '\0'; /* it was a '.' before */
168 GST_DEBUG (0, "have pad for element %s\n", element_name);
169 new = gst_bin_get_by_name_recurse_up (parent, element_name);
171 GST_DEBUG (0, "element %s does not exist! trying to continue\n", element_name);
174 srcpadname = ptr + 1;
179 GST_DEBUG (0, "have srcpad %s, sinkpad %s\n", srcpadname, sinkpadname);
181 g_slist_free (srcpads);
187 if (srcpadname != NULL) {
189 /* split name at commas */
190 if ((ptr = strchr (srcpadname, ','))) {
191 tempname = srcpadname;
192 srcpadname = &ptr[1];
195 tempname = srcpadname;
198 /* look for pad with that name */
199 if ((temppad = gst_element_get_pad (previous, tempname))) {
200 srcpads = g_slist_append (srcpads, temppad);
204 /* try to create a pad using that padtemplate name */
205 else if ((temppad = gst_element_request_pad_by_name (previous, tempname))) {
206 srcpads = g_slist_append (srcpads, temppad);
210 GST_DEBUG (0, "NO SUCH pad %s in element %s\n", tempname, GST_ELEMENT_NAME (previous));
212 GST_DEBUG (0, "have src pad %s:%s\n", GST_DEBUG_PAD_NAME (temppad));
215 /* if there is no more commas in srcpadname then we're done */
216 if (tempname == srcpadname)
221 /* check through the list to find the first src pad */
222 GST_DEBUG (0, "CHECKING element %s for pad named %s\n", GST_ELEMENT_NAME (previous),
224 pads = gst_element_get_pad_list (previous);
226 temppad = GST_PARSE_LISTPAD (pads);
227 GST_DEBUG (0, "have pad %s:%s\n", GST_DEBUG_PAD_NAME (temppad));
228 if (GST_IS_GHOST_PAD (temppad))
229 GST_DEBUG (0, "it's a ghost pad\n");
230 if (gst_pad_get_direction (temppad) == GST_PAD_SRC) {
231 srcpads = g_slist_append (srcpads, temppad);
235 pads = g_list_next (pads);
238 GST_DEBUG (0, "error, can't find a src pad!!!\n");
240 GST_DEBUG (0, "have src pad %s:%s\n", GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (srcpads)));
242 } else if (strchr (arg, '=')) {
247 DEBUG ("have a property\n");
249 /* we have a property */
250 propname = g_strdup (arg);
251 pos = strchr (propname, '=');
255 /* use g_object_set in the future when gst_util_{set|get} go away */
256 GST_DEBUG (0, "attempting to set property '%s' to '%s' on element '%s'\n",
257 propname, propval, GST_ELEMENT_NAME (previous));
258 gst_util_set_object_arg (G_OBJECT (previous), propname, propval);
260 } else if (arg[0] == '[') {
261 DEBUG ("have element name\n");
263 if (arg[1] != '\0') {
264 fprintf (stderr, "error, unexpected junk after [\n");
265 return GST_PARSE_ERROR_SYNTAX;
268 if ((arg = argv[++i])) {
270 g_critical("parser error, please report");
271 return GST_PARSE_ERROR_INTERNAL;
273 gst_element_set_name (previous, arg);
275 fprintf (stderr, "error, expected element name, found end of arguments\n");
276 return GST_PARSE_ERROR_SYNTAX;
279 if ((arg = argv[++i])) {
280 if (strcmp (arg, "]") != 0) {
281 fprintf (stderr, "error, expected ], found '%s'\n", arg);
282 return GST_PARSE_ERROR_SYNTAX;
285 fprintf (stderr, "error, expected ], found end of arguments\n");
286 return GST_PARSE_ERROR_SYNTAX;
289 /* we have an element, a bin, or a thread. we treat these together because
290 * once we rech them we have to resolve any dangling connections from
291 * previous array arguments. */
293 if (strchr ("({", arg[0])) {
294 DEBUG ("have bin or thread\n");
297 /* create a bin and add it to the current parent */
298 element = gst_bin_new (g_strdup_printf ("bin%d", priv->bincount++));
300 fprintf (stderr, "Couldn't create a bin!\n");
301 return GST_PARSE_ERROR_CREATING_ELEMENT;
303 GST_DEBUG (0, "CREATED bin %s\n", GST_ELEMENT_NAME (element));
304 } else if (arg[0] == '{') {
305 /* create a thread and add it to the current parent */
306 element = gst_thread_new (g_strdup_printf ("thread%d", priv->threadcount++));
308 fprintf (stderr, "Couldn't create a thread!\n");
309 return GST_PARSE_ERROR_CREATING_ELEMENT;
311 GST_DEBUG (0, "CREATED thread %s\n", GST_ELEMENT_NAME (element));
313 fprintf (stderr, "Illegal argument: %s\n", arg);
314 return GST_PARSE_ERROR_CREATING_ELEMENT;
317 gst_bin_add (GST_BIN (parent), element);
319 j = gst_parse_launchv_recurse (argv + i + 1, GST_BIN (element), priv);
320 /* check for parse error */
324 /* we will get autoincremented at the while */
328 /* we have an element */
329 DEBUG ("attempting to create element '%s'\n", arg);
331 ptr = gst_parse_unique_name (arg, priv);
332 element = gst_elementfactory_make (arg, ptr);
335 #ifndef GST_DISABLE_REGISTRY
337 "Couldn't create a '%s', no such element or need to run gst-register?\n", arg);
339 fprintf (stderr, "Couldn't create a '%s', no such element or need to load plugin?\n",
342 return GST_PARSE_ERROR_NOSUCH_ELEMENT;
345 GST_DEBUG (0, "CREATED element %s\n", GST_ELEMENT_NAME (element));
346 gst_bin_add (GST_BIN (parent), element);
351 g_slist_free (sinkpads);
357 if (sinkpadname != NULL) {
359 /* split name at commas */
360 if ((ptr = strchr (sinkpadname, ','))) {
361 tempname = g_strndup (sinkpadname, (ptr - sinkpadname));
362 sinkpadname = &ptr[1];
364 tempname = sinkpadname;
367 /* look for pad with that name */
368 if ((temppad = gst_element_get_pad (element, tempname))) {
369 sinkpads = g_slist_append (sinkpads, temppad);
373 /* try to create a pad using that padtemplate name */
374 else if ((temppad = gst_element_request_pad_by_name (element, tempname))) {
375 sinkpads = g_slist_append (sinkpads, temppad);
379 GST_DEBUG (0, "NO SUCH pad %s in element %s\n", tempname, GST_ELEMENT_NAME (element));
381 GST_DEBUG (0, "have sink pad %s:%s\n", GST_DEBUG_PAD_NAME (temppad));
384 /* if there is no more commas in sinkpadname then we're done */
385 if (tempname == sinkpadname)
390 /* check through the list to find the first sink pad */
391 pads = gst_element_get_pad_list (element);
393 temppad = GST_PAD (pads->data);
394 pads = g_list_next (pads);
395 if (gst_pad_get_direction (temppad) == GST_PAD_SINK) {
396 sinkpads = g_slist_append (sinkpads, temppad);
404 GST_DEBUG (0, "can't find a sink pad for element\n");
406 GST_DEBUG (0, "have sink pad %s:%s\n", GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (sinkpads)));
408 if (!srcpads && sinkpads && previous && srcpadname) {
409 dyn_connect *connect = g_malloc (sizeof (dyn_connect));
411 connect->srcpadname = srcpadname;
412 connect->target = GST_PARSE_LISTPAD (sinkpads);
413 connect->pipeline = GST_ELEMENT (parent);
415 GST_DEBUG (0, "SETTING UP dynamic connection %s:%s and %s:%s\n",
416 gst_element_get_name (previous),
417 srcpadname, GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (sinkpads)));
419 g_signal_connect (G_OBJECT (previous), "new_pad", G_CALLBACK (dynamic_connect), connect);
421 for (j = 0; (j < numsrcpads) && (j < numsinkpads); j++) {
422 GST_DEBUG (0, "CONNECTING %s:%s and %s:%s\n",
423 GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (g_slist_nth (srcpads, j))),
424 GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (g_slist_nth (sinkpads, j))));
425 gst_pad_connect (GST_PARSE_LISTPAD (g_slist_nth (srcpads, j)),
426 GST_PARSE_LISTPAD (g_slist_nth (sinkpads, j)));
430 g_slist_free (srcpads);
433 g_slist_free (sinkpads);
436 /* if we're the first element, ghost all the sinkpads */
437 if (elementcount == 1 && !backref) {
438 DEBUG ("first element, ghosting all of %s's sink pads to parent %s\n",
439 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (GST_ELEMENT (parent)));
440 pads = gst_element_get_pad_list (element);
442 temppad = GST_PAD (pads->data);
443 pads = g_list_next (pads);
445 DEBUG ("much oddness, pad doesn't seem to exist\n");
446 else if (gst_pad_get_direction (temppad) == GST_PAD_SINK) {
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 (GST_ELEMENT (parent)),
451 GST_PAD_NAME (temppad));
457 if (!GST_IS_BIN (element))
458 prevelement = element;
462 /* ghost all the src pads of the bin */
463 if (prevelement != NULL) {
464 DEBUG ("last element, ghosting all of %s's src pads to parent %s\n",
465 GST_ELEMENT_NAME (prevelement), GST_ELEMENT_NAME (GST_ELEMENT (parent)));
466 pads = gst_element_get_pad_list (prevelement);
468 temppad = GST_PAD (pads->data);
469 pads = g_list_next (pads);
471 DEBUG ("much oddness, pad doesn't seem to exist\n");
472 else if (gst_pad_get_direction (temppad) == GST_PAD_SRC) {
473 gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
474 g_strdup_printf ("%s-ghost", GST_PAD_NAME (temppad)));
475 GST_DEBUG (0, "GHOSTED %s:%s to %s as %s-ghost\n",
476 GST_DEBUG_PAD_NAME (temppad), GST_ELEMENT_NAME (parent), GST_PAD_NAME (temppad));
486 DEBUG (closingchar != '\0' ? "returning IN THE WRONG PLACE\n" : "ending pipeline\n");
494 * @argv: null-terminated array of arguments
496 * Create a new pipeline based on command line syntax.
498 * Returns: a new pipeline on success, NULL on failure
501 gst_parse_launchv (const gchar **argv)
503 GstPipeline *pipeline;
506 /* defer error detection to the _recurse function */
508 pipeline = (GstPipeline*) gst_pipeline_new ("launch");
510 ret = gst_parse_launchv_recurse (argv, GST_BIN (pipeline), NULL);
514 gst_object_unref (GST_OBJECT (pipeline));
523 * @pipeline_description: the command line describing the pipeline
525 * Create a new pipeline based on command line syntax.
527 * Returns: a new GstPipeline (cast to a Bin) on success, NULL on failure
530 gst_parse_launch (const gchar * pipeline_description)
535 const gchar *cp, *start, *end;
537 GSList *string_list = NULL, *slist;
538 GstPipeline *pipeline;
540 end = pipeline_description + strlen (pipeline_description);
545 /* Extract the arguments to a gslist in reverse order */
546 for (cp = pipeline_description; cp < end;) {
547 i = strcspn (cp, "([{}]) \"\\");
550 temp = g_strconcat (temp, g_strndup (cp, i), NULL);
552 /* see if we have an escape char */
554 /* normal argument - copy and add to the list */
555 string_list = g_slist_prepend (string_list, temp);
559 temp = g_strconcat (temp, g_strndup (&cp[++i], 1), NULL);
565 while (cp < end && *cp == ' ') {
569 /* handle quoted arguments */
573 /* find matching quote */
574 while (cp < end && *cp != '"')
577 /* make sure we got it */
579 g_warning ("gst_parse_launch: Unbalanced quote in command line");
580 /* FIXME: The list leaks here */
584 /* copy the string sans quotes */
585 string_list = g_slist_prepend (string_list, g_strndup (start, cp - start));
587 cp += 2; /* skip the quote aswell */
590 /* brackets exist in a separate argument slot */
591 if (*cp && strchr ("([{}])", *cp)) {
592 string_list = g_slist_prepend (string_list, g_strndup (cp, 1));
598 /* now allocate the new argv array, with room for NULL termination */
599 argvn = g_new0 (char *, newargc + 1);
601 GST_DEBUG (0, "got %d args\n", newargc);
603 /* reverse the list and put the strings in the new array */
606 for (slist = string_list; slist; slist = slist->next)
607 argvn[--i] = slist->data;
609 g_slist_free (string_list);
613 GST_DEBUG (0, "arg %d is: %s\n", i, argvn[i++]);
617 pipeline = gst_parse_launchv (argvn);
619 GST_DEBUG(0, "Finished - freeing temporary argument array");