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 toc = FALSE;
73 static gboolean messages = FALSE;
74 static gboolean is_live = FALSE;
75 static gboolean waiting_eos = FALSE;
77 /* convenience macro so we don't have to litter the code with if(!quiet) */
78 #define PRINT if(!quiet)g_print
80 #ifndef DISABLE_FAULT_HANDLER
83 fault_handler_sighandler (int signum)
87 /* printf is used instead of g_print(), since it's less likely to
91 fprintf (stderr, "Caught SIGSEGV\n");
95 printf ("Caught SIGQUIT\n");
98 fprintf (stderr, "signo: %d\n", signum);
105 #else /* USE_SIGINFO */
108 fault_handler_sigaction (int signum, siginfo_t * si, void *misc)
112 /* printf is used instead of g_print(), since it's less likely to
114 switch (si->si_signo) {
116 fprintf (stderr, "Caught SIGSEGV accessing address %p\n", si->si_addr);
120 printf ("Caught SIGQUIT\n");
123 fprintf (stderr, "signo: %d\n", si->si_signo);
124 fprintf (stderr, "errno: %d\n", si->si_errno);
125 fprintf (stderr, "code: %d\n", si->si_code);
131 #endif /* USE_SIGINFO */
138 glib_on_error_halt = FALSE;
139 g_on_error_stack_trace ("gst-launch");
143 /* FIXME how do we know if we were run by libtool? */
145 "Spinning. Please run 'gdb gst-launch %d' to continue debugging, "
146 "Ctrl-C to quit, or Ctrl-\\ to dump core.\n", (gint) getpid ());
154 struct sigaction action;
156 memset (&action, 0, sizeof (action));
157 action.sa_handler = SIG_DFL;
159 sigaction (SIGSEGV, &action, NULL);
160 sigaction (SIGQUIT, &action, NULL);
166 struct sigaction action;
168 memset (&action, 0, sizeof (action));
170 action.sa_sigaction = fault_handler_sigaction;
171 action.sa_flags = SA_SIGINFO;
173 action.sa_handler = fault_handler_sighandler;
176 sigaction (SIGSEGV, &action, NULL);
177 sigaction (SIGQUIT, &action, NULL);
179 #endif /* DISABLE_FAULT_HANDLER */
182 typedef struct _GstIndexStats
190 GstClockTime last_keyframe;
191 GstClockTime last_dltframe;
192 GstClockTime min_keyframe_gap;
193 GstClockTime max_keyframe_gap;
194 GstClockTime avg_keyframe_gap;
198 entry_added (GstIndex * index, GstIndexEntry * entry, gpointer user_data)
200 GPtrArray *index_stats = (GPtrArray *) user_data;
203 switch (entry->type) {
204 case GST_INDEX_ENTRY_ID:
205 /* we have a new writer */
206 GST_DEBUG_OBJECT (index, "id %d: describes writer %s", entry->id,
207 GST_INDEX_ID_DESCRIPTION (entry));
208 if (entry->id >= index_stats->len) {
209 g_ptr_array_set_size (index_stats, entry->id + 1);
211 s = g_new (GstIndexStats, 1);
213 s->desc = g_strdup (GST_INDEX_ID_DESCRIPTION (entry));
214 s->num_frames = s->num_keyframes = s->num_dltframes = 0;
215 s->last_keyframe = s->last_dltframe = GST_CLOCK_TIME_NONE;
216 s->min_keyframe_gap = s->max_keyframe_gap = s->avg_keyframe_gap =
218 g_ptr_array_index (index_stats, entry->id) = s;
220 case GST_INDEX_ENTRY_FORMAT:
221 /* have not found any code calling this */
222 GST_DEBUG_OBJECT (index, "id %d: registered format %d for %s\n",
223 entry->id, GST_INDEX_FORMAT_FORMAT (entry),
224 GST_INDEX_FORMAT_KEY (entry));
226 case GST_INDEX_ENTRY_ASSOCIATION:
229 GstAssocFlags flags = GST_INDEX_ASSOC_FLAGS (entry);
231 s = g_ptr_array_index (index_stats, entry->id);
232 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
234 if (flags & GST_ASSOCIATION_FLAG_KEY_UNIT) {
237 if (GST_CLOCK_TIME_IS_VALID (ts)) {
238 if (GST_CLOCK_TIME_IS_VALID (s->last_keyframe)) {
239 GstClockTimeDiff d = GST_CLOCK_DIFF (s->last_keyframe, ts);
241 if (G_UNLIKELY (d < 0)) {
242 GST_WARNING ("received out-of-order keyframe at %"
243 GST_TIME_FORMAT, GST_TIME_ARGS (ts));
244 /* FIXME: does it still make sense to use that for the statistics */
245 d = GST_CLOCK_DIFF (ts, s->last_keyframe);
248 if (GST_CLOCK_TIME_IS_VALID (s->min_keyframe_gap)) {
249 if (d < s->min_keyframe_gap)
250 s->min_keyframe_gap = d;
252 s->min_keyframe_gap = d;
254 if (GST_CLOCK_TIME_IS_VALID (s->max_keyframe_gap)) {
255 if (d > s->max_keyframe_gap)
256 s->max_keyframe_gap = d;
258 s->max_keyframe_gap = d;
260 if (GST_CLOCK_TIME_IS_VALID (s->avg_keyframe_gap)) {
261 s->avg_keyframe_gap = (d + s->num_frames * s->avg_keyframe_gap) /
264 s->avg_keyframe_gap = d;
267 s->last_keyframe = ts;
270 if (flags & GST_ASSOCIATION_FLAG_DELTA_UNIT) {
272 if (GST_CLOCK_TIME_IS_VALID (ts)) {
273 s->last_dltframe = ts;
285 /* print statistics from the entry_added callback, free the entries */
287 print_index_stats (GPtrArray * index_stats)
291 if (index_stats->len) {
292 g_print ("%s:\n", _("Index statistics"));
295 for (i = 0; i < index_stats->len; i++) {
296 GstIndexStats *s = g_ptr_array_index (index_stats, i);
298 g_print ("id %d, %s\n", s->id, s->desc);
300 GstClockTime last_frame = s->last_keyframe;
302 if (GST_CLOCK_TIME_IS_VALID (s->last_dltframe)) {
303 if (!GST_CLOCK_TIME_IS_VALID (last_frame) ||
304 (s->last_dltframe > last_frame))
305 last_frame = s->last_dltframe;
308 if (GST_CLOCK_TIME_IS_VALID (last_frame)) {
309 g_print (" total time = %" GST_TIME_FORMAT "\n",
310 GST_TIME_ARGS (last_frame));
312 g_print (" frame/keyframe rate = %u / %u = ", s->num_frames,
314 if (s->num_keyframes)
315 g_print ("%lf\n", s->num_frames / (gdouble) s->num_keyframes);
318 if (s->num_keyframes) {
319 g_print (" min/avg/max keyframe gap = %" GST_TIME_FORMAT ", %"
320 GST_TIME_FORMAT ", %" GST_TIME_FORMAT "\n",
321 GST_TIME_ARGS (s->min_keyframe_gap),
322 GST_TIME_ARGS (s->avg_keyframe_gap),
323 GST_TIME_ARGS (s->max_keyframe_gap));
326 g_print (" no stats\n");
336 /* Kids, use the functions from libgstpbutils in gst-plugins-base in your
337 * own code (we can't do that here because it would introduce a circular
340 gst_is_missing_plugin_message (GstMessage * msg)
342 if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ELEMENT
343 || gst_message_get_structure (msg) == NULL)
346 return gst_structure_has_name (gst_message_get_structure (msg),
351 gst_missing_plugin_message_get_description (GstMessage * msg)
353 return gst_structure_get_string (gst_message_get_structure (msg), "name");
357 print_error_message (GstMessage * msg)
360 gchar *name, *debug = NULL;
362 name = gst_object_get_path_string (msg->src);
363 gst_message_parse_error (msg, &err, &debug);
365 g_printerr (_("ERROR: from element %s: %s\n"), name, err->message);
367 g_printerr (_("Additional debug info:\n%s\n"), debug);
375 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
379 count = gst_tag_list_get_tag_size (list, tag);
381 for (i = 0; i < count; i++) {
384 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
385 if (!gst_tag_list_get_string_index (list, tag, i, &str))
386 g_assert_not_reached ();
387 } else if (gst_tag_get_type (tag) == GST_TYPE_BUFFER) {
390 img = gst_value_get_buffer (gst_tag_list_get_value_index (list, tag, i));
394 caps_str = g_strdup ("unknown");
395 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes, type: %s",
396 gst_buffer_get_size (img), caps_str);
399 str = g_strdup ("NULL buffer");
401 } else if (gst_tag_get_type (tag) == GST_TYPE_DATE_TIME) {
402 GstDateTime *dt = NULL;
404 gst_tag_list_get_date_time_index (list, tag, i, &dt);
405 if (gst_date_time_get_hour (dt) < 0) {
406 str = g_strdup_printf ("%02u-%02u-%04u", gst_date_time_get_day (dt),
407 gst_date_time_get_month (dt), gst_date_time_get_year (dt));
409 gdouble tz_offset = gst_date_time_get_time_zone_offset (dt);
412 if (tz_offset != 0.0) {
413 g_snprintf (tz_str, sizeof (tz_str), "(UTC %s%gh)",
414 (tz_offset > 0.0) ? "+" : "", tz_offset);
416 g_snprintf (tz_str, sizeof (tz_str), "(UTC)");
419 str = g_strdup_printf ("%04u-%02u-%02u %02u:%02u:%02u %s",
420 gst_date_time_get_year (dt), gst_date_time_get_month (dt),
421 gst_date_time_get_day (dt), gst_date_time_get_hour (dt),
422 gst_date_time_get_minute (dt), gst_date_time_get_second (dt),
425 gst_date_time_unref (dt);
428 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
432 PRINT ("%16s: %s\n", gst_tag_get_nick (tag), str);
434 PRINT ("%16s: %s\n", "", str);
442 print_toc_entry (gpointer data, gpointer user_data)
444 GstTocEntry *entry = (GstTocEntry *) data;
445 const guint max_indent = 40;
446 const gchar spc[max_indent + 1] = " ";
447 const gchar *entry_types[] = { "chapter", "edition" };
448 guint indent = MIN (GPOINTER_TO_UINT (user_data), max_indent);
451 gst_toc_entry_get_start_stop (entry, &start, &stop);
453 PRINT ("%s%s:", &spc[max_indent - indent], entry_types[entry->type]);
454 if (GST_CLOCK_TIME_IS_VALID (start)) {
455 PRINT (" start: %" GST_TIME_FORMAT, GST_TIME_ARGS (start));
457 if (GST_CLOCK_TIME_IS_VALID (stop)) {
458 PRINT (" stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
463 /* TODO: print tags */
465 /* loop over sub-toc entries */
466 g_list_foreach (entry->subentries, print_toc_entry,
467 GUINT_TO_POINTER (indent));
470 #ifndef DISABLE_FAULT_HANDLER
471 /* we only use sighandler here because the registers are not important */
473 sigint_handler_sighandler (int signum)
475 PRINT ("Caught interrupt -- ");
477 /* If we were waiting for an EOS, we still want to catch
478 * the next signal to shutdown properly (and the following one
479 * will quit the program). */
485 /* we set a flag that is checked by the mainloop, we cannot do much in the
486 * interrupt handler (no mutex or other blocking stuff) */
490 /* is called every 250 milliseconds (4 times a second), the interrupt handler
491 * will set a flag for us. We react to this by posting a message. */
493 check_intr (GstElement * pipeline)
499 PRINT ("handling interrupt.\n");
501 /* post an application specific message */
502 gst_element_post_message (GST_ELEMENT (pipeline),
503 gst_message_new_application (GST_OBJECT (pipeline),
504 gst_structure_new ("GstLaunchInterrupt",
505 "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));
507 /* remove timeout handler */
515 struct sigaction action;
517 memset (&action, 0, sizeof (action));
518 action.sa_handler = sigint_handler_sighandler;
520 sigaction (SIGINT, &action, NULL);
524 sigint_restore (void)
526 struct sigaction action;
528 memset (&action, 0, sizeof (action));
529 action.sa_handler = SIG_DFL;
531 sigaction (SIGINT, &action, NULL);
533 #endif /* DISABLE_FAULT_HANDLER */
535 /* returns ELR_ERROR if there was an error
536 * or ELR_INTERRUPT if we caught a keyboard interrupt
537 * or ELR_NO_ERROR otherwise. */
538 static EventLoopResult
539 event_loop (GstElement * pipeline, gboolean blocking, GstState target_state)
541 #ifndef DISABLE_FAULT_HANDLER
545 GstMessage *message = NULL;
546 EventLoopResult res = ELR_NO_ERROR;
547 gboolean buffering = FALSE;
549 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
551 #ifndef DISABLE_FAULT_HANDLER
552 timeout_id = g_timeout_add (250, (GSourceFunc) check_intr, pipeline);
556 message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
558 /* if the poll timed out, only when !blocking */
562 /* check if we need to dump messages to the console */
565 const GstStructure *s;
568 seqnum = gst_message_get_seqnum (message);
570 s = gst_message_get_structure (message);
572 src_obj = GST_MESSAGE_SRC (message);
574 if (GST_IS_ELEMENT (src_obj)) {
575 PRINT (_("Got message #%u from element \"%s\" (%s): "),
576 (guint) seqnum, GST_ELEMENT_NAME (src_obj),
577 GST_MESSAGE_TYPE_NAME (message));
578 } else if (GST_IS_PAD (src_obj)) {
579 PRINT (_("Got message #%u from pad \"%s:%s\" (%s): "),
580 (guint) seqnum, GST_DEBUG_PAD_NAME (src_obj),
581 GST_MESSAGE_TYPE_NAME (message));
582 } else if (GST_IS_OBJECT (src_obj)) {
583 PRINT (_("Got message #%u from object \"%s\" (%s): "),
584 (guint) seqnum, GST_OBJECT_NAME (src_obj),
585 GST_MESSAGE_TYPE_NAME (message));
587 PRINT (_("Got message #%u (%s): "), (guint) seqnum,
588 GST_MESSAGE_TYPE_NAME (message));
594 sstr = gst_structure_to_string (s);
595 PRINT ("%s\n", sstr);
598 PRINT ("no message details\n");
602 switch (GST_MESSAGE_TYPE (message)) {
603 case GST_MESSAGE_NEW_CLOCK:
607 gst_message_parse_new_clock (message, &clock);
609 PRINT ("New clock: %s\n", (clock ? GST_OBJECT_NAME (clock) : "NULL"));
612 case GST_MESSAGE_CLOCK_LOST:
613 PRINT ("Clock lost, selecting a new one\n");
614 gst_element_set_state (pipeline, GST_STATE_PAUSED);
615 gst_element_set_state (pipeline, GST_STATE_PLAYING);
617 case GST_MESSAGE_EOS:{
619 PRINT (_("Got EOS from element \"%s\".\n"),
620 GST_MESSAGE_SRC_NAME (message));
623 case GST_MESSAGE_TAG:
625 GstTagList *tag_list;
627 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
628 PRINT (_("FOUND TAG : found by element \"%s\".\n"),
629 GST_MESSAGE_SRC_NAME (message));
630 } else if (GST_IS_PAD (GST_MESSAGE_SRC (message))) {
631 PRINT (_("FOUND TAG : found by pad \"%s:%s\".\n"),
632 GST_DEBUG_PAD_NAME (GST_MESSAGE_SRC (message)));
633 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
634 PRINT (_("FOUND TAG : found by object \"%s\".\n"),
635 GST_MESSAGE_SRC_NAME (message));
637 PRINT (_("FOUND TAG\n"));
640 gst_message_parse_tag (message, &tag_list);
641 gst_tag_list_foreach (tag_list, print_tag, NULL);
642 gst_tag_list_free (tag_list);
645 case GST_MESSAGE_TOC:
650 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
651 PRINT (_("FOUND TOC : found by element \"%s\".\n"),
652 GST_MESSAGE_SRC_NAME (message));
653 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
654 PRINT (_("FOUND TOC : found by object \"%s\".\n"),
655 GST_MESSAGE_SRC_NAME (message));
657 PRINT (_("FOUND TOC\n"));
660 gst_message_parse_toc (message, &toc_msg, &updated);
661 /* recursively loop over toc entries */
662 g_list_foreach (toc_msg->entries, print_toc_entry,
663 GUINT_TO_POINTER (0));
664 gst_toc_free (toc_msg);
667 case GST_MESSAGE_INFO:{
670 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
672 gst_message_parse_info (message, &gerror, &debug);
674 PRINT (_("INFO:\n%s\n"), debug);
676 g_error_free (gerror);
681 case GST_MESSAGE_WARNING:{
684 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
686 /* dump graph on warning */
687 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
688 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.warning");
690 gst_message_parse_warning (message, &gerror, &debug);
691 PRINT (_("WARNING: from element %s: %s\n"), name, gerror->message);
693 PRINT (_("Additional debug info:\n%s\n"), debug);
695 g_error_free (gerror);
700 case GST_MESSAGE_ERROR:{
701 /* dump graph on error */
702 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
703 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.error");
705 print_error_message (message);
707 /* we have an error */
711 case GST_MESSAGE_STATE_CHANGED:{
712 GstState old, new, pending;
714 /* we only care about pipeline state change messages */
715 if (GST_MESSAGE_SRC (message) != GST_OBJECT_CAST (pipeline))
718 /* ignore when we are buffering since then we mess with the states
721 PRINT (_("Prerolled, waiting for buffering to finish...\n"));
725 gst_message_parse_state_changed (message, &old, &new, &pending);
727 /* if we reached the final target state, exit */
728 if (target_state == GST_STATE_PAUSED && new == target_state)
731 /* else not an interesting message */
734 case GST_MESSAGE_BUFFERING:{
737 gst_message_parse_buffering (message, &percent);
738 PRINT ("%s %d%% \r", _("buffering..."), percent);
740 /* no state management needed for live pipelines */
744 if (percent == 100) {
745 /* a 100% message means buffering is done */
747 /* if the desired state is playing, go back */
748 if (target_state == GST_STATE_PLAYING) {
749 PRINT (_("Done buffering, setting pipeline to PLAYING ...\n"));
750 gst_element_set_state (pipeline, GST_STATE_PLAYING);
755 if (buffering == FALSE && target_state == GST_STATE_PLAYING) {
756 /* we were not buffering but PLAYING, PAUSE the pipeline. */
757 PRINT (_("Buffering, setting pipeline to PAUSED ...\n"));
758 gst_element_set_state (pipeline, GST_STATE_PAUSED);
764 case GST_MESSAGE_LATENCY:
766 PRINT (_("Redistribute latency...\n"));
767 gst_bin_recalculate_latency (GST_BIN (pipeline));
770 case GST_MESSAGE_REQUEST_STATE:
773 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
775 gst_message_parse_request_state (message, &state);
777 PRINT (_("Setting state to %s as requested by %s...\n"),
778 gst_element_state_get_name (state), name);
780 gst_element_set_state (pipeline, state);
785 case GST_MESSAGE_APPLICATION:{
786 const GstStructure *s;
788 s = gst_message_get_structure (message);
790 if (gst_structure_has_name (s, "GstLaunchInterrupt")) {
791 /* this application message is posted when we caught an interrupt and
792 * we need to stop the pipeline. */
793 PRINT (_("Interrupt: Stopping pipeline ...\n"));
799 case GST_MESSAGE_ELEMENT:{
800 if (gst_is_missing_plugin_message (message)) {
803 desc = gst_missing_plugin_message_get_description (message);
804 PRINT (_("Missing element: %s\n"), desc ? desc : "(no description)");
809 /* just be quiet by default */
813 gst_message_unref (message);
815 g_assert_not_reached ();
820 gst_message_unref (message);
821 gst_object_unref (bus);
822 #ifndef DISABLE_FAULT_HANDLER
823 g_source_remove (timeout_id);
829 static GstBusSyncReply
830 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer data)
832 GstElement *pipeline = (GstElement *) data;
834 switch (GST_MESSAGE_TYPE (message)) {
835 case GST_MESSAGE_STATE_CHANGED:
836 /* we only care about pipeline state change messages */
837 if (GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
838 GstState old, new, pending;
839 gchar *state_transition_name;
841 gst_message_parse_state_changed (message, &old, &new, &pending);
843 state_transition_name = g_strdup_printf ("%s_%s",
844 gst_element_state_get_name (old), gst_element_state_get_name (new));
846 /* dump graph for (some) pipeline state changes */
848 gchar *dump_name = g_strconcat ("gst-launch.", state_transition_name,
850 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
851 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
855 /* place a marker into e.g. strace logs */
857 gchar *access_name = g_strconcat (g_get_tmp_dir (), G_DIR_SEPARATOR_S,
858 "gst-launch", G_DIR_SEPARATOR_S, state_transition_name, NULL);
859 g_file_test (access_name, G_FILE_TEST_EXISTS);
860 g_free (access_name);
863 g_free (state_transition_name);
872 main (int argc, char *argv[])
875 gboolean verbose = FALSE;
876 gboolean no_fault = FALSE;
877 gboolean eos_on_shutdown = FALSE;
879 gboolean check_index = FALSE;
881 gchar *savefile = NULL;
882 gchar *exclude_args = NULL;
883 #ifndef GST_DISABLE_OPTION_PARSING
884 GOptionEntry options[] = {
885 {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
886 N_("Output tags (also known as metadata)"), NULL},
887 {"toc", 'c', 0, G_OPTION_ARG_NONE, &toc,
888 N_("Ouput TOC (chapters and editions)"), NULL},
889 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
890 N_("Output status information and property notifications"), NULL},
891 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
892 N_("Do not print any progress information"), NULL},
893 {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
894 N_("Output messages"), NULL},
895 {"exclude", 'X', 0, G_OPTION_ARG_NONE, &exclude_args,
896 N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
897 {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
898 N_("Do not install a fault handler"), NULL},
899 {"eos-on-shutdown", 'e', 0, G_OPTION_ARG_NONE, &eos_on_shutdown,
900 N_("Force EOS on sources before shutting the pipeline down"), NULL},
902 {"index", 'i', 0, G_OPTION_ARG_NONE, &check_index,
903 N_("Gather and print index statistics"), NULL},
905 GST_TOOLS_GOPTION_VERSION,
913 GPtrArray *index_stats = NULL;
916 GError *error = NULL;
919 free (malloc (8)); /* -lefence */
922 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
923 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
924 textdomain (GETTEXT_PACKAGE);
927 gst_tools_set_prgname ("gst-launch");
929 #ifndef GST_DISABLE_OPTION_PARSING
930 ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
931 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
932 g_option_context_add_group (ctx, gst_init_get_option_group ());
933 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
935 g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
937 g_printerr ("Error initializing: Unknown error!\n");
940 g_option_context_free (ctx);
942 gst_init (&argc, &argv);
945 gst_tools_print_version ("gst-launch");
947 #ifndef DISABLE_FAULT_HANDLER
954 /* make a null-terminated version of argv */
955 argvn = g_new0 (char *, argc);
956 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
959 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
965 g_printerr (_("ERROR: pipeline could not be constructed: %s.\n"),
966 GST_STR_NULL (error->message));
967 g_error_free (error);
969 g_printerr (_("ERROR: pipeline could not be constructed.\n"));
973 g_printerr (_("WARNING: erroneous pipeline: %s\n"),
974 GST_STR_NULL (error->message));
975 g_error_free (error);
980 gchar **exclude_list =
981 exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
982 g_signal_connect (pipeline, "deep-notify",
983 G_CALLBACK (gst_object_default_deep_notify), exclude_list);
987 GstState state, pending;
988 GstStateChangeReturn ret;
991 /* If the top-level object is not a pipeline, place it in a pipeline. */
992 if (!GST_IS_PIPELINE (pipeline)) {
993 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
995 if (real_pipeline == NULL) {
996 g_printerr (_("ERROR: the 'pipeline' element wasn't found.\n"));
999 gst_bin_add (GST_BIN (real_pipeline), pipeline);
1000 pipeline = real_pipeline;
1004 /* gst_index_new() creates a null-index, it does not store anything, but
1005 * the entry-added signal works and this is what we use to build the
1007 index = gst_index_new ();
1009 index_stats = g_ptr_array_new ();
1010 g_signal_connect (G_OBJECT (index), "entry-added",
1011 G_CALLBACK (entry_added), index_stats);
1012 g_object_set (G_OBJECT (index), "resolver", GST_INDEX_RESOLVER_GTYPE,
1014 gst_element_set_index (pipeline, index);
1019 bus = gst_element_get_bus (pipeline);
1020 gst_bus_set_sync_handler (bus, bus_sync_handler, (gpointer) pipeline);
1021 gst_object_unref (bus);
1023 PRINT (_("Setting pipeline to PAUSED ...\n"));
1024 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
1027 case GST_STATE_CHANGE_FAILURE:
1028 g_printerr (_("ERROR: Pipeline doesn't want to pause.\n"));
1030 event_loop (pipeline, FALSE, GST_STATE_VOID_PENDING);
1032 case GST_STATE_CHANGE_NO_PREROLL:
1033 PRINT (_("Pipeline is live and does not need PREROLL ...\n"));
1036 case GST_STATE_CHANGE_ASYNC:
1037 PRINT (_("Pipeline is PREROLLING ...\n"));
1038 caught_error = event_loop (pipeline, TRUE, GST_STATE_PAUSED);
1040 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1043 state = GST_STATE_PAUSED;
1045 case GST_STATE_CHANGE_SUCCESS:
1046 PRINT (_("Pipeline is PREROLLED ...\n"));
1050 caught_error = event_loop (pipeline, FALSE, GST_STATE_PLAYING);
1053 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1055 GstClockTime tfthen, tfnow;
1056 GstClockTimeDiff diff;
1058 PRINT (_("Setting pipeline to PLAYING ...\n"));
1060 if (gst_element_set_state (pipeline,
1061 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
1062 GstMessage *err_msg;
1065 g_printerr (_("ERROR: pipeline doesn't want to play.\n"));
1066 bus = gst_element_get_bus (pipeline);
1067 if ((err_msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0))) {
1068 print_error_message (err_msg);
1069 gst_message_unref (err_msg);
1071 gst_object_unref (bus);
1076 tfthen = gst_util_get_timestamp ();
1077 caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
1078 if (eos_on_shutdown && caught_error == ELR_INTERRUPT) {
1079 PRINT (_("EOS on shutdown enabled -- Forcing EOS on the pipeline\n"));
1081 gst_element_send_event (pipeline, gst_event_new_eos ());
1082 PRINT (_("Waiting for EOS...\n"));
1083 caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
1085 if (caught_error == ELR_NO_ERROR) {
1087 PRINT (_("EOS received - stopping pipeline...\n"));
1088 } else if (caught_error == ELR_ERROR) {
1089 PRINT (_("An error happened while waiting for EOS\n"));
1092 tfnow = gst_util_get_timestamp ();
1094 diff = GST_CLOCK_DIFF (tfthen, tfnow);
1096 PRINT (_("Execution ended after %" G_GUINT64_FORMAT " ns.\n"), diff);
1099 PRINT (_("Setting pipeline to PAUSED ...\n"));
1100 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1101 if (caught_error == ELR_NO_ERROR)
1102 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1104 /* iterate mainloop to process pending stuff */
1105 while (g_main_context_iteration (NULL, FALSE));
1107 PRINT (_("Setting pipeline to READY ...\n"));
1108 gst_element_set_state (pipeline, GST_STATE_READY);
1109 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1113 print_index_stats (index_stats);
1114 g_ptr_array_free (index_stats, TRUE);
1119 PRINT (_("Setting pipeline to NULL ...\n"));
1120 gst_element_set_state (pipeline, GST_STATE_NULL);
1121 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1124 PRINT (_("Freeing pipeline ...\n"));
1125 gst_object_unref (pipeline);