managerimpl.cpp 118 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
54
#define DIRECT_IP_CALL	"IP CALL"
jpbl's avatar
jpbl committed
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
ManagerImpl::ManagerImpl (void) :
	_hasTriedToRegister(false), _config(), _currentCallId2(),
			_currentCallMutex(), _codecBuilder(NULL), _audiodriver(NULL),
			_dtmfKey(NULL), _codecDescriptorMap(), _toneMutex(),
			_telephoneTone(NULL), _audiofile(), _spkr_volume(0),
			_mic_volume(0), _mutex(), _dbus(NULL), _waitingCall(),
			_waitingCallMutex(), _nbIncomingWaitingCall(0), _path(""),
			_exist(0), _setupLoaded(false), _callAccountMap(),
			_callAccountMapMutex(), _callConfigMap(), _accountMap(),
			_directIpAccount(NULL), _cleaner(NULL), _history(NULL) {

	// initialize random generator for call id
	srand(time(NULL));

	_cleaner = new NumberCleaner();
	_history = new HistoryManager();
79

yanmorin's avatar
   
yanmorin committed
80
#ifdef TEST
81
82
83
84
	testAccountMap();
	loadAccountMap();
	testCallAccountMap();
	unloadAccountMap();
yanmorin's avatar
   
yanmorin committed
85
86
#endif

87
88
	// should be call before initConfigFile
	// loadAccountMap();, called in init() now.
jpbl's avatar
jpbl committed
89
90
91
}

// never call if we use only the singleton...
92
93
ManagerImpl::~ManagerImpl (void) {
	// terminate();
94
95
96
97
	delete _cleaner; _cleaner = NULL;
	delete _history; _history = NULL;

	_debug ("Manager: %s stop correctly.", PROGNAME);
jpbl's avatar
jpbl committed
98
99
}

100
void ManagerImpl::init () {
101

102
103
	// Load accounts, init map
	loadAccountMap();
104

105
	initVolume();
106

107
	if (_exist == 0) {
108
		_warn ("Manager: Cannot create config file in your home directory");
109
	}
110

111
	initAudioDriver();
jpbl's avatar
jpbl committed
112

113
	selectAudioDriver();
114

115
116
	// Initialize the list of supported audio codecs
	initAudioCodec();
117

118
	AudioLayer *audiolayer = getAudioDriver();
jpbl's avatar
jpbl committed
119

120
121
	if (audiolayer != 0) {
		unsigned int sampleRate = audiolayer->getSampleRate();
jpbl's avatar
jpbl committed
122

123
		_debugInit ("Manager: Load telephone tone");
124
125
		std::string country = getConfigString(PREFERENCES, ZONE_TONE);
		_telephoneTone = new TelephoneTone(country, sampleRate);
126

127
		_debugInit ("Manager: Loading DTMF key");
128
129
		_dtmfKey = new DTMF(sampleRate);
	}
130

131
132
	if (audiolayer == 0)
		audiolayer->stopStream();
133

134
135
	// Load the history
	_history->load_history(getConfigInt(PREFERENCES, CONFIG_HISTORY_LIMIT));
jpbl's avatar
jpbl committed
136
137
}

138
void ManagerImpl::terminate () {
139
140

	_debug ("Manager: Terminate ");
141
	saveConfig();
jpbl's avatar
jpbl committed
142

143
	unloadAccountMap();
144

145
	_debug ("Manager: Unload DTMF key");
146
	delete _dtmfKey;
jpbl's avatar
jpbl committed
147

148
149
150
151
152
	_debug("Manager: Unload telephone tone");
	delete _telephoneTone; _telephoneTone = NULL;

	_debug ("Manager: Unload audio driver");
	delete _audiodriver; _audiodriver = NULL;
jpbl's avatar
jpbl committed
153

154
155
	_debug ("Manager: Unload telephone tone");
	delete _telephoneTone; _telephoneTone = NULL;
156

157
	_debug ("Manager: Unload audio codecs ");
158
	_codecDescriptorMap.deleteHandlePointer();
159

jpbl's avatar
jpbl committed
160
161
}

162
163
bool ManagerImpl::isCurrentCall (const CallID& callId) {
	return (_currentCallId2 == callId ? true : false);
jpbl's avatar
jpbl committed
164
165
}

166
167
bool ManagerImpl::hasCurrentCall () {
	// _debug ("ManagerImpl::hasCurrentCall current call ID = %s", _currentCallId2.c_str());
168

169
170
171
	if (_currentCallId2 != "") {
		return true;
	}
172

173
	return false;
jpbl's avatar
jpbl committed
174
175
}

176
const CallID&
177
178
ManagerImpl::getCurrentCallId () {
	return _currentCallId2;
jpbl's avatar
jpbl committed
179
180
}

181
182
183
184
void ManagerImpl::switchCall (const CallID& id) {
	ost::MutexLock m(_currentCallMutex);
	_debug ("----- Switch current call id to %s -----", id.c_str());
	_currentCallId2 = id;
jpbl's avatar
jpbl committed
185
186
187
188
189
}

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

192
193
bool ManagerImpl::outgoingCall (const std::string& account_id,
		const CallID& call_id, const std::string& to) {
194

195
196
197
	std::string pattern, to_cleaned;
	Call::CallConfiguration callConfig;
	SIPVoIPLink *siplink;
198

199
	_debug ("Manager: New outgoing call %s to %s", call_id.c_str(), to.c_str());
200

201
	CallID current_call_id = getCurrentCallId();
Emmanuel Milou's avatar
Emmanuel Milou committed
202

203
204
205
206
207
	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("");
Emmanuel Milou's avatar
Emmanuel Milou committed
208

209
	to_cleaned = _cleaner->clean(to);
210

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

214
215
	// in any cases we have to detach from current communication
	if (hasCurrentCall()) {
216

217
		_debug ("Manager: Has current call (%s) put it onhold", current_call_id.c_str());
218
		// if this is not a conferenceand this and is not a conference participant
219
		if (!isConference(current_call_id) && !participToConference(current_call_id)) {
220
			onHoldCall(current_call_id);
221
		} else if (isConference(current_call_id) && !participToConference(call_id)) {
222
223
224
			detachParticipant(default_id, current_call_id);
		}
	}
225

226
	if (callConfig == Call::IPtoIP) {
227
		_debug ("Manager: Start IP2IP call");
228
229
		/* We need to retrieve the sip voiplink instance */
		siplink = SIPVoIPLink::instance("");
230

231
232
233
234
235
236
		if (siplink->new_ip_to_ip_call(call_id, to_cleaned)) {
			switchCall(call_id);
			return true;
		} else {
			callFailure(call_id);
		}
237

238
239
		return false;
	}
240

241
	_debug("Manager: Selecting account %s", account_id.c_str());
242
	if (!accountExists(account_id)) {
243
		_error ("Manager: Error: Account doesn't exist in new outgoing call");
244
245
		return false;
	}
246

247
	if (getAccountFromCall(call_id) != AccountNULL) {
248
		_error ("Manager: Error: Call id already exists in outgoing call");
249
250
		return false;
	}
251

252
	_debug ("Manager: Adding Outgoing Call %s on account %s", call_id.data(), account_id.data());
253
	associateCallToAccount(call_id, account_id);
254

255
	if (getAccountLink(account_id)->newOutgoingCall(call_id, to_cleaned)) {
256
257
	    switchCall(call_id);
	    return true;
258
	} else {
259
260
	    callFailure(call_id);
	    _debug ("Manager: Error: An error occur, the call was not created");
261
	}
262

263
	return false;
jpbl's avatar
jpbl committed
264
265
}

yanmorin's avatar
   
