Debian Codegraph @

Xpjproject/pjmedia/src/test/jbuf_test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <stdio.h>
20 #include <ctype.h>
21 #include <pj/pool.h>
22 #include "test.h"
23
24 #define JB_INIT_PREFETCH 0
25 #define JB_MIN_PREFETCH 0
26 #define JB_MAX_PREFETCH 10
27 #define JB_PTIME 20
28 #define JB_BUF_SIZE 50
29 #define THIS_FILE "jbuf_test.c"
30
31 //#define REPORT
32 //#define PRINT_COMMENT
33
34
35 typedef struct test_param_t {
36 pj_bool_t adaptive;
37 unsigned init_prefetch;
38 unsigned min_prefetch;
39 unsigned max_prefetch;
40 } test_param_t;
41
42 typedef struct test_cond_t {
43 int burst;
44 int discard;
45 int lost;
46 int empty;
47 int delay; /**< Average delay, in frames. */
48 int delay_min; /**< Minimum delay, in frames. */
49 } test_cond_t;
50
51 static pj_bool_t parse_test_headers(char *line, test_param_t *param,
52 test_cond_t *cond)
53 {
54 char *p = line;
55
56 if (*p == '%') {
57 /* Test params. */
58 char mode_st[16];
59
60 sscanf(p+1, "%s %u %u %u", mode_st, &param->init_prefetch,
61 &param->min_prefetch, &param->max_prefetch);
62 param->adaptive = (pj_ansi_stricmp(mode_st, "adaptive") == 0);
63
64 } else if (*p == '!') {
65 /* Success condition. */
66 char cond_st[16];
67 unsigned cond_val;
68
69 sscanf(p+1, "%s %u", cond_st, &cond_val);
70 if (pj_ansi_stricmp(cond_st, "burst") == 0)
71 cond->burst = cond_val;
72 else if (pj_ansi_stricmp(cond_st, "delay") == 0)
73 cond->delay = cond_val;
74 else if (pj_ansi_stricmp(cond_st, "delay_min") == 0)
75 cond->delay_min = cond_val;
76 else if (pj_ansi_stricmp(cond_st, "discard") == 0)
77 cond->discard = cond_val;
78 else if (pj_ansi_stricmp(cond_st, "empty") == 0)
79 cond->empty = cond_val;
80 else if (pj_ansi_stricmp(cond_st, "lost") == 0)
81 cond->lost = cond_val;
82
83 } else if (*p == '=') {
84 char *newline_pos;
85
86 /* Test title. */
87 ++p;
88 while (*p && isspace(*p)) ++p;
89
90 if ((newline_pos=strchr(p, '\n')) != NULL)
91 *newline_pos = '\0';
92
93 PJ_LOG(3, (THIS_FILE, "--- %s ---", p));
94
95 } else if (*p == '#') {
96 /* Ignore comment line. */
97
98 } else {
99 /* Unknown header, perhaps this is the test data */
100
101 /* Skip spaces */
102 while (*p && isspace(*p)) ++p;
103
104 /* Test data started.*/
105 if (*p != 0)
106 return PJ_FALSE;
107 }
108
109 return PJ_TRUE;
110 }
111
112 static pj_bool_t process_test_data(char data, pjmedia_jbuf *jb,
113 pj_uint16_t *seq, pj_uint16_t *last_seq)
114 {
115 char frame[1] = {0};
116 char f_type;
117 pj_bool_t print_state = PJ_TRUE;
118 pj_bool_t data_eos = PJ_FALSE;
119
120 switch (toupper(data)) {
121 case 'G': /* Get */
122 pjmedia_jbuf_get_frame(jb, frame, &f_type);
123 break;
124 case 'P': /* Put */
125 pjmedia_jbuf_put_frame(jb, (void*)frame, 1, *seq);
126 *last_seq = *seq;
127 ++*seq;
128 break;
129 case 'L': /* Lost */
130 *last_seq = *seq;
131 ++*seq;
132 PJ_LOG(3,(THIS_FILE, "Lost"));
133 break;
134 case 'R': /* Sequence restarts */
135 *seq = 1;
136 PJ_LOG(3,(THIS_FILE, "Sequence restarting, from %u to %u", *last_seq, *seq));
137 break;
138 case 'J': /* Sequence jumps */
139 (*seq) += 20;
140 PJ_LOG(3,(THIS_FILE, "Sequence jumping, from %u to %u", *last_seq, *seq));
141 break;
142 case 'D': /* Frame duplicated */
143 pjmedia_jbuf_put_frame(jb, (void*)frame, 1, *seq - 1);
144 break;
145 case 'O': /* Old/late frame */
146 pjmedia_jbuf_put_frame(jb, (void*)frame, 1, *seq - 10 - pj_rand()%40);
147 break;
148 case '.': /* End of test session. */
149 data_eos = PJ_TRUE;
150 break;
151 default:
152 print_state = PJ_FALSE;
153 PJ_LOG(3,(THIS_FILE, "Unknown test data '%c'", data));
154 break;
155 }
156
157 if (data_eos)
158 return PJ_FALSE;
159
160 #ifdef REPORT
161 if (print_state) {
162 pjmedia_jb_state state;
163
164 pjmedia_jbuf_get_state(jb, &state);
165 PJ_LOG(3,(THIS_FILE, ("seq=%d\t%c\tsize=%d\tprefetch=%d",
166 *last_seq, toupper(data), state.size, state.prefetch));
167 }
168 #else
169 PJ_UNUSED_ARG(print_state); /* Warning about variable set but unused */
170 #endif
171
172 return PJ_TRUE;
173 }
174
175 int jbuf_test(void)
176 {
177 FILE *input;
178 pj_bool_t data_eof = PJ_FALSE;
179 int old_log_level;
180 int rc = 0;
181 const char* input_filename = "Jbtest.dat";
182 const char* input_search_path[] = {
183 "../build",
184 "pjmedia/build",
185 "build"
186 };
187
188 /* Try to open test data file in the working directory */
189 input = fopen(input_filename, "rt");
190
191 /* If that fails, try to open test data file in specified search paths */
192 if (input == NULL) {
193 char input_path[PJ_MAXPATH];
194 unsigned i;
195
196 for (i = 0; !input && i < PJ_ARRAY_SIZE(input_search_path); ++i) {
197 pj_ansi_snprintf(input_path, PJ_MAXPATH, "%s/%s",
198 input_search_path[i],
199 input_filename);
200 input = fopen(input_path, "rt");
201 }
202 }
203
204 /* Failed to open test data file. */
205 PJ_TEST_NOT_NULL(input, "failed to open test data file, Jbtest.dat",
206 return -1);
207
208 old_log_level = pj_log_get_level();
209 pj_log_set_level(5);
210
211 while (rc == 0 && !data_eof) {
212 pj_str_t jb_name = {"JBTEST", 6};
213 pjmedia_jbuf *jb;
214 pj_pool_t *pool;
215 pjmedia_jb_state state;
216 pj_uint16_t last_seq = 0;
217 pj_uint16_t seq = 1;
218 char line[1024], *p = NULL;
219
220 test_param_t param;
221 test_cond_t cond;
222
223 param.adaptive = PJ_TRUE;
224 param.init_prefetch = JB_INIT_PREFETCH;
225 param.min_prefetch = JB_MIN_PREFETCH;
226 param.max_prefetch = JB_MAX_PREFETCH;
227
228 cond.burst = -1;
229 cond.delay = -1;
230 cond.delay_min = -1;
231 cond.discard = -1;
232 cond.empty = -1;
233 cond.lost = -1;
234
235 PJ_LOG(3,(THIS_FILE, "%s", ""));
236
237 /* Parse test session title, param, and conditions */
238 do {
239 p = fgets(line, sizeof(line), input);
240 } while (p && parse_test_headers(line, &param, &cond));
241
242 /* EOF test data */
243 if (p == NULL)
244 break;
245
246 /* Initialize test session */
247 pool = pj_pool_create(mem, "JBPOOL", 256*16, 256*16, NULL);
248 pjmedia_jbuf_create(pool, &jb_name, 1, JB_PTIME, JB_BUF_SIZE, &jb);
249 pjmedia_jbuf_reset(jb);
250
251 if (param.adaptive) {
252 pjmedia_jbuf_set_adaptive(jb,
253 param.init_prefetch,
254 param.min_prefetch,
255 param.max_prefetch);
256 } else {
257 pjmedia_jbuf_set_fixed(jb, param.init_prefetch);
258 }
259
260 #ifdef REPORT
261 pjmedia_jbuf_get_state(jb, &state);
262 PJ_LOG(3,(THIS_FILE, (("Initial\tsize=%d\tprefetch=%d\tmin.pftch=%d\tmax.pftch=%d",
263 state.size, state.prefetch, state.min_prefetch,
264 state.max_prefetch));
265 #endif
266
267
268 /* Test session start */
269 while (1) {
270 char c;
271
272 /* Get next line of test data */
273 if (!p || *p == 0) {
274 p = fgets(line, sizeof(line), input);
275 if (p == NULL) {
276 data_eof = PJ_TRUE;
277 break;
278 }
279 }
280
281 /* Get next char of test data */
282 c = *p++;
283
284 /* Skip spaces */
285 if (isspace(c))
286 continue;
287
288 /* Print comment line */
289 if (c == '#') {
290 #ifdef PRINT_COMMENT
291 while (*p && isspace(*p)) ++p;
292 if (*p) printf(("..%s", p));
293 #endif
294 *p = 0;
295 continue;
296 }
297
298 /* Process test data */
299 if (!process_test_data(c, jb, &seq, &last_seq))
300 break;
301 }
302
303 /* Print JB states */
304 pjmedia_jbuf_get_state(jb, &state);
305 //PJ_LOG(3,(THIS_FILE, "------------------------------------------------------"));
306 PJ_LOG(3,(THIS_FILE, "Summary:"));
307 PJ_LOG(3,(THIS_FILE, " size=%d prefetch=%d", state.size, state.prefetch));
308 PJ_LOG(3,(THIS_FILE, " delay (min/max/avg/dev)=%d/%d/%d/%d ms",
309 state.min_delay, state.max_delay, state.avg_delay,
310 state.dev_delay));
311 PJ_LOG(3,(THIS_FILE, " lost=%d discard=%d empty=%d burst(avg)=%d",
312 state.lost, state.discard, state.empty, state.avg_burst));
313
314 /* Evaluate test session */
315 if (cond.burst >= 0 && (int)state.avg_burst > cond.burst) {
316 PJ_LOG(1,(THIS_FILE, "! 'Burst' should be %d, it is %d",
317 cond.burst, state.avg_burst));
318 rc |= 1;
319 }
320 if (cond.delay >= 0 && (int)state.avg_delay/JB_PTIME > cond.delay) {
321 PJ_LOG(1,(THIS_FILE, "! 'Delay' should be %d, it is %d",
322 cond.delay, state.avg_delay/JB_PTIME));
323 rc |= 2;
324 }
325 if (cond.delay_min >= 0 && (int)state.min_delay/JB_PTIME > cond.delay_min) {
326 PJ_LOG(1,(THIS_FILE, "! 'Minimum delay' should be %d, it is %d",
327 cond.delay_min, state.min_delay/JB_PTIME));
328 rc |= 32;
329 }
330 if (cond.discard >= 0 && (int)state.discard > cond.discard) {
331 PJ_LOG(3,(THIS_FILE, "! 'Discard' should be %d, it is %d",
332 cond.discard, state.discard));
333 rc |= 4;
334 }
335 if (cond.empty >= 0 && (int)state.empty > cond.empty) {
336 PJ_LOG(3,(THIS_FILE, "! 'Empty' should be %d, it is %d",
337 cond.empty, state.empty));
338 rc |= 8;
339 }
340 if (cond.lost >= 0 && (int)state.lost > cond.lost) {
341 PJ_LOG(3,(THIS_FILE, "! 'Lost' should be %d, it is %d",
342 cond.lost, state.lost));
343 rc |= 16;
344 }
345
346 pjmedia_jbuf_destroy(jb);
347 pj_pool_release(pool);
348 }
349
350 fclose(input);
351 pj_log_set_level(old_log_level);
352
353 return rc;
354 }