Newer
Older
* Author : Laurielle Lea <laurielle.lea@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <errno.h>
#include <time.h>
#include <cc++/socket.h> // why do I need this here?
#include <ccrtp/channel.h> // why do I need this here?
#include <ccrtp/rtp.h> // why do I need this here?
#include "user_cfg.h"
#include "gui/guiframework.h"
#define fill_config_str(name, value) \
(_config.addConfigTreeItem(section, Conf::ConfigTreeItem(std::string(name), std::string(value), type_str)))
#define fill_config_int(name, value) \
(_config.addConfigTreeItem(section, Conf::ConfigTreeItem(std::string(name), std::string(value), type_int)))
_hasTriedToRegister = false;
// initialize random generator for call id
srand (time(NULL));
#ifdef USE_ZEROCONF
delete _DNSService; _DNSService = NULL;
#endif
if (_exist == 0) {
_debug("Cannot create config file in your home directory\n");
}
_debugInit("Load Telephone Tone");
std::string country = getConfigString(PREFERENCES, ZONE_TONE);
_telephoneTone = new TelephoneTone(country);
catch (const portaudio::PaException &e) {
getAudioDriver()->setErrorMessage(e.paErrorText());
}
catch (const portaudio::PaCppException &e) {
getAudioDriver()->setErrorMessage(e.what());
}
catch (const std::runtime_error &e) {
getAudioDriver()->setErrorMessage(e.what());
}
catch (...) {
displayError("An unknown exception occured while selecting audio driver.");
// initRegisterVoIP was here, but we doing it after the gui loaded...
// the stun detection is long, so it's a better idea to do it after getEvents
for(VoIPLinkVector::iterator pos = _voIPLinkVector.begin();
pos != _voIPLinkVector.end();
pos++) {
delete *pos;
for(CallVector::iterator pos = _callVector.begin();
pos != _callVector.end();
pos++) {
_debug("Unload Telephone Tone\n");
delete _telephoneTone; _telephoneTone = 0;
}
void
ManagerImpl::setGui (GuiFramework* gui)
{
_gui = gui;
}
Call* call = new Call(id, type, _voIPLinkVector.at(DFT_VOIP_LINK));
// Set the wanted voip-link (first of the list)
_callVector.push_back(call);
return call;
for (unsigned int i = 0; i < size; i++) {
call = _callVector.at(i);
if (call && call->getId() == id) {
break;
} else {
call = NULL;
CallVector::iterator iter = _callVector.begin();
while(iter!=_callVector.end()) {
if (call->getFlagNotAnswered() && call->isIncomingType() && call->getState() != Call::NotExist) {
//_debug("%10d: Setting current callid, old one was: %d\n", id, _currentCallId);
void
ManagerImpl::removeCallFromCurrent(CALLID id)
{
if ( _currentCallId == id ) {
//_debug("%10d: Setting current callid, old one was: %d\n", 0, _currentCallId);
///////////////////////////////////////////////////////////////////////////////
// Management of events' IP-phone user
///////////////////////////////////////////////////////////////////////////////
if (call->outgoingCall(to) == 0) {
return id;
} else {
return 0;
}
int result = -1;
if (call->getState() != Call::Error) {
result = call->hangup();
// current call id or no line selected
if (id == _currentCallId || _currentCallId == 0) {
ost::MutexLock m(_mutex);
Call* call = getCall(id);
if (call == NULL) {
return -1;
ost::MutexLock m(_mutex);
Call* call = getCall(id);
if (call == NULL) {
return -1;
}
if (call->getState() != Call::OnHold) {
switchCall(id);
}
stopTone(); // before answer, don't stop the audio stream after open it
ost::MutexLock m(_mutex);
Call* call = getCall(id);
if (call == NULL) {
return -1;
}
return -1;
}
if (call->getState() == Call::OffHold) {
return 1;
}
try {
getAudioDriver()->startStream();
} catch(...) {
_debugException("Off hold could not start audio stream");
}
if ( _mic_volume == 0 ) {
setMicVolume(_mic_volume_before_mute);
}
ost::MutexLock m(_mutex);
Call *call = getCall(id);
if (call == NULL) {
return -1;
setConfig(AUDIO, VOLUME_SPKR, getSpkrVolume());
setConfig(AUDIO, VOLUME_MICRO, getMicVolume());
_setupLoaded = _config.saveConfigTree(_path.data());
return _setupLoaded;
if ( _voIPLinkVector.at(DFT_VOIP_LINK)->init() ) {
// we call here, because it's long...
// If network is available and exosip is start..
if (getConfigInt(SIGNALISATION, AUTO_REGISTER) && _exist == 1) {
registerVoIPLink();
int returnValue = false;
if (_voIPLinkVector.at(DFT_VOIP_LINK)->setRegister() >= 0) {
returnValue = true;
_registerState = REGISTERED;
} else {
_registerState = FAILED;
}
_voIPLinkVector.at(DFT_VOIP_LINK)->carryingDTMFdigits(id, code);
returnValue = true;
break;
case 1: // Audio way
break;
case 2: // rfc 2833
break;
default: // unknown - error config?
break;
}
return returnValue;
// length in milliseconds
int pulselen = getConfigInt(SIGNALISATION, PULSE_LENGTH);
if (!pulselen) { return false; }
// numbers of int = length in milliseconds / 1000 (number of seconds)
// = number of seconds * SAMPLING_RATE by SECONDS
int size = pulselen * (SAMPLING_RATE/1000);
// this buffer is for mono
int16* _buf = new int16[size];
// allocation of more space, for stereo conversion
int16* buf_ctrl_vol = new int16[size*CHANNELS];
// Control volume and format mono->stereo
for (int j = 0; j < size; j++) {
// put the size in bytes...
// so size * CHANNELS * 2 (bytes for the int16)
int nbInt16InChar = sizeof(int16)/sizeof(char);
try {
// We activate the stream if it's not active yet.
if (!audiolayer->isStreamActive()) {
audiolayer->startStream();
} else {
audiolayer->sleep(pulselen); // in milliseconds
}
} catch(...) {
_debugException("Portaudio exception when playing a dtmf");
///////////////////////////////////////////////////////////////////////////////
// Management of event peer IP-phone
////////////////////////////////////////////////////////////////////////////////
/**
* Multi-thread
*/
ManagerImpl::incomingCallWaiting() {
ost::MutexLock m(_incomingCallMutex);
return (_nbIncomingWaitingCall > 0) ? true : false;
}
void
ManagerImpl::incWaitingCall() {
ost::MutexLock m(_incomingCallMutex);
_nbIncomingWaitingCall++;
void
ManagerImpl::decWaitingCall() {
ost::MutexLock m(_incomingCallMutex);
_nbIncomingWaitingCall--;
void
ManagerImpl::callSetInfo(CALLID id, const std::string& name, const std::string& number)
{
bool returnValue = false;
ost::MutexLock m(_mutex);
Call* call = getCall(id);
if (call != NULL && call->getState() != Call::Progressing) {
returnValue = true;
}
return returnValue;
}
/**
* SipEvent Thread
* ask if it can answer the call
*/
bool
bool returnValue = false;
ost::MutexLock m(_mutex);
Call* call = getCall(id);
(call->getState()!=Call::OnHold && call->getState()!=Call::OffHold) )) {
returnValue = true;
}
return returnValue;
}
/**
* SipEvent Thread
* ask if it can start the sound thread
*/
bool
ManagerImpl::callIsOnHold(CALLID id) {
bool returnValue = false;
ost::MutexLock m(_mutex);
Call* call = getCall(id);
if (call != NULL && (call->getState()==Call::OnHold)) {
ManagerImpl::incomingCall (CALLID id, const std::string& name, const std::string& number)
ost::MutexLock m(_mutex);
Call* call = getCall(id);
if (call == NULL) {
return -1;
}
call->setType(Incoming);
call->setState(Call::Progressing);
if ( _currentCallId == 0 ) {
call->setFlagNotAnswered(false);
ringtone();
std::string from = name; call->setCallerIdName(name);
call->setCallerIdNumber(number);
if ( !number.empty() ) {
from.append(" <");
from.append(number);
from.append(">");
}
return _gui->incomingCall(id, accountId, from);
/**
* SipEvent Thread
* for outgoing message, send by SipEvent
*/
void
ManagerImpl::incomingMessage(const std::string& message) {
if (_gui) {
_gui->incomingMessage(message);
}
}
stopTone();
// switch current call
switchCall(id);
if (_gui) _gui->peerAnsweredCall(id);
}
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
/**
* Multi Thread
*/
void
ManagerImpl::callBusy(CALLID id) {
_debug("%10d: Call is busy\n", id);
playATone(Tone::TONE_BUSY);
ost::MutexLock m(_mutex);
Call* call = getCall(id);
if (call != 0) {
call->setState(Call::Busy);
}
deleteCall(id);
call->setState(Call::Hungup);
removeCallFromCurrent(id);
}
/**
* Multi Thread
*/
void
ManagerImpl::callFailure(CALLID id) {
_debug("%10d: Call failed\n", id);
playATone(Tone::TONE_BUSY);
_mutex.enterMutex();
Call* call = getCall(id);
if (call != 0) {
call->setState(Call::Error);
}
_mutex.leaveMutex();
if (_gui) {
_gui->callFailure(id);
}
deleteCall(id);
call->setState(Call::Hungup);
removeCallFromCurrent(id);
}
/**
* SipEvent Thread
* for outgoing call, send by SipEvent
*/
void
ManagerImpl::displayConfigError (const std::string& message)
{
if(_gui) {
_gui->displayConfigError(message);
}
}
void
ManagerImpl::stopVoiceMessageNotification (void)
{
/**
* SipEvent Thread
*/
void
ManagerImpl::registrationSucceed()
{
if (_gui) _gui->sendRegistrationState(true);
}
/**
* SipEvent Thread
*/
void
ManagerImpl::registrationFailed()
{
if (_gui) _gui->sendRegistrationState(false);
}
try {
getAudioDriver()->startStream();
} catch(...) {
_debugException("Off hold could not start audio stream");
}
try {
getAudioDriver()->stopStream();
} catch(...) {
_debugException("Stop tone and stop stream");
}
_toneMutex.enterMutex();
_telephoneTone->setCurrentTone(Tone::TONE_NULL);