managerimpl.cpp 78.2 KB
Newer Older
jpbl's avatar
jpbl committed
1
/*
2
3
 *  Copyright (C) 2004-2007 Savoir-Faire Linux inc.
 *  Author: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
jpbl's avatar
jpbl committed
4
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
5
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
6
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
7
 *  Author: Guillaume Carmel-Archambault <guillaume.carmel-archambault@savoirfairelinux.com>
8
 *
jpbl's avatar
jpbl committed
9
10
 *  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
11
 *  the Free Software Foundation; either version 3 of the License, or
jpbl's avatar
jpbl committed
12
 *  (at your option) any later version.
13
 *
jpbl's avatar
jpbl committed
14
15
16
17
 *  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.
18
 *
jpbl's avatar
jpbl committed
19
20
21
22
23
24
25
26
27
28
 *  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 <cstdlib>
#include <iostream>
#include <fstream>
29
#include <sstream>
jpbl's avatar
jpbl committed
30
31
32
33
34
35
36
37
38
#include <sys/types.h> // mkdir(2)
#include <sys/stat.h>	// mkdir(2)

#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 <cc++/file.h>

#include "manager.h"
39
#include "account.h"
40
#include "sipaccount.h"
jpbl's avatar
jpbl committed
41
#include "audio/audiolayer.h"
42
43
#include "audio/alsalayer.h"
#include "audio/pulselayer.h"
jpbl's avatar
jpbl committed
44
45
#include "audio/tonelist.h"

yanmorin's avatar
   
yanmorin committed
46
#include "accountcreator.h" // create new account
47
48
#include "sipvoiplink.h"

jpbl's avatar
jpbl committed
49
50
51
52
53
54
55
#include "user_cfg.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)))

56
ManagerImpl::ManagerImpl (void)
Yun Liu's avatar
Yun Liu committed
57
58
59
	: _hasTriedToRegister(false)
        , _config()
	, _currentCallId2()
60
        , _currentCallMutex()
Yun Liu's avatar
Yun Liu committed
61
62
63
64
        , _codecBuilder(NULL)
        , _audiodriver(NULL)
        , _dtmfKey(NULL)
        , _codecDescriptorMap()
65
        , _toneMutex()
Yun Liu's avatar
Yun Liu committed
66
67
68
69
        , _telephoneTone(NULL)
        , _audiofile()
        , _spkr_volume(0)
        , _mic_volume(0)
70
        , _mutex()
Yun Liu's avatar
Yun Liu committed
71
72
	, _dbus(NULL)
        , _waitingCall()
73
        , _waitingCallMutex()
Yun Liu's avatar
Yun Liu committed
74
75
76
77
78
79
80
81
        , _nbIncomingWaitingCall(0)
        , _path("")
        , _exist(0)
        , _setupLoaded(false)
        , _firewallPort()
        , _firewallAddr("")
        , _hasZeroconf(false)
        , _callAccountMap()
82
        , _callAccountMapMutex()
Emmanuel Milou's avatar
Emmanuel Milou committed
83
        , _callConfigMap()
Yun Liu's avatar
Yun Liu committed
84
        , _accountMap()
85
        , _cleaner (NULL)
86
        , _history (NULL)
jpbl's avatar
jpbl committed
87
{
88

89
90
    // initialize random generator for call id
    srand (time(NULL));
yanmorin's avatar
   
yanmorin committed
91

92
    _cleaner = new NumberCleaner ();
93
    _history = new HistoryManager ();
94

yanmorin's avatar
   
yanmorin committed
95
96
97
98
99
100
101
102
#ifdef TEST
  testAccountMap();
  loadAccountMap();
  testCallAccountMap();
  unloadAccountMap();
#endif

  // should be call before initConfigFile
103
  // loadAccountMap();, called in init() now.
jpbl's avatar
jpbl committed
104
105
106
}

// never call if we use only the singleton...
107
ManagerImpl::~ManagerImpl (void)
jpbl's avatar
jpbl committed
108
{
109
    // terminate();
110
    delete _cleaner; _cleaner=0;
111
    _debug("%s stop correctly.\n", PROGNAME);
jpbl's avatar
jpbl committed
112
113
}

114
115
  void
ManagerImpl::init()
jpbl's avatar
jpbl committed
116
{
117
118
    // Load accounts, init map
    loadAccountMap();
119

120
    initVolume();
121

122
123
124
    if (_exist == 0) {
        _debug("Cannot create config file in your home directory\n");
    }
125

126
127
    initAudioDriver();
    selectAudioDriver();
jpbl's avatar
jpbl committed
128

129
130
    // Initialize the list of supported audio codecs
    initAudioCodec();
131

132
    AudioLayer *audiolayer = getAudioDriver();
133

134
    if (audiolayer != 0) {
135
        unsigned int sampleRate = audiolayer->getSampleRate();
jpbl's avatar
jpbl committed
136

137
138
139
        _debugInit("Load Telephone Tone");
        std::string country = getConfigString(PREFERENCES, ZONE_TONE);
        _telephoneTone = new TelephoneTone(country, sampleRate);
jpbl's avatar
jpbl committed
140

141
142
143
        _debugInit("Loading DTMF key");
        _dtmfKey = new DTMF(sampleRate);
    }
144
145

    if (audiolayer == 0)
146
        audiolayer->stopStream();
147
148
149


    // Load the history
150
    _history->load_history (getConfigInt (PREFERENCES, CONFIG_HISTORY_LIMIT));
jpbl's avatar
jpbl committed
151
152
153
154
}

void ManagerImpl::terminate()
{
155
    _debug("ManagerImpl::terminate \n");
156
    saveConfig();
jpbl's avatar
jpbl committed
157

158
    unloadAccountMap();
159

160
    _debug("Unload DTMF Key \n");
161
    delete _dtmfKey;
jpbl's avatar
jpbl committed
162

163
    _debug("Unload Audio Driver \n");
164
    delete _audiodriver; _audiodriver = NULL;
jpbl's avatar
jpbl committed
165

166
    _debug("Unload Telephone Tone \n");
167
    delete _telephoneTone; _telephoneTone = NULL;
168

169
    _debug("Unload Audio Codecs \n");
170
    _codecDescriptorMap.deleteHandlePointer();
171

jpbl's avatar
jpbl committed
172
173
}

yanmorin's avatar
   
yanmorin committed
174
175
176
bool
ManagerImpl::isCurrentCall(const CallID& callId) {
  return (_currentCallId2 == callId ? true : false);
jpbl's avatar
jpbl committed
177
178
}

yanmorin's avatar
   
yanmorin committed
179
180
bool
ManagerImpl::hasCurrentCall() {
Emmanuel Milou's avatar
Bug fix    
Emmanuel Milou committed
181
  _debug("Current call ID = %s\n", _currentCallId2.c_str());
yanmorin's avatar
   
yanmorin committed
182
183
  if ( _currentCallId2 != "") {
    return true;
jpbl's avatar
jpbl committed
184
  }
yanmorin's avatar
   
yanmorin committed
185
  return false;
jpbl's avatar
jpbl committed
186
187
}

188
const CallID&
yanmorin's avatar
   
yanmorin committed
189
190
ManagerImpl::getCurrentCallId() {
  return _currentCallId2;
jpbl's avatar
jpbl committed
191
192
193
}

void
yanmorin's avatar
   
yanmorin committed
194
ManagerImpl::switchCall(const CallID& id ) {
195
  ost::MutexLock m(_currentCallMutex);
yanmorin's avatar
   
yanmorin committed
196
  _currentCallId2 = id;
jpbl's avatar
jpbl committed
197
198
199
200
201
202
}


///////////////////////////////////////////////////////////////////////////////
// Management of events' IP-phone user
///////////////////////////////////////////////////////////////////////////////
203
/* Main Thread */
Emmanuel Milou's avatar
Emmanuel Milou committed
204

