manager.h 29.5 KB
Newer Older
1
/*
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2004-2019 Savoir-faire Linux Inc.
Guillaume Roguez's avatar
Guillaume Roguez committed
3
 *
4 5 6 7 8 9 10
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
 *  Author: Guillaume Carmel-Archambault <guillaume.carmel-archambault@savoirfairelinux.com>
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
 *  Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
 *  Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
Julien Bonjean's avatar
Julien Bonjean committed
11
 *
jpbl's avatar
jpbl committed
12 13
 *  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
14
 *  the Free Software Foundation; either version 3 of the License, or
jpbl's avatar
jpbl committed
15
 *  (at your option) any later version.
Julien Bonjean's avatar
Julien Bonjean committed
16
 *
jpbl's avatar
jpbl committed
17 18 19 20
 *  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.
Julien Bonjean's avatar
Julien Bonjean committed
21
 *
jpbl's avatar
jpbl committed
22 23
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
24
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
jpbl's avatar
jpbl committed
25 26
 */

27
#pragma once
jpbl's avatar
jpbl committed
28

29 30 31
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
jpbl's avatar
jpbl committed
32

33 34 35 36
#include "account_factory.h"
#include "call_factory.h"
#include "preferences.h"
#include "audio/audiolayer.h"
37
#include "scheduled_executor.h"
38

39 40 41 42 43 44
#include <string>
#include <vector>
#include <map>
#include <memory>
#include <atomic>
#include <functional>
45
#include <algorithm>
Guillaume Roguez's avatar
Guillaume Roguez committed
46

Adrien Béraud's avatar
Adrien Béraud committed
47
namespace jami {
48 49 50 51

namespace video {
class SinkClient;
}
52
class RingBufferPool;
Philippe Gorley's avatar
Philippe Gorley committed
53
struct VideoManager;
54 55
class Conference;
class AudioLoop;
56
class IceTransportFactory;
57
class DataTransferFacade;
58
class JamiAccount;
59

60 61
static constexpr size_t DRING_ID_MAX_VAL = 9007199254740992;

Philippe Gorley's avatar
Philippe Gorley committed
62
/** Manager (controller) of daemon */
63 64 65 66
class Manager {
    public:
        static Manager& instance();

67 68
        void setAutoAnswer(bool enable);

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        /**
         * General preferences configuration
         */
        Preferences preferences;

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

        /**
         * Hook preferences
         */
        HookPreference hookPreference;

        /**
         * Audio preferences
         */
        AudioPreference audioPreference;

        /**
         * Shortcut preferences
         */
        ShortcutPreferences shortcutPreferences;

Adrien Béraud's avatar
Adrien Béraud committed
94
#ifdef ENABLE_VIDEO
95 96 97 98
        /**
         * Video preferences
         */
        VideoPreferences videoPreferences;
99
#endif
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        // Manager should not be accessed until initialized.
        // FIXME this is an evil hack!
        static std::atomic_bool initialized;

        /**
         * Initialisation of thread (sound) and map.
         * Init a new VoIPLink, audio codec and audio driver
         */
        void init(const std::string &config_file);

        /*
         * Terminate all threads and exit DBus loop
         */
        void finish() noexcept;

        /**
         * Accessor to audiodriver.
         * it's multi-thread and use mutex internally
         * @return AudioLayer*  The audio layer object
         */
        std::shared_ptr<AudioLayer> getAudioDriver();

        void startAudioDriverStream();

        /**
         * Functions which occur with a user's action
         * Place a new call
128
         * @param accountId The account to make the call with
129 130 131 132 133 134
         * @param to  The recipient of the call
         * @param conf_id The conference identifier if any
         * @return id The call ID on success, empty string otherwise
         */
        std::string outgoingCall(const std::string& accountId,
                                 const std::string& to,
135 136
                                 const std::string& conf_id = "",
                                 const std::map<std::string, std::string>& volatileCallDetails = {});
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

        /**
         * Functions which occur with a user's action
         * Answer the call
         * @param id  The call identifier
         */
        bool answerCall(const std::string& id);

        /**
         * Functions which occur with a user's action
         * Hangup the call
         * @param id  The call identifier
         */
        bool hangupCall(const std::string& id);

