From f6e88e0c3564207609925acae24798c84dbceb2b Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Mon, 6 Mar 2006 16:25:59 +0000 Subject: Added --uas-duration and --uas-refresh option (the later is broken) git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@305 74dad513-b988-da41-8d7b-12977e46ad98 --- pjsip/include/pjsua-lib/pjsua.h | 4 ++ pjsip/src/pjsua-lib/pjsua_call.c | 89 ++++++++++++++++++++++++++++++++++++ pjsip/src/pjsua-lib/pjsua_core.c | 9 +++- pjsip/src/pjsua-lib/pjsua_settings.c | 36 ++++++++++++++- 4 files changed, 136 insertions(+), 2 deletions(-) diff --git a/pjsip/include/pjsua-lib/pjsua.h b/pjsip/include/pjsua-lib/pjsua.h index a2424063..95d1398c 100644 --- a/pjsip/include/pjsua-lib/pjsua.h +++ b/pjsip/include/pjsua-lib/pjsua.h @@ -100,6 +100,8 @@ struct pjsua_call pjmedia_sock_info skinfo; /**< Preallocated media sockets. */ void *app_data; /**< Application data. */ + pj_timer_entry refresh_tm;/**< Timer to send re-INVITE. */ + pj_timer_entry hangup_tm; /**< Timer to hangup call. */ }; typedef struct pjsua_call pjsua_call; @@ -196,6 +198,8 @@ struct pjsua /* User Agent behaviour: */ int auto_answer; /**< Automatically answer in calls. */ + int uas_refresh; /**< Time to re-INVITE. */ + int uas_duration; /**< Max call duration. */ /* Account: */ pj_bool_t has_acc; /**< Any --id cmdline? */ diff --git a/pjsip/src/pjsua-lib/pjsua_call.c b/pjsip/src/pjsua-lib/pjsua_call.c index b245a491..16823e4d 100644 --- a/pjsip/src/pjsua-lib/pjsua_call.c +++ b/pjsip/src/pjsua-lib/pjsua_call.c @@ -29,6 +29,74 @@ #define THIS_FILE "pjsua_inv.c" +#define REFRESH_CALL_TIMER 0x63 +#define HANGUP_CALL_TIMER 0x64 + +/* Proto */ +static void schedule_call_timer( pjsua_call *call, pj_timer_entry *e, + int timer_type, int duration ); + +/* + * Timer callback when UAS needs to send re-INVITE to see if remote + * is still there. + */ +static void call_on_timer(pj_timer_heap_t *ht, pj_timer_entry *e) +{ + pjsua_call *call = e->user_data; + + PJ_UNUSED_ARG(ht); + + if (e->id == REFRESH_CALL_TIMER) { + + /* If call is still not connected, hangup. */ + if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) { + PJ_LOG(3,(THIS_FILE, "Refresh call timer is called when " + "invite is still not confirmed. Call %d will " + "disconnect.", call->index)); + pjsua_call_hangup(call->index); + } else { + PJ_LOG(3,(THIS_FILE, "Refreshing call %d", call->index)); + schedule_call_timer(call,e,REFRESH_CALL_TIMER,pjsua.uas_refresh); + pjsua_call_reinvite(call->index); + } + + } else if (e->id == HANGUP_CALL_TIMER) { + PJ_LOG(3,(THIS_FILE, "Call %d duration exceeded, disconnecting call", + call->index)); + pjsua_call_hangup(call->index); + + } +} + +/* + * Schedule call timer. + */ +static void schedule_call_timer( pjsua_call *call, pj_timer_entry *e, + int timer_type, int duration ) +{ + pj_time_val timeout; + + if (duration == 0) { + /* Cancel timer. */ + if (e->id != 0) { + pjsip_endpt_cancel_timer(pjsua.endpt, e); + e->id = 0; + } + + } else { + /* Schedule timer. */ + timeout.sec = duration; + timeout.msec = 0; + + e->cb = &call_on_timer; + e->id = timer_type; + e->user_data = call; + + pjsip_endpt_schedule_timer( pjsua.endpt, e, &timeout); + } +} + + /** * Make outgoing call. */ @@ -334,6 +402,22 @@ pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata) ++pjsua.call_cnt; + /* Schedule timer to refresh. */ + if (pjsua.uas_refresh > 0) { + schedule_call_timer( &pjsua.calls[call_index], + &pjsua.calls[call_index].refresh_tm, + REFRESH_CALL_TIMER, + pjsua.uas_refresh); + } + + /* Schedule timer to hangup call. */ + if (pjsua.uas_duration > 0) { + schedule_call_timer( &pjsua.calls[call_index], + &pjsua.calls[call_index].hangup_tm, + HANGUP_CALL_TIMER, + pjsua.uas_duration); + } + /* This INVITE request has been handled. */ return PJ_TRUE; } @@ -418,6 +502,11 @@ static void pjsua_call_on_state_changed(pjsip_inv_session *inv, PJ_LOG(3,(THIS_FILE,"Media session is destroyed")); } + /* Remove timers. */ + schedule_call_timer(call, &call->refresh_tm, REFRESH_CALL_TIMER, 0); + schedule_call_timer(call, &call->hangup_tm, HANGUP_CALL_TIMER, 0); + + /* Free call */ call->inv = NULL; --pjsua.call_cnt; } diff --git a/pjsip/src/pjsua-lib/pjsua_core.c b/pjsip/src/pjsua-lib/pjsua_core.c index aaf5601a..36b6692e 100644 --- a/pjsip/src/pjsua-lib/pjsua_core.c +++ b/pjsip/src/pjsua-lib/pjsua_core.c @@ -69,6 +69,10 @@ void pjsua_default(void) PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE; + /* Default call settings. */ + pjsua.uas_refresh = -1; + pjsua.uas_duration = -1; + /* Default: do not use STUN: */ pjsua.stun_port1 = pjsua.stun_port2 = 0; @@ -90,8 +94,11 @@ void pjsua_default(void) } /* Init call array: */ - for (i=0; i0)")); + return -1; + } + break; + + case OPT_UAS_DURATION: + pjsua.uas_duration = my_atoi(pj_optarg); + if (pjsua.uas_duration < 1) { + PJ_LOG(1,(THIS_FILE,"Invalid value for --uas-duration (must be >0)")); + return -1; + } + break; } } @@ -915,6 +936,19 @@ int pjsua_dump_settings(char *buf, pj_size_t max) pjsua.max_calls); pj_strcat2(&cfg, line); + /* Uas-refresh. */ + if (pjsua.uas_refresh > 0) { + pj_ansi_sprintf(line, "--uas-refresh %d\n", + pjsua.uas_refresh); + pj_strcat2(&cfg, line); + } + + /* Uas-duration. */ + if (pjsua.uas_duration > 0) { + pj_ansi_sprintf(line, "--uas-duration %d\n", + pjsua.uas_duration); + pj_strcat2(&cfg, line); + } pj_strcat2(&cfg, "#\n# Buddies:\n#\n"); -- cgit v1.2.3