205
  bool
yanmorin's avatar
   
yanmorin committed
206
207
ManagerImpl::outgoingCall(const std::string& accountid, const CallID& id, const std::string& to)
{
208
    std::string pattern, to_cleaned;
Emmanuel Milou's avatar
Emmanuel Milou committed
209
210
    Call::CallConfiguration callConfig;
    SIPVoIPLink *siplink;
211

212
213
    _debug("ManagerImpl::outgoingCall() method \n");

214
215
216
217
218
    if (getConfigString (HOOKS, PHONE_NUMBER_HOOK_ENABLED) ==  "1") 
        _cleaner->set_phone_number_prefix (getConfigString (HOOKS, PHONE_NUMBER_HOOK_ADD_PREFIX ));
    else
        _cleaner->set_phone_number_prefix ("");
    to_cleaned = _cleaner->clean (to);
Emmanuel Milou's avatar
Emmanuel Milou committed
219
220

    /* Check what kind of call we are dealing with */
221
    check_call_configuration (id, to_cleaned, &callConfig);
222

Emmanuel Milou's avatar
Emmanuel Milou committed
223
224
225
    if (callConfig == Call::IPtoIP) {
        _debug ("Start IP to IP call\n");
        /* We need to retrieve the sip voiplink instance */
226
        siplink = SIPVoIPLink::instance("");
227
        if (siplink->new_ip_to_ip_call (id, to_cleaned)) {
Emmanuel Milou's avatar
Emmanuel Milou committed
228
229
230
231
232
233
234
            switchCall (id);
            return true;
        }
        else {
            callFailure (id);
        }
        return false;
235
    }
236

Emmanuel Milou's avatar
Emmanuel Milou committed
237
238
239
240
    if (!accountExists(accountid)) {
        _debug("! Manager Error: Outgoing Call: account doesn't exist\n");
        return false;
    }
241

Emmanuel Milou's avatar
Emmanuel Milou committed
242
243
244
245
    if (getAccountFromCall(id) != AccountNULL) {
        _debug("! Manager Error: Outgoing Call: call id already exists\n");
        return false;
    }
246

Emmanuel Milou's avatar
Emmanuel Milou committed
247
248
249
250
    if (hasCurrentCall()) {
        _debug("* Manager Info: there is currently a call, try to hold it\n");
        onHoldCall(getCurrentCallId());
    }
251

Emmanuel Milou's avatar
Emmanuel Milou committed
252
    _debug("- Manager Action: Adding Outgoing Call %s on account %s\n", id.data(), accountid.data());
yanmorin's avatar
   
yanmorin committed
253
    associateCallToAccount( id, accountid );
254
    if ( getAccountLink(accountid)->newOutgoingCall(id, to_cleaned) ) {
Emmanuel Milou's avatar
Emmanuel Milou committed
255
256
257
258
259
260
261
        switchCall(id);
        return true;
    } else {
        callFailure(id);
        _debug("! Manager Error: An error occur, the call was not created\n");
    }
    return false;
jpbl's avatar
jpbl committed
262
263
}

