Commit 48e1e942 authored by Emmanuel Milou's avatar Emmanuel Milou
Browse files

Terminate/Init pjsip for STUN

parent 2dcb9e7c
......@@ -96,6 +96,8 @@ class Account{
*/
inline VoIPLink* getVoIPLink() { return _link; }
inline void setVoIPLink (VoIPLink *link) { _link = link; }
/**
* Register the underlying VoIPLink. Launch the event listener.
* This should update the getRegistrationState() return value.
......
......@@ -1409,9 +1409,14 @@ int ManagerImpl::isStunEnabled (void)
void ManagerImpl::enableStun (void)
{
( getConfigInt( SIGNALISATION , STUN_ENABLE ) == STUN_ENABLED )? setConfig(SIGNALISATION , STUN_ENABLE , NO_STR ) : setConfig( SIGNALISATION , STUN_ENABLE , YES_STR );
/* Update the config */
( getConfigInt( SIGNALISATION , STUN_ENABLE ) == STUN_ENABLED )? setConfig(SIGNALISATION , STUN_ENABLE , NO_STR ) : setConfig( SIGNALISATION , STUN_ENABLE , YES_STR );
/* Restart PJSIP */
this->restartPJSIP ();
}
int
ManagerImpl::getVolumeControls( void )
{
......@@ -1946,6 +1951,7 @@ void ManagerImpl::setAccountDetails( const std::string& accountID, const std::ma
setConfig(accountID, CONFIG_ACCOUNT_MAILBOX,(*details.find(CONFIG_ACCOUNT_MAILBOX)).second);
// SIP SPECIFIC
/*
if (accountType == "SIP") {
link = Manager::instance().getAccountLink( accountID );
......@@ -1967,8 +1973,8 @@ void ManagerImpl::setAccountDetails( const std::string& accountID, const std::ma
{
link->setStunServer("");
}
//restartPjsip();
}
restartPJSIP();
}*/
saveConfig();
......@@ -2233,6 +2239,26 @@ AccountMap ManagerImpl::getSipAccountMap( void )
return sipaccounts;
}
void ManagerImpl::restartPJSIP (void)
{
SIPVoIPLink *siplink;
/* First unregister all SIP accounts */
this->unregisterCurSIPAccounts();
/* Terminate and initialize the PJSIP library */
siplink = dynamic_cast<SIPVoIPLink*> (getSIPAccountLink ());
if (siplink)
{
siplink->terminate ();
_debug ("*************************************************Terminate done\n");
//siplink = SIPVoIPLink::instance("");
siplink->init ();
}
_debug("***************************************************Init Done\n");
/* Then register all enabled SIP accounts */
//this->registerCurSIPAccounts(siplink);
}
VoIPLink* ManagerImpl::getAccountLink(const AccountID& accountID)
{
Account* acc = getAccount(accountID);
......@@ -2242,6 +2268,23 @@ VoIPLink* ManagerImpl::getAccountLink(const AccountID& accountID)
return 0;
}
VoIPLink* ManagerImpl::getSIPAccountLink()
{
/* We are looking for the first SIP account we met because all the SIP accounts have the same voiplink */
Account *account;
AccountMap::iterator iter;
for(iter = _accountMap.begin(); iter != _accountMap.end(); ++iter) {
account = iter->second;
if( account->getType() == "sip" ){
return account->getVoIPLink();
}
}
return NULL;
}
pjsip_regc
*getSipRegcFromID(const AccountID& id UNUSED)
{
......@@ -2254,32 +2297,35 @@ pjsip_regc
void ManagerImpl::unregisterCurSIPAccounts()
{
AccountMap::iterator iter = _accountMap.begin();
while( iter != _accountMap.end() ) {
if ( iter->second) {
std::string p = Manager::instance().getConfigString( iter->first , CONFIG_ACCOUNT_TYPE );
if ( iter->second->isEnabled() && p == "SIP") {
// NOW
iter->second->unregisterVoIPLink();
}
}
Account *current;
AccountMap::iterator iter = _accountMap.begin();
while( iter != _accountMap.end() ) {
current = iter->second;
if (current) {
if ( current->isEnabled() && current->getType() == "sip") {
current->unregisterVoIPLink();
}
}
iter++;
}
}
}
void ManagerImpl::registerCurSIPAccounts()
void ManagerImpl::registerCurSIPAccounts(VoIPLink *link)
{
AccountMap::iterator iter = _accountMap.begin();
while( iter != _accountMap.end() ) {
if ( iter->second) {
std::string p = Manager::instance().getConfigString( iter->first , CONFIG_ACCOUNT_TYPE );
if ( iter->second->isEnabled() && p == "SIP") {
// NOW
iter->second->registerVoIPLink();
}
}
Account *current;
AccountMap::iterator iter = _accountMap.begin();
while( iter != _accountMap.end() ) {
current = iter->second;
if (current) {
if ( current->isEnabled() && current->getType() == "sip") {
current->setVoIPLink(link);
current->registerVoIPLink();
}
}
iter++;
}
}
}
#ifdef TEST
......
......@@ -789,11 +789,11 @@ class ManagerImpl {
* @param void
* @return void
*/
void restartPjsip();
void restartPJSIP( );
void unregisterCurSIPAccounts();
void registerCurSIPAccounts();
void registerCurSIPAccounts(VoIPLink *link);
/**
* Returns a map with only the existing SIP accounts
......@@ -1014,6 +1014,8 @@ public:
*/
VoIPLink* getAccountLink(const AccountID& accountID);
VoIPLink* getSIPAccountLink (void);
AccountID getAccountIdFromNameAndServer(const std::string& userName, const std::string& server);
int getSipPort();
......
......@@ -152,9 +152,6 @@ SIPVoIPLink* SIPVoIPLink::_instance = NULL;
// to get random number for RANDOM_PORT
srand (time(NULL));
/* Instanciate the C++ thread */
_evThread = new EventThread(this);
/* Start pjsip initialization step */
init();
}
......@@ -187,6 +184,10 @@ bool SIPVoIPLink::init()
{
if(initDone())
return false;
/* Instanciate the C++ thread */
_evThread = new EventThread(this);
/* Initialize the pjsip library */
pjsip_init();
initDone(true);
......@@ -197,7 +198,9 @@ bool SIPVoIPLink::init()
void
SIPVoIPLink::terminate()
{
delete _evThread; _evThread = NULL;
if (_evThread){
delete _evThread; _evThread = NULL;
}
/* Clean shutdown of pjsip library */
if( initDone() )
......@@ -228,6 +231,7 @@ SIPVoIPLink::terminateSIPCall()
void
SIPVoIPLink::getEvent()
{
_debug("a");
// We have to register the external thread so it could access the pjsip framework
if(!pj_thread_is_registered())
pj_thread_register( NULL, desc, &thread );
......@@ -235,6 +239,7 @@ SIPVoIPLink::getEvent()
// PJSIP polling
pj_time_val timeout = {0, 10};
pjsip_endpt_handle_events( _endpt, &timeout);
}
int SIPVoIPLink::sendRegister( AccountID id )
......@@ -1287,8 +1292,39 @@ std::string SIPVoIPLink::getSipTo(const std::string& to_url, std::string hostnam
return returnValue;
}
void SIPVoIPLink::busy_sleep(unsigned msec)
{
#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
/* Ideally we shouldn't call pj_thread_sleep() and rather
* CActiveScheduler::WaitForAnyRequest() here, but that will
* drag in Symbian header and it doesn't look pretty.
*/
pj_thread_sleep(msec);
#else
pj_time_val timeout, now, tv;
pj_gettimeofday(&timeout);
timeout.msec += msec;
pj_time_val_normalize(&timeout);
tv.sec = 0;
tv.msec = 10;
pj_time_val_normalize(&tv);
do {
pjsip_endpt_handle_events(_endpt, &tv);
pj_gettimeofday(&now);
} while (PJ_TIME_VAL_LT(now, timeout));
#endif
}
bool SIPVoIPLink::pjsip_shutdown( void )
{
/*if (_endpt) {
_debug("UserAgent: Shutting down...\n");
busy_sleep(1000);
}*/
/* Destroy endpoint. */
if (_endpt) {
pjsip_endpt_destroy(_endpt);
......
......@@ -284,6 +284,7 @@ class SIPVoIPLink : public VoIPLink
/* The singleton instance */
static SIPVoIPLink* _instance;
void busy_sleep(unsigned msec);
int getModId();
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment