managerimpl.h 14.8 KB
Newer Older
jpbl's avatar
jpbl committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 *  Copyright (C) 2004-2005 Savoir-Faire Linux inc.
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __MANAGER_H__
#define __MANAGER_H__

yanmorin's avatar
   
yanmorin committed
24
25
//#define TEST

jpbl's avatar
jpbl committed
26
27
#include <string>
#include <vector>
yanmorin's avatar
   
yanmorin committed
28
#include <set>
yanmorin's avatar
   
yanmorin committed
29
#include <map>
jpbl's avatar
jpbl committed
30
31
32
33
34
#include <cc++/thread.h>

#include "stund/stun.h"
#include "observer.h"
#include "config/config.h"
yanmorin's avatar
   
yanmorin committed
35

yanmorin's avatar
   
yanmorin committed
36
#include "account.h"
yanmorin's avatar
   
yanmorin committed
37
#include "call.h"
jpbl's avatar
jpbl committed
38
39
40
41
42
43
44
45
46
47
48

#include "audio/tonelist.h" // for Tone::TONEID declaration
#include "audio/audiofile.h"
#include "audio/dtmf.h"
#include "audio/codecDescriptor.h"

class AudioLayer;
class CodecDescriptor;
class GuiFramework;
class TelephoneTone;
class VoIPLink;
yanmorin's avatar
   
yanmorin committed
49

jpbl's avatar
jpbl committed
50
51
52
53
#ifdef USE_ZEROCONF
class DNSService;
#endif

yanmorin's avatar
   
yanmorin committed
54
55
/**
 * Define a type for a AccountMap container
jpbl's avatar
jpbl committed
56
 */
yanmorin's avatar
   
yanmorin committed
57
58
typedef std::map<AccountID, Account*> AccountMap;
 
yanmorin's avatar
   
yanmorin committed
59
60
61
/**
 * Define a type for a CallID to AccountID Map inside ManagerImpl
 */
yanmorin's avatar
   
yanmorin committed
62
typedef std::map<CallID, AccountID> CallAccountMap;
yanmorin's avatar
   
yanmorin committed
63
64

/**
yanmorin's avatar
   
yanmorin committed
65
 * Define a type for CallID vector (waiting list, incoming not answered)
yanmorin's avatar
   
yanmorin committed
66
 */
yanmorin's avatar
   
yanmorin committed
67
68
typedef std::set<CallID> CallIDSet;

jpbl's avatar
jpbl committed
69
70
71
72
73
/**
 * To send multiple string
 */
typedef std::list<std::string> TokenList;

yanmorin's avatar
   
yanmorin committed
74
75
76
/**
 * Manager (controller) of sflphone daemon
 */
jpbl's avatar
jpbl committed
77
78
79
80
81
class ManagerImpl {
public:
  ManagerImpl (void);
  ~ManagerImpl (void);

yanmorin's avatar
   
yanmorin committed
82
  // Init a new VoIPLink, audio codec and audio driver
yanmorin's avatar
   
yanmorin committed
83
84
85
  /**
   * Initialisation of thread (sound) and map
   */
jpbl's avatar
jpbl committed
86
  void init (void);
yanmorin's avatar
   
yanmorin committed
87
88
89
90

  /**
   * Terminate all thread (sound, link) and clear map
   */
jpbl's avatar
jpbl committed
91
92
  void terminate (void);

yanmorin's avatar
   
yanmorin committed
93
94
95
96
97
  /**
   * Set the graphic user interface : only GuiServer right now
   * @param gui A GuiFramework gui implmentation
   */
  void setGui (GuiFramework* gui) { _gui = gui; }
jpbl's avatar
jpbl committed
98
99