        /**
         * Functions which occur with a user's action
         * Hangup the conference (hangup every participants)
         * @param id  The call identifier
         */
        bool hangupConference(const std::string& id);

        /**
         * Functions which occur with a user's action
         * Put the call on hold
         * @param id  The call identifier
         */
        bool onHoldCall(const std::string& id);

        /**
         * Functions which occur with a user's action
         * Put the call off hold
         * @param id  The call identifier
         */
        bool offHoldCall(const std::string& id);

        /**
         * Functions which occur with a user's action
         * Put the media of a call on mute or unmute
         * @param callID  The call identifier
         * @param mediaType The media type; eg : AUDIO or VIDEO
         * @param is_muted true to mute, false to unmute
         */
        bool muteMediaCall(const std::string& callId, const std::string& mediaType, bool is_muted);

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

        /**
         * Attended transfer
Hugo Lefeuvre's avatar
Hugo Lefeuvre committed
192
         * @param The call id to be transferred
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
         * @param The target
         */
        bool attendedTransfer(const std::string& transferID, const std::string& targetID);

        /**
         * Notify the client the transfer is successful
         */
        void transferSucceeded();

        /**
         * 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
         */
        bool refuseCall(const std::string& id);

        /**
         * Delete this conference
         * @param the conference ID
         */
        void removeConference(const std::string& conference_id);

        /**
         * Return the conference id for which this call is attached
         * @ param the call id
         */
        std::shared_ptr<Conference>
        getConferenceFromCallID(const std::string& call_id);

        /**
         * Hold every participant to a conference
         * @param the conference id
         */
        bool holdConference(const std::string& conference_id);

        /**
         * Unhold all conference participants
         * @param the conference id
         */
        bool unHoldConference(const std::string& conference_id);

        /**
Hugo Lefeuvre's avatar
Hugo Lefeuvre committed
240
         * Test if this id is a conference (useful to test current call)
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
         * @param the call id
         */
        bool isConference(const std::string& call_id) const;

        /**
         * Test if a call id corresponds to a conference participant
         * @param the call id
         */
        bool isConferenceParticipant(const std::string& call_id);

        /**
         * Add a participant to a conference
         * @param the call id
         * @param the conference id
         */
        bool addParticipant(const std::string& call_id, const std::string& conference_id);

        /**
         * Bind the main participant to a conference (mainly called on a double click action)
         * @param the conference id
         */
        bool addMainParticipant(const std::string& conference_id);

        /**
         * Join two participants to create a conference
         * @param the fist call id
         * @param the second call id
         */
        bool joinParticipant(const std::string& call_id1,
                             const std::string& call_id2);

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

        /**
         * Detach a participant from a conference, put the call on hold, do not hangup it
         * @param call id
         * @param the current call id
         */
        bool detachParticipant(const std::string& call_id);

285 286 287 288 289 290
        /**
         * Detach the local participant from curent conference.
         * Remote participants are placed in hold.
         */
        bool detachLocalParticipant();

291 292 293 294 295 296 297 298 299 300 301
        /**
         * Remove the conference participant from a conference
         * @param call id
         */
        void removeParticipant(const std::string& call_id);

        /**
         * Join two conference together into one unique conference
         */
        bool joinConference(const std::string& conf_id1, const std::string& conf_id2);

302
        void addAudio(Call& call);
303

304
        void removeAudio(Call& call);
305 306 307 308 309

        /**
         * Save config to file
         */
        void saveConfig();
310
        void saveConfig(const std::shared_ptr<Account>& acc);
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

        /**
         * Play a ringtone
         */
        void playTone();

        /**
         * Play a special ringtone ( BUSY ) if there's at least one message on the voice mail
         */
        void playToneWithMessage();

        /**
         * Acts on the audio streams and audio files
         */
        void stopTone();

        /**
         * Handle incoming call and notify user
         * @param call A call pointer
         * @param accountId an account id
         */
        void incomingCall(Call &call, const std::string& accountId);

        /**
         * Notify the user that the recipient of the call has answered and the put the
         * call in Current state
         * @param id  The call identifier
         */
        void peerAnsweredCall(Call& call);

        /**
         * Rings back because the outgoing call is ringing and the put the
         * call in Ringing state
         * @param id  The call identifier
         */
        void peerRingingCall(Call& call);

