From 2dd0fe09ceb1675de340d5d4ff84251b3697b435 Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Wed, 14 Oct 2009 01:58:04 +0000 Subject: Ticket #970: More gracefull PJSUA-LIB shutdown sequence. Enhancements: - wait for unregistration to complete (or a preconfigured delay expires) - new account config field to set the maximum delay to wait for unregistration - rejects incoming requests (INVITE, SUBSCRIBE, and OPTIONS) when shutdown is in progress git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@2943 74dad513-b988-da41-8d7b-12977e46ad98 --- pjsip/include/pjsua-lib/pjsua.h | 19 ++++++++++++ pjsip/src/pjsua-lib/pjsua_call.c | 8 +++++ pjsip/src/pjsua-lib/pjsua_core.c | 64 +++++++++++++++++++++++++++------------ pjsip/src/pjsua-lib/pjsua_media.c | 2 ++ pjsip/src/pjsua-lib/pjsua_pres.c | 10 ++++++ 5 files changed, 83 insertions(+), 20 deletions(-) (limited to 'pjsip') diff --git a/pjsip/include/pjsua-lib/pjsua.h b/pjsip/include/pjsua-lib/pjsua.h index d6db65b9..c0eb8b25 100644 --- a/pjsip/include/pjsua-lib/pjsua.h +++ b/pjsip/include/pjsua-lib/pjsua.h @@ -1724,6 +1724,17 @@ PJ_DECL(pj_status_t) pjsua_transport_close( pjsua_transport_id id, #endif +/** + * Default maximum time to wait for account unregistration transactions to + * complete during library shutdown sequence. + * + * Default: 4000 (4 seconds) + */ +#ifndef PJSUA_UNREG_TIMEOUT +# define PJSUA_UNREG_TIMEOUT 4000 +#endif + + /** * Default PUBLISH expiration */ @@ -1919,6 +1930,14 @@ typedef struct pjsua_acc_config */ unsigned reg_timeout; + /** + * Specify the maximum time to wait for unregistration requests to + * complete during library shutdown sequence. + * + * Default: PJSUA_UNREG_TIMEOUT + */ + unsigned unreg_timeout; + /** * Number of credentials in the credential array. */ diff --git a/pjsip/src/pjsua-lib/pjsua_call.c b/pjsip/src/pjsua-lib/pjsua_call.c index c57873f6..a845940c 100644 --- a/pjsip/src/pjsua-lib/pjsua_call.c +++ b/pjsip/src/pjsua-lib/pjsua_call.c @@ -645,6 +645,14 @@ pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata) if (dlg || tsx) return PJ_FALSE; + /* Don't want to accept the call if shutdown is in progress */ + if (pjsua_var.thread_quit_flag) { + pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, + PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL, + NULL, NULL); + return PJ_TRUE; + } + PJSUA_LOCK(); /* Find free call slot. */ diff --git a/pjsip/src/pjsua-lib/pjsua_core.c b/pjsip/src/pjsua-lib/pjsua_core.c index 39ad80d8..0b6f48c7 100644 --- a/pjsip/src/pjsua-lib/pjsua_core.c +++ b/pjsip/src/pjsua-lib/pjsua_core.c @@ -161,6 +161,7 @@ PJ_DEF(void) pjsua_acc_config_default(pjsua_acc_config *cfg) pj_bzero(cfg, sizeof(*cfg)); cfg->reg_timeout = PJSUA_REG_INTERVAL; + cfg->unreg_timeout = PJSUA_UNREG_TIMEOUT; pjsip_publishc_opt_default(&cfg->publish_opt); cfg->unpublish_max_wait_time_msec = PJSUA_UNPUBLISH_MAX_WAIT_TIME_MSEC; cfg->transport_id = PJSUA_INVALID_ID; @@ -305,6 +306,14 @@ static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata) return PJ_FALSE; } + /* Don't want to handle if shutdown is in progress */ + if (pjsua_var.thread_quit_flag) { + pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, + PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL, + NULL, NULL); + return PJ_TRUE; + } + /* Create basic response. */ status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL, &tdata); @@ -1231,6 +1240,8 @@ PJ_DEF(pj_status_t) pjsua_destroy(void) if (pjsua_var.endpt) { unsigned max_wait; + PJ_LOG(4,(THIS_FILE, "Shutting down...")); + /* Terminate all calls. */ pjsua_call_hangup_all(); @@ -1245,6 +1256,9 @@ PJ_DEF(pj_status_t) pjsua_destroy(void) /* Terminate all presence subscriptions. */ pjsua_pres_shutdown(); + /* Destroy media (to shutdown media transports etc) */ + pjsua_media_subsys_destroy(); + /* Wait for sometime until all publish client sessions are done * (ticket #364) */ @@ -1290,10 +1304,6 @@ PJ_DEF(pj_status_t) pjsua_destroy(void) pjsua_acc_set_registration(i, PJ_FALSE); } } - } - - /* Destroy endpoint. */ - if (pjsua_var.endpt) { /* Terminate any pending STUN resolution */ if (!pj_list_empty(&pjsua_var.stun_res)) { @@ -1305,23 +1315,40 @@ PJ_DEF(pj_status_t) pjsua_destroy(void) } } - /* Wait for some time to allow unregistration and ICE/TURN - * transports shutdown to complete: - */ - PJ_LOG(4,(THIS_FILE, "Shutting down...")); - busy_sleep(1000); + /* Wait until all unregistrations are done (ticket #364) */ + /* First stage, get the maximum wait time */ + max_wait = 100; + for (i=0; i max_wait) + max_wait = pjsua_var.acc[i].cfg.unreg_timeout; + } + + /* Second stage, wait for unregistrations to complete */ + for (i=0; i<(int)(max_wait/50); ++i) { + unsigned j; + for (j=0; j