	// Accessor to audiodriver
yanmorin's avatar
   
yanmorin committed
100
  // it's multi-thread and use mutex internally
101
  AudioLayer* getAudioDriver(void) const { return _audiodriver; }
jpbl's avatar
jpbl committed
102

yanmorin's avatar
   
yanmorin committed
103
104
105
  /**
   * Get a descriptor map of codec available
   */
jpbl's avatar
jpbl committed
106
107
  CodecDescriptorMap& getCodecDescriptorMap(void) {return _codecDescriptorMap;}

yanmorin's avatar
   
yanmorin committed
108
  /**
jpbl's avatar
jpbl committed
109
110
   * Functions which occur with a user's action
   */
yanmorin's avatar
   
yanmorin committed
111
112
113
114
115
116
117
  bool outgoingCall(const AccountID& accountId, const CallID& id, const std::string& to);
  bool answerCall(const CallID& id);
  bool hangupCall(const CallID& id);
  bool cancelCall(const CallID& id);
  bool onHoldCall(const CallID& id);
  bool offHoldCall(const CallID& id);
  bool transferCall(const CallID& id, const std::string& to);
jpbl's avatar
jpbl committed
118
119
  void mute();
  void unmute();
yanmorin's avatar
   
yanmorin committed
120
  bool refuseCall(const CallID& id);
jpbl's avatar
jpbl committed
121

yanmorin's avatar
   
yanmorin committed
122
  /** Save config on file */
jpbl's avatar
jpbl committed
123
  bool saveConfig (void);
yanmorin's avatar
   
yanmorin committed
124
125
126
127
128
129
130
131
132
133
134
135
  /**
  * Initialize action (main thread)
  * @param accountId Account to register
  * @return true if setRegister is call without failure, else return false
  */
  bool registerVoIPLink(const AccountID& accountId);
  /**
  * Unregister an account
  * @param accountId Account to unregister
  * @return true if the unregister method is send correctly
  */
  bool unregisterVoIPLink(const AccountID& accountId);
jpbl's avatar
jpbl committed
136

yanmorin's avatar
   
yanmorin committed
137
  bool sendTextMessage(const AccountID& accountId, const std::string& to, const std::string& message);
jpbl's avatar
jpbl committed
138
	
yanmorin's avatar
   
yanmorin committed
139
  /*
jpbl's avatar
jpbl committed
140
141
142
143
144
   * Handle choice of the DTMF-send-way
   *
   * @param   id: callid of the line.
   * @param   code: pressed key.
   */
yanmorin's avatar
   
yanmorin committed
145
146
  bool sendDtmf(const CallID& id, char code);
  bool playDtmf(char code);
jpbl's avatar
jpbl committed
147
  bool playTone ();
148
  void stopTone(bool stopAudio/*=true*/);
yanmorin's avatar
   
yanmorin committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

  // From links
  /**
   * When receiving a new incoming call, add it to the callaccount map
   * and notify user
   * @param call A call pointer
   * @param accountid an account id
   * @return true if the call was added correctly
   */
  bool incomingCall(Call* call, const AccountID& accountId);
  void peerAnsweredCall(const CallID& id);
  void peerRingingCall(const CallID& id);
  void peerHungupCall(const CallID& id);
  void incomingMessage(const AccountID& accountId, const std::string& message);

  void displayTextMessage (const CallID& id, const std::string& message);
  void displayErrorText (const CallID& id, const std::string& message);
  void displayError(const std::string& error);
  void displayStatus(const std::string& status);
jpbl's avatar
jpbl committed
168
169
  void displayConfigError(const std::string& message);

yanmorin's avatar
   
yanmorin committed
170
171
  void startVoiceMessageNotification(const AccountID& accountId, const std::string& nb_msg);
  void stopVoiceMessageNotification(const AccountID& accountId);
jpbl's avatar
jpbl committed
172

yanmorin's avatar
   
yanmorin committed
173
174
175
176
  /** Notify the user that registration succeeded  */
  void registrationSucceed(const AccountID& accountId);
  /** Notify the user that registration succeeded  */
  void registrationFailed(const AccountID& accountId);
jpbl's avatar
jpbl committed
177
178

  // configuration function requests
yanmorin's avatar
   
yanmorin committed
179
180
  /** Start events thread*/
  // TODO: receive account name
jpbl's avatar
jpbl committed
181
  bool getEvents();
yanmorin's avatar
   
yanmorin committed
182
183

  //
jpbl's avatar
jpbl committed
184
185
186
187
  bool getZeroconf(const std::string& sequenceId);
  bool attachZeroconfEvents(const std::string& sequenceId, Pattern::Observer& observer);
  bool detachZeroconfEvents(Pattern::Observer& observer);
  bool getCallStatus(const std::string& sequenceId);
188
189
190
191
192
  /** 
   * Get account list and status
   * @param return true if there is atleast one active
   */
  bool getAccountList(const std::string& sequenceId);
jpbl's avatar
jpbl committed
193
194
195
196
197
198
  bool getConfigAll(const std::string& sequenceId);
  bool getConfig(const std::string& section, const std::string& name, TokenList& arg);
  bool setConfig(const std::string& section, const std::string& name, const std::string& value);
  bool setConfig(const std::string& section, const std::string& name, int value);
  bool getConfigList(const std::string& sequenceId, const std::string& name);
  void selectAudioDriver(void);
199

yanmorin's avatar
   
yanmorin committed
200
201
202
203
204
205
206
  /** 
   * Set Audio Driver with switchName == audiodriver 
   * @param sflphoned internal parameter to change
   * @param message to return to the user
   * @return true if everything is ok
   */
  bool setSwitch(const std::string& switchName, std::string& message);
jpbl's avatar
jpbl committed
207
208
209

  // configuration function for extern
  // throw an Conf::ConfigTreeItemException if not found
yanmorin's avatar
   
yanmorin committed
210
  /** Get a int from the config tree */
jpbl's avatar
jpbl committed
211
  int getConfigInt(const std::string& section, const std::string& name);
yanmorin's avatar
   
yanmorin committed
212
  /** Get a string from the config tree */
jpbl's avatar
jpbl committed
213
214
  std::string getConfigString(const std::string& section, const std::string& name);

yanmorin's avatar
   
yanmorin committed
215
	/**
jpbl's avatar
jpbl committed
216
217
218
219
220
	 * Handle audio sounds heard by a caller while they wait for their 
	 * connection to a called party to be completed.
	 */
  void ringback ();

yanmorin's avatar
   
yanmorin committed
221
	/**
jpbl's avatar
jpbl committed
222
223
224
225
	 * Handle played music when an incoming call occurs
	 */
  void ringtone ();
  void congestion ();
yanmorin's avatar
   
yanmorin committed
226
227
  void callBusy(const CallID& id);
  void callFailure(const CallID& id);
jpbl's avatar
jpbl committed
228

yanmorin's avatar
   
yanmorin committed
229
  /** @return 0 if no tone (init before calling this function) */
jpbl's avatar
jpbl committed
230
  AudioLoop* getTelephoneTone();
yanmorin's avatar
   
yanmorin committed
231
  /** @return 0 if the wav is stopped */
jpbl's avatar
jpbl committed
232
233
234
235
236
237
238
  AudioLoop* getTelephoneFile();

  /**
   * @return true is there is one or many incoming call waiting
   * new call, not anwsered or refused
   */
  bool incomingCallWaiting(void);
yanmorin's avatar
   
yanmorin committed
239
	/**
jpbl's avatar
jpbl committed
240
241
	 * Notification of incoming call when you are already busy
	 */
yanmorin's avatar
   
yanmorin committed
242
243
  void notificationIncomingCall(void);

jpbl's avatar
jpbl committed
244
245
246
247
248
249
250
251
252
253
254

  /*
   * Inline functions to manage volume control
   * Read by main thread and AudioLayer thread
   * Write by main thread only
   */
  unsigned short getSpkrVolume(void) { return _spkr_volume; }
  void setSpkrVolume(unsigned short spkr_vol) {  _spkr_volume = spkr_vol; }
  unsigned short getMicVolume(void) {  return _mic_volume;  }
  void setMicVolume(unsigned short mic_vol) {    _mic_volume = mic_vol;   }

yanmorin's avatar
   
yanmorin committed
255
  // Manage information about firewall
jpbl's avatar
jpbl committed
256
  /*
yanmorin's avatar
   
yanmorin committed
257
258
259
260
   * Get information about firewall 
   * @param  stunSvrAddr: stun server
   * @param  port         port number to open to test the connection
   * @return true if the connection is successful
jpbl's avatar
jpbl committed
261
   */
yanmorin's avatar
   
yanmorin committed
262
263
  bool getStunInfo(StunAddress4& stunSvrAddr, int port);

yanmorin's avatar
   
yanmorin committed
264
265
  inline int getFirewallPort(void) 		{ return _firewallPort; }
  inline void setFirewallPort(int port) 	{ _firewallPort = port; }
jpbl's avatar
jpbl committed
266
267
  inline std::string getFirewallAddress (void) 	{ return _firewallAddr; }

yanmorin's avatar
   
yanmorin committed
268
269
270
271
272
  /**
   * If you are behind a NAT, you have to use STUN server, specified in 
   * STUN configuration(you can change this one by default) to give you an 
   * public IP address and assign a port number.
   * Note: Set firewall port/address retreive
yanmorin's avatar
   
yanmorin committed
273
274
   * @param svr  : serveur on which to send request
   * @param port : on which port we want to listen to
yanmorin's avatar
   
yanmorin committed
275
276
277
   * 
   * Return true if we are behind a NAT (without error)
   */
yanmorin's avatar
   
yanmorin committed
278
  bool behindNat(const std::string& svr, int port);
yanmorin's avatar
   
yanmorin committed
279
280

	/**
jpbl's avatar
jpbl committed
281
282
283
	 * Init default values for the different fields
	 */
  void initConfigFile (void);
yanmorin's avatar
   
yanmorin committed
284
285
286
287

  /**
   * Tell if the setup was already loaded
   */
jpbl's avatar
jpbl committed
288
289
  bool hasLoadedSetup() { return _setupLoaded; }
	
yanmorin's avatar
   
yanmorin committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  /** Return a new random callid that is not present in the list
   * @return a brand new callid
   */
  CallID getNewCallID();

  /**
   * Get the current call id
   * @return the call id or ""
   */
  const CallID& getCurrentCallId();

  /**
   * Check if a call is the current one
   * @param id the new callid
   * @return if the id is the current call
   */
  bool isCurrentCall(const CallID& callId);

jpbl's avatar
jpbl committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
private:
 /**
  * Create .PROGNAME directory in home user and create 
  * configuration tree from the settings file if this file exists.
  *
  * @return	0 if creating file failed
  *			1 if config-file exists
  *			2 if file doesn't exist yet.
  */
  int createSettingsPath (void);

  /*
   * Initialize audiocodec with config setting
   */
  void initAudioCodec(void);
	
  /*
   * Initialize audiodriver
   */
  void initAudioDriver(void);

  /*
   * Initialize zeroconf module and scanning
   */
  void initZeroconf(void);
  
  /*
   * Init the Gui interface (after setting it) inside setGui
   */
  void initGui();

  /*
   * Init the volume for speakers/micro from 0 to 100 value
   */ 
  void initVolume();

  /**
   * Configuration
   */
  bool getDirListing(const std::string& sequenceId, const std::string& path, int *nbFile);
  bool getAudioDeviceList(const std::string& sequenceId, int ioDeviceMask);
  Conf::ConfigTree _config;
  bool getCountryTones(const std::string& sequenceId);
  void sendCountryTone(const std::string& sequenceId, int index, const std::string& name);
352
353
  

jpbl's avatar
jpbl committed
354

yanmorin's avatar
   
yanmorin committed
355
356
357
358
359
360
361
362
363
  /**
   * Tell if there is a current call processed
   * @return true if there is a current call
   */
  bool hasCurrentCall();

  /**
   * Switch of current call id
   * @param id the new callid
jpbl's avatar
jpbl committed
364
   */
yanmorin's avatar
   
yanmorin committed
365
366
367
368
369
370
371
372
  void switchCall(const CallID& id);

  /** Current Call ID */
  CallID _currentCallId2;

  /** Protected current call access */
  ost::Mutex _currentCallMutex;

jpbl's avatar
jpbl committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

  /*
   * Play one tone
   * @return false if the driver is uninitialize
   */
  bool playATone(Tone::TONEID toneId);
  
  //
  // Multithread variable with extern accessor and change only inside the main thread
  //
  /** Vector of CodecDescriptor */
  CodecDescriptor* _codecBuilder;

  //
  // Sound variable
  //
389
  AudioLayer* _audiodriver;
jpbl's avatar
jpbl committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

  // Main thread
  DTMF* _dtmfKey;

  // map of codec (for configlist request)
  CodecDescriptorMap _codecDescriptorMap;

  /////////////////////
  // Protected by Mutex
  /////////////////////
  ost::Mutex _toneMutex;
  TelephoneTone* _telephoneTone;
  AudioFile _audiofile;

  // To handle volume control
  short _spkr_volume;
  short _mic_volume;
  short _mic_volume_before_mute;
  // End of sound variable
yanmorin's avatar
   
yanmorin committed
409

jpbl's avatar
jpbl committed
410
411
412
413
414
415
416
417
418
419
420

  // Multithread variable (protected by _mutex)
  // 
  /** Mutex to protect access to code section */
  ost::Mutex _mutex;

