gst-play: add --gapless mode
[platform/upstream/gstreamer.git] / tools / gst-play.c
1 /* GStreamer command line playback testing utility
2  *
3  * Copyright (C) 2013 Tim-Philipp Müller <tim centricular net>
4  * Copyright (C) 2013 Collabora Ltd.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <gst/gst.h>
27 #include <gst/gst-i18n-app.h>
28 #include <gst/pbutils/pbutils.h>
29 #include <stdlib.h>
30
31 GST_DEBUG_CATEGORY (play_debug);
32 #define GST_CAT_DEFAULT play_debug
33
34 typedef struct
35 {
36   gchar **uris;
37   guint num_uris;
38   gint cur_idx;
39
40   GstElement *playbin;
41
42   GMainLoop *loop;
43   guint bus_watch;
44   guint timeout;
45
46   /* missing plugin messages */
47   GList *missing;
48
49   gboolean buffering;
50   gboolean is_live;
51
52   /* configuration */
53   gboolean gapless;
54 } GstPlay;
55
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);
61
62 static GstPlay *
63 play_new (gchar ** uris, const gchar * audio_sink, const gchar * video_sink,
64     gboolean gapless)
65 {
66   GstElement *sink;
67   GstPlay *play;
68
69   play = g_new0 (GstPlay, 1);
70
71   play->uris = uris;
72   play->num_uris = g_strv_length (uris);
73   play->cur_idx = -1;
74
75   play->playbin = gst_element_factory_make ("playbin", "playbin");
76
77   if (audio_sink != NULL) {
78     sink = gst_element_factory_make (audio_sink, NULL);
79     if (sink != NULL)
80       g_object_set (play->playbin, "audio-sink", sink, NULL);
81     else
82       g_warning ("Couldn't create specified audio sink '%s'", audio_sink);
83   }
84   if (video_sink != NULL) {
85     sink = gst_element_factory_make (video_sink, NULL);
86     if (sink != NULL)
87       g_object_set (play->playbin, "video-sink", sink, NULL);
88     else
89       g_warning ("Couldn't create specified video sink '%s'", video_sink);
90   }
91
92   play->loop = g_main_loop_new (NULL, FALSE);
93
94   play->bus_watch = gst_bus_add_watch (GST_ELEMENT_BUS (play->playbin),
95       play_bus_msg, play);
96
97   /* FIXME: make configurable incl. 0 for disable */
98   play->timeout = g_timeout_add (100, play_timeout, play);
99
100   play->missing = NULL;
101   play->buffering = FALSE;
102   play->is_live = FALSE;
103
104   play->gapless = gapless;
105   if (gapless) {
106     g_signal_connect (play->playbin, "about-to-finish",
107         G_CALLBACK (play_about_to_finish), play);
108   }
109
110   return play;
111 }
112
113 static void
114 play_free (GstPlay * play)
115 {
116   play_reset (play);
117
118   gst_element_set_state (play->playbin, GST_STATE_NULL);
119   gst_object_unref (play->playbin);
120
121   g_source_remove (play->bus_watch);
122   g_source_remove (play->timeout);
123   g_main_loop_unref (play->loop);
124
125   g_strfreev (play->uris);
126   g_free (play);
127 }
128
129 /* reset for new file/stream */
130 static void
131 play_reset (GstPlay * play)
132 {
133   g_list_foreach (play->missing, (GFunc) gst_message_unref, NULL);
134   play->missing = NULL;
135
136   play->buffering = FALSE;
137   play->is_live = FALSE;
138 }
139
140 /* returns TRUE if something was installed and we should restart playback */
141 static gboolean
142 play_install_missing_plugins (GstPlay * play)
143 {
144   /* FIXME: implement: try to install any missing plugins we haven't
145    * tried to install before */
146   return FALSE;
147 }
148
149 static gboolean
150 play_bus_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
151 {
152   GstPlay *play = user_data;
153
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");
159         --play->cur_idx;
160         play_next (play);
161       }
162       break;
163     case GST_MESSAGE_BUFFERING:{
164       gint percent;
165
166       if (!play->buffering)
167         g_print ("\n");
168
169       gst_message_parse_buffering (msg, &percent);
170       g_print ("%s %d%%  \r", _("Buffering..."), percent);
171
172       /* no state management needed for live pipelines */
173       if (play->is_live)
174         break;
175
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);
181         }
182       } else {
183         /* buffering... */
184         if (!play->buffering) {
185           gst_element_set_state (play->playbin, GST_STATE_PAUSED);
186           play->buffering = TRUE;
187         }
188       }
189       break;
190     }
191     case GST_MESSAGE_LATENCY:
192       g_print ("Redistribute latency...\n");
193       gst_bin_recalculate_latency (GST_BIN (play->playbin));
194       break;
195     case GST_MESSAGE_REQUEST_STATE:{
196       GstState state;
197       gchar *name;
198
199       name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
200
201       gst_message_parse_request_state (msg, &state);
202
203       g_print ("Setting state to %s as requested by %s...\n",
204           gst_element_state_get_name (state), name);
205
206       gst_element_set_state (play->playbin, state);
207       g_free (name);
208       break;
209     }
210     case GST_MESSAGE_EOS:
211       /* print final position at end */
212       play_timeout (play);
213       g_print ("\n");
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);
218       }
219       break;
220     case GST_MESSAGE_WARNING:{
221       GError *err;
222       gchar *dbg = NULL;
223
224       gst_message_parse_warning (msg, &err, &dbg);
225       g_printerr ("WARNING %s\n", err->message);
226       if (dbg != NULL)
227         g_printerr ("WARNING debug information: %s\n", dbg);
228       g_error_free (err);
229       g_free (dbg);
230       break;
231     }
232     case GST_MESSAGE_ERROR:{
233       GError *err;
234       gchar *dbg;
235
236       gst_message_parse_error (msg, &err, &dbg);
237       g_printerr ("ERROR %s for %s\n", err->message, play->uris[play->cur_idx]);
238       if (dbg != NULL)
239         g_printerr ("ERROR debug information: %s\n", dbg);
240       g_error_free (err);
241       g_free (dbg);
242
243       if (play->missing != NULL && play_install_missing_plugins (play)) {
244         g_print ("New plugins installed, trying again...\n");
245         --play->cur_idx;
246         play_next (play);
247         break;
248       }
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);
253       }
254       break;
255     }
256     default:
257       if (gst_is_missing_plugin_message (msg)) {
258         gchar *desc;
259
260         desc = gst_missing_plugin_message_get_description (msg);
261         g_print ("Missing plugin: %s\n", desc);
262         g_free (desc);
263         play->missing = g_list_append (play->missing, gst_message_ref (msg));
264       }
265       break;
266   }
267
268   return TRUE;
269 }
270
271 static gboolean
272 play_timeout (gpointer user_data)
273 {
274   GstPlay *play = user_data;
275   gint64 pos = -1, dur = -1;
276
277   if (play->buffering)
278     return TRUE;
279
280   gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos);
281   gst_element_query_duration (play->playbin, GST_FORMAT_TIME, &dur);
282
283   if (pos >= 0 && dur > 0) {
284     gchar dstr[32], pstr[32];
285
286     /* FIXME: pretty print in nicer format */
287     g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
288     pstr[9] = '\0';
289     g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
290     dstr[9] = '\0';
291     g_print ("%s / %s\r", pstr, dstr);
292   }
293
294   return TRUE;
295 }
296
297 static gchar *
298 play_uri_get_display_name (GstPlay * play, const gchar * uri)
299 {
300   gchar *loc;
301
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);
306   } else {
307     loc = g_strdup (uri);
308   }
309
310   /* Maybe additionally use glib's filename to display name function */
311   return loc;
312 }
313
314 /* returns FALSE if we have reached the end of the playlist */
315 static gboolean
316 play_next (GstPlay * play)
317 {
318   GstStateChangeReturn sret;
319   const gchar *next_uri;
320   gchar *loc;
321
322   if (++play->cur_idx >= play->num_uris)
323     return FALSE;
324
325   gst_element_set_state (play->playbin, GST_STATE_READY);
326   play_reset (play);
327
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);
331   g_free (loc);
332
333   g_object_set (play->playbin, "uri", next_uri, NULL);
334
335   sret = gst_element_set_state (play->playbin, GST_STATE_PLAYING);
336   switch (sret) {
337     case GST_STATE_CHANGE_FAILURE:
338       /* ignore, we should get an error message posted on the bus */
339       break;
340     case GST_STATE_CHANGE_NO_PREROLL:
341       g_print ("Pipeline is live.\n");
342       play->is_live = TRUE;
343       break;
344     case GST_STATE_CHANGE_ASYNC:
345       g_print ("Prerolling...\r");
346       break;
347     default:
348       break;
349   }
350
351   return TRUE;
352 }
353
354 static void
355 play_about_to_finish (GstElement * playbin, gpointer user_data)
356 {
357   GstPlay *play = user_data;
358   const gchar *next_uri;
359   gchar *loc;
360   guint next_idx;
361
362   if (!play->gapless)
363     return;
364
365   next_idx = play->cur_idx + 1;
366   if (next_idx >= play->num_uris)
367     return;
368
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);
372   g_free (loc);
373
374   g_object_set (play->playbin, "uri", next_uri, NULL);
375 }
376
377 static void
378 do_play (GstPlay * play)
379 {
380   gint i;
381
382   /* dump playlist */
383   for (i = 0; i < play->num_uris; ++i)
384     GST_INFO ("%4u : %s", i, play->uris[i]);
385
386   if (!play_next (play))
387     return;
388
389   g_main_loop_run (play->loop);
390 }
391
392 static void
393 add_to_playlist (GPtrArray * playlist, const gchar * filename)
394 {
395   GDir *dir;
396   gchar *uri;
397
398   if (gst_uri_is_valid (filename)) {
399     g_ptr_array_add (playlist, g_strdup (filename));
400     return;
401   }
402
403   if ((dir = g_dir_open (filename, 0, NULL))) {
404     const gchar *entry;
405
406     /* FIXME: sort entries for each directory? */
407     while ((entry = g_dir_read_name (dir))) {
408       gchar *path;
409
410       path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
411       add_to_playlist (playlist, path);
412       g_free (path);
413     }
414
415     g_dir_close (dir);
416     return;
417   }
418
419   uri = gst_filename_to_uri (filename, NULL);
420   if (uri != NULL)
421     g_ptr_array_add (playlist, uri);
422   else
423     g_warning ("Could not make URI out of filename '%s'", filename);
424 }
425
426 int
427 main (int argc, char **argv)
428 {
429   GstPlay *play;
430   GPtrArray *playlist;
431   gboolean print_version = FALSE;
432   gboolean gapless = FALSE;
433   gchar **filenames = NULL;
434   gchar *audio_sink = NULL;
435   gchar *video_sink = NULL;
436   gchar **uris;
437   guint num, i;
438   GError *err = NULL;
439   GOptionContext *ctx;
440   GOptionEntry options[] = {
441     {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
442         N_("Print version information and exit"), NULL},
443     {"videosink", 0, 0, G_OPTION_ARG_STRING, &video_sink,
444         N_("Video sink to use (default is autovideosink)"), NULL},
445     {"audiosink", 0, 0, G_OPTION_ARG_STRING, &audio_sink,
446         N_("Audio sink to use (default is autoaudiosink)"), NULL},
447     {"gapless", 0, 0, G_OPTION_ARG_NONE, &gapless,
448         N_("Enable gapless playback"), NULL},
449     {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
450     {NULL}
451   };
452
453 #ifdef ENABLE_NLS
454   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
455   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
456   textdomain (GETTEXT_PACKAGE);
457 #endif
458
459   g_set_prgname ("gst-play-" GST_API_VERSION);
460
461   ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ...");
462   g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
463   g_option_context_add_group (ctx, gst_init_get_option_group ());
464   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
465     g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
466     return 1;
467   }
468   g_option_context_free (ctx);
469
470   GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play");
471
472   if (print_version) {
473     gchar *version_str;
474
475     version_str = gst_version_string ();
476     g_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
477     g_print ("%s\n", version_str);
478     g_print ("%s\n", GST_PACKAGE_ORIGIN);
479     g_free (version_str);
480     return 0;
481   }
482
483   if (filenames == NULL || *filenames == NULL) {
484     g_printerr (_("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."),
485         "gst-play-" GST_API_VERSION);
486     g_printerr ("\n\n"),
487         g_printerr ("%s\n\n",
488         _("You must provide at least one filename or URI to play."));
489     return 1;
490   }
491
492   playlist = g_ptr_array_new ();
493
494   /* fill playlist */
495   num = g_strv_length (filenames);
496   for (i = 0; i < num; ++i) {
497     GST_LOG ("command line argument: %s", filenames[i]);
498     add_to_playlist (playlist, filenames[i]);
499   }
500   g_strfreev (filenames);
501
502   g_ptr_array_add (playlist, NULL);
503
504   /* play */
505   uris = (gchar **) g_ptr_array_free (playlist, FALSE);
506   play = play_new (uris, audio_sink, video_sink, gapless);
507
508   do_play (play);
509
510   /* clean up */
511   play_free (play);
512
513   return 0;
514 }