这次我们来说一下pjsip中的定时器timer.
pjsip中的定时器来自于ACE库。( The timer scheduling implementation here is based on ACE library's
ACE_Timer_Heap, with only little modification to suit our library's style ) http://www.cs.wustl.edu/~schmidt/ACE.html
如何使用
1. 创建timer
pj_timer_heap_t *timer ;
pj_timer_heap_create(pool , 1 , &timer ) ;
2. 设置entry 及回调函数。
pj_timer_entry *entry ;
static void timer_callback(pj_timer_heap_t *ht , pj_timer_entry *en)
{
PJ_LOG(3 , ("timer" , "timercallback")) ;
}
entry = (pj_timer_entry*)pj_pool_calloc(pool , 1 , sizeof(*entry)) ;
entry->cb = &timer_callback ;
3.启动定时器。
pj_time_val delay ;
delay.sec = 1 ;
delay.msec = 0 ;
pj_timer_heap_schedule(timer , &entry, &delay) ;
4. 当然你需要不停的去poll这个timer(和ioqueue一样).
另起一个线程
while(!quit_flag)
{
pj_timer_heap_poll(timer , NULL ) ;
PJ_LOG(3,("test" , "printf")) ;
}
#include "pjlib.h"
pj_timer_heap_t *timer ;
int quit_flag = 0 ;
static void timer_callback(pj_timer_heap_t *ht , pj_timer_entry *en)
{
pj_time_val delay ;
PJ_LOG(3 , ("timer" , "timercallback")) ;
delay.sec = 1 ;
delay.msec = 0 ;
pj_timer_heap_schedule(ht , en , &delay) ;
}
static int main_thread_fun(void *arg)
{
while(!quit_flag)
{
pj_time_val timeout = {0 , 500 };
pj_timer_heap_poll(timer , NULL ) ;
PJ_LOG(3,("test" , "printf")) ;
}
return 0 ;
}
int main(){
pj_status_t status ;
pj_caching_pool cp ;
pj_pool_t *pool ;
pj_timer_entry *entry ;
pj_time_val delay ;
pj_thread_t * thread ;
status = pj_init();
pj_caching_pool_init(&cp , NULL , 0 ) ;
pool = pj_pool_create(&cp.factory , NULL , 512 , 512 , NULL ) ;
entry = (pj_timer_entry*)pj_pool_calloc(pool , 1 , sizeof(*entry)) ;
status = pj_timer_heap_create(pool , 1 , &timer ) ;
entry[0].cb = &timer_callback ;
delay.sec = 1 ;
delay.msec = 0 ;
pj_timer_heap_schedule(timer , &entry[0] , &delay) ;
pj_thread_create(pool , NULL , main_thread_fun , NULL , 0 , 0 , &thread) ;
while(!quit_flag)
{
pj_thread_sleep(1000) ;
}
pj_shutdown();
return 0 ;
}