        /**
         * Put the call in Hungup state, remove the call from the list
         * @param id  The call identifier
         */
        void peerHungupCall(Call& call);

        /**
         * Notify the client with an incoming message
356 357
         * @param accountId     The account identifier
         * @param messages A map if mime type as key and mime payload as value
358
         */
359 360 361
        void incomingMessage(const std::string& callID,
                             const std::string& from,
                             const std::map<std::string, std::string>& messages);
362 363 364

        /**
         * Send a new text message to the call, if participate to a conference, send to all participant.
365 366
         * @param callID        The call to send the message
         * @param message       A list of pair of mime types and payloads
367
         * @param from           The sender of this message (could be another participant of a conference)
368
         */
369 370 371
        void sendCallTextMessage(const std::string& callID,
                                 const std::map<std::string, std::string>& messages,
                                 const std::string& from, bool isMixed);
372 373 374

        /**
         * Notify the client he has voice mails
375
         * @param accountId   The account identifier
376 377 378 379 380 381 382 383
         * @param nb_msg The number of messages
         */
        void startVoiceMessageNotification(const std::string& accountId, int nb_msg);

        /**
         * ConfigurationManager - Send registration request
         * @param accountId The account to register/unregister
         * @param enable The flag for the type of registration
384 385
         *   false for unregistration request
         *   true for registration request
386 387 388
         */
        void sendRegister(const std::string& accountId, bool enable);

Adrien Béraud's avatar
Adrien Béraud committed
389
        uint64_t sendTextMessage(const std::string& accountID, const std::string& to,
390
                             const std::map<std::string, std::string>& payloads);
391

Adrien Béraud's avatar
Adrien Béraud committed
392 393
        int getMessageStatus(uint64_t id) const;
        int getMessageStatus(const std::string& accountID, uint64_t id) const;
Adrien Béraud's avatar
Adrien Béraud committed
394

395 396 397 398 399 400 401 402 403 404 405 406 407
        /**
         * Get account list
         * @return std::vector<std::string> A list of accoundIDs
         */
        std::vector<std::string> getAccountList() const;

        /**
         * Set the account order in the config file
         */
        void setAccountsOrder(const std::string& order);

        /**
         * Retrieve details about a given account
408
         * @param accountID   The account identifier
409 410 411 412 413 414 415 416 417 418 419 420 421
         * @return std::map< std::string, std::string > The account details
         */
        std::map<std::string, std::string> getAccountDetails(const std::string& accountID) const;

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

        /**
         * Retrieve details about a given call
422
         * @param callID      The account identifier
423 424 425 426 427
         * @return std::map< std::string, std::string > The call details
         */
        std::map<std::string, std::string> getCallDetails(const std::string& callID);

        /**
428 429
         * Get list of calls (internal subcalls are filter-out)
         * @return std::vector<std::string> A list of call IDs (without subcalls)
430 431 432 433 434
         */
        std::vector<std::string> getCallList() const;

        /**
         * Retrieve details about a given call
435
         * @param callID      The account identifier
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
         * @return std::map< std::string, std::string > The call details
         */
        std::map<std::string, std::string> getConferenceDetails(const std::string& callID) const;

        /**
         * Get call list
         * @return std::vector<std::string> A list of call IDs
         */
        std::vector<std::string> getConferenceList() const;


        /**
         * Get a list of participant to a conference
         * @return std::vector<std::string> A list of call IDs
         */
        std::vector<std::string> getParticipantList(const std::string& confID) const;

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

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

        /**
         * 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.
465 466
         * @param accountID   The account identifier
         * @param details     The account parameters
467 468 469 470
         */
        void setAccountDetails(const std::string& accountID,
                               const std::map<std::string, ::std::string > &details);

471 472
        void setAccountActive(const std::string& accountID, bool active);

473 474
        std::map<std::string, std::string> testAccountICEInitialization(const std::string& accountID);

475 476 477 478 479 480
        /**
         * Return a new random accountid that is not present in the list
         * @return A brand new accountid
         */
        std::string getNewAccountId();

481 482 483
        /**
         * Add a new account, and give it a new account ID automatically
         * @param details The new account parameters
484
         * @param accountId optionnal predetermined accountid to use
485 486
         * @return The account Id given to the new account
         */
487 488
        std::string addAccount(const std::map<std::string, std::string> &details,
                               const std::string& accountId = {});
489 490 491 492

