managerimpl.h 31 KB
Newer Older
jpbl's avatar
jpbl committed
1
/*
Adrien Béraud's avatar
Adrien Béraud committed
2
 *  Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
jpbl's avatar
jpbl committed
3 4
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
5
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
6
 *  Author: Guillaume Carmel-Archambault <guillaume.carmel-archambault@savoirfairelinux.com>
7
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
8
 *  Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
jpbl's avatar
jpbl committed
9 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 14 15 16 17 18 19 20 21
 *  (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
22
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
23 24 25 26 27 28 29 30 31 32 33
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
jpbl's avatar
jpbl committed
34 35
 */

36 37
#ifndef MANAGER_IMPL_H_
#define MANAGER_IMPL_H_
jpbl's avatar
jpbl committed
38

39 40 41 42
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

jpbl's avatar
jpbl committed
43 44
#include <string>
#include <vector>
Tristan Matthews's avatar
Tristan Matthews committed
45
#include <list>
yanmorin's avatar
 
yanmorin committed
46
#include <map>
47
#include <memory>
Adrien Béraud's avatar
Adrien Béraud committed
48
#include <mutex>
49
#include <random>
50
#include <atomic>
51

52
#include "conference.h"
Guillaume Roguez's avatar
Guillaume Roguez committed
53

54
#include "account_factory.h"
Guillaume Roguez's avatar
Guillaume Roguez committed
55
#include "call_factory.h"
jpbl's avatar
jpbl committed
56

57
#include "audio/audiolayer.h"
58
#include "audio/sound/tone.h"  // for Tone::TONEID declaration
59
#include "audio/codecs/audiocodecfactory.h"
60

61
#include "preferences.h"
62
#include "history/history.h"
63
#include "noncopyable.h"
64

Adrien Béraud's avatar
Adrien Béraud committed
65
namespace ring {
Guillaume Roguez's avatar
Guillaume Roguez committed
66 67 68 69

namespace Conf {
class YamlParser;
class YamlEmitter;
70 71
}

72
class PluginManager;
Guillaume Roguez's avatar
Guillaume Roguez committed
73 74 75
class AudioFile;
class DTMF;
class TelephoneTone;
76 77 78 79 80 81
class ConfigurationManager;
class PresenceManager;
class CallManager;
#ifdef RING_VIDEO
class VideoManager;
#endif
82

83
/** To send multiple string */
jpbl's avatar
jpbl committed
84 85
typedef std::list<std::string> TokenList;

86
/** To store conference objects by conference ids */
87
typedef std::map<std::string, std::shared_ptr<Conference> > ConferenceMap;
88

89 90
typedef std::set<std::string> CallIDSet;

91
static const char * const default_conf = "conf";
92

93 94
typedef std::set<std::string> CallIDSet;

95
/** Manager (controller) of Ring daemon */
96
class ManagerImpl {
97 98 99
    private:
        std::unique_ptr<PluginManager> pluginManager_;

Julien Bonjean's avatar
Julien Bonjean committed
100
    public:
101
        ManagerImpl();
102
        ~ManagerImpl();
Julien Bonjean's avatar
Julien Bonjean committed
103

104 105 106 107 108 109 110
        std::unique_ptr<ConfigurationManager> configurationManager_;
        std::unique_ptr<CallManager> callManager_;
        std::unique_ptr<PresenceManager> presenceManager_;

#ifdef RING_VIDEO
        std::unique_ptr<VideoManager> videoManager_;
#endif
Julien Bonjean's avatar
Julien Bonjean committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        /**
         * General preferences configuration
         */
        Preferences preferences;

        /**
         * Voip related preferences
         */
        VoipPreference voipPreferences;

        /**
         * Hook preferences
         */
        HookPreference hookPreference;

        /**
         * Audio preferences
         */
        AudioPreference audioPreference;

        /**
         * Shortcut preferences
         */
        ShortcutPreferences shortcutPreferences;

136 137 138 139
        // Manager should not be accessed until initialized.
        // FIXME this is an evil hack!
        static std::atomic_bool initialized;

Julien Bonjean's avatar
Julien Bonjean committed
140 141 142 143
        /**
         * Initialisation of thread (sound) and map.
         * Init a new VoIPLink, audio codec and audio driver
         */
144
        void init(const std::string &config_file);
Julien Bonjean's avatar
Julien Bonjean committed
145

146 147
        void setPath(const std::string &path);

148 149 150 151 152
        /*
         * Terminate all threads and exit DBus loop
         */
        void finish();

Julien Bonjean's avatar
Julien Bonjean committed
153 154 155 156 157
        /**
         * Accessor to audiodriver.
         * it's multi-thread and use mutex internally
         * @return AudioLayer*  The audio layer object
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
158
        std::shared_ptr<AudioLayer> getAudioDriver();
Julien Bonjean's avatar
Julien Bonjean committed
159

160 161
        void startAudioDriverStream();

Julien Bonjean's avatar
Julien Bonjean committed
162 163 164
        /**
         * Functions which occur with a user's action
         * Place a new call
Guillaume Roguez's avatar
Guillaume Roguez committed
165
         * @param accountId	The account to make the call with
166
         * @param call_id  The call identifier
Julien Bonjean's avatar
Julien Bonjean committed
167
         * @param to  The recipient of the call
168
         * @param conf_id The conference identifier if any
Julien Bonjean's avatar
Julien Bonjean committed
169 170 171
         * @return bool true on success
         *		  false otherwise
         */
172
        bool outgoingCall(const std::string&, const std::string&, const std::string&, const std::string& = "");
Julien Bonjean's avatar
Julien Bonjean committed
173 174 175 176 177 178

