managerimpl.cpp 76.6 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)
jpbl's avatar
jpbl committed
86
{
87

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

91
92
    _cleaner = new NumberCleaner ();

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

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

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

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

118
    initVolume();
119

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

124
125
    initAudioDriver();
    selectAudioDriver();
jpbl's avatar
jpbl committed
126

127
128
    // Initialize the list of supported audio codecs
    initAudioCodec();
129

130
    AudioLayer *audiolayer = getAudioDriver();
131

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

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

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

    if (audiolayer == 0)
      audiolayer->stopStream();
jpbl's avatar
jpbl committed
145
146
147
148
}

void ManagerImpl::terminate()
{
149
    _debug("ManagerImpl::terminate \n");
150
    saveConfig();
jpbl's avatar
jpbl committed
151

152
    unloadAccountMap();
153

154
    _debug("Unload DTMF Key \n");
155
    delete _dtmfKey;
jpbl's avatar
jpbl committed
156

157
    _debug("Unload Audio Driver \n");
158
    delete _audiodriver; _audiodriver = NULL;
jpbl's avatar
jpbl committed
159

160
    _debug("Unload Telephone Tone \n");
161
    delete _telephoneTone; _telephoneTone = NULL;
162

163
    _debug("Unload Audio Codecs \n");
164
    _codecDescriptorMap.deleteHandlePointer();
165

jpbl's avatar
jpbl committed
166
167
}

yanmorin's avatar
   
yanmorin committed
168
169
170
bool
ManagerImpl::isCurrentCall(const CallID& callId) {
  return (_currentCallId2 == callId ? true : false);
jpbl's avatar
jpbl committed
171
172
}

yanmorin's avatar
   
yanmorin committed
173
174
bool
ManagerImpl::hasCurrentCall() {
Emmanuel Milou's avatar
Bug fix    
Emmanuel Milou committed
175
  _debug("Current call ID = %s\n", _currentCallId2.c_str());
yanmorin's avatar
   
yanmorin committed
176
177
  if ( _currentCallId2 != "") {
    return true;
jpbl's avatar
jpbl committed
178
  }
yanmorin's avatar
   
yanmorin committed
179
  return false;
jpbl's avatar
jpbl committed
180
181
}

182
const CallID&
yanmorin's avatar
   
yanmorin committed
183
184
ManagerImpl::getCurrentCallId() {
  return _currentCallId2;
jpbl's avatar
jpbl committed
185
186
187
}

void
yanmorin's avatar
   
yanmorin committed
188
ManagerImpl::switchCall(const CallID& id ) {
189
  ost::MutexLock m(_currentCallMutex);
yanmorin's avatar
   
yanmorin committed
190
  _currentCallId2 = id;
jpbl's avatar
jpbl committed
191
192
193
194
195
196
}


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

199
  bool
yanmorin's avatar
   
