1 #include <gst/gst.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #define TEST_RUNTIME 120.0      /* how long to run the test, in seconds */
6 
7 static void
play_file(const gint delay,const gchar * uri)8 play_file (const gint delay, const gchar * uri)
9 {
10   GstStateChangeReturn sret;
11   GstMessage *msg;
12   GstElement *play;
13   guint wait_nanosecs;
14 
15   play = gst_element_factory_make ("playbin", "playbin");
16 
17   g_object_set (play, "uri", uri, NULL);
18   g_printerr ("Playing %s\n", uri);
19   sret = gst_element_set_state (play, GST_STATE_PLAYING);
20   if (sret != GST_STATE_CHANGE_ASYNC && sret != GST_STATE_CHANGE_SUCCESS) {
21     g_printerr ("ERROR: state change failed, sret=%d\n", sret);
22     goto next;
23   }
24 
25   wait_nanosecs = g_random_int_range (0, GST_MSECOND * delay);
26   msg = gst_bus_poll (GST_ELEMENT_BUS (play),
27       GST_MESSAGE_ERROR | GST_MESSAGE_EOS, wait_nanosecs);
28   if (msg) {
29     switch (GST_MESSAGE_TYPE (msg)) {
30       case GST_MESSAGE_ERROR:
31       {
32         GError *gerror;
33         gchar *debug;
34 
35         gst_message_parse_error (msg, &gerror, &debug);
36         gst_object_default_error (GST_MESSAGE_SRC (msg), gerror, debug);
37         g_clear_error (&gerror);
38         g_free (debug);
39         break;
40       }
41       case GST_MESSAGE_EOS:
42         g_printerr ("Got EOS\n");
43         break;
44       default:
45         g_printerr ("Got unexpected %s messge\n", GST_MESSAGE_TYPE_NAME (msg));
46         break;
47     }
48     gst_message_unref (msg);
49     goto next;
50   }
51 
52   /* on to the next one */
53   g_print (".");
54 
55 next:
56   gst_element_set_state (play, GST_STATE_NULL);
57   gst_object_unref (play);
58 }
59 
60 static void
check_arg(GPtrArray * files,const gchar * arg)61 check_arg (GPtrArray * files, const gchar * arg)
62 {
63   GDir *dir;
64 
65   if ((dir = g_dir_open (arg, 0, NULL))) {
66     const gchar *entry;
67 
68     while ((entry = g_dir_read_name (dir))) {
69       gchar *path;
70 
71       path = g_strconcat (arg, G_DIR_SEPARATOR_S, entry, NULL);
72       check_arg (files, path);
73       g_free (path);
74     }
75 
76     g_dir_close (dir);
77     return;
78   } else if (g_file_test (arg, G_FILE_TEST_EXISTS)) {
79     /* hack: technically an URI is not just file:// + path, but it'll do here */
80     g_ptr_array_add (files, g_strdup_printf ("file://%s", arg));
81   }
82 }
83 
84 int
main(int argc,char ** argv)85 main (int argc, char **argv)
86 {
87   GPtrArray *files;
88   gchar **args = NULL;
89   guint num, i;
90   GError *err = NULL;
91   gint run = 100;
92   GOptionContext *ctx;
93   GOptionEntry options[] = {
94     {"runtime", '\000', 0, G_OPTION_ARG_INT, &run, "maximum play time (ms)",
95         NULL},
96     {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL},
97     {NULL}
98   };
99   GTimer *timer;
100 
101   ctx = g_option_context_new ("FILES OR DIRECTORIES WITH AUDIO FILES");
102   g_option_context_add_main_entries (ctx, options, NULL);
103   g_option_context_add_group (ctx, gst_init_get_option_group ());
104   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
105     g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
106     g_option_context_free (ctx);
107     g_clear_error (&err);
108     exit (1);
109   }
110   g_option_context_free (ctx);
111 
112   if (args == NULL || *args == NULL) {
113     g_print ("Please provide one or more directories with audio files\n\n");
114     return 1;
115   }
116 
117   files = g_ptr_array_new ();
118 
119   num = g_strv_length (args);
120   for (i = 0; i < num; ++i) {
121     if (g_path_is_absolute (args[i])) {
122       check_arg (files, args[i]);
123     } else {
124       g_warning ("Argument '%s' is not an absolute file path", args[i]);
125     }
126   }
127 
128   if (files->len == 0) {
129     g_print ("Did not find any files\n\n");
130     return 1;
131   }
132 
133   timer = g_timer_new ();
134 
135   while (g_timer_elapsed (timer, NULL) < TEST_RUNTIME) {
136     gint32 idx;
137 
138     idx = g_random_int_range (0, files->len);
139     play_file (run, (const gchar *) g_ptr_array_index (files, idx));
140   }
141 
142   g_strfreev (args);
143   g_timer_destroy (timer);
144 
145   return 0;
146 }
147