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>
31 GST_DEBUG_CATEGORY (play_debug);
32 #define GST_CAT_DEFAULT play_debug
46 /* missing plugin messages */
56 static gboolean play_bus_msg (GstBus * bus, GstMessage * msg, gpointer data);
57 static gboolean play_next (GstPlay * play);
58 static gboolean play_timeout (gpointer user_data);
59 static void play_about_to_finish (GstElement * playbin, gpointer user_data);
60 static void play_reset (GstPlay * play);
63 play_new (gchar ** uris, const gchar * audio_sink, const gchar * video_sink,
69 play = g_new0 (GstPlay, 1);
72 play->num_uris = g_strv_length (uris);
75 play->playbin = gst_element_factory_make ("playbin", "playbin");
77 if (audio_sink != NULL) {
78 sink = gst_element_factory_make (audio_sink, NULL);
80 g_object_set (play->playbin, "audio-sink", sink, NULL);
82 g_warning ("Couldn't create specified audio sink '%s'", audio_sink);
84 if (video_sink != NULL) {
85 sink = gst_element_factory_make (video_sink, NULL);
87 g_object_set (play->playbin, "video-sink", sink, NULL);
89 g_warning ("Couldn't create specified video sink '%s'", video_sink);
92 play->loop = g_main_loop_new (NULL, FALSE);
94 play->bus_watch = gst_bus_add_watch (GST_ELEMENT_BUS (play->playbin),
97 /* FIXME: make configurable incl. 0 for disable */
98 play->timeout = g_timeout_add (100, play_timeout, play);
100 play->missing = NULL;
101 play->buffering = FALSE;
102 play->is_live = FALSE;
104 play->gapless = gapless;
106 g_signal_connect (play->playbin, "about-to-finish",
107 G_CALLBACK (play_about_to_finish), play);
114 play_free (GstPlay * play)
118 gst_element_set_state (play->playbin, GST_STATE_NULL);
119 gst_object_unref (play->playbin);
121 g_source_remove (play->bus_watch);
122 g_source_remove (play->timeout);
123 g_main_loop_unref (play->loop);
125 g_strfreev (play->uris);
129 /* reset for new file/stream */
131 play_reset (GstPlay * play)
133 g_list_foreach (play->missing, (GFunc) gst_message_unref, NULL);
134 play->missing = NULL;
136 play->buffering = FALSE;
137 play->is_live = FALSE;
140 /* returns TRUE if something was installed and we should restart playback */
142 play_install_missing_plugins (GstPlay * play)
144 /* FIXME: implement: try to install any missing plugins we haven't
145 * tried to install before */
150 play_bus_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
152 GstPlay *play = user_data;
154 switch (GST_MESSAGE_TYPE (msg)) {
155 case GST_MESSAGE_ASYNC_DONE:
156 g_print ("Prerolled.\r");
157 if (play->missing != NULL && play_install_missing_plugins (play)) {
158 g_print ("New plugins installed, trying again...\n");
163 case GST_MESSAGE_BUFFERING:{
166 if (!play->buffering)
169 gst_message_parse_buffering (msg, &percent);
170 g_print ("%s %d%% \r", _("Buffering..."), percent);
172 /* no state management needed for live pipelines */
176 if (percent == 100) {
177 /* a 100% message means buffering is done */
178 if (play->buffering) {
179 play->buffering = FALSE;
180 gst_element_set_state (play->playbin, GST_STATE_PLAYING);
184 if (!play->buffering) {
185 gst_element_set_state (play->playbin, GST_STATE_PAUSED);
186 play->buffering = TRUE;
191 case GST_MESSAGE_LATENCY:
192 g_print ("Redistribute latency...\n");
193 gst_bin_recalculate_latency (GST_BIN (play->playbin));
195 case GST_MESSAGE_REQUEST_STATE:{
199 name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
201 gst_message_parse_request_state (msg, &state);
203 g_print ("Setting state to %s as requested by %s...\n",
204 gst_element_state_get_name (state), name);
206 gst_element_set_state (play->playbin, state);
210 case GST_MESSAGE_EOS:
211 /* print final position at end */
214 /* and switch to next item in list */
215 if (!play_next (play)) {
216 g_print ("Reached end of play list.\n");
217 g_main_loop_quit (play->loop);
220 case GST_MESSAGE_WARNING:{
224 gst_message_parse_warning (msg, &err, &dbg);
225 g_printerr ("WARNING %s\n", err->message);
227 g_printerr ("WARNING debug information: %s\n", dbg);
232 case GST_MESSAGE_ERROR:{
236 gst_message_parse_error (msg, &err, &dbg);
237 g_printerr ("ERROR %s for %s\n", err->message, play->uris[play->cur_idx]);
239 g_printerr ("ERROR debug information: %s\n", dbg);
243 if (play->missing != NULL && play_install_missing_plugins (play)) {
244 g_print ("New plugins installed, trying again...\n");
249 /* try next item in list then */
250 if (!play_next (play)) {
251 g_print ("Reached end of play list.\n");
252 g_main_loop_quit (play->loop);
257 if (gst_is_missing_plugin_message (msg)) {
260 desc = gst_missing_plugin_message_get_description (msg);
261 g_print ("Missing plugin: %s\n", desc);
263 play->missing = g_list_append (play->missing, gst_message_ref (msg));
272 play_timeout (gpointer user_data)
274 GstPlay *play = user_data;
275 gint64 pos = -1, dur = -1;
280 gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos);
281 gst_element_query_duration (play->playbin, GST_FORMAT_TIME, &dur);
283 if (pos >= 0 && dur > 0) {
284 gchar dstr[32], pstr[32];
286 /* FIXME: pretty print in nicer format */
287 g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
289 g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
291 g_print ("%s / %s\r", pstr, dstr);
298 play_uri_get_display_name (GstPlay * play, const gchar * uri)
302 if (gst_uri_has_protocol (uri, "file")) {
303 loc = g_filename_from_uri (uri, NULL, NULL);
304 } else if (gst_uri_has_protocol (uri, "pushfile")) {
305 loc = g_filename_from_uri (uri + 4, NULL, NULL);
307 loc = g_strdup (uri);
310 /* Maybe additionally use glib's filename to display name function */
314 /* returns FALSE if we have reached the end of the playlist */
316 play_next (GstPlay * play)
318 GstStateChangeReturn sret;
319 const gchar *next_uri;
322 if (++play->cur_idx >= play->num_uris)
325 gst_element_set_state (play->playbin, GST_STATE_READY);
328 next_uri = play->uris[play->cur_idx];
329 loc = play_uri_get_display_name (play, next_uri);
330 g_print ("Now playing %s\n", loc);
333 g_object_set (play->playbin, "uri", next_uri, NULL);
335 sret = gst_element_set_state (play->playbin, GST_STATE_PLAYING);
337 case GST_STATE_CHANGE_FAILURE:
338 /* ignore, we should get an error message posted on the bus */
340 case GST_STATE_CHANGE_NO_PREROLL:
341 g_print ("Pipeline is live.\n");
342 play->is_live = TRUE;
344 case GST_STATE_CHANGE_ASYNC:
345 g_print ("Prerolling...\r");
355 play_about_to_finish (GstElement * playbin, gpointer user_data)
357 GstPlay *play = user_data;
358 const gchar *next_uri;
365 next_idx = play->cur_idx + 1;
366 if (next_idx >= play->num_uris)
369 next_uri = play->uris[next_idx];
370 loc = play_uri_get_display_name (play, next_uri);
371 g_print ("About to finish, preparing next title: %s\n", loc);
374 g_object_set (play->playbin, "uri", next_uri, NULL);
375 play->cur_idx = next_idx;
379 do_play (GstPlay * play)
384 for (i = 0; i < play->num_uris; ++i)
385 GST_INFO ("%4u : %s", i, play->uris[i]);
387 if (!play_next (play))
390 g_main_loop_run (play->loop);
394 add_to_playlist (GPtrArray * playlist, const gchar * filename)
399 if (gst_uri_is_valid (filename)) {
400 g_ptr_array_add (playlist, g_strdup (filename));
404 if ((dir = g_dir_open (filename, 0, NULL))) {
407 /* FIXME: sort entries for each directory? */
408 while ((entry = g_dir_read_name (dir))) {
411 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
412 add_to_playlist (playlist, path);
420 uri = gst_filename_to_uri (filename, NULL);
422 g_ptr_array_add (playlist, uri);
424 g_warning ("Could not make URI out of filename '%s'", filename);
428 main (int argc, char **argv)
432 gboolean print_version = FALSE;
433 gboolean gapless = FALSE;
434 gchar **filenames = NULL;
435 gchar *audio_sink = NULL;
436 gchar *video_sink = NULL;
441 GOptionEntry options[] = {
442 {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
443 N_("Print version information and exit"), NULL},
444 {"videosink", 0, 0, G_OPTION_ARG_STRING, &video_sink,
445 N_("Video sink to use (default is autovideosink)"), NULL},
446 {"audiosink", 0, 0, G_OPTION_ARG_STRING, &audio_sink,
447 N_("Audio sink to use (default is autoaudiosink)"), NULL},
448 {"gapless", 0, 0, G_OPTION_ARG_NONE, &gapless,
449 N_("Enable gapless playback"), NULL},
450 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
455 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
456 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
457 textdomain (GETTEXT_PACKAGE);
460 g_set_prgname ("gst-play-" GST_API_VERSION);
462 ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ...");
463 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
464 g_option_context_add_group (ctx, gst_init_get_option_group ());
465 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
466 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
469 g_option_context_free (ctx);
471 GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play");
476 version_str = gst_version_string ();
477 g_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
478 g_print ("%s\n", version_str);
479 g_print ("%s\n", GST_PACKAGE_ORIGIN);
480 g_free (version_str);
484 if (filenames == NULL || *filenames == NULL) {
485 g_printerr (_("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."),
486 "gst-play-" GST_API_VERSION);
488 g_printerr ("%s\n\n",
489 _("You must provide at least one filename or URI to play."));
493 playlist = g_ptr_array_new ();
496 num = g_strv_length (filenames);
497 for (i = 0; i < num; ++i) {
498 GST_LOG ("command line argument: %s", filenames[i]);
499 add_to_playlist (playlist, filenames[i]);
501 g_strfreev (filenames);
503 g_ptr_array_add (playlist, NULL);
506 uris = (gchar **) g_ptr_array_free (playlist, FALSE);
507 play = play_new (uris, audio_sink, video_sink, gapless);