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.
33 #include <locale.h> /* for LC_ALL */
34 #include "gst/gst-i18n-app.h"
38 /* FIXME: This is just a temporary hack. We should have a better
39 * check for siginfo handling. */
44 extern volatile gboolean glib_on_error_halt;
45 static void fault_restore (void);
46 static void fault_spin (void);
47 static void sigint_restore (void);
49 static gint max_iterations = 0;
50 static guint64 iterations = 0;
51 static guint64 sum = 0;
52 static guint64 min = G_MAXINT64;
53 static guint64 max = 0;
54 static GstClock *s_clock;
55 static GstElement *pipeline;
56 gboolean caught_intr = FALSE;
59 idle_func (gpointer data)
62 GTimeVal tfthen, tfnow;
63 GstClockTimeDiff diff;
65 g_get_current_time (&tfthen);
66 busy = gst_bin_iterate (GST_BIN (data));
68 g_get_current_time (&tfnow);
70 diff = GST_TIMEVAL_TO_TIME (tfnow) -
71 GST_TIMEVAL_TO_TIME (tfthen);
74 min = MIN (min, diff);
75 max = MAX (max, diff);
77 if (!busy || caught_intr || (max_iterations>0 && iterations>=max_iterations)) {
79 g_print (_("Execution ended after %" G_GUINT64_FORMAT " iterations (sum %" G_GUINT64_FORMAT " ns, average %" G_GUINT64_FORMAT " ns, min %" G_GUINT64_FORMAT " ns, max %" G_GUINT64_FORMAT " ns).\n"),
80 iterations, sum, sum/iterations, min, max);
86 #ifndef GST_DISABLE_LOADSAVE
88 xmllaunch_parse_cmdline (const gchar **argv)
90 GstElement *pipeline = NULL, *e;
94 gchar *element, *property, *value;
98 if (!(arg = argv[0])) {
99 g_print (_("Usage: gst-xmllaunch <file.xml> [ element.property=value ... ]\n"));
103 xml = gst_xml_new ();
104 err = gst_xml_parse_file(xml, arg, NULL);
107 fprintf (stderr, _("ERROR: parse of xml file '%s' failed.\n"), arg);
111 l = gst_xml_get_topelements (xml);
113 fprintf (stderr, _("ERROR: no toplevel pipeline element in file '%s'.\n"), arg);
118 fprintf (stderr, _("WARNING: only one toplevel element is supported at this time."));
120 pipeline = GST_ELEMENT (l->data);
122 while ((arg = argv[++i])) {
123 element = g_strdup (arg);
124 property = strchr (element, '.');
125 value = strchr (element, '=');
127 if (!(element < property && property < value)) {
128 fprintf (stderr, _("ERROR: could not parse command line argument %d: %s.\n"), i, element);
136 e = gst_bin_get_by_name (GST_BIN (pipeline), element);
138 fprintf (stderr, _("WARNING: element named '%s' not found.\n"), element);
140 gst_util_set_object_arg (G_OBJECT (e), property, value);
154 fault_handler_sighandler (int signum)
160 g_print ("Caught SIGSEGV\n");
163 g_print ("Caught SIGQUIT\n");
166 g_print ("signo: %d\n", signum);
176 fault_handler_sigaction (int signum, siginfo_t *si, void *misc)
180 switch (si->si_signo) {
182 g_print ("Caught SIGSEGV accessing address %p\n", si->si_addr);
185 g_print ("Caught SIGQUIT\n");
188 g_print ("signo: %d\n", si->si_signo);
189 g_print ("errno: %d\n", si->si_errno);
190 g_print ("code: %d\n", si->si_code);
203 glib_on_error_halt = FALSE;
204 g_on_error_stack_trace ("gst-launch");
208 /* FIXME how do we know if we were run by libtool? */
209 g_print ("Spinning. Please run 'gdb gst-launch %d' to continue debugging, "
210 "Ctrl-C to quit, or Ctrl-\\ to dump core.\n",
212 while (spinning) g_usleep (1000000);
218 struct sigaction action;
220 memset (&action, 0, sizeof (action));
221 action.sa_handler = SIG_DFL;
223 sigaction (SIGSEGV, &action, NULL);
224 sigaction (SIGQUIT, &action, NULL);
230 struct sigaction action;
232 memset (&action, 0, sizeof (action));
234 action.sa_sigaction = fault_handler_sigaction;
235 action.sa_flags = SA_SIGINFO;
237 action.sa_handler = fault_handler_sighandler;
240 sigaction (SIGSEGV, &action, NULL);
241 sigaction (SIGQUIT, &action, NULL);
245 print_tag (const GstTagList *list, const gchar *tag, gpointer unused)
249 count = gst_tag_list_get_tag_size (list, tag);
251 for (i = 0; i < count; i++) {
254 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
255 g_assert (gst_tag_list_get_string_index (list, tag, i, &str));
257 str = g_strdup_value_contents (
258 gst_tag_list_get_value_index (list, tag, i));
262 g_print ("%15s: %s\n", gst_tag_get_nick (tag), str);
264 g_print (" : %s\n", str);
271 found_tag (GObject *pipeline, GstElement *source, GstTagList *tags)
273 g_print (_("FOUND TAG : found by element \"%s\".\n"),
274 GST_STR_NULL (GST_ELEMENT_NAME (source)));
275 gst_tag_list_foreach (tags, print_tag, NULL);
278 /* we only use sighandler here because the registers are not important */
280 sigint_handler_sighandler (int signum)
282 g_print ("Caught interrupt.\n");
292 struct sigaction action;
294 memset (&action, 0, sizeof (action));
295 action.sa_handler = sigint_handler_sighandler;
297 sigaction (SIGINT, &action, NULL);
301 sigint_restore (void)
303 struct sigaction action;
305 memset (&action, 0, sizeof (action));
306 action.sa_handler = SIG_DFL;
308 sigaction (SIGINT, &action, NULL);
312 play_handler (int signum)
316 g_print ("Caught SIGUSR1 - Play request.\n");
317 gst_element_set_state (pipeline, GST_STATE_PLAYING);
320 g_print ("Caught SIGUSR2 - Stop request.\n");
321 gst_element_set_state (pipeline, GST_STATE_NULL);
327 play_signal_setup(void)
329 struct sigaction action;
331 memset (&action, 0, sizeof (action));
332 action.sa_handler = play_handler;
333 sigaction (SIGUSR1, &action, NULL);
334 sigaction (SIGUSR2, &action, NULL);
338 main(int argc, char *argv[])
342 gboolean verbose = FALSE;
343 gboolean tags = FALSE;
344 gboolean no_fault = FALSE;
345 gboolean trace = FALSE;
346 gchar *savefile = NULL;
347 gchar *exclude_args = NULL;
348 struct poptOption options[] = {
349 {"tags", 't', POPT_ARG_NONE|POPT_ARGFLAG_STRIP, &tags, 0,
350 N_("Output tags (also known as metadata)"), NULL},
351 {"verbose", 'v', POPT_ARG_NONE|POPT_ARGFLAG_STRIP, &verbose, 0,
352 N_("Output status information and property notifications"), NULL},
353 {"exclude", 'X', POPT_ARG_STRING|POPT_ARGFLAG_STRIP, &exclude_args, 0,
354 N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
355 #ifndef GST_DISABLE_LOADSAVE
356 {"output", 'o', POPT_ARG_STRING|POPT_ARGFLAG_STRIP, &savefile, 0,
357 N_("Save xml representation of pipeline to FILE and exit"), N_("FILE")},
359 {"no-fault", 'f', POPT_ARG_NONE|POPT_ARGFLAG_STRIP, &no_fault, 0,
360 N_("Do not install a fault handler"), NULL},
361 {"trace", 'T', POPT_ARG_NONE|POPT_ARGFLAG_STRIP, &trace, 0,
362 N_("Print alloc trace (if enabled at compile time)"), NULL},
363 {"iterations",'i',POPT_ARG_INT|POPT_ARGFLAG_STRIP, &max_iterations, 0,
364 N_("Number of times to iterate pipeline"), NULL},
369 GError *error = NULL;
372 free (malloc (8)); /* -lefence */
374 setlocale(LC_ALL, "");
375 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
376 textdomain (GETTEXT_PACKAGE);
378 gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
380 gst_init_with_popt_table (&argc, &argv, options);
382 /* FIXpopt: strip short args, too. We do it ourselves for now */
384 for (i = 1; i < argc; i++) {
385 if (*(argv[i]) == '-') {
386 if (strlen (argv[i]) == 2) {
389 if (*c == 'X' || *c == 'o') {
407 if (!gst_alloc_trace_available()) {
408 g_warning ("Trace not available (recompile with trace enabled).");
410 gst_alloc_trace_print_all ();
413 /* make a null-terminated version of argv */
414 argvn = g_new0 (char*, argc);
415 memcpy (argvn, argv+1, sizeof (char*) * (argc-1));
416 #ifndef GST_DISABLE_LOADSAVE
417 if (strstr (argv[0], "gst-xmllaunch")) {
418 pipeline = xmllaunch_parse_cmdline ((const gchar**)argvn);
423 pipeline = (GstElement*) gst_parse_launchv ((const gchar**)argvn, &error);
429 fprintf(stderr, _("ERROR: pipeline could not be constructed: %s.\n"),
431 g_error_free (error);
433 fprintf(stderr, _("ERROR: pipeline could not be constructed.\n"));
437 fprintf(stderr, _("WARNING: erroneous pipeline: %s\n"), error->message);
438 fprintf(stderr, _(" Trying to run anyway.\n"));
439 g_error_free (error);
443 gchar **exclude_list = exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
444 g_signal_connect (pipeline, "deep_notify", G_CALLBACK (gst_element_default_deep_notify), exclude_list);
447 g_signal_connect (pipeline, "found-tag", G_CALLBACK (found_tag), NULL);
449 g_signal_connect (pipeline, "error", G_CALLBACK (gst_element_default_error), NULL);
451 #ifndef GST_DISABLE_LOADSAVE
453 gst_xml_write_file (GST_ELEMENT (pipeline), fopen (savefile, "w"));
459 if (!GST_IS_BIN (pipeline)) {
460 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
461 if (real_pipeline == NULL) {
462 fprintf(stderr, _("ERROR: the 'pipeline' element wasn't found.\n"));
465 gst_bin_add (GST_BIN (real_pipeline), pipeline);
466 pipeline = real_pipeline;
469 fprintf(stderr, _("RUNNING pipeline ...\n"));
470 if (gst_element_set_state (pipeline, GST_STATE_PLAYING) == GST_STATE_FAILURE) {
471 fprintf(stderr, _("ERROR: pipeline doesn't want to play.\n"));
476 s_clock = gst_bin_get_clock (GST_BIN (pipeline));
478 if (!GST_FLAG_IS_SET (GST_OBJECT (pipeline), GST_BIN_SELF_SCHEDULABLE)) {
479 g_idle_add (idle_func, pipeline);
482 g_print ("Waiting for the state change... ");
483 gst_element_wait_state_change (pipeline);
484 g_print ("got the state change.\n");
487 gst_element_set_state (pipeline, GST_STATE_NULL);
492 gst_object_unref (GST_OBJECT (pipeline));
495 gst_alloc_trace_print_all ();