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_LINEAR);
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_LINEAR, 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 /* no state management needed for live pipelines */
224 if (percent == 100) {
225 /* a 100% message means buffering is done */
226 if (play->buffering) {
227 play->buffering = FALSE;
228 gst_element_set_state (play->playbin, play->desired_state);
232 if (!play->buffering) {
233 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
234 play->buffering = TRUE;
239 case GST_MESSAGE_CLOCK_LOST:{
240 g_print (_("Clock lost, selecting a new one\n"));
241 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
242 gst_element_set_state (play->playbin, GST_STATE_PLAYING);
245 case GST_MESSAGE_LATENCY:
246 g_print ("Redistribute latency...\n");
247 gst_bin_recalculate_latency (GST_BIN (play->playbin));
249 case GST_MESSAGE_REQUEST_STATE:{
253 name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
255 gst_message_parse_request_state (msg, &state);
257 g_print ("Setting state to %s as requested by %s...\n",
258 gst_element_state_get_name (state), name);
260 gst_element_set_state (play->playbin, state);
264 case GST_MESSAGE_EOS:
265 /* print final position at end */
268 /* and switch to next item in list */
269 if (!play_next (play)) {
270 g_print ("Reached end of play list.\n");
271 g_main_loop_quit (play->loop);
274 case GST_MESSAGE_WARNING:{
278 /* dump graph on warning */
279 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
280 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.warning");
282 gst_message_parse_warning (msg, &err, &dbg);
283 g_printerr ("WARNING %s\n", err->message);
285 g_printerr ("WARNING debug information: %s\n", dbg);
290 case GST_MESSAGE_ERROR:{
294 /* dump graph on error */
295 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
296 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.error");
298 gst_message_parse_error (msg, &err, &dbg);
299 g_printerr ("ERROR %s for %s\n", err->message, play->uris[play->cur_idx]);
301 g_printerr ("ERROR debug information: %s\n", dbg);
305 /* flush any other error messages from the bus and clean up */
306 gst_element_set_state (play->playbin, GST_STATE_NULL);
308 if (play->missing != NULL && play_install_missing_plugins (play)) {
309 g_print ("New plugins installed, trying again...\n");
314 /* try next item in list then */
315 if (!play_next (play)) {
316 g_print ("Reached end of play list.\n");
317 g_main_loop_quit (play->loop);
322 if (gst_is_missing_plugin_message (msg)) {
325 desc = gst_missing_plugin_message_get_description (msg);
326 g_print ("Missing plugin: %s\n", desc);
328 play->missing = g_list_append (play->missing, gst_message_ref (msg));
337 play_timeout (gpointer user_data)
339 GstPlay *play = user_data;
340 gint64 pos = -1, dur = -1;
341 gchar status[64] = { 0, };
346 gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos);
347 gst_element_query_duration (play->playbin, GST_FORMAT_TIME, &dur);
349 if (play->desired_state == GST_STATE_PAUSED)
350 g_snprintf (status, sizeof (status), "Paused");
352 memset (status, ' ', sizeof (status) - 1);
354 if (pos >= 0 && dur > 0) {
355 gchar dstr[32], pstr[32];
357 /* FIXME: pretty print in nicer format */
358 g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
360 g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
362 g_print ("%s / %s %s\r", pstr, dstr, status);
369 play_uri_get_display_name (GstPlay * play, const gchar * uri)
373 if (gst_uri_has_protocol (uri, "file")) {
374 loc = g_filename_from_uri (uri, NULL, NULL);
375 } else if (gst_uri_has_protocol (uri, "pushfile")) {
376 loc = g_filename_from_uri (uri + 4, NULL, NULL);
378 loc = g_strdup (uri);
381 /* Maybe additionally use glib's filename to display name function */
386 play_uri (GstPlay * play, const gchar * next_uri)
388 GstStateChangeReturn sret;
391 gst_element_set_state (play->playbin, GST_STATE_READY);
394 loc = play_uri_get_display_name (play, next_uri);
395 g_print ("Now playing %s\n", loc);
398 g_object_set (play->playbin, "uri", next_uri, NULL);
400 sret = gst_element_set_state (play->playbin, play->desired_state);
402 case GST_STATE_CHANGE_FAILURE:
403 /* ignore, we should get an error message posted on the bus */
405 case GST_STATE_CHANGE_NO_PREROLL:
406 g_print ("Pipeline is live.\n");
407 play->is_live = TRUE;
409 case GST_STATE_CHANGE_ASYNC:
410 g_print ("Prerolling...\r");
417 /* returns FALSE if we have reached the end of the playlist */
419 play_next (GstPlay * play)
421 if ((play->cur_idx + 1) >= play->num_uris)
424 play_uri (play, play->uris[++play->cur_idx]);
428 /* returns FALSE if we have reached the beginning of the playlist */
430 play_prev (GstPlay * play)
432 if (play->cur_idx == 0 || play->num_uris <= 1)
435 play_uri (play, play->uris[--play->cur_idx]);
440 play_about_to_finish (GstElement * playbin, gpointer user_data)
442 GstPlay *play = user_data;
443 const gchar *next_uri;
450 next_idx = play->cur_idx + 1;
451 if (next_idx >= play->num_uris)
454 next_uri = play->uris[next_idx];
455 loc = play_uri_get_display_name (play, next_uri);
456 g_print ("About to finish, preparing next title: %s\n", loc);
459 g_object_set (play->playbin, "uri", next_uri, NULL);
460 play->cur_idx = next_idx;
464 do_play (GstPlay * play)
469 for (i = 0; i < play->num_uris; ++i)
470 GST_INFO ("%4u : %s", i, play->uris[i]);
472 if (!play_next (play))
475 g_main_loop_run (play->loop);
479 add_to_playlist (GPtrArray * playlist, const gchar * filename)
484 if (gst_uri_is_valid (filename)) {
485 g_ptr_array_add (playlist, g_strdup (filename));
489 if ((dir = g_dir_open (filename, 0, NULL))) {
492 /* FIXME: sort entries for each directory? */
493 while ((entry = g_dir_read_name (dir))) {
496 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
497 add_to_playlist (playlist, path);
505 uri = gst_filename_to_uri (filename, NULL);
507 g_ptr_array_add (playlist, uri);
509 g_warning ("Could not make URI out of filename '%s'", filename);
513 shuffle_uris (gchar ** uris, guint num)
521 for (i = 0; i < num; i++) {
522 /* gets equally distributed random number in 0..num-1 [0;num[ */
523 j = g_random_int_range (0, num);
531 restore_terminal (void)
533 gst_play_kb_set_key_handler (NULL, NULL);
537 toggle_paused (GstPlay * play)
539 if (play->desired_state == GST_STATE_PLAYING)
540 play->desired_state = GST_STATE_PAUSED;
542 play->desired_state = GST_STATE_PLAYING;
544 if (!play->buffering) {
545 gst_element_set_state (play->playbin, play->desired_state);
546 } else if (play->desired_state == GST_STATE_PLAYING) {
547 g_print ("\nWill play as soon as buffering finishes)\n");
552 relative_seek (GstPlay * play, gdouble percent)
555 gboolean seekable = FALSE;
556 gint64 dur = -1, pos = -1;
558 g_return_if_fail (percent >= -1.0 && percent <= 1.0);
560 if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
563 query = gst_query_new_seeking (GST_FORMAT_TIME);
564 if (!gst_element_query (play->playbin, query)) {
565 gst_query_unref (query);
569 gst_query_parse_seeking (query, NULL, &seekable, NULL, &dur);
570 gst_query_unref (query);
572 if (!seekable || dur <= 0)
575 pos = pos + dur * percent;
577 if (!play_next (play)) {
578 g_print ("\nReached end of play list.\n");
579 g_main_loop_quit (play->loop);
584 if (!gst_element_seek_simple (play->playbin, GST_FORMAT_TIME,
585 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, pos))
593 g_print ("\nCould not seek.\n");
598 keyboard_cb (const gchar * key_input, gpointer user_data)
600 GstPlay *play = (GstPlay *) user_data;
602 switch (g_ascii_tolower (key_input[0])) {
604 toggle_paused (play);
608 g_main_loop_quit (play->loop);
611 if (!play_next (play)) {
612 g_print ("\nReached end of play list.\n");
613 g_main_loop_quit (play->loop);
620 if (key_input[1] == '\0') {
621 g_main_loop_quit (play->loop);
626 if (strcmp (key_input, GST_PLAY_KB_ARROW_RIGHT) == 0) {
627 relative_seek (play, +0.08);
628 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_LEFT) == 0) {
629 relative_seek (play, -0.01);
630 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_UP) == 0) {
631 play_set_relative_volume (play, +1.0 / VOLUME_STEPS);
632 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_DOWN) == 0) {
633 play_set_relative_volume (play, -1.0 / VOLUME_STEPS);
635 GST_INFO ("keyboard input:");
636 for (; *key_input != '\0'; ++key_input)
637 GST_INFO (" code %3d", *key_input);
644 main (int argc, char **argv)
648 gboolean print_version = FALSE;
649 gboolean interactive = FALSE; /* FIXME: maybe enable by default? */
650 gboolean gapless = FALSE;
651 gboolean shuffle = FALSE;
652 gdouble volume = 1.0;
653 gchar **filenames = NULL;
654 gchar *audio_sink = NULL;
655 gchar *video_sink = NULL;
660 GOptionEntry options[] = {
661 {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
662 N_("Print version information and exit"), NULL},
663 {"videosink", 0, 0, G_OPTION_ARG_STRING, &video_sink,
664 N_("Video sink to use (default is autovideosink)"), NULL},
665 {"audiosink", 0, 0, G_OPTION_ARG_STRING, &audio_sink,
666 N_("Audio sink to use (default is autoaudiosink)"), NULL},
667 {"gapless", 0, 0, G_OPTION_ARG_NONE, &gapless,
668 N_("Enable gapless playback"), NULL},
669 {"shuffle", 0, 0, G_OPTION_ARG_NONE, &shuffle,
670 N_("Shuffle playlist"), NULL},
671 {"interactive", 0, 0, G_OPTION_ARG_NONE, &interactive,
672 N_("Interactive control via keyboard"), NULL},
673 {"volume", 0, 0, G_OPTION_ARG_DOUBLE, &volume,
675 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
679 setlocale (LC_ALL, "");
682 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
683 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
684 textdomain (GETTEXT_PACKAGE);
687 g_set_prgname ("gst-play-" GST_API_VERSION);
689 ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ...");
690 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
691 g_option_context_add_group (ctx, gst_init_get_option_group ());
692 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
693 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
696 g_option_context_free (ctx);
698 GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play");
703 version_str = gst_version_string ();
704 g_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
705 g_print ("%s\n", version_str);
706 g_print ("%s\n", GST_PACKAGE_ORIGIN);
707 g_free (version_str);
711 if (filenames == NULL || *filenames == NULL) {
712 g_printerr (_("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."),
713 "gst-play-" GST_API_VERSION);
715 g_printerr ("%s\n\n",
716 _("You must provide at least one filename or URI to play."));
720 playlist = g_ptr_array_new ();
723 num = g_strv_length (filenames);
724 for (i = 0; i < num; ++i) {
725 GST_LOG ("command line argument: %s", filenames[i]);
726 add_to_playlist (playlist, filenames[i]);
728 g_strfreev (filenames);
731 g_ptr_array_add (playlist, NULL);
733 uris = (gchar **) g_ptr_array_free (playlist, FALSE);
736 shuffle_uris (uris, num);
739 play = play_new (uris, audio_sink, video_sink, gapless, volume);
742 if (gst_play_kb_set_key_handler (keyboard_cb, play)) {
743 atexit (restore_terminal);
745 g_print ("Interactive keyboard handling in terminal not available.\n");