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 static gboolean caught_intr = FALSE;
64 static gboolean caught_error = FALSE;
65 static gboolean tags = FALSE;
66 static gboolean messages = FALSE;
69 #ifndef GST_DISABLE_LOADSAVE
71 xmllaunch_parse_cmdline (const gchar ** argv)
73 GstElement *pipeline = NULL, *e;
77 gchar *element, *property, *value;
81 if (!(arg = argv[0])) {
83 ("Usage: gst-xmllaunch <file.xml> [ element.property=value ... ]\n"));
88 /* FIXME guchar from gstxml.c */
89 err = gst_xml_parse_file (xml, (guchar *) arg, NULL);
92 fprintf (stderr, _("ERROR: parse of xml file '%s' failed.\n"), arg);
96 l = gst_xml_get_topelements (xml);
98 fprintf (stderr, _("ERROR: no toplevel pipeline element in file '%s'.\n"),
105 _("WARNING: only one toplevel element is supported at this time."));
107 pipeline = GST_ELEMENT (l->data);
109 while ((arg = argv[++i])) {
110 element = g_strdup (arg);
111 property = strchr (element, '.');
112 value = strchr (element, '=');
114 if (!(element < property && property < value)) {
116 _("ERROR: could not parse command line argument %d: %s.\n"), i,
125 e = gst_bin_get_by_name (GST_BIN (pipeline), element);
127 fprintf (stderr, _("WARNING: element named '%s' not found.\n"), element);
129 gst_util_set_object_arg (G_OBJECT (e), property, value);
141 #ifndef DISABLE_FAULT_HANDLER
144 fault_handler_sighandler (int signum)
148 /* printf is used instead of g_print(), since it's less likely to
152 printf ("Caught SIGSEGV\n");
155 printf ("Caught SIGQUIT\n");
158 printf ("signo: %d\n", signum);
165 #else /* USE_SIGINFO */
168 fault_handler_sigaction (int signum, siginfo_t * si, void *misc)
172 /* printf is used instead of g_print(), since it's less likely to
174 switch (si->si_signo) {
176 printf ("Caught SIGSEGV accessing address %p\n", si->si_addr);
179 printf ("Caught SIGQUIT\n");
182 printf ("signo: %d\n", si->si_signo);
183 printf ("errno: %d\n", si->si_errno);
184 printf ("code: %d\n", si->si_code);
190 #endif /* USE_SIGINFO */
197 glib_on_error_halt = FALSE;
198 g_on_error_stack_trace ("gst-launch");
202 /* FIXME how do we know if we were run by libtool? */
203 printf ("Spinning. Please run 'gdb gst-launch %d' to continue debugging, "
204 "Ctrl-C to quit, or Ctrl-\\ to dump core.\n", (gint) getpid ());
212 struct sigaction action;
214 memset (&action, 0, sizeof (action));
215 action.sa_handler = SIG_DFL;
217 sigaction (SIGSEGV, &action, NULL);
218 sigaction (SIGQUIT, &action, NULL);
224 struct sigaction action;
226 memset (&action, 0, sizeof (action));
228 action.sa_sigaction = fault_handler_sigaction;
229 action.sa_flags = SA_SIGINFO;
231 action.sa_handler = fault_handler_sighandler;
234 sigaction (SIGSEGV, &action, NULL);
235 sigaction (SIGQUIT, &action, NULL);
237 #endif /* DISABLE_FAULT_HANDLER */
240 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
244 count = gst_tag_list_get_tag_size (list, tag);
246 for (i = 0; i < count; i++) {
249 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
250 if (!gst_tag_list_get_string_index (list, tag, i, &str))
251 g_assert_not_reached ();
254 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
258 g_print ("%15s: %s\n", gst_tag_get_nick (tag), str);
260 g_print (" : %s\n", str);
267 #ifndef DISABLE_FAULT_HANDLER
268 /* we only use sighandler here because the registers are not important */
270 sigint_handler_sighandler (int signum)
272 g_print ("Caught interrupt -- ");
280 check_intr (GstElement * pipeline)
289 g_print ("Pausing pipeline.\n");
291 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
292 message = gst_message_new_warning (GST_OBJECT (pipeline),
293 NULL, "pipeline interrupted");
294 gst_bus_post (bus, message);
296 /* pipeline will wait for element to go to PAUSED */
297 gst_element_set_state (pipeline, GST_STATE_PAUSED);
298 g_print ("Pipeline paused.\n");
300 gst_object_unref (bus);
309 struct sigaction action;
311 memset (&action, 0, sizeof (action));
312 action.sa_handler = sigint_handler_sighandler;
314 sigaction (SIGINT, &action, NULL);
318 sigint_restore (void)
320 struct sigaction action;
322 memset (&action, 0, sizeof (action));
323 action.sa_handler = SIG_DFL;
325 sigaction (SIGINT, &action, NULL);
329 play_handler (int signum)
333 g_print ("Caught SIGUSR1 - Play request.\n");
334 gst_element_set_state (pipeline, GST_STATE_PLAYING);
337 g_print ("Caught SIGUSR2 - Stop request.\n");
338 gst_element_set_state (pipeline, GST_STATE_NULL);
344 play_signal_setup (void)
346 struct sigaction action;
348 memset (&action, 0, sizeof (action));
349 action.sa_handler = play_handler;
350 sigaction (SIGUSR1, &action, NULL);
351 sigaction (SIGUSR2, &action, NULL);
353 #endif /* DISABLE_FAULT_HANDLER */
356 event_loop (GstElement * pipeline, gboolean blocking)
359 GstMessageType revent;
360 GstMessage *message = NULL;
362 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
364 g_timeout_add (50, (GSourceFunc) check_intr, pipeline);
367 revent = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
369 /* if the poll timed out, only when !blocking */
370 if (revent == GST_MESSAGE_UNKNOWN) {
371 gst_object_unref (bus);
375 message = gst_bus_pop (bus);
376 g_return_val_if_fail (message != NULL, TRUE);
379 const GstStructure *s;
381 s = gst_message_get_structure (message);
382 g_print (_("Got Message from element \"%s\"\n"),
383 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
387 sstr = gst_structure_to_string (s);
388 g_print ("%s\n", sstr);
394 case GST_MESSAGE_EOS:
396 ("Got EOS from element \"%s\".\n"),
397 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
398 gst_message_unref (message);
399 gst_object_unref (bus);
401 case GST_MESSAGE_TAG:
405 gst_message_parse_tag (message, &tags);
406 g_print (_("FOUND TAG : found by element \"%s\".\n"),
407 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
408 gst_tag_list_foreach (tags, print_tag, NULL);
409 gst_tag_list_free (tags);
411 gst_message_unref (message);
413 case GST_MESSAGE_WARNING:{
417 gst_message_parse_warning (message, &gerror, &debug);
419 g_print ("WARNING: Element \"%s\" warns: %s\n",
420 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
423 gst_message_unref (message);
425 g_error_free (gerror);
429 case GST_MESSAGE_ERROR:{
433 gst_message_parse_error (message, &gerror, &debug);
434 if (GST_IS_OBJECT (GST_MESSAGE_SRC (message)))
435 gst_object_default_error (GST_OBJECT (GST_MESSAGE_SRC (message)),
437 gst_message_unref (message);
439 g_error_free (gerror);
441 gst_object_unref (bus);
444 case GST_MESSAGE_STATE_CHANGED:{
445 GstElementState old, new;
447 gst_message_parse_state_changed (message, &old, &new);
448 if (!(old == GST_STATE_PLAYING && new == GST_STATE_PAUSED &&
449 GST_MESSAGE_SRC (message) == G_OBJECT (pipeline))) {
450 gst_message_unref (message);
454 ("Element \"%s\" has gone from PLAYING to PAUSED, quitting.\n"),
455 GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
456 /* cut out of the event loop if check_intr set us to PAUSED */
457 gst_message_unref (message);
458 gst_object_unref (bus);
462 /* just be quiet by default */
463 gst_message_unref (message);
468 g_assert_not_reached ();
473 main (int argc, char *argv[])
478 gboolean verbose = FALSE;
479 gboolean no_fault = FALSE;
480 gboolean trace = FALSE;
481 gchar *savefile = NULL;
482 gchar *exclude_args = NULL;
483 struct poptOption options[] = {
484 {"tags", 't', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &tags, 0,
485 N_("Output tags (also known as metadata)"), NULL},
486 {"messages", 'm', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &messages, 0,
487 N_("Output messages"), NULL},
488 {"verbose", 'v', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &verbose, 0,
489 N_("Output status information and property notifications"), NULL},
490 {"exclude", 'X', POPT_ARG_STRING | POPT_ARGFLAG_STRIP, &exclude_args, 0,
491 N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
492 #ifndef GST_DISABLE_LOADSAVE
493 {"output", 'o', POPT_ARG_STRING | POPT_ARGFLAG_STRIP, &savefile, 0,
494 N_("Save xml representation of pipeline to FILE and exit"), N_("FILE")},
496 {"no-fault", 'f', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &no_fault, 0,
497 N_("Do not install a fault handler"), NULL},
498 {"trace", 'T', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &trace, 0,
499 N_("Print alloc trace (if enabled at compile time)"), NULL},
500 {"iterations", 'i', POPT_ARG_INT | POPT_ARGFLAG_STRIP, &max_iterations, 0,
501 N_("Number of times to iterate pipeline"), NULL},
506 GError *error = NULL;
509 free (malloc (8)); /* -lefence */
511 #ifdef GETTEXT_PACKAGE
512 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
513 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
514 textdomain (GETTEXT_PACKAGE);
517 gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
519 gst_init_with_popt_table (&argc, &argv, options);
521 /* FIXpopt: strip short args, too. We do it ourselves for now */
523 for (i = 1; i < argc; i++) {
524 if (*(argv[i]) == '-') {
525 if (strlen (argv[i]) == 2) {
529 if (*c == 'X' || *c == 'o') {
540 #ifndef DISABLE_FAULT_HANDLER
545 play_signal_setup ();
549 if (!gst_alloc_trace_available ()) {
550 g_warning ("Trace not available (recompile with trace enabled).");
552 gst_alloc_trace_print_live ();
555 /* make a null-terminated version of argv */
556 argvn = g_new0 (char *, argc);
557 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
558 #ifndef GST_DISABLE_LOADSAVE
559 if (strstr (argv[0], "gst-xmllaunch")) {
560 pipeline = xmllaunch_parse_cmdline ((const gchar **) argvn);
565 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
571 fprintf (stderr, _("ERROR: pipeline could not be constructed: %s.\n"),
573 g_error_free (error);
575 fprintf (stderr, _("ERROR: pipeline could not be constructed.\n"));
579 fprintf (stderr, _("WARNING: erroneous pipeline: %s\n"), error->message);
580 fprintf (stderr, _(" Trying to run anyway.\n"));
581 g_error_free (error);
585 gchar **exclude_list =
586 exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
587 g_signal_connect (pipeline, "deep_notify",
588 G_CALLBACK (gst_object_default_deep_notify), exclude_list);
590 #ifndef GST_DISABLE_LOADSAVE
592 gst_xml_write_file (GST_ELEMENT (pipeline), fopen (savefile, "w"));
597 GstElementState state, pending;
598 GstElementStateReturn ret;
600 if (!GST_IS_BIN (pipeline)) {
601 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
603 if (real_pipeline == NULL) {
604 fprintf (stderr, _("ERROR: the 'pipeline' element wasn't found.\n"));
607 gst_bin_add (GST_BIN (real_pipeline), pipeline);
608 pipeline = real_pipeline;
611 fprintf (stderr, _("PAUSE pipeline ...\n"));
612 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
615 case GST_STATE_FAILURE:
616 fprintf (stderr, _("ERROR: pipeline doesn't want to pause.\n"));
619 case GST_STATE_NO_PREROLL:
620 fprintf (stderr, _("NO_PREROLL pipeline ...\n"));
622 case GST_STATE_ASYNC:
623 fprintf (stderr, _("PREROLL pipeline ...\n"));
624 gst_element_get_state (pipeline, &state, &pending, NULL);
626 case GST_STATE_SUCCESS:
627 fprintf (stderr, _("PREROLLED pipeline ...\n"));
631 caught_error = event_loop (pipeline, FALSE);
634 fprintf (stderr, _("ERROR: pipeline doesn't want to preroll.\n"));
636 GTimeVal tfthen, tfnow;
637 GstClockTimeDiff diff;
639 fprintf (stderr, _("RUNNING pipeline ...\n"));
640 if (gst_element_set_state (pipeline,
641 GST_STATE_PLAYING) == GST_STATE_FAILURE) {
642 fprintf (stderr, _("ERROR: pipeline doesn't want to play.\n"));
647 g_get_current_time (&tfthen);
648 caught_error = event_loop (pipeline, TRUE);
649 g_get_current_time (&tfnow);
651 diff = GST_TIMEVAL_TO_TIME (tfnow) - GST_TIMEVAL_TO_TIME (tfthen);
653 g_print (_("Execution ended after %" G_GUINT64_FORMAT " ns.\n"), diff);
655 while (g_main_context_iteration (NULL, FALSE));
657 fprintf (stderr, _("PAUSE pipeline ...\n"));
658 gst_element_set_state (pipeline, GST_STATE_PAUSED);
659 gst_element_get_state (pipeline, &state, &pending, NULL);
660 fprintf (stderr, _("READY pipeline ...\n"));
661 gst_element_set_state (pipeline, GST_STATE_READY);
662 gst_element_get_state (pipeline, &state, &pending, NULL);
665 fprintf (stderr, _("NULL pipeline ...\n"));
666 gst_element_set_state (pipeline, GST_STATE_NULL);
667 gst_element_get_state (pipeline, &state, &pending, NULL);
670 fprintf (stderr, _("FREEING pipeline ...\n"));
671 gst_object_unref (pipeline);
675 gst_alloc_trace_print_live ();