yanmorin committed
200
201
ManagerImpl::outgoingCall(const std::string& accountid, const CallID& id, const std::string& to)
{
202
    std::string pattern, to_cleaned;
Emmanuel Milou's avatar
Emmanuel Milou committed
203
204
    Call::CallConfiguration callConfig;
    SIPVoIPLink *siplink;
205

206
207
    _debug("ManagerImpl::outgoingCall() method \n");

208
209
210
211
212
    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
213
214

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

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

Emmanuel Milou's avatar
Emmanuel Milou committed
231
232
233
234
    if (!accountExists(accountid)) {
        _debug("! Manager Error: Outgoing Call: account doesn't exist\n");
        return false;
    }
235

Emmanuel Milou's avatar
Emmanuel Milou committed
236
237
238
239
    if (getAccountFromCall(id) != AccountNULL) {
        _debug("! Manager Error: Outgoing Call: call id already exists\n");
        return false;
    }
240

Emmanuel Milou's avatar
Emmanuel Milou committed
241
242
243
244
    if (hasCurrentCall()) {
        _debug("* Manager Info: there is currently a call, try to hold it\n");
        onHoldCall(getCurrentCallId());
    }
245

Emmanuel Milou's avatar
Emmanuel Milou committed
246
    _debug("- Manager Action: Adding Outgoing Call %s on account %s\n", id.data(), accountid.data());
yanmorin's avatar
   
yanmorin committed
247
    associateCallToAccount( id, accountid );
248
    if ( getAccountLink(accountid)->newOutgoingCall(id, to_cleaned) ) {
Emmanuel Milou's avatar
Emmanuel Milou committed
249
250
251
252
253
254
255
        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
256
257
}

yanmorin's avatar
   
yanmorin committed
258
//THREAD=Main : for outgoing Call
259
  bool
yanmorin's avatar
   
yanmorin committed
260
ManagerImpl::answerCall(const CallID& id)
jpbl's avatar
jpbl committed
261
{
262
263
  bool isActive = false;

264
265
  stopTone(true);

266
267
268
269
270
271
  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;
272

273
  // stopTone(false);
274
  _debug("Try to answer call: %s\n", id.data());
yanmorin's avatar
   
yanmorin committed
275
276
277
  AccountID accountid = getAccountFromCall( id );
  if (accountid == AccountNULL) {
    _debug("Answering Call: Call doesn't exists\n");
278
    //return false;
jpbl's avatar
jpbl committed
279
  }
280

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

Yun Liu's avatar
Yun Liu committed
285
286
287
    onHoldCall(getCurrentCallId());
  }

yanmorin's avatar
   
yanmorin committed
288
289
290
291
  if (!getAccountLink(accountid)->answer(id)) {
    // error when receiving...
    removeCallAccount(id);
    return false;
jpbl's avatar
jpbl committed
292
  }
Alexandre Savard's avatar
Alexandre Savard committed
293

yanmorin's avatar
   
yanmorin committed
294
  // if it was waiting, it's waiting no more
295
  if (_dbus) _dbus->getCallManager()->callStateChanged(id, "CURRENT");
yanmorin's avatar
   
yanmorin committed
296
297
  removeWaitingCall(id);
  switchCall(id);
298

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

yanmorin's avatar
   
yanmorin committed
303
  return true;
jpbl's avatar
jpbl committed
304
305
}

yanmorin's avatar
   
yanmorin committed
306
//THREAD=Main
307
  bool
yanmorin's avatar
   
yanmorin committed
308
ManagerImpl::hangupCall(const CallID& id)
jpbl's avatar
jpbl committed
309
{
310
    _debug("ManagerImpl::hangupCall()\n");
311
312
313
    PulseLayer *pulselayer;
    AccountID accountid;
    bool returnValue;
314
    AudioLayer *audiolayer;
Emmanuel Milou's avatar
Emmanuel Milou committed
315

316
    stopTone(false);
yanmorin's avatar
   
yanmorin committed
317

318
319
    /* Broadcast a signal over DBus */
    if (_dbus) _dbus->getCallManager()->callStateChanged(id, "HUNGUP");
320

321
322
323
324
    _debug("Stop audio stream\n");
    audiolayer = getAudioDriver();
    audiolayer->stopStream();

Emmanuel Milou's avatar
Emmanuel Milou committed
325
326
    /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
327
        returnValue = SIPVoIPLink::instance (AccountNULL)->hangup (id);
328
    }
Emmanuel Milou's avatar
Emmanuel Milou committed
329
    /* Classic call, attached to an account */
330
    else {
Emmanuel Milou's avatar
Emmanuel Milou committed
331
332
333
334
335
336
337
338
        accountid = getAccountFromCall( id );
        if (accountid == AccountNULL) {
            _debug("! Manager Hangup Call: Call doesn't exists\n");
            return false;
        }
        returnValue = getAccountLink(accountid)->hangup(id);
        removeCallAccount(id);
    }
339

340
    switchCall("");
341

342
343
344
345
    if( _audiodriver->getLayerType() == PULSEAUDIO && getConfigInt( PREFERENCES , CONFIG_PA_VOLUME_CTRL ) ) {
        pulselayer = dynamic_cast<PulseLayer *> (getAudioDriver());
        if(pulselayer)  pulselayer->restorePulseAppsVolume();
    }
