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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, 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_SAMPLE) {
389 GstSample *sample = NULL;
391 if (gst_tag_list_get_sample_index (list, tag, i, &sample)) {
392 GstBuffer *img = gst_sample_get_buffer (sample);
393 GstCaps *caps = gst_sample_get_caps (sample);
399 caps_str = gst_caps_to_string (caps);
400 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes, "
401 "type: %s", gst_buffer_get_size (img), caps_str);
404 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes",
405 gst_buffer_get_size (img));
408 str = g_strdup ("NULL buffer");
411 } else if (gst_tag_get_type (tag) == GST_TYPE_DATE_TIME) {
412 GstDateTime *dt = NULL;
414 gst_tag_list_get_date_time_index (list, tag, i, &dt);
415 if (!gst_date_time_has_time (dt)) {
416 str = gst_date_time_to_iso8601_string (dt);
418 gdouble tz_offset = gst_date_time_get_time_zone_offset (dt);
421 if (tz_offset != 0.0) {
422 g_snprintf (tz_str, sizeof (tz_str), "(UTC %s%gh)",
423 (tz_offset > 0.0) ? "+" : "", tz_offset);
425 g_snprintf (tz_str, sizeof (tz_str), "(UTC)");
428 str = g_strdup_printf ("%04u-%02u-%02u %02u:%02u:%02u %s",
429 gst_date_time_get_year (dt), gst_date_time_get_month (dt),
430 gst_date_time_get_day (dt), gst_date_time_get_hour (dt),
431 gst_date_time_get_minute (dt), gst_date_time_get_second (dt),
434 gst_date_time_unref (dt);
437 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
441 PRINT ("%16s: %s\n", gst_tag_get_nick (tag), str);
443 PRINT ("%16s: %s\n", "", str);
451 print_tag_foreach (const GstTagList * tags, const gchar * tag,
456 gint depth = GPOINTER_TO_INT (user_data);
458 gst_tag_list_copy_value (&val, tags, tag);
460 if (G_VALUE_HOLDS_STRING (&val))
461 str = g_value_dup_string (&val);
463 str = gst_value_serialize (&val);
465 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
468 g_value_unset (&val);
471 #define MAX_INDENT 40
474 print_toc_entry (gpointer data, gpointer user_data)
476 GstTocEntry *entry = (GstTocEntry *) data;
477 const gchar spc[MAX_INDENT + 1] = " ";
478 guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
479 const GstTagList *tags;
483 gst_toc_entry_get_start_stop_times (entry, &start, &stop);
485 PRINT ("%s%s:", &spc[MAX_INDENT - indent],
486 gst_toc_entry_type_get_nick (gst_toc_entry_get_entry_type (entry)));
487 if (GST_CLOCK_TIME_IS_VALID (start)) {
488 PRINT (" start: %" GST_TIME_FORMAT, GST_TIME_ARGS (start));
490 if (GST_CLOCK_TIME_IS_VALID (stop)) {
491 PRINT (" stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
497 tags = gst_toc_entry_get_tags (entry);
499 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (indent));
501 /* loop over sub-toc entries */
502 subentries = gst_toc_entry_get_sub_entries (entry);
503 g_list_foreach (subentries, print_toc_entry, GUINT_TO_POINTER (indent));
506 #ifndef DISABLE_FAULT_HANDLER
507 /* we only use sighandler here because the registers are not important */
509 sigint_handler_sighandler (int signum)
511 PRINT ("Caught interrupt -- ");
513 /* If we were waiting for an EOS, we still want to catch
514 * the next signal to shutdown properly (and the following one
515 * will quit the program). */
521 /* we set a flag that is checked by the mainloop, we cannot do much in the
522 * interrupt handler (no mutex or other blocking stuff) */
526 /* is called every 250 milliseconds (4 times a second), the interrupt handler
527 * will set a flag for us. We react to this by posting a message. */
529 check_intr (GstElement * pipeline)
535 PRINT ("handling interrupt.\n");
537 /* post an application specific message */
538 gst_element_post_message (GST_ELEMENT (pipeline),
539 gst_message_new_application (GST_OBJECT (pipeline),
540 gst_structure_new ("GstLaunchInterrupt",
541 "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));
543 /* remove timeout handler */
551 struct sigaction action;
553 memset (&action, 0, sizeof (action));
554 action.sa_handler = sigint_handler_sighandler;
556 sigaction (SIGINT, &action, NULL);
560 sigint_restore (void)
562 struct sigaction action;
564 memset (&action, 0, sizeof (action));
565 action.sa_handler = SIG_DFL;
567 sigaction (SIGINT, &action, NULL);
569 #endif /* DISABLE_FAULT_HANDLER */
571 /* returns ELR_ERROR if there was an error
572 * or ELR_INTERRUPT if we caught a keyboard interrupt
573 * or ELR_NO_ERROR otherwise. */
574 static EventLoopResult
575 event_loop (GstElement * pipeline, gboolean blocking, GstState target_state)
577 #ifndef DISABLE_FAULT_HANDLER
581 GstMessage *message = NULL;
582 EventLoopResult res = ELR_NO_ERROR;
583 gboolean buffering = FALSE;
585 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
587 #ifndef DISABLE_FAULT_HANDLER
588 timeout_id = g_timeout_add (250, (GSourceFunc) check_intr, pipeline);
592 message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
594 /* if the poll timed out, only when !blocking */
598 /* check if we need to dump messages to the console */
601 const GstStructure *s;
604 seqnum = gst_message_get_seqnum (message);
606 s = gst_message_get_structure (message);
608 src_obj = GST_MESSAGE_SRC (message);
610 if (GST_IS_ELEMENT (src_obj)) {
611 PRINT (_("Got message #%u from element \"%s\" (%s): "),
612 (guint) seqnum, GST_ELEMENT_NAME (src_obj),
613 GST_MESSAGE_TYPE_NAME (message));
614 } else if (GST_IS_PAD (src_obj)) {
615 PRINT (_("Got message #%u from pad \"%s:%s\" (%s): "),
616 (guint) seqnum, GST_DEBUG_PAD_NAME (src_obj),
617 GST_MESSAGE_TYPE_NAME (message));
618 } else if (GST_IS_OBJECT (src_obj)) {
619 PRINT (_("Got message #%u from object \"%s\" (%s): "),
620 (guint) seqnum, GST_OBJECT_NAME (src_obj),
621 GST_MESSAGE_TYPE_NAME (message));
623 PRINT (_("Got message #%u (%s): "), (guint) seqnum,
624 GST_MESSAGE_TYPE_NAME (message));
630 sstr = gst_structure_to_string (s);
631 PRINT ("%s\n", sstr);
634 PRINT ("no message details\n");
638 switch (GST_MESSAGE_TYPE (message)) {
639 case GST_MESSAGE_NEW_CLOCK:
643 gst_message_parse_new_clock (message, &clock);
645 PRINT ("New clock: %s\n", (clock ? GST_OBJECT_NAME (clock) : "NULL"));
648 case GST_MESSAGE_CLOCK_LOST:
649 PRINT ("Clock lost, selecting a new one\n");
650 gst_element_set_state (pipeline, GST_STATE_PAUSED);
651 gst_element_set_state (pipeline, GST_STATE_PLAYING);
653 case GST_MESSAGE_EOS:{
655 PRINT (_("Got EOS from element \"%s\".\n"),
656 GST_MESSAGE_SRC_NAME (message));
659 case GST_MESSAGE_TAG:
661 GstTagList *tag_list;
663 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
664 PRINT (_("FOUND TAG : found by element \"%s\".\n"),
665 GST_MESSAGE_SRC_NAME (message));
666 } else if (GST_IS_PAD (GST_MESSAGE_SRC (message))) {
667 PRINT (_("FOUND TAG : found by pad \"%s:%s\".\n"),
668 GST_DEBUG_PAD_NAME (GST_MESSAGE_SRC (message)));
669 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
670 PRINT (_("FOUND TAG : found by object \"%s\".\n"),
671 GST_MESSAGE_SRC_NAME (message));
673 PRINT (_("FOUND TAG\n"));
676 gst_message_parse_tag (message, &tag_list);
677 gst_tag_list_foreach (tag_list, print_tag, NULL);
678 gst_tag_list_unref (tag_list);
681 case GST_MESSAGE_TOC:
687 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
688 PRINT (_("FOUND TOC : found by element \"%s\".\n"),
689 GST_MESSAGE_SRC_NAME (message));
690 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
691 PRINT (_("FOUND TOC : found by object \"%s\".\n"),
692 GST_MESSAGE_SRC_NAME (message));
694 PRINT (_("FOUND TOC\n"));
697 gst_message_parse_toc (message, &toc, &updated);
698 /* recursively loop over toc entries */
699 entries = gst_toc_get_entries (toc);
700 g_list_foreach (entries, print_toc_entry, GUINT_TO_POINTER (0));
704 case GST_MESSAGE_INFO:{
707 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
709 gst_message_parse_info (message, &gerror, &debug);
711 PRINT (_("INFO:\n%s\n"), debug);
713 g_error_free (gerror);
718 case GST_MESSAGE_WARNING:{
721 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
723 /* dump graph on warning */
724 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
725 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.warning");
727 gst_message_parse_warning (message, &gerror, &debug);
728 PRINT (_("WARNING: from element %s: %s\n"), name, gerror->message);
730 PRINT (_("Additional debug info:\n%s\n"), debug);
732 g_error_free (gerror);
737 case GST_MESSAGE_ERROR:{
738 /* dump graph on error */
739 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
740 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.error");
742 print_error_message (message);
744 /* we have an error */
748 case GST_MESSAGE_STATE_CHANGED:{
749 GstState old, new, pending;
751 /* we only care about pipeline state change messages */
752 if (GST_MESSAGE_SRC (message) != GST_OBJECT_CAST (pipeline))
755 /* ignore when we are buffering since then we mess with the states
758 PRINT (_("Prerolled, waiting for buffering to finish...\n"));
762 gst_message_parse_state_changed (message, &old, &new, &pending);
764 /* if we reached the final target state, exit */
765 if (target_state == GST_STATE_PAUSED && new == target_state)
768 /* else not an interesting message */
771 case GST_MESSAGE_BUFFERING:{
774 gst_message_parse_buffering (message, &percent);
775 PRINT ("%s %d%% \r", _("buffering..."), percent);
777 /* no state management needed for live pipelines */
781 if (percent == 100) {
782 /* a 100% message means buffering is done */
784 /* if the desired state is playing, go back */
785 if (target_state == GST_STATE_PLAYING) {
786 PRINT (_("Done buffering, setting pipeline to PLAYING ...\n"));
787 gst_element_set_state (pipeline, GST_STATE_PLAYING);
792 if (buffering == FALSE && target_state == GST_STATE_PLAYING) {
793 /* we were not buffering but PLAYING, PAUSE the pipeline. */
794 PRINT (_("Buffering, setting pipeline to PAUSED ...\n"));
795 gst_element_set_state (pipeline, GST_STATE_PAUSED);
801 case GST_MESSAGE_LATENCY:
803 PRINT (_("Redistribute latency...\n"));
804 gst_bin_recalculate_latency (GST_BIN (pipeline));
807 case GST_MESSAGE_REQUEST_STATE:
810 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
812 gst_message_parse_request_state (message, &state);
814 PRINT (_("Setting state to %s as requested by %s...\n"),
815 gst_element_state_get_name (state), name);
817 gst_element_set_state (pipeline, state);
822 case GST_MESSAGE_APPLICATION:{
823 const GstStructure *s;
825 s = gst_message_get_structure (message);
827 if (gst_structure_has_name (s, "GstLaunchInterrupt")) {
828 /* this application message is posted when we caught an interrupt and
829 * we need to stop the pipeline. */
830 PRINT (_("Interrupt: Stopping pipeline ...\n"));
836 case GST_MESSAGE_ELEMENT:{
837 if (gst_is_missing_plugin_message (message)) {
840 desc = gst_missing_plugin_message_get_description (message);
841 PRINT (_("Missing element: %s\n"), desc ? desc : "(no description)");
846 /* just be quiet by default */
850 gst_message_unref (message);
852 g_assert_not_reached ();
857 gst_message_unref (message);
858 gst_object_unref (bus);
859 #ifndef DISABLE_FAULT_HANDLER
860 g_source_remove (timeout_id);
866 static GstBusSyncReply
867 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer data)
869 GstElement *pipeline = (GstElement *) data;
871 switch (GST_MESSAGE_TYPE (message)) {
872 case GST_MESSAGE_STATE_CHANGED:
873 /* we only care about pipeline state change messages */
874 if (GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
875 GstState old, new, pending;
876 gchar *state_transition_name;
878 gst_message_parse_state_changed (message, &old, &new, &pending);
880 state_transition_name = g_strdup_printf ("%s_%s",
881 gst_element_state_get_name (old), gst_element_state_get_name (new));
883 /* dump graph for (some) pipeline state changes */
885 gchar *dump_name = g_strconcat ("gst-launch.", state_transition_name,
887 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
888 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
892 /* place a marker into e.g. strace logs */
894 gchar *access_name = g_strconcat (g_get_tmp_dir (), G_DIR_SEPARATOR_S,
895 "gst-launch", G_DIR_SEPARATOR_S, state_transition_name, NULL);
896 g_file_test (access_name, G_FILE_TEST_EXISTS);
897 g_free (access_name);
900 g_free (state_transition_name);
909 main (int argc, char *argv[])
912 gboolean verbose = FALSE;
913 gboolean no_fault = FALSE;
914 gboolean eos_on_shutdown = FALSE;
916 gboolean check_index = FALSE;
918 gchar *savefile = NULL;
919 gchar *exclude_args = NULL;
920 #ifndef GST_DISABLE_OPTION_PARSING
921 GOptionEntry options[] = {
922 {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
923 N_("Output tags (also known as metadata)"), NULL},
924 {"toc", 'c', 0, G_OPTION_ARG_NONE, &toc,
925 N_("Output TOC (chapters and editions)"), NULL},
926 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
927 N_("Output status information and property notifications"), NULL},
928 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
929 N_("Do not print any progress information"), NULL},
930 {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
931 N_("Output messages"), NULL},
932 {"exclude", 'X', 0, G_OPTION_ARG_NONE, &exclude_args,
933 N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
934 {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
935 N_("Do not install a fault handler"), NULL},
936 {"eos-on-shutdown", 'e', 0, G_OPTION_ARG_NONE, &eos_on_shutdown,
937 N_("Force EOS on sources before shutting the pipeline down"), NULL},
939 {"index", 'i', 0, G_OPTION_ARG_NONE, &check_index,
940 N_("Gather and print index statistics"), NULL},
942 GST_TOOLS_GOPTION_VERSION,
950 GPtrArray *index_stats = NULL;
953 GError *error = NULL;
956 free (malloc (8)); /* -lefence */
959 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
960 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
961 textdomain (GETTEXT_PACKAGE);
964 g_set_prgname ("gst-launch-" GST_API_VERSION);
966 #ifndef GST_DISABLE_OPTION_PARSING
967 ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
968 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
969 g_option_context_add_group (ctx, gst_init_get_option_group ());
970 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
972 g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
974 g_printerr ("Error initializing: Unknown error!\n");
977 g_option_context_free (ctx);
979 gst_init (&argc, &argv);
982 gst_tools_print_version ();
984 #ifndef DISABLE_FAULT_HANDLER
991 /* make a null-terminated version of argv */
992 argvn = g_new0 (char *, argc);
993 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
996 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
1002 g_printerr (_("ERROR: pipeline could not be constructed: %s.\n"),
1003 GST_STR_NULL (error->message));
1004 g_error_free (error);
1006 g_printerr (_("ERROR: pipeline could not be constructed.\n"));
1010 g_printerr (_("WARNING: erroneous pipeline: %s\n"),
1011 GST_STR_NULL (error->message));
1012 g_error_free (error);
1017 gchar **exclude_list =
1018 exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
1019 g_signal_connect (pipeline, "deep-notify",
1020 G_CALLBACK (gst_object_default_deep_notify), exclude_list);
1024 GstState state, pending;
1025 GstStateChangeReturn ret;
1028 /* If the top-level object is not a pipeline, place it in a pipeline. */
1029 if (!GST_IS_PIPELINE (pipeline)) {
1030 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
1032 if (real_pipeline == NULL) {
1033 g_printerr (_("ERROR: the 'pipeline' element wasn't found.\n"));
1036 gst_bin_add (GST_BIN (real_pipeline), pipeline);
1037 pipeline = real_pipeline;
1041 /* gst_index_new() creates a null-index, it does not store anything, but
1042 * the entry-added signal works and this is what we use to build the
1044 index = gst_index_new ();
1046 index_stats = g_ptr_array_new ();
1047 g_signal_connect (G_OBJECT (index), "entry-added",
1048 G_CALLBACK (entry_added), index_stats);
1049 g_object_set (G_OBJECT (index), "resolver", GST_INDEX_RESOLVER_GTYPE,
1051 gst_element_set_index (pipeline, index);
1056 bus = gst_element_get_bus (pipeline);
1057 gst_bus_set_sync_handler (bus, bus_sync_handler, (gpointer) pipeline, NULL);
1058 gst_object_unref (bus);
1060 PRINT (_("Setting pipeline to PAUSED ...\n"));
1061 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
1064 case GST_STATE_CHANGE_FAILURE:
1065 g_printerr (_("ERROR: Pipeline doesn't want to pause.\n"));
1067 event_loop (pipeline, FALSE, GST_STATE_VOID_PENDING);
1069 case GST_STATE_CHANGE_NO_PREROLL:
1070 PRINT (_("Pipeline is live and does not need PREROLL ...\n"));
1073 case GST_STATE_CHANGE_ASYNC:
1074 PRINT (_("Pipeline is PREROLLING ...\n"));
1075 caught_error = event_loop (pipeline, TRUE, GST_STATE_PAUSED);
1077 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1080 state = GST_STATE_PAUSED;
1082 case GST_STATE_CHANGE_SUCCESS:
1083 PRINT (_("Pipeline is PREROLLED ...\n"));
1087 caught_error = event_loop (pipeline, FALSE, GST_STATE_PLAYING);
1090 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1092 GstClockTime tfthen, tfnow;
1093 GstClockTimeDiff diff;
1095 PRINT (_("Setting pipeline to PLAYING ...\n"));
1097 if (gst_element_set_state (pipeline,
1098 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
1099 GstMessage *err_msg;
1102 g_printerr (_("ERROR: pipeline doesn't want to play.\n"));
1103 bus = gst_element_get_bus (pipeline);
1104 if ((err_msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0))) {
1105 print_error_message (err_msg);
1106 gst_message_unref (err_msg);
1108 gst_object_unref (bus);
1113 tfthen = gst_util_get_timestamp ();
1114 caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
1115 if (eos_on_shutdown && caught_error != ELR_NO_ERROR) {
1116 gboolean ignore_errors;
1119 if (caught_error == ELR_INTERRUPT) {
1120 PRINT (_("EOS on shutdown enabled -- Forcing EOS on the pipeline\n"));
1121 gst_element_send_event (pipeline, gst_event_new_eos ());
1122 ignore_errors = FALSE;
1124 PRINT (_("EOS on shutdown enabled -- waiting for EOS after Error\n"));
1125 ignore_errors = TRUE;
1127 PRINT (_("Waiting for EOS...\n"));
1130 caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
1132 if (caught_error == ELR_NO_ERROR) {
1134 PRINT (_("EOS received - stopping pipeline...\n"));
1136 } else if (caught_error == ELR_INTERRUPT) {
1138 ("Interrupt while waiting for EOS - stopping pipeline...\n"));
1140 } else if (caught_error == ELR_ERROR) {
1141 if (!ignore_errors) {
1142 PRINT (_("An error happened while waiting for EOS\n"));
1148 tfnow = gst_util_get_timestamp ();
1150 diff = GST_CLOCK_DIFF (tfthen, tfnow);
1152 PRINT (_("Execution ended after %" G_GINT64_FORMAT " ns.\n"), diff);
1155 PRINT (_("Setting pipeline to PAUSED ...\n"));
1156 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1157 if (caught_error == ELR_NO_ERROR)
1158 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1160 /* iterate mainloop to process pending stuff */
1161 while (g_main_context_iteration (NULL, FALSE));
1163 PRINT (_("Setting pipeline to READY ...\n"));
1164 gst_element_set_state (pipeline, GST_STATE_READY);
1165 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1169 print_index_stats (index_stats);
1170 g_ptr_array_free (index_stats, TRUE);
1175 PRINT (_("Setting pipeline to NULL ...\n"));
1176 gst_element_set_state (pipeline, GST_STATE_NULL);
1179 PRINT (_("Freeing pipeline ...\n"));
1180 gst_object_unref (pipeline);