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/math-compat.h>
39 #include <glib/gprintf.h>
41 #include "gst-play-kb.h"
43 #define VOLUME_STEPS 20
45 GST_DEBUG_CATEGORY (play_debug);
46 #define GST_CAT_DEFAULT play_debug
50 GST_PLAY_TRICK_MODE_NONE = 0,
51 GST_PLAY_TRICK_MODE_DEFAULT,
52 GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO,
53 GST_PLAY_TRICK_MODE_KEY_UNITS,
54 GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO,
55 GST_PLAY_TRICK_MODE_LAST
70 /* missing plugin messages */
76 GstState desired_state; /* as per user interaction, PAUSED or PLAYING */
81 GstPlayTrickMode trick_mode;
85 static gboolean quiet = FALSE;
87 static gboolean play_bus_msg (GstBus * bus, GstMessage * msg, gpointer data);
88 static gboolean play_next (GstPlay * play);
89 static gboolean play_prev (GstPlay * play);
90 static gboolean play_timeout (gpointer user_data);
91 static void play_about_to_finish (GstElement * playbin, gpointer user_data);
92 static void play_reset (GstPlay * play);
93 static void play_set_relative_volume (GstPlay * play, gdouble volume_step);
94 static gboolean play_do_seek (GstPlay * play, gint64 pos, gdouble rate,
95 GstPlayTrickMode mode);
98 static void gst_play_printf (const gchar * format, ...) G_GNUC_PRINTF (1, 2);
101 static void keyboard_cb (const gchar * key_input, gpointer user_data);
102 static void relative_seek (GstPlay * play, gdouble percent);
105 gst_play_printf (const gchar * format, ...)
114 va_start (args, format);
116 len = g_vasprintf (&str, format, args);
120 if (len > 0 && str != NULL)
126 #define g_print gst_play_printf
129 play_new (gchar ** uris, const gchar * audio_sink, const gchar * video_sink,
130 gboolean gapless, gdouble initial_volume)
135 play = g_new0 (GstPlay, 1);
138 play->num_uris = g_strv_length (uris);
141 play->playbin = gst_element_factory_make ("playbin", "playbin");
143 if (audio_sink != NULL) {
144 if (strchr (audio_sink, ' ') != NULL)
145 sink = gst_parse_bin_from_description (audio_sink, TRUE, NULL);
147 sink = gst_element_factory_make (audio_sink, NULL);
150 g_object_set (play->playbin, "audio-sink", sink, NULL);
152 g_warning ("Couldn't create specified audio sink '%s'", audio_sink);
154 if (video_sink != NULL) {
155 if (strchr (video_sink, ' ') != NULL)
156 sink = gst_parse_bin_from_description (video_sink, TRUE, NULL);
158 sink = gst_element_factory_make (video_sink, NULL);
161 g_object_set (play->playbin, "video-sink", sink, NULL);
163 g_warning ("Couldn't create specified video sink '%s'", video_sink);
166 play->loop = g_main_loop_new (NULL, FALSE);
168 play->bus_watch = gst_bus_add_watch (GST_ELEMENT_BUS (play->playbin),
171 /* FIXME: make configurable incl. 0 for disable */
172 play->timeout = g_timeout_add (100, play_timeout, play);
174 play->missing = NULL;
175 play->buffering = FALSE;
176 play->is_live = FALSE;
178 play->desired_state = GST_STATE_PLAYING;
180 play->gapless = gapless;
182 g_signal_connect (play->playbin, "about-to-finish",
183 G_CALLBACK (play_about_to_finish), play);
186 if (initial_volume != -1)
187 play_set_relative_volume (play, initial_volume - 1.0);
190 play->trick_mode = GST_PLAY_TRICK_MODE_NONE;
196 play_free (GstPlay * play)
200 gst_element_set_state (play->playbin, GST_STATE_NULL);
201 gst_object_unref (play->playbin);
203 g_source_remove (play->bus_watch);
204 g_source_remove (play->timeout);
205 g_main_loop_unref (play->loop);
207 g_strfreev (play->uris);
211 /* reset for new file/stream */
213 play_reset (GstPlay * play)
215 g_list_foreach (play->missing, (GFunc) gst_message_unref, NULL);
216 play->missing = NULL;
218 play->buffering = FALSE;
219 play->is_live = FALSE;
223 play_set_relative_volume (GstPlay * play, gdouble volume_step)
227 volume = gst_stream_volume_get_volume (GST_STREAM_VOLUME (play->playbin),
228 GST_STREAM_VOLUME_FORMAT_CUBIC);
230 volume = round ((volume + volume_step) * VOLUME_STEPS) / VOLUME_STEPS;
231 volume = CLAMP (volume, 0.0, 10.0);
233 gst_stream_volume_set_volume (GST_STREAM_VOLUME (play->playbin),
234 GST_STREAM_VOLUME_FORMAT_CUBIC, volume);
236 g_print (_("Volume: %.0f%%"), volume * 100);
240 /* returns TRUE if something was installed and we should restart playback */
242 play_install_missing_plugins (GstPlay * play)
244 /* FIXME: implement: try to install any missing plugins we haven't
245 * tried to install before */
250 play_bus_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
252 GstPlay *play = user_data;
254 switch (GST_MESSAGE_TYPE (msg)) {
255 case GST_MESSAGE_ASYNC_DONE:
257 /* dump graph on preroll */
258 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
259 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.async-done");
261 g_print ("Prerolled.\r");
262 if (play->missing != NULL && play_install_missing_plugins (play)) {
263 g_print ("New plugins installed, trying again...\n");
268 case GST_MESSAGE_BUFFERING:{
271 if (!play->buffering)
274 gst_message_parse_buffering (msg, &percent);
275 g_print ("%s %d%% \r", _("Buffering..."), percent);
277 if (percent == 100) {
278 /* a 100% message means buffering is done */
279 if (play->buffering) {
280 play->buffering = FALSE;
281 /* no state management needed for live pipelines */
283 gst_element_set_state (play->playbin, play->desired_state);
287 if (!play->buffering) {
289 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
290 play->buffering = TRUE;
295 case GST_MESSAGE_CLOCK_LOST:{
296 g_print (_("Clock lost, selecting a new one\n"));
297 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
298 gst_element_set_state (play->playbin, GST_STATE_PLAYING);
301 case GST_MESSAGE_LATENCY:
302 g_print ("Redistribute latency...\n");
303 gst_bin_recalculate_latency (GST_BIN (play->playbin));
305 case GST_MESSAGE_REQUEST_STATE:{
309 name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
311 gst_message_parse_request_state (msg, &state);
313 g_print ("Setting state to %s as requested by %s...\n",
314 gst_element_state_get_name (state), name);
316 gst_element_set_state (play->playbin, state);
320 case GST_MESSAGE_EOS:
321 /* print final position at end */
324 /* and switch to next item in list */
325 if (!play_next (play)) {
326 g_print ("%s\n", _("Reached end of play list."));
327 g_main_loop_quit (play->loop);
330 case GST_MESSAGE_WARNING:{
334 /* dump graph on warning */
335 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
336 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.warning");
338 gst_message_parse_warning (msg, &err, &dbg);
339 g_printerr ("WARNING %s\n", err->message);
341 g_printerr ("WARNING debug information: %s\n", dbg);
346 case GST_MESSAGE_ERROR:{
350 /* dump graph on error */
351 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
352 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.error");
354 gst_message_parse_error (msg, &err, &dbg);
355 g_printerr ("ERROR %s for %s\n", err->message, play->uris[play->cur_idx]);
357 g_printerr ("ERROR debug information: %s\n", dbg);
361 /* flush any other error messages from the bus and clean up */
362 gst_element_set_state (play->playbin, GST_STATE_NULL);
364 if (play->missing != NULL && play_install_missing_plugins (play)) {
365 g_print ("New plugins installed, trying again...\n");
370 /* try next item in list then */
371 if (!play_next (play)) {
372 g_print ("%s\n", _("Reached end of play list."));
373 g_main_loop_quit (play->loop);
377 case GST_MESSAGE_ELEMENT:
379 GstNavigationMessageType mtype = gst_navigation_message_get_type (msg);
380 if (mtype == GST_NAVIGATION_MESSAGE_EVENT) {
383 if (gst_navigation_message_parse_event (msg, &ev)) {
384 GstNavigationEventType e_type = gst_navigation_event_get_type (ev);
386 case GST_NAVIGATION_EVENT_KEY_PRESS:
390 if (gst_navigation_event_parse_key_event (ev, &key)) {
391 GST_INFO ("Key press: %s", key);
393 if (strcmp (key, "Left") == 0)
394 key = GST_PLAY_KB_ARROW_LEFT;
395 else if (strcmp (key, "Right") == 0)
396 key = GST_PLAY_KB_ARROW_RIGHT;
397 else if (strcmp (key, "Up") == 0)
398 key = GST_PLAY_KB_ARROW_UP;
399 else if (strcmp (key, "Down") == 0)
400 key = GST_PLAY_KB_ARROW_DOWN;
401 else if (strcmp (key, "space") == 0)
403 else if (strlen (key) > 1)
406 keyboard_cb (key, user_data);
410 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
413 if (gst_navigation_event_parse_mouse_button_event (ev, &button,
417 relative_seek (play, +0.08);
418 } else if (button == 5) {
420 relative_seek (play, -0.01);
433 if (gst_is_missing_plugin_message (msg)) {
436 desc = gst_missing_plugin_message_get_description (msg);
437 g_print ("Missing plugin: %s\n", desc);
439 play->missing = g_list_append (play->missing, gst_message_ref (msg));
448 play_timeout (gpointer user_data)
450 GstPlay *play = user_data;
451 gint64 pos = -1, dur = -1;
452 gchar status[64] = { 0, };
457 gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos);
458 gst_element_query_duration (play->playbin, GST_FORMAT_TIME, &dur);
460 if (play->desired_state == GST_STATE_PAUSED)
461 g_snprintf (status, sizeof (status), "Paused");
463 memset (status, ' ', sizeof (status) - 1);
465 if (pos >= 0 && dur > 0) {
466 gchar dstr[32], pstr[32];
468 /* FIXME: pretty print in nicer format */
469 g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
471 g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
473 g_print ("%s / %s %s\r", pstr, dstr, status);
480 play_uri_get_display_name (GstPlay * play, const gchar * uri)
484 if (gst_uri_has_protocol (uri, "file")) {
485 loc = g_filename_from_uri (uri, NULL, NULL);
486 } else if (gst_uri_has_protocol (uri, "pushfile")) {
487 loc = g_filename_from_uri (uri + 4, NULL, NULL);
489 loc = g_strdup (uri);
492 /* Maybe additionally use glib's filename to display name function */
497 play_uri (GstPlay * play, const gchar * next_uri)
499 GstStateChangeReturn sret;
502 gst_element_set_state (play->playbin, GST_STATE_READY);
505 loc = play_uri_get_display_name (play, next_uri);
506 g_print (_("Now playing %s"), loc);
510 g_object_set (play->playbin, "uri", next_uri, NULL);
512 sret = gst_element_set_state (play->playbin, GST_STATE_PAUSED);
514 case GST_STATE_CHANGE_FAILURE:
515 /* ignore, we should get an error message posted on the bus */
517 case GST_STATE_CHANGE_NO_PREROLL:
518 g_print ("Pipeline is live.\n");
519 play->is_live = TRUE;
521 case GST_STATE_CHANGE_ASYNC:
522 g_print ("Prerolling...\r");
527 if (play->desired_state != GST_STATE_PAUSED)
528 sret = gst_element_set_state (play->playbin, play->desired_state);
531 /* returns FALSE if we have reached the end of the playlist */
533 play_next (GstPlay * play)
535 if ((play->cur_idx + 1) >= play->num_uris)
538 play_uri (play, play->uris[++play->cur_idx]);
542 /* returns FALSE if we have reached the beginning of the playlist */
544 play_prev (GstPlay * play)
546 if (play->cur_idx == 0 || play->num_uris <= 1)
549 play_uri (play, play->uris[--play->cur_idx]);
554 play_about_to_finish (GstElement * playbin, gpointer user_data)
556 GstPlay *play = user_data;
557 const gchar *next_uri;
564 next_idx = play->cur_idx + 1;
565 if (next_idx >= play->num_uris)
568 next_uri = play->uris[next_idx];
569 loc = play_uri_get_display_name (play, next_uri);
570 g_print (_("About to finish, preparing next title: %s"), loc);
574 g_object_set (play->playbin, "uri", next_uri, NULL);
575 play->cur_idx = next_idx;
579 do_play (GstPlay * play)
584 for (i = 0; i < play->num_uris; ++i)
585 GST_INFO ("%4u : %s", i, play->uris[i]);
587 if (!play_next (play))
590 g_main_loop_run (play->loop);
594 add_to_playlist (GPtrArray * playlist, const gchar * filename)
599 if (gst_uri_is_valid (filename)) {
600 g_ptr_array_add (playlist, g_strdup (filename));
604 if ((dir = g_dir_open (filename, 0, NULL))) {
607 /* FIXME: sort entries for each directory? */
608 while ((entry = g_dir_read_name (dir))) {
611 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
612 add_to_playlist (playlist, path);
620 uri = gst_filename_to_uri (filename, NULL);
622 g_ptr_array_add (playlist, uri);
624 g_warning ("Could not make URI out of filename '%s'", filename);
628 shuffle_uris (gchar ** uris, guint num)
636 for (i = 0; i < num; i++) {
637 /* gets equally distributed random number in 0..num-1 [0;num[ */
638 j = g_random_int_range (0, num);
646 restore_terminal (void)
648 gst_play_kb_set_key_handler (NULL, NULL);
652 toggle_paused (GstPlay * play)
654 if (play->desired_state == GST_STATE_PLAYING)
655 play->desired_state = GST_STATE_PAUSED;
657 play->desired_state = GST_STATE_PLAYING;
659 if (!play->buffering) {
660 gst_element_set_state (play->playbin, play->desired_state);
661 } else if (play->desired_state == GST_STATE_PLAYING) {
662 g_print ("\nWill play as soon as buffering finishes)\n");
667 relative_seek (GstPlay * play, gdouble percent)
670 gboolean seekable = FALSE;
671 gint64 dur = -1, pos = -1;
673 g_return_if_fail (percent >= -1.0 && percent <= 1.0);
675 if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
678 query = gst_query_new_seeking (GST_FORMAT_TIME);
679 if (!gst_element_query (play->playbin, query)) {
680 gst_query_unref (query);
684 gst_query_parse_seeking (query, NULL, &seekable, NULL, &dur);
685 gst_query_unref (query);
687 if (!seekable || dur <= 0)
690 pos = pos + dur * percent;
692 if (!play_next (play)) {
693 g_print ("\n%s\n", _("Reached end of play list."));
694 g_main_loop_quit (play->loop);
700 play_do_seek (play, pos, play->rate, play->trick_mode);
707 g_print ("\nCould not seek.\n");
712 play_set_rate_and_trick_mode (GstPlay * play, gdouble rate,
713 GstPlayTrickMode mode)
717 g_return_val_if_fail (rate != 0, FALSE);
719 if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
722 return play_do_seek (play, pos, rate, mode);
726 play_do_seek (GstPlay * play, gint64 pos, gdouble rate, GstPlayTrickMode mode)
728 GstSeekFlags seek_flags;
731 gboolean seekable = FALSE;
733 query = gst_query_new_seeking (GST_FORMAT_TIME);
734 if (!gst_element_query (play->playbin, query)) {
735 gst_query_unref (query);
739 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
740 gst_query_unref (query);
745 seek_flags = GST_SEEK_FLAG_FLUSH;
748 case GST_PLAY_TRICK_MODE_DEFAULT:
749 seek_flags |= GST_SEEK_FLAG_TRICKMODE;
751 case GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO:
752 seek_flags |= GST_SEEK_FLAG_TRICKMODE | GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;
754 case GST_PLAY_TRICK_MODE_KEY_UNITS:
755 seek_flags |= GST_SEEK_FLAG_TRICKMODE_KEY_UNITS;
757 case GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO:
759 GST_SEEK_FLAG_TRICKMODE_KEY_UNITS | GST_SEEK_FLAG_TRICKMODE_NO_AUDIO;
761 case GST_PLAY_TRICK_MODE_NONE:
767 seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
768 seek_flags | GST_SEEK_FLAG_KEY_UNIT,
769 /* start */ GST_SEEK_TYPE_SET, pos,
770 /* stop */ GST_SEEK_TYPE_NONE, 0);
772 seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
773 seek_flags | GST_SEEK_FLAG_ACCURATE,
774 /* start */ GST_SEEK_TYPE_SET, 0,
775 /* stop */ GST_SEEK_TYPE_SET, pos);
777 if (!gst_element_send_event (play->playbin, seek))
781 play->trick_mode = mode;
786 play_set_playback_rate (GstPlay * play, gdouble rate)
788 if (play_set_rate_and_trick_mode (play, rate, play->trick_mode)) {
789 g_print (_("Playback rate: %.2f"), rate);
793 g_print (_("Could not change playback rate to %.2f"), rate);
799 play_set_relative_playback_rate (GstPlay * play, gdouble rate_step,
800 gboolean reverse_direction)
802 gdouble new_rate = play->rate + rate_step;
804 if (reverse_direction)
807 play_set_playback_rate (play, new_rate);
811 trick_mode_get_description (GstPlayTrickMode mode)
814 case GST_PLAY_TRICK_MODE_NONE:
815 return "normal playback, trick modes disabled";
816 case GST_PLAY_TRICK_MODE_DEFAULT:
817 return "trick mode: default";
818 case GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO:
819 return "trick mode: default, no audio";
820 case GST_PLAY_TRICK_MODE_KEY_UNITS:
821 return "trick mode: key frames only";
822 case GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO:
823 return "trick mode: key frames only, no audio";
827 return "unknown trick mode";
831 play_switch_trick_mode (GstPlay * play)
833 GstPlayTrickMode new_mode = ++play->trick_mode;
834 const gchar *mode_desc;
836 if (new_mode == GST_PLAY_TRICK_MODE_LAST)
837 new_mode = GST_PLAY_TRICK_MODE_NONE;
839 mode_desc = trick_mode_get_description (new_mode);
841 if (play_set_rate_and_trick_mode (play, play->rate, new_mode)) {
842 g_print ("Rate: %.2f (%s) \n", play->rate, mode_desc);
844 g_print ("\nCould not change trick mode to %s.\n", mode_desc);
849 print_keyboard_help (void)
853 const gchar *key_desc;
854 const gchar *key_help;
857 N_("space"), N_("pause/unpause")}, {
858 N_("q or ESC"), N_("quit")}, {
859 ">", N_("play next")}, {
860 "<", N_("play previous")}, {
861 "\342\206\222", N_("seek forward")}, {
862 "\342\206\220", N_("seek backward")}, {
863 "\342\206\221", N_("volume up")}, {
864 "\342\206\223", N_("volume down")}, {
865 "+", N_("increase playback rate")}, {
866 "-", N_("decrease playback rate")}, {
867 "d", N_("change playback direction")}, {
868 "t", N_("enable/disable trick modes")}, {
869 "k", N_("show keyboard shortcuts")},};
870 guint i, chars_to_pad, desc_len, max_desc_len = 0;
872 g_print ("\n\n%s\n\n", _("Interactive mode - keyboard controls:"));
874 for (i = 0; i < G_N_ELEMENTS (key_controls); ++i) {
875 desc_len = g_utf8_strlen (key_controls[i].key_desc, -1);
876 max_desc_len = MAX (max_desc_len, desc_len);
880 for (i = 0; i < G_N_ELEMENTS (key_controls); ++i) {
881 chars_to_pad = max_desc_len - g_utf8_strlen (key_controls[i].key_desc, -1);
882 g_print ("\t%s", key_controls[i].key_desc);
883 g_print ("%-*s: ", chars_to_pad, "");
884 g_print ("%s\n", key_controls[i].key_help);
890 keyboard_cb (const gchar * key_input, gpointer user_data)
892 GstPlay *play = (GstPlay *) user_data;
894 switch (g_ascii_tolower (key_input[0])) {
896 print_keyboard_help ();
899 toggle_paused (play);
903 g_main_loop_quit (play->loop);
906 if (!play_next (play)) {
907 g_print ("\n%s\n", _("Reached end of play list."));
908 g_main_loop_quit (play->loop);
915 if (play->rate > -0.2 && play->rate < 0.0)
916 play_set_relative_playback_rate (play, 0.0, TRUE);
917 else if (ABS (play->rate) < 2.0)
918 play_set_relative_playback_rate (play, 0.1, FALSE);
919 else if (ABS (play->rate) < 4.0)
920 play_set_relative_playback_rate (play, 0.5, FALSE);
922 play_set_relative_playback_rate (play, 1.0, FALSE);
925 if (play->rate > 0.0 && play->rate < 0.20)
926 play_set_relative_playback_rate (play, 0.0, TRUE);
927 else if (ABS (play->rate) <= 2.0)
928 play_set_relative_playback_rate (play, -0.1, FALSE);
929 else if (ABS (play->rate) <= 4.0)
930 play_set_relative_playback_rate (play, -0.5, FALSE);
932 play_set_relative_playback_rate (play, -1.0, FALSE);
935 play_set_relative_playback_rate (play, 0.0, TRUE);
938 play_switch_trick_mode (play);
941 if (key_input[1] == '\0') {
942 g_main_loop_quit (play->loop);
947 if (strcmp (key_input, GST_PLAY_KB_ARROW_RIGHT) == 0) {
948 relative_seek (play, +0.08);
949 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_LEFT) == 0) {
950 relative_seek (play, -0.01);
951 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_UP) == 0) {
952 play_set_relative_volume (play, +1.0 / VOLUME_STEPS);
953 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_DOWN) == 0) {
954 play_set_relative_volume (play, -1.0 / VOLUME_STEPS);
956 GST_INFO ("keyboard input:");
957 for (; *key_input != '\0'; ++key_input)
958 GST_INFO (" code %3d", *key_input);
965 main (int argc, char **argv)
969 gboolean print_version = FALSE;
970 gboolean interactive = TRUE;
971 gboolean gapless = FALSE;
972 gboolean shuffle = FALSE;
974 gchar **filenames = NULL;
975 gchar *audio_sink = NULL;
976 gchar *video_sink = NULL;
981 gchar *playlist_file = NULL;
982 GOptionEntry options[] = {
983 {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
984 N_("Print version information and exit"), NULL},
985 {"videosink", 0, 0, G_OPTION_ARG_STRING, &video_sink,
986 N_("Video sink to use (default is autovideosink)"), NULL},
987 {"audiosink", 0, 0, G_OPTION_ARG_STRING, &audio_sink,
988 N_("Audio sink to use (default is autoaudiosink)"), NULL},
989 {"gapless", 0, 0, G_OPTION_ARG_NONE, &gapless,
990 N_("Enable gapless playback"), NULL},
991 {"shuffle", 0, 0, G_OPTION_ARG_NONE, &shuffle,
992 N_("Shuffle playlist"), NULL},
993 {"no-interactive", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,
995 N_("Disable interactive control via the keyboard"), NULL},
996 {"volume", 0, 0, G_OPTION_ARG_DOUBLE, &volume,
998 {"playlist", 0, 0, G_OPTION_ARG_FILENAME, &playlist_file,
999 N_("Playlist file containing input media files"), NULL},
1000 {"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
1001 N_("Do not print any output (apart from errors)"), NULL},
1002 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
1006 setlocale (LC_ALL, "");
1009 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1010 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1011 textdomain (GETTEXT_PACKAGE);
1014 g_set_prgname ("gst-play-" GST_API_VERSION);
1016 ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ...");
1017 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
1018 g_option_context_add_group (ctx, gst_init_get_option_group ());
1019 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
1020 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
1023 g_option_context_free (ctx);
1025 GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play");
1027 if (print_version) {
1030 version_str = gst_version_string ();
1031 g_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
1032 g_print ("%s\n", version_str);
1033 g_print ("%s\n", GST_PACKAGE_ORIGIN);
1034 g_free (version_str);
1036 g_free (audio_sink);
1037 g_free (video_sink);
1038 g_free (playlist_file);
1043 playlist = g_ptr_array_new ();
1045 if (playlist_file != NULL) {
1046 gchar *playlist_contents = NULL;
1047 gchar **lines = NULL;
1049 if (g_file_get_contents (playlist_file, &playlist_contents, NULL, &err)) {
1050 lines = g_strsplit (playlist_contents, "\n", 0);
1051 num = g_strv_length (lines);
1053 for (i = 0; i < num; i++) {
1054 if (lines[i][0] != '\0') {
1055 GST_LOG ("Playlist[%d]: %s", i + 1, lines[i]);
1056 add_to_playlist (playlist, lines[i]);
1060 g_free (playlist_contents);
1062 g_printerr ("Could not read playlist: %s\n", err->message);
1063 g_clear_error (&err);
1065 g_free (playlist_file);
1066 playlist_file = NULL;
1069 if (playlist->len == 0 && (filenames == NULL || *filenames == NULL)) {
1070 g_printerr (_("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."),
1071 "gst-play-" GST_API_VERSION);
1072 g_printerr ("\n\n"),
1073 g_printerr ("%s\n\n",
1074 _("You must provide at least one filename or URI to play."));
1075 /* No input provided. Free array */
1076 g_ptr_array_free (playlist, TRUE);
1078 g_free (audio_sink);
1079 g_free (video_sink);
1085 if (filenames != NULL && *filenames != NULL) {
1086 num = g_strv_length (filenames);
1087 for (i = 0; i < num; ++i) {
1088 GST_LOG ("command line argument: %s", filenames[i]);
1089 add_to_playlist (playlist, filenames[i]);
1091 g_strfreev (filenames);
1094 num = playlist->len;
1095 g_ptr_array_add (playlist, NULL);
1097 uris = (gchar **) g_ptr_array_free (playlist, FALSE);
1100 shuffle_uris (uris, num);
1103 play = play_new (uris, audio_sink, video_sink, gapless, volume);
1106 if (gst_play_kb_set_key_handler (keyboard_cb, play)) {
1107 g_print (_("Press 'k' to see a list of keyboard shortcuts.\n"));
1108 atexit (restore_terminal);
1110 g_print ("Interactive keyboard handling in terminal not available.\n");
1120 g_free (audio_sink);
1121 g_free (video_sink);