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.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
29 #include <gst/gst-i18n-app.h>
30 #include <gst/audio/audio.h>
31 #include <gst/pbutils/pbutils.h>
32 #include <gst/math-compat.h>
37 #include "gst-play-kb.h"
39 #define VOLUME_STEPS 20
41 GST_DEBUG_CATEGORY (play_debug);
42 #define GST_CAT_DEFAULT play_debug
56 /* missing plugin messages */
62 GstState desired_state; /* as per user interaction, PAUSED or PLAYING */
68 static gboolean play_bus_msg (GstBus * bus, GstMessage * msg, gpointer data);
69 static gboolean play_next (GstPlay * play);
70 static gboolean play_prev (GstPlay * play);
71 static gboolean play_timeout (gpointer user_data);
72 static void play_about_to_finish (GstElement * playbin, gpointer user_data);
73 static void play_reset (GstPlay * play);
74 static void play_set_relative_volume (GstPlay * play, gdouble volume_step);
77 play_new (gchar ** uris, const gchar * audio_sink, const gchar * video_sink,
78 gboolean gapless, gdouble initial_volume)
83 play = g_new0 (GstPlay, 1);
86 play->num_uris = g_strv_length (uris);
89 play->playbin = gst_element_factory_make ("playbin", "playbin");
91 if (audio_sink != NULL) {
92 if (strchr (audio_sink, ' ') != NULL)
93 sink = gst_parse_bin_from_description (audio_sink, TRUE, NULL);
95 sink = gst_element_factory_make (audio_sink, NULL);
98 g_object_set (play->playbin, "audio-sink", sink, NULL);
100 g_warning ("Couldn't create specified audio sink '%s'", audio_sink);
102 if (video_sink != NULL) {
103 if (strchr (video_sink, ' ') != NULL)
104 sink = gst_parse_bin_from_description (video_sink, TRUE, NULL);
106 sink = gst_element_factory_make (video_sink, NULL);
109 g_object_set (play->playbin, "video-sink", sink, NULL);
111 g_warning ("Couldn't create specified video sink '%s'", video_sink);
114 play->loop = g_main_loop_new (NULL, FALSE);
116 play->bus_watch = gst_bus_add_watch (GST_ELEMENT_BUS (play->playbin),
119 /* FIXME: make configurable incl. 0 for disable */
120 play->timeout = g_timeout_add (100, play_timeout, play);
122 play->missing = NULL;
123 play->buffering = FALSE;
124 play->is_live = FALSE;
126 play->desired_state = GST_STATE_PLAYING;
128 play->gapless = gapless;
130 g_signal_connect (play->playbin, "about-to-finish",
131 G_CALLBACK (play_about_to_finish), play);
134 play_set_relative_volume (play, initial_volume - 1.0);
140 play_free (GstPlay * play)
144 gst_element_set_state (play->playbin, GST_STATE_NULL);
145 gst_object_unref (play->playbin);
147 g_source_remove (play->bus_watch);
148 g_source_remove (play->timeout);
149 g_main_loop_unref (play->loop);
151 g_strfreev (play->uris);
155 /* reset for new file/stream */
157 play_reset (GstPlay * play)
159 g_list_foreach (play->missing, (GFunc) gst_message_unref, NULL);
160 play->missing = NULL;
162 play->buffering = FALSE;
163 play->is_live = FALSE;
167 play_set_relative_volume (GstPlay * play, gdouble volume_step)
171 volume = gst_stream_volume_get_volume (GST_STREAM_VOLUME (play->playbin),
172 GST_STREAM_VOLUME_FORMAT_CUBIC);
174 volume = round ((volume + volume_step) * VOLUME_STEPS) / VOLUME_STEPS;
175 volume = CLAMP (volume, 0.0, 10.0);
177 gst_stream_volume_set_volume (GST_STREAM_VOLUME (play->playbin),
178 GST_STREAM_VOLUME_FORMAT_CUBIC, volume);
180 g_print ("Volume: %.0f%% \n", volume * 100);
183 /* returns TRUE if something was installed and we should restart playback */
185 play_install_missing_plugins (GstPlay * play)
187 /* FIXME: implement: try to install any missing plugins we haven't
188 * tried to install before */
193 play_bus_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
195 GstPlay *play = user_data;
197 switch (GST_MESSAGE_TYPE (msg)) {
198 case GST_MESSAGE_ASYNC_DONE:
200 /* dump graph on preroll */
201 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
202 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.async-done");
204 g_print ("Prerolled.\r");
205 if (play->missing != NULL && play_install_missing_plugins (play)) {
206 g_print ("New plugins installed, trying again...\n");
211 case GST_MESSAGE_BUFFERING:{
214 if (!play->buffering)
217 gst_message_parse_buffering (msg, &percent);
218 g_print ("%s %d%% \r", _("Buffering..."), percent);
220 if (percent == 100) {
221 /* a 100% message means buffering is done */
222 if (play->buffering) {
223 play->buffering = FALSE;
224 /* no state management needed for live pipelines */
226 gst_element_set_state (play->playbin, play->desired_state);
230 if (!play->buffering) {
232 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
233 play->buffering = TRUE;
238 case GST_MESSAGE_CLOCK_LOST:{
239 g_print (_("Clock lost, selecting a new one\n"));
240 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
241 gst_element_set_state (play->playbin, GST_STATE_PLAYING);
244 case GST_MESSAGE_LATENCY:
245 g_print ("Redistribute latency...\n");
246 gst_bin_recalculate_latency (GST_BIN (play->playbin));
248 case GST_MESSAGE_REQUEST_STATE:{
252 name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
254 gst_message_parse_request_state (msg, &state);
256 g_print ("Setting state to %s as requested by %s...\n",
257 gst_element_state_get_name (state), name);
259 gst_element_set_state (play->playbin, state);
263 case GST_MESSAGE_EOS:
264 /* print final position at end */
267 /* and switch to next item in list */
268 if (!play_next (play)) {
269 g_print ("Reached end of play list.\n");
270 g_main_loop_quit (play->loop);
273 case GST_MESSAGE_WARNING:{
277 /* dump graph on warning */
278 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
279 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.warning");
281 gst_message_parse_warning (msg, &err, &dbg);
282 g_printerr ("WARNING %s\n", err->message);
284 g_printerr ("WARNING debug information: %s\n", dbg);
289 case GST_MESSAGE_ERROR:{
293 /* dump graph on error */
294 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
295 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.error");
297 gst_message_parse_error (msg, &err, &dbg);
298 g_printerr ("ERROR %s for %s\n", err->message, play->uris[play->cur_idx]);
300 g_printerr ("ERROR debug information: %s\n", dbg);
304 /* flush any other error messages from the bus and clean up */
305 gst_element_set_state (play->playbin, GST_STATE_NULL);
307 if (play->missing != NULL && play_install_missing_plugins (play)) {
308 g_print ("New plugins installed, trying again...\n");
313 /* try next item in list then */
314 if (!play_next (play)) {
315 g_print ("Reached end of play list.\n");
316 g_main_loop_quit (play->loop);
321 if (gst_is_missing_plugin_message (msg)) {
324 desc = gst_missing_plugin_message_get_description (msg);
325 g_print ("Missing plugin: %s\n", desc);
327 play->missing = g_list_append (play->missing, gst_message_ref (msg));
336 play_timeout (gpointer user_data)
338 GstPlay *play = user_data;
339 gint64 pos = -1, dur = -1;
340 gchar status[64] = { 0, };
345 gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos);
346 gst_element_query_duration (play->playbin, GST_FORMAT_TIME, &dur);
348 if (play->desired_state == GST_STATE_PAUSED)
349 g_snprintf (status, sizeof (status), "Paused");
351 memset (status, ' ', sizeof (status) - 1);
353 if (pos >= 0 && dur > 0) {
354 gchar dstr[32], pstr[32];
356 /* FIXME: pretty print in nicer format */
357 g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
359 g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
361 g_print ("%s / %s %s\r", pstr, dstr, status);
368 play_uri_get_display_name (GstPlay * play, const gchar * uri)
372 if (gst_uri_has_protocol (uri, "file")) {
373 loc = g_filename_from_uri (uri, NULL, NULL);
374 } else if (gst_uri_has_protocol (uri, "pushfile")) {
375 loc = g_filename_from_uri (uri + 4, NULL, NULL);
377 loc = g_strdup (uri);
380 /* Maybe additionally use glib's filename to display name function */
385 play_uri (GstPlay * play, const gchar * next_uri)
387 GstStateChangeReturn sret;
390 gst_element_set_state (play->playbin, GST_STATE_READY);
393 loc = play_uri_get_display_name (play, next_uri);
394 g_print ("Now playing %s\n", loc);
397 g_object_set (play->playbin, "uri", next_uri, NULL);
399 sret = gst_element_set_state (play->playbin, GST_STATE_PAUSED);
401 case GST_STATE_CHANGE_FAILURE:
402 /* ignore, we should get an error message posted on the bus */
404 case GST_STATE_CHANGE_NO_PREROLL:
405 g_print ("Pipeline is live.\n");
406 play->is_live = TRUE;
408 case GST_STATE_CHANGE_ASYNC:
409 g_print ("Prerolling...\r");
414 if (play->desired_state != GST_STATE_PAUSED)
415 sret = gst_element_set_state (play->playbin, play->desired_state);
418 /* returns FALSE if we have reached the end of the playlist */
420 play_next (GstPlay * play)
422 if ((play->cur_idx + 1) >= play->num_uris)
425 play_uri (play, play->uris[++play->cur_idx]);
429 /* returns FALSE if we have reached the beginning of the playlist */
431 play_prev (GstPlay * play)
433 if (play->cur_idx == 0 || play->num_uris <= 1)
436 play_uri (play, play->uris[--play->cur_idx]);
441 play_about_to_finish (GstElement * playbin, gpointer user_data)
443 GstPlay *play = user_data;
444 const gchar *next_uri;
451 next_idx = play->cur_idx + 1;
452 if (next_idx >= play->num_uris)
455 next_uri = play->uris[next_idx];
456 loc = play_uri_get_display_name (play, next_uri);
457 g_print ("About to finish, preparing next title: %s\n", loc);
460 g_object_set (play->playbin, "uri", next_uri, NULL);
461 play->cur_idx = next_idx;
465 do_play (GstPlay * play)
470 for (i = 0; i < play->num_uris; ++i)
471 GST_INFO ("%4u : %s", i, play->uris[i]);
473 if (!play_next (play))
476 g_main_loop_run (play->loop);
480 add_to_playlist (GPtrArray * playlist, const gchar * filename)
485 if (gst_uri_is_valid (filename)) {
486 g_ptr_array_add (playlist, g_strdup (filename));
490 if ((dir = g_dir_open (filename, 0, NULL))) {
493 /* FIXME: sort entries for each directory? */
494 while ((entry = g_dir_read_name (dir))) {
497 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
498 add_to_playlist (playlist, path);
506 uri = gst_filename_to_uri (filename, NULL);
508 g_ptr_array_add (playlist, uri);
510 g_warning ("Could not make URI out of filename '%s'", filename);
514 shuffle_uris (gchar ** uris, guint num)
522 for (i = 0; i < num; i++) {
523 /* gets equally distributed random number in 0..num-1 [0;num[ */
524 j = g_random_int_range (0, num);
532 restore_terminal (void)
534 gst_play_kb_set_key_handler (NULL, NULL);
538 toggle_paused (GstPlay * play)
540 if (play->desired_state == GST_STATE_PLAYING)
541 play->desired_state = GST_STATE_PAUSED;
543 play->desired_state = GST_STATE_PLAYING;
545 if (!play->buffering) {
546 gst_element_set_state (play->playbin, play->desired_state);
547 } else if (play->desired_state == GST_STATE_PLAYING) {
548 g_print ("\nWill play as soon as buffering finishes)\n");
553 relative_seek (GstPlay * play, gdouble percent)
556 gboolean seekable = FALSE;
557 gint64 dur = -1, pos = -1;
559 g_return_if_fail (percent >= -1.0 && percent <= 1.0);
561 if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
564 query = gst_query_new_seeking (GST_FORMAT_TIME);
565 if (!gst_element_query (play->playbin, query)) {
566 gst_query_unref (query);
570 gst_query_parse_seeking (query, NULL, &seekable, NULL, &dur);
571 gst_query_unref (query);
573 if (!seekable || dur <= 0)
576 pos = pos + dur * percent;
578 if (!play_next (play)) {
579 g_print ("\nReached end of play list.\n");
580 g_main_loop_quit (play->loop);
585 if (!gst_element_seek_simple (play->playbin, GST_FORMAT_TIME,
586 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, pos))
594 g_print ("\nCould not seek.\n");
599 keyboard_cb (const gchar * key_input, gpointer user_data)
601 GstPlay *play = (GstPlay *) user_data;
603 switch (g_ascii_tolower (key_input[0])) {
605 toggle_paused (play);
609 g_main_loop_quit (play->loop);
612 if (!play_next (play)) {
613 g_print ("\nReached end of play list.\n");
614 g_main_loop_quit (play->loop);
621 if (key_input[1] == '\0') {
622 g_main_loop_quit (play->loop);
627 if (strcmp (key_input, GST_PLAY_KB_ARROW_RIGHT) == 0) {
628 relative_seek (play, +0.08);
629 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_LEFT) == 0) {
630 relative_seek (play, -0.01);
631 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_UP) == 0) {
632 play_set_relative_volume (play, +1.0 / VOLUME_STEPS);
633 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_DOWN) == 0) {
634 play_set_relative_volume (play, -1.0 / VOLUME_STEPS);
636 GST_INFO ("keyboard input:");
637 for (; *key_input != '\0'; ++key_input)
638 GST_INFO (" code %3d", *key_input);
645 main (int argc, char **argv)
649 gboolean print_version = FALSE;
650 gboolean interactive = FALSE; /* FIXME: maybe enable by default? */
651 gboolean gapless = FALSE;
652 gboolean shuffle = FALSE;
653 gdouble volume = 1.0;
654 gchar **filenames = NULL;
655 gchar *audio_sink = NULL;
656 gchar *video_sink = NULL;
661 gchar *playlist_file = NULL;
662 GOptionEntry options[] = {
663 {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
664 N_("Print version information and exit"), NULL},
665 {"videosink", 0, 0, G_OPTION_ARG_STRING, &video_sink,
666 N_("Video sink to use (default is autovideosink)"), NULL},
667 {"audiosink", 0, 0, G_OPTION_ARG_STRING, &audio_sink,
668 N_("Audio sink to use (default is autoaudiosink)"), NULL},
669 {"gapless", 0, 0, G_OPTION_ARG_NONE, &gapless,
670 N_("Enable gapless playback"), NULL},
671 {"shuffle", 0, 0, G_OPTION_ARG_NONE, &shuffle,
672 N_("Shuffle playlist"), NULL},
673 {"interactive", 0, 0, G_OPTION_ARG_NONE, &interactive,
674 N_("Interactive control via keyboard"), NULL},
675 {"volume", 0, 0, G_OPTION_ARG_DOUBLE, &volume,
677 {"playlist", 0, 0, G_OPTION_ARG_FILENAME, &playlist_file,
678 N_("Playlist file containing input media files"), NULL},
679 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
683 setlocale (LC_ALL, "");
686 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
687 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
688 textdomain (GETTEXT_PACKAGE);
691 g_set_prgname ("gst-play-" GST_API_VERSION);
693 ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ...");
694 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
695 g_option_context_add_group (ctx, gst_init_get_option_group ());
696 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
697 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
700 g_option_context_free (ctx);
702 GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play");
707 version_str = gst_version_string ();
708 g_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
709 g_print ("%s\n", version_str);
710 g_print ("%s\n", GST_PACKAGE_ORIGIN);
711 g_free (version_str);
715 g_free (playlist_file);
720 playlist = g_ptr_array_new ();
722 if (playlist_file != NULL) {
723 gchar *playlist_contents = NULL;
724 gchar **lines = NULL;
726 if (g_file_get_contents (playlist_file, &playlist_contents, NULL, &err)) {
727 lines = g_strsplit (playlist_contents, "\n", 0);
728 num = g_strv_length (lines);
730 for (i = 0; i < num; i++) {
731 if (lines[i][0] != '\0') {
732 GST_LOG ("Playlist[%d]: %s", i + 1, lines[i]);
733 add_to_playlist (playlist, lines[i]);
737 g_free (playlist_contents);
739 g_printerr ("Could not read playlist: %s\n", err->message);
740 g_clear_error (&err);
742 g_free (playlist_file);
743 playlist_file = NULL;
746 if (playlist->len == 0 && (filenames == NULL || *filenames == NULL)) {
747 g_printerr (_("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."),
748 "gst-play-" GST_API_VERSION);
750 g_printerr ("%s\n\n",
751 _("You must provide at least one filename or URI to play."));
752 /* No input provided. Free array */
753 g_ptr_array_free (playlist, TRUE);
762 if (filenames != NULL && *filenames != NULL) {
763 num = g_strv_length (filenames);
764 for (i = 0; i < num; ++i) {
765 GST_LOG ("command line argument: %s", filenames[i]);
766 add_to_playlist (playlist, filenames[i]);
768 g_strfreev (filenames);
772 g_ptr_array_add (playlist, NULL);
774 uris = (gchar **) g_ptr_array_free (playlist, FALSE);
777 shuffle_uris (uris, num);
780 play = play_new (uris, audio_sink, video_sink, gapless, volume);
783 if (gst_play_kb_set_key_handler (keyboard_cb, play)) {
784 atexit (restore_terminal);
786 g_print ("Interactive keyboard handling in terminal not available.\n");