yanmorin's avatar
   
yanmorin committed
264
//THREAD=Main : for outgoing Call
265
  bool
yanmorin's avatar
   
yanmorin committed
266
ManagerImpl::answerCall(const CallID& id)
jpbl's avatar
jpbl committed
267
{
268
269
  bool isActive = false;

270
271
  stopTone(true);

272
273
274
275
276
277
  AccountID currentaccountid = getAccountFromCall( id );
  Call* currentcall = getAccountLink(currentaccountid)->getCall(getCurrentCallId());
  _debug("ManagerImpl::answerCall :: current call->getState %i \n",currentcall->getState());

  if (currentcall->getState() == 1)
      isActive = true;
278

279
  // stopTone(false);
280
  _debug("Try to answer call: %s\n", id.data());
yanmorin's avatar
   
yanmorin committed
281
282
283
  AccountID accountid = getAccountFromCall( id );
  if (accountid == AccountNULL) {
    _debug("Answering Call: Call doesn't exists\n");
284
    //return false;
jpbl's avatar
jpbl committed
285
  }
286

287
  //  if (id != getCurrentCallId()) {
288
  if (isActive) {
Yun Liu's avatar
Yun Liu committed
289
    _debug("* Manager Info: there is currently a call, try to hold it\n");
290

Yun Liu's avatar
Yun Liu committed
291
292
293
    onHoldCall(getCurrentCallId());
  }

yanmorin's avatar
   
yanmorin committed
294
295
296
297
  if (!getAccountLink(accountid)->answer(id)) {
    // error when receiving...
    removeCallAccount(id);
    return false;
jpbl's avatar
jpbl committed
298
  }
Alexandre Savard's avatar
Alexandre Savard committed
299

yanmorin's avatar
   
yanmorin committed
300
  // if it was waiting, it's waiting no more
301
  if (_dbus) _dbus->getCallManager()->callStateChanged(id, "CURRENT");
yanmorin's avatar
   
yanmorin committed
302
303
  removeWaitingCall(id);
  switchCall(id);
304

305
  // std::string codecName = getCurrentCodecName(id);
Emmanuel Milou's avatar
Emmanuel Milou committed
306
  // _debug("ManagerImpl::hangupCall(): broadcast codec name %s \n",codecName.c_str());
307
  // if (_dbus) _dbus->getCallManager()->currentSelectedCodec(id,codecName.c_str());
308

yanmorin's avatar
   
yanmorin committed
309
  return true;
jpbl's avatar
jpbl committed
310
311
}

yanmorin's avatar
   
yanmorin committed
312
//THREAD=Main
313
  bool
yanmorin's avatar
   
yanmorin committed
314
ManagerImpl::hangupCall(const CallID& id)
jpbl's avatar
jpbl committed
315
{
316
    _debug("ManagerImpl::hangupCall()\n");
317
318
319
    PulseLayer *pulselayer;
    AccountID accountid;
    bool returnValue;
320
    AudioLayer *audiolayer;
Emmanuel Milou's avatar
Emmanuel Milou committed
321

322
    stopTone(false);
yanmorin's avatar
   
yanmorin committed
323

324
325
    /* Broadcast a signal over DBus */
    if (_dbus) _dbus->getCallManager()->callStateChanged(id, "HUNGUP");
326

327
328
    _debug("Stop audio stream\n");
    audiolayer = getAudioDriver();
329
330
331
332
333
334
335
336

    int nbCalls = getCallList().size();

    _debug("nbCalls %i \n", nbCalls);

    // stop stream 
    if (!(nbCalls > 1))
        audiolayer->stopStream();
337

Emmanuel Milou's avatar
Emmanuel Milou committed
338
339
    /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
340
        returnValue = SIPVoIPLink::instance (AccountNULL)->hangup (id);
341
    }
Emmanuel Milou's avatar
Emmanuel Milou committed
342
    /* Classic call, attached to an account */