yanmorin committed
266
//THREAD=Main : for outgoing Call
267
bool ManagerImpl::answerCall (const CallID& call_id) {
268

269
	_debug ("ManagerImpl: Answer call %s", call_id.c_str());
270

271
	stopTone();
272

273
274
	// store the current call id
	CallID current_call_id = getCurrentCallId();
275

276
	AccountID account_id = getAccountFromCall(call_id);
277

278
279
280
	if (account_id == AccountNULL) {
		_debug ("    answerCall: AccountId is null");
	}
281

282
	Call* call = NULL;
283

284
	call = getAccountLink(account_id)->getCall(call_id);
285

286
287
288
	if (call == NULL) {
		_debug ("    answerCall: Call is null");
	}
289

290
291
	// in any cases we have to detach from current communication
	if (hasCurrentCall()) {
292

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

296
297
298
299
300
		if (!isConference(current_call_id) && !participToConference(
				current_call_id)) {
			_debug ("    answerCall: Put the current call (%s) on hold", current_call_id.c_str());
			onHoldCall(current_call_id);
		}
301

302
303
304
305
306
307
		// 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");
			detachParticipant(default_id, current_call_id);
		}
308

309
	}
310

311
312
313
314
315
	if (!getAccountLink(account_id)->answer(call_id)) {
		// error when receiving...
		removeCallAccount(call_id);
		return false;
	}
Alexandre Savard's avatar
Alexandre Savard committed
316

317
318
319
	// if it was waiting, it's waiting no more
	if (_dbus)
		_dbus->getCallManager()->callStateChanged(call_id, "CURRENT");
Emmanuel Milou's avatar
Emmanuel Milou committed
320

321
322
	// std::string codecName = Manager::instance().getCurrentCodecName (call_id);
	// if (_dbus) _dbus->getCallManager()->currentSelectedCodec (call_id, codecName.c_str());
323

324
	removeWaitingCall(call_id);
325

326
327
	// if we dragged this call into a conference already
	if (participToConference(call_id)) {
328

329
330
		// AccountID currentAccountId;
		// Call* call = NULL;
331

332
333
		// currentAccountId = getAccountFromCall (call_id);
		// call = getAccountLink (currentAccountId)->getCall (call_id);
334

335
336
337
338
		switchCall(call->getConfId());
	} else {
		switchCall(call_id);
	}
339

340
	return true;
jpbl's avatar
jpbl committed
341
342
}

yanmorin's avatar
   
yanmorin committed
343
//THREAD=Main
344
bool ManagerImpl::hangupCall (const CallID& call_id) {
345
346
347

	_info("Manager: Hangup call %s", call_id.c_str());

348
349
350
	PulseLayer *pulselayer;
	AccountID account_id;
	bool returnValue = true;
Emmanuel Milou's avatar
Emmanuel Milou committed
351

352
353
	// store the current call id
	CallID current_call_id = getCurrentCallId();
354

355
	stopTone();
yanmorin's avatar
   
yanmorin committed
356

357
	/* Broadcast a signal over DBus */
358
	_debug ("Manager: Send DBUS call state change (HUNGUP) for id %s", call_id.c_str());
359

360
361
	if (_dbus)
		_dbus->getCallManager()->callStateChanged(call_id, "HUNGUP");
362

363
	if (participToConference(call_id)) {
364

365
		Conference *conf = getConferenceFromCallID(call_id);
366

367
368
369
		if (conf != NULL) {
			// remove this participant
			removeParticipant(call_id);
370

371
372
			processRemainingParticipant(current_call_id, conf);
		}
373

374
375
376
377
378
	} else {
		// we are not participating to a conference, current call switched to ""
		if (!isConference(current_call_id))
			switchCall("");
	}
379

380
381
382
383
384
385
386
	/* Direct IP to IP call */
	if (getConfigFromCall(call_id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)->hangup(call_id);
	}
	/* Classic call, attached to an account */
	else {
		account_id = getAccountFromCall(call_id);
387

388
		// Account may be NULL if call have not been sent yet
389
		if (account_id == AccountNULL) {
390
			_error ("Manager: Error: account id is NULL in hangup");
391
392
393
394
395
			returnValue = false;
		} else {
			returnValue = getAccountLink(account_id)->hangup(call_id);
			removeCallAccount(call_id);
		}
396
	}
397

398
	int nbCalls = getCallList().size();
399

400
	AudioLayer *audiolayer = getAudioDriver();
401

402
403
	// stop streams
	if (audiolayer && (nbCalls <= 0)) {
404
		_debug ("Manager: stop audio stream, ther is only %i call(s) remaining", nbCalls);
405
406
		audiolayer->stopStream();
	}
407

408
409
410
	if (_audiodriver->getLayerType() == PULSEAUDIO) {
		pulselayer = dynamic_cast<PulseLayer *> (getAudioDriver());
	}
411

412
	return returnValue;
jpbl's avatar
jpbl committed
413
414
}

