Change GST_MESSAGE_SRC to be a GObject rather than a GstObject, so that applications...
[platform/upstream/gstreamer.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  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 /* FIXME: hack alert */
29 #ifdef _MSC_VER
30 #define DISABLE_FAULT_HANDLER
31 #endif
32
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"
44
45 #include <gst/gst.h>
46
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
52
53 extern volatile gboolean glib_on_error_halt;
54
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
60
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;
67
68
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;
80
81   if (!(arg = argv[0])) {
82     g_print (_
83         ("Usage: gst-xmllaunch <file.xml> [ element.property=value ... ]\n"));
84     exit (1);
85   }
86
87   xml = gst_xml_new ();
88   /* FIXME guchar from gstxml.c */
89   err = gst_xml_parse_file (xml, (guchar *) arg, NULL);
90
91   if (err != TRUE) {
92     fprintf (stderr, _("ERROR: parse of xml file '%s' failed.\n"), arg);
93     exit (1);
94   }
95
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   }
102
103   if (l->next)
104     fprintf (stderr,
105         _("WARNING: only one toplevel element is supported at this time."));
106
107   pipeline = GST_ELEMENT (l->data);
108
109   while ((arg = argv[++i])) {
110     element = g_strdup (arg);
111     property = strchr (element, '.');
112     value = strchr (element, '=');
113
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     }
121
122     *property++ = '\0';
123     *value++ = '\0';
124
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   }
133
134   if (!l)
135     return NULL;
136   else
137     return l->data;
138 }
139 #endif
140
141 #ifndef DISABLE_FAULT_HANDLER
142 #ifndef USE_SIGINFO
143 static void
144 fault_handler_sighandler (int signum)
145 {
146   fault_restore ();
147
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   }
161
162   fault_spin ();
163 }
164
165 #else /* USE_SIGINFO */
166
167 static void
168 fault_handler_sigaction (int signum, siginfo_t * si, void *misc)
169 {
170   fault_restore ();
171
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   }
187
188   fault_spin ();
189 }
190 #endif /* USE_SIGINFO */
191
192 static void
193 fault_spin (void)
194 {
195   int spinning = TRUE;
196
197   glib_on_error_halt = FALSE;
198   g_on_error_stack_trace ("gst-launch");
199
200   wait (NULL);
201
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);
207 }
208
209 static void
210 fault_restore (void)
211 {
212   struct sigaction action;
213
214   memset (&action, 0, sizeof (action));
215   action.sa_handler = SIG_DFL;
216
217   sigaction (SIGSEGV, &action, NULL);
218   sigaction (SIGQUIT, &action, NULL);
219 }
220
221 static void
222 fault_setup (void)
223 {
224   struct sigaction action;
225
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
233
234   sigaction (SIGSEGV, &action, NULL);
235   sigaction (SIGQUIT, &action, NULL);
236 }
237 #endif /* DISABLE_FAULT_HANDLER */
238
239 static void
240 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
241 {
242   gint i, count;
243
244   count = gst_tag_list_get_tag_size (list, tag);
245
246   for (i = 0; i < count; i++) {
247     gchar *str;
248
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     }
256
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     }
262
263     g_free (str);
264   }
265 }
266
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)
271 {
272   g_print ("Caught interrupt -- ");
273
274   sigint_restore ();
275
276   caught_intr = TRUE;
277 }
278
279 static gboolean
280 check_intr (GstElement * pipeline)
281 {
282   if (!caught_intr) {
283     return TRUE;
284   } else {
285     GstBus *bus;
286     GstMessage *message;
287
288     caught_intr = FALSE;
289     g_print ("Pausing pipeline.\n");
290
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);
295
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");
299
300     gst_object_unref (bus);
301
302     return FALSE;
303   }
304 }
305
306 static void
307 sigint_setup (void)
308 {
309   struct sigaction action;
310
311   memset (&action, 0, sizeof (action));
312   action.sa_handler = sigint_handler_sighandler;
313
314   sigaction (SIGINT, &action, NULL);
315 }
316
317 static void
318 sigint_restore (void)
319 {
320   struct sigaction action;
321
322   memset (&action, 0, sizeof (action));
323   action.sa_handler = SIG_DFL;
324
325   sigaction (SIGINT, &action, NULL);
326 }
327
328 static void
329 play_handler (int signum)
330 {
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   }
341 }
342
343 static void
344 play_signal_setup (void)
345 {
346   struct sigaction action;
347
348   memset (&action, 0, sizeof (action));
349   action.sa_handler = play_handler;
350   sigaction (SIGUSR1, &action, NULL);
351   sigaction (SIGUSR2, &action, NULL);
352 }
353 #endif /* DISABLE_FAULT_HANDLER */
354
355 static gboolean
356 event_loop (GstElement * pipeline, gboolean blocking)
357 {
358   GstBus *bus;
359   GstMessageType revent;
360   GstMessage *message = NULL;
361
362   bus = gst_element_get_bus (GST_ELEMENT (pipeline));
363
364   g_timeout_add (50, (GSourceFunc) check_intr, pipeline);
365
366   while (TRUE) {
367     revent = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
368
369     /* if the poll timed out, only when !blocking */
370     if (revent == GST_MESSAGE_UNKNOWN) {
371       gst_object_unref (bus);
372       return FALSE;
373     }
374
375     message = gst_bus_pop (bus);
376     g_return_val_if_fail (message != NULL, TRUE);
377
378     if (messages) {
379       const GstStructure *s;
380
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))));
384       if (s) {
385         gchar *sstr;
386
387         sstr = gst_structure_to_string (s);
388         g_print ("%s\n", sstr);
389         g_free (sstr);
390       }
391     }
392
393     switch (revent) {
394       case GST_MESSAGE_EOS:
395         g_print (_
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);
400         return FALSE;
401       case GST_MESSAGE_TAG:
402         if (tags) {
403           GstTagList *tags;
404
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);
410         }
411         gst_message_unref (message);
412         break;
413       case GST_MESSAGE_WARNING:{
414         GError *gerror;
415         gchar *debug;
416
417         gst_message_parse_warning (message, &gerror, &debug);
418         if (debug) {
419           g_print ("WARNING: Element \"%s\" warns: %s\n",
420               GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
421               debug);
422         }
423         gst_message_unref (message);
424         if (gerror)
425           g_error_free (gerror);
426         g_free (debug);
427         break;
428       }
429       case GST_MESSAGE_ERROR:{
430         GError *gerror;
431         gchar *debug;
432
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)),
436               gerror, debug);
437         gst_message_unref (message);
438         if (gerror)
439           g_error_free (gerror);
440         g_free (debug);
441         gst_object_unref (bus);
442         return TRUE;
443       }
444       case GST_MESSAGE_STATE_CHANGED:{
445         GstElementState old, new;
446
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);
451           break;
452         }
453         g_print (_
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);
459         return FALSE;
460       }
461       default:
462         /* just be quiet by default */
463         gst_message_unref (message);
464         break;
465     }
466   }
467
468   g_assert_not_reached ();
469   return TRUE;
470 }
471
472 int
473 main (int argc, char *argv[])
474 {
475   gint i, j;
476
477   /* options */
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")},
495 #endif
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},
502     POPT_TABLEEND
503   };
504
505   gchar **argvn;
506   GError *error = NULL;
507   gint res = 0;
508
509   free (malloc (8));            /* -lefence */
510
511 #ifdef GETTEXT_PACKAGE
512   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
513   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
514   textdomain (GETTEXT_PACKAGE);
515 #endif
516
517   gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
518
519   gst_init_with_popt_table (&argc, &argv, options);
520
521   /* FIXpopt: strip short args, too. We do it ourselves for now */
522   j = 1;
523   for (i = 1; i < argc; i++) {
524     if (*(argv[i]) == '-') {
525       if (strlen (argv[i]) == 2) {
526         gchar *c = argv[i];
527
528         c++;
529         if (*c == 'X' || *c == 'o') {
530           i++;
531         }
532       }
533     } else {
534       argv[j] = argv[i];
535       j++;
536     }
537   }
538   argc = j;
539
540 #ifndef DISABLE_FAULT_HANDLER
541   if (!no_fault)
542     fault_setup ();
543
544   sigint_setup ();
545   play_signal_setup ();
546 #endif
547
548   if (trace) {
549     if (!gst_alloc_trace_available ()) {
550       g_warning ("Trace not available (recompile with trace enabled).");
551     }
552     gst_alloc_trace_print_live ();
553   }
554
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);
561   } else
562 #endif
563   {
564     pipeline =
565         (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
566   }
567   g_free (argvn);
568
569   if (!pipeline) {
570     if (error) {
571       fprintf (stderr, _("ERROR: pipeline could not be constructed: %s.\n"),
572           error->message);
573       g_error_free (error);
574     } else {
575       fprintf (stderr, _("ERROR: pipeline could not be constructed.\n"));
576     }
577     return 1;
578   } else if (error) {
579     fprintf (stderr, _("WARNING: erroneous pipeline: %s\n"), error->message);
580     fprintf (stderr, _("         Trying to run anyway.\n"));
581     g_error_free (error);
582   }
583
584   if (verbose) {
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);
589   }
590 #ifndef GST_DISABLE_LOADSAVE
591   if (savefile) {
592     gst_xml_write_file (GST_ELEMENT (pipeline), fopen (savefile, "w"));
593   }
594 #endif
595
596   if (!savefile) {
597     GstElementState state, pending;
598     GstElementStateReturn ret;
599
600     if (!GST_IS_BIN (pipeline)) {
601       GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
602
603       if (real_pipeline == NULL) {
604         fprintf (stderr, _("ERROR: the 'pipeline' element wasn't found.\n"));
605         return 1;
606       }
607       gst_bin_add (GST_BIN (real_pipeline), pipeline);
608       pipeline = real_pipeline;
609     }
610
611     fprintf (stderr, _("PAUSE pipeline ...\n"));
612     ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
613
614     switch (ret) {
615       case GST_STATE_FAILURE:
616         fprintf (stderr, _("ERROR: pipeline doesn't want to pause.\n"));
617         res = -1;
618         goto end;
619       case GST_STATE_NO_PREROLL:
620         fprintf (stderr, _("NO_PREROLL pipeline ...\n"));
621         break;
622       case GST_STATE_ASYNC:
623         fprintf (stderr, _("PREROLL pipeline ...\n"));
624         gst_element_get_state (pipeline, &state, &pending, NULL);
625         /* fallthrough */
626       case GST_STATE_SUCCESS:
627         fprintf (stderr, _("PREROLLED pipeline ...\n"));
628         break;
629     }
630
631     caught_error = event_loop (pipeline, FALSE);
632
633     if (caught_error) {
634       fprintf (stderr, _("ERROR: pipeline doesn't want to preroll.\n"));
635     } else {
636       GTimeVal tfthen, tfnow;
637       GstClockTimeDiff diff;
638
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"));
643         res = -1;
644         goto end;
645       }
646
647       g_get_current_time (&tfthen);
648       caught_error = event_loop (pipeline, TRUE);
649       g_get_current_time (&tfnow);
650
651       diff = GST_TIMEVAL_TO_TIME (tfnow) - GST_TIMEVAL_TO_TIME (tfthen);
652
653       g_print (_("Execution ended after %" G_GUINT64_FORMAT " ns.\n"), diff);
654     }
655     while (g_main_context_iteration (NULL, FALSE));
656
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);
663
664   end:
665     fprintf (stderr, _("NULL pipeline ...\n"));
666     gst_element_set_state (pipeline, GST_STATE_NULL);
667     gst_element_get_state (pipeline, &state, &pending, NULL);
668   }
669
670   fprintf (stderr, _("FREEING pipeline ...\n"));
671   gst_object_unref (pipeline);
672
673   gst_deinit ();
674   if (trace)
675     gst_alloc_trace_print_live ();
676
677   return res;
678 }