managerimpl.h 13.6 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 <map>
jpbl's avatar
jpbl committed
29
30
31
32
33
34
#include <cc++/thread.h>

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

yanmorin's avatar
   
yanmorin committed
37
//#include "audio/audiodevice.h"
jpbl's avatar
jpbl committed
38
39
40
41
42
43
44
45
46
47
48
49
50
#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
51

jpbl's avatar
jpbl committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifdef USE_ZEROCONF
class DNSService;
#endif

#define	NOTIFICATION_LEN	250
// Status
#define CONNECTED_STATUS	"Connected"
#define LOGGED_IN_STATUS	"Logged in"
#define RINGING_STATUS		"Ringing"
#define TRYING_STATUS		"Trying ..."
#define HANGUP_STATUS       "Hang up"
#define ONHOLD_STATUS       "On hold ..."
#define TRANSFER_STATUS     "Transfer to:"
#define MUTE_ON_STATUS		"Mute on"
#define ENTER_NUMBER_STATUS "Enter Phone Number:"

/*
 * Define a type for a list of call
 */
typedef std::vector< Call* > CallVector;

yanmorin's avatar
   
yanmorin committed
73
74
75
76
/**
 * Define a type for a CallID to AccountID Map inside ManagerImpl
 */
typedef std::map<CALLID, AccountID> CallAccountMap;
yanmorin's avatar
   
yanmorin committed
77
78
79
80
81

/**
 * Define a type for a AccountMap container
 */
typedef std::map<AccountID, Account*> AccountMap;
yanmorin's avatar
   
yanmorin committed
82
 
jpbl's avatar
jpbl committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
 * To send multiple string
 */
typedef std::list<std::string> TokenList;

class ManagerImpl {
public:
  ManagerImpl (void);
  ~ManagerImpl (void);

	// Init a new VoIPLink, audio codec and audio driver
  void init (void);
  void terminate (void);

	// Set the graphic user interface
  void setGui (GuiFramework* gui);

	// Accessor to audiodriver
        // it's multi-thread and use mutex internally
  AudioLayer* getAudioDriver(void) const { return _audiodriverPA ;}

  // Codec Descriptor
  CodecDescriptorMap& getCodecDescriptorMap(void) {return _codecDescriptorMap;}

  /* 
   * Attribute a new random id for a new call 
   * and check if it's already attributed to existing calls. 
   * If not exists, returns 'id' otherwise return 0 
   */ 
  CALLID generateNewCallId (void);

  /*
   * Add a new call at the end of the CallVector with identifiant 'id'
   */
yanmorin's avatar
   
yanmorin committed
117
  Call* pushBackNewCall (CALLID id, Call::CallType type);
jpbl's avatar
jpbl committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  void callSetInfo(CALLID id, const std::string& name, const std::string& number);
  bool callCanBeAnswered(CALLID id);
  bool callCanBeClosed(CALLID id);
  bool callIsOnHold(CALLID id);
	
  /*
   * Functions which occur with a user's action
   */
  int outgoingCall (const std::string& to);
  int hangupCall (CALLID id);
  int cancelCall (CALLID id);
  int answerCall (CALLID id);
  int onHoldCall (CALLID id);
  int offHoldCall (CALLID id);
  int transferCall (CALLID id, const std::string& to);
  void mute();
  void unmute();
  int refuseCall (CALLID id);

  bool saveConfig (void);
  bool registerVoIPLink (void);
  bool unregisterVoIPLink (void);

  bool sendTextMessage(const std::string& account, const std::string& to, const std::string& message);
	
  /**
   * Handle choice of the DTMF-send-way
   *
   * @param   id: callid of the line.
   * @param   code: pressed key.
   */
  bool sendDtmf (CALLID id, char code);
  bool playDtmf (char code);
  bool playTone ();
  void stopTone();
  CALLID getCurrentCallId() { ost::MutexLock m(_mutex); return _currentCallId; }

  int incomingCall (CALLID id, const std::string& name, const std::string& number);
  void peerAnsweredCall (CALLID id);
  int peerRingingCall (CALLID id);
  int peerHungupCall (CALLID id);
  void incomingMessage(const std::string& message);

  void displayTextMessage (CALLID id, const std::string& message);
  void displayErrorText (CALLID id, const std::string& message);
  void displayError (const std::string& error);
  void displayStatus (const std::string& status);
  void displayConfigError(const std::string& message);

  void startVoiceMessageNotification (const std::string& nb_msg);
  void stopVoiceMessageNotification (void);

  void registrationSucceed();
  void registrationFailed();