343
    else {
Emmanuel Milou's avatar
Emmanuel Milou committed
344
345
346
347
348
349
350
351
        accountid = getAccountFromCall( id );
        if (accountid == AccountNULL) {
            _debug("! Manager Hangup Call: Call doesn't exists\n");
            return false;
        }
        returnValue = getAccountLink(accountid)->hangup(id);
        removeCallAccount(id);
    }
352

353
    switchCall("");
354

355
356
357
358
    if( _audiodriver->getLayerType() == PULSEAUDIO && getConfigInt( PREFERENCES , CONFIG_PA_VOLUME_CTRL ) ) {
        pulselayer = dynamic_cast<PulseLayer *> (getAudioDriver());
        if(pulselayer)  pulselayer->restorePulseAppsVolume();
    }
359

360
361
    

362
    return returnValue;
jpbl's avatar
jpbl committed
363
364
}

yanmorin's avatar
   
yanmorin committed
365
//THREAD=Main
366
  bool
yanmorin's avatar
   
yanmorin committed
367
ManagerImpl::cancelCall (const CallID& id)
jpbl's avatar
jpbl committed
368
{
Emmanuel Milou's avatar
Emmanuel Milou committed
369
370
    AccountID accountid;
    bool returnValue;
yanmorin's avatar
   
yanmorin committed
371

Emmanuel Milou's avatar
Emmanuel Milou committed
372
    stopTone(true);
373

Emmanuel Milou's avatar
Emmanuel Milou committed
374
375
    /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
376
        returnValue = SIPVoIPLink::instance (AccountNULL)->cancel (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
377
378
379
    }

    /* Classic call, attached to an account */
380
    else {
Emmanuel Milou's avatar
Emmanuel Milou committed
381
382
383
384
385
386
387
388
        accountid = getAccountFromCall( id );
        if (accountid == AccountNULL) {
            _debug("! Manager Cancel Call: Call doesn't exists\n");
            return false;
        }
        returnValue = getAccountLink(accountid)->cancel(id);
        removeCallAccount(id);
    }
389

Emmanuel Milou's avatar
Emmanuel Milou committed
390
391
392
393
394
    // it could be a waiting call?
    removeWaitingCall(id);
    switchCall("");

    return returnValue;
jpbl's avatar
jpbl committed
395
396
}

yanmorin's avatar
   
yanmorin committed
397
//THREAD=Main
398
  bool
yanmorin's avatar
   
yanmorin committed
399
ManagerImpl::onHoldCall(const CallID& id)
jpbl's avatar
jpbl committed
400
{
Emmanuel Milou's avatar
Emmanuel Milou committed
401
402
    AccountID accountid;
    bool returnValue;
403
    CallID call_id;
yanmorin's avatar
   
yanmorin committed
404

Emmanuel Milou's avatar
Emmanuel Milou committed
405
    stopTone(true);
406

407
408
    call_id = id;

Emmanuel Milou's avatar
Emmanuel Milou committed
409
410
    /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
411
        returnValue = SIPVoIPLink::instance (AccountNULL)-> onhold (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
412
    }
413

Emmanuel Milou's avatar
Emmanuel Milou committed
414
    /* Classic call, attached to an account */
415
    else {
Emmanuel Milou's avatar
Emmanuel Milou committed
416
417
418
419
420
421
422
        accountid = getAccountFromCall( id );
        if (accountid == AccountNULL) {
            _debug("Manager On Hold Call: Account ID %s or callid %s doesn't exists\n", accountid.c_str(), id.c_str());
            return false;
        }
        returnValue = getAccountLink(accountid)->onhold(id);
    }
423

Emmanuel Milou's avatar
Emmanuel Milou committed
424
425
    removeWaitingCall(id);
    switchCall("");
426

427
    if (_dbus) _dbus->getCallManager()->callStateChanged(call_id, "HOLD");
428

Emmanuel Milou's avatar
Emmanuel Milou committed
429
    return returnValue;
yanmorin's avatar
   
yanmorin committed
430
431
432
}

//THREAD=Main
433
  bool
yanmorin's avatar
   
