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;
105 gdouble start_position;
108 static gboolean quiet = FALSE;
109 static gboolean instant_rate_changes = FALSE;
111 static gboolean play_bus_msg (GstBus * bus, GstMessage * msg, gpointer data);
112 static gboolean play_next (GstPlay * play);
113 static gboolean play_prev (GstPlay * play);
114 static gboolean play_timeout (gpointer user_data);
115 static void play_about_to_finish (GstElement * playbin, gpointer user_data);
116 static void play_reset (GstPlay * play);
117 static void play_set_relative_volume (GstPlay * play, gdouble volume_step);
118 static gboolean play_do_seek (GstPlay * play, gint64 pos, gdouble rate,
119 GstPlayTrickMode mode);
122 static void gst_play_printf (const gchar * format, ...) G_GNUC_PRINTF (1, 2);
125 static void keyboard_cb (const gchar * key_input, gpointer user_data);
126 static void relative_seek (GstPlay * play, gdouble percent);
129 gst_play_printf (const gchar * format, ...)
138 va_start (args, format);
140 len = g_vasprintf (&str, format, args);
144 if (len > 0 && str != NULL)
145 gst_print ("%s", str);
150 #define gst_print gst_play_printf
153 play_new (gchar ** uris, const gchar * audio_sink, const gchar * video_sink,
154 gboolean gapless, gdouble initial_volume, gboolean verbose,
155 const gchar * flags_string, gboolean use_playbin3, gdouble start_position)
157 GstElement *sink, *playbin;
162 playbin = gst_element_factory_make ("playbin3", "playbin");
164 playbin = gst_element_factory_make ("playbin", "playbin");
170 play = g_new0 (GstPlay, 1);
173 play->num_uris = g_strv_length (uris);
176 play->playbin = playbin;
179 play->is_playbin3 = TRUE;
181 const gchar *env = g_getenv ("USE_PLAYBIN3");
182 if (env && g_str_has_prefix (env, "1"))
183 play->is_playbin3 = TRUE;
186 g_mutex_init (&play->selection_lock);
188 if (audio_sink != NULL) {
189 if (strchr (audio_sink, ' ') != NULL)
190 sink = gst_parse_bin_from_description (audio_sink, TRUE, NULL);
192 sink = gst_element_factory_make (audio_sink, NULL);
195 g_object_set (play->playbin, "audio-sink", sink, NULL);
197 g_warning ("Couldn't create specified audio sink '%s'", audio_sink);
199 if (video_sink != NULL) {
200 if (strchr (video_sink, ' ') != NULL)
201 sink = gst_parse_bin_from_description (video_sink, TRUE, NULL);
203 sink = gst_element_factory_make (video_sink, NULL);
206 g_object_set (play->playbin, "video-sink", sink, NULL);
208 g_warning ("Couldn't create specified video sink '%s'", video_sink);
211 if (flags_string != NULL) {
216 g_object_class_find_property (G_OBJECT_GET_CLASS (playbin), "flags");
217 g_value_init (&val, pspec->value_type);
218 if (gst_value_deserialize (&val, flags_string))
219 g_object_set_property (G_OBJECT (play->playbin), "flags", &val);
221 gst_printerr ("Couldn't convert '%s' to playbin flags!\n", flags_string);
222 g_value_unset (&val);
226 play->deep_notify_id =
227 gst_element_add_property_deep_notify_watch (play->playbin, NULL, TRUE);
230 play->loop = g_main_loop_new (NULL, FALSE);
232 play->bus_watch = gst_bus_add_watch (GST_ELEMENT_BUS (play->playbin),
235 /* FIXME: make configurable incl. 0 for disable */
236 play->timeout = g_timeout_add (100, play_timeout, play);
238 play->missing = NULL;
239 play->buffering = FALSE;
240 play->is_live = FALSE;
242 play->desired_state = GST_STATE_PLAYING;
244 play->gapless = gapless;
246 g_signal_connect (play->playbin, "about-to-finish",
247 G_CALLBACK (play_about_to_finish), play);
250 if (initial_volume != -1)
251 play_set_relative_volume (play, initial_volume - 1.0);
254 play->trick_mode = GST_PLAY_TRICK_MODE_NONE;
255 play->start_position = start_position;
260 play_free (GstPlay * play)
262 /* No need to see all those pad caps going to NULL etc., it's just noise */
263 if (play->deep_notify_id != 0)
264 g_signal_handler_disconnect (play->playbin, play->deep_notify_id);
268 gst_element_set_state (play->playbin, GST_STATE_NULL);
269 gst_object_unref (play->playbin);
271 g_source_remove (play->bus_watch);
272 g_source_remove (play->timeout);
273 g_main_loop_unref (play->loop);
275 g_strfreev (play->uris);
277 if (play->collection)
278 gst_object_unref (play->collection);
279 g_free (play->cur_audio_sid);
280 g_free (play->cur_video_sid);
281 g_free (play->cur_text_sid);
283 g_mutex_clear (&play->selection_lock);
288 /* reset for new file/stream */
290 play_reset (GstPlay * play)
292 g_list_foreach (play->missing, (GFunc) gst_message_unref, NULL);
293 play->missing = NULL;
295 play->buffering = FALSE;
296 play->is_live = FALSE;
300 play_set_relative_volume (GstPlay * play, gdouble volume_step)
304 volume = gst_stream_volume_get_volume (GST_STREAM_VOLUME (play->playbin),
305 GST_STREAM_VOLUME_FORMAT_CUBIC);
307 volume = round ((volume + volume_step) * VOLUME_STEPS) / VOLUME_STEPS;
308 volume = CLAMP (volume, 0.0, 10.0);
310 gst_stream_volume_set_volume (GST_STREAM_VOLUME (play->playbin),
311 GST_STREAM_VOLUME_FORMAT_CUBIC, volume);
313 gst_print (_("Volume: %.0f%%"), volume * 100);
318 play_toggle_audio_mute (GstPlay * play)
322 mute = gst_stream_volume_get_mute (GST_STREAM_VOLUME (play->playbin));
325 gst_stream_volume_set_mute (GST_STREAM_VOLUME (play->playbin), mute);
328 gst_print (_("Mute: on"));
330 gst_print (_("Mute: off"));
334 /* returns TRUE if something was installed and we should restart playback */
336 play_install_missing_plugins (GstPlay * play)
338 /* FIXME: implement: try to install any missing plugins we haven't
339 * tried to install before */
344 play_bus_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
346 GstPlay *play = user_data;
348 switch (GST_MESSAGE_TYPE (msg)) {
349 case GST_MESSAGE_ASYNC_DONE:
351 /* dump graph on preroll */
352 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
353 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.async-done");
355 gst_print ("Prerolled.\r");
356 if (play->missing != NULL && play_install_missing_plugins (play)) {
357 gst_print ("New plugins installed, trying again...\n");
361 if (play->start_position > 0.0) {
362 play_do_seek (play, play->start_position * GST_SECOND,
363 play->rate, play->trick_mode);
364 play->start_position = 0;
367 case GST_MESSAGE_BUFFERING:{
370 if (!play->buffering)
373 gst_message_parse_buffering (msg, &percent);
374 gst_print ("%s %d%% \r", _("Buffering..."), percent);
376 if (percent == 100) {
377 /* a 100% message means buffering is done */
378 if (play->buffering) {
379 play->buffering = FALSE;
380 /* no state management needed for live pipelines */
382 gst_element_set_state (play->playbin, play->desired_state);
386 if (!play->buffering) {
388 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
389 play->buffering = TRUE;
394 case GST_MESSAGE_CLOCK_LOST:{
395 gst_print (_("Clock lost, selecting a new one\n"));
396 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
397 gst_element_set_state (play->playbin, GST_STATE_PLAYING);
400 case GST_MESSAGE_LATENCY:
401 gst_print ("Redistribute latency...\n");
402 gst_bin_recalculate_latency (GST_BIN (play->playbin));
404 case GST_MESSAGE_REQUEST_STATE:{
408 name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
410 gst_message_parse_request_state (msg, &state);
412 gst_print ("Setting state to %s as requested by %s...\n",
413 gst_element_state_get_name (state), name);
415 gst_element_set_state (play->playbin, state);
419 case GST_MESSAGE_EOS:
420 /* print final position at end */
423 /* and switch to next item in list */
424 if (!wait_on_eos && !play_next (play)) {
425 gst_print ("%s\n", _("Reached end of play list."));
426 g_main_loop_quit (play->loop);
429 case GST_MESSAGE_WARNING:{
433 /* dump graph on warning */
434 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
435 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.warning");
437 gst_message_parse_warning (msg, &err, &dbg);
438 gst_printerr ("WARNING %s\n", err->message);
440 gst_printerr ("WARNING debug information: %s\n", dbg);
441 g_clear_error (&err);
445 case GST_MESSAGE_ERROR:{
449 /* dump graph on error */
450 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
451 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.error");
453 gst_message_parse_error (msg, &err, &dbg);
454 gst_printerr ("ERROR %s for %s\n", err->message,
455 play->uris[play->cur_idx]);
457 gst_printerr ("ERROR debug information: %s\n", dbg);
458 g_clear_error (&err);
461 /* flush any other error messages from the bus and clean up */
462 gst_element_set_state (play->playbin, GST_STATE_NULL);
464 if (play->missing != NULL && play_install_missing_plugins (play)) {
465 gst_print ("New plugins installed, trying again...\n");
470 /* try next item in list then */
471 if (!play_next (play)) {
472 gst_print ("%s\n", _("Reached end of play list."));
473 g_main_loop_quit (play->loop);
477 case GST_MESSAGE_ELEMENT:
479 GstNavigationMessageType mtype = gst_navigation_message_get_type (msg);
480 if (mtype == GST_NAVIGATION_MESSAGE_EVENT) {
483 if (gst_navigation_message_parse_event (msg, &ev)) {
484 GstNavigationEventType e_type = gst_navigation_event_get_type (ev);
486 case GST_NAVIGATION_EVENT_KEY_PRESS:
490 if (gst_navigation_event_parse_key_event (ev, &key)) {
491 GST_INFO ("Key press: %s", key);
493 if (strcmp (key, "Left") == 0)
494 key = GST_PLAY_KB_ARROW_LEFT;
495 else if (strcmp (key, "Right") == 0)
496 key = GST_PLAY_KB_ARROW_RIGHT;
497 else if (strcmp (key, "Up") == 0)
498 key = GST_PLAY_KB_ARROW_UP;
499 else if (strcmp (key, "Down") == 0)
500 key = GST_PLAY_KB_ARROW_DOWN;
501 else if (strcmp (key, "space") == 0 ||
502 strcmp (key, "Space") == 0)
504 else if (strlen (key) > 1)
507 keyboard_cb (key, user_data);
511 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
514 if (gst_navigation_event_parse_mouse_button_event (ev, &button,
518 relative_seek (play, +0.08);
519 } else if (button == 5) {
521 relative_seek (play, -0.01);
531 gst_event_unref (ev);
535 case GST_MESSAGE_PROPERTY_NOTIFY:{
539 gchar *val_str = NULL;
542 gst_message_parse_property_notify (msg, &obj, &name, &val);
544 obj_name = gst_object_get_path_string (GST_OBJECT (obj));
546 if (G_VALUE_HOLDS_STRING (val))
547 val_str = g_value_dup_string (val);
548 else if (G_VALUE_TYPE (val) == GST_TYPE_CAPS)
549 val_str = gst_caps_to_string (g_value_get_boxed (val));
550 else if (G_VALUE_TYPE (val) == GST_TYPE_TAG_LIST)
551 val_str = gst_tag_list_to_string (g_value_get_boxed (val));
553 val_str = gst_value_serialize (val);
555 val_str = g_strdup ("(no value)");
558 gst_play_printf ("%s: %s = %s\n", obj_name, name, val_str);
563 case GST_MESSAGE_STREAM_COLLECTION:
565 GstStreamCollection *collection = NULL;
566 gst_message_parse_stream_collection (msg, &collection);
569 g_mutex_lock (&play->selection_lock);
570 gst_object_replace ((GstObject **) & play->collection,
571 (GstObject *) collection);
572 g_mutex_unlock (&play->selection_lock);
576 case GST_MESSAGE_STREAMS_SELECTED:
578 GstStreamCollection *collection = NULL;
581 gst_message_parse_streams_selected (msg, &collection);
583 g_mutex_lock (&play->selection_lock);
584 gst_object_replace ((GstObject **) & play->collection,
585 (GstObject *) collection);
587 /* Free all last stream-ids */
588 g_free (play->cur_audio_sid);
589 g_free (play->cur_video_sid);
590 g_free (play->cur_text_sid);
591 play->cur_audio_sid = NULL;
592 play->cur_video_sid = NULL;
593 play->cur_text_sid = NULL;
595 len = gst_message_streams_selected_get_size (msg);
596 for (i = 0; i < len; i++) {
597 GstStream *stream = gst_message_streams_selected_get_stream (msg, i);
599 GstStreamType type = gst_stream_get_stream_type (stream);
600 const gchar *stream_id = gst_stream_get_stream_id (stream);
602 if (type & GST_STREAM_TYPE_AUDIO) {
603 play->cur_audio_sid = g_strdup (stream_id);
604 } else if (type & GST_STREAM_TYPE_VIDEO) {
605 play->cur_video_sid = g_strdup (stream_id);
606 } else if (type & GST_STREAM_TYPE_TEXT) {
607 play->cur_text_sid = g_strdup (stream_id);
609 gst_print ("Unknown stream type with stream-id %s", stream_id);
611 gst_object_unref (stream);
615 gst_object_unref (collection);
616 g_mutex_unlock (&play->selection_lock);
621 if (gst_is_missing_plugin_message (msg)) {
624 desc = gst_missing_plugin_message_get_description (msg);
625 gst_print ("Missing plugin: %s\n", desc);
627 play->missing = g_list_append (play->missing, gst_message_ref (msg));
636 play_timeout (gpointer user_data)
638 GstPlay *play = user_data;
639 gint64 pos = -1, dur = -1;
640 const gchar *paused = _("Paused");
646 gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos);
647 gst_element_query_duration (play->playbin, GST_FORMAT_TIME, &dur);
649 if (play->desired_state == GST_STATE_PAUSED) {
650 status = (gchar *) paused;
652 gint len = g_utf8_strlen (paused, -1);
653 status = g_newa (gchar, len + 1);
654 memset (status, ' ', len);
658 if (pos >= 0 && dur > 0) {
659 gchar dstr[32], pstr[32];
661 /* FIXME: pretty print in nicer format */
662 g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
664 g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
666 gst_print ("%s / %s %s\r", pstr, dstr, status);
673 play_uri_get_display_name (GstPlay * play, const gchar * uri)
677 if (gst_uri_has_protocol (uri, "file")) {
678 loc = g_filename_from_uri (uri, NULL, NULL);
679 } else if (gst_uri_has_protocol (uri, "pushfile")) {
680 loc = g_filename_from_uri (uri + 4, NULL, NULL);
682 loc = g_strdup (uri);
685 /* Maybe additionally use glib's filename to display name function */
690 play_uri (GstPlay * play, const gchar * next_uri)
694 gst_element_set_state (play->playbin, GST_STATE_READY);
697 loc = play_uri_get_display_name (play, next_uri);
698 gst_print (_("Now playing %s\n"), loc);
701 g_object_set (play->playbin, "uri", next_uri, NULL);
703 switch (gst_element_set_state (play->playbin, GST_STATE_PAUSED)) {
704 case GST_STATE_CHANGE_FAILURE:
705 /* ignore, we should get an error message posted on the bus */
707 case GST_STATE_CHANGE_NO_PREROLL:
708 gst_print ("Pipeline is live.\n");
709 play->is_live = TRUE;
711 case GST_STATE_CHANGE_ASYNC:
712 gst_print ("Prerolling...\r");
718 if (play->desired_state != GST_STATE_PAUSED)
719 gst_element_set_state (play->playbin, play->desired_state);
722 /* returns FALSE if we have reached the end of the playlist */
724 play_next (GstPlay * play)
726 if ((play->cur_idx + 1) >= play->num_uris)
729 play_uri (play, play->uris[++play->cur_idx]);
733 /* returns FALSE if we have reached the beginning of the playlist */
735 play_prev (GstPlay * play)
737 if (play->cur_idx == 0 || play->num_uris <= 1)
740 play_uri (play, play->uris[--play->cur_idx]);
745 play_about_to_finish (GstElement * playbin, gpointer user_data)
747 GstPlay *play = user_data;
748 const gchar *next_uri;
755 next_idx = play->cur_idx + 1;
756 if (next_idx >= play->num_uris)
759 next_uri = play->uris[next_idx];
760 loc = play_uri_get_display_name (play, next_uri);
761 gst_print (_("About to finish, preparing next title: %s"), loc);
765 g_object_set (play->playbin, "uri", next_uri, NULL);
766 play->cur_idx = next_idx;
770 do_play (GstPlay * play)
775 for (i = 0; i < play->num_uris; ++i)
776 GST_INFO ("%4u : %s", i, play->uris[i]);
778 if (!play_next (play))
781 g_main_loop_run (play->loop);
785 compare (gconstpointer a, gconstpointer b)
790 a1 = g_utf8_collate_key_for_filename ((gchar *) a, -1);
791 b1 = g_utf8_collate_key_for_filename ((gchar *) b, -1);
792 ret = strcmp (a1, b1);
800 add_to_playlist (GPtrArray * playlist, const gchar * filename)
805 if (gst_uri_is_valid (filename)) {
806 g_ptr_array_add (playlist, g_strdup (filename));
810 if ((dir = g_dir_open (filename, 0, NULL))) {
812 GList *l, *files = NULL;
814 while ((entry = g_dir_read_name (dir))) {
817 path = g_build_filename (filename, entry, NULL);
818 files = g_list_insert_sorted (files, path, compare);
823 for (l = files; l != NULL; l = l->next) {
824 gchar *path = (gchar *) l->data;
826 add_to_playlist (playlist, path);
833 uri = gst_filename_to_uri (filename, NULL);
835 g_ptr_array_add (playlist, uri);
837 g_warning ("Could not make URI out of filename '%s'", filename);
841 shuffle_uris (gchar ** uris, guint num)
849 for (i = num - 1; i >= 1; i--) {
850 /* +1 because number returned will be in range [a;b[ so excl. stop */
851 j = g_random_int_range (0, i + 1);
859 restore_terminal (void)
861 gst_play_kb_set_key_handler (NULL, NULL);
865 toggle_paused (GstPlay * play)
867 if (play->desired_state == GST_STATE_PLAYING)
868 play->desired_state = GST_STATE_PAUSED;
870 play->desired_state = GST_STATE_PLAYING;
872 if (!play->buffering) {
873 gst_element_set_state (play->playbin, play->desired_state);
874 } else if (play->desired_state == GST_STATE_PLAYING) {
875 gst_print ("\nWill play as soon as buffering finishes)\n");
880 relative_seek (GstPlay * play, gdouble percent)
883 gboolean seekable = FALSE;
884 gint64 dur = -1, pos = -1, step;
886 g_return_if_fail (percent >= -1.0 && percent <= 1.0);
888 if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
891 query = gst_query_new_seeking (GST_FORMAT_TIME);
892 if (!gst_element_query (play->playbin, query)) {
893 gst_query_unref (query);
897 gst_query_parse_seeking (query, NULL, &seekable, NULL, &dur);
898 gst_query_unref (query);
900 if (!seekable || dur <= 0)
903 step = dur * percent;
904 if (ABS (step) < GST_SECOND)
905 step = (percent < 0) ? -GST_SECOND : GST_SECOND;
909 if (!play_next (play)) {
910 gst_print ("\n%s\n", _("Reached end of play list."));
911 g_main_loop_quit (play->loop);
917 play_do_seek (play, pos, play->rate, play->trick_mode);
924 gst_print ("\nCould not seek.\n");
929 play_set_rate_and_trick_mode (GstPlay * play, gdouble rate,
930 GstPlayTrickMode mode)
934 g_return_val_if_fail (rate != 0, FALSE);
936 if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
939 return play_do_seek (play, pos, rate, mode);
943 play_do_seek (GstPlay * play, gint64 pos, gdouble rate, GstPlayTrickMode mode)
945 GstSeekFlags seek_flags;
948 gboolean seekable = FALSE;
950 query = gst_query_new_seeking (GST_FORMAT_TIME);
951 if (!gst_element_query (play->playbin, query)) {
952 gst_query_unref (query);
956 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
957 gst_query_unref (query);
965 case GST_PLAY_TRICK_MODE_DEFAULT:
966 seek_flags |= GST_SEEK_FLAG_TRICKMODE;
968 case GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO:
969 seek_flags |= GST_SEEK_FLAG_TRICKMODE | GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;
971 case GST_PLAY_TRICK_MODE_KEY_UNITS:
972 seek_flags |= GST_SEEK_FLAG_TRICKMODE_KEY_UNITS;
974 case GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO:
976 GST_SEEK_FLAG_TRICKMODE_KEY_UNITS | GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;
978 case GST_PLAY_TRICK_MODE_NONE:
983 /* See if we can do an instant rate change (not changing dir) */
984 if (mode & GST_PLAY_TRICK_MODE_INSTANT_RATE && rate * play->rate > 0) {
985 seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
986 seek_flags | GST_SEEK_FLAG_INSTANT_RATE_CHANGE,
987 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE,
988 GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
989 if (gst_element_send_event (play->playbin, seek)) {
994 /* No instant rate change, need to do a flushing seek */
995 seek_flags |= GST_SEEK_FLAG_FLUSH;
997 seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
998 seek_flags | GST_SEEK_FLAG_ACCURATE,
999 /* start */ GST_SEEK_TYPE_SET, pos,
1000 /* stop */ GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
1002 seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
1003 seek_flags | GST_SEEK_FLAG_ACCURATE,
1004 /* start */ GST_SEEK_TYPE_SET, 0,
1005 /* stop */ GST_SEEK_TYPE_SET, pos);
1007 if (!gst_element_send_event (play->playbin, seek))
1012 play->trick_mode = mode & ~GST_PLAY_TRICK_MODE_INSTANT_RATE;
1017 play_set_playback_rate (GstPlay * play, gdouble rate)
1019 GstPlayTrickMode mode = play->trick_mode;
1021 if (instant_rate_changes)
1022 mode |= GST_PLAY_TRICK_MODE_INSTANT_RATE;
1024 if (play_set_rate_and_trick_mode (play, rate, mode)) {
1025 gst_print (_("Playback rate: %.2f"), rate);
1029 gst_print (_("Could not change playback rate to %.2f"), rate);
1035 play_set_relative_playback_rate (GstPlay * play, gdouble rate_step,
1036 gboolean reverse_direction)
1038 gdouble new_rate = play->rate + rate_step;
1040 if (reverse_direction)
1043 play_set_playback_rate (play, new_rate);
1046 static const gchar *
1047 trick_mode_get_description (GstPlayTrickMode mode)
1050 case GST_PLAY_TRICK_MODE_NONE:
1051 return "normal playback, trick modes disabled";
1052 case GST_PLAY_TRICK_MODE_DEFAULT:
1053 return "trick mode: default";
1054 case GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO:
1055 return "trick mode: default, no audio";
1056 case GST_PLAY_TRICK_MODE_KEY_UNITS:
1057 return "trick mode: key frames only";
1058 case GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO:
1059 return "trick mode: key frames only, no audio";
1063 return "unknown trick mode";
1067 play_switch_trick_mode (GstPlay * play)
1069 GstPlayTrickMode new_mode = ++play->trick_mode;
1070 const gchar *mode_desc;
1072 if (new_mode == GST_PLAY_TRICK_MODE_LAST)
1073 new_mode = GST_PLAY_TRICK_MODE_NONE;
1075 mode_desc = trick_mode_get_description (new_mode);
1077 if (play_set_rate_and_trick_mode (play, play->rate, new_mode)) {
1078 gst_print ("Rate: %.2f (%s) \n", play->rate,
1081 gst_print ("\nCould not change trick mode to %s.\n", mode_desc);
1086 play_get_nth_stream_in_collection (GstPlay * play, guint index,
1087 GstPlayTrackType track_type)
1089 guint len, i, n_streams = 0;
1090 GstStreamType target_type;
1092 switch (track_type) {
1093 case GST_PLAY_TRACK_TYPE_AUDIO:
1094 target_type = GST_STREAM_TYPE_AUDIO;
1096 case GST_PLAY_TRACK_TYPE_VIDEO:
1097 target_type = GST_STREAM_TYPE_VIDEO;
1099 case GST_PLAY_TRACK_TYPE_SUBTITLE:
1100 target_type = GST_STREAM_TYPE_TEXT;
1106 len = gst_stream_collection_get_size (play->collection);
1108 for (i = 0; i < len; i++) {
1109 GstStream *stream = gst_stream_collection_get_stream (play->collection, i);
1110 GstStreamType type = gst_stream_get_stream_type (stream);
1112 if (type & target_type) {
1113 if (index == n_streams)
1124 play_cycle_track_selection (GstPlay * play, GstPlayTrackType track_type)
1126 const gchar *prop_cur, *prop_n, *prop_get, *name;
1127 gint cur = -1, n = -1;
1128 guint flag, cur_flags;
1130 /* playbin3 variables */
1131 GList *selected_streams = NULL;
1132 gint cur_audio_idx = -1, cur_video_idx = -1, cur_text_idx = -1;
1133 gint nb_audio = 0, nb_video = 0, nb_text = 0;
1136 g_mutex_lock (&play->selection_lock);
1137 if (play->is_playbin3) {
1138 if (!play->collection) {
1139 gst_print ("No stream-collection\n");
1140 g_mutex_unlock (&play->selection_lock);
1144 /* Check the total number of streams of each type */
1145 len = gst_stream_collection_get_size (play->collection);
1146 for (i = 0; i < len; i++) {
1148 gst_stream_collection_get_stream (play->collection, i);
1150 GstStreamType type = gst_stream_get_stream_type (stream);
1151 const gchar *sid = gst_stream_get_stream_id (stream);
1153 if (type & GST_STREAM_TYPE_AUDIO) {
1154 if (play->cur_audio_sid && !g_strcmp0 (play->cur_audio_sid, sid))
1155 cur_audio_idx = nb_audio;
1157 } else if (type & GST_STREAM_TYPE_VIDEO) {
1158 if (play->cur_video_sid && !g_strcmp0 (play->cur_video_sid, sid))
1159 cur_video_idx = nb_video;
1161 } else if (type & GST_STREAM_TYPE_TEXT) {
1162 if (play->cur_text_sid && !g_strcmp0 (play->cur_text_sid, sid))
1163 cur_text_idx = nb_text;
1166 gst_print ("Unknown stream type with stream-id %s", sid);
1172 switch (track_type) {
1173 case GST_PLAY_TRACK_TYPE_AUDIO:
1174 prop_get = "get-audio-tags";
1175 prop_cur = "current-audio";
1179 if (play->is_playbin3) {
1181 cur = cur_audio_idx;
1182 if (play->cur_video_sid) {
1184 g_list_append (selected_streams, play->cur_video_sid);
1186 if (play->cur_text_sid) {
1188 g_list_append (selected_streams, play->cur_text_sid);
1192 case GST_PLAY_TRACK_TYPE_VIDEO:
1193 prop_get = "get-video-tags";
1194 prop_cur = "current-video";
1198 if (play->is_playbin3) {
1200 cur = cur_video_idx;
1201 if (play->cur_audio_sid) {
1203 g_list_append (selected_streams, play->cur_audio_sid);
1205 if (play->cur_text_sid) {
1207 g_list_append (selected_streams, play->cur_text_sid);
1211 case GST_PLAY_TRACK_TYPE_SUBTITLE:
1212 prop_get = "get-text-tags";
1213 prop_cur = "current-text";
1217 if (play->is_playbin3) {
1220 if (play->cur_audio_sid) {
1222 g_list_append (selected_streams, play->cur_audio_sid);
1224 if (play->cur_video_sid) {
1226 g_list_append (selected_streams, play->cur_video_sid);
1234 if (play->is_playbin3) {
1239 cur = (cur + 1) % (n + 1);
1242 g_object_get (play->playbin, prop_cur, &cur, prop_n, &n, "flags",
1245 if (!(cur_flags & flag))
1248 cur = (cur + 1) % (n + 1);
1252 gst_print ("No %s tracks.\n", name);
1253 g_mutex_unlock (&play->selection_lock);
1255 gchar *lcode = NULL, *lname = NULL;
1256 const gchar *lang = NULL;
1257 GstTagList *tags = NULL;
1259 if (cur >= n && track_type != GST_PLAY_TRACK_TYPE_VIDEO) {
1261 gst_print ("Disabling %s. \n", name);
1262 if (play->is_playbin3) {
1263 /* Just make it empty for the track type */
1264 } else if (cur_flags & flag) {
1266 g_object_set (play->playbin, "flags", cur_flags, NULL);
1269 /* For video we only want to switch between streams, not disable it altogether */
1273 if (play->is_playbin3) {
1276 stream = play_get_nth_stream_in_collection (play, cur, track_type);
1278 selected_streams = g_list_append (selected_streams,
1279 (gchar *) gst_stream_get_stream_id (stream));
1280 tags = gst_stream_get_tags (stream);
1282 gst_print ("Collection has no stream for track %d of %d.\n",
1286 if (!(cur_flags & flag) && track_type != GST_PLAY_TRACK_TYPE_VIDEO) {
1288 g_object_set (play->playbin, "flags", cur_flags, NULL);
1290 g_signal_emit_by_name (play->playbin, prop_get, cur, &tags);
1294 if (gst_tag_list_get_string (tags, GST_TAG_LANGUAGE_CODE, &lcode))
1295 lang = gst_tag_get_language_name (lcode);
1296 else if (gst_tag_list_get_string (tags, GST_TAG_LANGUAGE_NAME, &lname))
1298 gst_tag_list_unref (tags);
1301 gst_print ("Switching to %s track %d of %d (%s).\n", name, cur + 1, n,
1304 gst_print ("Switching to %s track %d of %d.\n", name, cur + 1, n);
1308 g_mutex_unlock (&play->selection_lock);
1310 if (play->is_playbin3) {
1311 if (selected_streams)
1312 gst_element_send_event (play->playbin,
1313 gst_event_new_select_streams (selected_streams));
1315 gst_print ("Can't disable all streams !\n");
1317 g_object_set (play->playbin, prop_cur, cur, NULL);
1321 if (selected_streams)
1322 g_list_free (selected_streams);
1326 print_keyboard_help (void)
1330 const gchar *key_desc;
1331 const gchar *key_help;
1332 } key_controls[] = {
1334 N_("space"), N_("pause/unpause")}, {
1335 N_("q or ESC"), N_("quit")}, {
1336 N_("> or n"), N_("play next")}, {
1337 N_("< or b"), N_("play previous")}, {
1338 "\342\206\222", N_("seek forward")}, {
1339 "\342\206\220", N_("seek backward")}, {
1340 "\342\206\221", N_("volume up")}, {
1341 "\342\206\223", N_("volume down")}, {
1342 "m", N_("toggle audio mute on/off")}, {
1343 "+", N_("increase playback rate")}, {
1344 "-", N_("decrease playback rate")}, {
1345 "d", N_("change playback direction")}, {
1346 "t", N_("enable/disable trick modes")}, {
1347 "a", N_("change audio track")}, {
1348 "v", N_("change video track")}, {
1349 "s", N_("change subtitle track")}, {
1350 "0", N_("seek to beginning")}, {
1351 "k", N_("show keyboard shortcuts")},};
1352 guint i, chars_to_pad, desc_len, max_desc_len = 0;
1354 gst_print ("\n\n%s\n\n", _("Interactive mode - keyboard controls:"));
1356 for (i = 0; i < G_N_ELEMENTS (key_controls); ++i) {
1357 desc_len = g_utf8_strlen (key_controls[i].key_desc, -1);
1358 max_desc_len = MAX (max_desc_len, desc_len);
1362 for (i = 0; i < G_N_ELEMENTS (key_controls); ++i) {
1363 chars_to_pad = max_desc_len - g_utf8_strlen (key_controls[i].key_desc, -1);
1364 gst_print ("\t%s", key_controls[i].key_desc);
1365 gst_print ("%-*s: ", chars_to_pad, "");
1366 gst_print ("%s\n", key_controls[i].key_help);
1372 keyboard_cb (const gchar * key_input, gpointer user_data)
1374 GstPlay *play = (GstPlay *) user_data;
1377 /* only want to switch/case on single char, not first char of string */
1378 if (key_input[0] != '\0' && key_input[1] == '\0')
1379 key = g_ascii_tolower (key_input[0]);
1383 print_keyboard_help ();
1386 toggle_paused (play);
1390 g_main_loop_quit (play->loop);
1394 if (!play_next (play)) {
1395 gst_print ("\n%s\n", _("Reached end of play list."));
1396 g_main_loop_quit (play->loop);
1404 if (play->rate > -0.2 && play->rate < 0.0)
1405 play_set_relative_playback_rate (play, 0.0, TRUE);
1406 else if (ABS (play->rate) < 2.0)
1407 play_set_relative_playback_rate (play, 0.1, FALSE);
1408 else if (ABS (play->rate) < 4.0)
1409 play_set_relative_playback_rate (play, 0.5, FALSE);
1411 play_set_relative_playback_rate (play, 1.0, FALSE);
1414 if (play->rate > 0.0 && play->rate < 0.20)
1415 play_set_relative_playback_rate (play, 0.0, TRUE);
1416 else if (ABS (play->rate) <= 2.0)
1417 play_set_relative_playback_rate (play, -0.1, FALSE);
1418 else if (ABS (play->rate) <= 4.0)
1419 play_set_relative_playback_rate (play, -0.5, FALSE);
1421 play_set_relative_playback_rate (play, -1.0, FALSE);
1424 play_set_relative_playback_rate (play, 0.0, TRUE);
1427 play_switch_trick_mode (play);
1430 if (key_input[1] == '\0') {
1431 g_main_loop_quit (play->loop);
1435 play_cycle_track_selection (play, GST_PLAY_TRACK_TYPE_AUDIO);
1438 play_cycle_track_selection (play, GST_PLAY_TRACK_TYPE_VIDEO);
1441 play_cycle_track_selection (play, GST_PLAY_TRACK_TYPE_SUBTITLE);
1444 play_do_seek (play, 0, play->rate, play->trick_mode);
1447 play_toggle_audio_mute (play);
1450 if (strcmp (key_input, GST_PLAY_KB_ARROW_RIGHT) == 0) {
1451 relative_seek (play, +0.08);
1452 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_LEFT) == 0) {
1453 relative_seek (play, -0.01);
1454 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_UP) == 0) {
1455 play_set_relative_volume (play, +1.0 / VOLUME_STEPS);
1456 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_DOWN) == 0) {
1457 play_set_relative_volume (play, -1.0 / VOLUME_STEPS);
1459 GST_INFO ("keyboard input:");
1460 for (; *key_input != '\0'; ++key_input)
1461 GST_INFO (" code %3d", *key_input);
1468 main (int argc, char **argv)
1471 GPtrArray *playlist;
1472 gboolean verbose = FALSE;
1473 gboolean print_version = FALSE;
1474 gboolean interactive = TRUE;
1475 gboolean gapless = FALSE;
1476 gboolean shuffle = FALSE;
1477 gdouble volume = -1;
1478 gdouble start_position = 0;
1479 gchar **filenames = NULL;
1480 gchar *audio_sink = NULL;
1481 gchar *video_sink = NULL;
1483 gchar *flags = NULL;
1486 GOptionContext *ctx;
1487 gchar *playlist_file = NULL;
1488 gboolean use_playbin3 = FALSE;
1489 GOptionEntry options[] = {
1490 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
1491 N_("Output status information and property notifications"), NULL},
1492 {"flags", 0, 0, G_OPTION_ARG_STRING, &flags,
1493 N_("Control playback behaviour setting playbin 'flags' property"),
1495 {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
1496 N_("Print version information and exit"), NULL},
1497 {"videosink", 0, 0, G_OPTION_ARG_STRING, &video_sink,
1498 N_("Video sink to use (default is autovideosink)"), NULL},
1499 {"audiosink", 0, 0, G_OPTION_ARG_STRING, &audio_sink,
1500 N_("Audio sink to use (default is autoaudiosink)"), NULL},
1501 {"gapless", 0, 0, G_OPTION_ARG_NONE, &gapless,
1502 N_("Enable gapless playback"), NULL},
1503 {"shuffle", 0, 0, G_OPTION_ARG_NONE, &shuffle,
1504 N_("Shuffle playlist"), NULL},
1505 {"no-interactive", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,
1507 N_("Disable interactive control via the keyboard"), NULL},
1508 {"volume", 0, 0, G_OPTION_ARG_DOUBLE, &volume,
1509 N_("Volume"), NULL},
1510 {"start-position", 's', 0, G_OPTION_ARG_DOUBLE, &start_position,
1511 N_("Start position in seconds."), NULL},
1512 {"playlist", 0, 0, G_OPTION_ARG_FILENAME, &playlist_file,
1513 N_("Playlist file containing input media files"), NULL},
1514 {"instant-rate-changes", 'i', 0, G_OPTION_ARG_NONE, &instant_rate_changes,
1516 ("Use the experimental instant-rate-change flag when changing rate"),
1518 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
1519 N_("Do not print any output (apart from errors)"), NULL},
1520 {"use-playbin3", 0, 0, G_OPTION_ARG_NONE, &use_playbin3,
1521 N_("Use playbin3 pipeline")
1522 N_("(default varies depending on 'USE_PLAYBIN' env variable)"),
1524 {"wait-on-eos", 0, 0, G_OPTION_ARG_NONE, &wait_on_eos,
1526 ("Keep showing the last frame on EOS until quit or playlist change command "
1527 "(gapless is ignored)"),
1529 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
1533 setlocale (LC_ALL, "");
1536 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1537 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1538 textdomain (GETTEXT_PACKAGE);
1541 g_set_prgname ("gst-play-" GST_API_VERSION);
1542 /* Ensure XInitThreads() is called if/when needed */
1543 g_setenv ("GST_GL_XINITTHREADS", "1", TRUE);
1545 ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ...");
1546 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
1547 g_option_context_add_group (ctx, gst_init_get_option_group ());
1548 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
1549 gst_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
1550 g_option_context_free (ctx);
1551 g_clear_error (&err);
1554 g_option_context_free (ctx);
1556 GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play");
1558 if (print_version) {
1561 version_str = gst_version_string ();
1562 gst_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
1563 gst_print ("%s\n", version_str);
1564 gst_print ("%s\n", GST_PACKAGE_ORIGIN);
1565 g_free (version_str);
1567 g_free (audio_sink);
1568 g_free (video_sink);
1569 g_free (playlist_file);
1577 playlist = g_ptr_array_new ();
1579 if (playlist_file != NULL) {
1580 gchar *playlist_contents = NULL;
1581 gchar **lines = NULL;
1583 if (g_file_get_contents (playlist_file, &playlist_contents, NULL, &err)) {
1584 lines = g_strsplit (playlist_contents, "\n", 0);
1585 num = g_strv_length (lines);
1587 for (i = 0; i < num; i++) {
1588 if (lines[i][0] != '\0') {
1589 GST_LOG ("Playlist[%d]: %s", i + 1, lines[i]);
1590 add_to_playlist (playlist, lines[i]);
1594 g_free (playlist_contents);
1596 gst_printerr ("Could not read playlist: %s\n", err->message);
1597 g_clear_error (&err);
1599 g_free (playlist_file);
1600 playlist_file = NULL;
1603 if (playlist->len == 0 && (filenames == NULL || *filenames == NULL)) {
1604 gst_printerr (_("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."),
1605 "gst-play-" GST_API_VERSION);
1606 gst_printerr ("\n\n"),
1607 gst_printerr ("%s\n\n",
1608 _("You must provide at least one filename or URI to play."));
1609 /* No input provided. Free array */
1610 g_ptr_array_free (playlist, TRUE);
1612 g_free (audio_sink);
1613 g_free (video_sink);
1619 if (filenames != NULL && *filenames != NULL) {
1620 num = g_strv_length (filenames);
1621 for (i = 0; i < num; ++i) {
1622 GST_LOG ("command line argument: %s", filenames[i]);
1623 add_to_playlist (playlist, filenames[i]);
1625 g_strfreev (filenames);
1628 num = playlist->len;
1629 g_ptr_array_add (playlist, NULL);
1631 uris = (gchar **) g_ptr_array_free (playlist, FALSE);
1634 shuffle_uris (uris, num);
1637 play = play_new (uris, audio_sink, video_sink, gapless, volume, verbose,
1638 flags, use_playbin3, start_position);
1642 ("Failed to create 'playbin' element. Check your GStreamer installation.\n");
1643 return EXIT_FAILURE;
1647 if (gst_play_kb_set_key_handler (keyboard_cb, play)) {
1648 gst_print (_("Press 'k' to see a list of keyboard shortcuts.\n"));
1649 atexit (restore_terminal);
1651 gst_print ("Interactive keyboard handling in terminal not available.\n");
1661 g_free (audio_sink);
1662 g_free (video_sink);