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 */
45 /* FIXME: This is just a temporary hack. We should have a better
46 * check for siginfo handling. */
51 extern volatile gboolean glib_on_error_halt;
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;
60 /* event_loop return codes */
61 typedef enum _EventLoopResult
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;
76 /* convenience macro so we don't have to litter the code with if(!quiet) */
77 #define PRINT if(!quiet)g_print
79 #ifndef DISABLE_FAULT_HANDLER
82 fault_handler_sighandler (int signum)
86 /* printf is used instead of g_print(), since it's less likely to
90 fprintf (stderr, "Caught SIGSEGV\n");
94 printf ("Caught SIGQUIT\n");
97 fprintf (stderr, "signo: %d\n", signum);
104 #else /* USE_SIGINFO */
107 fault_handler_sigaction (int signum, siginfo_t * si, void *misc)
111 /* printf is used instead of g_print(), since it's less likely to
113 switch (si->si_signo) {
115 fprintf (stderr, "Caught SIGSEGV accessing address %p\n", si->si_addr);
119 printf ("Caught SIGQUIT\n");
122 fprintf (stderr, "signo: %d\n", si->si_signo);
123 fprintf (stderr, "errno: %d\n", si->si_errno);
124 fprintf (stderr, "code: %d\n", si->si_code);
130 #endif /* USE_SIGINFO */
137 glib_on_error_halt = FALSE;
138 g_on_error_stack_trace ("gst-launch");
142 /* FIXME how do we know if we were run by libtool? */
144 "Spinning. Please run 'gdb gst-launch %d' to continue debugging, "
145 "Ctrl-C to quit, or Ctrl-\\ to dump core.\n", (gint) getpid ());
153 struct sigaction action;
155 memset (&action, 0, sizeof (action));
156 action.sa_handler = SIG_DFL;
158 sigaction (SIGSEGV, &action, NULL);
159 sigaction (SIGQUIT, &action, NULL);
165 struct sigaction action;
167 memset (&action, 0, sizeof (action));
169 action.sa_sigaction = fault_handler_sigaction;
170 action.sa_flags = SA_SIGINFO;
172 action.sa_handler = fault_handler_sighandler;
175 sigaction (SIGSEGV, &action, NULL);
176 sigaction (SIGQUIT, &action, NULL);
178 #endif /* DISABLE_FAULT_HANDLER */
180 typedef struct _GstIndexStats
188 GstClockTime last_keyframe;
189 GstClockTime last_dltframe;
190 GstClockTime min_keyframe_gap;
191 GstClockTime max_keyframe_gap;
192 GstClockTime avg_keyframe_gap;
196 entry_added (GstIndex * index, GstIndexEntry * entry, gpointer user_data)
198 GPtrArray *index_stats = (GPtrArray *) user_data;
201 switch (entry->type) {
202 case GST_INDEX_ENTRY_ID:
203 /* we have a new writer */
204 GST_DEBUG_OBJECT (index, "id %d: describes writer %s", entry->id,
205 GST_INDEX_ID_DESCRIPTION (entry));
206 if (entry->id >= index_stats->len) {
207 g_ptr_array_set_size (index_stats, entry->id + 1);
209 s = g_new (GstIndexStats, 1);
211 s->desc = g_strdup (GST_INDEX_ID_DESCRIPTION (entry));
212 s->num_frames = s->num_keyframes = s->num_dltframes = 0;
213 s->last_keyframe = s->last_dltframe = GST_CLOCK_TIME_NONE;
214 s->min_keyframe_gap = s->max_keyframe_gap = s->avg_keyframe_gap =
216 g_ptr_array_index (index_stats, entry->id) = s;
218 case GST_INDEX_ENTRY_FORMAT:
219 /* have not found any code calling this */
220 GST_DEBUG_OBJECT (index, "id %d: registered format %d for %s\n",
221 entry->id, GST_INDEX_FORMAT_FORMAT (entry),
222 GST_INDEX_FORMAT_KEY (entry));
224 case GST_INDEX_ENTRY_ASSOCIATION:
227 GstAssocFlags flags = GST_INDEX_ASSOC_FLAGS (entry);
229 s = g_ptr_array_index (index_stats, entry->id);
230 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
232 if (flags & GST_ASSOCIATION_FLAG_KEY_UNIT) {
235 if (GST_CLOCK_TIME_IS_VALID (ts)) {
236 if (GST_CLOCK_TIME_IS_VALID (s->last_keyframe)) {
237 GstClockTimeDiff d = GST_CLOCK_DIFF (s->last_keyframe, ts);
239 if (G_UNLIKELY (d < 0)) {
240 GST_WARNING ("received out-of-order keyframe at %"
241 GST_TIME_FORMAT, GST_TIME_ARGS (ts));
242 /* FIXME: does it still make sense to use that for the statistics */
243 d = GST_CLOCK_DIFF (ts, s->last_keyframe);
246 if (GST_CLOCK_TIME_IS_VALID (s->min_keyframe_gap)) {
247 if (d < s->min_keyframe_gap)
248 s->min_keyframe_gap = d;
250 s->min_keyframe_gap = d;
252 if (GST_CLOCK_TIME_IS_VALID (s->max_keyframe_gap)) {
253 if (d > s->max_keyframe_gap)
254 s->max_keyframe_gap = d;
256 s->max_keyframe_gap = d;
258 if (GST_CLOCK_TIME_IS_VALID (s->avg_keyframe_gap)) {
259 s->avg_keyframe_gap = (d + s->num_frames * s->avg_keyframe_gap) /
262 s->avg_keyframe_gap = d;
265 s->last_keyframe = ts;
268 if (flags & GST_ASSOCIATION_FLAG_DELTA_UNIT) {
270 if (GST_CLOCK_TIME_IS_VALID (ts)) {
271 s->last_dltframe = ts;
283 /* print statistics from the entry_added callback, free the entries */
285 print_index_stats (GPtrArray * index_stats)
289 if (index_stats->len) {
290 g_print ("%s:\n", _("Index statistics"));
293 for (i = 0; i < index_stats->len; i++) {
294 GstIndexStats *s = g_ptr_array_index (index_stats, i);
296 g_print ("id %d, %s\n", s->id, s->desc);
298 GstClockTime last_frame = s->last_keyframe;
300 if (GST_CLOCK_TIME_IS_VALID (s->last_dltframe)) {
301 if (!GST_CLOCK_TIME_IS_VALID (last_frame) ||
302 (s->last_dltframe > last_frame))
303 last_frame = s->last_dltframe;
306 if (GST_CLOCK_TIME_IS_VALID (last_frame)) {
307 g_print (" total time = %" GST_TIME_FORMAT "\n",
308 GST_TIME_ARGS (last_frame));
310 g_print (" frame/keyframe rate = %u / %u = ", s->num_frames,
312 if (s->num_keyframes)
313 g_print ("%lf\n", s->num_frames / (gdouble) s->num_keyframes);
316 if (s->num_keyframes) {
317 g_print (" min/avg/max keyframe gap = %" GST_TIME_FORMAT ", %"
318 GST_TIME_FORMAT ", %" GST_TIME_FORMAT "\n",
319 GST_TIME_ARGS (s->min_keyframe_gap),
320 GST_TIME_ARGS (s->avg_keyframe_gap),
321 GST_TIME_ARGS (s->max_keyframe_gap));
324 g_print (" no stats\n");
334 print_error_message (GstMessage * msg)
337 gchar *name, *debug = NULL;
339 name = gst_object_get_path_string (msg->src);
340 gst_message_parse_error (msg, &err, &debug);
342 g_printerr (_("ERROR: from element %s: %s\n"), name, err->message);
344 g_printerr (_("Additional debug info:\n%s\n"), debug);
352 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
356 count = gst_tag_list_get_tag_size (list, tag);
358 for (i = 0; i < count; i++) {
361 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
362 if (!gst_tag_list_get_string_index (list, tag, i, &str))
363 g_assert_not_reached ();
364 } else if (gst_tag_get_type (tag) == GST_TYPE_BUFFER) {
367 img = gst_value_get_buffer (gst_tag_list_get_value_index (list, tag, i));
371 caps_str = GST_BUFFER_CAPS (img) ?
372 gst_caps_to_string (GST_BUFFER_CAPS (img)) : g_strdup ("unknown");
373 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes, type: %s",
374 gst_buffer_get_size (img), caps_str);
377 str = g_strdup ("NULL buffer");
379 } else if (gst_tag_get_type (tag) == GST_TYPE_DATE_TIME) {
380 GstDateTime *dt = NULL;
382 gst_tag_list_get_date_time_index (list, tag, i, &dt);
383 if (gst_date_time_get_hour (dt) < 0) {
384 str = g_strdup_printf ("%02u-%02u-%04u", gst_date_time_get_day (dt),
385 gst_date_time_get_month (dt), gst_date_time_get_year (dt));
387 gdouble tz_offset = gst_date_time_get_time_zone_offset (dt);
390 if (tz_offset != 0.0) {
391 g_snprintf (tz_str, sizeof (tz_str), "(UTC %s%gh)",
392 (tz_offset > 0.0) ? "+" : "", tz_offset);
394 g_snprintf (tz_str, sizeof (tz_str), "(UTC)");
397 str = g_strdup_printf ("%04u-%02u-%02u %02u:%02u:%02u %s",
398 gst_date_time_get_year (dt), gst_date_time_get_month (dt),
399 gst_date_time_get_day (dt), gst_date_time_get_hour (dt),
400 gst_date_time_get_minute (dt), gst_date_time_get_second (dt),
403 gst_date_time_unref (dt);
406 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
410 PRINT ("%16s: %s\n", gst_tag_get_nick (tag), str);
412 PRINT ("%16s: %s\n", "", str);
419 #ifndef DISABLE_FAULT_HANDLER
420 /* we only use sighandler here because the registers are not important */
422 sigint_handler_sighandler (int signum)
424 PRINT ("Caught interrupt -- ");
426 /* If we were waiting for an EOS, we still want to catch
427 * the next signal to shutdown properly (and the following one
428 * will quit the program). */
434 /* we set a flag that is checked by the mainloop, we cannot do much in the
435 * interrupt handler (no mutex or other blocking stuff) */
439 /* is called every 250 milliseconds (4 times a second), the interrupt handler
440 * will set a flag for us. We react to this by posting a message. */
442 check_intr (GstElement * pipeline)
448 PRINT ("handling interrupt.\n");
450 /* post an application specific message */
451 gst_element_post_message (GST_ELEMENT (pipeline),
452 gst_message_new_application (GST_OBJECT (pipeline),
453 gst_structure_new ("GstLaunchInterrupt",
454 "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));
456 /* remove timeout handler */
464 struct sigaction action;
466 memset (&action, 0, sizeof (action));
467 action.sa_handler = sigint_handler_sighandler;
469 sigaction (SIGINT, &action, NULL);
473 sigint_restore (void)
475 struct sigaction action;
477 memset (&action, 0, sizeof (action));
478 action.sa_handler = SIG_DFL;
480 sigaction (SIGINT, &action, NULL);
483 /* FIXME 0.11: remove SIGUSR handling (also from man page) */
485 play_handler (int signum)
489 PRINT ("Caught SIGUSR1 - Play request.\n");
490 gst_element_set_state (pipeline, GST_STATE_PLAYING);
493 PRINT ("Caught SIGUSR2 - Stop request.\n");
494 gst_element_set_state (pipeline, GST_STATE_NULL);
500 play_signal_setup (void)
502 struct sigaction action;
504 memset (&action, 0, sizeof (action));
505 action.sa_handler = play_handler;
506 sigaction (SIGUSR1, &action, NULL);
507 sigaction (SIGUSR2, &action, NULL);
509 #endif /* DISABLE_FAULT_HANDLER */
511 /* returns ELR_ERROR if there was an error
512 * or ELR_INTERRUPT if we caught a keyboard interrupt
513 * or ELR_NO_ERROR otherwise. */
514 static EventLoopResult
515 event_loop (GstElement * pipeline, gboolean blocking, GstState target_state)
517 #ifndef DISABLE_FAULT_HANDLER
521 GstMessage *message = NULL;
522 EventLoopResult res = ELR_NO_ERROR;
523 gboolean buffering = FALSE;
525 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
527 #ifndef DISABLE_FAULT_HANDLER
528 timeout_id = g_timeout_add (250, (GSourceFunc) check_intr, pipeline);
532 message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
534 /* if the poll timed out, only when !blocking */
538 /* check if we need to dump messages to the console */
541 const GstStructure *s;
544 seqnum = gst_message_get_seqnum (message);
546 s = gst_message_get_structure (message);
548 src_obj = GST_MESSAGE_SRC (message);
550 if (GST_IS_ELEMENT (src_obj)) {
551 PRINT (_("Got message #%u from element \"%s\" (%s): "),
552 (guint) seqnum, GST_ELEMENT_NAME (src_obj),
553 GST_MESSAGE_TYPE_NAME (message));
554 } else if (GST_IS_PAD (src_obj)) {
555 PRINT (_("Got message #%u from pad \"%s:%s\" (%s): "),
556 (guint) seqnum, GST_DEBUG_PAD_NAME (src_obj),
557 GST_MESSAGE_TYPE_NAME (message));
558 } else if (GST_IS_OBJECT (src_obj)) {
559 PRINT (_("Got message #%u from object \"%s\" (%s): "),
560 (guint) seqnum, GST_OBJECT_NAME (src_obj),
561 GST_MESSAGE_TYPE_NAME (message));
563 PRINT (_("Got message #%u (%s): "), (guint) seqnum,
564 GST_MESSAGE_TYPE_NAME (message));
570 sstr = gst_structure_to_string (s);
571 PRINT ("%s\n", sstr);
574 PRINT ("no message details\n");
578 switch (GST_MESSAGE_TYPE (message)) {
579 case GST_MESSAGE_NEW_CLOCK:
583 gst_message_parse_new_clock (message, &clock);
585 PRINT ("New clock: %s\n", (clock ? GST_OBJECT_NAME (clock) : "NULL"));
588 case GST_MESSAGE_CLOCK_LOST:
589 PRINT ("Clock lost, selecting a new one\n");
590 gst_element_set_state (pipeline, GST_STATE_PAUSED);
591 gst_element_set_state (pipeline, GST_STATE_PLAYING);
593 case GST_MESSAGE_EOS:{
595 PRINT (_("Got EOS from element \"%s\".\n"),
596 GST_MESSAGE_SRC_NAME (message));
599 case GST_MESSAGE_TAG:
603 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
604 PRINT (_("FOUND TAG : found by element \"%s\".\n"),
605 GST_MESSAGE_SRC_NAME (message));
606 } else if (GST_IS_PAD (GST_MESSAGE_SRC (message))) {
607 PRINT (_("FOUND TAG : found by pad \"%s:%s\".\n"),
608 GST_DEBUG_PAD_NAME (GST_MESSAGE_SRC (message)));
609 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
610 PRINT (_("FOUND TAG : found by object \"%s\".\n"),
611 GST_MESSAGE_SRC_NAME (message));
613 PRINT (_("FOUND TAG\n"));
616 gst_message_parse_tag (message, &tags);
617 gst_tag_list_foreach (tags, print_tag, NULL);
618 gst_tag_list_free (tags);
621 case GST_MESSAGE_INFO:{
624 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
626 gst_message_parse_info (message, &gerror, &debug);
628 PRINT (_("INFO:\n%s\n"), debug);
630 g_error_free (gerror);
635 case GST_MESSAGE_WARNING:{
638 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
640 /* dump graph on warning */
641 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
642 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.warning");
644 gst_message_parse_warning (message, &gerror, &debug);
645 PRINT (_("WARNING: from element %s: %s\n"), name, gerror->message);
647 PRINT (_("Additional debug info:\n%s\n"), debug);
649 g_error_free (gerror);
654 case GST_MESSAGE_ERROR:{
655 /* dump graph on error */
656 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
657 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.error");
659 print_error_message (message);
661 /* we have an error */
665 case GST_MESSAGE_STATE_CHANGED:{
666 GstState old, new, pending;
668 /* we only care about pipeline state change messages */
669 if (GST_MESSAGE_SRC (message) != GST_OBJECT_CAST (pipeline))
672 /* ignore when we are buffering since then we mess with the states
675 PRINT (_("Prerolled, waiting for buffering to finish...\n"));
679 gst_message_parse_state_changed (message, &old, &new, &pending);
681 /* if we reached the final target state, exit */
682 if (target_state == GST_STATE_PAUSED && new == target_state)
685 /* else not an interesting message */
688 case GST_MESSAGE_BUFFERING:{
691 gst_message_parse_buffering (message, &percent);
692 PRINT ("%s %d%% \r", _("buffering..."), percent);
694 /* no state management needed for live pipelines */
698 if (percent == 100) {
699 /* a 100% message means buffering is done */
701 /* if the desired state is playing, go back */
702 if (target_state == GST_STATE_PLAYING) {
703 PRINT (_("Done buffering, setting pipeline to PLAYING ...\n"));
704 gst_element_set_state (pipeline, GST_STATE_PLAYING);
709 if (buffering == FALSE && target_state == GST_STATE_PLAYING) {
710 /* we were not buffering but PLAYING, PAUSE the pipeline. */
711 PRINT (_("Buffering, setting pipeline to PAUSED ...\n"));
712 gst_element_set_state (pipeline, GST_STATE_PAUSED);
718 case GST_MESSAGE_LATENCY:
720 PRINT (_("Redistribute latency...\n"));
721 gst_bin_recalculate_latency (GST_BIN (pipeline));
724 case GST_MESSAGE_REQUEST_STATE:
727 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
729 gst_message_parse_request_state (message, &state);
731 PRINT (_("Setting state to %s as requested by %s...\n"),
732 gst_element_state_get_name (state), name);
734 gst_element_set_state (pipeline, state);
739 case GST_MESSAGE_APPLICATION:{
740 const GstStructure *s;
742 s = gst_message_get_structure (message);
744 if (gst_structure_has_name (s, "GstLaunchInterrupt")) {
745 /* this application message is posted when we caught an interrupt and
746 * we need to stop the pipeline. */
747 PRINT (_("Interrupt: Stopping pipeline ...\n"));
753 /* just be quiet by default */
757 gst_message_unref (message);
759 g_assert_not_reached ();
764 gst_message_unref (message);
765 gst_object_unref (bus);
766 #ifndef DISABLE_FAULT_HANDLER
767 g_source_remove (timeout_id);
773 static GstBusSyncReply
774 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer data)
776 GstElement *pipeline = (GstElement *) data;
778 switch (GST_MESSAGE_TYPE (message)) {
779 case GST_MESSAGE_STATE_CHANGED:
780 /* we only care about pipeline state change messages */
781 if (GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
782 GstState old, new, pending;
783 gchar *state_transition_name;
785 gst_message_parse_state_changed (message, &old, &new, &pending);
787 state_transition_name = g_strdup_printf ("%s_%s",
788 gst_element_state_get_name (old), gst_element_state_get_name (new));
790 /* dump graph for (some) pipeline state changes */
792 gchar *dump_name = g_strconcat ("gst-launch.", state_transition_name,
794 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
795 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
799 /* place a marker into e.g. strace logs */
801 gchar *access_name = g_strconcat (g_get_tmp_dir (), G_DIR_SEPARATOR_S,
802 "gst-launch", G_DIR_SEPARATOR_S, state_transition_name, NULL);
803 g_file_test (access_name, G_FILE_TEST_EXISTS);
804 g_free (access_name);
807 g_free (state_transition_name);
816 main (int argc, char *argv[])
819 gboolean verbose = FALSE;
820 gboolean no_fault = FALSE;
821 gboolean no_sigusr_handler = FALSE;
822 gboolean trace = FALSE;
823 gboolean eos_on_shutdown = FALSE;
824 gboolean check_index = FALSE;
825 gchar *savefile = NULL;
826 gchar *exclude_args = NULL;
827 #ifndef GST_DISABLE_OPTION_PARSING
828 GOptionEntry options[] = {
829 {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
830 N_("Output tags (also known as metadata)"), NULL},
831 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
832 N_("Output status information and property notifications"), NULL},
833 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
834 N_("Do not print any progress information"), NULL},
835 {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
836 N_("Output messages"), NULL},
837 {"exclude", 'X', 0, G_OPTION_ARG_NONE, &exclude_args,
838 N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
839 {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
840 N_("Do not install a fault handler"), NULL},
841 {"no-sigusr-handler", '\0', 0, G_OPTION_ARG_NONE, &no_sigusr_handler,
842 N_("Do not install signal handlers for SIGUSR1 and SIGUSR2"), NULL},
843 {"trace", 'T', 0, G_OPTION_ARG_NONE, &trace,
844 N_("Print alloc trace (if enabled at compile time)"), NULL},
845 {"eos-on-shutdown", 'e', 0, G_OPTION_ARG_NONE, &eos_on_shutdown,
846 N_("Force EOS on sources before shutting the pipeline down"), NULL},
847 {"index", 'i', 0, G_OPTION_ARG_NONE, &check_index,
848 N_("Gather and print index statistics"), NULL},
849 GST_TOOLS_GOPTION_VERSION,
856 GPtrArray *index_stats = NULL;
858 GError *error = NULL;
861 free (malloc (8)); /* -lefence */
864 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
865 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
866 textdomain (GETTEXT_PACKAGE);
869 g_thread_init (NULL);
871 gst_tools_set_prgname ("gst-launch");
873 #ifndef GST_DISABLE_OPTION_PARSING
874 ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
875 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
876 g_option_context_add_group (ctx, gst_init_get_option_group ());
877 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
879 g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
881 g_printerr ("Error initializing: Unknown error!\n");
884 g_option_context_free (ctx);
886 gst_init (&argc, &argv);
889 gst_tools_print_version ("gst-launch");
891 #ifndef DISABLE_FAULT_HANDLER
897 if (!no_sigusr_handler)
898 play_signal_setup ();
902 if (!gst_alloc_trace_available ()) {
903 g_warning ("Trace not available (recompile with trace enabled).");
905 gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE |
906 GST_ALLOC_TRACE_MEM_LIVE);
907 gst_alloc_trace_print_live ();
910 /* make a null-terminated version of argv */
911 argvn = g_new0 (char *, argc);
912 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
915 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
921 g_printerr (_("ERROR: pipeline could not be constructed: %s.\n"),
922 GST_STR_NULL (error->message));
923 g_error_free (error);
925 g_printerr (_("ERROR: pipeline could not be constructed.\n"));
929 g_printerr (_("WARNING: erroneous pipeline: %s\n"),
930 GST_STR_NULL (error->message));
931 g_error_free (error);
936 gchar **exclude_list =
937 exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
938 g_signal_connect (pipeline, "deep-notify",
939 G_CALLBACK (gst_object_default_deep_notify), exclude_list);
943 GstState state, pending;
944 GstStateChangeReturn ret;
947 /* If the top-level object is not a pipeline, place it in a pipeline. */
948 if (!GST_IS_PIPELINE (pipeline)) {
949 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
951 if (real_pipeline == NULL) {
952 g_printerr (_("ERROR: the 'pipeline' element wasn't found.\n"));
955 gst_bin_add (GST_BIN (real_pipeline), pipeline);
956 pipeline = real_pipeline;
960 /* gst_index_new() creates a null-index, it does not store anything, but
961 * the entry-added signal works and this is what we use to build the
963 index = gst_index_new ();
965 index_stats = g_ptr_array_new ();
966 g_signal_connect (G_OBJECT (index), "entry-added",
967 G_CALLBACK (entry_added), index_stats);
968 g_object_set (G_OBJECT (index), "resolver", GST_INDEX_RESOLVER_GTYPE,
970 gst_element_set_index (pipeline, index);
974 bus = gst_element_get_bus (pipeline);
975 gst_bus_set_sync_handler (bus, bus_sync_handler, (gpointer) pipeline);
976 gst_object_unref (bus);
978 PRINT (_("Setting pipeline to PAUSED ...\n"));
979 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
982 case GST_STATE_CHANGE_FAILURE:
983 g_printerr (_("ERROR: Pipeline doesn't want to pause.\n"));
985 event_loop (pipeline, FALSE, GST_STATE_VOID_PENDING);
987 case GST_STATE_CHANGE_NO_PREROLL:
988 PRINT (_("Pipeline is live and does not need PREROLL ...\n"));
991 case GST_STATE_CHANGE_ASYNC:
992 PRINT (_("Pipeline is PREROLLING ...\n"));
993 caught_error = event_loop (pipeline, TRUE, GST_STATE_PAUSED);
995 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
998 state = GST_STATE_PAUSED;
1000 case GST_STATE_CHANGE_SUCCESS:
1001 PRINT (_("Pipeline is PREROLLED ...\n"));
1005 caught_error = event_loop (pipeline, FALSE, GST_STATE_PLAYING);
1008 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1010 GstClockTime tfthen, tfnow;
1011 GstClockTimeDiff diff;
1013 PRINT (_("Setting pipeline to PLAYING ...\n"));
1015 if (gst_element_set_state (pipeline,
1016 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
1017 GstMessage *err_msg;
1020 g_printerr (_("ERROR: pipeline doesn't want to play.\n"));
1021 bus = gst_element_get_bus (pipeline);
1022 if ((err_msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0))) {
1023 print_error_message (err_msg);
1024 gst_message_unref (err_msg);
1026 gst_object_unref (bus);
1031 tfthen = gst_util_get_timestamp ();
1032 caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
1033 if (eos_on_shutdown && caught_error == ELR_INTERRUPT) {
1034 PRINT (_("EOS on shutdown enabled -- Forcing EOS on the pipeline\n"));
1036 gst_element_send_event (pipeline, gst_event_new_eos ());
1037 PRINT (_("Waiting for EOS...\n"));
1038 caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
1040 if (caught_error == ELR_NO_ERROR) {
1042 PRINT (_("EOS received - stopping pipeline...\n"));
1043 } else if (caught_error == ELR_ERROR) {
1044 PRINT (_("An error happened while waiting for EOS\n"));
1047 tfnow = gst_util_get_timestamp ();
1049 diff = GST_CLOCK_DIFF (tfthen, tfnow);
1051 PRINT (_("Execution ended after %" G_GUINT64_FORMAT " ns.\n"), diff);
1054 PRINT (_("Setting pipeline to PAUSED ...\n"));
1055 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1056 if (caught_error == ELR_NO_ERROR)
1057 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1059 /* iterate mainloop to process pending stuff */
1060 while (g_main_context_iteration (NULL, FALSE));
1062 PRINT (_("Setting pipeline to READY ...\n"));
1063 gst_element_set_state (pipeline, GST_STATE_READY);
1064 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1067 print_index_stats (index_stats);
1068 g_ptr_array_free (index_stats, TRUE);
1072 PRINT (_("Setting pipeline to NULL ...\n"));
1073 gst_element_set_state (pipeline, GST_STATE_NULL);
1074 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1077 PRINT (_("Freeing pipeline ...\n"));
1078 gst_object_unref (pipeline);
1082 gst_alloc_trace_print_live ();