yanmorin committed
434
435
ManagerImpl::offHoldCall(const CallID& id)
{
436

Emmanuel Milou's avatar
Emmanuel Milou committed
437
438
439
    AccountID accountid;
    bool returnValue, rec;
    std::string codecName;
440
    CallID call_id;
441

Emmanuel Milou's avatar
Emmanuel Milou committed
442
    stopTone(false);
443

444
    call_id = id;
Emmanuel Milou's avatar
Emmanuel Milou committed
445
    //Place current call on hold if it isn't
446
447
    if (hasCurrentCall())
    {
448
        _debug ("Put the current call (ID=%s) on hold\n", getCurrentCallId().c_str());
Emmanuel Milou's avatar
Emmanuel Milou committed
449
450
        onHoldCall(getCurrentCallId());
    }
451

Emmanuel Milou's avatar
Emmanuel Milou committed
452
453
454
    /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
        rec = SIPVoIPLink::instance (AccountNULL)-> isRecording (id);
455
        returnValue = SIPVoIPLink::instance (AccountNULL)-> offhold (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
456
    }
alexandresavard's avatar
alexandresavard committed
457

Emmanuel Milou's avatar
Emmanuel Milou committed
458
    /* Classic call, attached to an account */
459
    else {
Emmanuel Milou's avatar
Emmanuel Milou committed
460
461
462
463
464
465
466
467
468
        accountid = getAccountFromCall( id );
        if (accountid == AccountNULL) {
            _debug("Manager OffHold Call: Call doesn't exists\n");
            return false;
        }
        _debug("Setting OFFHOLD, Account %s, callid %s\n", accountid.c_str(), id.c_str());
        rec = getAccountLink(accountid)->isRecording(id);
        returnValue = getAccountLink(accountid)->offhold(id);
    }
alexandresavard's avatar
alexandresavard committed
469

470

471
    if (_dbus){
Emmanuel Milou's avatar
Emmanuel Milou committed
472
        if (rec)
473
            _dbus->getCallManager()->callStateChanged(call_id, "UNHOLD_RECORD");
474
        else
475
            _dbus->getCallManager()->callStateChanged(call_id, "UNHOLD_CURRENT");
476

Emmanuel Milou's avatar
Emmanuel Milou committed
477
    }
478

Emmanuel Milou's avatar
Emmanuel Milou committed
479
    switchCall(id);
480

481
    codecName = getCurrentCodecName(id);
482
    // _debug("ManagerImpl::hangupCall(): broadcast codec name %s \n",codecName.c_str());
483
    if (_dbus) _dbus->getCallManager()->currentSelectedCodec(id,codecName.c_str());
484

Emmanuel Milou's avatar
Emmanuel Milou committed
485
    return returnValue;
jpbl's avatar
jpbl committed
486
487
}

yanmorin's avatar
   
yanmorin committed
488
//THREAD=Main
489
  bool
yanmorin's avatar
   
yanmorin committed
490
ManagerImpl::transferCall(const CallID& id, const std::string& to)
jpbl's avatar
jpbl committed
491
{
Emmanuel Milou's avatar
Emmanuel Milou committed
492
493
    AccountID accountid;
    bool returnValue;
494

Emmanuel Milou's avatar
Emmanuel Milou committed
495
    stopTone(true);
496

Emmanuel Milou's avatar
Emmanuel Milou committed
497
498
    /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
499
        returnValue = SIPVoIPLink::instance (AccountNULL)-> transfer (id, to);
Emmanuel Milou's avatar
Emmanuel Milou committed
500
501
502
    }

    /* Classic call, attached to an account */
503
    else {
Emmanuel Milou's avatar
Emmanuel Milou committed
504
505
506
507
508
509
510
511
        accountid = getAccountFromCall( id );
        if (accountid == AccountNULL) {
            _debug("! Manager Transfer Call: Call doesn't exists\n");
            return false;
        }
        returnValue = getAccountLink(accountid)->transfer(id, to);
        removeCallAccount(id);
    }
512

Emmanuel Milou's avatar
Emmanuel Milou committed
513
514
    removeWaitingCall(id);
    switchCall("");
515

Emmanuel Milou's avatar
Emmanuel Milou committed
516
517
    if (_dbus) _dbus->getCallManager()->callStateChanged(id, "HUNGUP");
    return returnValue;
jpbl's avatar
jpbl committed
518
519
}

520
521
522
523
524
525
526
527
528
529
530
531
void ManagerImpl::transferFailed()
{
  if(_dbus) _dbus->getCallManager()->transferFailed();
}

void ManagerImpl::transferSucceded()
{
  if(_dbus) _dbus->getCallManager()->transferSucceded();

}


yanmorin's avatar
   
yanmorin committed
532
//THREAD=Main : Call:Incoming
533
  bool
yanmorin's avatar
   
yanmorin committed
534
ManagerImpl::refuseCall (const CallID& id)
jpbl's avatar
jpbl committed
535
{
Emmanuel Milou's avatar
Emmanuel Milou committed
536
537
    AccountID accountid;
    bool returnValue;
538

Emmanuel Milou's avatar
Emmanuel Milou committed
539
540
541
542
    stopTone(true);

     /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
543
        returnValue = SIPVoIPLink::instance (AccountNULL)-> refuse (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
544
545
546
    }

    /* Classic call, attached to an account */
547
    else {
Emmanuel Milou's avatar
Emmanuel Milou committed
548
549
550
551
552
553
554
        accountid = getAccountFromCall( id );
        if (accountid == AccountNULL) {
            _debug("! Manager OffHold Call: Call doesn't exists\n");
            return false;
        }
        returnValue = getAccountLink(accountid)->refuse(id);
        removeCallAccount(id);
555
556
    }

Emmanuel Milou's avatar
Emmanuel Milou committed
557
558
559
560
561
562
563
564
    // if the call was outgoing or established, we didn't refuse it
    // so the method did nothing
    if (returnValue) {
        removeWaitingCall(id);
        if (_dbus) _dbus->getCallManager()->callStateChanged(id, "HUNGUP");
        switchCall("");
    }
    return returnValue;
jpbl's avatar
jpbl committed
565
566
}

