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
38 #ifndef DISABLE_FAULT_HANDLER
41 #include <locale.h> /* for LC_ALL */
44 /* FIXME: This is just a temporary hack. We should have a better
45 * check for siginfo handling. */
50 extern volatile gboolean glib_on_error_halt;
52 #ifndef DISABLE_FAULT_HANDLER
53 static void fault_restore (void);
54 static void fault_spin (void);
55 static void sigint_restore (void);
56 static gboolean caught_intr = FALSE;
59 static GstElement *pipeline;
60 static gboolean caught_error = FALSE;
61 static gboolean tags = FALSE;
62 static gboolean messages = FALSE;
65 #ifndef GST_DISABLE_LOADSAVE
67 xmllaunch_parse_cmdline (const gchar ** argv)
69 GstElement *pipeline = NULL, *e;
73 gchar *element, *property, *value;
77 if (!(arg = argv[0])) {
79 ("Usage: gst-xmllaunch <file.xml> [ element.property=value ... ]\n"));
84 /* FIXME guchar from gstxml.c */
85 err = gst_xml_parse_file (xml, (guchar *) arg, NULL);
88 fprintf (stderr, _("ERROR: parse of xml file '%s' failed.\n"), arg);
92 l = gst_xml_get_topelements (xml);
94 fprintf (stderr, _("ERROR: no toplevel pipeline element in file '%s'.\n"),
101 _("WARNING: only one toplevel element is supported at this time."));
103 pipeline = GST_ELEMENT (l->data);
105 while ((arg = argv[++i])) {
106 element = g_strdup (arg);
107 property = strchr (element, '.');
108 value = strchr (element, '=');
110 if (!(element < property && property < value)) {
112 _("ERROR: could not parse command line argument %d: %s.\n"), i,
121 e = gst_bin_get_by_name (GST_BIN (pipeline), element);
123 fprintf (stderr, _("WARNING: element named '%s' not found.\n"), element);
125 gst_util_set_object_arg (G_OBJECT (e), property, value);
137 #ifndef DISABLE_FAULT_HANDLER
140 fault_handler_sighandler (int signum)
144 /* printf is used instead of g_print(), since it's less likely to
148 printf ("Caught SIGSEGV\n");
151 printf ("Caught SIGQUIT\n");
154 printf ("signo: %d\n", signum);
161 #else /* USE_SIGINFO */
164 fault_handler_sigaction (int signum, siginfo_t * si, void *misc)
168 /* printf is used instead of g_print(), since it's less likely to
170 switch (si->si_signo) {
172 printf ("Caught SIGSEGV accessing address %p\n", si->si_addr);
175 printf ("Caught SIGQUIT\n");
178 printf ("signo: %d\n", si->si_signo);
179 printf ("errno: %d\n", si->si_errno);
180 printf ("code: %d\n", si->si_code);
186 #endif /* USE_SIGINFO */
193 glib_on_error_halt = FALSE;
194 g_on_error_stack_trace ("gst-launch");
198 /* FIXME how do we know if we were run by libtool? */
199 printf ("Spinning. Please run 'gdb gst-launch %d' to continue debugging, "
200 "Ctrl-C to quit, or Ctrl-\\ to dump core.\n", (gint) getpid ());
208 struct sigaction action;
210 memset (&action, 0, sizeof (action));
211 action.sa_handler = SIG_DFL;
213 sigaction (SIGSEGV, &action, NULL);
214 sigaction (SIGQUIT, &action, NULL);
220 struct sigaction action;
222 memset (&action, 0, sizeof (action));
224 action.sa_sigaction = fault_handler_sigaction;
225 action.sa_flags = SA_SIGINFO;
227 action.sa_handler = fault_handler_sighandler;
230 sigaction (SIGSEGV, &action, NULL);
231 sigaction (SIGQUIT, &action, NULL);
233 #endif /* DISABLE_FAULT_HANDLER */
236 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
240 count = gst_tag_list_get_tag_size (list, tag);
242 for (i = 0; i < count; i++) {
245 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
246 if (!gst_tag_list_get_string_index (list, tag, i, &str))
247 g_assert_not_reached ();
248 } else if (gst_tag_get_type (tag) == GST_TYPE_BUFFER) {
251 img = gst_value_get_buffer (gst_tag_list_get_value_index (list, tag, i));
255 caps_str = GST_BUFFER_CAPS (img) ?
256 gst_caps_to_string (GST_BUFFER_CAPS (img)) : g_strdup ("unknown");
257 str = g_strdup_printf ("buffer of %u bytes, type: %s",
258 GST_BUFFER_SIZE (img), caps_str);
261 str = g_strdup ("NULL buffer");
265 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
269 g_print ("%15s: %s\n", gst_tag_get_nick (tag), str);
271 g_print (" : %s\n", str);
278 #ifndef DISABLE_FAULT_HANDLER
279 /* we only use sighandler here because the registers are not important */
281 sigint_handler_sighandler (int signum)
283 g_print ("Caught interrupt -- ");
291 check_intr (GstElement * pipeline)
300 g_print ("Pausing pipeline.\n");
302 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
303 message = gst_message_new_warning (GST_OBJECT (pipeline),
304 NULL, "pipeline interrupted");
305 gst_bus_post (bus, message);
307 /* pipeline will wait for element to go to PAUSED */
308 gst_element_set_state (pipeline, GST_STATE_PAUSED);
309 g_print ("Pipeline paused.\n");
311 gst_object_unref (bus);
320 struct sigaction action;
322 memset (&action, 0, sizeof (action));
323 action.sa_handler = sigint_handler_sighandler;
325 sigaction (SIGINT, &action, NULL);
329 sigint_restore (void)
331 struct sigaction action;
333 memset (&action, 0, sizeof (action));
334 action.sa_handler = SIG_DFL;
336 sigaction (SIGINT, &action, NULL);
340 play_handler (int signum)
344 g_print ("Caught SIGUSR1 - Play request.\n");
345 gst_element_set_state (pipeline, GST_STATE_PLAYING);
348 g_print ("Caught SIGUSR2 - Stop request.\n");
349 gst_element_set_state (pipeline, GST_STATE_NULL);
355 play_signal_setup (void)
357 struct sigaction action;
359 memset (&action, 0, sizeof (action));
360 action.sa_handler = play_handler;
361 sigaction (SIGUSR1, &action, NULL);
362 sigaction (SIGUSR2, &action, NULL);
364 #endif /* DISABLE_FAULT_HANDLER */
367 event_loop (GstElement * pipeline, gboolean blocking, GstState target_state)
370 GstMessage *message = NULL;
372 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
374 #ifndef DISABLE_FAULT_HANDLER
375 g_timeout_add (50, (GSourceFunc) check_intr, pipeline);
379 message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
381 /* if the poll timed out, only when !blocking */
382 if (message == NULL) {
383 gst_object_unref (bus);
388 const GstStructure *s;
390 s = gst_message_get_structure (message);
392 g_print (_("Got Message from element \"%s\" (%s): "),
393 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
394 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
398 sstr = gst_structure_to_string (s);
399 g_print ("%s\n", sstr);
402 g_print ("no message details\n");
406 switch (GST_MESSAGE_TYPE (message)) {
407 case GST_MESSAGE_NEW_CLOCK:
411 gst_message_parse_new_clock (message, &clock);
413 g_print ("New clock: %s\n", GST_OBJECT_NAME (clock));
414 gst_message_unref (message);
417 case GST_MESSAGE_EOS:
419 ("Got EOS from element \"%s\".\n"),
420 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
421 gst_message_unref (message);
422 gst_object_unref (bus);
424 case GST_MESSAGE_TAG:
428 gst_message_parse_tag (message, &tags);
429 g_print (_("FOUND TAG : found by element \"%s\".\n"),
430 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
431 gst_tag_list_foreach (tags, print_tag, NULL);
432 gst_tag_list_free (tags);
434 gst_message_unref (message);
436 case GST_MESSAGE_WARNING:{
440 gst_message_parse_warning (message, &gerror, &debug);
442 g_print ("WARNING: Element \"%s\" warns: %s\n",
443 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
446 gst_message_unref (message);
448 g_error_free (gerror);
452 case GST_MESSAGE_ERROR:{
456 gst_message_parse_error (message, &gerror, &debug);
457 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
458 gst_message_unref (message);
460 g_error_free (gerror);
462 gst_object_unref (bus);
465 case GST_MESSAGE_STATE_CHANGED:{
466 GstState old, new, pending;
468 gst_message_parse_state_changed (message, &old, &new, &pending);
469 if (GST_MESSAGE_SRC (message) == GST_OBJECT (pipeline) &&
470 target_state != GST_STATE_VOID_PENDING && new == target_state) {
471 gst_message_unref (message);
472 gst_object_unref (bus);
475 if (!(old == GST_STATE_PLAYING && new == GST_STATE_PAUSED &&
476 GST_MESSAGE_SRC (message) == GST_OBJECT (pipeline))) {
477 gst_message_unref (message);
481 ("Element \"%s\" has gone from PLAYING to PAUSED, quitting.\n"),
482 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
483 /* cut out of the event loop if check_intr set us to PAUSED */
484 gst_message_unref (message);
485 gst_object_unref (bus);
489 /* just be quiet by default */
490 gst_message_unref (message);
495 g_assert_not_reached ();
500 main (int argc, char *argv[])
505 gboolean verbose = FALSE;
506 gboolean no_fault = FALSE;
507 gboolean trace = FALSE;
508 gchar *savefile = NULL;
509 gchar *exclude_args = NULL;
510 GOptionEntry options[] = {
511 {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
512 N_("Output tags (also known as metadata)"), NULL},
513 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
514 N_("Output status information and property notifications"), NULL},
515 {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
516 N_("Output messages"), NULL},
517 {"exclude", 'X', 0, G_OPTION_ARG_NONE, &exclude_args,
518 N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
519 #ifndef GST_DISABLE_LOADSAVE
520 {"output", 'o', 0, G_OPTION_ARG_STRING, &savefile,
521 N_("Save xml representation of pipeline to FILE and exit"), N_("FILE")},
523 {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
524 N_("Do not install a fault handler"), NULL},
525 {"trace", 'T', 0, G_OPTION_ARG_NONE, &trace,
526 N_("Print alloc trace (if enabled at compile time)"), NULL},
527 GST_TOOLS_GOPTION_VERSION,
533 GError *error = NULL;
536 free (malloc (8)); /* -lefence */
539 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
540 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
541 textdomain (GETTEXT_PACKAGE);
544 gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
546 ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
547 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
548 g_option_context_add_group (ctx, gst_init_get_option_group ());
549 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
550 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
553 g_option_context_free (ctx);
555 gst_tools_print_version ("gst-launch");
557 /* FIXpopt: strip short args, too. We do it ourselves for now */
559 for (i = 1; i < argc; i++) {
560 if (*(argv[i]) == '-') {
561 if (strlen (argv[i]) == 2) {
565 if (*c == 'X' || *c == 'o') {
576 #ifndef DISABLE_FAULT_HANDLER
581 play_signal_setup ();
585 if (!gst_alloc_trace_available ()) {
586 g_warning ("Trace not available (recompile with trace enabled).");
588 gst_alloc_trace_print_live ();
591 /* make a null-terminated version of argv */
592 argvn = g_new0 (char *, argc);
593 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
594 #ifndef GST_DISABLE_LOADSAVE
595 if (strstr (argv[0], "gst-xmllaunch")) {
596 pipeline = xmllaunch_parse_cmdline ((const gchar **) argvn);
601 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
607 fprintf (stderr, _("ERROR: pipeline could not be constructed: %s.\n"),
608 GST_STR_NULL (error->message));
609 g_error_free (error);
611 fprintf (stderr, _("ERROR: pipeline could not be constructed.\n"));
615 fprintf (stderr, _("WARNING: erroneous pipeline: %s\n"),
616 GST_STR_NULL (error->message));
617 g_error_free (error);
622 gchar **exclude_list =
623 exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
624 g_signal_connect (pipeline, "deep_notify",
625 G_CALLBACK (gst_object_default_deep_notify), exclude_list);
627 #ifndef GST_DISABLE_LOADSAVE
629 gst_xml_write_file (GST_ELEMENT (pipeline), fopen (savefile, "w"));
634 GstState state, pending;
635 GstStateChangeReturn ret;
637 if (!GST_IS_BIN (pipeline)) {
638 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
640 if (real_pipeline == NULL) {
641 fprintf (stderr, _("ERROR: the 'pipeline' element wasn't found.\n"));
644 gst_bin_add (GST_BIN (real_pipeline), pipeline);
645 pipeline = real_pipeline;
647 fprintf (stderr, _("Setting pipeline to PAUSED ...\n"));
648 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
651 case GST_STATE_CHANGE_FAILURE:
652 fprintf (stderr, _("ERROR: Pipeline doesn't want to pause.\n"));
654 event_loop (pipeline, FALSE, GST_STATE_VOID_PENDING);
656 case GST_STATE_CHANGE_NO_PREROLL:
657 fprintf (stderr, _("Pipeline is live and does not need PREROLL ...\n"));
659 case GST_STATE_CHANGE_ASYNC:
660 fprintf (stderr, _("Pipeline is PREROLLING ...\n"));
661 caught_error = event_loop (pipeline, TRUE, GST_STATE_PAUSED);
663 fprintf (stderr, _("ERROR: pipeline doesn't want to preroll.\n"));
666 state = GST_STATE_PAUSED;
668 case GST_STATE_CHANGE_SUCCESS:
669 fprintf (stderr, _("Pipeline is PREROLLED ...\n"));
673 caught_error = event_loop (pipeline, FALSE, GST_STATE_VOID_PENDING);
676 fprintf (stderr, _("ERROR: pipeline doesn't want to preroll.\n"));
678 GTimeVal tfthen, tfnow;
679 GstClockTimeDiff diff;
681 fprintf (stderr, _("Setting pipeline to PLAYING ...\n"));
682 if (gst_element_set_state (pipeline,
683 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
684 fprintf (stderr, _("ERROR: pipeline doesn't want to play.\n"));
689 g_get_current_time (&tfthen);
690 caught_error = event_loop (pipeline, TRUE, GST_STATE_VOID_PENDING);
691 g_get_current_time (&tfnow);
693 diff = GST_TIMEVAL_TO_TIME (tfnow) - GST_TIMEVAL_TO_TIME (tfthen);
695 g_print (_("Execution ended after %" G_GUINT64_FORMAT " ns.\n"), diff);
697 while (g_main_context_iteration (NULL, FALSE));
699 fprintf (stderr, _("Setting pipeline to PAUSED ...\n"));
700 gst_element_set_state (pipeline, GST_STATE_PAUSED);
701 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
702 fprintf (stderr, _("Setting pipeline to READY ...\n"));
703 gst_element_set_state (pipeline, GST_STATE_READY);
704 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
707 fprintf (stderr, _("Setting pipeline to NULL ...\n"));
708 gst_element_set_state (pipeline, GST_STATE_NULL);
709 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
712 fprintf (stderr, _("FREEING pipeline ...\n"));
713 gst_object_unref (pipeline);
717 gst_alloc_trace_print_live ();