415
bool ManagerImpl::hangupConference (const ConfID& id) {
416
417

	_debug ("Manager: Hangup conference %s", id.c_str());
Alexandre Savard's avatar
Alexandre Savard committed
418

419
420
	Conference *conf;
	ConferenceMap::iterator iter_conf = _conferencemap.find(id);
Alexandre Savard's avatar
Alexandre Savard committed
421

422
	AccountID currentAccountId;
Alexandre Savard's avatar
Alexandre Savard committed
423

424
	// Call* call = NULL;
Alexandre Savard's avatar
Alexandre Savard committed
425
426


427
428
	if (iter_conf != _conferencemap.end()) {
		conf = iter_conf->second;
Alexandre Savard's avatar
Alexandre Savard committed
429

430
431
		ParticipantSet participants = conf->getParticipantList();
		ParticipantSet::iterator iter_participant = participants.begin();
Alexandre Savard's avatar
Alexandre Savard committed
432

433
		while (iter_participant != participants.end()) {
434
			_debug ("Manager: Hangup onference participant %s", (*iter_participant).c_str());
Alexandre Savard's avatar
Alexandre Savard committed
435

436
			hangupCall(*iter_participant);
Alexandre Savard's avatar
Alexandre Savard committed
437

438
			iter_participant++;
439

440
		}
Alexandre Savard's avatar
Alexandre Savard committed
441

442
	}
Alexandre Savard's avatar
Alexandre Savard committed
443

444
	switchCall("");
445

446
	return true;
Alexandre Savard's avatar
Alexandre Savard committed
447
448
}

yanmorin's avatar
   
yanmorin committed
449
//THREAD=Main
450
451
452
bool ManagerImpl::cancelCall (const CallID& id) {
	AccountID accountid;
	bool returnValue;
yanmorin's avatar
   
yanmorin committed
453

454
455
	_debug("Manager: Cancel call");

456
	stopTone();
457

458
	/* Direct IP to IP call */
459

460
461
462
	if (getConfigFromCall(id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)->cancel(id);
	}
Emmanuel Milou's avatar
Emmanuel Milou committed
463

464
465
466
	/* Classic call, attached to an account */
	else {
		accountid = getAccountFromCall(id);
467

468
469
470
471
		if (accountid == AccountNULL) {
			_debug ("! Manager Cancel Call: Call doesn't exists");
			return false;
		}
472

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

475
476
		removeCallAccount(id);
	}
477

478
479
	// it could be a waiting call?
	removeWaitingCall(id);
480

481
	switchCall("");
Emmanuel Milou's avatar
Emmanuel Milou committed
482

483
	return returnValue;
jpbl's avatar
jpbl committed
484
485
}

yanmorin's avatar
   
yanmorin committed
486
//THREAD=Main
487
488
489
bool ManagerImpl::onHoldCall (const CallID& call_id) {
	AccountID account_id;
	bool returnValue;
yanmorin's avatar
   
yanmorin committed
490

491
	_debug ("Manager: Put call %s on hold", call_id.c_str());
492

493
	stopTone();
494

495
	CallID current_call_id = getCurrentCallId();
496

497

498
	/* Direct IP to IP call */
499

500
501
502
	if (getConfigFromCall(call_id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)-> onhold(call_id);
	}
503

504
505
506
	/* Classic call, attached to an account */
	else {
		account_id = getAccountFromCall(call_id);
507

508
		if (account_id == AccountNULL) {
509
			_debug ("Manager: Account ID %s or callid %s doesn't exists in call onHold", account_id.c_str(), call_id.c_str());
510
511
			return false;
		}
512

513
514
		returnValue = getAccountLink(account_id)->onhold(call_id);
	}
515

516
	removeWaitingCall(call_id);
517

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

520
	if (current_call_id == call_id) {
521

522
523
		switchCall("");
	}
524

525
526
	if (_dbus)
		_dbus->getCallManager()->callStateChanged(call_id, "HOLD");
527

528
	return returnValue;
yanmorin's avatar
   
yanmorin committed
529
530
531
}