yanmorin's avatar
   
yanmorin committed
567
//THREAD=Main
568
  bool
jpbl's avatar
jpbl committed
569
570
571
572
573
574
575
576
577
578
ManagerImpl::saveConfig (void)
{
  _debug("Saving Configuration...\n");
  setConfig(AUDIO, VOLUME_SPKR, getSpkrVolume());
  setConfig(AUDIO, VOLUME_MICRO, getMicVolume());

  _setupLoaded = _config.saveConfigTree(_path.data());
  return _setupLoaded;
}

yanmorin's avatar
   
yanmorin committed
579
//THREAD=Main
580
581
 int
ManagerImpl::initRegisterAccounts()
jpbl's avatar
jpbl committed
582
{
583
    int status;
584
    bool flag = true;
585
586
587
588
589
    AccountMap::iterator iter;

    _debugInit("Initiate VoIP Links Registration");
    iter = _accountMap.begin();

590
    /* Loop on the account map previously loaded */
591
    while( iter != _accountMap.end() ) {
592
593
594
595
596
597
598
599
600
        if ( iter->second ) {
            iter->second->loadConfig();
            /* If the account is set as enabled, try to register */
            if ( iter->second->isEnabled() ) {
	            status = iter->second->registerVoIPLink();
	            if (status != SUCCESS){
		            flag = false;
                }
            }
601
        }
602
        iter++;
Emmanuel Milou's avatar
Emmanuel Milou committed
603
    }
604

605
606
607
    // calls the client notification here in case of errors at startup...
    if( _audiodriver -> getErrorMessage() != -1 )
      notifyErrClient( _audiodriver -> getErrorMessage() );
608

609
    ASSERT( flag, true );
610
    return SUCCESS;
jpbl's avatar
jpbl committed
611
612
}

yanmorin's avatar
   
yanmorin committed
613
//THREAD=Main
614
  bool
yanmorin's avatar
   
yanmorin committed
615
ManagerImpl::sendDtmf(const CallID& id, char code)
jpbl's avatar
jpbl committed
616
{
yanmorin's avatar
   
yanmorin committed
617
618
  AccountID accountid = getAccountFromCall( id );
  if (accountid == AccountNULL) {
Emmanuel Milou's avatar
Emmanuel Milou committed
619
620
    //_debug("Send DTMF: call doesn't exists\n");
    playDtmf(code, false);
yanmorin's avatar
   
yanmorin committed
621
622
623
    return false;
  }

jpbl's avatar
jpbl committed
624
  int sendType = getConfigInt(SIGNALISATION, SEND_DTMF_AS);
yanmorin's avatar
   
yanmorin committed
625
  bool returnValue = false;
jpbl's avatar
jpbl committed
626
  switch (sendType) {
627
    case 0: // SIP INFO
Emmanuel Milou's avatar
Emmanuel Milou committed
628
      playDtmf(code , true);
629
630
631
632
633
634
635
636
637
      returnValue = getAccountLink(accountid)->carryingDTMFdigits(id, code);
      break;

    case 1: // Audio way
      break;
    case 2: // rfc 2833
      break;
    default: // unknown - error config?
      break;
jpbl's avatar
jpbl committed
638
639
640
641
  }
  return returnValue;
}

yanmorin's avatar
   
yanmorin committed
642
//THREAD=Main | VoIPLink
643
  bool
Emmanuel Milou's avatar
Emmanuel Milou committed
644
ManagerImpl::playDtmf(char code, bool isTalking)
jpbl's avatar
jpbl committed
645
{
646
647
648
649
    int hasToPlayTone, pulselen, layer, size;
    bool ret = false;
    AudioLayer *audiolayer;
    SFLDataFormat *buf;
650

651
    stopTone(false);
652

653
    hasToPlayTone = getConfigInt(SIGNALISATION, PLAY_DTMF);
654
    if (!hasToPlayTone)
655
        return false;
jpbl's avatar
jpbl committed
656

657
658
659
660
    // length in milliseconds
    pulselen = getConfigInt(SIGNALISATION, PULSE_LENGTH);
    if (!pulselen)
        return false;
jpbl's avatar
jpbl committed
661

662
663
664
665
    // numbers of int = length in milliseconds / 1000 (number of seconds)
    //                = number of seconds * SAMPLING_RATE by SECONDS
    audiolayer = getAudioDriver();
    layer = audiolayer->getLayerType();
jpbl's avatar
jpbl committed
666

667
668
669
    // fast return, no sound, so no dtmf
    if (audiolayer==0 || _dtmfKey == 0)
        return false;
670

671
    // number of data sampling in one pulselen depends on samplerate
672
    // size (n sampling) = time_ms * sampling/s
673
674
675
676
677
678
679
680
681
682
683
684
685
    //                     ---------------------
    //                            ms/s
    size = (int)(pulselen * ((float)audiolayer->getSampleRate()/1000));

    // this buffer is for mono
    // TODO <-- this should be global and hide if same size
    buf = new SFLDataFormat[size];

    // Handle dtmf
    _dtmfKey->startTone(code);

    // copy the sound
    if ( _dtmfKey->generateDTMF(buf, size) ) {
686
        // Put buffer to urgentRingBuffer
687
688
        // put the size in bytes...
        // so size * 1 channel (mono) * sizeof (bytes for the data)
689
        audiolayer->startStream();
690
691
692
        audiolayer->putUrgent (buf, size * sizeof(SFLDataFormat));
    }
    ret = true;
693

694
    // TODO Cache the DTMF
Emmanuel Milou's avatar
Emmanuel Milou committed
695

696
    delete[] buf; buf = 0;
697

698
    return ret;
jpbl's avatar
jpbl committed
699
700
}