  //
  // Multithread variable (non protected)
  //
  GuiFramework* _gui;

yanmorin's avatar
   
yanmorin committed
421
422
423
424
425
426
  /** Waiting Call Vectors */
  CallIDSet _waitingCall;
  /** Protect waiting call list, access by many voip/audio threads */
  ost::Mutex _waitingCallMutex;
  /** Number of waiting call, synchronize with waitingcall callidvector */
  unsigned int _nbIncomingWaitingCall;
jpbl's avatar
jpbl committed
427
  /**
yanmorin's avatar
   
yanmorin committed
428
429
   * Add incoming callid to the waiting list
   * @param id CallID to add
jpbl's avatar
jpbl committed
430
   */
yanmorin's avatar
   
yanmorin committed
431
432
433
434
435
436
437
438
439
440
441
442
  void addWaitingCall(const CallID& id);
  /**
   * Remove incoming callid to the waiting list
   * @param id CallID to remove
   */
  void removeWaitingCall(const CallID& id);
  /**
   * Tell if a call is waiting and should be remove
   * @param id CallID to test
   * @return true if the call is waiting
   */
  bool isWaitingCall(const CallID& id);
jpbl's avatar
jpbl committed
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457

	/**
   * Path of the ConfigFile 
	 */
  std::string 	_path;
  int _exist;
  int _setupLoaded;

	// To handle firewall
  int _firewallPort;
  std::string _firewallAddr;

  // return false if exosip or the network checking failed
  bool initRegisterVoIPLink();
  // true if we tried to register Once
yanmorin's avatar
   
yanmorin committed
458
  bool _hasTriedToRegister;
jpbl's avatar
jpbl committed
459
460
461
462
463
464
465
466
467
468

  // tell if we have zeroconf is enabled
  int _hasZeroconf;

#ifdef USE_ZEROCONF
  // DNSService contain every zeroconf services
  //  configuration detected on the network
  DNSService *_DNSService;
#endif

yanmorin's avatar
   
yanmorin committed
469
470
471
472
473
474
475
476
477
478
479
  /** Map to associate a CallID to the good account */
  CallAccountMap _callAccountMap;
  /** Mutex to lock the call account map (main thread + voiplink thread) */
  ost::Mutex _callAccountMapMutex;

  /** Associate a new CallID to a AccountID
   * Protected by mutex
   * @param callID the new CallID not in the list yet
   * @param accountID the known accountID present in accountMap
   * @return true if the new association is create
   */
yanmorin's avatar
   
yanmorin committed
480
  bool associateCallToAccount(const CallID& callID, const AccountID& accountID);
yanmorin's avatar
   
yanmorin committed
481
482
483
484
485
486

  /** Return the AccountID from a CallID
   * Protected by mutex
   * @param callID the CallID in the list
   * @return the accountID associated or "" if the callID is not found
   */
yanmorin's avatar
   
yanmorin committed
487
  AccountID getAccountFromCall(const CallID& callID);
yanmorin's avatar
   
yanmorin committed
488
489
490
491
492
493

  /** Remove a CallID/AccountID association
   * Protected by mutex
   * @param callID the CallID to remove
   * @return true if association is removed
   */
yanmorin's avatar
   
yanmorin committed
494
  bool removeCallAccount(const CallID& callID);
yanmorin's avatar
   
yanmorin committed
495

yanmorin's avatar
   
yanmorin committed
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
  /** Contains a list of account (sip, aix, etc) and their respective voiplink/calls */
  AccountMap _accountMap;

  /**
   * Load the account from configuration
   * @return number of account
   */
  short loadAccountMap();

  /**
   * Unload the account (delete them)
   */
  void unloadAccountMap();

  /**
   * Tell if an account exists
   * @param accountID account ID check
   */
yanmorin's avatar
   
yanmorin committed
514
  bool accountExists(const AccountID& accountID);
yanmorin's avatar
   
yanmorin committed
515
516
517
518
519
520

  /**
   * Get an account pointer
   * @param accountID account ID to get
   * @param the account pointer or 0
   */
yanmorin's avatar
   
yanmorin committed
521
522
523
524
525
526
527
528
  Account* getAccount(const AccountID& accountID);

  /**
   * Get the voip link from the account pointer
   * @param accountID account ID to get
   * @param the voip link from the account pointer or 0
   */
  VoIPLink* getAccountLink(const AccountID& accountID);
yanmorin's avatar
   
yanmorin committed
529

yanmorin's avatar
   
yanmorin committed
530
531
532
533
534
535
  /**
   * load default account variable for each protocol
   */
  void initConfigAccount();
  

yanmorin's avatar
   
yanmorin committed
536
537
  #ifdef TEST
  bool testCallAccountMap();
yanmorin's avatar
   
yanmorin committed
538
  bool testAccountMap();
yanmorin's avatar
   
yanmorin committed
539
  #endif
yanmorin's avatar
   
yanmorin committed
540

jpbl's avatar
jpbl committed
541
542
543
};

#endif // __MANAGER_H__