Ring Daemon 16.0.0
Loading...
Searching...
No Matches
manager.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#include "account_factory.h"
24#include "call_factory.h"
25#include "preferences.h"
27#include "scheduled_executor.h"
28#include "gittransport.h"
29#include <dhtnet/certstore.h>
30
31#include <algorithm>
32#include <atomic>
33#include <functional>
34#include <map>
35#include <memory>
36#include <optional>
37#include <string>
38#include <vector>
39
40#include "trace-tools.h"
41
42namespace asio {
43class io_context;
44}
45
46namespace dhtnet {
47class ChannelSocket;
48class IceTransportFactory;
49} // namespace dhtnet
50
51namespace jami {
52namespace video {
53class SinkClient;
54class VideoGenerator;
55} // namespace video
56class RingBufferPool;
57struct VideoManager;
58class Conference;
59class AudioLoop;
60class JamiAccount;
61class SIPVoIPLink;
62class JamiPluginManager;
63
65// TODO LIBJAMI_PUBLIC only if tests
67{
68private:
69 std::mt19937_64 rand_;
70
71public:
72 // TODO LIBJAMI_PUBLIC only if tests
73 static LIBJAMI_TEST_EXPORT Manager& instance();
74
75 void setAutoAnswer(bool enable);
76
81
86
91
92#ifdef ENABLE_PLUGIN
96 PluginPreferences pluginPreferences;
97#endif
98
99#ifdef ENABLE_VIDEO
103 VideoPreferences videoPreferences;
104#endif
105
106 // Manager should not be accessed until initialized.
107 // FIXME this is an evil hack!
108 static std::atomic_bool initialized;
109
110#if TARGET_OS_IOS
111 static bool isIOSExtension;
112#endif
113
114 static bool syncOnRegister;
115
116 static bool autoLoad;
117
122 void init(const std::filesystem::path& config_file, libjami::InitFlag flags);
123
124 /*
125 * Terminate all threads and exit DBus loop
126 */
127 void finish() noexcept;
128
129 void monitor(bool continuous);
130
131 std::vector<std::map<std::string, std::string>> getConnectionList(
132 const std::string& accountId, const std::string& conversationId);
133 std::vector<std::map<std::string, std::string>> getChannelList(const std::string& accountId,
134 const std::string& connectionId);
135
141 std::shared_ptr<AudioLayer> getAudioDriver();
142
143 inline std::unique_ptr<AudioDeviceGuard> startAudioStream(AudioDeviceType stream)
144 {
145 return std::make_unique<AudioDeviceGuard>(*this, stream);
146 }
147
156 std::string outgoingCall(const std::string& accountId,
157 const std::string& callee,
158 const std::vector<libjami::MediaMap>& mediaList = {});
159
165 bool answerCall(const std::string& accountId,
166 const std::string& callId,
167 const std::vector<libjami::MediaMap>& mediaList = {});
168 bool answerCall(Call& call, const std::vector<libjami::MediaMap>& mediaList = {});
169
175 void incomingCall(const std::string& accountId, Call& call);
176
183 bool hangupCall(const std::string& accountId, const std::string& callId);
184
190 bool hangupConference(const std::string& accountId, const std::string& confId);
191
198 bool onHoldCall(const std::string& accountId, const std::string& callId);
199
206 bool offHoldCall(const std::string& accountId, const std::string& callId);
207
214 bool transferCall(const std::string& accountId, const std::string& id, const std::string& to);
215
219 void transferSucceeded();
220
224 void transferFailed();
225
231 bool refuseCall(const std::string& accountId, const std::string& id);
232
237 bool holdConference(const std::string& accountId, const std::string& confId);
238
243 bool unHoldConference(const std::string& accountId, const std::string& confId);
244
250 bool addSubCall(const std::string& accountId,
251 const std::string& callId,
252 const std::string& account2Id,
253 const std::string& confId);
254 bool addSubCall(Call& call, Conference& conference);
255 void bindCallToConference(Call& call, Conference& conf);
256
261 bool addMainParticipant(const std::string& accountId, const std::string& confId);
262
268 bool joinParticipant(const std::string& accountId,
269 const std::string& callId1,
270 const std::string& account2Id,
271 const std::string& callId2,
272 bool attached = true);
273
278 void createConfFromParticipantList(const std::string& accountId,
279 const std::vector<std::string>&);
280
286 bool detachParticipant(const std::string& callId);
287
292 bool detachHost(const std::shared_ptr<Conference>& conf = {});
293
298 void removeParticipant(Call& call);
299
303 bool joinConference(const std::string& accountId,
304 const std::string& confId1,
305 const std::string& account2Id,
306 const std::string& confId2);
307
308 void addAudio(Call& call);
309
310 void removeAudio(Call& call);
311
315 void saveConfig();
316 void saveConfig(const std::shared_ptr<Account>& acc);
317
321 void playTone();
322
326 void playToneWithMessage();
327
331 void stopTone();
332
338 void peerAnsweredCall(Call& call);
339
345 void peerRingingCall(Call& call);
346
351 void peerHungupCall(Call& call);
352
359 void incomingMessage(const std::string& accountId,
360 const std::string& callId,
361 const std::string& from,
362 const std::map<std::string, std::string>& messages);
363
371 void sendCallTextMessage(const std::string& accountId,
372 const std::string& callID,
373 const std::map<std::string, std::string>& messages,
374 const std::string& from,
375 bool isMixed);
376
384 void sendRegister(const std::string& accountId, bool enable);
385
386 uint64_t sendTextMessage(const std::string& accountID,
387 const std::string& to,
388 const std::map<std::string, std::string>& payloads,
389 bool fromPlugin = false,
390 bool onlyConnected = false);
391
392 int getMessageStatus(uint64_t id) const;
393 int getMessageStatus(const std::string& accountID, uint64_t id) const;
394
399 std::vector<std::string> getAccountList() const;
400
404 void setAccountsOrder(const std::string& order);
405
411 std::map<std::string, std::string> getAccountDetails(const std::string& accountID) const;
412
418 std::map<std::string, std::string> getVolatileAccountDetails(const std::string& accountID) const;
419
424 std::vector<std::string> getCallList() const;
425
433 void setAccountDetails(const std::string& accountID,
434 const std::map<std::string, std::string>& details);
435
436 void setAccountActive(const std::string& accountID,
437 bool active,
438 bool shutdownConnections);
439 void loadAccountAndConversation(const std::string& accountId,
440 bool loadAll,
441 const std::string& convId);
442
443 std::mt19937_64 getSeededRandomEngine();
444
449 std::string getNewAccountId();
450
457 std::string addAccount(const std::map<std::string, std::string>& details,
458 const std::string& accountId = {});
459
466 void removeAccount(const std::string& accountID, bool flush = false);
467
468 void removeAccounts();
469
474 void setAudioPlugin(const std::string& audioPlugin);
475
481 void setAudioDevice(int index, AudioDeviceType streamType);
482
483 void startAudio();
484
489 std::vector<std::string> getAudioOutputDeviceList();
490
495 std::vector<std::string> getAudioInputDeviceList();
496
501 std::vector<std::string> getCurrentAudioDevicesIndex();
502
508 int getAudioInputDeviceIndex(const std::string& name);
509 int getAudioOutputDeviceIndex(const std::string& name);
510
515 std::string getCurrentAudioOutputPlugin() const;
516
521 std::string getNoiseSuppressState() const;
522
527 void setNoiseSuppressState(const std::string& state);
528
529 bool isAGCEnabled() const;
530 void setAGCState(bool enabled);
531
535 bool getIsAlwaysRecording() const;
536
541 void setIsAlwaysRecording(bool isAlwaysRec);
542
549 bool toggleRecordingCall(const std::string& accountId, const std::string& id);
550
555 bool startRecordedFilePlayback(const std::string&);
556
557 void recordingPlaybackSeek(const double value);
558
563
568 void setHistoryLimit(int days);
569
574 int getHistoryLimit() const;
575
581 void setRingingTimeout(int timeout);
582
588 int getRingingTimeout() const;
589
596 std::string getAudioManager() const;
597
602 bool setAudioManager(const std::string& api);
603
608 AudioFormat hardwareAudioFormatChanged(AudioFormat format);
609
616 AudioFormat audioFormatUsed(AudioFormat format);
617
622 void ringback();
623
627 void playRingtone(const std::string& accountID);
628
632 void congestion();
633
638 void playDtmf(char code);
639
643 void callBusy(Call& call);
644
648 void callFailure(Call& call);
649
654 std::shared_ptr<AudioLoop> getTelephoneTone();
655
660 std::shared_ptr<AudioLoop> getTelephoneFile();
661
666 bool incomingCallsWaiting();
667
672 std::shared_ptr<Call> getCurrentCall() const;
673
678 const std::string& getCurrentCallId() const;
679
685 bool isCurrentCall(const Call& call) const;
686
691 std::vector<std::string_view> loadAccountOrder() const;
692
696 int loadAccountMap(const YAML::Node& node);
697
702 std::shared_ptr<Call> getCallFromCallID(const std::string& callID) const;
703
707 RingBufferPool& getRingBufferPool();
708
713 bool hasCurrentCall() const;
714
720 template<class T = Account>
721 inline std::shared_ptr<T> getAccount(std::string_view accountId) const
722 {
723 return accountFactory.getAccount<T>(accountId);
724 }
725
730 template<class T = Account>
731 std::vector<std::shared_ptr<T>> getAllAccounts() const
732 {
733 const auto& account_order = loadAccountOrder();
734 const auto& all_accounts = accountFactory.getAllAccounts<T>();
735 std::vector<std::shared_ptr<T>> accountList;
736 accountList.reserve(all_accounts.size());
737 for (const auto& id : account_order) {
738 if (auto acc = accountFactory.getAccount<T>(id))
739 accountList.emplace_back(std::move(acc));
740 }
741 for (auto& account : all_accounts) {
742 if (std::find(accountList.begin(), accountList.end(), account) == accountList.end())
743 accountList.emplace_back(std::move(account));
744 }
745 return accountList;
746 }
747
748 template<class T = Account>
749 std::size_t accountCount() const
750 {
751 return accountFactory.accountCount<T>();
752 }
753
754 template<class T>
755 inline std::shared_ptr<T> findAccount(const std::function<bool(const std::shared_ptr<T>&)>& pred)
756 {
757 for (const auto& account : getAllAccounts<T>()) {
758 if (pred(account))
759 return account;
760 }
761 return {};
762 }
763
764 // only used by test framework
765 bool hasAccount(const std::string& accountID);
766
770 void registerAccounts();
771
775 void registerAccount(const std::string& accountID, const std::string& convId = {});
776
780 void unregisterAccounts();
781
790 std::shared_ptr<Call> newOutgoingCall(std::string_view toUrl,
791 const std::string& accountId,
792 const std::vector<libjami::MediaMap>& mediaList);
793
795
796 const std::shared_ptr<dhtnet::IceTransportFactory>& getIceTransportFactory();
797
798 std::shared_ptr<asio::io_context> ioContext() const;
799 std::shared_ptr<dhtnet::upnp::UPnPContext> upnpContext() const;
800
801 ScheduledExecutor& scheduler();
802 std::shared_ptr<Task> scheduleTask(std::function<void()>&& task,
803 std::chrono::steady_clock::time_point when,
804 const char* filename = CURRENT_FILENAME(),
805 uint32_t linum = CURRENT_LINE());
806
807 std::shared_ptr<Task> scheduleTaskIn(std::function<void()>&& task,
808 std::chrono::steady_clock::duration timeout,
809 const char* filename = CURRENT_FILENAME(),
810 uint32_t linum = CURRENT_LINE());
811
812 std::map<std::string, std::string> getNearbyPeers(const std::string& accountID);
813
814#ifdef ENABLE_VIDEO
823 std::shared_ptr<video::SinkClient> createSinkClient(const std::string& id = "",
824 bool mixer = false);
825
834 void createSinkClients(
835 const std::string& callId,
836 const ConfInfo& infos,
837 const std::vector<std::shared_ptr<video::VideoFrameActiveWriter>>& videoStreams,
838 std::map<std::string, std::shared_ptr<video::SinkClient>>& sinksMap,
839 const std::string& accountId = "");
840
847 std::shared_ptr<video::SinkClient> getSinkClient(const std::string& id);
848
849#endif // ENABLE_VIDEO
850 VideoManager* getVideoManager() const;
851
852 unsigned dhtLogLevel {0}; // default = disable
854
855 std::vector<libjami::Message> getLastMessages(const std::string& accountID,
856 const uint64_t& base_timestamp);
857
858 SIPVoIPLink& sipVoIPLink() const;
859#ifdef ENABLE_PLUGIN
860 JamiPluginManager& getJamiPluginManager() const;
861#endif
869 std::shared_ptr<dhtnet::ChannelSocket> gitSocket(std::string_view accountId,
870 std::string_view deviceId,
871 std::string_view conversationId);
872
873 void setDefaultModerator(const std::string& accountID, const std::string& peerURI, bool state);
874 std::vector<std::string> getDefaultModerators(const std::string& accountID);
875 void enableLocalModerators(const std::string& accountID, bool state);
876 bool isLocalModeratorsEnabled(const std::string& accountID);
877 void setAllModerators(const std::string& accountID, bool allModerators);
878 bool isAllModerators(const std::string& accountID);
879
880 void insertGitTransport(git_smart_subtransport* tr, std::unique_ptr<P2PSubTransport>&& sub);
881 void eraseGitTransport(git_smart_subtransport* tr);
882
883 dhtnet::tls::CertificateStore& certStore(const std::string& accountId) const;
884
885private:
886 Manager();
887 ~Manager();
888 friend class AudioDeviceGuard;
889
890 // Data members
891 struct ManagerPimpl;
892 std::unique_ptr<ManagerPimpl> pimpl_;
893};
894
896{
897public:
900
901private:
902 Manager& manager_;
903 const AudioDeviceType type_;
904};
905
906// Helper to install a callback to be called once by the main event loop
907template<typename Callback>
908static void
910{
911 Manager::instance().scheduler().run([cb = std::forward<Callback>(cb)]() mutable { cb(); });
912}
913
914} // namespace jami
Main sound class.
This class provides an interface to functions exposed to the Plugin System interface for lrc and clie...
Manager (controller) of daemon.
Definition manager.h:67
std::vector< std::shared_ptr< T > > getAllAccounts() const
Get a list of account pointers of type T (baseclass Account)
Definition manager.h:731
static LIBJAMI_TEST_EXPORT Manager & instance()
Definition manager.cpp:676
ScheduledExecutor & scheduler()
Definition manager.cpp:1706
std::shared_ptr< T > getAccount(std::string_view accountId) const
Get an account pointer, looks for account of type T.
Definition manager.h:721
CallFactory callFactory
Definition manager.h:794
static std::atomic_bool initialized
Definition manager.h:108
AccountFactory accountFactory
Definition manager.h:853
Preferences preferences
General preferences configuration.
Definition manager.h:80
static bool autoLoad
Definition manager.h:116
static bool syncOnRegister
Definition manager.h:114
VoipPreference voipPreferences
Voip related preferences.
Definition manager.h:85
std::shared_ptr< T > findAccount(const std::function< bool(const std::shared_ptr< T > &)> &pred)
Definition manager.h:755
AudioPreference audioPreference
Audio preferences.
Definition manager.h:90
void registerAccount(const std::string &accountID, const std::string &convId={})
Send registration for one account.
std::size_t accountCount() const
Definition manager.h:749
void run(std::function< void()> &&job, const char *filename=CURRENT_FILENAME(), uint32_t linum=CURRENT_LINE())
Schedule job to be run ASAP.
#define LIBJAMI_TEST_EXPORT
Definition def.h:49
Definition manager.h:42
void emitSignal(Args... args)
Definition ring_signal.h:64
AudioDeviceType
Definition audiolayer.h:58
static void runOnMainThread(Callback &&cb)
Definition manager.h:909
void setRingingTimeout(int32_t timeout)
void setAudioPlugin(const std::string &audioPlugin)
void setAccountDetails(const std::string &accountId, const std::map< std::string, std::string > &details)
void setHistoryLimit(int32_t days)
void setAccountsOrder(const std::string &order)
bool startRecordedFilePlayback(const std::string &filepath)
bool holdConference(const std::string &accountId, const std::string &confId)
bool setAudioManager(const std::string &api)
void loadAccountAndConversation(const std::string &accountId, bool loadAll, const std::string &convId)
int32_t getAudioOutputDeviceIndex(const std::string &name)
void setIsAlwaysRecording(bool rec)
int32_t getRingingTimeout()
void stopRecordedFilePlayback()
std::vector< std::string > getAccountList()
int32_t getAudioInputDeviceIndex(const std::string &name)
void setAccountActive(const std::string &accountId, bool enable, bool shutdownConnections)
std::string addAccount(const std::map< std::string, std::string > &details, const std::string &accountId)
std::string getCurrentAudioOutputPlugin()
bool getIsAlwaysRecording()
void sendRegister(const std::string &accountId, bool enable)
std::string getNoiseSuppressState()
int32_t getHistoryLimit()
bool addMainParticipant(const std::string &accountId, const std::string &confId)
void setNoiseSuppressState(const std::string &state)
std::vector< std::string > getAudioInputDeviceList()
std::vector< std::string > getCurrentAudioDevicesIndex()
void createConfFromParticipantList(const std::string &accountId, const std::vector< std::string > &participants)
bool detachParticipant(const std::string &, const std::string &callId)
void removeAccount(const std::string &accountId)
bool joinConference(const std::string &accountId, const std::string &sel_confId, const std::string &account2Id, const std::string &drag_confId)
std::map< std::string, std::string > getAccountDetails(const std::string &accountId)
int getMessageStatus(uint64_t messageId)
std::vector< std::string > getAudioOutputDeviceList()
std::map< std::string, std::string > getVolatileAccountDetails(const std::string &accountId)
std::vector< std::string > getCallList()
void sendTextMessage(const std::string &accountId, const std::string &callId, const std::map< std::string, std::string > &messages, const std::string &from, bool isMixed)
bool joinParticipant(const std::string &accountId, const std::string &sel_callId, const std::string &account2Id, const std::string &drag_callId)
InitFlag
Definition jami.h:35
std::string getAudioManager()
#define CURRENT_LINE()
Definition trace-tools.h:28
#define CURRENT_FILENAME()
Definition trace-tools.h:27