701
// Multi-thread
jpbl's avatar
jpbl committed
702
703
704
705
706
707
bool
ManagerImpl::incomingCallWaiting() {
  return (_nbIncomingWaitingCall > 0) ? true : false;
}

void
yanmorin's avatar
   
yanmorin committed
708
ManagerImpl::addWaitingCall(const CallID& id) {
709
  ost::MutexLock m(_waitingCallMutex);
yanmorin's avatar
   
yanmorin committed
710
  _waitingCall.insert(id);
jpbl's avatar
jpbl committed
711
712
713
714
  _nbIncomingWaitingCall++;
}

void
yanmorin's avatar
   
yanmorin committed
715
ManagerImpl::removeWaitingCall(const CallID& id) {
716
  ost::MutexLock m(_waitingCallMutex);
yanmorin's avatar
   
yanmorin committed
717
718
719
  // should return more than 1 if it erase a call
  if (_waitingCall.erase(id)) {
    _nbIncomingWaitingCall--;
jpbl's avatar
jpbl committed
720
721
722
723
  }
}

bool
yanmorin's avatar
   
yanmorin committed
724
725
726
727
ManagerImpl::isWaitingCall(const CallID& id) {
  CallIDSet::iterator iter = _waitingCall.find(id);
  if (iter != _waitingCall.end()) {
    return false;
jpbl's avatar
jpbl committed
728
  }
yanmorin's avatar
   
yanmorin committed
729
  return true;
jpbl's avatar
jpbl committed
730
731
}

yanmorin's avatar
   
yanmorin committed
732
///////////////////////////////////////////////////////////////////////////////
733
// Management of event peer IP-phone
yanmorin's avatar
   
yanmorin committed
734
////////////////////////////////////////////////////////////////////////////////
735
736
737
// SipEvent Thread
bool
ManagerImpl::incomingCall(Call* call, const AccountID& accountId)
jpbl's avatar
jpbl committed
738
{
739
740
    PulseLayer *pulselayer;
    std::string from, number;
741

742
743
    stopTone(true);

744
    _debug("Incoming call %s for account %s\n", call->getCallId().data(), accountId.c_str());
745

746
    associateCallToAccount(call->getCallId(), accountId);
jpbl's avatar
jpbl committed
747

748
749
750
    if (accountId==AccountNULL)
        associateConfigToCall (call->getCallId(), Call::IPtoIP);

751
752
    _debug("ManagerImpl::incomingCall :: hasCurrentCall() %i \n",hasCurrentCall());

753
754
755
756
    if ( !hasCurrentCall() ) {
        call->setConnectionState(Call::Ringing);
        ringtone();
        switchCall(call->getCallId());
757

758
    }
759
    /*
760
    else {
761
762
        addWaitingCall(call->getCallId());
    }
763
764
765
    */

    addWaitingCall(call->getCallId());
766
767
768
769
770
771
772
773
774
775
776
777
778

    from = call->getPeerName();
    number = call->getPeerNumber();

    if (from != "" && number != "") {
        from.append(" <");
        from.append(number);
        from.append(">");
    } else if ( from.empty() ) {
        from.append("<");
        from.append(number);
        from.append(">");
    }
779
780
781
782
783
784
785
786
787

    /*
    CallIDSet::iterator iter = _waitingCall.begin();
    while (iter != _waitingCall.end()) {
        CallID ident = *iter;
        _debug("ManagerImpl::incomingCall :: CALL iteration: %s \n",ident.c_str());
        ++iter;
    }
    */
788

789
    /* Broadcast a signal over DBus */
790
791
    if (_dbus) _dbus->getCallManager()->incomingCall(accountId, call->getCallId(), from);

792
    //if (_dbus) _dbus->getCallManager()->callStateChanged(call->getCallId(), "INCOMING");
793

794
795
796
797
798
    // Reduce volume of the other pulseaudio-connected audio applications
    if( _audiodriver->getLayerType() == PULSEAUDIO && getConfigInt( PREFERENCES , CONFIG_PA_VOLUME_CTRL ) ) {
        pulselayer = dynamic_cast<PulseLayer *> (getAudioDriver());
        if(pulselayer)  pulselayer->reducePulseAppsVolume();
    }
799

800
    return true;
jpbl's avatar
jpbl committed
801
802
}

