| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 32 |
|
| 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; |
| 48 |
int delay_min; |
| 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 |
|
| 58 |
char mode_st[16]; |
| 59 |
|
| 60 |
sscanf(p+1, "%s %u %u %u", mode_st, ¶m->init_prefetch, |
| 61 |
¶m->min_prefetch, ¶m->max_prefetch); |
| 62 |
param->adaptive = (pj_ansi_stricmp(mode_st, "adaptive") == 0); |
| 63 |
|
| 64 |
} else if (*p == '!') { |
| 65 |
|
| 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 |
|
| 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 |
|
| 97 |
|
| 98 |
} else { |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
while (*p && isspace(*p)) ++p; |
| 103 |
|
| 104 |
|
| 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': |
| 122 |
pjmedia_jbuf_get_frame(jb, frame, &f_type); |
| 123 |
break; |
| 124 |
case 'P': |
| 125 |
pjmedia_jbuf_put_frame(jb, (void*)frame, 1, *seq); |
| 126 |
*last_seq = *seq; |
| 127 |
++*seq; |
| 128 |
break; |
| 129 |
case 'L': |
| 130 |
*last_seq = *seq; |
| 131 |
++*seq; |
| 132 |
PJ_LOG(3,(THIS_FILE, "Lost")); |
| 133 |
break; |
| 134 |
case 'R': |
| 135 |
*seq = 1; |
| 136 |
PJ_LOG(3,(THIS_FILE, "Sequence restarting, from %u to %u", *last_seq, *seq)); |
| 137 |
break; |
| 138 |
case 'J': |
| 139 |
(*seq) += 20; |
| 140 |
PJ_LOG(3,(THIS_FILE, "Sequence jumping, from %u to %u", *last_seq, *seq)); |
| 141 |
break; |
| 142 |
case 'D': |
| 143 |
pjmedia_jbuf_put_frame(jb, (void*)frame, 1, *seq - 1); |
| 144 |
break; |
| 145 |
case 'O': |
| 146 |
pjmedia_jbuf_put_frame(jb, (void*)frame, 1, *seq - 10 - pj_rand()%40); |
| 147 |
break; |
| 148 |
case '.': |
| 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); |
| 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 |
|
| 189 |
input = fopen(input_filename, "rt"); |
| 190 |
|
| 191 |
|
| 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 |
|
| 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 |
|
| 238 |
do { |
| 239 |
p = fgets(line, sizeof(line), input); |
| 240 |
} while (p && parse_test_headers(line, ¶m, &cond)); |
| 241 |
|
| 242 |
|
| 243 |
if (p == NULL) |
| 244 |
break; |
| 245 |
|
| 246 |
|
| 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 |
|
| 269 |
while (1) { |
| 270 |
char c; |
| 271 |
|
| 272 |
|
| 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 |
|
| 282 |
c = *p++; |
| 283 |
|
| 284 |
|
| 285 |
if (isspace(c)) |
| 286 |
continue; |
| 287 |
|
| 288 |
|
| 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 |
|
| 299 |
if (!process_test_data(c, jb, &seq, &last_seq)) |
| 300 |
break; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
pjmedia_jbuf_get_state(jb, &state); |
| 305 |
|
| 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 |
|
| 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 |
} |