tools: call g_set_prgname() before doing the option parsing
[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 HAVE_WIN32
30 #define DISABLE_FAULT_HANDLER
31 #endif
32
33 #include <stdio.h>
34 #include <string.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 "tools.h"
44
45 /* FIXME: This is just a temporary hack.  We should have a better
46  * check for siginfo handling. */
47 #ifdef SA_SIGINFO
48 #define USE_SIGINFO
49 #endif
50
51 extern volatile gboolean glib_on_error_halt;
52
53 #ifndef DISABLE_FAULT_HANDLER
54 static void fault_restore (void);
55 static void fault_spin (void);
56 static void sigint_restore (void);
57 static gboolean caught_intr = FALSE;
58 #endif
59
60 /* event_loop return codes */
61 typedef enum _EventLoopResult
62 {
63   ELR_NO_ERROR = 0,
64   ELR_ERROR,
65   ELR_INTERRUPT
66 } EventLoopResult;
67
68 static GstElement *pipeline;
69 static EventLoopResult caught_error = ELR_NO_ERROR;
70 static gboolean quiet = FALSE;
71 static gboolean tags = FALSE;
72 static gboolean messages = FALSE;
73 static gboolean is_live = FALSE;
74 static gboolean waiting_eos = FALSE;
75
76 /* convenience macro so we don't have to litter the code with if(!quiet) */
77 #define PRINT if(!quiet)g_print
78
79 #ifndef GST_DISABLE_LOADSAVE
80 static GstElement *
81 xmllaunch_parse_cmdline (const gchar ** argv)
82 {
83   GstElement *pipeline = NULL, *e;
84   GstXML *xml;
85   gboolean err;
86   const gchar *arg;
87   gchar *element, *property, *value;
88   GList *l;
89   gint i = 0;
90
91   if (!(arg = argv[0])) {
92     g_print (_
93         ("Usage: gst-xmllaunch <file.xml> [ element.property=value ... ]\n"));
94     exit (1);
95   }
96
97   xml = gst_xml_new ();
98   /* FIXME guchar from gstxml.c */
99   err = gst_xml_parse_file (xml, (guchar *) arg, NULL);
100
101   if (err != TRUE) {
102     g_printerr (_("ERROR: parse of xml file '%s' failed.\n"), arg);
103     exit (1);
104   }
105
106   l = gst_xml_get_topelements (xml);
107   if (!l) {
108     g_printerr (_("ERROR: no toplevel pipeline element in file '%s'.\n"), arg);
109     exit (1);
110   }
111
112   if (l->next) {
113     g_printerr ("%s",
114         _("WARNING: only one toplevel element is supported at this time.\n"));
115   }
116
117   pipeline = GST_ELEMENT (l->data);
118
119   while ((arg = argv[++i])) {
120     element = g_strdup (arg);
121     property = strchr (element, '.');
122     value = strchr (element, '=');
123
124     if (!(element < property && property < value)) {
125       g_printerr (_("ERROR: could not parse command line argument %d: %s.\n"),
126           i, element);
127       g_free (element);
128       exit (1);
129     }
130
131     *property++ = '\0';
132     *value++ = '\0';
133
134     e = gst_bin_get_by_name (GST_BIN (pipeline), element);
135     if (!e) {
136       g_printerr (_("WARNING: element named '%s' not found.\n"), element);
137     } else {
138       gst_util_set_object_arg (G_OBJECT (e), property, value);
139     }
140     g_free (element);
141   }
142
143   if (!l)
144     return NULL;
145
146   gst_object_ref (pipeline);
147   gst_object_unref (xml);
148   return pipeline;
149 }
150 #endif
151
152 #ifndef DISABLE_FAULT_HANDLER
153 #ifndef USE_SIGINFO
154 static void
155 fault_handler_sighandler (int signum)
156 {
157   fault_restore ();
158
159   /* printf is used instead of g_print(), since it's less likely to
160    * deadlock */
161   switch (signum) {
162     case SIGSEGV:
163       printf ("Caught SIGSEGV\n");
164       break;
165     case SIGQUIT:
166       printf ("Caught SIGQUIT\n");
167       break;
168     default:
169       printf ("signo:  %d\n", signum);
170       break;
171   }
172
173   fault_spin ();
174 }
175
176 #else /* USE_SIGINFO */
177
178 static void
179 fault_handler_sigaction (int signum, siginfo_t * si, void *misc)
180 {
181   fault_restore ();
182
183   /* printf is used instead of g_print(), since it's less likely to
184    * deadlock */
185   switch (si->si_signo) {
186     case SIGSEGV:
187       printf ("Caught SIGSEGV accessing address %p\n", si->si_addr);
188       break;
189     case SIGQUIT:
190       printf ("Caught SIGQUIT\n");
191       break;
192     default:
193       printf ("signo:  %d\n", si->si_signo);
194       printf ("errno:  %d\n", si->si_errno);
195       printf ("code:   %d\n", si->si_code);
196       break;
197   }
198
199   fault_spin ();
200 }
201 #endif /* USE_SIGINFO */
202
203 static void
204 fault_spin (void)
205 {
206   int spinning = TRUE;
207
208   glib_on_error_halt = FALSE;
209   g_on_error_stack_trace ("gst-launch");
210
211   wait (NULL);
212
213   /* FIXME how do we know if we were run by libtool? */
214   printf ("Spinning.  Please run 'gdb gst-launch %d' to continue debugging, "
215       "Ctrl-C to quit, or Ctrl-\\ to dump core.\n", (gint) getpid ());
216   while (spinning)
217     g_usleep (1000000);
218 }
219
220 static void
221 fault_restore (void)
222 {
223   struct sigaction action;
224
225   memset (&action, 0, sizeof (action));
226   action.sa_handler = SIG_DFL;
227
228   sigaction (SIGSEGV, &action, NULL);
229   sigaction (SIGQUIT, &action, NULL);
230 }
231
232 static void
233 fault_setup (void)
234 {
235   struct sigaction action;
236
237   memset (&action, 0, sizeof (action));
238 #ifdef USE_SIGINFO
239   action.sa_sigaction = fault_handler_sigaction;
240   action.sa_flags = SA_SIGINFO;
241 #else
242   action.sa_handler = fault_handler_sighandler;
243 #endif
244
245   sigaction (SIGSEGV, &action, NULL);
246   sigaction (SIGQUIT, &action, NULL);
247 }
248 #endif /* DISABLE_FAULT_HANDLER */
249
250 static void
251 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
252 {
253   gint i, count;
254
255   count = gst_tag_list_get_tag_size (list, tag);
256
257   for (i = 0; i < count; i++) {
258     gchar *str;
259
260     if (gst_tag_get_type (tag) == G_TYPE_STRING) {
261       if (!gst_tag_list_get_string_index (list, tag, i, &str))
262         g_assert_not_reached ();
263     } else if (gst_tag_get_type (tag) == GST_TYPE_BUFFER) {
264       GstBuffer *img;
265
266       img = gst_value_get_buffer (gst_tag_list_get_value_index (list, tag, i));
267       if (img) {
268         gchar *caps_str;
269
270         caps_str = GST_BUFFER_CAPS (img) ?
271             gst_caps_to_string (GST_BUFFER_CAPS (img)) : g_strdup ("unknown");
272         str = g_strdup_printf ("buffer of %u bytes, type: %s",
273             GST_BUFFER_SIZE (img), caps_str);
274         g_free (caps_str);
275       } else {
276         str = g_strdup ("NULL buffer");
277       }
278     } else {
279       str =
280           g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
281     }
282
283     if (i == 0) {
284       g_print ("%16s: %s\n", gst_tag_get_nick (tag), str);
285     } else {
286       g_print ("%16s: %s\n", "", str);
287     }
288
289     g_free (str);
290   }
291 }
292
293 #ifndef DISABLE_FAULT_HANDLER
294 /* we only use sighandler here because the registers are not important */
295 static void
296 sigint_handler_sighandler (int signum)
297 {
298   g_print ("Caught interrupt -- ");
299
300   /* If we were waiting for an EOS, we still want to catch
301    * the next signal to shutdown properly (and the following one
302    * will quit the program). */
303   if (waiting_eos) {
304     waiting_eos = FALSE;
305   } else {
306     sigint_restore ();
307   }
308   /* we set a flag that is checked by the mainloop, we cannot do much in the
309    * interrupt handler (no mutex or other blocking stuff) */
310   caught_intr = TRUE;
311 }
312
313 /* is called every 250 milliseconds (4 times a second), the interrupt handler
314  * will set a flag for us. We react to this by posting a message. */
315 static gboolean
316 check_intr (GstElement * pipeline)
317 {
318   if (!caught_intr) {
319     return TRUE;
320   } else {
321     caught_intr = FALSE;
322     g_print ("handling interrupt.\n");
323
324     /* post an application specific message */
325     gst_element_post_message (GST_ELEMENT (pipeline),
326         gst_message_new_application (GST_OBJECT (pipeline),
327             gst_structure_new ("GstLaunchInterrupt",
328                 "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));
329
330     /* remove timeout handler */
331     return FALSE;
332   }
333 }
334
335 static void
336 sigint_setup (void)
337 {
338   struct sigaction action;
339
340   memset (&action, 0, sizeof (action));
341   action.sa_handler = sigint_handler_sighandler;
342
343   sigaction (SIGINT, &action, NULL);
344 }
345
346 static void
347 sigint_restore (void)
348 {
349   struct sigaction action;
350
351   memset (&action, 0, sizeof (action));
352   action.sa_handler = SIG_DFL;
353
354   sigaction (SIGINT, &action, NULL);
355 }
356
357 static void
358 play_handler (int signum)
359 {
360   switch (signum) {
361     case SIGUSR1:
362       g_print ("Caught SIGUSR1 - Play request.\n");
363       gst_element_set_state (pipeline, GST_STATE_PLAYING);
364       break;
365     case SIGUSR2:
366       g_print ("Caught SIGUSR2 - Stop request.\n");
367       gst_element_set_state (pipeline, GST_STATE_NULL);
368       break;
369   }
370 }
371
372 static void
373 play_signal_setup (void)
374 {
375   struct sigaction action;
376
377   memset (&action, 0, sizeof (action));
378   action.sa_handler = play_handler;
379   sigaction (SIGUSR1, &action, NULL);
380   sigaction (SIGUSR2, &action, NULL);
381 }
382 #endif /* DISABLE_FAULT_HANDLER */
383
384 /* returns ELR_ERROR if there was an error
385  * or ELR_INTERRUPT if we caught a keyboard interrupt
386  * or ELR_NO_ERROR otherwise. */
387 static EventLoopResult
388 event_loop (GstElement * pipeline, gboolean blocking, GstState target_state)
389 {
390 #ifndef DISABLE_FAULT_HANDLER
391   gulong timeout_id;
392 #endif
393   GstBus *bus;
394   GstMessage *message = NULL;
395   EventLoopResult res = ELR_NO_ERROR;
396   gboolean buffering = FALSE;
397
398   bus = gst_element_get_bus (GST_ELEMENT (pipeline));
399
400 #ifndef DISABLE_FAULT_HANDLER
401   timeout_id = g_timeout_add (250, (GSourceFunc) check_intr, pipeline);
402 #endif
403
404   while (TRUE) {
405     message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
406
407     /* if the poll timed out, only when !blocking */
408     if (message == NULL)
409       goto exit;
410
411     /* check if we need to dump messages to the console */
412     if (messages) {
413       GstObject *src_obj;
414       const GstStructure *s;
415       guint32 seqnum;
416
417       seqnum = gst_message_get_seqnum (message);
418
419       s = gst_message_get_structure (message);
420
421       src_obj = GST_MESSAGE_SRC (message);
422
423       if (GST_IS_ELEMENT (src_obj)) {
424         g_print (_("Got message #%u from element \"%s\" (%s): "),
425             (guint) seqnum, GST_ELEMENT_NAME (src_obj),
426             GST_MESSAGE_TYPE_NAME (message));
427       } else if (GST_IS_PAD (src_obj)) {
428         g_print (_("Got message #%u from pad \"%s:%s\" (%s): "),
429             (guint) seqnum, GST_DEBUG_PAD_NAME (src_obj),
430             GST_MESSAGE_TYPE_NAME (message));
431       } else if (GST_IS_OBJECT (src_obj)) {
432         g_print (_("Got message #%u from object \"%s\" (%s): "),
433             (guint) seqnum, GST_OBJECT_NAME (src_obj),
434             GST_MESSAGE_TYPE_NAME (message));
435       } else {
436         g_print (_("Got message #%u (%s): "), (guint) seqnum,
437             GST_MESSAGE_TYPE_NAME (message));
438       }
439
440       if (s) {
441         gchar *sstr;
442
443         sstr = gst_structure_to_string (s);
444         g_print ("%s\n", sstr);
445         g_free (sstr);
446       } else {
447         g_print ("no message details\n");
448       }
449     }
450
451     switch (GST_MESSAGE_TYPE (message)) {
452       case GST_MESSAGE_NEW_CLOCK:
453       {
454         GstClock *clock;
455
456         gst_message_parse_new_clock (message, &clock);
457
458         g_print ("New clock: %s\n", (clock ? GST_OBJECT_NAME (clock) : "NULL"));
459         break;
460       }
461       case GST_MESSAGE_CLOCK_LOST:
462 #if 0
463         /* disabled for now as it caused problems with rtspsrc. We need to fix
464          * rtspsrc first, then release -good before we can reenable this again
465          */
466         g_print ("Clock lost, selecting a new one\n");
467         gst_element_set_state (pipeline, GST_STATE_PAUSED);
468         gst_element_set_state (pipeline, GST_STATE_PLAYING);
469 #endif
470         break;
471       case GST_MESSAGE_EOS:{
472         waiting_eos = FALSE;
473         g_print (_("Got EOS from element \"%s\".\n"),
474             GST_MESSAGE_SRC_NAME (message));
475         goto exit;
476       }
477       case GST_MESSAGE_TAG:
478         if (tags) {
479           GstTagList *tags;
480
481           if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
482             g_print (_("FOUND TAG      : found by element \"%s\".\n"),
483                 GST_MESSAGE_SRC_NAME (message));
484           } else if (GST_IS_PAD (GST_MESSAGE_SRC (message))) {
485             g_print (_("FOUND TAG      : found by pad \"%s:%s\".\n"),
486                 GST_DEBUG_PAD_NAME (GST_MESSAGE_SRC (message)));
487           } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
488             g_print (_("FOUND TAG      : found by object \"%s\".\n"),
489                 GST_MESSAGE_SRC_NAME (message));
490           } else {
491             g_print (_("FOUND TAG\n"));
492           }
493
494           gst_message_parse_tag (message, &tags);
495           gst_tag_list_foreach (tags, print_tag, NULL);
496           gst_tag_list_free (tags);
497         }
498         break;
499       case GST_MESSAGE_INFO:{
500         GError *gerror;
501         gchar *debug;
502         gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
503
504         gst_message_parse_info (message, &gerror, &debug);
505         if (debug) {
506           g_print (_("INFO:\n%s\n"), debug);
507         }
508         g_error_free (gerror);
509         g_free (debug);
510         g_free (name);
511         break;
512       }
513       case GST_MESSAGE_WARNING:{
514         GError *gerror;
515         gchar *debug;
516         gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
517
518         /* dump graph on warning */
519         GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
520             GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.warning");
521
522         gst_message_parse_warning (message, &gerror, &debug);
523         g_print (_("WARNING: from element %s: %s\n"), name, gerror->message);
524         if (debug) {
525           g_print (_("Additional debug info:\n%s\n"), debug);
526         }
527         g_error_free (gerror);
528         g_free (debug);
529         g_free (name);
530         break;
531       }
532       case GST_MESSAGE_ERROR:{
533         GError *gerror;
534         gchar *debug;
535
536         /* dump graph on error */
537         GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
538             GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.error");
539
540         gst_message_parse_error (message, &gerror, &debug);
541         gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
542         g_error_free (gerror);
543         g_free (debug);
544         /* we have an error */
545         res = ELR_ERROR;
546         goto exit;
547       }
548       case GST_MESSAGE_STATE_CHANGED:{
549         GstState old, new, pending;
550
551         gst_message_parse_state_changed (message, &old, &new, &pending);
552
553         /* we only care about pipeline state change messages */
554         if (GST_MESSAGE_SRC (message) != GST_OBJECT_CAST (pipeline))
555           break;
556
557         /* dump graph for pipeline state changes */
558         {
559           gchar *dump_name = g_strdup_printf ("gst-launch.%s_%s",
560               gst_element_state_get_name (old),
561               gst_element_state_get_name (new));
562           GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
563               GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
564           g_free (dump_name);
565         }
566
567         /* ignore when we are buffering since then we mess with the states
568          * ourselves. */
569         if (buffering) {
570           PRINT (_("Prerolled, waiting for buffering to finish...\n"));
571           break;
572         }
573
574         /* if we reached the final target state, exit */
575         if (target_state == GST_STATE_PAUSED && new == target_state)
576           goto exit;
577
578         /* else not an interesting message */
579         break;
580       }
581       case GST_MESSAGE_BUFFERING:{
582         gint percent;
583
584         gst_message_parse_buffering (message, &percent);
585         PRINT ("%s %d%%  \r", _("buffering..."), percent);
586
587         /* no state management needed for live pipelines */
588         if (is_live)
589           break;
590
591         if (percent == 100) {
592           /* a 100% message means buffering is done */
593           buffering = FALSE;
594           /* if the desired state is playing, go back */
595           if (target_state == GST_STATE_PLAYING) {
596             PRINT (_("Done buffering, setting pipeline to PLAYING ...\n"));
597             gst_element_set_state (pipeline, GST_STATE_PLAYING);
598           } else
599             goto exit;
600         } else {
601           /* buffering busy */
602           if (buffering == FALSE && target_state == GST_STATE_PLAYING) {
603             /* we were not buffering but PLAYING, PAUSE  the pipeline. */
604             PRINT (_("Buffering, setting pipeline to PAUSED ...\n"));
605             gst_element_set_state (pipeline, GST_STATE_PAUSED);
606           }
607           buffering = TRUE;
608         }
609         break;
610       }
611       case GST_MESSAGE_LATENCY:
612       {
613         PRINT (_("Redistribute latency...\n"));
614         gst_bin_recalculate_latency (GST_BIN (pipeline));
615         break;
616       }
617       case GST_MESSAGE_REQUEST_STATE:
618       {
619         GstState state;
620         gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
621
622         gst_message_parse_request_state (message, &state);
623
624         PRINT (_("Setting state to %s as requested by %s...\n"),
625             gst_element_state_get_name (state), name);
626
627         gst_element_set_state (pipeline, state);
628
629         g_free (name);
630         break;
631       }
632       case GST_MESSAGE_APPLICATION:{
633         const GstStructure *s;
634
635         s = gst_message_get_structure (message);
636
637         if (gst_structure_has_name (s, "GstLaunchInterrupt")) {
638           /* this application message is posted when we caught an interrupt and
639            * we need to stop the pipeline. */
640           PRINT (_("Interrupt: Stopping pipeline ...\n"));
641           res = ELR_INTERRUPT;
642           goto exit;
643         }
644       }
645       default:
646         /* just be quiet by default */
647         break;
648     }
649     if (message)
650       gst_message_unref (message);
651   }
652   g_assert_not_reached ();
653
654 exit:
655   {
656     if (message)
657       gst_message_unref (message);
658     gst_object_unref (bus);
659 #ifndef DISABLE_FAULT_HANDLER
660     g_source_remove (timeout_id);
661 #endif
662     return res;
663   }
664 }
665
666 int
667 main (int argc, char *argv[])
668 {
669   /* options */
670   gboolean verbose = FALSE;
671   gboolean no_fault = FALSE;
672   gboolean trace = FALSE;
673   gboolean eos_on_shutdown = FALSE;
674   gchar *savefile = NULL;
675   gchar *exclude_args = NULL;
676 #ifndef GST_DISABLE_OPTION_PARSING
677   GOptionEntry options[] = {
678     {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
679         N_("Output tags (also known as metadata)"), NULL},
680     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
681         N_("Output status information and property notifications"), NULL},
682     {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
683         N_("Do not print any progress information"), NULL},
684     {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
685         N_("Output messages"), NULL},
686     {"exclude", 'X', 0, G_OPTION_ARG_NONE, &exclude_args,
687         N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
688 #ifndef GST_DISABLE_LOADSAVE
689     {"output", 'o', 0, G_OPTION_ARG_STRING, &savefile,
690         N_("Save xml representation of pipeline to FILE and exit"), N_("FILE")},
691 #endif
692     {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
693         N_("Do not install a fault handler"), NULL},
694     {"trace", 'T', 0, G_OPTION_ARG_NONE, &trace,
695         N_("Print alloc trace (if enabled at compile time)"), NULL},
696     {"eos-on-shutdown", 'e', 0, G_OPTION_ARG_NONE, &eos_on_shutdown,
697         N_("Force EOS on sources before shutting the pipeline down"), NULL},
698     GST_TOOLS_GOPTION_VERSION,
699     {NULL}
700   };
701   GOptionContext *ctx;
702   GError *err = NULL;
703 #endif
704   gchar **argvn;
705   GError *error = NULL;
706   gint res = 0;
707
708   free (malloc (8));            /* -lefence */
709
710 #ifdef ENABLE_NLS
711   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
712   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
713   textdomain (GETTEXT_PACKAGE);
714 #endif
715
716   g_thread_init (NULL);
717
718   gst_tools_set_prgname ("gst-launch");
719
720 #ifndef GST_DISABLE_OPTION_PARSING
721   ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
722   g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
723   g_option_context_add_group (ctx, gst_init_get_option_group ());
724   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
725     if (err)
726       g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
727     else
728       g_print ("Error initializing: Unknown error!\n");
729     exit (1);
730   }
731   g_option_context_free (ctx);
732 #else
733   gst_init (&argc, &argv);
734 #endif
735
736   gst_tools_print_version ("gst-launch");
737
738 #ifndef DISABLE_FAULT_HANDLER
739   if (!no_fault)
740     fault_setup ();
741
742   sigint_setup ();
743   play_signal_setup ();
744 #endif
745
746   if (trace) {
747     if (!gst_alloc_trace_available ()) {
748       g_warning ("Trace not available (recompile with trace enabled).");
749     }
750     gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE |
751         GST_ALLOC_TRACE_MEM_LIVE);
752     gst_alloc_trace_print_live ();
753   }
754
755   /* make a null-terminated version of argv */
756   argvn = g_new0 (char *, argc);
757   memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
758 #ifndef GST_DISABLE_LOADSAVE
759   if (strstr (argv[0], "gst-xmllaunch")) {
760     pipeline = xmllaunch_parse_cmdline ((const gchar **) argvn);
761   } else
762 #endif
763   {
764     pipeline =
765         (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
766   }
767   g_free (argvn);
768
769   if (!pipeline) {
770     if (error) {
771       g_printerr (_("ERROR: pipeline could not be constructed: %s.\n"),
772           GST_STR_NULL (error->message));
773       g_error_free (error);
774     } else {
775       g_printerr (_("ERROR: pipeline could not be constructed.\n"));
776     }
777     return 1;
778   } else if (error) {
779     g_printerr (_("WARNING: erroneous pipeline: %s\n"),
780         GST_STR_NULL (error->message));
781     g_error_free (error);
782     return 1;
783   }
784
785   if (verbose) {
786     gchar **exclude_list =
787         exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
788     g_signal_connect (pipeline, "deep-notify",
789         G_CALLBACK (gst_object_default_deep_notify), exclude_list);
790   }
791 #ifndef GST_DISABLE_LOADSAVE
792   if (savefile) {
793     gst_xml_write_file (GST_ELEMENT (pipeline), fopen (savefile, "w"));
794   }
795 #endif
796
797   if (!savefile) {
798     GstState state, pending;
799     GstStateChangeReturn ret;
800
801     /* If the top-level object is not a pipeline, place it in a pipeline. */
802     if (!GST_IS_PIPELINE (pipeline)) {
803       GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
804
805       if (real_pipeline == NULL) {
806         g_printerr (_("ERROR: the 'pipeline' element wasn't found.\n"));
807         return 1;
808       }
809       gst_bin_add (GST_BIN (real_pipeline), pipeline);
810       pipeline = real_pipeline;
811     }
812     PRINT (_("Setting pipeline to PAUSED ...\n"));
813     ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
814
815     switch (ret) {
816       case GST_STATE_CHANGE_FAILURE:
817         g_printerr (_("ERROR: Pipeline doesn't want to pause.\n"));
818         res = -1;
819         event_loop (pipeline, FALSE, GST_STATE_VOID_PENDING);
820         goto end;
821       case GST_STATE_CHANGE_NO_PREROLL:
822         PRINT (_("Pipeline is live and does not need PREROLL ...\n"));
823         is_live = TRUE;
824         break;
825       case GST_STATE_CHANGE_ASYNC:
826         PRINT (_("Pipeline is PREROLLING ...\n"));
827         caught_error = event_loop (pipeline, TRUE, GST_STATE_PAUSED);
828         if (caught_error) {
829           g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
830           goto end;
831         }
832         state = GST_STATE_PAUSED;
833         /* fallthrough */
834       case GST_STATE_CHANGE_SUCCESS:
835         PRINT (_("Pipeline is PREROLLED ...\n"));
836         break;
837     }
838
839     caught_error = event_loop (pipeline, FALSE, GST_STATE_PLAYING);
840
841     if (caught_error) {
842       g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
843     } else {
844       GstClockTime tfthen, tfnow;
845       GstClockTimeDiff diff;
846
847       PRINT (_("Setting pipeline to PLAYING ...\n"));
848
849       if (gst_element_set_state (pipeline,
850               GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
851         GstMessage *err_msg;
852         GstBus *bus;
853
854         g_printerr (_("ERROR: pipeline doesn't want to play.\n"));
855         bus = gst_element_get_bus (pipeline);
856         if ((err_msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0))) {
857           GError *gerror;
858           gchar *debug;
859
860           gst_message_parse_error (err_msg, &gerror, &debug);
861           gst_object_default_error (GST_MESSAGE_SRC (err_msg), gerror, debug);
862           gst_message_unref (err_msg);
863           g_error_free (gerror);
864           g_free (debug);
865         }
866         gst_object_unref (bus);
867         res = -1;
868         goto end;
869       }
870
871       tfthen = gst_util_get_timestamp ();
872       caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
873       if (eos_on_shutdown && caught_error == ELR_INTERRUPT) {
874         PRINT (_("EOS on shutdown enabled -- Forcing EOS on the pipeline\n"));
875         waiting_eos = TRUE;
876         gst_element_send_event (pipeline, gst_event_new_eos ());
877         PRINT (_("Waiting for EOS...\n"));
878         caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
879
880         if (caught_error == ELR_NO_ERROR) {
881           /* we got EOS */
882           PRINT (_("EOS received - stopping pipeline...\n"));
883         } else if (caught_error == ELR_ERROR) {
884           PRINT (_("An error happened while waiting for EOS\n"));
885         }
886       }
887       tfnow = gst_util_get_timestamp ();
888
889       diff = GST_CLOCK_DIFF (tfthen, tfnow);
890
891       g_print (_("Execution ended after %" G_GUINT64_FORMAT " ns.\n"), diff);
892     }
893
894     PRINT (_("Setting pipeline to PAUSED ...\n"));
895     gst_element_set_state (pipeline, GST_STATE_PAUSED);
896     if (caught_error == ELR_NO_ERROR)
897       gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
898
899     /* iterate mainloop to process pending stuff */
900     while (g_main_context_iteration (NULL, FALSE));
901
902     PRINT (_("Setting pipeline to READY ...\n"));
903     gst_element_set_state (pipeline, GST_STATE_READY);
904     gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
905
906   end:
907     PRINT (_("Setting pipeline to NULL ...\n"));
908     gst_element_set_state (pipeline, GST_STATE_NULL);
909     gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
910   }
911
912   PRINT (_("Freeing pipeline ...\n"));
913   gst_object_unref (pipeline);
914
915   gst_deinit ();
916   if (trace)
917     gst_alloc_trace_print_live ();
918
919   return res;
920 }