yanmorin's avatar
   
yanmorin committed
803
804
805
//THREAD=VoIP
void
ManagerImpl::incomingMessage(const AccountID& accountId, const std::string& message) {
806
807
  if (_dbus) {
    _dbus->getCallManager()->incomingMessage(accountId, message);
jpbl's avatar
jpbl committed
808
809
810
  }
}

yanmorin's avatar
   
yanmorin committed
811
//THREAD=VoIP CALL=Outgoing
812
  void
yanmorin's avatar
   
yanmorin committed
813
ManagerImpl::peerAnsweredCall(const CallID& id)
jpbl's avatar
jpbl committed
814
{
Emmanuel Milou's avatar
Emmanuel Milou committed
815
816
817
    if (isCurrentCall(id)) {
        stopTone(false);
    }
818

Emmanuel Milou's avatar
Emmanuel Milou committed
819
    if (_dbus) _dbus->getCallManager()->callStateChanged(id, "CURRENT");
820

Alexandre Savard's avatar
Alexandre Savard committed
821
  std::string codecName = getCurrentCodecName(id);
Emmanuel Milou's avatar
Emmanuel Milou committed
822
  // _debug("ManagerImpl::hangupCall(): broadcast codec name %s \n",codecName.c_str());
Alexandre Savard's avatar
Alexandre Savard committed
823
  if (_dbus) _dbus->getCallManager()->currentSelectedCodec(id,codecName.c_str());
jpbl's avatar
jpbl committed
824
825
}

yanmorin's avatar
   
yanmorin committed
826
//THREAD=VoIP Call=Outgoing
827
  void
yanmorin's avatar
   
yanmorin committed
828
ManagerImpl::peerRingingCall(const CallID& id)
jpbl's avatar
jpbl committed
829
{
yanmorin's avatar
   
yanmorin committed
830
  if (isCurrentCall(id)) {
jpbl's avatar
jpbl committed
831
832
    ringback();
  }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
833
  if (_dbus) _dbus->getCallManager()->callStateChanged(id, "RINGING");
jpbl's avatar
jpbl committed
834
835
}

yanmorin's avatar
   
yanmorin committed
836
//THREAD=VoIP Call=Outgoing/Ingoing
837
  void
yanmorin's avatar
   
yanmorin committed
838
ManagerImpl::peerHungupCall(const CallID& id)
jpbl's avatar
jpbl committed
839
{
840
841
    PulseLayer *pulselayer;
    AccountID accountid;
842
    bool returnValue;
843

844
845
846
847
848
849
850
851
852
853
854
855
856
    /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
        SIPVoIPLink::instance (AccountNULL)->hangup (id);
    }

    else
    {
        accountid = getAccountFromCall( id );
        if (accountid == AccountNULL) {
            _debug("peerHungupCall: Call doesn't exists\n");
            return;
        }
        returnValue = getAccountLink(accountid)->peerHungup(id);
857
    }
858

859
860
    /* Broadcast a signal over DBus */
    if (_dbus) _dbus->getCallManager()->callStateChanged(id, "HUNGUP");
861

862
863
864
865
866
867
868
    if (isCurrentCall(id)) {
        stopTone(true);
        switchCall("");
    }

    removeWaitingCall(id);
    removeCallAccount(id);
869

870
871
872
873
    if( _audiodriver->getLayerType() == PULSEAUDIO && getConfigInt( PREFERENCES , CONFIG_PA_VOLUME_CTRL ) ) {
        pulselayer = dynamic_cast<PulseLayer *> (getAudioDriver());
        if(pulselayer)  pulselayer->restorePulseAppsVolume();
    }
jpbl's avatar
jpbl committed
874
875
}

yanmorin's avatar
   
yanmorin committed
876
//THREAD=VoIP
jpbl's avatar
jpbl committed
877
void
yanmorin's avatar
   
yanmorin committed
878
879
ManagerImpl::callBusy(const CallID& id) {
  _debug("Call busy\n");
880

881
  if (_dbus) _dbus->getCallManager()->callStateChanged(id, "BUSY");
yanmorin's avatar
   
yanmorin committed
882
883
884
  if (isCurrentCall(id) ) {
    playATone(Tone::TONE_BUSY);
    switchCall("");
jpbl's avatar
jpbl committed
885
  }
yanmorin's avatar
   
yanmorin committed
886
887
  removeCallAccount(id);
  removeWaitingCall(id);
jpbl's avatar
jpbl committed
888
889
}

yanmorin's avatar
   
yanmorin committed