346

347
348
    

349
    return returnValue;
jpbl's avatar
jpbl committed
350
351
}

yanmorin's avatar
   
yanmorin committed
352
//THREAD=Main
353
  bool
yanmorin's avatar
   
yanmorin committed
354
ManagerImpl::cancelCall (const CallID& id)
jpbl's avatar
jpbl committed
355
{
Emmanuel Milou's avatar
Emmanuel Milou committed
356
357
    AccountID accountid;
    bool returnValue;
yanmorin's avatar
   
yanmorin committed
358

Emmanuel Milou's avatar
Emmanuel Milou committed
359
    stopTone(true);
360

Emmanuel Milou's avatar
Emmanuel Milou committed
361
362
    /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
363
        returnValue = SIPVoIPLink::instance (AccountNULL)->cancel (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
364
365
366
    }

    /* Classic call, attached to an account */
367
    else {
Emmanuel Milou's avatar
Emmanuel Milou committed
368
369
370
371
372
373
374
375
        accountid = getAccountFromCall( id );
        if (accountid == AccountNULL) {
            _debug("! Manager Cancel Call: Call doesn't exists\n");
            return false;
        }
        returnValue = getAccountLink(accountid)->cancel(id);
        removeCallAccount(id);
    }
376

Emmanuel Milou's avatar
Emmanuel Milou committed
377
378
379
380
381
    // it could be a waiting call?
    removeWaitingCall(id);
    switchCall("");

    return returnValue;
jpbl's avatar
jpbl committed
382
383
}

yanmorin's avatar
   
yanmorin committed
384
//THREAD=Main
385
  bool
yanmorin's avatar
   
yanmorin committed
386
ManagerImpl::onHoldCall(const CallID& id)
jpbl's avatar
jpbl committed
387
{
Emmanuel Milou's avatar
Emmanuel Milou committed
388
389
    AccountID accountid;
    bool returnValue;
390
    CallID call_id;
yanmorin's avatar
   
yanmorin committed
391

Emmanuel Milou's avatar
Emmanuel Milou committed
392
    stopTone(true);
393

394
395
    call_id = id;

Emmanuel Milou's avatar
Emmanuel Milou committed
396
397
    /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
398
        returnValue = SIPVoIPLink::instance (AccountNULL)-> onhold (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
399
    }
400

Emmanuel Milou's avatar
Emmanuel Milou committed
401
    /* Classic call, attached to an account */
402
    else {
Emmanuel Milou's avatar
Emmanuel Milou committed
403
404
405
406
407
408
409
        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);
    }
410

Emmanuel Milou's avatar
Emmanuel Milou committed
411
412
    removeWaitingCall(id);
    switchCall("");
413

414
    if (_dbus) _dbus->getCallManager()->callStateChanged(call_id, "HOLD");
415

Emmanuel Milou's avatar
Emmanuel Milou committed
416
    return returnValue;
yanmorin's avatar
   
yanmorin committed
417
418
419
}

//THREAD=Main
420
  bool
yanmorin's avatar
   
yanmorin committed
421
422
ManagerImpl::offHoldCall(const CallID& id)
{
423

Emmanuel Milou's avatar
Emmanuel Milou committed
424
425
426
    AccountID accountid;
    bool returnValue, rec;
    std::string codecName;
427
    CallID call_id;
428

Emmanuel Milou's avatar
Emmanuel Milou committed
429
    stopTone(false);
430

431
    call_id = id;
Emmanuel Milou's avatar
Emmanuel Milou committed
432
    //Place current call on hold if it isn't
433
434
    if (hasCurrentCall())
    {
435
        _debug ("Put the current call (ID=%s) on hold\n", getCurrentCallId().c_str());
Emmanuel Milou's avatar
Emmanuel Milou committed
436
437
        onHoldCall(getCurrentCallId());
    }
438

Emmanuel Milou's avatar
Emmanuel Milou committed
439
440
441
    /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
        rec = SIPVoIPLink::instance (AccountNULL)-> isRecording (id);
442
        returnValue = SIPVoIPLink::instance (AccountNULL)-> offhold (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
443
    }
alexandresavard's avatar
alexandresavard committed
444

Emmanuel Milou's avatar
Emmanuel Milou committed
445
    /* Classic call, attached to an account */
446
    else {
Emmanuel Milou's avatar
Emmanuel Milou committed
447
448
449
450
451
452
453
454
455
        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
456

457

458
    if (_dbus){
Emmanuel Milou's avatar
Emmanuel Milou committed
459
        if (rec)
460
            _dbus->getCallManager()->callStateChanged(call_id, "UNHOLD_RECORD");
461
        else
462
            _dbus->getCallManager()->callStateChanged(call_id, "UNHOLD_CURRENT");
463

Emmanuel Milou's avatar
Emmanuel Milou committed
464
    }
465

Emmanuel Milou's avatar
Emmanuel Milou committed
466
    switchCall(id);
467

468
    codecName = getCurrentCodecName(id);
469
    // _debug("ManagerImpl::hangupCall(): broadcast codec name %s \n",codecName.c_str());
470
    if (_dbus) _dbus->getCallManager()->currentSelectedCodec(id,codecName.c_str());
471

Emmanuel Milou's avatar
Emmanuel Milou committed
472
    return returnValue;
jpbl's avatar
jpbl committed
473
474
}

yanmorin's avatar
   
yanmorin committed
475
//THREAD=Main
476
  bool
yanmorin's avatar
   
yanmorin committed
477
ManagerImpl::transferCall(const CallID& id, const std::string& to)
jpbl's avatar
jpbl committed
478
{
Emmanuel Milou's avatar
Emmanuel Milou committed
479
480
    AccountID accountid;
    bool returnValue;
481

Emmanuel Milou's avatar
Emmanuel Milou committed
482
    stopTone(true);
483

Emmanuel Milou's avatar
Emmanuel Milou committed
484
485
    /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
486
        returnValue = SIPVoIPLink::instance (AccountNULL)-> transfer (id, to);
Emmanuel Milou's avatar
Emmanuel Milou committed
487
488
489
    }

    /* Classic call, attached to an account */
490
    else {
Emmanuel Milou's avatar
Emmanuel Milou committed
491
492
493
494
495
496
497
498
        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);
    }
499

Emmanuel Milou's avatar
Emmanuel Milou committed
500
501
    removeWaitingCall(id);
    switchCall("");
502

Emmanuel Milou's avatar
Emmanuel Milou committed
503
504
    if (_dbus) _dbus->getCallManager()->callStateChanged(id, "HUNGUP");
    return returnValue;
jpbl's avatar
jpbl committed
505
506
}

yanmorin's avatar
   
yanmorin committed
507
//THREAD=Main : Call:Incoming
508
  bool
yanmorin's avatar
   
yanmorin committed
509
ManagerImpl::refuseCall (const CallID& id)
jpbl's avatar
jpbl committed
510
{
Emmanuel Milou's avatar
Emmanuel Milou committed
511
512
    AccountID accountid;
    bool returnValue;
513

Emmanuel Milou's avatar
Emmanuel Milou committed
514
515
516
517
    stopTone(true);

     /* Direct IP to IP call */
    if (getConfigFromCall (id) == Call::IPtoIP) {
518
        returnValue = SIPVoIPLink::instance (AccountNULL)-> refuse (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
519
520
521
    }

    /* Classic call, attached to an account */
522
    else {
Emmanuel Milou's avatar
Emmanuel Milou committed
523
524
525
526
527
528
529
        accountid = getAccountFromCall( id );
        if (accountid == AccountNULL) {
            _debug("! Manager OffHold Call: Call doesn't exists\n");
            return false;
        }
        returnValue = getAccountLink(accountid)->refuse(id);
        removeCallAccount(id);
530
531
    }

Emmanuel Milou's avatar
Emmanuel Milou committed
532
533
534
535
536
537
538
539
    // 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
540
541
}

yanmorin's avatar
   
yanmorin committed
542
//THREAD=Main
543
  bool
jpbl's avatar
jpbl committed
544
545
546
547
548
549
550
551
552
553
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
554
//THREAD=Main
555
556
 int
ManagerImpl::initRegisterAccounts()
jpbl's avatar
jpbl committed
557
{
558
    int status;
559
    bool flag = true;
560
561
562
563
564
    AccountMap::iterator iter;

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

565
    /* Loop on the account map previously loaded */
566
    while( iter != _accountMap.end() ) {
567
568
569
570
571
572
573
574
575
        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;
                }
            }
576
        }