        /**
         * Delete an existing account, unregister VoIPLink associated, and
         * purge from configuration.
493
         * If 'flush' argument is true, filesystem entries are also removed.
494
         * @param accountID The account unique ID
495
         */
496
        void removeAccount(const std::string& accountID, bool flush=false);
497

498 499
        void removeAccounts();

500 501 502 503 504 505 506
        /**
         * Set input audio plugin
         * @param audioPlugin The audio plugin
         */
        void setAudioPlugin(const std::string& audioPlugin);

        /**
507 508 509 510
         * Set audio device
         * @param index The index of the soundcard
         * @param the type of stream, either PLAYBACK, CAPTURE, RINGTONE
         */
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
        void setAudioDevice(int index, DeviceType streamType);

        /**
         * Get list of supported audio output device
         * @return std::vector<std::string> A list of the audio devices supporting playback
         */
        std::vector<std::string> getAudioOutputDeviceList();

        /**
         * Get list of supported audio input device
         * @return std::vector<std::string> A list of the audio devices supporting capture
         */
        std::vector<std::string> getAudioInputDeviceList();

        /**
         * Get string array representing integer indexes of output, input, and ringtone device
         * @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
         */
        int getAudioInputDeviceIndex(const std::string &name);
        int getAudioOutputDeviceIndex(const std::string &name);

        /**
         * Get current alsa plugin
         * @return std::string  The Alsa plugin
         */
        std::string getCurrentAudioOutputPlugin() const;

        /**
         * Get the noise reduction engin state from
         * the current audio layer.
         */
        bool getNoiseSuppressState() const;

        /**
         * Set the noise reduction engin state in the current
         * audio layer.
         */
        void setNoiseSuppressState(bool state);

        bool isAGCEnabled() const;
        void setAGCState(bool enabled);

        bool switchInput(const std::string& callid, const std::string& res);

        /**
         * Ringtone option.
         * If ringtone is enabled, ringtone on incoming call use custom choice. If not, only standart tone.
565 566
         * @return int  1 if enabled
         *          0 otherwise
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
         */
        int isRingtoneEnabled(const std::string& id);

        /**
         * Set the ringtone option
         * Inverse current value
         */
        void ringtoneEnabled(const std::string& id);

        /**
         * Get is always recording functionality
         */
        bool getIsAlwaysRecording() const;

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

        /**
         * Set recording on / off
         * Start recording
         * @param id  The call identifier
         * Returns true if the call was set to record
         */
        bool toggleRecordingCall(const std::string& id);

        /**
         * Return true if the call is currently recorded
         */
        bool isRecording(const std::string& id);

        /**
         * Start playback fo a recorded file if and only if audio layer is not already started.
         * @param File path of the file to play
603
         */
604 605 606 607 608 609 610
        bool startRecordedFilePlayback(const std::string&);

        void recordingPlaybackSeek(const double value);

        /**
         * Stop playback of recorded file
         */
611
        void stopRecordedFilePlayback();
612 613 614 615 616 617 618 619 620 621 622 623 624

        /**
         * Set the maximum number of days to keep in the history
         * @param calls The number of days
         */
        void setHistoryLimit(int days);

        /**
         * Get the maximum number of days to keep in the history
         * @return double The number of days
         */
        int getHistoryLimit() const;

625 626 627 628 629 630 631 632 633 634 635 636 637 638
        /**
         * Set ringing timeout (number of seconds after which a call will
         * enter BUSY state if not answered).
         * @param timeout in seconds
         */
        void setRingingTimeout(int timeout);

        /**
         * Get ringing timeout (number of seconds after which a call will
         * enter BUSY state if not answered).
         * @return timeout in seconds
         */
        int getRingingTimeout() const;

639 640 641
        /**
         * Get the audio manager
         * @return int The audio manager
642 643
         *          "alsa"
         *          "pulseaudio"
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
         */
        std::string getAudioManager() const;

        /**
         * Set the audio manager
         * @return true if api is now in use, false otherwise
         */
        bool setAudioManager(const std::string &api);