        /**
         * Functions which occur with a user's action
         * Answer the call
         * @param id  The call identifier
         */
179
        bool answerCall(const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
180 181 182 183 184 185

        /**
         * Functions which occur with a user's action
         * Hangup the call
         * @param id  The call identifier
         */
186
        bool hangupCall(const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
187 188 189 190 191 192 193


        /**
         * Functions which occur with a user's action
         * Hangup the conference (hangup every participants)
         * @param id  The call identifier
         */
194
        bool hangupConference(const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
195 196 197 198 199 200

        /**
         * Functions which occur with a user's action
         * Put the call on hold
         * @param id  The call identifier
         */
201
        bool onHoldCall(const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
202 203 204 205 206 207

        /**
         * Functions which occur with a user's action
         * Put the call off hold
         * @param id  The call identifier
         */
208
        bool offHoldCall(const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
209 210 211 212 213 214 215

        /**
         * Functions which occur with a user's action
         * Transfer the call
         * @param id  The call identifier
         * @param to  The recipient of the transfer
         */
216
        bool transferCall(const std::string& id, const std::string& to);
Julien Bonjean's avatar
Julien Bonjean committed
217

218 219 220 221 222
        /**
         * Attended transfer
         * @param The call id to be transfered
         * @param The target
         */
223
        bool attendedTransfer(const std::string& transferID, const std::string& targetID);
224

Julien Bonjean's avatar
Julien Bonjean committed
225 226 227
        /**
         * Notify the client the transfer is successful
         */
228
        void transferSucceeded();
Julien Bonjean's avatar
Julien Bonjean committed
229 230 231 232 233 234 235 236 237 238 239

        /**
         * Notify the client that the transfer failed
         */
        void transferFailed();

        /**
         * Functions which occur with a user's action
         * Refuse the call
         * @param id  The call identifier
         */
240
        bool refuseCall(const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
241 242 243 244 245 246

        /**
         * Create a new conference given two participant
         * @param the first participant ID
         * @param the second participant ID
         */
247 248
        std::shared_ptr<Conference>
        createConference(const std::string& id1, const std::string& id2);
Julien Bonjean's avatar
Julien Bonjean committed
249 250 251 252 253

        /**
         * Delete this conference
         * @param the conference ID
         */
254
        void removeConference(const std::string& conference_id);
Julien Bonjean's avatar
Julien Bonjean committed
255 256 257 258 259

        /**
         * Return the conference id for which this call is attached
         * @ param the call id
         */
260 261
        std::shared_ptr<Conference>
        getConferenceFromCallID(const std::string& call_id);
Julien Bonjean's avatar
Julien Bonjean committed
262 263 264 265 266

        /**
         * Hold every participant to a conference
         * @param the conference id
         */
267
        bool holdConference(const std::string& conference_id);
Julien Bonjean's avatar
Julien Bonjean committed
268 269 270 271 272

        /**
         * Unhold all conference participants
         * @param the conference id
         */
273
        bool unHoldConference(const std::string& conference_id);
Julien Bonjean's avatar
Julien Bonjean committed
274 275 276 277 278

        /**
         * Test if this id is a conference (usefull to test current call)
         * @param the call id
         */
279
        bool isConference(const std::string& call_id) const;
Julien Bonjean's avatar
Julien Bonjean committed
280 281

        /**
282
         * Test if a call id corresponds to a conference participant
Julien Bonjean's avatar
Julien Bonjean committed
283 284
         * @param the call id
         */
285
        bool isConferenceParticipant(const std::string& call_id);
Julien Bonjean's avatar
Julien Bonjean committed
286 287 288 289 290 291

        /**
         * Add a participant to a conference
         * @param the call id
         * @param the conference id
         */
292
        bool addParticipant(const std::string& call_id, const std::string& conference_id);
Julien Bonjean's avatar
Julien Bonjean committed
293 294 295 296 297

        /**
         * Bind the main participant to a conference (mainly called on a double click action)
         * @param the conference id
         */
298
        bool addMainParticipant(const std::string& conference_id);
Julien Bonjean's avatar
Julien Bonjean committed
299 300 301 302 303 304

        /**
         * Join two participants to create a conference
         * @param the fist call id
         * @param the second call id
         */
305 306
        bool joinParticipant(const std::string& call_id1,
                             const std::string& call_id2);
Julien Bonjean's avatar
Julien Bonjean committed
307

308 309 310 311 312
        /**
         * Create a conference from a list of participant
         * @param A vector containing the list of participant
         */
        void createConfFromParticipantList(const std::vector< std::string > &);
313

Julien Bonjean's avatar
Julien Bonjean committed
314 315 316 317 318
        /**
         * Detach a participant from a conference, put the call on hold, do not hangup it
         * @param call id
         * @param the current call id
         */
319
        bool detachParticipant(const std::string& call_id);
Julien Bonjean's avatar
Julien Bonjean committed
320 321 322 323 324

        /**
         * Remove the conference participant from a conference
         * @param call id
         */
325
        void removeParticipant(const std::string& call_id);
Julien Bonjean's avatar
Julien Bonjean committed
326 327 328 329

        /**
         * Join two conference together into one unique conference
         */
330
        bool joinConference(const std::string& conf_id1, const std::string& conf_id2);
Julien Bonjean's avatar
Julien Bonjean committed
331

332
        void addStream(Call& call);
Julien Bonjean's avatar
Julien Bonjean committed
333

334
        void removeStream(Call& call);
Julien Bonjean's avatar
Julien Bonjean committed
335 336 337 338

        /**
         * Save config to file
         */
339
        void saveConfig();
Julien Bonjean's avatar
Julien Bonjean committed
340 341 342 343

        /**
         * @return true if we tried to register once
         */
344
        bool hasTriedToRegister_;
Julien Bonjean's avatar
Julien Bonjean committed
345 346 347 348

        /**
         * Play a ringtone
         */
349
        void playTone();
Julien Bonjean's avatar
Julien Bonjean committed
350 351 352 353

        /**
         * Play a special ringtone ( BUSY ) if there's at least one message on the voice mail
         */
354
        void playToneWithMessage();
Julien Bonjean's avatar
Julien Bonjean committed
355 356 357 358

        /**
         * Acts on the audio streams and audio files
         */
359
        void stopTone();
Julien Bonjean's avatar
Julien Bonjean committed
360 361

        /**
362
         * Handle incoming call and notify user
Julien Bonjean's avatar
Julien Bonjean committed
363 364 365
         * @param call A call pointer
         * @param accountId an account id
         */
366
        void incomingCall(Call &call, const std::string& accountId);
Julien Bonjean's avatar
Julien Bonjean committed
367 368 369 370 371 372

        /**
         * Notify the user that the recipient of the call has answered and the put the
         * call in Current state
         * @param id  The call identifier
         */
373
        void peerAnsweredCall(Call& call);
Julien Bonjean's avatar
Julien Bonjean committed
374 375 376 377 378 379

        /**
         * Rings back because the outgoing call is ringing and the put the
         * call in Ringing state
         * @param id  The call identifier
         */
380
        void peerRingingCall(Call& call);
Julien Bonjean's avatar
Julien Bonjean committed
381 382 383 384 385

        /**
         * Put the call in Hungup state, remove the call from the list
         * @param id  The call identifier
         */
386
        void peerHungupCall(Call& call);
Julien Bonjean's avatar
Julien Bonjean committed
387

388
#if HAVE_INSTANT_MESSAGING
Julien Bonjean's avatar
Julien Bonjean committed
389 390 391 392 393
        /**
         * Notify the client with an incoming message
         * @param accountId	The account identifier
         * @param message The content of the message
         */
394
        void incomingMessage(const std::string& callID, const std::string& from, const std::string& message);
Julien Bonjean's avatar
Julien Bonjean committed
395

396 397

        /**
398
         * Send a new text message to the call, if participate to a conference, send to all participant.
399 400
         * @param callID	The call to send the message
         * @param message	The content of the message
401 402
        * @param from	        The sender of this message (could be another participant of a conference)
         */
403
        bool sendTextMessage(const std::string& callID, const std::string& message, const std::string& from);
404
#endif // HAVE_INSTANT_MESSAGING
405

Julien Bonjean's avatar
Julien Bonjean committed
406 407 408 409 410
        /**
         * Notify the client he has voice mails
         * @param accountId	  The account identifier
         * @param nb_msg The number of messages
         */
411
        void startVoiceMessageNotification(const std::string& accountId, int nb_msg);
Julien Bonjean's avatar
Julien Bonjean committed
412 413 414 415 416

        /**
         * ConfigurationManager - Send registration request
         * @param accountId The account to register/unregister
         * @param enable The flag for the type of registration
417 418
         *		 false for unregistration request
         *		 true for registration request
Julien Bonjean's avatar
Julien Bonjean committed
419
         */
420
        void sendRegister(const std::string& accountId, bool enable);
Julien Bonjean's avatar
Julien Bonjean committed
421 422 423 424 425

        /**
         * Get account list
         * @return std::vector<std::string> A list of accoundIDs
         */
426
        std::vector<std::string> getAccountList() const;
Julien Bonjean's avatar
Julien Bonjean committed
427 428 429 430

        /**
         * Set the account order in the config file
         */
431
        void setAccountsOrder(const std::string& order);
Julien Bonjean's avatar
Julien Bonjean committed
432 433 434 435 436 437

        /**
         * Retrieve details about a given account
         * @param accountID	  The account identifier
         * @return std::map< std::string, std::string > The account details
         */
438
        std::map<std::string, std::string> getAccountDetails(const std::string& accountID) const;
Julien Bonjean's avatar
Julien Bonjean committed
439

440 441 442 443 444 445 446
        /**
         * Retrieve volatile details such as recent registration errors
         * @param accountID The account identifier
         * @return std::map< std::string, std::string > The account volatile details
         */
        std::map<std::string, std::string> getVolatileAccountDetails(const std::string& accountID) const;

Julien Bonjean's avatar
Julien Bonjean committed
447 448 449 450 451
        /**
         * Retrieve details about a given call
         * @param callID	  The account identifier
         * @return std::map< std::string, std::string > The call details
         */
452
        std::map<std::string, std::string> getCallDetails(const std::string& callID);
Julien Bonjean's avatar
Julien Bonjean committed
453 454 455 456 457

        /**
         * Get call list
         * @return std::vector<std::string> A list of call IDs
         */
458
        std::vector<std::string> getCallList() const;
Julien Bonjean's avatar
Julien Bonjean committed
459 460 461 462 463 464

        /**
         * Retrieve details about a given call
         * @param callID	  The account identifier
         * @return std::map< std::string, std::string > The call details
         */
465
        std::map<std::string, std::string> getConferenceDetails(const std::string& callID) const;
Julien Bonjean's avatar
Julien Bonjean committed
466 467 468 469 470

        /**
         * Get call list
         * @return std::vector<std::string> A list of call IDs
         */
471
        std::vector<std::string> getConferenceList() const;
Julien Bonjean's avatar
Julien Bonjean committed
472 473 474 475 476 477


        /**
         * Get a list of participant to a conference
         * @return std::vector<std::string> A list of call IDs
         */
478
        std::vector<std::string> getParticipantList(const std::string& confID) const;
Julien Bonjean's avatar
Julien Bonjean committed
479

480 481 482 483 484 485
        /**
         * Get a list of the display names for everyone in a conference
         * @return std::vector<std::string> A list of display names
         */
        std::vector<std::string> getDisplayNames(const std::string& confID) const;

486 487
        std::string getConferenceId(const std::string& callID);

Julien Bonjean's avatar
Julien Bonjean committed
488 489 490 491 492 493 494
        /**
         * Save the details of an existing account, given the account ID
         * This will load the configuration map with the given data.
         * It will also register/unregister links where the 'Enabled' switched.
         * @param accountID	  The account identifier
         * @param details	  The account parameters
         */
495 496
        void setAccountDetails(const std::string& accountID,
                               const std::map<std::string, ::std::string > &details);
Julien Bonjean's avatar
Julien Bonjean committed
497 498 499 500 501 502

        /**
         * Add a new account, and give it a new account ID automatically
         * @param details The new account parameters
         * @return The account Id given to the new account
         */
503
        std::string addAccount(const std::map<std::string, std::string> &details);
Julien Bonjean's avatar
Julien Bonjean committed
504 505 506 507 508 509

        /**
         * Delete an existing account, unregister VoIPLink associated, and
         * purge from configuration.
         * @param accountID	The account unique ID
         */
510
        void removeAccount(const std::string& accountID);
Julien Bonjean's avatar
Julien Bonjean committed
511 512 513 514 515

        /**
         * Set input audio plugin
         * @param audioPlugin The audio plugin
         */
516
        void setAudioPlugin(const std::string& audioPlugin);
Julien Bonjean's avatar
Julien Bonjean committed
517 518

        /**
519 520
             * Set audio device
             * @param index The index of the soundcard
521
             * @param the type of stream, either PLAYBACK, CAPTURE, RINGTONE
522
             */
Guillaume Roguez's avatar
Guillaume Roguez committed
523
        void setAudioDevice(int index, DeviceType streamType);
Julien Bonjean's avatar
Julien Bonjean committed
524 525 526 527 528

        /**
         * Get list of supported audio output device
         * @return std::vector<std::string> A list of the audio devices supporting playback
         */
529
        std::vector<std::string> getAudioOutputDeviceList();
Julien Bonjean's avatar
Julien Bonjean committed
530 531 532 533 534

        /**
         * Get list of supported audio input device
         * @return std::vector<std::string> A list of the audio devices supporting capture
         */
535
        std::vector<std::string> getAudioInputDeviceList();
Julien Bonjean's avatar
Julien Bonjean committed
536 537

        /**
538
         * Get string array representing integer indexes of output, input, and ringtone device
Julien Bonjean's avatar
Julien Bonjean committed
539 540 541 542 543 544 545 546 547
         * @return std::vector<std::string> A list of the current audio devices
         */
        std::vector<std::string> getCurrentAudioDevicesIndex();

        /**
         * Get index of an audio device
         * @param name The string description of an audio device
         * @return int  His index
         */
548 549
        int getAudioInputDeviceIndex(const std::string &name);
        int getAudioOutputDeviceIndex(const std::string &name);
Julien Bonjean's avatar
Julien Bonjean committed
550 551 552 553 554

        /**
         * Get current alsa plugin
         * @return std::string  The Alsa plugin
         */
555
        std::string getCurrentAudioOutputPlugin() const;
Julien Bonjean's avatar
Julien Bonjean committed
556

557 558 559 560
        /**
         * Get the noise reduction engin state from
         * the current audio layer.
         */
561
        bool getNoiseSuppressState() const;
Julien Bonjean's avatar
Julien Bonjean committed
562

563 564 565 566
        /**
         * Set the noise reduction engin state in the current
         * audio layer.
         */
567
        void setNoiseSuppressState(bool state);
Julien Bonjean's avatar
Julien Bonjean committed
568

569 570
        bool isAGCEnabled() const;
        void setAGCState(bool enabled);
571

Julien Bonjean's avatar
Julien Bonjean committed
572 573 574 575 576 577
        /**
         * Ringtone option.
         * If ringtone is enabled, ringtone on incoming call use custom choice. If not, only standart tone.
         * @return int	1 if enabled
         *	        0 otherwise
         */
578
        int isRingtoneEnabled(const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
579 580 581 582 583

        /**
         * Set the ringtone option
         * Inverse current value
         */
584
        void ringtoneEnabled(const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
585

586 587 588
        /**
         * Get is always recording functionality
         */
589
        bool getIsAlwaysRecording() const;
590 591 592 593 594 595 596

        /**
         * Set is always recording functionality, every calls will then be set in RECORDING mode
         * once answered
         */
        void setIsAlwaysRecording(bool isAlwaysRec);

Julien Bonjean's avatar
Julien Bonjean committed
597 598 599 600
        /**
         * Set recording on / off
         * Start recording
         * @param id  The call identifier
601
         * Returns true if the call was set to record
Julien Bonjean's avatar
Julien Bonjean committed
602
         */
603
        bool toggleRecordingCall(const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
604 605 606 607

        /**
         * Return true if the call is currently recorded
         */
608
        bool isRecording(const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
609

610 611 612 613
        /**
         * Start playback fo a recorded file if and only if audio layer is not already started.
         * @param File path of the file to play
             */
614
        bool startRecordedFilePlayback(const std::string&);
615

616 617
        void recordingPlaybackSeek(const double value);

618 619 620 621
        /**
         * Stop playback of recorded file
         * @param File of the file to stop
         */
622 623
        void stopRecordedFilePlayback(const std::string&);

Julien Bonjean's avatar
Julien Bonjean committed
624 625 626 627
        /**
         * Set the maximum number of days to keep in the history
         * @param calls The number of days
         */
628
        void setHistoryLimit(int days);
Julien Bonjean's avatar
Julien Bonjean committed
629 630 631 632 633

        /**
         * Get the maximum number of days to keep in the history
         * @return double The number of days
         */
634
        int getHistoryLimit() const;
Julien Bonjean's avatar
Julien Bonjean committed
635 636 637

        /**
         * Configure the start-up option
638
         * @return int	1 if Ring should start in the system tray
Julien Bonjean's avatar
Julien Bonjean committed
639 640
         *	        0 otherwise
         */
641
        int isStartHidden();
Julien Bonjean's avatar
Julien Bonjean committed
642 643 644

        /**
         * Configure the start-up option
645
         * At startup, Ring can be displayed or start hidden in the system tray
Julien Bonjean's avatar
Julien Bonjean committed
646
         */
647
        void startHidden();
Julien Bonjean's avatar
Julien Bonjean committed
648 649 650 651

        /**
         * Get the audio manager
         * @return int The audio manager
652 653
         *		    "alsa"
         *		    "pulseaudio"
Julien Bonjean's avatar
Julien Bonjean committed
654
         */
655
        std::string getAudioManager() const;
Julien Bonjean's avatar
Julien Bonjean committed
656 657 658

        /**
         * Set the audio manager
659
         * @return true if api is now in use, false otherwise
Julien Bonjean's avatar
Julien Bonjean committed
660
         */
661
        bool setAudioManager(const std::string &api);
Julien Bonjean's avatar
Julien Bonjean committed
662

663
        /**
664 665
         * Callback called when the audio layer initialised with its
         * preferred format.
666
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
667
        void hardwareAudioFormatChanged(AudioFormat format);
Julien Bonjean's avatar
Julien Bonjean committed
668

669 670 671 672 673
        /**
         * Should be called by any component dealing with an external
         * audio source, indicating the format used so the mixer format
         * can be eventually adapted.
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
674
        void audioFormatUsed(AudioFormat format);
675

Julien Bonjean's avatar
Julien Bonjean committed
676 677 678 679
        /**
         * Handle audio sounds heard by a caller while they wait for their
         * connection to a called party to be completed.
         */
680
        void ringback();
Julien Bonjean's avatar
Julien Bonjean committed
681 682 683 684

        /**
         * Handle played music when an incoming call occurs
         */
685
        void playRingtone(const std::string& accountID);
Julien Bonjean's avatar
Julien Bonjean committed
686 687 688 689

        /**
         * Handle played music when a congestion occurs
         */
690
        void congestion();
Julien Bonjean's avatar
Julien Bonjean committed
691

692 693 694 695 696 697
        /**
         * Play the dtmf-associated sound
         * @param code  The pressed key
         */
        void playDtmf(char code);

Julien Bonjean's avatar
Julien Bonjean committed
698 699 700
        /**
         * Handle played sound when a call can not be conpleted because of a busy recipient
         */
701
        void callBusy(Call& call);
Julien Bonjean's avatar
Julien Bonjean committed
702 703 704 705

        /**
         * Handle played sound when a failure occurs
         */
706
        void callFailure(Call& call);
Julien Bonjean's avatar
Julien Bonjean committed
707 708 709 710 711

        /**
         * Retrieve the current telephone tone
         * @return AudioLoop*   The audio tone or 0 if no tone (init before calling this function)
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
712
        AudioLoop* getTelephoneTone();
Julien Bonjean's avatar
Julien Bonjean committed
713 714 715 716 717

        /**
         * Retrieve the current telephone file
         * @return AudioLoop* The audio file or 0 if the wav is stopped
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
718
        AudioLoop* getTelephoneFile();
Julien Bonjean's avatar
Julien Bonjean committed
719 720 721 722 723

        /**
         * @return true is there is one or many incoming call waiting
         * new call, not anwsered or refused
         */
724
        bool incomingCallsWaiting();
Julien Bonjean's avatar
Julien Bonjean committed
725 726 727

        /**
         * Return a new random callid that is not present in the list
728
         * @return std::string A brand new callid
Julien Bonjean's avatar
Julien Bonjean committed
729
         */
730
        std::string getNewCallID();
Julien Bonjean's avatar
Julien Bonjean committed
731

732 733 734 735 736 737
        /**
         * Get the current call
         * @return std::shared_ptr<Call> A call shared pointer (could be empty)
         */
        std::shared_ptr<Call> getCurrentCall() const;

Julien Bonjean's avatar
Julien Bonjean committed
738 739
        /**
         * Get the current call id
740
         * @return std::string	The call id or ""
Julien Bonjean's avatar
Julien Bonjean committed
741
         */
742
        const std::string getCurrentCallId() const;
Julien Bonjean's avatar
Julien Bonjean committed
743 744 745

        /**
         * Check if a call is the current one
746 747
         * @param call the new call
         * @return bool True if the call is the current
Julien Bonjean's avatar
Julien Bonjean committed
748
         */
749
        bool isCurrentCall(const Call& call) const;
Julien Bonjean's avatar
Julien Bonjean committed
750

751
        void initAudioDriver();
Julien Bonjean's avatar
Julien Bonjean committed
752

753
        /**
Adrien Béraud's avatar
Adrien Béraud committed
754
         * Load the accounts order set by the user from the dringrc config file
755 756
         * @return std::vector<std::string> A vector containing the account ID's
         */
757
        std::vector<std::string> loadAccountOrder() const;
758 759 760 761

        // map of codec (for configlist request)
        const AudioCodecFactory audioCodecFactory;

Julien Bonjean's avatar
Julien Bonjean committed
762
    private:
763 764
        void removeAccounts();

765
        bool parseConfiguration();
766

767 768
        // Set the ringtone or recorded call to be played
        void updateAudioFile(const std::string &file, int sampleRate);
769 770 771 772

        /**
         * Get the Call referred to by callID. If the Call does not exist, return NULL
         */
773
        std::shared_ptr<Call> getCallFromCallID(const std::string &callID);
774

775 776 777 778 779 780
        /**
         * Process remaining participant given a conference and the current call id.
         * Mainly called when a participant is detached or hagned up
         * @param current call id
         * @param conference pointer
         */
781
        void processRemainingParticipants(Conference &conf);
782

Julien Bonjean's avatar
Julien Bonjean committed
783
        /**
784
         * Create config directory in home user and return configuration file path
Julien Bonjean's avatar
Julien Bonjean committed
785
         */
786
        std::string retrieveConfigPath() const;
Julien Bonjean's avatar
Julien Bonjean committed
787

788 789
        void unsetCurrentCall();

790
        void switchCall(std::shared_ptr<Call> call);
Julien Bonjean's avatar
Julien Bonjean committed
791 792 793 794 795

        /*
         * Play one tone
         * @return false if the driver is uninitialize
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
796
        void playATone(Tone::TONEID toneId);
Julien Bonjean's avatar
Julien Bonjean committed
797 798

        /** Current Call ID */
799
        std::shared_ptr<Call> currentCall_ = nullptr;
Julien Bonjean's avatar
Julien Bonjean committed
800 801

        /** Protected current call access */
Adrien Béraud's avatar
Adrien Béraud committed
802
        std::mutex currentCallMutex_;
Julien Bonjean's avatar
Julien Bonjean committed
803 804

        /** Audio layer */
Guillaume Roguez's avatar
Guillaume Roguez committed
805
        std::shared_ptr<AudioLayer> audiodriver_{nullptr};
Julien Bonjean's avatar
Julien Bonjean committed
806 807

        // Main thread
Guillaume Roguez's avatar
Guillaume Roguez committed
808
        std::unique_ptr<DTMF> dtmfKey_;
Julien Bonjean's avatar
Julien Bonjean committed
809

810
        /** Buffer to generate DTMF */
Guillaume Roguez's avatar
Guillaume Roguez committed
811
        AudioBuffer dtmfBuf_;
812

Julien Bonjean's avatar
Julien Bonjean committed
813 814 815
        /////////////////////
        // Protected by Mutex
        /////////////////////
Adrien Béraud's avatar
Adrien Béraud committed
816
        std::mutex toneMutex_;
Guillaume Roguez's avatar
Guillaume Roguez committed
817 818
        std::unique_ptr<TelephoneTone> telephoneTone_;
        std::unique_ptr<AudioFile> audiofile_;
Julien Bonjean's avatar
Julien Bonjean committed
819 820

        // To handle volume control
821 822
        // short speakerVolume_;
        // short micVolume_;
Julien Bonjean's avatar
Julien Bonjean committed
823 824
        // End of sound variable

825 826 827
        /**
         * Mutex used to protect audio layer
         */
Adrien Béraud's avatar
Adrien Béraud committed
828
        std::mutex audioLayerMutex_;
Julien Bonjean's avatar
Julien Bonjean committed
829

830 831 832
        /**
         * Waiting Call Vectors
         */
833
        CallIDSet waitingCalls_;
Julien Bonjean's avatar
Julien Bonjean committed
834

835 836 837
        /**
         * Protect waiting call list, access by many voip/audio threads
         */
Adrien Béraud's avatar
Adrien Béraud committed
838
        std::mutex waitingCallsMutex_;
Julien Bonjean's avatar
Julien Bonjean committed
839 840 841

        /**
         * Add incoming callid to the waiting list
842
         * @param id std::string to add
Julien Bonjean's avatar
Julien Bonjean committed
843
         */
844
        void addWaitingCall(const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
845 846 847

        /**
         * Remove incoming callid to the waiting list
848
         * @param id std::string to remove
Julien Bonjean's avatar
Julien Bonjean committed
849
         */
850
        void removeWaitingCall(const std::string& id);
Julien Bonjean's avatar
Julien Bonjean committed
851 852 853 854

        /**
         * Path of the ConfigFile
         */
855
        std::string path_;
856

Julien Bonjean's avatar
Julien Bonjean committed
857
        /**
858
         * Load the account map from configuration
Julien Bonjean's avatar
Julien Bonjean committed
859
         */
Tristan Matthews's avatar
Tristan Matthews committed
860
        int loadAccountMap(const YAML::Node &node);
Julien Bonjean's avatar
Julien Bonjean committed
861 862

        /**
863
         * Instance of the RingBufferPool for the whole application
Julien Bonjean's avatar
Julien Bonjean committed
864
         *
865 866
         * In order to send signal to other parts of the application, one must pass through the RingBufferMananger.
         * Audio instances must be registered into the RingBufferMananger and bound together via the ManagerImpl.
Julien Bonjean's avatar
Julien Bonjean committed
867 868
         *
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
869
        std::unique_ptr<RingBufferPool> ringbufferpool_;
Julien Bonjean's avatar
Julien Bonjean committed
870 871 872 873

    public:

        /**
874
         * Return a pointer to the instance of the RingBufferPool
Julien Bonjean's avatar
Julien Bonjean committed
875
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
876
        RingBufferPool& getRingBufferPool() { return *ringbufferpool_; }
Julien Bonjean's avatar
Julien Bonjean committed
877 878 879 880 881

        /**
         * Tell if there is a current call processed
         * @return bool True if there is a current call
         */
882
        bool hasCurrentCall() const;
Julien Bonjean's avatar
Julien Bonjean committed
883

884 885 886 887
        CallManager* getCallManager();
        ConfigurationManager* getConfigurationManager();
        PresenceManager* getPresenceManager();

Adrien Béraud's avatar
Adrien Béraud committed
888
#ifdef RING_VIDEO
889
        VideoManager * getVideoManager();
890
#endif
891

892
        std::vector<std::map<std::string, std::string> > getHistory();
893
        void clearHistory();
894

Julien Bonjean's avatar
Julien Bonjean committed
895
        /**
896
         * Get an account pointer, looks for both SIP and IAX
Julien Bonjean's avatar
Julien Bonjean committed
897
         * @param accountID account ID to get
898
         * @return std::shared_ptr<Account> Shared pointer on an Account instance or nullptr if not found
899
         */
900 901 902 903
        template <class T=Account>
        std::shared_ptr<T> getAccount(const std::string& accountID) const {
            return accountFactory_.getAccount<T>(accountID);
        }
904

905 906 907 908
        template <class T=Account>
        std::vector<std::shared_ptr<T> > getAllAccounts() const {
            return accountFactory_.getAllAccounts<T>();
        }
909

910 911 912 913
        template <class T=Account>
        bool accountCount() const {
            return accountFactory_.accountCount<T>();
        }
Julien Bonjean's avatar
Julien Bonjean committed
914

915 916 917
        std::shared_ptr<Account> getIP2IPAccount() const {
            return accountFactory_.getIP2IPAccount();
        }
918

919 920 921 922
        // only used by test framework
        bool hasAccount(const std::string& accountID) {
            return accountFactory_.hasAccount(accountID);
        }
923

924
        /**