577
        iter++;
Emmanuel Milou's avatar
Emmanuel Milou committed
578
    }
579

580
581
582
    // calls the client notification here in case of errors at startup...
    if( _audiodriver -> getErrorMessage() != -1 )
      notifyErrClient( _audiodriver -> getErrorMessage() );
583

584
    ASSERT( flag, true );
585
    return SUCCESS;
jpbl's avatar
jpbl committed
586
587
}

yanmorin's avatar
   
yanmorin committed
588
//THREAD=Main
589
  bool
yanmorin's avatar
   
yanmorin committed
590
ManagerImpl::sendDtmf(const CallID& id, char code)
jpbl's avatar
jpbl committed
591
{
yanmorin's avatar
   
yanmorin committed
592
593
  AccountID accountid = getAccountFromCall( id );
  if (accountid == AccountNULL) {
Emmanuel Milou's avatar
Emmanuel Milou committed
594
595
    //_debug("Send DTMF: call doesn't exists\n");
    playDtmf(code, false);
yanmorin's avatar
   
yanmorin committed
596
597
598
    return false;
  }

jpbl's avatar
jpbl committed
599
  int sendType = getConfigInt(SIGNALISATION, SEND_DTMF_AS);
yanmorin's avatar
   
yanmorin committed
600
  bool returnValue = false;
jpbl's avatar
jpbl committed
601
  switch (sendType) {
602
    case 0: // SIP INFO
Emmanuel Milou's avatar
Emmanuel Milou committed
603
      playDtmf(code , true);
604
605
606
607
608
609
610
611
612
      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
613
614
615
616
  }
  return returnValue;
}

yanmorin's avatar
   
yanmorin committed
617
//THREAD=Main | VoIPLink
618
  bool
Emmanuel Milou's avatar
Emmanuel Milou committed
619
ManagerImpl::playDtmf(char code, bool isTalking)
jpbl's avatar
jpbl committed
620
{
621
622
623
624
    int hasToPlayTone, pulselen, layer, size;
    bool ret = false;
    AudioLayer *audiolayer;
    SFLDataFormat *buf;
625

626
    stopTone(false);
627

628
    hasToPlayTone = getConfigInt(SIGNALISATION, PLAY_DTMF);
629
    if (!hasToPlayTone)
630
        return false;
jpbl's avatar
jpbl committed
631

632
633
634
635
    // length in milliseconds
    pulselen = getConfigInt(SIGNALISATION, PULSE_LENGTH);
    if (!pulselen)
        return false;
jpbl's avatar
jpbl committed
636

637
638
639
640
    // 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
641

642
643
644
    // fast return, no sound, so no dtmf
    if (audiolayer==0 || _dtmfKey == 0)
        return false;
645

646
    // number of data sampling in one pulselen depends on samplerate
647
    // size (n sampling) = time_ms * sampling/s
648
649
650
651
652
653
654
655
656
657
658
659
660
    //                     ---------------------
    //                            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) ) {
661
        // Put buffer to urgentRingBuffer
662
663
        // put the size in bytes...
        // so size * 1 channel (mono) * sizeof (bytes for the data)
664
        audiolayer->startStream();
665
666
667
        audiolayer->putUrgent (buf, size * sizeof(SFLDataFormat));
    }
    ret = true;
