4545f9d7a9c5b1776d9dae3ea05f042fa64a0a85
[platform/upstream/gstreamer.git] / tests / old / testsuite / bytestream / test1.c
1 #include <string.h>
2 #include <stdlib.h>
3
4 #include <gst/gst.h>
5
6 #define VM_THRES 1000
7 #define MAX_CONFIG_LINE 255
8 #define MAX_CONFIG_PATTERN 64
9
10 typedef struct
11 {
12   gint src_data;
13   gint src_sizetype;
14
15   gchar *bs_accesspattern;
16
17   gboolean integrity_check;
18 } TestParam;
19
20 static GSList *params = NULL;
21
22 static guint8 count;
23 static guint iterations;
24 static gboolean integrity_check = TRUE;
25 static gboolean verbose = FALSE;
26 static gboolean dump = FALSE;
27
28 static void
29 handoff (GstElement *element, GstBuffer *buf, GstPad *pad, gpointer data)
30 {
31   if (GST_IS_BUFFER (buf)) {
32     if (integrity_check) {
33       gint i;
34       guint8 *ptr = GST_BUFFER_DATA (buf);
35     
36       for (i=0; i<GST_BUFFER_SIZE (buf); i++) {
37         if (*ptr++ != count++) {
38           g_print ("data error!\n");
39           return;
40         }
41       }
42     }
43   }
44   else {
45     g_print ("not a buffer ! %p\n", buf);
46   }
47 }
48 static gchar*
49 create_desc (TestParam *param) 
50 {
51   gchar *desc;
52
53   desc = g_strdup_printf ("%s %s, pattern %s",  (param->src_sizetype == 2 ? "fixed" : "random"), 
54                                                 (param->src_data == 1 ? "src" : "subbuffer"),
55                                                 param->bs_accesspattern);
56   return desc;
57 }
58
59 static gboolean 
60 read_param_file (gchar *filename) 
61 {
62   FILE *fp;
63   gchar line[MAX_CONFIG_LINE+1];
64   guint linenr = 0;
65   gchar pattern[MAX_CONFIG_PATTERN];
66   gint data, sizetype, integrity_check;
67   gchar *scan_str;
68   gboolean res = TRUE;
69
70   fp = fopen (filename, "r");
71   if (fp == NULL) 
72     return FALSE;
73
74   scan_str = g_strdup_printf ("%%d %%d %%%ds %%d", MAX_CONFIG_PATTERN-1);
75   
76   while (fgets (line, MAX_CONFIG_LINE, fp)) { 
77     linenr++;
78
79     if (line[0] == '\n' || line[0] == '#')
80       continue;
81
82     if (sscanf (line, scan_str, &data, &sizetype, pattern, &integrity_check) != 4) {
83       g_print ("error on line: %d\n", linenr);
84       res = FALSE;
85       break;
86     }
87     else {
88       TestParam *param = g_malloc (sizeof (TestParam));
89
90       param->src_data = data;
91       param->src_sizetype = sizetype;
92       param->bs_accesspattern = g_strdup (pattern);
93       param->integrity_check = (integrity_check == 0 ? FALSE : TRUE);
94
95       params = g_slist_append (params, param);
96     }
97   }
98   g_free (scan_str);
99   
100   return res;
101 }
102
103 static void 
104 run_test (GstBin *pipeline, gint iters)
105 {
106   gint vm = 0;
107   gint maxiters = iters;
108   gint prev_percent = -1;
109
110   count = 0;
111   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
112
113   while (iters) {
114     gint newvm = gst_alloc_trace_live_all ();
115     gint percent;
116
117     percent = (gint)((maxiters-iters+1)*100.0/maxiters);
118
119     if (percent != prev_percent || newvm - vm > VM_THRES) {
120       g_print ("\r%d (delta %d) %.3d%%               ", newvm, newvm - vm, percent);
121       prev_percent = percent;
122       vm = newvm;
123     }
124     gst_bin_iterate (pipeline);
125
126     if (iters > 0) iters--;
127   }
128   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
129 }
130
131 static void 
132 usage (char *argv[])
133 {
134   g_print ("usage: %s [--verbose] [--dump] <paramfile> <iterations>\n", argv[0]);
135 }
136
137 int
138 main (int argc, char *argv[]) 
139 {
140   GstElement *src;
141   GstElement *sink;
142   GstElement *bs;
143   GstElement *pipeline;
144   gint testnum = 0;
145   GSList *walk;
146   gint arg_walk;
147
148   gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
149   gst_init (&argc, &argv);
150
151   arg_walk = 1;
152   while ((arg_walk < argc)  && (argv[arg_walk][0] == '-')) {
153     if (!strncmp (argv[arg_walk], "--verbose", 9))
154       verbose = TRUE;
155     else if (!strncmp (argv[arg_walk], "--dump", 6))
156       dump = TRUE;
157     else {
158       g_print ("unknown option %s (ignored)\n", argv[arg_walk]);
159     }
160
161     arg_walk++;
162   }
163   if (argc - arg_walk < 2) {
164     usage(argv);
165     return -1;
166   }
167   if (!read_param_file (argv[arg_walk])) { 
168     g_print ("error reading file %s\n", argv[arg_walk]);
169     usage (argv);
170     return -1;
171   }
172   arg_walk++;
173   iterations = atoi (argv[arg_walk]);
174
175   pipeline = gst_element_factory_make ("pipeline", "pipeline");
176   g_assert (pipeline);
177
178   src = gst_element_factory_make ("fakesrc", "src");
179   g_assert (src);
180
181   sink = gst_element_factory_make ("fakesink", "sink");
182   g_assert (sink);
183   g_object_set (sink, "signal-handoff", TRUE, NULL);
184   g_signal_connect (G_OBJECT (sink), "handoff", G_CALLBACK (handoff), NULL);
185
186   bs = gst_element_factory_make ("bstest", "bs");
187   g_assert (bs);
188
189   gst_element_link_many (src, bs, sink);
190
191   gst_bin_add_many (GST_BIN (pipeline), src, bs, sink);
192
193   walk = params;
194
195   while (walk) {
196     gchar *desc;
197     TestParam *param = (TestParam *) (walk->data);
198
199     integrity_check = param->integrity_check;
200
201     g_print ("\n\nrunning test %d (%d iterations):\n", testnum+1, iterations);
202     desc = create_desc (param);
203     g_print ("%s\n", desc);
204     g_free (desc);
205
206     g_object_set (G_OBJECT (src), "data", param->src_data, 
207                                   "sizetype", param->src_sizetype, 
208                                   "filltype", (integrity_check?5:0),
209                                   "silent", !verbose, NULL);
210
211     g_object_set (G_OBJECT (bs),  "accesspattern", param->bs_accesspattern, 
212                                   "silent", !verbose, NULL);
213
214     g_object_set (G_OBJECT (sink), "dump", dump,
215                                    "silent", !verbose, NULL);
216
217     run_test (GST_BIN (pipeline), iterations);
218
219     testnum++;
220
221     walk = g_slist_next (walk);
222   }
223
224   g_print ("\n\ndone\n");
225
226   return 0;
227
228 }