1 /* GStreamer command line playback testing utility
3 * Copyright (C) 2013-2014 Tim-Philipp Müller <tim centricular net>
4 * Copyright (C) 2013 Collabora Ltd.
5 * Copyright (C) 2015 Centricular Ltd
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
30 #include <gst/gst-i18n-app.h>
31 #include <gst/audio/audio.h>
32 #include <gst/video/video.h>
33 #include <gst/pbutils/pbutils.h>
34 #include <gst/tag/tag.h>
35 #include <gst/math-compat.h>
40 #include <glib/gprintf.h>
42 #include "gst-play-kb.h"
44 #define VOLUME_STEPS 20
46 static gboolean wait_on_eos = FALSE;
48 GST_DEBUG_CATEGORY (play_debug);
49 #define GST_CAT_DEFAULT play_debug
53 GST_PLAY_TRICK_MODE_NONE = 0,
54 GST_PLAY_TRICK_MODE_DEFAULT,
55 GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO,
56 GST_PLAY_TRICK_MODE_KEY_UNITS,
57 GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO,
58 GST_PLAY_TRICK_MODE_INSTANT_RATE,
59 GST_PLAY_TRICK_MODE_LAST
64 GST_PLAY_TRACK_TYPE_INVALID = 0,
65 GST_PLAY_TRACK_TYPE_AUDIO,
66 GST_PLAY_TRACK_TYPE_VIDEO,
67 GST_PLAY_TRACK_TYPE_SUBTITLE
78 /* playbin3 variables */
80 GstStreamCollection *collection;
84 GMutex selection_lock;
90 /* missing plugin messages */
96 GstState desired_state; /* as per user interaction, PAUSED or PLAYING */
98 gulong deep_notify_id;
103 GstPlayTrickMode trick_mode;
107 static gboolean quiet = FALSE;
108 static gboolean instant_rate_changes = FALSE;
110 static gboolean play_bus_msg (GstBus * bus, GstMessage * msg, gpointer data);
111 static gboolean play_next (GstPlay * play);
112 static gboolean play_prev (GstPlay * play);
113 static gboolean play_timeout (gpointer user_data);
114 static void play_about_to_finish (GstElement * playbin, gpointer user_data);
115 static void play_reset (GstPlay * play);
116 static void play_set_relative_volume (GstPlay * play, gdouble volume_step);
117 static gboolean play_do_seek (GstPlay * play, gint64 pos, gdouble rate,
118 GstPlayTrickMode mode);
121 static void gst_play_printf (const gchar * format, ...) G_GNUC_PRINTF (1, 2);
124 static void keyboard_cb (const gchar * key_input, gpointer user_data);
125 static void relative_seek (GstPlay * play, gdouble percent);
128 gst_play_printf (const gchar * format, ...)
137 va_start (args, format);
139 len = g_vasprintf (&str, format, args);
143 if (len > 0 && str != NULL)
144 gst_print ("%s", str);
149 #define gst_print gst_play_printf
152 play_new (gchar ** uris, const gchar * audio_sink, const gchar * video_sink,
153 gboolean gapless, gdouble initial_volume, gboolean verbose,
154 const gchar * flags_string, gboolean use_playbin3)
156 GstElement *sink, *playbin;
161 playbin = gst_element_factory_make ("playbin3", "playbin");
163 playbin = gst_element_factory_make ("playbin", "playbin");
169 play = g_new0 (GstPlay, 1);
172 play->num_uris = g_strv_length (uris);
175 play->playbin = playbin;
178 play->is_playbin3 = TRUE;
180 const gchar *env = g_getenv ("USE_PLAYBIN3");
181 if (env && g_str_has_prefix (env, "1"))
182 play->is_playbin3 = TRUE;
185 g_mutex_init (&play->selection_lock);
187 if (audio_sink != NULL) {
188 if (strchr (audio_sink, ' ') != NULL)
189 sink = gst_parse_bin_from_description (audio_sink, TRUE, NULL);
191 sink = gst_element_factory_make (audio_sink, NULL);
194 g_object_set (play->playbin, "audio-sink", sink, NULL);
196 g_warning ("Couldn't create specified audio sink '%s'", audio_sink);
198 if (video_sink != NULL) {
199 if (strchr (video_sink, ' ') != NULL)
200 sink = gst_parse_bin_from_description (video_sink, TRUE, NULL);
202 sink = gst_element_factory_make (video_sink, NULL);
205 g_object_set (play->playbin, "video-sink", sink, NULL);
207 g_warning ("Couldn't create specified video sink '%s'", video_sink);
210 if (flags_string != NULL) {
215 g_object_class_find_property (G_OBJECT_GET_CLASS (playbin), "flags");
216 g_value_init (&val, pspec->value_type);
217 if (gst_value_deserialize (&val, flags_string))
218 g_object_set_property (G_OBJECT (play->playbin), "flags", &val);
220 gst_printerr ("Couldn't convert '%s' to playbin flags!\n", flags_string);
221 g_value_unset (&val);
225 play->deep_notify_id =
226 gst_element_add_property_deep_notify_watch (play->playbin, NULL, TRUE);
229 play->loop = g_main_loop_new (NULL, FALSE);
231 play->bus_watch = gst_bus_add_watch (GST_ELEMENT_BUS (play->playbin),
234 /* FIXME: make configurable incl. 0 for disable */
235 play->timeout = g_timeout_add (100, play_timeout, play);
237 play->missing = NULL;
238 play->buffering = FALSE;
239 play->is_live = FALSE;
241 play->desired_state = GST_STATE_PLAYING;
243 play->gapless = gapless;
245 g_signal_connect (play->playbin, "about-to-finish",
246 G_CALLBACK (play_about_to_finish), play);
249 if (initial_volume != -1)
250 play_set_relative_volume (play, initial_volume - 1.0);
253 play->trick_mode = GST_PLAY_TRICK_MODE_NONE;
259 play_free (GstPlay * play)
261 /* No need to see all those pad caps going to NULL etc., it's just noise */
262 if (play->deep_notify_id != 0)
263 g_signal_handler_disconnect (play->playbin, play->deep_notify_id);
267 gst_element_set_state (play->playbin, GST_STATE_NULL);
268 gst_object_unref (play->playbin);
270 g_source_remove (play->bus_watch);
271 g_source_remove (play->timeout);
272 g_main_loop_unref (play->loop);
274 g_strfreev (play->uris);
276 if (play->collection)
277 gst_object_unref (play->collection);
278 g_free (play->cur_audio_sid);
279 g_free (play->cur_video_sid);
280 g_free (play->cur_text_sid);
282 g_mutex_clear (&play->selection_lock);
287 /* reset for new file/stream */
289 play_reset (GstPlay * play)
291 g_list_foreach (play->missing, (GFunc) gst_message_unref, NULL);
292 play->missing = NULL;
294 play->buffering = FALSE;
295 play->is_live = FALSE;
299 play_set_relative_volume (GstPlay * play, gdouble volume_step)
303 volume = gst_stream_volume_get_volume (GST_STREAM_VOLUME (play->playbin),
304 GST_STREAM_VOLUME_FORMAT_CUBIC);
306 volume = round ((volume + volume_step) * VOLUME_STEPS) / VOLUME_STEPS;
307 volume = CLAMP (volume, 0.0, 10.0);
309 gst_stream_volume_set_volume (GST_STREAM_VOLUME (play->playbin),
310 GST_STREAM_VOLUME_FORMAT_CUBIC, volume);
312 gst_print (_("Volume: %.0f%%"), volume * 100);
317 play_toggle_audio_mute (GstPlay * play)
321 mute = gst_stream_volume_get_mute (GST_STREAM_VOLUME (play->playbin));
324 gst_stream_volume_set_mute (GST_STREAM_VOLUME (play->playbin), mute);
327 gst_print (_("Mute: on"));
329 gst_print (_("Mute: off"));
333 /* returns TRUE if something was installed and we should restart playback */
335 play_install_missing_plugins (GstPlay * play)
337 /* FIXME: implement: try to install any missing plugins we haven't
338 * tried to install before */
343 play_bus_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
345 GstPlay *play = user_data;
347 switch (GST_MESSAGE_TYPE (msg)) {
348 case GST_MESSAGE_ASYNC_DONE:
350 /* dump graph on preroll */
351 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
352 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.async-done");
354 gst_print ("Prerolled.\r");
355 if (play->missing != NULL && play_install_missing_plugins (play)) {
356 gst_print ("New plugins installed, trying again...\n");
361 case GST_MESSAGE_BUFFERING:{
364 if (!play->buffering)
367 gst_message_parse_buffering (msg, &percent);
368 gst_print ("%s %d%% \r", _("Buffering..."), percent);
370 if (percent == 100) {
371 /* a 100% message means buffering is done */
372 if (play->buffering) {
373 play->buffering = FALSE;
374 /* no state management needed for live pipelines */
376 gst_element_set_state (play->playbin, play->desired_state);
380 if (!play->buffering) {
382 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
383 play->buffering = TRUE;
388 case GST_MESSAGE_CLOCK_LOST:{
389 gst_print (_("Clock lost, selecting a new one\n"));
390 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
391 gst_element_set_state (play->playbin, GST_STATE_PLAYING);
394 case GST_MESSAGE_LATENCY:
395 gst_print ("Redistribute latency...\n");
396 gst_bin_recalculate_latency (GST_BIN (play->playbin));
398 case GST_MESSAGE_REQUEST_STATE:{
402 name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
404 gst_message_parse_request_state (msg, &state);
406 gst_print ("Setting state to %s as requested by %s...\n",
407 gst_element_state_get_name (state), name);
409 gst_element_set_state (play->playbin, state);
413 case GST_MESSAGE_EOS:
414 /* print final position at end */
417 /* and switch to next item in list */
418 if (!wait_on_eos && !play_next (play)) {
419 gst_print ("%s\n", _("Reached end of play list."));
420 g_main_loop_quit (play->loop);
423 case GST_MESSAGE_WARNING:{
427 /* dump graph on warning */
428 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
429 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.warning");
431 gst_message_parse_warning (msg, &err, &dbg);
432 gst_printerr ("WARNING %s\n", err->message);
434 gst_printerr ("WARNING debug information: %s\n", dbg);
435 g_clear_error (&err);
439 case GST_MESSAGE_ERROR:{
443 /* dump graph on error */
444 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
445 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.error");
447 gst_message_parse_error (msg, &err, &dbg);
448 gst_printerr ("ERROR %s for %s\n", err->message,
449 play->uris[play->cur_idx]);
451 gst_printerr ("ERROR debug information: %s\n", dbg);
452 g_clear_error (&err);
455 /* flush any other error messages from the bus and clean up */
456 gst_element_set_state (play->playbin, GST_STATE_NULL);
458 if (play->missing != NULL && play_install_missing_plugins (play)) {
459 gst_print ("New plugins installed, trying again...\n");
464 /* try next item in list then */
465 if (!play_next (play)) {
466 gst_print ("%s\n", _("Reached end of play list."));
467 g_main_loop_quit (play->loop);
471 case GST_MESSAGE_ELEMENT:
473 GstNavigationMessageType mtype = gst_navigation_message_get_type (msg);
474 if (mtype == GST_NAVIGATION_MESSAGE_EVENT) {
477 if (gst_navigation_message_parse_event (msg, &ev)) {
478 GstNavigationEventType e_type = gst_navigation_event_get_type (ev);
480 case GST_NAVIGATION_EVENT_KEY_PRESS:
484 if (gst_navigation_event_parse_key_event (ev, &key)) {
485 GST_INFO ("Key press: %s", key);
487 if (strcmp (key, "Left") == 0)
488 key = GST_PLAY_KB_ARROW_LEFT;
489 else if (strcmp (key, "Right") == 0)
490 key = GST_PLAY_KB_ARROW_RIGHT;
491 else if (strcmp (key, "Up") == 0)
492 key = GST_PLAY_KB_ARROW_UP;
493 else if (strcmp (key, "Down") == 0)
494 key = GST_PLAY_KB_ARROW_DOWN;
495 else if (strcmp (key, "space") == 0 ||
496 strcmp (key, "Space") == 0)
498 else if (strlen (key) > 1)
501 keyboard_cb (key, user_data);
505 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
508 if (gst_navigation_event_parse_mouse_button_event (ev, &button,
512 relative_seek (play, +0.08);
513 } else if (button == 5) {
515 relative_seek (play, -0.01);
525 gst_event_unref (ev);
529 case GST_MESSAGE_PROPERTY_NOTIFY:{
533 gchar *val_str = NULL;
536 gst_message_parse_property_notify (msg, &obj, &name, &val);
538 obj_name = gst_object_get_path_string (GST_OBJECT (obj));
540 if (G_VALUE_HOLDS_STRING (val))
541 val_str = g_value_dup_string (val);
542 else if (G_VALUE_TYPE (val) == GST_TYPE_CAPS)
543 val_str = gst_caps_to_string (g_value_get_boxed (val));
544 else if (G_VALUE_TYPE (val) == GST_TYPE_TAG_LIST)
545 val_str = gst_tag_list_to_string (g_value_get_boxed (val));
547 val_str = gst_value_serialize (val);
549 val_str = g_strdup ("(no value)");
552 gst_play_printf ("%s: %s = %s\n", obj_name, name, val_str);
557 case GST_MESSAGE_STREAM_COLLECTION:
559 GstStreamCollection *collection = NULL;
560 gst_message_parse_stream_collection (msg, &collection);
563 g_mutex_lock (&play->selection_lock);
564 gst_object_replace ((GstObject **) & play->collection,
565 (GstObject *) collection);
566 g_mutex_unlock (&play->selection_lock);
570 case GST_MESSAGE_STREAMS_SELECTED:
572 GstStreamCollection *collection = NULL;
575 gst_message_parse_streams_selected (msg, &collection);
577 g_mutex_lock (&play->selection_lock);
578 gst_object_replace ((GstObject **) & play->collection,
579 (GstObject *) collection);
581 /* Free all last stream-ids */
582 g_free (play->cur_audio_sid);
583 g_free (play->cur_video_sid);
584 g_free (play->cur_text_sid);
585 play->cur_audio_sid = NULL;
586 play->cur_video_sid = NULL;
587 play->cur_text_sid = NULL;
589 len = gst_message_streams_selected_get_size (msg);
590 for (i = 0; i < len; i++) {
591 GstStream *stream = gst_message_streams_selected_get_stream (msg, i);
593 GstStreamType type = gst_stream_get_stream_type (stream);
594 const gchar *stream_id = gst_stream_get_stream_id (stream);
596 if (type & GST_STREAM_TYPE_AUDIO) {
597 play->cur_audio_sid = g_strdup (stream_id);
598 } else if (type & GST_STREAM_TYPE_VIDEO) {
599 play->cur_video_sid = g_strdup (stream_id);
600 } else if (type & GST_STREAM_TYPE_TEXT) {
601 play->cur_text_sid = g_strdup (stream_id);
603 gst_print ("Unknown stream type with stream-id %s", stream_id);
605 gst_object_unref (stream);
609 gst_object_unref (collection);
610 g_mutex_unlock (&play->selection_lock);
615 if (gst_is_missing_plugin_message (msg)) {
618 desc = gst_missing_plugin_message_get_description (msg);
619 gst_print ("Missing plugin: %s\n", desc);
621 play->missing = g_list_append (play->missing, gst_message_ref (msg));
630 play_timeout (gpointer user_data)
632 GstPlay *play = user_data;
633 gint64 pos = -1, dur = -1;
634 const gchar *paused = _("Paused");
640 gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos);
641 gst_element_query_duration (play->playbin, GST_FORMAT_TIME, &dur);
643 if (play->desired_state == GST_STATE_PAUSED) {
644 status = (gchar *) paused;
646 gint len = g_utf8_strlen (paused, -1);
647 status = g_newa (gchar, len + 1);
648 memset (status, ' ', len);
652 if (pos >= 0 && dur > 0) {
653 gchar dstr[32], pstr[32];
655 /* FIXME: pretty print in nicer format */
656 g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
658 g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
660 gst_print ("%s / %s %s\r", pstr, dstr, status);
667 play_uri_get_display_name (GstPlay * play, const gchar * uri)
671 if (gst_uri_has_protocol (uri, "file")) {
672 loc = g_filename_from_uri (uri, NULL, NULL);
673 } else if (gst_uri_has_protocol (uri, "pushfile")) {
674 loc = g_filename_from_uri (uri + 4, NULL, NULL);
676 loc = g_strdup (uri);
679 /* Maybe additionally use glib's filename to display name function */
684 play_uri (GstPlay * play, const gchar * next_uri)
688 gst_element_set_state (play->playbin, GST_STATE_READY);
691 loc = play_uri_get_display_name (play, next_uri);
692 gst_print (_("Now playing %s\n"), loc);
695 g_object_set (play->playbin, "uri", next_uri, NULL);
697 switch (gst_element_set_state (play->playbin, GST_STATE_PAUSED)) {
698 case GST_STATE_CHANGE_FAILURE:
699 /* ignore, we should get an error message posted on the bus */
701 case GST_STATE_CHANGE_NO_PREROLL:
702 gst_print ("Pipeline is live.\n");
703 play->is_live = TRUE;
705 case GST_STATE_CHANGE_ASYNC:
706 gst_print ("Prerolling...\r");
712 if (play->desired_state != GST_STATE_PAUSED)
713 gst_element_set_state (play->playbin, play->desired_state);
716 /* returns FALSE if we have reached the end of the playlist */
718 play_next (GstPlay * play)
720 if ((play->cur_idx + 1) >= play->num_uris)
723 play_uri (play, play->uris[++play->cur_idx]);
727 /* returns FALSE if we have reached the beginning of the playlist */
729 play_prev (GstPlay * play)
731 if (play->cur_idx == 0 || play->num_uris <= 1)
734 play_uri (play, play->uris[--play->cur_idx]);
739 play_about_to_finish (GstElement * playbin, gpointer user_data)
741 GstPlay *play = user_data;
742 const gchar *next_uri;
749 next_idx = play->cur_idx + 1;
750 if (next_idx >= play->num_uris)
753 next_uri = play->uris[next_idx];
754 loc = play_uri_get_display_name (play, next_uri);
755 gst_print (_("About to finish, preparing next title: %s"), loc);
759 g_object_set (play->playbin, "uri", next_uri, NULL);
760 play->cur_idx = next_idx;
764 do_play (GstPlay * play)
769 for (i = 0; i < play->num_uris; ++i)
770 GST_INFO ("%4u : %s", i, play->uris[i]);
772 if (!play_next (play))
775 g_main_loop_run (play->loop);
779 compare (gconstpointer a, gconstpointer b)
784 a1 = g_utf8_collate_key_for_filename ((gchar *) a, -1);
785 b1 = g_utf8_collate_key_for_filename ((gchar *) b, -1);
786 ret = strcmp (a1, b1);
794 add_to_playlist (GPtrArray * playlist, const gchar * filename)
799 if (gst_uri_is_valid (filename)) {
800 g_ptr_array_add (playlist, g_strdup (filename));
804 if ((dir = g_dir_open (filename, 0, NULL))) {
806 GList *l, *files = NULL;
808 while ((entry = g_dir_read_name (dir))) {
811 path = g_build_filename (filename, entry, NULL);
812 files = g_list_insert_sorted (files, path, compare);
817 for (l = files; l != NULL; l = l->next) {
818 gchar *path = (gchar *) l->data;
820 add_to_playlist (playlist, path);
827 uri = gst_filename_to_uri (filename, NULL);
829 g_ptr_array_add (playlist, uri);
831 g_warning ("Could not make URI out of filename '%s'", filename);
835 shuffle_uris (gchar ** uris, guint num)
843 for (i = num - 1; i >= 1; i--) {
844 /* +1 because number returned will be in range [a;b[ so excl. stop */
845 j = g_random_int_range (0, i + 1);
853 restore_terminal (void)
855 gst_play_kb_set_key_handler (NULL, NULL);
859 toggle_paused (GstPlay * play)
861 if (play->desired_state == GST_STATE_PLAYING)
862 play->desired_state = GST_STATE_PAUSED;
864 play->desired_state = GST_STATE_PLAYING;
866 if (!play->buffering) {
867 gst_element_set_state (play->playbin, play->desired_state);
868 } else if (play->desired_state == GST_STATE_PLAYING) {
869 gst_print ("\nWill play as soon as buffering finishes)\n");
874 relative_seek (GstPlay * play, gdouble percent)
877 gboolean seekable = FALSE;
878 gint64 dur = -1, pos = -1, step;
880 g_return_if_fail (percent >= -1.0 && percent <= 1.0);
882 if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
885 query = gst_query_new_seeking (GST_FORMAT_TIME);
886 if (!gst_element_query (play->playbin, query)) {
887 gst_query_unref (query);
891 gst_query_parse_seeking (query, NULL, &seekable, NULL, &dur);
892 gst_query_unref (query);
894 if (!seekable || dur <= 0)
897 step = dur * percent;
898 if (ABS (step) < GST_SECOND)
899 step = (percent < 0) ? -GST_SECOND : GST_SECOND;
903 if (!play_next (play)) {
904 gst_print ("\n%s\n", _("Reached end of play list."));
905 g_main_loop_quit (play->loop);
911 play_do_seek (play, pos, play->rate, play->trick_mode);
918 gst_print ("\nCould not seek.\n");
923 play_set_rate_and_trick_mode (GstPlay * play, gdouble rate,
924 GstPlayTrickMode mode)
928 g_return_val_if_fail (rate != 0, FALSE);
930 if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
933 return play_do_seek (play, pos, rate, mode);
937 play_do_seek (GstPlay * play, gint64 pos, gdouble rate, GstPlayTrickMode mode)
939 GstSeekFlags seek_flags;
942 gboolean seekable = FALSE;
944 query = gst_query_new_seeking (GST_FORMAT_TIME);
945 if (!gst_element_query (play->playbin, query)) {
946 gst_query_unref (query);
950 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
951 gst_query_unref (query);
959 case GST_PLAY_TRICK_MODE_DEFAULT:
960 seek_flags |= GST_SEEK_FLAG_TRICKMODE;
962 case GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO:
963 seek_flags |= GST_SEEK_FLAG_TRICKMODE | GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;
965 case GST_PLAY_TRICK_MODE_KEY_UNITS:
966 seek_flags |= GST_SEEK_FLAG_TRICKMODE_KEY_UNITS;
968 case GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO:
970 GST_SEEK_FLAG_TRICKMODE_KEY_UNITS | GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;
972 case GST_PLAY_TRICK_MODE_NONE:
977 /* See if we can do an instant rate change (not changing dir) */
978 if (mode & GST_PLAY_TRICK_MODE_INSTANT_RATE && rate * play->rate > 0) {
979 seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
980 seek_flags | GST_SEEK_FLAG_INSTANT_RATE_CHANGE,
981 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE,
982 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
983 if (gst_element_send_event (play->playbin, seek)) {
988 /* No instant rate change, need to do a flushing seek */
989 seek_flags |= GST_SEEK_FLAG_FLUSH;
991 seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
992 seek_flags | GST_SEEK_FLAG_ACCURATE,
993 /* start */ GST_SEEK_TYPE_SET, pos,
994 /* stop */ GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
996 seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
997 seek_flags | GST_SEEK_FLAG_ACCURATE,
998 /* start */ GST_SEEK_TYPE_SET, 0,
999 /* stop */ GST_SEEK_TYPE_SET, pos);
1001 if (!gst_element_send_event (play->playbin, seek))
1006 play->trick_mode = mode & ~GST_PLAY_TRICK_MODE_INSTANT_RATE;
1011 play_set_playback_rate (GstPlay * play, gdouble rate)
1013 GstPlayTrickMode mode = play->trick_mode;
1015 if (instant_rate_changes)
1016 mode |= GST_PLAY_TRICK_MODE_INSTANT_RATE;
1018 if (play_set_rate_and_trick_mode (play, rate, mode)) {
1019 gst_print (_("Playback rate: %.2f"), rate);
1023 gst_print (_("Could not change playback rate to %.2f"), rate);
1029 play_set_relative_playback_rate (GstPlay * play, gdouble rate_step,
1030 gboolean reverse_direction)
1032 gdouble new_rate = play->rate + rate_step;
1034 if (reverse_direction)
1037 play_set_playback_rate (play, new_rate);
1040 static const gchar *
1041 trick_mode_get_description (GstPlayTrickMode mode)
1044 case GST_PLAY_TRICK_MODE_NONE:
1045 return "normal playback, trick modes disabled";
1046 case GST_PLAY_TRICK_MODE_DEFAULT:
1047 return "trick mode: default";
1048 case GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO:
1049 return "trick mode: default, no audio";
1050 case GST_PLAY_TRICK_MODE_KEY_UNITS:
1051 return "trick mode: key frames only";
1052 case GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO:
1053 return "trick mode: key frames only, no audio";
1057 return "unknown trick mode";
1061 play_switch_trick_mode (GstPlay * play)
1063 GstPlayTrickMode new_mode = ++play->trick_mode;
1064 const gchar *mode_desc;
1066 if (new_mode == GST_PLAY_TRICK_MODE_LAST)
1067 new_mode = GST_PLAY_TRICK_MODE_NONE;
1069 mode_desc = trick_mode_get_description (new_mode);
1071 if (play_set_rate_and_trick_mode (play, play->rate, new_mode)) {
1072 gst_print ("Rate: %.2f (%s) \n", play->rate,
1075 gst_print ("\nCould not change trick mode to %s.\n", mode_desc);
1080 play_get_nth_stream_in_collection (GstPlay * play, guint index,
1081 GstPlayTrackType track_type)
1083 guint len, i, n_streams = 0;
1084 GstStreamType target_type;
1086 switch (track_type) {
1087 case GST_PLAY_TRACK_TYPE_AUDIO:
1088 target_type = GST_STREAM_TYPE_AUDIO;
1090 case GST_PLAY_TRACK_TYPE_VIDEO:
1091 target_type = GST_STREAM_TYPE_VIDEO;
1093 case GST_PLAY_TRACK_TYPE_SUBTITLE:
1094 target_type = GST_STREAM_TYPE_TEXT;
1100 len = gst_stream_collection_get_size (play->collection);
1102 for (i = 0; i < len; i++) {
1103 GstStream *stream = gst_stream_collection_get_stream (play->collection, i);
1104 GstStreamType type = gst_stream_get_stream_type (stream);
1106 if (type & target_type) {
1107 if (index == n_streams)
1118 play_cycle_track_selection (GstPlay * play, GstPlayTrackType track_type)
1120 const gchar *prop_cur, *prop_n, *prop_get, *name;
1121 gint cur = -1, n = -1;
1122 guint flag, cur_flags;
1124 /* playbin3 variables */
1125 GList *selected_streams = NULL;
1126 gint cur_audio_idx = -1, cur_video_idx = -1, cur_text_idx = -1;
1127 gint nb_audio = 0, nb_video = 0, nb_text = 0;
1130 g_mutex_lock (&play->selection_lock);
1131 if (play->is_playbin3) {
1132 if (!play->collection) {
1133 gst_print ("No stream-collection\n");
1134 g_mutex_unlock (&play->selection_lock);
1138 /* Check the total number of streams of each type */
1139 len = gst_stream_collection_get_size (play->collection);
1140 for (i = 0; i < len; i++) {
1142 gst_stream_collection_get_stream (play->collection, i);
1144 GstStreamType type = gst_stream_get_stream_type (stream);
1145 const gchar *sid = gst_stream_get_stream_id (stream);
1147 if (type & GST_STREAM_TYPE_AUDIO) {
1148 if (play->cur_audio_sid && !g_strcmp0 (play->cur_audio_sid, sid))
1149 cur_audio_idx = nb_audio;
1151 } else if (type & GST_STREAM_TYPE_VIDEO) {
1152 if (play->cur_video_sid && !g_strcmp0 (play->cur_video_sid, sid))
1153 cur_video_idx = nb_video;
1155 } else if (type & GST_STREAM_TYPE_TEXT) {
1156 if (play->cur_text_sid && !g_strcmp0 (play->cur_text_sid, sid))
1157 cur_text_idx = nb_text;
1160 gst_print ("Unknown stream type with stream-id %s", sid);
1166 switch (track_type) {
1167 case GST_PLAY_TRACK_TYPE_AUDIO:
1168 prop_get = "get-audio-tags";
1169 prop_cur = "current-audio";
1173 if (play->is_playbin3) {
1175 cur = cur_audio_idx;
1176 if (play->cur_video_sid) {
1178 g_list_append (selected_streams, play->cur_video_sid);
1180 if (play->cur_text_sid) {
1182 g_list_append (selected_streams, play->cur_text_sid);
1186 case GST_PLAY_TRACK_TYPE_VIDEO:
1187 prop_get = "get-video-tags";
1188 prop_cur = "current-video";
1192 if (play->is_playbin3) {
1194 cur = cur_video_idx;
1195 if (play->cur_audio_sid) {
1197 g_list_append (selected_streams, play->cur_audio_sid);
1199 if (play->cur_text_sid) {
1201 g_list_append (selected_streams, play->cur_text_sid);
1205 case GST_PLAY_TRACK_TYPE_SUBTITLE:
1206 prop_get = "get-text-tags";
1207 prop_cur = "current-text";
1211 if (play->is_playbin3) {
1214 if (play->cur_audio_sid) {
1216 g_list_append (selected_streams, play->cur_audio_sid);
1218 if (play->cur_video_sid) {
1220 g_list_append (selected_streams, play->cur_video_sid);
1228 if (play->is_playbin3) {
1233 cur = (cur + 1) % (n + 1);
1236 g_object_get (play->playbin, prop_cur, &cur, prop_n, &n, "flags",
1239 if (!(cur_flags & flag))
1242 cur = (cur + 1) % (n + 1);
1246 gst_print ("No %s tracks.\n", name);
1247 g_mutex_unlock (&play->selection_lock);
1249 gchar *lcode = NULL, *lname = NULL;
1250 const gchar *lang = NULL;
1251 GstTagList *tags = NULL;
1253 if (cur >= n && track_type != GST_PLAY_TRACK_TYPE_VIDEO) {
1255 gst_print ("Disabling %s. \n", name);
1256 if (play->is_playbin3) {
1257 /* Just make it empty for the track type */
1258 } else if (cur_flags & flag) {
1260 g_object_set (play->playbin, "flags", cur_flags, NULL);
1263 /* For video we only want to switch between streams, not disable it altogether */
1267 if (play->is_playbin3) {
1270 stream = play_get_nth_stream_in_collection (play, cur, track_type);
1272 selected_streams = g_list_append (selected_streams,
1273 (gchar *) gst_stream_get_stream_id (stream));
1274 tags = gst_stream_get_tags (stream);
1276 gst_print ("Collection has no stream for track %d of %d.\n",
1280 if (!(cur_flags & flag) && track_type != GST_PLAY_TRACK_TYPE_VIDEO) {
1282 g_object_set (play->playbin, "flags", cur_flags, NULL);
1284 g_signal_emit_by_name (play->playbin, prop_get, cur, &tags);
1288 if (gst_tag_list_get_string (tags, GST_TAG_LANGUAGE_CODE, &lcode))
1289 lang = gst_tag_get_language_name (lcode);
1290 else if (gst_tag_list_get_string (tags, GST_TAG_LANGUAGE_NAME, &lname))
1292 gst_tag_list_unref (tags);
1295 gst_print ("Switching to %s track %d of %d (%s).\n", name, cur + 1, n,
1298 gst_print ("Switching to %s track %d of %d.\n", name, cur + 1, n);
1302 g_mutex_unlock (&play->selection_lock);
1304 if (play->is_playbin3) {
1305 if (selected_streams)
1306 gst_element_send_event (play->playbin,
1307 gst_event_new_select_streams (selected_streams));
1309 gst_print ("Can't disable all streams !\n");
1311 g_object_set (play->playbin, prop_cur, cur, NULL);
1315 if (selected_streams)
1316 g_list_free (selected_streams);
1320 print_keyboard_help (void)
1324 const gchar *key_desc;
1325 const gchar *key_help;
1326 } key_controls[] = {
1328 N_("space"), N_("pause/unpause")}, {
1329 N_("q or ESC"), N_("quit")}, {
1330 N_("> or n"), N_("play next")}, {
1331 N_("< or b"), N_("play previous")}, {
1332 "\342\206\222", N_("seek forward")}, {
1333 "\342\206\220", N_("seek backward")}, {
1334 "\342\206\221", N_("volume up")}, {
1335 "\342\206\223", N_("volume down")}, {
1336 "m", N_("toggle audio mute on/off")}, {
1337 "+", N_("increase playback rate")}, {
1338 "-", N_("decrease playback rate")}, {
1339 "d", N_("change playback direction")}, {
1340 "t", N_("enable/disable trick modes")}, {
1341 "a", N_("change audio track")}, {
1342 "v", N_("change video track")}, {
1343 "s", N_("change subtitle track")}, {
1344 "0", N_("seek to beginning")}, {
1345 "k", N_("show keyboard shortcuts")},};
1346 guint i, chars_to_pad, desc_len, max_desc_len = 0;
1348 gst_print ("\n\n%s\n\n", _("Interactive mode - keyboard controls:"));
1350 for (i = 0; i < G_N_ELEMENTS (key_controls); ++i) {
1351 desc_len = g_utf8_strlen (key_controls[i].key_desc, -1);
1352 max_desc_len = MAX (max_desc_len, desc_len);
1356 for (i = 0; i < G_N_ELEMENTS (key_controls); ++i) {
1357 chars_to_pad = max_desc_len - g_utf8_strlen (key_controls[i].key_desc, -1);
1358 gst_print ("\t%s", key_controls[i].key_desc);
1359 gst_print ("%-*s: ", chars_to_pad, "");
1360 gst_print ("%s\n", key_controls[i].key_help);
1366 keyboard_cb (const gchar * key_input, gpointer user_data)
1368 GstPlay *play = (GstPlay *) user_data;
1371 /* only want to switch/case on single char, not first char of string */
1372 if (key_input[0] != '\0' && key_input[1] == '\0')
1373 key = g_ascii_tolower (key_input[0]);
1377 print_keyboard_help ();
1380 toggle_paused (play);
1384 g_main_loop_quit (play->loop);
1388 if (!play_next (play)) {
1389 gst_print ("\n%s\n", _("Reached end of play list."));
1390 g_main_loop_quit (play->loop);
1398 if (play->rate > -0.2 && play->rate < 0.0)
1399 play_set_relative_playback_rate (play, 0.0, TRUE);
1400 else if (ABS (play->rate) < 2.0)
1401 play_set_relative_playback_rate (play, 0.1, FALSE);
1402 else if (ABS (play->rate) < 4.0)
1403 play_set_relative_playback_rate (play, 0.5, FALSE);
1405 play_set_relative_playback_rate (play, 1.0, FALSE);
1408 if (play->rate > 0.0 && play->rate < 0.20)
1409 play_set_relative_playback_rate (play, 0.0, TRUE);
1410 else if (ABS (play->rate) <= 2.0)
1411 play_set_relative_playback_rate (play, -0.1, FALSE);
1412 else if (ABS (play->rate) <= 4.0)
1413 play_set_relative_playback_rate (play, -0.5, FALSE);
1415 play_set_relative_playback_rate (play, -1.0, FALSE);
1418 play_set_relative_playback_rate (play, 0.0, TRUE);
1421 play_switch_trick_mode (play);
1424 if (key_input[1] == '\0') {
1425 g_main_loop_quit (play->loop);
1429 play_cycle_track_selection (play, GST_PLAY_TRACK_TYPE_AUDIO);
1432 play_cycle_track_selection (play, GST_PLAY_TRACK_TYPE_VIDEO);
1435 play_cycle_track_selection (play, GST_PLAY_TRACK_TYPE_SUBTITLE);
1438 play_do_seek (play, 0, play->rate, play->trick_mode);
1441 play_toggle_audio_mute (play);
1444 if (strcmp (key_input, GST_PLAY_KB_ARROW_RIGHT) == 0) {
1445 relative_seek (play, +0.08);
1446 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_LEFT) == 0) {
1447 relative_seek (play, -0.01);
1448 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_UP) == 0) {
1449 play_set_relative_volume (play, +1.0 / VOLUME_STEPS);
1450 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_DOWN) == 0) {
1451 play_set_relative_volume (play, -1.0 / VOLUME_STEPS);
1453 GST_INFO ("keyboard input:");
1454 for (; *key_input != '\0'; ++key_input)
1455 GST_INFO (" code %3d", *key_input);
1462 main (int argc, char **argv)
1465 GPtrArray *playlist;
1466 gboolean verbose = FALSE;
1467 gboolean print_version = FALSE;
1468 gboolean interactive = TRUE;
1469 gboolean gapless = FALSE;
1470 gboolean shuffle = FALSE;
1471 gdouble volume = -1;
1472 gchar **filenames = NULL;
1473 gchar *audio_sink = NULL;
1474 gchar *video_sink = NULL;
1476 gchar *flags = NULL;
1479 GOptionContext *ctx;
1480 gchar *playlist_file = NULL;
1481 gboolean use_playbin3 = FALSE;
1482 GOptionEntry options[] = {
1483 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
1484 N_("Output status information and property notifications"), NULL},
1485 {"flags", 0, 0, G_OPTION_ARG_STRING, &flags,
1486 N_("Control playback behaviour setting playbin 'flags' property"),
1488 {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
1489 N_("Print version information and exit"), NULL},
1490 {"videosink", 0, 0, G_OPTION_ARG_STRING, &video_sink,
1491 N_("Video sink to use (default is autovideosink)"), NULL},
1492 {"audiosink", 0, 0, G_OPTION_ARG_STRING, &audio_sink,
1493 N_("Audio sink to use (default is autoaudiosink)"), NULL},
1494 {"gapless", 0, 0, G_OPTION_ARG_NONE, &gapless,
1495 N_("Enable gapless playback"), NULL},
1496 {"shuffle", 0, 0, G_OPTION_ARG_NONE, &shuffle,
1497 N_("Shuffle playlist"), NULL},
1498 {"no-interactive", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,
1500 N_("Disable interactive control via the keyboard"), NULL},
1501 {"volume", 0, 0, G_OPTION_ARG_DOUBLE, &volume,
1502 N_("Volume"), NULL},
1503 {"playlist", 0, 0, G_OPTION_ARG_FILENAME, &playlist_file,
1504 N_("Playlist file containing input media files"), NULL},
1505 {"instant-rate-changes", 'i', 0, G_OPTION_ARG_NONE, &instant_rate_changes,
1507 ("Use the experimental instant-rate-change flag when changing rate"),
1509 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
1510 N_("Do not print any output (apart from errors)"), NULL},
1511 {"use-playbin3", 0, 0, G_OPTION_ARG_NONE, &use_playbin3,
1512 N_("Use playbin3 pipeline")
1513 N_("(default varies depending on 'USE_PLAYBIN' env variable)"),
1515 {"wait-on-eos", 0, 0, G_OPTION_ARG_NONE, &wait_on_eos,
1517 ("Keep showing the last frame on EOS until quit or playlist change command "
1518 "(gapless is ignored)"),
1520 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
1524 setlocale (LC_ALL, "");
1527 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1528 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1529 textdomain (GETTEXT_PACKAGE);
1532 g_set_prgname ("gst-play-" GST_API_VERSION);
1533 /* Ensure XInitThreads() is called if/when needed */
1534 g_setenv ("GST_GL_XINITTHREADS", "1", TRUE);
1536 ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ...");
1537 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
1538 g_option_context_add_group (ctx, gst_init_get_option_group ());
1539 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
1540 gst_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
1541 g_option_context_free (ctx);
1542 g_clear_error (&err);
1545 g_option_context_free (ctx);
1547 GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play");
1549 if (print_version) {
1552 version_str = gst_version_string ();
1553 gst_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
1554 gst_print ("%s\n", version_str);
1555 gst_print ("%s\n", GST_PACKAGE_ORIGIN);
1556 g_free (version_str);
1558 g_free (audio_sink);
1559 g_free (video_sink);
1560 g_free (playlist_file);
1568 playlist = g_ptr_array_new ();
1570 if (playlist_file != NULL) {
1571 gchar *playlist_contents = NULL;
1572 gchar **lines = NULL;
1574 if (g_file_get_contents (playlist_file, &playlist_contents, NULL, &err)) {
1575 lines = g_strsplit (playlist_contents, "\n", 0);
1576 num = g_strv_length (lines);
1578 for (i = 0; i < num; i++) {
1579 if (lines[i][0] != '\0') {
1580 GST_LOG ("Playlist[%d]: %s", i + 1, lines[i]);
1581 add_to_playlist (playlist, lines[i]);
1585 g_free (playlist_contents);
1587 gst_printerr ("Could not read playlist: %s\n", err->message);
1588 g_clear_error (&err);
1590 g_free (playlist_file);
1591 playlist_file = NULL;
1594 if (playlist->len == 0 && (filenames == NULL || *filenames == NULL)) {
1595 gst_printerr (_("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."),
1596 "gst-play-" GST_API_VERSION);
1597 gst_printerr ("\n\n"),
1598 gst_printerr ("%s\n\n",
1599 _("You must provide at least one filename or URI to play."));
1600 /* No input provided. Free array */
1601 g_ptr_array_free (playlist, TRUE);
1603 g_free (audio_sink);
1604 g_free (video_sink);
1610 if (filenames != NULL && *filenames != NULL) {
1611 num = g_strv_length (filenames);
1612 for (i = 0; i < num; ++i) {
1613 GST_LOG ("command line argument: %s", filenames[i]);
1614 add_to_playlist (playlist, filenames[i]);
1616 g_strfreev (filenames);
1619 num = playlist->len;
1620 g_ptr_array_add (playlist, NULL);
1622 uris = (gchar **) g_ptr_array_free (playlist, FALSE);
1625 shuffle_uris (uris, num);
1628 play = play_new (uris, audio_sink, video_sink, gapless, volume, verbose,
1629 flags, use_playbin3);
1633 ("Failed to create 'playbin' element. Check your GStreamer installation.\n");
1634 return EXIT_FAILURE;
1638 if (gst_play_kb_set_key_handler (keyboard_cb, play)) {
1639 gst_print (_("Press 'k' to see a list of keyboard shortcuts.\n"));
1640 atexit (restore_terminal);
1642 gst_print ("Interactive keyboard handling in terminal not available.\n");
1652 g_free (audio_sink);
1653 g_free (video_sink);