        /**
         * Callback called when the audio layer initialised with its
         * preferred format.
         */
        AudioFormat hardwareAudioFormatChanged(AudioFormat format);

        /**
         * Should be called by any component dealing with an external
         * audio source, indicating the format used so the mixer format
         * can be eventually adapted.
         * @returns the new format used by the main buffer.
         */
        AudioFormat audioFormatUsed(AudioFormat format);

        /**
         * 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 playRingtone(const std::string& accountID);

        /**
         * Handle played music when a congestion occurs
         */
        void congestion();

        /**
         * Play the dtmf-associated sound
         * @param code  The pressed key
         */
        void playDtmf(char code);

        /**
         * Handle played sound when a call can not be conpleted because of a busy recipient
         */
        void callBusy(Call& call);

        /**
         * Handle played sound when a failure occurs
         */
Guillaume Roguez's avatar
Guillaume Roguez committed
697
        void callFailure(Call& call);
698 699 700 701 702

        /**
         * Retrieve the current telephone tone
         * @return AudioLoop*   The audio tone or 0 if no tone (init before calling this function)
         */
703
        std::shared_ptr<AudioLoop> getTelephoneTone();
704 705 706 707 708

        /**
         * Retrieve the current telephone file
         * @return AudioLoop* The audio file or 0 if the wav is stopped
         */
709
        std::shared_ptr<AudioLoop> getTelephoneFile();
710 711 712

        /**
         * @return true is there is one or many incoming call waiting
Hugo Lefeuvre's avatar
Hugo Lefeuvre committed
713
         * new call, not answered or refused
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
         */
        bool incomingCallsWaiting();

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

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

        /**
         * Get the current call id
731
         * @return std::string  The call id or ""
732
         */
733
        const std::string& getCurrentCallId() const;
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749

        /**
         * Check if a call is the current one
         * @param call the new call
         * @return bool True if the call is the current
         */
        bool isCurrentCall(const Call& call) const;

        void initAudioDriver();

        /**
         * Load the accounts order set by the user from the dringrc config file
         * @return std::vector<std::string> A vector containing the account ID's
         */
        std::vector<std::string> loadAccountOrder() const;

750 751 752 753 754
        /**
        * Load the account map from configuration
        */
        int loadAccountMap(const YAML::Node& node);

Gasuleg's avatar
Gasuleg committed
755 756 757 758 759 760
        /**
         * Get the Call referred by callID. If the Call does not exist, return
         * empty std::shared_ptr<Call> instance
         */
        std::shared_ptr<Call> getCallFromCallID(const std::string &callID) const;

761 762 763
        /**
         * Return a pointer to the instance of the RingBufferPool
         */
764
        RingBufferPool& getRingBufferPool();
765 766 767 768 769 770 771 772

        /**
         * Tell if there is a current call processed
         * @return bool True if there is a current call
         */
        bool hasCurrentCall() const;

        /**
773
         * Get an account pointer, looks for account of type T
774 775 776 777 778
         * @param accountID account ID to get
         * @return std::shared_ptr<Account> Shared pointer on an Account instance or nullptr if not found
         */
        template <class T=Account>
        std::shared_ptr<T> getAccount(const std::string& accountID) const {
779
            return accountFactory.getAccount<T>(accountID);
780 781
        }

782 783 784 785
        /**
         * Get a list of account pointers of type T (baseclass Account)
         * @return a sorted vector of all accounts of type T
         */
786
        template <class T=Account>
787
        std::vector<std::shared_ptr<T>> getAllAccounts() const {
788 789
            const auto& account_order = loadAccountOrder();
            const auto& all_accounts = accountFactory.getAllAccounts<T>();
790
            std::vector<std::shared_ptr<T>> accountList;
791 792 793 794 795 796 797
            accountList.reserve(all_accounts.size());
            for (const auto& id : account_order) {
                if (auto acc = accountFactory.getAccount<T>(id))
                    accountList.push_back(acc);
            }
            for (const auto& account : all_accounts) {
                if (std::find(accountList.begin(), accountList.end(), account) == accountList.end())
798 799 800
                    accountList.emplace_back(account);
            }
            return accountList;
801 802 803 804
        }