668

669
    // TODO Cache the DTMF
Emmanuel Milou's avatar
Emmanuel Milou committed
670

671
    delete[] buf; buf = 0;
672

673
    return ret;
jpbl's avatar
jpbl committed
674
675
}

676
// Multi-thread
jpbl's avatar
jpbl committed
677
678
679
680
681
682
bool
ManagerImpl::incomingCallWaiting() {
  return (_nbIncomingWaitingCall > 0) ? true : false;
}

void
yanmorin's avatar
   
yanmorin committed
683
ManagerImpl::addWaitingCall(const CallID& id) {
684
  ost::MutexLock m(_waitingCallMutex);
yanmorin's avatar
   
yanmorin committed
685
  _waitingCall.insert(id);
jpbl's avatar
jpbl committed
686
687
688
689
  _nbIncomingWaitingCall++;
}

void
yanmorin's avatar
   
yanmorin committed
690
ManagerImpl::removeWaitingCall(const CallID& id) {
691
  ost::MutexLock m(_waitingCallMutex);
yanmorin's avatar
   
yanmorin committed
692
693
694
  // should return more than 1 if it erase a call
  if (_waitingCall.erase(id)) {
    _nbIncomingWaitingCall--;
jpbl's avatar
jpbl committed
695
696
697
698
  }
}

bool
yanmorin's avatar
   
yanmorin committed
699
700
701
702
ManagerImpl::isWaitingCall(const CallID& id) {
  CallIDSet::iterator iter = _waitingCall.find(id);
  if (iter != _waitingCall.end()) {
    return false;
jpbl's avatar
jpbl committed
703
  }
yanmorin's avatar
   
yanmorin committed
704
  return true;
jpbl's avatar
jpbl committed
705
706
}

yanmorin's avatar
   
yanmorin committed
707
///////////////////////////////////////////////////////////////////////////////
708
// Management of event peer IP-phone
yanmorin's avatar
   
yanmorin committed
709
////////////////////////////////////////////////////////////////////////////////
710
711
712
// SipEvent Thread
bool
ManagerImpl::incomingCall(Call* call, const AccountID& accountId)
jpbl's avatar
jpbl committed
713
{
714
715
    PulseLayer *pulselayer;
    std::string from, number;
716

717
718
    stopTone(true);

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

721
    associateCallToAccount(call->getCallId(), accountId);
jpbl's avatar
jpbl committed
722

723
724
725
    if (accountId==AccountNULL)
        associateConfigToCall (call->getCallId(), Call::IPtoIP);

726
727
    _debug("ManagerImpl::incomingCall :: hasCurrentCall() %i \n",hasCurrentCall());

728
729
730
731
    if ( !hasCurrentCall() ) {
        call->setConnectionState(Call::Ringing);
        ringtone();
        switchCall(call->getCallId());
732

733
    }
734
    /*
735
    else {
736
737
        addWaitingCall(call->getCallId());
    }
738
739
740
    */

    addWaitingCall(call->getCallId());
741
742
743
744
745
746
747
748
749
750
751
752
753

    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(">");
    }
754
755
756
757
758
759
760
761
762

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

764
    /* Broadcast a signal over DBus */
765
766
    if (_dbus) _dbus->getCallManager()->incomingCall(accountId, call->getCallId(), from);

767
    //if (_dbus) _dbus->getCallManager()->callStateChanged(call->getCallId(), "INCOMING");
768

769
770
771
772
773
    // 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();
    }
774

775
    return true;
jpbl's avatar
jpbl committed
776
777
}

yanmorin's avatar
   
yanmorin committed
778
779
780
//THREAD=VoIP
void
ManagerImpl::incomingMessage(const AccountID& accountId, const std::string& message) {
781
782
  if (_dbus) {
    _dbus->getCallManager()->incomingMessage(accountId, message);
jpbl's avatar
jpbl committed
783
784
785
  }
}

yanmorin's avatar
   
yanmorin committed
786
//THREAD=VoIP CALL=Outgoing
787
  void
