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 G_LOCK_DEFINE_STATIC (context);
67 static GstContext *context = NULL;
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);
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_assert_not_reached ();
346 } else if (gst_tag_get_type (tag) == GST_TYPE_SAMPLE) {
347 GstSample *sample = NULL;
349 if (gst_tag_list_get_sample_index (list, tag, i, &sample)) {
350 GstBuffer *img = gst_sample_get_buffer (sample);
351 GstCaps *caps = gst_sample_get_caps (sample);
357 caps_str = gst_caps_to_string (caps);
358 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes, "
359 "type: %s", gst_buffer_get_size (img), caps_str);
362 str = g_strdup_printf ("buffer of %" G_GSIZE_FORMAT " bytes",
363 gst_buffer_get_size (img));
366 str = g_strdup ("NULL buffer");
369 } else if (gst_tag_get_type (tag) == GST_TYPE_DATE_TIME) {
370 GstDateTime *dt = NULL;
372 gst_tag_list_get_date_time_index (list, tag, i, &dt);
373 if (!gst_date_time_has_time (dt)) {
374 str = gst_date_time_to_iso8601_string (dt);
376 gdouble tz_offset = gst_date_time_get_time_zone_offset (dt);
379 if (tz_offset != 0.0) {
380 g_snprintf (tz_str, sizeof (tz_str), "(UTC %s%gh)",
381 (tz_offset > 0.0) ? "+" : "", tz_offset);
383 g_snprintf (tz_str, sizeof (tz_str), "(UTC)");
386 str = g_strdup_printf ("%04u-%02u-%02u %02u:%02u:%02u %s",
387 gst_date_time_get_year (dt), gst_date_time_get_month (dt),
388 gst_date_time_get_day (dt), gst_date_time_get_hour (dt),
389 gst_date_time_get_minute (dt), gst_date_time_get_second (dt),
392 gst_date_time_unref (dt);
395 g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
399 PRINT ("%16s: %s\n", gst_tag_get_nick (tag), str);
401 PRINT ("%16s: %s\n", "", str);
409 print_tag_foreach (const GstTagList * tags, const gchar * tag,
414 gint depth = GPOINTER_TO_INT (user_data);
416 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 /* As the interrupt handler is dispatched from GMainContext as a GSourceFunc
466 * handler, we can react to this by posting a message. */
468 intr_handler (gpointer user_data)
470 GstElement *pipeline = (GstElement *) user_data;
472 PRINT ("handling interrupt.\n");
474 /* post an application specific message */
475 gst_element_post_message (GST_ELEMENT (pipeline),
476 gst_message_new_application (GST_OBJECT (pipeline),
477 gst_structure_new ("GstLaunchInterrupt",
478 "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));
480 /* remove signal handler */
484 #endif /* G_OS_UNIX */
487 merge_structures (GQuark field_id, const GValue * value, gpointer user_data)
489 GstStructure *s2 = user_data;
491 /* Copy all fields that are not set yet */
492 if (!gst_structure_id_has_field (s2, field_id))
493 gst_structure_id_set_value (s2, field_id, value);
498 /* returns ELR_ERROR if there was an error
499 * or ELR_INTERRUPT if we caught a keyboard interrupt
500 * or ELR_NO_ERROR otherwise. */
501 static EventLoopResult
502 event_loop (GstElement * pipeline, gboolean blocking, gboolean do_progress,
503 GstState target_state)
506 guint signal_watch_id;
509 GstMessage *message = NULL;
510 EventLoopResult res = ELR_NO_ERROR;
511 gboolean buffering = FALSE, in_progress = FALSE;
512 gboolean prerolled = target_state != GST_STATE_PAUSED;
514 bus = gst_element_get_bus (GST_ELEMENT (pipeline));
518 g_unix_signal_add (SIGINT, (GSourceFunc) intr_handler, pipeline);
522 message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
524 /* if the poll timed out, only when !blocking */
528 /* check if we need to dump messages to the console */
531 const GstStructure *s;
534 seqnum = gst_message_get_seqnum (message);
536 s = gst_message_get_structure (message);
538 src_obj = GST_MESSAGE_SRC (message);
540 if (GST_IS_ELEMENT (src_obj)) {
541 PRINT (_("Got message #%u from element \"%s\" (%s): "),
542 (guint) seqnum, GST_ELEMENT_NAME (src_obj),
543 GST_MESSAGE_TYPE_NAME (message));
544 } else if (GST_IS_PAD (src_obj)) {
545 PRINT (_("Got message #%u from pad \"%s:%s\" (%s): "),
546 (guint) seqnum, GST_DEBUG_PAD_NAME (src_obj),
547 GST_MESSAGE_TYPE_NAME (message));
548 } else if (GST_IS_OBJECT (src_obj)) {
549 PRINT (_("Got message #%u from object \"%s\" (%s): "),
550 (guint) seqnum, GST_OBJECT_NAME (src_obj),
551 GST_MESSAGE_TYPE_NAME (message));
553 PRINT (_("Got message #%u (%s): "), (guint) seqnum,
554 GST_MESSAGE_TYPE_NAME (message));
560 sstr = gst_structure_to_string (s);
561 PRINT ("%s\n", sstr);
564 PRINT ("no message details\n");
568 switch (GST_MESSAGE_TYPE (message)) {
569 case GST_MESSAGE_NEW_CLOCK:
573 gst_message_parse_new_clock (message, &clock);
575 PRINT ("New clock: %s\n", (clock ? GST_OBJECT_NAME (clock) : "NULL"));
578 case GST_MESSAGE_CLOCK_LOST:
579 PRINT ("Clock lost, selecting a new one\n");
580 gst_element_set_state (pipeline, GST_STATE_PAUSED);
581 gst_element_set_state (pipeline, GST_STATE_PLAYING);
583 case GST_MESSAGE_EOS:{
585 PRINT (_("Got EOS from element \"%s\".\n"),
586 GST_MESSAGE_SRC_NAME (message));
589 case GST_MESSAGE_TAG:
591 GstTagList *tag_list;
593 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
594 PRINT (_("FOUND TAG : found by element \"%s\".\n"),
595 GST_MESSAGE_SRC_NAME (message));
596 } else if (GST_IS_PAD (GST_MESSAGE_SRC (message))) {
597 PRINT (_("FOUND TAG : found by pad \"%s:%s\".\n"),
598 GST_DEBUG_PAD_NAME (GST_MESSAGE_SRC (message)));
599 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
600 PRINT (_("FOUND TAG : found by object \"%s\".\n"),
601 GST_MESSAGE_SRC_NAME (message));
603 PRINT (_("FOUND TAG\n"));
606 gst_message_parse_tag (message, &tag_list);
607 gst_tag_list_foreach (tag_list, print_tag, NULL);
608 gst_tag_list_unref (tag_list);
611 case GST_MESSAGE_TOC:
617 if (GST_IS_ELEMENT (GST_MESSAGE_SRC (message))) {
618 PRINT (_("FOUND TOC : found by element \"%s\".\n"),
619 GST_MESSAGE_SRC_NAME (message));
620 } else if (GST_IS_OBJECT (GST_MESSAGE_SRC (message))) {
621 PRINT (_("FOUND TOC : found by object \"%s\".\n"),
622 GST_MESSAGE_SRC_NAME (message));
624 PRINT (_("FOUND TOC\n"));
627 gst_message_parse_toc (message, &toc, &updated);
628 /* recursively loop over toc entries */
629 entries = gst_toc_get_entries (toc);
630 g_list_foreach (entries, print_toc_entry, GUINT_TO_POINTER (0));
634 case GST_MESSAGE_INFO:{
637 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
639 gst_message_parse_info (message, &gerror, &debug);
641 PRINT (_("INFO:\n%s\n"), debug);
643 g_error_free (gerror);
648 case GST_MESSAGE_WARNING:{
651 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
653 /* dump graph on warning */
654 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
655 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.warning");
657 gst_message_parse_warning (message, &gerror, &debug);
658 PRINT (_("WARNING: from element %s: %s\n"), name, gerror->message);
660 PRINT (_("Additional debug info:\n%s\n"), debug);
662 g_error_free (gerror);
667 case GST_MESSAGE_ERROR:{
668 /* dump graph on error */
669 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
670 GST_DEBUG_GRAPH_SHOW_ALL, "gst-launch.error");
672 print_error_message (message);
674 /* we have an error */
678 case GST_MESSAGE_STATE_CHANGED:{
679 GstState old, new, pending;
681 /* we only care about pipeline state change messages */
682 if (GST_MESSAGE_SRC (message) != GST_OBJECT_CAST (pipeline))
685 gst_message_parse_state_changed (message, &old, &new, &pending);
687 /* if we reached the final target state, exit */
688 if (target_state == GST_STATE_PAUSED && new == target_state) {
690 /* ignore when we are buffering since then we mess with the states
693 PRINT (_("Prerolled, waiting for buffering to finish...\n"));
697 PRINT (_("Prerolled, waiting for progress to finish...\n"));
702 /* else not an interesting message */
705 case GST_MESSAGE_BUFFERING:{
708 gst_message_parse_buffering (message, &percent);
709 PRINT ("%s %d%% \r", _("buffering..."), percent);
711 /* no state management needed for live pipelines */
715 if (percent == 100) {
716 /* a 100% message means buffering is done */
718 /* if the desired state is playing, go back */
719 if (target_state == GST_STATE_PLAYING) {
720 PRINT (_("Done buffering, setting pipeline to PLAYING ...\n"));
721 gst_element_set_state (pipeline, GST_STATE_PLAYING);
722 } else if (prerolled && !in_progress)
726 if (buffering == FALSE && target_state == GST_STATE_PLAYING) {
727 /* we were not buffering but PLAYING, PAUSE the pipeline. */
728 PRINT (_("Buffering, setting pipeline to PAUSED ...\n"));
729 gst_element_set_state (pipeline, GST_STATE_PAUSED);
735 case GST_MESSAGE_LATENCY:
737 PRINT (_("Redistribute latency...\n"));
738 gst_bin_recalculate_latency (GST_BIN (pipeline));
741 case GST_MESSAGE_REQUEST_STATE:
744 gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (message));
746 gst_message_parse_request_state (message, &state);
748 PRINT (_("Setting state to %s as requested by %s...\n"),
749 gst_element_state_get_name (state), name);
751 gst_element_set_state (pipeline, state);
756 case GST_MESSAGE_APPLICATION:{
757 const GstStructure *s;
759 s = gst_message_get_structure (message);
761 if (gst_structure_has_name (s, "GstLaunchInterrupt")) {
762 /* this application message is posted when we caught an interrupt and
763 * we need to stop the pipeline. */
764 PRINT (_("Interrupt: Stopping pipeline ...\n"));
770 case GST_MESSAGE_PROGRESS:
772 GstProgressType type;
775 gst_message_parse_progress (message, &type, &code, &text);
778 case GST_PROGRESS_TYPE_START:
779 case GST_PROGRESS_TYPE_CONTINUE:
785 case GST_PROGRESS_TYPE_COMPLETE:
786 case GST_PROGRESS_TYPE_CANCELED:
787 case GST_PROGRESS_TYPE_ERROR:
793 PRINT (_("Progress: (%s) %s\n"), code, text);
797 if (do_progress && !in_progress && !buffering && prerolled)
801 case GST_MESSAGE_ELEMENT:{
802 if (gst_is_missing_plugin_message (message)) {
805 desc = gst_missing_plugin_message_get_description (message);
806 PRINT (_("Missing element: %s\n"), desc ? desc : "(no description)");
810 case GST_MESSAGE_HAVE_CONTEXT:{
811 GstContext *context_new;
814 gst_message_parse_have_context (message, &context_new);
817 gst_structure_to_string (gst_context_get_structure (context_new));
818 PRINT (_("Got context from element '%s': %s\n"),
819 GST_ELEMENT_NAME (GST_MESSAGE_SRC (message)), context_str);
820 g_free (context_str);
821 gst_context_unref (context_new);
823 /* The contexts were merged in the sync handler already, here
824 * now just print them and propagate the merged context to the
825 * complete pipeline */
827 context_new = gst_context_ref (context_new);
829 gst_element_set_context (pipeline, context_new);
830 gst_context_unref (context_new);
834 /* just be quiet by default */
838 gst_message_unref (message);
840 g_assert_not_reached ();
845 gst_message_unref (message);
846 gst_object_unref (bus);
848 g_source_remove (signal_watch_id);
854 static GstBusSyncReply
855 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer data)
857 GstElement *pipeline = (GstElement *) data;
859 switch (GST_MESSAGE_TYPE (message)) {
860 case GST_MESSAGE_STATE_CHANGED:
861 /* we only care about pipeline state change messages */
862 if (GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
863 GstState old, new, pending;
864 gchar *state_transition_name;
866 gst_message_parse_state_changed (message, &old, &new, &pending);
868 state_transition_name = g_strdup_printf ("%s_%s",
869 gst_element_state_get_name (old), gst_element_state_get_name (new));
871 /* dump graph for (some) pipeline state changes */
873 gchar *dump_name = g_strconcat ("gst-launch.", state_transition_name,
875 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pipeline),
876 GST_DEBUG_GRAPH_SHOW_ALL, dump_name);
880 /* place a marker into e.g. strace logs */
882 gchar *access_name = g_strconcat (g_get_tmp_dir (), G_DIR_SEPARATOR_S,
883 "gst-launch", G_DIR_SEPARATOR_S, state_transition_name, NULL);
884 g_file_test (access_name, G_FILE_TEST_EXISTS);
885 g_free (access_name);
888 g_free (state_transition_name);
890 case GST_MESSAGE_NEED_CONTEXT:{
892 /* We could filter something here, but instead we can also just pass the complete
893 * context knowledge we have to the element. If we have what it needs, it will be
894 * happy, otherwise we can't do anything else anyway */
896 gst_element_set_context (GST_ELEMENT_CAST (GST_MESSAGE_SRC (message)),
902 case GST_MESSAGE_HAVE_CONTEXT:{
903 GstContext *context_new;
905 gst_message_parse_have_context (message, &context_new);
907 /* Merge the contexts here as soon as possible and not
908 * in the async bus handler, in case something asks for
909 * a specific context before the async bus handler is run.
911 * Don't set the context on the complete pipeline here as it
912 * might deadlock, but do that from the async bus handler
917 const GstStructure *s1;
920 /* Merge structures */
921 context = gst_context_make_writable (context);
922 s1 = gst_context_get_structure (context_new);
923 s2 = gst_context_writable_structure (context);
924 gst_structure_foreach (s1, merge_structures, s2);
926 /* Copy over the context */
927 gst_context_replace (&context, context_new);
929 gst_context_unref (context_new);
940 main (int argc, char *argv[])
943 gboolean verbose = FALSE;
944 gboolean no_fault = FALSE;
945 gboolean eos_on_shutdown = FALSE;
947 gboolean check_index = FALSE;
949 gchar *savefile = NULL;
950 gchar *exclude_args = NULL;
951 #ifndef GST_DISABLE_OPTION_PARSING
952 GOptionEntry options[] = {
953 {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
954 N_("Output tags (also known as metadata)"), NULL},
955 {"toc", 'c', 0, G_OPTION_ARG_NONE, &toc,
956 N_("Output TOC (chapters and editions)"), NULL},
957 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
958 N_("Output status information and property notifications"), NULL},
959 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
960 N_("Do not print any progress information"), NULL},
961 {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
962 N_("Output messages"), NULL},
963 {"exclude", 'X', 0, G_OPTION_ARG_NONE, &exclude_args,
964 N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
965 {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
966 N_("Do not install a fault handler"), NULL},
967 {"eos-on-shutdown", 'e', 0, G_OPTION_ARG_NONE, &eos_on_shutdown,
968 N_("Force EOS on sources before shutting the pipeline down"), NULL},
970 {"index", 'i', 0, G_OPTION_ARG_NONE, &check_index,
971 N_("Gather and print index statistics"), NULL},
973 GST_TOOLS_GOPTION_VERSION,
981 GPtrArray *index_stats = NULL;
984 GError *error = NULL;
987 free (malloc (8)); /* -lefence */
990 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
991 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
992 textdomain (GETTEXT_PACKAGE);
995 g_set_prgname ("gst-launch-" GST_API_VERSION);
997 #ifndef GST_DISABLE_OPTION_PARSING
998 ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
999 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
1000 g_option_context_add_group (ctx, gst_init_get_option_group ());
1001 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
1003 g_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message));
1005 g_printerr ("Error initializing: Unknown error!\n");
1008 g_option_context_free (ctx);
1010 gst_init (&argc, &argv);
1013 gst_tools_print_version ();
1020 /* make a null-terminated version of argv */
1021 argvn = g_new0 (char *, argc);
1022 memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
1025 (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
1031 g_printerr (_("ERROR: pipeline could not be constructed: %s.\n"),
1032 GST_STR_NULL (error->message));
1033 g_error_free (error);
1035 g_printerr (_("ERROR: pipeline could not be constructed.\n"));
1039 g_printerr (_("WARNING: erroneous pipeline: %s\n"),
1040 GST_STR_NULL (error->message));
1041 g_error_free (error);
1046 gchar **exclude_list =
1047 exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
1048 g_signal_connect (pipeline, "deep-notify",
1049 G_CALLBACK (gst_object_default_deep_notify), exclude_list);
1053 GstState state, pending;
1054 GstStateChangeReturn ret;
1057 /* If the top-level object is not a pipeline, place it in a pipeline. */
1058 if (!GST_IS_PIPELINE (pipeline)) {
1059 GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
1061 if (real_pipeline == NULL) {
1062 g_printerr (_("ERROR: the 'pipeline' element wasn't found.\n"));
1065 gst_bin_add (GST_BIN (real_pipeline), pipeline);
1066 pipeline = real_pipeline;
1070 /* gst_index_new() creates a null-index, it does not store anything, but
1071 * the entry-added signal works and this is what we use to build the
1073 index = gst_index_new ();
1075 index_stats = g_ptr_array_new ();
1076 g_signal_connect (G_OBJECT (index), "entry-added",
1077 G_CALLBACK (entry_added), index_stats);
1078 g_object_set (G_OBJECT (index), "resolver", GST_INDEX_RESOLVER_GTYPE,
1080 gst_element_set_index (pipeline, index);
1085 bus = gst_element_get_bus (pipeline);
1086 gst_bus_set_sync_handler (bus, bus_sync_handler, (gpointer) pipeline, NULL);
1087 gst_object_unref (bus);
1089 PRINT (_("Setting pipeline to PAUSED ...\n"));
1090 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
1093 case GST_STATE_CHANGE_FAILURE:
1094 g_printerr (_("ERROR: Pipeline doesn't want to pause.\n"));
1096 event_loop (pipeline, FALSE, FALSE, GST_STATE_VOID_PENDING);
1098 case GST_STATE_CHANGE_NO_PREROLL:
1099 PRINT (_("Pipeline is live and does not need PREROLL ...\n"));
1102 case GST_STATE_CHANGE_ASYNC:
1103 PRINT (_("Pipeline is PREROLLING ...\n"));
1104 caught_error = event_loop (pipeline, TRUE, TRUE, GST_STATE_PAUSED);
1106 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1109 state = GST_STATE_PAUSED;
1111 case GST_STATE_CHANGE_SUCCESS:
1112 PRINT (_("Pipeline is PREROLLED ...\n"));
1116 caught_error = event_loop (pipeline, FALSE, TRUE, GST_STATE_PLAYING);
1119 g_printerr (_("ERROR: pipeline doesn't want to preroll.\n"));
1121 GstClockTime tfthen, tfnow;
1122 GstClockTimeDiff diff;
1124 PRINT (_("Setting pipeline to PLAYING ...\n"));
1126 if (gst_element_set_state (pipeline,
1127 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
1128 GstMessage *err_msg;
1131 g_printerr (_("ERROR: pipeline doesn't want to play.\n"));
1132 bus = gst_element_get_bus (pipeline);
1133 if ((err_msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0))) {
1134 print_error_message (err_msg);
1135 gst_message_unref (err_msg);
1137 gst_object_unref (bus);
1142 tfthen = gst_util_get_timestamp ();
1143 caught_error = event_loop (pipeline, TRUE, FALSE, GST_STATE_PLAYING);
1144 if (eos_on_shutdown && caught_error != ELR_NO_ERROR) {
1145 gboolean ignore_errors;
1148 if (caught_error == ELR_INTERRUPT) {
1149 PRINT (_("EOS on shutdown enabled -- Forcing EOS on the pipeline\n"));
1150 gst_element_send_event (pipeline, gst_event_new_eos ());
1151 ignore_errors = FALSE;
1153 PRINT (_("EOS on shutdown enabled -- waiting for EOS after Error\n"));
1154 ignore_errors = TRUE;
1156 PRINT (_("Waiting for EOS...\n"));
1159 caught_error = event_loop (pipeline, TRUE, FALSE, GST_STATE_PLAYING);
1161 if (caught_error == ELR_NO_ERROR) {
1163 PRINT (_("EOS received - stopping pipeline...\n"));
1165 } else if (caught_error == ELR_INTERRUPT) {
1167 ("Interrupt while waiting for EOS - stopping pipeline...\n"));
1169 } else if (caught_error == ELR_ERROR) {
1170 if (!ignore_errors) {
1171 PRINT (_("An error happened while waiting for EOS\n"));
1177 tfnow = gst_util_get_timestamp ();
1179 diff = GST_CLOCK_DIFF (tfthen, tfnow);
1181 PRINT (_("Execution ended after %" GST_TIME_FORMAT "\n"),
1182 GST_TIME_ARGS (diff));
1185 PRINT (_("Setting pipeline to PAUSED ...\n"));
1186 gst_element_set_state (pipeline, GST_STATE_PAUSED);
1187 if (caught_error == ELR_NO_ERROR)
1188 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1190 /* iterate mainloop to process pending stuff */
1191 while (g_main_context_iteration (NULL, FALSE));
1193 PRINT (_("Setting pipeline to READY ...\n"));
1194 gst_element_set_state (pipeline, GST_STATE_READY);
1195 gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
1199 print_index_stats (index_stats);
1200 g_ptr_array_free (index_stats, TRUE);
1205 PRINT (_("Setting pipeline to NULL ...\n"));
1206 gst_element_set_state (pipeline, GST_STATE_NULL);
1209 PRINT (_("Freeing pipeline ...\n"));
1210 gst_object_unref (pipeline);
1211 gst_context_replace (&context, NULL);