本文整理汇总了C++中pj_rand函数的典型用法代码示例。如果您正苦于以下问题:C++ pj_rand函数的具体用法?C++ pj_rand怎么用?C++ pj_rand使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pj_rand函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: PJ_DEF
PJ_DEF(char*) pj_create_random_string(char *str, pj_size_t len)
{
unsigned i;
char *p = str;
PJ_CHECK_STACK();
for (i=0; i<len/8; ++i) {
pj_uint32_t val = pj_rand();
pj_val_to_hex_digit( (val & 0xFF000000) >> 24, p+0 );
pj_val_to_hex_digit( (val & 0x00FF0000) >> 16, p+2 );
pj_val_to_hex_digit( (val & 0x0000FF00) >> 8, p+4 );
pj_val_to_hex_digit( (val & 0x000000FF) >> 0, p+6 );
p += 8;
}
for (i=i * 8; i<len; ++i) {
*p++ = pj_hex_digits[ pj_rand() & 0x0F ];
}
return str;
}
示例2: create_stream
/*
* Create stream based on the codec, dir, remote address, etc.
*/
static pj_status_t create_stream( pj_pool_t *pool,
pjmedia_endpt *med_endpt,
const pjmedia_codec_info *codec_info,
pjmedia_dir dir,
pj_uint16_t local_port,
const pj_sockaddr_in *rem_addr,
pjmedia_stream **p_stream )
{
pjmedia_stream_info info;
pjmedia_transport *transport;
pj_status_t status;
/* Reset stream info. */
pj_bzero(&info, sizeof(info));
/* Initialize stream info formats */
info.type = PJMEDIA_TYPE_AUDIO;
info.dir = dir;
pj_memcpy(&info.fmt, codec_info, sizeof(pjmedia_codec_info));
info.tx_pt = codec_info->pt;
info.ssrc = pj_rand();
/* Copy remote address */
pj_memcpy(&info.rem_addr, rem_addr, sizeof(pj_sockaddr_in));
/* Create media transport */
status = pjmedia_transport_udp_create(med_endpt, NULL, local_port,
0, &transport);
if (status != PJ_SUCCESS)
return status;
/* Now that the stream info is initialized, we can create the
* stream.
*/
status = pjmedia_stream_create( med_endpt, pool, &info,
transport, NULL, p_stream);
if (status != PJ_SUCCESS) {
app_perror(THIS_FILE, "Error creating stream", status);
pjmedia_transport_udp_close(transport);
return status;
}
return PJ_SUCCESS;
}
示例3: plc_put_frame
static pj_status_t plc_put_frame( pjmedia_port *this_port,
const pjmedia_frame *frame)
{
pj_status_t status;
int i;
struct plc_port *plcp = (struct plc_port*)this_port;
PJ_ASSERT_RETURN(this_port->info.signature == SIGNATURE, PJ_EINVAL);
PJ_LOG(6, (THIS_FILE, "packet: sz=%d ts=%llu",
frame->size/sizeof(pj_uint16_t), frame->timestamp.u64));
if (frame->type == PJMEDIA_FRAME_TYPE_NONE ) {
em_plc_mode mode = plcp->frame.type == PJMEDIA_FRAME_TYPE_NONE ? \
EM_PLC_EMPTY : plcp->plc_mode;
switch (mode) {
case EM_PLC_SMART:
for (i=0; i<plcp->fpp; i++){
status = plcp->codec->op->recover(plcp->codec, BUF_SIZE, &plcp->frame);
plcp->frame.timestamp.u64 = 0;
if (status != PJ_SUCCESS) return status;
status = pjmedia_port_put_frame(plcp->dn_port, &plcp->frame);
if (status != PJ_SUCCESS) return status;
}
break;
case EM_PLC_REPEAT:
for (i=0; i<plcp->fpp; i++){
plcp->frame.timestamp.u64 = 0;
status = pjmedia_port_put_frame(plcp->dn_port, &plcp->frame);
if (status != PJ_SUCCESS) return status;
}
break;
case EM_PLC_NOISE:
for (i=0; i<plcp->fpp; i++){
int j=0;
plcp->frame.size = plcp->dn_port->info.bytes_per_frame;
plcp->frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
plcp->frame.timestamp.u64 = 0;
for (j=0; j<plcp->frame.size; j++)
((char*)plcp->frame.buf)[j] = ((char)pj_rand()) >> 5;
status = pjmedia_port_put_frame(plcp->dn_port, &plcp->frame);
if (status != PJ_SUCCESS) return status;
}
default:
for (i=0; i<plcp->fpp; i++){
plcp->frame.size = plcp->dn_port->info.bytes_per_frame;
plcp->frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
plcp->frame.timestamp.u64 = 0;
pj_bzero(plcp->frame.buf, plcp->frame.size);
status = pjmedia_port_put_frame(plcp->dn_port, &plcp->frame);
}
}
plcp->stats.lost++;
} else {
示例4: do_test
int do_test(void *user) {
int i;
pj_pool_t *pool = (pj_pool_t *)user;
opool_init(&opool, 10, strlen(s)+10, pool);
for (i = 0; i < 100; i++) {
opool_item_t *p_item = opool_get(&opool);
pj_memcpy(p_item->data, s, strlen(s));
printf("%d: %s\n", i, p_item->data);
pj_thread_sleep(pj_rand()%100);
opool_free(&opool, p_item);
}
return 0;
}
示例5: transport_send_rtp
/* Called by application to send RTP packet */
static pj_status_t transport_send_rtp( pjmedia_transport *tp,
const void *pkt,
pj_size_t size)
{
struct transport_udp *udp = (struct transport_udp*)tp;
pj_ssize_t sent;
unsigned id;
struct pending_write *pw;
pj_status_t status;
/* Must be attached */
PJ_ASSERT_RETURN(udp->attached, PJ_EINVALIDOP);
/* Check that the size is supported */
PJ_ASSERT_RETURN(size <= RTP_LEN, PJ_ETOOBIG);
/* Simulate packet lost on TX direction */
if (udp->tx_drop_pct) {
if ((pj_rand() % 100) <= (int)udp->tx_drop_pct) {
PJ_LOG(5,(udp->base.name,
"TX RTP packet dropped because of pkt lost "
"simulation"));
return PJ_SUCCESS;
}
}
id = udp->rtp_write_op_id;
pw = &udp->rtp_pending_write[id];
/* We need to copy packet to our buffer because when the
* operation is pending, caller might write something else
* to the original buffer.
*/
pj_memcpy(pw->buffer, pkt, size);
sent = size;
status = pj_ioqueue_sendto( udp->rtp_key,
&udp->rtp_pending_write[id].op_key,
pw->buffer, &sent, 0,
&udp->rem_rtp_addr,
sizeof(pj_sockaddr_in));
udp->rtp_write_op_id = (udp->rtp_write_op_id + 1) %
PJ_ARRAY_SIZE(udp->rtp_pending_write);
if (status==PJ_SUCCESS || status==PJ_EPENDING)
return PJ_SUCCESS;
return status;
}
示例6: transport_send_rtp
/* Called by application to send RTP packet */
static pj_status_t transport_send_rtp( pjmedia_transport *tp,
const void *pkt,
pj_size_t size)
{
struct transport_loop *loop = (struct transport_loop*)tp;
unsigned i;
/* Simulate packet lost on TX direction */
if (loop->tx_drop_pct) {
if ((pj_rand() % 100) <= (int)loop->tx_drop_pct) {
PJ_LOG(5,(loop->base.name,
"TX RTP packet dropped because of pkt lost "
"simulation"));
return PJ_SUCCESS;
}
}
/* Simulate packet lost on RX direction */
if (loop->rx_drop_pct) {
if ((pj_rand() % 100) <= (int)loop->rx_drop_pct) {
PJ_LOG(5,(loop->base.name,
"RX RTP packet dropped because of pkt lost "
"simulation"));
return PJ_SUCCESS;
}
}
/* Distribute to users */
for (i=0; i<loop->user_cnt; ++i) {
if (!loop->users[i].rx_disabled && loop->users[i].rtp_cb)
(*loop->users[i].rtp_cb)(loop->users[i].user_data, (void*)pkt,
size);
}
return PJ_SUCCESS;
}
示例7: rand_test
/*
* rand_test(), simply generates COUNT number of random number and
* check that there's no duplicate numbers.
*/
int rand_test(void)
{
int i;
for (i=0; i<COUNT; ++i) {
int j;
values[i] = pj_rand();
for (j=0; j<i; ++j) {
if (values[i] == values[j]) {
PJ_LOG(3,("test", "error: duplicate value %d at %d-th index",
values[i], i));
return -10;
}
}
}
return 0;
}
示例8: pjsip_messaging_create_session
pjsip_messaging_create_session( pjsip_endpoint *endpt, const pj_str_t *param_from,
const pj_str_t *param_to )
{
pj_pool_t *pool;
pjsip_messaging_session *ses;
pj_str_t tmp, to;
pool = pjsip_endpt_create_pool(endpt, "imsess", 1024, 1024);
if (!pool)
return NULL;
ses = pj_pool_calloc(pool, 1, sizeof(pjsip_messaging_session));
ses->pool = pool;
ses->endpt = endpt;
ses->call_id = pjsip_cid_hdr_create(pool);
pj_create_unique_string(pool, &ses->call_id->id);
ses->cseq = pjsip_cseq_hdr_create(pool);
ses->cseq->cseq = pj_rand();
ses->cseq->method = message_method;
ses->from = pjsip_from_hdr_create(pool);
pj_strdup_with_null(pool, &tmp, param_from);
ses->from->uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, PJSIP_PARSE_URI_AS_NAMEADDR);
if (ses->from->uri == NULL) {
pjsip_endpt_destroy_pool(endpt, pool);
return NULL;
}
pj_create_unique_string(pool, &ses->from->tag);
ses->to = pjsip_to_hdr_create(pool);
pj_strdup_with_null(pool, &to, param_from);
ses->to->uri = pjsip_parse_uri(pool, to.ptr, to.slen, PJSIP_PARSE_URI_AS_NAMEADDR);
if (ses->to->uri == NULL) {
pjsip_endpt_destroy_pool(endpt, pool);
return NULL;
}
PJ_LOG(4,(THIS_FILE, "IM session created: recipient=%s", to.ptr));
return ses;
}
示例9: test_timer_heap
static int test_timer_heap(void)
{
int i, j;
pj_timer_entry *entry;
pj_pool_t *pool;
pj_timer_heap_t *timer;
pj_time_val delay;
pj_status_t rc; int err=0;
unsigned size, count;
size = pj_timer_heap_mem_size(MAX_COUNT)+MAX_COUNT*sizeof(pj_timer_entry);
pool = pj_pool_create( mem, NULL, size, 4000, NULL);
if (!pool) {
PJ_LOG(3,("test", "...error: unable to create pool of %u bytes",
size));
return -10;
}
entry = (pj_timer_entry*)pj_pool_calloc(pool, MAX_COUNT, sizeof(*entry));
if (!entry)
return -20;
for (i=0; i<MAX_COUNT; ++i) {
entry[i].cb = &timer_callback;
}
rc = pj_timer_heap_create(pool, MAX_COUNT, &timer);
if (rc != PJ_SUCCESS) {
app_perror("...error: unable to create timer heap", rc);
return -30;
}
count = MIN_COUNT;
for (i=0; i<LOOP; ++i) {
int early = 0;
int done=0;
int cancelled=0;
int rc;
pj_timestamp t1, t2, t_sched, t_cancel, t_poll;
pj_time_val now, expire;
pj_gettimeofday(&now);
pj_srand(now.sec);
t_sched.u32.lo = t_cancel.u32.lo = t_poll.u32.lo = 0;
// Register timers
for (j=0; j<(int)count; ++j) {
delay.sec = pj_rand() % DELAY;
delay.msec = pj_rand() % 1000;
// Schedule timer
pj_get_timestamp(&t1);
rc = pj_timer_heap_schedule(timer, &entry[j], &delay);
if (rc != 0)
return -40;
pj_get_timestamp(&t2);
t_sched.u32.lo += (t2.u32.lo - t1.u32.lo);
// Poll timers.
pj_get_timestamp(&t1);
rc = pj_timer_heap_poll(timer, NULL);
pj_get_timestamp(&t2);
if (rc > 0) {
t_poll.u32.lo += (t2.u32.lo - t1.u32.lo);
early += rc;
}
}
// Set the time where all timers should finish
pj_gettimeofday(&expire);
delay.sec = DELAY;
delay.msec = 0;
PJ_TIME_VAL_ADD(expire, delay);
// Wait unfil all timers finish, cancel some of them.
do {
int index = pj_rand() % count;
pj_get_timestamp(&t1);
rc = pj_timer_heap_cancel(timer, &entry[index]);
pj_get_timestamp(&t2);
if (rc > 0) {
cancelled += rc;
t_cancel.u32.lo += (t2.u32.lo - t1.u32.lo);
}
pj_gettimeofday(&now);
pj_get_timestamp(&t1);
#if defined(PJ_SYMBIAN) && PJ_SYMBIAN!=0
/* On Symbian, we must use OS poll (Active Scheduler poll) since
* timer is implemented using Active Object.
*/
rc = 0;
while (pj_symbianos_poll(-1, 0))
++rc;
#else
rc = pj_timer_heap_poll(timer, NULL);
#endif
pj_get_timestamp(&t2);
if (rc > 0) {
//.........这里部分代码省略.........
示例10: perf_test
//.........这里部分代码省略.........
pj_sockaddr_cp(&listen_addr, &info.local_addr);
pj_sockaddr_print((pj_sockaddr_t*)&listen_addr, buf, sizeof(buf), 1);
PJ_LOG(3, ("", "...Listener ready at %s", buf));
}
/* CLIENTS */
clients_num = clients;
param.timeout.sec = 0;
param.timeout.msec = 0;
/* Init random seed */
{
pj_time_val now;
pj_gettimeofday(&now);
pj_srand((unsigned)now.sec);
}
/* Allocate SSL socket pointers and test state */
ssock_cli = pj_pool_calloc(pool, clients, sizeof(pj_ssl_sock_t*));
state_cli = pj_pool_calloc(pool, clients, sizeof(struct test_state));
/* Get start timestamp */
pj_gettimeofday(&start);
/* Setup clients */
for (i = 0; i < clients; ++i) {
param.user_data = &state_cli[i];
state_cli[i].pool = pool;
state_cli[i].check_echo = PJ_TRUE;
state_cli[i].send_str_len = (pj_rand() % 5 + 1) * 1024 + pj_rand() % 1024;
state_cli[i].send_str = pj_pool_alloc(pool, state_cli[i].send_str_len);
{
unsigned j;
for (j = 0; j < state_cli[i].send_str_len; ++j)
state_cli[i].send_str[j] = (char)(pj_rand() % 256);
}
status = pj_ssl_sock_create(pool, ¶m, &ssock_cli[i]);
if (status != PJ_SUCCESS) {
app_perror("...ERROR pj_ssl_sock_create()", status);
cli_err++;
clients_num--;
continue;
}
status = pj_ssl_sock_start_connect(ssock_cli[i], pool, &addr, &listen_addr, pj_sockaddr_get_len(&addr));
if (status == PJ_SUCCESS) {
ssl_on_connect_complete(ssock_cli[i], PJ_SUCCESS);
} else if (status == PJ_EPENDING) {
status = PJ_SUCCESS;
} else {
app_perror("...ERROR pj_ssl_sock_create()", status);
pj_ssl_sock_close(ssock_cli[i]);
ssock_cli[i] = NULL;
clients_num--;
cli_err++;
continue;
}
/* Give chance to server to accept this client */
{
unsigned n = 5;
示例11: stun_destroy_test
static int stun_destroy_test(void)
{
enum { LOOP = 500 };
struct stun_test_session test_sess;
pj_sockaddr bind_addr;
int addr_len;
pj_caching_pool cp;
pj_pool_t *pool;
unsigned i;
pj_status_t status;
int rc = 0;
PJ_LOG(3,(THIS_FILE, " STUN destroy concurrency test"));
pj_bzero(&test_sess, sizeof(test_sess));
pj_caching_pool_init(&cp, NULL, 0);
pool = pj_pool_create(&cp.factory, "testsess", 512, 512, NULL);
pj_stun_config_init(&test_sess.stun_cfg, &cp.factory, 0, NULL, NULL);
status = pj_timer_heap_create(pool, 1023, &test_sess.stun_cfg.timer_heap);
pj_assert(status == PJ_SUCCESS);
status = pj_lock_create_recursive_mutex(pool, NULL, &test_sess.lock);
pj_assert(status == PJ_SUCCESS);
pj_timer_heap_set_lock(test_sess.stun_cfg.timer_heap, test_sess.lock, PJ_TRUE);
pj_assert(status == PJ_SUCCESS);
status = pj_ioqueue_create(pool, 512, &test_sess.stun_cfg.ioqueue);
pj_assert(status == PJ_SUCCESS);
pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &test_sess.server_sock);
pj_sockaddr_init(pj_AF_INET(), &bind_addr, NULL, 0);
status = pj_sock_bind(test_sess.server_sock, &bind_addr, pj_sockaddr_get_len(&bind_addr));
pj_assert(status == PJ_SUCCESS);
addr_len = sizeof(bind_addr);
status = pj_sock_getsockname(test_sess.server_sock, &bind_addr, &addr_len);
pj_assert(status == PJ_SUCCESS);
test_sess.server_port = pj_sockaddr_get_port(&bind_addr);
status = pj_event_create(pool, NULL, PJ_TRUE, PJ_FALSE, &test_sess.server_event);
pj_assert(status == PJ_SUCCESS);
for (i=0; i<SERVER_THREAD_CNT; ++i) {
status = pj_thread_create(pool, NULL,
&server_thread_proc, &test_sess,
0, 0, &test_sess.server_threads[i]);
pj_assert(status == PJ_SUCCESS);
}
for (i=0; i<WORKER_THREAD_CNT; ++i) {
status = pj_thread_create(pool, NULL,
&worker_thread_proc, &test_sess,
0, 0, &test_sess.worker_threads[i]);
pj_assert(status == PJ_SUCCESS);
}
/* Test 1: Main thread calls destroy while callback is processing response */
PJ_LOG(3,(THIS_FILE, " Destroy in main thread while callback is running"));
for (i=0; i<LOOP; ++i) {
int sleep = pj_rand() % 5;
PJ_LOG(3,(THIS_FILE, " Try %-3d of %d", i+1, LOOP));
/* Test 1: destroy at the same time when receiving response */
pj_bzero(&test_sess.param, sizeof(test_sess.param));
test_sess.param.client_sleep_after_start = 20;
test_sess.param.client_sleep_before_destroy = sleep;
test_sess.param.server_wait_for_event = PJ_TRUE;
stun_destroy_test_session(&test_sess);
PJ_LOG(3,(THIS_FILE,
" stun test a: sleep delay:%d: clients with response: %d",
sleep, test_sess.param.client_got_response));
/* Test 2: destroy at the same time with STUN retransmit timer */
test_sess.param.server_drop_request = PJ_TRUE;
test_sess.param.client_sleep_after_start = 0;
test_sess.param.client_sleep_before_destroy = PJ_STUN_RTO_VALUE;
test_sess.param.server_wait_for_event = PJ_FALSE;
stun_destroy_test_session(&test_sess);
PJ_LOG(3,(THIS_FILE, " stun test b: retransmit concurrency"));
/* Test 3: destroy at the same time with receiving response
* AND STUN retransmit timer */
test_sess.param.client_got_response = 0;
test_sess.param.server_drop_request = PJ_FALSE;
test_sess.param.client_sleep_after_start = PJ_STUN_RTO_VALUE;
test_sess.param.client_sleep_before_destroy = 0;
test_sess.param.server_wait_for_event = PJ_TRUE;
stun_destroy_test_session(&test_sess);
PJ_LOG(3,(THIS_FILE,
" stun test c: clients with response: %d",
test_sess.param.client_got_response));
pj_thread_sleep(10);
//.........这里部分代码省略.........
示例12: PJ_DEF
PJ_DEF(pj_status_t) pjstun_get_mapped_addr( pj_pool_factory *pf,
int sock_cnt, pj_sock_t sock[],
const pj_str_t *srv1, int port1,
const pj_str_t *srv2, int port2,
pj_sockaddr_in mapped_addr[])
{
unsigned srv_cnt;
pj_sockaddr_in srv_addr[2];
int i, j, send_cnt = 0, nfds;
pj_pool_t *pool;
struct query_rec {
struct {
pj_uint32_t mapped_addr;
pj_uint32_t mapped_port;
} srv[2];
} *rec;
void *out_msg;
pj_size_t out_msg_len;
int wait_resp = 0;
pj_status_t status;
PJ_CHECK_STACK();
TRACE_((THIS_FILE, "Entering pjstun_get_mapped_addr()"));
/* Create pool. */
pool = pj_pool_create(pf, "stun%p", 400, 400, NULL);
if (!pool)
return PJ_ENOMEM;
/* Allocate client records */
rec = (struct query_rec*) pj_pool_calloc(pool, sock_cnt, sizeof(*rec));
if (!rec) {
status = PJ_ENOMEM;
goto on_error;
}
TRACE_((THIS_FILE, " Memory allocated."));
/* Create the outgoing BIND REQUEST message template */
status = pjstun_create_bind_req( pool, &out_msg, &out_msg_len,
pj_rand(), pj_rand());
if (status != PJ_SUCCESS)
goto on_error;
TRACE_((THIS_FILE, " Binding request created."));
/* Resolve servers. */
status = pj_sockaddr_in_init(&srv_addr[0], srv1, (pj_uint16_t)port1);
if (status != PJ_SUCCESS)
goto on_error;
srv_cnt = 1;
if (srv2 && port2) {
status = pj_sockaddr_in_init(&srv_addr[1], srv2, (pj_uint16_t)port2);
if (status != PJ_SUCCESS)
goto on_error;
if (srv_addr[1].sin_addr.s_addr != srv_addr[0].sin_addr.s_addr &&
srv_addr[1].sin_port != srv_addr[0].sin_port)
{
srv_cnt++;
}
}
TRACE_((THIS_FILE, " Server initialized, using %d server(s)", srv_cnt));
/* Init mapped addresses to zero */
pj_memset(mapped_addr, 0, sock_cnt * sizeof(pj_sockaddr_in));
/* We need these many responses */
wait_resp = sock_cnt * srv_cnt;
TRACE_((THIS_FILE, " Done initialization."));
#if defined(PJ_SELECT_NEEDS_NFDS) && PJ_SELECT_NEEDS_NFDS!=0
nfds = -1;
for (i=0; i<sock_cnt; ++i) {
if (sock[i] > nfds) {
nfds = sock[i];
}
}
#else
nfds = FD_SETSIZE-1;
#endif
/* Main retransmission loop. */
for (send_cnt=0; send_cnt<MAX_REQUEST; ++send_cnt) {
pj_time_val next_tx, now;
pj_fd_set_t r;
int select_rc;
PJ_FD_ZERO(&r);
/* Send messages to servers that has not given us response. */
for (i=0; i<sock_cnt && status==PJ_SUCCESS; ++i) {
for (j=0; j<srv_cnt && status==PJ_SUCCESS; ++j) {
pjstun_msg_hdr *msg_hdr = (pjstun_msg_hdr*) out_msg;
//.........这里部分代码省略.........
示例13: timestamp_test
int timestamp_test(void)
{
enum { CONSECUTIVE_LOOP = 100 };
volatile unsigned i;
pj_timestamp freq, t1, t2;
pj_time_val tv1, tv2;
unsigned elapsed;
pj_status_t rc;
PJ_LOG(3,(THIS_FILE, "...Testing timestamp (high res time)"));
/* Get and display timestamp frequency. */
if ((rc=pj_get_timestamp_freq(&freq)) != PJ_SUCCESS) {
app_perror("...ERROR: get timestamp freq", rc);
return -1000;
}
PJ_LOG(3,(THIS_FILE, "....frequency: hiword=%lu loword=%lu",
freq.u32.hi, freq.u32.lo));
PJ_LOG(3,(THIS_FILE, "...checking if time can run backwards (pls wait).."));
/*
* Check if consecutive readings should yield timestamp value
* that is bigger than previous value.
* First we get the first timestamp.
*/
rc = pj_get_timestamp(&t1);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR: pj_get_timestamp", rc);
return -1001;
}
rc = pj_gettimeofday(&tv1);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR: pj_gettimeofday", rc);
return -1002;
}
for (i=0; i<CONSECUTIVE_LOOP; ++i) {
pj_thread_sleep(pj_rand() % 100);
rc = pj_get_timestamp(&t2);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR: pj_get_timestamp", rc);
return -1003;
}
rc = pj_gettimeofday(&tv2);
if (rc != PJ_SUCCESS) {
app_perror("...ERROR: pj_gettimeofday", rc);
return -1004;
}
/* compare t2 with t1, expecting t2 >= t1. */
if (t2.u32.hi < t1.u32.hi ||
(t2.u32.hi == t1.u32.hi && t2.u32.lo < t1.u32.lo))
{
PJ_LOG(3,(THIS_FILE, "...ERROR: timestamp run backwards!"));
return -1005;
}
/* compare tv2 with tv1, expecting tv2 >= tv1. */
if (PJ_TIME_VAL_LT(tv2, tv1)) {
PJ_LOG(3,(THIS_FILE, "...ERROR: time run backwards!"));
return -1006;
}
}
/*
* Simple test to time some loop.
*/
PJ_LOG(3,(THIS_FILE, "....testing simple 1000000 loop"));
/* Mark start time. */
if ((rc=pj_get_timestamp(&t1)) != PJ_SUCCESS) {
app_perror("....error: cat't get timestamp", rc);
return -1010;
}
/* Loop.. */
for (i=0; i<1000000; ++i) {
/* Try to do something so that smart compilers wont
* remove this silly loop.
*/
null_func();
}
pj_thread_sleep(0);
/* Mark end time. */
pj_get_timestamp(&t2);
/* Get elapsed time in usec. */
elapsed = pj_elapsed_usec(&t1, &t2);
PJ_LOG(3,(THIS_FILE, "....elapsed: %u usec", (unsigned)elapsed));
/* See if elapsed time is "reasonable".
* This should be good even on 50Mhz embedded powerpc.
*/
if (elapsed < 1 || elapsed > 1000000) {
//.........这里部分代码省略.........
示例14: on_rx_rtp
/* Notification from ioqueue about incoming RTP packet */
static void on_rx_rtp( pj_ioqueue_key_t *key,
pj_ioqueue_op_key_t *op_key,
pj_ssize_t bytes_read)
{
struct transport_udp *udp;
pj_status_t status;
PJ_UNUSED_ARG(op_key);
udp = pj_ioqueue_get_user_data(key);
do {
void (*cb)(void*,const void*,pj_ssize_t);
void *user_data;
cb = udp->rtp_cb;
user_data = udp->user_data;
/* Simulate packet lost on RX direction */
if (udp->rx_drop_pct) {
if ((pj_rand() % 100) <= (int)udp->rx_drop_pct) {
PJ_LOG(5,(udp->base.name,
"RX RTP packet dropped because of pkt lost "
"simulation"));
goto read_next_packet;
}
}
if (udp->attached && cb)
(*cb)(user_data, udp->rtp_pkt, bytes_read);
/* See if source address of RTP packet is different than the
* configured address, and switch RTP remote address to
* source packet address after several consecutive packets
* have been received.
*/
if (bytes_read>0 &&
(udp->options & PJMEDIA_UDP_NO_SRC_ADDR_CHECKING)==0)
{
if ((udp->rem_rtp_addr.sin_addr.s_addr !=
udp->rtp_src_addr.sin_addr.s_addr) ||
(udp->rem_rtp_addr.sin_port !=
udp->rtp_src_addr.sin_port))
{
udp->rtp_src_cnt++;
if (udp->rtp_src_cnt >= PJMEDIA_RTP_NAT_PROBATION_CNT) {
/* Set remote RTP address to source address */
udp->rem_rtp_addr = udp->rtp_src_addr;
/* Reset counter */
udp->rtp_src_cnt = 0;
PJ_LOG(4,(udp->base.name,
"Remote RTP address switched to %s:%d",
pj_inet_ntoa(udp->rtp_src_addr.sin_addr),
pj_ntohs(udp->rtp_src_addr.sin_port)));
/* Also update remote RTCP address if actual RTCP source
* address is not heard yet.
*/
if (udp->rtcp_src_addr.sin_addr.s_addr == 0) {
pj_uint16_t port;
pj_memcpy(&udp->rem_rtcp_addr, &udp->rem_rtp_addr,
sizeof(pj_sockaddr_in));
port = (pj_uint16_t)
(pj_ntohs(udp->rem_rtp_addr.sin_port)+1);
udp->rem_rtcp_addr.sin_port = pj_htons(port);
pj_memcpy(&udp->rtcp_src_addr, &udp->rem_rtcp_addr,
sizeof(pj_sockaddr_in));
PJ_LOG(4,(udp->base.name,
"Remote RTCP address switched to %s:%d",
pj_inet_ntoa(udp->rtcp_src_addr.sin_addr),
pj_ntohs(udp->rtcp_src_addr.sin_port)));
}
}
}
}
read_next_packet:
bytes_read = sizeof(udp->rtp_pkt);
udp->rtp_addrlen = sizeof(pj_sockaddr_in);
status = pj_ioqueue_recvfrom(udp->rtp_key, &udp->rtp_read_op,
udp->rtp_pkt, &bytes_read, 0,
&udp->rtp_src_addr,
&udp->rtp_addrlen);
if (status != PJ_EPENDING && status != PJ_SUCCESS)
bytes_read = -status;
} while (status != PJ_EPENDING);
}
示例15: create_stream
/*
* Create stream based on the codec, dir, remote address, etc.
*/
static pj_status_t create_stream( pj_pool_t *pool,
pjmedia_endpt *med_endpt,
const pjmedia_vid_codec_info *codec_info,
pjmedia_vid_codec_param *codec_param,
pjmedia_dir dir,
pj_int8_t rx_pt,
pj_int8_t tx_pt,
pj_uint16_t local_port,
const pj_sockaddr_in *rem_addr,
#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
pj_bool_t use_srtp,
const pj_str_t *crypto_suite,
const pj_str_t *srtp_tx_key,
const pj_str_t *srtp_rx_key,
#endif
pjmedia_vid_stream **p_stream )
{
pjmedia_vid_stream_info info;
pjmedia_transport *transport = NULL;
pj_status_t status;
#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
pjmedia_transport *srtp_tp = NULL;
#endif
/* Reset stream info. */
pj_bzero(&info, sizeof(info));
/* Initialize stream info formats */
info.type = PJMEDIA_TYPE_VIDEO;
info.dir = dir;
info.codec_info = *codec_info;
info.tx_pt = (tx_pt == -1)? codec_info->pt : tx_pt;
info.rx_pt = (rx_pt == -1)? codec_info->pt : rx_pt;
info.ssrc = pj_rand();
if (codec_param)
info.codec_param = codec_param;
/* Copy remote address */
pj_memcpy(&info.rem_addr, rem_addr, sizeof(pj_sockaddr_in));
/* If remote address is not set, set to an arbitrary address
* (otherwise stream will assert).
*/
if (info.rem_addr.addr.sa_family == 0) {
const pj_str_t addr = pj_str("127.0.0.1");
pj_sockaddr_in_init(&info.rem_addr.ipv4, &addr, 0);
}
/* Create media transport */
status = pjmedia_transport_udp_create(med_endpt, NULL, local_port,
0, &transport);
if (status != PJ_SUCCESS)
return status;
#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
/* Check if SRTP enabled */
if (use_srtp) {
pjmedia_srtp_crypto tx_plc, rx_plc;
status = pjmedia_transport_srtp_create(med_endpt, transport,
NULL, &srtp_tp);
if (status != PJ_SUCCESS)
return status;
pj_bzero(&tx_plc, sizeof(pjmedia_srtp_crypto));
pj_bzero(&rx_plc, sizeof(pjmedia_srtp_crypto));
tx_plc.key = *srtp_tx_key;
tx_plc.name = *crypto_suite;
rx_plc.key = *srtp_rx_key;
rx_plc.name = *crypto_suite;
status = pjmedia_transport_srtp_start(srtp_tp, &tx_plc, &rx_plc);
if (status != PJ_SUCCESS)
return status;
transport = srtp_tp;
}
#endif
/* Now that the stream info is initialized, we can create the
* stream.
*/
status = pjmedia_vid_stream_create( med_endpt, pool, &info,
transport,
NULL, p_stream);
if (status != PJ_SUCCESS) {
app_perror(THIS_FILE, "Error creating stream", status);
pjmedia_transport_close(transport);
return status;
}
return PJ_SUCCESS;
}