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 */
181 typedef struct _GstIndexStats
189 GstClockTime last_keyframe;
190 GstClockTime last_dltframe;
191 GstClockTime min_keyframe_gap;
192 GstClockTime max_keyframe_gap;
193 GstClockTime avg_keyframe_gap;
197 entry_added (GstIndex * index, GstIndexEntry * entry, gpointer user_data)
199 GPtrArray *index_stats = (GPtrArray *) user_data;
202 switch (entry->type) {
203 case GST_INDEX_ENTRY_ID:
204 /* we have a new writer */
205 GST_DEBUG_OBJECT (index, "id %d: describes writer %s", entry->id,
206 GST_INDEX_ID_DESCRIPTION (entry));
207 if (entry->id >= index_stats->len) {
208 g_ptr_array_set_size (index_stats, entry->id + 1);
210 s = g_new (GstIndexStats, 1);
212 s->desc = g_strdup (GST_INDEX_ID_DESCRIPTION (entry));
213 s->num_frames = s->num_keyframes = s->num_dltframes = 0;
214 s->last_keyframe = s->last_dltframe = GST_CLOCK_TIME_NONE;
215 s->min_keyframe_gap = s->max_keyframe_gap = s->avg_keyframe_gap =
217 g_ptr_array_index (index_stats, entry->id) = s;
219 case GST_INDEX_ENTRY_FORMAT:
220 /* have not found any code calling this */
221 GST_DEBUG_OBJECT (index, "id %d: registered format %d for %s\n",
222 entry->id, GST_INDEX_FORMAT_FORMAT (entry),
223 GST_INDEX_FORMAT_KEY (entry));
225 case GST_INDEX_ENTRY_ASSOCIATION:
228 GstAssocFlags flags = GST_INDEX_ASSOC_FLAGS (entry);
230 s = g_ptr_array_index (index_stats, entry->id);
231 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
233 if (flags & GST_ASSOCIATION_FLAG_KEY_UNIT) {
236 if (GST_CLOCK_TIME_IS_VALID (ts)) {
237 if (GST_CLOCK_TIME_IS_VALID (s->last_keyframe)) {
238 GstClockTimeDiff d = GST_CLOCK_DIFF (s->last_keyframe, ts);
240 if (G_UNLIKELY (d < 0)) {
241 GST_WARNING ("received out-of-order keyframe at %"
242 GST_TIME_FORMAT, GST_TIME_ARGS (ts));
243 /* FIXME: does it still make sense to use that for the statistics */
244 d = GST_CLOCK_DIFF (ts, s->last_keyframe);
247 if (GST_CLOCK_TIME_IS_VALID (s->min_keyframe_gap)) {
248 if (d < s->min_keyframe_gap)
249 s->min_keyframe_gap = d;
251 s->min_keyframe_gap = d;
253 if (GST_CLOCK_TIME_IS_VALID (s->max_keyframe_gap)) {
254 if (d > s->max_keyframe_gap)
255 s->max_keyframe_gap = d;
257 s->max_keyframe_gap = d;
259 if (GST_CLOCK_TIME_IS_VALID (s->avg_keyframe_gap)) {
260 s->avg_keyframe_gap = (d + s->num_frames * s->avg_keyframe_gap) /
263 s->avg_keyframe_gap = d;
266 s->last_keyframe = ts;
269 if (flags & GST_ASSOCIATION_FLAG_DELTA_UNIT) {
271 if (GST_CLOCK_TIME_IS_VALID (ts)) {
272 s->last_dltframe = ts;
284 /* print statistics from the entry_added callback, free the entries */
286 print_index_stats (GPtrArray * index_stats)
290 if (index_stats->len) {
291 g_print ("%s:\n", _("Index statistics"));
294 for (i = 0; i < index_stats->len; i++) {
295 GstIndexStats *s = g_ptr_array_index (index_stats, i);
297 g_print ("id %d, %s\n", s->id, s->desc);
299 GstClockTime last_frame = s->last_keyframe;
301 if (GST_CLOCK_TIME_IS_VALID (s->last_dltframe)) {
302 if (!GST_CLOCK_TIME_IS_VALID (last_frame) ||
303 (s->last_dltframe > last_frame))
304 last_frame = s->last_dltframe;
307 if (GST_CLOCK_TIME_IS_VALID (last_frame)) {
308 g_print (" total time = %" GST_TIME_FORMAT "\n",
309 GST_TIME_ARGS (last_frame));
311 g_print (" frame/keyframe rate = %u / %u = ", s->num_frames,
313 if (s->num_keyframes)
314 g_print ("%lf\n", s->num_frames / (gdouble) s->num_keyframes);
317 if (s->num_keyframes) {
318 g_print (" min/avg/max keyframe gap = %" GST_TIME_FORMAT ", %"
319 GST_TIME_FORMAT ", %" GST_TIME_FORMAT "\n",
320 GST_TIME_ARGS (s->min_keyframe_gap),
321 GST_TIME_ARGS (s->avg_keyframe_gap),
322 GST_TIME_ARGS (s->max_keyframe_gap));
325 g_print (" no stats\n");
335 /* Kids, use the functions from libgstpbutils in gst-plugins-base in your
336 * own code (we can't do that here because it would introduce a circular
339 gst_is_missing_plugin_message (GstMessage * msg)
341 if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ELEMENT
342 || gst_message_get_structure (msg) == NULL)
345 return gst_structure_has_name (gst_message_get_structure (msg),
350 gst_missing_plugin_message_get_description (GstMessage * msg)
352 return gst_structure_get_string (gst_message_get_structure (msg), "name");
356 print_error_message (GstMessage * msg)
359 gchar *name, *debug = NULL;
361 name = gst_object_get_path_string (msg->src);
362 gst_message_parse_error (msg, &err, &debug);
364 g_printerr (_("ERROR: from element %s: %s\n"), name, err->message);
366 g_printerr (_("Additional debug info:\n%s\n"), debug);
374 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
378 count = gst_tag_list_get_tag_size (list, tag);
380 for (i = 0; i < count; i++) {
383 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
384 if (!gst_tag_list_get_string_index (list, tag, i, &str))
385 g_assert_not_reached ();
386 } else if (gst_tag_get_type (tag) == GST_TYPE_BUFFER) {
389 img = gst_value_get_buffer (gst_tag_list_get_value_index (list, tag, i));
393 caps_str = g_strdup ("unknown");
394 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes, type: %s",
395 gst_buffer_get_size (img), caps_str);
398 str = g_strdup ("NULL buffer");
400 } else if (gst_tag_get_type (tag) == GST_TYPE_DATE_TIME) {
401 GstDateTime *dt = NULL;
403 gst_tag_list_get_date_time_index (list, tag, i, &dt);
404 if (gst_date_time_get_hour (dt) < 0) {
405 str = g_strdup_printf ("%02u-%02u-%04u", gst_date_time_get_day (dt),
406 gst_date_time_get_month (dt), gst_date_time_get_year (dt));
408 gdouble tz_offset = gst_date_time_get_time_zone_offset (dt);
411 if (tz_offset != 0.0) {
412 g_snprintf (tz_str, sizeof (tz_str), "(UTC %s%gh)",
413 (tz_offset > 0.0) ? "+" : "", tz_offset);
415 g_snprintf (tz_str, sizeof (tz_str), "(UTC)");
418 str = g_strdup_printf ("%04u-%02u-%02u %02u:%02u:%02u %s",
419 gst_date_time_get_year (dt), gst_date_time_get_month (dt),
420 gst_date_time_get_day (dt), gst_date_time_get_hour (dt),
421 gst_date_time_get_minute (dt), gst_date_time_get_second (dt),
424 gst_date_time_unref (dt);
427 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
431 PRINT ("%16s: %s\n", gst_tag_get_nick (tag), str);
433 PRINT ("%16s: %s\n", "", str);
440 #ifndef DISABLE_FAULT_HANDLER
441 /* we only use sighandler here because the registers are not important */
443 sigint_handler_sighandler (int signum)
445 PRINT ("Caught interrupt -- ");
447 /* If we were waiting for an EOS, we still want to catch
448 * the next signal to shutdown properly (and the following one
449 * will quit the program). */
455 /* we set a flag that is checked by the mainloop, we cannot do much in the
456 * interrupt handler (no mutex or other blocking stuff) */
460 /* is called every 250 milliseconds (4 times a second), the interrupt handler
461 * will set a flag for us. We react to this by posting a message. */
463 check_intr (GstElement * pipeline)
469 PRINT ("handling interrupt.\n");
471 /* post an application specific message */
472 gst_element_post_message (GST_ELEMENT (pipeline),
473 gst_message_new_application (GST_OBJECT (pipeline),
474 gst_structure_new ("GstLaunchInterrupt",
475 "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));
477 /* remove timeout handler */
485 struct sigaction action;
487 memset (&action, 0, sizeof (action));
488 action.sa_handler = sigint_handler_sighandler;
490 sigaction (SIGINT, &action, NULL);
494 sigint_restore (void)
496 struct sigaction action;
498 memset (&action, 0, sizeof (action));
499 action.sa_handler = SIG_DFL;
501 sigaction (SIGINT, &action, NULL);
503 #endif /* DISABLE_FAULT_HANDLER */
505 /* returns ELR_ERROR if there was an error
506 * or ELR_INTERRUPT if we caught a keyboard interrupt
507 * or ELR_NO_ERROR otherwise. */
508 static EventLoopResult
509 event_loop (GstElement * pipeline, gboolean blocking, GstState target_state)
511 #ifndef DISABLE_FAULT_HANDLER
515 GstMessage *message = NULL;
516 EventLoopResult res = ELR_NO_ERROR;
517 gboolean buffering = FALSE;
519 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
521 #ifndef DISABLE_FAULT_HANDLER
522 timeout_id = g_timeout_add (250, (GSourceFunc) check_intr, pipeline);
526 message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
528 /* if the poll timed out, only when !blocking */
532 /* check if we need to dump messages to the console */
535 const GstStructure *s;
538 seqnum = gst_message_get_seqnum (message);
540 s = gst_message_get_structure (message);
542 src_obj = GST_MESSAGE_SRC (message);
544 if (GST_IS_ELEMENT (src_obj)) {
545 PRINT (_("Got message #%u from element \"%s\" (%s): "),
546 (guint) seqnum, GST_ELEMENT_NAME (src_obj),
547 GST_MESSAGE_TYPE_NAME (message));
548 } else if (GST_IS_PAD (src_obj)) {
549 PRINT (_("Got message #%u from pad \"%s:%s\" (%s): "),
550 (guint) seqnum, GST_DEBUG_PAD_NAME (src_obj),
551 GST_MESSAGE_TYPE_NAME (message));
552 } else if (GST_IS_OBJECT (src_obj)) {
553 PRINT (_("Got message #%u from object \"%s\" (%s): "),
554 (guint) seqnum, GST_OBJECT_NAME (src_obj),
555 GST_MESSAGE_TYPE_NAME (message));
557 PRINT (_("Got message #%u (%s): "), (guint) seqnum,
558 GST_MESSAGE_TYPE_NAME (message));
564 sstr = gst_structure_to_string (s);
565 PRINT ("%s\n", sstr);
568 PRINT ("no message details\n");
572 switch (GST_MESSAGE_TYPE (message)) {
573 case GST_MESSAGE_NEW_CLOCK:
577 gst_message_parse_new_clock (message, &clock);
579 PRINT ("New clock: %s\n", (clock ? GST_OBJECT_NAME (clock) : "NULL"));
582 case GST_MESSAGE_CLOCK_LOST:
583 PRINT ("Clock lost, selecting a new one\n");
584 gst_element_set_state (pipeline, GST_STATE_PAUSED);
585 gst_element_set_state (pipeline, GST_STATE_PLAYING);
587 case GST_MESSAGE_EOS:{
589 PRINT (_("Got EOS from element \"%s\".\n"),
590 GST_MESSAGE_SRC_NAME (message));
593 case GST_MESSAGE_TAG:
597 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
598 PRINT (_("FOUND TAG : found by element \"%s\".\n"),
599 GST_MESSAGE_SRC_NAME (message));
600 } else if (GST_IS_PAD (GST_MESSAGE_SRC (message))) {
601 PRINT (_("FOUND TAG : found by pad \"%s:%s\".\n"),
602 GST_DEBUG_PAD_NAME (GST_MESSAGE_SRC (message)));
603 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
604 PRINT (_("FOUND TAG : found by object \"%s\".\n"),
605 GST_MESSAGE_SRC_NAME (message));
607 PRINT (_("FOUND TAG\n"));
610 gst_message_parse_tag (message, &tags);
611 gst_tag_list_foreach (tags, print_tag, NULL);
612 gst_tag_list_free (tags);
615 case GST_MESSAGE_INFO:{
618 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
620 gst_message_parse_info (message, &gerror, &debug);
622 PRINT (_("INFO:\n%s\n"), debug);
624 g_error_free (gerror);
629 case GST_MESSAGE_WARNING:{
632 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
634 /* dump graph on warning */
635 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
636 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.warning");
638 gst_message_parse_warning (message, &gerror, &debug);
639 PRINT (_("WARNING: from element %s: %s\n"), name, gerror->message);
641 PRINT (_("Additional debug info:\n%s\n"), debug);
643 g_error_free (gerror);
648 case GST_MESSAGE_ERROR:{
649 /* dump graph on error */
650 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
651 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.error");
653 print_error_message (message);
655 /* we have an error */
659 case GST_MESSAGE_STATE_CHANGED:{
660 GstState old, new, pending;
662 /* we only care about pipeline state change messages */
663 if (GST_MESSAGE_SRC (message) != GST_OBJECT_CAST (pipeline))
666 /* ignore when we are buffering since then we mess with the states
669 PRINT (_("Prerolled, waiting for buffering to finish...\n"));
673 gst_message_parse_state_changed (message, &old, &new, &pending);
675 /* if we reached the final target state, exit */
676 if (target_state == GST_STATE_PAUSED && new == target_state)
679 /* else not an interesting message */
682 case GST_MESSAGE_BUFFERING:{
685 gst_message_parse_buffering (message, &percent);
686 PRINT ("%s %d%% \r", _("buffering..."), percent);
688 /* no state management needed for live pipelines */
692 if (percent == 100) {
693 /* a 100% message means buffering is done */
695 /* if the desired state is playing, go back */
696 if (target_state == GST_STATE_PLAYING) {
697 PRINT (_("Done buffering, setting pipeline to PLAYING ...\n"));
698 gst_element_set_state (pipeline, GST_STATE_PLAYING);
703 if (buffering == FALSE && target_state == GST_STATE_PLAYING) {
704 /* we were not buffering but PLAYING, PAUSE the pipeline. */
705 PRINT (_("Buffering, setting pipeline to PAUSED ...\n"));
706 gst_element_set_state (pipeline, GST_STATE_PAUSED);
712 case GST_MESSAGE_LATENCY:
714 PRINT (_("Redistribute latency...\n"));
715 gst_bin_recalculate_latency (GST_BIN (pipeline));
718 case GST_MESSAGE_REQUEST_STATE:
721 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
723 gst_message_parse_request_state (message, &state);
725 PRINT (_("Setting state to %s as requested by %s...\n"),
726 gst_element_state_get_name (state), name);
728 gst_element_set_state (pipeline, state);
733 case GST_MESSAGE_APPLICATION:{
734 const GstStructure *s;
736 s = gst_message_get_structure (message);
738 if (gst_structure_has_name (s, "GstLaunchInterrupt")) {
739 /* this application message is posted when we caught an interrupt and
740 * we need to stop the pipeline. */
741 PRINT (_("Interrupt: Stopping pipeline ...\n"));
747 case GST_MESSAGE_ELEMENT:{
748 if (gst_is_missing_plugin_message (message)) {
751 desc = gst_missing_plugin_message_get_description (message);
752 PRINT (_("Missing element: %s\n"), desc ? desc : "(no description)");
757 /* just be quiet by default */
761 gst_message_unref (message);
763 g_assert_not_reached ();
768 gst_message_unref (message);
769 gst_object_unref (bus);
770 #ifndef DISABLE_FAULT_HANDLER
771 g_source_remove (timeout_id);
777 static GstBusSyncReply
778 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer data)
780 GstElement *pipeline = (GstElement *) data;
782 switch (GST_MESSAGE_TYPE (message)) {
783 case GST_MESSAGE_STATE_CHANGED:
784 /* we only care about pipeline state change messages */
785 if (GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
786 GstState old, new, pending;
787 gchar *state_transition_name;
789 gst_message_parse_state_changed (message, &old, &new, &pending);
791 state_transition_name = g_strdup_printf ("%s_%s",
792 gst_element_state_get_name (old), gst_element_state_get_name (new));
794 /* dump graph for (some) pipeline state changes */
796 gchar *dump_name = g_strconcat ("gst-launch.", state_transition_name,
798 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
799 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
803 /* place a marker into e.g. strace logs */
805 gchar *access_name = g_strconcat (g_get_tmp_dir (), G_DIR_SEPARATOR_S,
806 "gst-launch", G_DIR_SEPARATOR_S, state_transition_name, NULL);
807 g_file_test (access_name, G_FILE_TEST_EXISTS);
808 g_free (access_name);
811 g_free (state_transition_name);
820 main (int argc, char *argv[])
823 gboolean verbose = FALSE;
824 gboolean no_fault = FALSE;
825 gboolean eos_on_shutdown = FALSE;
827 gboolean check_index = FALSE;
829 gchar *savefile = NULL;
830 gchar *exclude_args = NULL;
831 #ifndef GST_DISABLE_OPTION_PARSING
832 GOptionEntry options[] = {
833 {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
834 N_("Output tags (also known as metadata)"), NULL},
835 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
836 N_("Output status information and property notifications"), NULL},
837 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
838 N_("Do not print any progress information"), NULL},
839 {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
840 N_("Output messages"), NULL},
841 {"exclude", 'X', 0, G_OPTION_ARG_NONE, &exclude_args,
842 N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
843 {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
844 N_("Do not install a fault handler"), 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},
848 {"index", 'i', 0, G_OPTION_ARG_NONE, &check_index,
849 N_("Gather and print index statistics"), NULL},
851 GST_TOOLS_GOPTION_VERSION,
859 GPtrArray *index_stats = NULL;
862 GError *error = NULL;
865 free (malloc (8)); /* -lefence */
868 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
869 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
870 textdomain (GETTEXT_PACKAGE);
873 gst_tools_set_prgname ("gst-launch");
875 #ifndef GST_DISABLE_OPTION_PARSING
876 ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
877 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
878 g_option_context_add_group (ctx, gst_init_get_option_group ());
879 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
881 g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
883 g_printerr ("Error initializing: Unknown error!\n");
886 g_option_context_free (ctx);
888 gst_init (&argc, &argv);
891 gst_tools_print_version ("gst-launch");
893 #ifndef DISABLE_FAULT_HANDLER
900 /* make a null-terminated version of argv */
901 argvn = g_new0 (char *, argc);
902 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
905 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
911 g_printerr (_("ERROR: pipeline could not be constructed: %s.\n"),
912 GST_STR_NULL (error->message));
913 g_error_free (error);
915 g_printerr (_("ERROR: pipeline could not be constructed.\n"));
919 g_printerr (_("WARNING: erroneous pipeline: %s\n"),
920 GST_STR_NULL (error->message));
921 g_error_free (error);
926 gchar **exclude_list =
927 exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
928 g_signal_connect (pipeline, "deep-notify",
929 G_CALLBACK (gst_object_default_deep_notify), exclude_list);
933 GstState state, pending;
934 GstStateChangeReturn ret;
937 /* If the top-level object is not a pipeline, place it in a pipeline. */
938 if (!GST_IS_PIPELINE (pipeline)) {
939 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
941 if (real_pipeline == NULL) {
942 g_printerr (_("ERROR: the 'pipeline' element wasn't found.\n"));
945 gst_bin_add (GST_BIN (real_pipeline), pipeline);
946 pipeline = real_pipeline;
950 /* gst_index_new() creates a null-index, it does not store anything, but
951 * the entry-added signal works and this is what we use to build the
953 index = gst_index_new ();
955 index_stats = g_ptr_array_new ();
956 g_signal_connect (G_OBJECT (index), "entry-added",
957 G_CALLBACK (entry_added), index_stats);
958 g_object_set (G_OBJECT (index), "resolver", GST_INDEX_RESOLVER_GTYPE,
960 gst_element_set_index (pipeline, index);
965 bus = gst_element_get_bus (pipeline);
966 gst_bus_set_sync_handler (bus, bus_sync_handler, (gpointer) pipeline);
967 gst_object_unref (bus);
969 PRINT (_("Setting pipeline to PAUSED ...\n"));
970 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
973 case GST_STATE_CHANGE_FAILURE:
974 g_printerr (_("ERROR: Pipeline doesn't want to pause.\n"));
976 event_loop (pipeline, FALSE, GST_STATE_VOID_PENDING);
978 case GST_STATE_CHANGE_NO_PREROLL:
979 PRINT (_("Pipeline is live and does not need PREROLL ...\n"));
982 case GST_STATE_CHANGE_ASYNC:
983 PRINT (_("Pipeline is PREROLLING ...\n"));
984 caught_error = event_loop (pipeline, TRUE, GST_STATE_PAUSED);
986 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
989 state = GST_STATE_PAUSED;
991 case GST_STATE_CHANGE_SUCCESS:
992 PRINT (_("Pipeline is PREROLLED ...\n"));
996 caught_error = event_loop (pipeline, FALSE, GST_STATE_PLAYING);
999 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1001 GstClockTime tfthen, tfnow;
1002 GstClockTimeDiff diff;
1004 PRINT (_("Setting pipeline to PLAYING ...\n"));
1006 if (gst_element_set_state (pipeline,
1007 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
1008 GstMessage *err_msg;
1011 g_printerr (_("ERROR: pipeline doesn't want to play.\n"));
1012 bus = gst_element_get_bus (pipeline);
1013 if ((err_msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0))) {
1014 print_error_message (err_msg);
1015 gst_message_unref (err_msg);
1017 gst_object_unref (bus);
1022 tfthen = gst_util_get_timestamp ();
1023 caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
1024 if (eos_on_shutdown && caught_error == ELR_INTERRUPT) {
1025 PRINT (_("EOS on shutdown enabled -- Forcing EOS on the pipeline\n"));
1027 gst_element_send_event (pipeline, gst_event_new_eos ());
1028 PRINT (_("Waiting for EOS...\n"));
1029 caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
1031 if (caught_error == ELR_NO_ERROR) {
1033 PRINT (_("EOS received - stopping pipeline...\n"));
1034 } else if (caught_error == ELR_ERROR) {
1035 PRINT (_("An error happened while waiting for EOS\n"));
1038 tfnow = gst_util_get_timestamp ();
1040 diff = GST_CLOCK_DIFF (tfthen, tfnow);
1042 PRINT (_("Execution ended after %" G_GUINT64_FORMAT " ns.\n"), diff);
1045 PRINT (_("Setting pipeline to PAUSED ...\n"));
1046 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1047 if (caught_error == ELR_NO_ERROR)
1048 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1050 /* iterate mainloop to process pending stuff */
1051 while (g_main_context_iteration (NULL, FALSE));
1053 PRINT (_("Setting pipeline to READY ...\n"));
1054 gst_element_set_state (pipeline, GST_STATE_READY);
1055 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1059 print_index_stats (index_stats);
1060 g_ptr_array_free (index_stats, TRUE);
1065 PRINT (_("Setting pipeline to NULL ...\n"));
1066 gst_element_set_state (pipeline, GST_STATE_NULL);
1067 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1070 PRINT (_("Freeing pipeline ...\n"));
1071 gst_object_unref (pipeline);