| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <stdbool.h> |
| 16 |
#include <unistd.h> |
| 17 |
#include <stdint.h> |
| 18 |
#include <inttypes.h> |
| 19 |
#include <string.h> |
| 20 |
#include <signal.h> |
| 21 |
#include <arpa/inet.h> |
| 22 |
#include <sys/time.h> |
| 23 |
#include <getopt.h> |
| 24 |
|
| 25 |
#include <osmocom/core/select.h> |
| 26 |
#include <osmocom/core/application.h> |
| 27 |
#include <osmocom/core/logging.h> |
| 28 |
#include <osmocom/core/msgb.h> |
| 29 |
#include <osmocom/core/linuxlist.h> |
| 30 |
#include <osmocom/netif/jibuf.h> |
| 31 |
#include <osmocom/netif/rtp.h> |
| 32 |
#include <osmocom/netif/osmux.h> |
| 33 |
|
| 34 |
#include "osmo-pcap-test/osmo_pcap.h" |
| 35 |
|
| 36 |
|
| 37 |
struct checkpoint { |
| 38 |
struct timeval ts; |
| 39 |
int transit; |
| 40 |
double jitter; |
| 41 |
uint32_t timestamp; |
| 42 |
uint16_t seq; |
| 43 |
}; |
| 44 |
|
| 45 |
struct rtp_pkt_info { |
| 46 |
struct osmo_timer_list timer; |
| 47 |
struct timeval tx_prev_time; |
| 48 |
struct timeval tx_time; |
| 49 |
uint32_t tx_delay; |
| 50 |
int32_t skew_us; |
| 51 |
struct checkpoint prequeue; |
| 52 |
struct checkpoint postqueue; |
| 53 |
}; |
| 54 |
|
| 55 |
struct rtp_pkt_info_cb { |
| 56 |
struct rtp_pkt_info *data; |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
static bool opt_test_rand; |
| 61 |
static bool opt_debug_human; |
| 62 |
static bool opt_debug_table; |
| 63 |
static bool opt_osmux; |
| 64 |
static bool opt_skew; |
| 65 |
static char* opt_pcap_file; |
| 66 |
uint32_t opt_buffer_min = 60; |
| 67 |
uint32_t opt_buffer_max = 500; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
#define INT2IDX(x) (-1*(x)-1) |
| 72 |
struct log_info_cat jibuf_test_cat[] = { |
| 73 |
[INT2IDX(DLJIBUF)] = { |
| 74 |
.name = "DLJIBUF", |
| 75 |
.description = "Osmocom Jitter Buffer", |
| 76 |
.enabled = 1, .loglevel = LOGL_DEBUG, |
| 77 |
}, |
| 78 |
}; |
| 79 |
const struct log_info jibuf_test_log_info = { |
| 80 |
.filter_fn = NULL, |
| 81 |
.cat = jibuf_test_cat, |
| 82 |
.num_cat = ARRAY_SIZE(jibuf_test_cat), |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#define SAMPLES_PER_PKT 160 |
| 88 |
#define RTP_FREQ_MS 20 |
| 89 |
#define RTP_PKTS_PER_SEC (1000/RTP_FREQ_MS) |
| 90 |
#define NET_DELAY_MS 300 |
| 91 |
#define GENERATED_JITTER_MS 160 |
| 92 |
#define NUM_PACKETS_TO_SEND 1000 |
| 93 |
|
| 94 |
|
| 95 |
static uint8_t rtp_pkt[] = { |
| 96 |
0x80, 0x62, 0x3f, 0xcc, 0x00, 0x01, 0xa7, 0x6f, |
| 97 |
0x07, 0x09, 0x00, 0x62, 0x20, 0x14, 0xff, 0xd4, |
| 98 |
0xf9, 0xff, 0xfb, 0xe7, 0xeb, 0xf9, 0x9f, 0xf8, |
| 99 |
0xf2, 0x26, 0x33, 0x65, 0x54, |
| 100 |
}; |
| 101 |
|
| 102 |
static struct osmo_jibuf *jb; |
| 103 |
static uint16_t rtp_first_seq; |
| 104 |
static uint16_t rtp_next_seq; |
| 105 |
static uint32_t rtp_next_ts; |
| 106 |
static struct timeval tx_prev_time; |
| 107 |
static uint32_t packets_sent; |
| 108 |
static uint32_t packets_received; |
| 109 |
static uint32_t packets_dropped; |
| 110 |
static uint32_t packets_too_much_jitter; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
static struct osmo_pcap osmo_pcap; |
| 115 |
static bool pcap_finished; |
| 116 |
static struct osmux_out_handle *pcap_osmux_h; |
| 117 |
|
| 118 |
|
| 119 |
static void sigalarm_handler(int foo) |
| 120 |
{ |
| 121 |
printf("FAIL: test did not run successfully\n"); |
| 122 |
exit(EXIT_FAILURE); |
| 123 |
} |
| 124 |
|
| 125 |
struct rtp_pkt_info *msgb_get_pinfo(struct msgb* msg) |
| 126 |
{ |
| 127 |
struct rtp_pkt_info_cb *cb = (struct rtp_pkt_info_cb *)&((msg)->cb[0]); |
| 128 |
return cb->data; |
| 129 |
} |
| 130 |
|
| 131 |
static uint32_t timeval2ms(const struct timeval *ts) |
| 132 |
{ |
| 133 |
return ts->tv_sec * 1000 + ts->tv_usec / 1000; |
| 134 |
} |
| 135 |
|
| 136 |
bool pkt_is_syncpoint(struct msgb* msg, uint16_t prev_seq, uint32_t prev_timestamp) |
| 137 |
{ |
| 138 |
struct rtp_hdr *rtph = osmo_rtp_get_hdr(msg); |
| 139 |
|
| 140 |
uint16_t current_seq = ntohs(rtph->sequence); |
| 141 |
uint32_t current_tx_ts = ntohl(rtph->timestamp); |
| 142 |
bool insync = (current_tx_ts - prev_timestamp) == (current_seq - prev_seq)*SAMPLES_PER_PKT; |
| 143 |
return !insync || rtph->marker; |
| 144 |
} |
| 145 |
|
| 146 |
int32_t calc_rel_transmit_time(uint32_t tx_0, uint32_t tx_f, uint32_t rx_0, uint32_t rx_f, bool tx_is_samples, bool pre) |
| 147 |
{ |
| 148 |
int32_t rxdiff, txdiff, res; |
| 149 |
rxdiff = (rx_f - rx_0); |
| 150 |
txdiff = (tx_f - tx_0); |
| 151 |
if(tx_is_samples) |
| 152 |
txdiff = txdiff * RTP_FREQ_MS/SAMPLES_PER_PKT; |
| 153 |
res = rxdiff - txdiff; |
| 154 |
|
| 155 |
return res; |
| 156 |
} |
| 157 |
|
| 158 |
void trace_pkt(struct msgb *msg, char* info) { |
| 159 |
struct timeval now, total_delay; |
| 160 |
struct rtp_hdr *rtph = osmo_rtp_get_hdr(msg); |
| 161 |
struct rtp_pkt_info *pinfo = msgb_get_pinfo(msg); |
| 162 |
|
| 163 |
gettimeofday(&now, NULL); |
| 164 |
timersub(&now, &pinfo->tx_time, &total_delay); |
| 165 |
|
| 166 |
if (opt_debug_human) { |
| 167 |
uint32_t total_delay_ms = timeval2ms(&total_delay); |
| 168 |
LOGP(DLJIBUF, LOGL_DEBUG, "%s: seq=%"PRIu16" ts=%"PRIu32" (%ld.%06ld) tx_delay=%"PRIu32 \ |
| 169 |
" end_delay=%"PRIu32" pre_trans=%d pre_jitter=%f post_trans=%d post_jitter=%f skew=%"PRId32"\n", |
| 170 |
info, ntohs(rtph->sequence), ntohl(rtph->timestamp), |
| 171 |
(unsigned int long) pinfo->tx_time.tv_sec, (unsigned int long) pinfo->tx_time.tv_usec, |
| 172 |
pinfo->tx_delay, total_delay_ms, |
| 173 |
pinfo->prequeue.transit, pinfo->prequeue.jitter, |
| 174 |
pinfo->postqueue.transit, pinfo->postqueue.jitter, pinfo->skew_us); |
| 175 |
|
| 176 |
if (pinfo->prequeue.jitter < pinfo->postqueue.jitter) |
| 177 |
LOGP(DLJIBUF, LOGL_ERROR, "JITTER HIGHER THAN REF: seq=%"PRIu16" ts=%"PRIu32 \ |
| 178 |
" (%ld.%06ld) tx_delay=%"PRIu32" end_delay=%"PRIu32 \ |
| 179 |
" pre_trans=%d pre_jitter=%f post_trans=%d post_jitter=%f dropped=%"PRIu32 \ |
| 180 |
" buffer=%"PRIu32"\n", |
| 181 |
ntohs(rtph->sequence), ntohl(rtph->timestamp), |
| 182 |
(unsigned int long) pinfo->tx_time.tv_sec, (unsigned int long) pinfo->tx_time.tv_usec, |
| 183 |
pinfo->tx_delay, total_delay_ms, |
| 184 |
pinfo->prequeue.transit, pinfo->prequeue.jitter, |
| 185 |
pinfo->postqueue.transit, pinfo->postqueue.jitter, |
| 186 |
packets_dropped, jb->threshold_delay); |
| 187 |
} |
| 188 |
|
| 189 |
if (opt_debug_table) { |
| 190 |
|
| 191 |
uint32_t tx_time_ms = timeval2ms(&pinfo->tx_time); |
| 192 |
uint32_t prequeue_time_ms = timeval2ms(&pinfo->prequeue.ts); |
| 193 |
uint32_t postqueue_time_ms = timeval2ms(&pinfo->postqueue.ts); |
| 194 |
fprintf(stderr, "%"PRIu16"\t%"PRIu32"\t%"PRIu32"\t%"PRIu32"\t%d\t%d\t%f\t%f\t%"PRIu32"\t%"PRIu32"\t%f\n", |
| 195 |
ntohs(rtph->sequence), tx_time_ms, |
| 196 |
prequeue_time_ms, postqueue_time_ms, |
| 197 |
pinfo->prequeue.transit, pinfo->postqueue.transit, |
| 198 |
pinfo->prequeue.jitter, pinfo->postqueue.jitter, |
| 199 |
packets_dropped, jb->threshold_delay, (double)jb->skew_us/1000.0); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
void pkt_add_result(struct msgb *msg, bool dropped) |
| 204 |
{ |
| 205 |
struct rtp_pkt_info *pinfo = msgb_get_pinfo(msg); |
| 206 |
|
| 207 |
if (dropped) { |
| 208 |
packets_dropped++; |
| 209 |
trace_pkt(msg,"dropped"); |
| 210 |
} else { |
| 211 |
packets_received++; |
| 212 |
trace_pkt(msg,"received"); |
| 213 |
|
| 214 |
if (pinfo->prequeue.jitter < pinfo->postqueue.jitter) |
| 215 |
packets_too_much_jitter++; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
void dequeue_cb(struct msgb *msg, void *data) |
| 220 |
{ |
| 221 |
static struct checkpoint postqueue_prev; |
| 222 |
static bool postqueue_started = false; |
| 223 |
|
| 224 |
bool tx_is_samples; |
| 225 |
struct rtp_hdr *rtph = osmo_rtp_get_hdr(msg); |
| 226 |
struct rtp_pkt_info *pinfo = msgb_get_pinfo(msg); |
| 227 |
|
| 228 |
uint32_t tx1, tx0, rx0, rx1; |
| 229 |
|
| 230 |
gettimeofday(&pinfo->postqueue.ts, NULL); |
| 231 |
pinfo->postqueue.timestamp = htonl(rtph->timestamp); |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
if (postqueue_started && !pkt_is_syncpoint(msg, postqueue_prev.seq, postqueue_prev.timestamp)) { |
| 236 |
|
| 237 |
|
| 238 |
if(opt_test_rand) { |
| 239 |
tx0 = timeval2ms(&pinfo->tx_prev_time); |
| 240 |
tx1 = timeval2ms(&pinfo->tx_time); |
| 241 |
tx_is_samples = false; |
| 242 |
} else { |
| 243 |
tx0 = postqueue_prev.timestamp; |
| 244 |
tx1 = pinfo->postqueue.timestamp; |
| 245 |
tx_is_samples = true; |
| 246 |
} |
| 247 |
rx0 = timeval2ms(&postqueue_prev.ts); |
| 248 |
rx1 = timeval2ms(&pinfo->postqueue.ts); |
| 249 |
pinfo->postqueue.transit = calc_rel_transmit_time(tx0, tx1, rx0, rx1, tx_is_samples, 0); |
| 250 |
|
| 251 |
uint32_t abs_transit = pinfo->postqueue.transit * |
| 252 |
( pinfo->postqueue.transit >= 0 ? 1 : -1 ); |
| 253 |
|
| 254 |
pinfo->postqueue.jitter = postqueue_prev.jitter + |
| 255 |
((double)abs_transit - postqueue_prev.jitter)/16.0; |
| 256 |
} else { |
| 257 |
postqueue_started = true; |
| 258 |
pinfo->postqueue.transit = 0; |
| 259 |
pinfo->postqueue.jitter = 0; |
| 260 |
} |
| 261 |
|
| 262 |
postqueue_prev = pinfo->postqueue; |
| 263 |
postqueue_prev.seq = htons(rtph->sequence); |
| 264 |
|
| 265 |
pkt_add_result(msg, false); |
| 266 |
|
| 267 |
osmo_timer_del(&pinfo->timer); |
| 268 |
msgb_free(msg); |
| 269 |
} |
| 270 |
|
| 271 |
void pkt_arrived_cb(void *data) |
| 272 |
{ |
| 273 |
static struct checkpoint prequeue_prev; |
| 274 |
static bool prequeue_started = false; |
| 275 |
|
| 276 |
bool tx_is_samples; |
| 277 |
struct msgb *msg = (struct msgb*) data; |
| 278 |
struct rtp_hdr *rtph = osmo_rtp_get_hdr(msg); |
| 279 |
struct rtp_pkt_info *pinfo = msgb_get_pinfo(msg); |
| 280 |
|
| 281 |
|
| 282 |
uint32_t tx1, tx0, rx0, rx1; |
| 283 |
|
| 284 |
gettimeofday(&pinfo->prequeue.ts, NULL); |
| 285 |
pinfo->prequeue.timestamp = htonl(rtph->timestamp); |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
if (prequeue_started && !pkt_is_syncpoint(msg, prequeue_prev.seq, prequeue_prev.timestamp)) { |
| 290 |
|
| 291 |
|
| 292 |
if(opt_test_rand) { |
| 293 |
tx0 = timeval2ms(&pinfo->tx_prev_time); |
| 294 |
tx1 = timeval2ms(&pinfo->tx_time); |
| 295 |
tx_is_samples = false; |
| 296 |
} else { |
| 297 |
tx0 = prequeue_prev.timestamp; |
| 298 |
tx1 = pinfo->prequeue.timestamp; |
| 299 |
tx_is_samples = true; |
| 300 |
} |
| 301 |
rx0 = timeval2ms(&prequeue_prev.ts); |
| 302 |
rx1 = timeval2ms(&pinfo->prequeue.ts); |
| 303 |
pinfo->prequeue.transit = calc_rel_transmit_time(tx0, tx1, rx0, rx1, tx_is_samples, 1); |
| 304 |
|
| 305 |
uint32_t abs_transit = pinfo->prequeue.transit * |
| 306 |
( pinfo->prequeue.transit >= 0 ? 1 : -1 ); |
| 307 |
|
| 308 |
pinfo->prequeue.jitter = prequeue_prev.jitter + |
| 309 |
((double)abs_transit - prequeue_prev.jitter)/16.0; |
| 310 |
} else { |
| 311 |
prequeue_started = true; |
| 312 |
pinfo->prequeue.transit = 0; |
| 313 |
pinfo->prequeue.jitter = 0; |
| 314 |
} |
| 315 |
|
| 316 |
prequeue_prev = pinfo->prequeue; |
| 317 |
prequeue_prev.seq = htons(rtph->sequence); |
| 318 |
|
| 319 |
int n = osmo_jibuf_enqueue(jb, msg); |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
pinfo->skew_us = jb->skew_us; |
| 324 |
|
| 325 |
if (n<0) { |
| 326 |
pkt_add_result(msg, true); |
| 327 |
osmo_timer_del(&pinfo->timer); |
| 328 |
msgb_free(msg); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
struct rtp_pkt_info * msgb_allocate_pinfo(struct msgb *msg) |
| 333 |
{ |
| 334 |
struct rtp_pkt_info_cb *cb = (struct rtp_pkt_info_cb *)&((msg)->cb[0]); |
| 335 |
cb->data = (struct rtp_pkt_info *) talloc_zero(msg, struct rtp_pkt_info); |
| 336 |
return cb->data; |
| 337 |
} |
| 338 |
|
| 339 |
void rand_send_rtp_packet(void) |
| 340 |
{ |
| 341 |
|
| 342 |
struct rtp_pkt_info *pinfo; |
| 343 |
struct rtp_hdr *rtph; |
| 344 |
struct msgb *msg; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
if (rtp_next_seq == rtp_first_seq) { |
| 349 |
struct timeval now, time_rate = { .tv_sec = 0, .tv_usec = RTP_FREQ_MS * 1000}; |
| 350 |
gettimeofday(&now, NULL); |
| 351 |
timersub(&now, &time_rate, &tx_prev_time); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
msg = msgb_alloc(1500, "test"); |
| 356 |
if (!msg) |
| 357 |
exit(EXIT_FAILURE); |
| 358 |
|
| 359 |
memcpy(msg->data, rtp_pkt, sizeof(rtp_pkt)); |
| 360 |
msgb_put(msg, sizeof(rtp_pkt)); |
| 361 |
|
| 362 |
rtph = osmo_rtp_get_hdr(msg); |
| 363 |
|
| 364 |
rtph->sequence = htons(rtp_next_seq); |
| 365 |
rtp_next_seq++; |
| 366 |
|
| 367 |
rtph->timestamp = htonl(rtp_next_ts); |
| 368 |
rtp_next_ts += SAMPLES_PER_PKT; |
| 369 |
|
| 370 |
pinfo = msgb_allocate_pinfo(msg); |
| 371 |
|
| 372 |
gettimeofday(&pinfo->tx_time, NULL); |
| 373 |
pinfo->tx_prev_time = tx_prev_time; |
| 374 |
memset(&pinfo->timer, 0, sizeof(struct osmo_timer_list)); |
| 375 |
pinfo->timer.cb = pkt_arrived_cb; |
| 376 |
pinfo->timer.data = msg; |
| 377 |
pinfo->tx_delay = NET_DELAY_MS + (random() % (GENERATED_JITTER_MS)); |
| 378 |
|
| 379 |
tx_prev_time = pinfo->tx_time; |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
osmo_timer_schedule(&pinfo->timer, 0, pinfo->tx_delay * 1000); |
| 384 |
} |
| 385 |
|
| 386 |
void rand_generate_pkt_cb(void *data) |
| 387 |
{ |
| 388 |
static struct osmo_timer_list enqueue_timer = {.cb = rand_generate_pkt_cb, .data = NULL}; |
| 389 |
static struct timeval last_generated; |
| 390 |
|
| 391 |
struct timeval time_rate = { .tv_sec = 0, .tv_usec = RTP_FREQ_MS * 1000}; |
| 392 |
struct timeval sched_ts; |
| 393 |
|
| 394 |
if (!packets_sent) |
| 395 |
gettimeofday(&last_generated, NULL); |
| 396 |
|
| 397 |
rand_send_rtp_packet(); |
| 398 |
packets_sent++; |
| 399 |
|
| 400 |
timeradd(&last_generated, &time_rate, &sched_ts); |
| 401 |
last_generated = sched_ts; |
| 402 |
if (packets_sent < NUM_PACKETS_TO_SEND) { |
| 403 |
enqueue_timer.timeout = sched_ts; |
| 404 |
osmo_timer_add(&enqueue_timer); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
static int pcap_generate_pkt_cb(struct msgb *msg) |
| 410 |
{ |
| 411 |
struct rtp_pkt_info *pinfo; |
| 412 |
|
| 413 |
|
| 414 |
if (!packets_sent) { |
| 415 |
struct timeval now, time_rate = { .tv_sec = 0, .tv_usec = RTP_FREQ_MS * 1000}; |
| 416 |
gettimeofday(&now, NULL); |
| 417 |
timersub(&now, &time_rate, &tx_prev_time); |
| 418 |
} |
| 419 |
|
| 420 |
pinfo = msgb_allocate_pinfo(msg); |
| 421 |
gettimeofday(&pinfo->tx_time, NULL); |
| 422 |
pinfo->tx_prev_time = tx_prev_time; |
| 423 |
|
| 424 |
tx_prev_time = pinfo->tx_time; |
| 425 |
packets_sent++; |
| 426 |
pkt_arrived_cb(msg); |
| 427 |
return 0; |
| 428 |
} |
| 429 |
|
| 430 |
void glue_cb(struct msgb *msg, void *data) |
| 431 |
{ |
| 432 |
pcap_generate_pkt_cb(msg); |
| 433 |
} |
| 434 |
|
| 435 |
int pcap_read_osmux(struct msgb *msg) |
| 436 |
{ |
| 437 |
struct osmux_hdr *osmuxh; |
| 438 |
|
| 439 |
|
| 440 |
while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) |
| 441 |
osmux_xfrm_output_sched(pcap_osmux_h, osmuxh); |
| 442 |
msgb_free(msg); |
| 443 |
return 0; |
| 444 |
} |
| 445 |
|
| 446 |
void pcap_pkt_timer_cb(void *data) |
| 447 |
{ |
| 448 |
int (*mycb)(struct msgb *msgb); |
| 449 |
if(opt_osmux) |
| 450 |
mycb = pcap_read_osmux; |
| 451 |
else |
| 452 |
mycb = pcap_generate_pkt_cb; |
| 453 |
|
| 454 |
if (osmo_pcap_test_run(&osmo_pcap, IPPROTO_UDP, mycb) < 0) { |
| 455 |
osmo_pcap_stats_printf(); |
| 456 |
osmo_pcap_test_close(osmo_pcap.h); |
| 457 |
pcap_finished=true; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
void rand_test_check(void) |
| 462 |
{ |
| 463 |
uint32_t drop_threshold = NUM_PACKETS_TO_SEND * 5 / 100; |
| 464 |
if (packets_dropped > drop_threshold) { |
| 465 |
fprintf(stdout, "Too many dropped packets (%"PRIu32" > %"PRIu32")\n", |
| 466 |
packets_dropped, drop_threshold); |
| 467 |
exit(EXIT_FAILURE); |
| 468 |
} |
| 469 |
|
| 470 |
uint32_t jitter_high_threshold = NUM_PACKETS_TO_SEND * 8 / 100; |
| 471 |
if (packets_too_much_jitter > jitter_high_threshold) { |
| 472 |
fprintf(stdout, "Too many packets with higher jitter (%"PRIu32" > %"PRIu32")\n", |
| 473 |
packets_too_much_jitter, jitter_high_threshold); |
| 474 |
exit(EXIT_FAILURE); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
void rand_test(void) |
| 479 |
{ |
| 480 |
srandom(time(NULL)); |
| 481 |
rtp_first_seq = (uint16_t) random(); |
| 482 |
rtp_next_seq = rtp_first_seq; |
| 483 |
rtp_next_ts = (uint32_t) random(); |
| 484 |
jb = osmo_jibuf_alloc(NULL); |
| 485 |
|
| 486 |
osmo_jibuf_set_min_delay(jb, GENERATED_JITTER_MS - RTP_FREQ_MS); |
| 487 |
osmo_jibuf_set_max_delay(jb, GENERATED_JITTER_MS + RTP_FREQ_MS*2); |
| 488 |
osmo_jibuf_enable_skew_compensation(jb, opt_skew); |
| 489 |
osmo_jibuf_set_dequeue_cb(jb, dequeue_cb, NULL); |
| 490 |
|
| 491 |
|
| 492 |
rand_generate_pkt_cb(NULL); |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
alarm(NUM_PACKETS_TO_SEND*20/1000 +10); |
| 497 |
|
| 498 |
while((packets_received + packets_dropped) < NUM_PACKETS_TO_SEND) |
| 499 |
osmo_select_main(0); |
| 500 |
|
| 501 |
osmo_jibuf_delete(jb); |
| 502 |
|
| 503 |
rand_test_check(); |
| 504 |
} |
| 505 |
|
| 506 |
void pcap_test_check(void) { |
| 507 |
|
| 508 |
} |
| 509 |
|
| 510 |
void pcap_test(void) { |
| 511 |
osmo_pcap_init(); |
| 512 |
|
| 513 |
osmo_pcap.h = osmo_pcap_test_open(opt_pcap_file); |
| 514 |
if (osmo_pcap.h == NULL) |
| 515 |
exit(EXIT_FAILURE); |
| 516 |
|
| 517 |
osmo_pcap.timer.cb = pcap_pkt_timer_cb; |
| 518 |
|
| 519 |
if(opt_osmux) { |
| 520 |
pcap_osmux_h = osmux_xfrm_output_alloc(NULL); |
| 521 |
osmux_xfrm_output_set_rtp_ssrc(pcap_osmux_h, 0); |
| 522 |
osmux_xfrm_output_set_rtp_pl_type(pcap_osmux_h, 98); |
| 523 |
osmux_xfrm_output_set_tx_cb(pcap_osmux_h, glue_cb, NULL); |
| 524 |
} |
| 525 |
|
| 526 |
jb = osmo_jibuf_alloc(NULL); |
| 527 |
osmo_jibuf_set_dequeue_cb(jb, dequeue_cb, NULL); |
| 528 |
osmo_jibuf_set_min_delay(jb, opt_buffer_min); |
| 529 |
osmo_jibuf_set_max_delay(jb, opt_buffer_max); |
| 530 |
osmo_jibuf_enable_skew_compensation(jb, opt_skew); |
| 531 |
|
| 532 |
|
| 533 |
pcap_pkt_timer_cb(NULL); |
| 534 |
|
| 535 |
while(!pcap_finished || !osmo_jibuf_empty(jb)) { |
| 536 |
if (pcap_finished && opt_osmux) |
| 537 |
osmux_xfrm_output_flush(pcap_osmux_h); |
| 538 |
osmo_select_main(0); |
| 539 |
} |
| 540 |
|
| 541 |
osmo_jibuf_delete(jb); |
| 542 |
talloc_free(pcap_osmux_h); |
| 543 |
|
| 544 |
pcap_test_check(); |
| 545 |
} |
| 546 |
|
| 547 |
static void print_help(void) |
| 548 |
{ |
| 549 |
printf("jibuf_test [-r] [-p pcap] [-o] [-d] [-g] [-s] [-m ms] [-M ms]\n"); |
| 550 |
printf(" -h Print this help message\n"); |
| 551 |
printf(" -r Run test with randomly generated jitter\n"); |
| 552 |
printf(" -p Run test with specified pcap file\n"); |
| 553 |
printf(" -o The pcap contains OSMUX packets isntead of RTP\n"); |
| 554 |
printf(" -d Enable packet trace debug suitable for humans\n"); |
| 555 |
printf(" -t Enable packet trace debug suitable for gnuplot\n"); |
| 556 |
printf(" -s Enable skew estimation and compensation algorithm on the jitter-buffer\n"); |
| 557 |
printf(" -m Minimum buffer size for the jitter-buffer, in ms (only used in -p mode)\n"); |
| 558 |
printf(" -M Maximum buffer size for the jitter-buffer, in ms (only used in -p mode)\n"); |
| 559 |
} |
| 560 |
|
| 561 |
static int parse_options(int argc, char **argv) |
| 562 |
{ |
| 563 |
int opt; |
| 564 |
|
| 565 |
while ((opt = getopt(argc, argv, "hdtrosp:m:M:")) != -1) { |
| 566 |
switch (opt) { |
| 567 |
case 'h': |
| 568 |
print_help(); |
| 569 |
return -1; |
| 570 |
case 'd': |
| 571 |
opt_debug_human = true; |
| 572 |
break; |
| 573 |
case 't': |
| 574 |
opt_debug_table = true; |
| 575 |
break; |
| 576 |
case 'r': |
| 577 |
opt_test_rand = true; |
| 578 |
break; |
| 579 |
case 'o': |
| 580 |
opt_osmux = true; |
| 581 |
break; |
| 582 |
case 's': |
| 583 |
opt_skew = true; |
| 584 |
break; |
| 585 |
case 'p': |
| 586 |
opt_pcap_file = strdup(optarg); |
| 587 |
break; |
| 588 |
case 'm': |
| 589 |
opt_buffer_min = (uint32_t) atoi(optarg); |
| 590 |
break; |
| 591 |
case 'M': |
| 592 |
opt_buffer_max = (uint32_t) atoi(optarg); |
| 593 |
break; |
| 594 |
default: |
| 595 |
return -1; |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
return 0; |
| 600 |
} |
| 601 |
|
| 602 |
int main(int argc, char **argv) |
| 603 |
{ |
| 604 |
|
| 605 |
if (signal(SIGALRM, sigalarm_handler) == SIG_ERR) { |
| 606 |
perror("signal"); |
| 607 |
exit(EXIT_FAILURE); |
| 608 |
} |
| 609 |
|
| 610 |
if(parse_options(argc, argv) < 0) |
| 611 |
exit(EXIT_FAILURE); |
| 612 |
|
| 613 |
void *tall_ctx = talloc_named_const(NULL, 1, "Root context"); |
| 614 |
msgb_talloc_ctx_init(tall_ctx, 0); |
| 615 |
osmo_init_logging2(tall_ctx, &jibuf_test_log_info); |
| 616 |
log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE); |
| 617 |
log_set_log_level(osmo_stderr_target, LOGL_DEBUG); |
| 618 |
|
| 619 |
if(opt_debug_human && !opt_debug_table) |
| 620 |
log_set_category_filter(osmo_stderr_target, DLMIB, 1, LOGL_DEBUG); |
| 621 |
|
| 622 |
if(opt_pcap_file && opt_test_rand) { |
| 623 |
print_help(); |
| 624 |
exit(EXIT_FAILURE); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
if(opt_pcap_file) |
| 629 |
pcap_test(); |
| 630 |
|
| 631 |
if(opt_test_rand) |
| 632 |
rand_test(); |
| 633 |
|
| 634 |
fprintf(stdout, "OK: Test passed\n"); |
| 635 |
return EXIT_SUCCESS; |
| 636 |
} |