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 GST_DEBUG_CATEGORY (play_debug);
47 #define GST_CAT_DEFAULT play_debug
51 GST_PLAY_TRICK_MODE_NONE = 0,
52 GST_PLAY_TRICK_MODE_DEFAULT,
53 GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO,
54 GST_PLAY_TRICK_MODE_KEY_UNITS,
55 GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO,
56 GST_PLAY_TRICK_MODE_LAST
61 GST_PLAY_TRACK_TYPE_INVALID = 0,
62 GST_PLAY_TRACK_TYPE_AUDIO,
63 GST_PLAY_TRACK_TYPE_VIDEO,
64 GST_PLAY_TRACK_TYPE_SUBTITLE
79 /* missing plugin messages */
85 GstState desired_state; /* as per user interaction, PAUSED or PLAYING */
87 gulong deep_notify_id;
92 GstPlayTrickMode trick_mode;
96 static gboolean quiet = FALSE;
98 static gboolean play_bus_msg (GstBus * bus, GstMessage * msg, gpointer data);
99 static gboolean play_next (GstPlay * play);
100 static gboolean play_prev (GstPlay * play);
101 static gboolean play_timeout (gpointer user_data);
102 static void play_about_to_finish (GstElement * playbin, gpointer user_data);
103 static void play_reset (GstPlay * play);
104 static void play_set_relative_volume (GstPlay * play, gdouble volume_step);
105 static gboolean play_do_seek (GstPlay * play, gint64 pos, gdouble rate,
106 GstPlayTrickMode mode);
109 static void gst_play_printf (const gchar * format, ...) G_GNUC_PRINTF (1, 2);
112 static void keyboard_cb (const gchar * key_input, gpointer user_data);
113 static void relative_seek (GstPlay * play, gdouble percent);
116 gst_play_printf (const gchar * format, ...)
125 va_start (args, format);
127 len = g_vasprintf (&str, format, args);
131 if (len > 0 && str != NULL)
137 #define g_print gst_play_printf
140 play_new (gchar ** uris, const gchar * audio_sink, const gchar * video_sink,
141 gboolean gapless, gdouble initial_volume, gboolean verbose,
142 const gchar * flags_string)
144 GstElement *sink, *playbin;
147 playbin = gst_element_factory_make ("playbin", "playbin");
151 play = g_new0 (GstPlay, 1);
154 play->num_uris = g_strv_length (uris);
157 play->playbin = playbin;
159 if (audio_sink != NULL) {
160 if (strchr (audio_sink, ' ') != NULL)
161 sink = gst_parse_bin_from_description (audio_sink, TRUE, NULL);
163 sink = gst_element_factory_make (audio_sink, NULL);
166 g_object_set (play->playbin, "audio-sink", sink, NULL);
168 g_warning ("Couldn't create specified audio sink '%s'", audio_sink);
170 if (video_sink != NULL) {
171 if (strchr (video_sink, ' ') != NULL)
172 sink = gst_parse_bin_from_description (video_sink, TRUE, NULL);
174 sink = gst_element_factory_make (video_sink, NULL);
177 g_object_set (play->playbin, "video-sink", sink, NULL);
179 g_warning ("Couldn't create specified video sink '%s'", video_sink);
182 if (flags_string != NULL) {
187 g_object_class_find_property (G_OBJECT_GET_CLASS (playbin), "flags");
188 g_value_init (&val, pspec->value_type);
189 if (gst_value_deserialize (&val, flags_string))
190 g_object_set_property (G_OBJECT (play->playbin), "flags", &val);
192 g_printerr ("Couldn't convert '%s' to playbin flags!\n", flags_string);
193 g_value_unset (&val);
197 play->deep_notify_id = g_signal_connect (play->playbin, "deep-notify",
198 G_CALLBACK (gst_object_default_deep_notify), NULL);
201 play->loop = g_main_loop_new (NULL, FALSE);
203 play->bus_watch = gst_bus_add_watch (GST_ELEMENT_BUS (play->playbin),
206 /* FIXME: make configurable incl. 0 for disable */
207 play->timeout = g_timeout_add (100, play_timeout, play);
209 play->missing = NULL;
210 play->buffering = FALSE;
211 play->is_live = FALSE;
213 play->desired_state = GST_STATE_PLAYING;
215 play->gapless = gapless;
217 g_signal_connect (play->playbin, "about-to-finish",
218 G_CALLBACK (play_about_to_finish), play);
221 if (initial_volume != -1)
222 play_set_relative_volume (play, initial_volume - 1.0);
225 play->trick_mode = GST_PLAY_TRICK_MODE_NONE;
231 play_free (GstPlay * play)
233 /* No need to see all those pad caps going to NULL etc., it's just noise */
234 if (play->deep_notify_id != 0)
235 g_signal_handler_disconnect (play->playbin, play->deep_notify_id);
239 gst_element_set_state (play->playbin, GST_STATE_NULL);
240 gst_object_unref (play->playbin);
242 g_source_remove (play->bus_watch);
243 g_source_remove (play->timeout);
244 g_main_loop_unref (play->loop);
246 g_strfreev (play->uris);
250 /* reset for new file/stream */
252 play_reset (GstPlay * play)
254 g_list_foreach (play->missing, (GFunc) gst_message_unref, NULL);
255 play->missing = NULL;
257 play->buffering = FALSE;
258 play->is_live = FALSE;
262 play_set_relative_volume (GstPlay * play, gdouble volume_step)
266 volume = gst_stream_volume_get_volume (GST_STREAM_VOLUME (play->playbin),
267 GST_STREAM_VOLUME_FORMAT_CUBIC);
269 volume = round ((volume + volume_step) * VOLUME_STEPS) / VOLUME_STEPS;
270 volume = CLAMP (volume, 0.0, 10.0);
272 gst_stream_volume_set_volume (GST_STREAM_VOLUME (play->playbin),
273 GST_STREAM_VOLUME_FORMAT_CUBIC, volume);
275 g_print (_("Volume: %.0f%%"), volume * 100);
279 /* returns TRUE if something was installed and we should restart playback */
281 play_install_missing_plugins (GstPlay * play)
283 /* FIXME: implement: try to install any missing plugins we haven't
284 * tried to install before */
289 play_bus_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
291 GstPlay *play = user_data;
293 switch (GST_MESSAGE_TYPE (msg)) {
294 case GST_MESSAGE_ASYNC_DONE:
296 /* dump graph on preroll */
297 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
298 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.async-done");
300 g_print ("Prerolled.\r");
301 if (play->missing != NULL && play_install_missing_plugins (play)) {
302 g_print ("New plugins installed, trying again...\n");
307 case GST_MESSAGE_BUFFERING:{
310 if (!play->buffering)
313 gst_message_parse_buffering (msg, &percent);
314 g_print ("%s %d%% \r", _("Buffering..."), percent);
316 if (percent == 100) {
317 /* a 100% message means buffering is done */
318 if (play->buffering) {
319 play->buffering = FALSE;
320 /* no state management needed for live pipelines */
322 gst_element_set_state (play->playbin, play->desired_state);
326 if (!play->buffering) {
328 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
329 play->buffering = TRUE;
334 case GST_MESSAGE_CLOCK_LOST:{
335 g_print (_("Clock lost, selecting a new one\n"));
336 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
337 gst_element_set_state (play->playbin, GST_STATE_PLAYING);
340 case GST_MESSAGE_LATENCY:
341 g_print ("Redistribute latency...\n");
342 gst_bin_recalculate_latency (GST_BIN (play->playbin));
344 case GST_MESSAGE_REQUEST_STATE:{
348 name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
350 gst_message_parse_request_state (msg, &state);
352 g_print ("Setting state to %s as requested by %s...\n",
353 gst_element_state_get_name (state), name);
355 gst_element_set_state (play->playbin, state);
359 case GST_MESSAGE_EOS:
360 /* print final position at end */
363 /* and switch to next item in list */
364 if (!play_next (play)) {
365 g_print ("%s\n", _("Reached end of play list."));
366 g_main_loop_quit (play->loop);
369 case GST_MESSAGE_WARNING:{
373 /* dump graph on warning */
374 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
375 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.warning");
377 gst_message_parse_warning (msg, &err, &dbg);
378 g_printerr ("WARNING %s\n", err->message);
380 g_printerr ("WARNING debug information: %s\n", dbg);
381 g_clear_error (&err);
385 case GST_MESSAGE_ERROR:{
389 /* dump graph on error */
390 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
391 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.error");
393 gst_message_parse_error (msg, &err, &dbg);
394 g_printerr ("ERROR %s for %s\n", err->message, play->uris[play->cur_idx]);
396 g_printerr ("ERROR debug information: %s\n", dbg);
397 g_clear_error (&err);
400 /* flush any other error messages from the bus and clean up */
401 gst_element_set_state (play->playbin, GST_STATE_NULL);
403 if (play->missing != NULL && play_install_missing_plugins (play)) {
404 g_print ("New plugins installed, trying again...\n");
409 /* try next item in list then */
410 if (!play_next (play)) {
411 g_print ("%s\n", _("Reached end of play list."));
412 g_main_loop_quit (play->loop);
416 case GST_MESSAGE_ELEMENT:
418 GstNavigationMessageType mtype = gst_navigation_message_get_type (msg);
419 if (mtype == GST_NAVIGATION_MESSAGE_EVENT) {
422 if (gst_navigation_message_parse_event (msg, &ev)) {
423 GstNavigationEventType e_type = gst_navigation_event_get_type (ev);
425 case GST_NAVIGATION_EVENT_KEY_PRESS:
429 if (gst_navigation_event_parse_key_event (ev, &key)) {
430 GST_INFO ("Key press: %s", key);
432 if (strcmp (key, "Left") == 0)
433 key = GST_PLAY_KB_ARROW_LEFT;
434 else if (strcmp (key, "Right") == 0)
435 key = GST_PLAY_KB_ARROW_RIGHT;
436 else if (strcmp (key, "Up") == 0)
437 key = GST_PLAY_KB_ARROW_UP;
438 else if (strcmp (key, "Down") == 0)
439 key = GST_PLAY_KB_ARROW_DOWN;
440 else if (strcmp (key, "space") == 0)
442 else if (strlen (key) > 1)
445 keyboard_cb (key, user_data);
449 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
452 if (gst_navigation_event_parse_mouse_button_event (ev, &button,
456 relative_seek (play, +0.08);
457 } else if (button == 5) {
459 relative_seek (play, -0.01);
469 gst_event_unref (ev);
474 if (gst_is_missing_plugin_message (msg)) {
477 desc = gst_missing_plugin_message_get_description (msg);
478 g_print ("Missing plugin: %s\n", desc);
480 play->missing = g_list_append (play->missing, gst_message_ref (msg));
489 play_timeout (gpointer user_data)
491 GstPlay *play = user_data;
492 gint64 pos = -1, dur = -1;
493 const gchar *paused = _("Paused");
499 gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos);
500 gst_element_query_duration (play->playbin, GST_FORMAT_TIME, &dur);
502 if (play->desired_state == GST_STATE_PAUSED) {
503 status = (gchar *) paused;
505 gint len = g_utf8_strlen (paused, -1);
506 status = g_newa (gchar, len + 1);
507 memset (status, ' ', len);
511 if (pos >= 0 && dur > 0) {
512 gchar dstr[32], pstr[32];
514 /* FIXME: pretty print in nicer format */
515 g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
517 g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
519 g_print ("%s / %s %s\r", pstr, dstr, status);
526 play_uri_get_display_name (GstPlay * play, const gchar * uri)
530 if (gst_uri_has_protocol (uri, "file")) {
531 loc = g_filename_from_uri (uri, NULL, NULL);
532 } else if (gst_uri_has_protocol (uri, "pushfile")) {
533 loc = g_filename_from_uri (uri + 4, NULL, NULL);
535 loc = g_strdup (uri);
538 /* Maybe additionally use glib's filename to display name function */
543 play_uri (GstPlay * play, const gchar * next_uri)
547 gst_element_set_state (play->playbin, GST_STATE_READY);
550 loc = play_uri_get_display_name (play, next_uri);
551 g_print (_("Now playing %s\n"), loc);
554 g_object_set (play->playbin, "uri", next_uri, NULL);
556 switch (gst_element_set_state (play->playbin, GST_STATE_PAUSED)) {
557 case GST_STATE_CHANGE_FAILURE:
558 /* ignore, we should get an error message posted on the bus */
560 case GST_STATE_CHANGE_NO_PREROLL:
561 g_print ("Pipeline is live.\n");
562 play->is_live = TRUE;
564 case GST_STATE_CHANGE_ASYNC:
565 g_print ("Prerolling...\r");
571 if (play->desired_state != GST_STATE_PAUSED)
572 gst_element_set_state (play->playbin, play->desired_state);
575 /* returns FALSE if we have reached the end of the playlist */
577 play_next (GstPlay * play)
579 if ((play->cur_idx + 1) >= play->num_uris)
582 play_uri (play, play->uris[++play->cur_idx]);
586 /* returns FALSE if we have reached the beginning of the playlist */
588 play_prev (GstPlay * play)
590 if (play->cur_idx == 0 || play->num_uris <= 1)
593 play_uri (play, play->uris[--play->cur_idx]);
598 play_about_to_finish (GstElement * playbin, gpointer user_data)
600 GstPlay *play = user_data;
601 const gchar *next_uri;
608 next_idx = play->cur_idx + 1;
609 if (next_idx >= play->num_uris)
612 next_uri = play->uris[next_idx];
613 loc = play_uri_get_display_name (play, next_uri);
614 g_print (_("About to finish, preparing next title: %s"), loc);
618 g_object_set (play->playbin, "uri", next_uri, NULL);
619 play->cur_idx = next_idx;
623 do_play (GstPlay * play)
628 for (i = 0; i < play->num_uris; ++i)
629 GST_INFO ("%4u : %s", i, play->uris[i]);
631 if (!play_next (play))
634 g_main_loop_run (play->loop);
638 compare (gconstpointer a, gconstpointer b)
643 a1 = g_utf8_collate_key_for_filename ((gchar *) a, -1);
644 b1 = g_utf8_collate_key_for_filename ((gchar *) b, -1);
645 ret = strcmp (a1, b1);
653 add_to_playlist (GPtrArray * playlist, const gchar * filename)
658 if (gst_uri_is_valid (filename)) {
659 g_ptr_array_add (playlist, g_strdup (filename));
663 if ((dir = g_dir_open (filename, 0, NULL))) {
665 GList *l, *files = NULL;
667 while ((entry = g_dir_read_name (dir))) {
670 path = g_build_filename (filename, entry, NULL);
671 files = g_list_insert_sorted (files, path, compare);
676 for (l = files; l != NULL; l = l->next) {
677 gchar *path = (gchar *) l->data;
679 add_to_playlist (playlist, path);
686 uri = gst_filename_to_uri (filename, NULL);
688 g_ptr_array_add (playlist, uri);
690 g_warning ("Could not make URI out of filename '%s'", filename);
694 shuffle_uris (gchar ** uris, guint num)
702 for (i = 0; i < num; i++) {
703 /* gets equally distributed random number in 0..num-1 [0;num[ */
704 j = g_random_int_range (0, num);
712 restore_terminal (void)
714 gst_play_kb_set_key_handler (NULL, NULL);
718 toggle_paused (GstPlay * play)
720 if (play->desired_state == GST_STATE_PLAYING)
721 play->desired_state = GST_STATE_PAUSED;
723 play->desired_state = GST_STATE_PLAYING;
725 if (!play->buffering) {
726 gst_element_set_state (play->playbin, play->desired_state);
727 } else if (play->desired_state == GST_STATE_PLAYING) {
728 g_print ("\nWill play as soon as buffering finishes)\n");
733 relative_seek (GstPlay * play, gdouble percent)
736 gboolean seekable = FALSE;
737 gint64 dur = -1, pos = -1, step;
739 g_return_if_fail (percent >= -1.0 && percent <= 1.0);
741 if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
744 query = gst_query_new_seeking (GST_FORMAT_TIME);
745 if (!gst_element_query (play->playbin, query)) {
746 gst_query_unref (query);
750 gst_query_parse_seeking (query, NULL, &seekable, NULL, &dur);
751 gst_query_unref (query);
753 if (!seekable || dur <= 0)
756 step = dur * percent;
757 if (ABS (step) < GST_SECOND)
758 step = (percent < 0) ? -GST_SECOND : GST_SECOND;
762 if (!play_next (play)) {
763 g_print ("\n%s\n", _("Reached end of play list."));
764 g_main_loop_quit (play->loop);
770 play_do_seek (play, pos, play->rate, play->trick_mode);
777 g_print ("\nCould not seek.\n");
782 play_set_rate_and_trick_mode (GstPlay * play, gdouble rate,
783 GstPlayTrickMode mode)
787 g_return_val_if_fail (rate != 0, FALSE);
789 if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
792 return play_do_seek (play, pos, rate, mode);
796 play_do_seek (GstPlay * play, gint64 pos, gdouble rate, GstPlayTrickMode mode)
798 GstSeekFlags seek_flags;
801 gboolean seekable = FALSE;
803 query = gst_query_new_seeking (GST_FORMAT_TIME);
804 if (!gst_element_query (play->playbin, query)) {
805 gst_query_unref (query);
809 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
810 gst_query_unref (query);
815 seek_flags = GST_SEEK_FLAG_FLUSH;
818 case GST_PLAY_TRICK_MODE_DEFAULT:
819 seek_flags |= GST_SEEK_FLAG_TRICKMODE;
821 case GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO:
822 seek_flags |= GST_SEEK_FLAG_TRICKMODE | GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;
824 case GST_PLAY_TRICK_MODE_KEY_UNITS:
825 seek_flags |= GST_SEEK_FLAG_TRICKMODE_KEY_UNITS;
827 case GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO:
829 GST_SEEK_FLAG_TRICKMODE_KEY_UNITS | GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;
831 case GST_PLAY_TRICK_MODE_NONE:
837 seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
838 seek_flags | GST_SEEK_FLAG_ACCURATE,
839 /* start */ GST_SEEK_TYPE_SET, pos,
840 /* stop */ GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
842 seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
843 seek_flags | GST_SEEK_FLAG_ACCURATE,
844 /* start */ GST_SEEK_TYPE_SET, 0,
845 /* stop */ GST_SEEK_TYPE_SET, pos);
847 if (!gst_element_send_event (play->playbin, seek))
851 play->trick_mode = mode;
856 play_set_playback_rate (GstPlay * play, gdouble rate)
858 if (play_set_rate_and_trick_mode (play, rate, play->trick_mode)) {
859 g_print (_("Playback rate: %.2f"), rate);
863 g_print (_("Could not change playback rate to %.2f"), rate);
869 play_set_relative_playback_rate (GstPlay * play, gdouble rate_step,
870 gboolean reverse_direction)
872 gdouble new_rate = play->rate + rate_step;
874 if (reverse_direction)
877 play_set_playback_rate (play, new_rate);
881 trick_mode_get_description (GstPlayTrickMode mode)
884 case GST_PLAY_TRICK_MODE_NONE:
885 return "normal playback, trick modes disabled";
886 case GST_PLAY_TRICK_MODE_DEFAULT:
887 return "trick mode: default";
888 case GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO:
889 return "trick mode: default, no audio";
890 case GST_PLAY_TRICK_MODE_KEY_UNITS:
891 return "trick mode: key frames only";
892 case GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO:
893 return "trick mode: key frames only, no audio";
897 return "unknown trick mode";
901 play_switch_trick_mode (GstPlay * play)
903 GstPlayTrickMode new_mode = ++play->trick_mode;
904 const gchar *mode_desc;
906 if (new_mode == GST_PLAY_TRICK_MODE_LAST)
907 new_mode = GST_PLAY_TRICK_MODE_NONE;
909 mode_desc = trick_mode_get_description (new_mode);
911 if (play_set_rate_and_trick_mode (play, play->rate, new_mode)) {
912 g_print ("Rate: %.2f (%s) \n", play->rate, mode_desc);
914 g_print ("\nCould not change trick mode to %s.\n", mode_desc);
919 play_cycle_track_selection (GstPlay * play, GstPlayTrackType track_type)
921 const gchar *prop_cur, *prop_n, *prop_get, *name;
922 gint cur = -1, n = -1;
923 guint flag, cur_flags;
925 switch (track_type) {
926 case GST_PLAY_TRACK_TYPE_AUDIO:
927 prop_get = "get-audio-tags";
928 prop_cur = "current-audio";
933 case GST_PLAY_TRACK_TYPE_VIDEO:
934 prop_get = "get-video-tags";
935 prop_cur = "current-video";
940 case GST_PLAY_TRACK_TYPE_SUBTITLE:
941 prop_get = "get-text-tags";
942 prop_cur = "current-text";
951 g_object_get (play->playbin, prop_cur, &cur, prop_n, &n, "flags", &cur_flags,
955 g_print ("No %s tracks.\n", name);
957 gchar *lcode = NULL, *lname = NULL;
958 const gchar *lang = NULL;
959 GstTagList *tags = NULL;
961 if (!(cur_flags & flag))
964 cur = (cur + 1) % (n + 1);
966 if (cur >= n && track_type != GST_PLAY_TRACK_TYPE_VIDEO) {
968 g_print ("Disabling %s. \n", name);
969 if (cur_flags & flag) {
971 g_object_set (play->playbin, "flags", cur_flags, NULL);
974 /* For video we only want to switch between streams, not disable it altogether */
977 if (!(cur_flags & flag) && track_type != GST_PLAY_TRACK_TYPE_VIDEO) {
979 g_object_set (play->playbin, "flags", cur_flags, NULL);
981 g_signal_emit_by_name (play->playbin, prop_get, cur, &tags);
983 if (gst_tag_list_get_string (tags, GST_TAG_LANGUAGE_CODE, &lcode))
984 lang = gst_tag_get_language_name (lcode);
985 else if (gst_tag_list_get_string (tags, GST_TAG_LANGUAGE_NAME, &lname))
987 gst_tag_list_unref (tags);
990 g_print ("Switching to %s track %d of %d (%s).\n", name, cur + 1, n,
993 g_print ("Switching to %s track %d of %d.\n", name, cur + 1, n);
995 g_object_set (play->playbin, prop_cur, cur, NULL);
1002 print_keyboard_help (void)
1006 const gchar *key_desc;
1007 const gchar *key_help;
1008 } key_controls[] = {
1010 N_("space"), N_("pause/unpause")}, {
1011 N_("q or ESC"), N_("quit")}, {
1012 N_("> or n"), N_("play next")}, {
1013 N_("< or b"), N_("play previous")}, {
1014 "\342\206\222", N_("seek forward")}, {
1015 "\342\206\220", N_("seek backward")}, {
1016 "\342\206\221", N_("volume up")}, {
1017 "\342\206\223", N_("volume down")}, {
1018 "+", N_("increase playback rate")}, {
1019 "-", N_("decrease playback rate")}, {
1020 "d", N_("change playback direction")}, {
1021 "t", N_("enable/disable trick modes")}, {
1022 "a", N_("change audio track")}, {
1023 "v", N_("change video track")}, {
1024 "s", N_("change subtitle track")}, {
1025 "0", N_("seek to beginning")}, {
1026 "k", N_("show keyboard shortcuts")},};
1027 guint i, chars_to_pad, desc_len, max_desc_len = 0;
1029 g_print ("\n\n%s\n\n", _("Interactive mode - keyboard controls:"));
1031 for (i = 0; i < G_N_ELEMENTS (key_controls); ++i) {
1032 desc_len = g_utf8_strlen (key_controls[i].key_desc, -1);
1033 max_desc_len = MAX (max_desc_len, desc_len);
1037 for (i = 0; i < G_N_ELEMENTS (key_controls); ++i) {
1038 chars_to_pad = max_desc_len - g_utf8_strlen (key_controls[i].key_desc, -1);
1039 g_print ("\t%s", key_controls[i].key_desc);
1040 g_print ("%-*s: ", chars_to_pad, "");
1041 g_print ("%s\n", key_controls[i].key_help);
1047 keyboard_cb (const gchar * key_input, gpointer user_data)
1049 GstPlay *play = (GstPlay *) user_data;
1052 /* only want to switch/case on single char, not first char of string */
1053 if (key_input[0] != '\0' && key_input[1] == '\0')
1054 key = g_ascii_tolower (key_input[0]);
1058 print_keyboard_help ();
1061 toggle_paused (play);
1065 g_main_loop_quit (play->loop);
1069 if (!play_next (play)) {
1070 g_print ("\n%s\n", _("Reached end of play list."));
1071 g_main_loop_quit (play->loop);
1079 if (play->rate > -0.2 && play->rate < 0.0)
1080 play_set_relative_playback_rate (play, 0.0, TRUE);
1081 else if (ABS (play->rate) < 2.0)
1082 play_set_relative_playback_rate (play, 0.1, FALSE);
1083 else if (ABS (play->rate) < 4.0)
1084 play_set_relative_playback_rate (play, 0.5, FALSE);
1086 play_set_relative_playback_rate (play, 1.0, FALSE);
1089 if (play->rate > 0.0 && play->rate < 0.20)
1090 play_set_relative_playback_rate (play, 0.0, TRUE);
1091 else if (ABS (play->rate) <= 2.0)
1092 play_set_relative_playback_rate (play, -0.1, FALSE);
1093 else if (ABS (play->rate) <= 4.0)
1094 play_set_relative_playback_rate (play, -0.5, FALSE);
1096 play_set_relative_playback_rate (play, -1.0, FALSE);
1099 play_set_relative_playback_rate (play, 0.0, TRUE);
1102 play_switch_trick_mode (play);
1105 if (key_input[1] == '\0') {
1106 g_main_loop_quit (play->loop);
1110 play_cycle_track_selection (play, GST_PLAY_TRACK_TYPE_AUDIO);
1113 play_cycle_track_selection (play, GST_PLAY_TRACK_TYPE_VIDEO);
1116 play_cycle_track_selection (play, GST_PLAY_TRACK_TYPE_SUBTITLE);
1119 play_do_seek (play, 0, play->rate, play->trick_mode);
1122 if (strcmp (key_input, GST_PLAY_KB_ARROW_RIGHT) == 0) {
1123 relative_seek (play, +0.08);
1124 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_LEFT) == 0) {
1125 relative_seek (play, -0.01);
1126 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_UP) == 0) {
1127 play_set_relative_volume (play, +1.0 / VOLUME_STEPS);
1128 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_DOWN) == 0) {
1129 play_set_relative_volume (play, -1.0 / VOLUME_STEPS);
1131 GST_INFO ("keyboard input:");
1132 for (; *key_input != '\0'; ++key_input)
1133 GST_INFO (" code %3d", *key_input);
1140 main (int argc, char **argv)
1143 GPtrArray *playlist;
1144 gboolean verbose = FALSE;
1145 gboolean print_version = FALSE;
1146 gboolean interactive = TRUE;
1147 gboolean gapless = FALSE;
1148 gboolean shuffle = FALSE;
1149 gdouble volume = -1;
1150 gchar **filenames = NULL;
1151 gchar *audio_sink = NULL;
1152 gchar *video_sink = NULL;
1154 gchar *flags = NULL;
1157 GOptionContext *ctx;
1158 gchar *playlist_file = NULL;
1159 GOptionEntry options[] = {
1160 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
1161 N_("Output status information and property notifications"), NULL},
1162 {"flags", 0, 0, G_OPTION_ARG_STRING, &flags,
1163 N_("Control playback behaviour setting playbin 'flags' property"),
1165 {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
1166 N_("Print version information and exit"), NULL},
1167 {"videosink", 0, 0, G_OPTION_ARG_STRING, &video_sink,
1168 N_("Video sink to use (default is autovideosink)"), NULL},
1169 {"audiosink", 0, 0, G_OPTION_ARG_STRING, &audio_sink,
1170 N_("Audio sink to use (default is autoaudiosink)"), NULL},
1171 {"gapless", 0, 0, G_OPTION_ARG_NONE, &gapless,
1172 N_("Enable gapless playback"), NULL},
1173 {"shuffle", 0, 0, G_OPTION_ARG_NONE, &shuffle,
1174 N_("Shuffle playlist"), NULL},
1175 {"no-interactive", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,
1177 N_("Disable interactive control via the keyboard"), NULL},
1178 {"volume", 0, 0, G_OPTION_ARG_DOUBLE, &volume,
1179 N_("Volume"), NULL},
1180 {"playlist", 0, 0, G_OPTION_ARG_FILENAME, &playlist_file,
1181 N_("Playlist file containing input media files"), NULL},
1182 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
1183 N_("Do not print any output (apart from errors)"), NULL},
1184 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
1188 setlocale (LC_ALL, "");
1191 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1192 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1193 textdomain (GETTEXT_PACKAGE);
1196 g_set_prgname ("gst-play-" GST_API_VERSION);
1198 ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ...");
1199 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
1200 g_option_context_add_group (ctx, gst_init_get_option_group ());
1201 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
1202 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
1203 g_option_context_free (ctx);
1204 g_clear_error (&err);
1207 g_option_context_free (ctx);
1209 GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play");
1211 if (print_version) {
1214 version_str = gst_version_string ();
1215 g_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
1216 g_print ("%s\n", version_str);
1217 g_print ("%s\n", GST_PACKAGE_ORIGIN);
1218 g_free (version_str);
1220 g_free (audio_sink);
1221 g_free (video_sink);
1222 g_free (playlist_file);
1227 playlist = g_ptr_array_new ();
1229 if (playlist_file != NULL) {
1230 gchar *playlist_contents = NULL;
1231 gchar **lines = NULL;
1233 if (g_file_get_contents (playlist_file, &playlist_contents, NULL, &err)) {
1234 lines = g_strsplit (playlist_contents, "\n", 0);
1235 num = g_strv_length (lines);
1237 for (i = 0; i < num; i++) {
1238 if (lines[i][0] != '\0') {
1239 GST_LOG ("Playlist[%d]: %s", i + 1, lines[i]);
1240 add_to_playlist (playlist, lines[i]);
1244 g_free (playlist_contents);
1246 g_printerr ("Could not read playlist: %s\n", err->message);
1247 g_clear_error (&err);
1249 g_free (playlist_file);
1250 playlist_file = NULL;
1253 if (playlist->len == 0 && (filenames == NULL || *filenames == NULL)) {
1254 g_printerr (_("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."),
1255 "gst-play-" GST_API_VERSION);
1256 g_printerr ("\n\n"),
1257 g_printerr ("%s\n\n",
1258 _("You must provide at least one filename or URI to play."));
1259 /* No input provided. Free array */
1260 g_ptr_array_free (playlist, TRUE);
1262 g_free (audio_sink);
1263 g_free (video_sink);
1269 if (filenames != NULL && *filenames != NULL) {
1270 num = g_strv_length (filenames);
1271 for (i = 0; i < num; ++i) {
1272 GST_LOG ("command line argument: %s", filenames[i]);
1273 add_to_playlist (playlist, filenames[i]);
1275 g_strfreev (filenames);
1278 num = playlist->len;
1279 g_ptr_array_add (playlist, NULL);
1281 uris = (gchar **) g_ptr_array_free (playlist, FALSE);
1284 shuffle_uris (uris, num);
1288 play_new (uris, audio_sink, video_sink, gapless, volume, verbose, flags);
1292 ("Failed to create 'playbin' element. Check your GStreamer installation.\n");
1293 return EXIT_FAILURE;
1297 if (gst_play_kb_set_key_handler (keyboard_cb, play)) {
1298 g_print (_("Press 'k' to see a list of keyboard shortcuts.\n"));
1299 atexit (restore_terminal);
1301 g_print ("Interactive keyboard handling in terminal not available.\n");
1311 g_free (audio_sink);
1312 g_free (video_sink);