yanmorin's avatar
   
yanmorin committed
788
ManagerImpl::peerAnsweredCall(const CallID& id)
jpbl's avatar
jpbl committed
789
{
Emmanuel Milou's avatar
Emmanuel Milou committed
790
791
792
    if (isCurrentCall(id)) {
        stopTone(false);
    }
793

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

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

yanmorin's avatar
   
yanmorin committed
801
//THREAD=VoIP Call=Outgoing
802
  void
yanmorin's avatar
   
yanmorin committed
803
ManagerImpl::peerRingingCall(const CallID& id)
jpbl's avatar
jpbl committed
804
{
yanmorin's avatar
   
yanmorin committed
805
  if (isCurrentCall(id)) {
jpbl's avatar
jpbl committed
806
807
    ringback();
  }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
808
  if (_dbus) _dbus->getCallManager()->callStateChanged(id, "RINGING");
jpbl's avatar
jpbl committed
809
810
}

yanmorin's avatar
   
yanmorin committed
811
//THREAD=VoIP Call=Outgoing/Ingoing
812
  void
yanmorin's avatar
   
yanmorin committed
813
ManagerImpl::peerHungupCall(const CallID& id)
jpbl's avatar
jpbl committed
814
{
815
816
    PulseLayer *pulselayer;
    AccountID accountid;
817
    bool returnValue;
818

819
820
821
822
823
824
825
826
827
828
829
830
831
    /* 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);
832
    }
833

834
835
    /* Broadcast a signal over DBus */
    if (_dbus) _dbus->getCallManager()->callStateChanged(id, "HUNGUP");
836

837
838
839
840
841
842
843
    if (isCurrentCall(id)) {
        stopTone(true);
        switchCall("");
    }

    removeWaitingCall(id);
    removeCallAccount(id);
844

845
846
847
848
    if( _audiodriver->getLayerType() == PULSEAUDIO && getConfigInt( PREFERENCES , CONFIG_PA_VOLUME_CTRL ) ) {
        pulselayer = dynamic_cast<PulseLayer *> (getAudioDriver());
        if(pulselayer)  pulselayer->restorePulseAppsVolume();
    }
jpbl's avatar
jpbl committed
849
850
}

yanmorin's avatar
   
yanmorin committed
851
//THREAD=VoIP
jpbl's avatar
jpbl committed
852
void
yanmorin's avatar
   
yanmorin committed
853
854
ManagerImpl::callBusy(const CallID& id) {
  _debug("Call busy\n");
855

856
  if (_dbus) _dbus->getCallManager()->callStateChanged(id, "BUSY");
yanmorin's avatar
   
yanmorin committed
857
858
859
  if (isCurrentCall(id) ) {
    playATone(Tone::TONE_BUSY);
    switchCall("");
jpbl's avatar
jpbl committed
860
  }
yanmorin's avatar
   
yanmorin committed
861
862
  removeCallAccount(id);
  removeWaitingCall(id);
jpbl's avatar
jpbl committed
863
864
}

yanmorin's avatar
   
yanmorin committed
865
//THREAD=VoIP
866
  void
867
ManagerImpl::callFailure(const CallID& id)
yanmorin's avatar
   
yanmorin committed
868
{
869
  if (_dbus) _dbus->getCallManager()->callStateChanged(id, "FAILURE");
Emmanuel Milou's avatar
Emmanuel Milou committed
870
  _debug("CALL ID = %s\n" , id.c_str());
yanmorin's avatar
   
yanmorin committed
871
872
873
874
875
876
  if (isCurrentCall(id) ) {
    playATone(Tone::TONE_BUSY);
    switchCall("");
  }
  removeCallAccount(id);
  removeWaitingCall(id);
877

jpbl's avatar
jpbl committed
878
879
}

yanmorin's avatar
   
yanmorin committed
880
//THREAD=VoIP
881
  void
Emmanuel Milou's avatar
Emmanuel Milou committed
882
ManagerImpl::startVoiceMessageNotification(const AccountID&