Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / tests / icles / stress-playbin.c
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
8 play_file (const gchar * bin, 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 (bin, "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_error_free (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
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
85 main (int argc, char **argv)
86 {
87   GPtrArray *files;
88   gchar **args = NULL;
89   guint num, i;
90   GError *err = NULL;
91   gchar *bin = NULL;
92   gint run = 100;
93   GOptionContext *ctx;
94   GOptionEntry options[] = {
95     {"bin", '\000', 0, G_OPTION_ARG_STRING, &bin, "playbin factory name", NULL},
96     {"runtime", '\000', 0, G_OPTION_ARG_INT, &run, "maximum play time (ms)",
97         NULL},
98     {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL},
99     {NULL}
100   };
101   GTimer *timer;
102
103   if (!g_thread_supported ())
104     g_thread_init (NULL);
105
106   ctx = g_option_context_new ("FILES OR DIRECTORIES WITH AUDIO FILES");
107   g_option_context_add_main_entries (ctx, options, NULL);
108   g_option_context_add_group (ctx, gst_init_get_option_group ());
109   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
110     g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
111     exit (1);
112   }
113   g_option_context_free (ctx);
114
115   if (!bin)
116     bin = g_strdup ("playbin");
117
118   if (strcmp (bin, "playbin") && strcmp (bin, "playbin2")) {
119     g_print ("Please provide a valid playbin argument; playbin | playbin2");
120     return 1;
121   }
122   if (args == NULL || *args == NULL) {
123     g_print ("Please provide one or more directories with audio files\n\n");
124     return 1;
125   }
126
127   files = g_ptr_array_new ();
128
129   num = g_strv_length (args);
130   for (i = 0; i < num; ++i) {
131     if (g_path_is_absolute (args[i])) {
132       check_arg (files, args[i]);
133     } else {
134       g_warning ("Argument '%s' is not an absolute file path", args[i]);
135     }
136   }
137
138   if (files->len == 0) {
139     g_print ("Did not find any files\n\n");
140     return 1;
141   }
142
143   timer = g_timer_new ();
144
145   while (g_timer_elapsed (timer, NULL) < TEST_RUNTIME) {
146     gint32 idx;
147
148     idx = g_random_int_range (0, files->len);
149     play_file (bin, run, (const gchar *) g_ptr_array_index (files, idx));
150   }
151
152   g_strfreev (args);
153   g_free (bin);
154   g_timer_destroy (timer);
155
156   return 0;
157 }