gst-play: move current playlist index along in about-to-finish
[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   play->cur_idx = next_idx;
376 }
377
378 static void
379 do_play (GstPlay * play)
380 {
381   gint i;
382
383   /* dump playlist */
384   for (i = 0; i < play->num_uris; ++i)
385     GST_INFO ("%4u : %s", i, play->uris[i]);
386
387   if (!play_next (play))
388     return;
389
390   g_main_loop_run (play->loop);
391 }
392
393 static void
394 add_to_playlist (GPtrArray * playlist, const gchar * filename)
395 {
396   GDir *dir;
397   gchar *uri;
398
399   if (gst_uri_is_valid (filename)) {
400     g_ptr_array_add (playlist, g_strdup (filename));
401     return;
402   }
403
404   if ((dir = g_dir_open (filename, 0, NULL))) {
405     const gchar *entry;
406
407     /* FIXME: sort entries for each directory? */
408     while ((entry = g_dir_read_name (dir))) {
409       gchar *path;
410
411       path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
412       add_to_playlist (playlist, path);
413       g_free (path);
414     }
415
416     g_dir_close (dir);
417     return;
418   }
419
420   uri = gst_filename_to_uri (filename, NULL);
421   if (uri != NULL)
422     g_ptr_array_add (playlist, uri);
423   else
424     g_warning ("Could not make URI out of filename '%s'", filename);
425 }
426
427 int
428 main (int argc, char **argv)
429 {
430   GstPlay *play;
431   GPtrArray *playlist;
432   gboolean print_version = FALSE;
433   gboolean gapless = FALSE;
434   gchar **filenames = NULL;
435   gchar *audio_sink = NULL;
436   gchar *video_sink = NULL;
437   gchar **uris;
438   guint num, i;
439   GError *err = NULL;
440   GOptionContext *ctx;
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},
451     {NULL}
452   };
453
454 #ifdef ENABLE_NLS
455   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
456   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
457   textdomain (GETTEXT_PACKAGE);
458 #endif
459
460   g_set_prgname ("gst-play-" GST_API_VERSION);
461
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));
467     return 1;
468   }
469   g_option_context_free (ctx);
470
471   GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play");
472
473   if (print_version) {
474     gchar *version_str;
475
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);
481     return 0;
482   }
483
484   if (filenames == NULL || *filenames == NULL) {
485     g_printerr (_("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."),
486         "gst-play-" GST_API_VERSION);
487     g_printerr ("\n\n"),
488         g_printerr ("%s\n\n",
489         _("You must provide at least one filename or URI to play."));
490     return 1;
491   }
492
493   playlist = g_ptr_array_new ();
494
495   /* fill playlist */
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]);
500   }
501   g_strfreev (filenames);
502
503   g_ptr_array_add (playlist, NULL);
504
505   /* play */
506   uris = (gchar **) g_ptr_array_free (playlist, FALSE);
507   play = play_new (uris, audio_sink, video_sink, gapless);
508
509   do_play (play);
510
511   /* clean up */
512   play_free (play);
513
514   return 0;
515 }