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>
39 #include <locale.h> /* for LC_ALL */
42 extern volatile gboolean glib_on_error_halt;
45 static void fault_restore (void);
46 static void fault_spin (void);
49 /* event_loop return codes */
50 typedef enum _EventLoopResult
57 static GstElement *pipeline;
58 static EventLoopResult caught_error = ELR_NO_ERROR;
59 static gboolean quiet = FALSE;
60 static gboolean tags = FALSE;
61 static gboolean toc = FALSE;
62 static gboolean messages = FALSE;
63 static gboolean is_live = FALSE;
64 static gboolean waiting_eos = FALSE;
66 /* convenience macro so we don't have to litter the code with if(!quiet) */
67 #define PRINT if(!quiet)g_print
71 fault_handler_sighandler (int signum)
75 /* printf is used instead of g_print(), since it's less likely to
79 fprintf (stderr, "Caught SIGSEGV\n");
83 printf ("Caught SIGQUIT\n");
86 fprintf (stderr, "signo: %d\n", signum);
98 glib_on_error_halt = FALSE;
99 g_on_error_stack_trace ("gst-launch-" GST_API_VERSION);
103 /* FIXME how do we know if we were run by libtool? */
105 "Spinning. Please run 'gdb gst-launch-" GST_API_VERSION " %d' to "
106 "continue debugging, Ctrl-C to quit, or Ctrl-\\ to dump core.\n",
115 struct sigaction action;
117 memset (&action, 0, sizeof (action));
118 action.sa_handler = SIG_DFL;
120 sigaction (SIGSEGV, &action, NULL);
121 sigaction (SIGQUIT, &action, NULL);
127 struct sigaction action;
129 memset (&action, 0, sizeof (action));
130 action.sa_handler = fault_handler_sighandler;
132 sigaction (SIGSEGV, &action, NULL);
133 sigaction (SIGQUIT, &action, NULL);
135 #endif /* G_OS_UNIX */
138 typedef struct _GstIndexStats
146 GstClockTime last_keyframe;
147 GstClockTime last_dltframe;
148 GstClockTime min_keyframe_gap;
149 GstClockTime max_keyframe_gap;
150 GstClockTime avg_keyframe_gap;
154 entry_added (GstIndex * index, GstIndexEntry * entry, gpointer user_data)
156 GPtrArray *index_stats = (GPtrArray *) user_data;
159 switch (entry->type) {
160 case GST_INDEX_ENTRY_ID:
161 /* we have a new writer */
162 GST_DEBUG_OBJECT (index, "id %d: describes writer %s", entry->id,
163 GST_INDEX_ID_DESCRIPTION (entry));
164 if (entry->id >= index_stats->len) {
165 g_ptr_array_set_size (index_stats, entry->id + 1);
167 s = g_new (GstIndexStats, 1);
169 s->desc = g_strdup (GST_INDEX_ID_DESCRIPTION (entry));
170 s->num_frames = s->num_keyframes = s->num_dltframes = 0;
171 s->last_keyframe = s->last_dltframe = GST_CLOCK_TIME_NONE;
172 s->min_keyframe_gap = s->max_keyframe_gap = s->avg_keyframe_gap =
174 g_ptr_array_index (index_stats, entry->id) = s;
176 case GST_INDEX_ENTRY_FORMAT:
177 /* have not found any code calling this */
178 GST_DEBUG_OBJECT (index, "id %d: registered format %d for %s\n",
179 entry->id, GST_INDEX_FORMAT_FORMAT (entry),
180 GST_INDEX_FORMAT_KEY (entry));
182 case GST_INDEX_ENTRY_ASSOCIATION:
185 GstAssocFlags flags = GST_INDEX_ASSOC_FLAGS (entry);
187 s = g_ptr_array_index (index_stats, entry->id);
188 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
190 if (flags & GST_ASSOCIATION_FLAG_KEY_UNIT) {
193 if (GST_CLOCK_TIME_IS_VALID (ts)) {
194 if (GST_CLOCK_TIME_IS_VALID (s->last_keyframe)) {
195 GstClockTimeDiff d = GST_CLOCK_DIFF (s->last_keyframe, ts);
197 if (G_UNLIKELY (d < 0)) {
198 GST_WARNING ("received out-of-order keyframe at %"
199 GST_TIME_FORMAT, GST_TIME_ARGS (ts));
200 /* FIXME: does it still make sense to use that for the statistics */
201 d = GST_CLOCK_DIFF (ts, s->last_keyframe);
204 if (GST_CLOCK_TIME_IS_VALID (s->min_keyframe_gap)) {
205 if (d < s->min_keyframe_gap)
206 s->min_keyframe_gap = d;
208 s->min_keyframe_gap = d;
210 if (GST_CLOCK_TIME_IS_VALID (s->max_keyframe_gap)) {
211 if (d > s->max_keyframe_gap)
212 s->max_keyframe_gap = d;
214 s->max_keyframe_gap = d;
216 if (GST_CLOCK_TIME_IS_VALID (s->avg_keyframe_gap)) {
217 s->avg_keyframe_gap = (d + s->num_frames * s->avg_keyframe_gap) /
220 s->avg_keyframe_gap = d;
223 s->last_keyframe = ts;
226 if (flags & GST_ASSOCIATION_FLAG_DELTA_UNIT) {
228 if (GST_CLOCK_TIME_IS_VALID (ts)) {
229 s->last_dltframe = ts;
241 /* print statistics from the entry_added callback, free the entries */
243 print_index_stats (GPtrArray * index_stats)
247 if (index_stats->len) {
248 g_print ("%s:\n", _("Index statistics"));
251 for (i = 0; i < index_stats->len; i++) {
252 GstIndexStats *s = g_ptr_array_index (index_stats, i);
254 g_print ("id %d, %s\n", s->id, s->desc);
256 GstClockTime last_frame = s->last_keyframe;
258 if (GST_CLOCK_TIME_IS_VALID (s->last_dltframe)) {
259 if (!GST_CLOCK_TIME_IS_VALID (last_frame) ||
260 (s->last_dltframe > last_frame))
261 last_frame = s->last_dltframe;
264 if (GST_CLOCK_TIME_IS_VALID (last_frame)) {
265 g_print (" total time = %" GST_TIME_FORMAT "\n",
266 GST_TIME_ARGS (last_frame));
268 g_print (" frame/keyframe rate = %u / %u = ", s->num_frames,
270 if (s->num_keyframes)
271 g_print ("%lf\n", s->num_frames / (gdouble) s->num_keyframes);
274 if (s->num_keyframes) {
275 g_print (" min/avg/max keyframe gap = %" GST_TIME_FORMAT ", %"
276 GST_TIME_FORMAT ", %" GST_TIME_FORMAT "\n",
277 GST_TIME_ARGS (s->min_keyframe_gap),
278 GST_TIME_ARGS (s->avg_keyframe_gap),
279 GST_TIME_ARGS (s->max_keyframe_gap));
282 g_print (" no stats\n");
292 /* Kids, use the functions from libgstpbutils in gst-plugins-base in your
293 * own code (we can't do that here because it would introduce a circular
296 gst_is_missing_plugin_message (GstMessage * msg)
298 if (GST_MESSAGE_TYPE (msg) != GST_MESSAGE_ELEMENT
299 || gst_message_get_structure (msg) == NULL)
302 return gst_structure_has_name (gst_message_get_structure (msg),
307 gst_missing_plugin_message_get_description (GstMessage * msg)
309 return gst_structure_get_string (gst_message_get_structure (msg), "name");
313 print_error_message (GstMessage * msg)
316 gchar *name, *debug = NULL;
318 name = gst_object_get_path_string (msg->src);
319 gst_message_parse_error (msg, &err, &debug);
321 g_printerr (_("ERROR: from element %s: %s\n"), name, err->message);
323 g_printerr (_("Additional debug info:\n%s\n"), debug);
331 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
335 count = gst_tag_list_get_tag_size (list, tag);
337 for (i = 0; i < count; i++) {
340 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
341 if (!gst_tag_list_get_string_index (list, tag, i, &str)) {
342 g_warning ("Couldn't fetch string for %s tag", tag);
343 g_assert_not_reached ();
345 } else if (gst_tag_get_type (tag) == GST_TYPE_SAMPLE) {
346 GstSample *sample = NULL;
348 if (gst_tag_list_get_sample_index (list, tag, i, &sample)) {
349 GstBuffer *img = gst_sample_get_buffer (sample);
350 GstCaps *caps = gst_sample_get_caps (sample);
356 caps_str = gst_caps_to_string (caps);
357 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes, "
358 "type: %s", gst_buffer_get_size (img), caps_str);
361 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes",
362 gst_buffer_get_size (img));
365 str = g_strdup ("NULL buffer");
368 g_warning ("Couldn't fetch sample for %s tag", tag);
369 g_assert_not_reached ();
371 } else if (gst_tag_get_type (tag) == GST_TYPE_DATE_TIME) {
372 GstDateTime *dt = NULL;
374 gst_tag_list_get_date_time_index (list, tag, i, &dt);
375 if (!gst_date_time_has_time (dt)) {
376 str = gst_date_time_to_iso8601_string (dt);
378 gdouble tz_offset = gst_date_time_get_time_zone_offset (dt);
381 if (tz_offset != 0.0) {
382 g_snprintf (tz_str, sizeof (tz_str), "(UTC %s%gh)",
383 (tz_offset > 0.0) ? "+" : "", tz_offset);
385 g_snprintf (tz_str, sizeof (tz_str), "(UTC)");
388 str = g_strdup_printf ("%04u-%02u-%02u %02u:%02u:%02u %s",
389 gst_date_time_get_year (dt), gst_date_time_get_month (dt),
390 gst_date_time_get_day (dt), gst_date_time_get_hour (dt),
391 gst_date_time_get_minute (dt), gst_date_time_get_second (dt),
394 gst_date_time_unref (dt);
397 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
401 PRINT ("%16s: %s\n", i == 0 ? gst_tag_get_nick (tag) : "", str);
408 print_tag_foreach (const GstTagList * tags, const gchar * tag,
413 gint depth = GPOINTER_TO_INT (user_data);
415 if (!gst_tag_list_copy_value (&val, tags, tag))
418 if (G_VALUE_HOLDS_STRING (&val))
419 str = g_value_dup_string (&val);
421 str = gst_value_serialize (&val);
423 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
426 g_value_unset (&val);
429 #define MAX_INDENT 40
432 print_toc_entry (gpointer data, gpointer user_data)
434 GstTocEntry *entry = (GstTocEntry *) data;
435 const gchar spc[MAX_INDENT + 1] = " ";
436 guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
437 const GstTagList *tags;
441 gst_toc_entry_get_start_stop_times (entry, &start, &stop);
443 PRINT ("%s%s:", &spc[MAX_INDENT - indent],
444 gst_toc_entry_type_get_nick (gst_toc_entry_get_entry_type (entry)));
445 if (GST_CLOCK_TIME_IS_VALID (start)) {
446 PRINT (" start: %" GST_TIME_FORMAT, GST_TIME_ARGS (start));
448 if (GST_CLOCK_TIME_IS_VALID (stop)) {
449 PRINT (" stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
455 tags = gst_toc_entry_get_tags (entry);
457 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (indent));
459 /* loop over sub-toc entries */
460 subentries = gst_toc_entry_get_sub_entries (entry);
461 g_list_foreach (subentries, print_toc_entry, GUINT_TO_POINTER (indent));
465 static guint signal_watch_id;
469 /* As the interrupt handler is dispatched from GMainContext as a GSourceFunc
470 * handler, we can react to this by posting a message. */
472 intr_handler (gpointer user_data)
474 GstElement *pipeline = (GstElement *) user_data;
476 PRINT ("handling interrupt.\n");
478 /* post an application specific message */
479 gst_element_post_message (GST_ELEMENT (pipeline),
480 gst_message_new_application (GST_OBJECT (pipeline),
481 gst_structure_new ("GstLaunchInterrupt",
482 "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));
484 /* remove signal handler */
489 #endif /* G_OS_UNIX */
491 /* returns ELR_ERROR if there was an error
492 * or ELR_INTERRUPT if we caught a keyboard interrupt
493 * or ELR_NO_ERROR otherwise. */
494 static EventLoopResult
495 event_loop (GstElement * pipeline, gboolean blocking, gboolean do_progress,
496 GstState target_state)
499 GstMessage *message = NULL;
500 EventLoopResult res = ELR_NO_ERROR;
501 gboolean buffering = FALSE, in_progress = FALSE;
502 gboolean prerolled = target_state != GST_STATE_PAUSED;
504 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
508 g_unix_signal_add (SIGINT, (GSourceFunc) intr_handler, pipeline);
512 message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
514 /* if the poll timed out, only when !blocking */
518 /* check if we need to dump messages to the console */
521 const GstStructure *s;
524 seqnum = gst_message_get_seqnum (message);
526 s = gst_message_get_structure (message);
528 src_obj = GST_MESSAGE_SRC (message);
530 if (GST_IS_ELEMENT (src_obj)) {
531 PRINT (_("Got message #%u from element \"%s\" (%s): "),
532 (guint) seqnum, GST_ELEMENT_NAME (src_obj),
533 GST_MESSAGE_TYPE_NAME (message));
534 } else if (GST_IS_PAD (src_obj)) {
535 PRINT (_("Got message #%u from pad \"%s:%s\" (%s): "),
536 (guint) seqnum, GST_DEBUG_PAD_NAME (src_obj),
537 GST_MESSAGE_TYPE_NAME (message));
538 } else if (GST_IS_OBJECT (src_obj)) {
539 PRINT (_("Got message #%u from object \"%s\" (%s): "),
540 (guint) seqnum, GST_OBJECT_NAME (src_obj),
541 GST_MESSAGE_TYPE_NAME (message));
543 PRINT (_("Got message #%u (%s): "), (guint) seqnum,
544 GST_MESSAGE_TYPE_NAME (message));
550 sstr = gst_structure_to_string (s);
551 PRINT ("%s\n", sstr);
554 PRINT ("no message details\n");
558 switch (GST_MESSAGE_TYPE (message)) {
559 case GST_MESSAGE_NEW_CLOCK:
563 gst_message_parse_new_clock (message, &clock);
565 PRINT ("New clock: %s\n", (clock ? GST_OBJECT_NAME (clock) : "NULL"));
568 case GST_MESSAGE_CLOCK_LOST:
569 PRINT ("Clock lost, selecting a new one\n");
570 gst_element_set_state (pipeline, GST_STATE_PAUSED);
571 gst_element_set_state (pipeline, GST_STATE_PLAYING);
573 case GST_MESSAGE_EOS:{
575 PRINT (_("Got EOS from element \"%s\".\n"),
576 GST_MESSAGE_SRC_NAME (message));
579 case GST_MESSAGE_TAG:
581 GstTagList *tag_list;
583 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
584 PRINT (_("FOUND TAG : found by element \"%s\".\n"),
585 GST_MESSAGE_SRC_NAME (message));
586 } else if (GST_IS_PAD (GST_MESSAGE_SRC (message))) {
587 PRINT (_("FOUND TAG : found by pad \"%s:%s\".\n"),
588 GST_DEBUG_PAD_NAME (GST_MESSAGE_SRC (message)));
589 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
590 PRINT (_("FOUND TAG : found by object \"%s\".\n"),
591 GST_MESSAGE_SRC_NAME (message));
593 PRINT (_("FOUND TAG\n"));
596 gst_message_parse_tag (message, &tag_list);
597 gst_tag_list_foreach (tag_list, print_tag, NULL);
598 gst_tag_list_unref (tag_list);
601 case GST_MESSAGE_TOC:
607 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
608 PRINT (_("FOUND TOC : found by element \"%s\".\n"),
609 GST_MESSAGE_SRC_NAME (message));
610 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
611 PRINT (_("FOUND TOC : found by object \"%s\".\n"),
612 GST_MESSAGE_SRC_NAME (message));
614 PRINT (_("FOUND TOC\n"));
617 gst_message_parse_toc (message, &toc, &updated);
618 /* recursively loop over toc entries */
619 entries = gst_toc_get_entries (toc);
620 g_list_foreach (entries, print_toc_entry, GUINT_TO_POINTER (0));
624 case GST_MESSAGE_INFO:{
627 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
629 gst_message_parse_info (message, &gerror, &debug);
631 PRINT (_("INFO:\n%s\n"), debug);
633 g_error_free (gerror);
638 case GST_MESSAGE_WARNING:{
641 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
643 /* dump graph on warning */
644 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
645 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.warning");
647 gst_message_parse_warning (message, &gerror, &debug);
648 PRINT (_("WARNING: from element %s: %s\n"), name, gerror->message);
650 PRINT (_("Additional debug info:\n%s\n"), debug);
652 g_error_free (gerror);
657 case GST_MESSAGE_ERROR:{
658 /* dump graph on error */
659 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
660 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.error");
662 print_error_message (message);
664 /* we have an error */
668 case GST_MESSAGE_STATE_CHANGED:{
669 GstState old, new, pending;
671 /* we only care about pipeline state change messages */
672 if (GST_MESSAGE_SRC (message) != GST_OBJECT_CAST (pipeline))
675 gst_message_parse_state_changed (message, &old, &new, &pending);
677 /* if we reached the final target state, exit */
678 if (target_state == GST_STATE_PAUSED && new == target_state) {
680 /* ignore when we are buffering since then we mess with the states
683 PRINT (_("Prerolled, waiting for buffering to finish...\n"));
687 PRINT (_("Prerolled, waiting for progress to finish...\n"));
692 /* else not an interesting message */
695 case GST_MESSAGE_BUFFERING:{
698 gst_message_parse_buffering (message, &percent);
699 PRINT ("%s %d%% \r", _("buffering..."), percent);
701 /* no state management needed for live pipelines */
705 if (percent == 100) {
706 /* a 100% message means buffering is done */
708 /* if the desired state is playing, go back */
709 if (target_state == GST_STATE_PLAYING) {
710 PRINT (_("Done buffering, setting pipeline to PLAYING ...\n"));
711 gst_element_set_state (pipeline, GST_STATE_PLAYING);
712 } else if (prerolled && !in_progress)
716 if (buffering == FALSE && target_state == GST_STATE_PLAYING) {
717 /* we were not buffering but PLAYING, PAUSE the pipeline. */
718 PRINT (_("Buffering, setting pipeline to PAUSED ...\n"));
719 gst_element_set_state (pipeline, GST_STATE_PAUSED);
725 case GST_MESSAGE_LATENCY:
727 PRINT (_("Redistribute latency...\n"));
728 gst_bin_recalculate_latency (GST_BIN (pipeline));
731 case GST_MESSAGE_REQUEST_STATE:
734 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
736 gst_message_parse_request_state (message, &state);
738 PRINT (_("Setting state to %s as requested by %s...\n"),
739 gst_element_state_get_name (state), name);
741 gst_element_set_state (pipeline, state);
746 case GST_MESSAGE_APPLICATION:{
747 const GstStructure *s;
749 s = gst_message_get_structure (message);
751 if (gst_structure_has_name (s, "GstLaunchInterrupt")) {
752 /* this application message is posted when we caught an interrupt and
753 * we need to stop the pipeline. */
754 PRINT (_("Interrupt: Stopping pipeline ...\n"));
760 case GST_MESSAGE_PROGRESS:
762 GstProgressType type;
765 gst_message_parse_progress (message, &type, &code, &text);
768 case GST_PROGRESS_TYPE_START:
769 case GST_PROGRESS_TYPE_CONTINUE:
775 case GST_PROGRESS_TYPE_COMPLETE:
776 case GST_PROGRESS_TYPE_CANCELED:
777 case GST_PROGRESS_TYPE_ERROR:
783 PRINT (_("Progress: (%s) %s\n"), code, text);
787 if (do_progress && !in_progress && !buffering && prerolled)
791 case GST_MESSAGE_ELEMENT:{
792 if (gst_is_missing_plugin_message (message)) {
795 desc = gst_missing_plugin_message_get_description (message);
796 PRINT (_("Missing element: %s\n"), desc ? desc : "(no description)");
800 case GST_MESSAGE_HAVE_CONTEXT:{
802 const gchar *context_type;
805 gst_message_parse_have_context (message, &context);
807 context_type = gst_context_get_context_type (context);
809 gst_structure_to_string (gst_context_get_structure (context));
810 PRINT (_("Got context from element '%s': %s=%s\n"),
811 GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)), context_type,
813 g_free (context_str);
814 gst_context_unref (context);
818 /* just be quiet by default */
822 gst_message_unref (message);
824 g_assert_not_reached ();
829 gst_message_unref (message);
830 gst_object_unref (bus);
832 if (signal_watch_id > 0)
833 g_source_remove (signal_watch_id);
839 static GstBusSyncReply
840 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer data)
842 GstElement *pipeline = (GstElement *) data;
844 switch (GST_MESSAGE_TYPE (message)) {
845 case GST_MESSAGE_STATE_CHANGED:
846 /* we only care about pipeline state change messages */
847 if (GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
848 GstState old, new, pending;
849 gchar *state_transition_name;
851 gst_message_parse_state_changed (message, &old, &new, &pending);
853 state_transition_name = g_strdup_printf ("%s_%s",
854 gst_element_state_get_name (old), gst_element_state_get_name (new));
856 /* dump graph for (some) pipeline state changes */
858 gchar *dump_name = g_strconcat ("gst-launch.", state_transition_name,
860 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
861 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
865 /* place a marker into e.g. strace logs */
867 gchar *access_name = g_strconcat (g_get_tmp_dir (), G_DIR_SEPARATOR_S,
868 "gst-launch", G_DIR_SEPARATOR_S, state_transition_name, NULL);
869 g_file_test (access_name, G_FILE_TEST_EXISTS);
870 g_free (access_name);
873 g_free (state_transition_name);
883 main (int argc, char *argv[])
886 gboolean verbose = FALSE;
887 gboolean no_fault = FALSE;
888 gboolean eos_on_shutdown = FALSE;
890 gboolean check_index = FALSE;
892 gchar *savefile = NULL;
893 gchar *exclude_args = NULL;
894 #ifndef GST_DISABLE_OPTION_PARSING
895 GOptionEntry options[] = {
896 {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
897 N_("Output tags (also known as metadata)"), NULL},
898 {"toc", 'c', 0, G_OPTION_ARG_NONE, &toc,
899 N_("Output TOC (chapters and editions)"), NULL},
900 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
901 N_("Output status information and property notifications"), NULL},
902 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
903 N_("Do not print any progress information"), NULL},
904 {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
905 N_("Output messages"), NULL},
906 {"exclude", 'X', 0, G_OPTION_ARG_NONE, &exclude_args,
907 N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
908 {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
909 N_("Do not install a fault handler"), NULL},
910 {"eos-on-shutdown", 'e', 0, G_OPTION_ARG_NONE, &eos_on_shutdown,
911 N_("Force EOS on sources before shutting the pipeline down"), NULL},
913 {"index", 'i', 0, G_OPTION_ARG_NONE, &check_index,
914 N_("Gather and print index statistics"), NULL},
916 GST_TOOLS_GOPTION_VERSION,
924 GPtrArray *index_stats = NULL;
927 GError *error = NULL;
928 gulong deep_notify_id = 0;
931 free (malloc (8)); /* -lefence */
933 setlocale (LC_ALL, "");
936 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
937 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
938 textdomain (GETTEXT_PACKAGE);
941 g_set_prgname ("gst-launch-" GST_API_VERSION);
943 #ifndef GST_DISABLE_OPTION_PARSING
944 ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
945 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
946 g_option_context_add_group (ctx, gst_init_get_option_group ());
947 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
949 g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
951 g_printerr ("Error initializing: Unknown error!\n");
954 g_option_context_free (ctx);
956 gst_init (&argc, &argv);
959 gst_tools_print_version ();
966 /* make a null-terminated version of argv */
967 argvn = g_new0 (char *, argc);
968 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
971 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
977 g_printerr (_("ERROR: pipeline could not be constructed: %s.\n"),
978 GST_STR_NULL (error->message));
979 g_error_free (error);
981 g_printerr (_("ERROR: pipeline could not be constructed.\n"));
985 g_printerr (_("WARNING: erroneous pipeline: %s\n"),
986 GST_STR_NULL (error->message));
987 g_error_free (error);
992 gchar **exclude_list =
993 exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
994 deep_notify_id = g_signal_connect (pipeline, "deep-notify",
995 G_CALLBACK (gst_object_default_deep_notify), exclude_list);
999 GstState state, pending;
1000 GstStateChangeReturn ret;
1003 /* If the top-level object is not a pipeline, place it in a pipeline. */
1004 if (!GST_IS_PIPELINE (pipeline)) {
1005 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
1007 if (real_pipeline == NULL) {
1008 g_printerr (_("ERROR: the 'pipeline' element wasn't found.\n"));
1011 gst_bin_add (GST_BIN (real_pipeline), pipeline);
1012 pipeline = real_pipeline;
1016 /* gst_index_new() creates a null-index, it does not store anything, but
1017 * the entry-added signal works and this is what we use to build the
1019 index = gst_index_new ();
1021 index_stats = g_ptr_array_new ();
1022 g_signal_connect (G_OBJECT (index), "entry-added",
1023 G_CALLBACK (entry_added), index_stats);
1024 g_object_set (G_OBJECT (index), "resolver", GST_INDEX_RESOLVER_GTYPE,
1026 gst_element_set_index (pipeline, index);
1031 bus = gst_element_get_bus (pipeline);
1032 gst_bus_set_sync_handler (bus, bus_sync_handler, (gpointer) pipeline, NULL);
1033 gst_object_unref (bus);
1035 PRINT (_("Setting pipeline to PAUSED ...\n"));
1036 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
1039 case GST_STATE_CHANGE_FAILURE:
1040 g_printerr (_("ERROR: Pipeline doesn't want to pause.\n"));
1042 event_loop (pipeline, FALSE, FALSE, GST_STATE_VOID_PENDING);
1044 case GST_STATE_CHANGE_NO_PREROLL:
1045 PRINT (_("Pipeline is live and does not need PREROLL ...\n"));
1048 case GST_STATE_CHANGE_ASYNC:
1049 PRINT (_("Pipeline is PREROLLING ...\n"));
1050 caught_error = event_loop (pipeline, TRUE, TRUE, GST_STATE_PAUSED);
1052 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1056 state = GST_STATE_PAUSED;
1058 case GST_STATE_CHANGE_SUCCESS:
1059 PRINT (_("Pipeline is PREROLLED ...\n"));
1063 caught_error = event_loop (pipeline, FALSE, TRUE, GST_STATE_PLAYING);
1066 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1069 GstClockTime tfthen, tfnow;
1070 GstClockTimeDiff diff;
1072 PRINT (_("Setting pipeline to PLAYING ...\n"));
1074 if (gst_element_set_state (pipeline,
1075 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
1076 GstMessage *err_msg;
1079 g_printerr (_("ERROR: pipeline doesn't want to play.\n"));
1080 bus = gst_element_get_bus (pipeline);
1081 if ((err_msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0))) {
1082 print_error_message (err_msg);
1083 gst_message_unref (err_msg);
1085 gst_object_unref (bus);
1090 tfthen = gst_util_get_timestamp ();
1091 caught_error = event_loop (pipeline, TRUE, FALSE, GST_STATE_PLAYING);
1092 if (eos_on_shutdown && caught_error != ELR_NO_ERROR) {
1093 gboolean ignore_errors;
1096 if (caught_error == ELR_INTERRUPT) {
1097 PRINT (_("EOS on shutdown enabled -- Forcing EOS on the pipeline\n"));
1098 gst_element_send_event (pipeline, gst_event_new_eos ());
1099 ignore_errors = FALSE;
1101 PRINT (_("EOS on shutdown enabled -- waiting for EOS after Error\n"));
1102 ignore_errors = TRUE;
1104 PRINT (_("Waiting for EOS...\n"));
1107 caught_error = event_loop (pipeline, TRUE, FALSE, GST_STATE_PLAYING);
1109 if (caught_error == ELR_NO_ERROR) {
1111 PRINT (_("EOS received - stopping pipeline...\n"));
1113 } else if (caught_error == ELR_INTERRUPT) {
1115 ("Interrupt while waiting for EOS - stopping pipeline...\n"));
1118 } else if (caught_error == ELR_ERROR) {
1119 if (!ignore_errors) {
1120 PRINT (_("An error happened while waiting for EOS\n"));
1127 tfnow = gst_util_get_timestamp ();
1129 diff = GST_CLOCK_DIFF (tfthen, tfnow);
1131 PRINT (_("Execution ended after %" GST_TIME_FORMAT "\n"),
1132 GST_TIME_ARGS (diff));
1135 PRINT (_("Setting pipeline to PAUSED ...\n"));
1136 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1137 if (caught_error == ELR_NO_ERROR)
1138 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1140 /* iterate mainloop to process pending stuff */
1141 while (g_main_context_iteration (NULL, FALSE));
1143 /* No need to see all those pad caps going to NULL etc., it's just noise */
1144 if (deep_notify_id != 0)
1145 g_signal_handler_disconnect (pipeline, deep_notify_id);
1147 PRINT (_("Setting pipeline to READY ...\n"));
1148 gst_element_set_state (pipeline, GST_STATE_READY);
1149 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1153 print_index_stats (index_stats);
1154 g_ptr_array_free (index_stats, TRUE);
1159 PRINT (_("Setting pipeline to NULL ...\n"));
1160 gst_element_set_state (pipeline, GST_STATE_NULL);
1163 PRINT (_("Freeing pipeline ...\n"));
1164 gst_object_unref (pipeline);