tests: add unit test for samiparser issue
[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       /* flush any other error messages from the bus and clean up */
244       gst_element_set_state (play->playbin, GST_STATE_NULL);
245
246       if (play->missing != NULL && play_install_missing_plugins (play)) {
247         g_print ("New plugins installed, trying again...\n");
248         --play->cur_idx;
249         play_next (play);
250         break;
251       }
252       /* try next item in list then */
253       if (!play_next (play)) {
254         g_print ("Reached end of play list.\n");
255         g_main_loop_quit (play->loop);
256       }
257       break;
258     }
259     default:
260       if (gst_is_missing_plugin_message (msg)) {
261         gchar *desc;
262
263         desc = gst_missing_plugin_message_get_description (msg);
264         g_print ("Missing plugin: %s\n", desc);
265         g_free (desc);
266         play->missing = g_list_append (play->missing, gst_message_ref (msg));
267       }
268       break;
269   }
270
271   return TRUE;
272 }
273
274 static gboolean
275 play_timeout (gpointer user_data)
276 {
277   GstPlay *play = user_data;
278   gint64 pos = -1, dur = -1;
279
280   if (play->buffering)
281     return TRUE;
282
283   gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos);
284   gst_element_query_duration (play->playbin, GST_FORMAT_TIME, &dur);
285
286   if (pos >= 0 && dur > 0) {
287     gchar dstr[32], pstr[32];
288
289     /* FIXME: pretty print in nicer format */
290     g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
291     pstr[9] = '\0';
292     g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
293     dstr[9] = '\0';
294     g_print ("%s / %s\r", pstr, dstr);
295   }
296
297   return TRUE;
298 }
299
300 static gchar *
301 play_uri_get_display_name (GstPlay * play, const gchar * uri)
302 {
303   gchar *loc;
304
305   if (gst_uri_has_protocol (uri, "file")) {
306     loc = g_filename_from_uri (uri, NULL, NULL);
307   } else if (gst_uri_has_protocol (uri, "pushfile")) {
308     loc = g_filename_from_uri (uri + 4, NULL, NULL);
309   } else {
310     loc = g_strdup (uri);
311   }
312
313   /* Maybe additionally use glib's filename to display name function */
314   return loc;
315 }
316
317 /* returns FALSE if we have reached the end of the playlist */
318 static gboolean
319 play_next (GstPlay * play)
320 {
321   GstStateChangeReturn sret;
322   const gchar *next_uri;
323   gchar *loc;
324
325   if (++play->cur_idx >= play->num_uris)
326     return FALSE;
327
328   gst_element_set_state (play->playbin, GST_STATE_READY);
329   play_reset (play);
330
331   next_uri = play->uris[play->cur_idx];
332   loc = play_uri_get_display_name (play, next_uri);
333   g_print ("Now playing %s\n", loc);
334   g_free (loc);
335
336   g_object_set (play->playbin, "uri", next_uri, NULL);
337
338   sret = gst_element_set_state (play->playbin, GST_STATE_PLAYING);
339   switch (sret) {
340     case GST_STATE_CHANGE_FAILURE:
341       /* ignore, we should get an error message posted on the bus */
342       break;
343     case GST_STATE_CHANGE_NO_PREROLL:
344       g_print ("Pipeline is live.\n");
345       play->is_live = TRUE;
346       break;
347     case GST_STATE_CHANGE_ASYNC:
348       g_print ("Prerolling...\r");
349       break;
350     default:
351       break;
352   }
353
354   return TRUE;
355 }
356
357 static void
358 play_about_to_finish (GstElement * playbin, gpointer user_data)
359 {
360   GstPlay *play = user_data;
361   const gchar *next_uri;
362   gchar *loc;
363   guint next_idx;
364
365   if (!play->gapless)
366     return;
367
368   next_idx = play->cur_idx + 1;
369   if (next_idx >= play->num_uris)
370     return;
371
372   next_uri = play->uris[next_idx];
373   loc = play_uri_get_display_name (play, next_uri);
374   g_print ("About to finish, preparing next title: %s\n", loc);
375   g_free (loc);
376
377   g_object_set (play->playbin, "uri", next_uri, NULL);
378   play->cur_idx = next_idx;
379 }
380
381 static void
382 do_play (GstPlay * play)
383 {
384   gint i;
385
386   /* dump playlist */
387   for (i = 0; i < play->num_uris; ++i)
388     GST_INFO ("%4u : %s", i, play->uris[i]);
389
390   if (!play_next (play))
391     return;
392
393   g_main_loop_run (play->loop);
394 }
395
396 static void
397 add_to_playlist (GPtrArray * playlist, const gchar * filename)
398 {
399   GDir *dir;
400   gchar *uri;
401
402   if (gst_uri_is_valid (filename)) {
403     g_ptr_array_add (playlist, g_strdup (filename));
404     return;
405   }
406
407   if ((dir = g_dir_open (filename, 0, NULL))) {
408     const gchar *entry;
409
410     /* FIXME: sort entries for each directory? */
411     while ((entry = g_dir_read_name (dir))) {
412       gchar *path;
413
414       path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
415       add_to_playlist (playlist, path);
416       g_free (path);
417     }
418
419     g_dir_close (dir);
420     return;
421   }
422
423   uri = gst_filename_to_uri (filename, NULL);
424   if (uri != NULL)
425     g_ptr_array_add (playlist, uri);
426   else
427     g_warning ("Could not make URI out of filename '%s'", filename);
428 }
429
430 int
431 main (int argc, char **argv)
432 {
433   GstPlay *play;
434   GPtrArray *playlist;
435   gboolean print_version = FALSE;
436   gboolean gapless = FALSE;
437   gchar **filenames = NULL;
438   gchar *audio_sink = NULL;
439   gchar *video_sink = NULL;
440   gchar **uris;
441   guint num, i;
442   GError *err = NULL;
443   GOptionContext *ctx;
444   GOptionEntry options[] = {
445     {"version", 0, 0, G_OPTION_ARG_NONE, &print_version,
446         N_("Print version information and exit"), NULL},
447     {"videosink", 0, 0, G_OPTION_ARG_STRING, &video_sink,
448         N_("Video sink to use (default is autovideosink)"), NULL},
449     {"audiosink", 0, 0, G_OPTION_ARG_STRING, &audio_sink,
450         N_("Audio sink to use (default is autoaudiosink)"), NULL},
451     {"gapless", 0, 0, G_OPTION_ARG_NONE, &gapless,
452         N_("Enable gapless playback"), NULL},
453     {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
454     {NULL}
455   };
456
457 #ifdef ENABLE_NLS
458   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
459   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
460   textdomain (GETTEXT_PACKAGE);
461 #endif
462
463   g_set_prgname ("gst-play-" GST_API_VERSION);
464
465   ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ...");
466   g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
467   g_option_context_add_group (ctx, gst_init_get_option_group ());
468   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
469     g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
470     return 1;
471   }
472   g_option_context_free (ctx);
473
474   GST_DEBUG_CATEGORY_INIT (play_debug, "play", 0, "gst-play");
475
476   if (print_version) {
477     gchar *version_str;
478
479     version_str = gst_version_string ();
480     g_print ("%s version %s\n", g_get_prgname (), PACKAGE_VERSION);
481     g_print ("%s\n", version_str);
482     g_print ("%s\n", GST_PACKAGE_ORIGIN);
483     g_free (version_str);
484     return 0;
485   }
486
487   if (filenames == NULL || *filenames == NULL) {
488     g_printerr (_("Usage: %s FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."),
489         "gst-play-" GST_API_VERSION);
490     g_printerr ("\n\n"),
491         g_printerr ("%s\n\n",
492         _("You must provide at least one filename or URI to play."));
493     return 1;
494   }
495
496   playlist = g_ptr_array_new ();
497
498   /* fill playlist */
499   num = g_strv_length (filenames);
500   for (i = 0; i < num; ++i) {
501     GST_LOG ("command line argument: %s", filenames[i]);
502     add_to_playlist (playlist, filenames[i]);
503   }
504   g_strfreev (filenames);
505
506   g_ptr_array_add (playlist, NULL);
507
508   /* play */
509   uris = (gchar **) g_ptr_array_free (playlist, FALSE);
510   play = play_new (uris, audio_sink, video_sink, gapless);
511
512   do_play (play);
513
514   /* clean up */
515   play_free (play);
516
517   return 0;
518 }