2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2004 Thomas Vander Stichele <thomas@apestaart.org>
6 * gst-launch.c: tool to launch GStreamer pipelines from the command line
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
28 /* FIXME: hack alert */
30 #define DISABLE_FAULT_HANDLER
39 #ifndef DISABLE_FAULT_HANDLER
42 #include <locale.h> /* for LC_ALL */
43 #include "gst/gst-i18n-app.h"
47 /* FIXME: This is just a temporary hack. We should have a better
48 * check for siginfo handling. */
53 extern volatile gboolean glib_on_error_halt;
55 #ifndef DISABLE_FAULT_HANDLER
56 static void fault_restore (void);
57 static void fault_spin (void);
58 static void sigint_restore (void);
61 static gint max_iterations = 0;
62 static GstElement *pipeline;
63 gboolean caught_intr = FALSE;
64 gboolean caught_error = FALSE;
65 gboolean tags = FALSE;
68 #ifndef GST_DISABLE_LOADSAVE
70 xmllaunch_parse_cmdline (const gchar ** argv)
72 GstElement *pipeline = NULL, *e;
76 gchar *element, *property, *value;
80 if (!(arg = argv[0])) {
82 ("Usage: gst-xmllaunch <file.xml> [ element.property=value ... ]\n"));
87 /* FIXME guchar from gstxml.c */
88 err = gst_xml_parse_file (xml, (guchar *) arg, NULL);
91 fprintf (stderr, _("ERROR: parse of xml file '%s' failed.\n"), arg);
95 l = gst_xml_get_topelements (xml);
97 fprintf (stderr, _("ERROR: no toplevel pipeline element in file '%s'.\n"),
104 _("WARNING: only one toplevel element is supported at this time."));
106 pipeline = GST_ELEMENT (l->data);
108 while ((arg = argv[++i])) {
109 element = g_strdup (arg);
110 property = strchr (element, '.');
111 value = strchr (element, '=');
113 if (!(element < property && property < value)) {
115 _("ERROR: could not parse command line argument %d: %s.\n"), i,
124 e = gst_bin_get_by_name (GST_BIN (pipeline), element);
126 fprintf (stderr, _("WARNING: element named '%s' not found.\n"), element);
128 gst_util_set_object_arg (G_OBJECT (e), property, value);
140 #ifndef DISABLE_FAULT_HANDLER
143 fault_handler_sighandler (int signum)
147 /* printf is used instead of g_print(), since it's less likely to
151 printf ("Caught SIGSEGV\n");
154 printf ("Caught SIGQUIT\n");
157 printf ("signo: %d\n", signum);
164 #else /* USE_SIGINFO */
167 fault_handler_sigaction (int signum, siginfo_t * si, void *misc)
171 /* printf is used instead of g_print(), since it's less likely to
173 switch (si->si_signo) {
175 printf ("Caught SIGSEGV accessing address %p\n", si->si_addr);
178 printf ("Caught SIGQUIT\n");
181 printf ("signo: %d\n", si->si_signo);
182 printf ("errno: %d\n", si->si_errno);
183 printf ("code: %d\n", si->si_code);
189 #endif /* USE_SIGINFO */
196 glib_on_error_halt = FALSE;
197 g_on_error_stack_trace ("gst-launch");
201 /* FIXME how do we know if we were run by libtool? */
202 printf ("Spinning. Please run 'gdb gst-launch %d' to continue debugging, "
203 "Ctrl-C to quit, or Ctrl-\\ to dump core.\n", (gint) getpid ());
211 struct sigaction action;
213 memset (&action, 0, sizeof (action));
214 action.sa_handler = SIG_DFL;
216 sigaction (SIGSEGV, &action, NULL);
217 sigaction (SIGQUIT, &action, NULL);
223 struct sigaction action;
225 memset (&action, 0, sizeof (action));
227 action.sa_sigaction = fault_handler_sigaction;
228 action.sa_flags = SA_SIGINFO;
230 action.sa_handler = fault_handler_sighandler;
233 sigaction (SIGSEGV, &action, NULL);
234 sigaction (SIGQUIT, &action, NULL);
236 #endif /* DISABLE_FAULT_HANDLER */
239 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
243 count = gst_tag_list_get_tag_size (list, tag);
245 for (i = 0; i < count; i++) {
248 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
249 if (!gst_tag_list_get_string_index (list, tag, i, &str))
250 g_assert_not_reached ();
253 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
257 g_print ("%15s: %s\n", gst_tag_get_nick (tag), str);
259 g_print (" : %s\n", str);
266 #ifndef DISABLE_FAULT_HANDLER
267 /* we only use sighandler here because the registers are not important */
269 sigint_handler_sighandler (int signum)
271 g_print ("Caught interrupt -- ");
279 check_intr (GstElement * pipeline)
288 g_print ("Pausing pipeline.\n");
290 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
291 message = gst_message_new_warning (GST_OBJECT (pipeline),
292 NULL, "pipeline interrupted");
293 gst_bus_post (bus, message);
295 gst_element_set_state (pipeline, GST_STATE_PAUSED);
296 gst_element_get_state (pipeline, NULL, NULL, NULL);
297 g_print ("Pipeline paused.\n");
307 struct sigaction action;
309 memset (&action, 0, sizeof (action));
310 action.sa_handler = sigint_handler_sighandler;
312 sigaction (SIGINT, &action, NULL);
316 sigint_restore (void)
318 struct sigaction action;
320 memset (&action, 0, sizeof (action));
321 action.sa_handler = SIG_DFL;
323 sigaction (SIGINT, &action, NULL);
327 play_handler (int signum)
331 g_print ("Caught SIGUSR1 - Play request.\n");
332 gst_element_set_state (pipeline, GST_STATE_PLAYING);
335 g_print ("Caught SIGUSR2 - Stop request.\n");
336 gst_element_set_state (pipeline, GST_STATE_NULL);
342 play_signal_setup (void)
344 struct sigaction action;
346 memset (&action, 0, sizeof (action));
347 action.sa_handler = play_handler;
348 sigaction (SIGUSR1, &action, NULL);
349 sigaction (SIGUSR2, &action, NULL);
351 #endif /* DISABLE_FAULT_HANDLER */
354 event_loop (GstElement * pipeline, gboolean blocking)
357 GstMessageType revent;
358 GstMessage *message = NULL;
360 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
362 g_timeout_add (50, (GSourceFunc) check_intr, pipeline);
365 revent = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
367 /* if the poll timed out, only when !blocking */
368 if (revent == GST_MESSAGE_UNKNOWN)
371 message = gst_bus_pop (bus);
372 g_return_val_if_fail (message != NULL, TRUE);
375 case GST_MESSAGE_EOS:
377 ("GOT EOS from element \"%s\".\n"),
378 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
379 gst_message_unref (message);
381 case GST_MESSAGE_TAG:
385 gst_message_parse_tag (message, &tags);
386 g_print (_("FOUND TAG : found by element \"%s\".\n"),
387 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
388 gst_tag_list_foreach (tags, print_tag, NULL);
389 gst_tag_list_free (tags);
391 gst_message_unref (message);
393 case GST_MESSAGE_WARNING:{
397 gst_message_parse_warning (message, &gerror, &debug);
399 g_print ("WARNING: Element \"%s\" warns: %s\n",
400 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
403 gst_message_unref (message);
405 g_error_free (gerror);
409 case GST_MESSAGE_ERROR:{
413 gst_message_parse_error (message, &gerror, &debug);
414 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
415 gst_message_unref (message);
417 g_error_free (gerror);
421 case GST_MESSAGE_STATE_CHANGED:{
422 GstElementState old, new;
424 gst_message_parse_state_changed (message, &old, &new);
425 if (!(old == GST_STATE_PLAYING && new == GST_STATE_PAUSED &&
426 GST_MESSAGE_SRC (message) == GST_OBJECT (pipeline))) {
427 gst_message_unref (message);
431 ("Element \"%s\" has gone from PLAYING to PAUSED, quitting.\n"),
432 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
433 /* cut out of the event loop if check_intr set us to PAUSED */
434 gst_message_unref (message);
438 /* just be quiet by default */
439 gst_message_unref (message);
444 g_assert_not_reached ();
449 main (int argc, char *argv[])
454 gboolean verbose = FALSE;
455 gboolean no_fault = FALSE;
456 gboolean trace = FALSE;
457 gchar *savefile = NULL;
458 gchar *exclude_args = NULL;
459 struct poptOption options[] = {
460 {"tags", 't', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &tags, 0,
461 N_("Output tags (also known as metadata)"), NULL},
462 {"verbose", 'v', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &verbose, 0,
463 N_("Output status information and property notifications"), NULL},
464 {"exclude", 'X', POPT_ARG_STRING | POPT_ARGFLAG_STRIP, &exclude_args, 0,
465 N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
466 #ifndef GST_DISABLE_LOADSAVE
467 {"output", 'o', POPT_ARG_STRING | POPT_ARGFLAG_STRIP, &savefile, 0,
468 N_("Save xml representation of pipeline to FILE and exit"), N_("FILE")},
470 {"no-fault", 'f', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &no_fault, 0,
471 N_("Do not install a fault handler"), NULL},
472 {"trace", 'T', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &trace, 0,
473 N_("Print alloc trace (if enabled at compile time)"), NULL},
474 {"iterations", 'i', POPT_ARG_INT | POPT_ARGFLAG_STRIP, &max_iterations, 0,
475 N_("Number of times to iterate pipeline"), NULL},
480 GError *error = NULL;
483 free (malloc (8)); /* -lefence */
485 #ifdef GETTEXT_PACKAGE
486 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
487 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
488 textdomain (GETTEXT_PACKAGE);
491 gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
493 gst_init_with_popt_table (&argc, &argv, options);
495 /* FIXpopt: strip short args, too. We do it ourselves for now */
497 for (i = 1; i < argc; i++) {
498 if (*(argv[i]) == '-') {
499 if (strlen (argv[i]) == 2) {
503 if (*c == 'X' || *c == 'o') {
514 #ifndef DISABLE_FAULT_HANDLER
519 play_signal_setup ();
523 if (!gst_alloc_trace_available ()) {
524 g_warning ("Trace not available (recompile with trace enabled).");
526 gst_alloc_trace_print_live ();
529 /* make a null-terminated version of argv */
530 argvn = g_new0 (char *, argc);
531 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
532 #ifndef GST_DISABLE_LOADSAVE
533 if (strstr (argv[0], "gst-xmllaunch")) {
534 pipeline = xmllaunch_parse_cmdline ((const gchar **) argvn);
539 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
545 fprintf (stderr, _("ERROR: pipeline could not be constructed: %s.\n"),
547 g_error_free (error);
549 fprintf (stderr, _("ERROR: pipeline could not be constructed.\n"));
553 fprintf (stderr, _("WARNING: erroneous pipeline: %s\n"), error->message);
554 fprintf (stderr, _(" Trying to run anyway.\n"));
555 g_error_free (error);
559 gchar **exclude_list =
560 exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
561 g_signal_connect (pipeline, "deep_notify",
562 G_CALLBACK (gst_object_default_deep_notify), exclude_list);
564 #ifndef GST_DISABLE_LOADSAVE
566 gst_xml_write_file (GST_ELEMENT (pipeline), fopen (savefile, "w"));
571 GstElementState state, pending;
572 GstElementStateReturn ret;
574 if (!GST_IS_BIN (pipeline)) {
575 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
577 if (real_pipeline == NULL) {
578 fprintf (stderr, _("ERROR: the 'pipeline' element wasn't found.\n"));
581 gst_bin_add (GST_BIN (real_pipeline), pipeline);
582 pipeline = real_pipeline;
585 fprintf (stderr, _("PAUSE pipeline ...\n"));
586 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
589 case GST_STATE_FAILURE:
590 fprintf (stderr, _("ERROR: pipeline doesn't want to pause.\n"));
593 case GST_STATE_NO_PREROLL:
594 fprintf (stderr, _("NO_PREROLL pipeline ...\n"));
596 case GST_STATE_ASYNC:
597 fprintf (stderr, _("PREROLL pipeline ...\n"));
598 gst_element_get_state (pipeline, &state, &pending, NULL);
600 case GST_STATE_SUCCESS:
601 fprintf (stderr, _("PREROLLED pipeline ...\n"));
605 caught_error = event_loop (pipeline, FALSE);
608 fprintf (stderr, _("ERROR: pipeline doesn't want to preroll.\n"));
610 GTimeVal tfthen, tfnow;
611 GstClockTimeDiff diff;
613 fprintf (stderr, _("RUNNING pipeline ...\n"));
614 if (gst_element_set_state (pipeline,
615 GST_STATE_PLAYING) == GST_STATE_FAILURE) {
616 fprintf (stderr, _("ERROR: pipeline doesn't want to play.\n"));
621 g_get_current_time (&tfthen);
622 caught_error = event_loop (pipeline, TRUE);
623 g_get_current_time (&tfnow);
625 diff = GST_TIMEVAL_TO_TIME (tfnow) - GST_TIMEVAL_TO_TIME (tfthen);
627 g_print (_("Execution ended after %" G_GUINT64_FORMAT " ns.\n"), diff);
629 fprintf (stderr, _("PAUSE pipeline ...\n"));
630 gst_element_set_state (pipeline, GST_STATE_PAUSED);
631 gst_element_get_state (pipeline, &state, &pending, NULL);
632 fprintf (stderr, _("READY pipeline ...\n"));
633 gst_element_set_state (pipeline, GST_STATE_READY);
634 gst_element_get_state (pipeline, &state, &pending, NULL);
635 fprintf (stderr, _("NULL pipeline ...\n"));
636 gst_element_set_state (pipeline, GST_STATE_NULL);
637 gst_element_get_state (pipeline, &state, &pending, NULL);
642 fprintf (stderr, _("FREEING pipeline ...\n"));
643 gst_object_unref (pipeline);
646 gst_alloc_trace_print_live ();