        template <class T=Account>
        bool accountCount() const {
805
            return accountFactory.accountCount<T>();
806 807
        }

808 809 810 811 812 813 814 815
        template <class T>
        inline std::shared_ptr<T>
        findAccount(const std::function<bool(const std::shared_ptr<T>&)>& pred) {
            for (const auto& account : getAllAccounts<T>()) {
                if (pred(account))
                    return account;
            }
            return {};
816 817
        }

818 819 820
        // only used by test framework
        bool hasAccount(const std::string& accountID);

821 822 823 824 825
        /**
         * Send registration for all enabled accounts
         */
        void registerAccounts();

826 827 828 829 830
        /**
         * Send unregister for all enabled accounts
         */
        void unregisterAccounts();

831
        /**
Philippe Gorley's avatar
Philippe Gorley committed
832
         * Suspends audio processing if no calls remain, allowing
833 834 835 836 837 838 839 840 841 842 843 844 845 846
         * other applications to resume audio.
         * See:
         * https://projects.savoirfairelinux.com/issues/7037
        */
        void checkAudio();

        /**
         * Call periodically to poll for VoIP events */
        void
        pollEvents();

        /**
         * Create a new outgoing call
         * @param toUrl The address to call
847
         * @param accountId account to use
848 849 850 851
         * @return Call*  A shared pointer on a valid call.
         * @note This function raises VoipLinkException() on errors.
         */
        std::shared_ptr<Call> newOutgoingCall(const std::string& toUrl,
852
                                              const std::string& accountId,
853
                                              const std::map<std::string, std::string>& volatileCallDetails = {});
854 855 856

        CallFactory callFactory;

857
        IceTransportFactory& getIceTransportFactory();
858

859
        ScheduledExecutor& scheduler();
860

861 862
        void addTask(std::function<bool()>&& task);
        std::shared_ptr<Task> scheduleTask(std::function<void()>&& task, std::chrono::steady_clock::time_point when);
Adrien Béraud's avatar
Adrien Béraud committed
863

864 865
        std::map<std::string, std::string> getNearbyPeers(const std::string& accountID);

Adrien Béraud's avatar
Adrien Béraud committed
866
#ifdef ENABLE_VIDEO
867 868 869 870 871 872 873 874
        /**
         * Create a new SinkClient instance, store it in an internal cache as a weak_ptr
         * and return it as a shared_ptr. If a SinkClient is already stored for the given id,
         * this method returns this instance.
         * @param id SinkClient identifier as a string. Default is empty.
         * @param mixer true if the SinkCient is the sink of a VideoMixer node. Default is false.
         * @return share_ptr<SinkClient> A shared pointer on the created instance.
         */
875
        std::shared_ptr<video::SinkClient> createSinkClient(const std::string& id="", bool mixer=false);
876 877 878 879 880 881 882

        /**
         * Return an existing SinkClient instance as a shared_ptr associated to the given identifier.
         * Return an empty shared_ptr (nullptr) if nothing found.
         * @param id SinkClient identifier as a string.
         * @return share_ptr<SinkClient> A shared pointer on the found instance. Empty if not found.
         */
883
        std::shared_ptr<video::SinkClient> getSinkClient(const std::string& id);
884

885
        VideoManager& getVideoManager() const;
Adrien Béraud's avatar
Adrien Béraud committed
886
#endif // ENABLE_VIDEO
887

888
        std::atomic<unsigned> dhtLogLevel {0}; // default = disable
889
        AccountFactory accountFactory;
890

891 892
        std::unique_ptr<DataTransferFacade> dataTransfers;

893 894
        std::vector<DRing::Message> getLastMessages(const std::string& accountID, const uint64_t& base_timestamp);

895 896 897
private:
        Manager();
        ~Manager();
898

899 900
        struct ManagerPimpl;
        std::unique_ptr<ManagerPimpl> pimpl_;
901 902
};

903 904 905
// Helper to install a callback to be called once by the main event loop
template<typename Callback>
static void runOnMainThread(Callback&& cb) {
Adrien Béraud's avatar
Adrien Béraud committed
906
    Manager::instance().scheduler().run([cb = std::forward<Callback>(cb)]() mutable {
907 908 909 910
        cb();
    });
}

Adrien Béraud's avatar
Adrien Béraud committed
911
} // namespace jami