managerimpl.cpp 137 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: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
8
 *  Author: Guillaume Carmel-Archambault <guillaume.carmel-archambault@savoirfairelinux.com>
9
 *
jpbl's avatar
jpbl committed
10
11
 *  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
12
 *  the Free Software Foundation; either version 3 of the License, or
jpbl's avatar
jpbl committed
13
 *  (at your option) any later version.
14
 *
jpbl's avatar
jpbl committed
15
16
17
18
 *  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.
19
 *
jpbl's avatar
jpbl committed
20
21
22
23
24
 *  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.
 */

25
26
27
28
29
#include "managerimpl.h"

#include "account.h"
#include "dbus/callmanager.h"
#include "user_cfg.h"
30
#include "global.h"
31
#include "sip/sipaccount.h"
32

33
34
35
36
37
38
39
40
41
42
#include "audio/audiolayer.h"
#include "audio/alsa/alsalayer.h"
#include "audio/pulseaudio/pulselayer.h"
#include "audio/sound/tonelist.h"
#include "history/historymanager.h"
#include "accountcreator.h" // create new account
#include "sip/sipvoiplink.h"
#include "manager.h"
#include "dbus/configurationmanager.h"

43
44
#include "conference.h"

jpbl's avatar
jpbl committed
45
46
47
48
49
#include <errno.h>
#include <time.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
50
#include <sstream>
jpbl's avatar
jpbl committed
51
#include <sys/types.h> // mkdir(2)
52
53
#include <sys/stat.h>  // mkdir(2)
#include <pwd.h>       // getpwuid
jpbl's avatar
jpbl committed
54
55
56
57
58
59
60


#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)))

61
62
#define MD5_APPEND(pms,buf,len) pj_md5_update(pms, (const pj_uint8_t*)buf, len)

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

94
    // initialize random generator for call id
95
    srand (time (NULL));
yanmorin's avatar
   
yanmorin committed
96

97
    _cleaner = new NumberCleaner ();
98
    _history = new HistoryManager ();
99

yanmorin's avatar
   
yanmorin committed
100
#ifdef TEST
101
102
103
104
    testAccountMap();
    loadAccountMap();
    testCallAccountMap();
    unloadAccountMap();
yanmorin's avatar
   
yanmorin committed
105
106
#endif

107
108
    // should be call before initConfigFile
    // loadAccountMap();, called in init() now.
jpbl's avatar
jpbl committed
109
110
111
}

// never call if we use only the singleton...
112
ManagerImpl::~ManagerImpl (void)
jpbl's avatar
jpbl committed
113
{
114
    // terminate();
115
116
117
    delete _cleaner;
    _cleaner=0;
    _debug ("%s stop correctly.\n", PROGNAME);
jpbl's avatar
jpbl committed
118
119
}

120
void
121
ManagerImpl::init()
jpbl's avatar
jpbl committed
122
{
123
124
    // Load accounts, init map
    loadAccountMap();
125

126
    initVolume();
127

128
    if (_exist == 0) {
129
        _debug ("Cannot create config file in your home directory\n");
130
    }
131

132
    initAudioDriver();
133

134
    selectAudioDriver();
jpbl's avatar
jpbl committed
135

136
137
    // Initialize the list of supported audio codecs
    initAudioCodec();
138

139
    AudioLayer *audiolayer = getAudioDriver();
140

141
    if (audiolayer != 0) {
142
        unsigned int sampleRate = audiolayer->getSampleRate();
jpbl's avatar
jpbl committed
143

144
145
146
        _debugInit ("Load Telephone Tone");
        std::string country = getConfigString (PREFERENCES, ZONE_TONE);
        _telephoneTone = new TelephoneTone (country, sampleRate);
jpbl's avatar
jpbl committed
147

148
149
        _debugInit ("Loading DTMF key");
        _dtmfKey = new DTMF (sampleRate);
150
    }
151
152

    if (audiolayer == 0)
153
        audiolayer->stopStream();
154
155
156


    // Load the history
157
    _history->load_history (getConfigInt (PREFERENCES, CONFIG_HISTORY_LIMIT));
jpbl's avatar
jpbl committed
158
159
160
161
}

void ManagerImpl::terminate()
{
162
    _debug ("ManagerImpl::terminate \n");
163
    saveConfig();
jpbl's avatar
jpbl committed
164

165
    unloadAccountMap();
166

167
    _debug ("Unload DTMF Key \n");
168
    delete _dtmfKey;
jpbl's avatar
jpbl committed
169

170
171
172
    _debug ("Unload Audio Driver \n");
    delete _audiodriver;
    _audiodriver = NULL;
jpbl's avatar
jpbl committed
173

174
175
176
    _debug ("Unload Telephone Tone \n");
    delete _telephoneTone;
    _telephoneTone = NULL;
177

178
    _debug ("Unload Audio Codecs \n");
179
    _codecDescriptorMap.deleteHandlePointer();
180

jpbl's avatar
jpbl committed
181
182
}

yanmorin's avatar
   
yanmorin committed
183
bool
184
185
186
ManagerImpl::isCurrentCall (const CallID& callId)
{
    return (_currentCallId2 == callId ? true : false);
jpbl's avatar
jpbl committed
187
188
}

yanmorin's avatar
   
yanmorin committed
189
bool
190
191
ManagerImpl::hasCurrentCall()
{
192
    // _debug ("ManagerImpl::hasCurrentCall current call ID = %s\n", _currentCallId2.c_str());
193
194
195
196
197
198

    if (_currentCallId2 != "") {
        return true;
    }

    return false;
jpbl's avatar
jpbl committed
199
200
}

201
const CallID&
202
203
204
ManagerImpl::getCurrentCallId()
{
    return _currentCallId2;
jpbl's avatar
jpbl committed
205
206
207
}

void
208
209
210
ManagerImpl::switchCall (const CallID& id)
{
    ost::MutexLock m (_currentCallMutex);
211
    _debug ("----- Switch current call id to %s -----\n", id.c_str());
212
    _currentCallId2 = id;
213

214
215
    /*
    AudioLayer *al = getAudioDriver();
216

217
    if (id != "") {
218

219
    if(isConference(id)) {
220

221
        Conference *conf;
222

223
224
225
226
227
228
229
230
231
        ConferenceMap::iterator iter = _conferencemap.find(id);
        if(iter != _conferencemap.end())
        {
    	_debug("    set call recordable in audio layer\n");
    	conf = iter->second;
    	al->setRecorderInstance((Recordable*)conf);
        }
    }
    else {
232

233
234
        // set the recordable instance in audiolayer
        AccountID account_id = getAccountFromCall(id);
235
236


237
238
239
240
241
242
        Call *call = NULL;
        call = getAccountLink (account_id)->getCall(id);

        _debug("    set call recordable in audio layer\n");
        al->setRecorderInstance((Recordable*)call);
    }
243
244
    }
    */
jpbl's avatar
jpbl committed
245
246
247
248
249
250
}


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

253
bool
254
ManagerImpl::outgoingCall (const std::string& account_id, const CallID& call_id, const std::string& to)
yanmorin's avatar
   
