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