1 /* GStreamer command line playback testing utility
3 * Copyright (C) 2013 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/pbutils/pbutils.h>
35 #include "gst-play-kb.h"
37 GST_DEBUG_CATEGORY (play_debug);
38 #define GST_CAT_DEFAULT play_debug
52 /* missing plugin messages */
58 GstState desired_state; /* as per user interaction, PAUSED or PLAYING */
64 static gboolean play_bus_msg (GstBus * bus, GstMessage * msg, gpointer data);
65 static gboolean play_next (GstPlay * play);
66 static gboolean play_prev (GstPlay * play);
67 static gboolean play_timeout (gpointer user_data);
68 static void play_about_to_finish (GstElement * playbin, gpointer user_data);
69 static void play_reset (GstPlay * play);
72 play_new (gchar ** uris, const gchar * audio_sink, const gchar * video_sink,
78 play = g_new0 (GstPlay, 1);
81 play->num_uris = g_strv_length (uris);
84 play->playbin = gst_element_factory_make ("playbin", "playbin");
86 if (audio_sink != NULL) {
87 if (strchr (audio_sink, ' ') != NULL)
88 sink = gst_parse_bin_from_description (audio_sink, TRUE, NULL);
90 sink = gst_element_factory_make (audio_sink, NULL);
93 g_object_set (play->playbin, "audio-sink", sink, NULL);
95 g_warning ("Couldn't create specified audio sink '%s'", audio_sink);
97 if (video_sink != NULL) {
98 if (strchr (video_sink, ' ') != NULL)
99 sink = gst_parse_bin_from_description (video_sink, TRUE, NULL);
101 sink = gst_element_factory_make (video_sink, NULL);
104 g_object_set (play->playbin, "video-sink", sink, NULL);
106 g_warning ("Couldn't create specified video sink '%s'", video_sink);
109 play->loop = g_main_loop_new (NULL, FALSE);
111 play->bus_watch = gst_bus_add_watch (GST_ELEMENT_BUS (play->playbin),
114 /* FIXME: make configurable incl. 0 for disable */
115 play->timeout = g_timeout_add (100, play_timeout, play);
117 play->missing = NULL;
118 play->buffering = FALSE;
119 play->is_live = FALSE;
121 play->desired_state = GST_STATE_PLAYING;
123 play->gapless = gapless;
125 g_signal_connect (play->playbin, "about-to-finish",
126 G_CALLBACK (play_about_to_finish), play);
133 play_free (GstPlay * play)
137 gst_element_set_state (play->playbin, GST_STATE_NULL);
138 gst_object_unref (play->playbin);
140 g_source_remove (play->bus_watch);
141 g_source_remove (play->timeout);
142 g_main_loop_unref (play->loop);
144 g_strfreev (play->uris);
148 /* reset for new file/stream */
150 play_reset (GstPlay * play)
152 g_list_foreach (play->missing, (GFunc) gst_message_unref, NULL);
153 play->missing = NULL;
155 play->buffering = FALSE;
156 play->is_live = FALSE;
159 /* returns TRUE if something was installed and we should restart playback */
161 play_install_missing_plugins (GstPlay * play)
163 /* FIXME: implement: try to install any missing plugins we haven't
164 * tried to install before */
169 play_bus_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
171 GstPlay *play = user_data;
173 switch (GST_MESSAGE_TYPE (msg)) {
174 case GST_MESSAGE_ASYNC_DONE:
176 /* dump graph on preroll */
177 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
178 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.async-done");
180 g_print ("Prerolled.\r");
181 if (play->missing != NULL && play_install_missing_plugins (play)) {
182 g_print ("New plugins installed, trying again...\n");
187 case GST_MESSAGE_BUFFERING:{
190 if (!play->buffering)
193 gst_message_parse_buffering (msg, &percent);
194 g_print ("%s %d%% \r", _("Buffering..."), percent);
196 /* no state management needed for live pipelines */
200 if (percent == 100) {
201 /* a 100% message means buffering is done */
202 if (play->buffering) {
203 play->buffering = FALSE;
204 gst_element_set_state (play->playbin, play->desired_state);
208 if (!play->buffering) {
209 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
210 play->buffering = TRUE;
215 case GST_MESSAGE_CLOCK_LOST:{
216 g_print (_("Clock lost, selecting a new one\n"));
217 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
218 gst_element_set_state (play->playbin, GST_STATE_PLAYING);
221 case GST_MESSAGE_LATENCY:
222 g_print ("Redistribute latency...\n");
223 gst_bin_recalculate_latency (GST_BIN (play->playbin));
225 case GST_MESSAGE_REQUEST_STATE:{
229 name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
231 gst_message_parse_request_state (msg, &state);
233 g_print ("Setting state to %s as requested by %s...\n",
234 gst_element_state_get_name (state), name);
236 gst_element_set_state (play->playbin, state);
240 case GST_MESSAGE_EOS:
241 /* print final position at end */
244 /* and switch to next item in list */
245 if (!play_next (play)) {
246 g_print ("Reached end of play list.\n");
247 g_main_loop_quit (play->loop);
250 case GST_MESSAGE_WARNING:{
254 /* dump graph on warning */
255 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
256 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.warning");
258 gst_message_parse_warning (msg, &err, &dbg);
259 g_printerr ("WARNING %s\n", err->message);
261 g_printerr ("WARNING debug information: %s\n", dbg);
266 case GST_MESSAGE_ERROR:{
270 /* dump graph on error */
271 GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
272 GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.error");
274 gst_message_parse_error (msg, &err, &dbg);
275 g_printerr ("ERROR %s for %s\n", err->message, play->uris[play->cur_idx]);
277 g_printerr ("ERROR debug information: %s\n", dbg);
281 /* flush any other error messages from the bus and clean up */
282 gst_element_set_state (play->playbin, GST_STATE_NULL);
284 if (play->missing != NULL && play_install_missing_plugins (play)) {
285 g_print ("New plugins installed, trying again...\n");
290 /* try next item in list then */
291 if (!play_next (play)) {
292 g_print ("Reached end of play list.\n");
293 g_main_loop_quit (play->loop);
298 if (gst_is_missing_plugin_message (msg)) {
301 desc = gst_missing_plugin_message_get_description (msg);
302 g_print ("Missing plugin: %s\n", desc);
304 play->missing = g_list_append (play->missing, gst_message_ref (msg));
313 play_timeout (gpointer user_data)
315 GstPlay *play = user_data;
316 gint64 pos = -1, dur = -1;
317 gchar status[64] = { 0, };
322 gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos);
323 gst_element_query_duration (play->playbin, GST_FORMAT_TIME, &dur);
325 if (play->desired_state == GST_STATE_PAUSED)
326 g_snprintf (status, sizeof (status), "Paused");
328 memset (status, ' ', sizeof (status) - 1);
330 if (pos >= 0 && dur > 0) {
331 gchar dstr[32], pstr[32];
333 /* FIXME: pretty print in nicer format */
334 g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
336 g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
338 g_print ("%s / %s %s\r", pstr, dstr, status);
345 play_uri_get_display_name (GstPlay * play, const gchar * uri)
349 if (gst_uri_has_protocol (uri, "file")) {
350 loc = g_filename_from_uri (uri, NULL, NULL);
351 } else if (gst_uri_has_protocol (uri, "pushfile")) {
352 loc = g_filename_from_uri (uri + 4, NULL, NULL);
354 loc = g_strdup (uri);
357 /* Maybe additionally use glib's filename to display name function */
362 play_uri (GstPlay * play, const gchar * next_uri)
364 GstStateChangeReturn sret;
367 gst_element_set_state (play->playbin, GST_STATE_READY);
370 loc = play_uri_get_display_name (play, next_uri);
371 g_print ("Now playing %s\n", loc);
374 g_object_set (play->playbin, "uri", next_uri, NULL);
376 sret = gst_element_set_state (play->playbin, play->desired_state);
378 case GST_STATE_CHANGE_FAILURE:
379 /* ignore, we should get an error message posted on the bus */
381 case GST_STATE_CHANGE_NO_PREROLL:
382 g_print ("Pipeline is live.\n");
383 play->is_live = TRUE;
385 case GST_STATE_CHANGE_ASYNC:
386 g_print ("Prerolling...\r");
393 /* returns FALSE if we have reached the end of the playlist */
395 play_next (GstPlay * play)
397 if ((play->cur_idx + 1) >= play->num_uris)
400 play_uri (play, play->uris[++play->cur_idx]);
404 /* returns FALSE if we have reached the beginning of the playlist */
406 play_prev (GstPlay * play)
408 if (play->cur_idx == 0 || play->num_uris <= 1)
411 play_uri (play, play->uris[--play->cur_idx]);
416 play_about_to_finish (GstElement * playbin, gpointer user_data)
418 GstPlay *play = user_data;
419 const gchar *next_uri;
426 next_idx = play->cur_idx + 1;
427 if (next_idx >= play->num_uris)
430 next_uri = play->uris[next_idx];
431 loc = play_uri_get_display_name (play, next_uri);
432 g_print ("About to finish, preparing next title: %s\n", loc);
435 g_object_set (play->playbin, "uri", next_uri, NULL);
436 play->cur_idx = next_idx;
440 do_play (GstPlay * play)
445 for (i = 0; i < play->num_uris; ++i)
446 GST_INFO ("%4u : %s", i, play->uris[i]);
448 if (!play_next (play))
451 g_main_loop_run (play->loop);
455 add_to_playlist (GPtrArray * playlist, const gchar * filename)
460 if (gst_uri_is_valid (filename)) {
461 g_ptr_array_add (playlist, g_strdup (filename));
465 if ((dir = g_dir_open (filename, 0, NULL))) {
468 /* FIXME: sort entries for each directory? */
469 while ((entry = g_dir_read_name (dir))) {
472 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
473 add_to_playlist (playlist, path);
481 uri = gst_filename_to_uri (filename, NULL);
483 g_ptr_array_add (playlist, uri);
485 g_warning ("Could not make URI out of filename '%s'", filename);
489 shuffle_uris (gchar ** uris, guint num)
497 for (i = 0; i < num; i++) {
498 /* gets equally distributed random number in 0..num-1 [0;num[ */
499 j = g_random_int_range (0, num);
507 restore_terminal (void)
509 gst_play_kb_set_key_handler (NULL, NULL);
513 toggle_paused (GstPlay * play)
515 if (play->desired_state == GST_STATE_PLAYING)
516 play->desired_state = GST_STATE_PAUSED;
518 play->desired_state = GST_STATE_PLAYING;
520 if (!play->buffering) {
521 gst_element_set_state (play->playbin, play->desired_state);
522 } else if (play->desired_state == GST_STATE_PLAYING) {
523 g_print ("\nWill play as soon as buffering finishes)\n");
528 relative_seek (GstPlay * play, gdouble percent)
531 gboolean seekable = FALSE;
532 gint64 dur = -1, pos = -1;
534 g_return_if_fail (percent >= -1.0 && percent <= 1.0);
536 if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
539 query = gst_query_new_seeking (GST_FORMAT_TIME);
540 if (!gst_element_query (play->playbin, query)) {
541 gst_query_unref (query);
545 gst_query_parse_seeking (query, NULL, &seekable, NULL, &dur);
546 gst_query_unref (query);
548 if (!seekable || dur <= 0)
551 pos = pos + dur * percent;
553 if (!play_next (play)) {
554 g_print ("\nReached end of play list.\n");
555 g_main_loop_quit (play->loop);
560 if (!gst_element_seek_simple (play->playbin, GST_FORMAT_TIME,
561 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, pos))
569 g_print ("\nCould not seek.\n");
574 keyboard_cb (const gchar * key_input, gpointer user_data)
576 GstPlay *play = (GstPlay *) user_data;
578 switch (g_ascii_tolower (key_input[0])) {
580 toggle_paused (play);
584 g_main_loop_quit (play->loop);
587 if (!play_next (play)) {
588 g_print ("\nReached end of play list.\n");
589 g_main_loop_quit (play->loop);
596 if (key_input[1] == '\0') {
597 g_main_loop_quit (play->loop);
602 if (strcmp (key_input, GST_PLAY_KB_ARROW_RIGHT) == 0) {
603 relative_seek (play, +0.08);
604 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_LEFT) == 0) {
605 relative_seek (play, -0.01);
607 GST_INFO ("keyboard input:");
608 for (; *key_input != '\0'; ++key_input)
609 GST_INFO (" code %3d", *key_input);
616 main (int argc, char **argv)
620 gboolean print_version = FALSE;
621 gboolean interactive = FALSE; /* FIXME: maybe enable by default? */
622 gboolean gapless = FALSE;
623 gboolean shuffle = FALSE;
624 gchar **filenames = NULL;
625 gchar *audio_sink = NULL;
626 gchar *video_sink = NULL;
631 GOptionEntry options[] = {
632 {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
633 N_("Print version information and exit"), NULL},
634 {"videosink", 0, 0, G_OPTION_ARG_STRING, &video_sink,
635 N_("Video sink to use (default is autovideosink)"), NULL},
636 {"audiosink", 0, 0, G_OPTION_ARG_STRING, &audio_sink,
637 N_("Audio sink to use (default is autoaudiosink)"), NULL},
638 {"gapless", 0, 0, G_OPTION_ARG_NONE, &gapless,
639 N_("Enable gapless playback"), NULL},
640 {"shuffle", 0, 0, G_OPTION_ARG_NONE, &shuffle,
641 N_("Shuffle playlist"), NULL},
642 {"interactive", 0, 0, G_OPTION_ARG_NONE, &interactive,
643 N_("interactive"), NULL},
644 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
648 setlocale (LC_ALL, "");
651 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
652 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
653 textdomain (GETTEXT_PACKAGE);
656 g_set_prgname ("gst-play-" GST_API_VERSION);
658 ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ...");
659 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
660 g_option_context_add_group (ctx, gst_init_get_option_group ());
661 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
662 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
665 g_option_context_free (ctx);
667 GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play");
672 version_str = gst_version_string ();
673 g_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
674 g_print ("%s\n", version_str);
675 g_print ("%s\n", GST_PACKAGE_ORIGIN);
676 g_free (version_str);
680 if (filenames == NULL || *filenames == NULL) {
681 g_printerr (_("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."),
682 "gst-play-" GST_API_VERSION);
684 g_printerr ("%s\n\n",
685 _("You must provide at least one filename or URI to play."));
689 playlist = g_ptr_array_new ();
692 num = g_strv_length (filenames);
693 for (i = 0; i < num; ++i) {
694 GST_LOG ("command line argument: %s", filenames[i]);
695 add_to_playlist (playlist, filenames[i]);
697 g_strfreev (filenames);
700 g_ptr_array_add (playlist, NULL);
702 uris = (gchar **) g_ptr_array_free (playlist, FALSE);
705 shuffle_uris (uris, num);
708 play = play_new (uris, audio_sink, video_sink, gapless);
711 if (gst_play_kb_set_key_handler (keyboard_cb, play)) {
712 atexit (restore_terminal);
714 g_print ("Interactive keyboard handling in terminal not available.\n");