yanmorin committed
255
{
256
    std::string pattern, to_cleaned;
Emmanuel Milou's avatar
Emmanuel Milou committed
257
258
    Call::CallConfiguration callConfig;
    SIPVoIPLink *siplink;
259

260
261
262
    _debug ("ManagerImpl::outgoingCall(%s)\n", call_id.c_str());

    CallID current_call_id = getCurrentCallId();
263

264
265
    if (getConfigString (HOOKS, PHONE_NUMBER_HOOK_ENABLED) ==  "1")
        _cleaner->set_phone_number_prefix (getConfigString (HOOKS, PHONE_NUMBER_HOOK_ADD_PREFIX));
266
267
    else
        _cleaner->set_phone_number_prefix ("");
Emmanuel Milou's avatar
Emmanuel Milou committed
268

269
    to_cleaned = _cleaner->clean (to);
Emmanuel Milou's avatar
Emmanuel Milou committed
270
271

    /* Check what kind of call we are dealing with */
272
    check_call_configuration (call_id, to_cleaned, &callConfig);
273

274
    // in any cases we have to detach from current communication
275
    if (hasCurrentCall()) {
276
277
278
279
280
281
282
283
284
285
286
287

        _debug ("    outgoingCall: Has current call (%s) put it onhold\n", current_call_id.c_str());

        // if this is not a conferenceand this and is not a conference participant

        if (!isConference (current_call_id) && !participToConference (current_call_id)) {
            _debug ("    outgoingCall: Put the current call (%s) on hold\n", current_call_id.c_str());
            onHoldCall (current_call_id);
        } else if (isConference (current_call_id) && !participToConference (call_id)) {
            _debug ("    outgoingCall: detach main participant from conference\n");
            detachParticipant (default_id, current_call_id);
        }
288
289
    }

Emmanuel Milou's avatar
Emmanuel Milou committed
290
    if (callConfig == Call::IPtoIP) {
291
        _debug ("    outgoingCall: Start IP to IP call\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
292
        /* We need to retrieve the sip voiplink instance */
293
294
        siplink = SIPVoIPLink::instance ("");

295
296
        if (siplink->new_ip_to_ip_call (call_id, to_cleaned)) {
            switchCall (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
297
            return true;
298
        } else {
299
            callFailure (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
300
        }
301

Emmanuel Milou's avatar
Emmanuel Milou committed
302
        return false;
303
    }
304

305
    if (!accountExists (account_id)) {
306
        _debug ("! Manager Error: Outgoing Call: account doesn't exist\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
307
308
        return false;
    }
309

310
    if (getAccountFromCall (call_id) != AccountNULL) {
311
        _debug ("! Manager Error: Outgoing Call: call id already exists\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
312
313
        return false;
    }
314
315


316
    _debug ("- Manager Action: Adding Outgoing Call %s on account %s\n", call_id.data(), account_id.data());
317

318
    associateCallToAccount (call_id, account_id);
319

320
321
    if (getAccountLink (account_id)->newOutgoingCall (call_id, to_cleaned)) {
        switchCall (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
322
323
        return true;
    } else {
324
        callFailure (call_id);
325
        _debug ("! Manager Error: An error occur, the call was not created\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
326
    }
327

Emmanuel Milou's avatar
Emmanuel Milou committed
328
    return false;
jpbl's avatar
jpbl committed
329
330
}

yanmorin's avatar
   
yanmorin committed
331
//THREAD=Main : for outgoing Call
332
bool
333
ManagerImpl::answerCall (const CallID& call_id)
jpbl's avatar
jpbl committed
334
{
335

336
    _debug ("ManagerImpl::answerCall(%s)\n", call_id.c_str());
337
338

    stopTone (true);
339

340
341
342
343
    // store the current call id
    CallID current_call_id = getCurrentCallId();

    AccountID account_id = getAccountFromCall (call_id);
344
345
346

    if (account_id == AccountNULL) {
        _debug ("    answerCall: AccountId is null\n");
347
    }
348

349
    Call* call = NULL;
350

351
    call = getAccountLink (account_id)->getCall (call_id);
352

353
    if (call == NULL) {
354
        _debug ("    answerCall: Call is null\n");
355
    }
356
357

    // in any cases we have to detach from current communication
Alexandre Savard's avatar
Alexandre Savard committed
358
    if (hasCurrentCall()) {
359
360
361
362
363
364
365
366
367
368
369
370
371
372

        _debug ("    answerCall: Currently conversing with %s\n", current_call_id.c_str());
        // if it is not a conference and is not a conference participant

        if (!isConference (current_call_id) && !participToConference (current_call_id)) {
            _debug ("    answerCall: Put the current call (%s) on hold\n", current_call_id.c_str());
            onHoldCall (current_call_id);
        }

        // if we are talking to a conference and we are answering an incoming call
        else if (isConference (current_call_id) && !participToConference (call_id)) {
            _debug ("    answerCall: Detach main participant from conference\n");
            detachParticipant (default_id, current_call_id);
        }
373

Alexandre Savard's avatar
Alexandre Savard committed
374
    }
375

376

377
    if (!getAccountLink (account_id)->answer (call_id)) {
378
        // error when receiving...
379
        removeCallAccount (call_id);
380
381
        return false;
    }
Alexandre Savard's avatar
Alexandre Savard committed
382

383
    // if it was waiting, it's waiting no more
384
    if (_dbus) _dbus->getCallManager()->callStateChanged (call_id, "CURRENT");
Emmanuel Milou's avatar
Emmanuel Milou committed
385

386
387
    // std::string codecName = Manager::instance().getCurrentCodecName (call_id);
    // if (_dbus) _dbus->getCallManager()->currentSelectedCodec (call_id, codecName.c_str());
388

389
    removeWaitingCall (call_id);
390

391
    // if we dragged this call into a conference already
392
393
394
    if (participToConference (call_id)) {

        // AccountID currentAccountId;
395
396
        // Call* call = NULL;

397
398
399
400
401
402
        // currentAccountId = getAccountFromCall (call_id);
        // call = getAccountLink (currentAccountId)->getCall (call_id);

        switchCall (call->getConfId());
    } else {
        switchCall (call_id);
403
    }
404
405

    return true;
jpbl's avatar
jpbl committed
406
407
}

yanmorin's avatar
   
yanmorin committed
408
//THREAD=Main
409
bool
410
ManagerImpl::hangupCall (const CallID& call_id)
jpbl's avatar
jpbl committed
411
{
412
    _debug ("ManagerImpl::hangupCall(%s)\n", call_id.c_str());
413
    PulseLayer *pulselayer;
414
    AccountID account_id;
415
    bool returnValue;
416
    AudioLayer *audiolayer;
Emmanuel Milou's avatar
Emmanuel Milou committed
417

418
    // store the current call id
419
420
    CallID current_call_id = getCurrentCallId();

421
    stopTone (false);
422
    // switchCall (call_id);
yanmorin's avatar
   
yanmorin committed
423

424
    /* Broadcast a signal over DBus */
425
426
    _debug ("    hangupCall: Send DBUS call state change (HUNGUP) for id %s\n", call_id.c_str());

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

429
430
    int nbCalls = getCallList().size();

431
432
433
    audiolayer = getAudioDriver();

    // stop streams
434
435
    if (audiolayer && (nbCalls <= 1)) {
        _debug ("    hangupCall: stop audio stream, ther is only %i call(s) remaining\n", nbCalls);
436
437
438
        audiolayer->stopStream();
    }

439

440
    if (participToConference (call_id)) {
441

442
        Conference *conf = getConferenceFromCallID (call_id);
443

444
445
446
447
448
449
450
451
452
453
        if (conf != NULL) {
            // remove this participant
            removeParticipant (call_id);

            processRemainingParticipant (current_call_id, conf);
        }
    } else {
        // we are not participating to a conference, current call switched to ""
        if (!isConference (current_call_id))
            switchCall ("");
454
    }
455

Emmanuel Milou's avatar
Emmanuel Milou committed
456
    /* Direct IP to IP call */
457
458
    if (getConfigFromCall (call_id) == Call::IPtoIP) {
        returnValue = SIPVoIPLink::instance (AccountNULL)->hangup (call_id);
459
    }
460

Emmanuel Milou's avatar
Emmanuel Milou committed
461
    /* Classic call, attached to an account */
462
    else {
463
        account_id = getAccountFromCall (call_id);
464

465
        if (account_id == AccountNULL) {
466
            _debug ("! Manager Hangup Call: Call doesn't exists\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
467
468
            return false;
        }
469

470
        returnValue = getAccountLink (account_id)->hangup (call_id);
471

472
        removeCallAccount (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
473
    }
474

475
    if (_audiodriver->getLayerType() == PULSEAUDIO) {
476
477
        pulselayer = dynamic_cast<PulseLayer *> (getAudioDriver());
    }
478

479
    return returnValue;
jpbl's avatar
jpbl committed
480
481
}

Alexandre Savard's avatar
Alexandre Savard committed
482
483
484
485
486
487
488

bool
ManagerImpl::hangupConference (const ConfID& id)
{
    _debug ("ManagerImpl::hangupConference()\n");

    Conference *conf;
489
    ConferenceMap::iterator iter_conf = _conferencemap.find (id);
Alexandre Savard's avatar
Alexandre Savard committed
490
491
492

    AccountID currentAccountId;

493
    // Call* call = NULL;
Alexandre Savard's avatar
Alexandre Savard committed
494
495


496
497
    if (iter_conf != _conferencemap.end()) {
        conf = iter_conf->second;
Alexandre Savard's avatar
Alexandre Savard committed
498

499
500
        ParticipantSet participants = conf->getParticipantList();
        ParticipantSet::iterator iter_participant = participants.begin();
Alexandre Savard's avatar
Alexandre Savard committed
501

502
503
        while (iter_participant != participants.end()) {
            _debug ("ManagerImpl::hangupConference participant %s\n", (*iter_participant).c_str());
Alexandre Savard's avatar
Alexandre Savard committed
504

505
            hangupCall (*iter_participant);
Alexandre Savard's avatar
Alexandre Savard committed
506

507
508
509
            iter_participant++;

        }
Alexandre Savard's avatar
Alexandre Savard committed
510
511
512

    }

513
514
    switchCall ("");

Alexandre Savard's avatar
Alexandre Savard committed
515
516
517
518
    return true;
}


yanmorin's avatar
   
yanmorin committed
519
//THREAD=Main
520
bool
yanmorin's avatar
   
yanmorin committed
521
ManagerImpl::cancelCall (const CallID& id)
jpbl's avatar
jpbl committed
522
{
Emmanuel Milou's avatar
Emmanuel Milou committed
523
524
    AccountID accountid;
    bool returnValue;
yanmorin's avatar
   
yanmorin committed
525

526
    stopTone (true);
527

Emmanuel Milou's avatar
Emmanuel Milou committed
528
    /* Direct IP to IP call */
529

Emmanuel Milou's avatar
Emmanuel Milou committed
530
    if (getConfigFromCall (id) == Call::IPtoIP) {
531
        returnValue = SIPVoIPLink::instance (AccountNULL)->cancel (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
532
533
534
    }

    /* Classic call, attached to an account */
535
    else {
536
537
        accountid = getAccountFromCall (id);

Emmanuel Milou's avatar
Emmanuel Milou committed
538
        if (accountid == AccountNULL) {
539
            _debug ("! Manager Cancel Call: Call doesn't exists\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
540
541
            return false;
        }
542
543
544
545

        returnValue = getAccountLink (accountid)->cancel (id);

        removeCallAccount (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
546
    }
547

Emmanuel Milou's avatar
Emmanuel Milou committed
548
    // it could be a waiting call?
549
550
551
    removeWaitingCall (id);

    switchCall ("");
Emmanuel Milou's avatar
Emmanuel Milou committed
552
553

    return returnValue;
jpbl's avatar
jpbl committed
554
555
}

yanmorin's avatar
   
yanmorin committed
556
//THREAD=Main
557
bool
558
ManagerImpl::onHoldCall (const CallID& call_id)
jpbl's avatar
jpbl committed
559
{
560
    AccountID account_id;
Emmanuel Milou's avatar
Emmanuel Milou committed
561
    bool returnValue;
yanmorin's avatar
   
yanmorin committed
562

563
    _debug ("ManagerImpl::onHoldCall(%s)\n", call_id.c_str());
564

565
    stopTone (true);
566

567
568
    CallID current_call_id = getCurrentCallId();

569
    _debug ("    onHoldCall: try to put call %s on hold\n", call_id.c_str());
570

571
    /* Direct IP to IP call */
572

573
    if (getConfigFromCall (call_id) == Call::IPtoIP) {
574
        returnValue = SIPVoIPLink::instance (AccountNULL)-> onhold (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
575
    }
576

Emmanuel Milou's avatar
Emmanuel Milou committed
577
    /* Classic call, attached to an account */
578
    else {
579
        account_id = getAccountFromCall (call_id);
580

581
582
583
584
        if (account_id == AccountNULL) {
            _debug ("    onHoldCall: Account ID %s or callid %s doesn't exists\n", account_id.c_str(), call_id.c_str());
            return false;
        }
585

586
        returnValue = getAccountLink (account_id)->onhold (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
587
    }
588

589
    removeWaitingCall (call_id);
590

591
    // keeps current call id if the action is not holding this call or a new outgoing call
592

593
594
595
    if (current_call_id == call_id) {

        switchCall ("");
596
    }
597

598
    if (_dbus) _dbus->getCallManager()->callStateChanged (call_id, "HOLD");
599

Emmanuel Milou's avatar
Emmanuel Milou committed
600
    return returnValue;
yanmorin's avatar
   
yanmorin committed
601
602
603
}

//THREAD=Main
604
bool
605
ManagerImpl::offHoldCall (const CallID& call_id)
yanmorin's avatar
   
yanmorin committed
606
{
607

608
    AccountID account_id;
609
    bool returnValue, is_rec;
Emmanuel Milou's avatar
Emmanuel Milou committed
610
    std::string codecName;
611

612
    is_rec = false;
613
614

    _debug ("ManagerImpl::offHoldCall(%s)\n", call_id.c_str());
615

616
    stopTone (false);
617

618
    CallID current_call_id = getCurrentCallId();
619

620
    //Place current call on hold if it isn't
621
622
623
624
625
626
627
628
629
630

    if (hasCurrentCall()) {
        // if this is not a conferenceand this and is not a conference participant
        if (!isConference (current_call_id) && !participToConference (current_call_id)) {
            _debug ("    offHoldCall: put current call (%s) on hold\n", current_call_id.c_str());
            onHoldCall (current_call_id);
        } else if (isConference (current_call_id) && !participToConference (call_id)) {
            _debug ("    offHoldCall Put current conference (%s) on hold\n", current_call_id.c_str());
            detachParticipant (default_id, current_call_id);
        }
Emmanuel Milou's avatar
Emmanuel Milou committed
631
    }
632

633
    // switch current call id to id since sipvoip link need it to amke a call
634
    // switchCall(id);
635

Emmanuel Milou's avatar
Emmanuel Milou committed
636
    /* Direct IP to IP call */
637
    if (getConfigFromCall (call_id) == Call::IPtoIP) {
638
        // is_rec = SIPVoIPLink::instance (AccountNULL)-> isRecording (call_id);
639
        returnValue = SIPVoIPLink::instance (AccountNULL)-> offhold (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
640
    }
alexandresavard's avatar
alexandresavard committed
641

Emmanuel Milou's avatar
Emmanuel Milou committed
642
    /* Classic call, attached to an account */
643
    else {
644
        account_id = getAccountFromCall (call_id);
645

646
        if (account_id == AccountNULL) {
647
            _debug ("Manager OffHold Call: Call doesn't exists\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
648
649
            return false;
        }
650

651
        _debug ("Setting OFFHOLD, Account %s, callid %s\n", account_id.c_str(), call_id.c_str());
652

653
        is_rec = getAccountLink (account_id)->getCall (call_id)->isRecording();
654
        returnValue = getAccountLink (account_id)->offhold (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
655
    }
alexandresavard's avatar
alexandresavard committed
656

657

658
    if (_dbus) {
659
        if (is_rec)
660
            _dbus->getCallManager()->callStateChanged (call_id, "UNHOLD_RECORD");
661
        else
662
            _dbus->getCallManager()->callStateChanged (call_id, "UNHOLD_CURRENT");
663

Emmanuel Milou's avatar
Emmanuel Milou committed
664
    }
665

666
667
668
    if (participToConference (call_id)) {

        AccountID currentAccountId;
669
670
        Call* call = NULL;

671
672
673
674
675
676
677
678
        currentAccountId = getAccountFromCall (call_id);
        call = getAccountLink (currentAccountId)->getCall (call_id);

        switchCall (call->getConfId());

    } else {
        switchCall (call_id);
        _audiodriver->flushMain();
679
    }
680

681

682
    // codecName = getCurrentCodecName (call_id);
683
    // _debug("ManagerImpl::hangupCall(): broadcast codec name %s \n",codecName.c_str());
684

685
    // if (_dbus) _dbus->getCallManager()->currentSelectedCodec (call_id,codecName.c_str());
686
687


Emmanuel Milou's avatar
Emmanuel Milou committed
688
    return returnValue;
jpbl's avatar
jpbl committed
689
690
}

yanmorin's avatar
   
yanmorin committed
691
//THREAD=Main
692
bool
693
ManagerImpl::transferCall (const CallID& call_id, const std::string& to)
jpbl's avatar
jpbl committed
694
{
Emmanuel Milou's avatar
Emmanuel Milou committed
695
696
    AccountID accountid;
    bool returnValue;
697

698
    stopTone (true);
699

700
701
    CallID current_call_id = getCurrentCallId();

Emmanuel Milou's avatar
Emmanuel Milou committed
702
    /* Direct IP to IP call */
703

704
705
    if (getConfigFromCall (call_id) == Call::IPtoIP) {
        returnValue = SIPVoIPLink::instance (AccountNULL)-> transfer (call_id, to);
Emmanuel Milou's avatar
Emmanuel Milou committed
706
707
708
    }

    /* Classic call, attached to an account */
709
    else {
710
        accountid = getAccountFromCall (call_id);
711

Emmanuel Milou's avatar
Emmanuel Milou committed
712
        if (accountid == AccountNULL) {
713
            _debug ("! Manager Transfer Call: Call doesn't exists\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
714
715
            return false;
        }
716

717
        returnValue = getAccountLink (accountid)->transfer (call_id, to);
718

719
        removeCallAccount (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
720
    }
721

722
    removeWaitingCall (call_id);
723

724
    if (participToConference (call_id)) {
725

726
        Conference *conf = getConferenceFromCallID (call_id);
727

728
729
730
        if (conf != NULL) {
            // remove this participant
            removeParticipant (call_id);
731

732
733
734
735
736
737
            processRemainingParticipant (current_call_id, conf);
        }
    } else {
        // we are not participating to a conference, current call switched to ""
        if (!isConference (current_call_id))
            switchCall ("");
738
739
740
    }

    if (_dbus) _dbus->getCallManager()->callStateChanged (call_id, "HUNGUP");
741

Emmanuel Milou's avatar
Emmanuel Milou committed
742
    return returnValue;
jpbl's avatar
jpbl committed
743
744
}

745
746
void ManagerImpl::transferFailed()
{
747
    if (_dbus) _dbus->getCallManager()->transferFailed();
748
749
750
751
}

void ManagerImpl::transferSucceded()
{
752
    if (_dbus) _dbus->getCallManager()->transferSucceded();
753
754
755
756

}


yanmorin's avatar
   
yanmorin committed
757
//THREAD=Main : Call:Incoming
758
bool
yanmorin's avatar