//THREAD=Main
532
bool ManagerImpl::offHoldCall (const CallID& call_id) {
533

534
535
536
	AccountID account_id;
	bool returnValue, is_rec;
	std::string codecName;
537

538
	is_rec = false;
539

540
	_debug ("Manager: Put call %s off hold", call_id.c_str());
541

542
	stopTone();
543

544
	CallID current_call_id = getCurrentCallId();
545

546
	//Place current call on hold if it isn't
547

548
549
550
551
552
553
554
555
556
557
	if (hasCurrentCall()) {
		// if this is not a conferenceand this and is not a conference participant
		if (!isConference(current_call_id) && !participToConference(
				current_call_id)) {
			onHoldCall(current_call_id);
		} else if (isConference(current_call_id) && !participToConference(
				call_id)) {
			detachParticipant(default_id, current_call_id);
		}
	}
alexandresavard's avatar
alexandresavard committed
558

559
560
	// switch current call id to id since sipvoip link need it to amke a call
	// switchCall(id);
561

562
563
564
565
566
	/* Direct IP to IP call */
	if (getConfigFromCall(call_id) == Call::IPtoIP) {
		// is_rec = SIPVoIPLink::instance (AccountNULL)-> isRecording (call_id);
		returnValue = SIPVoIPLink::instance(AccountNULL)-> offhold(call_id);
	}
567

568
569
570
	/* Classic call, attached to an account */
	else {
		account_id = getAccountFromCall(call_id);
571

572
		if (account_id == AccountNULL) {
573
			_warn ("Manager: Error: Call doesn't exists in off hold");
574
575
			return false;
		}
alexandresavard's avatar
alexandresavard committed
576

577
		_debug ("Manager: Setting offhold, Account %s, callid %s", account_id.c_str(), call_id.c_str());
578

579
580
581
		is_rec = getAccountLink(account_id)->getCall(call_id)->isRecording();
		returnValue = getAccountLink(account_id)->offhold(call_id);
	}
582

583
584
585
586
587
	if (_dbus) {
		if (is_rec)
			_dbus->getCallManager()->callStateChanged(call_id, "UNHOLD_RECORD");
		else
			_dbus->getCallManager()->callStateChanged(call_id, "UNHOLD_CURRENT");
588

589
	}
590

591
	if (participToConference(call_id)) {
592

593
594
		AccountID currentAccountId;
		Call* call = NULL;
595

596
597
		currentAccountId = getAccountFromCall(call_id);
		call = getAccountLink(currentAccountId)->getCall(call_id);
598

599
		switchCall(call->getConfId());
600

601
602
603
604
	} else {
		switchCall(call_id);
		_audiodriver->flushMain();
	}
605

606
	return returnValue;
jpbl's avatar
jpbl committed
607
608
}

yanmorin's avatar
   
yanmorin committed
609
//THREAD=Main
610
611
612
bool ManagerImpl::transferCall (const CallID& call_id, const std::string& to) {
	AccountID accountid;
	bool returnValue;
613

614
	_info("Manager: Transfer call %s\n", call_id.c_str());
615

616
	CallID current_call_id = getCurrentCallId();
617

618
	// Direct IP to IP call
619
	if (getConfigFromCall(call_id) == Call::IPtoIP) {
620
		returnValue = SIPVoIPLink::instance(AccountNULL)-> transfer(call_id, to);
621
	}
622
	// Classic call, attached to an account
623
	else {
624
625

	accountid = getAccountFromCall(call_id);
626

627
		if (accountid == AccountNULL) {
628
			_warn ("Manager: Call doesn't exists");
629
630
			return false;
		}
631

632
		returnValue = getAccountLink(accountid)->transfer(call_id, to);
633

634
	}
635

636
	// remove waiting call in case we make transfer without even answer
637
	removeWaitingCall(call_id);
638

639
	return returnValue;
jpbl's avatar
jpbl committed
640
641
}

