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;
68 static gchar **exclude_args = NULL;
70 /* convenience macro so we don't have to litter the code with if(!quiet) */
71 #define PRINT if(!quiet)g_print
75 fault_handler_sighandler (int signum)
79 /* printf is used instead of g_print(), since it's less likely to
83 fprintf (stderr, "Caught SIGSEGV\n");
87 printf ("Caught SIGQUIT\n");
90 fprintf (stderr, "signo: %d\n", signum);
102 glib_on_error_halt = FALSE;
103 g_on_error_stack_trace ("gst-launch-" GST_API_VERSION);
107 /* FIXME how do we know if we were run by libtool? */
109 "Spinning. Please run 'gdb gst-launch-" GST_API_VERSION " %d' to "
110 "continue debugging, Ctrl-C to quit, or Ctrl-\\ to dump core.\n",
119 struct sigaction action;
121 memset (&action, 0, sizeof (action));
122 action.sa_handler = SIG_DFL;
124 sigaction (SIGSEGV, &action, NULL);
125 sigaction (SIGQUIT, &action, NULL);
131 struct sigaction action;
133 memset (&action, 0, sizeof (action));
134 action.sa_handler = fault_handler_sighandler;
136 sigaction (SIGSEGV, &action, NULL);
137 sigaction (SIGQUIT, &action, NULL);
139 #endif /* G_OS_UNIX */
142 typedef struct _GstIndexStats
150 GstClockTime last_keyframe;
151 GstClockTime last_dltframe;
152 GstClockTime min_keyframe_gap;
153 GstClockTime max_keyframe_gap;
154 GstClockTime avg_keyframe_gap;
158 entry_added (GstIndex * index, GstIndexEntry * entry, gpointer user_data)
160 GPtrArray *index_stats = (GPtrArray *) user_data;
163 switch (entry->type) {
164 case GST_INDEX_ENTRY_ID:
165 /* we have a new writer */
166 GST_DEBUG_OBJECT (index, "id %d: describes writer %s", entry->id,
167 GST_INDEX_ID_DESCRIPTION (entry));
168 if (entry->id >= index_stats->len) {
169 g_ptr_array_set_size (index_stats, entry->id + 1);
171 s = g_new (GstIndexStats, 1);
173 s->desc = g_strdup (GST_INDEX_ID_DESCRIPTION (entry));
174 s->num_frames = s->num_keyframes = s->num_dltframes = 0;
175 s->last_keyframe = s->last_dltframe = GST_CLOCK_TIME_NONE;
176 s->min_keyframe_gap = s->max_keyframe_gap = s->avg_keyframe_gap =
178 g_ptr_array_index (index_stats, entry->id) = s;
180 case GST_INDEX_ENTRY_FORMAT:
181 /* have not found any code calling this */
182 GST_DEBUG_OBJECT (index, "id %d: registered format %d for %s\n",
183 entry->id, GST_INDEX_FORMAT_FORMAT (entry),
184 GST_INDEX_FORMAT_KEY (entry));
186 case GST_INDEX_ENTRY_ASSOCIATION:
189 GstAssocFlags flags = GST_INDEX_ASSOC_FLAGS (entry);
191 s = g_ptr_array_index (index_stats, entry->id);
192 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
194 if (flags & GST_ASSOCIATION_FLAG_KEY_UNIT) {
197 if (GST_CLOCK_TIME_IS_VALID (ts)) {
198 if (GST_CLOCK_TIME_IS_VALID (s->last_keyframe)) {
199 GstClockTimeDiff d = GST_CLOCK_DIFF (s->last_keyframe, ts);
201 if (G_UNLIKELY (d < 0)) {
202 GST_WARNING ("received out-of-order keyframe at %"
203 GST_TIME_FORMAT, GST_TIME_ARGS (ts));
204 /* FIXME: does it still make sense to use that for the statistics */
205 d = GST_CLOCK_DIFF (ts, s->last_keyframe);
208 if (GST_CLOCK_TIME_IS_VALID (s->min_keyframe_gap)) {
209 if (d < s->min_keyframe_gap)
210 s->min_keyframe_gap = d;
212 s->min_keyframe_gap = d;
214 if (GST_CLOCK_TIME_IS_VALID (s->max_keyframe_gap)) {
215 if (d > s->max_keyframe_gap)
216 s->max_keyframe_gap = d;
218 s->max_keyframe_gap = d;
220 if (GST_CLOCK_TIME_IS_VALID (s->avg_keyframe_gap)) {
221 s->avg_keyframe_gap = (d + s->num_frames * s->avg_keyframe_gap) /
224 s->avg_keyframe_gap = d;
227 s->last_keyframe = ts;
230 if (flags & GST_ASSOCIATION_FLAG_DELTA_UNIT) {
232 if (GST_CLOCK_TIME_IS_VALID (ts)) {
233 s->last_dltframe = ts;
245 /* print statistics from the entry_added callback, free the entries */
247 print_index_stats (GPtrArray * index_stats)
251 if (index_stats->len) {
252 g_print ("%s:\n", _("Index statistics"));
255 for (i = 0; i < index_stats->len; i++) {
256 GstIndexStats *s = g_ptr_array_index (index_stats, i);
258 g_print ("id %d, %s\n", s->id, s->desc);
260 GstClockTime last_frame = s->last_keyframe;
262 if (GST_CLOCK_TIME_IS_VALID (s->last_dltframe)) {
263 if (!GST_CLOCK_TIME_IS_VALID (last_frame) ||
264 (s->last_dltframe > last_frame))
265 last_frame = s->last_dltframe;
268 if (GST_CLOCK_TIME_IS_VALID (last_frame)) {
269 g_print (" total time = %" GST_TIME_FORMAT "\n",
270 GST_TIME_ARGS (last_frame));
272 g_print (" frame/keyframe rate = %u / %u = ", s->num_frames,
274 if (s->num_keyframes)
275 g_print ("%lf\n", s->num_frames / (gdouble) s->num_keyframes);
278 if (s->num_keyframes) {
279 g_print (" min/avg/max keyframe gap = %" GST_TIME_FORMAT ", %"
280 GST_TIME_FORMAT ", %" GST_TIME_FORMAT "\n",
281 GST_TIME_ARGS (s->min_keyframe_gap),
282 GST_TIME_ARGS (s->avg_keyframe_gap),
283 GST_TIME_ARGS (s->max_keyframe_gap));
286 g_print (" no stats\n");
296 /* Kids, use the functions from libgstpbutils in gst-plugins-base in your
297 * own code (we can't do that here because it would introduce a circular
300 gst_is_missing_plugin_message (GstMessage * msg)
302 if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ELEMENT
303 || gst_message_get_structure (msg) == NULL)
306 return gst_structure_has_name (gst_message_get_structure (msg),
311 gst_missing_plugin_message_get_description (GstMessage * msg)
313 return gst_structure_get_string (gst_message_get_structure (msg), "name");
317 print_error_message (GstMessage * msg)
320 gchar *name, *debug = NULL;
322 name = gst_object_get_path_string (msg->src);
323 gst_message_parse_error (msg, &err, &debug);
325 g_printerr (_("ERROR: from element %s: %s\n"), name, err->message);
327 g_printerr (_("Additional debug info:\n%s\n"), debug);
329 g_clear_error (&err);
335 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
339 count = gst_tag_list_get_tag_size (list, tag);
341 for (i = 0; i < count; i++) {
344 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
345 if (!gst_tag_list_get_string_index (list, tag, i, &str)) {
346 g_warning ("Couldn't fetch string for %s tag", tag);
347 g_assert_not_reached ();
349 } else if (gst_tag_get_type (tag) == GST_TYPE_SAMPLE) {
350 GstSample *sample = NULL;
352 if (gst_tag_list_get_sample_index (list, tag, i, &sample)) {
353 GstBuffer *img = gst_sample_get_buffer (sample);
354 GstCaps *caps = gst_sample_get_caps (sample);
360 caps_str = gst_caps_to_string (caps);
361 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes, "
362 "type: %s", gst_buffer_get_size (img), caps_str);
365 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes",
366 gst_buffer_get_size (img));
369 str = g_strdup ("NULL buffer");
372 g_warning ("Couldn't fetch sample for %s tag", tag);
373 g_assert_not_reached ();
375 gst_sample_unref (sample);
376 } else if (gst_tag_get_type (tag) == GST_TYPE_DATE_TIME) {
377 GstDateTime *dt = NULL;
379 gst_tag_list_get_date_time_index (list, tag, i, &dt);
380 if (!gst_date_time_has_time (dt)) {
381 str = gst_date_time_to_iso8601_string (dt);
383 gdouble tz_offset = gst_date_time_get_time_zone_offset (dt);
386 if (tz_offset != 0.0) {
387 g_snprintf (tz_str, sizeof (tz_str), "(UTC %s%gh)",
388 (tz_offset > 0.0) ? "+" : "", tz_offset);
390 g_snprintf (tz_str, sizeof (tz_str), "(UTC)");
393 str = g_strdup_printf ("%04u-%02u-%02u %02u:%02u:%02u %s",
394 gst_date_time_get_year (dt), gst_date_time_get_month (dt),
395 gst_date_time_get_day (dt), gst_date_time_get_hour (dt),
396 gst_date_time_get_minute (dt), gst_date_time_get_second (dt),
399 gst_date_time_unref (dt);
402 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
406 PRINT ("%16s: %s\n", i == 0 ? gst_tag_get_nick (tag) : "", str);
413 print_tag_foreach (const GstTagList * tags, const gchar * tag,
418 gint depth = GPOINTER_TO_INT (user_data);
420 if (!gst_tag_list_copy_value (&val, tags, tag))
423 if (G_VALUE_HOLDS_STRING (&val))
424 str = g_value_dup_string (&val);
426 str = gst_value_serialize (&val);
428 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
431 g_value_unset (&val);
434 #define MAX_INDENT 40
437 print_toc_entry (gpointer data, gpointer user_data)
439 GstTocEntry *entry = (GstTocEntry *) data;
440 const gchar spc[MAX_INDENT + 1] = " ";
441 guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
442 const GstTagList *tags;
446 gst_toc_entry_get_start_stop_times (entry, &start, &stop);
448 PRINT ("%s%s:", &spc[MAX_INDENT - indent],
449 gst_toc_entry_type_get_nick (gst_toc_entry_get_entry_type (entry)));
450 if (GST_CLOCK_TIME_IS_VALID (start)) {
451 PRINT (" start: %" GST_TIME_FORMAT, GST_TIME_ARGS (start));
453 if (GST_CLOCK_TIME_IS_VALID (stop)) {
454 PRINT (" stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
460 tags = gst_toc_entry_get_tags (entry);
462 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (indent));
464 /* loop over sub-toc entries */
465 subentries = gst_toc_entry_get_sub_entries (entry);
466 g_list_foreach (subentries, print_toc_entry, GUINT_TO_POINTER (indent));
469 #if defined(G_OS_UNIX) || defined(G_OS_WIN32)
470 static guint signal_watch_intr_id;
471 static guint signal_watch_hup_id;
472 #if defined(G_OS_WIN32)
473 static GstElement *intr_pipeline;
477 #if defined(G_OS_UNIX) || defined(G_OS_WIN32)
478 /* As the interrupt handler is dispatched from GMainContext as a GSourceFunc
479 * handler, we can react to this by posting a message. */
481 intr_handler (gpointer user_data)
483 GstElement *pipeline = (GstElement *) user_data;
485 PRINT ("handling interrupt.\n");
487 /* post an application specific message */
488 gst_element_post_message (GST_ELEMENT (pipeline),
489 gst_message_new_application (GST_OBJECT (pipeline),
490 gst_structure_new ("GstLaunchInterrupt",
491 "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));
493 /* remove signal handler */
494 signal_watch_intr_id = 0;
495 return G_SOURCE_REMOVE;
499 hup_handler (gpointer user_data)
501 GstElement *pipeline = (GstElement *) user_data;
503 if (g_getenv ("GST_DEBUG_DUMP_DOT_DIR") != NULL) {
504 PRINT ("SIGHUP: dumping dot file snapshot ...\n");
506 PRINT ("SIGHUP: not dumping dot file snapshot, GST_DEBUG_DUMP_DOT_DIR "
507 "environment variable not set.\n");
510 /* dump graph on hup */
511 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
512 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.snapshot");
514 return G_SOURCE_CONTINUE;
517 #if defined(G_OS_WIN32) /* G_OS_UNIX */
519 w32_intr_handler (DWORD dwCtrlType)
521 intr_handler ((gpointer) intr_pipeline);
522 intr_pipeline = NULL;
525 #endif /* G_OS_WIN32 */
526 #endif /* G_OS_UNIX */
528 /* returns ELR_ERROR if there was an error
529 * or ELR_INTERRUPT if we caught a keyboard interrupt
530 * or ELR_NO_ERROR otherwise. */
531 static EventLoopResult
532 event_loop (GstElement * pipeline, gboolean blocking, gboolean do_progress,
533 GstState target_state)
536 GstMessage *message = NULL;
537 EventLoopResult res = ELR_NO_ERROR;
538 gboolean buffering = FALSE, in_progress = FALSE;
539 gboolean prerolled = target_state != GST_STATE_PAUSED;
541 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
544 signal_watch_intr_id =
545 g_unix_signal_add (SIGINT, (GSourceFunc) intr_handler, pipeline);
546 signal_watch_hup_id =
547 g_unix_signal_add (SIGHUP, (GSourceFunc) hup_handler, pipeline);
548 #elif defined(G_OS_WIN32)
549 intr_pipeline = NULL;
550 if (SetConsoleCtrlHandler (w32_intr_handler, TRUE))
551 intr_pipeline = pipeline;
555 message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
557 /* if the poll timed out, only when !blocking */
561 /* check if we need to dump messages to the console */
564 const GstStructure *s;
567 seqnum = gst_message_get_seqnum (message);
569 s = gst_message_get_structure (message);
571 src_obj = GST_MESSAGE_SRC (message);
573 if (GST_IS_ELEMENT (src_obj)) {
574 PRINT (_("Got message #%u from element \"%s\" (%s): "),
575 (guint) seqnum, GST_ELEMENT_NAME (src_obj),
576 GST_MESSAGE_TYPE_NAME (message));
577 } else if (GST_IS_PAD (src_obj)) {
578 PRINT (_("Got message #%u from pad \"%s:%s\" (%s): "),
579 (guint) seqnum, GST_DEBUG_PAD_NAME (src_obj),
580 GST_MESSAGE_TYPE_NAME (message));
581 } else if (GST_IS_OBJECT (src_obj)) {
582 PRINT (_("Got message #%u from object \"%s\" (%s): "),
583 (guint) seqnum, GST_OBJECT_NAME (src_obj),
584 GST_MESSAGE_TYPE_NAME (message));
586 PRINT (_("Got message #%u (%s): "), (guint) seqnum,
587 GST_MESSAGE_TYPE_NAME (message));
593 sstr = gst_structure_to_string (s);
594 PRINT ("%s\n", sstr);
597 PRINT ("no message details\n");
601 switch (GST_MESSAGE_TYPE (message)) {
602 case GST_MESSAGE_NEW_CLOCK:
606 gst_message_parse_new_clock (message, &clock);
608 PRINT ("New clock: %s\n", (clock ? GST_OBJECT_NAME (clock) : "NULL"));
611 case GST_MESSAGE_CLOCK_LOST:
612 PRINT ("Clock lost, selecting a new one\n");
613 gst_element_set_state (pipeline, GST_STATE_PAUSED);
614 gst_element_set_state (pipeline, GST_STATE_PLAYING);
616 case GST_MESSAGE_EOS:{
618 PRINT (_("Got EOS from element \"%s\".\n"),
619 GST_MESSAGE_SRC_NAME (message));
622 case GST_MESSAGE_TAG:
624 GstTagList *tag_list;
626 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
627 PRINT (_("FOUND TAG : found by element \"%s\".\n"),
628 GST_MESSAGE_SRC_NAME (message));
629 } else if (GST_IS_PAD (GST_MESSAGE_SRC (message))) {
630 PRINT (_("FOUND TAG : found by pad \"%s:%s\".\n"),
631 GST_DEBUG_PAD_NAME (GST_MESSAGE_SRC (message)));
632 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
633 PRINT (_("FOUND TAG : found by object \"%s\".\n"),
634 GST_MESSAGE_SRC_NAME (message));
636 PRINT (_("FOUND TAG\n"));
639 gst_message_parse_tag (message, &tag_list);
640 gst_tag_list_foreach (tag_list, print_tag, NULL);
641 gst_tag_list_unref (tag_list);
644 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, &updated);
661 /* recursively loop over toc entries */
662 entries = gst_toc_get_entries (toc);
663 g_list_foreach (entries, print_toc_entry, GUINT_TO_POINTER (0));
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_clear_error (&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_clear_error (&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 gst_message_parse_state_changed (message, &old, &new, &pending);
720 /* if we reached the final target state, exit */
721 if (target_state == GST_STATE_PAUSED && new == target_state) {
723 /* ignore when we are buffering since then we mess with the states
726 PRINT (_("Prerolled, waiting for buffering to finish...\n"));
730 PRINT (_("Prerolled, waiting for progress to finish...\n"));
735 /* else not an interesting message */
738 case GST_MESSAGE_BUFFERING:{
741 gst_message_parse_buffering (message, &percent);
742 PRINT ("%s %d%% \r", _("buffering..."), percent);
744 /* no state management needed for live pipelines */
748 if (percent == 100) {
749 /* a 100% message means buffering is done */
751 /* if the desired state is playing, go back */
752 if (target_state == GST_STATE_PLAYING) {
753 PRINT (_("Done buffering, setting pipeline to PLAYING ...\n"));
754 gst_element_set_state (pipeline, GST_STATE_PLAYING);
755 } else if (prerolled && !in_progress)
759 if (!buffering && target_state == GST_STATE_PLAYING) {
760 /* we were not buffering but PLAYING, PAUSE the pipeline. */
761 PRINT (_("Buffering, setting pipeline to PAUSED ...\n"));
762 gst_element_set_state (pipeline, GST_STATE_PAUSED);
768 case GST_MESSAGE_LATENCY:
770 PRINT (_("Redistribute latency...\n"));
771 gst_bin_recalculate_latency (GST_BIN (pipeline));
774 case GST_MESSAGE_REQUEST_STATE:
777 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
779 gst_message_parse_request_state (message, &state);
781 PRINT (_("Setting state to %s as requested by %s...\n"),
782 gst_element_state_get_name (state), name);
784 gst_element_set_state (pipeline, state);
789 case GST_MESSAGE_APPLICATION:{
790 const GstStructure *s;
792 s = gst_message_get_structure (message);
794 if (gst_structure_has_name (s, "GstLaunchInterrupt")) {
795 /* this application message is posted when we caught an interrupt and
796 * we need to stop the pipeline. */
797 PRINT (_("Interrupt: Stopping pipeline ...\n"));
803 case GST_MESSAGE_PROGRESS:
805 GstProgressType type;
808 gst_message_parse_progress (message, &type, &code, &text);
811 case GST_PROGRESS_TYPE_START:
812 case GST_PROGRESS_TYPE_CONTINUE:
818 case GST_PROGRESS_TYPE_COMPLETE:
819 case GST_PROGRESS_TYPE_CANCELED:
820 case GST_PROGRESS_TYPE_ERROR:
826 PRINT (_("Progress: (%s) %s\n"), code, text);
830 if (do_progress && !in_progress && !buffering && prerolled)
834 case GST_MESSAGE_ELEMENT:{
835 if (gst_is_missing_plugin_message (message)) {
838 desc = gst_missing_plugin_message_get_description (message);
839 PRINT (_("Missing element: %s\n"), desc ? desc : "(no description)");
843 case GST_MESSAGE_HAVE_CONTEXT:{
845 const gchar *context_type;
848 gst_message_parse_have_context (message, &context);
850 context_type = gst_context_get_context_type (context);
852 gst_structure_to_string (gst_context_get_structure (context));
853 PRINT (_("Got context from element '%s': %s=%s\n"),
854 GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)), context_type,
856 g_free (context_str);
857 gst_context_unref (context);
860 case GST_MESSAGE_PROPERTY_NOTIFY:{
864 gchar *val_str = NULL;
865 gchar **ex_prop, *obj_name;
870 gst_message_parse_property_notify (message, &obj, &name, &val);
872 /* Let's not print anything for excluded properties... */
873 ex_prop = exclude_args;
874 while (ex_prop != NULL && *ex_prop != NULL) {
875 if (strcmp (name, *ex_prop) == 0)
879 if (ex_prop != NULL && *ex_prop != NULL)
882 obj_name = gst_object_get_path_string (GST_OBJECT (obj));
884 if (G_VALUE_HOLDS_STRING (val))
885 val_str = g_value_dup_string (val);
886 else if (G_VALUE_TYPE (val) == GST_TYPE_CAPS)
887 val_str = gst_caps_to_string (g_value_get_boxed (val));
888 else if (G_VALUE_TYPE (val) == GST_TYPE_TAG_LIST)
889 val_str = gst_tag_list_to_string (g_value_get_boxed (val));
890 else if (G_VALUE_TYPE (val) == GST_TYPE_STRUCTURE)
891 val_str = gst_structure_to_string (g_value_get_boxed (val));
893 val_str = gst_value_serialize (val);
895 val_str = g_strdup ("(no value)");
898 g_print ("%s: %s = %s\n", obj_name, name, val_str);
904 /* just be quiet by default */
908 gst_message_unref (message);
910 g_assert_not_reached ();
915 gst_message_unref (message);
916 gst_object_unref (bus);
918 if (signal_watch_intr_id > 0)
919 g_source_remove (signal_watch_intr_id);
920 if (signal_watch_hup_id > 0)
921 g_source_remove (signal_watch_hup_id);
922 #elif defined(G_OS_WIN32)
923 intr_pipeline = NULL;
924 SetConsoleCtrlHandler (w32_intr_handler, FALSE);
930 static GstBusSyncReply
931 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer data)
933 GstElement *pipeline = (GstElement *) data;
935 switch (GST_MESSAGE_TYPE (message)) {
936 case GST_MESSAGE_STATE_CHANGED:
937 /* we only care about pipeline state change messages */
938 if (GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
939 GstState old, new, pending;
940 gchar *state_transition_name;
942 gst_message_parse_state_changed (message, &old, &new, &pending);
944 state_transition_name = g_strdup_printf ("%s_%s",
945 gst_element_state_get_name (old), gst_element_state_get_name (new));
947 /* dump graph for (some) pipeline state changes */
949 gchar *dump_name = g_strconcat ("gst-launch.", state_transition_name,
951 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
952 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
956 /* place a marker into e.g. strace logs */
958 gchar *access_name = g_strconcat (g_get_tmp_dir (), G_DIR_SEPARATOR_S,
959 "gst-launch", G_DIR_SEPARATOR_S, state_transition_name, NULL);
960 g_file_test (access_name, G_FILE_TEST_EXISTS);
961 g_free (access_name);
964 g_free (state_transition_name);
974 main (int argc, char *argv[])
977 gboolean verbose = FALSE;
978 gboolean no_fault = FALSE;
979 gboolean eos_on_shutdown = FALSE;
981 gboolean check_index = FALSE;
983 gchar *savefile = NULL;
984 #ifndef GST_DISABLE_OPTION_PARSING
985 GOptionEntry options[] = {
986 {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
987 N_("Output tags (also known as metadata)"), NULL},
988 {"toc", 'c', 0, G_OPTION_ARG_NONE, &toc,
989 N_("Output TOC (chapters and editions)"), NULL},
990 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
991 N_("Output status information and property notifications"), NULL},
992 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
993 N_("Do not print any progress information"), NULL},
994 {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
995 N_("Output messages"), NULL},
996 {"exclude", 'X', 0, G_OPTION_ARG_STRING_ARRAY, &exclude_args,
997 N_("Do not output status information for the specified property "
998 "if verbose output is enabled (can be used multiple times)"),
999 N_("PROPERTY-NAME")},
1000 {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
1001 N_("Do not install a fault handler"), NULL},
1002 {"eos-on-shutdown", 'e', 0, G_OPTION_ARG_NONE, &eos_on_shutdown,
1003 N_("Force EOS on sources before shutting the pipeline down"), NULL},
1005 {"index", 'i', 0, G_OPTION_ARG_NONE, &check_index,
1006 N_("Gather and print index statistics"), NULL},
1008 GST_TOOLS_GOPTION_VERSION,
1011 GOptionContext *ctx;
1016 GPtrArray *index_stats = NULL;
1019 GError *error = NULL;
1020 gulong deep_notify_id = 0;
1023 free (malloc (8)); /* -lefence */
1025 setlocale (LC_ALL, "");
1028 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1029 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1030 textdomain (GETTEXT_PACKAGE);
1033 g_set_prgname ("gst-launch-" GST_API_VERSION);
1034 /* Ensure XInitThreads() is called if/when needed */
1035 g_setenv ("GST_GL_XINITTHREADS", "1", TRUE);
1037 #ifndef GST_DISABLE_OPTION_PARSING
1038 ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
1039 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
1040 g_option_context_add_group (ctx, gst_init_get_option_group ());
1041 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
1043 g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
1045 g_printerr ("Error initializing: Unknown error!\n");
1046 g_clear_error (&err);
1047 g_option_context_free (ctx);
1050 g_option_context_free (ctx);
1052 gst_init (&argc, &argv);
1055 gst_tools_print_version ();
1062 /* make a null-terminated version of argv */
1063 argvn = g_new0 (char *, argc);
1064 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
1067 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
1073 g_printerr (_("ERROR: pipeline could not be constructed: %s.\n"),
1074 GST_STR_NULL (error->message));
1075 g_clear_error (&error);
1077 g_printerr (_("ERROR: pipeline could not be constructed.\n"));
1081 g_printerr (_("WARNING: erroneous pipeline: %s\n"),
1082 GST_STR_NULL (error->message));
1083 g_clear_error (&error);
1088 GstState state, pending;
1089 GstStateChangeReturn ret;
1092 /* If the top-level object is not a pipeline, place it in a pipeline. */
1093 if (!GST_IS_PIPELINE (pipeline)) {
1094 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
1096 if (real_pipeline == NULL) {
1097 g_printerr (_("ERROR: the 'pipeline' element wasn't found.\n"));
1100 gst_bin_add (GST_BIN (real_pipeline), pipeline);
1101 pipeline = real_pipeline;
1105 gst_element_add_property_deep_notify_watch (pipeline, NULL, TRUE);
1109 /* gst_index_new() creates a null-index, it does not store anything, but
1110 * the entry-added signal works and this is what we use to build the
1112 index = gst_index_new ();
1114 index_stats = g_ptr_array_new ();
1115 g_signal_connect (G_OBJECT (index), "entry-added",
1116 G_CALLBACK (entry_added), index_stats);
1117 g_object_set (G_OBJECT (index), "resolver", GST_INDEX_RESOLVER_GTYPE,
1119 gst_element_set_index (pipeline, index);
1124 bus = gst_element_get_bus (pipeline);
1125 gst_bus_set_sync_handler (bus, bus_sync_handler, (gpointer) pipeline, NULL);
1126 gst_object_unref (bus);
1128 PRINT (_("Setting pipeline to PAUSED ...\n"));
1129 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
1132 case GST_STATE_CHANGE_FAILURE:
1133 g_printerr (_("ERROR: Pipeline doesn't want to pause.\n"));
1135 event_loop (pipeline, FALSE, FALSE, GST_STATE_VOID_PENDING);
1137 case GST_STATE_CHANGE_NO_PREROLL:
1138 PRINT (_("Pipeline is live and does not need PREROLL ...\n"));
1141 case GST_STATE_CHANGE_ASYNC:
1142 PRINT (_("Pipeline is PREROLLING ...\n"));
1143 caught_error = event_loop (pipeline, TRUE, TRUE, GST_STATE_PAUSED);
1145 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1149 state = GST_STATE_PAUSED;
1151 case GST_STATE_CHANGE_SUCCESS:
1152 PRINT (_("Pipeline is PREROLLED ...\n"));
1156 caught_error = event_loop (pipeline, FALSE, TRUE, GST_STATE_PLAYING);
1159 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1162 GstClockTime tfthen, tfnow;
1163 GstClockTimeDiff diff;
1165 PRINT (_("Setting pipeline to PLAYING ...\n"));
1167 if (gst_element_set_state (pipeline,
1168 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
1169 GstMessage *err_msg;
1172 g_printerr (_("ERROR: pipeline doesn't want to play.\n"));
1173 bus = gst_element_get_bus (pipeline);
1174 if ((err_msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0))) {
1175 print_error_message (err_msg);
1176 gst_message_unref (err_msg);
1178 gst_object_unref (bus);
1183 tfthen = gst_util_get_timestamp ();
1184 caught_error = event_loop (pipeline, TRUE, FALSE, GST_STATE_PLAYING);
1186 if (eos_on_shutdown && caught_error != ELR_NO_ERROR) {
1187 gboolean ignore_errors;
1190 if (caught_error == ELR_INTERRUPT) {
1191 PRINT (_("EOS on shutdown enabled -- Forcing EOS on the pipeline\n"));
1192 gst_element_send_event (pipeline, gst_event_new_eos ());
1193 ignore_errors = FALSE;
1195 PRINT (_("EOS on shutdown enabled -- waiting for EOS after Error\n"));
1196 ignore_errors = TRUE;
1198 PRINT (_("Waiting for EOS...\n"));
1201 caught_error = event_loop (pipeline, TRUE, FALSE, GST_STATE_PLAYING);
1203 if (caught_error == ELR_NO_ERROR) {
1205 PRINT (_("EOS received - stopping pipeline...\n"));
1207 } else if (caught_error == ELR_INTERRUPT) {
1209 ("Interrupt while waiting for EOS - stopping pipeline...\n"));
1212 } else if (caught_error == ELR_ERROR) {
1213 if (!ignore_errors) {
1214 PRINT (_("An error happened while waiting for EOS\n"));
1221 tfnow = gst_util_get_timestamp ();
1223 diff = GST_CLOCK_DIFF (tfthen, tfnow);
1225 PRINT (_("Execution ended after %" GST_TIME_FORMAT "\n"),
1226 GST_TIME_ARGS (diff));
1229 PRINT (_("Setting pipeline to PAUSED ...\n"));
1230 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1231 if (caught_error == ELR_NO_ERROR)
1232 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1234 /* iterate mainloop to process pending stuff */
1235 while (g_main_context_iteration (NULL, FALSE));
1237 /* No need to see all those pad caps going to NULL etc., it's just noise */
1238 if (deep_notify_id != 0)
1239 g_signal_handler_disconnect (pipeline, deep_notify_id);
1241 PRINT (_("Setting pipeline to READY ...\n"));
1242 gst_element_set_state (pipeline, GST_STATE_READY);
1243 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1247 print_index_stats (index_stats);
1248 g_ptr_array_free (index_stats, TRUE);
1253 PRINT (_("Setting pipeline to NULL ...\n"));
1254 gst_element_set_state (pipeline, GST_STATE_NULL);
1257 PRINT (_("Freeing pipeline ...\n"));
1258 gst_object_unref (pipeline);