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 if (!gst_pad_connect (newpad, connect->target))
72 g_warning ("could not connect %s:%s to %s:%s", GST_DEBUG_PAD_NAME (newpad),
73 GST_DEBUG_PAD_NAME (connect->target));
74 gst_element_set_state (connect->pipeline, GST_STATE_PLAYING);
79 gst_parse_unique_name (const gchar * type, gst_parse_priv * priv)
84 tmp = g_hash_table_lookup (priv->elementcounts, type);
85 count = GPOINTER_TO_INT (tmp);
87 g_hash_table_insert (priv->elementcounts, g_strdup (type), GINT_TO_POINTER (count));
89 return g_strdup_printf ("%s%d", type, count - 1);
93 gst_parse_launchv_recurse (const gchar **argv, GstBin * parent, gst_parse_priv * priv)
97 GstElement *element = NULL, *previous = NULL, *prevelement = NULL;
98 gchar closingchar = '\0';
102 gchar *sinkpadname = NULL, *srcpadname = NULL, *tempname;
104 GSList *sinkpads = NULL, *srcpads = NULL;
105 gint numsrcpads = 0, numsinkpads = 0;
107 gint elementcount = 0;
109 gboolean backref = FALSE;
112 priv = g_new0 (gst_parse_priv, 1);
113 priv->elementcounts = g_hash_table_new (g_str_hash, g_str_equal);
118 if (GST_IS_PIPELINE (parent)) {
120 DEBUG ("in pipeline ");
121 } else if (GST_IS_THREAD (parent)) {
123 DEBUG ("in thread ");
128 DEBUG_NOPREFIX ("%s\n", GST_ELEMENT_NAME (GST_ELEMENT (parent)));
130 while ((arg = argv[++i])) {
133 DEBUG ("** ARGUMENT is '%s'\n", arg);
136 DEBUG ("random arg, FIXME\n");
139 } else if (arg[0] == closingchar) {
140 DEBUG ("exiting container %s\n", GST_ELEMENT_NAME (GST_ELEMENT (parent)));
144 } else if ((cptr = strchr (arg, '!'))) {
145 DEBUG ("attempting to connect pads together....\n");
147 /* if it starts with the ! */
150 /* if there's a sinkpad... */
152 sinkpadname = g_strdup (&arg[1]);
156 srcpadname = g_strndup (arg, (cptr - arg));
157 /* if there's a sinkpad */
158 if (len > (cptr - arg) + 1)
159 sinkpadname = g_strdup(&cptr[1]);
164 if (srcpadname && (ptr = strchr (srcpadname, '.'))) {
165 gchar *element_name = srcpadname;
168 *ptr = '\0'; /* it was a '.' before */
170 GST_DEBUG (0, "have pad for element %s\n", element_name);
171 new = gst_bin_get_by_name_recurse_up (parent, element_name);
173 GST_DEBUG (0, "element %s does not exist! trying to continue\n", element_name);
176 srcpadname = ptr + 1;
181 GST_DEBUG (0, "have srcpad %s, sinkpad %s\n", srcpadname, sinkpadname);
183 g_slist_free (srcpads);
189 if (srcpadname != NULL) {
191 /* split name at commas */
192 if ((ptr = strchr (srcpadname, ','))) {
193 tempname = srcpadname;
194 srcpadname = &ptr[1];
197 tempname = srcpadname;
200 /* look for pad with that name */
201 if ((temppad = gst_element_get_pad (previous, tempname))) {
202 srcpads = g_slist_append (srcpads, temppad);
206 /* try to create a pad using that padtemplate name */
207 else if ((temppad = gst_element_request_pad_by_name (previous, tempname))) {
208 srcpads = g_slist_append (srcpads, temppad);
212 GST_DEBUG (0, "NO SUCH pad %s in element %s\n", tempname, GST_ELEMENT_NAME (previous));
214 GST_DEBUG (0, "have src pad %s:%s\n", GST_DEBUG_PAD_NAME (temppad));
217 /* if there is no more commas in srcpadname then we're done */
218 if (tempname == srcpadname)
223 /* check through the list to find the first src pad */
224 GST_DEBUG (0, "CHECKING element %s for pad named %s\n", GST_ELEMENT_NAME (previous),
226 pads = gst_element_get_pad_list (previous);
228 temppad = GST_PARSE_LISTPAD (pads);
229 GST_DEBUG (0, "have pad %s:%s\n", GST_DEBUG_PAD_NAME (temppad));
230 if (GST_IS_GHOST_PAD (temppad))
231 GST_DEBUG (0, "it's a ghost pad\n");
232 if (gst_pad_get_direction (temppad) == GST_PAD_SRC) {
233 srcpads = g_slist_append (srcpads, temppad);
237 pads = g_list_next (pads);
240 GST_DEBUG (0, "error, can't find a src pad!!!\n");
242 GST_DEBUG (0, "have src pad %s:%s\n", GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (srcpads)));
244 } else if (strchr (arg, '=')) {
249 DEBUG ("have a property\n");
251 /* we have a property */
252 propname = g_strdup (arg);
253 pos = strchr (propname, '=');
257 /* use g_object_set in the future when gst_util_{set|get} go away */
258 GST_DEBUG (0, "attempting to set property '%s' to '%s' on element '%s'\n",
259 propname, propval, GST_ELEMENT_NAME (previous));
260 gst_util_set_object_arg (G_OBJECT (previous), propname, propval);
262 } else if (arg[0] == '[') {
263 DEBUG ("have element name\n");
265 if (arg[1] != '\0') {
266 fprintf (stderr, "error, unexpected junk after [\n");
267 return GST_PARSE_ERROR_SYNTAX;
270 if ((arg = argv[++i])) {
272 g_critical("parser error, please report");
273 return GST_PARSE_ERROR_INTERNAL;
275 gst_element_set_name (previous, arg);
277 fprintf (stderr, "error, expected element name, found end of arguments\n");
278 return GST_PARSE_ERROR_SYNTAX;
281 if ((arg = argv[++i])) {
282 if (strcmp (arg, "]") != 0) {
283 fprintf (stderr, "error, expected ], found '%s'\n", arg);
284 return GST_PARSE_ERROR_SYNTAX;
287 fprintf (stderr, "error, expected ], found end of arguments\n");
288 return GST_PARSE_ERROR_SYNTAX;
291 /* we have an element, a bin, or a thread. we treat these together because
292 * once we rech them we have to resolve any dangling connections from
293 * previous array arguments. */
295 if (strchr ("({", arg[0])) {
296 DEBUG ("have bin or thread\n");
299 /* create a bin and add it to the current parent */
300 element = gst_bin_new (g_strdup_printf ("bin%d", priv->bincount++));
302 fprintf (stderr, "Couldn't create a bin!\n");
303 return GST_PARSE_ERROR_CREATING_ELEMENT;
305 GST_DEBUG (0, "CREATED bin %s\n", GST_ELEMENT_NAME (element));
306 } else if (arg[0] == '{') {
307 /* create a thread and add it to the current parent */
308 element = gst_thread_new (g_strdup_printf ("thread%d", priv->threadcount++));
310 fprintf (stderr, "Couldn't create a thread!\n");
311 return GST_PARSE_ERROR_CREATING_ELEMENT;
313 GST_DEBUG (0, "CREATED thread %s\n", GST_ELEMENT_NAME (element));
315 fprintf (stderr, "Illegal argument: %s\n", arg);
316 return GST_PARSE_ERROR_CREATING_ELEMENT;
319 gst_bin_add (GST_BIN (parent), element);
321 j = gst_parse_launchv_recurse (argv + i + 1, GST_BIN (element), priv);
322 /* check for parse error */
326 /* we will get autoincremented at the while */
330 /* we have an element */
331 DEBUG ("attempting to create element '%s'\n", arg);
333 ptr = gst_parse_unique_name (arg, priv);
334 element = gst_elementfactory_make (arg, ptr);
337 #ifndef GST_DISABLE_REGISTRY
339 "Couldn't create a '%s', no such element or need to run gst-register?\n", arg);
341 fprintf (stderr, "Couldn't create a '%s', no such element or need to load plugin?\n",
344 return GST_PARSE_ERROR_NOSUCH_ELEMENT;
347 GST_DEBUG (0, "CREATED element %s\n", GST_ELEMENT_NAME (element));
348 gst_bin_add (GST_BIN (parent), element);
353 g_slist_free (sinkpads);
359 if (sinkpadname != NULL) {
361 /* split name at commas */
362 if ((ptr = strchr (sinkpadname, ','))) {
363 tempname = g_strndup (sinkpadname, (ptr - sinkpadname));
364 sinkpadname = &ptr[1];
366 tempname = sinkpadname;
369 /* look for pad with that name */
370 if ((temppad = gst_element_get_pad (element, tempname))) {
371 sinkpads = g_slist_append (sinkpads, temppad);
375 /* try to create a pad using that padtemplate name */
376 else if ((temppad = gst_element_request_pad_by_name (element, tempname))) {
377 sinkpads = g_slist_append (sinkpads, temppad);
381 GST_DEBUG (0, "NO SUCH pad %s in element %s\n", tempname, GST_ELEMENT_NAME (element));
383 GST_DEBUG (0, "have sink pad %s:%s\n", GST_DEBUG_PAD_NAME (temppad));
386 /* if there is no more commas in sinkpadname then we're done */
387 if (tempname == sinkpadname)
392 /* check through the list to find the first sink pad */
393 pads = gst_element_get_pad_list (element);
395 temppad = GST_PAD (pads->data);
396 pads = g_list_next (pads);
397 if (gst_pad_get_direction (temppad) == GST_PAD_SINK) {
398 sinkpads = g_slist_append (sinkpads, temppad);
406 GST_DEBUG (0, "can't find a sink pad for element\n");
408 GST_DEBUG (0, "have sink pad %s:%s\n", GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (sinkpads)));
410 if (!srcpads && sinkpads && previous && srcpadname) {
411 dyn_connect *connect = g_malloc (sizeof (dyn_connect));
413 connect->srcpadname = srcpadname;
414 connect->target = GST_PARSE_LISTPAD (sinkpads);
415 connect->pipeline = GST_ELEMENT (parent);
417 GST_DEBUG (0, "SETTING UP dynamic connection %s:%s and %s:%s\n",
418 gst_element_get_name (previous),
419 srcpadname, GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (sinkpads)));
421 g_signal_connect (G_OBJECT (previous), "new_pad", G_CALLBACK (dynamic_connect), connect);
423 for (j = 0; (j < numsrcpads) && (j < numsinkpads); j++) {
424 GST_DEBUG (0, "CONNECTING %s:%s and %s:%s\n",
425 GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (g_slist_nth (srcpads, j))),
426 GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (g_slist_nth (sinkpads, j))));
427 if (!gst_pad_connect (GST_PARSE_LISTPAD (g_slist_nth (srcpads, j)),
428 GST_PARSE_LISTPAD (g_slist_nth (sinkpads, j)))) {
429 g_warning ("could not connect %s:%s to %s:%s",
430 GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (g_slist_nth (srcpads, j))),
431 GST_DEBUG_PAD_NAME (GST_PARSE_LISTPAD (g_slist_nth (sinkpads, j))));
432 return GST_PARSE_ERROR_CONNECT;
437 g_slist_free (srcpads);
440 g_slist_free (sinkpads);
443 /* if we're the first element, ghost all the sinkpads */
444 if (elementcount == 1 && !backref) {
445 DEBUG ("first element, ghosting all of %s's sink pads to parent %s\n",
446 GST_ELEMENT_NAME (element), GST_ELEMENT_NAME (GST_ELEMENT (parent)));
447 pads = gst_element_get_pad_list (element);
449 temppad = GST_PAD (pads->data);
450 pads = g_list_next (pads);
452 DEBUG ("much oddness, pad doesn't seem to exist\n");
453 else if (gst_pad_get_direction (temppad) == GST_PAD_SINK) {
454 gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
455 g_strdup_printf ("%s-ghost", GST_PAD_NAME (temppad)));
456 GST_DEBUG (0, "GHOSTED %s:%s to %s as %s-ghost\n",
457 GST_DEBUG_PAD_NAME (temppad), GST_ELEMENT_NAME (GST_ELEMENT (parent)),
458 GST_PAD_NAME (temppad));
464 if (!GST_IS_BIN (element))
465 prevelement = element;
469 /* ghost all the src pads of the bin */
470 if (prevelement != NULL) {
471 DEBUG ("last element, ghosting all of %s's src pads to parent %s\n",
472 GST_ELEMENT_NAME (prevelement), GST_ELEMENT_NAME (GST_ELEMENT (parent)));
473 pads = gst_element_get_pad_list (prevelement);
475 temppad = GST_PAD (pads->data);
476 pads = g_list_next (pads);
478 DEBUG ("much oddness, pad doesn't seem to exist\n");
479 else if (gst_pad_get_direction (temppad) == GST_PAD_SRC) {
480 gst_element_add_ghost_pad (GST_ELEMENT (parent), temppad,
481 g_strdup_printf ("%s-ghost", GST_PAD_NAME (temppad)));
482 GST_DEBUG (0, "GHOSTED %s:%s to %s as %s-ghost\n",
483 GST_DEBUG_PAD_NAME (temppad), GST_ELEMENT_NAME (parent), GST_PAD_NAME (temppad));
493 DEBUG (closingchar != '\0' ? "returning IN THE WRONG PLACE\n" : "ending pipeline\n");
501 * @argv: null-terminated array of arguments
503 * Create a new pipeline based on command line syntax.
505 * Returns: a new pipeline on success, NULL on failure
508 gst_parse_launchv (const gchar **argv)
510 GstPipeline *pipeline;
513 /* defer error detection to the _recurse function */
515 pipeline = (GstPipeline*) gst_pipeline_new ("launch");
517 ret = gst_parse_launchv_recurse (argv, GST_BIN (pipeline), NULL);
521 gst_object_unref (GST_OBJECT (pipeline));
530 * @pipeline_description: the command line describing the pipeline
532 * Create a new pipeline based on command line syntax.
534 * Returns: a new GstPipeline (cast to a Bin) on success, NULL on failure
537 gst_parse_launch (const gchar * pipeline_description)
542 const gchar *cp, *start, *end;
544 GSList *string_list = NULL, *slist;
545 GstPipeline *pipeline;
547 end = pipeline_description + strlen (pipeline_description);
552 /* Extract the arguments to a gslist in reverse order */
553 for (cp = pipeline_description; cp < end;) {
554 i = strcspn (cp, "([{}]) \"\\");
557 temp = g_strconcat (temp, g_strndup (cp, i), NULL);
559 /* see if we have an escape char */
561 /* normal argument - copy and add to the list */
562 string_list = g_slist_prepend (string_list, temp);
566 temp = g_strconcat (temp, g_strndup (&cp[++i], 1), NULL);
572 while (cp < end && *cp == ' ') {
576 /* handle quoted arguments */
580 /* find matching quote */
581 while (cp < end && *cp != '"')
584 /* make sure we got it */
586 g_warning ("gst_parse_launch: Unbalanced quote in command line");
587 /* FIXME: The list leaks here */
591 /* copy the string sans quotes */
592 string_list = g_slist_prepend (string_list, g_strndup (start, cp - start));
594 cp += 2; /* skip the quote aswell */
597 /* brackets exist in a separate argument slot */
598 if (*cp && strchr ("([{}])", *cp)) {
599 string_list = g_slist_prepend (string_list, g_strndup (cp, 1));
605 /* now allocate the new argv array, with room for NULL termination */
606 argvn = g_new0 (char *, newargc + 1);
608 GST_DEBUG (0, "got %d args\n", newargc);
610 /* reverse the list and put the strings in the new array */
613 for (slist = string_list; slist; slist = slist->next)
614 argvn[--i] = slist->data;
616 g_slist_free (string_list);
620 GST_DEBUG (0, "arg %d is: %s\n", i, argvn[i++]);
624 pipeline = gst_parse_launchv (argvn);
626 GST_DEBUG(0, "Finished - freeing temporary argument array");