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.
27 #include <gst/gst-i18n-app.h>
28 #include <gst/pbutils/pbutils.h>
33 #include "gst-play-kb.h"
35 GST_DEBUG_CATEGORY (play_debug);
36 #define GST_CAT_DEFAULT play_debug
50 /* missing plugin messages */
56 GstState desired_state; /* as per user interaction, PAUSED or PLAYING */
62 static gboolean play_bus_msg (GstBus * bus, GstMessage * msg, gpointer data);
63 static gboolean play_next (GstPlay * play);
64 static gboolean play_prev (GstPlay * play);
65 static gboolean play_timeout (gpointer user_data);
66 static void play_about_to_finish (GstElement * playbin, gpointer user_data);
67 static void play_reset (GstPlay * play);
70 play_new (gchar ** uris, const gchar * audio_sink, const gchar * video_sink,
76 play = g_new0 (GstPlay, 1);
79 play->num_uris = g_strv_length (uris);
82 play->playbin = gst_element_factory_make ("playbin", "playbin");
84 if (audio_sink != NULL) {
85 sink = gst_element_factory_make (audio_sink, NULL);
87 g_object_set (play->playbin, "audio-sink", sink, NULL);
89 g_warning ("Couldn't create specified audio sink '%s'", audio_sink);
91 if (video_sink != NULL) {
92 sink = gst_element_factory_make (video_sink, NULL);
94 g_object_set (play->playbin, "video-sink", sink, NULL);
96 g_warning ("Couldn't create specified video sink '%s'", video_sink);
99 play->loop = g_main_loop_new (NULL, FALSE);
101 play->bus_watch = gst_bus_add_watch (GST_ELEMENT_BUS (play->playbin),
104 /* FIXME: make configurable incl. 0 for disable */
105 play->timeout = g_timeout_add (100, play_timeout, play);
107 play->missing = NULL;
108 play->buffering = FALSE;
109 play->is_live = FALSE;
111 play->desired_state = GST_STATE_PLAYING;
113 play->gapless = gapless;
115 g_signal_connect (play->playbin, "about-to-finish",
116 G_CALLBACK (play_about_to_finish), play);
123 play_free (GstPlay * play)
127 gst_element_set_state (play->playbin, GST_STATE_NULL);
128 gst_object_unref (play->playbin);
130 g_source_remove (play->bus_watch);
131 g_source_remove (play->timeout);
132 g_main_loop_unref (play->loop);
134 g_strfreev (play->uris);
138 /* reset for new file/stream */
140 play_reset (GstPlay * play)
142 g_list_foreach (play->missing, (GFunc) gst_message_unref, NULL);
143 play->missing = NULL;
145 play->buffering = FALSE;
146 play->is_live = FALSE;
149 /* returns TRUE if something was installed and we should restart playback */
151 play_install_missing_plugins (GstPlay * play)
153 /* FIXME: implement: try to install any missing plugins we haven't
154 * tried to install before */
159 play_bus_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
161 GstPlay *play = user_data;
163 switch (GST_MESSAGE_TYPE (msg)) {
164 case GST_MESSAGE_ASYNC_DONE:
165 g_print ("Prerolled.\r");
166 if (play->missing != NULL && play_install_missing_plugins (play)) {
167 g_print ("New plugins installed, trying again...\n");
172 case GST_MESSAGE_BUFFERING:{
175 if (!play->buffering)
178 gst_message_parse_buffering (msg, &percent);
179 g_print ("%s %d%% \r", _("Buffering..."), percent);
181 /* no state management needed for live pipelines */
185 if (percent == 100) {
186 /* a 100% message means buffering is done */
187 if (play->buffering) {
188 play->buffering = FALSE;
189 gst_element_set_state (play->playbin, play->desired_state);
193 if (!play->buffering) {
194 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
195 play->buffering = TRUE;
200 case GST_MESSAGE_LATENCY:
201 g_print ("Redistribute latency...\n");
202 gst_bin_recalculate_latency (GST_BIN (play->playbin));
204 case GST_MESSAGE_REQUEST_STATE:{
208 name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
210 gst_message_parse_request_state (msg, &state);
212 g_print ("Setting state to %s as requested by %s...\n",
213 gst_element_state_get_name (state), name);
215 gst_element_set_state (play->playbin, state);
219 case GST_MESSAGE_EOS:
220 /* print final position at end */
223 /* and switch to next item in list */
224 if (!play_next (play)) {
225 g_print ("Reached end of play list.\n");
226 g_main_loop_quit (play->loop);
229 case GST_MESSAGE_WARNING:{
233 gst_message_parse_warning (msg, &err, &dbg);
234 g_printerr ("WARNING %s\n", err->message);
236 g_printerr ("WARNING debug information: %s\n", dbg);
241 case GST_MESSAGE_ERROR:{
245 gst_message_parse_error (msg, &err, &dbg);
246 g_printerr ("ERROR %s for %s\n", err->message, play->uris[play->cur_idx]);
248 g_printerr ("ERROR debug information: %s\n", dbg);
252 /* flush any other error messages from the bus and clean up */
253 gst_element_set_state (play->playbin, GST_STATE_NULL);
255 if (play->missing != NULL && play_install_missing_plugins (play)) {
256 g_print ("New plugins installed, trying again...\n");
261 /* try next item in list then */
262 if (!play_next (play)) {
263 g_print ("Reached end of play list.\n");
264 g_main_loop_quit (play->loop);
269 if (gst_is_missing_plugin_message (msg)) {
272 desc = gst_missing_plugin_message_get_description (msg);
273 g_print ("Missing plugin: %s\n", desc);
275 play->missing = g_list_append (play->missing, gst_message_ref (msg));
284 play_timeout (gpointer user_data)
286 GstPlay *play = user_data;
287 gint64 pos = -1, dur = -1;
288 gchar status[64] = { 0, };
293 gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos);
294 gst_element_query_duration (play->playbin, GST_FORMAT_TIME, &dur);
296 if (play->desired_state == GST_STATE_PAUSED)
297 g_snprintf (status, sizeof (status), "Paused");
299 memset (status, ' ', sizeof (status) - 1);
301 if (pos >= 0 && dur > 0) {
302 gchar dstr[32], pstr[32];
304 /* FIXME: pretty print in nicer format */
305 g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
307 g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
309 g_print ("%s / %s %s\r", pstr, dstr, status);
316 play_uri_get_display_name (GstPlay * play, const gchar * uri)
320 if (gst_uri_has_protocol (uri, "file")) {
321 loc = g_filename_from_uri (uri, NULL, NULL);
322 } else if (gst_uri_has_protocol (uri, "pushfile")) {
323 loc = g_filename_from_uri (uri + 4, NULL, NULL);
325 loc = g_strdup (uri);
328 /* Maybe additionally use glib's filename to display name function */
333 play_uri (GstPlay * play, const gchar * next_uri)
335 GstStateChangeReturn sret;
338 gst_element_set_state (play->playbin, GST_STATE_READY);
341 loc = play_uri_get_display_name (play, next_uri);
342 g_print ("Now playing %s\n", loc);
345 g_object_set (play->playbin, "uri", next_uri, NULL);
347 sret = gst_element_set_state (play->playbin, play->desired_state);
349 case GST_STATE_CHANGE_FAILURE:
350 /* ignore, we should get an error message posted on the bus */
352 case GST_STATE_CHANGE_NO_PREROLL:
353 g_print ("Pipeline is live.\n");
354 play->is_live = TRUE;
356 case GST_STATE_CHANGE_ASYNC:
357 g_print ("Prerolling...\r");
364 /* returns FALSE if we have reached the end of the playlist */
366 play_next (GstPlay * play)
368 if ((play->cur_idx + 1) >= play->num_uris)
371 play_uri (play, play->uris[++play->cur_idx]);
375 /* returns FALSE if we have reached the beginning of the playlist */
377 play_prev (GstPlay * play)
379 if (play->cur_idx == 0 || play->num_uris <= 1)
382 play_uri (play, play->uris[--play->cur_idx]);
387 play_about_to_finish (GstElement * playbin, gpointer user_data)
389 GstPlay *play = user_data;
390 const gchar *next_uri;
397 next_idx = play->cur_idx + 1;
398 if (next_idx >= play->num_uris)
401 next_uri = play->uris[next_idx];
402 loc = play_uri_get_display_name (play, next_uri);
403 g_print ("About to finish, preparing next title: %s\n", loc);
406 g_object_set (play->playbin, "uri", next_uri, NULL);
407 play->cur_idx = next_idx;
411 do_play (GstPlay * play)
416 for (i = 0; i < play->num_uris; ++i)
417 GST_INFO ("%4u : %s", i, play->uris[i]);
419 if (!play_next (play))
422 g_main_loop_run (play->loop);
426 add_to_playlist (GPtrArray * playlist, const gchar * filename)
431 if (gst_uri_is_valid (filename)) {
432 g_ptr_array_add (playlist, g_strdup (filename));
436 if ((dir = g_dir_open (filename, 0, NULL))) {
439 /* FIXME: sort entries for each directory? */
440 while ((entry = g_dir_read_name (dir))) {
443 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
444 add_to_playlist (playlist, path);
452 uri = gst_filename_to_uri (filename, NULL);
454 g_ptr_array_add (playlist, uri);
456 g_warning ("Could not make URI out of filename '%s'", filename);
460 shuffle_uris (gchar ** uris, guint num)
468 for (i = 0; i < num; i++) {
469 /* gets equally distributed random number in 0..num-1 [0;num[ */
470 j = g_random_int_range (0, num);
478 restore_terminal (void)
480 gst_play_kb_set_key_handler (NULL, NULL);
484 toggle_paused (GstPlay * play)
486 if (play->desired_state == GST_STATE_PLAYING)
487 play->desired_state = GST_STATE_PAUSED;
489 play->desired_state = GST_STATE_PLAYING;
491 if (!play->buffering) {
492 gst_element_set_state (play->playbin, play->desired_state);
493 } else if (play->desired_state == GST_STATE_PLAYING) {
494 g_print ("\nWill play as soon as buffering finishes)\n");
499 relative_seek (GstPlay * play, gdouble percent)
502 gboolean seekable = FALSE;
503 gint64 dur = -1, pos = -1;
505 g_return_if_fail (percent >= -1.0 && percent <= 1.0);
507 if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
510 query = gst_query_new_seeking (GST_FORMAT_TIME);
511 if (!gst_element_query (play->playbin, query)) {
512 gst_query_unref (query);
516 gst_query_parse_seeking (query, NULL, &seekable, NULL, &dur);
517 gst_query_unref (query);
519 if (!seekable || dur <= 0)
522 pos = pos + dur * percent;
524 if (!play_next (play)) {
525 g_print ("\nReached end of play list.\n");
526 g_main_loop_quit (play->loop);
531 if (!gst_element_seek_simple (play->playbin, GST_FORMAT_TIME,
532 GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, pos))
540 g_print ("\nCould not seek.\n");
545 keyboard_cb (const gchar * key_input, gpointer user_data)
547 GstPlay *play = (GstPlay *) user_data;
549 switch (g_ascii_tolower (key_input[0])) {
551 toggle_paused (play);
554 if (!play_next (play)) {
555 g_print ("\nReached end of play list.\n");
556 g_main_loop_quit (play->loop);
564 if (strcmp (key_input, GST_PLAY_KB_ARROW_RIGHT) == 0) {
565 relative_seek (play, +0.08);
566 } else if (strcmp (key_input, GST_PLAY_KB_ARROW_LEFT) == 0) {
567 relative_seek (play, -0.01);
569 GST_INFO ("keyboard input:");
570 for (; *key_input != '\0'; ++key_input)
571 GST_INFO (" code %3d", *key_input);
578 main (int argc, char **argv)
582 gboolean print_version = FALSE;
583 gboolean interactive = FALSE; /* FIXME: maybe enable by default? */
584 gboolean gapless = FALSE;
585 gboolean shuffle = FALSE;
586 gchar **filenames = NULL;
587 gchar *audio_sink = NULL;
588 gchar *video_sink = NULL;
593 GOptionEntry options[] = {
594 {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
595 N_("Print version information and exit"), NULL},
596 {"videosink", 0, 0, G_OPTION_ARG_STRING, &video_sink,
597 N_("Video sink to use (default is autovideosink)"), NULL},
598 {"audiosink", 0, 0, G_OPTION_ARG_STRING, &audio_sink,
599 N_("Audio sink to use (default is autoaudiosink)"), NULL},
600 {"gapless", 0, 0, G_OPTION_ARG_NONE, &gapless,
601 N_("Enable gapless playback"), NULL},
602 {"shuffle", 0, 0, G_OPTION_ARG_NONE, &shuffle,
603 N_("Shuffle playlist"), NULL},
604 {"interactive", 0, 0, G_OPTION_ARG_NONE, &interactive,
605 N_("interactive"), NULL},
606 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
611 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
612 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
613 textdomain (GETTEXT_PACKAGE);
616 g_set_prgname ("gst-play-" GST_API_VERSION);
618 ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ...");
619 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
620 g_option_context_add_group (ctx, gst_init_get_option_group ());
621 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
622 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
625 g_option_context_free (ctx);
627 GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play");
632 version_str = gst_version_string ();
633 g_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
634 g_print ("%s\n", version_str);
635 g_print ("%s\n", GST_PACKAGE_ORIGIN);
636 g_free (version_str);
640 if (filenames == NULL || *filenames == NULL) {
641 g_printerr (_("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."),
642 "gst-play-" GST_API_VERSION);
644 g_printerr ("%s\n\n",
645 _("You must provide at least one filename or URI to play."));
649 playlist = g_ptr_array_new ();
652 num = g_strv_length (filenames);
653 for (i = 0; i < num; ++i) {
654 GST_LOG ("command line argument: %s", filenames[i]);
655 add_to_playlist (playlist, filenames[i]);
657 g_strfreev (filenames);
660 g_ptr_array_add (playlist, NULL);
662 uris = (gchar **) g_ptr_array_free (playlist, FALSE);
665 shuffle_uris (uris, num);
668 play = play_new (uris, audio_sink, video_sink, gapless);
671 if (gst_play_kb_set_key_handler (keyboard_cb, play)) {
672 atexit (restore_terminal);
674 g_print ("Interactive keyboard handling in terminal not available.\n");