642
void ManagerImpl::transferFailed () {
643
644
645

	_debug("UserAgent: Transfer failed");

646
647
	if (_dbus)
		_dbus->getCallManager()->transferFailed();
648
649
}

650
void ManagerImpl::transferSucceded () {
651
652
653

	_debug("UserAgent: Transfer succeded");

654
655
	if (_dbus)
		_dbus->getCallManager()->transferSucceded();
656
657
658

}

yanmorin's avatar
   
yanmorin committed
659
//THREAD=Main : Call:Incoming
660
661
662
bool ManagerImpl::refuseCall (const CallID& id) {
	AccountID accountid;
	bool returnValue;
663

664
665
	_debug("Manager: Refuse call %s", id.c_str());

666
	CallID current_call_id = getCurrentCallId();
667

668
	stopTone();
669

670
	int nbCalls = getCallList().size();
671

672
	// AudioLayer* audiolayer = getAudioDriver();
673

674
	if (nbCalls <= 1) {
Emmanuel Milou's avatar
Emmanuel Milou committed
675
		_debug ("    refuseCall: stop audio stream, there is only %i call(s) remaining", nbCalls);
676

677
678
679
		AudioLayer* audiolayer = getAudioDriver();
		audiolayer->stopStream();
	}
Emmanuel Milou's avatar
Emmanuel Milou committed
680

681
	/* Direct IP to IP call */
682

683
684
685
	if (getConfigFromCall(id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)-> refuse(id);
	}
686

687
688
689
	/* Classic call, attached to an account */
	else {
		accountid = getAccountFromCall(id);
690

691
		if (accountid == AccountNULL) {
692
			_warn ("Manager: Call doesn't exists");
693
694
			return false;
		}
695

696
		returnValue = getAccountLink(accountid)->refuse(id);
697

698
699
		removeCallAccount(id);
	}
700

701
702
703
704
	// if the call was outgoing or established, we didn't refuse it
	// so the method did nothing
	if (returnValue) {
		removeWaitingCall(id);
705

706
707
708
		if (_dbus)
			_dbus->getCallManager()->callStateChanged(id, "HUNGUP");
	}
709

710
	return returnValue;
jpbl's avatar
jpbl committed
711
712
}

713
Conference*
714
ManagerImpl::createConference (const CallID& id1, const CallID& id2) {
Alexandre Savard's avatar
Alexandre Savard committed
715
	_debug ("Manager: Create conference with call %s and %s", id1.c_str(), id2.c_str());
716

717
	Conference* conf = new Conference();
718

719
720
	conf->add(id1);
	conf->add(id2);
721

Alexandre Savard's avatar
Alexandre Savard committed
722
723
724
	// Add conference to map
	_conferencemap.insert( std::pair<CallID, Conference*>(conf->getConfID(), conf));

725
726
	// broadcast a signal over dbus
	_dbus->getCallManager()->conferenceCreated(conf->getConfID());
727

728
	return conf;
729
730
}

731
void ManagerImpl::removeConference (const ConfID& conference_id) {
732

Alexandre Savard's avatar
Alexandre Savard committed
733
	_debug ("Manager: Remove conference %s", conference_id.c_str());
734

735
	Conference* conf = NULL;
736

Alexandre Savard's avatar
Alexandre Savard committed
737
	_debug ("Manager: number of participant: %d", (int) _conferencemap.size());
738
	ConferenceMap::iterator iter = _conferencemap.find(conference_id);
739

740
741
742
	if (iter != _conferencemap.end()) {
		conf = iter->second;
	}
743

744
	if (conf == NULL) {
Alexandre Savard's avatar
Alexandre Savard committed
745
		_error ("Manager: Error: Conference not found");
746
747
		return;
	}
748

749
	// We now need to bind the audio to the remain participant
750

Alexandre Savard's avatar
Alexandre Savard committed
751
	// Unbind main participant audio from conference
752
	_audiodriver->getMainBuffer()->unBindAll(default_id);
753

754
	ParticipantSet participants = conf->getParticipantList();