Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / mobile / tests / examples / adapter / adapter_test.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <sys/times.h>
5
6 #include <gst/gst.h>
7 #include <gst/base/gstadapter.h>
8
9 struct TestParams
10 {
11   gint tot_size;
12   gint read_size;
13   gint write_size;
14 };
15
16 /* This test pushes 'n' buffers of 'write size' into an adapter, then reads
17  * them out in 'read size' sized pieces, using take and then take_buffer, 
18  * and prints the timings */
19
20 static struct TestParams param_sets[] = {
21 /* These values put ~256MB in 1MB chunks in an adapter, then reads them out
22  * in 250kb blocks */
23   {256000000, 250000, 1000000},
24 /* These values put ~256MB in 1000 byte chunks in an adapter, then reads them 
25  * out in 200 byte blocks */
26   {25600000, 200, 1000},
27 /* These values put ~256MB in 200 chunks in an adapter, then reads them out
28  * in 1000 byte blocks */
29   {25600000, 1000, 200}
30 };
31 static const gint n_tests = sizeof (param_sets) / sizeof (struct TestParams);
32
33 static gint ticks_per_sec;
34
35 static void
36 run_test_take (struct TestParams *params)
37 {
38   /* Create an adapter and feed it data of fixed size, then retrieve it in 
39    * a different size */
40   GstAdapter *adapter = gst_adapter_new ();
41   guint8 *data;
42   GstBuffer *buf;
43   int i;
44   gint ntimes = params->tot_size / params->write_size;
45
46   for (i = 0; i < ntimes; i++) {
47     buf = gst_buffer_new_and_alloc (params->write_size);
48     memset (GST_BUFFER_DATA (buf), 0, params->write_size);
49
50     gst_adapter_push (adapter, buf);
51   }
52
53   do {
54     data = gst_adapter_take (adapter, params->read_size);
55     if (data == NULL)
56       break;
57     g_free (data);
58   } while (TRUE);
59
60   g_object_unref (adapter);
61 }
62
63 static void
64 run_test_take_buffer (struct TestParams *params)
65 {
66   /* Create an adapter and feed it data of fixed size, then retrieve it in 
67    * a different size
68    */
69   GstAdapter *adapter = gst_adapter_new ();
70   GstBuffer *buf;
71   int i;
72   gint ntimes = params->tot_size / params->write_size;
73
74   for (i = 0; i < ntimes; i++) {
75     buf = gst_buffer_new_and_alloc (params->write_size);
76     memset (GST_BUFFER_DATA (buf), 0, params->write_size);
77
78     gst_adapter_push (adapter, buf);
79   }
80
81   do {
82     buf = gst_adapter_take_buffer (adapter, params->read_size);
83     if (buf == NULL)
84       break;
85     gst_buffer_unref (buf);
86   } while (TRUE);
87
88   g_object_unref (adapter);
89 }
90
91 static void
92 run_tests (struct TestParams *params)
93 {
94   struct tms time_data;
95   gdouble start;
96   gdouble dur;
97
98   g_print ("Running on %d bytes, writing %d bytes/buf, reading %d bytes/buf\n",
99       params->tot_size, params->write_size, params->read_size);
100
101   start = 0.0;
102   run_test_take (params);
103
104   times (&time_data);
105   dur = (gdouble) (time_data.tms_utime + time_data.tms_stime) / ticks_per_sec;
106   g_print ("Time for take test: %g secs\n", dur - start);
107
108   start = dur;
109   run_test_take_buffer (params);
110
111   times (&time_data);
112   dur = (gdouble) (time_data.tms_utime + time_data.tms_stime) / ticks_per_sec;
113   g_print ("Time for TakeBuffer test: %g secs\n", dur - start);
114
115   g_print ("\n");
116 }
117
118 int
119 main (int argc, char **argv)
120 {
121   gint i;
122
123   ticks_per_sec = sysconf (_SC_CLK_TCK);
124
125   gst_init (&argc, &argv);
126
127   for (i = 0; i < n_tests; i++)
128     run_tests (param_sets + i);
129
130   return 0;
131 }