  // configuration function requests
  bool getEvents();
  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);
  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);
  bool setSwitch(const std::string& switchName);

  // configuration function for extern
  // throw an Conf::ConfigTreeItemException if not found
  int getConfigInt(const std::string& section, const std::string& name);
  std::string getConfigString(const std::string& section, const std::string& name);

	/*
	 * Handle audio sounds heard by a caller while they wait for their 
	 * connection to a called party to be completed.
	 */
  void ringback ();

	/*
	 * Handle played music when an incoming call occurs
	 */
  void ringtone ();
  void congestion ();
  void callBusy(CALLID id);
  void callFailure(CALLID id);

  // return 0 if no tone (init before calling this function)
  AudioLoop* getTelephoneTone();
  // return 0 if the wav is stopped
  AudioLoop* getTelephoneFile();

  /**
   * @return true is there is one or many incoming call waiting
   * new call, not anwsered or refused
   */
  bool incomingCallWaiting(void);
	/*
	 * Notification of incoming call when you are already busy
	 */
  void notificationIncomingCall (void);

yanmorin's avatar
   
yanmorin committed
221
222
223
224
225
226
227
  /*
   * 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
   */
  bool getStunInfo(StunAddress4& stunSvrAddr, int port);
jpbl's avatar
jpbl committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
  bool useStun (void);
	
  /*
   * 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;   }

  /*
   * Manage information about firewall
   */
  inline int getFirewallPort 		(void) 		{ return _firewallPort; }
  inline void setFirewallPort 	(int port) 	{ _firewallPort = port; }
  inline std::string getFirewallAddress (void) 	{ return _firewallAddr; }

	/*
	 * Init default values for the different fields
	 */
  void initConfigFile (void);
  bool hasLoadedSetup() { return _setupLoaded; }
	

  enum REGISTRATION_STATE {
    UNREGISTERED,
    REGISTERED,
    FAILED
  };

  REGISTRATION_STATE getRegistrationState() { return _registerState; }

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

  /*
   * Erase the Call(id) from the CallVector
   * Protected by other function by _mutex lock
   */
  void deleteCall	(CALLID id);
  Call* getCall (CALLID id);
  void setCurrentCallId(CALLID id);
  void removeCallFromCurrent(CALLID id);

  /*
   * 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
  //
  AudioLayer* _audiodriverPA;

  // 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
  //

  // Multithread variable (protected by _mutex)
  // 
  /** Mutex to protect access to code section */
  ost::Mutex _mutex;
  /* Vector of calls  */
  CallVector _callVector;
  // Current callid : protected implicitely by function using _mutex 
  CALLID _currentCallId; 
  // functions that set mutex:
  //  terminate, pushBackNewCall, generateNewCallId, outgoingCall (after gen/push)
  //  hangupCall, cancelCall, answerCall, onHoldCall, offHoldCall, transferCall, refuseCall, 
  //  callSetInfo, callCanBeClosed, callCanBeAnswered, callIsOnHold, incomingCall,
  //  peerAnsweredCall, peerRingingCall, peerHunguCall, callBusy, callFailure
  //  getCallStatus, getCurrentCallId
  // functions that are called by those functions
  //  getCall, deleteCall, stopTone, switchCall, decWaitingCall, setCurrentCallId, getAudioDriver, ringtone, incWaitingCall

  // warning, incomingCallWaiting | incWaitingCall | decWaitingCall are prtected by _incomingCallMutex
  

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

  /**
   * Multithreaded
   * Incomings Call:
   */
  ost::Mutex _incomingCallMutex;
  unsigned int _nbIncomingWaitingCall;
  void incWaitingCall(void);
  void decWaitingCall(void);
	

	/**
   * 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
  bool    _hasTriedToRegister;
  // Register state
  REGISTRATION_STATE _registerState;


  void switchCall(CALLID id);

  // 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// CALLID

  /** 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
   */
  bool associateCallToAccount(CALLID callID, const AccountID& accountID);

  /** 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
   */
  AccountID getAccountFromCall(const CALLID callID);

  /** Remove a CallID/AccountID association
   * Protected by mutex
   * @param callID the CallID to remove
   * @return true if association is removed
   */
  bool removeCallAccount(CALLID callID);

  /** Return a new random callid that is not present in the list
   * @return a brand new callid
   */
  CALLID getNewCallID();

yanmorin's avatar
   
yanmorin committed
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
  /** 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
472
  bool accountExists(const AccountID& accountID);
yanmorin's avatar
   
yanmorin committed
473
474
475
476
477
478

  /**
   * Get an account pointer
   * @param accountID account ID to get
   * @param the account pointer or 0
   */
yanmorin's avatar
   
yanmorin committed
479
480
481
482
483
484
485
486
  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
487

yanmorin's avatar
   
yanmorin committed
488
489
490
491
492
493
  /**
   * load default account variable for each protocol
   */
  void initConfigAccount();
  

yanmorin's avatar
   
yanmorin committed
494
495
  #ifdef TEST
  bool testCallAccountMap();
yanmorin's avatar
   
yanmorin committed
496
  bool testAccountMap();
yanmorin's avatar
   
yanmorin committed
497
  #endif
yanmorin's avatar
   
yanmorin committed
498

jpbl's avatar
jpbl committed
499
500
501
};

#endif // __MANAGER_H__