]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - tools/gst-launch.c
301351f532830300412d95c9d24e05d944b29987
[glsdk/gstreamer0-10.git] / tools / gst-launch.c
1 /* GStreamer
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>
5  *
6  * gst-launch.c: tool to launch GStreamer pipelines from the command line
7  *
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.
12  *
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.
17  *
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.
22  */
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
28 /* FIXME: hack alert */
29 #ifdef _MSC_VER
30 #define DISABLE_FAULT_HANDLER
31 #endif
33 #include <string.h>
34 #include <stdlib.h>
35 #include <signal.h>
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #ifndef DISABLE_FAULT_HANDLER
40 #include <sys/wait.h>
41 #endif
42 #include <locale.h>             /* for LC_ALL */
43 #include "gst/gst-i18n-app.h"
45 #include <gst/gst.h>
47 /* FIXME: This is just a temporary hack.  We should have a better
48  * check for siginfo handling. */
49 #ifdef SA_SIGINFO
50 #define USE_SIGINFO
51 #endif
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);
59 #endif
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
70 static GstElement *
71 xmllaunch_parse_cmdline (const gchar ** argv)
72 {
73   GstElement *pipeline = NULL, *e;
74   GstXML *xml;
75   gboolean err;
76   const gchar *arg;
77   gchar *element, *property, *value;
78   GList *l;
79   gint i = 0;
81   if (!(arg = argv[0])) {
82     g_print (_
83         ("Usage: gst-xmllaunch <file.xml> [ element.property=value ... ]\n"));
84     exit (1);
85   }
87   xml = gst_xml_new ();
88   /* FIXME guchar from gstxml.c */
89   err = gst_xml_parse_file (xml, (guchar *) arg, NULL);
91   if (err != TRUE) {
92     fprintf (stderr, _("ERROR: parse of xml file '%s' failed.\n"), arg);
93     exit (1);
94   }
96   l = gst_xml_get_topelements (xml);
97   if (!l) {
98     fprintf (stderr, _("ERROR: no toplevel pipeline element in file '%s'.\n"),
99         arg);
100     exit (1);
101   }
103   if (l->next)
104     fprintf (stderr,
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)) {
115       fprintf (stderr,
116           _("ERROR: could not parse command line argument %d: %s.\n"), i,
117           element);
118       g_free (element);
119       exit (1);
120     }
122     *property++ = '\0';
123     *value++ = '\0';
125     e = gst_bin_get_by_name (GST_BIN (pipeline), element);
126     if (!e) {
127       fprintf (stderr, _("WARNING: element named '%s' not found.\n"), element);
128     } else {
129       gst_util_set_object_arg (G_OBJECT (e), property, value);
130     }
131     g_free (element);
132   }
134   if (!l)
135     return NULL;
136   else
137     return l->data;
139 #endif
141 #ifndef DISABLE_FAULT_HANDLER
142 #ifndef USE_SIGINFO
143 static void
144 fault_handler_sighandler (int signum)
146   fault_restore ();
148   /* printf is used instead of g_print(), since it's less likely to
149    * deadlock */
150   switch (signum) {
151     case SIGSEGV:
152       printf ("Caught SIGSEGV\n");
153       break;
154     case SIGQUIT:
155       printf ("Caught SIGQUIT\n");
156       break;
157     default:
158       printf ("signo:  %d\n", signum);
159       break;
160   }
162   fault_spin ();
165 #else /* USE_SIGINFO */
167 static void
168 fault_handler_sigaction (int signum, siginfo_t * si, void *misc)
170   fault_restore ();
172   /* printf is used instead of g_print(), since it's less likely to
173    * deadlock */
174   switch (si->si_signo) {
175     case SIGSEGV:
176       printf ("Caught SIGSEGV accessing address %p\n", si->si_addr);
177       break;
178     case SIGQUIT:
179       printf ("Caught SIGQUIT\n");
180       break;
181     default:
182       printf ("signo:  %d\n", si->si_signo);
183       printf ("errno:  %d\n", si->si_errno);
184       printf ("code:   %d\n", si->si_code);
185       break;
186   }
188   fault_spin ();
190 #endif /* USE_SIGINFO */
192 static void
193 fault_spin (void)
195   int spinning = TRUE;
197   glib_on_error_halt = FALSE;
198   g_on_error_stack_trace ("gst-launch");
200   wait (NULL);
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 ());
205   while (spinning)
206     g_usleep (1000000);
209 static void
210 fault_restore (void)
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);
221 static void
222 fault_setup (void)
224   struct sigaction action;
226   memset (&action, 0, sizeof (action));
227 #ifdef USE_SIGINFO
228   action.sa_sigaction = fault_handler_sigaction;
229   action.sa_flags = SA_SIGINFO;
230 #else
231   action.sa_handler = fault_handler_sighandler;
232 #endif
234   sigaction (SIGSEGV, &action, NULL);
235   sigaction (SIGQUIT, &action, NULL);
237 #endif /* DISABLE_FAULT_HANDLER */
239 static void
240 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
242   gint i, count;
244   count = gst_tag_list_get_tag_size (list, tag);
246   for (i = 0; i < count; i++) {
247     gchar *str;
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 ();
252     } else {
253       str =
254           g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
255     }
257     if (i == 0) {
258       g_print ("%15s: %s\n", gst_tag_get_nick (tag), str);
259     } else {
260       g_print ("               : %s\n", str);
261     }
263     g_free (str);
264   }
267 #ifndef DISABLE_FAULT_HANDLER
268 /* we only use sighandler here because the registers are not important */
269 static void
270 sigint_handler_sighandler (int signum)
272   g_print ("Caught interrupt -- ");
274   sigint_restore ();
276   caught_intr = TRUE;
279 static gboolean
280 check_intr (GstElement * pipeline)
282   if (!caught_intr) {
283     return TRUE;
284   } else {
285     GstBus *bus;
286     GstMessage *message;
288     caught_intr = FALSE;
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);
302     return FALSE;
303   }
306 static void
307 sigint_setup (void)
309   struct sigaction action;
311   memset (&action, 0, sizeof (action));
312   action.sa_handler = sigint_handler_sighandler;
314   sigaction (SIGINT, &action, NULL);
317 static void
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);
328 static void
329 play_handler (int signum)
331   switch (signum) {
332     case SIGUSR1:
333       g_print ("Caught SIGUSR1 - Play request.\n");
334       gst_element_set_state (pipeline, GST_STATE_PLAYING);
335       break;
336     case SIGUSR2:
337       g_print ("Caught SIGUSR2 - Stop request.\n");
338       gst_element_set_state (pipeline, GST_STATE_NULL);
339       break;
340   }
343 static void
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 */
355 static gboolean
356 event_loop (GstElement * pipeline, gboolean blocking)
358   GstBus *bus;
359   GstMessage *message = NULL;
361   bus = gst_element_get_bus (GST_ELEMENT (pipeline));
363 #ifndef DISABLE_FAULT_HANDLER
364   g_timeout_add (50, (GSourceFunc) check_intr, pipeline);
365 #endif
367   while (TRUE) {
368     message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
370     /* if the poll timed out, only when !blocking */
371     if (message == NULL) {
372       gst_object_unref (bus);
373       return FALSE;
374     }
376     if (messages) {
377       const GstStructure *s;
379       s = gst_message_get_structure (message);
380       g_print (_("Got Message from element \"%s\"\n"),
381           GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
382       if (s) {
383         gchar *sstr;
385         sstr = gst_structure_to_string (s);
386         g_print ("%s\n", sstr);
387         g_free (sstr);
388       }
389     }
391     switch (GST_MESSAGE_TYPE (message)) {
392       case GST_MESSAGE_EOS:
393         g_print (_
394             ("Got EOS from element \"%s\".\n"),
395             GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
396         gst_message_unref (message);
397         gst_object_unref (bus);
398         return FALSE;
399       case GST_MESSAGE_TAG:
400         if (tags) {
401           GstTagList *tags;
403           gst_message_parse_tag (message, &tags);
404           g_print (_("FOUND TAG      : found by element \"%s\".\n"),
405               GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
406           gst_tag_list_foreach (tags, print_tag, NULL);
407           gst_tag_list_free (tags);
408         }
409         gst_message_unref (message);
410         break;
411       case GST_MESSAGE_WARNING:{
412         GError *gerror;
413         gchar *debug;
415         gst_message_parse_warning (message, &gerror, &debug);
416         if (debug) {
417           g_print ("WARNING: Element \"%s\" warns: %s\n",
418               GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
419               debug);
420         }
421         gst_message_unref (message);
422         if (gerror)
423           g_error_free (gerror);
424         g_free (debug);
425         break;
426       }
427       case GST_MESSAGE_ERROR:{
428         GError *gerror;
429         gchar *debug;
431         gst_message_parse_error (message, &gerror, &debug);
432         gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
433         gst_message_unref (message);
434         if (gerror)
435           g_error_free (gerror);
436         g_free (debug);
437         gst_object_unref (bus);
438         return TRUE;
439       }
440       case GST_MESSAGE_STATE_CHANGED:{
441         GstState old, new, pending;
443         gst_message_parse_state_changed (message, &old, &new, &pending);
444         if (!(old == GST_STATE_PLAYING && new == GST_STATE_PAUSED &&
445                 GST_MESSAGE_SRC (message) == GST_OBJECT (pipeline))) {
446           gst_message_unref (message);
447           break;
448         }
449         g_print (_
450             ("Element \"%s\" has gone from PLAYING to PAUSED, quitting.\n"),
451             GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
452         /* cut out of the event loop if check_intr set us to PAUSED */
453         gst_message_unref (message);
454         gst_object_unref (bus);
455         return FALSE;
456       }
457       default:
458         /* just be quiet by default */
459         gst_message_unref (message);
460         break;
461     }
462   }
464   g_assert_not_reached ();
465   return TRUE;
468 int
469 main (int argc, char *argv[])
471   gint i, j;
473   /* options */
474   gboolean verbose = FALSE;
475   gboolean no_fault = FALSE;
476   gboolean trace = FALSE;
477   gchar *savefile = NULL;
478   gchar *exclude_args = NULL;
479   struct poptOption options[] = {
480     {"tags", 't', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &tags, 0,
481         N_("Output tags (also known as metadata)"), NULL},
482     {"messages", 'm', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &messages, 0,
483         N_("Output messages"), NULL},
484     {"verbose", 'v', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &verbose, 0,
485         N_("Output status information and property notifications"), NULL},
486     {"exclude", 'X', POPT_ARG_STRING | POPT_ARGFLAG_STRIP, &exclude_args, 0,
487         N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
488 #ifndef GST_DISABLE_LOADSAVE
489     {"output", 'o', POPT_ARG_STRING | POPT_ARGFLAG_STRIP, &savefile, 0,
490         N_("Save xml representation of pipeline to FILE and exit"), N_("FILE")},
491 #endif
492     {"no-fault", 'f', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &no_fault, 0,
493         N_("Do not install a fault handler"), NULL},
494     {"trace", 'T', POPT_ARG_NONE | POPT_ARGFLAG_STRIP, &trace, 0,
495         N_("Print alloc trace (if enabled at compile time)"), NULL},
496     {"iterations", 'i', POPT_ARG_INT | POPT_ARGFLAG_STRIP, &max_iterations, 0,
497         N_("Number of times to iterate pipeline"), NULL},
498     POPT_TABLEEND
499   };
501   gchar **argvn;
502   GError *error = NULL;
503   gint res = 0;
505   free (malloc (8));            /* -lefence */
507 #ifdef GETTEXT_PACKAGE
508   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
509   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
510   textdomain (GETTEXT_PACKAGE);
511 #endif
513   gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
515   gst_init_with_popt_table (&argc, &argv, options);
517   /* FIXpopt: strip short args, too. We do it ourselves for now */
518   j = 1;
519   for (i = 1; i < argc; i++) {
520     if (*(argv[i]) == '-') {
521       if (strlen (argv[i]) == 2) {
522         gchar *c = argv[i];
524         c++;
525         if (*c == 'X' || *c == 'o') {
526           i++;
527         }
528       }
529     } else {
530       argv[j] = argv[i];
531       j++;
532     }
533   }
534   argc = j;
536 #ifndef DISABLE_FAULT_HANDLER
537   if (!no_fault)
538     fault_setup ();
540   sigint_setup ();
541   play_signal_setup ();
542 #endif
544   if (trace) {
545     if (!gst_alloc_trace_available ()) {
546       g_warning ("Trace not available (recompile with trace enabled).");
547     }
548     gst_alloc_trace_print_live ();
549   }
551   /* make a null-terminated version of argv */
552   argvn = g_new0 (char *, argc);
553   memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
554 #ifndef GST_DISABLE_LOADSAVE
555   if (strstr (argv[0], "gst-xmllaunch")) {
556     pipeline = xmllaunch_parse_cmdline ((const gchar **) argvn);
557   } else
558 #endif
559   {
560     pipeline =
561         (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
562   }
563   g_free (argvn);
565   if (!pipeline) {
566     if (error) {
567       fprintf (stderr, _("ERROR: pipeline could not be constructed: %s.\n"),
568           error->message);
569       g_error_free (error);
570     } else {
571       fprintf (stderr, _("ERROR: pipeline could not be constructed.\n"));
572     }
573     return 1;
574   } else if (error) {
575     fprintf (stderr, _("WARNING: erroneous pipeline: %s\n"), error->message);
576     g_error_free (error);
577     return 1;
578   }
580   if (verbose) {
581     gchar **exclude_list =
582         exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
583     g_signal_connect (pipeline, "deep_notify",
584         G_CALLBACK (gst_object_default_deep_notify), exclude_list);
585   }
586 #ifndef GST_DISABLE_LOADSAVE
587   if (savefile) {
588     gst_xml_write_file (GST_ELEMENT (pipeline), fopen (savefile, "w"));
589   }
590 #endif
592   if (!savefile) {
593     GstState state, pending;
594     GstStateChangeReturn ret;
596     if (!GST_IS_BIN (pipeline)) {
597       GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
599       if (real_pipeline == NULL) {
600         fprintf (stderr, _("ERROR: the 'pipeline' element wasn't found.\n"));
601         return 1;
602       }
603       gst_bin_add (GST_BIN (real_pipeline), pipeline);
604       pipeline = real_pipeline;
605     }
607     fprintf (stderr, _("PAUSE pipeline ...\n"));
608     ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
610     switch (ret) {
611       case GST_STATE_CHANGE_FAILURE:
612         fprintf (stderr, _("ERROR: pipeline doesn't want to pause.\n"));
613         res = -1;
614         event_loop (pipeline, FALSE);
615         goto end;
616       case GST_STATE_CHANGE_NO_PREROLL:
617         fprintf (stderr, _("NO_PREROLL pipeline ...\n"));
618         break;
619       case GST_STATE_CHANGE_ASYNC:
620         fprintf (stderr, _("PREROLL pipeline ...\n"));
621         gst_element_get_state (pipeline, &state, &pending, NULL);
622         /* fallthrough */
623       case GST_STATE_CHANGE_SUCCESS:
624         fprintf (stderr, _("PREROLLED pipeline ...\n"));
625         break;
626     }
628     caught_error = event_loop (pipeline, FALSE);
630     if (caught_error) {
631       fprintf (stderr, _("ERROR: pipeline doesn't want to preroll.\n"));
632     } else {
633       GTimeVal tfthen, tfnow;
634       GstClockTimeDiff diff;
636       fprintf (stderr, _("RUNNING pipeline ...\n"));
637       if (gst_element_set_state (pipeline,
638               GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
639         fprintf (stderr, _("ERROR: pipeline doesn't want to play.\n"));
640         res = -1;
641         goto end;
642       }
644       g_get_current_time (&tfthen);
645       caught_error = event_loop (pipeline, TRUE);
646       g_get_current_time (&tfnow);
648       diff = GST_TIMEVAL_TO_TIME (tfnow) - GST_TIMEVAL_TO_TIME (tfthen);
650       g_print (_("Execution ended after %" G_GUINT64_FORMAT " ns.\n"), diff);
651     }
652     while (g_main_context_iteration (NULL, FALSE));
654     fprintf (stderr, _("PAUSE pipeline ...\n"));
655     gst_element_set_state (pipeline, GST_STATE_PAUSED);
656     gst_element_get_state (pipeline, &state, &pending, NULL);
657     fprintf (stderr, _("READY pipeline ...\n"));
658     gst_element_set_state (pipeline, GST_STATE_READY);
659     gst_element_get_state (pipeline, &state, &pending, NULL);
661   end:
662     fprintf (stderr, _("NULL pipeline ...\n"));
663     gst_element_set_state (pipeline, GST_STATE_NULL);
664     gst_element_get_state (pipeline, &state, &pending, NULL);
665   }
667   fprintf (stderr, _("FREEING pipeline ...\n"));
668   gst_object_unref (pipeline);
670   gst_deinit ();
671   if (trace)
672     gst_alloc_trace_print_live ();
674   return res;