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-" GST_API_VERSION);
143 /* FIXME how do we know if we were run by libtool? */
145 "Spinning. Please run 'gdb gst-launch- " GST_API_VERSION " %d' to "
146 "continue debugging, Ctrl-C to quit, or Ctrl-\\ to dump core.\n",
155 struct sigaction action;
157 memset (&action, 0, sizeof (action));
158 action.sa_handler = SIG_DFL;
160 sigaction (SIGSEGV, &action, NULL);
161 sigaction (SIGQUIT, &action, NULL);
167 struct sigaction action;
169 memset (&action, 0, sizeof (action));
171 action.sa_sigaction = fault_handler_sigaction;
172 action.sa_flags = SA_SIGINFO;
174 action.sa_handler = fault_handler_sighandler;
177 sigaction (SIGSEGV, &action, NULL);
178 sigaction (SIGQUIT, &action, NULL);
180 #endif /* DISABLE_FAULT_HANDLER */
183 typedef struct _GstIndexStats
191 GstClockTime last_keyframe;
192 GstClockTime last_dltframe;
193 GstClockTime min_keyframe_gap;
194 GstClockTime max_keyframe_gap;
195 GstClockTime avg_keyframe_gap;
199 entry_added (GstIndex * index, GstIndexEntry * entry, gpointer user_data)
201 GPtrArray *index_stats = (GPtrArray *) user_data;
204 switch (entry->type) {
205 case GST_INDEX_ENTRY_ID:
206 /* we have a new writer */
207 GST_DEBUG_OBJECT (index, "id %d: describes writer %s", entry->id,
208 GST_INDEX_ID_DESCRIPTION (entry));
209 if (entry->id >= index_stats->len) {
210 g_ptr_array_set_size (index_stats, entry->id + 1);
212 s = g_new (GstIndexStats, 1);
214 s->desc = g_strdup (GST_INDEX_ID_DESCRIPTION (entry));
215 s->num_frames = s->num_keyframes = s->num_dltframes = 0;
216 s->last_keyframe = s->last_dltframe = GST_CLOCK_TIME_NONE;
217 s->min_keyframe_gap = s->max_keyframe_gap = s->avg_keyframe_gap =
219 g_ptr_array_index (index_stats, entry->id) = s;
221 case GST_INDEX_ENTRY_FORMAT:
222 /* have not found any code calling this */
223 GST_DEBUG_OBJECT (index, "id %d: registered format %d for %s\n",
224 entry->id, GST_INDEX_FORMAT_FORMAT (entry),
225 GST_INDEX_FORMAT_KEY (entry));
227 case GST_INDEX_ENTRY_ASSOCIATION:
230 GstAssocFlags flags = GST_INDEX_ASSOC_FLAGS (entry);
232 s = g_ptr_array_index (index_stats, entry->id);
233 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
235 if (flags & GST_ASSOCIATION_FLAG_KEY_UNIT) {
238 if (GST_CLOCK_TIME_IS_VALID (ts)) {
239 if (GST_CLOCK_TIME_IS_VALID (s->last_keyframe)) {
240 GstClockTimeDiff d = GST_CLOCK_DIFF (s->last_keyframe, ts);
242 if (G_UNLIKELY (d < 0)) {
243 GST_WARNING ("received out-of-order keyframe at %"
244 GST_TIME_FORMAT, GST_TIME_ARGS (ts));
245 /* FIXME: does it still make sense to use that for the statistics */
246 d = GST_CLOCK_DIFF (ts, s->last_keyframe);
249 if (GST_CLOCK_TIME_IS_VALID (s->min_keyframe_gap)) {
250 if (d < s->min_keyframe_gap)
251 s->min_keyframe_gap = d;
253 s->min_keyframe_gap = d;
255 if (GST_CLOCK_TIME_IS_VALID (s->max_keyframe_gap)) {
256 if (d > s->max_keyframe_gap)
257 s->max_keyframe_gap = d;
259 s->max_keyframe_gap = d;
261 if (GST_CLOCK_TIME_IS_VALID (s->avg_keyframe_gap)) {
262 s->avg_keyframe_gap = (d + s->num_frames * s->avg_keyframe_gap) /
265 s->avg_keyframe_gap = d;
268 s->last_keyframe = ts;
271 if (flags & GST_ASSOCIATION_FLAG_DELTA_UNIT) {
273 if (GST_CLOCK_TIME_IS_VALID (ts)) {
274 s->last_dltframe = ts;
286 /* print statistics from the entry_added callback, free the entries */
288 print_index_stats (GPtrArray * index_stats)
292 if (index_stats->len) {
293 g_print ("%s:\n", _("Index statistics"));
296 for (i = 0; i < index_stats->len; i++) {
297 GstIndexStats *s = g_ptr_array_index (index_stats, i);
299 g_print ("id %d, %s\n", s->id, s->desc);
301 GstClockTime last_frame = s->last_keyframe;
303 if (GST_CLOCK_TIME_IS_VALID (s->last_dltframe)) {
304 if (!GST_CLOCK_TIME_IS_VALID (last_frame) ||
305 (s->last_dltframe > last_frame))
306 last_frame = s->last_dltframe;
309 if (GST_CLOCK_TIME_IS_VALID (last_frame)) {
310 g_print (" total time = %" GST_TIME_FORMAT "\n",
311 GST_TIME_ARGS (last_frame));
313 g_print (" frame/keyframe rate = %u / %u = ", s->num_frames,
315 if (s->num_keyframes)
316 g_print ("%lf\n", s->num_frames / (gdouble) s->num_keyframes);
319 if (s->num_keyframes) {
320 g_print (" min/avg/max keyframe gap = %" GST_TIME_FORMAT ", %"
321 GST_TIME_FORMAT ", %" GST_TIME_FORMAT "\n",
322 GST_TIME_ARGS (s->min_keyframe_gap),
323 GST_TIME_ARGS (s->avg_keyframe_gap),
324 GST_TIME_ARGS (s->max_keyframe_gap));
327 g_print (" no stats\n");
337 /* Kids, use the functions from libgstpbutils in gst-plugins-base in your
338 * own code (we can't do that here because it would introduce a circular
341 gst_is_missing_plugin_message (GstMessage * msg)
343 if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ELEMENT
344 || gst_message_get_structure (msg) == NULL)
347 return gst_structure_has_name (gst_message_get_structure (msg),
352 gst_missing_plugin_message_get_description (GstMessage * msg)
354 return gst_structure_get_string (gst_message_get_structure (msg), "name");
358 print_error_message (GstMessage * msg)
361 gchar *name, *debug = NULL;
363 name = gst_object_get_path_string (msg->src);
364 gst_message_parse_error (msg, &err, &debug);
366 g_printerr (_("ERROR: from element %s: %s\n"), name, err->message);
368 g_printerr (_("Additional debug info:\n%s\n"), debug);
376 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
380 count = gst_tag_list_get_tag_size (list, tag);
382 for (i = 0; i < count; i++) {
385 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
386 if (!gst_tag_list_get_string_index (list, tag, i, &str))
387 g_assert_not_reached ();
388 } else if (gst_tag_get_type (tag) == GST_TYPE_BUFFER) {
391 img = gst_value_get_buffer (gst_tag_list_get_value_index (list, tag, i));
395 caps_str = g_strdup ("unknown");
396 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes, type: %s",
397 gst_buffer_get_size (img), caps_str);
400 str = g_strdup ("NULL buffer");
402 } else if (gst_tag_get_type (tag) == GST_TYPE_DATE_TIME) {
403 GstDateTime *dt = NULL;
405 gst_tag_list_get_date_time_index (list, tag, i, &dt);
406 if (gst_date_time_get_hour (dt) < 0) {
407 str = g_strdup_printf ("%02u-%02u-%04u", gst_date_time_get_day (dt),
408 gst_date_time_get_month (dt), gst_date_time_get_year (dt));
410 gdouble tz_offset = gst_date_time_get_time_zone_offset (dt);
413 if (tz_offset != 0.0) {
414 g_snprintf (tz_str, sizeof (tz_str), "(UTC %s%gh)",
415 (tz_offset > 0.0) ? "+" : "", tz_offset);
417 g_snprintf (tz_str, sizeof (tz_str), "(UTC)");
420 str = g_strdup_printf ("%04u-%02u-%02u %02u:%02u:%02u %s",
421 gst_date_time_get_year (dt), gst_date_time_get_month (dt),
422 gst_date_time_get_day (dt), gst_date_time_get_hour (dt),
423 gst_date_time_get_minute (dt), gst_date_time_get_second (dt),
426 gst_date_time_unref (dt);
429 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
433 PRINT ("%16s: %s\n", gst_tag_get_nick (tag), str);
435 PRINT ("%16s: %s\n", "", str);
443 print_tag_foreach (const GstTagList * tags, const gchar * tag,
448 gint depth = GPOINTER_TO_INT (user_data);
450 gst_tag_list_copy_value (&val, tags, tag);
452 if (G_VALUE_HOLDS_STRING (&val))
453 str = g_value_dup_string (&val);
455 str = gst_value_serialize (&val);
457 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
460 g_value_unset (&val);
463 #define MAX_INDENT 40
466 print_toc_entry (gpointer data, gpointer user_data)
468 GstTocEntry *entry = (GstTocEntry *) data;
469 const gchar spc[MAX_INDENT + 1] = " ";
470 guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
473 gst_toc_entry_get_start_stop (entry, &start, &stop);
475 PRINT ("%s%s:", &spc[MAX_INDENT - indent],
476 gst_toc_entry_type_get_nick (entry->type));
477 if (GST_CLOCK_TIME_IS_VALID (start)) {
478 PRINT (" start: %" GST_TIME_FORMAT, GST_TIME_ARGS (start));
480 if (GST_CLOCK_TIME_IS_VALID (stop)) {
481 PRINT (" stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
487 gst_tag_list_foreach (entry->tags, print_tag_foreach,
488 GUINT_TO_POINTER (indent));
490 /* loop over sub-toc entries */
491 g_list_foreach (entry->subentries, print_toc_entry,
492 GUINT_TO_POINTER (indent));
495 #ifndef DISABLE_FAULT_HANDLER
496 /* we only use sighandler here because the registers are not important */
498 sigint_handler_sighandler (int signum)
500 PRINT ("Caught interrupt -- ");
502 /* If we were waiting for an EOS, we still want to catch
503 * the next signal to shutdown properly (and the following one
504 * will quit the program). */
510 /* we set a flag that is checked by the mainloop, we cannot do much in the
511 * interrupt handler (no mutex or other blocking stuff) */
515 /* is called every 250 milliseconds (4 times a second), the interrupt handler
516 * will set a flag for us. We react to this by posting a message. */
518 check_intr (GstElement * pipeline)
524 PRINT ("handling interrupt.\n");
526 /* post an application specific message */
527 gst_element_post_message (GST_ELEMENT (pipeline),
528 gst_message_new_application (GST_OBJECT (pipeline),
529 gst_structure_new ("GstLaunchInterrupt",
530 "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));
532 /* remove timeout handler */
540 struct sigaction action;
542 memset (&action, 0, sizeof (action));
543 action.sa_handler = sigint_handler_sighandler;
545 sigaction (SIGINT, &action, NULL);
549 sigint_restore (void)
551 struct sigaction action;
553 memset (&action, 0, sizeof (action));
554 action.sa_handler = SIG_DFL;
556 sigaction (SIGINT, &action, NULL);
558 #endif /* DISABLE_FAULT_HANDLER */
560 /* returns ELR_ERROR if there was an error
561 * or ELR_INTERRUPT if we caught a keyboard interrupt
562 * or ELR_NO_ERROR otherwise. */
563 static EventLoopResult
564 event_loop (GstElement * pipeline, gboolean blocking, GstState target_state)
566 #ifndef DISABLE_FAULT_HANDLER
570 GstMessage *message = NULL;
571 EventLoopResult res = ELR_NO_ERROR;
572 gboolean buffering = FALSE;
574 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
576 #ifndef DISABLE_FAULT_HANDLER
577 timeout_id = g_timeout_add (250, (GSourceFunc) check_intr, pipeline);
581 message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
583 /* if the poll timed out, only when !blocking */
587 /* check if we need to dump messages to the console */
590 const GstStructure *s;
593 seqnum = gst_message_get_seqnum (message);
595 s = gst_message_get_structure (message);
597 src_obj = GST_MESSAGE_SRC (message);
599 if (GST_IS_ELEMENT (src_obj)) {
600 PRINT (_("Got message #%u from element \"%s\" (%s): "),
601 (guint) seqnum, GST_ELEMENT_NAME (src_obj),
602 GST_MESSAGE_TYPE_NAME (message));
603 } else if (GST_IS_PAD (src_obj)) {
604 PRINT (_("Got message #%u from pad \"%s:%s\" (%s): "),
605 (guint) seqnum, GST_DEBUG_PAD_NAME (src_obj),
606 GST_MESSAGE_TYPE_NAME (message));
607 } else if (GST_IS_OBJECT (src_obj)) {
608 PRINT (_("Got message #%u from object \"%s\" (%s): "),
609 (guint) seqnum, GST_OBJECT_NAME (src_obj),
610 GST_MESSAGE_TYPE_NAME (message));
612 PRINT (_("Got message #%u (%s): "), (guint) seqnum,
613 GST_MESSAGE_TYPE_NAME (message));
619 sstr = gst_structure_to_string (s);
620 PRINT ("%s\n", sstr);
623 PRINT ("no message details\n");
627 switch (GST_MESSAGE_TYPE (message)) {
628 case GST_MESSAGE_NEW_CLOCK:
632 gst_message_parse_new_clock (message, &clock);
634 PRINT ("New clock: %s\n", (clock ? GST_OBJECT_NAME (clock) : "NULL"));
637 case GST_MESSAGE_CLOCK_LOST:
638 PRINT ("Clock lost, selecting a new one\n");
639 gst_element_set_state (pipeline, GST_STATE_PAUSED);
640 gst_element_set_state (pipeline, GST_STATE_PLAYING);
642 case GST_MESSAGE_EOS:{
644 PRINT (_("Got EOS from element \"%s\".\n"),
645 GST_MESSAGE_SRC_NAME (message));
648 case GST_MESSAGE_TAG:
650 GstTagList *tag_list;
652 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
653 PRINT (_("FOUND TAG : found by element \"%s\".\n"),
654 GST_MESSAGE_SRC_NAME (message));
655 } else if (GST_IS_PAD (GST_MESSAGE_SRC (message))) {
656 PRINT (_("FOUND TAG : found by pad \"%s:%s\".\n"),
657 GST_DEBUG_PAD_NAME (GST_MESSAGE_SRC (message)));
658 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
659 PRINT (_("FOUND TAG : found by object \"%s\".\n"),
660 GST_MESSAGE_SRC_NAME (message));
662 PRINT (_("FOUND TAG\n"));
665 gst_message_parse_tag (message, &tag_list);
666 gst_tag_list_foreach (tag_list, print_tag, NULL);
667 gst_tag_list_unref (tag_list);
670 case GST_MESSAGE_TOC:
675 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
676 PRINT (_("FOUND TOC : found by element \"%s\".\n"),
677 GST_MESSAGE_SRC_NAME (message));
678 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
679 PRINT (_("FOUND TOC : found by object \"%s\".\n"),
680 GST_MESSAGE_SRC_NAME (message));
682 PRINT (_("FOUND TOC\n"));
685 gst_message_parse_toc (message, &toc_msg, &updated);
686 /* recursively loop over toc entries */
687 g_list_foreach (toc_msg->entries, print_toc_entry,
688 GUINT_TO_POINTER (0));
689 gst_toc_unref (toc_msg);
692 case GST_MESSAGE_INFO:{
695 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
697 gst_message_parse_info (message, &gerror, &debug);
699 PRINT (_("INFO:\n%s\n"), debug);
701 g_error_free (gerror);
706 case GST_MESSAGE_WARNING:{
709 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
711 /* dump graph on warning */
712 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
713 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.warning");
715 gst_message_parse_warning (message, &gerror, &debug);
716 PRINT (_("WARNING: from element %s: %s\n"), name, gerror->message);
718 PRINT (_("Additional debug info:\n%s\n"), debug);
720 g_error_free (gerror);
725 case GST_MESSAGE_ERROR:{
726 /* dump graph on error */
727 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
728 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.error");
730 print_error_message (message);
732 /* we have an error */
736 case GST_MESSAGE_STATE_CHANGED:{
737 GstState old, new, pending;
739 /* we only care about pipeline state change messages */
740 if (GST_MESSAGE_SRC (message) != GST_OBJECT_CAST (pipeline))
743 /* ignore when we are buffering since then we mess with the states
746 PRINT (_("Prerolled, waiting for buffering to finish...\n"));
750 gst_message_parse_state_changed (message, &old, &new, &pending);
752 /* if we reached the final target state, exit */
753 if (target_state == GST_STATE_PAUSED && new == target_state)
756 /* else not an interesting message */
759 case GST_MESSAGE_BUFFERING:{
762 gst_message_parse_buffering (message, &percent);
763 PRINT ("%s %d%% \r", _("buffering..."), percent);
765 /* no state management needed for live pipelines */
769 if (percent == 100) {
770 /* a 100% message means buffering is done */
772 /* if the desired state is playing, go back */
773 if (target_state == GST_STATE_PLAYING) {
774 PRINT (_("Done buffering, setting pipeline to PLAYING ...\n"));
775 gst_element_set_state (pipeline, GST_STATE_PLAYING);
780 if (buffering == FALSE && target_state == GST_STATE_PLAYING) {
781 /* we were not buffering but PLAYING, PAUSE the pipeline. */
782 PRINT (_("Buffering, setting pipeline to PAUSED ...\n"));
783 gst_element_set_state (pipeline, GST_STATE_PAUSED);
789 case GST_MESSAGE_LATENCY:
791 PRINT (_("Redistribute latency...\n"));
792 gst_bin_recalculate_latency (GST_BIN (pipeline));
795 case GST_MESSAGE_REQUEST_STATE:
798 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
800 gst_message_parse_request_state (message, &state);
802 PRINT (_("Setting state to %s as requested by %s...\n"),
803 gst_element_state_get_name (state), name);
805 gst_element_set_state (pipeline, state);
810 case GST_MESSAGE_APPLICATION:{
811 const GstStructure *s;
813 s = gst_message_get_structure (message);
815 if (gst_structure_has_name (s, "GstLaunchInterrupt")) {
816 /* this application message is posted when we caught an interrupt and
817 * we need to stop the pipeline. */
818 PRINT (_("Interrupt: Stopping pipeline ...\n"));
824 case GST_MESSAGE_ELEMENT:{
825 if (gst_is_missing_plugin_message (message)) {
828 desc = gst_missing_plugin_message_get_description (message);
829 PRINT (_("Missing element: %s\n"), desc ? desc : "(no description)");
834 /* just be quiet by default */
838 gst_message_unref (message);
840 g_assert_not_reached ();
845 gst_message_unref (message);
846 gst_object_unref (bus);
847 #ifndef DISABLE_FAULT_HANDLER
848 g_source_remove (timeout_id);
854 static GstBusSyncReply
855 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer data)
857 GstElement *pipeline = (GstElement *) data;
859 switch (GST_MESSAGE_TYPE (message)) {
860 case GST_MESSAGE_STATE_CHANGED:
861 /* we only care about pipeline state change messages */
862 if (GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
863 GstState old, new, pending;
864 gchar *state_transition_name;
866 gst_message_parse_state_changed (message, &old, &new, &pending);
868 state_transition_name = g_strdup_printf ("%s_%s",
869 gst_element_state_get_name (old), gst_element_state_get_name (new));
871 /* dump graph for (some) pipeline state changes */
873 gchar *dump_name = g_strconcat ("gst-launch.", state_transition_name,
875 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
876 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
880 /* place a marker into e.g. strace logs */
882 gchar *access_name = g_strconcat (g_get_tmp_dir (), G_DIR_SEPARATOR_S,
883 "gst-launch", G_DIR_SEPARATOR_S, state_transition_name, NULL);
884 g_file_test (access_name, G_FILE_TEST_EXISTS);
885 g_free (access_name);
888 g_free (state_transition_name);
897 main (int argc, char *argv[])
900 gboolean verbose = FALSE;
901 gboolean no_fault = FALSE;
902 gboolean eos_on_shutdown = FALSE;
904 gboolean check_index = FALSE;
906 gchar *savefile = NULL;
907 gchar *exclude_args = NULL;
908 #ifndef GST_DISABLE_OPTION_PARSING
909 GOptionEntry options[] = {
910 {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
911 N_("Output tags (also known as metadata)"), NULL},
912 {"toc", 'c', 0, G_OPTION_ARG_NONE, &toc,
913 N_("Output TOC (chapters and editions)"), NULL},
914 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
915 N_("Output status information and property notifications"), NULL},
916 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
917 N_("Do not print any progress information"), NULL},
918 {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
919 N_("Output messages"), NULL},
920 {"exclude", 'X', 0, G_OPTION_ARG_NONE, &exclude_args,
921 N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
922 {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
923 N_("Do not install a fault handler"), NULL},
924 {"eos-on-shutdown", 'e', 0, G_OPTION_ARG_NONE, &eos_on_shutdown,
925 N_("Force EOS on sources before shutting the pipeline down"), NULL},
927 {"index", 'i', 0, G_OPTION_ARG_NONE, &check_index,
928 N_("Gather and print index statistics"), NULL},
930 GST_TOOLS_GOPTION_VERSION,
938 GPtrArray *index_stats = NULL;
941 GError *error = NULL;
944 free (malloc (8)); /* -lefence */
947 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
948 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
949 textdomain (GETTEXT_PACKAGE);
952 gst_tools_set_prgname ("gst-launch");
954 #ifndef GST_DISABLE_OPTION_PARSING
955 ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
956 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
957 g_option_context_add_group (ctx, gst_init_get_option_group ());
958 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
960 g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
962 g_printerr ("Error initializing: Unknown error!\n");
965 g_option_context_free (ctx);
967 gst_init (&argc, &argv);
970 gst_tools_print_version ("gst-launch");
972 #ifndef DISABLE_FAULT_HANDLER
979 /* make a null-terminated version of argv */
980 argvn = g_new0 (char *, argc);
981 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
984 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
990 g_printerr (_("ERROR: pipeline could not be constructed: %s.\n"),
991 GST_STR_NULL (error->message));
992 g_error_free (error);
994 g_printerr (_("ERROR: pipeline could not be constructed.\n"));
998 g_printerr (_("WARNING: erroneous pipeline: %s\n"),
999 GST_STR_NULL (error->message));
1000 g_error_free (error);
1005 gchar **exclude_list =
1006 exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
1007 g_signal_connect (pipeline, "deep-notify",
1008 G_CALLBACK (gst_object_default_deep_notify), exclude_list);
1012 GstState state, pending;
1013 GstStateChangeReturn ret;
1016 /* If the top-level object is not a pipeline, place it in a pipeline. */
1017 if (!GST_IS_PIPELINE (pipeline)) {
1018 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
1020 if (real_pipeline == NULL) {
1021 g_printerr (_("ERROR: the 'pipeline' element wasn't found.\n"));
1024 gst_bin_add (GST_BIN (real_pipeline), pipeline);
1025 pipeline = real_pipeline;
1029 /* gst_index_new() creates a null-index, it does not store anything, but
1030 * the entry-added signal works and this is what we use to build the
1032 index = gst_index_new ();
1034 index_stats = g_ptr_array_new ();
1035 g_signal_connect (G_OBJECT (index), "entry-added",
1036 G_CALLBACK (entry_added), index_stats);
1037 g_object_set (G_OBJECT (index), "resolver", GST_INDEX_RESOLVER_GTYPE,
1039 gst_element_set_index (pipeline, index);
1044 bus = gst_element_get_bus (pipeline);
1045 gst_bus_set_sync_handler (bus, bus_sync_handler, (gpointer) pipeline, NULL);
1046 gst_object_unref (bus);
1048 PRINT (_("Setting pipeline to PAUSED ...\n"));
1049 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
1052 case GST_STATE_CHANGE_FAILURE:
1053 g_printerr (_("ERROR: Pipeline doesn't want to pause.\n"));
1055 event_loop (pipeline, FALSE, GST_STATE_VOID_PENDING);
1057 case GST_STATE_CHANGE_NO_PREROLL:
1058 PRINT (_("Pipeline is live and does not need PREROLL ...\n"));
1061 case GST_STATE_CHANGE_ASYNC:
1062 PRINT (_("Pipeline is PREROLLING ...\n"));
1063 caught_error = event_loop (pipeline, TRUE, GST_STATE_PAUSED);
1065 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1068 state = GST_STATE_PAUSED;
1070 case GST_STATE_CHANGE_SUCCESS:
1071 PRINT (_("Pipeline is PREROLLED ...\n"));
1075 caught_error = event_loop (pipeline, FALSE, GST_STATE_PLAYING);
1078 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1080 GstClockTime tfthen, tfnow;
1081 GstClockTimeDiff diff;
1083 PRINT (_("Setting pipeline to PLAYING ...\n"));
1085 if (gst_element_set_state (pipeline,
1086 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
1087 GstMessage *err_msg;
1090 g_printerr (_("ERROR: pipeline doesn't want to play.\n"));
1091 bus = gst_element_get_bus (pipeline);
1092 if ((err_msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0))) {
1093 print_error_message (err_msg);
1094 gst_message_unref (err_msg);
1096 gst_object_unref (bus);
1101 tfthen = gst_util_get_timestamp ();
1102 caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
1103 if (eos_on_shutdown && caught_error != ELR_NO_ERROR) {
1104 gboolean ignore_errors;
1107 if (caught_error == ELR_INTERRUPT) {
1108 PRINT (_("EOS on shutdown enabled -- Forcing EOS on the pipeline\n"));
1109 gst_element_send_event (pipeline, gst_event_new_eos ());
1110 ignore_errors = FALSE;
1112 PRINT (_("EOS on shutdown enabled -- waiting for EOS after Error\n"));
1113 ignore_errors = TRUE;
1115 PRINT (_("Waiting for EOS...\n"));
1118 caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
1120 if (caught_error == ELR_NO_ERROR) {
1122 PRINT (_("EOS received - stopping pipeline...\n"));
1124 } else if (caught_error == ELR_INTERRUPT) {
1126 ("Interrupt while waiting for EOS - stopping pipeline...\n"));
1128 } else if (caught_error == ELR_ERROR) {
1129 if (!ignore_errors) {
1130 PRINT (_("An error happened while waiting for EOS\n"));
1136 tfnow = gst_util_get_timestamp ();
1138 diff = GST_CLOCK_DIFF (tfthen, tfnow);
1140 PRINT (_("Execution ended after %" G_GUINT64_FORMAT " ns.\n"), diff);
1143 PRINT (_("Setting pipeline to PAUSED ...\n"));
1144 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1145 if (caught_error == ELR_NO_ERROR)
1146 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1148 /* iterate mainloop to process pending stuff */
1149 while (g_main_context_iteration (NULL, FALSE));
1151 PRINT (_("Setting pipeline to READY ...\n"));
1152 gst_element_set_state (pipeline, GST_STATE_READY);
1153 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1157 print_index_stats (index_stats);
1158 g_ptr_array_free (index_stats, TRUE);
1163 PRINT (_("Setting pipeline to NULL ...\n"));
1164 gst_element_set_state (pipeline, GST_STATE_NULL);
1167 PRINT (_("Freeing pipeline ...\n"));
1168 gst_object_unref (pipeline);