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