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);
133 gst_object_ref (pipeline);
134 gst_object_unref (xml);
139 #ifndef DISABLE_FAULT_HANDLER
142 fault_handler_sighandler (int signum)
146 /* printf is used instead of g_print(), since it's less likely to
150 printf ("Caught SIGSEGV\n");
153 printf ("Caught SIGQUIT\n");
156 printf ("signo: %d\n", signum);
163 #else /* USE_SIGINFO */
166 fault_handler_sigaction (int signum, siginfo_t * si, void *misc)
170 /* printf is used instead of g_print(), since it's less likely to
172 switch (si->si_signo) {
174 printf ("Caught SIGSEGV accessing address %p\n", si->si_addr);
177 printf ("Caught SIGQUIT\n");
180 printf ("signo: %d\n", si->si_signo);
181 printf ("errno: %d\n", si->si_errno);
182 printf ("code: %d\n", si->si_code);
188 #endif /* USE_SIGINFO */
195 glib_on_error_halt = FALSE;
196 g_on_error_stack_trace ("gst-launch");
200 /* FIXME how do we know if we were run by libtool? */
201 printf ("Spinning. Please run 'gdb gst-launch %d' to continue debugging, "
202 "Ctrl-C to quit, or Ctrl-\\ to dump core.\n", (gint) getpid ());
210 struct sigaction action;
212 memset (&action, 0, sizeof (action));
213 action.sa_handler = SIG_DFL;
215 sigaction (SIGSEGV, &action, NULL);
216 sigaction (SIGQUIT, &action, NULL);
222 struct sigaction action;
224 memset (&action, 0, sizeof (action));
226 action.sa_sigaction = fault_handler_sigaction;
227 action.sa_flags = SA_SIGINFO;
229 action.sa_handler = fault_handler_sighandler;
232 sigaction (SIGSEGV, &action, NULL);
233 sigaction (SIGQUIT, &action, NULL);
235 #endif /* DISABLE_FAULT_HANDLER */
238 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
242 count = gst_tag_list_get_tag_size (list, tag);
244 for (i = 0; i < count; i++) {
247 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
248 if (!gst_tag_list_get_string_index (list, tag, i, &str))
249 g_assert_not_reached ();
250 } else if (gst_tag_get_type (tag) == GST_TYPE_BUFFER) {
253 img = gst_value_get_buffer (gst_tag_list_get_value_index (list, tag, i));
257 caps_str = GST_BUFFER_CAPS (img) ?
258 gst_caps_to_string (GST_BUFFER_CAPS (img)) : g_strdup ("unknown");
259 str = g_strdup_printf ("buffer of %u bytes, type: %s",
260 GST_BUFFER_SIZE (img), caps_str);
263 str = g_strdup ("NULL buffer");
267 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
271 g_print ("%16s: %s\n", gst_tag_get_nick (tag), str);
273 g_print ("%16s: %s\n", "", str);
280 #ifndef DISABLE_FAULT_HANDLER
281 /* we only use sighandler here because the registers are not important */
283 sigint_handler_sighandler (int signum)
285 g_print ("Caught interrupt -- ");
289 /* we set a flag that is checked by the mainloop, we cannot do much in the
290 * interrupt handler (no mutex or other blocking stuff) */
294 /* is called every 50 milliseconds (20 times a second), the interrupt handler
295 * will set a flag for us. We react to this by posting a message. */
297 check_intr (GstElement * pipeline)
303 g_print ("handling interrupt.\n");
305 /* post an application specific message */
306 gst_element_post_message (GST_ELEMENT (pipeline),
307 gst_message_new_application (GST_OBJECT (pipeline),
308 gst_structure_new ("GstLaunchInterrupt",
309 "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));
311 /* remove timeout handler */
319 struct sigaction action;
321 memset (&action, 0, sizeof (action));
322 action.sa_handler = sigint_handler_sighandler;
324 sigaction (SIGINT, &action, NULL);
328 sigint_restore (void)
330 struct sigaction action;
332 memset (&action, 0, sizeof (action));
333 action.sa_handler = SIG_DFL;
335 sigaction (SIGINT, &action, NULL);
339 play_handler (int signum)
343 g_print ("Caught SIGUSR1 - Play request.\n");
344 gst_element_set_state (pipeline, GST_STATE_PLAYING);
347 g_print ("Caught SIGUSR2 - Stop request.\n");
348 gst_element_set_state (pipeline, GST_STATE_NULL);
354 play_signal_setup (void)
356 struct sigaction action;
358 memset (&action, 0, sizeof (action));
359 action.sa_handler = play_handler;
360 sigaction (SIGUSR1, &action, NULL);
361 sigaction (SIGUSR2, &action, NULL);
363 #endif /* DISABLE_FAULT_HANDLER */
365 /* returns TRUE if there was an error or we caught a keyboard interrupt. */
367 event_loop (GstElement * pipeline, gboolean blocking, GstState target_state)
370 GstMessage *message = NULL;
371 gboolean res = FALSE;
372 gboolean buffering = FALSE;
374 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
376 #ifndef DISABLE_FAULT_HANDLER
377 g_timeout_add (50, (GSourceFunc) check_intr, pipeline);
381 message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
383 /* if the poll timed out, only when !blocking */
387 /* check if we need to dump messages to the console */
389 const GstStructure *s;
391 s = gst_message_get_structure (message);
393 g_print (_("Got Message from element \"%s\" (%s): "),
394 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
395 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
399 sstr = gst_structure_to_string (s);
400 g_print ("%s\n", sstr);
403 g_print ("no message details\n");
407 switch (GST_MESSAGE_TYPE (message)) {
408 case GST_MESSAGE_NEW_CLOCK:
412 gst_message_parse_new_clock (message, &clock);
414 g_print ("New clock: %s\n", GST_OBJECT_NAME (clock));
417 case GST_MESSAGE_EOS:
419 ("Got EOS from element \"%s\".\n"),
420 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
422 case GST_MESSAGE_TAG:
426 gst_message_parse_tag (message, &tags);
427 g_print (_("FOUND TAG : found by element \"%s\".\n"),
428 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
429 gst_tag_list_foreach (tags, print_tag, NULL);
430 gst_tag_list_free (tags);
433 case GST_MESSAGE_WARNING:{
437 gst_message_parse_warning (message, &gerror, &debug);
439 g_print ("WARNING: Element \"%s\" warns: %s\n",
440 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
444 g_error_free (gerror);
448 case GST_MESSAGE_ERROR:{
452 gst_message_parse_error (message, &gerror, &debug);
453 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
455 g_error_free (gerror);
457 /* we have an error */
461 case GST_MESSAGE_STATE_CHANGED:{
462 GstState old, new, pending;
464 gst_message_parse_state_changed (message, &old, &new, &pending);
466 /* we only care about pipeline state change messages */
467 if (GST_MESSAGE_SRC (message) != GST_OBJECT_CAST (pipeline))
470 /* ignore when we are buffering since then we mess with the states
475 /* if we reached the final target state, exit */
476 if (target_state == GST_STATE_PAUSED && new == target_state)
479 /* else not an interesting message */
482 case GST_MESSAGE_BUFFERING:{
485 gst_message_parse_buffering (message, &percent);
486 fprintf (stderr, "buffering... %d\r", percent);
488 if (percent == 100) {
489 /* a 100% message means buffering is done */
491 /* if the desired state is playing, go back */
492 if (target_state == GST_STATE_PLAYING) {
493 fprintf (stderr, _("Setting pipeline to PLAYING ...\n"));
494 gst_element_set_state (pipeline, GST_STATE_PLAYING);
499 if (buffering == FALSE && target_state == GST_STATE_PLAYING) {
500 /* we were not buffering but PLAYING, PAUSE the pipeline. */
501 fprintf (stderr, _("Setting pipeline to PAUSED ...\n"));
502 gst_element_set_state (pipeline, GST_STATE_PAUSED);
508 case GST_MESSAGE_APPLICATION:{
509 const GstStructure *s;
511 s = gst_message_get_structure (message);
513 if (gst_structure_has_name (s, "GstLaunchInterrupt")) {
514 /* this application message is posted when we caught an interrupt and
515 * we need to stop the pipeline. */
516 fprintf (stderr, _("Interrupt: Setting pipeline to PAUSED ...\n"));
517 gst_element_set_state (pipeline, GST_STATE_PAUSED);
518 /* return TRUE when we caught an interrupt */
524 /* just be quiet by default */
528 gst_message_unref (message);
530 g_assert_not_reached ();
535 gst_message_unref (message);
536 gst_object_unref (bus);
542 main (int argc, char *argv[])
547 gboolean verbose = FALSE;
548 gboolean no_fault = FALSE;
549 gboolean trace = FALSE;
550 gchar *savefile = NULL;
551 gchar *exclude_args = NULL;
552 GOptionEntry options[] = {
553 {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
554 N_("Output tags (also known as metadata)"), NULL},
555 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
556 N_("Output status information and property notifications"), NULL},
557 {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
558 N_("Output messages"), NULL},
559 {"exclude", 'X', 0, G_OPTION_ARG_NONE, &exclude_args,
560 N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
561 #ifndef GST_DISABLE_LOADSAVE
562 {"output", 'o', 0, G_OPTION_ARG_STRING, &savefile,
563 N_("Save xml representation of pipeline to FILE and exit"), N_("FILE")},
565 {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
566 N_("Do not install a fault handler"), NULL},
567 {"trace", 'T', 0, G_OPTION_ARG_NONE, &trace,
568 N_("Print alloc trace (if enabled at compile time)"), NULL},
569 GST_TOOLS_GOPTION_VERSION,
575 GError *error = NULL;
578 free (malloc (8)); /* -lefence */
581 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
582 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
583 textdomain (GETTEXT_PACKAGE);
586 if (!g_thread_supported ())
587 g_thread_init (NULL);
589 gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
591 ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
592 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
593 g_option_context_add_group (ctx, gst_init_get_option_group ());
594 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
596 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
598 g_print ("Error initializing: Unknown error!\n");
601 g_option_context_free (ctx);
603 gst_tools_print_version ("gst-launch");
605 /* FIXpopt: strip short args, too. We do it ourselves for now */
607 for (i = 1; i < argc; i++) {
608 if (*(argv[i]) == '-') {
609 if (strlen (argv[i]) == 2) {
613 if (*c == 'X' || *c == 'o') {
624 #ifndef DISABLE_FAULT_HANDLER
629 play_signal_setup ();
633 if (!gst_alloc_trace_available ()) {
634 g_warning ("Trace not available (recompile with trace enabled).");
636 gst_alloc_trace_print_live ();
639 /* make a null-terminated version of argv */
640 argvn = g_new0 (char *, argc);
641 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
642 #ifndef GST_DISABLE_LOADSAVE
643 if (strstr (argv[0], "gst-xmllaunch")) {
644 pipeline = xmllaunch_parse_cmdline ((const gchar **) argvn);
649 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
655 fprintf (stderr, _("ERROR: pipeline could not be constructed: %s.\n"),
656 GST_STR_NULL (error->message));
657 g_error_free (error);
659 fprintf (stderr, _("ERROR: pipeline could not be constructed.\n"));
663 fprintf (stderr, _("WARNING: erroneous pipeline: %s\n"),
664 GST_STR_NULL (error->message));
665 g_error_free (error);
670 gchar **exclude_list =
671 exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
672 g_signal_connect (pipeline, "deep_notify",
673 G_CALLBACK (gst_object_default_deep_notify), exclude_list);
675 #ifndef GST_DISABLE_LOADSAVE
677 gst_xml_write_file (GST_ELEMENT (pipeline), fopen (savefile, "w"));
682 GstState state, pending;
683 GstStateChangeReturn ret;
685 /* If the top-level object is not a pipeline, place it in a pipeline. */
686 if (!GST_IS_PIPELINE (pipeline)) {
687 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
689 if (real_pipeline == NULL) {
690 fprintf (stderr, _("ERROR: the 'pipeline' element wasn't found.\n"));
693 gst_bin_add (GST_BIN (real_pipeline), pipeline);
694 pipeline = real_pipeline;
696 fprintf (stderr, _("Setting pipeline to PAUSED ...\n"));
697 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
700 case GST_STATE_CHANGE_FAILURE:
701 fprintf (stderr, _("ERROR: Pipeline doesn't want to pause.\n"));
703 event_loop (pipeline, FALSE, GST_STATE_VOID_PENDING);
705 case GST_STATE_CHANGE_NO_PREROLL:
706 fprintf (stderr, _("Pipeline is live and does not need PREROLL ...\n"));
708 case GST_STATE_CHANGE_ASYNC:
709 fprintf (stderr, _("Pipeline is PREROLLING ...\n"));
710 caught_error = event_loop (pipeline, TRUE, GST_STATE_PAUSED);
712 fprintf (stderr, _("ERROR: pipeline doesn't want to preroll.\n"));
715 state = GST_STATE_PAUSED;
717 case GST_STATE_CHANGE_SUCCESS:
718 fprintf (stderr, _("Pipeline is PREROLLED ...\n"));
722 caught_error = event_loop (pipeline, FALSE, GST_STATE_PLAYING);
725 fprintf (stderr, _("ERROR: pipeline doesn't want to preroll.\n"));
727 GTimeVal tfthen, tfnow;
728 GstClockTimeDiff diff;
730 fprintf (stderr, _("Setting pipeline to PLAYING ...\n"));
731 if (gst_element_set_state (pipeline,
732 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
733 fprintf (stderr, _("ERROR: pipeline doesn't want to play.\n"));
738 g_get_current_time (&tfthen);
739 caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
740 g_get_current_time (&tfnow);
742 diff = GST_TIMEVAL_TO_TIME (tfnow) - GST_TIMEVAL_TO_TIME (tfthen);
744 g_print (_("Execution ended after %" G_GUINT64_FORMAT " ns.\n"), diff);
747 /* iterate mainloop to process pending stuff */
748 while (g_main_context_iteration (NULL, FALSE));
750 fprintf (stderr, _("Setting pipeline to PAUSED ...\n"));
751 gst_element_set_state (pipeline, GST_STATE_PAUSED);
752 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
753 fprintf (stderr, _("Setting pipeline to READY ...\n"));
754 gst_element_set_state (pipeline, GST_STATE_READY);
755 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
758 fprintf (stderr, _("Setting pipeline to NULL ...\n"));
759 gst_element_set_state (pipeline, GST_STATE_NULL);
760 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
763 fprintf (stderr, _("FREEING pipeline ...\n"));
764 gst_object_unref (pipeline);
768 gst_alloc_trace_print_live ();