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.
36 #include <glib-unix.h>
38 #elif defined (G_OS_WIN32)
39 #define WIN32_LEAN_AND_MEAN
42 #include <locale.h> /* for LC_ALL */
45 extern volatile gboolean glib_on_error_halt;
48 static void fault_restore (void);
49 static void fault_spin (void);
52 /* event_loop return codes */
53 typedef enum _EventLoopResult
60 static GstElement *pipeline;
61 static EventLoopResult caught_error = ELR_NO_ERROR;
62 static gboolean quiet = FALSE;
63 static gboolean tags = FALSE;
64 static gboolean toc = FALSE;
65 static gboolean messages = FALSE;
66 static gboolean is_live = FALSE;
67 static gboolean waiting_eos = FALSE;
69 /* convenience macro so we don't have to litter the code with if(!quiet) */
70 #define PRINT if(!quiet)g_print
74 fault_handler_sighandler (int signum)
78 /* printf is used instead of g_print(), since it's less likely to
82 fprintf (stderr, "Caught SIGSEGV\n");
86 printf ("Caught SIGQUIT\n");
89 fprintf (stderr, "signo: %d\n", signum);
101 glib_on_error_halt = FALSE;
102 g_on_error_stack_trace ("gst-launch-" GST_API_VERSION);
106 /* FIXME how do we know if we were run by libtool? */
108 "Spinning. Please run 'gdb gst-launch-" GST_API_VERSION " %d' to "
109 "continue debugging, Ctrl-C to quit, or Ctrl-\\ to dump core.\n",
118 struct sigaction action;
120 memset (&action, 0, sizeof (action));
121 action.sa_handler = SIG_DFL;
123 sigaction (SIGSEGV, &action, NULL);
124 sigaction (SIGQUIT, &action, NULL);
130 struct sigaction action;
132 memset (&action, 0, sizeof (action));
133 action.sa_handler = fault_handler_sighandler;
135 sigaction (SIGSEGV, &action, NULL);
136 sigaction (SIGQUIT, &action, NULL);
138 #endif /* G_OS_UNIX */
141 typedef struct _GstIndexStats
149 GstClockTime last_keyframe;
150 GstClockTime last_dltframe;
151 GstClockTime min_keyframe_gap;
152 GstClockTime max_keyframe_gap;
153 GstClockTime avg_keyframe_gap;
157 entry_added (GstIndex * index, GstIndexEntry * entry, gpointer user_data)
159 GPtrArray *index_stats = (GPtrArray *) user_data;
162 switch (entry->type) {
163 case GST_INDEX_ENTRY_ID:
164 /* we have a new writer */
165 GST_DEBUG_OBJECT (index, "id %d: describes writer %s", entry->id,
166 GST_INDEX_ID_DESCRIPTION (entry));
167 if (entry->id >= index_stats->len) {
168 g_ptr_array_set_size (index_stats, entry->id + 1);
170 s = g_new (GstIndexStats, 1);
172 s->desc = g_strdup (GST_INDEX_ID_DESCRIPTION (entry));
173 s->num_frames = s->num_keyframes = s->num_dltframes = 0;
174 s->last_keyframe = s->last_dltframe = GST_CLOCK_TIME_NONE;
175 s->min_keyframe_gap = s->max_keyframe_gap = s->avg_keyframe_gap =
177 g_ptr_array_index (index_stats, entry->id) = s;
179 case GST_INDEX_ENTRY_FORMAT:
180 /* have not found any code calling this */
181 GST_DEBUG_OBJECT (index, "id %d: registered format %d for %s\n",
182 entry->id, GST_INDEX_FORMAT_FORMAT (entry),
183 GST_INDEX_FORMAT_KEY (entry));
185 case GST_INDEX_ENTRY_ASSOCIATION:
188 GstAssocFlags flags = GST_INDEX_ASSOC_FLAGS (entry);
190 s = g_ptr_array_index (index_stats, entry->id);
191 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
193 if (flags & GST_ASSOCIATION_FLAG_KEY_UNIT) {
196 if (GST_CLOCK_TIME_IS_VALID (ts)) {
197 if (GST_CLOCK_TIME_IS_VALID (s->last_keyframe)) {
198 GstClockTimeDiff d = GST_CLOCK_DIFF (s->last_keyframe, ts);
200 if (G_UNLIKELY (d < 0)) {
201 GST_WARNING ("received out-of-order keyframe at %"
202 GST_TIME_FORMAT, GST_TIME_ARGS (ts));
203 /* FIXME: does it still make sense to use that for the statistics */
204 d = GST_CLOCK_DIFF (ts, s->last_keyframe);
207 if (GST_CLOCK_TIME_IS_VALID (s->min_keyframe_gap)) {
208 if (d < s->min_keyframe_gap)
209 s->min_keyframe_gap = d;
211 s->min_keyframe_gap = d;
213 if (GST_CLOCK_TIME_IS_VALID (s->max_keyframe_gap)) {
214 if (d > s->max_keyframe_gap)
215 s->max_keyframe_gap = d;
217 s->max_keyframe_gap = d;
219 if (GST_CLOCK_TIME_IS_VALID (s->avg_keyframe_gap)) {
220 s->avg_keyframe_gap = (d + s->num_frames * s->avg_keyframe_gap) /
223 s->avg_keyframe_gap = d;
226 s->last_keyframe = ts;
229 if (flags & GST_ASSOCIATION_FLAG_DELTA_UNIT) {
231 if (GST_CLOCK_TIME_IS_VALID (ts)) {
232 s->last_dltframe = ts;
244 /* print statistics from the entry_added callback, free the entries */
246 print_index_stats (GPtrArray * index_stats)
250 if (index_stats->len) {
251 g_print ("%s:\n", _("Index statistics"));
254 for (i = 0; i < index_stats->len; i++) {
255 GstIndexStats *s = g_ptr_array_index (index_stats, i);
257 g_print ("id %d, %s\n", s->id, s->desc);
259 GstClockTime last_frame = s->last_keyframe;
261 if (GST_CLOCK_TIME_IS_VALID (s->last_dltframe)) {
262 if (!GST_CLOCK_TIME_IS_VALID (last_frame) ||
263 (s->last_dltframe > last_frame))
264 last_frame = s->last_dltframe;
267 if (GST_CLOCK_TIME_IS_VALID (last_frame)) {
268 g_print (" total time = %" GST_TIME_FORMAT "\n",
269 GST_TIME_ARGS (last_frame));
271 g_print (" frame/keyframe rate = %u / %u = ", s->num_frames,
273 if (s->num_keyframes)
274 g_print ("%lf\n", s->num_frames / (gdouble) s->num_keyframes);
277 if (s->num_keyframes) {
278 g_print (" min/avg/max keyframe gap = %" GST_TIME_FORMAT ", %"
279 GST_TIME_FORMAT ", %" GST_TIME_FORMAT "\n",
280 GST_TIME_ARGS (s->min_keyframe_gap),
281 GST_TIME_ARGS (s->avg_keyframe_gap),
282 GST_TIME_ARGS (s->max_keyframe_gap));
285 g_print (" no stats\n");
295 /* Kids, use the functions from libgstpbutils in gst-plugins-base in your
296 * own code (we can't do that here because it would introduce a circular
299 gst_is_missing_plugin_message (GstMessage * msg)
301 if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ELEMENT
302 || gst_message_get_structure (msg) == NULL)
305 return gst_structure_has_name (gst_message_get_structure (msg),
310 gst_missing_plugin_message_get_description (GstMessage * msg)
312 return gst_structure_get_string (gst_message_get_structure (msg), "name");
316 print_error_message (GstMessage * msg)
319 gchar *name, *debug = NULL;
321 name = gst_object_get_path_string (msg->src);
322 gst_message_parse_error (msg, &err, &debug);
324 g_printerr (_("ERROR: from element %s: %s\n"), name, err->message);
326 g_printerr (_("Additional debug info:\n%s\n"), debug);
328 g_clear_error (&err);
334 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
338 count = gst_tag_list_get_tag_size (list, tag);
340 for (i = 0; i < count; i++) {
343 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
344 if (!gst_tag_list_get_string_index (list, tag, i, &str)) {
345 g_warning ("Couldn't fetch string for %s tag", tag);
346 g_assert_not_reached ();
348 } else if (gst_tag_get_type (tag) == GST_TYPE_SAMPLE) {
349 GstSample *sample = NULL;
351 if (gst_tag_list_get_sample_index (list, tag, i, &sample)) {
352 GstBuffer *img = gst_sample_get_buffer (sample);
353 GstCaps *caps = gst_sample_get_caps (sample);
359 caps_str = gst_caps_to_string (caps);
360 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes, "
361 "type: %s", gst_buffer_get_size (img), caps_str);
364 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes",
365 gst_buffer_get_size (img));
368 str = g_strdup ("NULL buffer");
371 g_warning ("Couldn't fetch sample for %s tag", tag);
372 g_assert_not_reached ();
374 gst_sample_unref (sample);
375 } else if (gst_tag_get_type (tag) == GST_TYPE_DATE_TIME) {
376 GstDateTime *dt = NULL;
378 gst_tag_list_get_date_time_index (list, tag, i, &dt);
379 if (!gst_date_time_has_time (dt)) {
380 str = gst_date_time_to_iso8601_string (dt);
382 gdouble tz_offset = gst_date_time_get_time_zone_offset (dt);
385 if (tz_offset != 0.0) {
386 g_snprintf (tz_str, sizeof (tz_str), "(UTC %s%gh)",
387 (tz_offset > 0.0) ? "+" : "", tz_offset);
389 g_snprintf (tz_str, sizeof (tz_str), "(UTC)");
392 str = g_strdup_printf ("%04u-%02u-%02u %02u:%02u:%02u %s",
393 gst_date_time_get_year (dt), gst_date_time_get_month (dt),
394 gst_date_time_get_day (dt), gst_date_time_get_hour (dt),
395 gst_date_time_get_minute (dt), gst_date_time_get_second (dt),
398 gst_date_time_unref (dt);
401 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
405 PRINT ("%16s: %s\n", i == 0 ? gst_tag_get_nick (tag) : "", str);
412 print_tag_foreach (const GstTagList * tags, const gchar * tag,
417 gint depth = GPOINTER_TO_INT (user_data);
419 if (!gst_tag_list_copy_value (&val, tags, tag))
422 if (G_VALUE_HOLDS_STRING (&val))
423 str = g_value_dup_string (&val);
425 str = gst_value_serialize (&val);
427 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
430 g_value_unset (&val);
433 #define MAX_INDENT 40
436 print_toc_entry (gpointer data, gpointer user_data)
438 GstTocEntry *entry = (GstTocEntry *) data;
439 const gchar spc[MAX_INDENT + 1] = " ";
440 guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
441 const GstTagList *tags;
445 gst_toc_entry_get_start_stop_times (entry, &start, &stop);
447 PRINT ("%s%s:", &spc[MAX_INDENT - indent],
448 gst_toc_entry_type_get_nick (gst_toc_entry_get_entry_type (entry)));
449 if (GST_CLOCK_TIME_IS_VALID (start)) {
450 PRINT (" start: %" GST_TIME_FORMAT, GST_TIME_ARGS (start));
452 if (GST_CLOCK_TIME_IS_VALID (stop)) {
453 PRINT (" stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
459 tags = gst_toc_entry_get_tags (entry);
461 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (indent));
463 /* loop over sub-toc entries */
464 subentries = gst_toc_entry_get_sub_entries (entry);
465 g_list_foreach (subentries, print_toc_entry, GUINT_TO_POINTER (indent));
468 #if defined(G_OS_UNIX) || defined(G_OS_WIN32)
469 static guint signal_watch_id;
470 #if defined(G_OS_WIN32)
471 static GstElement *intr_pipeline;
475 #if defined(G_OS_UNIX) || defined(G_OS_WIN32)
476 /* As the interrupt handler is dispatched from GMainContext as a GSourceFunc
477 * handler, we can react to this by posting a message. */
479 intr_handler (gpointer user_data)
481 GstElement *pipeline = (GstElement *) user_data;
483 PRINT ("handling interrupt.\n");
485 /* post an application specific message */
486 gst_element_post_message (GST_ELEMENT (pipeline),
487 gst_message_new_application (GST_OBJECT (pipeline),
488 gst_structure_new ("GstLaunchInterrupt",
489 "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));
491 /* remove signal handler */
496 #if defined(G_OS_WIN32) /* G_OS_UNIX */
498 w32_intr_handler (DWORD dwCtrlType)
500 intr_handler ((gpointer) intr_pipeline);
501 intr_pipeline = NULL;
504 #endif /* G_OS_WIN32 */
505 #endif /* G_OS_UNIX */
507 /* returns ELR_ERROR if there was an error
508 * or ELR_INTERRUPT if we caught a keyboard interrupt
509 * or ELR_NO_ERROR otherwise. */
510 static EventLoopResult
511 event_loop (GstElement * pipeline, gboolean blocking, gboolean do_progress,
512 GstState target_state)
515 GstMessage *message = NULL;
516 EventLoopResult res = ELR_NO_ERROR;
517 gboolean buffering = FALSE, in_progress = FALSE;
518 gboolean prerolled = target_state != GST_STATE_PAUSED;
520 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
524 g_unix_signal_add (SIGINT, (GSourceFunc) intr_handler, pipeline);
525 #elif defined(G_OS_WIN32)
526 intr_pipeline = NULL;
527 if (SetConsoleCtrlHandler (w32_intr_handler, TRUE))
528 intr_pipeline = pipeline;
532 message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
534 /* if the poll timed out, only when !blocking */
538 /* check if we need to dump messages to the console */
541 const GstStructure *s;
544 seqnum = gst_message_get_seqnum (message);
546 s = gst_message_get_structure (message);
548 src_obj = GST_MESSAGE_SRC (message);
550 if (GST_IS_ELEMENT (src_obj)) {
551 PRINT (_("Got message #%u from element \"%s\" (%s): "),
552 (guint) seqnum, GST_ELEMENT_NAME (src_obj),
553 GST_MESSAGE_TYPE_NAME (message));
554 } else if (GST_IS_PAD (src_obj)) {
555 PRINT (_("Got message #%u from pad \"%s:%s\" (%s): "),
556 (guint) seqnum, GST_DEBUG_PAD_NAME (src_obj),
557 GST_MESSAGE_TYPE_NAME (message));
558 } else if (GST_IS_OBJECT (src_obj)) {
559 PRINT (_("Got message #%u from object \"%s\" (%s): "),
560 (guint) seqnum, GST_OBJECT_NAME (src_obj),
561 GST_MESSAGE_TYPE_NAME (message));
563 PRINT (_("Got message #%u (%s): "), (guint) seqnum,
564 GST_MESSAGE_TYPE_NAME (message));
570 sstr = gst_structure_to_string (s);
571 PRINT ("%s\n", sstr);
574 PRINT ("no message details\n");
578 switch (GST_MESSAGE_TYPE (message)) {
579 case GST_MESSAGE_NEW_CLOCK:
583 gst_message_parse_new_clock (message, &clock);
585 PRINT ("New clock: %s\n", (clock ? GST_OBJECT_NAME (clock) : "NULL"));
588 case GST_MESSAGE_CLOCK_LOST:
589 PRINT ("Clock lost, selecting a new one\n");
590 gst_element_set_state (pipeline, GST_STATE_PAUSED);
591 gst_element_set_state (pipeline, GST_STATE_PLAYING);
593 case GST_MESSAGE_EOS:{
595 PRINT (_("Got EOS from element \"%s\".\n"),
596 GST_MESSAGE_SRC_NAME (message));
599 case GST_MESSAGE_TAG:
601 GstTagList *tag_list;
603 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
604 PRINT (_("FOUND TAG : found by element \"%s\".\n"),
605 GST_MESSAGE_SRC_NAME (message));
606 } else if (GST_IS_PAD (GST_MESSAGE_SRC (message))) {
607 PRINT (_("FOUND TAG : found by pad \"%s:%s\".\n"),
608 GST_DEBUG_PAD_NAME (GST_MESSAGE_SRC (message)));
609 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
610 PRINT (_("FOUND TAG : found by object \"%s\".\n"),
611 GST_MESSAGE_SRC_NAME (message));
613 PRINT (_("FOUND TAG\n"));
616 gst_message_parse_tag (message, &tag_list);
617 gst_tag_list_foreach (tag_list, print_tag, NULL);
618 gst_tag_list_unref (tag_list);
621 case GST_MESSAGE_TOC:
627 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
628 PRINT (_("FOUND TOC : found by element \"%s\".\n"),
629 GST_MESSAGE_SRC_NAME (message));
630 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
631 PRINT (_("FOUND TOC : found by object \"%s\".\n"),
632 GST_MESSAGE_SRC_NAME (message));
634 PRINT (_("FOUND TOC\n"));
637 gst_message_parse_toc (message, &toc, &updated);
638 /* recursively loop over toc entries */
639 entries = gst_toc_get_entries (toc);
640 g_list_foreach (entries, print_toc_entry, GUINT_TO_POINTER (0));
644 case GST_MESSAGE_INFO:{
647 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
649 gst_message_parse_info (message, &gerror, &debug);
651 PRINT (_("INFO:\n%s\n"), debug);
653 g_clear_error (&gerror);
658 case GST_MESSAGE_WARNING:{
661 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
663 /* dump graph on warning */
664 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
665 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.warning");
667 gst_message_parse_warning (message, &gerror, &debug);
668 PRINT (_("WARNING: from element %s: %s\n"), name, gerror->message);
670 PRINT (_("Additional debug info:\n%s\n"), debug);
672 g_clear_error (&gerror);
677 case GST_MESSAGE_ERROR:{
678 /* dump graph on error */
679 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
680 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.error");
682 print_error_message (message);
684 /* we have an error */
688 case GST_MESSAGE_STATE_CHANGED:{
689 GstState old, new, pending;
691 /* we only care about pipeline state change messages */
692 if (GST_MESSAGE_SRC (message) != GST_OBJECT_CAST (pipeline))
695 gst_message_parse_state_changed (message, &old, &new, &pending);
697 /* if we reached the final target state, exit */
698 if (target_state == GST_STATE_PAUSED && new == target_state) {
700 /* ignore when we are buffering since then we mess with the states
703 PRINT (_("Prerolled, waiting for buffering to finish...\n"));
707 PRINT (_("Prerolled, waiting for progress to finish...\n"));
712 /* else not an interesting message */
715 case GST_MESSAGE_BUFFERING:{
718 gst_message_parse_buffering (message, &percent);
719 PRINT ("%s %d%% \r", _("buffering..."), percent);
721 /* no state management needed for live pipelines */
725 if (percent == 100) {
726 /* a 100% message means buffering is done */
728 /* if the desired state is playing, go back */
729 if (target_state == GST_STATE_PLAYING) {
730 PRINT (_("Done buffering, setting pipeline to PLAYING ...\n"));
731 gst_element_set_state (pipeline, GST_STATE_PLAYING);
732 } else if (prerolled && !in_progress)
736 if (!buffering && target_state == GST_STATE_PLAYING) {
737 /* we were not buffering but PLAYING, PAUSE the pipeline. */
738 PRINT (_("Buffering, setting pipeline to PAUSED ...\n"));
739 gst_element_set_state (pipeline, GST_STATE_PAUSED);
745 case GST_MESSAGE_LATENCY:
747 PRINT (_("Redistribute latency...\n"));
748 gst_bin_recalculate_latency (GST_BIN (pipeline));
751 case GST_MESSAGE_REQUEST_STATE:
754 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
756 gst_message_parse_request_state (message, &state);
758 PRINT (_("Setting state to %s as requested by %s...\n"),
759 gst_element_state_get_name (state), name);
761 gst_element_set_state (pipeline, state);
766 case GST_MESSAGE_APPLICATION:{
767 const GstStructure *s;
769 s = gst_message_get_structure (message);
771 if (gst_structure_has_name (s, "GstLaunchInterrupt")) {
772 /* this application message is posted when we caught an interrupt and
773 * we need to stop the pipeline. */
774 PRINT (_("Interrupt: Stopping pipeline ...\n"));
780 case GST_MESSAGE_PROGRESS:
782 GstProgressType type;
785 gst_message_parse_progress (message, &type, &code, &text);
788 case GST_PROGRESS_TYPE_START:
789 case GST_PROGRESS_TYPE_CONTINUE:
795 case GST_PROGRESS_TYPE_COMPLETE:
796 case GST_PROGRESS_TYPE_CANCELED:
797 case GST_PROGRESS_TYPE_ERROR:
803 PRINT (_("Progress: (%s) %s\n"), code, text);
807 if (do_progress && !in_progress && !buffering && prerolled)
811 case GST_MESSAGE_ELEMENT:{
812 if (gst_is_missing_plugin_message (message)) {
815 desc = gst_missing_plugin_message_get_description (message);
816 PRINT (_("Missing element: %s\n"), desc ? desc : "(no description)");
820 case GST_MESSAGE_HAVE_CONTEXT:{
822 const gchar *context_type;
825 gst_message_parse_have_context (message, &context);
827 context_type = gst_context_get_context_type (context);
829 gst_structure_to_string (gst_context_get_structure (context));
830 PRINT (_("Got context from element '%s': %s=%s\n"),
831 GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)), context_type,
833 g_free (context_str);
834 gst_context_unref (context);
838 /* just be quiet by default */
842 gst_message_unref (message);
844 g_assert_not_reached ();
849 gst_message_unref (message);
850 gst_object_unref (bus);
852 if (signal_watch_id > 0)
853 g_source_remove (signal_watch_id);
854 #elif defined(G_OS_WIN32)
855 intr_pipeline = NULL;
856 SetConsoleCtrlHandler (w32_intr_handler, FALSE);
862 static GstBusSyncReply
863 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer data)
865 GstElement *pipeline = (GstElement *) data;
867 switch (GST_MESSAGE_TYPE (message)) {
868 case GST_MESSAGE_STATE_CHANGED:
869 /* we only care about pipeline state change messages */
870 if (GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
871 GstState old, new, pending;
872 gchar *state_transition_name;
874 gst_message_parse_state_changed (message, &old, &new, &pending);
876 state_transition_name = g_strdup_printf ("%s_%s",
877 gst_element_state_get_name (old), gst_element_state_get_name (new));
879 /* dump graph for (some) pipeline state changes */
881 gchar *dump_name = g_strconcat ("gst-launch.", state_transition_name,
883 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
884 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
888 /* place a marker into e.g. strace logs */
890 gchar *access_name = g_strconcat (g_get_tmp_dir (), G_DIR_SEPARATOR_S,
891 "gst-launch", G_DIR_SEPARATOR_S, state_transition_name, NULL);
892 g_file_test (access_name, G_FILE_TEST_EXISTS);
893 g_free (access_name);
896 g_free (state_transition_name);
906 main (int argc, char *argv[])
909 gboolean verbose = FALSE;
910 gboolean no_fault = FALSE;
911 gboolean eos_on_shutdown = FALSE;
913 gboolean check_index = FALSE;
915 gchar *savefile = NULL;
916 gchar **exclude_args = NULL;
917 #ifndef GST_DISABLE_OPTION_PARSING
918 GOptionEntry options[] = {
919 {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
920 N_("Output tags (also known as metadata)"), NULL},
921 {"toc", 'c', 0, G_OPTION_ARG_NONE, &toc,
922 N_("Output TOC (chapters and editions)"), NULL},
923 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
924 N_("Output status information and property notifications"), NULL},
925 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
926 N_("Do not print any progress information"), NULL},
927 {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
928 N_("Output messages"), NULL},
929 {"exclude", 'X', 0, G_OPTION_ARG_STRING_ARRAY, &exclude_args,
930 N_("Do not output status information for the specified property "
931 "if verbose output is enabled (can be used multiple times)"),
932 N_("PROPERTY-NAME")},
933 {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
934 N_("Do not install a fault handler"), NULL},
935 {"eos-on-shutdown", 'e', 0, G_OPTION_ARG_NONE, &eos_on_shutdown,
936 N_("Force EOS on sources before shutting the pipeline down"), NULL},
938 {"index", 'i', 0, G_OPTION_ARG_NONE, &check_index,
939 N_("Gather and print index statistics"), NULL},
941 GST_TOOLS_GOPTION_VERSION,
949 GPtrArray *index_stats = NULL;
952 GError *error = NULL;
953 gulong deep_notify_id = 0;
956 free (malloc (8)); /* -lefence */
958 setlocale (LC_ALL, "");
961 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
962 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
963 textdomain (GETTEXT_PACKAGE);
966 g_set_prgname ("gst-launch-" GST_API_VERSION);
968 #ifndef GST_DISABLE_OPTION_PARSING
969 ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
970 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
971 g_option_context_add_group (ctx, gst_init_get_option_group ());
972 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
974 g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
976 g_printerr ("Error initializing: Unknown error!\n");
977 g_clear_error (&error);
978 g_option_context_free (ctx);
981 g_option_context_free (ctx);
983 gst_init (&argc, &argv);
986 gst_tools_print_version ();
993 /* make a null-terminated version of argv */
994 argvn = g_new0 (char *, argc);
995 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
998 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
1004 g_printerr (_("ERROR: pipeline could not be constructed: %s.\n"),
1005 GST_STR_NULL (error->message));
1006 g_clear_error (&error);
1008 g_printerr (_("ERROR: pipeline could not be constructed.\n"));
1012 g_printerr (_("WARNING: erroneous pipeline: %s\n"),
1013 GST_STR_NULL (error->message));
1014 g_clear_error (&error);
1019 GstState state, pending;
1020 GstStateChangeReturn ret;
1023 /* If the top-level object is not a pipeline, place it in a pipeline. */
1024 if (!GST_IS_PIPELINE (pipeline)) {
1025 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
1027 if (real_pipeline == NULL) {
1028 g_printerr (_("ERROR: the 'pipeline' element wasn't found.\n"));
1031 gst_bin_add (GST_BIN (real_pipeline), pipeline);
1032 pipeline = real_pipeline;
1035 deep_notify_id = g_signal_connect (pipeline, "deep-notify",
1036 G_CALLBACK (gst_object_default_deep_notify), exclude_args);
1040 /* gst_index_new() creates a null-index, it does not store anything, but
1041 * the entry-added signal works and this is what we use to build the
1043 index = gst_index_new ();
1045 index_stats = g_ptr_array_new ();
1046 g_signal_connect (G_OBJECT (index), "entry-added",
1047 G_CALLBACK (entry_added), index_stats);
1048 g_object_set (G_OBJECT (index), "resolver", GST_INDEX_RESOLVER_GTYPE,
1050 gst_element_set_index (pipeline, index);
1055 bus = gst_element_get_bus (pipeline);
1056 gst_bus_set_sync_handler (bus, bus_sync_handler, (gpointer) pipeline, NULL);
1057 gst_object_unref (bus);
1059 PRINT (_("Setting pipeline to PAUSED ...\n"));
1060 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
1063 case GST_STATE_CHANGE_FAILURE:
1064 g_printerr (_("ERROR: Pipeline doesn't want to pause.\n"));
1066 event_loop (pipeline, FALSE, FALSE, GST_STATE_VOID_PENDING);
1068 case GST_STATE_CHANGE_NO_PREROLL:
1069 PRINT (_("Pipeline is live and does not need PREROLL ...\n"));
1072 case GST_STATE_CHANGE_ASYNC:
1073 PRINT (_("Pipeline is PREROLLING ...\n"));
1074 caught_error = event_loop (pipeline, TRUE, TRUE, GST_STATE_PAUSED);
1076 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, TRUE, GST_STATE_PLAYING);
1090 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1093 GstClockTime tfthen, tfnow;
1094 GstClockTimeDiff diff;
1096 PRINT (_("Setting pipeline to PLAYING ...\n"));
1098 if (gst_element_set_state (pipeline,
1099 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
1100 GstMessage *err_msg;
1103 g_printerr (_("ERROR: pipeline doesn't want to play.\n"));
1104 bus = gst_element_get_bus (pipeline);
1105 if ((err_msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0))) {
1106 print_error_message (err_msg);
1107 gst_message_unref (err_msg);
1109 gst_object_unref (bus);
1114 tfthen = gst_util_get_timestamp ();
1115 caught_error = event_loop (pipeline, TRUE, FALSE, GST_STATE_PLAYING);
1116 if (eos_on_shutdown && caught_error != ELR_NO_ERROR) {
1117 gboolean ignore_errors;
1120 if (caught_error == ELR_INTERRUPT) {
1121 PRINT (_("EOS on shutdown enabled -- Forcing EOS on the pipeline\n"));
1122 gst_element_send_event (pipeline, gst_event_new_eos ());
1123 ignore_errors = FALSE;
1125 PRINT (_("EOS on shutdown enabled -- waiting for EOS after Error\n"));
1126 ignore_errors = TRUE;
1128 PRINT (_("Waiting for EOS...\n"));
1131 caught_error = event_loop (pipeline, TRUE, FALSE, GST_STATE_PLAYING);
1133 if (caught_error == ELR_NO_ERROR) {
1135 PRINT (_("EOS received - stopping pipeline...\n"));
1137 } else if (caught_error == ELR_INTERRUPT) {
1139 ("Interrupt while waiting for EOS - stopping pipeline...\n"));
1142 } else if (caught_error == ELR_ERROR) {
1143 if (!ignore_errors) {
1144 PRINT (_("An error happened while waiting for EOS\n"));
1151 tfnow = gst_util_get_timestamp ();
1153 diff = GST_CLOCK_DIFF (tfthen, tfnow);
1155 PRINT (_("Execution ended after %" GST_TIME_FORMAT "\n"),
1156 GST_TIME_ARGS (diff));
1159 PRINT (_("Setting pipeline to PAUSED ...\n"));
1160 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1161 if (caught_error == ELR_NO_ERROR)
1162 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1164 /* iterate mainloop to process pending stuff */
1165 while (g_main_context_iteration (NULL, FALSE));
1167 /* No need to see all those pad caps going to NULL etc., it's just noise */
1168 if (deep_notify_id != 0)
1169 g_signal_handler_disconnect (pipeline, deep_notify_id);
1171 PRINT (_("Setting pipeline to READY ...\n"));
1172 gst_element_set_state (pipeline, GST_STATE_READY);
1173 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1177 print_index_stats (index_stats);
1178 g_ptr_array_free (index_stats, TRUE);
1183 PRINT (_("Setting pipeline to NULL ...\n"));
1184 gst_element_set_state (pipeline, GST_STATE_NULL);
1187 PRINT (_("Freeing pipeline ...\n"));
1188 gst_object_unref (pipeline);