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